1 /***********************************************************
2 Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
7 Permission to use, copy, modify, and distribute this software and its
8 documentation for any purpose and without fee is hereby granted,
9 provided that the above copyright notice appear in all copies and that
10 both that copyright notice and this permission notice appear in
11 supporting documentation, and that the names of Stichting Mathematisch
12 Centrum or CWI not be used in advertising or publicity pertaining to
13 distribution of the software without specific, written prior permission.
15 STICHTING MATHEMATISCH CENTRUM DISCLAIMS ALL WARRANTIES WITH REGARD TO
16 THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
17 FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH CENTRUM BE LIABLE
18 FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
19 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
20 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
21 OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
23 ******************************************************************/
25 /* Map C struct members to Python object attributes */
27 #include "allobjects.h"
29 #include "structmember.h"
33 struct memberlist
*mlist
;
37 for (n
= 0; mlist
[n
].name
!= NULL
; n
++)
41 for (i
= 0; i
< n
; i
++)
42 setlistitem(v
, i
, newstringobject(mlist
[i
].name
));
55 getmember(addr
, mlist
, name
)
57 struct memberlist
*mlist
;
62 if (strcmp(name
, "__members__") == 0)
63 return listmembers(mlist
);
64 for (l
= mlist
; l
->name
!= NULL
; l
++) {
65 if (strcmp(l
->name
, name
) == 0) {
70 v
= newintobject((long)
71 (((*(char*)addr
& 0xff)
75 v
= newintobject((long) *(char*)addr
& 0xff);
78 v
= newintobject((long) *(short*)addr
);
81 v
= newintobject((long)
82 *(unsigned short*)addr
);
85 v
= newintobject((long) *(int*)addr
);
88 v
= newintobject((long) *(unsigned int*)addr
);
91 v
= newintobject(*(long*)addr
);
94 v
= dnewlongobject((double)
95 *(unsigned long*)addr
);
98 v
= newfloatobject((double)*(float*)addr
);
101 v
= newfloatobject(*(double*)addr
);
104 if (*(char**)addr
== NULL
) {
109 v
= newstringobject(*(char**)addr
);
111 case T_STRING_INPLACE
:
112 v
= newstringobject((char*)addr
);
116 if (*(char**)addr
== NULL
) {
121 v
= newsizedstringobject((*(char**)addr
)+1,
122 **(unsigned char**)addr
);
124 case T_PSTRING_INPLACE
:
125 v
= newsizedstringobject(((char*)addr
)+1,
126 *(unsigned char*)addr
);
128 #endif /* macintosh */
130 v
= newsizedstringobject((char*)addr
, 1);
133 v
= *(object
**)addr
;
139 err_setstr(SystemError
, "bad memberlist type");
146 err_setstr(AttributeError
, name
);
151 setmember(addr
, mlist
, name
, v
)
153 struct memberlist
*mlist
;
157 struct memberlist
*l
;
159 for (l
= mlist
; l
->name
!= NULL
; l
++) {
160 if (strcmp(l
->name
, name
) == 0) {
162 if (l
->readonly
|| l
->type
== T_STRING
|| l
->type
== T_PSTRING
) {
164 if (l
->readonly
|| l
->type
== T_STRING
) {
165 #endif /* macintosh */
166 err_setstr(TypeError
, "readonly attribute");
169 if (v
== NULL
&& l
->type
!= T_OBJECT
) {
170 err_setstr(TypeError
,
171 "can't delete numeric/char attribute");
178 if (!is_intobject(v
)) {
182 *(char*)addr
= getintvalue(v
);
186 if (!is_intobject(v
)) {
190 *(short*)addr
= getintvalue(v
);
194 if (!is_intobject(v
)) {
198 *(int*)addr
= getintvalue(v
);
201 if (!is_intobject(v
)) {
205 *(long*)addr
= getintvalue(v
);
209 *(long*)addr
= getintvalue(v
);
210 else if (is_longobject(v
))
211 *(long*)addr
= getlongvalue(v
);
219 *(float*)addr
= getintvalue(v
);
220 else if (is_floatobject(v
))
221 *(float*)addr
= getfloatvalue(v
);
229 *(double*)addr
= getintvalue(v
);
230 else if (is_floatobject(v
))
231 *(double*)addr
= getfloatvalue(v
);
238 XDECREF(*(object
**)addr
);
240 *(object
**)addr
= v
;
243 if (is_stringobject(v
) &&
244 getstringsize(v
) == 1) {
246 getstringvalue(v
)[0];
253 err_setstr(SystemError
, "bad memberlist type");
260 err_setstr(AttributeError
, name
);