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