Bug #1627575: Added _open() method to FileHandler which can be used to reopen files...
[python.git] / Python / structmember.c
blob94d222a1e4990dfe915869dbcf10990a03023087
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(*(char*)addr);
66 break;
67 case T_UBYTE:
68 v = PyLong_FromUnsignedLong(*(unsigned char*)addr);
69 break;
70 case T_SHORT:
71 v = PyInt_FromLong(*(short*)addr);
72 break;
73 case T_USHORT:
74 v = PyLong_FromUnsignedLong(*(unsigned short*)addr);
75 break;
76 case T_INT:
77 v = PyInt_FromLong(*(int*)addr);
78 break;
79 case T_UINT:
80 v = PyLong_FromUnsignedLong(*(unsigned int*)addr);
81 break;
82 case T_LONG:
83 v = PyInt_FromLong(*(long*)addr);
84 break;
85 case T_ULONG:
86 v = PyLong_FromUnsignedLong(*(unsigned long*)addr);
87 break;
88 case T_FLOAT:
89 v = PyFloat_FromDouble((double)*(float*)addr);
90 break;
91 case T_DOUBLE:
92 v = PyFloat_FromDouble(*(double*)addr);
93 break;
94 case T_STRING:
95 if (*(char**)addr == NULL) {
96 Py_INCREF(Py_None);
97 v = Py_None;
99 else
100 v = PyString_FromString(*(char**)addr);
101 break;
102 case T_STRING_INPLACE:
103 v = PyString_FromString((char*)addr);
104 break;
105 case T_CHAR:
106 v = PyString_FromStringAndSize((char*)addr, 1);
107 break;
108 case T_OBJECT:
109 v = *(PyObject **)addr;
110 if (v == NULL)
111 v = Py_None;
112 Py_INCREF(v);
113 break;
114 case T_OBJECT_EX:
115 v = *(PyObject **)addr;
116 if (v == NULL)
117 PyErr_SetString(PyExc_AttributeError, l->name);
118 Py_XINCREF(v);
119 break;
120 #ifdef HAVE_LONG_LONG
121 case T_LONGLONG:
122 v = PyLong_FromLongLong(*(PY_LONG_LONG *)addr);
123 break;
124 case T_ULONGLONG:
125 v = PyLong_FromUnsignedLongLong(*(unsigned PY_LONG_LONG *)addr);
126 break;
127 #endif /* HAVE_LONG_LONG */
128 default:
129 PyErr_SetString(PyExc_SystemError, "bad memberdescr type");
130 v = NULL;
132 return v;
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) {
142 PyMemberDef copy;
143 copy.name = l->name;
144 copy.type = l->type;
145 copy.offset = l->offset;
146 copy.flags = l->flags;
147 copy.doc = NULL;
148 return PyMember_SetOne(addr, &copy, v);
152 PyErr_SetString(PyExc_AttributeError, name);
153 return -1;
157 PyMember_SetOne(char *addr, PyMemberDef *l, PyObject *v)
159 PyObject *oldv;
161 if ((l->flags & READONLY) || l->type == T_STRING)
163 PyErr_SetString(PyExc_TypeError, "readonly attribute");
164 return -1;
166 if ((l->flags & WRITE_RESTRICTED) && PyEval_GetRestricted()) {
167 PyErr_SetString(PyExc_RuntimeError, "restricted attribute");
168 return -1;
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");
173 return -1;
175 addr += l->offset;
176 switch (l->type) {
177 case T_BYTE:{
178 long long_val;
179 long_val = PyInt_AsLong(v);
180 if ((long_val == -1) && PyErr_Occurred())
181 return -1;
182 /* XXX: For compatibility, only warn about truncations
183 for now. */
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;
187 break;
189 case T_UBYTE:{
190 long long_val;
191 long_val = PyInt_AsLong(v);
192 if ((long_val == -1) && PyErr_Occurred())
193 return -1;
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;
197 break;
199 case T_SHORT:{
200 long long_val;
201 long_val = PyInt_AsLong(v);
202 if ((long_val == -1) && PyErr_Occurred())
203 return -1;
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;
207 break;
209 case T_USHORT:{
210 long long_val;
211 long_val = PyInt_AsLong(v);
212 if ((long_val == -1) && PyErr_Occurred())
213 return -1;
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;
217 break;
219 case T_INT:{
220 long long_val;
221 long_val = PyInt_AsLong(v);
222 if ((long_val == -1) && PyErr_Occurred())
223 return -1;
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;
227 break;
229 case T_UINT:{
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
234 as well. */
235 PyErr_Clear();
236 ulong_val = PyLong_AsLong(v);
237 if ((ulong_val == (unsigned int)-1) && PyErr_Occurred())
238 return -1;
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;
244 break;
246 case T_LONG:{
247 *(long*)addr = PyLong_AsLong(v);
248 if ((*(long*)addr == -1) && PyErr_Occurred())
249 return -1;
250 break;
252 case T_ULONG:{
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
257 as well. */
258 PyErr_Clear();
259 *(unsigned long*)addr = PyLong_AsLong(v);
260 if ((*(unsigned long*)addr == (unsigned int)-1) && PyErr_Occurred())
261 return -1;
262 PyErr_Warn(PyExc_RuntimeWarning, "Writing negative value into unsigned field");
264 break;
266 case T_FLOAT:{
267 double double_val;
268 double_val = PyFloat_AsDouble(v);
269 if ((double_val == -1) && PyErr_Occurred())
270 return -1;
271 *(float*)addr = (float)double_val;
272 break;
274 case T_DOUBLE:
275 *(double*)addr = PyFloat_AsDouble(v);
276 if ((*(double*)addr == -1) && PyErr_Occurred())
277 return -1;
278 break;
279 case T_OBJECT:
280 case T_OBJECT_EX:
281 Py_XINCREF(v);
282 oldv = *(PyObject **)addr;
283 *(PyObject **)addr = v;
284 Py_XDECREF(oldv);
285 break;
286 case T_CHAR:
287 if (PyString_Check(v) && PyString_Size(v) == 1) {
288 *(char*)addr = PyString_AsString(v)[0];
290 else {
291 PyErr_BadArgument();
292 return -1;
294 break;
295 #ifdef HAVE_LONG_LONG
296 case T_LONGLONG:
297 if (!PyLong_Check(v)) {
298 PyErr_BadArgument();
299 return -1;
300 } else {
301 PY_LONG_LONG value;
302 *(PY_LONG_LONG*)addr = value = PyLong_AsLongLong(v);
303 if ((value == -1) && PyErr_Occurred()) {
304 return -1;
307 break;
308 case T_ULONGLONG:
309 if (!PyLong_Check(v)) {
310 PyErr_BadArgument();
311 return -1;
312 } else {
313 unsigned PY_LONG_LONG value;
314 *(unsigned PY_LONG_LONG*)addr = value = PyLong_AsUnsignedLongLong(v);
315 if ((value == (unsigned PY_LONG_LONG)-1) &&
316 PyErr_Occurred()) {
317 return -1;
320 break;
321 #endif /* HAVE_LONG_LONG */
322 default:
323 PyErr_Format(PyExc_SystemError,
324 "bad memberdescr type for %s", l->name);
325 return -1;
327 return 0;