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 | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 32 | /* Functions used by cgen output */ |
| 33 | |
Guido van Rossum | 3837de0 | 1997-04-29 15:43:55 +0000 | [diff] [blame] | 34 | #include "Python.h" |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 35 | #include "cgensupport.h" |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 36 | |
| 37 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 38 | /* Functions to extract arguments. |
| 39 | These needs to know the total number of arguments supplied, |
| 40 | since the argument list is a tuple only of there is more than |
| 41 | one argument. */ |
| 42 | |
| 43 | int |
Guido van Rossum | 3837de0 | 1997-04-29 15:43:55 +0000 | [diff] [blame] | 44 | PyArg_GetObject(args, nargs, i, p_arg) |
| 45 | register PyObject *args; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 46 | int nargs, i; |
Guido van Rossum | 3837de0 | 1997-04-29 15:43:55 +0000 | [diff] [blame] | 47 | PyObject **p_arg; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 48 | { |
| 49 | if (nargs != 1) { |
Guido van Rossum | 3837de0 | 1997-04-29 15:43:55 +0000 | [diff] [blame] | 50 | if (args == NULL || !PyTuple_Check(args) || |
| 51 | nargs != PyTuple_Size(args) || |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 52 | i < 0 || i >= nargs) { |
Guido van Rossum | 3837de0 | 1997-04-29 15:43:55 +0000 | [diff] [blame] | 53 | return PyErr_BadArgument(); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 54 | } |
| 55 | else { |
Guido van Rossum | 3837de0 | 1997-04-29 15:43:55 +0000 | [diff] [blame] | 56 | args = PyTuple_GetItem(args, i); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 57 | } |
| 58 | } |
| 59 | if (args == NULL) { |
Guido van Rossum | 3837de0 | 1997-04-29 15:43:55 +0000 | [diff] [blame] | 60 | return PyErr_BadArgument(); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 61 | } |
| 62 | *p_arg = args; |
| 63 | return 1; |
| 64 | } |
| 65 | |
| 66 | int |
Guido van Rossum | 3837de0 | 1997-04-29 15:43:55 +0000 | [diff] [blame] | 67 | PyArg_GetLong(args, nargs, i, p_arg) |
| 68 | register PyObject *args; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 69 | int nargs, i; |
| 70 | long *p_arg; |
| 71 | { |
| 72 | if (nargs != 1) { |
Guido van Rossum | 3837de0 | 1997-04-29 15:43:55 +0000 | [diff] [blame] | 73 | if (args == NULL || !PyTuple_Check(args) || |
| 74 | nargs != PyTuple_Size(args) || |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 75 | i < 0 || i >= nargs) { |
Guido van Rossum | 3837de0 | 1997-04-29 15:43:55 +0000 | [diff] [blame] | 76 | return PyErr_BadArgument(); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 77 | } |
Guido van Rossum | 3837de0 | 1997-04-29 15:43:55 +0000 | [diff] [blame] | 78 | args = PyTuple_GetItem(args, i); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 79 | } |
Guido van Rossum | 3837de0 | 1997-04-29 15:43:55 +0000 | [diff] [blame] | 80 | if (args == NULL || !PyInt_Check(args)) { |
| 81 | return PyErr_BadArgument(); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 82 | } |
Guido van Rossum | 3837de0 | 1997-04-29 15:43:55 +0000 | [diff] [blame] | 83 | *p_arg = PyInt_AsLong(args); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 84 | return 1; |
| 85 | } |
| 86 | |
| 87 | int |
Guido van Rossum | 3837de0 | 1997-04-29 15:43:55 +0000 | [diff] [blame] | 88 | PyArg_GetShort(args, nargs, i, p_arg) |
| 89 | register PyObject *args; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 90 | int nargs, i; |
| 91 | short *p_arg; |
| 92 | { |
| 93 | long x; |
Guido van Rossum | 3837de0 | 1997-04-29 15:43:55 +0000 | [diff] [blame] | 94 | if (!PyArg_GetLong(args, nargs, i, &x)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 95 | return 0; |
Guido van Rossum | 6bf62da | 1997-04-11 20:37:35 +0000 | [diff] [blame] | 96 | *p_arg = (short) x; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 97 | return 1; |
| 98 | } |
| 99 | |
| 100 | static int |
| 101 | extractdouble(v, p_arg) |
Guido van Rossum | 3837de0 | 1997-04-29 15:43:55 +0000 | [diff] [blame] | 102 | register PyObject *v; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 103 | double *p_arg; |
| 104 | { |
| 105 | if (v == NULL) { |
| 106 | /* Fall through to error return at end of function */ |
| 107 | } |
Guido van Rossum | 3837de0 | 1997-04-29 15:43:55 +0000 | [diff] [blame] | 108 | else if (PyFloat_Check(v)) { |
| 109 | *p_arg = PyFloat_AS_DOUBLE((PyFloatObject *)v); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 110 | return 1; |
| 111 | } |
Guido van Rossum | 3837de0 | 1997-04-29 15:43:55 +0000 | [diff] [blame] | 112 | else if (PyInt_Check(v)) { |
| 113 | *p_arg = PyInt_AS_LONG((PyIntObject *)v); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 114 | return 1; |
| 115 | } |
Guido van Rossum | 3837de0 | 1997-04-29 15:43:55 +0000 | [diff] [blame] | 116 | else if (PyLong_Check(v)) { |
| 117 | *p_arg = PyLong_AsDouble(v); |
Guido van Rossum | 76a00af | 1991-07-27 21:34:00 +0000 | [diff] [blame] | 118 | return 1; |
| 119 | } |
Guido van Rossum | 3837de0 | 1997-04-29 15:43:55 +0000 | [diff] [blame] | 120 | return PyErr_BadArgument(); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | static int |
| 124 | extractfloat(v, p_arg) |
Guido van Rossum | 3837de0 | 1997-04-29 15:43:55 +0000 | [diff] [blame] | 125 | register PyObject *v; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 126 | float *p_arg; |
| 127 | { |
| 128 | if (v == NULL) { |
| 129 | /* Fall through to error return at end of function */ |
| 130 | } |
Guido van Rossum | 3837de0 | 1997-04-29 15:43:55 +0000 | [diff] [blame] | 131 | else if (PyFloat_Check(v)) { |
| 132 | *p_arg = (float) PyFloat_AS_DOUBLE((PyFloatObject *)v); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 133 | return 1; |
| 134 | } |
Guido van Rossum | 3837de0 | 1997-04-29 15:43:55 +0000 | [diff] [blame] | 135 | else if (PyInt_Check(v)) { |
| 136 | *p_arg = (float) PyInt_AS_LONG((PyIntObject *)v); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 137 | return 1; |
| 138 | } |
Guido van Rossum | 3837de0 | 1997-04-29 15:43:55 +0000 | [diff] [blame] | 139 | else if (PyLong_Check(v)) { |
| 140 | *p_arg = (float) PyLong_AsDouble(v); |
Guido van Rossum | 76a00af | 1991-07-27 21:34:00 +0000 | [diff] [blame] | 141 | return 1; |
| 142 | } |
Guido van Rossum | 3837de0 | 1997-04-29 15:43:55 +0000 | [diff] [blame] | 143 | return PyErr_BadArgument(); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | int |
Guido van Rossum | 3837de0 | 1997-04-29 15:43:55 +0000 | [diff] [blame] | 147 | PyArg_GetFloat(args, nargs, i, p_arg) |
| 148 | register PyObject *args; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 149 | int nargs, i; |
| 150 | float *p_arg; |
| 151 | { |
Guido van Rossum | 3837de0 | 1997-04-29 15:43:55 +0000 | [diff] [blame] | 152 | PyObject *v; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 153 | float x; |
Guido van Rossum | 3837de0 | 1997-04-29 15:43:55 +0000 | [diff] [blame] | 154 | if (!PyArg_GetObject(args, nargs, i, &v)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 155 | return 0; |
| 156 | if (!extractfloat(v, &x)) |
| 157 | return 0; |
| 158 | *p_arg = x; |
| 159 | return 1; |
| 160 | } |
| 161 | |
| 162 | int |
Guido van Rossum | 3837de0 | 1997-04-29 15:43:55 +0000 | [diff] [blame] | 163 | PyArg_GetString(args, nargs, i, p_arg) |
| 164 | PyObject *args; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 165 | int nargs, i; |
| 166 | string *p_arg; |
| 167 | { |
Guido van Rossum | 3837de0 | 1997-04-29 15:43:55 +0000 | [diff] [blame] | 168 | PyObject *v; |
| 169 | if (!PyArg_GetObject(args, nargs, i, &v)) |
Guido van Rossum | 7cdf5eb | 1992-04-06 12:34:45 +0000 | [diff] [blame] | 170 | return 0; |
Guido van Rossum | 3837de0 | 1997-04-29 15:43:55 +0000 | [diff] [blame] | 171 | if (!PyString_Check(v)) { |
| 172 | return PyErr_BadArgument(); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 173 | } |
Guido van Rossum | 3837de0 | 1997-04-29 15:43:55 +0000 | [diff] [blame] | 174 | *p_arg = PyString_AsString(v); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 175 | return 1; |
| 176 | } |
| 177 | |
| 178 | int |
Guido van Rossum | 3837de0 | 1997-04-29 15:43:55 +0000 | [diff] [blame] | 179 | PyArg_GetChar(args, nargs, i, p_arg) |
| 180 | PyObject *args; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 181 | int nargs, i; |
| 182 | char *p_arg; |
| 183 | { |
| 184 | string x; |
Guido van Rossum | 3837de0 | 1997-04-29 15:43:55 +0000 | [diff] [blame] | 185 | if (!PyArg_GetString(args, nargs, i, &x)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 186 | return 0; |
| 187 | if (x[0] == '\0' || x[1] != '\0') { |
| 188 | /* Not exactly one char */ |
Guido van Rossum | 3837de0 | 1997-04-29 15:43:55 +0000 | [diff] [blame] | 189 | return PyErr_BadArgument(); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 190 | } |
| 191 | *p_arg = x[0]; |
| 192 | return 1; |
| 193 | } |
| 194 | |
| 195 | int |
Guido van Rossum | 3837de0 | 1997-04-29 15:43:55 +0000 | [diff] [blame] | 196 | PyArg_GetLongArraySize(args, nargs, i, p_arg) |
| 197 | PyObject *args; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 198 | int nargs, i; |
| 199 | long *p_arg; |
| 200 | { |
Guido van Rossum | 3837de0 | 1997-04-29 15:43:55 +0000 | [diff] [blame] | 201 | PyObject *v; |
| 202 | if (!PyArg_GetObject(args, nargs, i, &v)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 203 | return 0; |
Guido van Rossum | 3837de0 | 1997-04-29 15:43:55 +0000 | [diff] [blame] | 204 | if (PyTuple_Check(v)) { |
| 205 | *p_arg = PyTuple_Size(v); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 206 | return 1; |
| 207 | } |
Guido van Rossum | 3837de0 | 1997-04-29 15:43:55 +0000 | [diff] [blame] | 208 | if (PyList_Check(v)) { |
| 209 | *p_arg = PyList_Size(v); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 210 | return 1; |
| 211 | } |
Guido van Rossum | 3837de0 | 1997-04-29 15:43:55 +0000 | [diff] [blame] | 212 | return PyErr_BadArgument(); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 213 | } |
| 214 | |
| 215 | int |
Guido van Rossum | 3837de0 | 1997-04-29 15:43:55 +0000 | [diff] [blame] | 216 | PyArg_GetShortArraySize(args, nargs, i, p_arg) |
| 217 | PyObject *args; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 218 | int nargs, i; |
| 219 | short *p_arg; |
| 220 | { |
| 221 | long x; |
Guido van Rossum | 3837de0 | 1997-04-29 15:43:55 +0000 | [diff] [blame] | 222 | if (!PyArg_GetLongArraySize(args, nargs, i, &x)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 223 | return 0; |
Guido van Rossum | 6bf62da | 1997-04-11 20:37:35 +0000 | [diff] [blame] | 224 | *p_arg = (short) x; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 225 | return 1; |
| 226 | } |
| 227 | |
| 228 | /* XXX The following four are too similar. Should share more code. */ |
| 229 | |
| 230 | int |
Guido van Rossum | 3837de0 | 1997-04-29 15:43:55 +0000 | [diff] [blame] | 231 | PyArg_GetLongArray(args, nargs, i, n, p_arg) |
| 232 | PyObject *args; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 233 | int nargs, i; |
| 234 | int n; |
| 235 | long *p_arg; /* [n] */ |
| 236 | { |
Guido van Rossum | 3837de0 | 1997-04-29 15:43:55 +0000 | [diff] [blame] | 237 | PyObject *v, *w; |
| 238 | if (!PyArg_GetObject(args, nargs, i, &v)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 239 | return 0; |
Guido van Rossum | 3837de0 | 1997-04-29 15:43:55 +0000 | [diff] [blame] | 240 | if (PyTuple_Check(v)) { |
| 241 | if (PyTuple_Size(v) != n) { |
| 242 | return PyErr_BadArgument(); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 243 | } |
| 244 | for (i = 0; i < n; i++) { |
Guido van Rossum | 3837de0 | 1997-04-29 15:43:55 +0000 | [diff] [blame] | 245 | w = PyTuple_GetItem(v, i); |
| 246 | if (!PyInt_Check(w)) { |
| 247 | return PyErr_BadArgument(); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 248 | } |
Guido van Rossum | 3837de0 | 1997-04-29 15:43:55 +0000 | [diff] [blame] | 249 | p_arg[i] = PyInt_AsLong(w); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 250 | } |
| 251 | return 1; |
| 252 | } |
Guido van Rossum | 3837de0 | 1997-04-29 15:43:55 +0000 | [diff] [blame] | 253 | else if (PyList_Check(v)) { |
| 254 | if (PyList_Size(v) != n) { |
| 255 | return PyErr_BadArgument(); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 256 | } |
| 257 | for (i = 0; i < n; i++) { |
Guido van Rossum | 3837de0 | 1997-04-29 15:43:55 +0000 | [diff] [blame] | 258 | w = PyList_GetItem(v, i); |
| 259 | if (!PyInt_Check(w)) { |
| 260 | return PyErr_BadArgument(); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 261 | } |
Guido van Rossum | 3837de0 | 1997-04-29 15:43:55 +0000 | [diff] [blame] | 262 | p_arg[i] = PyInt_AsLong(w); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 263 | } |
| 264 | return 1; |
| 265 | } |
| 266 | else { |
Guido van Rossum | 3837de0 | 1997-04-29 15:43:55 +0000 | [diff] [blame] | 267 | return PyErr_BadArgument(); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 268 | } |
| 269 | } |
| 270 | |
| 271 | int |
Guido van Rossum | 3837de0 | 1997-04-29 15:43:55 +0000 | [diff] [blame] | 272 | PyArg_GetShortArray(args, nargs, i, n, p_arg) |
| 273 | PyObject *args; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 274 | int nargs, i; |
| 275 | int n; |
| 276 | short *p_arg; /* [n] */ |
| 277 | { |
Guido van Rossum | 3837de0 | 1997-04-29 15:43:55 +0000 | [diff] [blame] | 278 | PyObject *v, *w; |
| 279 | if (!PyArg_GetObject(args, nargs, i, &v)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 280 | return 0; |
Guido van Rossum | 3837de0 | 1997-04-29 15:43:55 +0000 | [diff] [blame] | 281 | if (PyTuple_Check(v)) { |
| 282 | if (PyTuple_Size(v) != n) { |
| 283 | return PyErr_BadArgument(); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 284 | } |
| 285 | for (i = 0; i < n; i++) { |
Guido van Rossum | 3837de0 | 1997-04-29 15:43:55 +0000 | [diff] [blame] | 286 | w = PyTuple_GetItem(v, i); |
| 287 | if (!PyInt_Check(w)) { |
| 288 | return PyErr_BadArgument(); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 289 | } |
Guido van Rossum | 3837de0 | 1997-04-29 15:43:55 +0000 | [diff] [blame] | 290 | p_arg[i] = (short) PyInt_AsLong(w); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 291 | } |
| 292 | return 1; |
| 293 | } |
Guido van Rossum | 3837de0 | 1997-04-29 15:43:55 +0000 | [diff] [blame] | 294 | else if (PyList_Check(v)) { |
| 295 | if (PyList_Size(v) != n) { |
| 296 | return PyErr_BadArgument(); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 297 | } |
| 298 | for (i = 0; i < n; i++) { |
Guido van Rossum | 3837de0 | 1997-04-29 15:43:55 +0000 | [diff] [blame] | 299 | w = PyList_GetItem(v, i); |
| 300 | if (!PyInt_Check(w)) { |
| 301 | return PyErr_BadArgument(); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 302 | } |
Guido van Rossum | 3837de0 | 1997-04-29 15:43:55 +0000 | [diff] [blame] | 303 | p_arg[i] = (short) PyInt_AsLong(w); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 304 | } |
| 305 | return 1; |
| 306 | } |
| 307 | else { |
Guido van Rossum | 3837de0 | 1997-04-29 15:43:55 +0000 | [diff] [blame] | 308 | return PyErr_BadArgument(); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 309 | } |
| 310 | } |
| 311 | |
| 312 | int |
Guido van Rossum | 3837de0 | 1997-04-29 15:43:55 +0000 | [diff] [blame] | 313 | PyArg_GetDoubleArray(args, nargs, i, n, p_arg) |
| 314 | PyObject *args; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 315 | int nargs, i; |
| 316 | int n; |
| 317 | double *p_arg; /* [n] */ |
| 318 | { |
Guido van Rossum | 3837de0 | 1997-04-29 15:43:55 +0000 | [diff] [blame] | 319 | PyObject *v, *w; |
| 320 | if (!PyArg_GetObject(args, nargs, i, &v)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 321 | return 0; |
Guido van Rossum | 3837de0 | 1997-04-29 15:43:55 +0000 | [diff] [blame] | 322 | if (PyTuple_Check(v)) { |
| 323 | if (PyTuple_Size(v) != n) { |
| 324 | return PyErr_BadArgument(); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 325 | } |
| 326 | for (i = 0; i < n; i++) { |
Guido van Rossum | 3837de0 | 1997-04-29 15:43:55 +0000 | [diff] [blame] | 327 | w = PyTuple_GetItem(v, i); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 328 | if (!extractdouble(w, &p_arg[i])) |
| 329 | return 0; |
| 330 | } |
| 331 | return 1; |
| 332 | } |
Guido van Rossum | 3837de0 | 1997-04-29 15:43:55 +0000 | [diff] [blame] | 333 | else if (PyList_Check(v)) { |
| 334 | if (PyList_Size(v) != n) { |
| 335 | return PyErr_BadArgument(); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 336 | } |
| 337 | for (i = 0; i < n; i++) { |
Guido van Rossum | 3837de0 | 1997-04-29 15:43:55 +0000 | [diff] [blame] | 338 | w = PyList_GetItem(v, i); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 339 | if (!extractdouble(w, &p_arg[i])) |
| 340 | return 0; |
| 341 | } |
| 342 | return 1; |
| 343 | } |
| 344 | else { |
Guido van Rossum | 3837de0 | 1997-04-29 15:43:55 +0000 | [diff] [blame] | 345 | return PyErr_BadArgument(); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 346 | } |
| 347 | } |
| 348 | |
| 349 | int |
Guido van Rossum | 3837de0 | 1997-04-29 15:43:55 +0000 | [diff] [blame] | 350 | PyArg_GetFloatArray(args, nargs, i, n, p_arg) |
| 351 | PyObject *args; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 352 | int nargs, i; |
| 353 | int n; |
| 354 | float *p_arg; /* [n] */ |
| 355 | { |
Guido van Rossum | 3837de0 | 1997-04-29 15:43:55 +0000 | [diff] [blame] | 356 | PyObject *v, *w; |
| 357 | if (!PyArg_GetObject(args, nargs, i, &v)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 358 | return 0; |
Guido van Rossum | 3837de0 | 1997-04-29 15:43:55 +0000 | [diff] [blame] | 359 | if (PyTuple_Check(v)) { |
| 360 | if (PyTuple_Size(v) != n) { |
| 361 | return PyErr_BadArgument(); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 362 | } |
| 363 | for (i = 0; i < n; i++) { |
Guido van Rossum | 3837de0 | 1997-04-29 15:43:55 +0000 | [diff] [blame] | 364 | w = PyTuple_GetItem(v, i); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 365 | if (!extractfloat(w, &p_arg[i])) |
| 366 | return 0; |
| 367 | } |
| 368 | return 1; |
| 369 | } |
Guido van Rossum | 3837de0 | 1997-04-29 15:43:55 +0000 | [diff] [blame] | 370 | else if (PyList_Check(v)) { |
| 371 | if (PyList_Size(v) != n) { |
| 372 | return PyErr_BadArgument(); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 373 | } |
| 374 | for (i = 0; i < n; i++) { |
Guido van Rossum | 3837de0 | 1997-04-29 15:43:55 +0000 | [diff] [blame] | 375 | w = PyList_GetItem(v, i); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 376 | if (!extractfloat(w, &p_arg[i])) |
| 377 | return 0; |
| 378 | } |
| 379 | return 1; |
| 380 | } |
| 381 | else { |
Guido van Rossum | 3837de0 | 1997-04-29 15:43:55 +0000 | [diff] [blame] | 382 | return PyErr_BadArgument(); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 383 | } |
| 384 | } |