2 /* Map C struct members to Python object attributes */
6 #include "structmember.h"
9 listmembers(struct memberlist
*mlist
)
13 for (n
= 0; mlist
[n
].name
!= NULL
; n
++)
17 for (i
= 0; i
< n
; i
++)
19 PyString_FromString(mlist
[i
].name
));
20 if (PyErr_Occurred()) {
32 PyMember_Get(const char *addr
, struct memberlist
*mlist
, const char *name
)
36 if (strcmp(name
, "__members__") == 0)
37 return listmembers(mlist
);
38 for (l
= mlist
; l
->name
!= NULL
; l
++) {
39 if (strcmp(l
->name
, name
) == 0) {
43 copy
.offset
= l
->offset
;
44 copy
.flags
= l
->flags
;
46 return PyMember_GetOne(addr
, ©
);
49 PyErr_SetString(PyExc_AttributeError
, name
);
54 PyMember_GetOne(const char *addr
, PyMemberDef
*l
)
57 if ((l
->flags
& READ_RESTRICTED
) &&
58 PyEval_GetRestricted()) {
59 PyErr_SetString(PyExc_RuntimeError
, "restricted attribute");
65 v
= PyInt_FromLong(*(char*)addr
);
68 v
= PyLong_FromUnsignedLong(*(unsigned char*)addr
);
71 v
= PyInt_FromLong(*(short*)addr
);
74 v
= PyLong_FromUnsignedLong(*(unsigned short*)addr
);
77 v
= PyInt_FromLong(*(int*)addr
);
80 v
= PyLong_FromUnsignedLong(*(unsigned int*)addr
);
83 v
= PyInt_FromLong(*(long*)addr
);
86 v
= PyLong_FromUnsignedLong(*(unsigned long*)addr
);
89 v
= PyFloat_FromDouble((double)*(float*)addr
);
92 v
= PyFloat_FromDouble(*(double*)addr
);
95 if (*(char**)addr
== NULL
) {
100 v
= PyString_FromString(*(char**)addr
);
102 case T_STRING_INPLACE
:
103 v
= PyString_FromString((char*)addr
);
106 v
= PyString_FromStringAndSize((char*)addr
, 1);
109 v
= *(PyObject
**)addr
;
115 v
= *(PyObject
**)addr
;
117 PyErr_SetString(PyExc_AttributeError
, l
->name
);
120 #ifdef HAVE_LONG_LONG
122 v
= PyLong_FromLongLong(*(PY_LONG_LONG
*)addr
);
125 v
= PyLong_FromUnsignedLongLong(*(unsigned PY_LONG_LONG
*)addr
);
127 #endif /* HAVE_LONG_LONG */
129 PyErr_SetString(PyExc_SystemError
, "bad memberdescr type");
136 PyMember_Set(char *addr
, struct memberlist
*mlist
, const char *name
, PyObject
*v
)
138 struct memberlist
*l
;
140 for (l
= mlist
; l
->name
!= NULL
; l
++) {
141 if (strcmp(l
->name
, name
) == 0) {
145 copy
.offset
= l
->offset
;
146 copy
.flags
= l
->flags
;
148 return PyMember_SetOne(addr
, ©
, v
);
152 PyErr_SetString(PyExc_AttributeError
, name
);
157 PyMember_SetOne(char *addr
, PyMemberDef
*l
, PyObject
*v
)
161 if ((l
->flags
& READONLY
) || l
->type
== T_STRING
)
163 PyErr_SetString(PyExc_TypeError
, "readonly attribute");
166 if ((l
->flags
& WRITE_RESTRICTED
) && PyEval_GetRestricted()) {
167 PyErr_SetString(PyExc_RuntimeError
, "restricted attribute");
170 if (v
== NULL
&& l
->type
!= T_OBJECT_EX
&& l
->type
!= T_OBJECT
) {
171 PyErr_SetString(PyExc_TypeError
,
172 "can't delete numeric/char attribute");
179 long_val
= PyInt_AsLong(v
);
180 if ((long_val
== -1) && PyErr_Occurred())
182 /* XXX: For compatibility, only warn about truncations
184 if ((long_val
> CHAR_MAX
) || (long_val
< CHAR_MIN
))
185 PyErr_Warn(PyExc_RuntimeWarning
, "Truncation of value to char");
186 *(char*)addr
= (char)long_val
;
191 long_val
= PyInt_AsLong(v
);
192 if ((long_val
== -1) && PyErr_Occurred())
194 if ((long_val
> UCHAR_MAX
) || (long_val
< 0))
195 PyErr_Warn(PyExc_RuntimeWarning
, "Truncation of value to unsigned char");
196 *(unsigned char*)addr
= (unsigned char)long_val
;
201 long_val
= PyInt_AsLong(v
);
202 if ((long_val
== -1) && PyErr_Occurred())
204 if ((long_val
> SHRT_MAX
) || (long_val
< SHRT_MIN
))
205 PyErr_Warn(PyExc_RuntimeWarning
, "Truncation of value to short");
206 *(short*)addr
= (short)long_val
;
211 long_val
= PyInt_AsLong(v
);
212 if ((long_val
== -1) && PyErr_Occurred())
214 if ((long_val
> USHRT_MAX
) || (long_val
< 0))
215 PyErr_Warn(PyExc_RuntimeWarning
, "Truncation of value to unsigned short");
216 *(unsigned short*)addr
= (unsigned short)long_val
;
221 long_val
= PyInt_AsLong(v
);
222 if ((long_val
== -1) && PyErr_Occurred())
224 if ((long_val
> INT_MAX
) || (long_val
< INT_MIN
))
225 PyErr_Warn(PyExc_RuntimeWarning
, "Truncation of value to int");
226 *(int *)addr
= (int)long_val
;
230 unsigned long ulong_val
;
231 ulong_val
= PyLong_AsUnsignedLong(v
);
232 if ((ulong_val
== (unsigned int)-1) && PyErr_Occurred()) {
233 /* XXX: For compatibility, accept negative int values
236 ulong_val
= PyLong_AsLong(v
);
237 if ((ulong_val
== (unsigned int)-1) && PyErr_Occurred())
239 PyErr_Warn(PyExc_RuntimeWarning
, "Writing negative value into unsigned field");
241 if (ulong_val
> UINT_MAX
)
242 PyErr_Warn(PyExc_RuntimeWarning
, "Truncation of value to unsigned int");
243 *(unsigned int *)addr
= (unsigned int)ulong_val
;
247 *(long*)addr
= PyLong_AsLong(v
);
248 if ((*(long*)addr
== -1) && PyErr_Occurred())
253 *(unsigned long*)addr
= PyLong_AsUnsignedLong(v
);
254 if ((*(unsigned long*)addr
== (unsigned long)-1)
255 && PyErr_Occurred()) {
256 /* XXX: For compatibility, accept negative int values
259 *(unsigned long*)addr
= PyLong_AsLong(v
);
260 if ((*(unsigned long*)addr
== (unsigned int)-1) && PyErr_Occurred())
262 PyErr_Warn(PyExc_RuntimeWarning
, "Writing negative value into unsigned field");
268 double_val
= PyFloat_AsDouble(v
);
269 if ((double_val
== -1) && PyErr_Occurred())
271 *(float*)addr
= (float)double_val
;
275 *(double*)addr
= PyFloat_AsDouble(v
);
276 if ((*(double*)addr
== -1) && PyErr_Occurred())
282 oldv
= *(PyObject
**)addr
;
283 *(PyObject
**)addr
= v
;
287 if (PyString_Check(v
) && PyString_Size(v
) == 1) {
288 *(char*)addr
= PyString_AsString(v
)[0];
295 #ifdef HAVE_LONG_LONG
297 if (!PyLong_Check(v
)) {
302 *(PY_LONG_LONG
*)addr
= value
= PyLong_AsLongLong(v
);
303 if ((value
== -1) && PyErr_Occurred()) {
309 if (!PyLong_Check(v
)) {
313 unsigned PY_LONG_LONG value
;
314 *(unsigned PY_LONG_LONG
*)addr
= value
= PyLong_AsUnsignedLongLong(v
);
315 if ((value
== (unsigned PY_LONG_LONG
)-1) &&
321 #endif /* HAVE_LONG_LONG */
323 PyErr_Format(PyExc_SystemError
,
324 "bad memberdescr type for %s", l
->name
);