blob: 5bdf5d98fdf51d9f075f7db55feeb20460564dfd [file] [log] [blame]
Guido van Rossumf70e43a1991-02-19 12:39:46 +00001/***********************************************************
2Copyright 1991 by Stichting Mathematisch Centrum, Amsterdam, The
3Netherlands.
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
Guido van Rossum3f5da241990-12-20 15:06:42 +000025/* Built-in functions */
26
27#include "allobjects.h"
28
29#include "node.h"
30#include "graminit.h"
31#include "errcode.h"
32#include "sysmodule.h"
Guido van Rossum86cd6e61991-01-21 15:12:35 +000033#include "bltinmodule.h"
Guido van Rossum3f5da241990-12-20 15:06:42 +000034#include "import.h"
35#include "pythonrun.h"
36#include "compile.h" /* For ceval.h */
37#include "ceval.h"
38#include "modsupport.h"
39
Guido van Rossum006bcd41991-10-24 14:54:44 +000040/* Should be in longobject.h */
41extern stringobject *long_format PROTO((object *, int));
42
Guido van Rossum3f5da241990-12-20 15:06:42 +000043static object *
44builtin_abs(self, v)
45 object *self;
46 object *v;
47{
Guido van Rossumd4905451991-05-05 20:00:36 +000048 number_methods *nm;
49 if (v == NULL || (nm = v->ob_type->tp_as_number) == NULL) {
50 err_setstr(TypeError, "abs() requires numeric argument");
51 return NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +000052 }
Guido van Rossumd4905451991-05-05 20:00:36 +000053 return (*nm->nb_absolute)(v);
Guido van Rossum3f5da241990-12-20 15:06:42 +000054}
55
56static object *
Guido van Rossumc02e15c1991-12-16 13:03:00 +000057builtin_apply(self, v)
58 object *self;
59 object *v;
60{
61 object *func, *args;
62 if (v == NULL || !is_tupleobject(v) || gettuplesize(v) != 2) {
63 err_setstr(TypeError, "apply() requires (func,args)");
64 return NULL;
65 }
66 func = gettupleitem(v, 0);
67 args = gettupleitem(v, 1);
68 return call_object(func, args);
69}
70
71static object *
Guido van Rossum3f5da241990-12-20 15:06:42 +000072builtin_chr(self, v)
73 object *self;
74 object *v;
75{
76 long x;
77 char s[1];
78 if (v == NULL || !is_intobject(v)) {
Guido van Rossum006bcd41991-10-24 14:54:44 +000079 err_setstr(TypeError, "chr() requires int argument");
Guido van Rossum3f5da241990-12-20 15:06:42 +000080 return NULL;
81 }
82 x = getintvalue(v);
83 if (x < 0 || x >= 256) {
Guido van Rossum50afb7a1991-12-10 13:52:31 +000084 err_setstr(ValueError, "chr() arg not in range(256)");
Guido van Rossum3f5da241990-12-20 15:06:42 +000085 return NULL;
86 }
87 s[0] = x;
88 return newsizedstringobject(s, 1);
89}
90
91static object *
92builtin_dir(self, v)
93 object *self;
94 object *v;
95{
96 object *d;
97 if (v == NULL) {
98 d = getlocals();
Guido van Rossumdc8a1081991-10-20 20:11:03 +000099 INCREF(d);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000100 }
101 else {
Guido van Rossumdc8a1081991-10-20 20:11:03 +0000102 d = getattr(v, "__dict__");
103 if (d == NULL) {
Guido van Rossum3f5da241990-12-20 15:06:42 +0000104 err_setstr(TypeError,
Guido van Rossum006bcd41991-10-24 14:54:44 +0000105 "dir() argument must have __dict__ attribute");
Guido van Rossum3f5da241990-12-20 15:06:42 +0000106 return NULL;
107 }
Guido van Rossum3f5da241990-12-20 15:06:42 +0000108 }
Guido van Rossum006bcd41991-10-24 14:54:44 +0000109 if (is_dictobject(d)) {
Guido van Rossumdc8a1081991-10-20 20:11:03 +0000110 v = getdictkeys(d);
111 if (sortlist(v) != 0) {
112 DECREF(v);
113 v = NULL;
114 }
115 }
Guido van Rossum006bcd41991-10-24 14:54:44 +0000116 else {
117 v = newlistobject(0);
118 }
Guido van Rossumdc8a1081991-10-20 20:11:03 +0000119 DECREF(d);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000120 return v;
121}
122
123static object *
Guido van Rossumc6bb8f71991-07-01 18:42:41 +0000124builtin_divmod(self, args)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000125 object *self;
Guido van Rossumc6bb8f71991-07-01 18:42:41 +0000126 object *args;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000127{
Guido van Rossumc6bb8f71991-07-01 18:42:41 +0000128 object *v, *w, *x;
129 if (args == NULL || !is_tupleobject(args) || gettuplesize(args) != 2) {
Guido van Rossumd4905451991-05-05 20:00:36 +0000130 err_setstr(TypeError, "divmod() requires 2 arguments");
Guido van Rossum3f5da241990-12-20 15:06:42 +0000131 return NULL;
132 }
Guido van Rossumc6bb8f71991-07-01 18:42:41 +0000133 v = gettupleitem(args, 0);
134 w = gettupleitem(args, 1);
135 if (v->ob_type->tp_as_number == NULL ||
136 w->ob_type->tp_as_number == NULL) {
Guido van Rossumd4905451991-05-05 20:00:36 +0000137 err_setstr(TypeError, "divmod() requires numeric arguments");
Guido van Rossum3f5da241990-12-20 15:06:42 +0000138 return NULL;
139 }
Guido van Rossumc6bb8f71991-07-01 18:42:41 +0000140 if (coerce(&v, &w) != 0)
141 return NULL;
142 x = (*v->ob_type->tp_as_number->nb_divmod)(v, w);
143 DECREF(v);
144 DECREF(w);
145 return x;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000146}
147
148static object *
149exec_eval(v, start)
150 object *v;
151 int start;
152{
153 object *str = NULL, *globals = NULL, *locals = NULL;
154 int n;
155 if (v != NULL) {
156 if (is_stringobject(v))
157 str = v;
158 else if (is_tupleobject(v) &&
159 ((n = gettuplesize(v)) == 2 || n == 3)) {
160 str = gettupleitem(v, 0);
161 globals = gettupleitem(v, 1);
162 if (n == 3)
163 locals = gettupleitem(v, 2);
164 }
165 }
166 if (str == NULL || !is_stringobject(str) ||
167 globals != NULL && !is_dictobject(globals) ||
168 locals != NULL && !is_dictobject(locals)) {
169 err_setstr(TypeError,
170 "exec/eval arguments must be string[,dict[,dict]]");
171 return NULL;
172 }
173 return run_string(getstringvalue(str), start, globals, locals);
174}
175
176static object *
177builtin_eval(self, v)
178 object *self;
179 object *v;
180{
181 return exec_eval(v, eval_input);
182}
183
184static object *
185builtin_exec(self, v)
186 object *self;
187 object *v;
188{
189 return exec_eval(v, file_input);
190}
191
192static object *
193builtin_float(self, v)
194 object *self;
195 object *v;
196{
197 if (v == NULL) {
198 /* */
199 }
Guido van Rossum3f5da241990-12-20 15:06:42 +0000200 else if (is_intobject(v)) {
201 long x = getintvalue(v);
202 return newfloatobject((double)x);
203 }
Guido van Rossumd4905451991-05-05 20:00:36 +0000204 else if (is_longobject(v)) {
Guido van Rossumd4905451991-05-05 20:00:36 +0000205 return newfloatobject(dgetlongvalue(v));
206 }
207 else if (is_floatobject(v)) {
208 INCREF(v);
209 return v;
210 }
211 err_setstr(TypeError, "float() argument must be int, long or float");
Guido van Rossum3f5da241990-12-20 15:06:42 +0000212 return NULL;
213}
214
215static object *
Guido van Rossum006bcd41991-10-24 14:54:44 +0000216builtin_hex(self, v)
217 object *self;
218 object *v;
219{
220 if (v != NULL) {
221 if (is_intobject(v)) {
222 char buf[20];
223 long x = getintvalue(v);
224 if (x >= 0)
225 sprintf(buf, "0x%lx", x);
226 else
227 sprintf(buf, "-0x%lx", -x);
228 return newstringobject(buf);
229 }
230 if (is_longobject(v)) {
231 return (object *) long_format(v, 16);
232 }
233 }
234 err_setstr(TypeError, "hex() requires int/long argument");
235 return NULL;
236}
237
238static object *
Guido van Rossum3f5da241990-12-20 15:06:42 +0000239builtin_input(self, v)
240 object *self;
241 object *v;
242{
243 FILE *in = sysgetfile("stdin", stdin);
244 FILE *out = sysgetfile("stdout", stdout);
245 node *n;
246 int err;
247 object *m, *d;
248 flushline();
Guido van Rossum90933611991-06-07 16:10:43 +0000249 if (v != NULL) {
250 if (printobject(v, out, PRINT_RAW) != 0)
251 return NULL;
252 }
Guido van Rossum3f5da241990-12-20 15:06:42 +0000253 m = add_module("__main__");
254 d = getmoduledict(m);
255 return run_file(in, "<stdin>", expr_input, d, d);
256}
257
258static object *
259builtin_int(self, v)
260 object *self;
261 object *v;
262{
263 if (v == NULL) {
264 /* */
265 }
266 else if (is_intobject(v)) {
267 INCREF(v);
268 return v;
269 }
Guido van Rossumd4905451991-05-05 20:00:36 +0000270 else if (is_longobject(v)) {
271 long x;
272 x = getlongvalue(v);
Guido van Rossumad405311991-06-03 10:58:01 +0000273 if (err_occurred())
Guido van Rossumd4905451991-05-05 20:00:36 +0000274 return NULL;
275 return newintobject(x);
276 }
Guido van Rossum3f5da241990-12-20 15:06:42 +0000277 else if (is_floatobject(v)) {
278 double x = getfloatvalue(v);
Guido van Rossumad405311991-06-03 10:58:01 +0000279 /* XXX should check for overflow */
Guido van Rossum3f5da241990-12-20 15:06:42 +0000280 return newintobject((long)x);
281 }
Guido van Rossumd4905451991-05-05 20:00:36 +0000282 err_setstr(TypeError, "int() argument must be int, long or float");
Guido van Rossum3f5da241990-12-20 15:06:42 +0000283 return NULL;
284}
285
286static object *
287builtin_len(self, v)
288 object *self;
289 object *v;
290{
291 long len;
292 typeobject *tp;
293 if (v == NULL) {
294 err_setstr(TypeError, "len() without argument");
295 return NULL;
296 }
297 tp = v->ob_type;
298 if (tp->tp_as_sequence != NULL) {
299 len = (*tp->tp_as_sequence->sq_length)(v);
300 }
301 else if (tp->tp_as_mapping != NULL) {
302 len = (*tp->tp_as_mapping->mp_length)(v);
303 }
304 else {
305 err_setstr(TypeError, "len() of unsized object");
306 return NULL;
307 }
308 return newintobject(len);
309}
310
311static object *
Guido van Rossumd4905451991-05-05 20:00:36 +0000312builtin_long(self, v)
313 object *self;
314 object *v;
315{
316 if (v == NULL) {
317 /* */
318 }
319 else if (is_intobject(v)) {
320 return newlongobject(getintvalue(v));
321 }
322 else if (is_longobject(v)) {
323 INCREF(v);
324 return v;
325 }
326 else if (is_floatobject(v)) {
327 double x = getfloatvalue(v);
Guido van Rossumad405311991-06-03 10:58:01 +0000328 return dnewlongobject(x);
Guido van Rossumd4905451991-05-05 20:00:36 +0000329 }
330 err_setstr(TypeError, "long() argument must be int, long or float");
331 return NULL;
332}
333
334static object *
Guido van Rossum3f5da241990-12-20 15:06:42 +0000335min_max(v, sign)
336 object *v;
337 int sign;
338{
339 int i, n, cmp;
340 object *w, *x;
341 sequence_methods *sq;
342 if (v == NULL) {
343 err_setstr(TypeError, "min() or max() without argument");
344 return NULL;
345 }
346 sq = v->ob_type->tp_as_sequence;
347 if (sq == NULL) {
348 err_setstr(TypeError, "min() or max() of non-sequence");
349 return NULL;
350 }
351 n = (*sq->sq_length)(v);
352 if (n == 0) {
Guido van Rossum50afb7a1991-12-10 13:52:31 +0000353 err_setstr(ValueError, "min() or max() of empty sequence");
Guido van Rossum3f5da241990-12-20 15:06:42 +0000354 return NULL;
355 }
356 w = (*sq->sq_item)(v, 0); /* Implies INCREF */
357 for (i = 1; i < n; i++) {
358 x = (*sq->sq_item)(v, i); /* Implies INCREF */
359 cmp = cmpobject(x, w);
360 if (cmp * sign > 0) {
361 DECREF(w);
362 w = x;
363 }
364 else
365 DECREF(x);
366 }
367 return w;
368}
369
370static object *
371builtin_min(self, v)
372 object *self;
373 object *v;
374{
375 return min_max(v, -1);
376}
377
378static object *
379builtin_max(self, v)
380 object *self;
381 object *v;
382{
383 return min_max(v, 1);
384}
385
386static object *
Guido van Rossum006bcd41991-10-24 14:54:44 +0000387builtin_oct(self, v)
388 object *self;
389 object *v;
390{
391 if (v != NULL) {
392 if (is_intobject(v)) {
393 char buf[20];
394 long x = getintvalue(v);
395 if (x >= 0)
396 sprintf(buf, "0%lo", x);
397 else
398 sprintf(buf, "-0%lo", -x);
399 return newstringobject(buf);
400 }
401 if (is_longobject(v)) {
402 return (object *) long_format(v, 8);
403 }
404 }
405 err_setstr(TypeError, "oct() requires int/long argument");
406 return NULL;
407}
408
409static object *
Guido van Rossum3f5da241990-12-20 15:06:42 +0000410builtin_open(self, v)
411 object *self;
412 object *v;
413{
414 object *name, *mode;
415 if (v == NULL || !is_tupleobject(v) || gettuplesize(v) != 2 ||
416 !is_stringobject(name = gettupleitem(v, 0)) ||
417 !is_stringobject(mode = gettupleitem(v, 1))) {
418 err_setstr(TypeError, "open() requires 2 string arguments");
419 return NULL;
420 }
421 v = newfileobject(getstringvalue(name), getstringvalue(mode));
422 return v;
423}
424
425static object *
426builtin_ord(self, v)
427 object *self;
428 object *v;
429{
430 if (v == NULL || !is_stringobject(v)) {
431 err_setstr(TypeError, "ord() must have string argument");
432 return NULL;
433 }
434 if (getstringsize(v) != 1) {
Guido van Rossum50afb7a1991-12-10 13:52:31 +0000435 err_setstr(ValueError, "ord() arg must have length 1");
Guido van Rossum3f5da241990-12-20 15:06:42 +0000436 return NULL;
437 }
438 return newintobject((long)(getstringvalue(v)[0] & 0xff));
439}
440
441static object *
Guido van Rossumc6bb8f71991-07-01 18:42:41 +0000442builtin_pow(self, args)
Guido van Rossumd4905451991-05-05 20:00:36 +0000443 object *self;
Guido van Rossumc6bb8f71991-07-01 18:42:41 +0000444 object *args;
Guido van Rossumd4905451991-05-05 20:00:36 +0000445{
Guido van Rossumc6bb8f71991-07-01 18:42:41 +0000446 object *v, *w, *x;
447 if (args == NULL || !is_tupleobject(args) || gettuplesize(args) != 2) {
Guido van Rossumd4905451991-05-05 20:00:36 +0000448 err_setstr(TypeError, "pow() requires 2 arguments");
449 return NULL;
450 }
Guido van Rossumc6bb8f71991-07-01 18:42:41 +0000451 v = gettupleitem(args, 0);
452 w = gettupleitem(args, 1);
453 if (v->ob_type->tp_as_number == NULL ||
454 w->ob_type->tp_as_number == NULL) {
Guido van Rossumd4905451991-05-05 20:00:36 +0000455 err_setstr(TypeError, "pow() requires numeric arguments");
456 return NULL;
457 }
Guido van Rossumc6bb8f71991-07-01 18:42:41 +0000458 if (coerce(&v, &w) != 0)
459 return NULL;
460 x = (*v->ob_type->tp_as_number->nb_power)(v, w);
461 DECREF(v);
462 DECREF(w);
463 return x;
Guido van Rossumd4905451991-05-05 20:00:36 +0000464}
465
466static object *
Guido van Rossum3f5da241990-12-20 15:06:42 +0000467builtin_range(self, v)
468 object *self;
469 object *v;
470{
471 static char *errmsg = "range() requires 1-3 int arguments";
472 int i, n;
473 long ilow, ihigh, istep;
474 if (v != NULL && is_intobject(v)) {
475 ilow = 0; ihigh = getintvalue(v); istep = 1;
476 }
477 else if (v == NULL || !is_tupleobject(v)) {
478 err_setstr(TypeError, errmsg);
479 return NULL;
480 }
481 else {
482 n = gettuplesize(v);
483 if (n < 1 || n > 3) {
484 err_setstr(TypeError, errmsg);
485 return NULL;
486 }
487 for (i = 0; i < n; i++) {
488 if (!is_intobject(gettupleitem(v, i))) {
489 err_setstr(TypeError, errmsg);
490 return NULL;
491 }
492 }
493 if (n == 3) {
494 istep = getintvalue(gettupleitem(v, 2));
495 --n;
496 }
497 else
498 istep = 1;
499 ihigh = getintvalue(gettupleitem(v, --n));
500 if (n > 0)
501 ilow = getintvalue(gettupleitem(v, 0));
502 else
503 ilow = 0;
504 }
505 if (istep == 0) {
Guido van Rossum50afb7a1991-12-10 13:52:31 +0000506 err_setstr(ValueError, "zero step for range()");
Guido van Rossum3f5da241990-12-20 15:06:42 +0000507 return NULL;
508 }
509 /* XXX ought to check overflow of subtraction */
510 if (istep > 0)
511 n = (ihigh - ilow + istep - 1) / istep;
512 else
513 n = (ihigh - ilow + istep + 1) / istep;
514 if (n < 0)
515 n = 0;
516 v = newlistobject(n);
517 if (v == NULL)
518 return NULL;
519 for (i = 0; i < n; i++) {
520 object *w = newintobject(ilow);
521 if (w == NULL) {
522 DECREF(v);
523 return NULL;
524 }
525 setlistitem(v, i, w);
526 ilow += istep;
527 }
528 return v;
529}
530
531static object *
532builtin_raw_input(self, v)
533 object *self;
534 object *v;
535{
Guido van Rossum3f5da241990-12-20 15:06:42 +0000536 FILE *out = sysgetfile("stdout", stdout);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000537 flushline();
Guido van Rossum90933611991-06-07 16:10:43 +0000538 if (v != NULL) {
539 if (printobject(v, out, PRINT_RAW) != 0)
540 return NULL;
541 }
Guido van Rossum26203aa1991-04-04 15:20:41 +0000542 return filegetline(sysget("stdin"), -1);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000543}
544
545static object *
546builtin_reload(self, v)
547 object *self;
548 object *v;
549{
550 return reload_module(v);
551}
552
553static object *
554builtin_type(self, v)
555 object *self;
556 object *v;
557{
558 if (v == NULL) {
559 err_setstr(TypeError, "type() requres an argument");
560 return NULL;
561 }
562 v = (object *)v->ob_type;
563 INCREF(v);
564 return v;
565}
566
567static struct methodlist builtin_methods[] = {
Guido van Rossumc02e15c1991-12-16 13:03:00 +0000568 {"abs", builtin_abs},
569 {"apply", builtin_apply},
570 {"chr", builtin_chr},
571 {"dir", builtin_dir},
572 {"divmod", builtin_divmod},
573 {"eval", builtin_eval},
574 {"exec", builtin_exec},
575 {"float", builtin_float},
576 {"hex", builtin_hex},
577 {"input", builtin_input},
578 {"int", builtin_int},
579 {"len", builtin_len},
580 {"long", builtin_long},
581 {"max", builtin_max},
582 {"min", builtin_min},
583 {"oct", builtin_oct},
584 {"open", builtin_open}, /* XXX move to OS module */
585 {"ord", builtin_ord},
586 {"pow", builtin_pow},
587 {"range", builtin_range},
588 {"raw_input", builtin_raw_input},
589 {"reload", builtin_reload},
590 {"type", builtin_type},
591 {NULL, NULL},
Guido van Rossum3f5da241990-12-20 15:06:42 +0000592};
593
594static object *builtin_dict;
595
596object *
597getbuiltin(name)
Guido van Rossuma57fb011991-08-16 08:54:58 +0000598 object *name;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000599{
Guido van Rossuma57fb011991-08-16 08:54:58 +0000600 return dict2lookup(builtin_dict, name);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000601}
602
603/* Predefined exceptions */
604
605object *RuntimeError;
606object *EOFError;
607object *TypeError;
608object *MemoryError;
609object *NameError;
610object *SystemError;
611object *KeyboardInterrupt;
612
Guido van Rossum50afb7a1991-12-10 13:52:31 +0000613/* New exceptions */
614object *AttributeError;
615object *IOError;
616object *ZeroDivisionError;
617object *IndexError;
618object *ValueError;
619object *KeyError;
620object *OverflowError;
Guido van Rossumc02e15c1991-12-16 13:03:00 +0000621object *SyntaxError;
Guido van Rossum50afb7a1991-12-10 13:52:31 +0000622
Guido van Rossum3f5da241990-12-20 15:06:42 +0000623static object *
624newstdexception(name, message)
625 char *name, *message;
626{
627 object *v = newstringobject(message);
628 if (v == NULL || dictinsert(builtin_dict, name, v) != 0)
629 fatal("no mem for new standard exception");
630 return v;
631}
632
633static void
634initerrors()
635{
636 RuntimeError = newstdexception("RuntimeError", "run-time error");
637 EOFError = newstdexception("EOFError", "end-of-file read");
638 TypeError = newstdexception("TypeError", "type error");
639 MemoryError = newstdexception("MemoryError", "out of memory");
640 NameError = newstdexception("NameError", "undefined name");
641 SystemError = newstdexception("SystemError", "system error");
642 KeyboardInterrupt =
643 newstdexception("KeyboardInterrupt", "keyboard interrupt");
Guido van Rossum50afb7a1991-12-10 13:52:31 +0000644
645 /* New exceptions */
646 AttributeError =
647 newstdexception("AttributeError", "undefined attribute");
648 IOError = newstdexception("IOError", "I/O error");
649 ZeroDivisionError =
650 newstdexception("ZeroDivisionError", "division by zero");
651 IndexError = newstdexception("IndexError", "index out of range");
652 ValueError = newstdexception("ValueError", "unacceptable value");
653 KeyError = newstdexception("KeyError", "invalid key");
654 OverflowError =
655 newstdexception("OverflowError", "arithmetic overflow");
Guido van Rossumc02e15c1991-12-16 13:03:00 +0000656 SyntaxError =
657 newstdexception("SyntaxError", "syntax error");
Guido van Rossum3f5da241990-12-20 15:06:42 +0000658}
659
660void
661initbuiltin()
662{
663 object *m;
664 m = initmodule("builtin", builtin_methods);
665 builtin_dict = getmoduledict(m);
666 INCREF(builtin_dict);
667 initerrors();
668 (void) dictinsert(builtin_dict, "None", None);
669}
Guido van Rossumc6bb8f71991-07-01 18:42:41 +0000670
671/* Coerce two numeric types to the "larger" one.
672 Increment the reference count on each argument.
673 Return -1 and raise an exception if no coercion is possible
674 (and then no reference count is incremented).
675 XXX This should be distributed over the various numeric types,
676 XXX but for now I don't see how to implement that.
677 XXX So, for now, if you add a new numeric type,
678 XXX you must add to this function as well. */
679
680int
681coerce(pv, pw)
682 object **pv, **pw;
683{
684 register object *v = *pv;
685 register object *w = *pw;
686 if (v->ob_type == w->ob_type) {
687 INCREF(v);
688 INCREF(w);
689 return 0;
690 }
691 if (v->ob_type->tp_as_number == NULL ||
692 w->ob_type->tp_as_number == NULL) {
693 err_setstr(TypeError, "mixing number and non-number");
694 return -1;
695 }
696 if (is_floatobject(v) || is_floatobject(w)) {
697 v = builtin_float((object *)0, v);
698 w = builtin_float((object *)0, w);
699 }
700 else if (is_longobject(v) || is_longobject(w)) {
701 v = builtin_long((object *)0, v);
702 w = builtin_long((object *)0, w);
703 }
704 else {
705 err_setstr(TypeError, "can't coerce numeric types?!?!?");
706 return -1;
707 }
708 if (v == NULL || w == NULL) {
709 XDECREF(v);
710 XDECREF(w);
711 return -1;
712 }
713 *pv = v;
714 *pw = w;
715 return 0;
716}