Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 1 | /*********************************************************** |
| 2 | Copyright 1991 by Stichting Mathematisch Centrum, Amsterdam, The |
| 3 | Netherlands. |
| 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 | |
| 31 | object * |
| 32 | getmember(addr, mlist, name) |
| 33 | char *addr; |
| 34 | struct memberlist *mlist; |
| 35 | char *name; |
| 36 | { |
| 37 | struct memberlist *l; |
| 38 | |
| 39 | for (l = mlist; l->name != NULL; l++) { |
| 40 | if (strcmp(l->name, name) == 0) { |
| 41 | object *v; |
| 42 | addr += l->offset; |
| 43 | switch (l->type) { |
| 44 | case T_SHORT: |
| 45 | v = newintobject((long) *(short*)addr); |
| 46 | break; |
| 47 | case T_INT: |
| 48 | v = newintobject((long) *(int*)addr); |
| 49 | break; |
| 50 | case T_LONG: |
| 51 | v = newintobject(*(long*)addr); |
| 52 | break; |
| 53 | case T_FLOAT: |
| 54 | v = newfloatobject((double)*(float*)addr); |
| 55 | break; |
| 56 | case T_DOUBLE: |
| 57 | v = newfloatobject(*(double*)addr); |
| 58 | break; |
| 59 | case T_STRING: |
| 60 | if (*(char**)addr == NULL) { |
| 61 | INCREF(None); |
| 62 | v = None; |
| 63 | } |
| 64 | else |
| 65 | v = newstringobject(*(char**)addr); |
| 66 | break; |
| 67 | case T_OBJECT: |
| 68 | v = *(object **)addr; |
| 69 | if (v == NULL) |
| 70 | v = None; |
| 71 | INCREF(v); |
| 72 | break; |
| 73 | default: |
| 74 | err_setstr(SystemError, "bad memberlist type"); |
| 75 | v = NULL; |
| 76 | } |
| 77 | return v; |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | err_setstr(NameError, name); |
| 82 | return NULL; |
| 83 | } |
| 84 | |
| 85 | int |
| 86 | setmember(addr, mlist, name, v) |
| 87 | char *addr; |
| 88 | struct memberlist *mlist; |
| 89 | char *name; |
| 90 | object *v; |
| 91 | { |
| 92 | struct memberlist *l; |
| 93 | |
| 94 | for (l = mlist; l->name != NULL; l++) { |
| 95 | if (strcmp(l->name, name) == 0) { |
| 96 | if (l->readonly || l->type == T_STRING) { |
| 97 | err_setstr(RuntimeError, "readonly attribute"); |
| 98 | return -1; |
| 99 | } |
| 100 | addr += l->offset; |
| 101 | switch (l->type) { |
| 102 | case T_SHORT: |
| 103 | if (!is_intobject(v)) { |
| 104 | err_badarg(); |
| 105 | return -1; |
| 106 | } |
| 107 | *(short*)addr = getintvalue(v); |
| 108 | break; |
| 109 | case T_INT: |
| 110 | if (!is_intobject(v)) { |
| 111 | err_badarg(); |
| 112 | return -1; |
| 113 | } |
| 114 | *(int*)addr = getintvalue(v); |
| 115 | break; |
| 116 | case T_LONG: |
| 117 | if (!is_intobject(v)) { |
| 118 | err_badarg(); |
| 119 | return -1; |
| 120 | } |
| 121 | *(long*)addr = getintvalue(v); |
| 122 | break; |
| 123 | case T_FLOAT: |
| 124 | if (is_intobject(v)) |
| 125 | *(float*)addr = getintvalue(v); |
| 126 | else if (is_floatobject(v)) |
| 127 | *(float*)addr = getfloatvalue(v); |
| 128 | else { |
| 129 | err_badarg(); |
| 130 | return -1; |
| 131 | } |
| 132 | break; |
| 133 | case T_DOUBLE: |
| 134 | if (is_intobject(v)) |
| 135 | *(double*)addr = getintvalue(v); |
| 136 | else if (is_floatobject(v)) |
| 137 | *(double*)addr = getfloatvalue(v); |
| 138 | else { |
| 139 | err_badarg(); |
| 140 | return -1; |
| 141 | } |
| 142 | break; |
| 143 | case T_OBJECT: |
| 144 | XDECREF(*(object **)addr); |
| 145 | XINCREF(v); |
| 146 | *(object **)addr = v; |
| 147 | break; |
| 148 | default: |
| 149 | err_setstr(SystemError, "bad memberlist type"); |
| 150 | return -1; |
| 151 | } |
| 152 | return 0; |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | err_setstr(NameError, name); |
Guido van Rossum | 73531a3 | 1990-12-20 23:12:40 +0000 | [diff] [blame] | 157 | return -1; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 158 | } |