Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 1 | /*********************************************************** |
Guido van Rossum | fd71b9e | 2000-06-30 23:50:40 +0000 | [diff] [blame] | 2 | Copyright (c) 2000, BeOpen.com. |
| 3 | Copyright (c) 1995-2000, Corporation for National Research Initiatives. |
| 4 | Copyright (c) 1990-1995, Stichting Mathematisch Centrum. |
| 5 | All rights reserved. |
Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 6 | |
Guido van Rossum | fd71b9e | 2000-06-30 23:50:40 +0000 | [diff] [blame] | 7 | See the file "Misc/COPYRIGHT" for information on usage and |
| 8 | redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES. |
Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 9 | ******************************************************************/ |
| 10 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 11 | /* Functions used by cgen output */ |
| 12 | |
Guido van Rossum | 3837de0 | 1997-04-29 15:43:55 +0000 | [diff] [blame] | 13 | #include "Python.h" |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 14 | #include "cgensupport.h" |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 15 | |
| 16 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 17 | /* Functions to extract arguments. |
| 18 | These needs to know the total number of arguments supplied, |
| 19 | since the argument list is a tuple only of there is more than |
| 20 | one argument. */ |
| 21 | |
| 22 | int |
Peter Schneider-Kamp | 6a85027 | 2000-07-10 17:04:33 +0000 | [diff] [blame] | 23 | PyArg_GetObject(register PyObject *args, int nargs, int i, PyObject **p_arg) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 24 | { |
| 25 | if (nargs != 1) { |
Guido van Rossum | 3837de0 | 1997-04-29 15:43:55 +0000 | [diff] [blame] | 26 | if (args == NULL || !PyTuple_Check(args) || |
| 27 | nargs != PyTuple_Size(args) || |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 28 | i < 0 || i >= nargs) { |
Guido van Rossum | 3837de0 | 1997-04-29 15:43:55 +0000 | [diff] [blame] | 29 | return PyErr_BadArgument(); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 30 | } |
| 31 | else { |
Guido van Rossum | 3837de0 | 1997-04-29 15:43:55 +0000 | [diff] [blame] | 32 | args = PyTuple_GetItem(args, i); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 33 | } |
| 34 | } |
| 35 | if (args == NULL) { |
Guido van Rossum | 3837de0 | 1997-04-29 15:43:55 +0000 | [diff] [blame] | 36 | return PyErr_BadArgument(); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 37 | } |
| 38 | *p_arg = args; |
| 39 | return 1; |
| 40 | } |
| 41 | |
| 42 | int |
Peter Schneider-Kamp | 6a85027 | 2000-07-10 17:04:33 +0000 | [diff] [blame] | 43 | PyArg_GetLong(register PyObject *args, int nargs, int i, long *p_arg) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 44 | { |
| 45 | if (nargs != 1) { |
Guido van Rossum | 3837de0 | 1997-04-29 15:43:55 +0000 | [diff] [blame] | 46 | if (args == NULL || !PyTuple_Check(args) || |
| 47 | nargs != PyTuple_Size(args) || |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 48 | i < 0 || i >= nargs) { |
Guido van Rossum | 3837de0 | 1997-04-29 15:43:55 +0000 | [diff] [blame] | 49 | return PyErr_BadArgument(); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 50 | } |
Guido van Rossum | 3837de0 | 1997-04-29 15:43:55 +0000 | [diff] [blame] | 51 | args = PyTuple_GetItem(args, i); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 52 | } |
Guido van Rossum | 3837de0 | 1997-04-29 15:43:55 +0000 | [diff] [blame] | 53 | if (args == NULL || !PyInt_Check(args)) { |
| 54 | return PyErr_BadArgument(); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 55 | } |
Guido van Rossum | 3837de0 | 1997-04-29 15:43:55 +0000 | [diff] [blame] | 56 | *p_arg = PyInt_AsLong(args); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 57 | return 1; |
| 58 | } |
| 59 | |
| 60 | int |
Peter Schneider-Kamp | 6a85027 | 2000-07-10 17:04:33 +0000 | [diff] [blame] | 61 | PyArg_GetShort(register PyObject *args, int nargs, int i, short *p_arg) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 62 | { |
| 63 | long x; |
Guido van Rossum | 3837de0 | 1997-04-29 15:43:55 +0000 | [diff] [blame] | 64 | if (!PyArg_GetLong(args, nargs, i, &x)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 65 | return 0; |
Guido van Rossum | 6bf62da | 1997-04-11 20:37:35 +0000 | [diff] [blame] | 66 | *p_arg = (short) x; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 67 | return 1; |
| 68 | } |
| 69 | |
| 70 | static int |
Peter Schneider-Kamp | 6a85027 | 2000-07-10 17:04:33 +0000 | [diff] [blame] | 71 | extractdouble(register PyObject *v, double *p_arg) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 72 | { |
| 73 | if (v == NULL) { |
| 74 | /* Fall through to error return at end of function */ |
| 75 | } |
Guido van Rossum | 3837de0 | 1997-04-29 15:43:55 +0000 | [diff] [blame] | 76 | else if (PyFloat_Check(v)) { |
| 77 | *p_arg = PyFloat_AS_DOUBLE((PyFloatObject *)v); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 78 | return 1; |
| 79 | } |
Guido van Rossum | 3837de0 | 1997-04-29 15:43:55 +0000 | [diff] [blame] | 80 | else if (PyInt_Check(v)) { |
| 81 | *p_arg = PyInt_AS_LONG((PyIntObject *)v); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 82 | return 1; |
| 83 | } |
Guido van Rossum | 3837de0 | 1997-04-29 15:43:55 +0000 | [diff] [blame] | 84 | else if (PyLong_Check(v)) { |
| 85 | *p_arg = PyLong_AsDouble(v); |
Guido van Rossum | 76a00af | 1991-07-27 21:34:00 +0000 | [diff] [blame] | 86 | return 1; |
| 87 | } |
Guido van Rossum | 3837de0 | 1997-04-29 15:43:55 +0000 | [diff] [blame] | 88 | return PyErr_BadArgument(); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | static int |
Peter Schneider-Kamp | 6a85027 | 2000-07-10 17:04:33 +0000 | [diff] [blame] | 92 | extractfloat(register PyObject *v, float *p_arg) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 93 | { |
| 94 | if (v == NULL) { |
| 95 | /* Fall through to error return at end of function */ |
| 96 | } |
Guido van Rossum | 3837de0 | 1997-04-29 15:43:55 +0000 | [diff] [blame] | 97 | else if (PyFloat_Check(v)) { |
| 98 | *p_arg = (float) PyFloat_AS_DOUBLE((PyFloatObject *)v); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 99 | return 1; |
| 100 | } |
Guido van Rossum | 3837de0 | 1997-04-29 15:43:55 +0000 | [diff] [blame] | 101 | else if (PyInt_Check(v)) { |
| 102 | *p_arg = (float) PyInt_AS_LONG((PyIntObject *)v); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 103 | return 1; |
| 104 | } |
Guido van Rossum | 3837de0 | 1997-04-29 15:43:55 +0000 | [diff] [blame] | 105 | else if (PyLong_Check(v)) { |
| 106 | *p_arg = (float) PyLong_AsDouble(v); |
Guido van Rossum | 76a00af | 1991-07-27 21:34:00 +0000 | [diff] [blame] | 107 | return 1; |
| 108 | } |
Guido van Rossum | 3837de0 | 1997-04-29 15:43:55 +0000 | [diff] [blame] | 109 | return PyErr_BadArgument(); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | int |
Peter Schneider-Kamp | 6a85027 | 2000-07-10 17:04:33 +0000 | [diff] [blame] | 113 | PyArg_GetFloat(register PyObject *args, int nargs, int i, float *p_arg) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 114 | { |
Guido van Rossum | 3837de0 | 1997-04-29 15:43:55 +0000 | [diff] [blame] | 115 | PyObject *v; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 116 | float x; |
Guido van Rossum | 3837de0 | 1997-04-29 15:43:55 +0000 | [diff] [blame] | 117 | if (!PyArg_GetObject(args, nargs, i, &v)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 118 | return 0; |
| 119 | if (!extractfloat(v, &x)) |
| 120 | return 0; |
| 121 | *p_arg = x; |
| 122 | return 1; |
| 123 | } |
| 124 | |
| 125 | int |
Peter Schneider-Kamp | 6a85027 | 2000-07-10 17:04:33 +0000 | [diff] [blame] | 126 | PyArg_GetString(PyObject *args, int nargs, int i, string *p_arg) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 127 | { |
Guido van Rossum | 3837de0 | 1997-04-29 15:43:55 +0000 | [diff] [blame] | 128 | PyObject *v; |
| 129 | if (!PyArg_GetObject(args, nargs, i, &v)) |
Guido van Rossum | 7cdf5eb | 1992-04-06 12:34:45 +0000 | [diff] [blame] | 130 | return 0; |
Guido van Rossum | 3837de0 | 1997-04-29 15:43:55 +0000 | [diff] [blame] | 131 | if (!PyString_Check(v)) { |
| 132 | return PyErr_BadArgument(); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 133 | } |
Guido van Rossum | 3837de0 | 1997-04-29 15:43:55 +0000 | [diff] [blame] | 134 | *p_arg = PyString_AsString(v); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 135 | return 1; |
| 136 | } |
| 137 | |
| 138 | int |
Peter Schneider-Kamp | 6a85027 | 2000-07-10 17:04:33 +0000 | [diff] [blame] | 139 | PyArg_GetChar(PyObject *args, int nargs, int i, char *p_arg) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 140 | { |
| 141 | string x; |
Guido van Rossum | 3837de0 | 1997-04-29 15:43:55 +0000 | [diff] [blame] | 142 | if (!PyArg_GetString(args, nargs, i, &x)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 143 | return 0; |
| 144 | if (x[0] == '\0' || x[1] != '\0') { |
| 145 | /* Not exactly one char */ |
Guido van Rossum | 3837de0 | 1997-04-29 15:43:55 +0000 | [diff] [blame] | 146 | return PyErr_BadArgument(); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 147 | } |
| 148 | *p_arg = x[0]; |
| 149 | return 1; |
| 150 | } |
| 151 | |
| 152 | int |
Peter Schneider-Kamp | 6a85027 | 2000-07-10 17:04:33 +0000 | [diff] [blame] | 153 | PyArg_GetLongArraySize(PyObject *args, int nargs, int i, long *p_arg) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 154 | { |
Guido van Rossum | 3837de0 | 1997-04-29 15:43:55 +0000 | [diff] [blame] | 155 | PyObject *v; |
| 156 | if (!PyArg_GetObject(args, nargs, i, &v)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 157 | return 0; |
Guido van Rossum | 3837de0 | 1997-04-29 15:43:55 +0000 | [diff] [blame] | 158 | if (PyTuple_Check(v)) { |
| 159 | *p_arg = PyTuple_Size(v); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 160 | return 1; |
| 161 | } |
Guido van Rossum | 3837de0 | 1997-04-29 15:43:55 +0000 | [diff] [blame] | 162 | if (PyList_Check(v)) { |
| 163 | *p_arg = PyList_Size(v); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 164 | return 1; |
| 165 | } |
Guido van Rossum | 3837de0 | 1997-04-29 15:43:55 +0000 | [diff] [blame] | 166 | return PyErr_BadArgument(); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 167 | } |
| 168 | |
| 169 | int |
Peter Schneider-Kamp | 6a85027 | 2000-07-10 17:04:33 +0000 | [diff] [blame] | 170 | PyArg_GetShortArraySize(PyObject *args, int nargs, int i, short *p_arg) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 171 | { |
| 172 | long x; |
Guido van Rossum | 3837de0 | 1997-04-29 15:43:55 +0000 | [diff] [blame] | 173 | if (!PyArg_GetLongArraySize(args, nargs, i, &x)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 174 | return 0; |
Guido van Rossum | 6bf62da | 1997-04-11 20:37:35 +0000 | [diff] [blame] | 175 | *p_arg = (short) x; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 176 | return 1; |
| 177 | } |
| 178 | |
| 179 | /* XXX The following four are too similar. Should share more code. */ |
| 180 | |
| 181 | int |
Peter Schneider-Kamp | 6a85027 | 2000-07-10 17:04:33 +0000 | [diff] [blame] | 182 | PyArg_GetLongArray(PyObject *args, int nargs, int i, int n, long *p_arg) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 183 | { |
Guido van Rossum | 3837de0 | 1997-04-29 15:43:55 +0000 | [diff] [blame] | 184 | PyObject *v, *w; |
| 185 | if (!PyArg_GetObject(args, nargs, i, &v)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 186 | return 0; |
Guido van Rossum | 3837de0 | 1997-04-29 15:43:55 +0000 | [diff] [blame] | 187 | if (PyTuple_Check(v)) { |
| 188 | if (PyTuple_Size(v) != n) { |
| 189 | return PyErr_BadArgument(); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 190 | } |
| 191 | for (i = 0; i < n; i++) { |
Guido van Rossum | 3837de0 | 1997-04-29 15:43:55 +0000 | [diff] [blame] | 192 | w = PyTuple_GetItem(v, i); |
| 193 | if (!PyInt_Check(w)) { |
| 194 | return PyErr_BadArgument(); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 195 | } |
Guido van Rossum | 3837de0 | 1997-04-29 15:43:55 +0000 | [diff] [blame] | 196 | p_arg[i] = PyInt_AsLong(w); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 197 | } |
| 198 | return 1; |
| 199 | } |
Guido van Rossum | 3837de0 | 1997-04-29 15:43:55 +0000 | [diff] [blame] | 200 | else if (PyList_Check(v)) { |
| 201 | if (PyList_Size(v) != n) { |
| 202 | return PyErr_BadArgument(); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 203 | } |
| 204 | for (i = 0; i < n; i++) { |
Guido van Rossum | 3837de0 | 1997-04-29 15:43:55 +0000 | [diff] [blame] | 205 | w = PyList_GetItem(v, i); |
| 206 | if (!PyInt_Check(w)) { |
| 207 | return PyErr_BadArgument(); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 208 | } |
Guido van Rossum | 3837de0 | 1997-04-29 15:43:55 +0000 | [diff] [blame] | 209 | p_arg[i] = PyInt_AsLong(w); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 210 | } |
| 211 | return 1; |
| 212 | } |
| 213 | else { |
Guido van Rossum | 3837de0 | 1997-04-29 15:43:55 +0000 | [diff] [blame] | 214 | return PyErr_BadArgument(); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 215 | } |
| 216 | } |
| 217 | |
| 218 | int |
Peter Schneider-Kamp | 6a85027 | 2000-07-10 17:04:33 +0000 | [diff] [blame] | 219 | PyArg_GetShortArray(PyObject *args, int nargs, int i, int n, short *p_arg) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 220 | { |
Guido van Rossum | 3837de0 | 1997-04-29 15:43:55 +0000 | [diff] [blame] | 221 | PyObject *v, *w; |
| 222 | if (!PyArg_GetObject(args, nargs, i, &v)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 223 | return 0; |
Guido van Rossum | 3837de0 | 1997-04-29 15:43:55 +0000 | [diff] [blame] | 224 | if (PyTuple_Check(v)) { |
| 225 | if (PyTuple_Size(v) != n) { |
| 226 | return PyErr_BadArgument(); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 227 | } |
| 228 | for (i = 0; i < n; i++) { |
Guido van Rossum | 3837de0 | 1997-04-29 15:43:55 +0000 | [diff] [blame] | 229 | w = PyTuple_GetItem(v, i); |
| 230 | if (!PyInt_Check(w)) { |
| 231 | return PyErr_BadArgument(); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 232 | } |
Guido van Rossum | 3837de0 | 1997-04-29 15:43:55 +0000 | [diff] [blame] | 233 | p_arg[i] = (short) PyInt_AsLong(w); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 234 | } |
| 235 | return 1; |
| 236 | } |
Guido van Rossum | 3837de0 | 1997-04-29 15:43:55 +0000 | [diff] [blame] | 237 | else if (PyList_Check(v)) { |
| 238 | if (PyList_Size(v) != n) { |
| 239 | return PyErr_BadArgument(); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 240 | } |
| 241 | for (i = 0; i < n; i++) { |
Guido van Rossum | 3837de0 | 1997-04-29 15:43:55 +0000 | [diff] [blame] | 242 | w = PyList_GetItem(v, i); |
| 243 | if (!PyInt_Check(w)) { |
| 244 | return PyErr_BadArgument(); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 245 | } |
Guido van Rossum | 3837de0 | 1997-04-29 15:43:55 +0000 | [diff] [blame] | 246 | p_arg[i] = (short) PyInt_AsLong(w); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 247 | } |
| 248 | return 1; |
| 249 | } |
| 250 | else { |
Guido van Rossum | 3837de0 | 1997-04-29 15:43:55 +0000 | [diff] [blame] | 251 | return PyErr_BadArgument(); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 252 | } |
| 253 | } |
| 254 | |
| 255 | int |
Peter Schneider-Kamp | 6a85027 | 2000-07-10 17:04:33 +0000 | [diff] [blame] | 256 | PyArg_GetDoubleArray(PyObject *args, int nargs, int i, int n, double *p_arg) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 257 | { |
Guido van Rossum | 3837de0 | 1997-04-29 15:43:55 +0000 | [diff] [blame] | 258 | PyObject *v, *w; |
| 259 | if (!PyArg_GetObject(args, nargs, i, &v)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 260 | return 0; |
Guido van Rossum | 3837de0 | 1997-04-29 15:43:55 +0000 | [diff] [blame] | 261 | if (PyTuple_Check(v)) { |
| 262 | if (PyTuple_Size(v) != n) { |
| 263 | return PyErr_BadArgument(); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 264 | } |
| 265 | for (i = 0; i < n; i++) { |
Guido van Rossum | 3837de0 | 1997-04-29 15:43:55 +0000 | [diff] [blame] | 266 | w = PyTuple_GetItem(v, i); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 267 | if (!extractdouble(w, &p_arg[i])) |
| 268 | return 0; |
| 269 | } |
| 270 | return 1; |
| 271 | } |
Guido van Rossum | 3837de0 | 1997-04-29 15:43:55 +0000 | [diff] [blame] | 272 | else if (PyList_Check(v)) { |
| 273 | if (PyList_Size(v) != n) { |
| 274 | return PyErr_BadArgument(); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 275 | } |
| 276 | for (i = 0; i < n; i++) { |
Guido van Rossum | 3837de0 | 1997-04-29 15:43:55 +0000 | [diff] [blame] | 277 | w = PyList_GetItem(v, i); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 278 | if (!extractdouble(w, &p_arg[i])) |
| 279 | return 0; |
| 280 | } |
| 281 | return 1; |
| 282 | } |
| 283 | else { |
Guido van Rossum | 3837de0 | 1997-04-29 15:43:55 +0000 | [diff] [blame] | 284 | return PyErr_BadArgument(); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 285 | } |
| 286 | } |
| 287 | |
| 288 | int |
Peter Schneider-Kamp | 6a85027 | 2000-07-10 17:04:33 +0000 | [diff] [blame] | 289 | PyArg_GetFloatArray(PyObject *args, int nargs, int i, int n, float *p_arg) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 290 | { |
Guido van Rossum | 3837de0 | 1997-04-29 15:43:55 +0000 | [diff] [blame] | 291 | PyObject *v, *w; |
| 292 | if (!PyArg_GetObject(args, nargs, i, &v)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 293 | return 0; |
Guido van Rossum | 3837de0 | 1997-04-29 15:43:55 +0000 | [diff] [blame] | 294 | if (PyTuple_Check(v)) { |
| 295 | if (PyTuple_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 = PyTuple_GetItem(v, i); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 300 | if (!extractfloat(w, &p_arg[i])) |
| 301 | return 0; |
| 302 | } |
| 303 | return 1; |
| 304 | } |
Guido van Rossum | 3837de0 | 1997-04-29 15:43:55 +0000 | [diff] [blame] | 305 | else if (PyList_Check(v)) { |
| 306 | if (PyList_Size(v) != n) { |
| 307 | return PyErr_BadArgument(); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 308 | } |
| 309 | for (i = 0; i < n; i++) { |
Guido van Rossum | 3837de0 | 1997-04-29 15:43:55 +0000 | [diff] [blame] | 310 | w = PyList_GetItem(v, i); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 311 | if (!extractfloat(w, &p_arg[i])) |
| 312 | return 0; |
| 313 | } |
| 314 | return 1; |
| 315 | } |
| 316 | else { |
Guido van Rossum | 3837de0 | 1997-04-29 15:43:55 +0000 | [diff] [blame] | 317 | return PyErr_BadArgument(); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 318 | } |
| 319 | } |