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 * |
Tim Peters | c3d12ac | 2005-12-24 06:03:06 +0000 | [diff] [blame] | 32 | PyMember_Get(const char *addr, struct memberlist *mlist, const 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 * |
Tim Peters | c3d12ac | 2005-12-24 06:03:06 +0000 | [diff] [blame] | 54 | PyMember_GetOne(const char *addr, PyMemberDef *l) |
Guido van Rossum | 6f79937 | 2001-09-20 20:46:19 +0000 | [diff] [blame] | 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; |
Guido van Rossum | 6f79937 | 2001-09-20 20:46:19 +0000 | [diff] [blame] | 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; |
Guido van Rossum | 6b70599 | 2001-12-04 16:23:42 +0000 | [diff] [blame] | 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; |
Martin v. Löwis | 96d743e | 2005-03-03 23:00:26 +0000 | [diff] [blame] | 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 */ |
Guido van Rossum | 6f79937 | 2001-09-20 20:46:19 +0000 | [diff] [blame] | 129 | default: |
| 130 | PyErr_SetString(PyExc_SystemError, "bad memberdescr type"); |
| 131 | v = NULL; |
| 132 | } |
| 133 | return v; |
| 134 | } |
| 135 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 136 | int |
Tim Peters | c3d12ac | 2005-12-24 06:03:06 +0000 | [diff] [blame] | 137 | PyMember_Set(char *addr, struct memberlist *mlist, const char *name, PyObject *v) |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 138 | { |
| 139 | struct memberlist *l; |
Guido van Rossum | 6f79937 | 2001-09-20 20:46:19 +0000 | [diff] [blame] | 140 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 141 | for (l = mlist; l->name != NULL; l++) { |
| 142 | if (strcmp(l->name, name) == 0) { |
Guido van Rossum | 6f79937 | 2001-09-20 20:46:19 +0000 | [diff] [blame] | 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, ©, v); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 150 | } |
| 151 | } |
Guido van Rossum | 6f79937 | 2001-09-20 20:46:19 +0000 | [diff] [blame] | 152 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 153 | PyErr_SetString(PyExc_AttributeError, name); |
Guido van Rossum | 73531a3 | 1990-12-20 23:12:40 +0000 | [diff] [blame] | 154 | return -1; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 155 | } |
Guido van Rossum | 6f79937 | 2001-09-20 20:46:19 +0000 | [diff] [blame] | 156 | |
| 157 | int |
| 158 | PyMember_SetOne(char *addr, PyMemberDef *l, PyObject *v) |
| 159 | { |
| 160 | PyObject *oldv; |
| 161 | |
Jack Jansen | eddc144 | 2003-11-20 01:44:59 +0000 | [diff] [blame] | 162 | if ((l->flags & READONLY) || l->type == T_STRING) |
Guido van Rossum | 6f79937 | 2001-09-20 20:46:19 +0000 | [diff] [blame] | 163 | { |
| 164 | PyErr_SetString(PyExc_TypeError, "readonly attribute"); |
| 165 | return -1; |
| 166 | } |
| 167 | if ((l->flags & WRITE_RESTRICTED) && PyEval_GetRestricted()) { |
| 168 | PyErr_SetString(PyExc_RuntimeError, "restricted attribute"); |
| 169 | return -1; |
| 170 | } |
Guido van Rossum | 6b70599 | 2001-12-04 16:23:42 +0000 | [diff] [blame] | 171 | 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] | 172 | PyErr_SetString(PyExc_TypeError, |
| 173 | "can't delete numeric/char attribute"); |
| 174 | return -1; |
| 175 | } |
| 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; |
| 183 | } |
| 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; |
| 191 | } |
| 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; |
| 199 | } |
| 200 | *(int*)addr = (int) PyInt_AsLong(v); |
| 201 | break; |
| 202 | case T_LONG: |
| 203 | if (!PyInt_Check(v)) { |
| 204 | PyErr_BadArgument(); |
| 205 | return -1; |
| 206 | } |
| 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; |
| 217 | } |
| 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; |
| 229 | } |
| 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; |
| 239 | } |
| 240 | break; |
| 241 | case T_OBJECT: |
Guido van Rossum | 6b70599 | 2001-12-04 16:23:42 +0000 | [diff] [blame] | 242 | case T_OBJECT_EX: |
Guido van Rossum | 6f79937 | 2001-09-20 20:46:19 +0000 | [diff] [blame] | 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]; |
| 251 | } |
| 252 | else { |
| 253 | PyErr_BadArgument(); |
| 254 | return -1; |
| 255 | } |
| 256 | break; |
Martin v. Löwis | 96d743e | 2005-03-03 23:00:26 +0000 | [diff] [blame] | 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*)addr = PyLong_AsLongLong(v); |
| 264 | if ((*addr == -1) && PyErr_Occurred()) { |
| 265 | return -1; |
| 266 | } |
| 267 | } |
| 268 | break; |
| 269 | case T_ULONGLONG: |
| 270 | if (!PyLong_Check(v)) { |
| 271 | PyErr_BadArgument(); |
| 272 | return -1; |
| 273 | } else { |
| 274 | *(unsigned PY_LONG_LONG*)addr = PyLong_AsUnsignedLongLong(v); |
| 275 | if ((*addr == -1) && PyErr_Occurred()) { |
| 276 | return -1; |
| 277 | } |
| 278 | } |
| 279 | break; |
| 280 | #endif /* HAVE_LONG_LONG */ |
Guido van Rossum | 6f79937 | 2001-09-20 20:46:19 +0000 | [diff] [blame] | 281 | default: |
Jeremy Hylton | 26633f4 | 2001-10-16 16:51:56 +0000 | [diff] [blame] | 282 | PyErr_Format(PyExc_SystemError, |
| 283 | "bad memberdescr type for %s", l->name); |
Guido van Rossum | 6f79937 | 2001-09-20 20:46:19 +0000 | [diff] [blame] | 284 | return -1; |
| 285 | } |
| 286 | return 0; |
| 287 | } |