1 /***********************************************************
2 Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
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.
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.
23 ******************************************************************/
25 /* Built-in functions */
27 #include "allobjects.h"
31 #include "sysmodule.h"
32 #include "bltinmodule.h"
34 #include "pythonrun.h"
36 #include "modsupport.h"
41 static object
*filterstring
PROTO((object
*, object
*));
42 static object
*filtertuple
PROTO((object
*, object
*));
45 builtin___import__(self
, args
)
50 object
*globals
= NULL
;
51 object
*locals
= NULL
;
52 object
*fromlist
= NULL
;
54 if (!newgetargs(args
, "s|OOO:__import__",
55 &name
, &globals
, &locals
, &fromlist
))
57 return import_module(name
);
62 builtin_abs(self
, args
)
69 if (!newgetargs(args
, "O:abs", &v
))
71 if ((nm
= v
->ob_type
->tp_as_number
) == NULL
) {
72 err_setstr(TypeError
, "abs() requires numeric argument");
75 return (*nm
->nb_absolute
)(v
);
79 builtin_apply(self
, args
)
83 object
*func
, *alist
= NULL
, *kwdict
= NULL
;
85 if (!newgetargs(args
, "O|OO:apply", &func
, &alist
, &kwdict
))
87 if (alist
!= NULL
&& !is_tupleobject(alist
)) {
88 err_setstr(TypeError
, "apply() 2nd argument must be tuple");
91 if (kwdict
!= NULL
&& !is_dictobject(kwdict
)) {
93 "apply() 3rd argument must be dictionary");
96 return PyEval_CallObjectWithKeywords(func
, alist
, kwdict
);
100 builtin_callable(self
, args
)
106 if (!newgetargs(args
, "O:callable", &v
))
108 return newintobject((long)callable(v
));
112 builtin_filter(self
, args
)
116 object
*func
, *seq
, *result
;
117 sequence_methods
*sqf
;
121 if (!newgetargs(args
, "OO:filter", &func
, &seq
))
124 if (is_stringobject(seq
)) {
125 object
*r
= filterstring(func
, seq
);
129 if (is_tupleobject(seq
)) {
130 object
*r
= filtertuple(func
, seq
);
134 if ((sqf
= seq
->ob_type
->tp_as_sequence
) == NULL
) {
135 err_setstr(TypeError
,
136 "argument 2 to filter() must be a sequence type");
140 if ((len
= (*sqf
->sq_length
)(seq
)) < 0)
143 if (is_listobject(seq
) && seq
->ob_refcnt
== 1) {
148 if ((result
= newlistobject(len
)) == NULL
)
152 for (i
= j
= 0; ; ++i
) {
156 if ((item
= (*sqf
->sq_item
)(seq
, i
)) == NULL
) {
159 if (err_occurred() == IndexError
) {
171 object
*arg
= mkvalue("(O)", item
);
174 good
= call_object(func
, arg
);
185 if (setlistitem(result
, j
++, item
) < 0)
190 if (addlistitem(result
, item
) < 0)
199 if (j
< len
&& setlistslice(result
, j
, len
, NULL
) < 0)
211 builtin_chr(self
, args
)
218 if (!newgetargs(args
, "l:chr", &x
))
220 if (x
< 0 || x
>= 256) {
221 err_setstr(ValueError
, "chr() arg not in range(256)");
225 return newsizedstringobject(s
, 1);
229 builtin_cmp(self
, args
)
235 if (!newgetargs(args
, "OO:cmp", &a
, &b
))
237 return newintobject((long)cmpobject(a
, b
));
241 builtin_coerce(self
, args
)
248 if (!newgetargs(args
, "OO:coerce", &v
, &w
))
250 if (coerce(&v
, &w
) < 0)
252 res
= mkvalue("(OO)", v
, w
);
259 builtin_compile(self
, args
)
268 if (!newgetargs(args
, "sss:compile", &str
, &filename
, &startstr
))
270 if (strcmp(startstr
, "exec") == 0)
272 else if (strcmp(startstr
, "eval") == 0)
274 else if (strcmp(startstr
, "single") == 0)
275 start
= single_input
;
277 err_setstr(ValueError
,
278 "compile() mode must be 'exec' or 'eval' or 'single'");
281 return compile_string(str
, filename
, start
);
284 #ifndef WITHOUT_COMPLEX
287 builtin_complex(self
, args
)
292 number_methods
*nbr
, *nbi
;
296 if (!newgetargs(args
, "O|O:complex", &r
, &i
))
298 if ((nbr
= r
->ob_type
->tp_as_number
) == NULL
||
299 nbr
->nb_float
== NULL
|| (i
!= NULL
&&
300 ((nbi
= i
->ob_type
->tp_as_number
) == NULL
||
301 nbi
->nb_float
== NULL
))) {
302 err_setstr(TypeError
,
303 "complex() argument can't be converted to complex");
306 if (is_complexobject(r
))
307 cr
= ((complexobject
*)r
)->cval
;
309 cr
.real
= getfloatvalue((*nbr
->nb_float
)(r
));
316 else if (is_complexobject(i
))
317 ci
= ((complexobject
*)i
)->cval
;
319 ci
.real
= getfloatvalue((*nbi
->nb_float
)(i
));
324 return newcomplexobject(cr
);
330 builtin_dir(self
, args
)
337 if (!newgetargs(args
, "|O:dir", &v
))
344 d
= getattr(v
, "__dict__");
346 err_setstr(TypeError
,
347 "dir() argument must have __dict__ attribute");
351 if (is_dictobject(d
)) {
353 if (sortlist(v
) != 0) {
359 v
= newlistobject(0);
371 if (is_instanceobject(v
) || is_instanceobject(w
))
372 return instancebinop(v
, w
, "__divmod__", "__rdivmod__",
374 if (v
->ob_type
->tp_as_number
== NULL
||
375 w
->ob_type
->tp_as_number
== NULL
) {
376 err_setstr(TypeError
,
377 "divmod() requires numeric or class instance arguments");
380 if (coerce(&v
, &w
) != 0)
382 res
= (*v
->ob_type
->tp_as_number
->nb_divmod
)(v
, w
);
389 builtin_divmod(self
, args
)
395 if (!newgetargs(args
, "OO:divmod", &v
, &w
))
397 return do_divmod(v
, w
);
401 builtin_eval(self
, args
)
406 object
*globals
= None
, *locals
= None
;
409 if (!newgetargs(args
, "O|O!O!:eval",
411 &Mappingtype
, &globals
,
412 &Mappingtype
, &locals
))
414 if (globals
== None
) {
415 globals
= getglobals();
417 locals
= getlocals();
419 else if (locals
== None
)
421 if (dictlookup(globals
, "__builtins__") == NULL
) {
422 if (dictinsert(globals
, "__builtins__", getbuiltins()) != 0)
425 if (is_codeobject(cmd
))
426 return eval_code((codeobject
*) cmd
, globals
, locals
);
427 if (!is_stringobject(cmd
)) {
428 err_setstr(TypeError
,
429 "eval() argument 1 must be string or code object");
432 str
= getstringvalue(cmd
);
433 if (strlen(str
) != getstringsize(cmd
)) {
434 err_setstr(ValueError
,
435 "embedded '\\0' in string arg");
438 while (*str
== ' ' || *str
== '\t')
440 return run_string(str
, eval_input
, globals
, locals
);
444 builtin_execfile(self
, args
)
449 object
*globals
= None
, *locals
= None
;
453 if (!newgetargs(args
, "s|O!O!:execfile",
455 &Mappingtype
, &globals
,
456 &Mappingtype
, &locals
))
458 if (globals
== None
) {
459 globals
= getglobals();
461 locals
= getlocals();
463 else if (locals
== None
)
465 if (dictlookup(globals
, "__builtins__") == NULL
) {
466 if (dictinsert(globals
, "__builtins__", getbuiltins()) != 0)
470 fp
= fopen(filename
, "r");
476 res
= run_file(fp
, filename
, file_input
, globals
, locals
);
484 builtin_float(self
, args
)
491 if (!newgetargs(args
, "O:float", &v
))
493 if ((nb
= v
->ob_type
->tp_as_number
) == NULL
||
494 nb
->nb_float
== NULL
) {
495 err_setstr(TypeError
,
496 "float() argument can't be converted to float");
499 return (*nb
->nb_float
)(v
);
503 builtin_getattr(self
, args
)
510 if (!newgetargs(args
, "OS:getattr", &v
, &name
))
512 return getattro(v
, name
);
516 builtin_globals(self
, args
)
522 if (!newgetargs(args
, ""))
530 builtin_hasattr(self
, args
)
537 if (!newgetargs(args
, "OS:hasattr", &v
, &name
))
539 v
= getattro(v
, name
);
542 return newintobject(0L);
545 return newintobject(1L);
549 builtin_id(self
, args
)
555 if (!newgetargs(args
, "O:id", &v
))
557 return newintobject((long)v
);
561 builtin_map(self
, args
)
567 sequence_methods
*sqf
;
571 object
*func
, *result
;
572 sequence
*seqs
= NULL
, *sqp
;
576 n
= gettuplesize(args
);
578 err_setstr(TypeError
, "map() requires at least two args");
582 func
= gettupleitem(args
, 0);
585 if ((seqs
= NEW(sequence
, n
)) == NULL
) {
590 for (len
= 0, i
= 0, sqp
= seqs
; i
< n
; ++i
, ++sqp
) {
593 if ((sqp
->seq
= gettupleitem(args
, i
+ 1)) == NULL
)
596 if (! (sqp
->sqf
= sqp
->seq
->ob_type
->tp_as_sequence
)) {
597 static char errmsg
[] =
598 "argument %d to map() must be a sequence object";
599 char errbuf
[sizeof(errmsg
) + 3];
601 sprintf(errbuf
, errmsg
, i
+2);
602 err_setstr(TypeError
, errbuf
);
606 if ((curlen
= sqp
->len
= (*sqp
->sqf
->sq_length
)(sqp
->seq
)) < 0)
613 if ((result
= (object
*) newlistobject(len
)) == NULL
)
616 /* XXX Special case map(None, single_list) could be more efficient */
618 object
*alist
, *item
, *value
;
621 if (func
== None
&& n
== 1)
624 if ((alist
= newtupleobject(n
)) == NULL
)
628 for (j
= 0, sqp
= seqs
; j
< n
; ++j
, ++sqp
) {
634 item
= (*sqp
->sqf
->sq_item
)(sqp
->seq
, i
);
638 if (err_occurred() == IndexError
) {
654 if (settupleitem(alist
, j
, item
) < 0) {
676 value
= call_object(func
, alist
);
682 if (addlistitem(result
, value
) < 0)
686 if (setlistitem(result
, i
, value
) < 0)
702 builtin_setattr(self
, args
)
710 if (!newgetargs(args
, "OSO:setattr", &v
, &name
, &value
))
712 if (setattro(v
, name
, value
) != 0)
719 builtin_delattr(self
, args
)
726 if (!newgetargs(args
, "OS:delattr", &v
, &name
))
728 if (setattro(v
, name
, (object
*)NULL
) != 0)
735 builtin_hash(self
, args
)
742 if (!newgetargs(args
, "O:hash", &v
))
747 return newintobject(x
);
751 builtin_hex(self
, args
)
758 if (!newgetargs(args
, "O:hex", &v
))
761 if ((nb
= v
->ob_type
->tp_as_number
) == NULL
||
762 nb
->nb_hex
== NULL
) {
763 err_setstr(TypeError
,
764 "hex() argument can't be converted to hex");
767 return (*nb
->nb_hex
)(v
);
770 static object
*builtin_raw_input
PROTO((object
*, object
*));
773 builtin_input(self
, args
)
780 object
*globals
, *locals
;
782 line
= builtin_raw_input(self
, args
);
785 if (!getargs(line
, "s;embedded '\\0' in input line", &str
))
787 while (*str
== ' ' || *str
== '\t')
789 globals
= getglobals();
790 locals
= getlocals();
791 if (dictlookup(globals
, "__builtins__") == NULL
) {
792 if (dictinsert(globals
, "__builtins__", getbuiltins()) != 0)
795 res
= run_string(str
, eval_input
, globals
, locals
);
801 builtin_int(self
, args
)
808 if (!newgetargs(args
, "O:int", &v
))
810 if ((nb
= v
->ob_type
->tp_as_number
) == NULL
||
811 nb
->nb_int
== NULL
) {
812 err_setstr(TypeError
,
813 "int() argument can't be converted to int");
816 return (*nb
->nb_int
)(v
);
820 builtin_len(self
, args
)
828 if (!newgetargs(args
, "O:len", &v
))
831 if (tp
->tp_as_sequence
!= NULL
) {
832 len
= (*tp
->tp_as_sequence
->sq_length
)(v
);
834 else if (tp
->tp_as_mapping
!= NULL
) {
835 len
= (*tp
->tp_as_mapping
->mp_length
)(v
);
838 err_setstr(TypeError
, "len() of unsized object");
844 return newintobject(len
);
848 builtin_locals(self
, args
)
854 if (!newgetargs(args
, ""))
862 builtin_long(self
, args
)
869 if (!newgetargs(args
, "O:long", &v
))
871 if ((nb
= v
->ob_type
->tp_as_number
) == NULL
||
872 nb
->nb_long
== NULL
) {
873 err_setstr(TypeError
,
874 "long() argument can't be converted to long");
877 return (*nb
->nb_long
)(v
);
887 sequence_methods
*sq
;
889 if (gettuplesize(args
) > 1)
891 else if (!newgetargs(args
, "O:min/max", &v
))
893 sq
= v
->ob_type
->tp_as_sequence
;
895 err_setstr(TypeError
, "min() or max() of non-sequence");
900 x
= (*sq
->sq_item
)(v
, i
); /* Implies INCREF */
902 if (err_occurred() == IndexError
) {
912 if (cmpobject(x
, w
) * sign
> 0) {
921 err_setstr(ValueError
, "min() or max() of empty sequence");
930 return min_max(v
, -1);
938 return min_max(v
, 1);
942 builtin_oct(self
, args
)
949 if (!newgetargs(args
, "O:oct", &v
))
951 if (v
== NULL
|| (nb
= v
->ob_type
->tp_as_number
) == NULL
||
952 nb
->nb_oct
== NULL
) {
953 err_setstr(TypeError
,
954 "oct() argument can't be converted to oct");
957 return (*nb
->nb_oct
)(v
);
961 builtin_open(self
, args
)
970 if (!newgetargs(args
, "s|si:open", &name
, &mode
, &bufsize
))
972 f
= newfileobject(name
, mode
);
974 setfilebufsize(f
, bufsize
);
979 builtin_ord(self
, args
)
985 if (!newgetargs(args
, "c:ord", &c
))
987 return newintobject((long)(c
& 0xff));
995 if (is_instanceobject(v
) || is_instanceobject(w
))
996 return instancebinop(v
, w
, "__pow__", "__rpow__", do_pow
);
997 if (v
->ob_type
->tp_as_number
== NULL
||
998 w
->ob_type
->tp_as_number
== NULL
) {
999 err_setstr(TypeError
, "pow() requires numeric arguments");
1003 #ifndef WITHOUT_COMPLEX
1004 !is_complexobject(v
) &&
1006 is_floatobject(w
) && getfloatvalue(v
) < 0.0) {
1007 if (!err_occurred())
1008 err_setstr(ValueError
, "negative number to float power");
1011 if (coerce(&v
, &w
) != 0)
1013 res
= (*v
->ob_type
->tp_as_number
->nb_power
)(v
, w
, None
);
1020 builtin_pow(self
, args
)
1024 object
*v
, *w
, *z
= None
, *res
;
1025 object
*v1
, *z1
, *w2
, *z2
;
1027 if (!newgetargs(args
, "OO|O:pow", &v
, &w
, &z
))
1030 return do_pow(v
, w
);
1031 /* XXX The ternary version doesn't do class instance coercions */
1032 if (is_instanceobject(v
))
1033 return v
->ob_type
->tp_as_number
->nb_power(v
, w
, z
);
1034 if (v
->ob_type
->tp_as_number
== NULL
||
1035 z
->ob_type
->tp_as_number
== NULL
||
1036 w
->ob_type
->tp_as_number
== NULL
) {
1037 err_setstr(TypeError
, "pow() requires numeric arguments");
1040 if (coerce(&v
, &w
) != 0)
1045 if (coerce(&v1
, &z1
) != 0)
1049 if (coerce(&w2
, &z2
) != 0)
1051 res
= (*v1
->ob_type
->tp_as_number
->nb_power
)(v1
, w2
, z2
);
1064 builtin_range(self
, args
)
1068 long ilow
= 0, ihigh
= 0, istep
= 1;
1072 if (gettuplesize(args
) <= 1) {
1073 if (!newgetargs(args
,
1074 "l;range() requires 1-3 int arguments",
1079 if (!newgetargs(args
,
1080 "ll|l;range() requires 1-3 int arguments",
1081 &ilow
, &ihigh
, &istep
))
1085 err_setstr(ValueError
, "zero step for range()");
1088 /* XXX ought to check overflow of subtraction */
1090 n
= (ihigh
- ilow
+ istep
- 1) / istep
;
1092 n
= (ihigh
- ilow
+ istep
+ 1) / istep
;
1095 v
= newlistobject(n
);
1098 for (i
= 0; i
< n
; i
++) {
1099 object
*w
= newintobject(ilow
);
1104 setlistitem(v
, i
, w
);
1111 builtin_xrange(self
, args
)
1115 long ilow
= 0, ihigh
= 0, istep
= 1;
1118 if (gettuplesize(args
) <= 1) {
1119 if (!newgetargs(args
,
1120 "l;xrange() requires 1-3 int arguments",
1125 if (!newgetargs(args
,
1126 "ll|l;xrange() requires 1-3 int arguments",
1127 &ilow
, &ihigh
, &istep
))
1131 err_setstr(ValueError
, "zero step for xrange()");
1134 /* XXX ought to check overflow of subtraction */
1136 n
= (ihigh
- ilow
+ istep
- 1) / istep
;
1138 n
= (ihigh
- ilow
+ istep
+ 1) / istep
;
1141 return newrangeobject(ilow
, n
, istep
, 1);
1144 extern char *my_readline
PROTO((char *));
1147 builtin_raw_input(self
, args
)
1154 if (!newgetargs(args
, "|O:[raw_]input", &v
))
1156 if (getfilefile(sysget("stdin")) == stdin
&&
1157 getfilefile(sysget("stdout")) == stdout
&&
1158 isatty(fileno(stdin
)) && isatty(fileno(stdout
))) {
1167 prompt
= getstringvalue(po
);
1173 s
= my_readline(prompt
);
1176 err_set(KeyboardInterrupt
);
1183 else { /* strip trailing '\n' */
1184 result
= newsizedstringobject(s
, strlen(s
)-1);
1190 f
= sysget("stdout");
1192 err_setstr(RuntimeError
, "lost sys.stdout");
1196 if (writeobject(v
, f
, PRINT_RAW
) != 0)
1199 f
= sysget("stdin");
1201 err_setstr(RuntimeError
, "lost sys.stdin");
1204 return filegetline(f
, -1);
1208 builtin_reduce(self
, args
)
1212 object
*seq
, *func
, *result
= NULL
;
1213 sequence_methods
*sqf
;
1216 if (!newgetargs(args
, "OO|O:reduce", &func
, &seq
, &result
))
1221 if ((sqf
= seq
->ob_type
->tp_as_sequence
) == NULL
) {
1222 err_setstr(TypeError
,
1223 "2nd argument to reduce() must be a sequence object");
1227 if ((args
= newtupleobject(2)) == NULL
)
1230 for (i
= 0; ; ++i
) {
1233 if (args
->ob_refcnt
> 1) {
1235 if ((args
= newtupleobject(2)) == NULL
)
1239 if ((op2
= (*sqf
->sq_item
)(seq
, i
)) == NULL
) {
1240 if (err_occurred() == IndexError
) {
1250 settupleitem(args
, 0, result
);
1251 settupleitem(args
, 1, op2
);
1252 if ((result
= call_object(func
, args
)) == NULL
)
1260 err_setstr(TypeError
,
1261 "reduce of empty sequence with no initial value");
1272 builtin_reload(self
, args
)
1278 if (!newgetargs(args
, "O:reload", &v
))
1280 return reload_module(v
);
1284 builtin_repr(self
, args
)
1290 if (!newgetargs(args
, "O:repr", &v
))
1292 return reprobject(v
);
1296 builtin_round(self
, args
)
1300 extern double floor
PROTO((double));
1301 extern double ceil
PROTO((double));
1307 if (!newgetargs(args
, "d|i:round", &x
, &ndigits
))
1310 for (i
= ndigits
; --i
>= 0; )
1312 for (i
= ndigits
; ++i
<= 0; )
1315 return newfloatobject(floor(x
*f
+ 0.5) / f
);
1317 return newfloatobject(ceil(x
*f
- 0.5) / f
);
1321 builtin_str(self
, args
)
1327 if (!newgetargs(args
, "O:str", &v
))
1329 return strobject(v
);
1333 builtin_tuple(self
, args
)
1338 sequence_methods
*sqf
;
1340 if (!newgetargs(args
, "O:tuple", &v
))
1342 if (is_tupleobject(v
)) {
1346 if (is_listobject(v
))
1347 return listtuple(v
);
1348 if (is_stringobject(v
)) {
1349 int n
= getstringsize(v
);
1350 object
*t
= newtupleobject(n
);
1353 char *p
= getstringvalue(v
);
1354 for (i
= 0; i
< n
; i
++) {
1355 object
*item
= newsizedstringobject(p
+i
, 1);
1361 settupleitem(t
, i
, item
);
1366 /* Generic sequence object */
1367 if ((sqf
= v
->ob_type
->tp_as_sequence
) != NULL
) {
1368 int n
= (*sqf
->sq_length
)(v
);
1373 t
= newtupleobject(n
);
1376 for (i
= 0; i
< n
; i
++) {
1377 object
*item
= (*sqf
->sq_item
)(v
, i
);
1383 settupleitem(t
, i
, item
);
1385 /* XXX Should support indefinite-length sequences */
1388 /* None of the above */
1389 err_setstr(TypeError
, "tuple() argument must be a sequence");
1394 builtin_type(self
, args
)
1400 if (!newgetargs(args
, "O:type", &v
))
1402 v
= (object
*)v
->ob_type
;
1408 builtin_vars(self
, args
)
1415 if (!newgetargs(args
, "|O:vars", &v
))
1420 if (!err_occurred())
1421 err_setstr(SystemError
, "no locals!?");
1427 d
= getattr(v
, "__dict__");
1429 err_setstr(TypeError
,
1430 "vars() argument must have __dict__ attribute");
1437 static struct methodlist builtin_methods
[] = {
1438 {"__import__", builtin___import__
, 1},
1439 {"abs", builtin_abs
, 1},
1440 {"apply", builtin_apply
, 1},
1441 {"callable", builtin_callable
, 1},
1442 {"chr", builtin_chr
, 1},
1443 {"cmp", builtin_cmp
, 1},
1444 {"coerce", builtin_coerce
, 1},
1445 {"compile", builtin_compile
, 1},
1446 #ifndef WITHOUT_COMPLEX
1447 {"complex", builtin_complex
, 1},
1449 {"delattr", builtin_delattr
, 1},
1450 {"dir", builtin_dir
, 1},
1451 {"divmod", builtin_divmod
, 1},
1452 {"eval", builtin_eval
, 1},
1453 {"execfile", builtin_execfile
, 1},
1454 {"filter", builtin_filter
, 1},
1455 {"float", builtin_float
, 1},
1456 {"getattr", builtin_getattr
, 1},
1457 {"globals", builtin_globals
, 1},
1458 {"hasattr", builtin_hasattr
, 1},
1459 {"hash", builtin_hash
, 1},
1460 {"hex", builtin_hex
, 1},
1461 {"id", builtin_id
, 1},
1462 {"input", builtin_input
, 1},
1463 {"int", builtin_int
, 1},
1464 {"len", builtin_len
, 1},
1465 {"locals", builtin_locals
, 1},
1466 {"long", builtin_long
, 1},
1467 {"map", builtin_map
, 1},
1468 {"max", builtin_max
, 1},
1469 {"min", builtin_min
, 1},
1470 {"oct", builtin_oct
, 1},
1471 {"open", builtin_open
, 1},
1472 {"ord", builtin_ord
, 1},
1473 {"pow", builtin_pow
, 1},
1474 {"range", builtin_range
, 1},
1475 {"raw_input", builtin_raw_input
, 1},
1476 {"reduce", builtin_reduce
, 1},
1477 {"reload", builtin_reload
, 1},
1478 {"repr", builtin_repr
, 1},
1479 {"round", builtin_round
, 1},
1480 {"setattr", builtin_setattr
, 1},
1481 {"str", builtin_str
, 1},
1482 {"tuple", builtin_tuple
, 1},
1483 {"type", builtin_type
, 1},
1484 {"vars", builtin_vars
, 1},
1485 {"xrange", builtin_xrange
, 1},
1489 static object
*builtin_mod
;
1490 static object
*builtin_dict
;
1501 return builtin_dict
;
1504 /* Predefined exceptions */
1506 object
*AccessError
;
1507 object
*AttributeError
;
1508 object
*ConflictError
;
1511 object
*ImportError
;
1514 object
*KeyboardInterrupt
;
1515 object
*MemoryError
;
1517 object
*OverflowError
;
1518 object
*RuntimeError
;
1519 object
*SyntaxError
;
1520 object
*SystemError
;
1524 object
*ZeroDivisionError
;
1527 newstdexception(name
)
1530 object
*v
= newstringobject(name
);
1531 if (v
== NULL
|| dictinsert(builtin_dict
, name
, v
) != 0)
1532 fatal("no mem for new standard exception");
1539 AccessError
= newstdexception("AccessError");
1540 AttributeError
= newstdexception("AttributeError");
1541 ConflictError
= newstdexception("ConflictError");
1542 EOFError
= newstdexception("EOFError");
1543 IOError
= newstdexception("IOError");
1544 ImportError
= newstdexception("ImportError");
1545 IndexError
= newstdexception("IndexError");
1546 KeyError
= newstdexception("KeyError");
1547 KeyboardInterrupt
= newstdexception("KeyboardInterrupt");
1548 MemoryError
= newstdexception("MemoryError");
1549 NameError
= newstdexception("NameError");
1550 OverflowError
= newstdexception("OverflowError");
1551 RuntimeError
= newstdexception("RuntimeError");
1552 SyntaxError
= newstdexception("SyntaxError");
1553 SystemError
= newstdexception("SystemError");
1554 SystemExit
= newstdexception("SystemExit");
1555 TypeError
= newstdexception("TypeError");
1556 ValueError
= newstdexception("ValueError");
1557 ZeroDivisionError
= newstdexception("ZeroDivisionError");
1563 builtin_mod
= initmodule("__builtin__", builtin_methods
);
1564 builtin_dict
= getmoduledict(builtin_mod
);
1565 INCREF(builtin_dict
);
1567 (void) dictinsert(builtin_dict
, "None", None
);
1571 /* Helper for filter(): filter a tuple through a function */
1574 filtertuple(func
, tuple
)
1580 int len
= gettuplesize(tuple
);
1587 if ((result
= newtupleobject(len
)) == NULL
)
1590 for (i
= j
= 0; i
< len
; ++i
) {
1591 object
*item
, *good
;
1594 if ((item
= gettupleitem(tuple
, i
)) == NULL
)
1601 object
*arg
= mkvalue("(O)", item
);
1604 good
= call_object(func
, arg
);
1609 ok
= testbool(good
);
1613 if (settupleitem(result
, j
++, item
) < 0)
1618 if (resizetuple(&result
, j
, 0) < 0)
1629 /* Helper for filter(): filter a string through a function */
1632 filterstring(func
, strobj
)
1638 int len
= getstringsize(strobj
);
1641 /* No character is ever false -- share input string */
1645 if ((result
= newsizedstringobject(NULL
, len
)) == NULL
)
1648 for (i
= j
= 0; i
< len
; ++i
) {
1649 object
*item
, *arg
, *good
;
1652 item
= (*strobj
->ob_type
->tp_as_sequence
->sq_item
)(strobj
, i
);
1655 arg
= mkvalue("(O)", item
);
1659 good
= call_object(func
, arg
);
1663 ok
= testbool(good
);
1666 GETSTRINGVALUE((stringobject
*)result
)[j
++] =
1667 GETSTRINGVALUE((stringobject
*)item
)[0];
1670 if (resizestring(&result
, j
) < 0)