pasted set_lk_detect line in wrong spot in previous commit. fixed. passes tests...
[python.git] / Python / structmember.c
blob54eb0556f7aa6aa1a9367e82683252f7c6d8e685
2 /* Map C struct members to Python object attributes */
4 #include "Python.h"
6 #include "structmember.h"
8 static PyObject *
9 listmembers(struct memberlist *mlist)
11 int i, n;
12 PyObject *v;
13 for (n = 0; mlist[n].name != NULL; n++)
15 v = PyList_New(n);
16 if (v != NULL) {
17 for (i = 0; i < n; i++)
18 PyList_SetItem(v, i,
19 PyString_FromString(mlist[i].name));
20 if (PyErr_Occurred()) {
21 Py_DECREF(v);
22 v = NULL;
24 else {
25 PyList_Sort(v);
28 return v;
31 PyObject *
32 PyMember_Get(const char *addr, struct memberlist *mlist, const char *name)
34 struct memberlist *l;
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) {
40 PyMemberDef copy;
41 copy.name = l->name;
42 copy.type = l->type;
43 copy.offset = l->offset;
44 copy.flags = l->flags;
45 copy.doc = NULL;
46 return PyMember_GetOne(addr, &copy);
49 PyErr_SetString(PyExc_AttributeError, name);
50 return NULL;
53 PyObject *
54 PyMember_GetOne(const char *addr, PyMemberDef *l)
56 PyObject *v;
57 if ((l->flags & READ_RESTRICTED) &&
58 PyEval_GetRestricted()) {
59 PyErr_SetString(PyExc_RuntimeError, "restricted attribute");
60 return NULL;
62 addr += l->offset;
63 switch (l->type) {
64 case T_BYTE:
65 v = PyInt_FromLong(
66 (long) (((*(char*)addr & 0xff) ^ 0x80) - 0x80));
67 break;
68 case T_UBYTE:
69 v = PyInt_FromLong((long) *(char*)addr & 0xff);
70 break;
71 case T_SHORT:
72 v = PyInt_FromLong((long) *(short*)addr);
73 break;
74 case T_USHORT:
75 v = PyInt_FromLong((long) *(unsigned short*)addr);
76 break;
77 case T_INT:
78 v = PyInt_FromLong((long) *(int*)addr);
79 break;
80 case T_UINT:
81 v = PyInt_FromLong((long) *(unsigned int*)addr);
82 break;
83 case T_LONG:
84 v = PyInt_FromLong(*(long*)addr);
85 break;
86 case T_ULONG:
87 v = PyLong_FromDouble((double) *(unsigned long*)addr);
88 break;
89 case T_FLOAT:
90 v = PyFloat_FromDouble((double)*(float*)addr);
91 break;
92 case T_DOUBLE:
93 v = PyFloat_FromDouble(*(double*)addr);
94 break;
95 case T_STRING:
96 if (*(char**)addr == NULL) {
97 Py_INCREF(Py_None);
98 v = Py_None;
100 else
101 v = PyString_FromString(*(char**)addr);
102 break;
103 case T_STRING_INPLACE:
104 v = PyString_FromString((char*)addr);
105 break;
106 case T_CHAR:
107 v = PyString_FromStringAndSize((char*)addr, 1);
108 break;
109 case T_OBJECT:
110 v = *(PyObject **)addr;
111 if (v == NULL)
112 v = Py_None;
113 Py_INCREF(v);
114 break;
115 case T_OBJECT_EX:
116 v = *(PyObject **)addr;
117 if (v == NULL)
118 PyErr_SetString(PyExc_AttributeError, l->name);
119 Py_XINCREF(v);
120 break;
121 #ifdef HAVE_LONG_LONG
122 case T_LONGLONG:
123 v = PyLong_FromLongLong(*(PY_LONG_LONG *)addr);
124 break;
125 case T_ULONGLONG:
126 v = PyLong_FromUnsignedLongLong(*(unsigned PY_LONG_LONG *)addr);
127 break;
128 #endif /* HAVE_LONG_LONG */
129 default:
130 PyErr_SetString(PyExc_SystemError, "bad memberdescr type");
131 v = NULL;
133 return v;
137 PyMember_Set(char *addr, struct memberlist *mlist, const char *name, PyObject *v)
139 struct memberlist *l;
141 for (l = mlist; l->name != NULL; l++) {
142 if (strcmp(l->name, name) == 0) {
143 PyMemberDef copy;
144 copy.name = l->name;
145 copy.type = l->type;
146 copy.offset = l->offset;
147 copy.flags = l->flags;
148 copy.doc = NULL;
149 return PyMember_SetOne(addr, &copy, v);
153 PyErr_SetString(PyExc_AttributeError, name);
154 return -1;
158 PyMember_SetOne(char *addr, PyMemberDef *l, PyObject *v)
160 PyObject *oldv;
162 if ((l->flags & READONLY) || l->type == T_STRING)
164 PyErr_SetString(PyExc_TypeError, "readonly attribute");
165 return -1;
167 if ((l->flags & WRITE_RESTRICTED) && PyEval_GetRestricted()) {
168 PyErr_SetString(PyExc_RuntimeError, "restricted attribute");
169 return -1;
171 if (v == NULL && l->type != T_OBJECT_EX && l->type != T_OBJECT) {
172 PyErr_SetString(PyExc_TypeError,
173 "can't delete numeric/char attribute");
174 return -1;
176 addr += l->offset;
177 switch (l->type) {
178 case T_BYTE:
179 case T_UBYTE:
180 if (!PyInt_Check(v)) {
181 PyErr_BadArgument();
182 return -1;
184 *(char*)addr = (char) PyInt_AsLong(v);
185 break;
186 case T_SHORT:
187 case T_USHORT:
188 if (!PyInt_Check(v)) {
189 PyErr_BadArgument();
190 return -1;
192 *(short*)addr = (short) PyInt_AsLong(v);
193 break;
194 case T_UINT:
195 case T_INT:
196 if (!PyInt_Check(v)) {
197 PyErr_BadArgument();
198 return -1;
200 *(int*)addr = (int) PyInt_AsLong(v);
201 break;
202 case T_LONG:
203 if (!PyInt_Check(v)) {
204 PyErr_BadArgument();
205 return -1;
207 *(long*)addr = PyInt_AsLong(v);
208 break;
209 case T_ULONG:
210 if (PyInt_Check(v))
211 *(long*)addr = PyInt_AsLong(v);
212 else if (PyLong_Check(v))
213 *(long*)addr = PyLong_AsLong(v);
214 else {
215 PyErr_BadArgument();
216 return -1;
218 break;
219 case T_FLOAT:
220 if (PyInt_Check(v))
221 *(float*)addr =
222 (float) PyInt_AsLong(v);
223 else if (PyFloat_Check(v))
224 *(float*)addr =
225 (float) PyFloat_AsDouble(v);
226 else {
227 PyErr_BadArgument();
228 return -1;
230 break;
231 case T_DOUBLE:
232 if (PyInt_Check(v))
233 *(double*)addr = (double) PyInt_AsLong(v);
234 else if (PyFloat_Check(v))
235 *(double*)addr = PyFloat_AsDouble(v);
236 else {
237 PyErr_BadArgument();
238 return -1;
240 break;
241 case T_OBJECT:
242 case T_OBJECT_EX:
243 Py_XINCREF(v);
244 oldv = *(PyObject **)addr;
245 *(PyObject **)addr = v;
246 Py_XDECREF(oldv);
247 break;
248 case T_CHAR:
249 if (PyString_Check(v) && PyString_Size(v) == 1) {
250 *(char*)addr = PyString_AsString(v)[0];
252 else {
253 PyErr_BadArgument();
254 return -1;
256 break;
257 #ifdef HAVE_LONG_LONG
258 case T_LONGLONG:
259 if (!PyLong_Check(v)) {
260 PyErr_BadArgument();
261 return -1;
262 } else {
263 PY_LONG_LONG value;
264 *(PY_LONG_LONG*)addr = value = PyLong_AsLongLong(v);
265 if ((value == -1) && PyErr_Occurred()) {
266 return -1;
269 break;
270 case T_ULONGLONG:
271 if (!PyLong_Check(v)) {
272 PyErr_BadArgument();
273 return -1;
274 } else {
275 unsigned PY_LONG_LONG value;
276 *(unsigned PY_LONG_LONG*)addr = value = PyLong_AsUnsignedLongLong(v);
277 if ((value == (unsigned PY_LONG_LONG)-1) &&
278 PyErr_Occurred()) {
279 return -1;
282 break;
283 #endif /* HAVE_LONG_LONG */
284 default:
285 PyErr_Format(PyExc_SystemError,
286 "bad memberdescr type for %s", l->name);
287 return -1;
289 return 0;