blob: 81a52030ce8e8164a7dbdd26e86e8fc6a56b3acf [file] [log] [blame]
Guido van Rossumf70e43a1991-02-19 12:39:46 +00001/***********************************************************
Guido van Rossum6d023c91995-01-04 19:12:13 +00002Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
3The Netherlands.
Guido van Rossumf70e43a1991-02-19 12:39:46 +00004
5 All Rights Reserved
6
7Permission to use, copy, modify, and distribute this software and its
8documentation for any purpose and without fee is hereby granted,
9provided that the above copyright notice appear in all copies and that
10both that copyright notice and this permission notice appear in
11supporting documentation, and that the names of Stichting Mathematisch
12Centrum or CWI not be used in advertising or publicity pertaining to
13distribution of the software without specific, written prior permission.
14
15STICHTING MATHEMATISCH CENTRUM DISCLAIMS ALL WARRANTIES WITH REGARD TO
16THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
17FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH CENTRUM BE LIABLE
18FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
19WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
20ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
21OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
22
23******************************************************************/
24
Guido van Rossum3f5da241990-12-20 15:06:42 +000025/* Map C struct members to Python object attributes */
26
27#include "allobjects.h"
28
29#include "structmember.h"
30
Guido van Rossum2f1d87e1991-10-20 20:24:14 +000031static object *
32listmembers(mlist)
33 struct memberlist *mlist;
34{
35 int i, n;
36 object *v;
37 for (n = 0; mlist[n].name != NULL; n++)
38 ;
39 v = newlistobject(n);
40 if (v != NULL) {
41 for (i = 0; i < n; i++)
42 setlistitem(v, i, newstringobject(mlist[i].name));
43 if (err_occurred()) {
44 DECREF(v);
45 v = NULL;
46 }
47 else {
48 sortlist(v);
49 }
50 }
51 return v;
52}
53
Guido van Rossum3f5da241990-12-20 15:06:42 +000054object *
55getmember(addr, mlist, name)
56 char *addr;
57 struct memberlist *mlist;
58 char *name;
59{
60 struct memberlist *l;
61
Guido van Rossum2f1d87e1991-10-20 20:24:14 +000062 if (strcmp(name, "__members__") == 0)
63 return listmembers(mlist);
Guido van Rossum3f5da241990-12-20 15:06:42 +000064 for (l = mlist; l->name != NULL; l++) {
65 if (strcmp(l->name, name) == 0) {
66 object *v;
67 addr += l->offset;
68 switch (l->type) {
Guido van Rossumab2490a1992-06-03 17:07:23 +000069 case T_BYTE:
70 v = newintobject((long)
71 (((*(char*)addr & 0xff)
72 ^ 0x80) - 0x80));
73 break;
74 case T_UBYTE:
75 v = newintobject((long) *(char*)addr & 0xff);
76 break;
Guido van Rossum3f5da241990-12-20 15:06:42 +000077 case T_SHORT:
78 v = newintobject((long) *(short*)addr);
79 break;
Guido van Rossumab2490a1992-06-03 17:07:23 +000080 case T_USHORT:
81 v = newintobject((long)
82 *(unsigned short*)addr);
83 break;
Guido van Rossum3f5da241990-12-20 15:06:42 +000084 case T_INT:
85 v = newintobject((long) *(int*)addr);
86 break;
Guido van Rossumab2490a1992-06-03 17:07:23 +000087 case T_UINT:
88 v = newintobject((long) *(unsigned int*)addr);
89 break;
Guido van Rossum3f5da241990-12-20 15:06:42 +000090 case T_LONG:
91 v = newintobject(*(long*)addr);
92 break;
Guido van Rossumab2490a1992-06-03 17:07:23 +000093 case T_ULONG:
94 v = dnewlongobject((double)
95 *(unsigned long*)addr);
96 break;
Guido van Rossum3f5da241990-12-20 15:06:42 +000097 case T_FLOAT:
98 v = newfloatobject((double)*(float*)addr);
99 break;
100 case T_DOUBLE:
101 v = newfloatobject(*(double*)addr);
102 break;
103 case T_STRING:
104 if (*(char**)addr == NULL) {
105 INCREF(None);
106 v = None;
107 }
108 else
109 v = newstringobject(*(char**)addr);
110 break;
Jack Jansen599f0d11994-12-14 13:04:05 +0000111 case T_STRING_INPLACE:
112 v = newstringobject((char*)addr);
113 break;
114#ifdef macintosh
115 case T_PSTRING:
116 if (*(char**)addr == NULL) {
117 INCREF(None);
118 v = None;
119 }
120 else
121 v = newsizedstringobject((*(char**)addr)+1,
122 **(unsigned char**)addr);
123 break;
124 case T_PSTRING_INPLACE:
125 v = newsizedstringobject(((char*)addr)+1,
126 *(unsigned char*)addr);
127 break;
128#endif /* macintosh */
Guido van Rossumab2490a1992-06-03 17:07:23 +0000129 case T_CHAR:
130 v = newsizedstringobject((char*)addr, 1);
131 break;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000132 case T_OBJECT:
133 v = *(object **)addr;
134 if (v == NULL)
135 v = None;
136 INCREF(v);
137 break;
138 default:
139 err_setstr(SystemError, "bad memberlist type");
140 v = NULL;
141 }
142 return v;
143 }
144 }
145
Guido van Rossum87e7ea71991-12-10 14:00:03 +0000146 err_setstr(AttributeError, name);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000147 return NULL;
148}
149
150int
151setmember(addr, mlist, name, v)
152 char *addr;
153 struct memberlist *mlist;
154 char *name;
155 object *v;
156{
157 struct memberlist *l;
158
159 for (l = mlist; l->name != NULL; l++) {
160 if (strcmp(l->name, name) == 0) {
Jack Jansen599f0d11994-12-14 13:04:05 +0000161#ifdef macintosh
162 if (l->readonly || l->type == T_STRING || l->type == T_PSTRING) {
163#else
164 if (l->readonly || l->type == T_STRING ) {
165#endif /* macintosh */
Guido van Rossum87e7ea71991-12-10 14:00:03 +0000166 err_setstr(TypeError, "readonly attribute");
Guido van Rossum3f5da241990-12-20 15:06:42 +0000167 return -1;
168 }
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000169 if (v == NULL && l->type != T_OBJECT) {
170 err_setstr(TypeError,
171 "can't delete numeric/char attribute");
172 return -1;
173 }
Guido van Rossum3f5da241990-12-20 15:06:42 +0000174 addr += l->offset;
175 switch (l->type) {
Guido van Rossumab2490a1992-06-03 17:07:23 +0000176 case T_BYTE:
177 case T_UBYTE:
178 if (!is_intobject(v)) {
179 err_badarg();
180 return -1;
181 }
182 *(char*)addr = getintvalue(v);
183 break;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000184 case T_SHORT:
Guido van Rossumab2490a1992-06-03 17:07:23 +0000185 case T_USHORT:
Guido van Rossum3f5da241990-12-20 15:06:42 +0000186 if (!is_intobject(v)) {
187 err_badarg();
188 return -1;
189 }
190 *(short*)addr = getintvalue(v);
191 break;
Guido van Rossumab2490a1992-06-03 17:07:23 +0000192 case T_UINT:
Guido van Rossum3f5da241990-12-20 15:06:42 +0000193 case T_INT:
194 if (!is_intobject(v)) {
195 err_badarg();
196 return -1;
197 }
198 *(int*)addr = getintvalue(v);
199 break;
200 case T_LONG:
201 if (!is_intobject(v)) {
202 err_badarg();
203 return -1;
204 }
205 *(long*)addr = getintvalue(v);
206 break;
Guido van Rossumab2490a1992-06-03 17:07:23 +0000207 case T_ULONG:
208 if (is_intobject(v))
209 *(long*)addr = getintvalue(v);
210 else if (is_longobject(v))
211 *(long*)addr = getlongvalue(v);
212 else {
213 err_badarg();
214 return -1;
215 }
216 break;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000217 case T_FLOAT:
218 if (is_intobject(v))
219 *(float*)addr = getintvalue(v);
220 else if (is_floatobject(v))
221 *(float*)addr = getfloatvalue(v);
222 else {
223 err_badarg();
224 return -1;
225 }
226 break;
227 case T_DOUBLE:
228 if (is_intobject(v))
229 *(double*)addr = getintvalue(v);
230 else if (is_floatobject(v))
231 *(double*)addr = getfloatvalue(v);
232 else {
233 err_badarg();
234 return -1;
235 }
236 break;
237 case T_OBJECT:
238 XDECREF(*(object **)addr);
239 XINCREF(v);
240 *(object **)addr = v;
241 break;
Guido van Rossumab2490a1992-06-03 17:07:23 +0000242 case T_CHAR:
243 if (is_stringobject(v) &&
244 getstringsize(v) == 1) {
245 *(char*)addr =
246 getstringvalue(v)[0];
247 }
248 else {
249 err_badarg();
250 return -1;
251 }
Guido van Rossum3f5da241990-12-20 15:06:42 +0000252 default:
253 err_setstr(SystemError, "bad memberlist type");
254 return -1;
255 }
256 return 0;
257 }
258 }
259
Guido van Rossum87e7ea71991-12-10 14:00:03 +0000260 err_setstr(AttributeError, name);
Guido van Rossum73531a31990-12-20 23:12:40 +0000261 return -1;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000262}