annotate Objects/classobject.c @ 1330:409ea74feb21 legacy-trunk

Minor fixes / changes for Mac compatibility.
author Guido van Rossum <guido@python.org>
date Thu, 29 Jul 1993 08:25:09 +0000
parents ac3c80abbf43
children e149c77d173c
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
145
853b736a6a9e Added copyright notice.
Guido van Rossum <guido@python.org>
parents: 86
diff changeset
1 /***********************************************************
1252
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1215
diff changeset
2 Copyright 1991, 1992, 1993 by Stichting Mathematisch Centrum,
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1215
diff changeset
3 Amsterdam, The Netherlands.
145
853b736a6a9e Added copyright notice.
Guido van Rossum <guido@python.org>
parents: 86
diff changeset
4
853b736a6a9e Added copyright notice.
Guido van Rossum <guido@python.org>
parents: 86
diff changeset
5 All Rights Reserved
853b736a6a9e Added copyright notice.
Guido van Rossum <guido@python.org>
parents: 86
diff changeset
6
853b736a6a9e Added copyright notice.
Guido van Rossum <guido@python.org>
parents: 86
diff changeset
7 Permission to use, copy, modify, and distribute this software and its
853b736a6a9e Added copyright notice.
Guido van Rossum <guido@python.org>
parents: 86
diff changeset
8 documentation for any purpose and without fee is hereby granted,
853b736a6a9e Added copyright notice.
Guido van Rossum <guido@python.org>
parents: 86
diff changeset
9 provided that the above copyright notice appear in all copies and that
853b736a6a9e Added copyright notice.
Guido van Rossum <guido@python.org>
parents: 86
diff changeset
10 both that copyright notice and this permission notice appear in
853b736a6a9e Added copyright notice.
Guido van Rossum <guido@python.org>
parents: 86
diff changeset
11 supporting documentation, and that the names of Stichting Mathematisch
853b736a6a9e Added copyright notice.
Guido van Rossum <guido@python.org>
parents: 86
diff changeset
12 Centrum or CWI not be used in advertising or publicity pertaining to
853b736a6a9e Added copyright notice.
Guido van Rossum <guido@python.org>
parents: 86
diff changeset
13 distribution of the software without specific, written prior permission.
853b736a6a9e Added copyright notice.
Guido van Rossum <guido@python.org>
parents: 86
diff changeset
14
853b736a6a9e Added copyright notice.
Guido van Rossum <guido@python.org>
parents: 86
diff changeset
15 STICHTING MATHEMATISCH CENTRUM DISCLAIMS ALL WARRANTIES WITH REGARD TO
853b736a6a9e Added copyright notice.
Guido van Rossum <guido@python.org>
parents: 86
diff changeset
16 THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
853b736a6a9e Added copyright notice.
Guido van Rossum <guido@python.org>
parents: 86
diff changeset
17 FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH CENTRUM BE LIABLE
853b736a6a9e Added copyright notice.
Guido van Rossum <guido@python.org>
parents: 86
diff changeset
18 FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
853b736a6a9e Added copyright notice.
Guido van Rossum <guido@python.org>
parents: 86
diff changeset
19 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
853b736a6a9e Added copyright notice.
Guido van Rossum <guido@python.org>
parents: 86
diff changeset
20 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
853b736a6a9e Added copyright notice.
Guido van Rossum <guido@python.org>
parents: 86
diff changeset
21 OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
853b736a6a9e Added copyright notice.
Guido van Rossum <guido@python.org>
parents: 86
diff changeset
22
853b736a6a9e Added copyright notice.
Guido van Rossum <guido@python.org>
parents: 86
diff changeset
23 ******************************************************************/
853b736a6a9e Added copyright notice.
Guido van Rossum <guido@python.org>
parents: 86
diff changeset
24
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
25 /* Class object implementation */
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
26
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 27
diff changeset
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 "modsupport.h"
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
29 #include "structmember.h"
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
30 #include "ceval.h"
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
31
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
32 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
33 newclassobject(bases, dict, name)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
34 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
35 object *dict;
386
0598f4f25d04 Add several secret __*__ attributes
Guido van Rossum <guido@python.org>
parents: 224
diff changeset
36 object *name; /* String; NULL if unknown */
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
37 {
1289
4d292c7857d4 Access checks now work, at least for instance data (not for methods
Guido van Rossum <guido@python.org>
parents: 1288
diff changeset
38 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
39 object *key, *value;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
40 classobject *op;
465
ca0391a812f8 The cl_bases member of a class object is now never NULL.
Guido van Rossum <guido@python.org>
parents: 441
diff changeset
41 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
42 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
43 if (bases == NULL)
1252
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1215
diff changeset
44 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
45 }
ca0391a812f8 The cl_bases member of a class object is now never NULL.
Guido van Rossum <guido@python.org>
parents: 441
diff changeset
46 else
ca0391a812f8 The cl_bases member of a class object is now never NULL.
Guido van Rossum <guido@python.org>
parents: 441
diff changeset
47 INCREF(bases);
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
48 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
49 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
50 DECREF(bases);
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
51 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
52 }
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
53 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
54 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
55 op->cl_dict = dict;
386
0598f4f25d04 Add several secret __*__ attributes
Guido van Rossum <guido@python.org>
parents: 224
diff changeset
56 XINCREF(name);
0598f4f25d04 Add several secret __*__ attributes
Guido van Rossum <guido@python.org>
parents: 224
diff changeset
57 op->cl_name = 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
58 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
59 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
60 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
61 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
62 }
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
63 return (object *) op;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
64 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
65
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
66 /* Class methods */
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
67
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
68 static void
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
69 class_dealloc(op)
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
70 classobject *op;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
71 {
465
ca0391a812f8 The cl_bases member of a class object is now never NULL.
Guido van Rossum <guido@python.org>
parents: 441
diff changeset
72 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
73 DECREF(op->cl_dict);
386
0598f4f25d04 Add several secret __*__ attributes
Guido van Rossum <guido@python.org>
parents: 224
diff changeset
74 XDECREF(op->cl_name);
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
75 free((ANY *)op);
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
76 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
77
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
78 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
79 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
80 classobject *cp;
4d292c7857d4 Access checks now work, at least for instance data (not for methods
Guido van Rossum <guido@python.org>
parents: 1288
diff changeset
81 char *name;
4d292c7857d4 Access checks now work, at least for instance data (not for methods
Guido van Rossum <guido@python.org>
parents: 1288
diff changeset
82 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
83 {
4d292c7857d4 Access checks now work, at least for instance data (not for methods
Guido van Rossum <guido@python.org>
parents: 1288
diff changeset
84 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
85 object *value = dictlookup(cp->cl_dict, name);
4d292c7857d4 Access checks now work, at least for instance data (not for methods
Guido van Rossum <guido@python.org>
parents: 1288
diff changeset
86 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
87 *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
88 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
89 }
4d292c7857d4 Access checks now work, at least for instance data (not for methods
Guido van Rossum <guido@python.org>
parents: 1288
diff changeset
90 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
91 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
92 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
93 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
94 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
95 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
96 }
4d292c7857d4 Access checks now work, at least for instance data (not for methods
Guido van Rossum <guido@python.org>
parents: 1288
diff changeset
97 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
98 }
4d292c7857d4 Access checks now work, at least for instance data (not for methods
Guido van Rossum <guido@python.org>
parents: 1288
diff changeset
99
4d292c7857d4 Access checks now work, at least for instance data (not for methods
Guido van Rossum <guido@python.org>
parents: 1288
diff changeset
100 static object *
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
101 class_getattr(op, name)
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
102 register classobject *op;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
103 register char *name;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
104 {
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
105 register object *v;
1289
4d292c7857d4 Access checks now work, at least for instance data (not for methods
Guido van Rossum <guido@python.org>
parents: 1288
diff changeset
106 object *class;
386
0598f4f25d04 Add several secret __*__ attributes
Guido van Rossum <guido@python.org>
parents: 224
diff changeset
107 if (strcmp(name, "__dict__") == 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
108 INCREF(op->cl_dict);
4d292c7857d4 Access checks now work, at least for instance data (not for methods
Guido van Rossum <guido@python.org>
parents: 1288
diff changeset
109 return op->cl_dict;
386
0598f4f25d04 Add several secret __*__ attributes
Guido van Rossum <guido@python.org>
parents: 224
diff changeset
110 }
0598f4f25d04 Add several secret __*__ attributes
Guido van Rossum <guido@python.org>
parents: 224
diff changeset
111 if (strcmp(name, "__bases__") == 0) {
0598f4f25d04 Add several secret __*__ attributes
Guido van Rossum <guido@python.org>
parents: 224
diff changeset
112 INCREF(op->cl_bases);
0598f4f25d04 Add several secret __*__ attributes
Guido van Rossum <guido@python.org>
parents: 224
diff changeset
113 return op->cl_bases;
0598f4f25d04 Add several secret __*__ attributes
Guido van Rossum <guido@python.org>
parents: 224
diff changeset
114 }
0598f4f25d04 Add several secret __*__ attributes
Guido van Rossum <guido@python.org>
parents: 224
diff changeset
115 if (strcmp(name, "__name__") == 0) {
0598f4f25d04 Add several secret __*__ attributes
Guido van Rossum <guido@python.org>
parents: 224
diff changeset
116 if (op->cl_name == NULL)
0598f4f25d04 Add several secret __*__ attributes
Guido van Rossum <guido@python.org>
parents: 224
diff changeset
117 v = None;
0598f4f25d04 Add several secret __*__ attributes
Guido van Rossum <guido@python.org>
parents: 224
diff changeset
118 else
0598f4f25d04 Add several secret __*__ attributes
Guido van Rossum <guido@python.org>
parents: 224
diff changeset
119 v = op->cl_name;
0598f4f25d04 Add several secret __*__ attributes
Guido van Rossum <guido@python.org>
parents: 224
diff changeset
120 INCREF(v);
0598f4f25d04 Add several secret __*__ attributes
Guido van Rossum <guido@python.org>
parents: 224
diff changeset
121 return v;
0598f4f25d04 Add several secret __*__ attributes
Guido van Rossum <guido@python.org>
parents: 224
diff changeset
122 }
1289
4d292c7857d4 Access checks now work, at least for instance data (not for methods
Guido van Rossum <guido@python.org>
parents: 1288
diff changeset
123 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
124 if (v == NULL) {
7971fecbf28e * Lots of small changes related to access.
Guido van Rossum <guido@python.org>
parents: 1289
diff changeset
125 err_setstr(AttributeError, name);
7971fecbf28e * Lots of small changes related to access.
Guido van Rossum <guido@python.org>
parents: 1289
diff changeset
126 return NULL;
7971fecbf28e * Lots of small changes related to access.
Guido van Rossum <guido@python.org>
parents: 1289
diff changeset
127 }
7971fecbf28e * Lots of small changes related to access.
Guido van Rossum <guido@python.org>
parents: 1289
diff changeset
128 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
129 v = getaccessvalue(v, getowner());
1290
7971fecbf28e * Lots of small changes related to access.
Guido van Rossum <guido@python.org>
parents: 1289
diff changeset
130 if (v == NULL)
7971fecbf28e * Lots of small changes related to access.
Guido van Rossum <guido@python.org>
parents: 1289
diff changeset
131 return NULL;
7971fecbf28e * Lots of small changes related to access.
Guido van Rossum <guido@python.org>
parents: 1289
diff changeset
132 }
7971fecbf28e * Lots of small changes related to access.
Guido van Rossum <guido@python.org>
parents: 1289
diff changeset
133 else
7971fecbf28e * Lots of small changes related to access.
Guido van Rossum <guido@python.org>
parents: 1289
diff changeset
134 INCREF(v);
7971fecbf28e * Lots of small changes related to access.
Guido van Rossum <guido@python.org>
parents: 1289
diff changeset
135 if (is_funcobject(v)) {
7971fecbf28e * Lots of small changes related to access.
Guido van Rossum <guido@python.org>
parents: 1289
diff changeset
136 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
137 (object *)class);
1290
7971fecbf28e * Lots of small changes related to access.
Guido van Rossum <guido@python.org>
parents: 1289
diff changeset
138 DECREF(v);
7971fecbf28e * Lots of small changes related to access.
Guido van Rossum <guido@python.org>
parents: 1289
diff changeset
139 v = w;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
140 }
1290
7971fecbf28e * Lots of small changes related to access.
Guido van Rossum <guido@python.org>
parents: 1289
diff changeset
141 return v;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
142 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
143
386
0598f4f25d04 Add several secret __*__ attributes
Guido van Rossum <guido@python.org>
parents: 224
diff changeset
144 static int
0598f4f25d04 Add several secret __*__ attributes
Guido van Rossum <guido@python.org>
parents: 224
diff changeset
145 class_setattr(op, name, v)
0598f4f25d04 Add several secret __*__ attributes
Guido van Rossum <guido@python.org>
parents: 224
diff changeset
146 classobject *op;
0598f4f25d04 Add several secret __*__ attributes
Guido van Rossum <guido@python.org>
parents: 224
diff changeset
147 char *name;
0598f4f25d04 Add several secret __*__ attributes
Guido van Rossum <guido@python.org>
parents: 224
diff changeset
148 object *v;
0598f4f25d04 Add several secret __*__ attributes
Guido van Rossum <guido@python.org>
parents: 224
diff changeset
149 {
1288
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1272
diff changeset
150 object *ac;
791
77ee9b222fdb Don't allow assignment to attributes named __*__
Guido van Rossum <guido@python.org>
parents: 735
diff changeset
151 if (name[0] == '_' && name[1] == '_') {
77ee9b222fdb Don't allow assignment to attributes named __*__
Guido van Rossum <guido@python.org>
parents: 735
diff changeset
152 int n = strlen(name);
77ee9b222fdb Don't allow assignment to attributes named __*__
Guido van Rossum <guido@python.org>
parents: 735
diff changeset
153 if (name[n-1] == '_' && name[n-2] == '_') {
77ee9b222fdb Don't allow assignment to attributes named __*__
Guido van Rossum <guido@python.org>
parents: 735
diff changeset
154 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
155 return -1;
77ee9b222fdb Don't allow assignment to attributes named __*__
Guido van Rossum <guido@python.org>
parents: 735
diff changeset
156 }
77ee9b222fdb Don't allow assignment to attributes named __*__
Guido van Rossum <guido@python.org>
parents: 735
diff changeset
157 }
1289
4d292c7857d4 Access checks now work, at least for instance data (not for methods
Guido van Rossum <guido@python.org>
parents: 1288
diff changeset
158 ac = dictlookup(op->cl_dict, name);
1288
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1272
diff changeset
159 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
160 return setaccessvalue(ac, getowner(), v);
1031
7a11a70f7f44 classobject.c moduleobject.c stdwinmodule.c xxobject.c:
Guido van Rossum <guido@python.org>
parents: 1021
diff changeset
161 if (v == 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
162 int rv = dictremove(op->cl_dict, name);
1031
7a11a70f7f44 classobject.c moduleobject.c stdwinmodule.c xxobject.c:
Guido van Rossum <guido@python.org>
parents: 1021
diff changeset
163 if (rv < 0)
7a11a70f7f44 classobject.c moduleobject.c stdwinmodule.c xxobject.c:
Guido van Rossum <guido@python.org>
parents: 1021
diff changeset
164 err_setstr(AttributeError,
7a11a70f7f44 classobject.c moduleobject.c stdwinmodule.c xxobject.c:
Guido van Rossum <guido@python.org>
parents: 1021
diff changeset
165 "delete non-existing class attribute");
7a11a70f7f44 classobject.c moduleobject.c stdwinmodule.c xxobject.c:
Guido van Rossum <guido@python.org>
parents: 1021
diff changeset
166 return rv;
7a11a70f7f44 classobject.c moduleobject.c stdwinmodule.c xxobject.c:
Guido van Rossum <guido@python.org>
parents: 1021
diff changeset
167 }
386
0598f4f25d04 Add several secret __*__ attributes
Guido van Rossum <guido@python.org>
parents: 224
diff changeset
168 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
169 return dictinsert(op->cl_dict, name, v);
386
0598f4f25d04 Add several secret __*__ attributes
Guido van Rossum <guido@python.org>
parents: 224
diff changeset
170 }
0598f4f25d04 Add several secret __*__ attributes
Guido van Rossum <guido@python.org>
parents: 224
diff changeset
171
1288
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1272
diff changeset
172 static object *
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1272
diff changeset
173 class_repr(op)
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1272
diff changeset
174 classobject *op;
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1272
diff changeset
175 {
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1272
diff changeset
176 char buf[140];
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1272
diff changeset
177 char *name;
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1272
diff changeset
178 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
179 name = "?";
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1272
diff changeset
180 else
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1272
diff changeset
181 name = getstringvalue(op->cl_name);
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1272
diff changeset
182 sprintf(buf, "<class %.100s at %lx>", name, (long)op);
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1272
diff changeset
183 return newstringobject(buf);
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1272
diff changeset
184 }
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1272
diff changeset
185
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
186 typeobject Classtype = {
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
187 OB_HEAD_INIT(&Typetype)
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
188 0,
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
189 "class",
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
190 sizeof(classobject),
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
191 0,
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
192 class_dealloc, /*tp_dealloc*/
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
193 0, /*tp_print*/
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
194 class_getattr, /*tp_getattr*/
386
0598f4f25d04 Add several secret __*__ attributes
Guido van Rossum <guido@python.org>
parents: 224
diff changeset
195 class_setattr, /*tp_setattr*/
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
196 0, /*tp_compare*/
1288
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1272
diff changeset
197 class_repr, /*tp_repr*/
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
198 0, /*tp_as_number*/
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
199 0, /*tp_as_sequence*/
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
200 0, /*tp_as_mapping*/
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
201 };
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
202
1289
4d292c7857d4 Access checks now work, at least for instance data (not for methods
Guido van Rossum <guido@python.org>
parents: 1288
diff changeset
203 int
4d292c7857d4 Access checks now work, at least for instance data (not for methods
Guido van Rossum <guido@python.org>
parents: 1288
diff changeset
204 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
205 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
206 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
207 {
4d292c7857d4 Access checks now work, at least for instance data (not for methods
Guido van Rossum <guido@python.org>
parents: 1288
diff changeset
208 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
209 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
210 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
211 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
212 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
213 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
214 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
215 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
216 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
217 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
218 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
219 }
4d292c7857d4 Access checks now work, at least for instance data (not for methods
Guido van Rossum <guido@python.org>
parents: 1288
diff changeset
220 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
221 }
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
222
1289
4d292c7857d4 Access checks now work, at least for instance data (not for methods
Guido van Rossum <guido@python.org>
parents: 1288
diff changeset
223
4d292c7857d4 Access checks now work, at least for instance data (not for methods
Guido van Rossum <guido@python.org>
parents: 1288
diff changeset
224 /* Instance objects */
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
225
1288
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1272
diff changeset
226 static object *instance_getattr PROTO((instanceobject *, char *));
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1272
diff changeset
227
1289
4d292c7857d4 Access checks now work, at least for instance data (not for methods
Guido van Rossum <guido@python.org>
parents: 1288
diff changeset
228 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
229 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
230 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
231 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
232 {
4d292c7857d4 Access checks now work, at least for instance data (not for methods
Guido van Rossum <guido@python.org>
parents: 1288
diff changeset
233 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
234 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
235
4d292c7857d4 Access checks now work, at least for instance data (not for methods
Guido van Rossum <guido@python.org>
parents: 1288
diff changeset
236 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
237 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
238 if (addaccess(gettupleitem(class->cl_bases, i), inst) < 0)
4d292c7857d4 Access checks now work, at least for instance data (not for methods
Guido van Rossum <guido@python.org>
parents: 1288
diff changeset
239 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
240 }
4d292c7857d4 Access checks now work, at least for instance data (not for methods
Guido van Rossum <guido@python.org>
parents: 1288
diff changeset
241
4d292c7857d4 Access checks now work, at least for instance data (not for methods
Guido van Rossum <guido@python.org>
parents: 1288
diff changeset
242 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
243 while (mappinggetnext(class->cl_dict, &pos, &key, &value)) {
1290
7971fecbf28e * Lots of small changes related to access.
Guido van Rossum <guido@python.org>
parents: 1289
diff changeset
244 object *v;
1289
4d292c7857d4 Access checks now work, at least for instance data (not for methods
Guido van Rossum <guido@python.org>
parents: 1288
diff changeset
245 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
246 continue;
1290
7971fecbf28e * Lots of small changes related to access.
Guido van Rossum <guido@python.org>
parents: 1289
diff changeset
247 if (hasaccessvalue(value))
7971fecbf28e * Lots of small changes related to access.
Guido van Rossum <guido@python.org>
parents: 1289
diff changeset
248 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
249 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
250 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
251 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
252 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
253 }
4d292c7857d4 Access checks now work, at least for instance data (not for methods
Guido van Rossum <guido@python.org>
parents: 1288
diff changeset
254 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
255 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
256 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
257 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
258 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
259 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
260 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
261 }
4d292c7857d4 Access checks now work, at least for instance data (not for methods
Guido van Rossum <guido@python.org>
parents: 1288
diff changeset
262 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
263 }
4d292c7857d4 Access checks now work, at least for instance data (not for methods
Guido van Rossum <guido@python.org>
parents: 1288
diff changeset
264
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
265 object *
1288
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1272
diff changeset
266 newinstanceobject(class, arg)
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1272
diff changeset
267 object *class;
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1272
diff changeset
268 object *arg;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
269 {
224
e71b6fec86e5 Renamed class methods to instance methods (which they are)
Guido van Rossum <guido@python.org>
parents: 202
diff changeset
270 register instanceobject *inst;
944
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
271 object *v;
1288
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1272
diff changeset
272 object *init;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
273 if (!is_classobject(class)) {
27
82c7bab78bb1 New errors.
Guido van Rossum <guido@python.org>
parents: 7
diff changeset
274 err_badcall();
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
275 return NULL;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
276 }
224
e71b6fec86e5 Renamed class methods to instance methods (which they are)
Guido van Rossum <guido@python.org>
parents: 202
diff changeset
277 inst = NEWOBJ(instanceobject, &Instancetype);
e71b6fec86e5 Renamed class methods to instance methods (which they are)
Guido van Rossum <guido@python.org>
parents: 202
diff changeset
278 if (inst == NULL)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
279 return NULL;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
280 INCREF(class);
224
e71b6fec86e5 Renamed class methods to instance methods (which they are)
Guido van Rossum <guido@python.org>
parents: 202
diff changeset
281 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
282 inst->in_dict = newdictobject();
4d292c7857d4 Access checks now work, at least for instance data (not for methods
Guido van Rossum <guido@python.org>
parents: 1288
diff changeset
283 if (inst->in_dict == NULL ||
4d292c7857d4 Access checks now work, at least for instance data (not for methods
Guido van Rossum <guido@python.org>
parents: 1288
diff changeset
284 addaccess((classobject *)class, inst) != 0) {
224
e71b6fec86e5 Renamed class methods to instance methods (which they are)
Guido van Rossum <guido@python.org>
parents: 202
diff changeset
285 DECREF(inst);
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
286 return NULL;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
287 }
1288
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1272
diff changeset
288 init = instance_getattr(inst, "__init__");
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1272
diff changeset
289 if (init == NULL) {
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1272
diff changeset
290 err_clear();
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1272
diff changeset
291 if (arg != NULL && !(is_tupleobject(arg) &&
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1272
diff changeset
292 gettuplesize(arg) == 0)) {
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1272
diff changeset
293 err_setstr(TypeError,
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1272
diff changeset
294 "this classobject() takes no arguments");
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1272
diff changeset
295 DECREF(inst);
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1272
diff changeset
296 inst = NULL;
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1272
diff changeset
297 }
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1272
diff changeset
298 }
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1272
diff changeset
299 else {
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1272
diff changeset
300 object *res = call_object(init, arg);
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1272
diff changeset
301 DECREF(init);
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1272
diff changeset
302 if (res == NULL) {
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1272
diff changeset
303 DECREF(inst);
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1272
diff changeset
304 inst = NULL;
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1272
diff changeset
305 }
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1272
diff changeset
306 else {
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1272
diff changeset
307 if (res != None) {
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1272
diff changeset
308 err_setstr(TypeError,
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1272
diff changeset
309 "__init__() should return None");
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1272
diff changeset
310 DECREF(inst);
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1272
diff changeset
311 inst = NULL;
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1272
diff changeset
312 }
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1272
diff changeset
313 DECREF(res);
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1272
diff changeset
314 }
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1272
diff changeset
315 }
224
e71b6fec86e5 Renamed class methods to instance methods (which they are)
Guido van Rossum <guido@python.org>
parents: 202
diff changeset
316 return (object *)inst;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
317 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
318
174
20fee4e2eecd Change ugly 'class member' to more normal 'instance'.
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
319 /* Instance methods */
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
320
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
321 static void
224
e71b6fec86e5 Renamed class methods to instance methods (which they are)
Guido van Rossum <guido@python.org>
parents: 202
diff changeset
322 instance_dealloc(inst)
e71b6fec86e5 Renamed class methods to instance methods (which they are)
Guido van Rossum <guido@python.org>
parents: 202
diff changeset
323 register instanceobject *inst;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
324 {
1288
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1272
diff changeset
325 object *error_type, *error_value;
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1272
diff changeset
326 object *del;
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1272
diff changeset
327 /* Call the __del__ method if it exists. First temporarily
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1272
diff changeset
328 revive the object and save the current exception, if any. */
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1272
diff changeset
329 INCREF(inst);
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1272
diff changeset
330 err_get(&error_type, &error_value);
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1272
diff changeset
331 if ((del = instance_getattr(inst, "__del__")) != NULL) {
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1272
diff changeset
332 object *args = newtupleobject(0);
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1272
diff changeset
333 object *res = args;
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1272
diff changeset
334 if (res != NULL)
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1272
diff changeset
335 res = call_object(del, args);
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1272
diff changeset
336 XDECREF(args);
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1272
diff changeset
337 DECREF(del);
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1272
diff changeset
338 XDECREF(res);
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1272
diff changeset
339 /* XXX If __del__ raised an exception, it is ignored! */
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1272
diff changeset
340 }
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1272
diff changeset
341 /* Restore the saved exception and undo the temporary revival */
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1272
diff changeset
342 err_setval(error_type, error_value);
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1272
diff changeset
343 /* Can't use DECREF here, it would cause a recursive call */
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1272
diff changeset
344 if (--inst->ob_refcnt > 0)
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1272
diff changeset
345 return; /* __del__ added a reference; don't delete now */
224
e71b6fec86e5 Renamed class methods to instance methods (which they are)
Guido van Rossum <guido@python.org>
parents: 202
diff changeset
346 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
347 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
348 free((ANY *)inst);
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
349 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
350
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
351 static object *
224
e71b6fec86e5 Renamed class methods to instance methods (which they are)
Guido van Rossum <guido@python.org>
parents: 202
diff changeset
352 instance_getattr(inst, name)
e71b6fec86e5 Renamed class methods to instance methods (which they are)
Guido van Rossum <guido@python.org>
parents: 202
diff changeset
353 register instanceobject *inst;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
354 register char *name;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
355 {
386
0598f4f25d04 Add several secret __*__ attributes
Guido van Rossum <guido@python.org>
parents: 224
diff changeset
356 register object *v;
1289
4d292c7857d4 Access checks now work, at least for instance data (not for methods
Guido van Rossum <guido@python.org>
parents: 1288
diff changeset
357 classobject *class;
386
0598f4f25d04 Add several secret __*__ attributes
Guido van Rossum <guido@python.org>
parents: 224
diff changeset
358 if (strcmp(name, "__dict__") == 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
359 INCREF(inst->in_dict);
4d292c7857d4 Access checks now work, at least for instance data (not for methods
Guido van Rossum <guido@python.org>
parents: 1288
diff changeset
360 return inst->in_dict;
386
0598f4f25d04 Add several secret __*__ attributes
Guido van Rossum <guido@python.org>
parents: 224
diff changeset
361 }
0598f4f25d04 Add several secret __*__ attributes
Guido van Rossum <guido@python.org>
parents: 224
diff changeset
362 if (strcmp(name, "__class__") == 0) {
0598f4f25d04 Add several secret __*__ attributes
Guido van Rossum <guido@python.org>
parents: 224
diff changeset
363 INCREF(inst->in_class);
0598f4f25d04 Add several secret __*__ attributes
Guido van Rossum <guido@python.org>
parents: 224
diff changeset
364 return (object *)inst->in_class;
0598f4f25d04 Add several secret __*__ attributes
Guido van Rossum <guido@python.org>
parents: 224
diff changeset
365 }
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
366 class = 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
367 v = dictlookup(inst->in_dict, name);
1290
7971fecbf28e * Lots of small changes related to access.
Guido van Rossum <guido@python.org>
parents: 1289
diff changeset
368 if (v == NULL) {
7971fecbf28e * Lots of small changes related to access.
Guido van Rossum <guido@python.org>
parents: 1289
diff changeset
369 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
370 if (v == NULL) {
7971fecbf28e * Lots of small changes related to access.
Guido van Rossum <guido@python.org>
parents: 1289
diff changeset
371 err_setstr(AttributeError, name);
7971fecbf28e * Lots of small changes related to access.
Guido van Rossum <guido@python.org>
parents: 1289
diff changeset
372 return NULL;
7971fecbf28e * Lots of small changes related to access.
Guido van Rossum <guido@python.org>
parents: 1289
diff changeset
373 }
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
374 }
1290
7971fecbf28e * Lots of small changes related to access.
Guido van Rossum <guido@python.org>
parents: 1289
diff changeset
375 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
376 v = getaccessvalue(v, getowner());
1290
7971fecbf28e * Lots of small changes related to access.
Guido van Rossum <guido@python.org>
parents: 1289
diff changeset
377 if (v == NULL)
7971fecbf28e * Lots of small changes related to access.
Guido van Rossum <guido@python.org>
parents: 1289
diff changeset
378 return NULL;
7971fecbf28e * Lots of small changes related to access.
Guido van Rossum <guido@python.org>
parents: 1289
diff changeset
379 }
7971fecbf28e * Lots of small changes related to access.
Guido van Rossum <guido@python.org>
parents: 1289
diff changeset
380 else
7971fecbf28e * Lots of small changes related to access.
Guido van Rossum <guido@python.org>
parents: 1289
diff changeset
381 INCREF(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
382 if (is_funcobject(v) && class != NULL) {
1290
7971fecbf28e * Lots of small changes related to access.
Guido van Rossum <guido@python.org>
parents: 1289
diff changeset
383 object *w = newinstancemethodobject(v, (object *)inst,
7971fecbf28e * Lots of small changes related to access.
Guido van Rossum <guido@python.org>
parents: 1289
diff changeset
384 (object *)class);
7971fecbf28e * Lots of small changes related to access.
Guido van Rossum <guido@python.org>
parents: 1289
diff changeset
385 DECREF(v);
7971fecbf28e * Lots of small changes related to access.
Guido van Rossum <guido@python.org>
parents: 1289
diff changeset
386 v = w;
7971fecbf28e * Lots of small changes related to access.
Guido van Rossum <guido@python.org>
parents: 1289
diff changeset
387 }
7971fecbf28e * Lots of small changes related to access.
Guido van Rossum <guido@python.org>
parents: 1289
diff changeset
388 return v;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
389 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
390
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
391 static int
224
e71b6fec86e5 Renamed class methods to instance methods (which they are)
Guido van Rossum <guido@python.org>
parents: 202
diff changeset
392 instance_setattr(inst, name, v)
e71b6fec86e5 Renamed class methods to instance methods (which they are)
Guido van Rossum <guido@python.org>
parents: 202
diff changeset
393 instanceobject *inst;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
394 char *name;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
395 object *v;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
396 {
1288
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1272
diff changeset
397 object *ac;
791
77ee9b222fdb Don't allow assignment to attributes named __*__
Guido van Rossum <guido@python.org>
parents: 735
diff changeset
398 if (name[0] == '_' && name[1] == '_') {
77ee9b222fdb Don't allow assignment to attributes named __*__
Guido van Rossum <guido@python.org>
parents: 735
diff changeset
399 int n = strlen(name);
77ee9b222fdb Don't allow assignment to attributes named __*__
Guido van Rossum <guido@python.org>
parents: 735
diff changeset
400 if (name[n-1] == '_' && name[n-2] == '_') {
77ee9b222fdb Don't allow assignment to attributes named __*__
Guido van Rossum <guido@python.org>
parents: 735
diff changeset
401 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
402 return -1;
77ee9b222fdb Don't allow assignment to attributes named __*__
Guido van Rossum <guido@python.org>
parents: 735
diff changeset
403 }
77ee9b222fdb Don't allow assignment to attributes named __*__
Guido van Rossum <guido@python.org>
parents: 735
diff changeset
404 }
1289
4d292c7857d4 Access checks now work, at least for instance data (not for methods
Guido van Rossum <guido@python.org>
parents: 1288
diff changeset
405 ac = dictlookup(inst->in_dict, name);
1288
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1272
diff changeset
406 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
407 return setaccessvalue(ac, getowner(), v);
1031
7a11a70f7f44 classobject.c moduleobject.c stdwinmodule.c xxobject.c:
Guido van Rossum <guido@python.org>
parents: 1021
diff changeset
408 if (v == 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
409 int rv = dictremove(inst->in_dict, name);
1031
7a11a70f7f44 classobject.c moduleobject.c stdwinmodule.c xxobject.c:
Guido van Rossum <guido@python.org>
parents: 1021
diff changeset
410 if (rv < 0)
7a11a70f7f44 classobject.c moduleobject.c stdwinmodule.c xxobject.c:
Guido van Rossum <guido@python.org>
parents: 1021
diff changeset
411 err_setstr(AttributeError,
7a11a70f7f44 classobject.c moduleobject.c stdwinmodule.c xxobject.c:
Guido van Rossum <guido@python.org>
parents: 1021
diff changeset
412 "delete non-existing instance attribute");
7a11a70f7f44 classobject.c moduleobject.c stdwinmodule.c xxobject.c:
Guido van Rossum <guido@python.org>
parents: 1021
diff changeset
413 return rv;
7a11a70f7f44 classobject.c moduleobject.c stdwinmodule.c xxobject.c:
Guido van Rossum <guido@python.org>
parents: 1021
diff changeset
414 }
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
415 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
416 return dictinsert(inst->in_dict, name, v);
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
417 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
418
1257
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
419 static object *
944
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
420 instance_repr(inst)
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
421 instanceobject *inst;
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
422 {
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
423 object *func;
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
424 object *res;
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
425
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
426 func = instance_getattr(inst, "__repr__");
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
427 if (func == NULL) {
1288
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1272
diff changeset
428 char buf[140];
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1272
diff changeset
429 object *classname = inst->in_class->cl_name;
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1272
diff changeset
430 char *cname;
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1272
diff changeset
431 if (classname != NULL && is_stringobject(classname))
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1272
diff changeset
432 cname = getstringvalue(classname);
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1272
diff changeset
433 else
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1272
diff changeset
434 cname = "?";
944
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
435 err_clear();
1288
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1272
diff changeset
436 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
437 return newstringobject(buf);
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
438 }
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
439 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
440 DECREF(func);
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
441 return res;
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
442 }
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
443
1257
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
444 static int
944
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
445 instance_compare(inst, other)
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
446 instanceobject *inst, *other;
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
447 {
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
448 object *func;
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
449 object *res;
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
450 int outcome;
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
451
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
452 func = instance_getattr(inst, "__cmp__");
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
453 if (func == NULL) {
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
454 err_clear();
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
455 if (inst < other)
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
456 return -1;
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
457 if (inst > other)
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
458 return 1;
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
459 return 0;
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
460 }
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
461 res = call_object(func, (object *)other);
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
462 DECREF(func);
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
463 if (res == NULL) {
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
464 err_clear(); /* XXX Should report the error, bot how...??? */
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
465 return 0;
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
466 }
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
467 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
468 outcome = getintvalue(res);
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
469 else
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
470 outcome = 0; /* XXX Should report the error, bot how...??? */
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
471 DECREF(res);
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
472 return outcome;
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
473 }
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
474
1257
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
475 static long
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
476 instance_hash(inst)
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
477 instanceobject *inst;
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
478 {
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
479 object *func;
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
480 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
481 long outcome;
1257
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
482
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
483 func = instance_getattr(inst, "__hash__");
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
484 if (func == NULL) {
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
485 /* 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
486 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
487 err_clear();
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
488 func = instance_getattr(inst, "__cmp__");
1272
7b7411f9b116 Fix bug in class instance hash (forgot to clear error condition).
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
489 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
490 err_clear();
7b7411f9b116 Fix bug in class instance hash (forgot to clear error condition).
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
491 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
492 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
493 outcome = -2;
7b7411f9b116 Fix bug in class instance hash (forgot to clear error condition).
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
494 return outcome;
7b7411f9b116 Fix bug in class instance hash (forgot to clear error condition).
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
495 }
1257
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
496 err_setstr(TypeError, "unhashable instance");
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
497 return -1;
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
498 }
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
499 res = call_object(func, (object *)NULL);
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
500 DECREF(func);
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
501 if (res == NULL)
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
502 return -1;
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
503 if (is_intobject(res)) {
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
504 outcome = getintvalue(res);
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
505 if (outcome == -1)
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
506 outcome = -2;
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
507 }
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
508 else {
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
509 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
510 outcome = -1;
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
511 }
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
512 DECREF(res);
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
513 return outcome;
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
514 }
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
515
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
516 static int
944
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
517 instance_length(inst)
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
518 instanceobject *inst;
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
519 {
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
520 object *func;
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
521 object *res;
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
522 int outcome;
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
523
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
524 func = instance_getattr(inst, "__len__");
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
525 if (func == NULL)
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
526 return -1;
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
527 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
528 DECREF(func);
1096
ce718457b172 * classobject.c: in instance_lenth, test result of call_object
Guido van Rossum <guido@python.org>
parents: 1051
diff changeset
529 if (res == NULL)
ce718457b172 * classobject.c: in instance_lenth, test result of call_object
Guido van Rossum <guido@python.org>
parents: 1051
diff changeset
530 return -1;
944
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
531 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
532 outcome = getintvalue(res);
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
533 if (outcome < 0)
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
534 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
535 }
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
536 else {
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
537 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
538 outcome = -1;
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
539 }
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
540 DECREF(res);
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
541 return outcome;
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
542 }
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
543
1257
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
544 static object *
944
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
545 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
546 instanceobject *inst;
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
547 object *key;
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
548 {
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
549 object *func;
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
550 object *arg;
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
551 object *res;
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
552
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
553 func = instance_getattr(inst, "__getitem__");
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
554 if (func == NULL)
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
555 return NULL;
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
556 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
557 if (arg == NULL) {
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
558 DECREF(func);
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
559 return NULL;
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
560 }
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
561 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
562 DECREF(func);
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
563 DECREF(arg);
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
564 return res;
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
565 }
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
566
1257
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
567 static int
944
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
568 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
569 instanceobject*inst;
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
570 object *key;
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
571 object *value;
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
572 {
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
573 object *func;
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
574 object *arg;
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
575 object *res;
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
576
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
577 if (value == NULL)
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
578 func = instance_getattr(inst, "__delitem__");
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
579 else
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
580 func = instance_getattr(inst, "__setitem__");
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
581 if (func == NULL)
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
582 return -1;
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
583 if (value == NULL)
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
584 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
585 else
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
586 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
587 if (arg == NULL) {
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
588 DECREF(func);
956
a60cb5321211 * macmodule.c: include allobjects.h, not .c
Guido van Rossum <guido@python.org>
parents: 955
diff changeset
589 return -1;
944
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
590 }
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
591 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
592 DECREF(func);
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
593 DECREF(arg);
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
594 if (res == NULL)
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
595 return -1;
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
596 DECREF(res);
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
597 return 0;
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
598 }
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
599
1257
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
600 static mapping_methods instance_as_mapping = {
944
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
601 instance_length, /*mp_length*/
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
602 instance_subscript, /*mp_subscript*/
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
603 instance_ass_subscript, /*mp_ass_subscript*/
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
604 };
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
605
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
606 static object *
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
607 instance_concat(inst, other)
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
608 instanceobject *inst, *other;
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
609 {
955
03de67080c95 * classobject.[ch], {float,long,int}object.c, bltinmodule.c:
Guido van Rossum <guido@python.org>
parents: 944
diff changeset
610 object *func, *arg, *res;
944
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
611
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
612 func = instance_getattr(inst, "__add__");
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
613 if (func == NULL)
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
614 return NULL;
955
03de67080c95 * classobject.[ch], {float,long,int}object.c, bltinmodule.c:
Guido van Rossum <guido@python.org>
parents: 944
diff changeset
615 arg = mkvalue("(O)", other);
03de67080c95 * classobject.[ch], {float,long,int}object.c, bltinmodule.c:
Guido van Rossum <guido@python.org>
parents: 944
diff changeset
616 if (arg == NULL) {
03de67080c95 * classobject.[ch], {float,long,int}object.c, bltinmodule.c:
Guido van Rossum <guido@python.org>
parents: 944
diff changeset
617 DECREF(func);
03de67080c95 * classobject.[ch], {float,long,int}object.c, bltinmodule.c:
Guido van Rossum <guido@python.org>
parents: 944
diff changeset
618 return NULL;
03de67080c95 * classobject.[ch], {float,long,int}object.c, bltinmodule.c:
Guido van Rossum <guido@python.org>
parents: 944
diff changeset
619 }
03de67080c95 * classobject.[ch], {float,long,int}object.c, bltinmodule.c:
Guido van Rossum <guido@python.org>
parents: 944
diff changeset
620 res = call_object(func, arg);
944
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
621 DECREF(func);
955
03de67080c95 * classobject.[ch], {float,long,int}object.c, bltinmodule.c:
Guido van Rossum <guido@python.org>
parents: 944
diff changeset
622 DECREF(arg);
944
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
623 return res;
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
624 }
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
625
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
626 static object *
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
627 instance_repeat(inst, count)
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
628 instanceobject *inst;
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
629 int count;
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
630 {
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
631 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
632
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
633 func = instance_getattr(inst, "__mul__");
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
634 if (func == NULL)
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
635 return NULL;
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
636 arg = newintobject((long)count);
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
637 if (arg == NULL) {
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
638 DECREF(func);
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
639 return NULL;
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
640 }
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
641 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
642 DECREF(func);
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
643 DECREF(arg);
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
644 return res;
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
645 }
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
646
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
647 static object *
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
648 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
649 instanceobject *inst;
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
650 int i;
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
651 {
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
652 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
653
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
654 func = instance_getattr(inst, "__getitem__");
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
655 if (func == NULL)
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
656 return NULL;
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
657 arg = newintobject((long)i);
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
658 if (arg == NULL) {
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
659 DECREF(func);
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
660 return NULL;
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
661 }
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
662 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
663 DECREF(func);
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
664 DECREF(arg);
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
665 return res;
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
666 }
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
667
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
668 static object *
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
669 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
670 instanceobject *inst;
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
671 int i, j;
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
672 {
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
673 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
674
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
675 func = instance_getattr(inst, "__getslice__");
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
676 if (func == NULL)
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
677 return NULL;
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
678 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
679 if (arg == NULL) {
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
680 DECREF(func);
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
681 return NULL;
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
682 }
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
683 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
684 DECREF(func);
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
685 DECREF(arg);
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
686 return res;
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
687 }
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
688
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
689 static int
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
690 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
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 int i;
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
693 object *item;
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
694 {
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
695 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
696
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
697 if (item == NULL)
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
698 func = instance_getattr(inst, "__delitem__");
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
699 else
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
700 func = instance_getattr(inst, "__setitem__");
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
701 if (func == NULL)
956
a60cb5321211 * macmodule.c: include allobjects.h, not .c
Guido van Rossum <guido@python.org>
parents: 955
diff changeset
702 return -1;
944
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
703 if (item == NULL)
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
704 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
705 else
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
706 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
707 if (arg == NULL) {
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
708 DECREF(func);
956
a60cb5321211 * macmodule.c: include allobjects.h, not .c
Guido van Rossum <guido@python.org>
parents: 955
diff changeset
709 return -1;
944
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 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
712 DECREF(func);
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
713 DECREF(arg);
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
714 if (res == NULL)
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
715 return -1;
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
716 DECREF(res);
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
717 return 0;
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
718 }
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
719
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
720 static int
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
721 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
722 instanceobject *inst;
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
723 int i, j;
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
724 object *value;
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
725 {
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
726 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
727
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
728 if (value == NULL)
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
729 func = instance_getattr(inst, "__delslice__");
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
730 else
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
731 func = instance_getattr(inst, "__setslice__");
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
732 if (func == NULL)
956
a60cb5321211 * macmodule.c: include allobjects.h, not .c
Guido van Rossum <guido@python.org>
parents: 955
diff changeset
733 return -1;
944
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
734 if (value == NULL)
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
735 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
736 else
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
737 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
738 if (arg == NULL) {
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
739 DECREF(func);
956
a60cb5321211 * macmodule.c: include allobjects.h, not .c
Guido van Rossum <guido@python.org>
parents: 955
diff changeset
740 return -1;
944
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
741 }
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
742 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
743 DECREF(func);
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
744 DECREF(arg);
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
745 if (res == NULL)
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
746 return -1;
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
747 DECREF(res);
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
748 return 0;
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
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
751 static sequence_methods instance_as_sequence = {
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
752 instance_length, /*sq_length*/
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
753 instance_concat, /*sq_concat*/
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
754 instance_repeat, /*sq_repeat*/
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
755 instance_item, /*sq_item*/
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
756 instance_slice, /*sq_slice*/
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
757 instance_ass_item, /*sq_ass_item*/
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
758 instance_ass_slice, /*sq_ass_slice*/
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
759 };
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
760
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
761 static object *
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
762 generic_binary_op(self, other, methodname)
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
763 instanceobject *self;
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
764 object *other;
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
765 char *methodname;
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
766 {
955
03de67080c95 * classobject.[ch], {float,long,int}object.c, bltinmodule.c:
Guido van Rossum <guido@python.org>
parents: 944
diff changeset
767 object *func, *arg, *res;
944
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
768
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
769 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
770 return NULL;
955
03de67080c95 * classobject.[ch], {float,long,int}object.c, bltinmodule.c:
Guido van Rossum <guido@python.org>
parents: 944
diff changeset
771 arg = mkvalue("O", other);
03de67080c95 * classobject.[ch], {float,long,int}object.c, bltinmodule.c:
Guido van Rossum <guido@python.org>
parents: 944
diff changeset
772 if (arg == NULL) {
03de67080c95 * classobject.[ch], {float,long,int}object.c, bltinmodule.c:
Guido van Rossum <guido@python.org>
parents: 944
diff changeset
773 DECREF(func);
03de67080c95 * classobject.[ch], {float,long,int}object.c, bltinmodule.c:
Guido van Rossum <guido@python.org>
parents: 944
diff changeset
774 return NULL;
03de67080c95 * classobject.[ch], {float,long,int}object.c, bltinmodule.c:
Guido van Rossum <guido@python.org>
parents: 944
diff changeset
775 }
03de67080c95 * classobject.[ch], {float,long,int}object.c, bltinmodule.c:
Guido van Rossum <guido@python.org>
parents: 944
diff changeset
776 res = call_object(func, arg);
944
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
777 DECREF(func);
955
03de67080c95 * classobject.[ch], {float,long,int}object.c, bltinmodule.c:
Guido van Rossum <guido@python.org>
parents: 944
diff changeset
778 DECREF(arg);
944
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
779 return res;
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
780 }
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 static object *
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
783 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
784 instanceobject *self;
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
785 char *methodname;
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
786 {
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
787 object *func, *res;
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 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
790 return NULL;
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
791 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
792 DECREF(func);
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
793 return res;
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
794 }
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
795
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
796 #define BINARY(funcname, methodname) \
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
797 static object * funcname(self, other) instanceobject *self; object *other; { \
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
798 return generic_binary_op(self, other, methodname); \
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
799 }
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
800
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
801 #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
802 static object *funcname(self) instanceobject *self; { \
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
803 return 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
804 }
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 BINARY(instance_add, "__add__")
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
807 BINARY(instance_sub, "__sub__")
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
808 BINARY(instance_mul, "__mul__")
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
809 BINARY(instance_div, "__div__")
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
810 BINARY(instance_mod, "__mod__")
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
811 BINARY(instance_divmod, "__divmod__")
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
812 BINARY(instance_pow, "__pow__")
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
813 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
814 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
815 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
816
1257
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
817 static int
944
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
818 instance_nonzero(self)
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
819 instanceobject *self;
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
820 {
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
821 object *func, *res;
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
822 long outcome;
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
823
1215
9d6cf529bb71 bltinmodule.c: added round(x, [n]); coerce() of two class instances
Guido van Rossum <guido@python.org>
parents: 1096
diff changeset
824 if ((func = instance_getattr(self, "__nonzero__")) == NULL) {
944
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
825 err_clear();
1215
9d6cf529bb71 bltinmodule.c: added round(x, [n]); coerce() of two class instances
Guido van Rossum <guido@python.org>
parents: 1096
diff changeset
826 if ((func = instance_getattr(self, "__len__")) == NULL) {
944
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
827 err_clear();
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
828 /* 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
829 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
830 return 1;
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
831 }
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
832 }
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
833 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
834 DECREF(func);
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
835 if (res == NULL)
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
836 return -1;
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
837 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
838 DECREF(res);
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
839 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
840 return -1;
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 outcome = getintvalue(res);
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
843 DECREF(res);
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
844 if (outcome < 0) {
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
845 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
846 return -1;
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
847 }
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
848 return outcome > 0;
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
849 }
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
850
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
851 UNARY(instance_invert, "__invert__")
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
852 BINARY(instance_lshift, "__lshift__")
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
853 BINARY(instance_rshift, "__rshift__")
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
854 BINARY(instance_and, "__and__")
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
855 BINARY(instance_xor, "__xor__")
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
856 BINARY(instance_or, "__or__")
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
857
955
03de67080c95 * classobject.[ch], {float,long,int}object.c, bltinmodule.c:
Guido van Rossum <guido@python.org>
parents: 944
diff changeset
858 static int
03de67080c95 * classobject.[ch], {float,long,int}object.c, bltinmodule.c:
Guido van Rossum <guido@python.org>
parents: 944
diff changeset
859 instance_coerce(pv, pw)
03de67080c95 * classobject.[ch], {float,long,int}object.c, bltinmodule.c:
Guido van Rossum <guido@python.org>
parents: 944
diff changeset
860 object **pv, **pw;
03de67080c95 * classobject.[ch], {float,long,int}object.c, bltinmodule.c:
Guido van Rossum <guido@python.org>
parents: 944
diff changeset
861 {
03de67080c95 * classobject.[ch], {float,long,int}object.c, bltinmodule.c:
Guido van Rossum <guido@python.org>
parents: 944
diff changeset
862 object *v = *pv;
03de67080c95 * classobject.[ch], {float,long,int}object.c, bltinmodule.c:
Guido van Rossum <guido@python.org>
parents: 944
diff changeset
863 object *w = *pw;
03de67080c95 * classobject.[ch], {float,long,int}object.c, bltinmodule.c:
Guido van Rossum <guido@python.org>
parents: 944
diff changeset
864 object *func;
03de67080c95 * classobject.[ch], {float,long,int}object.c, bltinmodule.c:
Guido van Rossum <guido@python.org>
parents: 944
diff changeset
865 object *res;
03de67080c95 * classobject.[ch], {float,long,int}object.c, bltinmodule.c:
Guido van Rossum <guido@python.org>
parents: 944
diff changeset
866 int outcome;
03de67080c95 * classobject.[ch], {float,long,int}object.c, bltinmodule.c:
Guido van Rossum <guido@python.org>
parents: 944
diff changeset
867
03de67080c95 * classobject.[ch], {float,long,int}object.c, bltinmodule.c:
Guido van Rossum <guido@python.org>
parents: 944
diff changeset
868 if (!is_instanceobject(v))
03de67080c95 * classobject.[ch], {float,long,int}object.c, bltinmodule.c:
Guido van Rossum <guido@python.org>
parents: 944
diff changeset
869 return 1; /* XXX shouldn't be possible */
03de67080c95 * classobject.[ch], {float,long,int}object.c, bltinmodule.c:
Guido van Rossum <guido@python.org>
parents: 944
diff changeset
870 func = instance_getattr((instanceobject *)v, "__coerce__");
03de67080c95 * classobject.[ch], {float,long,int}object.c, bltinmodule.c:
Guido van Rossum <guido@python.org>
parents: 944
diff changeset
871 if (func == NULL) {
03de67080c95 * classobject.[ch], {float,long,int}object.c, bltinmodule.c:
Guido van Rossum <guido@python.org>
parents: 944
diff changeset
872 err_clear();
03de67080c95 * classobject.[ch], {float,long,int}object.c, bltinmodule.c:
Guido van Rossum <guido@python.org>
parents: 944
diff changeset
873 return 1;
03de67080c95 * classobject.[ch], {float,long,int}object.c, bltinmodule.c:
Guido van Rossum <guido@python.org>
parents: 944
diff changeset
874 }
03de67080c95 * classobject.[ch], {float,long,int}object.c, bltinmodule.c:
Guido van Rossum <guido@python.org>
parents: 944
diff changeset
875 res = call_object(func, w);
03de67080c95 * classobject.[ch], {float,long,int}object.c, bltinmodule.c:
Guido van Rossum <guido@python.org>
parents: 944
diff changeset
876 if (res == NULL)
03de67080c95 * classobject.[ch], {float,long,int}object.c, bltinmodule.c:
Guido van Rossum <guido@python.org>
parents: 944
diff changeset
877 return -1;
03de67080c95 * classobject.[ch], {float,long,int}object.c, bltinmodule.c:
Guido van Rossum <guido@python.org>
parents: 944
diff changeset
878 if (res == None) {
03de67080c95 * classobject.[ch], {float,long,int}object.c, bltinmodule.c:
Guido van Rossum <guido@python.org>
parents: 944
diff changeset
879 DECREF(res);
03de67080c95 * classobject.[ch], {float,long,int}object.c, bltinmodule.c:
Guido van Rossum <guido@python.org>
parents: 944
diff changeset
880 return 1;
03de67080c95 * classobject.[ch], {float,long,int}object.c, bltinmodule.c:
Guido van Rossum <guido@python.org>
parents: 944
diff changeset
881 }
03de67080c95 * classobject.[ch], {float,long,int}object.c, bltinmodule.c:
Guido van Rossum <guido@python.org>
parents: 944
diff changeset
882 outcome = getargs(res, "(OO)", &v, &w);
03de67080c95 * classobject.[ch], {float,long,int}object.c, bltinmodule.c:
Guido van Rossum <guido@python.org>
parents: 944
diff changeset
883 if (!outcome || v->ob_type != w->ob_type ||
03de67080c95 * classobject.[ch], {float,long,int}object.c, bltinmodule.c:
Guido van Rossum <guido@python.org>
parents: 944
diff changeset
884 v->ob_type->tp_as_number == NULL) {
03de67080c95 * classobject.[ch], {float,long,int}object.c, bltinmodule.c:
Guido van Rossum <guido@python.org>
parents: 944
diff changeset
885 DECREF(res);
03de67080c95 * classobject.[ch], {float,long,int}object.c, bltinmodule.c:
Guido van Rossum <guido@python.org>
parents: 944
diff changeset
886 err_setstr(TypeError, "bad __coerce__ result");
03de67080c95 * classobject.[ch], {float,long,int}object.c, bltinmodule.c:
Guido van Rossum <guido@python.org>
parents: 944
diff changeset
887 return -1;
03de67080c95 * classobject.[ch], {float,long,int}object.c, bltinmodule.c:
Guido van Rossum <guido@python.org>
parents: 944
diff changeset
888 }
03de67080c95 * classobject.[ch], {float,long,int}object.c, bltinmodule.c:
Guido van Rossum <guido@python.org>
parents: 944
diff changeset
889 INCREF(v);
03de67080c95 * classobject.[ch], {float,long,int}object.c, bltinmodule.c:
Guido van Rossum <guido@python.org>
parents: 944
diff changeset
890 INCREF(w);
03de67080c95 * classobject.[ch], {float,long,int}object.c, bltinmodule.c:
Guido van Rossum <guido@python.org>
parents: 944
diff changeset
891 DECREF(res);
03de67080c95 * classobject.[ch], {float,long,int}object.c, bltinmodule.c:
Guido van Rossum <guido@python.org>
parents: 944
diff changeset
892 *pv = v;
03de67080c95 * classobject.[ch], {float,long,int}object.c, bltinmodule.c:
Guido van Rossum <guido@python.org>
parents: 944
diff changeset
893 *pw = w;
03de67080c95 * classobject.[ch], {float,long,int}object.c, bltinmodule.c:
Guido van Rossum <guido@python.org>
parents: 944
diff changeset
894 return 0;
03de67080c95 * classobject.[ch], {float,long,int}object.c, bltinmodule.c:
Guido van Rossum <guido@python.org>
parents: 944
diff changeset
895 }
03de67080c95 * classobject.[ch], {float,long,int}object.c, bltinmodule.c:
Guido van Rossum <guido@python.org>
parents: 944
diff changeset
896
1049
2ed511171809 Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents: 1031
diff changeset
897 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
898 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
899 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
900 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
901 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
902
944
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
903 static number_methods instance_as_number = {
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
904 instance_add, /*nb_add*/
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
905 instance_sub, /*nb_subtract*/
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
906 instance_mul, /*nb_multiply*/
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
907 instance_div, /*nb_divide*/
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
908 instance_mod, /*nb_remainder*/
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
909 instance_divmod, /*nb_divmod*/
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
910 instance_pow, /*nb_power*/
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
911 instance_neg, /*nb_negative*/
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
912 instance_pos, /*nb_positive*/
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
913 instance_abs, /*nb_absolute*/
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
914 instance_nonzero, /*nb_nonzero*/
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
915 instance_invert, /*nb_invert*/
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
916 instance_lshift, /*nb_lshift*/
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
917 instance_rshift, /*nb_rshift*/
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
918 instance_and, /*nb_and*/
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
919 instance_xor, /*nb_xor*/
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
920 instance_or, /*nb_or*/
955
03de67080c95 * classobject.[ch], {float,long,int}object.c, bltinmodule.c:
Guido van Rossum <guido@python.org>
parents: 944
diff changeset
921 instance_coerce, /*nb_coerce*/
1049
2ed511171809 Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents: 1031
diff changeset
922 instance_int, /*nb_int*/
2ed511171809 Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents: 1031
diff changeset
923 instance_long, /*nb_long*/
2ed511171809 Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents: 1031
diff changeset
924 instance_float, /*nb_float*/
2ed511171809 Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents: 1031
diff changeset
925 instance_oct, /*nb_oct*/
2ed511171809 Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents: 1031
diff changeset
926 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
927 };
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
928
174
20fee4e2eecd Change ugly 'class member' to more normal 'instance'.
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
929 typeobject Instancetype = {
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
930 OB_HEAD_INIT(&Typetype)
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
931 0,
174
20fee4e2eecd Change ugly 'class member' to more normal 'instance'.
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
932 "instance",
20fee4e2eecd Change ugly 'class member' to more normal 'instance'.
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
933 sizeof(instanceobject),
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
934 0,
174
20fee4e2eecd Change ugly 'class member' to more normal 'instance'.
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
935 instance_dealloc, /*tp_dealloc*/
1051
f54e64c8b9b7 * Makefile: added IMGFILE; moved some stuff around.
Guido van Rossum <guido@python.org>
parents: 1049
diff changeset
936 0, /*tp_print*/
1288
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1272
diff changeset
937 (object * (*) FPROTO((object *, char *)))
174
20fee4e2eecd Change ugly 'class member' to more normal 'instance'.
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
938 instance_getattr, /*tp_getattr*/
20fee4e2eecd Change ugly 'class member' to more normal 'instance'.
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
939 instance_setattr, /*tp_setattr*/
944
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
940 instance_compare, /*tp_compare*/
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
941 instance_repr, /*tp_repr*/
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
942 &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
943 &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
944 &instance_as_mapping, /*tp_as_mapping*/
1257
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
945 instance_hash, /*tp_hash*/
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
946 };
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
947
1257
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
948 static object *
944
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
949 instance_convert(inst, methodname)
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
950 object *inst;
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
951 char *methodname;
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
952 {
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
953 return generic_unary_op((instanceobject *)inst, methodname);
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
954 }
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 791
diff changeset
955
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
956
1289
4d292c7857d4 Access checks now work, at least for instance data (not for methods
Guido van Rossum <guido@python.org>
parents: 1288
diff changeset
957 /* 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
958 (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
959 (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
960 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
961 */
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
962
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
963 typedef struct {
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
964 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
965 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
966 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
967 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
968 } instancemethodobject;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
969
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
970 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
971 newinstancemethodobject(func, self, class)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
972 object *func;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
973 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
974 object *class;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
975 {
224
e71b6fec86e5 Renamed class methods to instance methods (which they are)
Guido van Rossum <guido@python.org>
parents: 202
diff changeset
976 register instancemethodobject *im;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
977 if (!is_funcobject(func)) {
27
82c7bab78bb1 New errors.
Guido van Rossum <guido@python.org>
parents: 7
diff changeset
978 err_badcall();
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
979 return NULL;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
980 }
224
e71b6fec86e5 Renamed class methods to instance methods (which they are)
Guido van Rossum <guido@python.org>
parents: 202
diff changeset
981 im = NEWOBJ(instancemethodobject, &Instancemethodtype);
e71b6fec86e5 Renamed class methods to instance methods (which they are)
Guido van Rossum <guido@python.org>
parents: 202
diff changeset
982 if (im == NULL)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
983 return NULL;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
984 INCREF(func);
224
e71b6fec86e5 Renamed class methods to instance methods (which they are)
Guido van Rossum <guido@python.org>
parents: 202
diff changeset
985 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
986 XINCREF(self);
224
e71b6fec86e5 Renamed class methods to instance methods (which they are)
Guido van Rossum <guido@python.org>
parents: 202
diff changeset
987 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
988 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
989 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
990 return (object *)im;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
991 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
992
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
993 object *
224
e71b6fec86e5 Renamed class methods to instance methods (which they are)
Guido van Rossum <guido@python.org>
parents: 202
diff changeset
994 instancemethodgetfunc(im)
e71b6fec86e5 Renamed class methods to instance methods (which they are)
Guido van Rossum <guido@python.org>
parents: 202
diff changeset
995 register object *im;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
996 {
224
e71b6fec86e5 Renamed class methods to instance methods (which they are)
Guido van Rossum <guido@python.org>
parents: 202
diff changeset
997 if (!is_instancemethodobject(im)) {
27
82c7bab78bb1 New errors.
Guido van Rossum <guido@python.org>
parents: 7
diff changeset
998 err_badcall();
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
999 return NULL;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1000 }
224
e71b6fec86e5 Renamed class methods to instance methods (which they are)
Guido van Rossum <guido@python.org>
parents: 202
diff changeset
1001 return ((instancemethodobject *)im)->im_func;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1002 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1003
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1004 object *
224
e71b6fec86e5 Renamed class methods to instance methods (which they are)
Guido van Rossum <guido@python.org>
parents: 202
diff changeset
1005 instancemethodgetself(im)
e71b6fec86e5 Renamed class methods to instance methods (which they are)
Guido van Rossum <guido@python.org>
parents: 202
diff changeset
1006 register object *im;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1007 {
224
e71b6fec86e5 Renamed class methods to instance methods (which they are)
Guido van Rossum <guido@python.org>
parents: 202
diff changeset
1008 if (!is_instancemethodobject(im)) {
27
82c7bab78bb1 New errors.
Guido van Rossum <guido@python.org>
parents: 7
diff changeset
1009 err_badcall();
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1010 return NULL;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1011 }
224
e71b6fec86e5 Renamed class methods to instance methods (which they are)
Guido van Rossum <guido@python.org>
parents: 202
diff changeset
1012 return ((instancemethodobject *)im)->im_self;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1013 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1014
1289
4d292c7857d4 Access checks now work, at least for instance data (not for methods
Guido van Rossum <guido@python.org>
parents: 1288
diff changeset
1015 object *
4d292c7857d4 Access checks now work, at least for instance data (not for methods
Guido van Rossum <guido@python.org>
parents: 1288
diff changeset
1016 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
1017 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
1018 {
4d292c7857d4 Access checks now work, at least for instance data (not for methods
Guido van Rossum <guido@python.org>
parents: 1288
diff changeset
1019 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
1020 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
1021 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
1022 }
4d292c7857d4 Access checks now work, at least for instance data (not for methods
Guido van Rossum <guido@python.org>
parents: 1288
diff changeset
1023 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
1024 }
4d292c7857d4 Access checks now work, at least for instance data (not for methods
Guido van Rossum <guido@python.org>
parents: 1288
diff changeset
1025
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1026 /* Class method methods */
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1027
224
e71b6fec86e5 Renamed class methods to instance methods (which they are)
Guido van Rossum <guido@python.org>
parents: 202
diff changeset
1028 #define OFF(x) offsetof(instancemethodobject, x)
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 27
diff changeset
1029
224
e71b6fec86e5 Renamed class methods to instance methods (which they are)
Guido van Rossum <guido@python.org>
parents: 202
diff changeset
1030 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
1031 {"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
1032 {"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
1033 {"im_class", T_OBJECT, OFF(im_class)},
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 27
diff changeset
1034 {NULL} /* Sentinel */
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 27
diff changeset
1035 };
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 27
diff changeset
1036
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 27
diff changeset
1037 static object *
224
e71b6fec86e5 Renamed class methods to instance methods (which they are)
Guido van Rossum <guido@python.org>
parents: 202
diff changeset
1038 instancemethod_getattr(im, name)
e71b6fec86e5 Renamed class methods to instance methods (which they are)
Guido van Rossum <guido@python.org>
parents: 202
diff changeset
1039 register instancemethodobject *im;
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 27
diff changeset
1040 char *name;
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 27
diff changeset
1041 {
224
e71b6fec86e5 Renamed class methods to instance methods (which they are)
Guido van Rossum <guido@python.org>
parents: 202
diff changeset
1042 return getmember((char *)im, instancemethod_memberlist, name);
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 27
diff changeset
1043 }
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 27
diff changeset
1044
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1045 static void
224
e71b6fec86e5 Renamed class methods to instance methods (which they are)
Guido van Rossum <guido@python.org>
parents: 202
diff changeset
1046 instancemethod_dealloc(im)
e71b6fec86e5 Renamed class methods to instance methods (which they are)
Guido van Rossum <guido@python.org>
parents: 202
diff changeset
1047 register instancemethodobject *im;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1048 {
224
e71b6fec86e5 Renamed class methods to instance methods (which they are)
Guido van Rossum <guido@python.org>
parents: 202
diff changeset
1049 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
1050 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
1051 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
1052 free((ANY *)im);
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1053 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1054
1021
0d7c82366704 Compare instance methods by comparing the object and the function.
Guido van Rossum <guido@python.org>
parents: 956
diff changeset
1055 static int
0d7c82366704 Compare instance methods by comparing the object and the function.
Guido van Rossum <guido@python.org>
parents: 956
diff changeset
1056 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
1057 instancemethodobject *a, *b;
0d7c82366704 Compare instance methods by comparing the object and the function.
Guido van Rossum <guido@python.org>
parents: 956
diff changeset
1058 {
0d7c82366704 Compare instance methods by comparing the object and the function.
Guido van Rossum <guido@python.org>
parents: 956
diff changeset
1059 int cmp = cmpobject(a->im_self, b->im_self);
0d7c82366704 Compare instance methods by comparing the object and the function.
Guido van Rossum <guido@python.org>
parents: 956
diff changeset
1060 if (cmp == 0)
0d7c82366704 Compare instance methods by comparing the object and the function.
Guido van Rossum <guido@python.org>
parents: 956
diff changeset
1061 cmp = cmpobject(a->im_func, b->im_func);
0d7c82366704 Compare instance methods by comparing the object and the function.
Guido van Rossum <guido@python.org>
parents: 956
diff changeset
1062 return cmp;
0d7c82366704 Compare instance methods by comparing the object and the function.
Guido van Rossum <guido@python.org>
parents: 956
diff changeset
1063 }
0d7c82366704 Compare instance methods by comparing the object and the function.
Guido van Rossum <guido@python.org>
parents: 956
diff changeset
1064
1288
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1272
diff changeset
1065 static object *
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1272
diff changeset
1066 instancemethod_repr(a)
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1272
diff changeset
1067 instancemethodobject *a;
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1272
diff changeset
1068 {
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1272
diff changeset
1069 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
1070 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
1071 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
1072 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
1073 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
1074 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
1075 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
1076 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
1077 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
1078 fcname = getstringvalue(fclassname);
1288
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1272
diff changeset
1079 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
1080 fcname = "?";
1288
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1272
diff changeset
1081 if (funcname != NULL && is_stringobject(funcname))
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1272
diff changeset
1082 fname = getstringvalue(funcname);
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1272
diff changeset
1083 else
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1272
diff changeset
1084 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
1085 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
1086 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
1087 else {
4d292c7857d4 Access checks now work, at least for instance data (not for methods
Guido van Rossum <guido@python.org>
parents: 1288
diff changeset
1088 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
1089 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
1090 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
1091 else
4d292c7857d4 Access checks now work, at least for instance data (not for methods
Guido van Rossum <guido@python.org>
parents: 1288
diff changeset
1092 icname = "?";
4d292c7857d4 Access checks now work, at least for instance data (not for methods
Guido van Rossum <guido@python.org>
parents: 1288
diff changeset
1093 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
1094 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
1095 }
1288
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1272
diff changeset
1096 return newstringobject(buf);
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1272
diff changeset
1097 }
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1272
diff changeset
1098
1257
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
1099 static long
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
1100 instancemethod_hash(a)
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
1101 instancemethodobject *a;
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
1102 {
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
1103 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
1104 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
1105 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
1106 else
4d292c7857d4 Access checks now work, at least for instance data (not for methods
Guido van Rossum <guido@python.org>
parents: 1288
diff changeset
1107 x = hashobject(a->im_self);
1257
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
1108 if (x == -1)
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
1109 return -1;
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
1110 y = hashobject(a->im_func);
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
1111 if (y == -1)
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
1112 return -1;
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
1113 return x ^ y;
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
1114 }
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
1115
224
e71b6fec86e5 Renamed class methods to instance methods (which they are)
Guido van Rossum <guido@python.org>
parents: 202
diff changeset
1116 typeobject Instancemethodtype = {
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1117 OB_HEAD_INIT(&Typetype)
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1118 0,
202
bfffc7e0fcc3 Rename class methods to instance methods (at least where user-visible)
Guido van Rossum <guido@python.org>
parents: 174
diff changeset
1119 "instance method",
224
e71b6fec86e5 Renamed class methods to instance methods (which they are)
Guido van Rossum <guido@python.org>
parents: 202
diff changeset
1120 sizeof(instancemethodobject),
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1121 0,
224
e71b6fec86e5 Renamed class methods to instance methods (which they are)
Guido van Rossum <guido@python.org>
parents: 202
diff changeset
1122 instancemethod_dealloc, /*tp_dealloc*/
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1123 0, /*tp_print*/
224
e71b6fec86e5 Renamed class methods to instance methods (which they are)
Guido van Rossum <guido@python.org>
parents: 202
diff changeset
1124 instancemethod_getattr, /*tp_getattr*/
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1125 0, /*tp_setattr*/
1021
0d7c82366704 Compare instance methods by comparing the object and the function.
Guido van Rossum <guido@python.org>
parents: 956
diff changeset
1126 instancemethod_compare, /*tp_compare*/
1288
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1272
diff changeset
1127 instancemethod_repr, /*tp_repr*/
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1128 0, /*tp_as_number*/
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1129 0, /*tp_as_sequence*/
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1130 0, /*tp_as_mapping*/
1257
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
1131 instancemethod_hash, /*tp_hash*/
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1132 };