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