2 /* Functions used by cgen output */
5 #include "cgensupport.h"
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
14 PyArg_GetObject(register PyObject
*args
, int nargs
, int i
, PyObject
**p_arg
)
17 if (args
== NULL
|| !PyTuple_Check(args
) ||
18 nargs
!= PyTuple_Size(args
) ||
19 i
< 0 || i
>= nargs
) {
20 return PyErr_BadArgument();
23 args
= PyTuple_GetItem(args
, i
);
27 return PyErr_BadArgument();
34 PyArg_GetLong(register PyObject
*args
, int nargs
, int i
, long *p_arg
)
37 if (args
== NULL
|| !PyTuple_Check(args
) ||
38 nargs
!= PyTuple_Size(args
) ||
39 i
< 0 || i
>= nargs
) {
40 return PyErr_BadArgument();
42 args
= PyTuple_GetItem(args
, i
);
44 if (args
== NULL
|| !PyInt_Check(args
)) {
45 return PyErr_BadArgument();
47 *p_arg
= PyInt_AsLong(args
);
52 PyArg_GetShort(register PyObject
*args
, int nargs
, int i
, short *p_arg
)
55 if (!PyArg_GetLong(args
, nargs
, i
, &x
))
62 extractdouble(register PyObject
*v
, double *p_arg
)
65 /* Fall through to error return at end of function */
67 else if (PyFloat_Check(v
)) {
68 *p_arg
= PyFloat_AS_DOUBLE((PyFloatObject
*)v
);
71 else if (PyInt_Check(v
)) {
72 *p_arg
= PyInt_AS_LONG((PyIntObject
*)v
);
75 else if (PyLong_Check(v
)) {
76 *p_arg
= PyLong_AsDouble(v
);
79 return PyErr_BadArgument();
83 extractfloat(register PyObject
*v
, float *p_arg
)
86 /* Fall through to error return at end of function */
88 else if (PyFloat_Check(v
)) {
89 *p_arg
= (float) PyFloat_AS_DOUBLE((PyFloatObject
*)v
);
92 else if (PyInt_Check(v
)) {
93 *p_arg
= (float) PyInt_AS_LONG((PyIntObject
*)v
);
96 else if (PyLong_Check(v
)) {
97 *p_arg
= (float) PyLong_AsDouble(v
);
100 return PyErr_BadArgument();
104 PyArg_GetFloat(register PyObject
*args
, int nargs
, int i
, float *p_arg
)
108 if (!PyArg_GetObject(args
, nargs
, i
, &v
))
110 if (!extractfloat(v
, &x
))
117 PyArg_GetString(PyObject
*args
, int nargs
, int i
, string
*p_arg
)
120 if (!PyArg_GetObject(args
, nargs
, i
, &v
))
122 if (!PyString_Check(v
)) {
123 return PyErr_BadArgument();
125 *p_arg
= PyString_AsString(v
);
130 PyArg_GetChar(PyObject
*args
, int nargs
, int i
, char *p_arg
)
133 if (!PyArg_GetString(args
, nargs
, i
, &x
))
135 if (x
[0] == '\0' || x
[1] != '\0') {
136 /* Not exactly one char */
137 return PyErr_BadArgument();
144 PyArg_GetLongArraySize(PyObject
*args
, int nargs
, int i
, long *p_arg
)
147 if (!PyArg_GetObject(args
, nargs
, i
, &v
))
149 if (PyTuple_Check(v
)) {
150 *p_arg
= PyTuple_Size(v
);
153 if (PyList_Check(v
)) {
154 *p_arg
= PyList_Size(v
);
157 return PyErr_BadArgument();
161 PyArg_GetShortArraySize(PyObject
*args
, int nargs
, int i
, short *p_arg
)
164 if (!PyArg_GetLongArraySize(args
, nargs
, i
, &x
))
170 /* XXX The following four are too similar. Should share more code. */
173 PyArg_GetLongArray(PyObject
*args
, int nargs
, int i
, int n
, long *p_arg
)
176 if (!PyArg_GetObject(args
, nargs
, i
, &v
))
178 if (PyTuple_Check(v
)) {
179 if (PyTuple_Size(v
) != n
) {
180 return PyErr_BadArgument();
182 for (i
= 0; i
< n
; i
++) {
183 w
= PyTuple_GetItem(v
, i
);
184 if (!PyInt_Check(w
)) {
185 return PyErr_BadArgument();
187 p_arg
[i
] = PyInt_AsLong(w
);
191 else if (PyList_Check(v
)) {
192 if (PyList_Size(v
) != n
) {
193 return PyErr_BadArgument();
195 for (i
= 0; i
< n
; i
++) {
196 w
= PyList_GetItem(v
, i
);
197 if (!PyInt_Check(w
)) {
198 return PyErr_BadArgument();
200 p_arg
[i
] = PyInt_AsLong(w
);
205 return PyErr_BadArgument();
210 PyArg_GetShortArray(PyObject
*args
, int nargs
, int i
, int n
, short *p_arg
)
213 if (!PyArg_GetObject(args
, nargs
, i
, &v
))
215 if (PyTuple_Check(v
)) {
216 if (PyTuple_Size(v
) != n
) {
217 return PyErr_BadArgument();
219 for (i
= 0; i
< n
; i
++) {
220 w
= PyTuple_GetItem(v
, i
);
221 if (!PyInt_Check(w
)) {
222 return PyErr_BadArgument();
224 p_arg
[i
] = (short) PyInt_AsLong(w
);
228 else if (PyList_Check(v
)) {
229 if (PyList_Size(v
) != n
) {
230 return PyErr_BadArgument();
232 for (i
= 0; i
< n
; i
++) {
233 w
= PyList_GetItem(v
, i
);
234 if (!PyInt_Check(w
)) {
235 return PyErr_BadArgument();
237 p_arg
[i
] = (short) PyInt_AsLong(w
);
242 return PyErr_BadArgument();
247 PyArg_GetDoubleArray(PyObject
*args
, int nargs
, int i
, int n
, double *p_arg
)
250 if (!PyArg_GetObject(args
, nargs
, i
, &v
))
252 if (PyTuple_Check(v
)) {
253 if (PyTuple_Size(v
) != n
) {
254 return PyErr_BadArgument();
256 for (i
= 0; i
< n
; i
++) {
257 w
= PyTuple_GetItem(v
, i
);
258 if (!extractdouble(w
, &p_arg
[i
]))
263 else if (PyList_Check(v
)) {
264 if (PyList_Size(v
) != n
) {
265 return PyErr_BadArgument();
267 for (i
= 0; i
< n
; i
++) {
268 w
= PyList_GetItem(v
, i
);
269 if (!extractdouble(w
, &p_arg
[i
]))
275 return PyErr_BadArgument();
280 PyArg_GetFloatArray(PyObject
*args
, int nargs
, int i
, int n
, float *p_arg
)
283 if (!PyArg_GetObject(args
, nargs
, i
, &v
))
285 if (PyTuple_Check(v
)) {
286 if (PyTuple_Size(v
) != n
) {
287 return PyErr_BadArgument();
289 for (i
= 0; i
< n
; i
++) {
290 w
= PyTuple_GetItem(v
, i
);
291 if (!extractfloat(w
, &p_arg
[i
]))
296 else if (PyList_Check(v
)) {
297 if (PyList_Size(v
) != n
) {
298 return PyErr_BadArgument();
300 for (i
= 0; i
< n
; i
++) {
301 w
= PyList_GetItem(v
, i
);
302 if (!extractfloat(w
, &p_arg
[i
]))
308 return PyErr_BadArgument();