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 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 34 | #include "Python.h" |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 35 | |
| 36 | #include "structmember.h" |
| 37 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 38 | static PyObject * |
Guido van Rossum | 2f1d87e | 1991-10-20 20:24:14 +0000 | [diff] [blame] | 39 | listmembers(mlist) |
| 40 | struct memberlist *mlist; |
| 41 | { |
| 42 | int i, n; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 43 | PyObject *v; |
Guido van Rossum | 2f1d87e | 1991-10-20 20:24:14 +0000 | [diff] [blame] | 44 | for (n = 0; mlist[n].name != NULL; n++) |
| 45 | ; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 46 | v = PyList_New(n); |
Guido van Rossum | 2f1d87e | 1991-10-20 20:24:14 +0000 | [diff] [blame] | 47 | if (v != NULL) { |
| 48 | for (i = 0; i < n; i++) |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 49 | PyList_SetItem(v, i, |
| 50 | PyString_FromString(mlist[i].name)); |
| 51 | if (PyErr_Occurred()) { |
| 52 | Py_DECREF(v); |
Guido van Rossum | 2f1d87e | 1991-10-20 20:24:14 +0000 | [diff] [blame] | 53 | v = NULL; |
| 54 | } |
| 55 | else { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 56 | PyList_Sort(v); |
Guido van Rossum | 2f1d87e | 1991-10-20 20:24:14 +0000 | [diff] [blame] | 57 | } |
| 58 | } |
| 59 | return v; |
| 60 | } |
| 61 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 62 | PyObject * |
| 63 | PyMember_Get(addr, mlist, name) |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 64 | char *addr; |
| 65 | struct memberlist *mlist; |
| 66 | char *name; |
| 67 | { |
| 68 | struct memberlist *l; |
| 69 | |
Guido van Rossum | 2f1d87e | 1991-10-20 20:24:14 +0000 | [diff] [blame] | 70 | if (strcmp(name, "__members__") == 0) |
| 71 | return listmembers(mlist); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 72 | for (l = mlist; l->name != NULL; l++) { |
| 73 | if (strcmp(l->name, name) == 0) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 74 | PyObject *v; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 75 | addr += l->offset; |
| 76 | switch (l->type) { |
Guido van Rossum | ab2490a | 1992-06-03 17:07:23 +0000 | [diff] [blame] | 77 | case T_BYTE: |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 78 | v = PyInt_FromLong((long) |
Guido van Rossum | ab2490a | 1992-06-03 17:07:23 +0000 | [diff] [blame] | 79 | (((*(char*)addr & 0xff) |
| 80 | ^ 0x80) - 0x80)); |
| 81 | break; |
| 82 | case T_UBYTE: |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 83 | v = PyInt_FromLong((long) *(char*)addr & 0xff); |
Guido van Rossum | ab2490a | 1992-06-03 17:07:23 +0000 | [diff] [blame] | 84 | break; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 85 | case T_SHORT: |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 86 | v = PyInt_FromLong((long) *(short*)addr); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 87 | break; |
Guido van Rossum | ab2490a | 1992-06-03 17:07:23 +0000 | [diff] [blame] | 88 | case T_USHORT: |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 89 | v = PyInt_FromLong((long) |
Guido van Rossum | ab2490a | 1992-06-03 17:07:23 +0000 | [diff] [blame] | 90 | *(unsigned short*)addr); |
| 91 | break; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 92 | case T_INT: |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 93 | v = PyInt_FromLong((long) *(int*)addr); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 94 | break; |
Guido van Rossum | ab2490a | 1992-06-03 17:07:23 +0000 | [diff] [blame] | 95 | case T_UINT: |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 96 | v = PyInt_FromLong((long) |
| 97 | *(unsigned int*)addr); |
Guido van Rossum | ab2490a | 1992-06-03 17:07:23 +0000 | [diff] [blame] | 98 | break; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 99 | case T_LONG: |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 100 | v = PyInt_FromLong(*(long*)addr); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 101 | break; |
Guido van Rossum | ab2490a | 1992-06-03 17:07:23 +0000 | [diff] [blame] | 102 | case T_ULONG: |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 103 | v = PyLong_FromDouble((double) |
Guido van Rossum | ab2490a | 1992-06-03 17:07:23 +0000 | [diff] [blame] | 104 | *(unsigned long*)addr); |
| 105 | break; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 106 | case T_FLOAT: |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 107 | v = PyFloat_FromDouble((double)*(float*)addr); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 108 | break; |
| 109 | case T_DOUBLE: |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 110 | v = PyFloat_FromDouble(*(double*)addr); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 111 | break; |
| 112 | case T_STRING: |
| 113 | if (*(char**)addr == NULL) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 114 | Py_INCREF(Py_None); |
| 115 | v = Py_None; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 116 | } |
| 117 | else |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 118 | v = PyString_FromString(*(char**)addr); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 119 | break; |
Jack Jansen | 599f0d1 | 1994-12-14 13:04:05 +0000 | [diff] [blame] | 120 | case T_STRING_INPLACE: |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 121 | v = PyString_FromString((char*)addr); |
Jack Jansen | 599f0d1 | 1994-12-14 13:04:05 +0000 | [diff] [blame] | 122 | break; |
| 123 | #ifdef macintosh |
| 124 | case T_PSTRING: |
| 125 | if (*(char**)addr == NULL) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 126 | Py_INCREF(Py_None); |
| 127 | v = Py_None; |
Jack Jansen | 599f0d1 | 1994-12-14 13:04:05 +0000 | [diff] [blame] | 128 | } |
| 129 | else |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 130 | v = PyString_FromStringAndSize( |
| 131 | (*(char**)addr)+1, |
| 132 | **(unsigned char**)addr); |
Jack Jansen | 599f0d1 | 1994-12-14 13:04:05 +0000 | [diff] [blame] | 133 | break; |
| 134 | case T_PSTRING_INPLACE: |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 135 | v = PyString_FromStringAndSize( |
| 136 | ((char*)addr)+1, |
| 137 | *(unsigned char*)addr); |
Jack Jansen | 599f0d1 | 1994-12-14 13:04:05 +0000 | [diff] [blame] | 138 | break; |
| 139 | #endif /* macintosh */ |
Guido van Rossum | ab2490a | 1992-06-03 17:07:23 +0000 | [diff] [blame] | 140 | case T_CHAR: |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 141 | v = PyString_FromStringAndSize((char*)addr, 1); |
Guido van Rossum | ab2490a | 1992-06-03 17:07:23 +0000 | [diff] [blame] | 142 | break; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 143 | case T_OBJECT: |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 144 | v = *(PyObject **)addr; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 145 | if (v == NULL) |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 146 | v = Py_None; |
| 147 | Py_INCREF(v); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 148 | break; |
| 149 | default: |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 150 | PyErr_SetString(PyExc_SystemError, |
| 151 | "bad memberlist type"); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 152 | v = NULL; |
| 153 | } |
| 154 | return v; |
| 155 | } |
| 156 | } |
| 157 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 158 | PyErr_SetString(PyExc_AttributeError, name); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 159 | return NULL; |
| 160 | } |
| 161 | |
| 162 | int |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 163 | PyMember_Set(addr, mlist, name, v) |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 164 | char *addr; |
| 165 | struct memberlist *mlist; |
| 166 | char *name; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 167 | PyObject *v; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 168 | { |
| 169 | struct memberlist *l; |
| 170 | |
| 171 | for (l = mlist; l->name != NULL; l++) { |
| 172 | if (strcmp(l->name, name) == 0) { |
Jack Jansen | 599f0d1 | 1994-12-14 13:04:05 +0000 | [diff] [blame] | 173 | #ifdef macintosh |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 174 | if (l->readonly || l->type == T_STRING || |
| 175 | l->type == T_PSTRING) |
| 176 | { |
Jack Jansen | 599f0d1 | 1994-12-14 13:04:05 +0000 | [diff] [blame] | 177 | #else |
| 178 | if (l->readonly || l->type == T_STRING ) { |
| 179 | #endif /* macintosh */ |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 180 | PyErr_SetString(PyExc_TypeError, |
| 181 | "readonly attribute"); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 182 | return -1; |
| 183 | } |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 184 | if (v == NULL && l->type != T_OBJECT) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 185 | PyErr_SetString(PyExc_TypeError, |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 186 | "can't delete numeric/char attribute"); |
| 187 | return -1; |
| 188 | } |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 189 | addr += l->offset; |
| 190 | switch (l->type) { |
Guido van Rossum | ab2490a | 1992-06-03 17:07:23 +0000 | [diff] [blame] | 191 | case T_BYTE: |
| 192 | case T_UBYTE: |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 193 | if (!PyInt_Check(v)) { |
| 194 | PyErr_BadArgument(); |
Guido van Rossum | ab2490a | 1992-06-03 17:07:23 +0000 | [diff] [blame] | 195 | return -1; |
| 196 | } |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 197 | *(char*)addr = (char) PyInt_AsLong(v); |
Guido van Rossum | ab2490a | 1992-06-03 17:07:23 +0000 | [diff] [blame] | 198 | break; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 199 | case T_SHORT: |
Guido van Rossum | ab2490a | 1992-06-03 17:07:23 +0000 | [diff] [blame] | 200 | case T_USHORT: |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 201 | if (!PyInt_Check(v)) { |
| 202 | PyErr_BadArgument(); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 203 | return -1; |
| 204 | } |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 205 | *(short*)addr = (short) PyInt_AsLong(v); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 206 | break; |
Guido van Rossum | ab2490a | 1992-06-03 17:07:23 +0000 | [diff] [blame] | 207 | case T_UINT: |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 208 | case T_INT: |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 209 | if (!PyInt_Check(v)) { |
| 210 | PyErr_BadArgument(); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 211 | return -1; |
| 212 | } |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 213 | *(int*)addr = (int) PyInt_AsLong(v); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 214 | break; |
| 215 | case T_LONG: |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 216 | if (!PyInt_Check(v)) { |
| 217 | PyErr_BadArgument(); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 218 | return -1; |
| 219 | } |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 220 | *(long*)addr = PyInt_AsLong(v); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 221 | break; |
Guido van Rossum | ab2490a | 1992-06-03 17:07:23 +0000 | [diff] [blame] | 222 | case T_ULONG: |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 223 | if (PyInt_Check(v)) |
| 224 | *(long*)addr = PyInt_AsLong(v); |
| 225 | else if (PyLong_Check(v)) |
| 226 | *(long*)addr = PyLong_AsLong(v); |
Guido van Rossum | ab2490a | 1992-06-03 17:07:23 +0000 | [diff] [blame] | 227 | else { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 228 | PyErr_BadArgument(); |
Guido van Rossum | ab2490a | 1992-06-03 17:07:23 +0000 | [diff] [blame] | 229 | return -1; |
| 230 | } |
| 231 | break; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 232 | case T_FLOAT: |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 233 | if (PyInt_Check(v)) |
Guido van Rossum | 644a12b | 1997-04-09 19:24:53 +0000 | [diff] [blame] | 234 | *(float*)addr = |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 235 | (float) PyInt_AsLong(v); |
| 236 | else if (PyFloat_Check(v)) |
| 237 | *(float*)addr = |
| 238 | (float) PyFloat_AsDouble(v); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 239 | else { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 240 | PyErr_BadArgument(); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 241 | return -1; |
| 242 | } |
| 243 | break; |
| 244 | case T_DOUBLE: |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 245 | if (PyInt_Check(v)) |
Guido van Rossum | 644a12b | 1997-04-09 19:24:53 +0000 | [diff] [blame] | 246 | *(double*)addr = |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 247 | (double) PyInt_AsLong(v); |
| 248 | else if (PyFloat_Check(v)) |
| 249 | *(double*)addr = PyFloat_AsDouble(v); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 250 | else { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 251 | PyErr_BadArgument(); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 252 | return -1; |
| 253 | } |
| 254 | break; |
| 255 | case T_OBJECT: |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 256 | Py_XDECREF(*(PyObject **)addr); |
| 257 | Py_XINCREF(v); |
| 258 | *(PyObject **)addr = v; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 259 | break; |
Guido van Rossum | ab2490a | 1992-06-03 17:07:23 +0000 | [diff] [blame] | 260 | case T_CHAR: |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 261 | if (PyString_Check(v) && |
| 262 | PyString_Size(v) == 1) { |
Guido van Rossum | ab2490a | 1992-06-03 17:07:23 +0000 | [diff] [blame] | 263 | *(char*)addr = |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 264 | PyString_AsString(v)[0]; |
Guido van Rossum | ab2490a | 1992-06-03 17:07:23 +0000 | [diff] [blame] | 265 | } |
| 266 | else { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 267 | PyErr_BadArgument(); |
Guido van Rossum | ab2490a | 1992-06-03 17:07:23 +0000 | [diff] [blame] | 268 | return -1; |
| 269 | } |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 270 | default: |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 271 | PyErr_SetString(PyExc_SystemError, |
| 272 | "bad memberlist type"); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 273 | return -1; |
| 274 | } |
| 275 | return 0; |
| 276 | } |
| 277 | } |
| 278 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 279 | PyErr_SetString(PyExc_AttributeError, name); |
Guido van Rossum | 73531a3 | 1990-12-20 23:12:40 +0000 | [diff] [blame] | 280 | return -1; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 281 | } |