blob: 7e7d0ff9f4c6356ffacafaae27901903de37745a [file] [log] [blame]
Guido van Rossumf70e43a1991-02-19 12:39:46 +00001
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002/* Functions used by cgen output */
3
Guido van Rossum3837de01997-04-29 15:43:55 +00004#include "Python.h"
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00005#include "cgensupport.h"
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00006
7
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00008/* 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
13int
Peter Schneider-Kamp6a850272000-07-10 17:04:33 +000014PyArg_GetObject(register PyObject *args, int nargs, int i, PyObject **p_arg)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000015{
16 if (nargs != 1) {
Guido van Rossum3837de01997-04-29 15:43:55 +000017 if (args == NULL || !PyTuple_Check(args) ||
18 nargs != PyTuple_Size(args) ||
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000019 i < 0 || i >= nargs) {
Guido van Rossum3837de01997-04-29 15:43:55 +000020 return PyErr_BadArgument();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000021 }
22 else {
Guido van Rossum3837de01997-04-29 15:43:55 +000023 args = PyTuple_GetItem(args, i);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000024 }
25 }
26 if (args == NULL) {
Guido van Rossum3837de01997-04-29 15:43:55 +000027 return PyErr_BadArgument();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000028 }
29 *p_arg = args;
30 return 1;
31}
32
33int
Peter Schneider-Kamp6a850272000-07-10 17:04:33 +000034PyArg_GetLong(register PyObject *args, int nargs, int i, long *p_arg)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000035{
36 if (nargs != 1) {
Guido van Rossum3837de01997-04-29 15:43:55 +000037 if (args == NULL || !PyTuple_Check(args) ||
38 nargs != PyTuple_Size(args) ||
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000039 i < 0 || i >= nargs) {
Guido van Rossum3837de01997-04-29 15:43:55 +000040 return PyErr_BadArgument();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000041 }
Guido van Rossum3837de01997-04-29 15:43:55 +000042 args = PyTuple_GetItem(args, i);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000043 }
Guido van Rossum3837de01997-04-29 15:43:55 +000044 if (args == NULL || !PyInt_Check(args)) {
45 return PyErr_BadArgument();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000046 }
Guido van Rossum3837de01997-04-29 15:43:55 +000047 *p_arg = PyInt_AsLong(args);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000048 return 1;
49}
50
51int
Peter Schneider-Kamp6a850272000-07-10 17:04:33 +000052PyArg_GetShort(register PyObject *args, int nargs, int i, short *p_arg)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000053{
54 long x;
Guido van Rossum3837de01997-04-29 15:43:55 +000055 if (!PyArg_GetLong(args, nargs, i, &x))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000056 return 0;
Guido van Rossum6bf62da1997-04-11 20:37:35 +000057 *p_arg = (short) x;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000058 return 1;
59}
60
61static int
Peter Schneider-Kamp6a850272000-07-10 17:04:33 +000062extractdouble(register PyObject *v, double *p_arg)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000063{
64 if (v == NULL) {
65 /* Fall through to error return at end of function */
66 }
Guido van Rossum3837de01997-04-29 15:43:55 +000067 else if (PyFloat_Check(v)) {
68 *p_arg = PyFloat_AS_DOUBLE((PyFloatObject *)v);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000069 return 1;
70 }
Guido van Rossum3837de01997-04-29 15:43:55 +000071 else if (PyInt_Check(v)) {
72 *p_arg = PyInt_AS_LONG((PyIntObject *)v);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000073 return 1;
74 }
Guido van Rossum3837de01997-04-29 15:43:55 +000075 else if (PyLong_Check(v)) {
76 *p_arg = PyLong_AsDouble(v);
Guido van Rossum76a00af1991-07-27 21:34:00 +000077 return 1;
78 }
Guido van Rossum3837de01997-04-29 15:43:55 +000079 return PyErr_BadArgument();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000080}
81
82static int
Peter Schneider-Kamp6a850272000-07-10 17:04:33 +000083extractfloat(register PyObject *v, float *p_arg)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000084{
85 if (v == NULL) {
86 /* Fall through to error return at end of function */
87 }
Guido van Rossum3837de01997-04-29 15:43:55 +000088 else if (PyFloat_Check(v)) {
89 *p_arg = (float) PyFloat_AS_DOUBLE((PyFloatObject *)v);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000090 return 1;
91 }
Guido van Rossum3837de01997-04-29 15:43:55 +000092 else if (PyInt_Check(v)) {
93 *p_arg = (float) PyInt_AS_LONG((PyIntObject *)v);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000094 return 1;
95 }
Guido van Rossum3837de01997-04-29 15:43:55 +000096 else if (PyLong_Check(v)) {
97 *p_arg = (float) PyLong_AsDouble(v);
Guido van Rossum76a00af1991-07-27 21:34:00 +000098 return 1;
99 }
Guido van Rossum3837de01997-04-29 15:43:55 +0000100 return PyErr_BadArgument();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000101}
102
103int
Peter Schneider-Kamp6a850272000-07-10 17:04:33 +0000104PyArg_GetFloat(register PyObject *args, int nargs, int i, float *p_arg)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000105{
Guido van Rossum3837de01997-04-29 15:43:55 +0000106 PyObject *v;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000107 float x;
Guido van Rossum3837de01997-04-29 15:43:55 +0000108 if (!PyArg_GetObject(args, nargs, i, &v))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000109 return 0;
110 if (!extractfloat(v, &x))
111 return 0;
112 *p_arg = x;
113 return 1;
114}
115
116int
Peter Schneider-Kamp6a850272000-07-10 17:04:33 +0000117PyArg_GetString(PyObject *args, int nargs, int i, string *p_arg)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000118{
Guido van Rossum3837de01997-04-29 15:43:55 +0000119 PyObject *v;
120 if (!PyArg_GetObject(args, nargs, i, &v))
Guido van Rossum7cdf5eb1992-04-06 12:34:45 +0000121 return 0;
Guido van Rossum3837de01997-04-29 15:43:55 +0000122 if (!PyString_Check(v)) {
123 return PyErr_BadArgument();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000124 }
Guido van Rossum3837de01997-04-29 15:43:55 +0000125 *p_arg = PyString_AsString(v);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000126 return 1;
127}
128
129int
Peter Schneider-Kamp6a850272000-07-10 17:04:33 +0000130PyArg_GetChar(PyObject *args, int nargs, int i, char *p_arg)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000131{
132 string x;
Guido van Rossum3837de01997-04-29 15:43:55 +0000133 if (!PyArg_GetString(args, nargs, i, &x))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000134 return 0;
135 if (x[0] == '\0' || x[1] != '\0') {
136 /* Not exactly one char */
Guido van Rossum3837de01997-04-29 15:43:55 +0000137 return PyErr_BadArgument();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000138 }
139 *p_arg = x[0];
140 return 1;
141}
142
143int
Peter Schneider-Kamp6a850272000-07-10 17:04:33 +0000144PyArg_GetLongArraySize(PyObject *args, int nargs, int i, long *p_arg)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000145{
Guido van Rossum3837de01997-04-29 15:43:55 +0000146 PyObject *v;
147 if (!PyArg_GetObject(args, nargs, i, &v))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000148 return 0;
Guido van Rossum3837de01997-04-29 15:43:55 +0000149 if (PyTuple_Check(v)) {
150 *p_arg = PyTuple_Size(v);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000151 return 1;
152 }
Guido van Rossum3837de01997-04-29 15:43:55 +0000153 if (PyList_Check(v)) {
154 *p_arg = PyList_Size(v);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000155 return 1;
156 }
Guido van Rossum3837de01997-04-29 15:43:55 +0000157 return PyErr_BadArgument();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000158}
159
160int
Peter Schneider-Kamp6a850272000-07-10 17:04:33 +0000161PyArg_GetShortArraySize(PyObject *args, int nargs, int i, short *p_arg)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000162{
163 long x;
Guido van Rossum3837de01997-04-29 15:43:55 +0000164 if (!PyArg_GetLongArraySize(args, nargs, i, &x))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000165 return 0;
Guido van Rossum6bf62da1997-04-11 20:37:35 +0000166 *p_arg = (short) x;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000167 return 1;
168}
169
170/* XXX The following four are too similar. Should share more code. */
171
172int
Peter Schneider-Kamp6a850272000-07-10 17:04:33 +0000173PyArg_GetLongArray(PyObject *args, int nargs, int i, int n, long *p_arg)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000174{
Guido van Rossum3837de01997-04-29 15:43:55 +0000175 PyObject *v, *w;
176 if (!PyArg_GetObject(args, nargs, i, &v))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000177 return 0;
Guido van Rossum3837de01997-04-29 15:43:55 +0000178 if (PyTuple_Check(v)) {
179 if (PyTuple_Size(v) != n) {
180 return PyErr_BadArgument();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000181 }
182 for (i = 0; i < n; i++) {
Guido van Rossum3837de01997-04-29 15:43:55 +0000183 w = PyTuple_GetItem(v, i);
184 if (!PyInt_Check(w)) {
185 return PyErr_BadArgument();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000186 }
Guido van Rossum3837de01997-04-29 15:43:55 +0000187 p_arg[i] = PyInt_AsLong(w);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000188 }
189 return 1;
190 }
Guido van Rossum3837de01997-04-29 15:43:55 +0000191 else if (PyList_Check(v)) {
192 if (PyList_Size(v) != n) {
193 return PyErr_BadArgument();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000194 }
195 for (i = 0; i < n; i++) {
Guido van Rossum3837de01997-04-29 15:43:55 +0000196 w = PyList_GetItem(v, i);
197 if (!PyInt_Check(w)) {
198 return PyErr_BadArgument();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000199 }
Guido van Rossum3837de01997-04-29 15:43:55 +0000200 p_arg[i] = PyInt_AsLong(w);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000201 }
202 return 1;
203 }
204 else {
Guido van Rossum3837de01997-04-29 15:43:55 +0000205 return PyErr_BadArgument();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000206 }
207}
208
209int
Peter Schneider-Kamp6a850272000-07-10 17:04:33 +0000210PyArg_GetShortArray(PyObject *args, int nargs, int i, int n, short *p_arg)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000211{
Guido van Rossum3837de01997-04-29 15:43:55 +0000212 PyObject *v, *w;
213 if (!PyArg_GetObject(args, nargs, i, &v))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000214 return 0;
Guido van Rossum3837de01997-04-29 15:43:55 +0000215 if (PyTuple_Check(v)) {
216 if (PyTuple_Size(v) != n) {
217 return PyErr_BadArgument();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000218 }
219 for (i = 0; i < n; i++) {
Guido van Rossum3837de01997-04-29 15:43:55 +0000220 w = PyTuple_GetItem(v, i);
221 if (!PyInt_Check(w)) {
222 return PyErr_BadArgument();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000223 }
Guido van Rossum3837de01997-04-29 15:43:55 +0000224 p_arg[i] = (short) PyInt_AsLong(w);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000225 }
226 return 1;
227 }
Guido van Rossum3837de01997-04-29 15:43:55 +0000228 else if (PyList_Check(v)) {
229 if (PyList_Size(v) != n) {
230 return PyErr_BadArgument();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000231 }
232 for (i = 0; i < n; i++) {
Guido van Rossum3837de01997-04-29 15:43:55 +0000233 w = PyList_GetItem(v, i);
234 if (!PyInt_Check(w)) {
235 return PyErr_BadArgument();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000236 }
Guido van Rossum3837de01997-04-29 15:43:55 +0000237 p_arg[i] = (short) PyInt_AsLong(w);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000238 }
239 return 1;
240 }
241 else {
Guido van Rossum3837de01997-04-29 15:43:55 +0000242 return PyErr_BadArgument();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000243 }
244}
245
246int
Peter Schneider-Kamp6a850272000-07-10 17:04:33 +0000247PyArg_GetDoubleArray(PyObject *args, int nargs, int i, int n, double *p_arg)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000248{
Guido van Rossum3837de01997-04-29 15:43:55 +0000249 PyObject *v, *w;
250 if (!PyArg_GetObject(args, nargs, i, &v))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000251 return 0;
Guido van Rossum3837de01997-04-29 15:43:55 +0000252 if (PyTuple_Check(v)) {
253 if (PyTuple_Size(v) != n) {
254 return PyErr_BadArgument();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000255 }
256 for (i = 0; i < n; i++) {
Guido van Rossum3837de01997-04-29 15:43:55 +0000257 w = PyTuple_GetItem(v, i);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000258 if (!extractdouble(w, &p_arg[i]))
259 return 0;
260 }
261 return 1;
262 }
Guido van Rossum3837de01997-04-29 15:43:55 +0000263 else if (PyList_Check(v)) {
264 if (PyList_Size(v) != n) {
265 return PyErr_BadArgument();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000266 }
267 for (i = 0; i < n; i++) {
Guido van Rossum3837de01997-04-29 15:43:55 +0000268 w = PyList_GetItem(v, i);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000269 if (!extractdouble(w, &p_arg[i]))
270 return 0;
271 }
272 return 1;
273 }
274 else {
Guido van Rossum3837de01997-04-29 15:43:55 +0000275 return PyErr_BadArgument();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000276 }
277}
278
279int
Peter Schneider-Kamp6a850272000-07-10 17:04:33 +0000280PyArg_GetFloatArray(PyObject *args, int nargs, int i, int n, float *p_arg)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000281{
Guido van Rossum3837de01997-04-29 15:43:55 +0000282 PyObject *v, *w;
283 if (!PyArg_GetObject(args, nargs, i, &v))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000284 return 0;
Guido van Rossum3837de01997-04-29 15:43:55 +0000285 if (PyTuple_Check(v)) {
286 if (PyTuple_Size(v) != n) {
287 return PyErr_BadArgument();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000288 }
289 for (i = 0; i < n; i++) {
Guido van Rossum3837de01997-04-29 15:43:55 +0000290 w = PyTuple_GetItem(v, i);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000291 if (!extractfloat(w, &p_arg[i]))
292 return 0;
293 }
294 return 1;
295 }
Guido van Rossum3837de01997-04-29 15:43:55 +0000296 else if (PyList_Check(v)) {
297 if (PyList_Size(v) != n) {
298 return PyErr_BadArgument();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000299 }
300 for (i = 0; i < n; i++) {
Guido van Rossum3837de01997-04-29 15:43:55 +0000301 w = PyList_GetItem(v, i);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000302 if (!extractfloat(w, &p_arg[i]))
303 return 0;
304 }
305 return 1;
306 }
307 else {
Guido van Rossum3837de01997-04-29 15:43:55 +0000308 return PyErr_BadArgument();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000309 }
310}