bltinmodule.c revision dc4b93db7097a34cee492b188ec2fd65c5b9bc4b
1/*********************************************************** 2Copyright 1991, 1992, 1993 by Stichting Mathematisch Centrum, 3Amsterdam, The Netherlands. 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 25/* Built-in functions */ 26 27#include "allobjects.h" 28 29#include "node.h" 30#include "graminit.h" 31#include "sysmodule.h" 32#include "bltinmodule.h" 33#include "import.h" 34#include "pythonrun.h" 35#include "ceval.h" 36#include "modsupport.h" 37#include "compile.h" 38#include "eval.h" 39 40/* Forward */ 41static object *filterstring PROTO((object *, object *)); 42static object *filtertuple PROTO((object *, object *)); 43static object *exec_eval PROTO((object *v, int start)); 44 45static object * 46builtin_abs(self, v) 47 object *self; 48 object *v; 49{ 50 number_methods *nm; 51 if (v == NULL || (nm = v->ob_type->tp_as_number) == NULL) { 52 err_setstr(TypeError, "abs() requires numeric argument"); 53 return NULL; 54 } 55 return (*nm->nb_absolute)(v); 56} 57 58static object * 59builtin_apply(self, args) 60 object *self; 61 object *args; 62{ 63 object *func, *arglist; 64 if (!getargs(args, "(OO)", &func, &arglist)) 65 return NULL; 66 return call_object(func, arglist); 67} 68 69static object * 70builtin_bagof(self, args) 71 object *self; 72 object *args; 73{ 74 object *func, *seq, *result; 75 sequence_methods *sqf; 76 int len; 77 register int i, j; 78 79 if (!getargs(args, "(OO)", &func, &seq)) 80 return NULL; 81 82 if (is_stringobject(func)) { 83 if ((func = exec_eval(func, lambda_input)) == NULL) 84 return NULL; 85 } 86 else { 87 INCREF(func); 88 } 89 90 if (is_stringobject(seq)) { 91 object *r = filterstring(func, seq); 92 DECREF(func); 93 return r; 94 } 95 96 if (is_tupleobject(seq)) { 97 object *r = filtertuple(func, seq); 98 DECREF(func); 99 return r; 100 } 101 102 if ((sqf = seq->ob_type->tp_as_sequence) == NULL) { 103 err_setstr(TypeError, 104 "argument 2 to bagof() must be a sequence type"); 105 goto Fail_2; 106 } 107 108 if ((len = (*sqf->sq_length)(seq)) < 0) 109 goto Fail_2; 110 111 if (is_listobject(seq) && seq->ob_refcnt == 1) { 112 INCREF(seq); 113 result = seq; 114 } 115 else { 116 if ((result = newlistobject(len)) == NULL) 117 goto Fail_2; 118 } 119 120 for (i = j = 0; i < len; ++i) { 121 object *item, *good; 122 int ok; 123 124 if ((item = (*sqf->sq_item)(seq, i)) == NULL) 125 goto Fail_1; 126 127 if (func == None) { 128 good = item; 129 } 130 else { 131 object *arg = mkvalue("(O)", item); 132 DECREF(item); 133 if (arg == NULL) 134 goto Fail_1; 135 good = call_object(func, arg); 136 DECREF(arg); 137 if (good == NULL) 138 goto Fail_1; 139 } 140 ok = testbool(good); 141 DECREF(good); 142 if (ok) { 143 INCREF(item); 144 if (setlistitem(result, j++, item) < 0) 145 goto Fail_1; 146 } 147 } 148 149 150 if (setlistslice(result, j, len, NULL) < 0) 151 goto Fail_1; 152 153 DECREF(func); 154 return result; 155 156Fail_1: 157 DECREF(result); 158Fail_2: 159 DECREF(func); 160 return NULL; 161} 162 163static object * 164builtin_chr(self, args) 165 object *self; 166 object *args; 167{ 168 long x; 169 char s[1]; 170 if (!getargs(args, "l", &x)) 171 return NULL; 172 if (x < 0 || x >= 256) { 173 err_setstr(ValueError, "chr() arg not in range(256)"); 174 return NULL; 175 } 176 s[0] = x; 177 return newsizedstringobject(s, 1); 178} 179 180static object * 181builtin_cmp(self, args) 182 object *self; 183 object *args; 184{ 185 object *a, *b; 186 if (!getargs(args, "(OO)", &a, &b)) 187 return NULL; 188 return newintobject((long)cmpobject(a, b)); 189} 190 191static object * 192builtin_coerce(self, args) 193 object *self; 194 object *args; 195{ 196 object *v, *w; 197 object *res; 198 199 if (!getargs(args, "(OO)", &v, &w)) 200 return NULL; 201 if (coerce(&v, &w) < 0) 202 return NULL; 203 res = mkvalue("(OO)", v, w); 204 DECREF(v); 205 DECREF(w); 206 return res; 207} 208 209static object * 210builtin_compile(self, args) 211 object *self; 212 object *args; 213{ 214 char *str; 215 char *filename; 216 char *startstr; 217 int start; 218 if (!getargs(args, "(sss)", &str, &filename, &startstr)) 219 return NULL; 220 if (strcmp(startstr, "exec") == 0) 221 start = file_input; 222 else if (strcmp(startstr, "eval") == 0) 223 start = eval_input; 224 else { 225 err_setstr(ValueError, 226 "compile() mode must be 'exec' or 'eval'"); 227 return NULL; 228 } 229 return compile_string(str, filename, start); 230} 231 232static object * 233builtin_dir(self, v) 234 object *self; 235 object *v; 236{ 237 object *d; 238 if (v == NULL) { 239 d = getlocals(); 240 INCREF(d); 241 } 242 else { 243 d = getattr(v, "__dict__"); 244 if (d == NULL) { 245 err_setstr(TypeError, 246 "dir() argument must have __dict__ attribute"); 247 return NULL; 248 } 249 } 250 if (is_dictobject(d)) { 251 v = getdictkeys(d); 252 if (sortlist(v) != 0) { 253 DECREF(v); 254 v = NULL; 255 } 256 } 257 else { 258 v = newlistobject(0); 259 } 260 DECREF(d); 261 return v; 262} 263 264static object * 265builtin_divmod(self, args) 266 object *self; 267 object *args; 268{ 269 object *v, *w, *x; 270 if (!getargs(args, "(OO)", &v, &w)) 271 return NULL; 272 if (v->ob_type->tp_as_number == NULL || 273 w->ob_type->tp_as_number == NULL) { 274 err_setstr(TypeError, "divmod() requires numeric arguments"); 275 return NULL; 276 } 277 if (coerce(&v, &w) != 0) 278 return NULL; 279 x = (*v->ob_type->tp_as_number->nb_divmod)(v, w); 280 DECREF(v); 281 DECREF(w); 282 return x; 283} 284 285static object * 286exec_eval(v, start) 287 object *v; 288 int start; 289{ 290 object *str = NULL, *globals = NULL, *locals = NULL; 291 char *s; 292 int n; 293 /* XXX This is a bit of a mess. Should make it varargs */ 294 if (v != NULL) { 295 if (is_tupleobject(v) && 296 ((n = gettuplesize(v)) == 2 || n == 3)) { 297 str = gettupleitem(v, 0); 298 globals = gettupleitem(v, 1); 299 if (n == 3) 300 locals = gettupleitem(v, 2); 301 } 302 else 303 str = v; 304 } 305 if (str == NULL || (!is_stringobject(str) && !is_codeobject(str)) || 306 globals != NULL && !is_dictobject(globals) || 307 locals != NULL && !is_dictobject(locals)) { 308 err_setstr(TypeError, 309 "eval/lambda arguments must be (string|code)[,dict[,dict]]"); 310 return NULL; 311 } 312 /* XXX The following is only correct for eval(), not for lambda() */ 313 if (is_codeobject(str)) 314 return eval_code((codeobject *) str, globals, locals, 315 (object *)NULL, (object *)NULL); 316 s = getstringvalue(str); 317 if (strlen(s) != getstringsize(str)) { 318 err_setstr(ValueError, "embedded '\\0' in string arg"); 319 return NULL; 320 } 321 if (start == eval_input || start == lambda_input) { 322 while (*s == ' ' || *s == '\t') 323 s++; 324 } 325 return run_string(s, start, globals, locals); 326} 327 328static object * 329builtin_eval(self, v) 330 object *self; 331 object *v; 332{ 333 return exec_eval(v, eval_input); 334} 335 336static object * 337builtin_execfile(self, v) 338 object *self; 339 object *v; 340{ 341 object *str = NULL, *globals = NULL, *locals = NULL, *w; 342 FILE* fp; 343 char *s; 344 int n; 345 if (v != NULL) { 346 if (is_stringobject(v)) 347 str = v; 348 else if (is_tupleobject(v) && 349 ((n = gettuplesize(v)) == 2 || n == 3)) { 350 str = gettupleitem(v, 0); 351 globals = gettupleitem(v, 1); 352 if (n == 3) 353 locals = gettupleitem(v, 2); 354 } 355 } 356 if (str == NULL || !is_stringobject(str) || 357 globals != NULL && !is_dictobject(globals) || 358 locals != NULL && !is_dictobject(locals)) { 359 err_setstr(TypeError, 360 "execfile arguments must be filename[,dict[,dict]]"); 361 return NULL; 362 } 363 s = getstringvalue(str); 364 if (strlen(s) != getstringsize(str)) { 365 err_setstr(ValueError, "embedded '\\0' in string arg"); 366 return NULL; 367 } 368 BGN_SAVE 369 fp = fopen(s, "r"); 370 END_SAVE 371 if (fp == NULL) { 372 err_setstr(IOError, "execfile cannot open the file argument"); 373 return NULL; 374 } 375 w = run_file(fp, getstringvalue(str), file_input, globals, locals); 376 BGN_SAVE 377 fclose(fp); 378 END_SAVE 379 return w; 380} 381 382static object * 383builtin_float(self, v) 384 object *self; 385 object *v; 386{ 387 number_methods *nb; 388 389 if (v == NULL || (nb = v->ob_type->tp_as_number) == NULL || 390 nb->nb_float == NULL) { 391 err_setstr(TypeError, 392 "float() argument can't be converted to float"); 393 return NULL; 394 } 395 return (*nb->nb_float)(v); 396} 397 398static object * 399builtin_getattr(self, args) 400 object *self; 401 object *args; 402{ 403 object *v; 404 object *name; 405 if (!getargs(args, "(OS)", &v, &name)) 406 return NULL; 407 return getattro(v, name); 408} 409 410static object * 411builtin_hasattr(self, args) 412 object *self; 413 object *args; 414{ 415 object *v; 416 object *name; 417 if (!getargs(args, "(OS)", &v, &name)) 418 return NULL; 419 v = getattro(v, name); 420 if (v == NULL) { 421 err_clear(); 422 return newintobject(0L); 423 } 424 DECREF(v); 425 return newintobject(1L); 426} 427 428static object * 429builtin_id(self, args) 430 object *self; 431 object *args; 432{ 433 object *v; 434 if (!getargs(args, "O", &v)) 435 return NULL; 436 return newintobject((long)v); 437} 438 439static object * 440builtin_map(self, args) 441 object *self; 442 object *args; 443{ 444 typedef struct { 445 object *seq; 446 sequence_methods *sqf; 447 int len; 448 } sequence; 449 450 object *func, *result; 451 sequence *seqs = NULL, *sqp; 452 int n, len, newfunc = 0; 453 register int i, j; 454 455 if (args == NULL || !is_tupleobject(args)) { 456 err_setstr(TypeError, "map() requires at least two args"); 457 return NULL; 458 } 459 460 func = gettupleitem(args, 0); 461 n = gettuplesize(args) - 1; 462 463 if (is_stringobject(func)) { 464 if ((func = exec_eval(func, lambda_input)) == NULL) 465 return NULL; 466 } 467 else { 468 INCREF(func); 469 } 470 471 if ((seqs = NEW(sequence, n)) == NULL) { 472 err_nomem(); 473 goto Fail_2; 474 } 475 476 for (len = -1, i = 0, sqp = seqs; i < n; ++i, ++sqp) { 477 int curlen; 478 479 if ((sqp->seq = gettupleitem(args, i + 1)) == NULL) 480 goto Fail_2; 481 482 if (! (sqp->sqf = sqp->seq->ob_type->tp_as_sequence)) { 483 static char errmsg[] = 484 "argument %d to map() must be a sequence object"; 485 char errbuf[sizeof(errmsg) + 3]; 486 487 sprintf(errbuf, errmsg, i+2); 488 err_setstr(TypeError, errbuf); 489 goto Fail_2; 490 } 491 492 if ((curlen = sqp->len = (*sqp->sqf->sq_length)(sqp->seq)) < 0) 493 goto Fail_2; 494 495 if (curlen > len) 496 len = curlen; 497 } 498 499 if ((result = (object *) newlistobject(len)) == NULL) 500 goto Fail_2; 501 502 /* XXX Special case map(None, single_list) could be more efficient */ 503 for (i = 0; i < len; ++i) { 504 object *arglist, *item; 505 506 if ((arglist = newtupleobject(n)) == NULL) 507 goto Fail_1; 508 509 for (j = 0, sqp = seqs; j < n; ++j, ++sqp) { 510 if (i >= sqp->len) { 511 INCREF(None); 512 item = None; 513 } 514 else { 515 item = (*sqp->sqf->sq_item)(sqp->seq, i); 516 if (item == NULL) 517 goto Fail_0; 518 519 } 520 if (settupleitem(arglist, j, item) < 0) 521 goto Fail_0; 522 continue; 523 524 Fail_0: 525 DECREF(arglist); 526 goto Fail_1; 527 } 528 529 if (func == None) { 530 if (n == 1) { /* avoid creating singleton */ 531 INCREF(item); /* This is arglist[0] !!! */ 532 DECREF(arglist); 533 if (setlistitem(result, i, item) < 0) 534 goto Fail_1; 535 } 536 else { 537 if (setlistitem(result, i, arglist) < 0) 538 goto Fail_1; 539 } 540 } 541 else { 542 object *value = call_object(func, arglist); 543 DECREF(arglist); 544 if (value == NULL) 545 goto Fail_1; 546 if (setlistitem((object *) result, i, value) < 0) 547 goto Fail_1; 548 } 549 } 550 551 if (seqs) DEL(seqs); 552 DECREF(func); 553 return result; 554 555Fail_1: 556 DECREF(result); 557Fail_2: 558 DECREF(func); 559 if (seqs) DEL(seqs); 560 return NULL; 561} 562 563static object * 564builtin_setattr(self, args) 565 object *self; 566 object *args; 567{ 568 object *v; 569 object *name; 570 object *value; 571 if (!getargs(args, "(OSO)", &v, &name, &value)) 572 return NULL; 573 if (setattro(v, name, value) != 0) 574 return NULL; 575 INCREF(None); 576 return None; 577} 578 579static object * 580builtin_hash(self, args) 581 object *self; 582 object *args; 583{ 584 object *v; 585 long x; 586 if (!getargs(args, "O", &v)) 587 return NULL; 588 x = hashobject(v); 589 if (x == -1) 590 return NULL; 591 return newintobject(x); 592} 593 594static object * 595builtin_hex(self, v) 596 object *self; 597 object *v; 598{ 599 number_methods *nb; 600 601 if (v == NULL || (nb = v->ob_type->tp_as_number) == NULL || 602 nb->nb_hex == NULL) { 603 err_setstr(TypeError, 604 "hex() argument can't be converted to hex"); 605 return NULL; 606 } 607 return (*nb->nb_hex)(v); 608} 609 610static object *builtin_raw_input PROTO((object *, object *)); 611 612static object * 613builtin_input(self, v) 614 object *self; 615 object *v; 616{ 617 object *line = builtin_raw_input(self, v); 618 if (line == NULL) 619 return line; 620 v = exec_eval(line, eval_input); 621 DECREF(line); 622 return v; 623} 624 625static object * 626builtin_int(self, v) 627 object *self; 628 object *v; 629{ 630 number_methods *nb; 631 632 if (v == NULL || (nb = v->ob_type->tp_as_number) == NULL || 633 nb->nb_int == NULL) { 634 err_setstr(TypeError, 635 "int() argument can't be converted to int"); 636 return NULL; 637 } 638 return (*nb->nb_int)(v); 639} 640 641static object * 642builtin_lambda(self, v) 643 object *self; 644 object *v; 645{ 646 return exec_eval(v, lambda_input); 647} 648 649static object * 650builtin_len(self, v) 651 object *self; 652 object *v; 653{ 654 long len; 655 typeobject *tp; 656 if (v == NULL) { 657 err_setstr(TypeError, "len() without argument"); 658 return NULL; 659 } 660 tp = v->ob_type; 661 if (tp->tp_as_sequence != NULL) { 662 len = (*tp->tp_as_sequence->sq_length)(v); 663 } 664 else if (tp->tp_as_mapping != NULL) { 665 len = (*tp->tp_as_mapping->mp_length)(v); 666 } 667 else { 668 err_setstr(TypeError, "len() of unsized object"); 669 return NULL; 670 } 671 if (len < 0) 672 return NULL; 673 else 674 return newintobject(len); 675} 676 677static object * 678builtin_long(self, v) 679 object *self; 680 object *v; 681{ 682 number_methods *nb; 683 684 if (v == NULL || (nb = v->ob_type->tp_as_number) == NULL || 685 nb->nb_long == NULL) { 686 err_setstr(TypeError, 687 "long() argument can't be converted to long"); 688 return NULL; 689 } 690 return (*nb->nb_long)(v); 691} 692 693static object * 694min_max(v, sign) 695 object *v; 696 int sign; 697{ 698 int i, n, cmp; 699 object *w, *x; 700 sequence_methods *sq; 701 if (v == NULL) { 702 err_setstr(TypeError, "min() or max() without argument"); 703 return NULL; 704 } 705 sq = v->ob_type->tp_as_sequence; 706 if (sq == NULL) { 707 err_setstr(TypeError, "min() or max() of non-sequence"); 708 return NULL; 709 } 710 n = (*sq->sq_length)(v); 711 if (n < 0) 712 return NULL; 713 if (n == 0) { 714 err_setstr(ValueError, "min() or max() of empty sequence"); 715 return NULL; 716 } 717 w = (*sq->sq_item)(v, 0); /* Implies INCREF */ 718 for (i = 1; i < n; i++) { 719 x = (*sq->sq_item)(v, i); /* Implies INCREF */ 720 cmp = cmpobject(x, w); 721 if (cmp * sign > 0) { 722 DECREF(w); 723 w = x; 724 } 725 else 726 DECREF(x); 727 } 728 return w; 729} 730 731static object * 732builtin_min(self, v) 733 object *self; 734 object *v; 735{ 736 return min_max(v, -1); 737} 738 739static object * 740builtin_max(self, v) 741 object *self; 742 object *v; 743{ 744 return min_max(v, 1); 745} 746 747static object * 748builtin_oct(self, v) 749 object *self; 750 object *v; 751{ 752 number_methods *nb; 753 754 if (v == NULL || (nb = v->ob_type->tp_as_number) == NULL || 755 nb->nb_oct == NULL) { 756 err_setstr(TypeError, 757 "oct() argument can't be converted to oct"); 758 return NULL; 759 } 760 return (*nb->nb_oct)(v); 761} 762 763static object * 764builtin_open(self, args) 765 object *self; 766 object *args; 767{ 768 char *name, *mode; 769 if (!getargs(args, "(ss)", &name, &mode)) 770 return NULL; 771 return newfileobject(name, mode); 772} 773 774static object * 775builtin_ord(self, args) 776 object *self; 777 object *args; 778{ 779 char *s; 780 int len; 781 if (!getargs(args, "s#", &s, &len)) 782 return NULL; 783 if (len != 1) { 784 err_setstr(ValueError, "ord() arg must have length 1"); 785 return NULL; 786 } 787 return newintobject((long)(s[0] & 0xff)); 788} 789 790static object * 791builtin_pow(self, args) 792 object *self; 793 object *args; 794{ 795 object *v, *w, *x; 796 if (!getargs(args, "(OO)", &v, &w)) 797 return NULL; 798 if (v->ob_type->tp_as_number == NULL || 799 w->ob_type->tp_as_number == NULL) { 800 err_setstr(TypeError, "pow() requires numeric arguments"); 801 return NULL; 802 } 803 if (coerce(&v, &w) != 0) 804 return NULL; 805 x = (*v->ob_type->tp_as_number->nb_power)(v, w); 806 DECREF(v); 807 DECREF(w); 808 return x; 809} 810 811static object * 812builtin_range(self, v) 813 object *self; 814 object *v; 815{ 816 static char *errmsg = "range() requires 1-3 int arguments"; 817 int i, n; 818 long ilow, ihigh, istep; 819 if (v != NULL && is_intobject(v)) { 820 ilow = 0; ihigh = getintvalue(v); istep = 1; 821 } 822 else if (v == NULL || !is_tupleobject(v)) { 823 err_setstr(TypeError, errmsg); 824 return NULL; 825 } 826 else { 827 n = gettuplesize(v); 828 if (n < 1 || n > 3) { 829 err_setstr(TypeError, errmsg); 830 return NULL; 831 } 832 for (i = 0; i < n; i++) { 833 if (!is_intobject(gettupleitem(v, i))) { 834 err_setstr(TypeError, errmsg); 835 return NULL; 836 } 837 } 838 if (n == 3) { 839 istep = getintvalue(gettupleitem(v, 2)); 840 --n; 841 } 842 else 843 istep = 1; 844 ihigh = getintvalue(gettupleitem(v, --n)); 845 if (n > 0) 846 ilow = getintvalue(gettupleitem(v, 0)); 847 else 848 ilow = 0; 849 } 850 if (istep == 0) { 851 err_setstr(ValueError, "zero step for range()"); 852 return NULL; 853 } 854 /* XXX ought to check overflow of subtraction */ 855 if (istep > 0) 856 n = (ihigh - ilow + istep - 1) / istep; 857 else 858 n = (ihigh - ilow + istep + 1) / istep; 859 if (n < 0) 860 n = 0; 861 v = newlistobject(n); 862 if (v == NULL) 863 return NULL; 864 for (i = 0; i < n; i++) { 865 object *w = newintobject(ilow); 866 if (w == NULL) { 867 DECREF(v); 868 return NULL; 869 } 870 setlistitem(v, i, w); 871 ilow += istep; 872 } 873 return v; 874} 875 876static object * 877builtin_xrange(self, v) 878 object *self; 879 object *v; 880{ 881 static char *errmsg = "xrange() requires 1-3 int arguments"; 882 int i, n; 883 long start, stop, step, len; 884 if (v != NULL && is_intobject(v)) 885 start = 0, stop = getintvalue(v), step = 1; 886 887 else if (v == NULL || !is_tupleobject(v)) { 888 err_setstr(TypeError, errmsg); 889 return NULL; 890 } 891 else { 892 n = gettuplesize(v); 893 if (n < 1 || n > 3) { 894 err_setstr(TypeError, errmsg); 895 return NULL; 896 } 897 for (i = 0; i < n; i++) { 898 if (!is_intobject(gettupleitem(v, i))) { 899 err_setstr(TypeError, errmsg); 900 return NULL; 901 } 902 } 903 if (n == 3) { 904 step = getintvalue(gettupleitem(v, 2)); 905 --n; 906 } 907 else 908 step = 1; 909 stop = getintvalue(gettupleitem(v, --n)); 910 if (n > 0) 911 start = getintvalue(gettupleitem(v, 0)); 912 else 913 start = 0; 914 } 915 916 if (step == 0) { 917 err_setstr(ValueError, "zero step for xrange()"); 918 return NULL; 919 } 920 921 len = (stop - start + step + ((step > 0) ? -1 : 1)) / step; 922 if (len < 0) 923 len = 0; 924 925 return newrangeobject(start, len, step, 1); 926} 927 928static object * 929builtin_raw_input(self, v) 930 object *self; 931 object *v; 932{ 933 object *f = sysget("stdout"); 934 if (f == NULL) { 935 err_setstr(RuntimeError, "lost sys.stdout"); 936 return NULL; 937 } 938 flushline(); 939 if (v != NULL) { 940 if (writeobject(v, f, PRINT_RAW) != 0) 941 return NULL; 942 } 943 return filegetline(sysget("stdin"), -1); 944} 945 946static object * 947builtin_reduce(self, args) 948 object *self; 949 object *args; 950{ 951 object *seq, *func, *result; 952 sequence_methods *sqf; 953 static char reduce_err[] = "reduce() requires 2 or 3 args"; 954 register int i; 955 int start = 0; 956 int len; 957 958 if (args == NULL || !is_tupleobject(args)) { 959 err_setstr(TypeError, reduce_err); 960 return NULL; 961 } 962 963 switch (gettuplesize(args)) { 964 case 2: 965 start = 1; /* fall through */ 966 case 3: 967 func = gettupleitem(args, 0); 968 seq = gettupleitem(args, 1); 969 break; 970 default: 971 err_setstr(TypeError, reduce_err); 972 } 973 974 if ((sqf = seq->ob_type->tp_as_sequence) == NULL) { 975 err_setstr(TypeError, 976 "2nd argument to reduce() must be a sequence object"); 977 return NULL; 978 } 979 980 if (is_stringobject(func)) { 981 if ((func = exec_eval(func, lambda_input)) == NULL) 982 return NULL; 983 } 984 else { 985 INCREF(func); 986 } 987 988 if ((len = (*sqf->sq_length)(seq)) < 0) 989 goto Fail_2; 990 991 if (start == 1) { 992 if (len == 0) { 993 err_setstr(TypeError, 994 "reduce of empty sequence with no initial value"); 995 goto Fail_2; 996 } 997 998 if ((result = (*sqf->sq_item)(seq, 0)) == NULL) 999 goto Fail_2; 1000 } 1001 else { 1002 result = gettupleitem(args, 2); 1003 INCREF(result); 1004 } 1005 1006 if ((args = newtupleobject(2)) == NULL) 1007 goto Fail_1; 1008 1009 for (i = start; i < len; ++i) { 1010 object *op2; 1011 1012 if (args->ob_refcnt > 1) { 1013 DECREF(args); 1014 if ((args = newtupleobject(2)) == NULL) 1015 goto Fail_1; 1016 } 1017 1018 if ((op2 = (*sqf->sq_item)(seq, i)) == NULL) 1019 goto Fail_2; 1020 1021 settupleitem(args, 0, result); 1022 settupleitem(args, 1, op2); 1023 if ((result = call_object(func, args)) == NULL) 1024 goto Fail_0; 1025 } 1026 1027 DECREF(args); 1028 DECREF(func); 1029 1030 return result; 1031 1032Fail_0: 1033 DECREF(args); 1034 goto Fail_2; 1035Fail_1: 1036 DECREF(result); 1037Fail_2: 1038 DECREF(func); 1039 return NULL; 1040} 1041 1042static object * 1043builtin_reload(self, v) 1044 object *self; 1045 object *v; 1046{ 1047 return reload_module(v); 1048} 1049 1050static object * 1051builtin_repr(self, v) 1052 object *self; 1053 object *v; 1054{ 1055 if (v == NULL) { 1056 err_badarg(); 1057 return NULL; 1058 } 1059 return reprobject(v); 1060} 1061 1062static object * 1063builtin_round(self, args) 1064 object *self; 1065 object *args; 1066{ 1067 extern double floor PROTO((double)); 1068 extern double ceil PROTO((double)); 1069 double x; 1070 double f; 1071 int ndigits = 0; 1072 int sign = 1; 1073 int i; 1074 if (!getargs(args, "d", &x)) { 1075 err_clear(); 1076 if (!getargs(args, "(di)", &x, &ndigits)) 1077 return NULL; 1078 } 1079 f = 1.0; 1080 for (i = ndigits; --i >= 0; ) 1081 f = f*10.0; 1082 for (i = ndigits; ++i <= 0; ) 1083 f = f*0.1; 1084 if (x >= 0.0) 1085 return newfloatobject(floor(x*f + 0.5) / f); 1086 else 1087 return newfloatobject(ceil(x*f - 0.5) / f); 1088} 1089 1090static object * 1091builtin_str(self, v) 1092 object *self; 1093 object *v; 1094{ 1095 if (v == NULL) { 1096 err_badarg(); 1097 return NULL; 1098 } 1099 if (is_stringobject(v)) { 1100 INCREF(v); 1101 return v; 1102 } 1103 else 1104 return reprobject(v); 1105} 1106 1107static object * 1108builtin_type(self, v) 1109 object *self; 1110 object *v; 1111{ 1112 if (v == NULL) { 1113 err_setstr(TypeError, "type() requires an argument"); 1114 return NULL; 1115 } 1116 v = (object *)v->ob_type; 1117 INCREF(v); 1118 return v; 1119} 1120 1121static struct methodlist builtin_methods[] = { 1122 {"abs", builtin_abs}, 1123 {"apply", builtin_apply}, 1124 {"bagof", builtin_bagof}, 1125 {"chr", builtin_chr}, 1126 {"cmp", builtin_cmp}, 1127 {"coerce", builtin_coerce}, 1128 {"compile", builtin_compile}, 1129 {"dir", builtin_dir}, 1130 {"divmod", builtin_divmod}, 1131 {"eval", builtin_eval}, 1132 {"execfile", builtin_execfile}, 1133 {"float", builtin_float}, 1134 {"getattr", builtin_getattr}, 1135 {"hasattr", builtin_hasattr}, 1136 {"hash", builtin_hash}, 1137 {"hex", builtin_hex}, 1138 {"id", builtin_id}, 1139 {"input", builtin_input}, 1140 {"int", builtin_int}, 1141 {"lambda", builtin_lambda}, 1142 {"len", builtin_len}, 1143 {"long", builtin_long}, 1144 {"map", builtin_map}, 1145 {"max", builtin_max}, 1146 {"min", builtin_min}, 1147 {"oct", builtin_oct}, 1148 {"open", builtin_open}, 1149 {"ord", builtin_ord}, 1150 {"pow", builtin_pow}, 1151 {"range", builtin_range}, 1152 {"raw_input", builtin_raw_input}, 1153 {"reduce", builtin_reduce}, 1154 {"reload", builtin_reload}, 1155 {"repr", builtin_repr}, 1156 {"round", builtin_round}, 1157 {"setattr", builtin_setattr}, 1158 {"str", builtin_str}, 1159 {"type", builtin_type}, 1160 {"xrange", builtin_xrange}, 1161 {NULL, NULL}, 1162}; 1163 1164static object *builtin_dict; 1165 1166object * 1167getbuiltin(name) 1168 object *name; 1169{ 1170 return dict2lookup(builtin_dict, name); 1171} 1172 1173/* Predefined exceptions */ 1174 1175object *AccessError; 1176object *AttributeError; 1177object *ConflictError; 1178object *EOFError; 1179object *IOError; 1180object *ImportError; 1181object *IndexError; 1182object *KeyError; 1183object *KeyboardInterrupt; 1184object *MemoryError; 1185object *NameError; 1186object *OverflowError; 1187object *RuntimeError; 1188object *SyntaxError; 1189object *SystemError; 1190object *SystemExit; 1191object *TypeError; 1192object *ValueError; 1193object *ZeroDivisionError; 1194 1195static object * 1196newstdexception(name) 1197 char *name; 1198{ 1199 object *v = newstringobject(name); 1200 if (v == NULL || dictinsert(builtin_dict, name, v) != 0) 1201 fatal("no mem for new standard exception"); 1202 return v; 1203} 1204 1205static void 1206initerrors() 1207{ 1208 AccessError = newstdexception("AccessError"); 1209 AttributeError = newstdexception("AttributeError"); 1210 ConflictError = newstdexception("ConflictError"); 1211 EOFError = newstdexception("EOFError"); 1212 IOError = newstdexception("IOError"); 1213 ImportError = newstdexception("ImportError"); 1214 IndexError = newstdexception("IndexError"); 1215 KeyError = newstdexception("KeyError"); 1216 KeyboardInterrupt = newstdexception("KeyboardInterrupt"); 1217 MemoryError = newstdexception("MemoryError"); 1218 NameError = newstdexception("NameError"); 1219 OverflowError = newstdexception("OverflowError"); 1220 RuntimeError = newstdexception("RuntimeError"); 1221 SyntaxError = newstdexception("SyntaxError"); 1222 SystemError = newstdexception("SystemError"); 1223 SystemExit = newstdexception("SystemExit"); 1224 TypeError = newstdexception("TypeError"); 1225 ValueError = newstdexception("ValueError"); 1226 ZeroDivisionError = newstdexception("ZeroDivisionError"); 1227} 1228 1229void 1230initbuiltin() 1231{ 1232 object *m; 1233 m = initmodule("__builtin__", builtin_methods); 1234 builtin_dict = getmoduledict(m); 1235 INCREF(builtin_dict); 1236 initerrors(); 1237 (void) dictinsert(builtin_dict, "None", None); 1238} 1239 1240/* Coerce two numeric types to the "larger" one. 1241 Increment the reference count on each argument. 1242 Return -1 and raise an exception if no coercion is possible 1243 (and then no reference count is incremented). 1244*/ 1245 1246int 1247coerce(pv, pw) 1248 object **pv, **pw; 1249{ 1250 register object *v = *pv; 1251 register object *w = *pw; 1252 int res; 1253 1254 if (v->ob_type == w->ob_type && !is_instanceobject(v)) { 1255 INCREF(v); 1256 INCREF(w); 1257 return 0; 1258 } 1259 if (v->ob_type->tp_as_number && v->ob_type->tp_as_number->nb_coerce) { 1260 res = (*v->ob_type->tp_as_number->nb_coerce)(pv, pw); 1261 if (res <= 0) 1262 return res; 1263 } 1264 if (w->ob_type->tp_as_number && w->ob_type->tp_as_number->nb_coerce) { 1265 res = (*w->ob_type->tp_as_number->nb_coerce)(pw, pv); 1266 if (res <= 0) 1267 return res; 1268 } 1269 err_setstr(TypeError, "number coercion failed"); 1270 return -1; 1271} 1272 1273 1274/* Helper for bagof(): filter a tuple through a function */ 1275 1276static object * 1277filtertuple(func, tuple) 1278 object *func; 1279 object *tuple; 1280{ 1281 object *result; 1282 register int i, j; 1283 int len = gettuplesize(tuple), shared = 0; 1284 1285 if (tuple->ob_refcnt == 1) { 1286 result = tuple; 1287 shared = 1; 1288 /* defer INCREF (resizetuple wants it to be one) */ 1289 } 1290 else 1291 if ((result = newtupleobject(len)) == NULL) 1292 return NULL; 1293 1294 for (i = j = 0; i < len; ++i) { 1295 object *item, *good; 1296 int ok; 1297 1298 if ((item = gettupleitem(tuple, i)) == NULL) 1299 goto Fail_1; 1300 if (func == None) { 1301 INCREF(item); 1302 good = item; 1303 } 1304 else { 1305 object *arg = mkvalue("(O)", item); 1306 if (arg == NULL) 1307 goto Fail_1; 1308 good = call_object(func, arg); 1309 DECREF(arg); 1310 if (good == NULL) 1311 goto Fail_1; 1312 } 1313 ok = testbool(good); 1314 DECREF(good); 1315 if (ok) { 1316 INCREF(item); 1317 if (settupleitem(result, j++, item) < 0) 1318 goto Fail_1; 1319 } 1320 } 1321 1322 if (resizetuple(&result, j) < 0) 1323 return NULL; 1324 1325 if (shared) 1326 INCREF(result); 1327 1328 return result; 1329 1330Fail_1: 1331 if (!shared) 1332 DECREF(result); 1333 return NULL; 1334} 1335 1336 1337/* Helper for bagof(): filter a string through a function */ 1338 1339static object * 1340filterstring(func, strobj) 1341 object *func; 1342 object *strobj; 1343{ 1344 object *result; 1345 register int i, j; 1346 int len = getstringsize(strobj), shared = 0; 1347 1348 if (strobj->ob_refcnt == 1) { 1349 result = strobj; 1350 shared = 1; 1351 /* defer INCREF (resizestring wants it to be one) */ 1352 1353 if (func == None) { 1354 INCREF(result); 1355 return result; 1356 } 1357 } 1358 else { 1359 if ((result = newsizedstringobject(NULL, len)) == NULL) 1360 return NULL; 1361 1362 if (func == None) { 1363 strcpy(GETSTRINGVALUE((stringobject *)result), 1364 GETSTRINGVALUE((stringobject *)strobj)); 1365 return result; 1366 } 1367 } 1368 1369 for (i = j = 0; i < len; ++i) { 1370 object *item, *arg, *good; 1371 int ok; 1372 1373 item = (*strobj->ob_type->tp_as_sequence->sq_item)(strobj, i); 1374 if (item == NULL) 1375 goto Fail_1; 1376 arg = mkvalue("(O)", item); 1377 DECREF(item); 1378 if (arg == NULL) 1379 goto Fail_1; 1380 good = call_object(func, arg); 1381 DECREF(arg); 1382 if (good == NULL) 1383 goto Fail_1; 1384 ok = testbool(good); 1385 DECREF(good); 1386 if (ok) 1387 GETSTRINGVALUE((stringobject *)result)[j++] = 1388 GETSTRINGVALUE((stringobject *)item)[0]; 1389 } 1390 1391 if (resizestring(&result, j) < 0) 1392 return NULL; 1393 1394 if (shared) 1395 INCREF(result); 1396 1397 return result; 1398 1399Fail_1: 1400 if (!shared) 1401 DECREF(result); 1402 return NULL; 1403} 1404