blob: ca40fe1f327098be9a2cf5d3dc395673507a8969 [file] [log] [blame]
Guido van Rossumf70e43a1991-02-19 12:39:46 +00001/***********************************************************
Guido van Rossum6d023c91995-01-04 19:12:13 +00002Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
3The Netherlands.
Guido van Rossumf70e43a1991-02-19 12:39:46 +00004
5 All Rights Reserved
6
Guido van Rossumd266eb41996-10-25 14:44:06 +00007Permission to use, copy, modify, and distribute this software and its
8documentation for any purpose and without fee is hereby granted,
Guido van Rossumf70e43a1991-02-19 12:39:46 +00009provided that the above copyright notice appear in all copies and that
Guido van Rossumd266eb41996-10-25 14:44:06 +000010both that copyright notice and this permission notice appear in
Guido van Rossumf70e43a1991-02-19 12:39:46 +000011supporting documentation, and that the names of Stichting Mathematisch
Guido van Rossumd266eb41996-10-25 14:44:06 +000012Centrum or CWI or Corporation for National Research Initiatives or
13CNRI not be used in advertising or publicity pertaining to
14distribution of the software without specific, written prior
15permission.
Guido van Rossumf70e43a1991-02-19 12:39:46 +000016
Guido van Rossumd266eb41996-10-25 14:44:06 +000017While CWI is the initial source for this software, a modified version
18is made available by the Corporation for National Research Initiatives
19(CNRI) at the Internet address ftp://ftp.python.org.
20
21STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH
22REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF
23MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH
24CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
25DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
26PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
27TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
28PERFORMANCE OF THIS SOFTWARE.
Guido van Rossumf70e43a1991-02-19 12:39:46 +000029
30******************************************************************/
31
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000032/* Functions used by cgen output */
33
Guido van Rossum3837de01997-04-29 15:43:55 +000034#include "Python.h"
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000035#include "cgensupport.h"
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000036
37
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000038/* 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
43int
Guido van Rossum3837de01997-04-29 15:43:55 +000044PyArg_GetObject(args, nargs, i, p_arg)
45 register PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000046 int nargs, i;
Guido van Rossum3837de01997-04-29 15:43:55 +000047 PyObject **p_arg;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000048{
49 if (nargs != 1) {
Guido van Rossum3837de01997-04-29 15:43:55 +000050 if (args == NULL || !PyTuple_Check(args) ||
51 nargs != PyTuple_Size(args) ||
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000052 i < 0 || i >= nargs) {
Guido van Rossum3837de01997-04-29 15:43:55 +000053 return PyErr_BadArgument();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000054 }
55 else {
Guido van Rossum3837de01997-04-29 15:43:55 +000056 args = PyTuple_GetItem(args, i);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000057 }
58 }
59 if (args == NULL) {
Guido van Rossum3837de01997-04-29 15:43:55 +000060 return PyErr_BadArgument();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000061 }
62 *p_arg = args;
63 return 1;
64}
65
66int
Guido van Rossum3837de01997-04-29 15:43:55 +000067PyArg_GetLong(args, nargs, i, p_arg)
68 register PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000069 int nargs, i;
70 long *p_arg;
71{
72 if (nargs != 1) {
Guido van Rossum3837de01997-04-29 15:43:55 +000073 if (args == NULL || !PyTuple_Check(args) ||
74 nargs != PyTuple_Size(args) ||
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000075 i < 0 || i >= nargs) {
Guido van Rossum3837de01997-04-29 15:43:55 +000076 return PyErr_BadArgument();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000077 }
Guido van Rossum3837de01997-04-29 15:43:55 +000078 args = PyTuple_GetItem(args, i);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000079 }
Guido van Rossum3837de01997-04-29 15:43:55 +000080 if (args == NULL || !PyInt_Check(args)) {
81 return PyErr_BadArgument();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000082 }
Guido van Rossum3837de01997-04-29 15:43:55 +000083 *p_arg = PyInt_AsLong(args);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000084 return 1;
85}
86
87int
Guido van Rossum3837de01997-04-29 15:43:55 +000088PyArg_GetShort(args, nargs, i, p_arg)
89 register PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000090 int nargs, i;
91 short *p_arg;
92{
93 long x;
Guido van Rossum3837de01997-04-29 15:43:55 +000094 if (!PyArg_GetLong(args, nargs, i, &x))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000095 return 0;
Guido van Rossum6bf62da1997-04-11 20:37:35 +000096 *p_arg = (short) x;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000097 return 1;
98}
99
100static int
101extractdouble(v, p_arg)
Guido van Rossum3837de01997-04-29 15:43:55 +0000102 register PyObject *v;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000103 double *p_arg;
104{
105 if (v == NULL) {
106 /* Fall through to error return at end of function */
107 }
Guido van Rossum3837de01997-04-29 15:43:55 +0000108 else if (PyFloat_Check(v)) {
109 *p_arg = PyFloat_AS_DOUBLE((PyFloatObject *)v);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000110 return 1;
111 }
Guido van Rossum3837de01997-04-29 15:43:55 +0000112 else if (PyInt_Check(v)) {
113 *p_arg = PyInt_AS_LONG((PyIntObject *)v);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000114 return 1;
115 }
Guido van Rossum3837de01997-04-29 15:43:55 +0000116 else if (PyLong_Check(v)) {
117 *p_arg = PyLong_AsDouble(v);
Guido van Rossum76a00af1991-07-27 21:34:00 +0000118 return 1;
119 }
Guido van Rossum3837de01997-04-29 15:43:55 +0000120 return PyErr_BadArgument();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000121}
122
123static int
124extractfloat(v, p_arg)
Guido van Rossum3837de01997-04-29 15:43:55 +0000125 register PyObject *v;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000126 float *p_arg;
127{
128 if (v == NULL) {
129 /* Fall through to error return at end of function */
130 }
Guido van Rossum3837de01997-04-29 15:43:55 +0000131 else if (PyFloat_Check(v)) {
132 *p_arg = (float) PyFloat_AS_DOUBLE((PyFloatObject *)v);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000133 return 1;
134 }
Guido van Rossum3837de01997-04-29 15:43:55 +0000135 else if (PyInt_Check(v)) {
136 *p_arg = (float) PyInt_AS_LONG((PyIntObject *)v);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000137 return 1;
138 }
Guido van Rossum3837de01997-04-29 15:43:55 +0000139 else if (PyLong_Check(v)) {
140 *p_arg = (float) PyLong_AsDouble(v);
Guido van Rossum76a00af1991-07-27 21:34:00 +0000141 return 1;
142 }
Guido van Rossum3837de01997-04-29 15:43:55 +0000143 return PyErr_BadArgument();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000144}
145
146int
Guido van Rossum3837de01997-04-29 15:43:55 +0000147PyArg_GetFloat(args, nargs, i, p_arg)
148 register PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000149 int nargs, i;
150 float *p_arg;
151{
Guido van Rossum3837de01997-04-29 15:43:55 +0000152 PyObject *v;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000153 float x;
Guido van Rossum3837de01997-04-29 15:43:55 +0000154 if (!PyArg_GetObject(args, nargs, i, &v))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000155 return 0;
156 if (!extractfloat(v, &x))
157 return 0;
158 *p_arg = x;
159 return 1;
160}
161
162int
Guido van Rossum3837de01997-04-29 15:43:55 +0000163PyArg_GetString(args, nargs, i, p_arg)
164 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000165 int nargs, i;
166 string *p_arg;
167{
Guido van Rossum3837de01997-04-29 15:43:55 +0000168 PyObject *v;
169 if (!PyArg_GetObject(args, nargs, i, &v))
Guido van Rossum7cdf5eb1992-04-06 12:34:45 +0000170 return 0;
Guido van Rossum3837de01997-04-29 15:43:55 +0000171 if (!PyString_Check(v)) {
172 return PyErr_BadArgument();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000173 }
Guido van Rossum3837de01997-04-29 15:43:55 +0000174 *p_arg = PyString_AsString(v);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000175 return 1;
176}
177
178int
Guido van Rossum3837de01997-04-29 15:43:55 +0000179PyArg_GetChar(args, nargs, i, p_arg)
180 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000181 int nargs, i;
182 char *p_arg;
183{
184 string x;
Guido van Rossum3837de01997-04-29 15:43:55 +0000185 if (!PyArg_GetString(args, nargs, i, &x))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000186 return 0;
187 if (x[0] == '\0' || x[1] != '\0') {
188 /* Not exactly one char */
Guido van Rossum3837de01997-04-29 15:43:55 +0000189 return PyErr_BadArgument();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000190 }
191 *p_arg = x[0];
192 return 1;
193}
194
195int
Guido van Rossum3837de01997-04-29 15:43:55 +0000196PyArg_GetLongArraySize(args, nargs, i, p_arg)
197 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000198 int nargs, i;
199 long *p_arg;
200{
Guido van Rossum3837de01997-04-29 15:43:55 +0000201 PyObject *v;
202 if (!PyArg_GetObject(args, nargs, i, &v))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000203 return 0;
Guido van Rossum3837de01997-04-29 15:43:55 +0000204 if (PyTuple_Check(v)) {
205 *p_arg = PyTuple_Size(v);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000206 return 1;
207 }
Guido van Rossum3837de01997-04-29 15:43:55 +0000208 if (PyList_Check(v)) {
209 *p_arg = PyList_Size(v);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000210 return 1;
211 }
Guido van Rossum3837de01997-04-29 15:43:55 +0000212 return PyErr_BadArgument();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000213}
214
215int
Guido van Rossum3837de01997-04-29 15:43:55 +0000216PyArg_GetShortArraySize(args, nargs, i, p_arg)
217 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000218 int nargs, i;
219 short *p_arg;
220{
221 long x;
Guido van Rossum3837de01997-04-29 15:43:55 +0000222 if (!PyArg_GetLongArraySize(args, nargs, i, &x))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000223 return 0;
Guido van Rossum6bf62da1997-04-11 20:37:35 +0000224 *p_arg = (short) x;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000225 return 1;
226}
227
228/* XXX The following four are too similar. Should share more code. */
229
230int
Guido van Rossum3837de01997-04-29 15:43:55 +0000231PyArg_GetLongArray(args, nargs, i, n, p_arg)
232 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000233 int nargs, i;
234 int n;
235 long *p_arg; /* [n] */
236{
Guido van Rossum3837de01997-04-29 15:43:55 +0000237 PyObject *v, *w;
238 if (!PyArg_GetObject(args, nargs, i, &v))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000239 return 0;
Guido van Rossum3837de01997-04-29 15:43:55 +0000240 if (PyTuple_Check(v)) {
241 if (PyTuple_Size(v) != n) {
242 return PyErr_BadArgument();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000243 }
244 for (i = 0; i < n; i++) {
Guido van Rossum3837de01997-04-29 15:43:55 +0000245 w = PyTuple_GetItem(v, i);
246 if (!PyInt_Check(w)) {
247 return PyErr_BadArgument();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000248 }
Guido van Rossum3837de01997-04-29 15:43:55 +0000249 p_arg[i] = PyInt_AsLong(w);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000250 }
251 return 1;
252 }
Guido van Rossum3837de01997-04-29 15:43:55 +0000253 else if (PyList_Check(v)) {
254 if (PyList_Size(v) != n) {
255 return PyErr_BadArgument();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000256 }
257 for (i = 0; i < n; i++) {
Guido van Rossum3837de01997-04-29 15:43:55 +0000258 w = PyList_GetItem(v, i);
259 if (!PyInt_Check(w)) {
260 return PyErr_BadArgument();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000261 }
Guido van Rossum3837de01997-04-29 15:43:55 +0000262 p_arg[i] = PyInt_AsLong(w);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000263 }
264 return 1;
265 }
266 else {
Guido van Rossum3837de01997-04-29 15:43:55 +0000267 return PyErr_BadArgument();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000268 }
269}
270
271int
Guido van Rossum3837de01997-04-29 15:43:55 +0000272PyArg_GetShortArray(args, nargs, i, n, p_arg)
273 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000274 int nargs, i;
275 int n;
276 short *p_arg; /* [n] */
277{
Guido van Rossum3837de01997-04-29 15:43:55 +0000278 PyObject *v, *w;
279 if (!PyArg_GetObject(args, nargs, i, &v))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000280 return 0;
Guido van Rossum3837de01997-04-29 15:43:55 +0000281 if (PyTuple_Check(v)) {
282 if (PyTuple_Size(v) != n) {
283 return PyErr_BadArgument();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000284 }
285 for (i = 0; i < n; i++) {
Guido van Rossum3837de01997-04-29 15:43:55 +0000286 w = PyTuple_GetItem(v, i);
287 if (!PyInt_Check(w)) {
288 return PyErr_BadArgument();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000289 }
Guido van Rossum3837de01997-04-29 15:43:55 +0000290 p_arg[i] = (short) PyInt_AsLong(w);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000291 }
292 return 1;
293 }
Guido van Rossum3837de01997-04-29 15:43:55 +0000294 else if (PyList_Check(v)) {
295 if (PyList_Size(v) != n) {
296 return PyErr_BadArgument();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000297 }
298 for (i = 0; i < n; i++) {
Guido van Rossum3837de01997-04-29 15:43:55 +0000299 w = PyList_GetItem(v, i);
300 if (!PyInt_Check(w)) {
301 return PyErr_BadArgument();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000302 }
Guido van Rossum3837de01997-04-29 15:43:55 +0000303 p_arg[i] = (short) PyInt_AsLong(w);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000304 }
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}
311
312int
Guido van Rossum3837de01997-04-29 15:43:55 +0000313PyArg_GetDoubleArray(args, nargs, i, n, p_arg)
314 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000315 int nargs, i;
316 int n;
317 double *p_arg; /* [n] */
318{
Guido van Rossum3837de01997-04-29 15:43:55 +0000319 PyObject *v, *w;
320 if (!PyArg_GetObject(args, nargs, i, &v))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000321 return 0;
Guido van Rossum3837de01997-04-29 15:43:55 +0000322 if (PyTuple_Check(v)) {
323 if (PyTuple_Size(v) != n) {
324 return PyErr_BadArgument();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000325 }
326 for (i = 0; i < n; i++) {
Guido van Rossum3837de01997-04-29 15:43:55 +0000327 w = PyTuple_GetItem(v, i);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000328 if (!extractdouble(w, &p_arg[i]))
329 return 0;
330 }
331 return 1;
332 }
Guido van Rossum3837de01997-04-29 15:43:55 +0000333 else if (PyList_Check(v)) {
334 if (PyList_Size(v) != n) {
335 return PyErr_BadArgument();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000336 }
337 for (i = 0; i < n; i++) {
Guido van Rossum3837de01997-04-29 15:43:55 +0000338 w = PyList_GetItem(v, i);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000339 if (!extractdouble(w, &p_arg[i]))
340 return 0;
341 }
342 return 1;
343 }
344 else {
Guido van Rossum3837de01997-04-29 15:43:55 +0000345 return PyErr_BadArgument();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000346 }
347}
348
349int
Guido van Rossum3837de01997-04-29 15:43:55 +0000350PyArg_GetFloatArray(args, nargs, i, n, p_arg)
351 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000352 int nargs, i;
353 int n;
354 float *p_arg; /* [n] */
355{
Guido van Rossum3837de01997-04-29 15:43:55 +0000356 PyObject *v, *w;
357 if (!PyArg_GetObject(args, nargs, i, &v))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000358 return 0;
Guido van Rossum3837de01997-04-29 15:43:55 +0000359 if (PyTuple_Check(v)) {
360 if (PyTuple_Size(v) != n) {
361 return PyErr_BadArgument();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000362 }
363 for (i = 0; i < n; i++) {
Guido van Rossum3837de01997-04-29 15:43:55 +0000364 w = PyTuple_GetItem(v, i);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000365 if (!extractfloat(w, &p_arg[i]))
366 return 0;
367 }
368 return 1;
369 }
Guido van Rossum3837de01997-04-29 15:43:55 +0000370 else if (PyList_Check(v)) {
371 if (PyList_Size(v) != n) {
372 return PyErr_BadArgument();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000373 }
374 for (i = 0; i < n; i++) {
Guido van Rossum3837de01997-04-29 15:43:55 +0000375 w = PyList_GetItem(v, i);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000376 if (!extractfloat(w, &p_arg[i]))
377 return 0;
378 }
379 return 1;
380 }
381 else {
Guido van Rossum3837de01997-04-29 15:43:55 +0000382 return PyErr_BadArgument();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000383 }
384}