Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 1 | /*********************************************************** |
Guido van Rossum | 6d023c9 | 1995-01-04 19:12:13 +0000 | [diff] [blame] | 2 | Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam, |
| 3 | The Netherlands. |
Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 4 | |
| 5 | All Rights Reserved |
| 6 | |
| 7 | Permission to use, copy, modify, and distribute this software and its |
| 8 | documentation for any purpose and without fee is hereby granted, |
| 9 | provided that the above copyright notice appear in all copies and that |
| 10 | both that copyright notice and this permission notice appear in |
| 11 | supporting documentation, and that the names of Stichting Mathematisch |
| 12 | Centrum or CWI not be used in advertising or publicity pertaining to |
| 13 | distribution of the software without specific, written prior permission. |
| 14 | |
| 15 | STICHTING MATHEMATISCH CENTRUM DISCLAIMS ALL WARRANTIES WITH REGARD TO |
| 16 | THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND |
| 17 | FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH CENTRUM BE LIABLE |
| 18 | FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
| 19 | WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
| 20 | ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT |
| 21 | OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 22 | |
| 23 | ******************************************************************/ |
| 24 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 25 | /* Map C struct members to Python object attributes */ |
| 26 | |
| 27 | #include "allobjects.h" |
| 28 | |
| 29 | #include "structmember.h" |
| 30 | |
Guido van Rossum | 2f1d87e | 1991-10-20 20:24:14 +0000 | [diff] [blame] | 31 | static object * |
| 32 | listmembers(mlist) |
| 33 | struct memberlist *mlist; |
| 34 | { |
| 35 | int i, n; |
| 36 | object *v; |
| 37 | for (n = 0; mlist[n].name != NULL; n++) |
| 38 | ; |
| 39 | v = newlistobject(n); |
| 40 | if (v != NULL) { |
| 41 | for (i = 0; i < n; i++) |
| 42 | setlistitem(v, i, newstringobject(mlist[i].name)); |
| 43 | if (err_occurred()) { |
| 44 | DECREF(v); |
| 45 | v = NULL; |
| 46 | } |
| 47 | else { |
| 48 | sortlist(v); |
| 49 | } |
| 50 | } |
| 51 | return v; |
| 52 | } |
| 53 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 54 | object * |
| 55 | getmember(addr, mlist, name) |
| 56 | char *addr; |
| 57 | struct memberlist *mlist; |
| 58 | char *name; |
| 59 | { |
| 60 | struct memberlist *l; |
| 61 | |
Guido van Rossum | 2f1d87e | 1991-10-20 20:24:14 +0000 | [diff] [blame] | 62 | if (strcmp(name, "__members__") == 0) |
| 63 | return listmembers(mlist); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 64 | for (l = mlist; l->name != NULL; l++) { |
| 65 | if (strcmp(l->name, name) == 0) { |
| 66 | object *v; |
| 67 | addr += l->offset; |
| 68 | switch (l->type) { |
Guido van Rossum | ab2490a | 1992-06-03 17:07:23 +0000 | [diff] [blame] | 69 | case T_BYTE: |
| 70 | v = newintobject((long) |
| 71 | (((*(char*)addr & 0xff) |
| 72 | ^ 0x80) - 0x80)); |
| 73 | break; |
| 74 | case T_UBYTE: |
| 75 | v = newintobject((long) *(char*)addr & 0xff); |
| 76 | break; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 77 | case T_SHORT: |
| 78 | v = newintobject((long) *(short*)addr); |
| 79 | break; |
Guido van Rossum | ab2490a | 1992-06-03 17:07:23 +0000 | [diff] [blame] | 80 | case T_USHORT: |
| 81 | v = newintobject((long) |
| 82 | *(unsigned short*)addr); |
| 83 | break; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 84 | case T_INT: |
| 85 | v = newintobject((long) *(int*)addr); |
| 86 | break; |
Guido van Rossum | ab2490a | 1992-06-03 17:07:23 +0000 | [diff] [blame] | 87 | case T_UINT: |
| 88 | v = newintobject((long) *(unsigned int*)addr); |
| 89 | break; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 90 | case T_LONG: |
| 91 | v = newintobject(*(long*)addr); |
| 92 | break; |
Guido van Rossum | ab2490a | 1992-06-03 17:07:23 +0000 | [diff] [blame] | 93 | case T_ULONG: |
| 94 | v = dnewlongobject((double) |
| 95 | *(unsigned long*)addr); |
| 96 | break; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 97 | case T_FLOAT: |
| 98 | v = newfloatobject((double)*(float*)addr); |
| 99 | break; |
| 100 | case T_DOUBLE: |
| 101 | v = newfloatobject(*(double*)addr); |
| 102 | break; |
| 103 | case T_STRING: |
| 104 | if (*(char**)addr == NULL) { |
| 105 | INCREF(None); |
| 106 | v = None; |
| 107 | } |
| 108 | else |
| 109 | v = newstringobject(*(char**)addr); |
| 110 | break; |
Jack Jansen | 599f0d1 | 1994-12-14 13:04:05 +0000 | [diff] [blame] | 111 | case T_STRING_INPLACE: |
| 112 | v = newstringobject((char*)addr); |
| 113 | break; |
| 114 | #ifdef macintosh |
| 115 | case T_PSTRING: |
| 116 | if (*(char**)addr == NULL) { |
| 117 | INCREF(None); |
| 118 | v = None; |
| 119 | } |
| 120 | else |
| 121 | v = newsizedstringobject((*(char**)addr)+1, |
| 122 | **(unsigned char**)addr); |
| 123 | break; |
| 124 | case T_PSTRING_INPLACE: |
| 125 | v = newsizedstringobject(((char*)addr)+1, |
| 126 | *(unsigned char*)addr); |
| 127 | break; |
| 128 | #endif /* macintosh */ |
Guido van Rossum | ab2490a | 1992-06-03 17:07:23 +0000 | [diff] [blame] | 129 | case T_CHAR: |
| 130 | v = newsizedstringobject((char*)addr, 1); |
| 131 | break; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 132 | case T_OBJECT: |
| 133 | v = *(object **)addr; |
| 134 | if (v == NULL) |
| 135 | v = None; |
| 136 | INCREF(v); |
| 137 | break; |
| 138 | default: |
| 139 | err_setstr(SystemError, "bad memberlist type"); |
| 140 | v = NULL; |
| 141 | } |
| 142 | return v; |
| 143 | } |
| 144 | } |
| 145 | |
Guido van Rossum | 87e7ea7 | 1991-12-10 14:00:03 +0000 | [diff] [blame] | 146 | err_setstr(AttributeError, name); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 147 | return NULL; |
| 148 | } |
| 149 | |
| 150 | int |
| 151 | setmember(addr, mlist, name, v) |
| 152 | char *addr; |
| 153 | struct memberlist *mlist; |
| 154 | char *name; |
| 155 | object *v; |
| 156 | { |
| 157 | struct memberlist *l; |
| 158 | |
| 159 | for (l = mlist; l->name != NULL; l++) { |
| 160 | if (strcmp(l->name, name) == 0) { |
Jack Jansen | 599f0d1 | 1994-12-14 13:04:05 +0000 | [diff] [blame] | 161 | #ifdef macintosh |
| 162 | if (l->readonly || l->type == T_STRING || l->type == T_PSTRING) { |
| 163 | #else |
| 164 | if (l->readonly || l->type == T_STRING ) { |
| 165 | #endif /* macintosh */ |
Guido van Rossum | 87e7ea7 | 1991-12-10 14:00:03 +0000 | [diff] [blame] | 166 | err_setstr(TypeError, "readonly attribute"); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 167 | return -1; |
| 168 | } |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 169 | if (v == NULL && l->type != T_OBJECT) { |
| 170 | err_setstr(TypeError, |
| 171 | "can't delete numeric/char attribute"); |
| 172 | return -1; |
| 173 | } |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 174 | addr += l->offset; |
| 175 | switch (l->type) { |
Guido van Rossum | ab2490a | 1992-06-03 17:07:23 +0000 | [diff] [blame] | 176 | case T_BYTE: |
| 177 | case T_UBYTE: |
| 178 | if (!is_intobject(v)) { |
| 179 | err_badarg(); |
| 180 | return -1; |
| 181 | } |
| 182 | *(char*)addr = getintvalue(v); |
| 183 | break; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 184 | case T_SHORT: |
Guido van Rossum | ab2490a | 1992-06-03 17:07:23 +0000 | [diff] [blame] | 185 | case T_USHORT: |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 186 | if (!is_intobject(v)) { |
| 187 | err_badarg(); |
| 188 | return -1; |
| 189 | } |
| 190 | *(short*)addr = getintvalue(v); |
| 191 | break; |
Guido van Rossum | ab2490a | 1992-06-03 17:07:23 +0000 | [diff] [blame] | 192 | case T_UINT: |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 193 | case T_INT: |
| 194 | if (!is_intobject(v)) { |
| 195 | err_badarg(); |
| 196 | return -1; |
| 197 | } |
| 198 | *(int*)addr = getintvalue(v); |
| 199 | break; |
| 200 | case T_LONG: |
| 201 | if (!is_intobject(v)) { |
| 202 | err_badarg(); |
| 203 | return -1; |
| 204 | } |
| 205 | *(long*)addr = getintvalue(v); |
| 206 | break; |
Guido van Rossum | ab2490a | 1992-06-03 17:07:23 +0000 | [diff] [blame] | 207 | case T_ULONG: |
| 208 | if (is_intobject(v)) |
| 209 | *(long*)addr = getintvalue(v); |
| 210 | else if (is_longobject(v)) |
| 211 | *(long*)addr = getlongvalue(v); |
| 212 | else { |
| 213 | err_badarg(); |
| 214 | return -1; |
| 215 | } |
| 216 | break; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 217 | case T_FLOAT: |
| 218 | if (is_intobject(v)) |
| 219 | *(float*)addr = getintvalue(v); |
| 220 | else if (is_floatobject(v)) |
| 221 | *(float*)addr = getfloatvalue(v); |
| 222 | else { |
| 223 | err_badarg(); |
| 224 | return -1; |
| 225 | } |
| 226 | break; |
| 227 | case T_DOUBLE: |
| 228 | if (is_intobject(v)) |
| 229 | *(double*)addr = getintvalue(v); |
| 230 | else if (is_floatobject(v)) |
| 231 | *(double*)addr = getfloatvalue(v); |
| 232 | else { |
| 233 | err_badarg(); |
| 234 | return -1; |
| 235 | } |
| 236 | break; |
| 237 | case T_OBJECT: |
| 238 | XDECREF(*(object **)addr); |
| 239 | XINCREF(v); |
| 240 | *(object **)addr = v; |
| 241 | break; |
Guido van Rossum | ab2490a | 1992-06-03 17:07:23 +0000 | [diff] [blame] | 242 | case T_CHAR: |
| 243 | if (is_stringobject(v) && |
| 244 | getstringsize(v) == 1) { |
| 245 | *(char*)addr = |
| 246 | getstringvalue(v)[0]; |
| 247 | } |
| 248 | else { |
| 249 | err_badarg(); |
| 250 | return -1; |
| 251 | } |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 252 | default: |
| 253 | err_setstr(SystemError, "bad memberlist type"); |
| 254 | return -1; |
| 255 | } |
| 256 | return 0; |
| 257 | } |
| 258 | } |
| 259 | |
Guido van Rossum | 87e7ea7 | 1991-12-10 14:00:03 +0000 | [diff] [blame] | 260 | err_setstr(AttributeError, name); |
Guido van Rossum | 73531a3 | 1990-12-20 23:12:40 +0000 | [diff] [blame] | 261 | return -1; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 262 | } |