1 | n/a | |
---|
2 | n/a | /* Map C struct members to Python object attributes */ |
---|
3 | n/a | |
---|
4 | n/a | #include "Python.h" |
---|
5 | n/a | |
---|
6 | n/a | #include "structmember.h" |
---|
7 | n/a | |
---|
8 | n/a | PyObject * |
---|
9 | n/a | PyMember_GetOne(const char *addr, PyMemberDef *l) |
---|
10 | n/a | { |
---|
11 | n/a | PyObject *v; |
---|
12 | n/a | |
---|
13 | n/a | addr += l->offset; |
---|
14 | n/a | switch (l->type) { |
---|
15 | n/a | case T_BOOL: |
---|
16 | n/a | v = PyBool_FromLong(*(char*)addr); |
---|
17 | n/a | break; |
---|
18 | n/a | case T_BYTE: |
---|
19 | n/a | v = PyLong_FromLong(*(char*)addr); |
---|
20 | n/a | break; |
---|
21 | n/a | case T_UBYTE: |
---|
22 | n/a | v = PyLong_FromUnsignedLong(*(unsigned char*)addr); |
---|
23 | n/a | break; |
---|
24 | n/a | case T_SHORT: |
---|
25 | n/a | v = PyLong_FromLong(*(short*)addr); |
---|
26 | n/a | break; |
---|
27 | n/a | case T_USHORT: |
---|
28 | n/a | v = PyLong_FromUnsignedLong(*(unsigned short*)addr); |
---|
29 | n/a | break; |
---|
30 | n/a | case T_INT: |
---|
31 | n/a | v = PyLong_FromLong(*(int*)addr); |
---|
32 | n/a | break; |
---|
33 | n/a | case T_UINT: |
---|
34 | n/a | v = PyLong_FromUnsignedLong(*(unsigned int*)addr); |
---|
35 | n/a | break; |
---|
36 | n/a | case T_LONG: |
---|
37 | n/a | v = PyLong_FromLong(*(long*)addr); |
---|
38 | n/a | break; |
---|
39 | n/a | case T_ULONG: |
---|
40 | n/a | v = PyLong_FromUnsignedLong(*(unsigned long*)addr); |
---|
41 | n/a | break; |
---|
42 | n/a | case T_PYSSIZET: |
---|
43 | n/a | v = PyLong_FromSsize_t(*(Py_ssize_t*)addr); |
---|
44 | n/a | break; |
---|
45 | n/a | case T_FLOAT: |
---|
46 | n/a | v = PyFloat_FromDouble((double)*(float*)addr); |
---|
47 | n/a | break; |
---|
48 | n/a | case T_DOUBLE: |
---|
49 | n/a | v = PyFloat_FromDouble(*(double*)addr); |
---|
50 | n/a | break; |
---|
51 | n/a | case T_STRING: |
---|
52 | n/a | if (*(char**)addr == NULL) { |
---|
53 | n/a | Py_INCREF(Py_None); |
---|
54 | n/a | v = Py_None; |
---|
55 | n/a | } |
---|
56 | n/a | else |
---|
57 | n/a | v = PyUnicode_FromString(*(char**)addr); |
---|
58 | n/a | break; |
---|
59 | n/a | case T_STRING_INPLACE: |
---|
60 | n/a | v = PyUnicode_FromString((char*)addr); |
---|
61 | n/a | break; |
---|
62 | n/a | case T_CHAR: |
---|
63 | n/a | v = PyUnicode_FromStringAndSize((char*)addr, 1); |
---|
64 | n/a | break; |
---|
65 | n/a | case T_OBJECT: |
---|
66 | n/a | v = *(PyObject **)addr; |
---|
67 | n/a | if (v == NULL) |
---|
68 | n/a | v = Py_None; |
---|
69 | n/a | Py_INCREF(v); |
---|
70 | n/a | break; |
---|
71 | n/a | case T_OBJECT_EX: |
---|
72 | n/a | v = *(PyObject **)addr; |
---|
73 | n/a | if (v == NULL) |
---|
74 | n/a | PyErr_SetString(PyExc_AttributeError, l->name); |
---|
75 | n/a | Py_XINCREF(v); |
---|
76 | n/a | break; |
---|
77 | n/a | case T_LONGLONG: |
---|
78 | n/a | v = PyLong_FromLongLong(*(long long *)addr); |
---|
79 | n/a | break; |
---|
80 | n/a | case T_ULONGLONG: |
---|
81 | n/a | v = PyLong_FromUnsignedLongLong(*(unsigned long long *)addr); |
---|
82 | n/a | break; |
---|
83 | n/a | case T_NONE: |
---|
84 | n/a | v = Py_None; |
---|
85 | n/a | Py_INCREF(v); |
---|
86 | n/a | break; |
---|
87 | n/a | default: |
---|
88 | n/a | PyErr_SetString(PyExc_SystemError, "bad memberdescr type"); |
---|
89 | n/a | v = NULL; |
---|
90 | n/a | } |
---|
91 | n/a | return v; |
---|
92 | n/a | } |
---|
93 | n/a | |
---|
94 | n/a | #define WARN(msg) \ |
---|
95 | n/a | do { \ |
---|
96 | n/a | if (PyErr_WarnEx(PyExc_RuntimeWarning, msg, 1) < 0) \ |
---|
97 | n/a | return -1; \ |
---|
98 | n/a | } while (0) |
---|
99 | n/a | |
---|
100 | n/a | int |
---|
101 | n/a | PyMember_SetOne(char *addr, PyMemberDef *l, PyObject *v) |
---|
102 | n/a | { |
---|
103 | n/a | PyObject *oldv; |
---|
104 | n/a | |
---|
105 | n/a | addr += l->offset; |
---|
106 | n/a | |
---|
107 | n/a | if ((l->flags & READONLY)) |
---|
108 | n/a | { |
---|
109 | n/a | PyErr_SetString(PyExc_AttributeError, "readonly attribute"); |
---|
110 | n/a | return -1; |
---|
111 | n/a | } |
---|
112 | n/a | if (v == NULL) { |
---|
113 | n/a | if (l->type == T_OBJECT_EX) { |
---|
114 | n/a | /* Check if the attribute is set. */ |
---|
115 | n/a | if (*(PyObject **)addr == NULL) { |
---|
116 | n/a | PyErr_SetString(PyExc_AttributeError, l->name); |
---|
117 | n/a | return -1; |
---|
118 | n/a | } |
---|
119 | n/a | } |
---|
120 | n/a | else if (l->type != T_OBJECT) { |
---|
121 | n/a | PyErr_SetString(PyExc_TypeError, |
---|
122 | n/a | "can't delete numeric/char attribute"); |
---|
123 | n/a | return -1; |
---|
124 | n/a | } |
---|
125 | n/a | } |
---|
126 | n/a | switch (l->type) { |
---|
127 | n/a | case T_BOOL:{ |
---|
128 | n/a | if (!PyBool_Check(v)) { |
---|
129 | n/a | PyErr_SetString(PyExc_TypeError, |
---|
130 | n/a | "attribute value type must be bool"); |
---|
131 | n/a | return -1; |
---|
132 | n/a | } |
---|
133 | n/a | if (v == Py_True) |
---|
134 | n/a | *(char*)addr = (char) 1; |
---|
135 | n/a | else |
---|
136 | n/a | *(char*)addr = (char) 0; |
---|
137 | n/a | break; |
---|
138 | n/a | } |
---|
139 | n/a | case T_BYTE:{ |
---|
140 | n/a | long long_val = PyLong_AsLong(v); |
---|
141 | n/a | if ((long_val == -1) && PyErr_Occurred()) |
---|
142 | n/a | return -1; |
---|
143 | n/a | *(char*)addr = (char)long_val; |
---|
144 | n/a | /* XXX: For compatibility, only warn about truncations |
---|
145 | n/a | for now. */ |
---|
146 | n/a | if ((long_val > CHAR_MAX) || (long_val < CHAR_MIN)) |
---|
147 | n/a | WARN("Truncation of value to char"); |
---|
148 | n/a | break; |
---|
149 | n/a | } |
---|
150 | n/a | case T_UBYTE:{ |
---|
151 | n/a | long long_val = PyLong_AsLong(v); |
---|
152 | n/a | if ((long_val == -1) && PyErr_Occurred()) |
---|
153 | n/a | return -1; |
---|
154 | n/a | *(unsigned char*)addr = (unsigned char)long_val; |
---|
155 | n/a | if ((long_val > UCHAR_MAX) || (long_val < 0)) |
---|
156 | n/a | WARN("Truncation of value to unsigned char"); |
---|
157 | n/a | break; |
---|
158 | n/a | } |
---|
159 | n/a | case T_SHORT:{ |
---|
160 | n/a | long long_val = PyLong_AsLong(v); |
---|
161 | n/a | if ((long_val == -1) && PyErr_Occurred()) |
---|
162 | n/a | return -1; |
---|
163 | n/a | *(short*)addr = (short)long_val; |
---|
164 | n/a | if ((long_val > SHRT_MAX) || (long_val < SHRT_MIN)) |
---|
165 | n/a | WARN("Truncation of value to short"); |
---|
166 | n/a | break; |
---|
167 | n/a | } |
---|
168 | n/a | case T_USHORT:{ |
---|
169 | n/a | long long_val = PyLong_AsLong(v); |
---|
170 | n/a | if ((long_val == -1) && PyErr_Occurred()) |
---|
171 | n/a | return -1; |
---|
172 | n/a | *(unsigned short*)addr = (unsigned short)long_val; |
---|
173 | n/a | if ((long_val > USHRT_MAX) || (long_val < 0)) |
---|
174 | n/a | WARN("Truncation of value to unsigned short"); |
---|
175 | n/a | break; |
---|
176 | n/a | } |
---|
177 | n/a | case T_INT:{ |
---|
178 | n/a | long long_val = PyLong_AsLong(v); |
---|
179 | n/a | if ((long_val == -1) && PyErr_Occurred()) |
---|
180 | n/a | return -1; |
---|
181 | n/a | *(int *)addr = (int)long_val; |
---|
182 | n/a | if ((long_val > INT_MAX) || (long_val < INT_MIN)) |
---|
183 | n/a | WARN("Truncation of value to int"); |
---|
184 | n/a | break; |
---|
185 | n/a | } |
---|
186 | n/a | case T_UINT:{ |
---|
187 | n/a | unsigned long ulong_val = PyLong_AsUnsignedLong(v); |
---|
188 | n/a | if ((ulong_val == (unsigned long)-1) && PyErr_Occurred()) { |
---|
189 | n/a | /* XXX: For compatibility, accept negative int values |
---|
190 | n/a | as well. */ |
---|
191 | n/a | PyErr_Clear(); |
---|
192 | n/a | ulong_val = PyLong_AsLong(v); |
---|
193 | n/a | if ((ulong_val == (unsigned long)-1) && |
---|
194 | n/a | PyErr_Occurred()) |
---|
195 | n/a | return -1; |
---|
196 | n/a | *(unsigned int *)addr = (unsigned int)ulong_val; |
---|
197 | n/a | WARN("Writing negative value into unsigned field"); |
---|
198 | n/a | } else |
---|
199 | n/a | *(unsigned int *)addr = (unsigned int)ulong_val; |
---|
200 | n/a | if (ulong_val > UINT_MAX) |
---|
201 | n/a | WARN("Truncation of value to unsigned int"); |
---|
202 | n/a | break; |
---|
203 | n/a | } |
---|
204 | n/a | case T_LONG:{ |
---|
205 | n/a | *(long*)addr = PyLong_AsLong(v); |
---|
206 | n/a | if ((*(long*)addr == -1) && PyErr_Occurred()) |
---|
207 | n/a | return -1; |
---|
208 | n/a | break; |
---|
209 | n/a | } |
---|
210 | n/a | case T_ULONG:{ |
---|
211 | n/a | *(unsigned long*)addr = PyLong_AsUnsignedLong(v); |
---|
212 | n/a | if ((*(unsigned long*)addr == (unsigned long)-1) |
---|
213 | n/a | && PyErr_Occurred()) { |
---|
214 | n/a | /* XXX: For compatibility, accept negative int values |
---|
215 | n/a | as well. */ |
---|
216 | n/a | PyErr_Clear(); |
---|
217 | n/a | *(unsigned long*)addr = PyLong_AsLong(v); |
---|
218 | n/a | if ((*(unsigned long*)addr == (unsigned long)-1) |
---|
219 | n/a | && PyErr_Occurred()) |
---|
220 | n/a | return -1; |
---|
221 | n/a | WARN("Writing negative value into unsigned field"); |
---|
222 | n/a | } |
---|
223 | n/a | break; |
---|
224 | n/a | } |
---|
225 | n/a | case T_PYSSIZET:{ |
---|
226 | n/a | *(Py_ssize_t*)addr = PyLong_AsSsize_t(v); |
---|
227 | n/a | if ((*(Py_ssize_t*)addr == (Py_ssize_t)-1) |
---|
228 | n/a | && PyErr_Occurred()) |
---|
229 | n/a | return -1; |
---|
230 | n/a | break; |
---|
231 | n/a | } |
---|
232 | n/a | case T_FLOAT:{ |
---|
233 | n/a | double double_val = PyFloat_AsDouble(v); |
---|
234 | n/a | if ((double_val == -1) && PyErr_Occurred()) |
---|
235 | n/a | return -1; |
---|
236 | n/a | *(float*)addr = (float)double_val; |
---|
237 | n/a | break; |
---|
238 | n/a | } |
---|
239 | n/a | case T_DOUBLE: |
---|
240 | n/a | *(double*)addr = PyFloat_AsDouble(v); |
---|
241 | n/a | if ((*(double*)addr == -1) && PyErr_Occurred()) |
---|
242 | n/a | return -1; |
---|
243 | n/a | break; |
---|
244 | n/a | case T_OBJECT: |
---|
245 | n/a | case T_OBJECT_EX: |
---|
246 | n/a | Py_XINCREF(v); |
---|
247 | n/a | oldv = *(PyObject **)addr; |
---|
248 | n/a | *(PyObject **)addr = v; |
---|
249 | n/a | Py_XDECREF(oldv); |
---|
250 | n/a | break; |
---|
251 | n/a | case T_CHAR: { |
---|
252 | n/a | const char *string; |
---|
253 | n/a | Py_ssize_t len; |
---|
254 | n/a | |
---|
255 | n/a | string = PyUnicode_AsUTF8AndSize(v, &len); |
---|
256 | n/a | if (string == NULL || len != 1) { |
---|
257 | n/a | PyErr_BadArgument(); |
---|
258 | n/a | return -1; |
---|
259 | n/a | } |
---|
260 | n/a | *(char*)addr = string[0]; |
---|
261 | n/a | break; |
---|
262 | n/a | } |
---|
263 | n/a | case T_STRING: |
---|
264 | n/a | case T_STRING_INPLACE: |
---|
265 | n/a | PyErr_SetString(PyExc_TypeError, "readonly attribute"); |
---|
266 | n/a | return -1; |
---|
267 | n/a | case T_LONGLONG:{ |
---|
268 | n/a | long long value; |
---|
269 | n/a | *(long long*)addr = value = PyLong_AsLongLong(v); |
---|
270 | n/a | if ((value == -1) && PyErr_Occurred()) |
---|
271 | n/a | return -1; |
---|
272 | n/a | break; |
---|
273 | n/a | } |
---|
274 | n/a | case T_ULONGLONG:{ |
---|
275 | n/a | unsigned long long value; |
---|
276 | n/a | /* ??? PyLong_AsLongLong accepts an int, but PyLong_AsUnsignedLongLong |
---|
277 | n/a | doesn't ??? */ |
---|
278 | n/a | if (PyLong_Check(v)) |
---|
279 | n/a | *(unsigned long long*)addr = value = PyLong_AsUnsignedLongLong(v); |
---|
280 | n/a | else |
---|
281 | n/a | *(unsigned long long*)addr = value = PyLong_AsLong(v); |
---|
282 | n/a | if ((value == (unsigned long long)-1) && PyErr_Occurred()) |
---|
283 | n/a | return -1; |
---|
284 | n/a | break; |
---|
285 | n/a | } |
---|
286 | n/a | default: |
---|
287 | n/a | PyErr_Format(PyExc_SystemError, |
---|
288 | n/a | "bad memberdescr type for %s", l->name); |
---|
289 | n/a | return -1; |
---|
290 | n/a | } |
---|
291 | n/a | return 0; |
---|
292 | n/a | } |
---|