blob: 3a3fe56f5028182c9e554f757b3bdd0753c3191b [file] [log] [blame]
Guido van Rossumf70e43a1991-02-19 12:39:46 +00001/***********************************************************
Guido van Rossum6d023c91995-01-04 19:12:13 +00002Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
3The Netherlands.
Guido van Rossumf70e43a1991-02-19 12:39:46 +00004
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"
Guido van Rossum86cd6e61991-01-21 15:12:35 +000031#include "bltinmodule.h"
Guido van Rossum3f5da241990-12-20 15:06:42 +000032#include "import.h"
Guido van Rossum5b722181993-03-30 17:46:03 +000033#include "compile.h"
34#include "eval.h"
Guido van Rossum3f5da241990-12-20 15:06:42 +000035
Guido van Rossum12d12c51993-10-26 17:58:25 +000036/* Forward */
37static object *filterstring PROTO((object *, object *));
38static object *filtertuple PROTO((object *, object *));
Guido van Rossum12d12c51993-10-26 17:58:25 +000039
Guido van Rossum3f5da241990-12-20 15:06:42 +000040static object *
Guido van Rossum1ae940a1995-01-02 19:04:15 +000041builtin___import__(self, args)
Guido van Rossum3f5da241990-12-20 15:06:42 +000042 object *self;
Guido van Rossum1ae940a1995-01-02 19:04:15 +000043 object *args;
Guido van Rossum3f5da241990-12-20 15:06:42 +000044{
Guido van Rossum1ae940a1995-01-02 19:04:15 +000045 char *name;
Guido van Rossum24c13741995-02-14 09:42:43 +000046 object *globals = NULL;
47 object *locals = NULL;
48 object *fromlist = NULL;
Guido van Rossum1ae940a1995-01-02 19:04:15 +000049
Guido van Rossum24c13741995-02-14 09:42:43 +000050 if (!newgetargs(args, "s|OOO:__import__",
51 &name, &globals, &locals, &fromlist))
Guido van Rossum1ae940a1995-01-02 19:04:15 +000052 return NULL;
Guido van Rossum7f9fa971995-01-20 16:53:12 +000053 return import_module(name);
Guido van Rossum1ae940a1995-01-02 19:04:15 +000054}
55
56
57static object *
58builtin_abs(self, args)
59 object *self;
60 object *args;
61{
62 object *v;
Guido van Rossumd4905451991-05-05 20:00:36 +000063 number_methods *nm;
Guido van Rossum1ae940a1995-01-02 19:04:15 +000064
65 if (!newgetargs(args, "O:abs", &v))
66 return NULL;
67 if ((nm = v->ob_type->tp_as_number) == NULL) {
Guido van Rossumd4905451991-05-05 20:00:36 +000068 err_setstr(TypeError, "abs() requires numeric argument");
69 return NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +000070 }
Guido van Rossumd4905451991-05-05 20:00:36 +000071 return (*nm->nb_absolute)(v);
Guido van Rossum3f5da241990-12-20 15:06:42 +000072}
73
74static object *
Guido van Rossum94390a41992-08-14 15:14:30 +000075builtin_apply(self, args)
Guido van Rossumc02e15c1991-12-16 13:03:00 +000076 object *self;
Guido van Rossum94390a41992-08-14 15:14:30 +000077 object *args;
Guido van Rossumc02e15c1991-12-16 13:03:00 +000078{
Guido van Rossumc96ef6a1996-01-26 20:44:30 +000079 object *func, *alist = NULL, *kwdict = NULL;
Guido van Rossum1ae940a1995-01-02 19:04:15 +000080
Guido van Rossum681d79a1995-07-18 14:51:37 +000081 if (!newgetargs(args, "O|OO:apply", &func, &alist, &kwdict))
Guido van Rossumc02e15c1991-12-16 13:03:00 +000082 return NULL;
Guido van Rossum681d79a1995-07-18 14:51:37 +000083 if (alist != NULL && !is_tupleobject(alist)) {
Guido van Rossum2d951851994-08-29 12:52:16 +000084 err_setstr(TypeError, "apply() 2nd argument must be tuple");
85 return NULL;
86 }
Guido van Rossum681d79a1995-07-18 14:51:37 +000087 if (kwdict != NULL && !is_dictobject(kwdict)) {
88 err_setstr(TypeError,
89 "apply() 3rd argument must be dictionary");
90 return NULL;
91 }
92 return PyEval_CallObjectWithKeywords(func, alist, kwdict);
Guido van Rossumc02e15c1991-12-16 13:03:00 +000093}
94
Guido van Rossum2d951851994-08-29 12:52:16 +000095static object *
96builtin_callable(self, args)
97 object *self;
98 object *args;
99{
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000100 object *v;
101
102 if (!newgetargs(args, "O:callable", &v))
Guido van Rossum2d951851994-08-29 12:52:16 +0000103 return NULL;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000104 return newintobject((long)callable(v));
Guido van Rossum2d951851994-08-29 12:52:16 +0000105}
106
Guido van Rossumc02e15c1991-12-16 13:03:00 +0000107static object *
Guido van Rossume77a7571993-11-03 15:01:26 +0000108builtin_filter(self, args)
Guido van Rossum12d12c51993-10-26 17:58:25 +0000109 object *self;
110 object *args;
111{
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000112 object *func, *seq, *result;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000113 sequence_methods *sqf;
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000114 int len;
115 register int i, j;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000116
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000117 if (!newgetargs(args, "OO:filter", &func, &seq))
Guido van Rossum12d12c51993-10-26 17:58:25 +0000118 return NULL;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000119
Guido van Rossum12d12c51993-10-26 17:58:25 +0000120 if (is_stringobject(seq)) {
121 object *r = filterstring(func, seq);
Guido van Rossum12d12c51993-10-26 17:58:25 +0000122 return r;
123 }
124
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000125 if (is_tupleobject(seq)) {
Guido van Rossum12d12c51993-10-26 17:58:25 +0000126 object *r = filtertuple(func, seq);
Guido van Rossum12d12c51993-10-26 17:58:25 +0000127 return r;
128 }
129
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000130 if ((sqf = seq->ob_type->tp_as_sequence) == NULL) {
Guido van Rossum12d12c51993-10-26 17:58:25 +0000131 err_setstr(TypeError,
Guido van Rossume77a7571993-11-03 15:01:26 +0000132 "argument 2 to filter() must be a sequence type");
Guido van Rossum12d12c51993-10-26 17:58:25 +0000133 goto Fail_2;
134 }
135
136 if ((len = (*sqf->sq_length)(seq)) < 0)
137 goto Fail_2;
138
139 if (is_listobject(seq) && seq->ob_refcnt == 1) {
140 INCREF(seq);
141 result = seq;
142 }
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000143 else {
Guido van Rossum12d12c51993-10-26 17:58:25 +0000144 if ((result = newlistobject(len)) == NULL)
145 goto Fail_2;
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000146 }
Guido van Rossum12d12c51993-10-26 17:58:25 +0000147
Guido van Rossum2d951851994-08-29 12:52:16 +0000148 for (i = j = 0; ; ++i) {
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000149 object *item, *good;
150 int ok;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000151
Guido van Rossum2d951851994-08-29 12:52:16 +0000152 if ((item = (*sqf->sq_item)(seq, i)) == NULL) {
153 if (i < len)
154 goto Fail_1;
155 if (err_occurred() == IndexError) {
156 err_clear();
157 break;
158 }
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000159 goto Fail_1;
Guido van Rossum2d951851994-08-29 12:52:16 +0000160 }
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000161
162 if (func == None) {
163 good = item;
Guido van Rossum58b68731995-01-10 17:40:55 +0000164 INCREF(good);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000165 }
166 else {
167 object *arg = mkvalue("(O)", item);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000168 if (arg == NULL)
169 goto Fail_1;
170 good = call_object(func, arg);
Guido van Rossum12d12c51993-10-26 17:58:25 +0000171 DECREF(arg);
Guido van Rossum58b68731995-01-10 17:40:55 +0000172 if (good == NULL) {
173 DECREF(item);
Guido van Rossum12d12c51993-10-26 17:58:25 +0000174 goto Fail_1;
Guido van Rossum58b68731995-01-10 17:40:55 +0000175 }
Guido van Rossum12d12c51993-10-26 17:58:25 +0000176 }
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000177 ok = testbool(good);
178 DECREF(good);
179 if (ok) {
Guido van Rossum2d951851994-08-29 12:52:16 +0000180 if (j < len) {
181 if (setlistitem(result, j++, item) < 0)
182 goto Fail_1;
183 }
184 else {
185 j++;
186 if (addlistitem(result, item) < 0)
187 goto Fail_1;
188 }
Guido van Rossum58b68731995-01-10 17:40:55 +0000189 } else {
190 DECREF(item);
Guido van Rossum12d12c51993-10-26 17:58:25 +0000191 }
Guido van Rossum12d12c51993-10-26 17:58:25 +0000192 }
193
Guido van Rossum12d12c51993-10-26 17:58:25 +0000194
Guido van Rossum2d951851994-08-29 12:52:16 +0000195 if (j < len && setlistslice(result, j, len, NULL) < 0)
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000196 goto Fail_1;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000197
Guido van Rossum12d12c51993-10-26 17:58:25 +0000198 return result;
199
Guido van Rossum12d12c51993-10-26 17:58:25 +0000200Fail_1:
201 DECREF(result);
202Fail_2:
Guido van Rossum12d12c51993-10-26 17:58:25 +0000203 return NULL;
204}
205
206static object *
Guido van Rossum94390a41992-08-14 15:14:30 +0000207builtin_chr(self, args)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000208 object *self;
Guido van Rossum94390a41992-08-14 15:14:30 +0000209 object *args;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000210{
211 long x;
212 char s[1];
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000213
214 if (!newgetargs(args, "l:chr", &x))
Guido van Rossum3f5da241990-12-20 15:06:42 +0000215 return NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000216 if (x < 0 || x >= 256) {
Guido van Rossum50afb7a1991-12-10 13:52:31 +0000217 err_setstr(ValueError, "chr() arg not in range(256)");
Guido van Rossum3f5da241990-12-20 15:06:42 +0000218 return NULL;
219 }
220 s[0] = x;
221 return newsizedstringobject(s, 1);
222}
223
224static object *
Guido van Rossuma9e7dc11992-10-18 18:53:57 +0000225builtin_cmp(self, args)
226 object *self;
227 object *args;
228{
229 object *a, *b;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000230
231 if (!newgetargs(args, "OO:cmp", &a, &b))
Guido van Rossuma9e7dc11992-10-18 18:53:57 +0000232 return NULL;
233 return newintobject((long)cmpobject(a, b));
234}
235
236static object *
Guido van Rossum5524a591995-01-10 15:26:20 +0000237builtin_coerce(self, args)
238 object *self;
239 object *args;
Guido van Rossum6a00cd81995-01-07 12:39:01 +0000240{
Guido van Rossum5524a591995-01-10 15:26:20 +0000241 object *v, *w;
Guido van Rossum04691fc1992-08-12 15:35:34 +0000242 object *res;
Guido van Rossum5524a591995-01-10 15:26:20 +0000243
244 if (!newgetargs(args, "OO:coerce", &v, &w))
245 return NULL;
Guido van Rossum04691fc1992-08-12 15:35:34 +0000246 if (coerce(&v, &w) < 0)
247 return NULL;
248 res = mkvalue("(OO)", v, w);
249 DECREF(v);
250 DECREF(w);
251 return res;
252}
253
254static object *
Guido van Rossum5b722181993-03-30 17:46:03 +0000255builtin_compile(self, args)
256 object *self;
257 object *args;
258{
259 char *str;
260 char *filename;
261 char *startstr;
262 int start;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000263
264 if (!newgetargs(args, "sss:compile", &str, &filename, &startstr))
Guido van Rossum5b722181993-03-30 17:46:03 +0000265 return NULL;
266 if (strcmp(startstr, "exec") == 0)
267 start = file_input;
268 else if (strcmp(startstr, "eval") == 0)
269 start = eval_input;
Guido van Rossum872537c1995-07-07 22:43:42 +0000270 else if (strcmp(startstr, "single") == 0)
271 start = single_input;
Guido van Rossum5b722181993-03-30 17:46:03 +0000272 else {
273 err_setstr(ValueError,
Guido van Rossum872537c1995-07-07 22:43:42 +0000274 "compile() mode must be 'exec' or 'eval' or 'single'");
Guido van Rossum5b722181993-03-30 17:46:03 +0000275 return NULL;
276 }
277 return compile_string(str, filename, start);
278}
279
Guido van Rossum8a5c5d21996-01-12 01:09:56 +0000280#ifndef WITHOUT_COMPLEX
281
282static object *
283builtin_complex(self, args)
284 object *self;
285 object *args;
286{
287 object *r, *i;
288 number_methods *nbr, *nbi;
Guido van Rossum530956d1996-07-21 02:27:43 +0000289 Py_complex cr, ci;
Guido van Rossum8a5c5d21996-01-12 01:09:56 +0000290
291 i = NULL;
292 if (!newgetargs(args, "O|O:complex", &r, &i))
293 return NULL;
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");
300 return NULL;
301 }
302 if (is_complexobject(r))
303 cr = ((complexobject*)r)->cval;
304 else {
305 cr.real = getfloatvalue((*nbr->nb_float)(r));
306 cr.imag = 0.;
307 }
308 if (i == NULL) {
309 ci.real = 0.;
310 ci.imag = 0.;
311 }
312 else if (is_complexobject(i))
313 ci = ((complexobject*)i)->cval;
314 else {
315 ci.real = getfloatvalue((*nbi->nb_float)(i));
316 ci.imag = 0.;
317 }
318 cr.real -= ci.imag;
319 cr.imag += ci.real;
320 return newcomplexobject(cr);
321}
322
323#endif
324
Guido van Rossum5b722181993-03-30 17:46:03 +0000325static object *
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000326builtin_dir(self, args)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000327 object *self;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000328 object *args;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000329{
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000330 object *v = NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000331 object *d;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000332
333 if (!newgetargs(args, "|O:dir", &v))
334 return NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000335 if (v == NULL) {
336 d = getlocals();
Guido van Rossumdc8a1081991-10-20 20:11:03 +0000337 INCREF(d);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000338 }
339 else {
Guido van Rossumdc8a1081991-10-20 20:11:03 +0000340 d = getattr(v, "__dict__");
341 if (d == NULL) {
Guido van Rossum3f5da241990-12-20 15:06:42 +0000342 err_setstr(TypeError,
Guido van Rossum006bcd41991-10-24 14:54:44 +0000343 "dir() argument must have __dict__ attribute");
Guido van Rossum3f5da241990-12-20 15:06:42 +0000344 return NULL;
345 }
Guido van Rossum3f5da241990-12-20 15:06:42 +0000346 }
Guido van Rossum006bcd41991-10-24 14:54:44 +0000347 if (is_dictobject(d)) {
Guido van Rossumdc8a1081991-10-20 20:11:03 +0000348 v = getdictkeys(d);
349 if (sortlist(v) != 0) {
350 DECREF(v);
351 v = NULL;
352 }
353 }
Guido van Rossum006bcd41991-10-24 14:54:44 +0000354 else {
Guido van Rossum795ba581996-05-23 22:49:07 +0000355 v = PyObject_CallMethod(d, "keys", NULL);
356 if (v == NULL) {
357 PyErr_Clear();
358 v = newlistobject(0);
359 }
Guido van Rossum006bcd41991-10-24 14:54:44 +0000360 }
Guido van Rossumdc8a1081991-10-20 20:11:03 +0000361 DECREF(d);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000362 return v;
363}
364
365static object *
Guido van Rossum6a00cd81995-01-07 12:39:01 +0000366do_divmod(v, w)
367 object *v, *w;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000368{
Guido van Rossum6a00cd81995-01-07 12:39:01 +0000369 object *res;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000370
Guido van Rossum180d7b41994-09-29 09:45:57 +0000371 if (is_instanceobject(v) || is_instanceobject(w))
Guido van Rossum6a00cd81995-01-07 12:39:01 +0000372 return instancebinop(v, w, "__divmod__", "__rdivmod__",
373 do_divmod);
Guido van Rossumc6bb8f71991-07-01 18:42:41 +0000374 if (v->ob_type->tp_as_number == NULL ||
375 w->ob_type->tp_as_number == NULL) {
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000376 err_setstr(TypeError,
377 "divmod() requires numeric or class instance arguments");
Guido van Rossum3f5da241990-12-20 15:06:42 +0000378 return NULL;
379 }
Guido van Rossumc6bb8f71991-07-01 18:42:41 +0000380 if (coerce(&v, &w) != 0)
381 return NULL;
Guido van Rossum6a00cd81995-01-07 12:39:01 +0000382 res = (*v->ob_type->tp_as_number->nb_divmod)(v, w);
Guido van Rossumc6bb8f71991-07-01 18:42:41 +0000383 DECREF(v);
384 DECREF(w);
Guido van Rossum6a00cd81995-01-07 12:39:01 +0000385 return res;
386}
387
388static object *
389builtin_divmod(self, args)
390 object *self;
391 object *args;
392{
393 object *v, *w;
394
395 if (!newgetargs(args, "OO:divmod", &v, &w))
396 return NULL;
397 return do_divmod(v, w);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000398}
399
400static object *
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000401builtin_eval(self, args)
402 object *self;
403 object *args;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000404{
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000405 object *cmd;
Guido van Rossum84eaa831995-01-10 10:47:05 +0000406 object *globals = None, *locals = None;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000407 char *str;
Guido van Rossum590baa41993-11-30 13:40:46 +0000408
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000409 if (!newgetargs(args, "O|O!O!:eval",
410 &cmd,
411 &Mappingtype, &globals,
412 &Mappingtype, &locals))
413 return NULL;
Guido van Rossum84eaa831995-01-10 10:47:05 +0000414 if (globals == None) {
Guido van Rossum6135a871995-01-09 17:53:26 +0000415 globals = getglobals();
Guido van Rossum84eaa831995-01-10 10:47:05 +0000416 if (locals == None)
417 locals = getlocals();
Guido van Rossum6135a871995-01-09 17:53:26 +0000418 }
Guido van Rossum84eaa831995-01-10 10:47:05 +0000419 else if (locals == None)
Guido van Rossum6135a871995-01-09 17:53:26 +0000420 locals = globals;
421 if (dictlookup(globals, "__builtins__") == NULL) {
422 if (dictinsert(globals, "__builtins__", getbuiltins()) != 0)
423 return NULL;
424 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000425 if (is_codeobject(cmd))
Guido van Rossum681d79a1995-07-18 14:51:37 +0000426 return eval_code((codeobject *) cmd, globals, locals);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000427 if (!is_stringobject(cmd)) {
428 err_setstr(TypeError,
429 "eval() argument 1 must be string or code object");
Guido van Rossum94390a41992-08-14 15:14:30 +0000430 return NULL;
431 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000432 str = getstringvalue(cmd);
433 if (strlen(str) != getstringsize(cmd)) {
434 err_setstr(ValueError,
435 "embedded '\\0' in string arg");
436 return NULL;
Guido van Rossumf08ab0a1992-03-04 16:41:41 +0000437 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000438 while (*str == ' ' || *str == '\t')
439 str++;
440 return run_string(str, eval_input, globals, locals);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000441}
442
443static object *
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000444builtin_execfile(self, args)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000445 object *self;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000446 object *args;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000447{
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000448 char *filename;
Guido van Rossum84eaa831995-01-10 10:47:05 +0000449 object *globals = None, *locals = None;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000450 object *res;
Guido van Rossum0f61f8a1992-02-25 18:55:05 +0000451 FILE* fp;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000452
453 if (!newgetargs(args, "s|O!O!:execfile",
454 &filename,
455 &Mappingtype, &globals,
456 &Mappingtype, &locals))
Guido van Rossum0f61f8a1992-02-25 18:55:05 +0000457 return NULL;
Guido van Rossum84eaa831995-01-10 10:47:05 +0000458 if (globals == None) {
Guido van Rossum6135a871995-01-09 17:53:26 +0000459 globals = getglobals();
Guido van Rossum84eaa831995-01-10 10:47:05 +0000460 if (locals == None)
461 locals = getlocals();
Guido van Rossum6135a871995-01-09 17:53:26 +0000462 }
Guido van Rossum84eaa831995-01-10 10:47:05 +0000463 else if (locals == None)
Guido van Rossum6135a871995-01-09 17:53:26 +0000464 locals = globals;
465 if (dictlookup(globals, "__builtins__") == NULL) {
466 if (dictinsert(globals, "__builtins__", getbuiltins()) != 0)
467 return NULL;
468 }
Guido van Rossumff4949e1992-08-05 19:58:53 +0000469 BGN_SAVE
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000470 fp = fopen(filename, "r");
Guido van Rossumff4949e1992-08-05 19:58:53 +0000471 END_SAVE
Guido van Rossum0f61f8a1992-02-25 18:55:05 +0000472 if (fp == NULL) {
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000473 err_errno(IOError);
Guido van Rossum0f61f8a1992-02-25 18:55:05 +0000474 return NULL;
475 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000476 res = run_file(fp, filename, file_input, globals, locals);
Guido van Rossumff4949e1992-08-05 19:58:53 +0000477 BGN_SAVE
Guido van Rossum0f61f8a1992-02-25 18:55:05 +0000478 fclose(fp);
Guido van Rossumff4949e1992-08-05 19:58:53 +0000479 END_SAVE
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000480 return res;
Guido van Rossum0f61f8a1992-02-25 18:55:05 +0000481}
482
483static object *
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000484builtin_float(self, args)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000485 object *self;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000486 object *args;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000487{
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000488 object *v;
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000489 number_methods *nb;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000490
491 if (!newgetargs(args, "O:float", &v))
492 return NULL;
493 if ((nb = v->ob_type->tp_as_number) == NULL ||
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000494 nb->nb_float == NULL) {
495 err_setstr(TypeError,
496 "float() argument can't be converted to float");
497 return NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000498 }
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000499 return (*nb->nb_float)(v);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000500}
501
502static object *
Guido van Rossum94390a41992-08-14 15:14:30 +0000503builtin_getattr(self, args)
Guido van Rossum33894be1992-01-27 16:53:09 +0000504 object *self;
Guido van Rossum94390a41992-08-14 15:14:30 +0000505 object *args;
Guido van Rossum33894be1992-01-27 16:53:09 +0000506{
Guido van Rossum94390a41992-08-14 15:14:30 +0000507 object *v;
Guido van Rossum9bfef441993-03-29 10:43:31 +0000508 object *name;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000509
510 if (!newgetargs(args, "OS:getattr", &v, &name))
Guido van Rossum33894be1992-01-27 16:53:09 +0000511 return NULL;
Guido van Rossum9bfef441993-03-29 10:43:31 +0000512 return getattro(v, name);
513}
514
515static object *
Guido van Rossum872537c1995-07-07 22:43:42 +0000516builtin_globals(self, args)
517 object *self;
518 object *args;
519{
520 object *d;
521
522 if (!newgetargs(args, ""))
523 return NULL;
524 d = getglobals();
525 INCREF(d);
526 return d;
527}
528
529static object *
Guido van Rossum9bfef441993-03-29 10:43:31 +0000530builtin_hasattr(self, args)
531 object *self;
532 object *args;
533{
534 object *v;
535 object *name;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000536
537 if (!newgetargs(args, "OS:hasattr", &v, &name))
Guido van Rossum9bfef441993-03-29 10:43:31 +0000538 return NULL;
539 v = getattro(v, name);
540 if (v == NULL) {
541 err_clear();
542 return newintobject(0L);
543 }
544 DECREF(v);
545 return newintobject(1L);
Guido van Rossum33894be1992-01-27 16:53:09 +0000546}
547
548static object *
Guido van Rossum5b722181993-03-30 17:46:03 +0000549builtin_id(self, args)
550 object *self;
551 object *args;
552{
553 object *v;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000554
555 if (!newgetargs(args, "O:id", &v))
Guido van Rossum5b722181993-03-30 17:46:03 +0000556 return NULL;
557 return newintobject((long)v);
558}
559
560static object *
Guido van Rossum12d12c51993-10-26 17:58:25 +0000561builtin_map(self, args)
562 object *self;
563 object *args;
564{
565 typedef struct {
566 object *seq;
567 sequence_methods *sqf;
568 int len;
569 } sequence;
570
571 object *func, *result;
572 sequence *seqs = NULL, *sqp;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000573 int n, len;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000574 register int i, j;
575
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000576 n = gettuplesize(args);
577 if (n < 2) {
Guido van Rossum12d12c51993-10-26 17:58:25 +0000578 err_setstr(TypeError, "map() requires at least two args");
579 return NULL;
580 }
581
582 func = gettupleitem(args, 0);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000583 n--;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000584
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000585 if ((seqs = NEW(sequence, n)) == NULL) {
586 err_nomem();
587 goto Fail_2;
588 }
Guido van Rossum12d12c51993-10-26 17:58:25 +0000589
Guido van Rossum2d951851994-08-29 12:52:16 +0000590 for (len = 0, i = 0, sqp = seqs; i < n; ++i, ++sqp) {
Guido van Rossum12d12c51993-10-26 17:58:25 +0000591 int curlen;
592
593 if ((sqp->seq = gettupleitem(args, i + 1)) == NULL)
594 goto Fail_2;
595
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];
600
601 sprintf(errbuf, errmsg, i+2);
602 err_setstr(TypeError, errbuf);
603 goto Fail_2;
604 }
605
606 if ((curlen = sqp->len = (*sqp->sqf->sq_length)(sqp->seq)) < 0)
607 goto Fail_2;
608
609 if (curlen > len)
610 len = curlen;
611 }
612
613 if ((result = (object *) newlistobject(len)) == NULL)
614 goto Fail_2;
615
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000616 /* XXX Special case map(None, single_list) could be more efficient */
Guido van Rossum2d951851994-08-29 12:52:16 +0000617 for (i = 0; ; ++i) {
Guido van Rossum32120311995-07-10 13:52:21 +0000618 object *alist, *item, *value;
Guido van Rossum2d951851994-08-29 12:52:16 +0000619 int any = 0;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000620
Guido van Rossum2d951851994-08-29 12:52:16 +0000621 if (func == None && n == 1)
Guido van Rossum32120311995-07-10 13:52:21 +0000622 alist = NULL;
Guido van Rossum2d951851994-08-29 12:52:16 +0000623 else {
Guido van Rossum32120311995-07-10 13:52:21 +0000624 if ((alist = newtupleobject(n)) == NULL)
Guido van Rossum2d951851994-08-29 12:52:16 +0000625 goto Fail_1;
626 }
Guido van Rossum12d12c51993-10-26 17:58:25 +0000627
628 for (j = 0, sqp = seqs; j < n; ++j, ++sqp) {
Guido van Rossum2d951851994-08-29 12:52:16 +0000629 if (sqp->len < 0) {
Guido van Rossum12d12c51993-10-26 17:58:25 +0000630 INCREF(None);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000631 item = None;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000632 }
Guido van Rossum12d12c51993-10-26 17:58:25 +0000633 else {
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000634 item = (*sqp->sqf->sq_item)(sqp->seq, i);
Guido van Rossum2d951851994-08-29 12:52:16 +0000635 if (item == NULL) {
636 if (i < sqp->len)
637 goto Fail_0;
638 if (err_occurred() == IndexError) {
639 err_clear();
640 INCREF(None);
641 item = None;
642 sqp->len = -1;
643 }
644 else {
645 goto Fail_0;
646 }
647 }
648 else
649 any = 1;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000650
Guido van Rossum12d12c51993-10-26 17:58:25 +0000651 }
Guido van Rossum32120311995-07-10 13:52:21 +0000652 if (!alist)
Guido van Rossum2d951851994-08-29 12:52:16 +0000653 break;
Guido van Rossum32120311995-07-10 13:52:21 +0000654 if (settupleitem(alist, j, item) < 0) {
Guido van Rossum2d951851994-08-29 12:52:16 +0000655 DECREF(item);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000656 goto Fail_0;
Guido van Rossum2d951851994-08-29 12:52:16 +0000657 }
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000658 continue;
659
660 Fail_0:
Guido van Rossum32120311995-07-10 13:52:21 +0000661 XDECREF(alist);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000662 goto Fail_1;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000663 }
664
Guido van Rossum32120311995-07-10 13:52:21 +0000665 if (!alist)
666 alist = item;
Guido van Rossum2d951851994-08-29 12:52:16 +0000667
668 if (!any) {
Guido van Rossum32120311995-07-10 13:52:21 +0000669 DECREF(alist);
Guido van Rossum2d951851994-08-29 12:52:16 +0000670 break;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000671 }
Guido van Rossum2d951851994-08-29 12:52:16 +0000672
673 if (func == None)
Guido van Rossum32120311995-07-10 13:52:21 +0000674 value = alist;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000675 else {
Guido van Rossum32120311995-07-10 13:52:21 +0000676 value = call_object(func, alist);
677 DECREF(alist);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000678 if (value == NULL)
679 goto Fail_1;
Guido van Rossum2d951851994-08-29 12:52:16 +0000680 }
681 if (i >= len) {
682 if (addlistitem(result, value) < 0)
683 goto Fail_1;
684 }
685 else {
686 if (setlistitem(result, i, value) < 0)
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000687 goto Fail_1;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000688 }
689 }
690
Guido van Rossum2d951851994-08-29 12:52:16 +0000691 DEL(seqs);
Guido van Rossum12d12c51993-10-26 17:58:25 +0000692 return result;
693
Guido van Rossum12d12c51993-10-26 17:58:25 +0000694Fail_1:
695 DECREF(result);
696Fail_2:
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000697 if (seqs) DEL(seqs);
Guido van Rossum12d12c51993-10-26 17:58:25 +0000698 return NULL;
699}
700
701static object *
Guido van Rossum94390a41992-08-14 15:14:30 +0000702builtin_setattr(self, args)
Guido van Rossum33894be1992-01-27 16:53:09 +0000703 object *self;
Guido van Rossum94390a41992-08-14 15:14:30 +0000704 object *args;
Guido van Rossum33894be1992-01-27 16:53:09 +0000705{
Guido van Rossum94390a41992-08-14 15:14:30 +0000706 object *v;
Guido van Rossum9bfef441993-03-29 10:43:31 +0000707 object *name;
Guido van Rossum94390a41992-08-14 15:14:30 +0000708 object *value;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000709
710 if (!newgetargs(args, "OSO:setattr", &v, &name, &value))
Guido van Rossum33894be1992-01-27 16:53:09 +0000711 return NULL;
Guido van Rossum9bfef441993-03-29 10:43:31 +0000712 if (setattro(v, name, value) != 0)
Guido van Rossum33894be1992-01-27 16:53:09 +0000713 return NULL;
714 INCREF(None);
715 return None;
716}
717
718static object *
Guido van Rossum14144fc1994-08-29 12:53:40 +0000719builtin_delattr(self, args)
720 object *self;
721 object *args;
722{
723 object *v;
724 object *name;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000725
726 if (!newgetargs(args, "OS:delattr", &v, &name))
Guido van Rossum14144fc1994-08-29 12:53:40 +0000727 return NULL;
728 if (setattro(v, name, (object *)NULL) != 0)
729 return NULL;
730 INCREF(None);
731 return None;
732}
733
734static object *
Guido van Rossum9bfef441993-03-29 10:43:31 +0000735builtin_hash(self, args)
736 object *self;
737 object *args;
738{
739 object *v;
740 long x;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000741
742 if (!newgetargs(args, "O:hash", &v))
Guido van Rossum9bfef441993-03-29 10:43:31 +0000743 return NULL;
744 x = hashobject(v);
745 if (x == -1)
746 return NULL;
747 return newintobject(x);
748}
749
750static object *
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000751builtin_hex(self, args)
Guido van Rossum006bcd41991-10-24 14:54:44 +0000752 object *self;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000753 object *args;
Guido van Rossum006bcd41991-10-24 14:54:44 +0000754{
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000755 object *v;
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000756 number_methods *nb;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000757
758 if (!newgetargs(args, "O:hex", &v))
759 return NULL;
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000760
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000761 if ((nb = v->ob_type->tp_as_number) == NULL ||
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000762 nb->nb_hex == NULL) {
763 err_setstr(TypeError,
764 "hex() argument can't be converted to hex");
765 return NULL;
Guido van Rossum006bcd41991-10-24 14:54:44 +0000766 }
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000767 return (*nb->nb_hex)(v);
Guido van Rossum006bcd41991-10-24 14:54:44 +0000768}
769
Guido van Rossum3165fe61992-09-25 21:59:05 +0000770static object *builtin_raw_input PROTO((object *, object *));
771
Guido van Rossum006bcd41991-10-24 14:54:44 +0000772static object *
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000773builtin_input(self, args)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000774 object *self;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000775 object *args;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000776{
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000777 object *line;
778 char *str;
779 object *res;
Guido van Rossum6135a871995-01-09 17:53:26 +0000780 object *globals, *locals;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000781
782 line = builtin_raw_input(self, args);
Guido van Rossum3165fe61992-09-25 21:59:05 +0000783 if (line == NULL)
784 return line;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000785 if (!getargs(line, "s;embedded '\\0' in input line", &str))
786 return NULL;
787 while (*str == ' ' || *str == '\t')
788 str++;
Guido van Rossum6135a871995-01-09 17:53:26 +0000789 globals = getglobals();
790 locals = getlocals();
791 if (dictlookup(globals, "__builtins__") == NULL) {
792 if (dictinsert(globals, "__builtins__", getbuiltins()) != 0)
793 return NULL;
794 }
795 res = run_string(str, eval_input, globals, locals);
Guido van Rossum3165fe61992-09-25 21:59:05 +0000796 DECREF(line);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000797 return res;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000798}
799
800static object *
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000801builtin_int(self, args)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000802 object *self;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000803 object *args;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000804{
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000805 object *v;
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000806 number_methods *nb;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000807
808 if (!newgetargs(args, "O:int", &v))
809 return NULL;
810 if ((nb = v->ob_type->tp_as_number) == NULL ||
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000811 nb->nb_int == NULL) {
812 err_setstr(TypeError,
813 "int() argument can't be converted to int");
814 return NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000815 }
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000816 return (*nb->nb_int)(v);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000817}
818
819static object *
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000820builtin_len(self, args)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000821 object *self;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000822 object *args;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000823{
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000824 object *v;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000825 long len;
826 typeobject *tp;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000827
828 if (!newgetargs(args, "O:len", &v))
Guido van Rossum3f5da241990-12-20 15:06:42 +0000829 return NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000830 tp = v->ob_type;
831 if (tp->tp_as_sequence != NULL) {
832 len = (*tp->tp_as_sequence->sq_length)(v);
833 }
834 else if (tp->tp_as_mapping != NULL) {
835 len = (*tp->tp_as_mapping->mp_length)(v);
836 }
837 else {
838 err_setstr(TypeError, "len() of unsized object");
839 return NULL;
840 }
Guido van Rossum04691fc1992-08-12 15:35:34 +0000841 if (len < 0)
842 return NULL;
843 else
844 return newintobject(len);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000845}
846
847static object *
Guido van Rossumd1705771996-04-09 02:41:06 +0000848builtin_list(self, args)
849 object *self;
850 object *args;
851{
852 object *v;
853 sequence_methods *sqf;
854
855 if (!newgetargs(args, "O:list", &v))
856 return NULL;
857 if ((sqf = v->ob_type->tp_as_sequence) != NULL) {
858 int n = (*sqf->sq_length)(v);
859 int i;
860 object *l;
861 if (n < 0)
862 return NULL;
863 l = newlistobject(n);
864 if (l == NULL)
865 return NULL;
866 for (i = 0; i < n; i++) {
867 object *item = (*sqf->sq_item)(v, i);
868 if (item == NULL) {
869 DECREF(l);
870 l = NULL;
871 break;
872 }
873 setlistitem(l, i, item);
874 }
875 /* XXX Should support indefinite-length sequences */
876 return l;
877 }
878 err_setstr(TypeError, "list() argument must be a sequence");
879 return NULL;
880}
881
882static object *
Guido van Rossum872537c1995-07-07 22:43:42 +0000883builtin_locals(self, args)
884 object *self;
885 object *args;
886{
887 object *d;
888
889 if (!newgetargs(args, ""))
890 return NULL;
891 d = getlocals();
892 INCREF(d);
893 return d;
894}
895
896static object *
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000897builtin_long(self, args)
Guido van Rossumd4905451991-05-05 20:00:36 +0000898 object *self;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000899 object *args;
Guido van Rossumd4905451991-05-05 20:00:36 +0000900{
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000901 object *v;
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000902 number_methods *nb;
903
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000904 if (!newgetargs(args, "O:long", &v))
905 return NULL;
906 if ((nb = v->ob_type->tp_as_number) == NULL ||
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000907 nb->nb_long == NULL) {
908 err_setstr(TypeError,
909 "long() argument can't be converted to long");
910 return NULL;
Guido van Rossumd4905451991-05-05 20:00:36 +0000911 }
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000912 return (*nb->nb_long)(v);
Guido van Rossumd4905451991-05-05 20:00:36 +0000913}
914
915static object *
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000916min_max(args, sign)
917 object *args;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000918 int sign;
919{
Guido van Rossum2d951851994-08-29 12:52:16 +0000920 int i;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000921 object *v, *w, *x;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000922 sequence_methods *sq;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000923
924 if (gettuplesize(args) > 1)
925 v = args;
926 else if (!newgetargs(args, "O:min/max", &v))
Guido van Rossum3f5da241990-12-20 15:06:42 +0000927 return NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000928 sq = v->ob_type->tp_as_sequence;
929 if (sq == NULL) {
930 err_setstr(TypeError, "min() or max() of non-sequence");
931 return NULL;
932 }
Guido van Rossum2d951851994-08-29 12:52:16 +0000933 w = NULL;
934 for (i = 0; ; i++) {
Guido van Rossum3f5da241990-12-20 15:06:42 +0000935 x = (*sq->sq_item)(v, i); /* Implies INCREF */
Guido van Rossum2d951851994-08-29 12:52:16 +0000936 if (x == NULL) {
937 if (err_occurred() == IndexError) {
938 err_clear();
939 break;
940 }
941 XDECREF(w);
942 return NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000943 }
Guido van Rossum2d951851994-08-29 12:52:16 +0000944 if (w == NULL)
945 w = x;
946 else {
947 if (cmpobject(x, w) * sign > 0) {
948 DECREF(w);
949 w = x;
950 }
951 else
952 DECREF(x);
953 }
Guido van Rossum3f5da241990-12-20 15:06:42 +0000954 }
Guido van Rossum2d951851994-08-29 12:52:16 +0000955 if (w == NULL)
956 err_setstr(ValueError, "min() or max() of empty sequence");
Guido van Rossum3f5da241990-12-20 15:06:42 +0000957 return w;
958}
959
960static object *
961builtin_min(self, v)
962 object *self;
963 object *v;
964{
965 return min_max(v, -1);
966}
967
968static object *
969builtin_max(self, v)
970 object *self;
971 object *v;
972{
973 return min_max(v, 1);
974}
975
976static object *
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000977builtin_oct(self, args)
Guido van Rossum006bcd41991-10-24 14:54:44 +0000978 object *self;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000979 object *args;
Guido van Rossum006bcd41991-10-24 14:54:44 +0000980{
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000981 object *v;
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000982 number_methods *nb;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000983
984 if (!newgetargs(args, "O:oct", &v))
985 return NULL;
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000986 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");
990 return NULL;
Guido van Rossum006bcd41991-10-24 14:54:44 +0000991 }
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000992 return (*nb->nb_oct)(v);
Guido van Rossum006bcd41991-10-24 14:54:44 +0000993}
994
995static object *
Guido van Rossum94390a41992-08-14 15:14:30 +0000996builtin_open(self, args)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000997 object *self;
Guido van Rossum94390a41992-08-14 15:14:30 +0000998 object *args;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000999{
Guido van Rossum2d951851994-08-29 12:52:16 +00001000 char *name;
1001 char *mode = "r";
1002 int bufsize = -1;
1003 object *f;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001004
1005 if (!newgetargs(args, "s|si:open", &name, &mode, &bufsize))
Guido van Rossum3f5da241990-12-20 15:06:42 +00001006 return NULL;
Guido van Rossum2d951851994-08-29 12:52:16 +00001007 f = newfileobject(name, mode);
1008 if (f != NULL)
1009 setfilebufsize(f, bufsize);
1010 return f;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001011}
1012
1013static object *
Guido van Rossum94390a41992-08-14 15:14:30 +00001014builtin_ord(self, args)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001015 object *self;
Guido van Rossum94390a41992-08-14 15:14:30 +00001016 object *args;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001017{
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001018 char c;
1019
1020 if (!newgetargs(args, "c:ord", &c))
Guido van Rossum3f5da241990-12-20 15:06:42 +00001021 return NULL;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001022 return newintobject((long)(c & 0xff));
Guido van Rossum3f5da241990-12-20 15:06:42 +00001023}
1024
1025static object *
Guido van Rossum6a00cd81995-01-07 12:39:01 +00001026do_pow(v, w)
1027 object *v, *w;
Guido van Rossumd4905451991-05-05 20:00:36 +00001028{
Guido van Rossum6a00cd81995-01-07 12:39:01 +00001029 object *res;
1030 if (is_instanceobject(v) || is_instanceobject(w))
1031 return instancebinop(v, w, "__pow__", "__rpow__", do_pow);
Guido van Rossumc6bb8f71991-07-01 18:42:41 +00001032 if (v->ob_type->tp_as_number == NULL ||
Guido van Rossumdf05ac61994-08-29 12:52:37 +00001033 w->ob_type->tp_as_number == NULL) {
Guido van Rossumd4905451991-05-05 20:00:36 +00001034 err_setstr(TypeError, "pow() requires numeric arguments");
1035 return NULL;
1036 }
Guido van Rossum8a5c5d21996-01-12 01:09:56 +00001037 if (
1038#ifndef WITHOUT_COMPLEX
1039 !is_complexobject(v) &&
1040#endif
1041 is_floatobject(w) && getfloatvalue(v) < 0.0) {
Guido van Rossum8a1e8eb1995-02-18 14:51:32 +00001042 if (!err_occurred())
1043 err_setstr(ValueError, "negative number to float power");
Guido van Rossum21651581995-02-10 16:57:16 +00001044 return NULL;
1045 }
Guido van Rossumc6bb8f71991-07-01 18:42:41 +00001046 if (coerce(&v, &w) != 0)
1047 return NULL;
Guido van Rossum6a00cd81995-01-07 12:39:01 +00001048 res = (*v->ob_type->tp_as_number->nb_power)(v, w, None);
Guido van Rossumc6bb8f71991-07-01 18:42:41 +00001049 DECREF(v);
1050 DECREF(w);
Guido van Rossum6a00cd81995-01-07 12:39:01 +00001051 return res;
1052}
1053
1054static object *
1055builtin_pow(self, args)
1056 object *self;
1057 object *args;
1058{
1059 object *v, *w, *z = None, *res;
1060 object *v1, *z1, *w2, *z2;
1061
1062 if (!newgetargs(args, "OO|O:pow", &v, &w, &z))
1063 return NULL;
1064 if (z == None)
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");
1073 return NULL;
1074 }
1075 if (coerce(&v, &w) != 0)
1076 return NULL;
1077 res = NULL;
1078 v1 = v;
1079 z1 = z;
1080 if (coerce(&v1, &z1) != 0)
1081 goto error2;
1082 w2 = w;
1083 z2 = z1;
1084 if (coerce(&w2, &z2) != 0)
1085 goto error1;
1086 res = (*v1->ob_type->tp_as_number->nb_power)(v1, w2, z2);
1087 DECREF(w2);
1088 DECREF(z2);
1089 error1:
1090 DECREF(v1);
1091 DECREF(z1);
1092 error2:
1093 DECREF(v);
1094 DECREF(w);
1095 return res;
Guido van Rossumd4905451991-05-05 20:00:36 +00001096}
1097
1098static object *
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001099builtin_range(self, args)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001100 object *self;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001101 object *args;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001102{
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001103 long ilow = 0, ihigh = 0, istep = 1;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001104 int i, n;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001105 object *v;
1106
1107 if (gettuplesize(args) <= 1) {
1108 if (!newgetargs(args,
Guido van Rossum0865dd91995-01-17 16:30:22 +00001109 "l;range() requires 1-3 int arguments",
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001110 &ihigh))
1111 return NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001112 }
1113 else {
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001114 if (!newgetargs(args,
Guido van Rossum0865dd91995-01-17 16:30:22 +00001115 "ll|l;range() requires 1-3 int arguments",
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001116 &ilow, &ihigh, &istep))
Guido van Rossum3f5da241990-12-20 15:06:42 +00001117 return NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001118 }
1119 if (istep == 0) {
Guido van Rossum50afb7a1991-12-10 13:52:31 +00001120 err_setstr(ValueError, "zero step for range()");
Guido van Rossum3f5da241990-12-20 15:06:42 +00001121 return NULL;
1122 }
1123 /* XXX ought to check overflow of subtraction */
1124 if (istep > 0)
1125 n = (ihigh - ilow + istep - 1) / istep;
1126 else
1127 n = (ihigh - ilow + istep + 1) / istep;
1128 if (n < 0)
1129 n = 0;
1130 v = newlistobject(n);
1131 if (v == NULL)
1132 return NULL;
1133 for (i = 0; i < n; i++) {
1134 object *w = newintobject(ilow);
1135 if (w == NULL) {
1136 DECREF(v);
1137 return NULL;
1138 }
1139 setlistitem(v, i, w);
1140 ilow += istep;
1141 }
1142 return v;
1143}
1144
1145static object *
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001146builtin_xrange(self, args)
Guido van Rossum12d12c51993-10-26 17:58:25 +00001147 object *self;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001148 object *args;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001149{
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001150 long ilow = 0, ihigh = 0, istep = 1;
Guido van Rossum0865dd91995-01-17 16:30:22 +00001151 long n;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001152
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001153 if (gettuplesize(args) <= 1) {
1154 if (!newgetargs(args,
Guido van Rossum0865dd91995-01-17 16:30:22 +00001155 "l;xrange() requires 1-3 int arguments",
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001156 &ihigh))
1157 return NULL;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001158 }
1159 else {
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001160 if (!newgetargs(args,
Guido van Rossum0865dd91995-01-17 16:30:22 +00001161 "ll|l;xrange() requires 1-3 int arguments",
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001162 &ilow, &ihigh, &istep))
Guido van Rossum12d12c51993-10-26 17:58:25 +00001163 return NULL;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001164 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001165 if (istep == 0) {
Guido van Rossum12d12c51993-10-26 17:58:25 +00001166 err_setstr(ValueError, "zero step for xrange()");
1167 return NULL;
1168 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001169 /* XXX ought to check overflow of subtraction */
1170 if (istep > 0)
1171 n = (ihigh - ilow + istep - 1) / istep;
1172 else
1173 n = (ihigh - ilow + istep + 1) / istep;
1174 if (n < 0)
1175 n = 0;
1176 return newrangeobject(ilow, n, istep, 1);
Guido van Rossum12d12c51993-10-26 17:58:25 +00001177}
1178
Guido van Rossum872537c1995-07-07 22:43:42 +00001179extern char *my_readline PROTO((char *));
1180
Guido van Rossum12d12c51993-10-26 17:58:25 +00001181static object *
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001182builtin_raw_input(self, args)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001183 object *self;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001184 object *args;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001185{
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001186 object *v = NULL;
1187 object *f;
1188
1189 if (!newgetargs(args, "|O:[raw_]input", &v))
Guido van Rossum3165fe61992-09-25 21:59:05 +00001190 return NULL;
Guido van Rossum872537c1995-07-07 22:43:42 +00001191 if (getfilefile(sysget("stdin")) == stdin &&
Guido van Rossum53bb7ff1995-07-26 16:26:31 +00001192 getfilefile(sysget("stdout")) == stdout &&
1193 isatty(fileno(stdin)) && isatty(fileno(stdout))) {
Guido van Rossum872537c1995-07-07 22:43:42 +00001194 object *po;
1195 char *prompt;
1196 char *s;
1197 object *result;
1198 if (v != NULL) {
1199 po = strobject(v);
1200 if (po == NULL)
1201 return NULL;
1202 prompt = getstringvalue(po);
1203 }
1204 else {
1205 po = NULL;
1206 prompt = "";
1207 }
1208 s = my_readline(prompt);
1209 XDECREF(po);
1210 if (s == NULL) {
1211 err_set(KeyboardInterrupt);
1212 return NULL;
1213 }
1214 if (*s == '\0') {
1215 err_set(EOFError);
1216 result = NULL;
1217 }
1218 else { /* strip trailing '\n' */
1219 result = newsizedstringobject(s, strlen(s)-1);
1220 }
1221 free(s);
1222 return result;
1223 }
Guido van Rossum90933611991-06-07 16:10:43 +00001224 if (v != NULL) {
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001225 f = sysget("stdout");
1226 if (f == NULL) {
1227 err_setstr(RuntimeError, "lost sys.stdout");
1228 return NULL;
1229 }
1230 flushline();
Guido van Rossum3165fe61992-09-25 21:59:05 +00001231 if (writeobject(v, f, PRINT_RAW) != 0)
Guido van Rossum90933611991-06-07 16:10:43 +00001232 return NULL;
1233 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001234 f = sysget("stdin");
1235 if (f == NULL) {
1236 err_setstr(RuntimeError, "lost sys.stdin");
1237 return NULL;
1238 }
1239 return filegetline(f, -1);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001240}
1241
1242static object *
Guido van Rossum12d12c51993-10-26 17:58:25 +00001243builtin_reduce(self, args)
1244 object *self;
1245 object *args;
1246{
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001247 object *seq, *func, *result = NULL;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001248 sequence_methods *sqf;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001249 register int i;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001250
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001251 if (!newgetargs(args, "OO|O:reduce", &func, &seq, &result))
1252 return NULL;
1253 if (result != NULL)
Guido van Rossum2d951851994-08-29 12:52:16 +00001254 INCREF(result);
Guido van Rossum12d12c51993-10-26 17:58:25 +00001255
1256 if ((sqf = seq->ob_type->tp_as_sequence) == NULL) {
1257 err_setstr(TypeError,
1258 "2nd argument to reduce() must be a sequence object");
1259 return NULL;
1260 }
1261
Guido van Rossum12d12c51993-10-26 17:58:25 +00001262 if ((args = newtupleobject(2)) == NULL)
Guido van Rossum2d951851994-08-29 12:52:16 +00001263 goto Fail;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001264
Guido van Rossum2d951851994-08-29 12:52:16 +00001265 for (i = 0; ; ++i) {
Guido van Rossum12d12c51993-10-26 17:58:25 +00001266 object *op2;
1267
1268 if (args->ob_refcnt > 1) {
1269 DECREF(args);
1270 if ((args = newtupleobject(2)) == NULL)
Guido van Rossum2d951851994-08-29 12:52:16 +00001271 goto Fail;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001272 }
1273
Guido van Rossum2d951851994-08-29 12:52:16 +00001274 if ((op2 = (*sqf->sq_item)(seq, i)) == NULL) {
1275 if (err_occurred() == IndexError) {
1276 err_clear();
1277 break;
1278 }
1279 goto Fail;
1280 }
Guido van Rossum12d12c51993-10-26 17:58:25 +00001281
Guido van Rossum2d951851994-08-29 12:52:16 +00001282 if (result == NULL)
1283 result = op2;
1284 else {
1285 settupleitem(args, 0, result);
1286 settupleitem(args, 1, op2);
1287 if ((result = call_object(func, args)) == NULL)
1288 goto Fail;
1289 }
Guido van Rossum12d12c51993-10-26 17:58:25 +00001290 }
1291
1292 DECREF(args);
Guido van Rossum12d12c51993-10-26 17:58:25 +00001293
Guido van Rossum2d951851994-08-29 12:52:16 +00001294 if (result == NULL)
1295 err_setstr(TypeError,
1296 "reduce of empty sequence with no initial value");
1297
Guido van Rossum12d12c51993-10-26 17:58:25 +00001298 return result;
1299
Guido van Rossum2d951851994-08-29 12:52:16 +00001300Fail:
1301 XDECREF(args);
1302 XDECREF(result);
Guido van Rossum12d12c51993-10-26 17:58:25 +00001303 return NULL;
1304}
1305
1306static object *
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001307builtin_reload(self, args)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001308 object *self;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001309 object *args;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001310{
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001311 object *v;
1312
1313 if (!newgetargs(args, "O:reload", &v))
1314 return NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001315 return reload_module(v);
1316}
1317
1318static object *
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001319builtin_repr(self, args)
Guido van Rossumc89705d1992-11-26 08:54:07 +00001320 object *self;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001321 object *args;
Guido van Rossumc89705d1992-11-26 08:54:07 +00001322{
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001323 object *v;
1324
1325 if (!newgetargs(args, "O:repr", &v))
Guido van Rossumc89705d1992-11-26 08:54:07 +00001326 return NULL;
Guido van Rossumc89705d1992-11-26 08:54:07 +00001327 return reprobject(v);
1328}
1329
1330static object *
Guido van Rossum9e51f9b1993-02-12 16:29:05 +00001331builtin_round(self, args)
1332 object *self;
1333 object *args;
1334{
1335 extern double floor PROTO((double));
1336 extern double ceil PROTO((double));
1337 double x;
1338 double f;
1339 int ndigits = 0;
Guido van Rossum9e51f9b1993-02-12 16:29:05 +00001340 int i;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001341
1342 if (!newgetargs(args, "d|i:round", &x, &ndigits))
Guido van Rossum9e51f9b1993-02-12 16:29:05 +00001343 return NULL;
Guido van Rossum9e51f9b1993-02-12 16:29:05 +00001344 f = 1.0;
1345 for (i = ndigits; --i >= 0; )
1346 f = f*10.0;
1347 for (i = ndigits; ++i <= 0; )
1348 f = f*0.1;
1349 if (x >= 0.0)
1350 return newfloatobject(floor(x*f + 0.5) / f);
1351 else
1352 return newfloatobject(ceil(x*f - 0.5) / f);
1353}
1354
1355static object *
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001356builtin_str(self, args)
Guido van Rossumc89705d1992-11-26 08:54:07 +00001357 object *self;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001358 object *args;
Guido van Rossumc89705d1992-11-26 08:54:07 +00001359{
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001360 object *v;
1361
1362 if (!newgetargs(args, "O:str", &v))
Guido van Rossumc89705d1992-11-26 08:54:07 +00001363 return NULL;
Guido van Rossumc6004111993-11-05 10:22:19 +00001364 return strobject(v);
Guido van Rossumc89705d1992-11-26 08:54:07 +00001365}
1366
1367static object *
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001368builtin_tuple(self, args)
Guido van Rossumcae027b1994-08-29 12:53:11 +00001369 object *self;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001370 object *args;
Guido van Rossumcae027b1994-08-29 12:53:11 +00001371{
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001372 object *v;
Guido van Rossumcae027b1994-08-29 12:53:11 +00001373 sequence_methods *sqf;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001374
1375 if (!newgetargs(args, "O:tuple", &v))
1376 return NULL;
Guido van Rossumcae027b1994-08-29 12:53:11 +00001377 if (is_tupleobject(v)) {
1378 INCREF(v);
1379 return v;
1380 }
Guido van Rossume4ab6471994-08-30 12:38:05 +00001381 if (is_listobject(v))
1382 return listtuple(v);
Guido van Rossumcae027b1994-08-29 12:53:11 +00001383 if (is_stringobject(v)) {
1384 int n = getstringsize(v);
1385 object *t = newtupleobject(n);
1386 if (t != NULL) {
1387 int i;
1388 char *p = getstringvalue(v);
1389 for (i = 0; i < n; i++) {
1390 object *item = newsizedstringobject(p+i, 1);
1391 if (item == NULL) {
1392 DECREF(t);
1393 t = NULL;
1394 break;
1395 }
1396 settupleitem(t, i, item);
1397 }
1398 }
1399 return t;
1400 }
1401 /* Generic sequence object */
1402 if ((sqf = v->ob_type->tp_as_sequence) != NULL) {
1403 int n = (*sqf->sq_length)(v);
1404 int i;
1405 object *t;
1406 if (n < 0)
1407 return NULL;
1408 t = newtupleobject(n);
1409 if (t == NULL)
1410 return NULL;
1411 for (i = 0; i < n; i++) {
1412 object *item = (*sqf->sq_item)(v, i);
1413 if (item == NULL) {
1414 DECREF(t);
1415 t = NULL;
1416 break;
1417 }
1418 settupleitem(t, i, item);
1419 }
1420 /* XXX Should support indefinite-length sequences */
1421 return t;
1422 }
1423 /* None of the above */
1424 err_setstr(TypeError, "tuple() argument must be a sequence");
1425 return NULL;
1426}
1427
1428static object *
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001429builtin_type(self, args)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001430 object *self;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001431 object *args;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001432{
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001433 object *v;
1434
1435 if (!newgetargs(args, "O:type", &v))
Guido van Rossum3f5da241990-12-20 15:06:42 +00001436 return NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001437 v = (object *)v->ob_type;
1438 INCREF(v);
1439 return v;
1440}
1441
Guido van Rossum2d951851994-08-29 12:52:16 +00001442static object *
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001443builtin_vars(self, args)
Guido van Rossum2d951851994-08-29 12:52:16 +00001444 object *self;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001445 object *args;
Guido van Rossum2d951851994-08-29 12:52:16 +00001446{
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001447 object *v = NULL;
Guido van Rossum2d951851994-08-29 12:52:16 +00001448 object *d;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001449
1450 if (!newgetargs(args, "|O:vars", &v))
1451 return NULL;
Guido van Rossum2d951851994-08-29 12:52:16 +00001452 if (v == NULL) {
1453 d = getlocals();
Guido van Rossum53bb7ff1995-07-26 16:26:31 +00001454 if (d == NULL) {
1455 if (!err_occurred())
1456 err_setstr(SystemError, "no locals!?");
1457 }
1458 else
1459 INCREF(d);
Guido van Rossum2d951851994-08-29 12:52:16 +00001460 }
1461 else {
1462 d = getattr(v, "__dict__");
1463 if (d == NULL) {
1464 err_setstr(TypeError,
1465 "vars() argument must have __dict__ attribute");
1466 return NULL;
1467 }
1468 }
1469 return d;
1470}
1471
Guido van Rossum3f5da241990-12-20 15:06:42 +00001472static struct methodlist builtin_methods[] = {
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001473 {"__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},
Guido van Rossum8a5c5d21996-01-12 01:09:56 +00001481#ifndef WITHOUT_COMPLEX
1482 {"complex", builtin_complex, 1},
1483#endif
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001484 {"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},
Guido van Rossum872537c1995-07-07 22:43:42 +00001492 {"globals", builtin_globals, 1},
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001493 {"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},
Guido van Rossumd1705771996-04-09 02:41:06 +00001500 {"list", builtin_list, 1},
Guido van Rossum872537c1995-07-07 22:43:42 +00001501 {"locals", builtin_locals, 1},
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001502 {"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},
Guido van Rossum030ae171994-11-10 22:33:19 +00001509 {"pow", builtin_pow, 1},
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001510 {"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},
Guido van Rossumc02e15c1991-12-16 13:03:00 +00001522 {NULL, NULL},
Guido van Rossum3f5da241990-12-20 15:06:42 +00001523};
1524
Guido van Rossum0865dd91995-01-17 16:30:22 +00001525static object *builtin_mod;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001526static object *builtin_dict;
1527
1528object *
Guido van Rossum0865dd91995-01-17 16:30:22 +00001529getbuiltinmod()
1530{
1531 return builtin_mod;
1532}
1533
1534object *
Guido van Rossum6135a871995-01-09 17:53:26 +00001535getbuiltindict()
Guido van Rossum3f5da241990-12-20 15:06:42 +00001536{
Guido van Rossum6135a871995-01-09 17:53:26 +00001537 return builtin_dict;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001538}
1539
1540/* Predefined exceptions */
1541
Guido van Rossum25831651993-05-19 14:50:45 +00001542object *AccessError;
Guido van Rossumfb905c31991-12-16 15:42:38 +00001543object *AttributeError;
Guido van Rossum25831651993-05-19 14:50:45 +00001544object *ConflictError;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001545object *EOFError;
Guido van Rossumfb905c31991-12-16 15:42:38 +00001546object *IOError;
Guido van Rossumed7711b1991-12-24 13:24:53 +00001547object *ImportError;
Guido van Rossumfb905c31991-12-16 15:42:38 +00001548object *IndexError;
1549object *KeyError;
1550object *KeyboardInterrupt;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001551object *MemoryError;
1552object *NameError;
Guido van Rossum50afb7a1991-12-10 13:52:31 +00001553object *OverflowError;
Guido van Rossumfb905c31991-12-16 15:42:38 +00001554object *RuntimeError;
Guido van Rossumc02e15c1991-12-16 13:03:00 +00001555object *SyntaxError;
Guido van Rossumfb905c31991-12-16 15:42:38 +00001556object *SystemError;
Guido van Rossum768a3f01991-12-31 13:13:47 +00001557object *SystemExit;
Guido van Rossumfb905c31991-12-16 15:42:38 +00001558object *TypeError;
1559object *ValueError;
1560object *ZeroDivisionError;
Guido van Rossum50afb7a1991-12-10 13:52:31 +00001561
Guido van Rossum3f5da241990-12-20 15:06:42 +00001562static object *
Guido van Rossumfb905c31991-12-16 15:42:38 +00001563newstdexception(name)
1564 char *name;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001565{
Guido van Rossumfb905c31991-12-16 15:42:38 +00001566 object *v = newstringobject(name);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001567 if (v == NULL || dictinsert(builtin_dict, name, v) != 0)
1568 fatal("no mem for new standard exception");
1569 return v;
1570}
1571
1572static void
1573initerrors()
1574{
Guido van Rossum25831651993-05-19 14:50:45 +00001575 AccessError = newstdexception("AccessError");
Guido van Rossumed7711b1991-12-24 13:24:53 +00001576 AttributeError = newstdexception("AttributeError");
Guido van Rossum25831651993-05-19 14:50:45 +00001577 ConflictError = newstdexception("ConflictError");
Guido van Rossumfb905c31991-12-16 15:42:38 +00001578 EOFError = newstdexception("EOFError");
Guido van Rossumed7711b1991-12-24 13:24:53 +00001579 IOError = newstdexception("IOError");
1580 ImportError = newstdexception("ImportError");
1581 IndexError = newstdexception("IndexError");
1582 KeyError = newstdexception("KeyError");
1583 KeyboardInterrupt = newstdexception("KeyboardInterrupt");
Guido van Rossumfb905c31991-12-16 15:42:38 +00001584 MemoryError = newstdexception("MemoryError");
1585 NameError = newstdexception("NameError");
Guido van Rossumfb905c31991-12-16 15:42:38 +00001586 OverflowError = newstdexception("OverflowError");
Guido van Rossumed7711b1991-12-24 13:24:53 +00001587 RuntimeError = newstdexception("RuntimeError");
Guido van Rossumfb905c31991-12-16 15:42:38 +00001588 SyntaxError = newstdexception("SyntaxError");
Guido van Rossumed7711b1991-12-24 13:24:53 +00001589 SystemError = newstdexception("SystemError");
Guido van Rossum768a3f01991-12-31 13:13:47 +00001590 SystemExit = newstdexception("SystemExit");
Guido van Rossumed7711b1991-12-24 13:24:53 +00001591 TypeError = newstdexception("TypeError");
1592 ValueError = newstdexception("ValueError");
1593 ZeroDivisionError = newstdexception("ZeroDivisionError");
Guido van Rossum3f5da241990-12-20 15:06:42 +00001594}
1595
1596void
1597initbuiltin()
1598{
Guido van Rossum0865dd91995-01-17 16:30:22 +00001599 builtin_mod = initmodule("__builtin__", builtin_methods);
1600 builtin_dict = getmoduledict(builtin_mod);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001601 INCREF(builtin_dict);
1602 initerrors();
1603 (void) dictinsert(builtin_dict, "None", None);
1604}
Guido van Rossumc6bb8f71991-07-01 18:42:41 +00001605
Guido van Rossum12d12c51993-10-26 17:58:25 +00001606
Guido van Rossume77a7571993-11-03 15:01:26 +00001607/* Helper for filter(): filter a tuple through a function */
Guido van Rossum12d12c51993-10-26 17:58:25 +00001608
1609static object *
1610filtertuple(func, tuple)
1611 object *func;
1612 object *tuple;
1613{
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00001614 object *result;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001615 register int i, j;
Guido van Rossum2586bf01993-11-01 16:21:44 +00001616 int len = gettuplesize(tuple);
Guido van Rossum12d12c51993-10-26 17:58:25 +00001617
Guido van Rossumb7b45621995-08-04 04:07:45 +00001618 if (len == 0) {
1619 INCREF(tuple);
1620 return tuple;
1621 }
1622
Guido van Rossum2586bf01993-11-01 16:21:44 +00001623 if ((result = newtupleobject(len)) == NULL)
1624 return NULL;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001625
Guido van Rossum12d12c51993-10-26 17:58:25 +00001626 for (i = j = 0; i < len; ++i) {
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00001627 object *item, *good;
1628 int ok;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001629
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00001630 if ((item = gettupleitem(tuple, i)) == NULL)
1631 goto Fail_1;
1632 if (func == None) {
1633 INCREF(item);
1634 good = item;
1635 }
1636 else {
1637 object *arg = mkvalue("(O)", item);
1638 if (arg == NULL)
1639 goto Fail_1;
1640 good = call_object(func, arg);
Guido van Rossum12d12c51993-10-26 17:58:25 +00001641 DECREF(arg);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00001642 if (good == NULL)
Guido van Rossum12d12c51993-10-26 17:58:25 +00001643 goto Fail_1;
1644 }
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00001645 ok = testbool(good);
1646 DECREF(good);
1647 if (ok) {
1648 INCREF(item);
1649 if (settupleitem(result, j++, item) < 0)
1650 goto Fail_1;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001651 }
Guido van Rossum12d12c51993-10-26 17:58:25 +00001652 }
1653
Sjoerd Mullender615194a1993-11-01 13:46:50 +00001654 if (resizetuple(&result, j, 0) < 0)
Guido van Rossum12d12c51993-10-26 17:58:25 +00001655 return NULL;
1656
Guido van Rossum12d12c51993-10-26 17:58:25 +00001657 return result;
1658
Guido van Rossum12d12c51993-10-26 17:58:25 +00001659Fail_1:
Guido van Rossum2586bf01993-11-01 16:21:44 +00001660 DECREF(result);
Guido van Rossum12d12c51993-10-26 17:58:25 +00001661 return NULL;
1662}
1663
1664
Guido van Rossume77a7571993-11-03 15:01:26 +00001665/* Helper for filter(): filter a string through a function */
Guido van Rossum12d12c51993-10-26 17:58:25 +00001666
1667static object *
1668filterstring(func, strobj)
1669 object *func;
1670 object *strobj;
1671{
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00001672 object *result;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001673 register int i, j;
Guido van Rossum2586bf01993-11-01 16:21:44 +00001674 int len = getstringsize(strobj);
Guido van Rossum12d12c51993-10-26 17:58:25 +00001675
Guido van Rossum2586bf01993-11-01 16:21:44 +00001676 if (func == None) {
1677 /* No character is ever false -- share input string */
Guido van Rossum2d951851994-08-29 12:52:16 +00001678 INCREF(strobj);
1679 return strobj;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001680 }
Guido van Rossum2586bf01993-11-01 16:21:44 +00001681 if ((result = newsizedstringobject(NULL, len)) == NULL)
1682 return NULL;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001683
Guido van Rossum12d12c51993-10-26 17:58:25 +00001684 for (i = j = 0; i < len; ++i) {
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00001685 object *item, *arg, *good;
1686 int ok;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001687
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00001688 item = (*strobj->ob_type->tp_as_sequence->sq_item)(strobj, i);
1689 if (item == NULL)
1690 goto Fail_1;
1691 arg = mkvalue("(O)", item);
1692 DECREF(item);
1693 if (arg == NULL)
1694 goto Fail_1;
1695 good = call_object(func, arg);
1696 DECREF(arg);
1697 if (good == NULL)
1698 goto Fail_1;
1699 ok = testbool(good);
1700 DECREF(good);
1701 if (ok)
Guido van Rossum12d12c51993-10-26 17:58:25 +00001702 GETSTRINGVALUE((stringobject *)result)[j++] =
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00001703 GETSTRINGVALUE((stringobject *)item)[0];
Guido van Rossum12d12c51993-10-26 17:58:25 +00001704 }
1705
Guido van Rossum12d12c51993-10-26 17:58:25 +00001706 if (resizestring(&result, j) < 0)
1707 return NULL;
1708
Guido van Rossum12d12c51993-10-26 17:58:25 +00001709 return result;
1710
Guido van Rossum12d12c51993-10-26 17:58:25 +00001711Fail_1:
Guido van Rossum2586bf01993-11-01 16:21:44 +00001712 DECREF(result);
Guido van Rossum12d12c51993-10-26 17:58:25 +00001713 return NULL;
1714}