annotate Python/structmember.c @ 95:a5bf7d5959ce legacy-trunk

Return -1 for errors.
author Guido van Rossum <guido@python.org>
date Thu, 20 Dec 1990 23:12:40 +0000
parents a6934380c6e7
children 853b736a6a9e
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
1 /* Map C struct members to Python object attributes */
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
2
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
3 #include "allobjects.h"
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
4
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
5 #include "structmember.h"
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
6
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
7 object *
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
8 getmember(addr, mlist, name)
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
9 char *addr;
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
10 struct memberlist *mlist;
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
11 char *name;
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
12 {
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
13 struct memberlist *l;
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
14
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
15 for (l = mlist; l->name != NULL; l++) {
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
16 if (strcmp(l->name, name) == 0) {
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
17 object *v;
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
18 addr += l->offset;
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
19 switch (l->type) {
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
20 case T_SHORT:
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
21 v = newintobject((long) *(short*)addr);
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
22 break;
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
23 case T_INT:
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
24 v = newintobject((long) *(int*)addr);
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
25 break;
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
26 case T_LONG:
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
27 v = newintobject(*(long*)addr);
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
28 break;
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
29 case T_FLOAT:
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
30 v = newfloatobject((double)*(float*)addr);
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
31 break;
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
32 case T_DOUBLE:
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
33 v = newfloatobject(*(double*)addr);
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
34 break;
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
35 case T_STRING:
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
36 if (*(char**)addr == NULL) {
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
37 INCREF(None);
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
38 v = None;
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
39 }
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
40 else
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
41 v = newstringobject(*(char**)addr);
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
42 break;
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
43 case T_OBJECT:
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
44 v = *(object **)addr;
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
45 if (v == NULL)
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
46 v = None;
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
47 INCREF(v);
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
48 break;
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
49 default:
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
50 err_setstr(SystemError, "bad memberlist type");
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
51 v = NULL;
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
52 }
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
53 return v;
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
54 }
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
55 }
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
56
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
57 err_setstr(NameError, name);
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
58 return NULL;
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
59 }
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
60
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
61 int
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
62 setmember(addr, mlist, name, v)
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
63 char *addr;
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
64 struct memberlist *mlist;
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
65 char *name;
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
66 object *v;
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
67 {
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
68 struct memberlist *l;
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
69
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
70 for (l = mlist; l->name != NULL; l++) {
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
71 if (strcmp(l->name, name) == 0) {
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
72 if (l->readonly || l->type == T_STRING) {
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
73 err_setstr(RuntimeError, "readonly attribute");
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
74 return -1;
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
75 }
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
76 addr += l->offset;
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
77 switch (l->type) {
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
78 case T_SHORT:
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
79 if (!is_intobject(v)) {
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
80 err_badarg();
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
81 return -1;
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
82 }
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
83 *(short*)addr = getintvalue(v);
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
84 break;
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
85 case T_INT:
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
86 if (!is_intobject(v)) {
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
87 err_badarg();
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
88 return -1;
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
89 }
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
90 *(int*)addr = getintvalue(v);
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
91 break;
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
92 case T_LONG:
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
93 if (!is_intobject(v)) {
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
94 err_badarg();
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
95 return -1;
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
96 }
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
97 *(long*)addr = getintvalue(v);
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
98 break;
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
99 case T_FLOAT:
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
100 if (is_intobject(v))
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
101 *(float*)addr = getintvalue(v);
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
102 else if (is_floatobject(v))
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
103 *(float*)addr = getfloatvalue(v);
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
104 else {
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
105 err_badarg();
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
106 return -1;
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
107 }
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
108 break;
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
109 case T_DOUBLE:
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
110 if (is_intobject(v))
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
111 *(double*)addr = getintvalue(v);
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
112 else if (is_floatobject(v))
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
113 *(double*)addr = getfloatvalue(v);
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
114 else {
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
115 err_badarg();
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
116 return -1;
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
117 }
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
118 break;
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
119 case T_OBJECT:
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
120 XDECREF(*(object **)addr);
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
121 XINCREF(v);
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
122 *(object **)addr = v;
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
123 break;
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
124 default:
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
125 err_setstr(SystemError, "bad memberlist type");
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
126 return -1;
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
127 }
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
128 return 0;
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
129 }
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
130 }
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
131
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
132 err_setstr(NameError, name);
95
a5bf7d5959ce Return -1 for errors.
Guido van Rossum <guido@python.org>
parents: 86
diff changeset
133 return -1;
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
134 }