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; |
Guido van Rossum | adf0e43 | 1998-05-20 22:25:32 +0000 | [diff] [blame] | 170 | PyObject *oldv; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 171 | |
| 172 | for (l = mlist; l->name != NULL; l++) { |
| 173 | if (strcmp(l->name, name) == 0) { |
Jack Jansen | 599f0d1 | 1994-12-14 13:04:05 +0000 | [diff] [blame] | 174 | #ifdef macintosh |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 175 | if (l->readonly || l->type == T_STRING || |
| 176 | l->type == T_PSTRING) |
| 177 | { |
Jack Jansen | 599f0d1 | 1994-12-14 13:04:05 +0000 | [diff] [blame] | 178 | #else |
| 179 | if (l->readonly || l->type == T_STRING ) { |
| 180 | #endif /* macintosh */ |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 181 | PyErr_SetString(PyExc_TypeError, |
| 182 | "readonly attribute"); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 183 | return -1; |
| 184 | } |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 185 | if (v == NULL && l->type != T_OBJECT) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 186 | PyErr_SetString(PyExc_TypeError, |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 187 | "can't delete numeric/char attribute"); |
| 188 | return -1; |
| 189 | } |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 190 | addr += l->offset; |
| 191 | switch (l->type) { |
Guido van Rossum | ab2490a | 1992-06-03 17:07:23 +0000 | [diff] [blame] | 192 | case T_BYTE: |
| 193 | case T_UBYTE: |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 194 | if (!PyInt_Check(v)) { |
| 195 | PyErr_BadArgument(); |
Guido van Rossum | ab2490a | 1992-06-03 17:07:23 +0000 | [diff] [blame] | 196 | return -1; |
| 197 | } |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 198 | *(char*)addr = (char) PyInt_AsLong(v); |
Guido van Rossum | ab2490a | 1992-06-03 17:07:23 +0000 | [diff] [blame] | 199 | break; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 200 | case T_SHORT: |
Guido van Rossum | ab2490a | 1992-06-03 17:07:23 +0000 | [diff] [blame] | 201 | case T_USHORT: |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 202 | if (!PyInt_Check(v)) { |
| 203 | PyErr_BadArgument(); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 204 | return -1; |
| 205 | } |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 206 | *(short*)addr = (short) PyInt_AsLong(v); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 207 | break; |
Guido van Rossum | ab2490a | 1992-06-03 17:07:23 +0000 | [diff] [blame] | 208 | case T_UINT: |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 209 | case T_INT: |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 210 | if (!PyInt_Check(v)) { |
| 211 | PyErr_BadArgument(); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 212 | return -1; |
| 213 | } |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 214 | *(int*)addr = (int) PyInt_AsLong(v); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 215 | break; |
| 216 | case T_LONG: |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 217 | if (!PyInt_Check(v)) { |
| 218 | PyErr_BadArgument(); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 219 | return -1; |
| 220 | } |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 221 | *(long*)addr = PyInt_AsLong(v); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 222 | break; |
Guido van Rossum | ab2490a | 1992-06-03 17:07:23 +0000 | [diff] [blame] | 223 | case T_ULONG: |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 224 | if (PyInt_Check(v)) |
| 225 | *(long*)addr = PyInt_AsLong(v); |
| 226 | else if (PyLong_Check(v)) |
| 227 | *(long*)addr = PyLong_AsLong(v); |
Guido van Rossum | ab2490a | 1992-06-03 17:07:23 +0000 | [diff] [blame] | 228 | else { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 229 | PyErr_BadArgument(); |
Guido van Rossum | ab2490a | 1992-06-03 17:07:23 +0000 | [diff] [blame] | 230 | return -1; |
| 231 | } |
| 232 | break; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 233 | case T_FLOAT: |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 234 | if (PyInt_Check(v)) |
Guido van Rossum | 644a12b | 1997-04-09 19:24:53 +0000 | [diff] [blame] | 235 | *(float*)addr = |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 236 | (float) PyInt_AsLong(v); |
| 237 | else if (PyFloat_Check(v)) |
| 238 | *(float*)addr = |
| 239 | (float) PyFloat_AsDouble(v); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 240 | else { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 241 | PyErr_BadArgument(); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 242 | return -1; |
| 243 | } |
| 244 | break; |
| 245 | case T_DOUBLE: |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 246 | if (PyInt_Check(v)) |
Guido van Rossum | 644a12b | 1997-04-09 19:24:53 +0000 | [diff] [blame] | 247 | *(double*)addr = |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 248 | (double) PyInt_AsLong(v); |
| 249 | else if (PyFloat_Check(v)) |
| 250 | *(double*)addr = PyFloat_AsDouble(v); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 251 | else { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 252 | PyErr_BadArgument(); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 253 | return -1; |
| 254 | } |
| 255 | break; |
| 256 | case T_OBJECT: |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 257 | Py_XINCREF(v); |
Guido van Rossum | adf0e43 | 1998-05-20 22:25:32 +0000 | [diff] [blame] | 258 | oldv = *(PyObject **)addr; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 259 | *(PyObject **)addr = v; |
Guido van Rossum | adf0e43 | 1998-05-20 22:25:32 +0000 | [diff] [blame] | 260 | Py_XDECREF(oldv); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 261 | break; |
Guido van Rossum | ab2490a | 1992-06-03 17:07:23 +0000 | [diff] [blame] | 262 | case T_CHAR: |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 263 | if (PyString_Check(v) && |
| 264 | PyString_Size(v) == 1) { |
Guido van Rossum | ab2490a | 1992-06-03 17:07:23 +0000 | [diff] [blame] | 265 | *(char*)addr = |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 266 | PyString_AsString(v)[0]; |
Guido van Rossum | ab2490a | 1992-06-03 17:07:23 +0000 | [diff] [blame] | 267 | } |
| 268 | else { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 269 | PyErr_BadArgument(); |
Guido van Rossum | ab2490a | 1992-06-03 17:07:23 +0000 | [diff] [blame] | 270 | return -1; |
| 271 | } |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 272 | default: |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 273 | PyErr_SetString(PyExc_SystemError, |
| 274 | "bad memberlist type"); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 275 | return -1; |
| 276 | } |
| 277 | return 0; |
| 278 | } |
| 279 | } |
| 280 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 281 | PyErr_SetString(PyExc_AttributeError, name); |
Guido van Rossum | 73531a3 | 1990-12-20 23:12:40 +0000 | [diff] [blame] | 282 | return -1; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 283 | } |