Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 1 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 2 | /* Map C struct members to Python object attributes */ |
| 3 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 4 | #include "Python.h" |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 5 | |
| 6 | #include "structmember.h" |
| 7 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 8 | static PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 9 | listmembers(struct memberlist *mlist) |
Guido van Rossum | 2f1d87e | 1991-10-20 20:24:14 +0000 | [diff] [blame] | 10 | { |
| 11 | int i, n; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 12 | PyObject *v; |
Guido van Rossum | 2f1d87e | 1991-10-20 20:24:14 +0000 | [diff] [blame] | 13 | for (n = 0; mlist[n].name != NULL; n++) |
| 14 | ; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 15 | v = PyList_New(n); |
Guido van Rossum | 2f1d87e | 1991-10-20 20:24:14 +0000 | [diff] [blame] | 16 | if (v != NULL) { |
| 17 | for (i = 0; i < n; i++) |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 18 | PyList_SetItem(v, i, |
| 19 | PyString_FromString(mlist[i].name)); |
| 20 | if (PyErr_Occurred()) { |
| 21 | Py_DECREF(v); |
Guido van Rossum | 2f1d87e | 1991-10-20 20:24:14 +0000 | [diff] [blame] | 22 | v = NULL; |
| 23 | } |
| 24 | else { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 25 | PyList_Sort(v); |
Guido van Rossum | 2f1d87e | 1991-10-20 20:24:14 +0000 | [diff] [blame] | 26 | } |
| 27 | } |
| 28 | return v; |
| 29 | } |
| 30 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 31 | PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 32 | PyMember_Get(char *addr, struct memberlist *mlist, char *name) |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 33 | { |
| 34 | struct memberlist *l; |
Guido van Rossum | 6f79937 | 2001-09-20 20:46:19 +0000 | [diff] [blame] | 35 | |
Guido van Rossum | 2f1d87e | 1991-10-20 20:24:14 +0000 | [diff] [blame] | 36 | if (strcmp(name, "__members__") == 0) |
| 37 | return listmembers(mlist); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 38 | for (l = mlist; l->name != NULL; l++) { |
| 39 | if (strcmp(l->name, name) == 0) { |
Guido van Rossum | 6f79937 | 2001-09-20 20:46:19 +0000 | [diff] [blame] | 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, ©); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 47 | } |
| 48 | } |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 49 | PyErr_SetString(PyExc_AttributeError, name); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 50 | return NULL; |
| 51 | } |
| 52 | |
Guido van Rossum | 6f79937 | 2001-09-20 20:46:19 +0000 | [diff] [blame] | 53 | PyObject * |
| 54 | PyMember_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 Rossum | 6b70599 | 2001-12-04 16:23:42 +0000 | [diff] [blame] | 132 | 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 Rossum | 6f79937 | 2001-09-20 20:46:19 +0000 | [diff] [blame] | 138 | default: |
| 139 | PyErr_SetString(PyExc_SystemError, "bad memberdescr type"); |
| 140 | v = NULL; |
| 141 | } |
| 142 | return v; |
| 143 | } |
| 144 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 145 | int |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 146 | PyMember_Set(char *addr, struct memberlist *mlist, char *name, PyObject *v) |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 147 | { |
| 148 | struct memberlist *l; |
Guido van Rossum | 6f79937 | 2001-09-20 20:46:19 +0000 | [diff] [blame] | 149 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 150 | for (l = mlist; l->name != NULL; l++) { |
| 151 | if (strcmp(l->name, name) == 0) { |
Guido van Rossum | 6f79937 | 2001-09-20 20:46:19 +0000 | [diff] [blame] | 152 | 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, ©, v); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 159 | } |
| 160 | } |
Guido van Rossum | 6f79937 | 2001-09-20 20:46:19 +0000 | [diff] [blame] | 161 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 162 | PyErr_SetString(PyExc_AttributeError, name); |
Guido van Rossum | 73531a3 | 1990-12-20 23:12:40 +0000 | [diff] [blame] | 163 | return -1; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 164 | } |
Guido van Rossum | 6f79937 | 2001-09-20 20:46:19 +0000 | [diff] [blame] | 165 | |
| 166 | int |
| 167 | PyMember_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 Rossum | 6b70599 | 2001-12-04 16:23:42 +0000 | [diff] [blame] | 184 | if (v == NULL && l->type != T_OBJECT_EX && l->type != T_OBJECT) { |
Guido van Rossum | 6f79937 | 2001-09-20 20:46:19 +0000 | [diff] [blame] | 185 | 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 Rossum | 6b70599 | 2001-12-04 16:23:42 +0000 | [diff] [blame] | 255 | case T_OBJECT_EX: |
Guido van Rossum | 6f79937 | 2001-09-20 20:46:19 +0000 | [diff] [blame] | 256 | 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 Hylton | 26633f4 | 2001-10-16 16:51:56 +0000 | [diff] [blame] | 271 | PyErr_Format(PyExc_SystemError, |
| 272 | "bad memberdescr type for %s", l->name); |
Guido van Rossum | 6f79937 | 2001-09-20 20:46:19 +0000 | [diff] [blame] | 273 | return -1; |
| 274 | } |
| 275 | return 0; |
| 276 | } |