Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 1 | /*********************************************************** |
| 2 | Copyright 1991 by Stichting Mathematisch Centrum, Amsterdam, The |
| 3 | Netherlands. |
| 4 | |
| 5 | All Rights Reserved |
| 6 | |
| 7 | Permission to use, copy, modify, and distribute this software and its |
| 8 | documentation for any purpose and without fee is hereby granted, |
| 9 | provided that the above copyright notice appear in all copies and that |
| 10 | both that copyright notice and this permission notice appear in |
| 11 | supporting documentation, and that the names of Stichting Mathematisch |
| 12 | Centrum or CWI not be used in advertising or publicity pertaining to |
| 13 | distribution of the software without specific, written prior permission. |
| 14 | |
| 15 | STICHTING MATHEMATISCH CENTRUM DISCLAIMS ALL WARRANTIES WITH REGARD TO |
| 16 | THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND |
| 17 | FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH CENTRUM BE LIABLE |
| 18 | FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
| 19 | WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
| 20 | ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT |
| 21 | OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 22 | |
| 23 | ******************************************************************/ |
| 24 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 25 | /* Module support implementation */ |
| 26 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 27 | #include "allobjects.h" |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 28 | #include "modsupport.h" |
| 29 | #include "import.h" |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 30 | |
| 31 | |
| 32 | object * |
| 33 | initmodule(name, methods) |
| 34 | char *name; |
| 35 | struct methodlist *methods; |
| 36 | { |
| 37 | object *m, *d, *v; |
| 38 | struct methodlist *ml; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 39 | char namebuf[256]; |
| 40 | if ((m = add_module(name)) == NULL) { |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 41 | 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 Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 46 | 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 Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 50 | 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 Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 56 | return m; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 60 | /* Argument list handling tools. |
| 61 | All return 1 for success, or call err_set*() and return 0 for failure */ |
| 62 | |
| 63 | int |
| 64 | getnoarg(v) |
| 65 | object *v; |
| 66 | { |
| 67 | if (v != NULL) { |
| 68 | return err_badarg(); |
| 69 | } |
| 70 | return 1; |
| 71 | } |
| 72 | |
| 73 | int |
| 74 | getintarg(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 | |
| 85 | int |
| 86 | getintintarg(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 | |
| 98 | int |
Guido van Rossum | ef0068f | 1991-07-01 18:47:30 +0000 | [diff] [blame] | 99 | getintintintarg(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 | |
| 113 | int |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 114 | getlongarg(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 | |
| 125 | int |
Guido van Rossum | c5da350 | 1991-09-10 14:56:32 +0000 | [diff] [blame^] | 126 | getlonglongarg(v, a, b) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 127 | 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 | |
| 137 | int |
Guido van Rossum | c5da350 | 1991-09-10 14:56:32 +0000 | [diff] [blame^] | 138 | getlongobjectarg(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 | |
| 155 | int |
| 156 | getlonglongobjectarg(v, a, b, c) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 157 | 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 | |
| 174 | int |
| 175 | getstrarg(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 | |
| 186 | int |
| 187 | getstrstrarg(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 | |
| 199 | int |
| 200 | getstrstrintarg(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 | |
| 214 | int |
| 215 | getstrintarg(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 | |
| 227 | int |
| 228 | getintstrarg(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 | |
| 240 | int |
| 241 | getpointarg(v, a) |
| 242 | object *v; |
| 243 | int *a; /* [2] */ |
| 244 | { |
| 245 | return getintintarg(v, a, a+1); |
| 246 | } |
| 247 | |
| 248 | int |
| 249 | get3pointarg(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 | |
| 261 | int |
| 262 | getrectarg(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 | |
| 273 | int |
| 274 | getrectintarg(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 | |
| 285 | int |
| 286 | getpointintarg(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 | |
| 297 | int |
| 298 | getpointstrarg(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 | |
| 310 | int |
| 311 | getstrintintarg(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 | |
| 324 | int |
| 325 | getrectpointarg(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 | |
| 336 | int |
| 337 | getlongtuplearg(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 | |
| 356 | int |
| 357 | getshorttuplearg(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 | |
| 376 | int |
| 377 | getlonglistarg(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 | |
| 396 | int |
| 397 | getshortlistarg(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 Rossum | 7a904ed | 1991-04-03 19:06:26 +0000 | [diff] [blame] | 415 | |
| 416 | int |
| 417 | getdoublearg(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 Rossum | 139e57b | 1991-07-27 21:39:31 +0000 | [diff] [blame] | 431 | if (is_longobject(args)) { |
| 432 | *px = dgetlongvalue(args); |
| 433 | return 1; |
| 434 | } |
Guido van Rossum | 7a904ed | 1991-04-03 19:06:26 +0000 | [diff] [blame] | 435 | return err_badarg(); |
| 436 | } |
| 437 | |
| 438 | int |
| 439 | get2doublearg(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 | } |