Mercurial > cpython-lru_cache-weakref
annotate Objects/classobject.c @ 4439:fb1237bf7022 legacy-trunk
Changed version to plain "1.4".
| author | Guido van Rossum <guido@python.org> |
|---|---|
| date | Tue, 22 Oct 1996 03:09:46 +0000 |
| parents | 75abe737e70b |
| children | 65d2d8e67da3 |
| rev | line source |
|---|---|
| 145 | 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 | 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 /* Class object implementation */ |
| 26 | |
| 86 | 27 #include "allobjects.h" |
|
944
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
28 #include "structmember.h" |
| 6 | 29 |
|
1861
d405e3e8ffd3
Yet another version (by me) of __getattr__ etc.
Guido van Rossum <guido@python.org>
parents:
1860
diff
changeset
|
30 /* Forward */ |
|
3987
6fbc5c665812
Optimizations by Sjoerd:
Guido van Rossum <guido@python.org>
parents:
3769
diff
changeset
|
31 static object *class_lookup PROTO((classobject *, object *, classobject **)); |
|
6fbc5c665812
Optimizations by Sjoerd:
Guido van Rossum <guido@python.org>
parents:
3769
diff
changeset
|
32 static object *instance_getattr1 PROTO((instanceobject *, object *)); |
|
1861
d405e3e8ffd3
Yet another version (by me) of __getattr__ etc.
Guido van Rossum <guido@python.org>
parents:
1860
diff
changeset
|
33 |
| 6 | 34 object * |
|
1289
4d292c7857d4
Access checks now work, at least for instance data (not for methods
Guido van Rossum <guido@python.org>
parents:
1288
diff
changeset
|
35 newclassobject(bases, dict, name) |
| 6 | 36 object *bases; /* NULL or tuple of classobjects! */ |
|
1289
4d292c7857d4
Access checks now work, at least for instance data (not for methods
Guido van Rossum <guido@python.org>
parents:
1288
diff
changeset
|
37 object *dict; |
|
386
0598f4f25d04
Add several secret __*__ attributes
Guido van Rossum <guido@python.org>
parents:
224
diff
changeset
|
38 object *name; /* String; NULL if unknown */ |
| 6 | 39 { |
|
4003
138fa15a1032
Disable support for access statement
Guido van Rossum <guido@python.org>
parents:
3987
diff
changeset
|
40 #ifdef SUPPORT_OBSOLETE_ACCESS |
|
1289
4d292c7857d4
Access checks now work, at least for instance data (not for methods
Guido van Rossum <guido@python.org>
parents:
1288
diff
changeset
|
41 int pos; |
|
4d292c7857d4
Access checks now work, at least for instance data (not for methods
Guido van Rossum <guido@python.org>
parents:
1288
diff
changeset
|
42 object *key, *value; |
|
4003
138fa15a1032
Disable support for access statement
Guido van Rossum <guido@python.org>
parents:
3987
diff
changeset
|
43 #endif |
|
1861
d405e3e8ffd3
Yet another version (by me) of __getattr__ etc.
Guido van Rossum <guido@python.org>
parents:
1860
diff
changeset
|
44 classobject *op, *dummy; |
|
3987
6fbc5c665812
Optimizations by Sjoerd:
Guido van Rossum <guido@python.org>
parents:
3769
diff
changeset
|
45 static object *getattrstr, *setattrstr, *delattrstr; |
|
4086
831b420e411e
More efficient handling of "__doc__" lookup.
Guido van Rossum <guido@python.org>
parents:
4065
diff
changeset
|
46 static object *docstr; |
|
831b420e411e
More efficient handling of "__doc__" lookup.
Guido van Rossum <guido@python.org>
parents:
4065
diff
changeset
|
47 if (docstr == NULL) { |
|
831b420e411e
More efficient handling of "__doc__" lookup.
Guido van Rossum <guido@python.org>
parents:
4065
diff
changeset
|
48 docstr= newstringobject("__doc__"); |
|
831b420e411e
More efficient handling of "__doc__" lookup.
Guido van Rossum <guido@python.org>
parents:
4065
diff
changeset
|
49 if (docstr == NULL) |
|
831b420e411e
More efficient handling of "__doc__" lookup.
Guido van Rossum <guido@python.org>
parents:
4065
diff
changeset
|
50 return NULL; |
|
831b420e411e
More efficient handling of "__doc__" lookup.
Guido van Rossum <guido@python.org>
parents:
4065
diff
changeset
|
51 } |
|
831b420e411e
More efficient handling of "__doc__" lookup.
Guido van Rossum <guido@python.org>
parents:
4065
diff
changeset
|
52 if (mappinglookup(dict, docstr) == NULL) { |
|
831b420e411e
More efficient handling of "__doc__" lookup.
Guido van Rossum <guido@python.org>
parents:
4065
diff
changeset
|
53 if (mappinginsert(dict, docstr, None) < 0) |
|
2084
abbf2a4b488b
* Objects/classobject.c: added 5th (function) parameter to
Guido van Rossum <guido@python.org>
parents:
2059
diff
changeset
|
54 return NULL; |
|
abbf2a4b488b
* Objects/classobject.c: added 5th (function) parameter to
Guido van Rossum <guido@python.org>
parents:
2059
diff
changeset
|
55 } |
|
465
ca0391a812f8
The cl_bases member of a class object is now never NULL.
Guido van Rossum <guido@python.org>
parents:
441
diff
changeset
|
56 if (bases == NULL) { |
|
ca0391a812f8
The cl_bases member of a class object is now never NULL.
Guido van Rossum <guido@python.org>
parents:
441
diff
changeset
|
57 bases = newtupleobject(0); |
|
ca0391a812f8
The cl_bases member of a class object is now never NULL.
Guido van Rossum <guido@python.org>
parents:
441
diff
changeset
|
58 if (bases == NULL) |
|
1252
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1215
diff
changeset
|
59 return NULL; |
|
465
ca0391a812f8
The cl_bases member of a class object is now never NULL.
Guido van Rossum <guido@python.org>
parents:
441
diff
changeset
|
60 } |
|
ca0391a812f8
The cl_bases member of a class object is now never NULL.
Guido van Rossum <guido@python.org>
parents:
441
diff
changeset
|
61 else |
|
ca0391a812f8
The cl_bases member of a class object is now never NULL.
Guido van Rossum <guido@python.org>
parents:
441
diff
changeset
|
62 INCREF(bases); |
| 6 | 63 op = NEWOBJ(classobject, &Classtype); |
|
465
ca0391a812f8
The cl_bases member of a class object is now never NULL.
Guido van Rossum <guido@python.org>
parents:
441
diff
changeset
|
64 if (op == NULL) { |
|
ca0391a812f8
The cl_bases member of a class object is now never NULL.
Guido van Rossum <guido@python.org>
parents:
441
diff
changeset
|
65 DECREF(bases); |
| 6 | 66 return NULL; |
|
465
ca0391a812f8
The cl_bases member of a class object is now never NULL.
Guido van Rossum <guido@python.org>
parents:
441
diff
changeset
|
67 } |
| 6 | 68 op->cl_bases = bases; |
|
1289
4d292c7857d4
Access checks now work, at least for instance data (not for methods
Guido van Rossum <guido@python.org>
parents:
1288
diff
changeset
|
69 INCREF(dict); |
|
4d292c7857d4
Access checks now work, at least for instance data (not for methods
Guido van Rossum <guido@python.org>
parents:
1288
diff
changeset
|
70 op->cl_dict = dict; |
|
386
0598f4f25d04
Add several secret __*__ attributes
Guido van Rossum <guido@python.org>
parents:
224
diff
changeset
|
71 XINCREF(name); |
|
0598f4f25d04
Add several secret __*__ attributes
Guido van Rossum <guido@python.org>
parents:
224
diff
changeset
|
72 op->cl_name = name; |
|
3987
6fbc5c665812
Optimizations by Sjoerd:
Guido van Rossum <guido@python.org>
parents:
3769
diff
changeset
|
73 if (getattrstr == NULL) { |
|
6fbc5c665812
Optimizations by Sjoerd:
Guido van Rossum <guido@python.org>
parents:
3769
diff
changeset
|
74 getattrstr = newstringobject("__getattr__"); |
|
6fbc5c665812
Optimizations by Sjoerd:
Guido van Rossum <guido@python.org>
parents:
3769
diff
changeset
|
75 setattrstr = newstringobject("__setattr__"); |
|
6fbc5c665812
Optimizations by Sjoerd:
Guido van Rossum <guido@python.org>
parents:
3769
diff
changeset
|
76 delattrstr = newstringobject("__delattr__"); |
|
6fbc5c665812
Optimizations by Sjoerd:
Guido van Rossum <guido@python.org>
parents:
3769
diff
changeset
|
77 } |
|
6fbc5c665812
Optimizations by Sjoerd:
Guido van Rossum <guido@python.org>
parents:
3769
diff
changeset
|
78 op->cl_getattr = class_lookup(op, getattrstr, &dummy); |
|
6fbc5c665812
Optimizations by Sjoerd:
Guido van Rossum <guido@python.org>
parents:
3769
diff
changeset
|
79 op->cl_setattr = class_lookup(op, setattrstr, &dummy); |
|
6fbc5c665812
Optimizations by Sjoerd:
Guido van Rossum <guido@python.org>
parents:
3769
diff
changeset
|
80 op->cl_delattr = class_lookup(op, delattrstr, &dummy); |
|
1861
d405e3e8ffd3
Yet another version (by me) of __getattr__ etc.
Guido van Rossum <guido@python.org>
parents:
1860
diff
changeset
|
81 XINCREF(op->cl_getattr); |
|
d405e3e8ffd3
Yet another version (by me) of __getattr__ etc.
Guido van Rossum <guido@python.org>
parents:
1860
diff
changeset
|
82 XINCREF(op->cl_setattr); |
|
d405e3e8ffd3
Yet another version (by me) of __getattr__ etc.
Guido van Rossum <guido@python.org>
parents:
1860
diff
changeset
|
83 XINCREF(op->cl_delattr); |
|
4003
138fa15a1032
Disable support for access statement
Guido van Rossum <guido@python.org>
parents:
3987
diff
changeset
|
84 #ifdef SUPPORT_OBSOLETE_ACCESS |
|
1289
4d292c7857d4
Access checks now work, at least for instance data (not for methods
Guido van Rossum <guido@python.org>
parents:
1288
diff
changeset
|
85 pos = 0; |
|
4d292c7857d4
Access checks now work, at least for instance data (not for methods
Guido van Rossum <guido@python.org>
parents:
1288
diff
changeset
|
86 while (mappinggetnext(dict, &pos, &key, &value)) { |
|
4d292c7857d4
Access checks now work, at least for instance data (not for methods
Guido van Rossum <guido@python.org>
parents:
1288
diff
changeset
|
87 if (is_accessobject(value)) |
|
4d292c7857d4
Access checks now work, at least for instance data (not for methods
Guido van Rossum <guido@python.org>
parents:
1288
diff
changeset
|
88 setaccessowner(value, (object *)op); |
|
4d292c7857d4
Access checks now work, at least for instance data (not for methods
Guido van Rossum <guido@python.org>
parents:
1288
diff
changeset
|
89 } |
|
4003
138fa15a1032
Disable support for access statement
Guido van Rossum <guido@python.org>
parents:
3987
diff
changeset
|
90 #endif |
| 6 | 91 return (object *) op; |
| 92 } | |
| 93 | |
| 94 /* Class methods */ | |
| 95 | |
| 96 static void | |
| 97 class_dealloc(op) | |
| 98 classobject *op; | |
| 99 { | |
|
465
ca0391a812f8
The cl_bases member of a class object is now never NULL.
Guido van Rossum <guido@python.org>
parents:
441
diff
changeset
|
100 DECREF(op->cl_bases); |
|
1289
4d292c7857d4
Access checks now work, at least for instance data (not for methods
Guido van Rossum <guido@python.org>
parents:
1288
diff
changeset
|
101 DECREF(op->cl_dict); |
|
386
0598f4f25d04
Add several secret __*__ attributes
Guido van Rossum <guido@python.org>
parents:
224
diff
changeset
|
102 XDECREF(op->cl_name); |
| 6 | 103 free((ANY *)op); |
| 104 } | |
| 105 | |
| 106 static object * | |
|
1289
4d292c7857d4
Access checks now work, at least for instance data (not for methods
Guido van Rossum <guido@python.org>
parents:
1288
diff
changeset
|
107 class_lookup(cp, name, pclass) |
|
4d292c7857d4
Access checks now work, at least for instance data (not for methods
Guido van Rossum <guido@python.org>
parents:
1288
diff
changeset
|
108 classobject *cp; |
|
3987
6fbc5c665812
Optimizations by Sjoerd:
Guido van Rossum <guido@python.org>
parents:
3769
diff
changeset
|
109 object *name; |
|
1289
4d292c7857d4
Access checks now work, at least for instance data (not for methods
Guido van Rossum <guido@python.org>
parents:
1288
diff
changeset
|
110 classobject **pclass; |
|
4d292c7857d4
Access checks now work, at least for instance data (not for methods
Guido van Rossum <guido@python.org>
parents:
1288
diff
changeset
|
111 { |
|
4d292c7857d4
Access checks now work, at least for instance data (not for methods
Guido van Rossum <guido@python.org>
parents:
1288
diff
changeset
|
112 int i, n; |
|
3987
6fbc5c665812
Optimizations by Sjoerd:
Guido van Rossum <guido@python.org>
parents:
3769
diff
changeset
|
113 object *value = mappinglookup(cp->cl_dict, name); |
|
1289
4d292c7857d4
Access checks now work, at least for instance data (not for methods
Guido van Rossum <guido@python.org>
parents:
1288
diff
changeset
|
114 if (value != NULL) { |
|
4d292c7857d4
Access checks now work, at least for instance data (not for methods
Guido van Rossum <guido@python.org>
parents:
1288
diff
changeset
|
115 *pclass = cp; |
|
4d292c7857d4
Access checks now work, at least for instance data (not for methods
Guido van Rossum <guido@python.org>
parents:
1288
diff
changeset
|
116 return value; |
|
4d292c7857d4
Access checks now work, at least for instance data (not for methods
Guido van Rossum <guido@python.org>
parents:
1288
diff
changeset
|
117 } |
|
4d292c7857d4
Access checks now work, at least for instance data (not for methods
Guido van Rossum <guido@python.org>
parents:
1288
diff
changeset
|
118 n = gettuplesize(cp->cl_bases); |
|
4d292c7857d4
Access checks now work, at least for instance data (not for methods
Guido van Rossum <guido@python.org>
parents:
1288
diff
changeset
|
119 for (i = 0; i < n; i++) { |
|
4d292c7857d4
Access checks now work, at least for instance data (not for methods
Guido van Rossum <guido@python.org>
parents:
1288
diff
changeset
|
120 object *v = class_lookup((classobject *) |
|
4d292c7857d4
Access checks now work, at least for instance data (not for methods
Guido van Rossum <guido@python.org>
parents:
1288
diff
changeset
|
121 gettupleitem(cp->cl_bases, i), name, pclass); |
|
4d292c7857d4
Access checks now work, at least for instance data (not for methods
Guido van Rossum <guido@python.org>
parents:
1288
diff
changeset
|
122 if (v != NULL) |
|
4d292c7857d4
Access checks now work, at least for instance data (not for methods
Guido van Rossum <guido@python.org>
parents:
1288
diff
changeset
|
123 return v; |
|
4d292c7857d4
Access checks now work, at least for instance data (not for methods
Guido van Rossum <guido@python.org>
parents:
1288
diff
changeset
|
124 } |
|
4d292c7857d4
Access checks now work, at least for instance data (not for methods
Guido van Rossum <guido@python.org>
parents:
1288
diff
changeset
|
125 return NULL; |
|
4d292c7857d4
Access checks now work, at least for instance data (not for methods
Guido van Rossum <guido@python.org>
parents:
1288
diff
changeset
|
126 } |
|
4d292c7857d4
Access checks now work, at least for instance data (not for methods
Guido van Rossum <guido@python.org>
parents:
1288
diff
changeset
|
127 |
|
4d292c7857d4
Access checks now work, at least for instance data (not for methods
Guido van Rossum <guido@python.org>
parents:
1288
diff
changeset
|
128 static object * |
| 6 | 129 class_getattr(op, name) |
| 130 register classobject *op; | |
|
3987
6fbc5c665812
Optimizations by Sjoerd:
Guido van Rossum <guido@python.org>
parents:
3769
diff
changeset
|
131 object *name; |
| 6 | 132 { |
| 133 register object *v; | |
|
3987
6fbc5c665812
Optimizations by Sjoerd:
Guido van Rossum <guido@python.org>
parents:
3769
diff
changeset
|
134 register char *sname = getstringvalue(name); |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1413
diff
changeset
|
135 classobject *class; |
|
3987
6fbc5c665812
Optimizations by Sjoerd:
Guido van Rossum <guido@python.org>
parents:
3769
diff
changeset
|
136 if (sname[0] == '_' && sname[1] == '_') { |
|
6fbc5c665812
Optimizations by Sjoerd:
Guido van Rossum <guido@python.org>
parents:
3769
diff
changeset
|
137 if (strcmp(sname, "__dict__") == 0) { |
|
2105
ea7b2bf0b521
add restrictions in restricted mode
Guido van Rossum <guido@python.org>
parents:
2084
diff
changeset
|
138 if (getrestricted()) { |
|
ea7b2bf0b521
add restrictions in restricted mode
Guido van Rossum <guido@python.org>
parents:
2084
diff
changeset
|
139 err_setstr(RuntimeError, |
|
ea7b2bf0b521
add restrictions in restricted mode
Guido van Rossum <guido@python.org>
parents:
2084
diff
changeset
|
140 "class.__dict__ not accessible in restricted mode"); |
|
ea7b2bf0b521
add restrictions in restricted mode
Guido van Rossum <guido@python.org>
parents:
2084
diff
changeset
|
141 return NULL; |
|
ea7b2bf0b521
add restrictions in restricted mode
Guido van Rossum <guido@python.org>
parents:
2084
diff
changeset
|
142 } |
|
ea7b2bf0b521
add restrictions in restricted mode
Guido van Rossum <guido@python.org>
parents:
2084
diff
changeset
|
143 INCREF(op->cl_dict); |
|
ea7b2bf0b521
add restrictions in restricted mode
Guido van Rossum <guido@python.org>
parents:
2084
diff
changeset
|
144 return op->cl_dict; |
|
ea7b2bf0b521
add restrictions in restricted mode
Guido van Rossum <guido@python.org>
parents:
2084
diff
changeset
|
145 } |
|
3987
6fbc5c665812
Optimizations by Sjoerd:
Guido van Rossum <guido@python.org>
parents:
3769
diff
changeset
|
146 if (strcmp(sname, "__bases__") == 0) { |
|
2105
ea7b2bf0b521
add restrictions in restricted mode
Guido van Rossum <guido@python.org>
parents:
2084
diff
changeset
|
147 INCREF(op->cl_bases); |
|
ea7b2bf0b521
add restrictions in restricted mode
Guido van Rossum <guido@python.org>
parents:
2084
diff
changeset
|
148 return op->cl_bases; |
|
ea7b2bf0b521
add restrictions in restricted mode
Guido van Rossum <guido@python.org>
parents:
2084
diff
changeset
|
149 } |
|
3987
6fbc5c665812
Optimizations by Sjoerd:
Guido van Rossum <guido@python.org>
parents:
3769
diff
changeset
|
150 if (strcmp(sname, "__name__") == 0) { |
|
2105
ea7b2bf0b521
add restrictions in restricted mode
Guido van Rossum <guido@python.org>
parents:
2084
diff
changeset
|
151 if (op->cl_name == NULL) |
|
ea7b2bf0b521
add restrictions in restricted mode
Guido van Rossum <guido@python.org>
parents:
2084
diff
changeset
|
152 v = None; |
|
ea7b2bf0b521
add restrictions in restricted mode
Guido van Rossum <guido@python.org>
parents:
2084
diff
changeset
|
153 else |
|
ea7b2bf0b521
add restrictions in restricted mode
Guido van Rossum <guido@python.org>
parents:
2084
diff
changeset
|
154 v = op->cl_name; |
|
ea7b2bf0b521
add restrictions in restricted mode
Guido van Rossum <guido@python.org>
parents:
2084
diff
changeset
|
155 INCREF(v); |
|
ea7b2bf0b521
add restrictions in restricted mode
Guido van Rossum <guido@python.org>
parents:
2084
diff
changeset
|
156 return v; |
|
ea7b2bf0b521
add restrictions in restricted mode
Guido van Rossum <guido@python.org>
parents:
2084
diff
changeset
|
157 } |
|
386
0598f4f25d04
Add several secret __*__ attributes
Guido van Rossum <guido@python.org>
parents:
224
diff
changeset
|
158 } |
|
1289
4d292c7857d4
Access checks now work, at least for instance data (not for methods
Guido van Rossum <guido@python.org>
parents:
1288
diff
changeset
|
159 v = class_lookup(op, name, &class); |
|
1290
7971fecbf28e
* Lots of small changes related to access.
Guido van Rossum <guido@python.org>
parents:
1289
diff
changeset
|
160 if (v == NULL) { |
|
3987
6fbc5c665812
Optimizations by Sjoerd:
Guido van Rossum <guido@python.org>
parents:
3769
diff
changeset
|
161 err_setval(AttributeError, name); |
|
1290
7971fecbf28e
* Lots of small changes related to access.
Guido van Rossum <guido@python.org>
parents:
1289
diff
changeset
|
162 return NULL; |
|
7971fecbf28e
* Lots of small changes related to access.
Guido van Rossum <guido@python.org>
parents:
1289
diff
changeset
|
163 } |
|
4003
138fa15a1032
Disable support for access statement
Guido van Rossum <guido@python.org>
parents:
3987
diff
changeset
|
164 #ifdef SUPPORT_OBSOLETE_ACCESS |
|
1290
7971fecbf28e
* Lots of small changes related to access.
Guido van Rossum <guido@python.org>
parents:
1289
diff
changeset
|
165 if (is_accessobject(v)) { |
|
1293
ac3c80abbf43
* classobject.c: in instance_getattr, don't make a method out of a
Guido van Rossum <guido@python.org>
parents:
1290
diff
changeset
|
166 v = getaccessvalue(v, getowner()); |
|
1290
7971fecbf28e
* Lots of small changes related to access.
Guido van Rossum <guido@python.org>
parents:
1289
diff
changeset
|
167 if (v == NULL) |
|
7971fecbf28e
* Lots of small changes related to access.
Guido van Rossum <guido@python.org>
parents:
1289
diff
changeset
|
168 return NULL; |
|
7971fecbf28e
* Lots of small changes related to access.
Guido van Rossum <guido@python.org>
parents:
1289
diff
changeset
|
169 } |
|
7971fecbf28e
* Lots of small changes related to access.
Guido van Rossum <guido@python.org>
parents:
1289
diff
changeset
|
170 else |
|
4003
138fa15a1032
Disable support for access statement
Guido van Rossum <guido@python.org>
parents:
3987
diff
changeset
|
171 #endif |
|
1290
7971fecbf28e
* Lots of small changes related to access.
Guido van Rossum <guido@python.org>
parents:
1289
diff
changeset
|
172 INCREF(v); |
|
7971fecbf28e
* Lots of small changes related to access.
Guido van Rossum <guido@python.org>
parents:
1289
diff
changeset
|
173 if (is_funcobject(v)) { |
|
7971fecbf28e
* Lots of small changes related to access.
Guido van Rossum <guido@python.org>
parents:
1289
diff
changeset
|
174 object *w = newinstancemethodobject(v, (object *)NULL, |
|
1289
4d292c7857d4
Access checks now work, at least for instance data (not for methods
Guido van Rossum <guido@python.org>
parents:
1288
diff
changeset
|
175 (object *)class); |
|
1290
7971fecbf28e
* Lots of small changes related to access.
Guido van Rossum <guido@python.org>
parents:
1289
diff
changeset
|
176 DECREF(v); |
|
7971fecbf28e
* Lots of small changes related to access.
Guido van Rossum <guido@python.org>
parents:
1289
diff
changeset
|
177 v = w; |
| 6 | 178 } |
|
1290
7971fecbf28e
* Lots of small changes related to access.
Guido van Rossum <guido@python.org>
parents:
1289
diff
changeset
|
179 return v; |
| 6 | 180 } |
| 181 | |
|
386
0598f4f25d04
Add several secret __*__ attributes
Guido van Rossum <guido@python.org>
parents:
224
diff
changeset
|
182 static int |
|
0598f4f25d04
Add several secret __*__ attributes
Guido van Rossum <guido@python.org>
parents:
224
diff
changeset
|
183 class_setattr(op, name, v) |
|
0598f4f25d04
Add several secret __*__ attributes
Guido van Rossum <guido@python.org>
parents:
224
diff
changeset
|
184 classobject *op; |
|
3987
6fbc5c665812
Optimizations by Sjoerd:
Guido van Rossum <guido@python.org>
parents:
3769
diff
changeset
|
185 object *name; |
|
386
0598f4f25d04
Add several secret __*__ attributes
Guido van Rossum <guido@python.org>
parents:
224
diff
changeset
|
186 object *v; |
|
0598f4f25d04
Add several secret __*__ attributes
Guido van Rossum <guido@python.org>
parents:
224
diff
changeset
|
187 { |
|
4003
138fa15a1032
Disable support for access statement
Guido van Rossum <guido@python.org>
parents:
3987
diff
changeset
|
188 #ifdef SUPPORT_OBSOLETE_ACCESS |
|
1288
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1272
diff
changeset
|
189 object *ac; |
|
4003
138fa15a1032
Disable support for access statement
Guido van Rossum <guido@python.org>
parents:
3987
diff
changeset
|
190 #endif |
|
3987
6fbc5c665812
Optimizations by Sjoerd:
Guido van Rossum <guido@python.org>
parents:
3769
diff
changeset
|
191 char *sname = getstringvalue(name); |
|
6fbc5c665812
Optimizations by Sjoerd:
Guido van Rossum <guido@python.org>
parents:
3769
diff
changeset
|
192 if (sname[0] == '_' && sname[1] == '_') { |
|
6fbc5c665812
Optimizations by Sjoerd:
Guido van Rossum <guido@python.org>
parents:
3769
diff
changeset
|
193 int n = getstringsize(name); |
|
6fbc5c665812
Optimizations by Sjoerd:
Guido van Rossum <guido@python.org>
parents:
3769
diff
changeset
|
194 if (sname[n-1] == '_' && sname[n-2] == '_') { |
|
791
77ee9b222fdb
Don't allow assignment to attributes named __*__
Guido van Rossum <guido@python.org>
parents:
735
diff
changeset
|
195 err_setstr(TypeError, "read-only special attribute"); |
|
77ee9b222fdb
Don't allow assignment to attributes named __*__
Guido van Rossum <guido@python.org>
parents:
735
diff
changeset
|
196 return -1; |
|
77ee9b222fdb
Don't allow assignment to attributes named __*__
Guido van Rossum <guido@python.org>
parents:
735
diff
changeset
|
197 } |
|
77ee9b222fdb
Don't allow assignment to attributes named __*__
Guido van Rossum <guido@python.org>
parents:
735
diff
changeset
|
198 } |
|
2889
e513f0d65368
class objects are read-only in restricted mode
Guido van Rossum <guido@python.org>
parents:
2864
diff
changeset
|
199 if (getrestricted()) { |
|
e513f0d65368
class objects are read-only in restricted mode
Guido van Rossum <guido@python.org>
parents:
2864
diff
changeset
|
200 err_setstr(RuntimeError, |
|
e513f0d65368
class objects are read-only in restricted mode
Guido van Rossum <guido@python.org>
parents:
2864
diff
changeset
|
201 "classes are read-only in restricted mode"); |
|
e513f0d65368
class objects are read-only in restricted mode
Guido van Rossum <guido@python.org>
parents:
2864
diff
changeset
|
202 return -1; |
|
e513f0d65368
class objects are read-only in restricted mode
Guido van Rossum <guido@python.org>
parents:
2864
diff
changeset
|
203 } |
|
4003
138fa15a1032
Disable support for access statement
Guido van Rossum <guido@python.org>
parents:
3987
diff
changeset
|
204 #ifdef SUPPORT_OBSOLETE_ACCESS |
|
3987
6fbc5c665812
Optimizations by Sjoerd:
Guido van Rossum <guido@python.org>
parents:
3769
diff
changeset
|
205 ac = mappinglookup(op->cl_dict, name); |
|
1288
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1272
diff
changeset
|
206 if (ac != NULL && is_accessobject(ac)) |
|
1293
ac3c80abbf43
* classobject.c: in instance_getattr, don't make a method out of a
Guido van Rossum <guido@python.org>
parents:
1290
diff
changeset
|
207 return setaccessvalue(ac, getowner(), v); |
|
4003
138fa15a1032
Disable support for access statement
Guido van Rossum <guido@python.org>
parents:
3987
diff
changeset
|
208 #endif |
|
1031
7a11a70f7f44
classobject.c moduleobject.c stdwinmodule.c xxobject.c:
Guido van Rossum <guido@python.org>
parents:
1021
diff
changeset
|
209 if (v == NULL) { |
|
3987
6fbc5c665812
Optimizations by Sjoerd:
Guido van Rossum <guido@python.org>
parents:
3769
diff
changeset
|
210 int rv = mappingremove(op->cl_dict, name); |
|
1031
7a11a70f7f44
classobject.c moduleobject.c stdwinmodule.c xxobject.c:
Guido van Rossum <guido@python.org>
parents:
1021
diff
changeset
|
211 if (rv < 0) |
|
7a11a70f7f44
classobject.c moduleobject.c stdwinmodule.c xxobject.c:
Guido van Rossum <guido@python.org>
parents:
1021
diff
changeset
|
212 err_setstr(AttributeError, |
|
7a11a70f7f44
classobject.c moduleobject.c stdwinmodule.c xxobject.c:
Guido van Rossum <guido@python.org>
parents:
1021
diff
changeset
|
213 "delete non-existing class attribute"); |
|
7a11a70f7f44
classobject.c moduleobject.c stdwinmodule.c xxobject.c:
Guido van Rossum <guido@python.org>
parents:
1021
diff
changeset
|
214 return rv; |
|
7a11a70f7f44
classobject.c moduleobject.c stdwinmodule.c xxobject.c:
Guido van Rossum <guido@python.org>
parents:
1021
diff
changeset
|
215 } |
|
386
0598f4f25d04
Add several secret __*__ attributes
Guido van Rossum <guido@python.org>
parents:
224
diff
changeset
|
216 else |
|
3987
6fbc5c665812
Optimizations by Sjoerd:
Guido van Rossum <guido@python.org>
parents:
3769
diff
changeset
|
217 return mappinginsert(op->cl_dict, name, v); |
|
386
0598f4f25d04
Add several secret __*__ attributes
Guido van Rossum <guido@python.org>
parents:
224
diff
changeset
|
218 } |
|
0598f4f25d04
Add several secret __*__ attributes
Guido van Rossum <guido@python.org>
parents:
224
diff
changeset
|
219 |
|
1288
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1272
diff
changeset
|
220 static object * |
|
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1272
diff
changeset
|
221 class_repr(op) |
|
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1272
diff
changeset
|
222 classobject *op; |
|
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1272
diff
changeset
|
223 { |
|
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1272
diff
changeset
|
224 char buf[140]; |
|
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1272
diff
changeset
|
225 char *name; |
|
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1272
diff
changeset
|
226 if (op->cl_name == NULL || !is_stringobject(op->cl_name)) |
|
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1272
diff
changeset
|
227 name = "?"; |
|
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1272
diff
changeset
|
228 else |
|
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1272
diff
changeset
|
229 name = getstringvalue(op->cl_name); |
|
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1272
diff
changeset
|
230 sprintf(buf, "<class %.100s at %lx>", name, (long)op); |
|
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1272
diff
changeset
|
231 return newstringobject(buf); |
|
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1272
diff
changeset
|
232 } |
|
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1272
diff
changeset
|
233 |
| 6 | 234 typeobject Classtype = { |
| 235 OB_HEAD_INIT(&Typetype) | |
| 236 0, | |
| 237 "class", | |
| 238 sizeof(classobject), | |
| 239 0, | |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1413
diff
changeset
|
240 (destructor)class_dealloc, /*tp_dealloc*/ |
| 6 | 241 0, /*tp_print*/ |
|
3987
6fbc5c665812
Optimizations by Sjoerd:
Guido van Rossum <guido@python.org>
parents:
3769
diff
changeset
|
242 0, /*tp_getattr*/ |
|
6fbc5c665812
Optimizations by Sjoerd:
Guido van Rossum <guido@python.org>
parents:
3769
diff
changeset
|
243 0, /*tp_setattr*/ |
| 6 | 244 0, /*tp_compare*/ |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1413
diff
changeset
|
245 (reprfunc)class_repr, /*tp_repr*/ |
| 6 | 246 0, /*tp_as_number*/ |
| 247 0, /*tp_as_sequence*/ | |
| 248 0, /*tp_as_mapping*/ | |
|
3987
6fbc5c665812
Optimizations by Sjoerd:
Guido van Rossum <guido@python.org>
parents:
3769
diff
changeset
|
249 0, /*tp_hash*/ |
|
6fbc5c665812
Optimizations by Sjoerd:
Guido van Rossum <guido@python.org>
parents:
3769
diff
changeset
|
250 0, /*tp_call*/ |
|
6fbc5c665812
Optimizations by Sjoerd:
Guido van Rossum <guido@python.org>
parents:
3769
diff
changeset
|
251 0, /*tp_str*/ |
|
6fbc5c665812
Optimizations by Sjoerd:
Guido van Rossum <guido@python.org>
parents:
3769
diff
changeset
|
252 (getattrofunc)class_getattr, /*tp_getattro*/ |
|
6fbc5c665812
Optimizations by Sjoerd:
Guido van Rossum <guido@python.org>
parents:
3769
diff
changeset
|
253 (setattrofunc)class_setattr, /*tp_setattro*/ |
| 6 | 254 }; |
| 255 | |
|
1289
4d292c7857d4
Access checks now work, at least for instance data (not for methods
Guido van Rossum <guido@python.org>
parents:
1288
diff
changeset
|
256 int |
|
4d292c7857d4
Access checks now work, at least for instance data (not for methods
Guido van Rossum <guido@python.org>
parents:
1288
diff
changeset
|
257 issubclass(class, base) |
|
4d292c7857d4
Access checks now work, at least for instance data (not for methods
Guido van Rossum <guido@python.org>
parents:
1288
diff
changeset
|
258 object *class; |
|
4d292c7857d4
Access checks now work, at least for instance data (not for methods
Guido van Rossum <guido@python.org>
parents:
1288
diff
changeset
|
259 object *base; |
|
4d292c7857d4
Access checks now work, at least for instance data (not for methods
Guido van Rossum <guido@python.org>
parents:
1288
diff
changeset
|
260 { |
|
4d292c7857d4
Access checks now work, at least for instance data (not for methods
Guido van Rossum <guido@python.org>
parents:
1288
diff
changeset
|
261 int i, n; |
|
4d292c7857d4
Access checks now work, at least for instance data (not for methods
Guido van Rossum <guido@python.org>
parents:
1288
diff
changeset
|
262 classobject *cp; |
|
1293
ac3c80abbf43
* classobject.c: in instance_getattr, don't make a method out of a
Guido van Rossum <guido@python.org>
parents:
1290
diff
changeset
|
263 if (class == base) |
|
ac3c80abbf43
* classobject.c: in instance_getattr, don't make a method out of a
Guido van Rossum <guido@python.org>
parents:
1290
diff
changeset
|
264 return 1; |
|
1289
4d292c7857d4
Access checks now work, at least for instance data (not for methods
Guido van Rossum <guido@python.org>
parents:
1288
diff
changeset
|
265 if (class == NULL || !is_classobject(class)) |
|
4d292c7857d4
Access checks now work, at least for instance data (not for methods
Guido van Rossum <guido@python.org>
parents:
1288
diff
changeset
|
266 return 0; |
|
4d292c7857d4
Access checks now work, at least for instance data (not for methods
Guido van Rossum <guido@python.org>
parents:
1288
diff
changeset
|
267 cp = (classobject *)class; |
|
4d292c7857d4
Access checks now work, at least for instance data (not for methods
Guido van Rossum <guido@python.org>
parents:
1288
diff
changeset
|
268 n = gettuplesize(cp->cl_bases); |
|
4d292c7857d4
Access checks now work, at least for instance data (not for methods
Guido van Rossum <guido@python.org>
parents:
1288
diff
changeset
|
269 for (i = 0; i < n; i++) { |
|
4d292c7857d4
Access checks now work, at least for instance data (not for methods
Guido van Rossum <guido@python.org>
parents:
1288
diff
changeset
|
270 if (issubclass(gettupleitem(cp->cl_bases, i), base)) |
|
4d292c7857d4
Access checks now work, at least for instance data (not for methods
Guido van Rossum <guido@python.org>
parents:
1288
diff
changeset
|
271 return 1; |
|
4d292c7857d4
Access checks now work, at least for instance data (not for methods
Guido van Rossum <guido@python.org>
parents:
1288
diff
changeset
|
272 } |
|
4d292c7857d4
Access checks now work, at least for instance data (not for methods
Guido van Rossum <guido@python.org>
parents:
1288
diff
changeset
|
273 return 0; |
|
4d292c7857d4
Access checks now work, at least for instance data (not for methods
Guido van Rossum <guido@python.org>
parents:
1288
diff
changeset
|
274 } |
| 6 | 275 |
|
1289
4d292c7857d4
Access checks now work, at least for instance data (not for methods
Guido van Rossum <guido@python.org>
parents:
1288
diff
changeset
|
276 |
|
4d292c7857d4
Access checks now work, at least for instance data (not for methods
Guido van Rossum <guido@python.org>
parents:
1288
diff
changeset
|
277 /* Instance objects */ |
| 6 | 278 |
|
4003
138fa15a1032
Disable support for access statement
Guido van Rossum <guido@python.org>
parents:
3987
diff
changeset
|
279 #ifdef SUPPORT_OBSOLETE_ACCESS |
|
1289
4d292c7857d4
Access checks now work, at least for instance data (not for methods
Guido van Rossum <guido@python.org>
parents:
1288
diff
changeset
|
280 static int |
|
4d292c7857d4
Access checks now work, at least for instance data (not for methods
Guido van Rossum <guido@python.org>
parents:
1288
diff
changeset
|
281 addaccess(class, inst) |
|
4d292c7857d4
Access checks now work, at least for instance data (not for methods
Guido van Rossum <guido@python.org>
parents:
1288
diff
changeset
|
282 classobject *class; |
|
4d292c7857d4
Access checks now work, at least for instance data (not for methods
Guido van Rossum <guido@python.org>
parents:
1288
diff
changeset
|
283 instanceobject *inst; |
|
4d292c7857d4
Access checks now work, at least for instance data (not for methods
Guido van Rossum <guido@python.org>
parents:
1288
diff
changeset
|
284 { |
|
4d292c7857d4
Access checks now work, at least for instance data (not for methods
Guido van Rossum <guido@python.org>
parents:
1288
diff
changeset
|
285 int i, n, pos, ret; |
|
4d292c7857d4
Access checks now work, at least for instance data (not for methods
Guido van Rossum <guido@python.org>
parents:
1288
diff
changeset
|
286 object *key, *value, *ac; |
|
4d292c7857d4
Access checks now work, at least for instance data (not for methods
Guido van Rossum <guido@python.org>
parents:
1288
diff
changeset
|
287 |
|
4d292c7857d4
Access checks now work, at least for instance data (not for methods
Guido van Rossum <guido@python.org>
parents:
1288
diff
changeset
|
288 n = gettuplesize(class->cl_bases); |
|
4d292c7857d4
Access checks now work, at least for instance data (not for methods
Guido van Rossum <guido@python.org>
parents:
1288
diff
changeset
|
289 for (i = 0; i < n; i++) { |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1413
diff
changeset
|
290 if (addaccess((classobject *)gettupleitem(class->cl_bases, i), inst) < 0) |
|
1289
4d292c7857d4
Access checks now work, at least for instance data (not for methods
Guido van Rossum <guido@python.org>
parents:
1288
diff
changeset
|
291 return -1; |
|
4d292c7857d4
Access checks now work, at least for instance data (not for methods
Guido van Rossum <guido@python.org>
parents:
1288
diff
changeset
|
292 } |
|
4d292c7857d4
Access checks now work, at least for instance data (not for methods
Guido van Rossum <guido@python.org>
parents:
1288
diff
changeset
|
293 |
|
4d292c7857d4
Access checks now work, at least for instance data (not for methods
Guido van Rossum <guido@python.org>
parents:
1288
diff
changeset
|
294 pos = 0; |
|
4d292c7857d4
Access checks now work, at least for instance data (not for methods
Guido van Rossum <guido@python.org>
parents:
1288
diff
changeset
|
295 while (mappinggetnext(class->cl_dict, &pos, &key, &value)) { |
|
4d292c7857d4
Access checks now work, at least for instance data (not for methods
Guido van Rossum <guido@python.org>
parents:
1288
diff
changeset
|
296 if (!is_accessobject(value)) |
|
4d292c7857d4
Access checks now work, at least for instance data (not for methods
Guido van Rossum <guido@python.org>
parents:
1288
diff
changeset
|
297 continue; |
|
1290
7971fecbf28e
* Lots of small changes related to access.
Guido van Rossum <guido@python.org>
parents:
1289
diff
changeset
|
298 if (hasaccessvalue(value)) |
|
7971fecbf28e
* Lots of small changes related to access.
Guido van Rossum <guido@python.org>
parents:
1289
diff
changeset
|
299 continue; |
|
1289
4d292c7857d4
Access checks now work, at least for instance data (not for methods
Guido van Rossum <guido@python.org>
parents:
1288
diff
changeset
|
300 ac = dict2lookup(inst->in_dict, key); |
|
4d292c7857d4
Access checks now work, at least for instance data (not for methods
Guido van Rossum <guido@python.org>
parents:
1288
diff
changeset
|
301 if (ac != NULL && is_accessobject(ac)) { |
|
4d292c7857d4
Access checks now work, at least for instance data (not for methods
Guido van Rossum <guido@python.org>
parents:
1288
diff
changeset
|
302 err_setval(ConflictError, key); |
|
4d292c7857d4
Access checks now work, at least for instance data (not for methods
Guido van Rossum <guido@python.org>
parents:
1288
diff
changeset
|
303 return -1; |
|
4d292c7857d4
Access checks now work, at least for instance data (not for methods
Guido van Rossum <guido@python.org>
parents:
1288
diff
changeset
|
304 } |
|
4d292c7857d4
Access checks now work, at least for instance data (not for methods
Guido van Rossum <guido@python.org>
parents:
1288
diff
changeset
|
305 ac = cloneaccessobject(value); |
|
4d292c7857d4
Access checks now work, at least for instance data (not for methods
Guido van Rossum <guido@python.org>
parents:
1288
diff
changeset
|
306 if (ac == NULL) |
|
4d292c7857d4
Access checks now work, at least for instance data (not for methods
Guido van Rossum <guido@python.org>
parents:
1288
diff
changeset
|
307 return -1; |
|
4d292c7857d4
Access checks now work, at least for instance data (not for methods
Guido van Rossum <guido@python.org>
parents:
1288
diff
changeset
|
308 ret = dict2insert(inst->in_dict, key, ac); |
|
4d292c7857d4
Access checks now work, at least for instance data (not for methods
Guido van Rossum <guido@python.org>
parents:
1288
diff
changeset
|
309 DECREF(ac); |
|
4d292c7857d4
Access checks now work, at least for instance data (not for methods
Guido van Rossum <guido@python.org>
parents:
1288
diff
changeset
|
310 if (ret != 0) |
|
4d292c7857d4
Access checks now work, at least for instance data (not for methods
Guido van Rossum <guido@python.org>
parents:
1288
diff
changeset
|
311 return -1; |
|
4d292c7857d4
Access checks now work, at least for instance data (not for methods
Guido van Rossum <guido@python.org>
parents:
1288
diff
changeset
|
312 } |
|
4d292c7857d4
Access checks now work, at least for instance data (not for methods
Guido van Rossum <guido@python.org>
parents:
1288
diff
changeset
|
313 return 0; |
|
4d292c7857d4
Access checks now work, at least for instance data (not for methods
Guido van Rossum <guido@python.org>
parents:
1288
diff
changeset
|
314 } |
|
4003
138fa15a1032
Disable support for access statement
Guido van Rossum <guido@python.org>
parents:
3987
diff
changeset
|
315 #endif |
|
1289
4d292c7857d4
Access checks now work, at least for instance data (not for methods
Guido van Rossum <guido@python.org>
parents:
1288
diff
changeset
|
316 |
| 6 | 317 object * |
|
2864
5f951912e995
changes for keyword args to built-in functions and classes
Guido van Rossum <guido@python.org>
parents:
2827
diff
changeset
|
318 newinstanceobject(class, arg, kw) |
|
1288
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1272
diff
changeset
|
319 object *class; |
|
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1272
diff
changeset
|
320 object *arg; |
|
2864
5f951912e995
changes for keyword args to built-in functions and classes
Guido van Rossum <guido@python.org>
parents:
2827
diff
changeset
|
321 object *kw; |
| 6 | 322 { |
|
224
e71b6fec86e5
Renamed class methods to instance methods (which they are)
Guido van Rossum <guido@python.org>
parents:
202
diff
changeset
|
323 register instanceobject *inst; |
|
1288
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1272
diff
changeset
|
324 object *init; |
|
3987
6fbc5c665812
Optimizations by Sjoerd:
Guido van Rossum <guido@python.org>
parents:
3769
diff
changeset
|
325 static object *initstr; |
| 6 | 326 if (!is_classobject(class)) { |
| 27 | 327 err_badcall(); |
| 6 | 328 return NULL; |
| 329 } | |
|
224
e71b6fec86e5
Renamed class methods to instance methods (which they are)
Guido van Rossum <guido@python.org>
parents:
202
diff
changeset
|
330 inst = NEWOBJ(instanceobject, &Instancetype); |
|
e71b6fec86e5
Renamed class methods to instance methods (which they are)
Guido van Rossum <guido@python.org>
parents:
202
diff
changeset
|
331 if (inst == NULL) |
| 6 | 332 return NULL; |
| 333 INCREF(class); | |
|
224
e71b6fec86e5
Renamed class methods to instance methods (which they are)
Guido van Rossum <guido@python.org>
parents:
202
diff
changeset
|
334 inst->in_class = (classobject *)class; |
|
1289
4d292c7857d4
Access checks now work, at least for instance data (not for methods
Guido van Rossum <guido@python.org>
parents:
1288
diff
changeset
|
335 inst->in_dict = newdictobject(); |
|
4003
138fa15a1032
Disable support for access statement
Guido van Rossum <guido@python.org>
parents:
3987
diff
changeset
|
336 if (inst->in_dict == NULL |
|
138fa15a1032
Disable support for access statement
Guido van Rossum <guido@python.org>
parents:
3987
diff
changeset
|
337 #ifdef SUPPORT_OBSOLETE_ACCESS |
|
138fa15a1032
Disable support for access statement
Guido van Rossum <guido@python.org>
parents:
3987
diff
changeset
|
338 || addaccess((classobject *)class, inst) != 0 |
|
138fa15a1032
Disable support for access statement
Guido van Rossum <guido@python.org>
parents:
3987
diff
changeset
|
339 #endif |
|
138fa15a1032
Disable support for access statement
Guido van Rossum <guido@python.org>
parents:
3987
diff
changeset
|
340 ) { |
|
224
e71b6fec86e5
Renamed class methods to instance methods (which they are)
Guido van Rossum <guido@python.org>
parents:
202
diff
changeset
|
341 DECREF(inst); |
| 6 | 342 return NULL; |
| 343 } | |
|
3987
6fbc5c665812
Optimizations by Sjoerd:
Guido van Rossum <guido@python.org>
parents:
3769
diff
changeset
|
344 if (initstr == NULL) |
|
6fbc5c665812
Optimizations by Sjoerd:
Guido van Rossum <guido@python.org>
parents:
3769
diff
changeset
|
345 initstr = newstringobject("__init__"); |
|
6fbc5c665812
Optimizations by Sjoerd:
Guido van Rossum <guido@python.org>
parents:
3769
diff
changeset
|
346 init = instance_getattr1(inst, initstr); |
|
1288
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1272
diff
changeset
|
347 if (init == NULL) { |
|
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1272
diff
changeset
|
348 err_clear(); |
|
2864
5f951912e995
changes for keyword args to built-in functions and classes
Guido van Rossum <guido@python.org>
parents:
2827
diff
changeset
|
349 if (arg != NULL && (!is_tupleobject(arg) || |
|
4426
75abe737e70b
Support passing in an empty dictionary of keywords to newinstanceobject.
Guido van Rossum <guido@python.org>
parents:
4289
diff
changeset
|
350 gettuplesize(arg) != 0) |
|
75abe737e70b
Support passing in an empty dictionary of keywords to newinstanceobject.
Guido van Rossum <guido@python.org>
parents:
4289
diff
changeset
|
351 || kw != NULL && (!is_dictobject(kw) || |
|
75abe737e70b
Support passing in an empty dictionary of keywords to newinstanceobject.
Guido van Rossum <guido@python.org>
parents:
4289
diff
changeset
|
352 getdictsize(kw) != 0)) { |
|
1288
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1272
diff
changeset
|
353 err_setstr(TypeError, |
|
2864
5f951912e995
changes for keyword args to built-in functions and classes
Guido van Rossum <guido@python.org>
parents:
2827
diff
changeset
|
354 "this constructor takes no arguments"); |
|
1288
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1272
diff
changeset
|
355 DECREF(inst); |
|
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1272
diff
changeset
|
356 inst = NULL; |
|
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1272
diff
changeset
|
357 } |
|
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1272
diff
changeset
|
358 } |
|
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1272
diff
changeset
|
359 else { |
|
2864
5f951912e995
changes for keyword args to built-in functions and classes
Guido van Rossum <guido@python.org>
parents:
2827
diff
changeset
|
360 object *res = PyEval_CallObjectWithKeywords(init, arg, kw); |
|
1288
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1272
diff
changeset
|
361 DECREF(init); |
|
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1272
diff
changeset
|
362 if (res == NULL) { |
|
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1272
diff
changeset
|
363 DECREF(inst); |
|
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1272
diff
changeset
|
364 inst = NULL; |
|
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1272
diff
changeset
|
365 } |
|
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1272
diff
changeset
|
366 else { |
|
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1272
diff
changeset
|
367 if (res != None) { |
|
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1272
diff
changeset
|
368 err_setstr(TypeError, |
|
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1272
diff
changeset
|
369 "__init__() should return None"); |
|
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1272
diff
changeset
|
370 DECREF(inst); |
|
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1272
diff
changeset
|
371 inst = NULL; |
|
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1272
diff
changeset
|
372 } |
|
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1272
diff
changeset
|
373 DECREF(res); |
|
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1272
diff
changeset
|
374 } |
|
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1272
diff
changeset
|
375 } |
|
224
e71b6fec86e5
Renamed class methods to instance methods (which they are)
Guido van Rossum <guido@python.org>
parents:
202
diff
changeset
|
376 return (object *)inst; |
| 6 | 377 } |
| 378 | |
|
174
20fee4e2eecd
Change ugly 'class member' to more normal 'instance'.
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
379 /* Instance methods */ |
| 6 | 380 |
| 381 static void | |
|
224
e71b6fec86e5
Renamed class methods to instance methods (which they are)
Guido van Rossum <guido@python.org>
parents:
202
diff
changeset
|
382 instance_dealloc(inst) |
|
e71b6fec86e5
Renamed class methods to instance methods (which they are)
Guido van Rossum <guido@python.org>
parents:
202
diff
changeset
|
383 register instanceobject *inst; |
| 6 | 384 { |
|
2053
de49d49044d7
Lots of minor changes. Note for mappingobject.c: the hash table pointer
Guido van Rossum <guido@python.org>
parents:
2001
diff
changeset
|
385 object *error_type, *error_value, *error_traceback; |
|
1288
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1272
diff
changeset
|
386 object *del; |
|
3987
6fbc5c665812
Optimizations by Sjoerd:
Guido van Rossum <guido@python.org>
parents:
3769
diff
changeset
|
387 static object *delstr; |
|
1288
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1272
diff
changeset
|
388 /* Call the __del__ method if it exists. First temporarily |
|
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1272
diff
changeset
|
389 revive the object and save the current exception, if any. */ |
|
3586
36cc3ee54f28
new debugger symbol names
Guido van Rossum <guido@python.org>
parents:
3549
diff
changeset
|
390 #ifdef Py_TRACE_REFS |
|
36cc3ee54f28
new debugger symbol names
Guido van Rossum <guido@python.org>
parents:
3549
diff
changeset
|
391 /* much too complicated if Py_TRACE_REFS defined */ |
|
3022
c5d852735e7f
Fixed calling of __del__ method with TRACE_REFS defined.
Sjoerd Mullender <sjoerd@acm.org>
parents:
2889
diff
changeset
|
392 extern long ref_total; |
|
c5d852735e7f
Fixed calling of __del__ method with TRACE_REFS defined.
Sjoerd Mullender <sjoerd@acm.org>
parents:
2889
diff
changeset
|
393 inst->ob_type = &Instancetype; |
|
3096
84104d6b4ae7
Get ordering right for TRACE_REFS/COUNT_ALLOCS combination (otherwise
Sjoerd Mullender <sjoerd@acm.org>
parents:
3022
diff
changeset
|
394 NEWREF(inst); |
|
3022
c5d852735e7f
Fixed calling of __del__ method with TRACE_REFS defined.
Sjoerd Mullender <sjoerd@acm.org>
parents:
2889
diff
changeset
|
395 ref_total--; /* compensate for increment in NEWREF */ |
|
c5d852735e7f
Fixed calling of __del__ method with TRACE_REFS defined.
Sjoerd Mullender <sjoerd@acm.org>
parents:
2889
diff
changeset
|
396 #ifdef COUNT_ALLOCS |
|
c5d852735e7f
Fixed calling of __del__ method with TRACE_REFS defined.
Sjoerd Mullender <sjoerd@acm.org>
parents:
2889
diff
changeset
|
397 inst->ob_type->tp_alloc--; /* ditto */ |
|
c5d852735e7f
Fixed calling of __del__ method with TRACE_REFS defined.
Sjoerd Mullender <sjoerd@acm.org>
parents:
2889
diff
changeset
|
398 #endif |
|
3586
36cc3ee54f28
new debugger symbol names
Guido van Rossum <guido@python.org>
parents:
3549
diff
changeset
|
399 #else /* !Py_TRACE_REFS */ |
|
1288
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1272
diff
changeset
|
400 INCREF(inst); |
|
3586
36cc3ee54f28
new debugger symbol names
Guido van Rossum <guido@python.org>
parents:
3549
diff
changeset
|
401 #endif /* !Py_TRACE_REFS */ |
|
2053
de49d49044d7
Lots of minor changes. Note for mappingobject.c: the hash table pointer
Guido van Rossum <guido@python.org>
parents:
2001
diff
changeset
|
402 err_fetch(&error_type, &error_value, &error_traceback); |
|
3987
6fbc5c665812
Optimizations by Sjoerd:
Guido van Rossum <guido@python.org>
parents:
3769
diff
changeset
|
403 if (delstr == NULL) |
|
6fbc5c665812
Optimizations by Sjoerd:
Guido van Rossum <guido@python.org>
parents:
3769
diff
changeset
|
404 delstr = newstringobject("__del__"); |
|
6fbc5c665812
Optimizations by Sjoerd:
Guido van Rossum <guido@python.org>
parents:
3769
diff
changeset
|
405 if ((del = instance_getattr1(inst, delstr)) != NULL) { |
|
2827
ff1faa4cc1df
args to call_object must be tuple or NULL
Guido van Rossum <guido@python.org>
parents:
2641
diff
changeset
|
406 object *res = call_object(del, (object *)NULL); |
|
1288
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1272
diff
changeset
|
407 DECREF(del); |
|
4003
138fa15a1032
Disable support for access statement
Guido van Rossum <guido@python.org>
parents:
3987
diff
changeset
|
408 if (res == NULL) { |
|
4114
5d52395f40b0
Be a bit more careful with printing a warning for a failed __del__.
Guido van Rossum <guido@python.org>
parents:
4086
diff
changeset
|
409 PyObject *f = sysget("stderr"); |
|
5d52395f40b0
Be a bit more careful with printing a warning for a failed __del__.
Guido van Rossum <guido@python.org>
parents:
4086
diff
changeset
|
410 err_clear(); |
|
4289
0036dcf28402
More detailed error message about exception in __del__
Guido van Rossum <guido@python.org>
parents:
4142
diff
changeset
|
411 if (f != NULL) { |
|
0036dcf28402
More detailed error message about exception in __del__
Guido van Rossum <guido@python.org>
parents:
4142
diff
changeset
|
412 writestring("exception in ", f); |
|
0036dcf28402
More detailed error message about exception in __del__
Guido van Rossum <guido@python.org>
parents:
4142
diff
changeset
|
413 writestring(PyString_AsString( |
|
0036dcf28402
More detailed error message about exception in __del__
Guido van Rossum <guido@python.org>
parents:
4142
diff
changeset
|
414 inst->in_class->cl_name), f); |
|
0036dcf28402
More detailed error message about exception in __del__
Guido van Rossum <guido@python.org>
parents:
4142
diff
changeset
|
415 writestring(".__del__() ignored\n", f); |
|
0036dcf28402
More detailed error message about exception in __del__
Guido van Rossum <guido@python.org>
parents:
4142
diff
changeset
|
416 } |
|
4003
138fa15a1032
Disable support for access statement
Guido van Rossum <guido@python.org>
parents:
3987
diff
changeset
|
417 } |
|
138fa15a1032
Disable support for access statement
Guido van Rossum <guido@python.org>
parents:
3987
diff
changeset
|
418 else |
|
138fa15a1032
Disable support for access statement
Guido van Rossum <guido@python.org>
parents:
3987
diff
changeset
|
419 DECREF(res); |
|
1288
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1272
diff
changeset
|
420 } |
|
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1272
diff
changeset
|
421 /* Restore the saved exception and undo the temporary revival */ |
|
2053
de49d49044d7
Lots of minor changes. Note for mappingobject.c: the hash table pointer
Guido van Rossum <guido@python.org>
parents:
2001
diff
changeset
|
422 err_restore(error_type, error_value, error_traceback); |
|
1288
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1272
diff
changeset
|
423 /* Can't use DECREF here, it would cause a recursive call */ |
|
3022
c5d852735e7f
Fixed calling of __del__ method with TRACE_REFS defined.
Sjoerd Mullender <sjoerd@acm.org>
parents:
2889
diff
changeset
|
424 if (--inst->ob_refcnt > 0) { |
|
c5d852735e7f
Fixed calling of __del__ method with TRACE_REFS defined.
Sjoerd Mullender <sjoerd@acm.org>
parents:
2889
diff
changeset
|
425 #ifdef COUNT_ALLOCS |
|
c5d852735e7f
Fixed calling of __del__ method with TRACE_REFS defined.
Sjoerd Mullender <sjoerd@acm.org>
parents:
2889
diff
changeset
|
426 inst->ob_type->tp_free--; |
|
c5d852735e7f
Fixed calling of __del__ method with TRACE_REFS defined.
Sjoerd Mullender <sjoerd@acm.org>
parents:
2889
diff
changeset
|
427 #endif |
|
1288
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1272
diff
changeset
|
428 return; /* __del__ added a reference; don't delete now */ |
|
3022
c5d852735e7f
Fixed calling of __del__ method with TRACE_REFS defined.
Sjoerd Mullender <sjoerd@acm.org>
parents:
2889
diff
changeset
|
429 } |
|
3586
36cc3ee54f28
new debugger symbol names
Guido van Rossum <guido@python.org>
parents:
3549
diff
changeset
|
430 #ifdef Py_TRACE_REFS |
|
3022
c5d852735e7f
Fixed calling of __del__ method with TRACE_REFS defined.
Sjoerd Mullender <sjoerd@acm.org>
parents:
2889
diff
changeset
|
431 #ifdef COUNT_ALLOCS |
|
c5d852735e7f
Fixed calling of __del__ method with TRACE_REFS defined.
Sjoerd Mullender <sjoerd@acm.org>
parents:
2889
diff
changeset
|
432 inst->ob_type->tp_free--; /* compensate for increment in UNREF */ |
|
c5d852735e7f
Fixed calling of __del__ method with TRACE_REFS defined.
Sjoerd Mullender <sjoerd@acm.org>
parents:
2889
diff
changeset
|
433 #endif |
|
c5d852735e7f
Fixed calling of __del__ method with TRACE_REFS defined.
Sjoerd Mullender <sjoerd@acm.org>
parents:
2889
diff
changeset
|
434 UNREF(inst); |
|
c5d852735e7f
Fixed calling of __del__ method with TRACE_REFS defined.
Sjoerd Mullender <sjoerd@acm.org>
parents:
2889
diff
changeset
|
435 inst->ob_type = NULL; |
|
3586
36cc3ee54f28
new debugger symbol names
Guido van Rossum <guido@python.org>
parents:
3549
diff
changeset
|
436 #endif /* Py_TRACE_REFS */ |
|
224
e71b6fec86e5
Renamed class methods to instance methods (which they are)
Guido van Rossum <guido@python.org>
parents:
202
diff
changeset
|
437 DECREF(inst->in_class); |
|
1289
4d292c7857d4
Access checks now work, at least for instance data (not for methods
Guido van Rossum <guido@python.org>
parents:
1288
diff
changeset
|
438 XDECREF(inst->in_dict); |
|
224
e71b6fec86e5
Renamed class methods to instance methods (which they are)
Guido van Rossum <guido@python.org>
parents:
202
diff
changeset
|
439 free((ANY *)inst); |
| 6 | 440 } |
| 441 | |
|
1860
78898b04a65b
Mods (really diffs to 2.29) by Michael Scharf for alternative __getattr__ etc.
Guido van Rossum <guido@python.org>
parents:
1781
diff
changeset
|
442 static object * |
|
78898b04a65b
Mods (really diffs to 2.29) by Michael Scharf for alternative __getattr__ etc.
Guido van Rossum <guido@python.org>
parents:
1781
diff
changeset
|
443 instance_getattr1(inst, name) |
|
224
e71b6fec86e5
Renamed class methods to instance methods (which they are)
Guido van Rossum <guido@python.org>
parents:
202
diff
changeset
|
444 register instanceobject *inst; |
|
3987
6fbc5c665812
Optimizations by Sjoerd:
Guido van Rossum <guido@python.org>
parents:
3769
diff
changeset
|
445 object *name; |
| 6 | 446 { |
|
386
0598f4f25d04
Add several secret __*__ attributes
Guido van Rossum <guido@python.org>
parents:
224
diff
changeset
|
447 register object *v; |
|
3987
6fbc5c665812
Optimizations by Sjoerd:
Guido van Rossum <guido@python.org>
parents:
3769
diff
changeset
|
448 register char *sname = getstringvalue(name); |
|
1289
4d292c7857d4
Access checks now work, at least for instance data (not for methods
Guido van Rossum <guido@python.org>
parents:
1288
diff
changeset
|
449 classobject *class; |
|
3987
6fbc5c665812
Optimizations by Sjoerd:
Guido van Rossum <guido@python.org>
parents:
3769
diff
changeset
|
450 if (sname[0] == '_' && sname[1] == '_') { |
|
6fbc5c665812
Optimizations by Sjoerd:
Guido van Rossum <guido@python.org>
parents:
3769
diff
changeset
|
451 if (strcmp(sname, "__dict__") == 0) { |
|
2105
ea7b2bf0b521
add restrictions in restricted mode
Guido van Rossum <guido@python.org>
parents:
2084
diff
changeset
|
452 if (getrestricted()) { |
|
ea7b2bf0b521
add restrictions in restricted mode
Guido van Rossum <guido@python.org>
parents:
2084
diff
changeset
|
453 err_setstr(RuntimeError, |
|
ea7b2bf0b521
add restrictions in restricted mode
Guido van Rossum <guido@python.org>
parents:
2084
diff
changeset
|
454 "instance.__dict__ not accessible in restricted mode"); |
|
ea7b2bf0b521
add restrictions in restricted mode
Guido van Rossum <guido@python.org>
parents:
2084
diff
changeset
|
455 return NULL; |
|
ea7b2bf0b521
add restrictions in restricted mode
Guido van Rossum <guido@python.org>
parents:
2084
diff
changeset
|
456 } |
|
1356
043da19302ef
Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1351
diff
changeset
|
457 INCREF(inst->in_dict); |
|
043da19302ef
Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1351
diff
changeset
|
458 return inst->in_dict; |
|
043da19302ef
Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1351
diff
changeset
|
459 } |
|
3987
6fbc5c665812
Optimizations by Sjoerd:
Guido van Rossum <guido@python.org>
parents:
3769
diff
changeset
|
460 if (strcmp(sname, "__class__") == 0) { |
|
1356
043da19302ef
Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1351
diff
changeset
|
461 INCREF(inst->in_class); |
|
043da19302ef
Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1351
diff
changeset
|
462 return (object *)inst->in_class; |
|
043da19302ef
Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1351
diff
changeset
|
463 } |
|
386
0598f4f25d04
Add several secret __*__ attributes
Guido van Rossum <guido@python.org>
parents:
224
diff
changeset
|
464 } |
|
1293
ac3c80abbf43
* classobject.c: in instance_getattr, don't make a method out of a
Guido van Rossum <guido@python.org>
parents:
1290
diff
changeset
|
465 class = NULL; |
|
3987
6fbc5c665812
Optimizations by Sjoerd:
Guido van Rossum <guido@python.org>
parents:
3769
diff
changeset
|
466 v = mappinglookup(inst->in_dict, name); |
|
1290
7971fecbf28e
* Lots of small changes related to access.
Guido van Rossum <guido@python.org>
parents:
1289
diff
changeset
|
467 if (v == NULL) { |
|
7971fecbf28e
* Lots of small changes related to access.
Guido van Rossum <guido@python.org>
parents:
1289
diff
changeset
|
468 v = class_lookup(inst->in_class, name, &class); |
|
7971fecbf28e
* Lots of small changes related to access.
Guido van Rossum <guido@python.org>
parents:
1289
diff
changeset
|
469 if (v == NULL) { |
|
3987
6fbc5c665812
Optimizations by Sjoerd:
Guido van Rossum <guido@python.org>
parents:
3769
diff
changeset
|
470 err_setval(AttributeError, name); |
|
1290
7971fecbf28e
* Lots of small changes related to access.
Guido van Rossum <guido@python.org>
parents:
1289
diff
changeset
|
471 return NULL; |
|
7971fecbf28e
* Lots of small changes related to access.
Guido van Rossum <guido@python.org>
parents:
1289
diff
changeset
|
472 } |
| 6 | 473 } |
|
4003
138fa15a1032
Disable support for access statement
Guido van Rossum <guido@python.org>
parents:
3987
diff
changeset
|
474 #ifdef SUPPORT_OBSOLETE_ACCESS |
|
1290
7971fecbf28e
* Lots of small changes related to access.
Guido van Rossum <guido@python.org>
parents:
1289
diff
changeset
|
475 if (is_accessobject(v)) { |
|
1293
ac3c80abbf43
* classobject.c: in instance_getattr, don't make a method out of a
Guido van Rossum <guido@python.org>
parents:
1290
diff
changeset
|
476 v = getaccessvalue(v, getowner()); |
|
1290
7971fecbf28e
* Lots of small changes related to access.
Guido van Rossum <guido@python.org>
parents:
1289
diff
changeset
|
477 if (v == NULL) |
|
7971fecbf28e
* Lots of small changes related to access.
Guido van Rossum <guido@python.org>
parents:
1289
diff
changeset
|
478 return NULL; |
|
7971fecbf28e
* Lots of small changes related to access.
Guido van Rossum <guido@python.org>
parents:
1289
diff
changeset
|
479 } |
|
7971fecbf28e
* Lots of small changes related to access.
Guido van Rossum <guido@python.org>
parents:
1289
diff
changeset
|
480 else |
|
4003
138fa15a1032
Disable support for access statement
Guido van Rossum <guido@python.org>
parents:
3987
diff
changeset
|
481 #endif |
|
1290
7971fecbf28e
* Lots of small changes related to access.
Guido van Rossum <guido@python.org>
parents:
1289
diff
changeset
|
482 INCREF(v); |
|
1351
e149c77d173c
Makefile, import.c: Lance's alternative module search (allow .pyc file
Guido van Rossum <guido@python.org>
parents:
1293
diff
changeset
|
483 if (class != NULL) { |
|
e149c77d173c
Makefile, import.c: Lance's alternative module search (allow .pyc file
Guido van Rossum <guido@python.org>
parents:
1293
diff
changeset
|
484 if (is_funcobject(v)) { |
|
e149c77d173c
Makefile, import.c: Lance's alternative module search (allow .pyc file
Guido van Rossum <guido@python.org>
parents:
1293
diff
changeset
|
485 object *w = newinstancemethodobject(v, (object *)inst, |
|
e149c77d173c
Makefile, import.c: Lance's alternative module search (allow .pyc file
Guido van Rossum <guido@python.org>
parents:
1293
diff
changeset
|
486 (object *)class); |
|
e149c77d173c
Makefile, import.c: Lance's alternative module search (allow .pyc file
Guido van Rossum <guido@python.org>
parents:
1293
diff
changeset
|
487 DECREF(v); |
|
e149c77d173c
Makefile, import.c: Lance's alternative module search (allow .pyc file
Guido van Rossum <guido@python.org>
parents:
1293
diff
changeset
|
488 v = w; |
|
e149c77d173c
Makefile, import.c: Lance's alternative module search (allow .pyc file
Guido van Rossum <guido@python.org>
parents:
1293
diff
changeset
|
489 } |
|
e149c77d173c
Makefile, import.c: Lance's alternative module search (allow .pyc file
Guido van Rossum <guido@python.org>
parents:
1293
diff
changeset
|
490 else if (is_instancemethodobject(v)) { |
|
e149c77d173c
Makefile, import.c: Lance's alternative module search (allow .pyc file
Guido van Rossum <guido@python.org>
parents:
1293
diff
changeset
|
491 object *im_class = instancemethodgetclass(v); |
|
e149c77d173c
Makefile, import.c: Lance's alternative module search (allow .pyc file
Guido van Rossum <guido@python.org>
parents:
1293
diff
changeset
|
492 /* Only if classes are compatible */ |
|
e149c77d173c
Makefile, import.c: Lance's alternative module search (allow .pyc file
Guido van Rossum <guido@python.org>
parents:
1293
diff
changeset
|
493 if (issubclass((object *)class, im_class)) { |
|
e149c77d173c
Makefile, import.c: Lance's alternative module search (allow .pyc file
Guido van Rossum <guido@python.org>
parents:
1293
diff
changeset
|
494 object *im_func = instancemethodgetfunc(v); |
|
e149c77d173c
Makefile, import.c: Lance's alternative module search (allow .pyc file
Guido van Rossum <guido@python.org>
parents:
1293
diff
changeset
|
495 object *w = newinstancemethodobject(im_func, |
|
e149c77d173c
Makefile, import.c: Lance's alternative module search (allow .pyc file
Guido van Rossum <guido@python.org>
parents:
1293
diff
changeset
|
496 (object *)inst, im_class); |
|
e149c77d173c
Makefile, import.c: Lance's alternative module search (allow .pyc file
Guido van Rossum <guido@python.org>
parents:
1293
diff
changeset
|
497 DECREF(v); |
|
e149c77d173c
Makefile, import.c: Lance's alternative module search (allow .pyc file
Guido van Rossum <guido@python.org>
parents:
1293
diff
changeset
|
498 v = w; |
|
e149c77d173c
Makefile, import.c: Lance's alternative module search (allow .pyc file
Guido van Rossum <guido@python.org>
parents:
1293
diff
changeset
|
499 } |
|
e149c77d173c
Makefile, import.c: Lance's alternative module search (allow .pyc file
Guido van Rossum <guido@python.org>
parents:
1293
diff
changeset
|
500 } |
|
1290
7971fecbf28e
* Lots of small changes related to access.
Guido van Rossum <guido@python.org>
parents:
1289
diff
changeset
|
501 } |
|
7971fecbf28e
* Lots of small changes related to access.
Guido van Rossum <guido@python.org>
parents:
1289
diff
changeset
|
502 return v; |
| 6 | 503 } |
| 504 | |
|
1860
78898b04a65b
Mods (really diffs to 2.29) by Michael Scharf for alternative __getattr__ etc.
Guido van Rossum <guido@python.org>
parents:
1781
diff
changeset
|
505 static object * |
|
78898b04a65b
Mods (really diffs to 2.29) by Michael Scharf for alternative __getattr__ etc.
Guido van Rossum <guido@python.org>
parents:
1781
diff
changeset
|
506 instance_getattr(inst, name) |
|
78898b04a65b
Mods (really diffs to 2.29) by Michael Scharf for alternative __getattr__ etc.
Guido van Rossum <guido@python.org>
parents:
1781
diff
changeset
|
507 register instanceobject *inst; |
|
3987
6fbc5c665812
Optimizations by Sjoerd:
Guido van Rossum <guido@python.org>
parents:
3769
diff
changeset
|
508 object *name; |
|
1860
78898b04a65b
Mods (really diffs to 2.29) by Michael Scharf for alternative __getattr__ etc.
Guido van Rossum <guido@python.org>
parents:
1781
diff
changeset
|
509 { |
|
78898b04a65b
Mods (really diffs to 2.29) by Michael Scharf for alternative __getattr__ etc.
Guido van Rossum <guido@python.org>
parents:
1781
diff
changeset
|
510 register object *func, *res; |
|
1861
d405e3e8ffd3
Yet another version (by me) of __getattr__ etc.
Guido van Rossum <guido@python.org>
parents:
1860
diff
changeset
|
511 res = instance_getattr1(inst, name); |
|
d405e3e8ffd3
Yet another version (by me) of __getattr__ etc.
Guido van Rossum <guido@python.org>
parents:
1860
diff
changeset
|
512 if (res == NULL && (func = inst->in_class->cl_getattr) != NULL) { |
|
d405e3e8ffd3
Yet another version (by me) of __getattr__ etc.
Guido van Rossum <guido@python.org>
parents:
1860
diff
changeset
|
513 object *args; |
|
2053
de49d49044d7
Lots of minor changes. Note for mappingobject.c: the hash table pointer
Guido van Rossum <guido@python.org>
parents:
2001
diff
changeset
|
514 err_clear(); |
|
3987
6fbc5c665812
Optimizations by Sjoerd:
Guido van Rossum <guido@python.org>
parents:
3769
diff
changeset
|
515 args = mkvalue("(OO)", inst, name); |
|
1861
d405e3e8ffd3
Yet another version (by me) of __getattr__ etc.
Guido van Rossum <guido@python.org>
parents:
1860
diff
changeset
|
516 if (args == NULL) |
|
1860
78898b04a65b
Mods (really diffs to 2.29) by Michael Scharf for alternative __getattr__ etc.
Guido van Rossum <guido@python.org>
parents:
1781
diff
changeset
|
517 return NULL; |
|
1861
d405e3e8ffd3
Yet another version (by me) of __getattr__ etc.
Guido van Rossum <guido@python.org>
parents:
1860
diff
changeset
|
518 res = call_object(func, args); |
|
d405e3e8ffd3
Yet another version (by me) of __getattr__ etc.
Guido van Rossum <guido@python.org>
parents:
1860
diff
changeset
|
519 DECREF(args); |
|
1860
78898b04a65b
Mods (really diffs to 2.29) by Michael Scharf for alternative __getattr__ etc.
Guido van Rossum <guido@python.org>
parents:
1781
diff
changeset
|
520 } |
|
78898b04a65b
Mods (really diffs to 2.29) by Michael Scharf for alternative __getattr__ etc.
Guido van Rossum <guido@python.org>
parents:
1781
diff
changeset
|
521 return res; |
|
78898b04a65b
Mods (really diffs to 2.29) by Michael Scharf for alternative __getattr__ etc.
Guido van Rossum <guido@python.org>
parents:
1781
diff
changeset
|
522 } |
|
78898b04a65b
Mods (really diffs to 2.29) by Michael Scharf for alternative __getattr__ etc.
Guido van Rossum <guido@python.org>
parents:
1781
diff
changeset
|
523 |
| 6 | 524 static int |
|
1860
78898b04a65b
Mods (really diffs to 2.29) by Michael Scharf for alternative __getattr__ etc.
Guido van Rossum <guido@python.org>
parents:
1781
diff
changeset
|
525 instance_setattr1(inst, name, v) |
|
224
e71b6fec86e5
Renamed class methods to instance methods (which they are)
Guido van Rossum <guido@python.org>
parents:
202
diff
changeset
|
526 instanceobject *inst; |
|
3987
6fbc5c665812
Optimizations by Sjoerd:
Guido van Rossum <guido@python.org>
parents:
3769
diff
changeset
|
527 object *name; |
| 6 | 528 object *v; |
| 529 { | |
|
4003
138fa15a1032
Disable support for access statement
Guido van Rossum <guido@python.org>
parents:
3987
diff
changeset
|
530 #ifdef SUPPORT_OBSOLETE_ACCESS |
|
1288
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1272
diff
changeset
|
531 object *ac; |
|
3987
6fbc5c665812
Optimizations by Sjoerd:
Guido van Rossum <guido@python.org>
parents:
3769
diff
changeset
|
532 ac = mappinglookup(inst->in_dict, name); |
|
1288
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1272
diff
changeset
|
533 if (ac != NULL && is_accessobject(ac)) |
|
1293
ac3c80abbf43
* classobject.c: in instance_getattr, don't make a method out of a
Guido van Rossum <guido@python.org>
parents:
1290
diff
changeset
|
534 return setaccessvalue(ac, getowner(), v); |
|
4003
138fa15a1032
Disable support for access statement
Guido van Rossum <guido@python.org>
parents:
3987
diff
changeset
|
535 #endif |
|
1031
7a11a70f7f44
classobject.c moduleobject.c stdwinmodule.c xxobject.c:
Guido van Rossum <guido@python.org>
parents:
1021
diff
changeset
|
536 if (v == NULL) { |
|
3987
6fbc5c665812
Optimizations by Sjoerd:
Guido van Rossum <guido@python.org>
parents:
3769
diff
changeset
|
537 int rv = mappingremove(inst->in_dict, name); |
|
1031
7a11a70f7f44
classobject.c moduleobject.c stdwinmodule.c xxobject.c:
Guido van Rossum <guido@python.org>
parents:
1021
diff
changeset
|
538 if (rv < 0) |
|
7a11a70f7f44
classobject.c moduleobject.c stdwinmodule.c xxobject.c:
Guido van Rossum <guido@python.org>
parents:
1021
diff
changeset
|
539 err_setstr(AttributeError, |
|
7a11a70f7f44
classobject.c moduleobject.c stdwinmodule.c xxobject.c:
Guido van Rossum <guido@python.org>
parents:
1021
diff
changeset
|
540 "delete non-existing instance attribute"); |
|
7a11a70f7f44
classobject.c moduleobject.c stdwinmodule.c xxobject.c:
Guido van Rossum <guido@python.org>
parents:
1021
diff
changeset
|
541 return rv; |
|
7a11a70f7f44
classobject.c moduleobject.c stdwinmodule.c xxobject.c:
Guido van Rossum <guido@python.org>
parents:
1021
diff
changeset
|
542 } |
| 6 | 543 else |
|
3987
6fbc5c665812
Optimizations by Sjoerd:
Guido van Rossum <guido@python.org>
parents:
3769
diff
changeset
|
544 return mappinginsert(inst->in_dict, name, v); |
| 6 | 545 } |
| 546 | |
|
1860
78898b04a65b
Mods (really diffs to 2.29) by Michael Scharf for alternative __getattr__ etc.
Guido van Rossum <guido@python.org>
parents:
1781
diff
changeset
|
547 static int |
|
78898b04a65b
Mods (really diffs to 2.29) by Michael Scharf for alternative __getattr__ etc.
Guido van Rossum <guido@python.org>
parents:
1781
diff
changeset
|
548 instance_setattr(inst, name, v) |
|
78898b04a65b
Mods (really diffs to 2.29) by Michael Scharf for alternative __getattr__ etc.
Guido van Rossum <guido@python.org>
parents:
1781
diff
changeset
|
549 instanceobject *inst; |
|
3987
6fbc5c665812
Optimizations by Sjoerd:
Guido van Rossum <guido@python.org>
parents:
3769
diff
changeset
|
550 object *name; |
|
1860
78898b04a65b
Mods (really diffs to 2.29) by Michael Scharf for alternative __getattr__ etc.
Guido van Rossum <guido@python.org>
parents:
1781
diff
changeset
|
551 object *v; |
|
78898b04a65b
Mods (really diffs to 2.29) by Michael Scharf for alternative __getattr__ etc.
Guido van Rossum <guido@python.org>
parents:
1781
diff
changeset
|
552 { |
|
1861
d405e3e8ffd3
Yet another version (by me) of __getattr__ etc.
Guido van Rossum <guido@python.org>
parents:
1860
diff
changeset
|
553 object *func, *args, *res; |
|
3987
6fbc5c665812
Optimizations by Sjoerd:
Guido van Rossum <guido@python.org>
parents:
3769
diff
changeset
|
554 char *sname = getstringvalue(name); |
|
6fbc5c665812
Optimizations by Sjoerd:
Guido van Rossum <guido@python.org>
parents:
3769
diff
changeset
|
555 if (sname[0] == '_' && sname[1] == '_' |
|
6fbc5c665812
Optimizations by Sjoerd:
Guido van Rossum <guido@python.org>
parents:
3769
diff
changeset
|
556 && (strcmp(sname, "__dict__") == 0 || |
|
6fbc5c665812
Optimizations by Sjoerd:
Guido van Rossum <guido@python.org>
parents:
3769
diff
changeset
|
557 strcmp(sname, "__class__") == 0)) { |
|
4142
44872f255f93
Use getstringsize where available instead of strlen.
Guido van Rossum <guido@python.org>
parents:
4114
diff
changeset
|
558 int n = getstringsize(name); |
|
3987
6fbc5c665812
Optimizations by Sjoerd:
Guido van Rossum <guido@python.org>
parents:
3769
diff
changeset
|
559 if (sname[n-1] == '_' && sname[n-2] == '_') { |
|
1861
d405e3e8ffd3
Yet another version (by me) of __getattr__ etc.
Guido van Rossum <guido@python.org>
parents:
1860
diff
changeset
|
560 err_setstr(TypeError, "read-only special attribute"); |
|
1860
78898b04a65b
Mods (really diffs to 2.29) by Michael Scharf for alternative __getattr__ etc.
Guido van Rossum <guido@python.org>
parents:
1781
diff
changeset
|
561 return -1; |
|
78898b04a65b
Mods (really diffs to 2.29) by Michael Scharf for alternative __getattr__ etc.
Guido van Rossum <guido@python.org>
parents:
1781
diff
changeset
|
562 } |
|
78898b04a65b
Mods (really diffs to 2.29) by Michael Scharf for alternative __getattr__ etc.
Guido van Rossum <guido@python.org>
parents:
1781
diff
changeset
|
563 } |
|
1861
d405e3e8ffd3
Yet another version (by me) of __getattr__ etc.
Guido van Rossum <guido@python.org>
parents:
1860
diff
changeset
|
564 if (v == NULL) |
|
d405e3e8ffd3
Yet another version (by me) of __getattr__ etc.
Guido van Rossum <guido@python.org>
parents:
1860
diff
changeset
|
565 func = inst->in_class->cl_delattr; |
|
d405e3e8ffd3
Yet another version (by me) of __getattr__ etc.
Guido van Rossum <guido@python.org>
parents:
1860
diff
changeset
|
566 else |
|
d405e3e8ffd3
Yet another version (by me) of __getattr__ etc.
Guido van Rossum <guido@python.org>
parents:
1860
diff
changeset
|
567 func = inst->in_class->cl_setattr; |
|
d405e3e8ffd3
Yet another version (by me) of __getattr__ etc.
Guido van Rossum <guido@python.org>
parents:
1860
diff
changeset
|
568 if (func == NULL) |
|
d405e3e8ffd3
Yet another version (by me) of __getattr__ etc.
Guido van Rossum <guido@python.org>
parents:
1860
diff
changeset
|
569 return instance_setattr1(inst, name, v); |
|
d405e3e8ffd3
Yet another version (by me) of __getattr__ etc.
Guido van Rossum <guido@python.org>
parents:
1860
diff
changeset
|
570 if (v == NULL) |
|
3987
6fbc5c665812
Optimizations by Sjoerd:
Guido van Rossum <guido@python.org>
parents:
3769
diff
changeset
|
571 args = mkvalue("(OO)", inst, name); |
|
1861
d405e3e8ffd3
Yet another version (by me) of __getattr__ etc.
Guido van Rossum <guido@python.org>
parents:
1860
diff
changeset
|
572 else |
|
3987
6fbc5c665812
Optimizations by Sjoerd:
Guido van Rossum <guido@python.org>
parents:
3769
diff
changeset
|
573 args = mkvalue("(OOO)", inst, name, v); |
|
1861
d405e3e8ffd3
Yet another version (by me) of __getattr__ etc.
Guido van Rossum <guido@python.org>
parents:
1860
diff
changeset
|
574 if (args == NULL) |
|
d405e3e8ffd3
Yet another version (by me) of __getattr__ etc.
Guido van Rossum <guido@python.org>
parents:
1860
diff
changeset
|
575 return -1; |
|
d405e3e8ffd3
Yet another version (by me) of __getattr__ etc.
Guido van Rossum <guido@python.org>
parents:
1860
diff
changeset
|
576 res = call_object(func, args); |
|
d405e3e8ffd3
Yet another version (by me) of __getattr__ etc.
Guido van Rossum <guido@python.org>
parents:
1860
diff
changeset
|
577 DECREF(args); |
|
d405e3e8ffd3
Yet another version (by me) of __getattr__ etc.
Guido van Rossum <guido@python.org>
parents:
1860
diff
changeset
|
578 if (res == NULL) |
|
d405e3e8ffd3
Yet another version (by me) of __getattr__ etc.
Guido van Rossum <guido@python.org>
parents:
1860
diff
changeset
|
579 return -1; |
|
d405e3e8ffd3
Yet another version (by me) of __getattr__ etc.
Guido van Rossum <guido@python.org>
parents:
1860
diff
changeset
|
580 DECREF(res); |
|
1860
78898b04a65b
Mods (really diffs to 2.29) by Michael Scharf for alternative __getattr__ etc.
Guido van Rossum <guido@python.org>
parents:
1781
diff
changeset
|
581 return 0; |
|
78898b04a65b
Mods (really diffs to 2.29) by Michael Scharf for alternative __getattr__ etc.
Guido van Rossum <guido@python.org>
parents:
1781
diff
changeset
|
582 } |
|
78898b04a65b
Mods (really diffs to 2.29) by Michael Scharf for alternative __getattr__ etc.
Guido van Rossum <guido@python.org>
parents:
1781
diff
changeset
|
583 |
|
1257
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1252
diff
changeset
|
584 static object * |
|
944
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
585 instance_repr(inst) |
|
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
586 instanceobject *inst; |
|
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
587 { |
|
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
588 object *func; |
|
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
589 object *res; |
|
3987
6fbc5c665812
Optimizations by Sjoerd:
Guido van Rossum <guido@python.org>
parents:
3769
diff
changeset
|
590 static object *reprstr; |
|
944
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
591 |
|
3987
6fbc5c665812
Optimizations by Sjoerd:
Guido van Rossum <guido@python.org>
parents:
3769
diff
changeset
|
592 if (reprstr == NULL) |
|
6fbc5c665812
Optimizations by Sjoerd:
Guido van Rossum <guido@python.org>
parents:
3769
diff
changeset
|
593 reprstr = newstringobject("__repr__"); |
|
6fbc5c665812
Optimizations by Sjoerd:
Guido van Rossum <guido@python.org>
parents:
3769
diff
changeset
|
594 func = instance_getattr(inst, reprstr); |
|
944
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
595 if (func == NULL) { |
|
1288
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1272
diff
changeset
|
596 char buf[140]; |
|
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1272
diff
changeset
|
597 object *classname = inst->in_class->cl_name; |
|
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1272
diff
changeset
|
598 char *cname; |
|
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1272
diff
changeset
|
599 if (classname != NULL && is_stringobject(classname)) |
|
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1272
diff
changeset
|
600 cname = getstringvalue(classname); |
|
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1272
diff
changeset
|
601 else |
|
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1272
diff
changeset
|
602 cname = "?"; |
|
944
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
603 err_clear(); |
|
1288
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1272
diff
changeset
|
604 sprintf(buf, "<%.100s instance at %lx>", cname, (long)inst); |
|
944
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
605 return newstringobject(buf); |
|
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
606 } |
|
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
607 res = call_object(func, (object *)NULL); |
|
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
608 DECREF(func); |
|
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
609 return res; |
|
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
610 } |
|
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
611 |
|
2084
abbf2a4b488b
* Objects/classobject.c: added 5th (function) parameter to
Guido van Rossum <guido@python.org>
parents:
2059
diff
changeset
|
612 static object * |
|
abbf2a4b488b
* Objects/classobject.c: added 5th (function) parameter to
Guido van Rossum <guido@python.org>
parents:
2059
diff
changeset
|
613 instance_compare1(inst, other) |
|
abbf2a4b488b
* Objects/classobject.c: added 5th (function) parameter to
Guido van Rossum <guido@python.org>
parents:
2059
diff
changeset
|
614 object *inst, *other; |
|
abbf2a4b488b
* Objects/classobject.c: added 5th (function) parameter to
Guido van Rossum <guido@python.org>
parents:
2059
diff
changeset
|
615 { |
|
abbf2a4b488b
* Objects/classobject.c: added 5th (function) parameter to
Guido van Rossum <guido@python.org>
parents:
2059
diff
changeset
|
616 return instancebinop(inst, other, "__cmp__", "__rcmp__", |
|
abbf2a4b488b
* Objects/classobject.c: added 5th (function) parameter to
Guido van Rossum <guido@python.org>
parents:
2059
diff
changeset
|
617 instance_compare1); |
|
abbf2a4b488b
* Objects/classobject.c: added 5th (function) parameter to
Guido van Rossum <guido@python.org>
parents:
2059
diff
changeset
|
618 } |
|
abbf2a4b488b
* Objects/classobject.c: added 5th (function) parameter to
Guido van Rossum <guido@python.org>
parents:
2059
diff
changeset
|
619 |
|
1257
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1252
diff
changeset
|
620 static int |
|
944
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
621 instance_compare(inst, other) |
|
1908
0437738279a8
* Include/classobject.h, Objects/classobject.c, Python/ceval.c:
Guido van Rossum <guido@python.org>
parents:
1861
diff
changeset
|
622 object *inst, *other; |
|
944
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
623 { |
|
1908
0437738279a8
* Include/classobject.h, Objects/classobject.c, Python/ceval.c:
Guido van Rossum <guido@python.org>
parents:
1861
diff
changeset
|
624 object *result; |
|
2084
abbf2a4b488b
* Objects/classobject.c: added 5th (function) parameter to
Guido van Rossum <guido@python.org>
parents:
2059
diff
changeset
|
625 long outcome; |
|
abbf2a4b488b
* Objects/classobject.c: added 5th (function) parameter to
Guido van Rossum <guido@python.org>
parents:
2059
diff
changeset
|
626 result = instance_compare1(inst, other); |
|
abbf2a4b488b
* Objects/classobject.c: added 5th (function) parameter to
Guido van Rossum <guido@python.org>
parents:
2059
diff
changeset
|
627 if (result == NULL || !is_intobject(result)) { |
|
2001
f05b5cd79fbe
fix comparison of instances without _-cmp__
Guido van Rossum <guido@python.org>
parents:
1982
diff
changeset
|
628 error: |
|
1982
33515485ead0
Comparison of two class instances without __cmp__ or __rcmp__ methods
Sjoerd Mullender <sjoerd@acm.org>
parents:
1908
diff
changeset
|
629 err_clear(); |
|
2001
f05b5cd79fbe
fix comparison of instances without _-cmp__
Guido van Rossum <guido@python.org>
parents:
1982
diff
changeset
|
630 return (inst < other) ? -1 : 1; |
|
1982
33515485ead0
Comparison of two class instances without __cmp__ or __rcmp__ methods
Sjoerd Mullender <sjoerd@acm.org>
parents:
1908
diff
changeset
|
631 } |
|
1908
0437738279a8
* Include/classobject.h, Objects/classobject.c, Python/ceval.c:
Guido van Rossum <guido@python.org>
parents:
1861
diff
changeset
|
632 outcome = getintvalue(result); |
|
0437738279a8
* Include/classobject.h, Objects/classobject.c, Python/ceval.c:
Guido van Rossum <guido@python.org>
parents:
1861
diff
changeset
|
633 DECREF(result); |
|
0437738279a8
* Include/classobject.h, Objects/classobject.c, Python/ceval.c:
Guido van Rossum <guido@python.org>
parents:
1861
diff
changeset
|
634 if (outcome < 0) |
|
0437738279a8
* Include/classobject.h, Objects/classobject.c, Python/ceval.c:
Guido van Rossum <guido@python.org>
parents:
1861
diff
changeset
|
635 return -1; |
|
0437738279a8
* Include/classobject.h, Objects/classobject.c, Python/ceval.c:
Guido van Rossum <guido@python.org>
parents:
1861
diff
changeset
|
636 else if (outcome > 0) |
|
0437738279a8
* Include/classobject.h, Objects/classobject.c, Python/ceval.c:
Guido van Rossum <guido@python.org>
parents:
1861
diff
changeset
|
637 return 1; |
|
0437738279a8
* Include/classobject.h, Objects/classobject.c, Python/ceval.c:
Guido van Rossum <guido@python.org>
parents:
1861
diff
changeset
|
638 return 0; |
|
944
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
639 } |
|
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
640 |
|
1257
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1252
diff
changeset
|
641 static long |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1252
diff
changeset
|
642 instance_hash(inst) |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1252
diff
changeset
|
643 instanceobject *inst; |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1252
diff
changeset
|
644 { |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1252
diff
changeset
|
645 object *func; |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1252
diff
changeset
|
646 object *res; |
|
1272
7b7411f9b116
Fix bug in class instance hash (forgot to clear error condition).
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
647 long outcome; |
|
3987
6fbc5c665812
Optimizations by Sjoerd:
Guido van Rossum <guido@python.org>
parents:
3769
diff
changeset
|
648 static object *hashstr, *cmpstr; |
|
1257
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1252
diff
changeset
|
649 |
|
3987
6fbc5c665812
Optimizations by Sjoerd:
Guido van Rossum <guido@python.org>
parents:
3769
diff
changeset
|
650 if (hashstr == NULL) |
|
6fbc5c665812
Optimizations by Sjoerd:
Guido van Rossum <guido@python.org>
parents:
3769
diff
changeset
|
651 hashstr = newstringobject("__hash__"); |
|
6fbc5c665812
Optimizations by Sjoerd:
Guido van Rossum <guido@python.org>
parents:
3769
diff
changeset
|
652 func = instance_getattr(inst, hashstr); |
|
1257
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1252
diff
changeset
|
653 if (func == NULL) { |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1252
diff
changeset
|
654 /* If there is no __cmp__ method, we hash on the address. |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1252
diff
changeset
|
655 If a __cmp__ method exists, there must be a __hash__. */ |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1252
diff
changeset
|
656 err_clear(); |
|
3987
6fbc5c665812
Optimizations by Sjoerd:
Guido van Rossum <guido@python.org>
parents:
3769
diff
changeset
|
657 if (cmpstr == NULL) |
|
6fbc5c665812
Optimizations by Sjoerd:
Guido van Rossum <guido@python.org>
parents:
3769
diff
changeset
|
658 cmpstr = newstringobject("__cmp__"); |
|
6fbc5c665812
Optimizations by Sjoerd:
Guido van Rossum <guido@python.org>
parents:
3769
diff
changeset
|
659 func = instance_getattr(inst, cmpstr); |
|
1272
7b7411f9b116
Fix bug in class instance hash (forgot to clear error condition).
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
660 if (func == NULL) { |
|
7b7411f9b116
Fix bug in class instance hash (forgot to clear error condition).
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
661 err_clear(); |
|
7b7411f9b116
Fix bug in class instance hash (forgot to clear error condition).
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
662 outcome = (long)inst; |
|
7b7411f9b116
Fix bug in class instance hash (forgot to clear error condition).
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
663 if (outcome == -1) |
|
7b7411f9b116
Fix bug in class instance hash (forgot to clear error condition).
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
664 outcome = -2; |
|
7b7411f9b116
Fix bug in class instance hash (forgot to clear error condition).
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
665 return outcome; |
|
7b7411f9b116
Fix bug in class instance hash (forgot to clear error condition).
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
666 } |
|
1257
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1252
diff
changeset
|
667 err_setstr(TypeError, "unhashable instance"); |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1252
diff
changeset
|
668 return -1; |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1252
diff
changeset
|
669 } |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1252
diff
changeset
|
670 res = call_object(func, (object *)NULL); |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1252
diff
changeset
|
671 DECREF(func); |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1252
diff
changeset
|
672 if (res == NULL) |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1252
diff
changeset
|
673 return -1; |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1252
diff
changeset
|
674 if (is_intobject(res)) { |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1252
diff
changeset
|
675 outcome = getintvalue(res); |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1252
diff
changeset
|
676 if (outcome == -1) |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1252
diff
changeset
|
677 outcome = -2; |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1252
diff
changeset
|
678 } |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1252
diff
changeset
|
679 else { |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1252
diff
changeset
|
680 err_setstr(TypeError, "__hash__() should return an int"); |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1252
diff
changeset
|
681 outcome = -1; |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1252
diff
changeset
|
682 } |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1252
diff
changeset
|
683 DECREF(res); |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1252
diff
changeset
|
684 return outcome; |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1252
diff
changeset
|
685 } |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1252
diff
changeset
|
686 |
|
3987
6fbc5c665812
Optimizations by Sjoerd:
Guido van Rossum <guido@python.org>
parents:
3769
diff
changeset
|
687 static object *getitemstr, *setitemstr, *delitemstr, *lenstr; |
|
6fbc5c665812
Optimizations by Sjoerd:
Guido van Rossum <guido@python.org>
parents:
3769
diff
changeset
|
688 |
|
1257
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1252
diff
changeset
|
689 static int |
|
944
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
690 instance_length(inst) |
|
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
691 instanceobject *inst; |
|
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
692 { |
|
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
693 object *func; |
|
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
694 object *res; |
|
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
695 int outcome; |
|
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
696 |
|
3987
6fbc5c665812
Optimizations by Sjoerd:
Guido van Rossum <guido@python.org>
parents:
3769
diff
changeset
|
697 if (lenstr == NULL) |
|
6fbc5c665812
Optimizations by Sjoerd:
Guido van Rossum <guido@python.org>
parents:
3769
diff
changeset
|
698 lenstr = newstringobject("__len__"); |
|
6fbc5c665812
Optimizations by Sjoerd:
Guido van Rossum <guido@python.org>
parents:
3769
diff
changeset
|
699 func = instance_getattr(inst, lenstr); |
|
944
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
700 if (func == NULL) |
|
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
701 return -1; |
|
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
702 res = call_object(func, (object *)NULL); |
|
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
703 DECREF(func); |
|
1096
ce718457b172
* classobject.c: in instance_lenth, test result of call_object
Guido van Rossum <guido@python.org>
parents:
1051
diff
changeset
|
704 if (res == NULL) |
|
ce718457b172
* classobject.c: in instance_lenth, test result of call_object
Guido van Rossum <guido@python.org>
parents:
1051
diff
changeset
|
705 return -1; |
|
944
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
706 if (is_intobject(res)) { |
|
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
707 outcome = getintvalue(res); |
|
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
708 if (outcome < 0) |
|
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
709 err_setstr(ValueError, "__len__() should return >= 0"); |
|
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
710 } |
|
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
711 else { |
|
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
712 err_setstr(TypeError, "__len__() should return an int"); |
|
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
713 outcome = -1; |
|
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
714 } |
|
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
715 DECREF(res); |
|
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
716 return outcome; |
|
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
717 } |
|
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
718 |
|
1257
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1252
diff
changeset
|
719 static object * |
|
944
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
720 instance_subscript(inst, key) |
|
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
721 instanceobject *inst; |
|
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
722 object *key; |
|
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
723 { |
|
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
724 object *func; |
|
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
725 object *arg; |
|
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
726 object *res; |
|
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
727 |
|
3987
6fbc5c665812
Optimizations by Sjoerd:
Guido van Rossum <guido@python.org>
parents:
3769
diff
changeset
|
728 if (getitemstr == NULL) |
|
6fbc5c665812
Optimizations by Sjoerd:
Guido van Rossum <guido@python.org>
parents:
3769
diff
changeset
|
729 getitemstr = newstringobject("__getitem__"); |
|
6fbc5c665812
Optimizations by Sjoerd:
Guido van Rossum <guido@python.org>
parents:
3769
diff
changeset
|
730 func = instance_getattr(inst, getitemstr); |
|
944
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
731 if (func == NULL) |
|
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
732 return NULL; |
|
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
733 arg = mkvalue("(O)", key); |
|
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
734 if (arg == NULL) { |
|
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
735 DECREF(func); |
|
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
736 return NULL; |
|
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
737 } |
|
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
738 res = call_object(func, arg); |
|
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
739 DECREF(func); |
|
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
740 DECREF(arg); |
|
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
741 return res; |
|
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
742 } |
|
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
743 |
|
1257
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1252
diff
changeset
|
744 static int |
|
944
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
745 instance_ass_subscript(inst, key, value) |
|
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
746 instanceobject*inst; |
|
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
747 object *key; |
|
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
748 object *value; |
|
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
749 { |
|
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
750 object *func; |
|
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
751 object *arg; |
|
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
752 object *res; |
|
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
753 |
|
3987
6fbc5c665812
Optimizations by Sjoerd:
Guido van Rossum <guido@python.org>
parents:
3769
diff
changeset
|
754 if (value == NULL) { |
|
6fbc5c665812
Optimizations by Sjoerd:
Guido van Rossum <guido@python.org>
parents:
3769
diff
changeset
|
755 if (delitemstr == NULL) |
|
6fbc5c665812
Optimizations by Sjoerd:
Guido van Rossum <guido@python.org>
parents:
3769
diff
changeset
|
756 delitemstr = newstringobject("__delitem__"); |
|
6fbc5c665812
Optimizations by Sjoerd:
Guido van Rossum <guido@python.org>
parents:
3769
diff
changeset
|
757 func = instance_getattr(inst, delitemstr); |
|
6fbc5c665812
Optimizations by Sjoerd:
Guido van Rossum <guido@python.org>
parents:
3769
diff
changeset
|
758 } |
|
6fbc5c665812
Optimizations by Sjoerd:
Guido van Rossum <guido@python.org>
parents:
3769
diff
changeset
|
759 else { |
|
6fbc5c665812
Optimizations by Sjoerd:
Guido van Rossum <guido@python.org>
parents:
3769
diff
changeset
|
760 if (setitemstr == NULL) |
|
6fbc5c665812
Optimizations by Sjoerd:
Guido van Rossum <guido@python.org>
parents:
3769
diff
changeset
|
761 setitemstr = newstringobject("__setitem__"); |
|
6fbc5c665812
Optimizations by Sjoerd:
Guido van Rossum <guido@python.org>
parents:
3769
diff
changeset
|
762 func = instance_getattr(inst, setitemstr); |
|
6fbc5c665812
Optimizations by Sjoerd:
Guido van Rossum <guido@python.org>
parents:
3769
diff
changeset
|
763 } |
|
944
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
764 if (func == NULL) |
|
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
765 return -1; |
|
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
766 if (value == NULL) |
|
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
767 arg = mkvalue("(O)", key); |
|
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
768 else |
|
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
769 arg = mkvalue("(OO)", key, value); |
|
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
770 if (arg == NULL) { |
|
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
771 DECREF(func); |
|
956
a60cb5321211
* macmodule.c: include allobjects.h, not .c
Guido van Rossum <guido@python.org>
parents:
955
diff
changeset
|
772 return -1; |
|
944
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
773 } |
|
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
774 res = call_object(func, arg); |
|
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
775 DECREF(func); |
|
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
776 DECREF(arg); |
|
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
777 if (res == NULL) |
|
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
778 return -1; |
|
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
779 DECREF(res); |
|
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
780 return 0; |
|
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
781 } |
|
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
782 |
|
1257
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1252
diff
changeset
|
783 static mapping_methods instance_as_mapping = { |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1413
diff
changeset
|
784 (inquiry)instance_length, /*mp_length*/ |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1413
diff
changeset
|
785 (binaryfunc)instance_subscript, /*mp_subscript*/ |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1413
diff
changeset
|
786 (objobjargproc)instance_ass_subscript, /*mp_ass_subscript*/ |
|
944
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
787 }; |
|
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
788 |
|
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
789 static object * |
|
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
790 instance_item(inst, i) |
|
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
791 instanceobject *inst; |
|
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
792 int i; |
|
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
793 { |
|
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
794 object *func, *arg, *res; |
|
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
795 |
|
3987
6fbc5c665812
Optimizations by Sjoerd:
Guido van Rossum <guido@python.org>
parents:
3769
diff
changeset
|
796 if (getitemstr == NULL) |
|
6fbc5c665812
Optimizations by Sjoerd:
Guido van Rossum <guido@python.org>
parents:
3769
diff
changeset
|
797 getitemstr = newstringobject("__getitem__"); |
|
6fbc5c665812
Optimizations by Sjoerd:
Guido van Rossum <guido@python.org>
parents:
3769
diff
changeset
|
798 func = instance_getattr(inst, getitemstr); |
|
944
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
799 if (func == NULL) |
|
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
800 return NULL; |
|
2827
ff1faa4cc1df
args to call_object must be tuple or NULL
Guido van Rossum <guido@python.org>
parents:
2641
diff
changeset
|
801 arg = mkvalue("(i)", i); |
|
944
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
802 if (arg == NULL) { |
|
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
803 DECREF(func); |
|
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
804 return NULL; |
|
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
805 } |
|
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
806 res = call_object(func, arg); |
|
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
807 DECREF(func); |
|
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
808 DECREF(arg); |
|
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
809 return res; |
|
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
810 } |
|
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
811 |
|
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
812 static object * |
|
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
813 instance_slice(inst, i, j) |
|
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
814 instanceobject *inst; |
|
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
815 int i, j; |
|
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
816 { |
|
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
817 object *func, *arg, *res; |
|
3987
6fbc5c665812
Optimizations by Sjoerd:
Guido van Rossum <guido@python.org>
parents:
3769
diff
changeset
|
818 static object *getslicestr; |
|
944
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
819 |
|
3987
6fbc5c665812
Optimizations by Sjoerd:
Guido van Rossum <guido@python.org>
parents:
3769
diff
changeset
|
820 if (getslicestr == NULL) |
|
6fbc5c665812
Optimizations by Sjoerd:
Guido van Rossum <guido@python.org>
parents:
3769
diff
changeset
|
821 getslicestr = newstringobject("__getslice__"); |
|
6fbc5c665812
Optimizations by Sjoerd:
Guido van Rossum <guido@python.org>
parents:
3769
diff
changeset
|
822 func = instance_getattr(inst, getslicestr); |
|
944
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
823 if (func == NULL) |
|
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
824 return NULL; |
|
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
825 arg = mkvalue("(ii)", i, j); |
|
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
826 if (arg == NULL) { |
|
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
827 DECREF(func); |
|
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
828 return NULL; |
|
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
829 } |
|
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
830 res = call_object(func, arg); |
|
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
831 DECREF(func); |
|
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
832 DECREF(arg); |
|
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
833 return res; |
|
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
834 } |
|
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
835 |
|
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
836 static int |
|
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
837 instance_ass_item(inst, i, item) |
|
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
838 instanceobject *inst; |
|
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
839 int i; |
|
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
840 object *item; |
|
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
841 { |
|
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
842 object *func, *arg, *res; |
|
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
843 |
|
3987
6fbc5c665812
Optimizations by Sjoerd:
Guido van Rossum <guido@python.org>
parents:
3769
diff
changeset
|
844 if (item == NULL) { |
|
6fbc5c665812
Optimizations by Sjoerd:
Guido van Rossum <guido@python.org>
parents:
3769
diff
changeset
|
845 if (delitemstr == NULL) |
|
6fbc5c665812
Optimizations by Sjoerd:
Guido van Rossum <guido@python.org>
parents:
3769
diff
changeset
|
846 delitemstr = newstringobject("__delitem__"); |
|
6fbc5c665812
Optimizations by Sjoerd:
Guido van Rossum <guido@python.org>
parents:
3769
diff
changeset
|
847 func = instance_getattr(inst, delitemstr); |
|
6fbc5c665812
Optimizations by Sjoerd:
Guido van Rossum <guido@python.org>
parents:
3769
diff
changeset
|
848 } |
|
6fbc5c665812
Optimizations by Sjoerd:
Guido van Rossum <guido@python.org>
parents:
3769
diff
changeset
|
849 else { |
|
6fbc5c665812
Optimizations by Sjoerd:
Guido van Rossum <guido@python.org>
parents:
3769
diff
changeset
|
850 if (setitemstr == NULL) |
|
6fbc5c665812
Optimizations by Sjoerd:
Guido van Rossum <guido@python.org>
parents:
3769
diff
changeset
|
851 setitemstr = newstringobject("__setitem__"); |
|
6fbc5c665812
Optimizations by Sjoerd:
Guido van Rossum <guido@python.org>
parents:
3769
diff
changeset
|
852 func = instance_getattr(inst, setitemstr); |
|
6fbc5c665812
Optimizations by Sjoerd:
Guido van Rossum <guido@python.org>
parents:
3769
diff
changeset
|
853 } |
|
944
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
854 if (func == NULL) |
|
956
a60cb5321211
* macmodule.c: include allobjects.h, not .c
Guido van Rossum <guido@python.org>
parents:
955
diff
changeset
|
855 return -1; |
|
944
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
856 if (item == NULL) |
|
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
857 arg = mkvalue("i", i); |
|
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
858 else |
|
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
859 arg = mkvalue("(iO)", i, item); |
|
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
860 if (arg == NULL) { |
|
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
861 DECREF(func); |
|
956
a60cb5321211
* macmodule.c: include allobjects.h, not .c
Guido van Rossum <guido@python.org>
parents:
955
diff
changeset
|
862 return -1; |
|
944
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
863 } |
|
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
864 res = call_object(func, arg); |
|
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
865 DECREF(func); |
|
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
866 DECREF(arg); |
|
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
867 if (res == NULL) |
|
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
868 return -1; |
|
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
869 DECREF(res); |
|
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
870 return 0; |
|
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
871 } |
|
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
872 |
|
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
873 static int |
|
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
874 instance_ass_slice(inst, i, j, value) |
|
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
875 instanceobject *inst; |
|
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
876 int i, j; |
|
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
877 object *value; |
|
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
878 { |
|
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
879 object *func, *arg, *res; |
|
3987
6fbc5c665812
Optimizations by Sjoerd:
Guido van Rossum <guido@python.org>
parents:
3769
diff
changeset
|
880 static object *setslicestr, *delslicestr; |
|
944
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
881 |
|
3987
6fbc5c665812
Optimizations by Sjoerd:
Guido van Rossum <guido@python.org>
parents:
3769
diff
changeset
|
882 if (value == NULL) { |
|
6fbc5c665812
Optimizations by Sjoerd:
Guido van Rossum <guido@python.org>
parents:
3769
diff
changeset
|
883 if (delslicestr == NULL) |
|
6fbc5c665812
Optimizations by Sjoerd:
Guido van Rossum <guido@python.org>
parents:
3769
diff
changeset
|
884 delslicestr = newstringobject("__delslice__"); |
|
6fbc5c665812
Optimizations by Sjoerd:
Guido van Rossum <guido@python.org>
parents:
3769
diff
changeset
|
885 func = instance_getattr(inst, delslicestr); |
|
6fbc5c665812
Optimizations by Sjoerd:
Guido van Rossum <guido@python.org>
parents:
3769
diff
changeset
|
886 } |
|
6fbc5c665812
Optimizations by Sjoerd:
Guido van Rossum <guido@python.org>
parents:
3769
diff
changeset
|
887 else { |
|
6fbc5c665812
Optimizations by Sjoerd:
Guido van Rossum <guido@python.org>
parents:
3769
diff
changeset
|
888 if (setslicestr == NULL) |
|
6fbc5c665812
Optimizations by Sjoerd:
Guido van Rossum <guido@python.org>
parents:
3769
diff
changeset
|
889 setslicestr = newstringobject("__setslice__"); |
|
6fbc5c665812
Optimizations by Sjoerd:
Guido van Rossum <guido@python.org>
parents:
3769
diff
changeset
|
890 func = instance_getattr(inst, setslicestr); |
|
6fbc5c665812
Optimizations by Sjoerd:
Guido van Rossum <guido@python.org>
parents:
3769
diff
changeset
|
891 } |
|
944
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
892 if (func == NULL) |
|
956
a60cb5321211
* macmodule.c: include allobjects.h, not .c
Guido van Rossum <guido@python.org>
parents:
955
diff
changeset
|
893 return -1; |
|
944
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
894 if (value == NULL) |
|
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
895 arg = mkvalue("(ii)", i, j); |
|
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
896 else |
|
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
897 arg = mkvalue("(iiO)", i, j, value); |
|
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
898 if (arg == NULL) { |
|
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
899 DECREF(func); |
|
956
a60cb5321211
* macmodule.c: include allobjects.h, not .c
Guido van Rossum <guido@python.org>
parents:
955
diff
changeset
|
900 return -1; |
|
944
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
901 } |
|
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
902 res = call_object(func, arg); |
|
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
903 DECREF(func); |
|
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
904 DECREF(arg); |
|
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
905 if (res == NULL) |
|
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
906 return -1; |
|
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
907 DECREF(res); |
|
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
908 return 0; |
|
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
909 } |
|
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
910 |
|
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
911 static sequence_methods instance_as_sequence = { |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1413
diff
changeset
|
912 (inquiry)instance_length, /*sq_length*/ |
|
1908
0437738279a8
* Include/classobject.h, Objects/classobject.c, Python/ceval.c:
Guido van Rossum <guido@python.org>
parents:
1861
diff
changeset
|
913 0, /*sq_concat*/ |
|
0437738279a8
* Include/classobject.h, Objects/classobject.c, Python/ceval.c:
Guido van Rossum <guido@python.org>
parents:
1861
diff
changeset
|
914 0, /*sq_repeat*/ |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1413
diff
changeset
|
915 (intargfunc)instance_item, /*sq_item*/ |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1413
diff
changeset
|
916 (intintargfunc)instance_slice, /*sq_slice*/ |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1413
diff
changeset
|
917 (intobjargproc)instance_ass_item, /*sq_ass_item*/ |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1413
diff
changeset
|
918 (intintobjargproc)instance_ass_slice, /*sq_ass_slice*/ |
|
944
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
919 }; |
|
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
920 |
|
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
921 static object * |
|
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
922 generic_unary_op(self, methodname) |
|
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
923 instanceobject *self; |
|
3987
6fbc5c665812
Optimizations by Sjoerd:
Guido van Rossum <guido@python.org>
parents:
3769
diff
changeset
|
924 object *methodname; |
|
944
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
925 { |
|
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
926 object *func, *res; |
|
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
927 |
|
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
928 if ((func = instance_getattr(self, methodname)) == NULL) |
|
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
929 return NULL; |
|
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
930 res = call_object(func, (object *)NULL); |
|
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
931 DECREF(func); |
|
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
932 return res; |
|
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
933 } |
|
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
934 |
|
1908
0437738279a8
* Include/classobject.h, Objects/classobject.c, Python/ceval.c:
Guido van Rossum <guido@python.org>
parents:
1861
diff
changeset
|
935 |
|
0437738279a8
* Include/classobject.h, Objects/classobject.c, Python/ceval.c:
Guido van Rossum <guido@python.org>
parents:
1861
diff
changeset
|
936 /* Forward */ |
|
3987
6fbc5c665812
Optimizations by Sjoerd:
Guido van Rossum <guido@python.org>
parents:
3769
diff
changeset
|
937 static int halfbinop Py_PROTO((object *, object *, char *, object **, |
|
6fbc5c665812
Optimizations by Sjoerd:
Guido van Rossum <guido@python.org>
parents:
3769
diff
changeset
|
938 object * (*) Py_PROTO((object *, object *)), int )); |
|
1908
0437738279a8
* Include/classobject.h, Objects/classobject.c, Python/ceval.c:
Guido van Rossum <guido@python.org>
parents:
1861
diff
changeset
|
939 |
|
0437738279a8
* Include/classobject.h, Objects/classobject.c, Python/ceval.c:
Guido van Rossum <guido@python.org>
parents:
1861
diff
changeset
|
940 |
|
0437738279a8
* Include/classobject.h, Objects/classobject.c, Python/ceval.c:
Guido van Rossum <guido@python.org>
parents:
1861
diff
changeset
|
941 /* Implement a binary operator involving at least one class instance. */ |
|
0437738279a8
* Include/classobject.h, Objects/classobject.c, Python/ceval.c:
Guido van Rossum <guido@python.org>
parents:
1861
diff
changeset
|
942 |
|
0437738279a8
* Include/classobject.h, Objects/classobject.c, Python/ceval.c:
Guido van Rossum <guido@python.org>
parents:
1861
diff
changeset
|
943 object * |
|
2084
abbf2a4b488b
* Objects/classobject.c: added 5th (function) parameter to
Guido van Rossum <guido@python.org>
parents:
2059
diff
changeset
|
944 instancebinop(v, w, opname, ropname, thisfunc) |
|
1908
0437738279a8
* Include/classobject.h, Objects/classobject.c, Python/ceval.c:
Guido van Rossum <guido@python.org>
parents:
1861
diff
changeset
|
945 object *v; |
|
0437738279a8
* Include/classobject.h, Objects/classobject.c, Python/ceval.c:
Guido van Rossum <guido@python.org>
parents:
1861
diff
changeset
|
946 object *w; |
|
0437738279a8
* Include/classobject.h, Objects/classobject.c, Python/ceval.c:
Guido van Rossum <guido@python.org>
parents:
1861
diff
changeset
|
947 char *opname; |
|
0437738279a8
* Include/classobject.h, Objects/classobject.c, Python/ceval.c:
Guido van Rossum <guido@python.org>
parents:
1861
diff
changeset
|
948 char *ropname; |
|
2084
abbf2a4b488b
* Objects/classobject.c: added 5th (function) parameter to
Guido van Rossum <guido@python.org>
parents:
2059
diff
changeset
|
949 object * (*thisfunc) PROTO((object *, object *)); |
|
1908
0437738279a8
* Include/classobject.h, Objects/classobject.c, Python/ceval.c:
Guido van Rossum <guido@python.org>
parents:
1861
diff
changeset
|
950 { |
|
0437738279a8
* Include/classobject.h, Objects/classobject.c, Python/ceval.c:
Guido van Rossum <guido@python.org>
parents:
1861
diff
changeset
|
951 char buf[256]; |
|
0437738279a8
* Include/classobject.h, Objects/classobject.c, Python/ceval.c:
Guido van Rossum <guido@python.org>
parents:
1861
diff
changeset
|
952 object *result = NULL; |
|
2084
abbf2a4b488b
* Objects/classobject.c: added 5th (function) parameter to
Guido van Rossum <guido@python.org>
parents:
2059
diff
changeset
|
953 if (halfbinop(v, w, opname, &result, thisfunc, 0) <= 0) |
|
1908
0437738279a8
* Include/classobject.h, Objects/classobject.c, Python/ceval.c:
Guido van Rossum <guido@python.org>
parents:
1861
diff
changeset
|
954 return result; |
|
2084
abbf2a4b488b
* Objects/classobject.c: added 5th (function) parameter to
Guido van Rossum <guido@python.org>
parents:
2059
diff
changeset
|
955 if (halfbinop(w, v, ropname, &result, thisfunc, 1) <= 0) |
|
1908
0437738279a8
* Include/classobject.h, Objects/classobject.c, Python/ceval.c:
Guido van Rossum <guido@python.org>
parents:
1861
diff
changeset
|
956 return result; |
|
0437738279a8
* Include/classobject.h, Objects/classobject.c, Python/ceval.c:
Guido van Rossum <guido@python.org>
parents:
1861
diff
changeset
|
957 sprintf(buf, "%s nor %s defined for these operands", opname, ropname); |
|
0437738279a8
* Include/classobject.h, Objects/classobject.c, Python/ceval.c:
Guido van Rossum <guido@python.org>
parents:
1861
diff
changeset
|
958 err_setstr(TypeError, buf); |
|
0437738279a8
* Include/classobject.h, Objects/classobject.c, Python/ceval.c:
Guido van Rossum <guido@python.org>
parents:
1861
diff
changeset
|
959 return NULL; |
|
944
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
960 } |
|
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
961 |
|
1908
0437738279a8
* Include/classobject.h, Objects/classobject.c, Python/ceval.c:
Guido van Rossum <guido@python.org>
parents:
1861
diff
changeset
|
962 |
|
0437738279a8
* Include/classobject.h, Objects/classobject.c, Python/ceval.c:
Guido van Rossum <guido@python.org>
parents:
1861
diff
changeset
|
963 /* Try one half of a binary operator involving a class instance. |
|
0437738279a8
* Include/classobject.h, Objects/classobject.c, Python/ceval.c:
Guido van Rossum <guido@python.org>
parents:
1861
diff
changeset
|
964 Return value: |
|
0437738279a8
* Include/classobject.h, Objects/classobject.c, Python/ceval.c:
Guido van Rossum <guido@python.org>
parents:
1861
diff
changeset
|
965 -1 if an exception is to be reported right away |
|
0437738279a8
* Include/classobject.h, Objects/classobject.c, Python/ceval.c:
Guido van Rossum <guido@python.org>
parents:
1861
diff
changeset
|
966 0 if we have a valid result |
|
0437738279a8
* Include/classobject.h, Objects/classobject.c, Python/ceval.c:
Guido van Rossum <guido@python.org>
parents:
1861
diff
changeset
|
967 1 if we could try another operation |
|
0437738279a8
* Include/classobject.h, Objects/classobject.c, Python/ceval.c:
Guido van Rossum <guido@python.org>
parents:
1861
diff
changeset
|
968 */ |
|
0437738279a8
* Include/classobject.h, Objects/classobject.c, Python/ceval.c:
Guido van Rossum <guido@python.org>
parents:
1861
diff
changeset
|
969 |
|
3987
6fbc5c665812
Optimizations by Sjoerd:
Guido van Rossum <guido@python.org>
parents:
3769
diff
changeset
|
970 static object *coerce_obj; |
|
6fbc5c665812
Optimizations by Sjoerd:
Guido van Rossum <guido@python.org>
parents:
3769
diff
changeset
|
971 |
|
1908
0437738279a8
* Include/classobject.h, Objects/classobject.c, Python/ceval.c:
Guido van Rossum <guido@python.org>
parents:
1861
diff
changeset
|
972 static int |
|
2084
abbf2a4b488b
* Objects/classobject.c: added 5th (function) parameter to
Guido van Rossum <guido@python.org>
parents:
2059
diff
changeset
|
973 halfbinop(v, w, opname, r_result, thisfunc, swapped) |
|
1908
0437738279a8
* Include/classobject.h, Objects/classobject.c, Python/ceval.c:
Guido van Rossum <guido@python.org>
parents:
1861
diff
changeset
|
974 object *v; |
|
0437738279a8
* Include/classobject.h, Objects/classobject.c, Python/ceval.c:
Guido van Rossum <guido@python.org>
parents:
1861
diff
changeset
|
975 object *w; |
|
0437738279a8
* Include/classobject.h, Objects/classobject.c, Python/ceval.c:
Guido van Rossum <guido@python.org>
parents:
1861
diff
changeset
|
976 char *opname; |
|
0437738279a8
* Include/classobject.h, Objects/classobject.c, Python/ceval.c:
Guido van Rossum <guido@python.org>
parents:
1861
diff
changeset
|
977 object **r_result; |
|
2084
abbf2a4b488b
* Objects/classobject.c: added 5th (function) parameter to
Guido van Rossum <guido@python.org>
parents:
2059
diff
changeset
|
978 object * (*thisfunc) PROTO((object *, object *)); |
|
abbf2a4b488b
* Objects/classobject.c: added 5th (function) parameter to
Guido van Rossum <guido@python.org>
parents:
2059
diff
changeset
|
979 int swapped; |
|
1908
0437738279a8
* Include/classobject.h, Objects/classobject.c, Python/ceval.c:
Guido van Rossum <guido@python.org>
parents:
1861
diff
changeset
|
980 { |
|
0437738279a8
* Include/classobject.h, Objects/classobject.c, Python/ceval.c:
Guido van Rossum <guido@python.org>
parents:
1861
diff
changeset
|
981 object *func; |
|
0437738279a8
* Include/classobject.h, Objects/classobject.c, Python/ceval.c:
Guido van Rossum <guido@python.org>
parents:
1861
diff
changeset
|
982 object *args; |
|
0437738279a8
* Include/classobject.h, Objects/classobject.c, Python/ceval.c:
Guido van Rossum <guido@python.org>
parents:
1861
diff
changeset
|
983 object *coerce; |
|
0437738279a8
* Include/classobject.h, Objects/classobject.c, Python/ceval.c:
Guido van Rossum <guido@python.org>
parents:
1861
diff
changeset
|
984 object *coerced = NULL; |
|
0437738279a8
* Include/classobject.h, Objects/classobject.c, Python/ceval.c:
Guido van Rossum <guido@python.org>
parents:
1861
diff
changeset
|
985 object *v1; |
|
0437738279a8
* Include/classobject.h, Objects/classobject.c, Python/ceval.c:
Guido van Rossum <guido@python.org>
parents:
1861
diff
changeset
|
986 |
|
0437738279a8
* Include/classobject.h, Objects/classobject.c, Python/ceval.c:
Guido van Rossum <guido@python.org>
parents:
1861
diff
changeset
|
987 if (!is_instanceobject(v)) |
|
0437738279a8
* Include/classobject.h, Objects/classobject.c, Python/ceval.c:
Guido van Rossum <guido@python.org>
parents:
1861
diff
changeset
|
988 return 1; |
|
3987
6fbc5c665812
Optimizations by Sjoerd:
Guido van Rossum <guido@python.org>
parents:
3769
diff
changeset
|
989 if (coerce_obj == NULL) { |
|
6fbc5c665812
Optimizations by Sjoerd:
Guido van Rossum <guido@python.org>
parents:
3769
diff
changeset
|
990 coerce_obj = newstringobject("__coerce__"); |
|
6fbc5c665812
Optimizations by Sjoerd:
Guido van Rossum <guido@python.org>
parents:
3769
diff
changeset
|
991 if (coerce_obj == NULL) |
|
6fbc5c665812
Optimizations by Sjoerd:
Guido van Rossum <guido@python.org>
parents:
3769
diff
changeset
|
992 return -1; |
|
6fbc5c665812
Optimizations by Sjoerd:
Guido van Rossum <guido@python.org>
parents:
3769
diff
changeset
|
993 } |
|
6fbc5c665812
Optimizations by Sjoerd:
Guido van Rossum <guido@python.org>
parents:
3769
diff
changeset
|
994 coerce = getattro(v, coerce_obj); |
|
1908
0437738279a8
* Include/classobject.h, Objects/classobject.c, Python/ceval.c:
Guido van Rossum <guido@python.org>
parents:
1861
diff
changeset
|
995 if (coerce == NULL) { |
|
0437738279a8
* Include/classobject.h, Objects/classobject.c, Python/ceval.c:
Guido van Rossum <guido@python.org>
parents:
1861
diff
changeset
|
996 err_clear(); |
|
0437738279a8
* Include/classobject.h, Objects/classobject.c, Python/ceval.c:
Guido van Rossum <guido@python.org>
parents:
1861
diff
changeset
|
997 } |
|
0437738279a8
* Include/classobject.h, Objects/classobject.c, Python/ceval.c:
Guido van Rossum <guido@python.org>
parents:
1861
diff
changeset
|
998 else { |
|
0437738279a8
* Include/classobject.h, Objects/classobject.c, Python/ceval.c:
Guido van Rossum <guido@python.org>
parents:
1861
diff
changeset
|
999 args = mkvalue("(O)", w); |
|
0437738279a8
* Include/classobject.h, Objects/classobject.c, Python/ceval.c:
Guido van Rossum <guido@python.org>
parents:
1861
diff
changeset
|
1000 if (args == NULL) { |
|
0437738279a8
* Include/classobject.h, Objects/classobject.c, Python/ceval.c:
Guido van Rossum <guido@python.org>
parents:
1861
diff
changeset
|
1001 return -1; |
|
0437738279a8
* Include/classobject.h, Objects/classobject.c, Python/ceval.c:
Guido van Rossum <guido@python.org>
parents:
1861
diff
changeset
|
1002 } |
|
0437738279a8
* Include/classobject.h, Objects/classobject.c, Python/ceval.c:
Guido van Rossum <guido@python.org>
parents:
1861
diff
changeset
|
1003 coerced = call_object(coerce, args); |
|
0437738279a8
* Include/classobject.h, Objects/classobject.c, Python/ceval.c:
Guido van Rossum <guido@python.org>
parents:
1861
diff
changeset
|
1004 DECREF(args); |
|
0437738279a8
* Include/classobject.h, Objects/classobject.c, Python/ceval.c:
Guido van Rossum <guido@python.org>
parents:
1861
diff
changeset
|
1005 DECREF(coerce); |
|
0437738279a8
* Include/classobject.h, Objects/classobject.c, Python/ceval.c:
Guido van Rossum <guido@python.org>
parents:
1861
diff
changeset
|
1006 if (coerced == NULL) { |
|
0437738279a8
* Include/classobject.h, Objects/classobject.c, Python/ceval.c:
Guido van Rossum <guido@python.org>
parents:
1861
diff
changeset
|
1007 return -1; |
|
0437738279a8
* Include/classobject.h, Objects/classobject.c, Python/ceval.c:
Guido van Rossum <guido@python.org>
parents:
1861
diff
changeset
|
1008 } |
|
0437738279a8
* Include/classobject.h, Objects/classobject.c, Python/ceval.c:
Guido van Rossum <guido@python.org>
parents:
1861
diff
changeset
|
1009 if (coerced == None) { |
|
0437738279a8
* Include/classobject.h, Objects/classobject.c, Python/ceval.c:
Guido van Rossum <guido@python.org>
parents:
1861
diff
changeset
|
1010 DECREF(coerced); |
|
0437738279a8
* Include/classobject.h, Objects/classobject.c, Python/ceval.c:
Guido van Rossum <guido@python.org>
parents:
1861
diff
changeset
|
1011 return 1; |
|
0437738279a8
* Include/classobject.h, Objects/classobject.c, Python/ceval.c:
Guido van Rossum <guido@python.org>
parents:
1861
diff
changeset
|
1012 } |
|
0437738279a8
* Include/classobject.h, Objects/classobject.c, Python/ceval.c:
Guido van Rossum <guido@python.org>
parents:
1861
diff
changeset
|
1013 if (!is_tupleobject(coerced) || gettuplesize(coerced) != 2) { |
|
0437738279a8
* Include/classobject.h, Objects/classobject.c, Python/ceval.c:
Guido van Rossum <guido@python.org>
parents:
1861
diff
changeset
|
1014 DECREF(coerced); |
|
2084
abbf2a4b488b
* Objects/classobject.c: added 5th (function) parameter to
Guido van Rossum <guido@python.org>
parents:
2059
diff
changeset
|
1015 err_setstr(TypeError, |
|
abbf2a4b488b
* Objects/classobject.c: added 5th (function) parameter to
Guido van Rossum <guido@python.org>
parents:
2059
diff
changeset
|
1016 "coercion should return None or 2-tuple"); |
|
1908
0437738279a8
* Include/classobject.h, Objects/classobject.c, Python/ceval.c:
Guido van Rossum <guido@python.org>
parents:
1861
diff
changeset
|
1017 return -1; |
|
0437738279a8
* Include/classobject.h, Objects/classobject.c, Python/ceval.c:
Guido van Rossum <guido@python.org>
parents:
1861
diff
changeset
|
1018 } |
|
0437738279a8
* Include/classobject.h, Objects/classobject.c, Python/ceval.c:
Guido van Rossum <guido@python.org>
parents:
1861
diff
changeset
|
1019 v1 = gettupleitem(coerced, 0); |
|
2084
abbf2a4b488b
* Objects/classobject.c: added 5th (function) parameter to
Guido van Rossum <guido@python.org>
parents:
2059
diff
changeset
|
1020 w = gettupleitem(coerced, 1); |
|
1908
0437738279a8
* Include/classobject.h, Objects/classobject.c, Python/ceval.c:
Guido van Rossum <guido@python.org>
parents:
1861
diff
changeset
|
1021 if (v1 != v) { |
|
0437738279a8
* Include/classobject.h, Objects/classobject.c, Python/ceval.c:
Guido van Rossum <guido@python.org>
parents:
1861
diff
changeset
|
1022 v = v1; |
|
2084
abbf2a4b488b
* Objects/classobject.c: added 5th (function) parameter to
Guido van Rossum <guido@python.org>
parents:
2059
diff
changeset
|
1023 if (!is_instanceobject(v) && !is_instanceobject(w)) { |
|
abbf2a4b488b
* Objects/classobject.c: added 5th (function) parameter to
Guido van Rossum <guido@python.org>
parents:
2059
diff
changeset
|
1024 if (swapped) |
|
abbf2a4b488b
* Objects/classobject.c: added 5th (function) parameter to
Guido van Rossum <guido@python.org>
parents:
2059
diff
changeset
|
1025 *r_result = (*thisfunc)(w, v); |
|
abbf2a4b488b
* Objects/classobject.c: added 5th (function) parameter to
Guido van Rossum <guido@python.org>
parents:
2059
diff
changeset
|
1026 else |
|
abbf2a4b488b
* Objects/classobject.c: added 5th (function) parameter to
Guido van Rossum <guido@python.org>
parents:
2059
diff
changeset
|
1027 *r_result = (*thisfunc)(v, w); |
|
abbf2a4b488b
* Objects/classobject.c: added 5th (function) parameter to
Guido van Rossum <guido@python.org>
parents:
2059
diff
changeset
|
1028 DECREF(coerced); |
|
abbf2a4b488b
* Objects/classobject.c: added 5th (function) parameter to
Guido van Rossum <guido@python.org>
parents:
2059
diff
changeset
|
1029 return *r_result == NULL ? -1 : 0; |
|
1908
0437738279a8
* Include/classobject.h, Objects/classobject.c, Python/ceval.c:
Guido van Rossum <guido@python.org>
parents:
1861
diff
changeset
|
1030 } |
|
0437738279a8
* Include/classobject.h, Objects/classobject.c, Python/ceval.c:
Guido van Rossum <guido@python.org>
parents:
1861
diff
changeset
|
1031 } |
|
0437738279a8
* Include/classobject.h, Objects/classobject.c, Python/ceval.c:
Guido van Rossum <guido@python.org>
parents:
1861
diff
changeset
|
1032 w = gettupleitem(coerced, 1); |
|
0437738279a8
* Include/classobject.h, Objects/classobject.c, Python/ceval.c:
Guido van Rossum <guido@python.org>
parents:
1861
diff
changeset
|
1033 } |
|
2084
abbf2a4b488b
* Objects/classobject.c: added 5th (function) parameter to
Guido van Rossum <guido@python.org>
parents:
2059
diff
changeset
|
1034 func = getattr(v, opname); |
|
abbf2a4b488b
* Objects/classobject.c: added 5th (function) parameter to
Guido van Rossum <guido@python.org>
parents:
2059
diff
changeset
|
1035 if (func == NULL) { |
|
abbf2a4b488b
* Objects/classobject.c: added 5th (function) parameter to
Guido van Rossum <guido@python.org>
parents:
2059
diff
changeset
|
1036 XDECREF(coerced); |
|
abbf2a4b488b
* Objects/classobject.c: added 5th (function) parameter to
Guido van Rossum <guido@python.org>
parents:
2059
diff
changeset
|
1037 if (err_occurred() != AttributeError) |
|
abbf2a4b488b
* Objects/classobject.c: added 5th (function) parameter to
Guido van Rossum <guido@python.org>
parents:
2059
diff
changeset
|
1038 return -1; |
|
abbf2a4b488b
* Objects/classobject.c: added 5th (function) parameter to
Guido van Rossum <guido@python.org>
parents:
2059
diff
changeset
|
1039 err_clear(); |
|
abbf2a4b488b
* Objects/classobject.c: added 5th (function) parameter to
Guido van Rossum <guido@python.org>
parents:
2059
diff
changeset
|
1040 return 1; |
|
abbf2a4b488b
* Objects/classobject.c: added 5th (function) parameter to
Guido van Rossum <guido@python.org>
parents:
2059
diff
changeset
|
1041 } |
|
1908
0437738279a8
* Include/classobject.h, Objects/classobject.c, Python/ceval.c:
Guido van Rossum <guido@python.org>
parents:
1861
diff
changeset
|
1042 args = mkvalue("(O)", w); |
|
0437738279a8
* Include/classobject.h, Objects/classobject.c, Python/ceval.c:
Guido van Rossum <guido@python.org>
parents:
1861
diff
changeset
|
1043 if (args == NULL) { |
|
0437738279a8
* Include/classobject.h, Objects/classobject.c, Python/ceval.c:
Guido van Rossum <guido@python.org>
parents:
1861
diff
changeset
|
1044 DECREF(func); |
|
0437738279a8
* Include/classobject.h, Objects/classobject.c, Python/ceval.c:
Guido van Rossum <guido@python.org>
parents:
1861
diff
changeset
|
1045 XDECREF(coerced); |
|
0437738279a8
* Include/classobject.h, Objects/classobject.c, Python/ceval.c:
Guido van Rossum <guido@python.org>
parents:
1861
diff
changeset
|
1046 return -1; |
|
0437738279a8
* Include/classobject.h, Objects/classobject.c, Python/ceval.c:
Guido van Rossum <guido@python.org>
parents:
1861
diff
changeset
|
1047 } |
|
0437738279a8
* Include/classobject.h, Objects/classobject.c, Python/ceval.c:
Guido van Rossum <guido@python.org>
parents:
1861
diff
changeset
|
1048 *r_result = call_object(func, args); |
|
0437738279a8
* Include/classobject.h, Objects/classobject.c, Python/ceval.c:
Guido van Rossum <guido@python.org>
parents:
1861
diff
changeset
|
1049 DECREF(args); |
|
0437738279a8
* Include/classobject.h, Objects/classobject.c, Python/ceval.c:
Guido van Rossum <guido@python.org>
parents:
1861
diff
changeset
|
1050 DECREF(func); |
|
0437738279a8
* Include/classobject.h, Objects/classobject.c, Python/ceval.c:
Guido van Rossum <guido@python.org>
parents:
1861
diff
changeset
|
1051 XDECREF(coerced); |
|
0437738279a8
* Include/classobject.h, Objects/classobject.c, Python/ceval.c:
Guido van Rossum <guido@python.org>
parents:
1861
diff
changeset
|
1052 return *r_result == NULL ? -1 : 0; |
|
0437738279a8
* Include/classobject.h, Objects/classobject.c, Python/ceval.c:
Guido van Rossum <guido@python.org>
parents:
1861
diff
changeset
|
1053 } |
|
0437738279a8
* Include/classobject.h, Objects/classobject.c, Python/ceval.c:
Guido van Rossum <guido@python.org>
parents:
1861
diff
changeset
|
1054 |
|
2112
2669ef14c205
implement coercions involving instances properly
Guido van Rossum <guido@python.org>
parents:
2105
diff
changeset
|
1055 static int |
|
2669ef14c205
implement coercions involving instances properly
Guido van Rossum <guido@python.org>
parents:
2105
diff
changeset
|
1056 instance_coerce(pv, pw) |
|
2669ef14c205
implement coercions involving instances properly
Guido van Rossum <guido@python.org>
parents:
2105
diff
changeset
|
1057 object **pv; |
|
2669ef14c205
implement coercions involving instances properly
Guido van Rossum <guido@python.org>
parents:
2105
diff
changeset
|
1058 object **pw; |
|
2669ef14c205
implement coercions involving instances properly
Guido van Rossum <guido@python.org>
parents:
2105
diff
changeset
|
1059 { |
|
2669ef14c205
implement coercions involving instances properly
Guido van Rossum <guido@python.org>
parents:
2105
diff
changeset
|
1060 object *v = *pv; |
|
2669ef14c205
implement coercions involving instances properly
Guido van Rossum <guido@python.org>
parents:
2105
diff
changeset
|
1061 object *w = *pw; |
|
2669ef14c205
implement coercions involving instances properly
Guido van Rossum <guido@python.org>
parents:
2105
diff
changeset
|
1062 object *coerce; |
|
2669ef14c205
implement coercions involving instances properly
Guido van Rossum <guido@python.org>
parents:
2105
diff
changeset
|
1063 object *args; |
|
2669ef14c205
implement coercions involving instances properly
Guido van Rossum <guido@python.org>
parents:
2105
diff
changeset
|
1064 object *coerced; |
|
2669ef14c205
implement coercions involving instances properly
Guido van Rossum <guido@python.org>
parents:
2105
diff
changeset
|
1065 |
|
3987
6fbc5c665812
Optimizations by Sjoerd:
Guido van Rossum <guido@python.org>
parents:
3769
diff
changeset
|
1066 if (coerce_obj == NULL) { |
|
6fbc5c665812
Optimizations by Sjoerd:
Guido van Rossum <guido@python.org>
parents:
3769
diff
changeset
|
1067 coerce_obj = newstringobject("__coerce__"); |
|
6fbc5c665812
Optimizations by Sjoerd:
Guido van Rossum <guido@python.org>
parents:
3769
diff
changeset
|
1068 if (coerce_obj == NULL) |
|
6fbc5c665812
Optimizations by Sjoerd:
Guido van Rossum <guido@python.org>
parents:
3769
diff
changeset
|
1069 return -1; |
|
6fbc5c665812
Optimizations by Sjoerd:
Guido van Rossum <guido@python.org>
parents:
3769
diff
changeset
|
1070 } |
|
6fbc5c665812
Optimizations by Sjoerd:
Guido van Rossum <guido@python.org>
parents:
3769
diff
changeset
|
1071 coerce = getattro(v, coerce_obj); |
|
2112
2669ef14c205
implement coercions involving instances properly
Guido van Rossum <guido@python.org>
parents:
2105
diff
changeset
|
1072 if (coerce == NULL) { |
|
2669ef14c205
implement coercions involving instances properly
Guido van Rossum <guido@python.org>
parents:
2105
diff
changeset
|
1073 /* No __coerce__ method: always OK */ |
|
2669ef14c205
implement coercions involving instances properly
Guido van Rossum <guido@python.org>
parents:
2105
diff
changeset
|
1074 err_clear(); |
|
2669ef14c205
implement coercions involving instances properly
Guido van Rossum <guido@python.org>
parents:
2105
diff
changeset
|
1075 INCREF(v); |
|
2669ef14c205
implement coercions involving instances properly
Guido van Rossum <guido@python.org>
parents:
2105
diff
changeset
|
1076 INCREF(w); |
|
2669ef14c205
implement coercions involving instances properly
Guido van Rossum <guido@python.org>
parents:
2105
diff
changeset
|
1077 return 0; |
|
2669ef14c205
implement coercions involving instances properly
Guido van Rossum <guido@python.org>
parents:
2105
diff
changeset
|
1078 } |
|
2669ef14c205
implement coercions involving instances properly
Guido van Rossum <guido@python.org>
parents:
2105
diff
changeset
|
1079 /* Has __coerce__ method: call it */ |
|
2669ef14c205
implement coercions involving instances properly
Guido van Rossum <guido@python.org>
parents:
2105
diff
changeset
|
1080 args = mkvalue("(O)", w); |
|
2669ef14c205
implement coercions involving instances properly
Guido van Rossum <guido@python.org>
parents:
2105
diff
changeset
|
1081 if (args == NULL) { |
|
2669ef14c205
implement coercions involving instances properly
Guido van Rossum <guido@python.org>
parents:
2105
diff
changeset
|
1082 return -1; |
|
2669ef14c205
implement coercions involving instances properly
Guido van Rossum <guido@python.org>
parents:
2105
diff
changeset
|
1083 } |
|
2669ef14c205
implement coercions involving instances properly
Guido van Rossum <guido@python.org>
parents:
2105
diff
changeset
|
1084 coerced = call_object(coerce, args); |
|
2669ef14c205
implement coercions involving instances properly
Guido van Rossum <guido@python.org>
parents:
2105
diff
changeset
|
1085 DECREF(args); |
|
2669ef14c205
implement coercions involving instances properly
Guido van Rossum <guido@python.org>
parents:
2105
diff
changeset
|
1086 DECREF(coerce); |
|
2669ef14c205
implement coercions involving instances properly
Guido van Rossum <guido@python.org>
parents:
2105
diff
changeset
|
1087 if (coerced == NULL) { |
|
2669ef14c205
implement coercions involving instances properly
Guido van Rossum <guido@python.org>
parents:
2105
diff
changeset
|
1088 /* __coerce__ call raised an exception */ |
|
2669ef14c205
implement coercions involving instances properly
Guido van Rossum <guido@python.org>
parents:
2105
diff
changeset
|
1089 return -1; |
|
2669ef14c205
implement coercions involving instances properly
Guido van Rossum <guido@python.org>
parents:
2105
diff
changeset
|
1090 } |
|
2669ef14c205
implement coercions involving instances properly
Guido van Rossum <guido@python.org>
parents:
2105
diff
changeset
|
1091 if (coerced == None) { |
|
2669ef14c205
implement coercions involving instances properly
Guido van Rossum <guido@python.org>
parents:
2105
diff
changeset
|
1092 /* __coerce__ says "I can't do it" */ |
|
2669ef14c205
implement coercions involving instances properly
Guido van Rossum <guido@python.org>
parents:
2105
diff
changeset
|
1093 DECREF(coerced); |
|
2669ef14c205
implement coercions involving instances properly
Guido van Rossum <guido@python.org>
parents:
2105
diff
changeset
|
1094 return 1; |
|
2669ef14c205
implement coercions involving instances properly
Guido van Rossum <guido@python.org>
parents:
2105
diff
changeset
|
1095 } |
|
2669ef14c205
implement coercions involving instances properly
Guido van Rossum <guido@python.org>
parents:
2105
diff
changeset
|
1096 if (!is_tupleobject(coerced) || gettuplesize(coerced) != 2) { |
|
2669ef14c205
implement coercions involving instances properly
Guido van Rossum <guido@python.org>
parents:
2105
diff
changeset
|
1097 /* __coerce__ return value is malformed */ |
|
2669ef14c205
implement coercions involving instances properly
Guido van Rossum <guido@python.org>
parents:
2105
diff
changeset
|
1098 DECREF(coerced); |
|
2669ef14c205
implement coercions involving instances properly
Guido van Rossum <guido@python.org>
parents:
2105
diff
changeset
|
1099 err_setstr(TypeError, |
|
2669ef14c205
implement coercions involving instances properly
Guido van Rossum <guido@python.org>
parents:
2105
diff
changeset
|
1100 "coercion should return None or 2-tuple"); |
|
2669ef14c205
implement coercions involving instances properly
Guido van Rossum <guido@python.org>
parents:
2105
diff
changeset
|
1101 return -1; |
|
2669ef14c205
implement coercions involving instances properly
Guido van Rossum <guido@python.org>
parents:
2105
diff
changeset
|
1102 } |
|
2669ef14c205
implement coercions involving instances properly
Guido van Rossum <guido@python.org>
parents:
2105
diff
changeset
|
1103 /* __coerce__ returned two new values */ |
|
2669ef14c205
implement coercions involving instances properly
Guido van Rossum <guido@python.org>
parents:
2105
diff
changeset
|
1104 *pv = gettupleitem(coerced, 0); |
|
2669ef14c205
implement coercions involving instances properly
Guido van Rossum <guido@python.org>
parents:
2105
diff
changeset
|
1105 *pw = gettupleitem(coerced, 1); |
|
2669ef14c205
implement coercions involving instances properly
Guido van Rossum <guido@python.org>
parents:
2105
diff
changeset
|
1106 INCREF(*pv); |
|
2669ef14c205
implement coercions involving instances properly
Guido van Rossum <guido@python.org>
parents:
2105
diff
changeset
|
1107 INCREF(*pw); |
|
2669ef14c205
implement coercions involving instances properly
Guido van Rossum <guido@python.org>
parents:
2105
diff
changeset
|
1108 DECREF(coerced); |
|
2669ef14c205
implement coercions involving instances properly
Guido van Rossum <guido@python.org>
parents:
2105
diff
changeset
|
1109 return 0; |
|
2669ef14c205
implement coercions involving instances properly
Guido van Rossum <guido@python.org>
parents:
2105
diff
changeset
|
1110 } |
|
2669ef14c205
implement coercions involving instances properly
Guido van Rossum <guido@python.org>
parents:
2105
diff
changeset
|
1111 |
|
1908
0437738279a8
* Include/classobject.h, Objects/classobject.c, Python/ceval.c:
Guido van Rossum <guido@python.org>
parents:
1861
diff
changeset
|
1112 |
|
944
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
1113 #define UNARY(funcname, methodname) \ |
|
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
1114 static object *funcname(self) instanceobject *self; { \ |
|
3987
6fbc5c665812
Optimizations by Sjoerd:
Guido van Rossum <guido@python.org>
parents:
3769
diff
changeset
|
1115 static object *o; \ |
|
6fbc5c665812
Optimizations by Sjoerd:
Guido van Rossum <guido@python.org>
parents:
3769
diff
changeset
|
1116 if (o == NULL) o = newstringobject(methodname); \ |
|
6fbc5c665812
Optimizations by Sjoerd:
Guido van Rossum <guido@python.org>
parents:
3769
diff
changeset
|
1117 return generic_unary_op(self, o); \ |
|
944
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
1118 } |
|
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
1119 |
|
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
1120 UNARY(instance_neg, "__neg__") |
|
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
1121 UNARY(instance_pos, "__pos__") |
|
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
1122 UNARY(instance_abs, "__abs__") |
|
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
1123 |
|
1257
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1252
diff
changeset
|
1124 static int |
|
944
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
1125 instance_nonzero(self) |
|
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
1126 instanceobject *self; |
|
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
1127 { |
|
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
1128 object *func, *res; |
|
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
1129 long outcome; |
|
3987
6fbc5c665812
Optimizations by Sjoerd:
Guido van Rossum <guido@python.org>
parents:
3769
diff
changeset
|
1130 static object *nonzerostr; |
|
944
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
1131 |
|
3987
6fbc5c665812
Optimizations by Sjoerd:
Guido van Rossum <guido@python.org>
parents:
3769
diff
changeset
|
1132 if (nonzerostr == NULL) |
|
6fbc5c665812
Optimizations by Sjoerd:
Guido van Rossum <guido@python.org>
parents:
3769
diff
changeset
|
1133 nonzerostr = newstringobject("__nonzero__"); |
|
6fbc5c665812
Optimizations by Sjoerd:
Guido van Rossum <guido@python.org>
parents:
3769
diff
changeset
|
1134 if ((func = instance_getattr(self, nonzerostr)) == NULL) { |
|
944
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
1135 err_clear(); |
|
3987
6fbc5c665812
Optimizations by Sjoerd:
Guido van Rossum <guido@python.org>
parents:
3769
diff
changeset
|
1136 if (lenstr == NULL) |
|
6fbc5c665812
Optimizations by Sjoerd:
Guido van Rossum <guido@python.org>
parents:
3769
diff
changeset
|
1137 lenstr = newstringobject("__len__"); |
|
6fbc5c665812
Optimizations by Sjoerd:
Guido van Rossum <guido@python.org>
parents:
3769
diff
changeset
|
1138 if ((func = instance_getattr(self, lenstr)) == NULL) { |
|
944
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
1139 err_clear(); |
|
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
1140 /* Fall back to the default behavior: |
|
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
1141 all instances are nonzero */ |
|
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
1142 return 1; |
|
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
1143 } |
|
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
1144 } |
|
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
1145 res = call_object(func, (object *)NULL); |
|
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
1146 DECREF(func); |
|
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
1147 if (res == NULL) |
|
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
1148 return -1; |
|
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
1149 if (!is_intobject(res)) { |
|
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
1150 DECREF(res); |
|
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
1151 err_setstr(TypeError, "__nonzero__ should return an int"); |
|
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
1152 return -1; |
|
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
1153 } |
|
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
1154 outcome = getintvalue(res); |
|
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
1155 DECREF(res); |
|
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
1156 if (outcome < 0) { |
|
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
1157 err_setstr(ValueError, "__nonzero__ should return >= 0"); |
|
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
1158 return -1; |
|
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
1159 } |
|
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
1160 return outcome > 0; |
|
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
1161 } |
|
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
1162 |
|
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
1163 UNARY(instance_invert, "__invert__") |
|
1049
2ed511171809
Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents:
1031
diff
changeset
|
1164 UNARY(instance_int, "__int__") |
|
2ed511171809
Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents:
1031
diff
changeset
|
1165 UNARY(instance_long, "__long__") |
|
2ed511171809
Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents:
1031
diff
changeset
|
1166 UNARY(instance_float, "__float__") |
|
2ed511171809
Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents:
1031
diff
changeset
|
1167 UNARY(instance_oct, "__oct__") |
|
2ed511171809
Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents:
1031
diff
changeset
|
1168 UNARY(instance_hex, "__hex__") |
|
2ed511171809
Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents:
1031
diff
changeset
|
1169 |
|
1908
0437738279a8
* Include/classobject.h, Objects/classobject.c, Python/ceval.c:
Guido van Rossum <guido@python.org>
parents:
1861
diff
changeset
|
1170 /* This version is for ternary calls only (z != None) */ |
|
0437738279a8
* Include/classobject.h, Objects/classobject.c, Python/ceval.c:
Guido van Rossum <guido@python.org>
parents:
1861
diff
changeset
|
1171 static object * |
|
0437738279a8
* Include/classobject.h, Objects/classobject.c, Python/ceval.c:
Guido van Rossum <guido@python.org>
parents:
1861
diff
changeset
|
1172 instance_pow(v, w, z) |
|
0437738279a8
* Include/classobject.h, Objects/classobject.c, Python/ceval.c:
Guido van Rossum <guido@python.org>
parents:
1861
diff
changeset
|
1173 object *v; |
|
0437738279a8
* Include/classobject.h, Objects/classobject.c, Python/ceval.c:
Guido van Rossum <guido@python.org>
parents:
1861
diff
changeset
|
1174 object *w; |
|
0437738279a8
* Include/classobject.h, Objects/classobject.c, Python/ceval.c:
Guido van Rossum <guido@python.org>
parents:
1861
diff
changeset
|
1175 object *z; |
|
0437738279a8
* Include/classobject.h, Objects/classobject.c, Python/ceval.c:
Guido van Rossum <guido@python.org>
parents:
1861
diff
changeset
|
1176 { |
|
0437738279a8
* Include/classobject.h, Objects/classobject.c, Python/ceval.c:
Guido van Rossum <guido@python.org>
parents:
1861
diff
changeset
|
1177 /* XXX Doesn't do coercions... */ |
|
0437738279a8
* Include/classobject.h, Objects/classobject.c, Python/ceval.c:
Guido van Rossum <guido@python.org>
parents:
1861
diff
changeset
|
1178 object *func; |
|
0437738279a8
* Include/classobject.h, Objects/classobject.c, Python/ceval.c:
Guido van Rossum <guido@python.org>
parents:
1861
diff
changeset
|
1179 object *args; |
|
0437738279a8
* Include/classobject.h, Objects/classobject.c, Python/ceval.c:
Guido van Rossum <guido@python.org>
parents:
1861
diff
changeset
|
1180 object *result; |
|
3987
6fbc5c665812
Optimizations by Sjoerd:
Guido van Rossum <guido@python.org>
parents:
3769
diff
changeset
|
1181 static object *powstr; |
|
6fbc5c665812
Optimizations by Sjoerd:
Guido van Rossum <guido@python.org>
parents:
3769
diff
changeset
|
1182 |
|
6fbc5c665812
Optimizations by Sjoerd:
Guido van Rossum <guido@python.org>
parents:
3769
diff
changeset
|
1183 if (powstr == NULL) |
|
6fbc5c665812
Optimizations by Sjoerd:
Guido van Rossum <guido@python.org>
parents:
3769
diff
changeset
|
1184 powstr = newstringobject("__pow__"); |
|
6fbc5c665812
Optimizations by Sjoerd:
Guido van Rossum <guido@python.org>
parents:
3769
diff
changeset
|
1185 func = getattro(v, powstr); |
|
1908
0437738279a8
* Include/classobject.h, Objects/classobject.c, Python/ceval.c:
Guido van Rossum <guido@python.org>
parents:
1861
diff
changeset
|
1186 if (func == NULL) |
|
0437738279a8
* Include/classobject.h, Objects/classobject.c, Python/ceval.c:
Guido van Rossum <guido@python.org>
parents:
1861
diff
changeset
|
1187 return NULL; |
|
0437738279a8
* Include/classobject.h, Objects/classobject.c, Python/ceval.c:
Guido van Rossum <guido@python.org>
parents:
1861
diff
changeset
|
1188 args = mkvalue("(OO)", w, z); |
|
0437738279a8
* Include/classobject.h, Objects/classobject.c, Python/ceval.c:
Guido van Rossum <guido@python.org>
parents:
1861
diff
changeset
|
1189 if (args == NULL) { |
|
0437738279a8
* Include/classobject.h, Objects/classobject.c, Python/ceval.c:
Guido van Rossum <guido@python.org>
parents:
1861
diff
changeset
|
1190 DECREF(func); |
|
0437738279a8
* Include/classobject.h, Objects/classobject.c, Python/ceval.c:
Guido van Rossum <guido@python.org>
parents:
1861
diff
changeset
|
1191 return NULL; |
|
0437738279a8
* Include/classobject.h, Objects/classobject.c, Python/ceval.c:
Guido van Rossum <guido@python.org>
parents:
1861
diff
changeset
|
1192 } |
|
0437738279a8
* Include/classobject.h, Objects/classobject.c, Python/ceval.c:
Guido van Rossum <guido@python.org>
parents:
1861
diff
changeset
|
1193 result = call_object(func, args); |
|
0437738279a8
* Include/classobject.h, Objects/classobject.c, Python/ceval.c:
Guido van Rossum <guido@python.org>
parents:
1861
diff
changeset
|
1194 DECREF(func); |
|
0437738279a8
* Include/classobject.h, Objects/classobject.c, Python/ceval.c:
Guido van Rossum <guido@python.org>
parents:
1861
diff
changeset
|
1195 DECREF(args); |
|
0437738279a8
* Include/classobject.h, Objects/classobject.c, Python/ceval.c:
Guido van Rossum <guido@python.org>
parents:
1861
diff
changeset
|
1196 return result; |
|
0437738279a8
* Include/classobject.h, Objects/classobject.c, Python/ceval.c:
Guido van Rossum <guido@python.org>
parents:
1861
diff
changeset
|
1197 } |
|
0437738279a8
* Include/classobject.h, Objects/classobject.c, Python/ceval.c:
Guido van Rossum <guido@python.org>
parents:
1861
diff
changeset
|
1198 |
|
944
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
1199 static number_methods instance_as_number = { |
|
1908
0437738279a8
* Include/classobject.h, Objects/classobject.c, Python/ceval.c:
Guido van Rossum <guido@python.org>
parents:
1861
diff
changeset
|
1200 0, /*nb_add*/ |
|
0437738279a8
* Include/classobject.h, Objects/classobject.c, Python/ceval.c:
Guido van Rossum <guido@python.org>
parents:
1861
diff
changeset
|
1201 0, /*nb_subtract*/ |
|
0437738279a8
* Include/classobject.h, Objects/classobject.c, Python/ceval.c:
Guido van Rossum <guido@python.org>
parents:
1861
diff
changeset
|
1202 0, /*nb_multiply*/ |
|
0437738279a8
* Include/classobject.h, Objects/classobject.c, Python/ceval.c:
Guido van Rossum <guido@python.org>
parents:
1861
diff
changeset
|
1203 0, /*nb_divide*/ |
|
0437738279a8
* Include/classobject.h, Objects/classobject.c, Python/ceval.c:
Guido van Rossum <guido@python.org>
parents:
1861
diff
changeset
|
1204 0, /*nb_remainder*/ |
|
0437738279a8
* Include/classobject.h, Objects/classobject.c, Python/ceval.c:
Guido van Rossum <guido@python.org>
parents:
1861
diff
changeset
|
1205 0, /*nb_divmod*/ |
|
1861
d405e3e8ffd3
Yet another version (by me) of __getattr__ etc.
Guido van Rossum <guido@python.org>
parents:
1860
diff
changeset
|
1206 (ternaryfunc)instance_pow, /*nb_power*/ |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1413
diff
changeset
|
1207 (unaryfunc)instance_neg, /*nb_negative*/ |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1413
diff
changeset
|
1208 (unaryfunc)instance_pos, /*nb_positive*/ |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1413
diff
changeset
|
1209 (unaryfunc)instance_abs, /*nb_absolute*/ |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1413
diff
changeset
|
1210 (inquiry)instance_nonzero, /*nb_nonzero*/ |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1413
diff
changeset
|
1211 (unaryfunc)instance_invert, /*nb_invert*/ |
|
1908
0437738279a8
* Include/classobject.h, Objects/classobject.c, Python/ceval.c:
Guido van Rossum <guido@python.org>
parents:
1861
diff
changeset
|
1212 0, /*nb_lshift*/ |
|
0437738279a8
* Include/classobject.h, Objects/classobject.c, Python/ceval.c:
Guido van Rossum <guido@python.org>
parents:
1861
diff
changeset
|
1213 0, /*nb_rshift*/ |
|
0437738279a8
* Include/classobject.h, Objects/classobject.c, Python/ceval.c:
Guido van Rossum <guido@python.org>
parents:
1861
diff
changeset
|
1214 0, /*nb_and*/ |
|
0437738279a8
* Include/classobject.h, Objects/classobject.c, Python/ceval.c:
Guido van Rossum <guido@python.org>
parents:
1861
diff
changeset
|
1215 0, /*nb_xor*/ |
|
0437738279a8
* Include/classobject.h, Objects/classobject.c, Python/ceval.c:
Guido van Rossum <guido@python.org>
parents:
1861
diff
changeset
|
1216 0, /*nb_or*/ |
|
2112
2669ef14c205
implement coercions involving instances properly
Guido van Rossum <guido@python.org>
parents:
2105
diff
changeset
|
1217 (coercion)instance_coerce, /*nb_coerce*/ |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1413
diff
changeset
|
1218 (unaryfunc)instance_int, /*nb_int*/ |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1413
diff
changeset
|
1219 (unaryfunc)instance_long, /*nb_long*/ |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1413
diff
changeset
|
1220 (unaryfunc)instance_float, /*nb_float*/ |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1413
diff
changeset
|
1221 (unaryfunc)instance_oct, /*nb_oct*/ |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1413
diff
changeset
|
1222 (unaryfunc)instance_hex, /*nb_hex*/ |
|
944
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
1223 }; |
|
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
1224 |
|
174
20fee4e2eecd
Change ugly 'class member' to more normal 'instance'.
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
1225 typeobject Instancetype = { |
| 6 | 1226 OB_HEAD_INIT(&Typetype) |
| 1227 0, | |
|
174
20fee4e2eecd
Change ugly 'class member' to more normal 'instance'.
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
1228 "instance", |
|
20fee4e2eecd
Change ugly 'class member' to more normal 'instance'.
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
1229 sizeof(instanceobject), |
| 6 | 1230 0, |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1413
diff
changeset
|
1231 (destructor)instance_dealloc, /*tp_dealloc*/ |
|
1051
f54e64c8b9b7
* Makefile: added IMGFILE; moved some stuff around.
Guido van Rossum <guido@python.org>
parents:
1049
diff
changeset
|
1232 0, /*tp_print*/ |
|
3987
6fbc5c665812
Optimizations by Sjoerd:
Guido van Rossum <guido@python.org>
parents:
3769
diff
changeset
|
1233 0, /*tp_getattr*/ |
|
6fbc5c665812
Optimizations by Sjoerd:
Guido van Rossum <guido@python.org>
parents:
3769
diff
changeset
|
1234 0, /*tp_setattr*/ |
|
6fbc5c665812
Optimizations by Sjoerd:
Guido van Rossum <guido@python.org>
parents:
3769
diff
changeset
|
1235 instance_compare, /*tp_compare*/ |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1413
diff
changeset
|
1236 (reprfunc)instance_repr, /*tp_repr*/ |
|
944
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
1237 &instance_as_number, /*tp_as_number*/ |
|
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
1238 &instance_as_sequence, /*tp_as_sequence*/ |
|
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
1239 &instance_as_mapping, /*tp_as_mapping*/ |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1413
diff
changeset
|
1240 (hashfunc)instance_hash, /*tp_hash*/ |
|
3987
6fbc5c665812
Optimizations by Sjoerd:
Guido van Rossum <guido@python.org>
parents:
3769
diff
changeset
|
1241 0, /*tp_call*/ |
|
6fbc5c665812
Optimizations by Sjoerd:
Guido van Rossum <guido@python.org>
parents:
3769
diff
changeset
|
1242 0, /*tp_str*/ |
|
6fbc5c665812
Optimizations by Sjoerd:
Guido van Rossum <guido@python.org>
parents:
3769
diff
changeset
|
1243 (getattrofunc)instance_getattr, /*tp_getattro*/ |
|
6fbc5c665812
Optimizations by Sjoerd:
Guido van Rossum <guido@python.org>
parents:
3769
diff
changeset
|
1244 (setattrofunc)instance_setattr, /*tp_setattro*/ |
| 6 | 1245 }; |
| 1246 | |
| 1247 | |
|
1289
4d292c7857d4
Access checks now work, at least for instance data (not for methods
Guido van Rossum <guido@python.org>
parents:
1288
diff
changeset
|
1248 /* Instance method objects are used for two purposes: |
|
4d292c7857d4
Access checks now work, at least for instance data (not for methods
Guido van Rossum <guido@python.org>
parents:
1288
diff
changeset
|
1249 (a) as bound instance methods (returned by instancename.methodname) |
|
4d292c7857d4
Access checks now work, at least for instance data (not for methods
Guido van Rossum <guido@python.org>
parents:
1288
diff
changeset
|
1250 (b) as unbound methods (returned by ClassName.methodname) |
|
4d292c7857d4
Access checks now work, at least for instance data (not for methods
Guido van Rossum <guido@python.org>
parents:
1288
diff
changeset
|
1251 In case (b), im_self is NULL |
|
4d292c7857d4
Access checks now work, at least for instance data (not for methods
Guido van Rossum <guido@python.org>
parents:
1288
diff
changeset
|
1252 */ |
| 6 | 1253 |
| 1254 typedef struct { | |
| 1255 OB_HEAD | |
|
1289
4d292c7857d4
Access checks now work, at least for instance data (not for methods
Guido van Rossum <guido@python.org>
parents:
1288
diff
changeset
|
1256 object *im_func; /* The function implementing the method */ |
|
4d292c7857d4
Access checks now work, at least for instance data (not for methods
Guido van Rossum <guido@python.org>
parents:
1288
diff
changeset
|
1257 object *im_self; /* The instance it is bound to, or NULL */ |
|
4d292c7857d4
Access checks now work, at least for instance data (not for methods
Guido van Rossum <guido@python.org>
parents:
1288
diff
changeset
|
1258 object *im_class; /* The class that defined the method */ |
|
224
e71b6fec86e5
Renamed class methods to instance methods (which they are)
Guido van Rossum <guido@python.org>
parents:
202
diff
changeset
|
1259 } instancemethodobject; |
| 6 | 1260 |
| 1261 object * | |
|
1289
4d292c7857d4
Access checks now work, at least for instance data (not for methods
Guido van Rossum <guido@python.org>
parents:
1288
diff
changeset
|
1262 newinstancemethodobject(func, self, class) |
| 6 | 1263 object *func; |
| 1264 object *self; | |
|
1289
4d292c7857d4
Access checks now work, at least for instance data (not for methods
Guido van Rossum <guido@python.org>
parents:
1288
diff
changeset
|
1265 object *class; |
| 6 | 1266 { |
|
224
e71b6fec86e5
Renamed class methods to instance methods (which they are)
Guido van Rossum <guido@python.org>
parents:
202
diff
changeset
|
1267 register instancemethodobject *im; |
| 6 | 1268 if (!is_funcobject(func)) { |
| 27 | 1269 err_badcall(); |
| 6 | 1270 return NULL; |
| 1271 } | |
|
224
e71b6fec86e5
Renamed class methods to instance methods (which they are)
Guido van Rossum <guido@python.org>
parents:
202
diff
changeset
|
1272 im = NEWOBJ(instancemethodobject, &Instancemethodtype); |
|
e71b6fec86e5
Renamed class methods to instance methods (which they are)
Guido van Rossum <guido@python.org>
parents:
202
diff
changeset
|
1273 if (im == NULL) |
| 6 | 1274 return NULL; |
| 1275 INCREF(func); | |
|
224
e71b6fec86e5
Renamed class methods to instance methods (which they are)
Guido van Rossum <guido@python.org>
parents:
202
diff
changeset
|
1276 im->im_func = func; |
|
1289
4d292c7857d4
Access checks now work, at least for instance data (not for methods
Guido van Rossum <guido@python.org>
parents:
1288
diff
changeset
|
1277 XINCREF(self); |
|
224
e71b6fec86e5
Renamed class methods to instance methods (which they are)
Guido van Rossum <guido@python.org>
parents:
202
diff
changeset
|
1278 im->im_self = self; |
|
1289
4d292c7857d4
Access checks now work, at least for instance data (not for methods
Guido van Rossum <guido@python.org>
parents:
1288
diff
changeset
|
1279 INCREF(class); |
|
4d292c7857d4
Access checks now work, at least for instance data (not for methods
Guido van Rossum <guido@python.org>
parents:
1288
diff
changeset
|
1280 im->im_class = class; |
|
224
e71b6fec86e5
Renamed class methods to instance methods (which they are)
Guido van Rossum <guido@python.org>
parents:
202
diff
changeset
|
1281 return (object *)im; |
| 6 | 1282 } |
| 1283 | |
| 1284 object * | |
|
224
e71b6fec86e5
Renamed class methods to instance methods (which they are)
Guido van Rossum <guido@python.org>
parents:
202
diff
changeset
|
1285 instancemethodgetfunc(im) |
|
e71b6fec86e5
Renamed class methods to instance methods (which they are)
Guido van Rossum <guido@python.org>
parents:
202
diff
changeset
|
1286 register object *im; |
| 6 | 1287 { |
|
224
e71b6fec86e5
Renamed class methods to instance methods (which they are)
Guido van Rossum <guido@python.org>
parents:
202
diff
changeset
|
1288 if (!is_instancemethodobject(im)) { |
| 27 | 1289 err_badcall(); |
| 6 | 1290 return NULL; |
| 1291 } | |
|
224
e71b6fec86e5
Renamed class methods to instance methods (which they are)
Guido van Rossum <guido@python.org>
parents:
202
diff
changeset
|
1292 return ((instancemethodobject *)im)->im_func; |
| 6 | 1293 } |
| 1294 | |
| 1295 object * | |
|
224
e71b6fec86e5
Renamed class methods to instance methods (which they are)
Guido van Rossum <guido@python.org>
parents:
202
diff
changeset
|
1296 instancemethodgetself(im) |
|
e71b6fec86e5
Renamed class methods to instance methods (which they are)
Guido van Rossum <guido@python.org>
parents:
202
diff
changeset
|
1297 register object *im; |
| 6 | 1298 { |
|
224
e71b6fec86e5
Renamed class methods to instance methods (which they are)
Guido van Rossum <guido@python.org>
parents:
202
diff
changeset
|
1299 if (!is_instancemethodobject(im)) { |
| 27 | 1300 err_badcall(); |
| 6 | 1301 return NULL; |
| 1302 } | |
|
224
e71b6fec86e5
Renamed class methods to instance methods (which they are)
Guido van Rossum <guido@python.org>
parents:
202
diff
changeset
|
1303 return ((instancemethodobject *)im)->im_self; |
| 6 | 1304 } |
| 1305 | |
|
1289
4d292c7857d4
Access checks now work, at least for instance data (not for methods
Guido van Rossum <guido@python.org>
parents:
1288
diff
changeset
|
1306 object * |
|
4d292c7857d4
Access checks now work, at least for instance data (not for methods
Guido van Rossum <guido@python.org>
parents:
1288
diff
changeset
|
1307 instancemethodgetclass(im) |
|
4d292c7857d4
Access checks now work, at least for instance data (not for methods
Guido van Rossum <guido@python.org>
parents:
1288
diff
changeset
|
1308 register object *im; |
|
4d292c7857d4
Access checks now work, at least for instance data (not for methods
Guido van Rossum <guido@python.org>
parents:
1288
diff
changeset
|
1309 { |
|
4d292c7857d4
Access checks now work, at least for instance data (not for methods
Guido van Rossum <guido@python.org>
parents:
1288
diff
changeset
|
1310 if (!is_instancemethodobject(im)) { |
|
4d292c7857d4
Access checks now work, at least for instance data (not for methods
Guido van Rossum <guido@python.org>
parents:
1288
diff
changeset
|
1311 err_badcall(); |
|
4d292c7857d4
Access checks now work, at least for instance data (not for methods
Guido van Rossum <guido@python.org>
parents:
1288
diff
changeset
|
1312 return NULL; |
|
4d292c7857d4
Access checks now work, at least for instance data (not for methods
Guido van Rossum <guido@python.org>
parents:
1288
diff
changeset
|
1313 } |
|
4d292c7857d4
Access checks now work, at least for instance data (not for methods
Guido van Rossum <guido@python.org>
parents:
1288
diff
changeset
|
1314 return ((instancemethodobject *)im)->im_class; |
|
4d292c7857d4
Access checks now work, at least for instance data (not for methods
Guido van Rossum <guido@python.org>
parents:
1288
diff
changeset
|
1315 } |
|
4d292c7857d4
Access checks now work, at least for instance data (not for methods
Guido van Rossum <guido@python.org>
parents:
1288
diff
changeset
|
1316 |
| 6 | 1317 /* Class method methods */ |
| 1318 | |
|
224
e71b6fec86e5
Renamed class methods to instance methods (which they are)
Guido van Rossum <guido@python.org>
parents:
202
diff
changeset
|
1319 #define OFF(x) offsetof(instancemethodobject, x) |
| 86 | 1320 |
|
224
e71b6fec86e5
Renamed class methods to instance methods (which they are)
Guido van Rossum <guido@python.org>
parents:
202
diff
changeset
|
1321 static struct memberlist instancemethod_memberlist[] = { |
|
e71b6fec86e5
Renamed class methods to instance methods (which they are)
Guido van Rossum <guido@python.org>
parents:
202
diff
changeset
|
1322 {"im_func", T_OBJECT, OFF(im_func)}, |
|
e71b6fec86e5
Renamed class methods to instance methods (which they are)
Guido van Rossum <guido@python.org>
parents:
202
diff
changeset
|
1323 {"im_self", T_OBJECT, OFF(im_self)}, |
|
1289
4d292c7857d4
Access checks now work, at least for instance data (not for methods
Guido van Rossum <guido@python.org>
parents:
1288
diff
changeset
|
1324 {"im_class", T_OBJECT, OFF(im_class)}, |
|
3549
f66306bc0663
Added __name__ attribute to class instance method objects.
Guido van Rossum <guido@python.org>
parents:
3096
diff
changeset
|
1325 /* Dummies that are not handled by getattr() except for __members__ */ |
|
f66306bc0663
Added __name__ attribute to class instance method objects.
Guido van Rossum <guido@python.org>
parents:
3096
diff
changeset
|
1326 {"__doc__", T_INT, 0}, |
|
f66306bc0663
Added __name__ attribute to class instance method objects.
Guido van Rossum <guido@python.org>
parents:
3096
diff
changeset
|
1327 {"__name__", T_INT, 0}, |
| 86 | 1328 {NULL} /* Sentinel */ |
| 1329 }; | |
| 1330 | |
| 1331 static object * | |
|
224
e71b6fec86e5
Renamed class methods to instance methods (which they are)
Guido van Rossum <guido@python.org>
parents:
202
diff
changeset
|
1332 instancemethod_getattr(im, name) |
|
e71b6fec86e5
Renamed class methods to instance methods (which they are)
Guido van Rossum <guido@python.org>
parents:
202
diff
changeset
|
1333 register instancemethodobject *im; |
|
3987
6fbc5c665812
Optimizations by Sjoerd:
Guido van Rossum <guido@python.org>
parents:
3769
diff
changeset
|
1334 object *name; |
| 86 | 1335 { |
|
3987
6fbc5c665812
Optimizations by Sjoerd:
Guido van Rossum <guido@python.org>
parents:
3769
diff
changeset
|
1336 char *sname = getstringvalue(name); |
|
6fbc5c665812
Optimizations by Sjoerd:
Guido van Rossum <guido@python.org>
parents:
3769
diff
changeset
|
1337 if (sname[0] == '_') { |
|
3549
f66306bc0663
Added __name__ attribute to class instance method objects.
Guido van Rossum <guido@python.org>
parents:
3096
diff
changeset
|
1338 funcobject *func = (funcobject *)(im->im_func); |
|
3987
6fbc5c665812
Optimizations by Sjoerd:
Guido van Rossum <guido@python.org>
parents:
3769
diff
changeset
|
1339 if (strcmp(sname, "__name__") == 0) { |
|
3549
f66306bc0663
Added __name__ attribute to class instance method objects.
Guido van Rossum <guido@python.org>
parents:
3096
diff
changeset
|
1340 INCREF(func->func_name); |
|
f66306bc0663
Added __name__ attribute to class instance method objects.
Guido van Rossum <guido@python.org>
parents:
3096
diff
changeset
|
1341 return func->func_name; |
|
f66306bc0663
Added __name__ attribute to class instance method objects.
Guido van Rossum <guido@python.org>
parents:
3096
diff
changeset
|
1342 } |
|
3987
6fbc5c665812
Optimizations by Sjoerd:
Guido van Rossum <guido@python.org>
parents:
3769
diff
changeset
|
1343 if (strcmp(sname, "__doc__") == 0) { |
|
3549
f66306bc0663
Added __name__ attribute to class instance method objects.
Guido van Rossum <guido@python.org>
parents:
3096
diff
changeset
|
1344 INCREF(func->func_doc); |
|
f66306bc0663
Added __name__ attribute to class instance method objects.
Guido van Rossum <guido@python.org>
parents:
3096
diff
changeset
|
1345 return func->func_doc; |
|
f66306bc0663
Added __name__ attribute to class instance method objects.
Guido van Rossum <guido@python.org>
parents:
3096
diff
changeset
|
1346 } |
|
f66306bc0663
Added __name__ attribute to class instance method objects.
Guido van Rossum <guido@python.org>
parents:
3096
diff
changeset
|
1347 } |
|
f66306bc0663
Added __name__ attribute to class instance method objects.
Guido van Rossum <guido@python.org>
parents:
3096
diff
changeset
|
1348 if (getrestricted()) { |
|
2105
ea7b2bf0b521
add restrictions in restricted mode
Guido van Rossum <guido@python.org>
parents:
2084
diff
changeset
|
1349 err_setstr(RuntimeError, |
|
ea7b2bf0b521
add restrictions in restricted mode
Guido van Rossum <guido@python.org>
parents:
2084
diff
changeset
|
1350 "instance-method attributes not accessible in restricted mode"); |
|
ea7b2bf0b521
add restrictions in restricted mode
Guido van Rossum <guido@python.org>
parents:
2084
diff
changeset
|
1351 return NULL; |
|
ea7b2bf0b521
add restrictions in restricted mode
Guido van Rossum <guido@python.org>
parents:
2084
diff
changeset
|
1352 } |
|
3987
6fbc5c665812
Optimizations by Sjoerd:
Guido van Rossum <guido@python.org>
parents:
3769
diff
changeset
|
1353 return getmember((char *)im, instancemethod_memberlist, sname); |
| 86 | 1354 } |
| 1355 | |
| 6 | 1356 static void |
|
224
e71b6fec86e5
Renamed class methods to instance methods (which they are)
Guido van Rossum <guido@python.org>
parents:
202
diff
changeset
|
1357 instancemethod_dealloc(im) |
|
e71b6fec86e5
Renamed class methods to instance methods (which they are)
Guido van Rossum <guido@python.org>
parents:
202
diff
changeset
|
1358 register instancemethodobject *im; |
| 6 | 1359 { |
|
224
e71b6fec86e5
Renamed class methods to instance methods (which they are)
Guido van Rossum <guido@python.org>
parents:
202
diff
changeset
|
1360 DECREF(im->im_func); |
|
1289
4d292c7857d4
Access checks now work, at least for instance data (not for methods
Guido van Rossum <guido@python.org>
parents:
1288
diff
changeset
|
1361 XDECREF(im->im_self); |
|
4d292c7857d4
Access checks now work, at least for instance data (not for methods
Guido van Rossum <guido@python.org>
parents:
1288
diff
changeset
|
1362 DECREF(im->im_class); |
|
224
e71b6fec86e5
Renamed class methods to instance methods (which they are)
Guido van Rossum <guido@python.org>
parents:
202
diff
changeset
|
1363 free((ANY *)im); |
| 6 | 1364 } |
| 1365 | |
|
1021
0d7c82366704
Compare instance methods by comparing the object and the function.
Guido van Rossum <guido@python.org>
parents:
956
diff
changeset
|
1366 static int |
|
0d7c82366704
Compare instance methods by comparing the object and the function.
Guido van Rossum <guido@python.org>
parents:
956
diff
changeset
|
1367 instancemethod_compare(a, b) |
|
0d7c82366704
Compare instance methods by comparing the object and the function.
Guido van Rossum <guido@python.org>
parents:
956
diff
changeset
|
1368 instancemethodobject *a, *b; |
|
0d7c82366704
Compare instance methods by comparing the object and the function.
Guido van Rossum <guido@python.org>
parents:
956
diff
changeset
|
1369 { |
|
2641
f98a2233b337
change comparing instance methods
Guido van Rossum <guido@python.org>
parents:
2239
diff
changeset
|
1370 if (a->im_self != b->im_self) |
|
f98a2233b337
change comparing instance methods
Guido van Rossum <guido@python.org>
parents:
2239
diff
changeset
|
1371 return (a->im_self < b->im_self) ? -1 : 1; |
|
f98a2233b337
change comparing instance methods
Guido van Rossum <guido@python.org>
parents:
2239
diff
changeset
|
1372 return cmpobject(a->im_func, b->im_func); |
|
1021
0d7c82366704
Compare instance methods by comparing the object and the function.
Guido van Rossum <guido@python.org>
parents:
956
diff
changeset
|
1373 } |
|
0d7c82366704
Compare instance methods by comparing the object and the function.
Guido van Rossum <guido@python.org>
parents:
956
diff
changeset
|
1374 |
|
1288
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1272
diff
changeset
|
1375 static object * |
|
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1272
diff
changeset
|
1376 instancemethod_repr(a) |
|
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1272
diff
changeset
|
1377 instancemethodobject *a; |
|
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1272
diff
changeset
|
1378 { |
|
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1272
diff
changeset
|
1379 char buf[240]; |
|
1289
4d292c7857d4
Access checks now work, at least for instance data (not for methods
Guido van Rossum <guido@python.org>
parents:
1288
diff
changeset
|
1380 instanceobject *self = (instanceobject *)(a->im_self); |
|
4d292c7857d4
Access checks now work, at least for instance data (not for methods
Guido van Rossum <guido@python.org>
parents:
1288
diff
changeset
|
1381 funcobject *func = (funcobject *)(a->im_func); |
|
4d292c7857d4
Access checks now work, at least for instance data (not for methods
Guido van Rossum <guido@python.org>
parents:
1288
diff
changeset
|
1382 classobject *class = (classobject *)(a->im_class); |
|
4d292c7857d4
Access checks now work, at least for instance data (not for methods
Guido van Rossum <guido@python.org>
parents:
1288
diff
changeset
|
1383 object *fclassname, *iclassname, *funcname; |
|
4d292c7857d4
Access checks now work, at least for instance data (not for methods
Guido van Rossum <guido@python.org>
parents:
1288
diff
changeset
|
1384 char *fcname, *icname, *fname; |
|
4d292c7857d4
Access checks now work, at least for instance data (not for methods
Guido van Rossum <guido@python.org>
parents:
1288
diff
changeset
|
1385 fclassname = class->cl_name; |
|
4d292c7857d4
Access checks now work, at least for instance data (not for methods
Guido van Rossum <guido@python.org>
parents:
1288
diff
changeset
|
1386 funcname = func->func_name; |
|
4d292c7857d4
Access checks now work, at least for instance data (not for methods
Guido van Rossum <guido@python.org>
parents:
1288
diff
changeset
|
1387 if (fclassname != NULL && is_stringobject(fclassname)) |
|
4d292c7857d4
Access checks now work, at least for instance data (not for methods
Guido van Rossum <guido@python.org>
parents:
1288
diff
changeset
|
1388 fcname = getstringvalue(fclassname); |
|
1288
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1272
diff
changeset
|
1389 else |
|
1289
4d292c7857d4
Access checks now work, at least for instance data (not for methods
Guido van Rossum <guido@python.org>
parents:
1288
diff
changeset
|
1390 fcname = "?"; |
|
1288
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1272
diff
changeset
|
1391 if (funcname != NULL && is_stringobject(funcname)) |
|
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1272
diff
changeset
|
1392 fname = getstringvalue(funcname); |
|
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1272
diff
changeset
|
1393 else |
|
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1272
diff
changeset
|
1394 fname = "?"; |
|
1289
4d292c7857d4
Access checks now work, at least for instance data (not for methods
Guido van Rossum <guido@python.org>
parents:
1288
diff
changeset
|
1395 if (self == NULL) |
|
4d292c7857d4
Access checks now work, at least for instance data (not for methods
Guido van Rossum <guido@python.org>
parents:
1288
diff
changeset
|
1396 sprintf(buf, "<unbound method %.100s.%.100s>", fcname, fname); |
|
4d292c7857d4
Access checks now work, at least for instance data (not for methods
Guido van Rossum <guido@python.org>
parents:
1288
diff
changeset
|
1397 else { |
|
4d292c7857d4
Access checks now work, at least for instance data (not for methods
Guido van Rossum <guido@python.org>
parents:
1288
diff
changeset
|
1398 iclassname = self->in_class->cl_name; |
|
4d292c7857d4
Access checks now work, at least for instance data (not for methods
Guido van Rossum <guido@python.org>
parents:
1288
diff
changeset
|
1399 if (iclassname != NULL && is_stringobject(iclassname)) |
|
4d292c7857d4
Access checks now work, at least for instance data (not for methods
Guido van Rossum <guido@python.org>
parents:
1288
diff
changeset
|
1400 icname = getstringvalue(iclassname); |
|
4d292c7857d4
Access checks now work, at least for instance data (not for methods
Guido van Rossum <guido@python.org>
parents:
1288
diff
changeset
|
1401 else |
|
4d292c7857d4
Access checks now work, at least for instance data (not for methods
Guido van Rossum <guido@python.org>
parents:
1288
diff
changeset
|
1402 icname = "?"; |
|
4d292c7857d4
Access checks now work, at least for instance data (not for methods
Guido van Rossum <guido@python.org>
parents:
1288
diff
changeset
|
1403 sprintf(buf, "<method %.60s.%.60s of %.60s instance at %lx>", |
|
4d292c7857d4
Access checks now work, at least for instance data (not for methods
Guido van Rossum <guido@python.org>
parents:
1288
diff
changeset
|
1404 fcname, fname, icname, (long)self); |
|
4d292c7857d4
Access checks now work, at least for instance data (not for methods
Guido van Rossum <guido@python.org>
parents:
1288
diff
changeset
|
1405 } |
|
1288
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1272
diff
changeset
|
1406 return newstringobject(buf); |
|
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1272
diff
changeset
|
1407 } |
|
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1272
diff
changeset
|
1408 |
|
1257
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1252
diff
changeset
|
1409 static long |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1252
diff
changeset
|
1410 instancemethod_hash(a) |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1252
diff
changeset
|
1411 instancemethodobject *a; |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1252
diff
changeset
|
1412 { |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1252
diff
changeset
|
1413 long x, y; |
|
1289
4d292c7857d4
Access checks now work, at least for instance data (not for methods
Guido van Rossum <guido@python.org>
parents:
1288
diff
changeset
|
1414 if (a->im_self == NULL) |
|
4d292c7857d4
Access checks now work, at least for instance data (not for methods
Guido van Rossum <guido@python.org>
parents:
1288
diff
changeset
|
1415 x = hashobject(None); |
|
4d292c7857d4
Access checks now work, at least for instance data (not for methods
Guido van Rossum <guido@python.org>
parents:
1288
diff
changeset
|
1416 else |
|
4d292c7857d4
Access checks now work, at least for instance data (not for methods
Guido van Rossum <guido@python.org>
parents:
1288
diff
changeset
|
1417 x = hashobject(a->im_self); |
|
1257
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1252
diff
changeset
|
1418 if (x == -1) |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1252
diff
changeset
|
1419 return -1; |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1252
diff
changeset
|
1420 y = hashobject(a->im_func); |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1252
diff
changeset
|
1421 if (y == -1) |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1252
diff
changeset
|
1422 return -1; |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1252
diff
changeset
|
1423 return x ^ y; |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1252
diff
changeset
|
1424 } |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1252
diff
changeset
|
1425 |
|
224
e71b6fec86e5
Renamed class methods to instance methods (which they are)
Guido van Rossum <guido@python.org>
parents:
202
diff
changeset
|
1426 typeobject Instancemethodtype = { |
| 6 | 1427 OB_HEAD_INIT(&Typetype) |
| 1428 0, | |
|
202
bfffc7e0fcc3
Rename class methods to instance methods (at least where user-visible)
Guido van Rossum <guido@python.org>
parents:
174
diff
changeset
|
1429 "instance method", |
|
224
e71b6fec86e5
Renamed class methods to instance methods (which they are)
Guido van Rossum <guido@python.org>
parents:
202
diff
changeset
|
1430 sizeof(instancemethodobject), |
| 6 | 1431 0, |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1413
diff
changeset
|
1432 (destructor)instancemethod_dealloc, /*tp_dealloc*/ |
| 6 | 1433 0, /*tp_print*/ |
|
3987
6fbc5c665812
Optimizations by Sjoerd:
Guido van Rossum <guido@python.org>
parents:
3769
diff
changeset
|
1434 0, /*tp_getattr*/ |
| 6 | 1435 0, /*tp_setattr*/ |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1413
diff
changeset
|
1436 (cmpfunc)instancemethod_compare, /*tp_compare*/ |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1413
diff
changeset
|
1437 (reprfunc)instancemethod_repr, /*tp_repr*/ |
| 6 | 1438 0, /*tp_as_number*/ |
| 1439 0, /*tp_as_sequence*/ | |
| 1440 0, /*tp_as_mapping*/ | |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1413
diff
changeset
|
1441 (hashfunc)instancemethod_hash, /*tp_hash*/ |
|
3987
6fbc5c665812
Optimizations by Sjoerd:
Guido van Rossum <guido@python.org>
parents:
3769
diff
changeset
|
1442 0, /*tp_call*/ |
|
6fbc5c665812
Optimizations by Sjoerd:
Guido van Rossum <guido@python.org>
parents:
3769
diff
changeset
|
1443 0, /*tp_str*/ |
|
6fbc5c665812
Optimizations by Sjoerd:
Guido van Rossum <guido@python.org>
parents:
3769
diff
changeset
|
1444 (getattrofunc)instancemethod_getattr, /*tp_getattro*/ |
|
6fbc5c665812
Optimizations by Sjoerd:
Guido van Rossum <guido@python.org>
parents:
3769
diff
changeset
|
1445 0, /*tp_setattro*/ |
| 6 | 1446 }; |
