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 "bltinmodule.h"
37 static object
*filterstring
PROTO((object
*, object
*));
38 static object
*filtertuple
PROTO((object
*, object
*));
41 builtin___import__(self
, args
)
46 object
*globals
= NULL
;
47 object
*locals
= NULL
;
48 object
*fromlist
= NULL
;
50 if (!newgetargs(args
, "s|OOO:__import__",
51 &name
, &globals
, &locals
, &fromlist
))
53 return import_module(name
);
58 builtin_abs(self
, args
)
65 if (!newgetargs(args
, "O:abs", &v
))
67 if ((nm
= v
->ob_type
->tp_as_number
) == NULL
) {
68 err_setstr(TypeError
, "abs() requires numeric argument");
71 return (*nm
->nb_absolute
)(v
);
75 builtin_apply(self
, args
)
79 object
*func
, *alist
= NULL
, *kwdict
= NULL
;
81 if (!newgetargs(args
, "O|OO:apply", &func
, &alist
, &kwdict
))
83 if (alist
!= NULL
&& !is_tupleobject(alist
)) {
84 err_setstr(TypeError
, "apply() 2nd argument must be tuple");
87 if (kwdict
!= NULL
&& !is_dictobject(kwdict
)) {
89 "apply() 3rd argument must be dictionary");
92 return PyEval_CallObjectWithKeywords(func
, alist
, kwdict
);
96 builtin_callable(self
, args
)
102 if (!newgetargs(args
, "O:callable", &v
))
104 return newintobject((long)callable(v
));
108 builtin_filter(self
, args
)
112 object
*func
, *seq
, *result
;
113 sequence_methods
*sqf
;
117 if (!newgetargs(args
, "OO:filter", &func
, &seq
))
120 if (is_stringobject(seq
)) {
121 object
*r
= filterstring(func
, seq
);
125 if (is_tupleobject(seq
)) {
126 object
*r
= filtertuple(func
, seq
);
130 if ((sqf
= seq
->ob_type
->tp_as_sequence
) == NULL
) {
131 err_setstr(TypeError
,
132 "argument 2 to filter() must be a sequence type");
136 if ((len
= (*sqf
->sq_length
)(seq
)) < 0)
139 if (is_listobject(seq
) && seq
->ob_refcnt
== 1) {
144 if ((result
= newlistobject(len
)) == NULL
)
148 for (i
= j
= 0; ; ++i
) {
152 if ((item
= (*sqf
->sq_item
)(seq
, i
)) == NULL
) {
155 if (err_occurred() == IndexError
) {
167 object
*arg
= mkvalue("(O)", item
);
170 good
= call_object(func
, arg
);
181 if (setlistitem(result
, j
++, item
) < 0)
186 if (addlistitem(result
, item
) < 0)
195 if (j
< len
&& setlistslice(result
, j
, len
, NULL
) < 0)
207 builtin_chr(self
, args
)
214 if (!newgetargs(args
, "l:chr", &x
))
216 if (x
< 0 || x
>= 256) {
217 err_setstr(ValueError
, "chr() arg not in range(256)");
221 return newsizedstringobject(s
, 1);
225 builtin_cmp(self
, args
)
231 if (!newgetargs(args
, "OO:cmp", &a
, &b
))
233 return newintobject((long)cmpobject(a
, b
));
237 builtin_coerce(self
, args
)
244 if (!newgetargs(args
, "OO:coerce", &v
, &w
))
246 if (coerce(&v
, &w
) < 0)
248 res
= mkvalue("(OO)", v
, w
);
255 builtin_compile(self
, args
)
264 if (!newgetargs(args
, "sss:compile", &str
, &filename
, &startstr
))
266 if (strcmp(startstr
, "exec") == 0)
268 else if (strcmp(startstr
, "eval") == 0)
270 else if (strcmp(startstr
, "single") == 0)
271 start
= single_input
;
273 err_setstr(ValueError
,
274 "compile() mode must be 'exec' or 'eval' or 'single'");
277 return compile_string(str
, filename
, start
);
280 #ifndef WITHOUT_COMPLEX
283 builtin_complex(self
, args
)
288 number_methods
*nbr
, *nbi
;
292 if (!newgetargs(args
, "O|O:complex", &r
, &i
))
294 if ((nbr
= r
->ob_type
->tp_as_number
) == NULL
||
295 nbr
->nb_float
== NULL
|| (i
!= NULL
&&
296 ((nbi
= i
->ob_type
->tp_as_number
) == NULL
||
297 nbi
->nb_float
== NULL
))) {
298 err_setstr(TypeError
,
299 "complex() argument can't be converted to complex");
302 if (is_complexobject(r
))
303 cr
= ((complexobject
*)r
)->cval
;
305 cr
.real
= getfloatvalue((*nbr
->nb_float
)(r
));
312 else if (is_complexobject(i
))
313 ci
= ((complexobject
*)i
)->cval
;
315 ci
.real
= getfloatvalue((*nbi
->nb_float
)(i
));
320 return newcomplexobject(cr
);
326 builtin_dir(self
, args
)
333 if (!newgetargs(args
, "|O:dir", &v
))
340 d
= getattr(v
, "__dict__");
342 err_setstr(TypeError
,
343 "dir() argument must have __dict__ attribute");
347 if (is_dictobject(d
)) {
349 if (sortlist(v
) != 0) {
355 v
= PyObject_CallMethod(d
, "keys", NULL
);
358 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_list(self
, args
)
853 sequence_methods
*sqf
;
855 if (!newgetargs(args
, "O:list", &v
))
857 if ((sqf
= v
->ob_type
->tp_as_sequence
) != NULL
) {
858 int n
= (*sqf
->sq_length
)(v
);
863 l
= newlistobject(n
);
866 for (i
= 0; i
< n
; i
++) {
867 object
*item
= (*sqf
->sq_item
)(v
, i
);
873 setlistitem(l
, i
, item
);
875 /* XXX Should support indefinite-length sequences */
878 err_setstr(TypeError
, "list() argument must be a sequence");
883 builtin_locals(self
, args
)
889 if (!newgetargs(args
, ""))
897 builtin_long(self
, args
)
904 if (!newgetargs(args
, "O:long", &v
))
906 if ((nb
= v
->ob_type
->tp_as_number
) == NULL
||
907 nb
->nb_long
== NULL
) {
908 err_setstr(TypeError
,
909 "long() argument can't be converted to long");
912 return (*nb
->nb_long
)(v
);
922 sequence_methods
*sq
;
924 if (gettuplesize(args
) > 1)
926 else if (!newgetargs(args
, "O:min/max", &v
))
928 sq
= v
->ob_type
->tp_as_sequence
;
930 err_setstr(TypeError
, "min() or max() of non-sequence");
935 x
= (*sq
->sq_item
)(v
, i
); /* Implies INCREF */
937 if (err_occurred() == IndexError
) {
947 if (cmpobject(x
, w
) * sign
> 0) {
956 err_setstr(ValueError
, "min() or max() of empty sequence");
965 return min_max(v
, -1);
973 return min_max(v
, 1);
977 builtin_oct(self
, args
)
984 if (!newgetargs(args
, "O:oct", &v
))
986 if (v
== NULL
|| (nb
= v
->ob_type
->tp_as_number
) == NULL
||
987 nb
->nb_oct
== NULL
) {
988 err_setstr(TypeError
,
989 "oct() argument can't be converted to oct");
992 return (*nb
->nb_oct
)(v
);
996 builtin_open(self
, args
)
1005 if (!newgetargs(args
, "s|si:open", &name
, &mode
, &bufsize
))
1007 f
= newfileobject(name
, mode
);
1009 setfilebufsize(f
, bufsize
);
1014 builtin_ord(self
, args
)
1020 if (!newgetargs(args
, "c:ord", &c
))
1022 return newintobject((long)(c
& 0xff));
1030 if (is_instanceobject(v
) || is_instanceobject(w
))
1031 return instancebinop(v
, w
, "__pow__", "__rpow__", do_pow
);
1032 if (v
->ob_type
->tp_as_number
== NULL
||
1033 w
->ob_type
->tp_as_number
== NULL
) {
1034 err_setstr(TypeError
, "pow() requires numeric arguments");
1038 #ifndef WITHOUT_COMPLEX
1039 !is_complexobject(v
) &&
1041 is_floatobject(w
) && getfloatvalue(v
) < 0.0) {
1042 if (!err_occurred())
1043 err_setstr(ValueError
, "negative number to float power");
1046 if (coerce(&v
, &w
) != 0)
1048 res
= (*v
->ob_type
->tp_as_number
->nb_power
)(v
, w
, None
);
1055 builtin_pow(self
, args
)
1059 object
*v
, *w
, *z
= None
, *res
;
1060 object
*v1
, *z1
, *w2
, *z2
;
1062 if (!newgetargs(args
, "OO|O:pow", &v
, &w
, &z
))
1065 return do_pow(v
, w
);
1066 /* XXX The ternary version doesn't do class instance coercions */
1067 if (is_instanceobject(v
))
1068 return v
->ob_type
->tp_as_number
->nb_power(v
, w
, z
);
1069 if (v
->ob_type
->tp_as_number
== NULL
||
1070 z
->ob_type
->tp_as_number
== NULL
||
1071 w
->ob_type
->tp_as_number
== NULL
) {
1072 err_setstr(TypeError
, "pow() requires numeric arguments");
1075 if (coerce(&v
, &w
) != 0)
1080 if (coerce(&v1
, &z1
) != 0)
1084 if (coerce(&w2
, &z2
) != 0)
1086 res
= (*v1
->ob_type
->tp_as_number
->nb_power
)(v1
, w2
, z2
);
1099 builtin_range(self
, args
)
1103 long ilow
= 0, ihigh
= 0, istep
= 1;
1107 if (gettuplesize(args
) <= 1) {
1108 if (!newgetargs(args
,
1109 "l;range() requires 1-3 int arguments",
1114 if (!newgetargs(args
,
1115 "ll|l;range() requires 1-3 int arguments",
1116 &ilow
, &ihigh
, &istep
))
1120 err_setstr(ValueError
, "zero step for range()");
1123 /* XXX ought to check overflow of subtraction */
1125 n
= (ihigh
- ilow
+ istep
- 1) / istep
;
1127 n
= (ihigh
- ilow
+ istep
+ 1) / istep
;
1130 v
= newlistobject(n
);
1133 for (i
= 0; i
< n
; i
++) {
1134 object
*w
= newintobject(ilow
);
1139 setlistitem(v
, i
, w
);
1146 builtin_xrange(self
, args
)
1150 long ilow
= 0, ihigh
= 0, istep
= 1;
1153 if (gettuplesize(args
) <= 1) {
1154 if (!newgetargs(args
,
1155 "l;xrange() requires 1-3 int arguments",
1160 if (!newgetargs(args
,
1161 "ll|l;xrange() requires 1-3 int arguments",
1162 &ilow
, &ihigh
, &istep
))
1166 err_setstr(ValueError
, "zero step for xrange()");
1169 /* XXX ought to check overflow of subtraction */
1171 n
= (ihigh
- ilow
+ istep
- 1) / istep
;
1173 n
= (ihigh
- ilow
+ istep
+ 1) / istep
;
1176 return newrangeobject(ilow
, n
, istep
, 1);
1179 extern char *my_readline
PROTO((char *));
1182 builtin_raw_input(self
, args
)
1189 if (!newgetargs(args
, "|O:[raw_]input", &v
))
1191 if (getfilefile(sysget("stdin")) == stdin
&&
1192 getfilefile(sysget("stdout")) == stdout
&&
1193 isatty(fileno(stdin
)) && isatty(fileno(stdout
))) {
1202 prompt
= getstringvalue(po
);
1208 s
= my_readline(prompt
);
1211 err_set(KeyboardInterrupt
);
1218 else { /* strip trailing '\n' */
1219 result
= newsizedstringobject(s
, strlen(s
)-1);
1225 f
= sysget("stdout");
1227 err_setstr(RuntimeError
, "lost sys.stdout");
1231 if (writeobject(v
, f
, PRINT_RAW
) != 0)
1234 f
= sysget("stdin");
1236 err_setstr(RuntimeError
, "lost sys.stdin");
1239 return filegetline(f
, -1);
1243 builtin_reduce(self
, args
)
1247 object
*seq
, *func
, *result
= NULL
;
1248 sequence_methods
*sqf
;
1251 if (!newgetargs(args
, "OO|O:reduce", &func
, &seq
, &result
))
1256 if ((sqf
= seq
->ob_type
->tp_as_sequence
) == NULL
) {
1257 err_setstr(TypeError
,
1258 "2nd argument to reduce() must be a sequence object");
1262 if ((args
= newtupleobject(2)) == NULL
)
1265 for (i
= 0; ; ++i
) {
1268 if (args
->ob_refcnt
> 1) {
1270 if ((args
= newtupleobject(2)) == NULL
)
1274 if ((op2
= (*sqf
->sq_item
)(seq
, i
)) == NULL
) {
1275 if (err_occurred() == IndexError
) {
1285 settupleitem(args
, 0, result
);
1286 settupleitem(args
, 1, op2
);
1287 if ((result
= call_object(func
, args
)) == NULL
)
1295 err_setstr(TypeError
,
1296 "reduce of empty sequence with no initial value");
1307 builtin_reload(self
, args
)
1313 if (!newgetargs(args
, "O:reload", &v
))
1315 return reload_module(v
);
1319 builtin_repr(self
, args
)
1325 if (!newgetargs(args
, "O:repr", &v
))
1327 return reprobject(v
);
1331 builtin_round(self
, args
)
1335 extern double floor
PROTO((double));
1336 extern double ceil
PROTO((double));
1342 if (!newgetargs(args
, "d|i:round", &x
, &ndigits
))
1345 for (i
= ndigits
; --i
>= 0; )
1347 for (i
= ndigits
; ++i
<= 0; )
1350 return newfloatobject(floor(x
*f
+ 0.5) / f
);
1352 return newfloatobject(ceil(x
*f
- 0.5) / f
);
1356 builtin_str(self
, args
)
1362 if (!newgetargs(args
, "O:str", &v
))
1364 return strobject(v
);
1368 builtin_tuple(self
, args
)
1373 sequence_methods
*sqf
;
1375 if (!newgetargs(args
, "O:tuple", &v
))
1377 if (is_tupleobject(v
)) {
1381 if (is_listobject(v
))
1382 return listtuple(v
);
1383 if (is_stringobject(v
)) {
1384 int n
= getstringsize(v
);
1385 object
*t
= newtupleobject(n
);
1388 char *p
= getstringvalue(v
);
1389 for (i
= 0; i
< n
; i
++) {
1390 object
*item
= newsizedstringobject(p
+i
, 1);
1396 settupleitem(t
, i
, item
);
1401 /* Generic sequence object */
1402 if ((sqf
= v
->ob_type
->tp_as_sequence
) != NULL
) {
1403 int n
= (*sqf
->sq_length
)(v
);
1408 t
= newtupleobject(n
);
1411 for (i
= 0; i
< n
; i
++) {
1412 object
*item
= (*sqf
->sq_item
)(v
, i
);
1418 settupleitem(t
, i
, item
);
1420 /* XXX Should support indefinite-length sequences */
1423 /* None of the above */
1424 err_setstr(TypeError
, "tuple() argument must be a sequence");
1429 builtin_type(self
, args
)
1435 if (!newgetargs(args
, "O:type", &v
))
1437 v
= (object
*)v
->ob_type
;
1443 builtin_vars(self
, args
)
1450 if (!newgetargs(args
, "|O:vars", &v
))
1455 if (!err_occurred())
1456 err_setstr(SystemError
, "no locals!?");
1462 d
= getattr(v
, "__dict__");
1464 err_setstr(TypeError
,
1465 "vars() argument must have __dict__ attribute");
1472 static struct methodlist builtin_methods
[] = {
1473 {"__import__", builtin___import__
, 1},
1474 {"abs", builtin_abs
, 1},
1475 {"apply", builtin_apply
, 1},
1476 {"callable", builtin_callable
, 1},
1477 {"chr", builtin_chr
, 1},
1478 {"cmp", builtin_cmp
, 1},
1479 {"coerce", builtin_coerce
, 1},
1480 {"compile", builtin_compile
, 1},
1481 #ifndef WITHOUT_COMPLEX
1482 {"complex", builtin_complex
, 1},
1484 {"delattr", builtin_delattr
, 1},
1485 {"dir", builtin_dir
, 1},
1486 {"divmod", builtin_divmod
, 1},
1487 {"eval", builtin_eval
, 1},
1488 {"execfile", builtin_execfile
, 1},
1489 {"filter", builtin_filter
, 1},
1490 {"float", builtin_float
, 1},
1491 {"getattr", builtin_getattr
, 1},
1492 {"globals", builtin_globals
, 1},
1493 {"hasattr", builtin_hasattr
, 1},
1494 {"hash", builtin_hash
, 1},
1495 {"hex", builtin_hex
, 1},
1496 {"id", builtin_id
, 1},
1497 {"input", builtin_input
, 1},
1498 {"int", builtin_int
, 1},
1499 {"len", builtin_len
, 1},
1500 {"list", builtin_list
, 1},
1501 {"locals", builtin_locals
, 1},
1502 {"long", builtin_long
, 1},
1503 {"map", builtin_map
, 1},
1504 {"max", builtin_max
, 1},
1505 {"min", builtin_min
, 1},
1506 {"oct", builtin_oct
, 1},
1507 {"open", builtin_open
, 1},
1508 {"ord", builtin_ord
, 1},
1509 {"pow", builtin_pow
, 1},
1510 {"range", builtin_range
, 1},
1511 {"raw_input", builtin_raw_input
, 1},
1512 {"reduce", builtin_reduce
, 1},
1513 {"reload", builtin_reload
, 1},
1514 {"repr", builtin_repr
, 1},
1515 {"round", builtin_round
, 1},
1516 {"setattr", builtin_setattr
, 1},
1517 {"str", builtin_str
, 1},
1518 {"tuple", builtin_tuple
, 1},
1519 {"type", builtin_type
, 1},
1520 {"vars", builtin_vars
, 1},
1521 {"xrange", builtin_xrange
, 1},
1525 static object
*builtin_mod
;
1526 static object
*builtin_dict
;
1537 return builtin_dict
;
1540 /* Predefined exceptions */
1542 object
*AccessError
;
1543 object
*AttributeError
;
1544 object
*ConflictError
;
1547 object
*ImportError
;
1550 object
*KeyboardInterrupt
;
1551 object
*MemoryError
;
1553 object
*OverflowError
;
1554 object
*RuntimeError
;
1555 object
*SyntaxError
;
1556 object
*SystemError
;
1560 object
*ZeroDivisionError
;
1563 newstdexception(name
)
1566 object
*v
= newstringobject(name
);
1567 if (v
== NULL
|| dictinsert(builtin_dict
, name
, v
) != 0)
1568 fatal("no mem for new standard exception");
1575 AccessError
= newstdexception("AccessError");
1576 AttributeError
= newstdexception("AttributeError");
1577 ConflictError
= newstdexception("ConflictError");
1578 EOFError
= newstdexception("EOFError");
1579 IOError
= newstdexception("IOError");
1580 ImportError
= newstdexception("ImportError");
1581 IndexError
= newstdexception("IndexError");
1582 KeyError
= newstdexception("KeyError");
1583 KeyboardInterrupt
= newstdexception("KeyboardInterrupt");
1584 MemoryError
= newstdexception("MemoryError");
1585 NameError
= newstdexception("NameError");
1586 OverflowError
= newstdexception("OverflowError");
1587 RuntimeError
= newstdexception("RuntimeError");
1588 SyntaxError
= newstdexception("SyntaxError");
1589 SystemError
= newstdexception("SystemError");
1590 SystemExit
= newstdexception("SystemExit");
1591 TypeError
= newstdexception("TypeError");
1592 ValueError
= newstdexception("ValueError");
1593 ZeroDivisionError
= newstdexception("ZeroDivisionError");
1599 builtin_mod
= initmodule("__builtin__", builtin_methods
);
1600 builtin_dict
= getmoduledict(builtin_mod
);
1601 INCREF(builtin_dict
);
1603 (void) dictinsert(builtin_dict
, "None", None
);
1607 /* Helper for filter(): filter a tuple through a function */
1610 filtertuple(func
, tuple
)
1616 int len
= gettuplesize(tuple
);
1623 if ((result
= newtupleobject(len
)) == NULL
)
1626 for (i
= j
= 0; i
< len
; ++i
) {
1627 object
*item
, *good
;
1630 if ((item
= gettupleitem(tuple
, i
)) == NULL
)
1637 object
*arg
= mkvalue("(O)", item
);
1640 good
= call_object(func
, arg
);
1645 ok
= testbool(good
);
1649 if (settupleitem(result
, j
++, item
) < 0)
1654 if (resizetuple(&result
, j
, 0) < 0)
1665 /* Helper for filter(): filter a string through a function */
1668 filterstring(func
, strobj
)
1674 int len
= getstringsize(strobj
);
1677 /* No character is ever false -- share input string */
1681 if ((result
= newsizedstringobject(NULL
, len
)) == NULL
)
1684 for (i
= j
= 0; i
< len
; ++i
) {
1685 object
*item
, *arg
, *good
;
1688 item
= (*strobj
->ob_type
->tp_as_sequence
->sq_item
)(strobj
, i
);
1691 arg
= mkvalue("(O)", item
);
1695 good
= call_object(func
, arg
);
1699 ok
= testbool(good
);
1702 GETSTRINGVALUE((stringobject
*)result
)[j
++] =
1703 GETSTRINGVALUE((stringobject
*)item
)[0];
1706 if (resizestring(&result
, j
) < 0)