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
, *arglist
;
85 if (!newgetargs(args
, "OO:apply", &func
, &arglist
))
87 if (!is_tupleobject(arglist
)) {
88 err_setstr(TypeError
, "apply() 2nd argument must be tuple");
91 return call_object(func
, arglist
);
95 builtin_callable(self
, args
)
101 if (!newgetargs(args
, "O:callable", &v
))
103 return newintobject((long)callable(v
));
107 builtin_filter(self
, args
)
111 object
*func
, *seq
, *result
;
112 sequence_methods
*sqf
;
116 if (!newgetargs(args
, "OO:filter", &func
, &seq
))
119 if (is_stringobject(seq
)) {
120 object
*r
= filterstring(func
, seq
);
124 if (is_tupleobject(seq
)) {
125 object
*r
= filtertuple(func
, seq
);
129 if ((sqf
= seq
->ob_type
->tp_as_sequence
) == NULL
) {
130 err_setstr(TypeError
,
131 "argument 2 to filter() must be a sequence type");
135 if ((len
= (*sqf
->sq_length
)(seq
)) < 0)
138 if (is_listobject(seq
) && seq
->ob_refcnt
== 1) {
143 if ((result
= newlistobject(len
)) == NULL
)
147 for (i
= j
= 0; ; ++i
) {
151 if ((item
= (*sqf
->sq_item
)(seq
, i
)) == NULL
) {
154 if (err_occurred() == IndexError
) {
166 object
*arg
= mkvalue("(O)", item
);
169 good
= call_object(func
, arg
);
180 if (setlistitem(result
, j
++, item
) < 0)
185 if (addlistitem(result
, item
) < 0)
194 if (j
< len
&& setlistslice(result
, j
, len
, NULL
) < 0)
206 builtin_chr(self
, args
)
213 if (!newgetargs(args
, "l:chr", &x
))
215 if (x
< 0 || x
>= 256) {
216 err_setstr(ValueError
, "chr() arg not in range(256)");
220 return newsizedstringobject(s
, 1);
224 builtin_cmp(self
, args
)
230 if (!newgetargs(args
, "OO:cmp", &a
, &b
))
232 return newintobject((long)cmpobject(a
, b
));
236 builtin_coerce(self
, args
)
243 if (!newgetargs(args
, "OO:coerce", &v
, &w
))
245 if (coerce(&v
, &w
) < 0)
247 res
= mkvalue("(OO)", v
, w
);
254 builtin_compile(self
, args
)
263 if (!newgetargs(args
, "sss:compile", &str
, &filename
, &startstr
))
265 if (strcmp(startstr
, "exec") == 0)
267 else if (strcmp(startstr
, "eval") == 0)
270 err_setstr(ValueError
,
271 "compile() mode must be 'exec' or 'eval'");
274 return compile_string(str
, filename
, start
);
278 builtin_dir(self
, args
)
285 if (!newgetargs(args
, "|O:dir", &v
))
292 d
= getattr(v
, "__dict__");
294 err_setstr(TypeError
,
295 "dir() argument must have __dict__ attribute");
299 if (is_dictobject(d
)) {
301 if (sortlist(v
) != 0) {
307 v
= newlistobject(0);
319 if (is_instanceobject(v
) || is_instanceobject(w
))
320 return instancebinop(v
, w
, "__divmod__", "__rdivmod__",
322 if (v
->ob_type
->tp_as_number
== NULL
||
323 w
->ob_type
->tp_as_number
== NULL
) {
324 err_setstr(TypeError
,
325 "divmod() requires numeric or class instance arguments");
328 if (coerce(&v
, &w
) != 0)
330 res
= (*v
->ob_type
->tp_as_number
->nb_divmod
)(v
, w
);
337 builtin_divmod(self
, args
)
343 if (!newgetargs(args
, "OO:divmod", &v
, &w
))
345 return do_divmod(v
, w
);
349 builtin_eval(self
, args
)
354 object
*globals
= None
, *locals
= None
;
357 if (!newgetargs(args
, "O|O!O!:eval",
359 &Mappingtype
, &globals
,
360 &Mappingtype
, &locals
))
362 if (globals
== None
) {
363 globals
= getglobals();
365 locals
= getlocals();
367 else if (locals
== None
)
369 if (dictlookup(globals
, "__builtins__") == NULL
) {
370 if (dictinsert(globals
, "__builtins__", getbuiltins()) != 0)
373 if (is_codeobject(cmd
))
374 return eval_code((codeobject
*) cmd
, globals
, locals
,
375 (object
*)NULL
, (object
*)NULL
);
376 if (!is_stringobject(cmd
)) {
377 err_setstr(TypeError
,
378 "eval() argument 1 must be string or code object");
381 str
= getstringvalue(cmd
);
382 if (strlen(str
) != getstringsize(cmd
)) {
383 err_setstr(ValueError
,
384 "embedded '\\0' in string arg");
387 while (*str
== ' ' || *str
== '\t')
389 return run_string(str
, eval_input
, globals
, locals
);
393 builtin_execfile(self
, args
)
398 object
*globals
= None
, *locals
= None
;
402 if (!newgetargs(args
, "s|O!O!:execfile",
404 &Mappingtype
, &globals
,
405 &Mappingtype
, &locals
))
407 if (globals
== None
) {
408 globals
= getglobals();
410 locals
= getlocals();
412 else if (locals
== None
)
414 if (dictlookup(globals
, "__builtins__") == NULL
) {
415 if (dictinsert(globals
, "__builtins__", getbuiltins()) != 0)
419 fp
= fopen(filename
, "r");
425 res
= run_file(fp
, filename
, file_input
, globals
, locals
);
433 builtin_float(self
, args
)
440 if (!newgetargs(args
, "O:float", &v
))
442 if ((nb
= v
->ob_type
->tp_as_number
) == NULL
||
443 nb
->nb_float
== NULL
) {
444 err_setstr(TypeError
,
445 "float() argument can't be converted to float");
448 return (*nb
->nb_float
)(v
);
452 builtin_getattr(self
, args
)
459 if (!newgetargs(args
, "OS:getattr", &v
, &name
))
461 return getattro(v
, name
);
465 builtin_hasattr(self
, args
)
472 if (!newgetargs(args
, "OS:hasattr", &v
, &name
))
474 v
= getattro(v
, name
);
477 return newintobject(0L);
480 return newintobject(1L);
484 builtin_id(self
, args
)
490 if (!newgetargs(args
, "O:id", &v
))
492 return newintobject((long)v
);
496 builtin_map(self
, args
)
502 sequence_methods
*sqf
;
506 object
*func
, *result
;
507 sequence
*seqs
= NULL
, *sqp
;
511 n
= gettuplesize(args
);
513 err_setstr(TypeError
, "map() requires at least two args");
517 func
= gettupleitem(args
, 0);
520 if ((seqs
= NEW(sequence
, n
)) == NULL
) {
525 for (len
= 0, i
= 0, sqp
= seqs
; i
< n
; ++i
, ++sqp
) {
528 if ((sqp
->seq
= gettupleitem(args
, i
+ 1)) == NULL
)
531 if (! (sqp
->sqf
= sqp
->seq
->ob_type
->tp_as_sequence
)) {
532 static char errmsg
[] =
533 "argument %d to map() must be a sequence object";
534 char errbuf
[sizeof(errmsg
) + 3];
536 sprintf(errbuf
, errmsg
, i
+2);
537 err_setstr(TypeError
, errbuf
);
541 if ((curlen
= sqp
->len
= (*sqp
->sqf
->sq_length
)(sqp
->seq
)) < 0)
548 if ((result
= (object
*) newlistobject(len
)) == NULL
)
551 /* XXX Special case map(None, single_list) could be more efficient */
553 object
*arglist
, *item
, *value
;
556 if (func
== None
&& n
== 1)
559 if ((arglist
= newtupleobject(n
)) == NULL
)
563 for (j
= 0, sqp
= seqs
; j
< n
; ++j
, ++sqp
) {
569 item
= (*sqp
->sqf
->sq_item
)(sqp
->seq
, i
);
573 if (err_occurred() == IndexError
) {
589 if (settupleitem(arglist
, j
, item
) < 0) {
611 value
= call_object(func
, arglist
);
617 if (addlistitem(result
, value
) < 0)
621 if (setlistitem(result
, i
, value
) < 0)
637 builtin_setattr(self
, args
)
645 if (!newgetargs(args
, "OSO:setattr", &v
, &name
, &value
))
647 if (setattro(v
, name
, value
) != 0)
654 builtin_delattr(self
, args
)
661 if (!newgetargs(args
, "OS:delattr", &v
, &name
))
663 if (setattro(v
, name
, (object
*)NULL
) != 0)
670 builtin_hash(self
, args
)
677 if (!newgetargs(args
, "O:hash", &v
))
682 return newintobject(x
);
686 builtin_hex(self
, args
)
693 if (!newgetargs(args
, "O:hex", &v
))
696 if ((nb
= v
->ob_type
->tp_as_number
) == NULL
||
697 nb
->nb_hex
== NULL
) {
698 err_setstr(TypeError
,
699 "hex() argument can't be converted to hex");
702 return (*nb
->nb_hex
)(v
);
705 static object
*builtin_raw_input
PROTO((object
*, object
*));
708 builtin_input(self
, args
)
715 object
*globals
, *locals
;
717 line
= builtin_raw_input(self
, args
);
720 if (!getargs(line
, "s;embedded '\\0' in input line", &str
))
722 while (*str
== ' ' || *str
== '\t')
724 globals
= getglobals();
725 locals
= getlocals();
726 if (dictlookup(globals
, "__builtins__") == NULL
) {
727 if (dictinsert(globals
, "__builtins__", getbuiltins()) != 0)
730 res
= run_string(str
, eval_input
, globals
, locals
);
736 builtin_int(self
, args
)
743 if (!newgetargs(args
, "O:int", &v
))
745 if ((nb
= v
->ob_type
->tp_as_number
) == NULL
||
746 nb
->nb_int
== NULL
) {
747 err_setstr(TypeError
,
748 "int() argument can't be converted to int");
751 return (*nb
->nb_int
)(v
);
755 builtin_len(self
, args
)
763 if (!newgetargs(args
, "O:len", &v
))
766 if (tp
->tp_as_sequence
!= NULL
) {
767 len
= (*tp
->tp_as_sequence
->sq_length
)(v
);
769 else if (tp
->tp_as_mapping
!= NULL
) {
770 len
= (*tp
->tp_as_mapping
->mp_length
)(v
);
773 err_setstr(TypeError
, "len() of unsized object");
779 return newintobject(len
);
783 builtin_long(self
, args
)
790 if (!newgetargs(args
, "O:long", &v
))
792 if ((nb
= v
->ob_type
->tp_as_number
) == NULL
||
793 nb
->nb_long
== NULL
) {
794 err_setstr(TypeError
,
795 "long() argument can't be converted to long");
798 return (*nb
->nb_long
)(v
);
808 sequence_methods
*sq
;
810 if (gettuplesize(args
) > 1)
812 else if (!newgetargs(args
, "O:min/max", &v
))
814 sq
= v
->ob_type
->tp_as_sequence
;
816 err_setstr(TypeError
, "min() or max() of non-sequence");
821 x
= (*sq
->sq_item
)(v
, i
); /* Implies INCREF */
823 if (err_occurred() == IndexError
) {
833 if (cmpobject(x
, w
) * sign
> 0) {
842 err_setstr(ValueError
, "min() or max() of empty sequence");
851 return min_max(v
, -1);
859 return min_max(v
, 1);
863 builtin_oct(self
, args
)
870 if (!newgetargs(args
, "O:oct", &v
))
872 if (v
== NULL
|| (nb
= v
->ob_type
->tp_as_number
) == NULL
||
873 nb
->nb_oct
== NULL
) {
874 err_setstr(TypeError
,
875 "oct() argument can't be converted to oct");
878 return (*nb
->nb_oct
)(v
);
882 builtin_open(self
, args
)
891 if (!newgetargs(args
, "s|si:open", &name
, &mode
, &bufsize
))
893 f
= newfileobject(name
, mode
);
895 setfilebufsize(f
, bufsize
);
900 builtin_ord(self
, args
)
906 if (!newgetargs(args
, "c:ord", &c
))
908 return newintobject((long)(c
& 0xff));
916 if (is_instanceobject(v
) || is_instanceobject(w
))
917 return instancebinop(v
, w
, "__pow__", "__rpow__", do_pow
);
918 if (v
->ob_type
->tp_as_number
== NULL
||
919 w
->ob_type
->tp_as_number
== NULL
) {
920 err_setstr(TypeError
, "pow() requires numeric arguments");
923 if (is_floatobject(w
) && getfloatvalue(v
) < 0.0) {
925 err_setstr(ValueError
, "negative number to float power");
928 if (coerce(&v
, &w
) != 0)
930 res
= (*v
->ob_type
->tp_as_number
->nb_power
)(v
, w
, None
);
937 builtin_pow(self
, args
)
941 object
*v
, *w
, *z
= None
, *res
;
942 object
*v1
, *z1
, *w2
, *z2
;
944 if (!newgetargs(args
, "OO|O:pow", &v
, &w
, &z
))
948 /* XXX The ternary version doesn't do class instance coercions */
949 if (is_instanceobject(v
))
950 return v
->ob_type
->tp_as_number
->nb_power(v
, w
, z
);
951 if (v
->ob_type
->tp_as_number
== NULL
||
952 z
->ob_type
->tp_as_number
== NULL
||
953 w
->ob_type
->tp_as_number
== NULL
) {
954 err_setstr(TypeError
, "pow() requires numeric arguments");
957 if (coerce(&v
, &w
) != 0)
962 if (coerce(&v1
, &z1
) != 0)
966 if (coerce(&w2
, &z2
) != 0)
968 res
= (*v1
->ob_type
->tp_as_number
->nb_power
)(v1
, w2
, z2
);
981 builtin_range(self
, args
)
985 long ilow
= 0, ihigh
= 0, istep
= 1;
989 if (gettuplesize(args
) <= 1) {
990 if (!newgetargs(args
,
991 "l;range() requires 1-3 int arguments",
996 if (!newgetargs(args
,
997 "ll|l;range() requires 1-3 int arguments",
998 &ilow
, &ihigh
, &istep
))
1002 err_setstr(ValueError
, "zero step for range()");
1005 /* XXX ought to check overflow of subtraction */
1007 n
= (ihigh
- ilow
+ istep
- 1) / istep
;
1009 n
= (ihigh
- ilow
+ istep
+ 1) / istep
;
1012 v
= newlistobject(n
);
1015 for (i
= 0; i
< n
; i
++) {
1016 object
*w
= newintobject(ilow
);
1021 setlistitem(v
, i
, w
);
1028 builtin_xrange(self
, args
)
1032 long ilow
= 0, ihigh
= 0, istep
= 1;
1035 if (gettuplesize(args
) <= 1) {
1036 if (!newgetargs(args
,
1037 "l;xrange() requires 1-3 int arguments",
1042 if (!newgetargs(args
,
1043 "ll|l;xrange() requires 1-3 int arguments",
1044 &ilow
, &ihigh
, &istep
))
1048 err_setstr(ValueError
, "zero step for xrange()");
1051 /* XXX ought to check overflow of subtraction */
1053 n
= (ihigh
- ilow
+ istep
- 1) / istep
;
1055 n
= (ihigh
- ilow
+ istep
+ 1) / istep
;
1058 return newrangeobject(ilow
, n
, istep
, 1);
1062 builtin_raw_input(self
, args
)
1069 if (!newgetargs(args
, "|O:[raw_]input", &v
))
1072 f
= sysget("stdout");
1074 err_setstr(RuntimeError
, "lost sys.stdout");
1078 if (writeobject(v
, f
, PRINT_RAW
) != 0)
1081 f
= sysget("stdin");
1083 err_setstr(RuntimeError
, "lost sys.stdin");
1086 return filegetline(f
, -1);
1090 builtin_reduce(self
, args
)
1094 object
*seq
, *func
, *result
= NULL
;
1095 sequence_methods
*sqf
;
1098 if (!newgetargs(args
, "OO|O:reduce", &func
, &seq
, &result
))
1103 if ((sqf
= seq
->ob_type
->tp_as_sequence
) == NULL
) {
1104 err_setstr(TypeError
,
1105 "2nd argument to reduce() must be a sequence object");
1109 if ((args
= newtupleobject(2)) == NULL
)
1112 for (i
= 0; ; ++i
) {
1115 if (args
->ob_refcnt
> 1) {
1117 if ((args
= newtupleobject(2)) == NULL
)
1121 if ((op2
= (*sqf
->sq_item
)(seq
, i
)) == NULL
) {
1122 if (err_occurred() == IndexError
) {
1132 settupleitem(args
, 0, result
);
1133 settupleitem(args
, 1, op2
);
1134 if ((result
= call_object(func
, args
)) == NULL
)
1142 err_setstr(TypeError
,
1143 "reduce of empty sequence with no initial value");
1154 builtin_reload(self
, args
)
1160 if (!newgetargs(args
, "O:reload", &v
))
1162 return reload_module(v
);
1166 builtin_repr(self
, args
)
1172 if (!newgetargs(args
, "O:repr", &v
))
1174 return reprobject(v
);
1178 builtin_round(self
, args
)
1182 extern double floor
PROTO((double));
1183 extern double ceil
PROTO((double));
1189 if (!newgetargs(args
, "d|i:round", &x
, &ndigits
))
1192 for (i
= ndigits
; --i
>= 0; )
1194 for (i
= ndigits
; ++i
<= 0; )
1197 return newfloatobject(floor(x
*f
+ 0.5) / f
);
1199 return newfloatobject(ceil(x
*f
- 0.5) / f
);
1203 builtin_str(self
, args
)
1209 if (!newgetargs(args
, "O:str", &v
))
1211 return strobject(v
);
1215 builtin_tuple(self
, args
)
1220 sequence_methods
*sqf
;
1222 if (!newgetargs(args
, "O:tuple", &v
))
1224 if (is_tupleobject(v
)) {
1228 if (is_listobject(v
))
1229 return listtuple(v
);
1230 if (is_stringobject(v
)) {
1231 int n
= getstringsize(v
);
1232 object
*t
= newtupleobject(n
);
1235 char *p
= getstringvalue(v
);
1236 for (i
= 0; i
< n
; i
++) {
1237 object
*item
= newsizedstringobject(p
+i
, 1);
1243 settupleitem(t
, i
, item
);
1248 /* Generic sequence object */
1249 if ((sqf
= v
->ob_type
->tp_as_sequence
) != NULL
) {
1250 int n
= (*sqf
->sq_length
)(v
);
1255 t
= newtupleobject(n
);
1258 for (i
= 0; i
< n
; i
++) {
1259 object
*item
= (*sqf
->sq_item
)(v
, i
);
1265 settupleitem(t
, i
, item
);
1267 /* XXX Should support indefinite-length sequences */
1270 /* None of the above */
1271 err_setstr(TypeError
, "tuple() argument must be a sequence");
1276 builtin_type(self
, args
)
1282 if (!newgetargs(args
, "O:type", &v
))
1284 v
= (object
*)v
->ob_type
;
1290 builtin_vars(self
, args
)
1297 if (!newgetargs(args
, "|O:vars", &v
))
1304 d
= getattr(v
, "__dict__");
1306 err_setstr(TypeError
,
1307 "vars() argument must have __dict__ attribute");
1314 static struct methodlist builtin_methods
[] = {
1315 {"__import__", builtin___import__
, 1},
1316 {"abs", builtin_abs
, 1},
1317 {"apply", builtin_apply
, 1},
1318 {"callable", builtin_callable
, 1},
1319 {"chr", builtin_chr
, 1},
1320 {"cmp", builtin_cmp
, 1},
1321 {"coerce", builtin_coerce
, 1},
1322 {"compile", builtin_compile
, 1},
1323 {"delattr", builtin_delattr
, 1},
1324 {"dir", builtin_dir
, 1},
1325 {"divmod", builtin_divmod
, 1},
1326 {"eval", builtin_eval
, 1},
1327 {"execfile", builtin_execfile
, 1},
1328 {"filter", builtin_filter
, 1},
1329 {"float", builtin_float
, 1},
1330 {"getattr", builtin_getattr
, 1},
1331 {"hasattr", builtin_hasattr
, 1},
1332 {"hash", builtin_hash
, 1},
1333 {"hex", builtin_hex
, 1},
1334 {"id", builtin_id
, 1},
1335 {"input", builtin_input
, 1},
1336 {"int", builtin_int
, 1},
1337 {"len", builtin_len
, 1},
1338 {"long", builtin_long
, 1},
1339 {"map", builtin_map
, 1},
1340 {"max", builtin_max
, 1},
1341 {"min", builtin_min
, 1},
1342 {"oct", builtin_oct
, 1},
1343 {"open", builtin_open
, 1},
1344 {"ord", builtin_ord
, 1},
1345 {"pow", builtin_pow
, 1},
1346 {"range", builtin_range
, 1},
1347 {"raw_input", builtin_raw_input
, 1},
1348 {"reduce", builtin_reduce
, 1},
1349 {"reload", builtin_reload
, 1},
1350 {"repr", builtin_repr
, 1},
1351 {"round", builtin_round
, 1},
1352 {"setattr", builtin_setattr
, 1},
1353 {"str", builtin_str
, 1},
1354 {"tuple", builtin_tuple
, 1},
1355 {"type", builtin_type
, 1},
1356 {"vars", builtin_vars
, 1},
1357 {"xrange", builtin_xrange
, 1},
1361 static object
*builtin_mod
;
1362 static object
*builtin_dict
;
1373 return builtin_dict
;
1376 /* Predefined exceptions */
1378 object
*AccessError
;
1379 object
*AttributeError
;
1380 object
*ConflictError
;
1383 object
*ImportError
;
1386 object
*KeyboardInterrupt
;
1387 object
*MemoryError
;
1389 object
*OverflowError
;
1390 object
*RuntimeError
;
1391 object
*SyntaxError
;
1392 object
*SystemError
;
1396 object
*ZeroDivisionError
;
1399 newstdexception(name
)
1402 object
*v
= newstringobject(name
);
1403 if (v
== NULL
|| dictinsert(builtin_dict
, name
, v
) != 0)
1404 fatal("no mem for new standard exception");
1411 AccessError
= newstdexception("AccessError");
1412 AttributeError
= newstdexception("AttributeError");
1413 ConflictError
= newstdexception("ConflictError");
1414 EOFError
= newstdexception("EOFError");
1415 IOError
= newstdexception("IOError");
1416 ImportError
= newstdexception("ImportError");
1417 IndexError
= newstdexception("IndexError");
1418 KeyError
= newstdexception("KeyError");
1419 KeyboardInterrupt
= newstdexception("KeyboardInterrupt");
1420 MemoryError
= newstdexception("MemoryError");
1421 NameError
= newstdexception("NameError");
1422 OverflowError
= newstdexception("OverflowError");
1423 RuntimeError
= newstdexception("RuntimeError");
1424 SyntaxError
= newstdexception("SyntaxError");
1425 SystemError
= newstdexception("SystemError");
1426 SystemExit
= newstdexception("SystemExit");
1427 TypeError
= newstdexception("TypeError");
1428 ValueError
= newstdexception("ValueError");
1429 ZeroDivisionError
= newstdexception("ZeroDivisionError");
1435 builtin_mod
= initmodule("__builtin__", builtin_methods
);
1436 builtin_dict
= getmoduledict(builtin_mod
);
1437 INCREF(builtin_dict
);
1439 (void) dictinsert(builtin_dict
, "None", None
);
1443 /* Helper for filter(): filter a tuple through a function */
1446 filtertuple(func
, tuple
)
1452 int len
= gettuplesize(tuple
);
1454 if ((result
= newtupleobject(len
)) == NULL
)
1457 for (i
= j
= 0; i
< len
; ++i
) {
1458 object
*item
, *good
;
1461 if ((item
= gettupleitem(tuple
, i
)) == NULL
)
1468 object
*arg
= mkvalue("(O)", item
);
1471 good
= call_object(func
, arg
);
1476 ok
= testbool(good
);
1480 if (settupleitem(result
, j
++, item
) < 0)
1485 if (resizetuple(&result
, j
, 0) < 0)
1496 /* Helper for filter(): filter a string through a function */
1499 filterstring(func
, strobj
)
1505 int len
= getstringsize(strobj
);
1508 /* No character is ever false -- share input string */
1512 if ((result
= newsizedstringobject(NULL
, len
)) == NULL
)
1515 for (i
= j
= 0; i
< len
; ++i
) {
1516 object
*item
, *arg
, *good
;
1519 item
= (*strobj
->ob_type
->tp_as_sequence
->sq_item
)(strobj
, i
);
1522 arg
= mkvalue("(O)", item
);
1526 good
= call_object(func
, arg
);
1530 ok
= testbool(good
);
1533 GETSTRINGVALUE((stringobject
*)result
)[j
++] =
1534 GETSTRINGVALUE((stringobject
*)item
)[0];
1537 if (resizestring(&result
, j
) < 0)