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
= PyBool_FromLong(*(char*)addr
);
68 v
= PyInt_FromLong(*(char*)addr
);
71 v
= PyLong_FromUnsignedLong(*(unsigned char*)addr
);
74 v
= PyInt_FromLong(*(short*)addr
);
77 v
= PyLong_FromUnsignedLong(*(unsigned short*)addr
);
80 v
= PyInt_FromLong(*(int*)addr
);
83 v
= PyLong_FromUnsignedLong(*(unsigned int*)addr
);
86 v
= PyInt_FromLong(*(long*)addr
);
89 v
= PyLong_FromUnsignedLong(*(unsigned long*)addr
);
92 v
= PyInt_FromSsize_t(*(Py_ssize_t
*)addr
);
95 v
= PyFloat_FromDouble((double)*(float*)addr
);
98 v
= PyFloat_FromDouble(*(double*)addr
);
101 if (*(char**)addr
== NULL
) {
106 v
= PyString_FromString(*(char**)addr
);
108 case T_STRING_INPLACE
:
109 v
= PyString_FromString((char*)addr
);
112 v
= PyString_FromStringAndSize((char*)addr
, 1);
115 v
= *(PyObject
**)addr
;
121 v
= *(PyObject
**)addr
;
123 PyErr_SetString(PyExc_AttributeError
, l
->name
);
126 #ifdef HAVE_LONG_LONG
128 v
= PyLong_FromLongLong(*(PY_LONG_LONG
*)addr
);
131 v
= PyLong_FromUnsignedLongLong(*(unsigned PY_LONG_LONG
*)addr
);
133 #endif /* HAVE_LONG_LONG */
135 PyErr_SetString(PyExc_SystemError
, "bad memberdescr type");
142 PyMember_Set(char *addr
, struct memberlist
*mlist
, const char *name
, PyObject
*v
)
144 struct memberlist
*l
;
146 for (l
= mlist
; l
->name
!= NULL
; l
++) {
147 if (strcmp(l
->name
, name
) == 0) {
151 copy
.offset
= l
->offset
;
152 copy
.flags
= l
->flags
;
154 return PyMember_SetOne(addr
, ©
, v
);
158 PyErr_SetString(PyExc_AttributeError
, name
);
164 if (PyErr_Warn(PyExc_RuntimeWarning, msg) < 0) \
169 PyMember_SetOne(char *addr
, PyMemberDef
*l
, PyObject
*v
)
173 if ((l
->flags
& READONLY
) || l
->type
== T_STRING
)
175 PyErr_SetString(PyExc_TypeError
, "readonly attribute");
178 if ((l
->flags
& PY_WRITE_RESTRICTED
) && PyEval_GetRestricted()) {
179 PyErr_SetString(PyExc_RuntimeError
, "restricted attribute");
182 if (v
== NULL
&& l
->type
!= T_OBJECT_EX
&& l
->type
!= T_OBJECT
) {
183 PyErr_SetString(PyExc_TypeError
,
184 "can't delete numeric/char attribute");
190 if (!PyBool_Check(v
)) {
191 PyErr_SetString(PyExc_TypeError
,
192 "attribute value type must be bool");
196 *(char*)addr
= (char) 1;
198 *(char*)addr
= (char) 0;
202 long long_val
= PyInt_AsLong(v
);
203 if ((long_val
== -1) && PyErr_Occurred())
205 *(char*)addr
= (char)long_val
;
206 /* XXX: For compatibility, only warn about truncations
208 if ((long_val
> CHAR_MAX
) || (long_val
< CHAR_MIN
))
209 WARN("Truncation of value to char");
213 long long_val
= PyInt_AsLong(v
);
214 if ((long_val
== -1) && PyErr_Occurred())
216 *(unsigned char*)addr
= (unsigned char)long_val
;
217 if ((long_val
> UCHAR_MAX
) || (long_val
< 0))
218 WARN("Truncation of value to unsigned char");
222 long long_val
= PyInt_AsLong(v
);
223 if ((long_val
== -1) && PyErr_Occurred())
225 *(short*)addr
= (short)long_val
;
226 if ((long_val
> SHRT_MAX
) || (long_val
< SHRT_MIN
))
227 WARN("Truncation of value to short");
231 long long_val
= PyInt_AsLong(v
);
232 if ((long_val
== -1) && PyErr_Occurred())
234 *(unsigned short*)addr
= (unsigned short)long_val
;
235 if ((long_val
> USHRT_MAX
) || (long_val
< 0))
236 WARN("Truncation of value to unsigned short");
240 long long_val
= PyInt_AsLong(v
);
241 if ((long_val
== -1) && PyErr_Occurred())
243 *(int *)addr
= (int)long_val
;
244 if ((long_val
> INT_MAX
) || (long_val
< INT_MIN
))
245 WARN("Truncation of value to int");
249 unsigned long ulong_val
= PyLong_AsUnsignedLong(v
);
250 if ((ulong_val
== (unsigned int)-1) && PyErr_Occurred()) {
251 /* XXX: For compatibility, accept negative int values
254 ulong_val
= PyLong_AsLong(v
);
255 if ((ulong_val
== (unsigned int)-1) && PyErr_Occurred())
257 *(unsigned int *)addr
= (unsigned int)ulong_val
;
258 WARN("Writing negative value into unsigned field");
260 *(unsigned int *)addr
= (unsigned int)ulong_val
;
261 if (ulong_val
> UINT_MAX
)
262 WARN("Truncation of value to unsigned int");
266 *(long*)addr
= PyLong_AsLong(v
);
267 if ((*(long*)addr
== -1) && PyErr_Occurred())
272 *(unsigned long*)addr
= PyLong_AsUnsignedLong(v
);
273 if ((*(unsigned long*)addr
== (unsigned long)-1)
274 && PyErr_Occurred()) {
275 /* XXX: For compatibility, accept negative int values
278 *(unsigned long*)addr
= PyLong_AsLong(v
);
279 if ((*(unsigned long*)addr
== (unsigned int)-1)
282 WARN("Writing negative value into unsigned field");
287 *(Py_ssize_t
*)addr
= PyInt_AsSsize_t(v
);
288 if ((*(Py_ssize_t
*)addr
== (Py_ssize_t
)-1)
294 double double_val
= PyFloat_AsDouble(v
);
295 if ((double_val
== -1) && PyErr_Occurred())
297 *(float*)addr
= (float)double_val
;
301 *(double*)addr
= PyFloat_AsDouble(v
);
302 if ((*(double*)addr
== -1) && PyErr_Occurred())
308 oldv
= *(PyObject
**)addr
;
309 *(PyObject
**)addr
= v
;
313 if (PyString_Check(v
) && PyString_Size(v
) == 1) {
314 *(char*)addr
= PyString_AsString(v
)[0];
321 #ifdef HAVE_LONG_LONG
324 *(PY_LONG_LONG
*)addr
= value
= PyLong_AsLongLong(v
);
325 if ((value
== -1) && PyErr_Occurred())
330 unsigned PY_LONG_LONG value
;
331 /* ??? PyLong_AsLongLong accepts an int, but PyLong_AsUnsignedLongLong
334 *(unsigned PY_LONG_LONG
*)addr
= value
= PyLong_AsUnsignedLongLong(v
);
336 *(unsigned PY_LONG_LONG
*)addr
= value
= PyInt_AsLong(v
);
337 if ((value
== (unsigned PY_LONG_LONG
)-1) && PyErr_Occurred())
341 #endif /* HAVE_LONG_LONG */
343 PyErr_Format(PyExc_SystemError
,
344 "bad memberdescr type for %s", l
->name
);