blob: 01cd5fac33961a766d8ec3f4ab3ebc798b4640a5 [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
40static object *
41builtin_abs(self, v)
42 object *self;
43 object *v;
44{
Guido van Rossumd4905451991-05-05 20:00:36 +000045 number_methods *nm;
46 if (v == NULL || (nm = v->ob_type->tp_as_number) == NULL) {
47 err_setstr(TypeError, "abs() requires numeric argument");
48 return NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +000049 }
Guido van Rossumd4905451991-05-05 20:00:36 +000050 return (*nm->nb_absolute)(v);
Guido van Rossum3f5da241990-12-20 15:06:42 +000051}
52
53static object *
54builtin_chr(self, v)
55 object *self;
56 object *v;
57{
58 long x;
59 char s[1];
60 if (v == NULL || !is_intobject(v)) {
61 err_setstr(TypeError, "chr() must have int argument");
62 return NULL;
63 }
64 x = getintvalue(v);
65 if (x < 0 || x >= 256) {
66 err_setstr(RuntimeError, "chr() arg not in range(256)");
67 return NULL;
68 }
69 s[0] = x;
70 return newsizedstringobject(s, 1);
71}
72
73static object *
74builtin_dir(self, v)
75 object *self;
76 object *v;
77{
78 object *d;
79 if (v == NULL) {
80 d = getlocals();
81 }
82 else {
83 if (!is_moduleobject(v)) {
84 err_setstr(TypeError,
Guido van Rossumc6bb8f71991-07-01 18:42:41 +000085 "dir() argument must be module or absent");
Guido van Rossum3f5da241990-12-20 15:06:42 +000086 return NULL;
87 }
88 d = getmoduledict(v);
89 }
90 v = getdictkeys(d);
91 if (sortlist(v) != 0) {
92 DECREF(v);
93 v = NULL;
94 }
95 return v;
96}
97
98static object *
Guido van Rossumc6bb8f71991-07-01 18:42:41 +000099builtin_divmod(self, args)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000100 object *self;
Guido van Rossumc6bb8f71991-07-01 18:42:41 +0000101 object *args;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000102{
Guido van Rossumc6bb8f71991-07-01 18:42:41 +0000103 object *v, *w, *x;
104 if (args == NULL || !is_tupleobject(args) || gettuplesize(args) != 2) {
Guido van Rossumd4905451991-05-05 20:00:36 +0000105 err_setstr(TypeError, "divmod() requires 2 arguments");
Guido van Rossum3f5da241990-12-20 15:06:42 +0000106 return NULL;
107 }
Guido van Rossumc6bb8f71991-07-01 18:42:41 +0000108 v = gettupleitem(args, 0);
109 w = gettupleitem(args, 1);
110 if (v->ob_type->tp_as_number == NULL ||
111 w->ob_type->tp_as_number == NULL) {
Guido van Rossumd4905451991-05-05 20:00:36 +0000112 err_setstr(TypeError, "divmod() requires numeric arguments");
Guido van Rossum3f5da241990-12-20 15:06:42 +0000113 return NULL;
114 }
Guido van Rossumc6bb8f71991-07-01 18:42:41 +0000115 if (coerce(&v, &w) != 0)
116 return NULL;
117 x = (*v->ob_type->tp_as_number->nb_divmod)(v, w);
118 DECREF(v);
119 DECREF(w);
120 return x;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000121}
122
123static object *
124exec_eval(v, start)
125 object *v;
126 int start;
127{
128 object *str = NULL, *globals = NULL, *locals = NULL;
129 int n;
130 if (v != NULL) {
131 if (is_stringobject(v))
132 str = v;
133 else if (is_tupleobject(v) &&
134 ((n = gettuplesize(v)) == 2 || n == 3)) {
135 str = gettupleitem(v, 0);
136 globals = gettupleitem(v, 1);
137 if (n == 3)
138 locals = gettupleitem(v, 2);
139 }
140 }
141 if (str == NULL || !is_stringobject(str) ||
142 globals != NULL && !is_dictobject(globals) ||
143 locals != NULL && !is_dictobject(locals)) {
144 err_setstr(TypeError,
145 "exec/eval arguments must be string[,dict[,dict]]");
146 return NULL;
147 }
148 return run_string(getstringvalue(str), start, globals, locals);
149}
150
151static object *
152builtin_eval(self, v)
153 object *self;
154 object *v;
155{
156 return exec_eval(v, eval_input);
157}
158
159static object *
160builtin_exec(self, v)
161 object *self;
162 object *v;
163{
164 return exec_eval(v, file_input);
165}
166
167static object *
168builtin_float(self, v)
169 object *self;
170 object *v;
171{
172 if (v == NULL) {
173 /* */
174 }
Guido van Rossum3f5da241990-12-20 15:06:42 +0000175 else if (is_intobject(v)) {
176 long x = getintvalue(v);
177 return newfloatobject((double)x);
178 }
Guido van Rossumd4905451991-05-05 20:00:36 +0000179 else if (is_longobject(v)) {
Guido van Rossumd4905451991-05-05 20:00:36 +0000180 return newfloatobject(dgetlongvalue(v));
181 }
182 else if (is_floatobject(v)) {
183 INCREF(v);
184 return v;
185 }
186 err_setstr(TypeError, "float() argument must be int, long or float");
Guido van Rossum3f5da241990-12-20 15:06:42 +0000187 return NULL;
188}
189
190static object *
191builtin_input(self, v)
192 object *self;
193 object *v;
194{
195 FILE *in = sysgetfile("stdin", stdin);
196 FILE *out = sysgetfile("stdout", stdout);
197 node *n;
198 int err;
199 object *m, *d;
200 flushline();
Guido van Rossum90933611991-06-07 16:10:43 +0000201 if (v != NULL) {
202 if (printobject(v, out, PRINT_RAW) != 0)
203 return NULL;
204 }
Guido van Rossum3f5da241990-12-20 15:06:42 +0000205 m = add_module("__main__");
206 d = getmoduledict(m);
207 return run_file(in, "<stdin>", expr_input, d, d);
208}
209
210static object *
211builtin_int(self, v)
212 object *self;
213 object *v;
214{
215 if (v == NULL) {
216 /* */
217 }
218 else if (is_intobject(v)) {
219 INCREF(v);
220 return v;
221 }
Guido van Rossumd4905451991-05-05 20:00:36 +0000222 else if (is_longobject(v)) {
223 long x;
224 x = getlongvalue(v);
Guido van Rossumad405311991-06-03 10:58:01 +0000225 if (err_occurred())
Guido van Rossumd4905451991-05-05 20:00:36 +0000226 return NULL;
227 return newintobject(x);
228 }
Guido van Rossum3f5da241990-12-20 15:06:42 +0000229 else if (is_floatobject(v)) {
230 double x = getfloatvalue(v);
Guido van Rossumad405311991-06-03 10:58:01 +0000231 /* XXX should check for overflow */
Guido van Rossum3f5da241990-12-20 15:06:42 +0000232 return newintobject((long)x);
233 }
Guido van Rossumd4905451991-05-05 20:00:36 +0000234 err_setstr(TypeError, "int() argument must be int, long or float");
Guido van Rossum3f5da241990-12-20 15:06:42 +0000235 return NULL;
236}
237
238static object *
239builtin_len(self, v)
240 object *self;
241 object *v;
242{
243 long len;
244 typeobject *tp;
245 if (v == NULL) {
246 err_setstr(TypeError, "len() without argument");
247 return NULL;
248 }
249 tp = v->ob_type;
250 if (tp->tp_as_sequence != NULL) {
251 len = (*tp->tp_as_sequence->sq_length)(v);
252 }
253 else if (tp->tp_as_mapping != NULL) {
254 len = (*tp->tp_as_mapping->mp_length)(v);
255 }
256 else {
257 err_setstr(TypeError, "len() of unsized object");
258 return NULL;
259 }
260 return newintobject(len);
261}
262
263static object *
Guido van Rossumd4905451991-05-05 20:00:36 +0000264builtin_long(self, v)
265 object *self;
266 object *v;
267{
268 if (v == NULL) {
269 /* */
270 }
271 else if (is_intobject(v)) {
272 return newlongobject(getintvalue(v));
273 }
274 else if (is_longobject(v)) {
275 INCREF(v);
276 return v;
277 }
278 else if (is_floatobject(v)) {
279 double x = getfloatvalue(v);
Guido van Rossumad405311991-06-03 10:58:01 +0000280 return dnewlongobject(x);
Guido van Rossumd4905451991-05-05 20:00:36 +0000281 }
282 err_setstr(TypeError, "long() argument must be int, long or float");
283 return NULL;
284}
285
286static object *
Guido van Rossum3f5da241990-12-20 15:06:42 +0000287min_max(v, sign)
288 object *v;
289 int sign;
290{
291 int i, n, cmp;
292 object *w, *x;
293 sequence_methods *sq;
294 if (v == NULL) {
295 err_setstr(TypeError, "min() or max() without argument");
296 return NULL;
297 }
298 sq = v->ob_type->tp_as_sequence;
299 if (sq == NULL) {
300 err_setstr(TypeError, "min() or max() of non-sequence");
301 return NULL;
302 }
303 n = (*sq->sq_length)(v);
304 if (n == 0) {
305 err_setstr(RuntimeError, "min() or max() of empty sequence");
306 return NULL;
307 }
308 w = (*sq->sq_item)(v, 0); /* Implies INCREF */
309 for (i = 1; i < n; i++) {
310 x = (*sq->sq_item)(v, i); /* Implies INCREF */
311 cmp = cmpobject(x, w);
312 if (cmp * sign > 0) {
313 DECREF(w);
314 w = x;
315 }
316 else
317 DECREF(x);
318 }
319 return w;
320}
321
322static object *
323builtin_min(self, v)
324 object *self;
325 object *v;
326{
327 return min_max(v, -1);
328}
329
330static object *
331builtin_max(self, v)
332 object *self;
333 object *v;
334{
335 return min_max(v, 1);
336}
337
338static object *
339builtin_open(self, v)
340 object *self;
341 object *v;
342{
343 object *name, *mode;
344 if (v == NULL || !is_tupleobject(v) || gettuplesize(v) != 2 ||
345 !is_stringobject(name = gettupleitem(v, 0)) ||
346 !is_stringobject(mode = gettupleitem(v, 1))) {
347 err_setstr(TypeError, "open() requires 2 string arguments");
348 return NULL;
349 }
350 v = newfileobject(getstringvalue(name), getstringvalue(mode));
351 return v;
352}
353
354static object *
355builtin_ord(self, v)
356 object *self;
357 object *v;
358{
359 if (v == NULL || !is_stringobject(v)) {
360 err_setstr(TypeError, "ord() must have string argument");
361 return NULL;
362 }
363 if (getstringsize(v) != 1) {
364 err_setstr(RuntimeError, "ord() arg must have length 1");
365 return NULL;
366 }
367 return newintobject((long)(getstringvalue(v)[0] & 0xff));
368}
369
370static object *
Guido van Rossumc6bb8f71991-07-01 18:42:41 +0000371builtin_pow(self, args)
Guido van Rossumd4905451991-05-05 20:00:36 +0000372 object *self;
Guido van Rossumc6bb8f71991-07-01 18:42:41 +0000373 object *args;
Guido van Rossumd4905451991-05-05 20:00:36 +0000374{
Guido van Rossumc6bb8f71991-07-01 18:42:41 +0000375 object *v, *w, *x;
376 if (args == NULL || !is_tupleobject(args) || gettuplesize(args) != 2) {
Guido van Rossumd4905451991-05-05 20:00:36 +0000377 err_setstr(TypeError, "pow() requires 2 arguments");
378 return NULL;
379 }
Guido van Rossumc6bb8f71991-07-01 18:42:41 +0000380 v = gettupleitem(args, 0);
381 w = gettupleitem(args, 1);
382 if (v->ob_type->tp_as_number == NULL ||
383 w->ob_type->tp_as_number == NULL) {
Guido van Rossumd4905451991-05-05 20:00:36 +0000384 err_setstr(TypeError, "pow() requires numeric arguments");
385 return NULL;
386 }
Guido van Rossumc6bb8f71991-07-01 18:42:41 +0000387 if (coerce(&v, &w) != 0)
388 return NULL;
389 x = (*v->ob_type->tp_as_number->nb_power)(v, w);
390 DECREF(v);
391 DECREF(w);
392 return x;
Guido van Rossumd4905451991-05-05 20:00:36 +0000393}
394
395static object *
Guido van Rossum3f5da241990-12-20 15:06:42 +0000396builtin_range(self, v)
397 object *self;
398 object *v;
399{
400 static char *errmsg = "range() requires 1-3 int arguments";
401 int i, n;
402 long ilow, ihigh, istep;
403 if (v != NULL && is_intobject(v)) {
404 ilow = 0; ihigh = getintvalue(v); istep = 1;
405 }
406 else if (v == NULL || !is_tupleobject(v)) {
407 err_setstr(TypeError, errmsg);
408 return NULL;
409 }
410 else {
411 n = gettuplesize(v);
412 if (n < 1 || n > 3) {
413 err_setstr(TypeError, errmsg);
414 return NULL;
415 }
416 for (i = 0; i < n; i++) {
417 if (!is_intobject(gettupleitem(v, i))) {
418 err_setstr(TypeError, errmsg);
419 return NULL;
420 }
421 }
422 if (n == 3) {
423 istep = getintvalue(gettupleitem(v, 2));
424 --n;
425 }
426 else
427 istep = 1;
428 ihigh = getintvalue(gettupleitem(v, --n));
429 if (n > 0)
430 ilow = getintvalue(gettupleitem(v, 0));
431 else
432 ilow = 0;
433 }
434 if (istep == 0) {
435 err_setstr(RuntimeError, "zero step for range()");
436 return NULL;
437 }
438 /* XXX ought to check overflow of subtraction */
439 if (istep > 0)
440 n = (ihigh - ilow + istep - 1) / istep;
441 else
442 n = (ihigh - ilow + istep + 1) / istep;
443 if (n < 0)
444 n = 0;
445 v = newlistobject(n);
446 if (v == NULL)
447 return NULL;
448 for (i = 0; i < n; i++) {
449 object *w = newintobject(ilow);
450 if (w == NULL) {
451 DECREF(v);
452 return NULL;
453 }
454 setlistitem(v, i, w);
455 ilow += istep;
456 }
457 return v;
458}
459
460static object *
461builtin_raw_input(self, v)
462 object *self;
463 object *v;
464{
Guido van Rossum3f5da241990-12-20 15:06:42 +0000465 FILE *out = sysgetfile("stdout", stdout);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000466 flushline();
Guido van Rossum90933611991-06-07 16:10:43 +0000467 if (v != NULL) {
468 if (printobject(v, out, PRINT_RAW) != 0)
469 return NULL;
470 }
Guido van Rossum26203aa1991-04-04 15:20:41 +0000471 return filegetline(sysget("stdin"), -1);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000472}
473
474static object *
475builtin_reload(self, v)
476 object *self;
477 object *v;
478{
479 return reload_module(v);
480}
481
482static object *
483builtin_type(self, v)
484 object *self;
485 object *v;
486{
487 if (v == NULL) {
488 err_setstr(TypeError, "type() requres an argument");
489 return NULL;
490 }
491 v = (object *)v->ob_type;
492 INCREF(v);
493 return v;
494}
495
496static struct methodlist builtin_methods[] = {
497 {"abs", builtin_abs},
498 {"chr", builtin_chr},
499 {"dir", builtin_dir},
500 {"divmod", builtin_divmod},
501 {"eval", builtin_eval},
502 {"exec", builtin_exec},
503 {"float", builtin_float},
504 {"input", builtin_input},
505 {"int", builtin_int},
506 {"len", builtin_len},
Guido van Rossumd4905451991-05-05 20:00:36 +0000507 {"long", builtin_long},
Guido van Rossum3f5da241990-12-20 15:06:42 +0000508 {"max", builtin_max},
Guido van Rossum865828d1991-02-19 12:21:50 +0000509 {"min", builtin_min},
Guido van Rossum3f5da241990-12-20 15:06:42 +0000510 {"open", builtin_open}, /* XXX move to OS module */
511 {"ord", builtin_ord},
Guido van Rossumd4905451991-05-05 20:00:36 +0000512 {"pow", builtin_pow},
Guido van Rossum3f5da241990-12-20 15:06:42 +0000513 {"range", builtin_range},
514 {"raw_input", builtin_raw_input},
515 {"reload", builtin_reload},
516 {"type", builtin_type},
517 {NULL, NULL},
518};
519
520static object *builtin_dict;
521
522object *
523getbuiltin(name)
Guido van Rossuma57fb011991-08-16 08:54:58 +0000524 object *name;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000525{
Guido van Rossuma57fb011991-08-16 08:54:58 +0000526 return dict2lookup(builtin_dict, name);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000527}
528
529/* Predefined exceptions */
530
531object *RuntimeError;
532object *EOFError;
533object *TypeError;
534object *MemoryError;
535object *NameError;
536object *SystemError;
537object *KeyboardInterrupt;
538
539static object *
540newstdexception(name, message)
541 char *name, *message;
542{
543 object *v = newstringobject(message);
544 if (v == NULL || dictinsert(builtin_dict, name, v) != 0)
545 fatal("no mem for new standard exception");
546 return v;
547}
548
549static void
550initerrors()
551{
552 RuntimeError = newstdexception("RuntimeError", "run-time error");
553 EOFError = newstdexception("EOFError", "end-of-file read");
554 TypeError = newstdexception("TypeError", "type error");
555 MemoryError = newstdexception("MemoryError", "out of memory");
556 NameError = newstdexception("NameError", "undefined name");
557 SystemError = newstdexception("SystemError", "system error");
558 KeyboardInterrupt =
559 newstdexception("KeyboardInterrupt", "keyboard interrupt");
560}
561
562void
563initbuiltin()
564{
565 object *m;
566 m = initmodule("builtin", builtin_methods);
567 builtin_dict = getmoduledict(m);
568 INCREF(builtin_dict);
569 initerrors();
570 (void) dictinsert(builtin_dict, "None", None);
571}
Guido van Rossumc6bb8f71991-07-01 18:42:41 +0000572
573/* Coerce two numeric types to the "larger" one.
574 Increment the reference count on each argument.
575 Return -1 and raise an exception if no coercion is possible
576 (and then no reference count is incremented).
577 XXX This should be distributed over the various numeric types,
578 XXX but for now I don't see how to implement that.
579 XXX So, for now, if you add a new numeric type,
580 XXX you must add to this function as well. */
581
582int
583coerce(pv, pw)
584 object **pv, **pw;
585{
586 register object *v = *pv;
587 register object *w = *pw;
588 if (v->ob_type == w->ob_type) {
589 INCREF(v);
590 INCREF(w);
591 return 0;
592 }
593 if (v->ob_type->tp_as_number == NULL ||
594 w->ob_type->tp_as_number == NULL) {
595 err_setstr(TypeError, "mixing number and non-number");
596 return -1;
597 }
598 if (is_floatobject(v) || is_floatobject(w)) {
599 v = builtin_float((object *)0, v);
600 w = builtin_float((object *)0, w);
601 }
602 else if (is_longobject(v) || is_longobject(w)) {
603 v = builtin_long((object *)0, v);
604 w = builtin_long((object *)0, w);
605 }
606 else {
607 err_setstr(TypeError, "can't coerce numeric types?!?!?");
608 return -1;
609 }
610 if (v == NULL || w == NULL) {
611 XDECREF(v);
612 XDECREF(w);
613 return -1;
614 }
615 *pv = v;
616 *pw = w;
617 return 0;
618}