blob: 5d56241e2736dbe2fdb3e3d146079214f0d2bf9e [file] [log] [blame]
Guido van Rossumf70e43a1991-02-19 12:39:46 +00001/***********************************************************
2Copyright 1991 by Stichting Mathematisch Centrum, Amsterdam, The
3Netherlands.
4
5 All Rights Reserved
6
7Permission to use, copy, modify, and distribute this software and its
8documentation for any purpose and without fee is hereby granted,
9provided that the above copyright notice appear in all copies and that
10both that copyright notice and this permission notice appear in
11supporting documentation, and that the names of Stichting Mathematisch
12Centrum or CWI not be used in advertising or publicity pertaining to
13distribution of the software without specific, written prior permission.
14
15STICHTING MATHEMATISCH CENTRUM DISCLAIMS ALL WARRANTIES WITH REGARD TO
16THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
17FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH CENTRUM BE LIABLE
18FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
19WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
20ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
21OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
22
23******************************************************************/
24
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000025/* Module support implementation */
26
Guido van Rossum3f5da241990-12-20 15:06:42 +000027#include "allobjects.h"
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000028#include "modsupport.h"
29#include "import.h"
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000030
31
32object *
33initmodule(name, methods)
34 char *name;
35 struct methodlist *methods;
36{
37 object *m, *d, *v;
38 struct methodlist *ml;
Guido van Rossum3f5da241990-12-20 15:06:42 +000039 char namebuf[256];
40 if ((m = add_module(name)) == NULL) {
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000041 fprintf(stderr, "initializing module: %s\n", name);
42 fatal("can't create a module");
43 }
44 d = getmoduledict(m);
45 for (ml = methods; ml->ml_name != NULL; ml++) {
Guido van Rossum3f5da241990-12-20 15:06:42 +000046 sprintf(namebuf, "%s.%s", name, ml->ml_name);
47 v = newmethodobject(strdup(namebuf), ml->ml_meth,
48 (object *)NULL);
49 /* XXX The strdup'ed memory is never freed */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000050 if (v == NULL || dictinsert(d, ml->ml_name, v) != 0) {
51 fprintf(stderr, "initializing module: %s\n", name);
52 fatal("can't initialize module");
53 }
54 DECREF(v);
55 }
Guido van Rossum3f5da241990-12-20 15:06:42 +000056 return m;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000057}
58
59
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000060/* Argument list handling tools.
61 All return 1 for success, or call err_set*() and return 0 for failure */
62
63int
64getnoarg(v)
65 object *v;
66{
67 if (v != NULL) {
68 return err_badarg();
69 }
70 return 1;
71}
72
73int
74getintarg(v, a)
75 object *v;
76 int *a;
77{
78 if (v == NULL || !is_intobject(v)) {
79 return err_badarg();
80 }
81 *a = getintvalue(v);
82 return 1;
83}
84
85int
86getintintarg(v, a, b)
87 object *v;
88 int *a;
89 int *b;
90{
91 if (v == NULL || !is_tupleobject(v) || gettuplesize(v) != 2) {
92 return err_badarg();
93 }
94 return getintarg(gettupleitem(v, 0), a) &&
95 getintarg(gettupleitem(v, 1), b);
96}
97
98int
Guido van Rossumef0068f1991-07-01 18:47:30 +000099getintintintarg(v, a, b, c)
100 object *v;
101 int *a;
102 int *b;
103 int *c;
104{
105 if (v == NULL || !is_tupleobject(v) || gettuplesize(v) != 3) {
106 return err_badarg();
107 }
108 return getintarg(gettupleitem(v, 0), a) &&
109 getintarg(gettupleitem(v, 1), b) &&
110 getintarg(gettupleitem(v, 2), c);
111}
112
113int
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000114getlongarg(v, a)
115 object *v;
116 long *a;
117{
118 if (v == NULL || !is_intobject(v)) {
119 return err_badarg();
120 }
121 *a = getintvalue(v);
122 return 1;
123}
124
125int
Guido van Rossumc5da3501991-09-10 14:56:32 +0000126getlonglongarg(v, a, b)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000127 object *v;
128 long *a, *b;
129{
130 if (v == NULL || !is_tupleobject(v) || gettuplesize(v) != 2) {
131 return err_badarg();
132 }
133 return getlongarg(gettupleitem(v, 0), a) &&
134 getlongarg(gettupleitem(v, 1), b);
135}
136
137int
Guido van Rossumc5da3501991-09-10 14:56:32 +0000138getlongobjectarg(v, a, b)
139 object *v;
140 long *a;
141 object **b;
142{
143 if (v == NULL || !is_tupleobject(v) || gettuplesize(v) != 2) {
144 return err_badarg();
145 }
146 if (getlongarg(gettupleitem(v, 0), a)) {
147 *b = gettupleitem(v, 1);
148 return 1;
149 }
150 else {
151 return err_badarg();
152 }
153}
154
155int
156getlonglongobjectarg(v, a, b, c)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000157 object *v;
158 long *a, *b;
159 object **c;
160{
161 if (v == NULL || !is_tupleobject(v) || gettuplesize(v) != 3) {
162 return err_badarg();
163 }
164 if (getlongarg(gettupleitem(v, 0), a) &&
165 getlongarg(gettupleitem(v, 1), b)) {
166 *c = gettupleitem(v, 2);
167 return 1;
168 }
169 else {
170 return err_badarg();
171 }
172}
173
174int
175getstrarg(v, a)
176 object *v;
177 object **a;
178{
179 if (v == NULL || !is_stringobject(v)) {
180 return err_badarg();
181 }
182 *a = v;
183 return 1;
184}
185
186int
187getstrstrarg(v, a, b)
188 object *v;
189 object **a;
190 object **b;
191{
192 if (v == NULL || !is_tupleobject(v) || gettuplesize(v) != 2) {
193 return err_badarg();
194 }
195 return getstrarg(gettupleitem(v, 0), a) &&
196 getstrarg(gettupleitem(v, 1), b);
197}
198
199int
200getstrstrintarg(v, a, b, c)
201 object *v;
202 object **a;
203 object **b;
204 int *c;
205{
206 if (v == NULL || !is_tupleobject(v) || gettuplesize(v) != 3) {
207 return err_badarg();
208 }
209 return getstrarg(gettupleitem(v, 0), a) &&
210 getstrarg(gettupleitem(v, 1), b) &&
211 getintarg(gettupleitem(v, 2), c);
212}
213
214int
215getstrintarg(v, a, b)
216 object *v;
217 object **a;
218 int *b;
219{
220 if (v == NULL || !is_tupleobject(v) || gettuplesize(v) != 2) {
221 return err_badarg();
222 }
223 return getstrarg(gettupleitem(v, 0), a) &&
224 getintarg(gettupleitem(v, 1), b);
225}
226
227int
228getintstrarg(v, a, b)
229 object *v;
230 int *a;
231 object **b;
232{
233 if (v == NULL || !is_tupleobject(v) || gettuplesize(v) != 2) {
234 return err_badarg();
235 }
236 return getintarg(gettupleitem(v, 0), a) &&
237 getstrarg(gettupleitem(v, 1), b);
238}
239
240int
241getpointarg(v, a)
242 object *v;
243 int *a; /* [2] */
244{
245 return getintintarg(v, a, a+1);
246}
247
248int
249get3pointarg(v, a)
250 object *v;
251 int *a; /* [6] */
252{
253 if (v == NULL || !is_tupleobject(v) || gettuplesize(v) != 3) {
254 return err_badarg();
255 }
256 return getpointarg(gettupleitem(v, 0), a) &&
257 getpointarg(gettupleitem(v, 1), a+2) &&
258 getpointarg(gettupleitem(v, 2), a+4);
259}
260
261int
262getrectarg(v, a)
263 object *v;
264 int *a; /* [2+2] */
265{
266 if (v == NULL || !is_tupleobject(v) || gettuplesize(v) != 2) {
267 return err_badarg();
268 }
269 return getpointarg(gettupleitem(v, 0), a) &&
270 getpointarg(gettupleitem(v, 1), a+2);
271}
272
273int
274getrectintarg(v, a)
275 object *v;
276 int *a; /* [4+1] */
277{
278 if (v == NULL || !is_tupleobject(v) || gettuplesize(v) != 2) {
279 return err_badarg();
280 }
281 return getrectarg(gettupleitem(v, 0), a) &&
282 getintarg(gettupleitem(v, 1), a+4);
283}
284
285int
286getpointintarg(v, a)
287 object *v;
288 int *a; /* [2+1] */
289{
290 if (v == NULL || !is_tupleobject(v) || gettuplesize(v) != 2) {
291 return err_badarg();
292 }
293 return getpointarg(gettupleitem(v, 0), a) &&
294 getintarg(gettupleitem(v, 1), a+2);
295}
296
297int
298getpointstrarg(v, a, b)
299 object *v;
300 int *a; /* [2] */
301 object **b;
302{
303 if (v == NULL || !is_tupleobject(v) || gettuplesize(v) != 2) {
304 return err_badarg();
305 }
306 return getpointarg(gettupleitem(v, 0), a) &&
307 getstrarg(gettupleitem(v, 1), b);
308}
309
310int
311getstrintintarg(v, a, b, c)
312 object *v;
313 object *a;
314 int *b, *c;
315{
316 if (v == NULL || !is_tupleobject(v) || gettuplesize(v) != 3) {
317 return err_badarg();
318 }
319 return getstrarg(gettupleitem(v, 0), a) &&
320 getintarg(gettupleitem(v, 1), b) &&
321 getintarg(gettupleitem(v, 2), c);
322}
323
324int
325getrectpointarg(v, a)
326 object *v;
327 int *a; /* [4+2] */
328{
329 if (v == NULL || !is_tupleobject(v) || gettuplesize(v) != 2) {
330 return err_badarg();
331 }
332 return getrectarg(gettupleitem(v, 0), a) &&
333 getpointarg(gettupleitem(v, 1), a+4);
334}
335
336int
337getlongtuplearg(args, a, n)
338 object *args;
339 long *a; /* [n] */
340 int n;
341{
342 int i;
343 if (!is_tupleobject(args) || gettuplesize(args) != n) {
344 return err_badarg();
345 }
346 for (i = 0; i < n; i++) {
347 object *v = gettupleitem(args, i);
348 if (!is_intobject(v)) {
349 return err_badarg();
350 }
351 a[i] = getintvalue(v);
352 }
353 return 1;
354}
355
356int
357getshorttuplearg(args, a, n)
358 object *args;
359 short *a; /* [n] */
360 int n;
361{
362 int i;
363 if (!is_tupleobject(args) || gettuplesize(args) != n) {
364 return err_badarg();
365 }
366 for (i = 0; i < n; i++) {
367 object *v = gettupleitem(args, i);
368 if (!is_intobject(v)) {
369 return err_badarg();
370 }
371 a[i] = getintvalue(v);
372 }
373 return 1;
374}
375
376int
377getlonglistarg(args, a, n)
378 object *args;
379 long *a; /* [n] */
380 int n;
381{
382 int i;
383 if (!is_listobject(args) || getlistsize(args) != n) {
384 return err_badarg();
385 }
386 for (i = 0; i < n; i++) {
387 object *v = getlistitem(args, i);
388 if (!is_intobject(v)) {
389 return err_badarg();
390 }
391 a[i] = getintvalue(v);
392 }
393 return 1;
394}
395
396int
397getshortlistarg(args, a, n)
398 object *args;
399 short *a; /* [n] */
400 int n;
401{
402 int i;
403 if (!is_listobject(args) || getlistsize(args) != n) {
404 return err_badarg();
405 }
406 for (i = 0; i < n; i++) {
407 object *v = getlistitem(args, i);
408 if (!is_intobject(v)) {
409 return err_badarg();
410 }
411 a[i] = getintvalue(v);
412 }
413 return 1;
414}
Guido van Rossum7a904ed1991-04-03 19:06:26 +0000415
416int
417getdoublearg(args, px)
418 register object *args;
419 double *px;
420{
421 if (args == NULL)
422 return err_badarg();
423 if (is_floatobject(args)) {
424 *px = getfloatvalue(args);
425 return 1;
426 }
427 if (is_intobject(args)) {
428 *px = getintvalue(args);
429 return 1;
430 }
Guido van Rossum139e57b1991-07-27 21:39:31 +0000431 if (is_longobject(args)) {
432 *px = dgetlongvalue(args);
433 return 1;
434 }
Guido van Rossum7a904ed1991-04-03 19:06:26 +0000435 return err_badarg();
436}
437
438int
439get2doublearg(args, px, py)
440 register object *args;
441 double *px, *py;
442{
443 if (args == NULL || !is_tupleobject(args) || gettuplesize(args) != 2)
444 return err_badarg();
445 return getdoublearg(gettupleitem(args, 0), px) &&
446 getdoublearg(gettupleitem(args, 1), py);
447}