blob: acdf9c6717356990377b7fbdc2b2ade4dae36095 [file] [log] [blame]
Guido van Rossumf70e43a1991-02-19 12:39:46 +00001/***********************************************************
2Copyright 1991 by Stichting Mathematisch Centrum, Amsterdam, The
3Netherlands.
4
5 All Rights Reserved
6
7Permission to use, copy, modify, and distribute this software and its
8documentation for any purpose and without fee is hereby granted,
9provided that the above copyright notice appear in all copies and that
10both that copyright notice and this permission notice appear in
11supporting documentation, and that the names of Stichting Mathematisch
12Centrum or CWI not be used in advertising or publicity pertaining to
13distribution of the software without specific, written prior permission.
14
15STICHTING MATHEMATISCH CENTRUM DISCLAIMS ALL WARRANTIES WITH REGARD TO
16THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
17FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH CENTRUM BE LIABLE
18FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
19WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
20ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
21OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
22
23******************************************************************/
24
Guido van Rossum3f5da241990-12-20 15:06:42 +000025/* Map C struct members to Python object attributes */
26
27#include "allobjects.h"
28
29#include "structmember.h"
30
Guido van Rossum2f1d87e1991-10-20 20:24:14 +000031static object *
32listmembers(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 Rossum3f5da241990-12-20 15:06:42 +000054object *
55getmember(addr, mlist, name)
56 char *addr;
57 struct memberlist *mlist;
58 char *name;
59{
60 struct memberlist *l;
61
Guido van Rossum2f1d87e1991-10-20 20:24:14 +000062 if (strcmp(name, "__members__") == 0)
63 return listmembers(mlist);
Guido van Rossum3f5da241990-12-20 15:06:42 +000064 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) {
69 case T_SHORT:
70 v = newintobject((long) *(short*)addr);
71 break;
72 case T_INT:
73 v = newintobject((long) *(int*)addr);
74 break;
75 case T_LONG:
76 v = newintobject(*(long*)addr);
77 break;
78 case T_FLOAT:
79 v = newfloatobject((double)*(float*)addr);
80 break;
81 case T_DOUBLE:
82 v = newfloatobject(*(double*)addr);
83 break;
84 case T_STRING:
85 if (*(char**)addr == NULL) {
86 INCREF(None);
87 v = None;
88 }
89 else
90 v = newstringobject(*(char**)addr);
91 break;
92 case T_OBJECT:
93 v = *(object **)addr;
94 if (v == NULL)
95 v = None;
96 INCREF(v);
97 break;
98 default:
99 err_setstr(SystemError, "bad memberlist type");
100 v = NULL;
101 }
102 return v;
103 }
104 }
105
106 err_setstr(NameError, name);
107 return NULL;
108}
109
110int
111setmember(addr, mlist, name, v)
112 char *addr;
113 struct memberlist *mlist;
114 char *name;
115 object *v;
116{
117 struct memberlist *l;
118
119 for (l = mlist; l->name != NULL; l++) {
120 if (strcmp(l->name, name) == 0) {
121 if (l->readonly || l->type == T_STRING) {
122 err_setstr(RuntimeError, "readonly attribute");
123 return -1;
124 }
125 addr += l->offset;
126 switch (l->type) {
127 case T_SHORT:
128 if (!is_intobject(v)) {
129 err_badarg();
130 return -1;
131 }
132 *(short*)addr = getintvalue(v);
133 break;
134 case T_INT:
135 if (!is_intobject(v)) {
136 err_badarg();
137 return -1;
138 }
139 *(int*)addr = getintvalue(v);
140 break;
141 case T_LONG:
142 if (!is_intobject(v)) {
143 err_badarg();
144 return -1;
145 }
146 *(long*)addr = getintvalue(v);
147 break;
148 case T_FLOAT:
149 if (is_intobject(v))
150 *(float*)addr = getintvalue(v);
151 else if (is_floatobject(v))
152 *(float*)addr = getfloatvalue(v);
153 else {
154 err_badarg();
155 return -1;
156 }
157 break;
158 case T_DOUBLE:
159 if (is_intobject(v))
160 *(double*)addr = getintvalue(v);
161 else if (is_floatobject(v))
162 *(double*)addr = getfloatvalue(v);
163 else {
164 err_badarg();
165 return -1;
166 }
167 break;
168 case T_OBJECT:
169 XDECREF(*(object **)addr);
170 XINCREF(v);
171 *(object **)addr = v;
172 break;
173 default:
174 err_setstr(SystemError, "bad memberlist type");
175 return -1;
176 }
177 return 0;
178 }
179 }
180
181 err_setstr(NameError, name);
Guido van Rossum73531a31990-12-20 23:12:40 +0000182 return -1;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000183}