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 or Corporation for National Research Initiatives or
13 CNRI not be used in advertising or publicity pertaining to
14 distribution of the software without specific, written prior
17 While CWI is the initial source for this software, a modified version
18 is made available by the Corporation for National Research Initiatives
19 (CNRI) at the Internet address ftp://ftp.python.org.
21 STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH
22 REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF
23 MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH
24 CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
25 DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
26 PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
27 TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
28 PERFORMANCE OF THIS SOFTWARE.
30 ******************************************************************/
32 /* Map C struct members to Python object attributes */
36 #include "structmember.h"
40 struct memberlist
*mlist
;
44 for (n
= 0; mlist
[n
].name
!= NULL
; n
++)
48 for (i
= 0; i
< n
; i
++)
50 PyString_FromString(mlist
[i
].name
));
51 if (PyErr_Occurred()) {
63 PyMember_Get(addr
, mlist
, name
)
65 struct memberlist
*mlist
;
70 if (strcmp(name
, "__members__") == 0)
71 return listmembers(mlist
);
72 for (l
= mlist
; l
->name
!= NULL
; l
++) {
73 if (strcmp(l
->name
, name
) == 0) {
78 v
= PyInt_FromLong((long)
79 (((*(char*)addr
& 0xff)
83 v
= PyInt_FromLong((long) *(char*)addr
& 0xff);
86 v
= PyInt_FromLong((long) *(short*)addr
);
89 v
= PyInt_FromLong((long)
90 *(unsigned short*)addr
);
93 v
= PyInt_FromLong((long) *(int*)addr
);
96 v
= PyInt_FromLong((long)
97 *(unsigned int*)addr
);
100 v
= PyInt_FromLong(*(long*)addr
);
103 v
= PyLong_FromDouble((double)
104 *(unsigned long*)addr
);
107 v
= PyFloat_FromDouble((double)*(float*)addr
);
110 v
= PyFloat_FromDouble(*(double*)addr
);
113 if (*(char**)addr
== NULL
) {
118 v
= PyString_FromString(*(char**)addr
);
120 case T_STRING_INPLACE
:
121 v
= PyString_FromString((char*)addr
);
125 if (*(char**)addr
== NULL
) {
130 v
= PyString_FromStringAndSize(
132 **(unsigned char**)addr
);
134 case T_PSTRING_INPLACE
:
135 v
= PyString_FromStringAndSize(
137 *(unsigned char*)addr
);
139 #endif /* macintosh */
141 v
= PyString_FromStringAndSize((char*)addr
, 1);
144 v
= *(PyObject
**)addr
;
150 PyErr_SetString(PyExc_SystemError
,
151 "bad memberlist type");
158 PyErr_SetString(PyExc_AttributeError
, name
);
163 PyMember_Set(addr
, mlist
, name
, v
)
165 struct memberlist
*mlist
;
169 struct memberlist
*l
;
172 for (l
= mlist
; l
->name
!= NULL
; l
++) {
173 if (strcmp(l
->name
, name
) == 0) {
175 if (l
->readonly
|| l
->type
== T_STRING
||
176 l
->type
== T_PSTRING
)
179 if (l
->readonly
|| l
->type
== T_STRING
) {
180 #endif /* macintosh */
181 PyErr_SetString(PyExc_TypeError
,
182 "readonly attribute");
185 if (v
== NULL
&& l
->type
!= T_OBJECT
) {
186 PyErr_SetString(PyExc_TypeError
,
187 "can't delete numeric/char attribute");
194 if (!PyInt_Check(v
)) {
198 *(char*)addr
= (char) PyInt_AsLong(v
);
202 if (!PyInt_Check(v
)) {
206 *(short*)addr
= (short) PyInt_AsLong(v
);
210 if (!PyInt_Check(v
)) {
214 *(int*)addr
= (int) PyInt_AsLong(v
);
217 if (!PyInt_Check(v
)) {
221 *(long*)addr
= PyInt_AsLong(v
);
225 *(long*)addr
= PyInt_AsLong(v
);
226 else if (PyLong_Check(v
))
227 *(long*)addr
= PyLong_AsLong(v
);
236 (float) PyInt_AsLong(v
);
237 else if (PyFloat_Check(v
))
239 (float) PyFloat_AsDouble(v
);
248 (double) PyInt_AsLong(v
);
249 else if (PyFloat_Check(v
))
250 *(double*)addr
= PyFloat_AsDouble(v
);
258 oldv
= *(PyObject
**)addr
;
259 *(PyObject
**)addr
= v
;
263 if (PyString_Check(v
) &&
264 PyString_Size(v
) == 1) {
266 PyString_AsString(v
)[0];
273 PyErr_SetString(PyExc_SystemError
,
274 "bad memberlist type");
281 PyErr_SetString(PyExc_AttributeError
, name
);