blob: 34310b8979d1222de95c9f8e40870ac1e3de01c8 [file] [log] [blame]
Guido van Rossumf70e43a1991-02-19 12:39:46 +00001
Guido van Rossum3f5da241990-12-20 15:06:42 +00002/* Map C struct members to Python object attributes */
3
Guido van Rossum79f25d91997-04-29 20:08:16 +00004#include "Python.h"
Guido van Rossum3f5da241990-12-20 15:06:42 +00005
6#include "structmember.h"
7
Guido van Rossum79f25d91997-04-29 20:08:16 +00008static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00009listmembers(struct memberlist *mlist)
Guido van Rossum2f1d87e1991-10-20 20:24:14 +000010{
11 int i, n;
Guido van Rossum79f25d91997-04-29 20:08:16 +000012 PyObject *v;
Guido van Rossum2f1d87e1991-10-20 20:24:14 +000013 for (n = 0; mlist[n].name != NULL; n++)
14 ;
Guido van Rossum79f25d91997-04-29 20:08:16 +000015 v = PyList_New(n);
Guido van Rossum2f1d87e1991-10-20 20:24:14 +000016 if (v != NULL) {
17 for (i = 0; i < n; i++)
Guido van Rossum79f25d91997-04-29 20:08:16 +000018 PyList_SetItem(v, i,
19 PyString_FromString(mlist[i].name));
20 if (PyErr_Occurred()) {
21 Py_DECREF(v);
Guido van Rossum2f1d87e1991-10-20 20:24:14 +000022 v = NULL;
23 }
24 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +000025 PyList_Sort(v);
Guido van Rossum2f1d87e1991-10-20 20:24:14 +000026 }
27 }
28 return v;
29}
30
Guido van Rossum79f25d91997-04-29 20:08:16 +000031PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +000032PyMember_Get(char *addr, struct memberlist *mlist, char *name)
Guido van Rossum3f5da241990-12-20 15:06:42 +000033{
34 struct memberlist *l;
Guido van Rossum6f799372001-09-20 20:46:19 +000035
Guido van Rossum2f1d87e1991-10-20 20:24:14 +000036 if (strcmp(name, "__members__") == 0)
37 return listmembers(mlist);
Guido van Rossum3f5da241990-12-20 15:06:42 +000038 for (l = mlist; l->name != NULL; l++) {
39 if (strcmp(l->name, name) == 0) {
Guido van Rossum6f799372001-09-20 20:46:19 +000040 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);
Guido van Rossum3f5da241990-12-20 15:06:42 +000047 }
48 }
Guido van Rossum79f25d91997-04-29 20:08:16 +000049 PyErr_SetString(PyExc_AttributeError, name);
Guido van Rossum3f5da241990-12-20 15:06:42 +000050 return NULL;
51}
52
Guido van Rossum6f799372001-09-20 20:46:19 +000053PyObject *
54PyMember_GetOne(char *addr, PyMemberDef *l)
55{
56 PyObject *v;
57 if ((l->flags & READ_RESTRICTED) &&
58 PyEval_GetRestricted()) {
59 PyErr_SetString(PyExc_RuntimeError, "restricted attribute");
60 return NULL;
61 }
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;
99 }
100 else
101 v = PyString_FromString(*(char**)addr);
102 break;
103 case T_STRING_INPLACE:
104 v = PyString_FromString((char*)addr);
105 break;
106#ifdef macintosh
107 case T_PSTRING:
108 if (*(char**)addr == NULL) {
109 Py_INCREF(Py_None);
110 v = Py_None;
111 }
112 else
113 v = PyString_FromStringAndSize(
114 (*(char**)addr)+1,
115 **(unsigned char**)addr);
116 break;
117 case T_PSTRING_INPLACE:
118 v = PyString_FromStringAndSize(
119 ((char*)addr)+1,
120 *(unsigned char*)addr);
121 break;
122#endif /* macintosh */
123 case T_CHAR:
124 v = PyString_FromStringAndSize((char*)addr, 1);
125 break;
126 case T_OBJECT:
127 v = *(PyObject **)addr;
128 if (v == NULL)
129 v = Py_None;
130 Py_INCREF(v);
131 break;
Guido van Rossum6b705992001-12-04 16:23:42 +0000132 case T_OBJECT_EX:
133 v = *(PyObject **)addr;
134 if (v == NULL)
135 PyErr_SetString(PyExc_AttributeError, l->name);
136 Py_XINCREF(v);
137 break;
Guido van Rossum6f799372001-09-20 20:46:19 +0000138 default:
139 PyErr_SetString(PyExc_SystemError, "bad memberdescr type");
140 v = NULL;
141 }
142 return v;
143}
144
Guido van Rossum3f5da241990-12-20 15:06:42 +0000145int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000146PyMember_Set(char *addr, struct memberlist *mlist, char *name, PyObject *v)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000147{
148 struct memberlist *l;
Guido van Rossum6f799372001-09-20 20:46:19 +0000149
Guido van Rossum3f5da241990-12-20 15:06:42 +0000150 for (l = mlist; l->name != NULL; l++) {
151 if (strcmp(l->name, name) == 0) {
Guido van Rossum6f799372001-09-20 20:46:19 +0000152 PyMemberDef copy;
153 copy.name = l->name;
154 copy.type = l->type;
155 copy.offset = l->offset;
156 copy.flags = l->flags;
157 copy.doc = NULL;
158 return PyMember_SetOne(addr, &copy, v);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000159 }
160 }
Guido van Rossum6f799372001-09-20 20:46:19 +0000161
Guido van Rossum79f25d91997-04-29 20:08:16 +0000162 PyErr_SetString(PyExc_AttributeError, name);
Guido van Rossum73531a31990-12-20 23:12:40 +0000163 return -1;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000164}
Guido van Rossum6f799372001-09-20 20:46:19 +0000165
166int
167PyMember_SetOne(char *addr, PyMemberDef *l, PyObject *v)
168{
169 PyObject *oldv;
170
171 if ((l->flags & READONLY) || l->type == T_STRING
172#ifdef macintosh
173 || l->type == T_PSTRING
174#endif
175 )
176 {
177 PyErr_SetString(PyExc_TypeError, "readonly attribute");
178 return -1;
179 }
180 if ((l->flags & WRITE_RESTRICTED) && PyEval_GetRestricted()) {
181 PyErr_SetString(PyExc_RuntimeError, "restricted attribute");
182 return -1;
183 }
Guido van Rossum6b705992001-12-04 16:23:42 +0000184 if (v == NULL && l->type != T_OBJECT_EX && l->type != T_OBJECT) {
Guido van Rossum6f799372001-09-20 20:46:19 +0000185 PyErr_SetString(PyExc_TypeError,
186 "can't delete numeric/char attribute");
187 return -1;
188 }
189 addr += l->offset;
190 switch (l->type) {
191 case T_BYTE:
192 case T_UBYTE:
193 if (!PyInt_Check(v)) {
194 PyErr_BadArgument();
195 return -1;
196 }
197 *(char*)addr = (char) PyInt_AsLong(v);
198 break;
199 case T_SHORT:
200 case T_USHORT:
201 if (!PyInt_Check(v)) {
202 PyErr_BadArgument();
203 return -1;
204 }
205 *(short*)addr = (short) PyInt_AsLong(v);
206 break;
207 case T_UINT:
208 case T_INT:
209 if (!PyInt_Check(v)) {
210 PyErr_BadArgument();
211 return -1;
212 }
213 *(int*)addr = (int) PyInt_AsLong(v);
214 break;
215 case T_LONG:
216 if (!PyInt_Check(v)) {
217 PyErr_BadArgument();
218 return -1;
219 }
220 *(long*)addr = PyInt_AsLong(v);
221 break;
222 case T_ULONG:
223 if (PyInt_Check(v))
224 *(long*)addr = PyInt_AsLong(v);
225 else if (PyLong_Check(v))
226 *(long*)addr = PyLong_AsLong(v);
227 else {
228 PyErr_BadArgument();
229 return -1;
230 }
231 break;
232 case T_FLOAT:
233 if (PyInt_Check(v))
234 *(float*)addr =
235 (float) PyInt_AsLong(v);
236 else if (PyFloat_Check(v))
237 *(float*)addr =
238 (float) PyFloat_AsDouble(v);
239 else {
240 PyErr_BadArgument();
241 return -1;
242 }
243 break;
244 case T_DOUBLE:
245 if (PyInt_Check(v))
246 *(double*)addr = (double) PyInt_AsLong(v);
247 else if (PyFloat_Check(v))
248 *(double*)addr = PyFloat_AsDouble(v);
249 else {
250 PyErr_BadArgument();
251 return -1;
252 }
253 break;
254 case T_OBJECT:
Guido van Rossum6b705992001-12-04 16:23:42 +0000255 case T_OBJECT_EX:
Guido van Rossum6f799372001-09-20 20:46:19 +0000256 Py_XINCREF(v);
257 oldv = *(PyObject **)addr;
258 *(PyObject **)addr = v;
259 Py_XDECREF(oldv);
260 break;
261 case T_CHAR:
262 if (PyString_Check(v) && PyString_Size(v) == 1) {
263 *(char*)addr = PyString_AsString(v)[0];
264 }
265 else {
266 PyErr_BadArgument();
267 return -1;
268 }
269 break;
270 default:
Jeremy Hylton26633f42001-10-16 16:51:56 +0000271 PyErr_Format(PyExc_SystemError,
272 "bad memberdescr type for %s", l->name);
Guido van Rossum6f799372001-09-20 20:46:19 +0000273 return -1;
274 }
275 return 0;
276}