blob: fd31405733d29d9a0063c1780690f825f63e6775 [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
Guido van Rossumd266eb41996-10-25 14:44:06 +00007Permission to use, copy, modify, and distribute this software and its
8documentation for any purpose and without fee is hereby granted,
Guido van Rossumf70e43a1991-02-19 12:39:46 +00009provided that the above copyright notice appear in all copies and that
Guido van Rossumd266eb41996-10-25 14:44:06 +000010both that copyright notice and this permission notice appear in
Guido van Rossumf70e43a1991-02-19 12:39:46 +000011supporting documentation, and that the names of Stichting Mathematisch
Guido van Rossumd266eb41996-10-25 14:44:06 +000012Centrum or CWI or Corporation for National Research Initiatives or
13CNRI not be used in advertising or publicity pertaining to
14distribution of the software without specific, written prior
15permission.
Guido van Rossumf70e43a1991-02-19 12:39:46 +000016
Guido van Rossumd266eb41996-10-25 14:44:06 +000017While CWI is the initial source for this software, a modified version
18is made available by the Corporation for National Research Initiatives
19(CNRI) at the Internet address ftp://ftp.python.org.
20
21STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH
22REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF
23MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH
24CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
25DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
26PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
27TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
28PERFORMANCE OF THIS SOFTWARE.
Guido van Rossumf70e43a1991-02-19 12:39:46 +000029
30******************************************************************/
31
Guido van Rossum3f5da241990-12-20 15:06:42 +000032/* Built-in functions */
33
Guido van Rossum79f25d91997-04-29 20:08:16 +000034#include "Python.h"
Guido van Rossum3f5da241990-12-20 15:06:42 +000035
36#include "node.h"
Guido van Rossum5b722181993-03-30 17:46:03 +000037#include "compile.h"
38#include "eval.h"
Guido van Rossum3f5da241990-12-20 15:06:42 +000039
Guido van Rossumfe4b6ee1996-08-08 18:49:41 +000040#include "mymath.h"
41
Guido van Rossum6bf62da1997-04-11 20:37:35 +000042#include <ctype.h>
43
Guido van Rossum1a2c5cb1996-12-10 15:37:36 +000044#ifdef HAVE_UNISTD_H
45#include <unistd.h>
46#endif
47
Guido van Rossum12d12c51993-10-26 17:58:25 +000048/* Forward */
Guido van Rossum79f25d91997-04-29 20:08:16 +000049static PyObject *filterstring Py_PROTO((PyObject *, PyObject *));
50static PyObject *filtertuple Py_PROTO((PyObject *, PyObject *));
51static PyObject *int_from_string Py_PROTO((PyObject *));
52static PyObject *long_from_string Py_PROTO((PyObject *));
53static PyObject *float_from_string Py_PROTO((PyObject *));
Guido van Rossum12d12c51993-10-26 17:58:25 +000054
Guido van Rossum79f25d91997-04-29 20:08:16 +000055static PyObject *
Guido van Rossum1ae940a1995-01-02 19:04:15 +000056builtin___import__(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +000057 PyObject *self;
58 PyObject *args;
Guido van Rossum3f5da241990-12-20 15:06:42 +000059{
Guido van Rossum1ae940a1995-01-02 19:04:15 +000060 char *name;
Guido van Rossum79f25d91997-04-29 20:08:16 +000061 PyObject *globals = NULL;
62 PyObject *locals = NULL;
63 PyObject *fromlist = NULL;
Guido van Rossum1ae940a1995-01-02 19:04:15 +000064
Guido van Rossum79f25d91997-04-29 20:08:16 +000065 if (!PyArg_ParseTuple(args, "s|OOO:__import__",
Guido van Rossum24c13741995-02-14 09:42:43 +000066 &name, &globals, &locals, &fromlist))
Guido van Rossum1ae940a1995-01-02 19:04:15 +000067 return NULL;
Guido van Rossumaee0bad1997-09-05 07:33:22 +000068 return PyImport_ImportModuleEx(name, globals, locals, fromlist);
Guido van Rossum1ae940a1995-01-02 19:04:15 +000069}
70
71
Guido van Rossum79f25d91997-04-29 20:08:16 +000072static PyObject *
Guido van Rossum1ae940a1995-01-02 19:04:15 +000073builtin_abs(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +000074 PyObject *self;
75 PyObject *args;
Guido van Rossum1ae940a1995-01-02 19:04:15 +000076{
Guido van Rossum79f25d91997-04-29 20:08:16 +000077 PyObject *v;
78 PyNumberMethods *nm;
Guido van Rossum1ae940a1995-01-02 19:04:15 +000079
Guido van Rossum79f25d91997-04-29 20:08:16 +000080 if (!PyArg_ParseTuple(args, "O:abs", &v))
Guido van Rossum1ae940a1995-01-02 19:04:15 +000081 return NULL;
82 if ((nm = v->ob_type->tp_as_number) == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +000083 PyErr_SetString(PyExc_TypeError,
84 "abs() requires numeric argument");
Guido van Rossumd4905451991-05-05 20:00:36 +000085 return NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +000086 }
Guido van Rossumd4905451991-05-05 20:00:36 +000087 return (*nm->nb_absolute)(v);
Guido van Rossum3f5da241990-12-20 15:06:42 +000088}
89
Guido van Rossum79f25d91997-04-29 20:08:16 +000090static PyObject *
Guido van Rossum94390a41992-08-14 15:14:30 +000091builtin_apply(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +000092 PyObject *self;
93 PyObject *args;
Guido van Rossumc02e15c1991-12-16 13:03:00 +000094{
Guido van Rossum79f25d91997-04-29 20:08:16 +000095 PyObject *func, *alist = NULL, *kwdict = NULL;
Guido van Rossum1ae940a1995-01-02 19:04:15 +000096
Guido van Rossum79f25d91997-04-29 20:08:16 +000097 if (!PyArg_ParseTuple(args, "O|OO:apply", &func, &alist, &kwdict))
Guido van Rossumc02e15c1991-12-16 13:03:00 +000098 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +000099 if (alist != NULL && !PyTuple_Check(alist)) {
100 PyErr_SetString(PyExc_TypeError,
101 "apply() 2nd argument must be tuple");
Guido van Rossum2d951851994-08-29 12:52:16 +0000102 return NULL;
103 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000104 if (kwdict != NULL && !PyDict_Check(kwdict)) {
105 PyErr_SetString(PyExc_TypeError,
Guido van Rossum681d79a1995-07-18 14:51:37 +0000106 "apply() 3rd argument must be dictionary");
107 return NULL;
108 }
109 return PyEval_CallObjectWithKeywords(func, alist, kwdict);
Guido van Rossumc02e15c1991-12-16 13:03:00 +0000110}
111
Guido van Rossum79f25d91997-04-29 20:08:16 +0000112static PyObject *
Guido van Rossum2d951851994-08-29 12:52:16 +0000113builtin_callable(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000114 PyObject *self;
115 PyObject *args;
Guido van Rossum2d951851994-08-29 12:52:16 +0000116{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000117 PyObject *v;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000118
Guido van Rossum79f25d91997-04-29 20:08:16 +0000119 if (!PyArg_ParseTuple(args, "O:callable", &v))
Guido van Rossum2d951851994-08-29 12:52:16 +0000120 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000121 return PyInt_FromLong((long)PyCallable_Check(v));
Guido van Rossum2d951851994-08-29 12:52:16 +0000122}
123
Guido van Rossum79f25d91997-04-29 20:08:16 +0000124static PyObject *
Guido van Rossume77a7571993-11-03 15:01:26 +0000125builtin_filter(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000126 PyObject *self;
127 PyObject *args;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000128{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000129 PyObject *func, *seq, *result;
130 PySequenceMethods *sqf;
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000131 int len;
132 register int i, j;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000133
Guido van Rossum79f25d91997-04-29 20:08:16 +0000134 if (!PyArg_ParseTuple(args, "OO:filter", &func, &seq))
Guido van Rossum12d12c51993-10-26 17:58:25 +0000135 return NULL;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000136
Guido van Rossum79f25d91997-04-29 20:08:16 +0000137 if (PyString_Check(seq)) {
138 PyObject *r = filterstring(func, seq);
Guido van Rossum12d12c51993-10-26 17:58:25 +0000139 return r;
140 }
141
Guido van Rossum79f25d91997-04-29 20:08:16 +0000142 if (PyTuple_Check(seq)) {
143 PyObject *r = filtertuple(func, seq);
Guido van Rossum12d12c51993-10-26 17:58:25 +0000144 return r;
145 }
146
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000147 if ((sqf = seq->ob_type->tp_as_sequence) == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000148 PyErr_SetString(PyExc_TypeError,
Guido van Rossume77a7571993-11-03 15:01:26 +0000149 "argument 2 to filter() must be a sequence type");
Guido van Rossum12d12c51993-10-26 17:58:25 +0000150 goto Fail_2;
151 }
152
153 if ((len = (*sqf->sq_length)(seq)) < 0)
154 goto Fail_2;
155
Guido van Rossum79f25d91997-04-29 20:08:16 +0000156 if (PyList_Check(seq) && seq->ob_refcnt == 1) {
157 Py_INCREF(seq);
Guido van Rossum12d12c51993-10-26 17:58:25 +0000158 result = seq;
159 }
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000160 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000161 if ((result = PyList_New(len)) == NULL)
Guido van Rossum12d12c51993-10-26 17:58:25 +0000162 goto Fail_2;
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000163 }
Guido van Rossum12d12c51993-10-26 17:58:25 +0000164
Guido van Rossum2d951851994-08-29 12:52:16 +0000165 for (i = j = 0; ; ++i) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000166 PyObject *item, *good;
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000167 int ok;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000168
Guido van Rossum2d951851994-08-29 12:52:16 +0000169 if ((item = (*sqf->sq_item)(seq, i)) == NULL) {
170 if (i < len)
171 goto Fail_1;
Barry Warsawcde8b1b1997-08-22 21:14:38 +0000172 if (PyErr_ExceptionMatches(PyExc_IndexError)) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000173 PyErr_Clear();
Guido van Rossum2d951851994-08-29 12:52:16 +0000174 break;
175 }
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000176 goto Fail_1;
Guido van Rossum2d951851994-08-29 12:52:16 +0000177 }
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000178
Guido van Rossum79f25d91997-04-29 20:08:16 +0000179 if (func == Py_None) {
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000180 good = item;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000181 Py_INCREF(good);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000182 }
183 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000184 PyObject *arg = Py_BuildValue("(O)", item);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000185 if (arg == NULL)
186 goto Fail_1;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000187 good = PyEval_CallObject(func, arg);
188 Py_DECREF(arg);
Guido van Rossum58b68731995-01-10 17:40:55 +0000189 if (good == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000190 Py_DECREF(item);
Guido van Rossum12d12c51993-10-26 17:58:25 +0000191 goto Fail_1;
Guido van Rossum58b68731995-01-10 17:40:55 +0000192 }
Guido van Rossum12d12c51993-10-26 17:58:25 +0000193 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000194 ok = PyObject_IsTrue(good);
195 Py_DECREF(good);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000196 if (ok) {
Guido van Rossum2d951851994-08-29 12:52:16 +0000197 if (j < len) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000198 if (PyList_SetItem(result, j++, item) < 0)
Guido van Rossum2d951851994-08-29 12:52:16 +0000199 goto Fail_1;
200 }
201 else {
202 j++;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000203 if (PyList_Append(result, item) < 0)
Guido van Rossum2d951851994-08-29 12:52:16 +0000204 goto Fail_1;
205 }
Guido van Rossum58b68731995-01-10 17:40:55 +0000206 } else {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000207 Py_DECREF(item);
Guido van Rossum12d12c51993-10-26 17:58:25 +0000208 }
Guido van Rossum12d12c51993-10-26 17:58:25 +0000209 }
210
Guido van Rossum12d12c51993-10-26 17:58:25 +0000211
Guido van Rossum79f25d91997-04-29 20:08:16 +0000212 if (j < len && PyList_SetSlice(result, j, len, NULL) < 0)
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000213 goto Fail_1;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000214
Guido van Rossum12d12c51993-10-26 17:58:25 +0000215 return result;
216
Guido van Rossum12d12c51993-10-26 17:58:25 +0000217Fail_1:
Guido van Rossum79f25d91997-04-29 20:08:16 +0000218 Py_DECREF(result);
Guido van Rossum12d12c51993-10-26 17:58:25 +0000219Fail_2:
Guido van Rossum12d12c51993-10-26 17:58:25 +0000220 return NULL;
221}
222
Guido van Rossum79f25d91997-04-29 20:08:16 +0000223static PyObject *
Guido van Rossum94390a41992-08-14 15:14:30 +0000224builtin_chr(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000225 PyObject *self;
226 PyObject *args;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000227{
228 long x;
229 char s[1];
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000230
Guido van Rossum79f25d91997-04-29 20:08:16 +0000231 if (!PyArg_ParseTuple(args, "l:chr", &x))
Guido van Rossum3f5da241990-12-20 15:06:42 +0000232 return NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000233 if (x < 0 || x >= 256) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000234 PyErr_SetString(PyExc_ValueError,
235 "chr() arg not in range(256)");
Guido van Rossum3f5da241990-12-20 15:06:42 +0000236 return NULL;
237 }
Guido van Rossum6bf62da1997-04-11 20:37:35 +0000238 s[0] = (char)x;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000239 return PyString_FromStringAndSize(s, 1);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000240}
241
Guido van Rossum79f25d91997-04-29 20:08:16 +0000242static PyObject *
Guido van Rossuma9e7dc11992-10-18 18:53:57 +0000243builtin_cmp(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000244 PyObject *self;
245 PyObject *args;
Guido van Rossuma9e7dc11992-10-18 18:53:57 +0000246{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000247 PyObject *a, *b;
Guido van Rossumc8b6df91997-05-23 00:06:51 +0000248 long c;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000249
Guido van Rossum79f25d91997-04-29 20:08:16 +0000250 if (!PyArg_ParseTuple(args, "OO:cmp", &a, &b))
Guido van Rossuma9e7dc11992-10-18 18:53:57 +0000251 return NULL;
Guido van Rossumc8b6df91997-05-23 00:06:51 +0000252 c = PyObject_Compare(a, b);
253 if (c && PyErr_Occurred())
254 return NULL;
255 return PyInt_FromLong(c);
Guido van Rossuma9e7dc11992-10-18 18:53:57 +0000256}
257
Guido van Rossum79f25d91997-04-29 20:08:16 +0000258static PyObject *
Guido van Rossum5524a591995-01-10 15:26:20 +0000259builtin_coerce(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000260 PyObject *self;
261 PyObject *args;
Guido van Rossum6a00cd81995-01-07 12:39:01 +0000262{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000263 PyObject *v, *w;
264 PyObject *res;
Guido van Rossum5524a591995-01-10 15:26:20 +0000265
Guido van Rossum79f25d91997-04-29 20:08:16 +0000266 if (!PyArg_ParseTuple(args, "OO:coerce", &v, &w))
Guido van Rossum5524a591995-01-10 15:26:20 +0000267 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000268 if (PyNumber_Coerce(&v, &w) < 0)
Guido van Rossum04691fc1992-08-12 15:35:34 +0000269 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000270 res = Py_BuildValue("(OO)", v, w);
271 Py_DECREF(v);
272 Py_DECREF(w);
Guido van Rossum04691fc1992-08-12 15:35:34 +0000273 return res;
274}
275
Guido van Rossum79f25d91997-04-29 20:08:16 +0000276static PyObject *
Guido van Rossum5b722181993-03-30 17:46:03 +0000277builtin_compile(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000278 PyObject *self;
279 PyObject *args;
Guido van Rossum5b722181993-03-30 17:46:03 +0000280{
281 char *str;
282 char *filename;
283 char *startstr;
284 int start;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000285
Guido van Rossum79f25d91997-04-29 20:08:16 +0000286 if (!PyArg_ParseTuple(args, "sss:compile", &str, &filename, &startstr))
Guido van Rossum5b722181993-03-30 17:46:03 +0000287 return NULL;
288 if (strcmp(startstr, "exec") == 0)
Guido van Rossumb05a5c71997-05-07 17:46:13 +0000289 start = Py_file_input;
Guido van Rossum5b722181993-03-30 17:46:03 +0000290 else if (strcmp(startstr, "eval") == 0)
Guido van Rossumb05a5c71997-05-07 17:46:13 +0000291 start = Py_eval_input;
Guido van Rossum872537c1995-07-07 22:43:42 +0000292 else if (strcmp(startstr, "single") == 0)
Guido van Rossumb05a5c71997-05-07 17:46:13 +0000293 start = Py_single_input;
Guido van Rossum5b722181993-03-30 17:46:03 +0000294 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000295 PyErr_SetString(PyExc_ValueError,
Guido van Rossum872537c1995-07-07 22:43:42 +0000296 "compile() mode must be 'exec' or 'eval' or 'single'");
Guido van Rossum5b722181993-03-30 17:46:03 +0000297 return NULL;
298 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000299 return Py_CompileString(str, filename, start);
Guido van Rossum5b722181993-03-30 17:46:03 +0000300}
301
Guido van Rossum8a5c5d21996-01-12 01:09:56 +0000302#ifndef WITHOUT_COMPLEX
303
Guido van Rossum79f25d91997-04-29 20:08:16 +0000304static PyObject *
Guido van Rossum8a5c5d21996-01-12 01:09:56 +0000305builtin_complex(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000306 PyObject *self;
307 PyObject *args;
Guido van Rossum8a5c5d21996-01-12 01:09:56 +0000308{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000309 PyObject *r, *i, *tmp;
310 PyNumberMethods *nbr, *nbi = NULL;
Guido van Rossum530956d1996-07-21 02:27:43 +0000311 Py_complex cr, ci;
Guido van Rossumc6472e91997-03-31 17:15:43 +0000312 int own_r = 0;
Guido van Rossum8a5c5d21996-01-12 01:09:56 +0000313
314 i = NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000315 if (!PyArg_ParseTuple(args, "O|O:complex", &r, &i))
Guido van Rossum8a5c5d21996-01-12 01:09:56 +0000316 return NULL;
317 if ((nbr = r->ob_type->tp_as_number) == NULL ||
Guido van Rossumc6472e91997-03-31 17:15:43 +0000318 nbr->nb_float == NULL ||
319 (i != NULL &&
320 ((nbi = i->ob_type->tp_as_number) == NULL ||
321 nbi->nb_float == NULL))) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000322 PyErr_SetString(PyExc_TypeError,
Guido van Rossum8a5c5d21996-01-12 01:09:56 +0000323 "complex() argument can't be converted to complex");
324 return NULL;
325 }
Guido van Rossumed0af8f1996-12-05 23:18:18 +0000326 /* XXX Hack to support classes with __complex__ method */
Guido van Rossum79f25d91997-04-29 20:08:16 +0000327 if (PyInstance_Check(r)) {
328 static PyObject *complexstr;
329 PyObject *f;
Guido van Rossumed0af8f1996-12-05 23:18:18 +0000330 if (complexstr == NULL) {
Guido van Rossum8d751611997-01-18 08:04:16 +0000331 complexstr = PyString_InternFromString("__complex__");
Guido van Rossumed0af8f1996-12-05 23:18:18 +0000332 if (complexstr == NULL)
333 return NULL;
334 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000335 f = PyObject_GetAttr(r, complexstr);
Guido van Rossumed0af8f1996-12-05 23:18:18 +0000336 if (f == NULL)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000337 PyErr_Clear();
Guido van Rossumed0af8f1996-12-05 23:18:18 +0000338 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000339 PyObject *args = Py_BuildValue("()");
Guido van Rossumed0af8f1996-12-05 23:18:18 +0000340 if (args == NULL)
341 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000342 r = PyEval_CallObject(f, args);
343 Py_DECREF(args);
Guido van Rossumed0af8f1996-12-05 23:18:18 +0000344 if (r == NULL)
345 return NULL;
Guido van Rossumc6472e91997-03-31 17:15:43 +0000346 own_r = 1;
Guido van Rossumed0af8f1996-12-05 23:18:18 +0000347 }
348 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000349 if (PyComplex_Check(r)) {
350 cr = ((PyComplexObject*)r)->cval;
Guido van Rossum730806d1998-04-10 22:27:42 +0000351 if (own_r) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000352 Py_DECREF(r);
Guido van Rossum730806d1998-04-10 22:27:42 +0000353 }
Guido van Rossumc6472e91997-03-31 17:15:43 +0000354 }
Guido van Rossum8a5c5d21996-01-12 01:09:56 +0000355 else {
Guido van Rossumfe4b6ee1996-08-08 18:49:41 +0000356 tmp = (*nbr->nb_float)(r);
Guido van Rossum730806d1998-04-10 22:27:42 +0000357 if (own_r) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000358 Py_DECREF(r);
Guido van Rossum730806d1998-04-10 22:27:42 +0000359 }
Guido van Rossumfe4b6ee1996-08-08 18:49:41 +0000360 if (tmp == NULL)
361 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000362 cr.real = PyFloat_AsDouble(tmp);
363 Py_DECREF(tmp);
Guido van Rossum8a5c5d21996-01-12 01:09:56 +0000364 cr.imag = 0.;
365 }
366 if (i == NULL) {
367 ci.real = 0.;
368 ci.imag = 0.;
369 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000370 else if (PyComplex_Check(i))
371 ci = ((PyComplexObject*)i)->cval;
Guido van Rossum8a5c5d21996-01-12 01:09:56 +0000372 else {
Guido van Rossumc6472e91997-03-31 17:15:43 +0000373 tmp = (*nbi->nb_float)(i);
Guido van Rossumfe4b6ee1996-08-08 18:49:41 +0000374 if (tmp == NULL)
375 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000376 ci.real = PyFloat_AsDouble(tmp);
377 Py_DECREF(tmp);
Guido van Rossum8a5c5d21996-01-12 01:09:56 +0000378 ci.imag = 0.;
379 }
380 cr.real -= ci.imag;
381 cr.imag += ci.real;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000382 return PyComplex_FromCComplex(cr);
Guido van Rossum8a5c5d21996-01-12 01:09:56 +0000383}
384
385#endif
386
Guido van Rossum79f25d91997-04-29 20:08:16 +0000387static PyObject *
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000388builtin_dir(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000389 PyObject *self;
390 PyObject *args;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000391{
Guido van Rossum666b17a1997-05-06 16:36:57 +0000392 static char *attrlist[] = {"__members__", "__methods__", NULL};
393 PyObject *v = NULL, *l = NULL, *m = NULL;
394 PyObject *d, *x;
395 int i;
396 char **s;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000397
Guido van Rossum79f25d91997-04-29 20:08:16 +0000398 if (!PyArg_ParseTuple(args, "|O:dir", &v))
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000399 return NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000400 if (v == NULL) {
Guido van Rossum666b17a1997-05-06 16:36:57 +0000401 x = PyEval_GetLocals();
402 if (x == NULL)
403 goto error;
404 l = PyMapping_Keys(x);
405 if (l == NULL)
406 goto error;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000407 }
408 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000409 d = PyObject_GetAttrString(v, "__dict__");
Guido van Rossum666b17a1997-05-06 16:36:57 +0000410 if (d == NULL)
Guido van Rossum795ba581996-05-23 22:49:07 +0000411 PyErr_Clear();
Guido van Rossum666b17a1997-05-06 16:36:57 +0000412 else {
413 l = PyMapping_Keys(d);
414 if (l == NULL)
415 PyErr_Clear();
416 Py_DECREF(d);
417 }
418 if (l == NULL) {
419 l = PyList_New(0);
420 if (l == NULL)
421 goto error;
422 }
423 for (s = attrlist; *s != NULL; s++) {
424 m = PyObject_GetAttrString(v, *s);
425 if (m == NULL) {
426 PyErr_Clear();
427 continue;
428 }
429 for (i = 0; ; i++) {
430 x = PySequence_GetItem(m, i);
431 if (x == NULL) {
432 PyErr_Clear();
433 break;
434 }
435 if (PyList_Append(l, x) != 0) {
436 Py_DECREF(x);
437 Py_DECREF(m);
438 goto error;
439 }
440 Py_DECREF(x);
441 }
442 Py_DECREF(m);
Guido van Rossum795ba581996-05-23 22:49:07 +0000443 }
Guido van Rossum006bcd41991-10-24 14:54:44 +0000444 }
Guido van Rossum666b17a1997-05-06 16:36:57 +0000445 if (PyList_Sort(l) != 0)
446 goto error;
447 return l;
448 error:
449 Py_XDECREF(l);
450 return NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000451}
452
Guido van Rossum79f25d91997-04-29 20:08:16 +0000453static PyObject *
Guido van Rossum6a00cd81995-01-07 12:39:01 +0000454do_divmod(v, w)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000455 PyObject *v, *w;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000456{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000457 PyObject *res;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000458
Guido van Rossum79f25d91997-04-29 20:08:16 +0000459 if (PyInstance_Check(v) || PyInstance_Check(w))
460 return PyInstance_DoBinOp(v, w, "__divmod__", "__rdivmod__",
Guido van Rossum6a00cd81995-01-07 12:39:01 +0000461 do_divmod);
Guido van Rossumc6bb8f71991-07-01 18:42:41 +0000462 if (v->ob_type->tp_as_number == NULL ||
463 w->ob_type->tp_as_number == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000464 PyErr_SetString(PyExc_TypeError,
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000465 "divmod() requires numeric or class instance arguments");
Guido van Rossum3f5da241990-12-20 15:06:42 +0000466 return NULL;
467 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000468 if (PyNumber_Coerce(&v, &w) != 0)
Guido van Rossumc6bb8f71991-07-01 18:42:41 +0000469 return NULL;
Guido van Rossum6a00cd81995-01-07 12:39:01 +0000470 res = (*v->ob_type->tp_as_number->nb_divmod)(v, w);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000471 Py_DECREF(v);
472 Py_DECREF(w);
Guido van Rossum6a00cd81995-01-07 12:39:01 +0000473 return res;
474}
475
Guido van Rossum79f25d91997-04-29 20:08:16 +0000476static PyObject *
Guido van Rossum6a00cd81995-01-07 12:39:01 +0000477builtin_divmod(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000478 PyObject *self;
479 PyObject *args;
Guido van Rossum6a00cd81995-01-07 12:39:01 +0000480{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000481 PyObject *v, *w;
Guido van Rossum6a00cd81995-01-07 12:39:01 +0000482
Guido van Rossum79f25d91997-04-29 20:08:16 +0000483 if (!PyArg_ParseTuple(args, "OO:divmod", &v, &w))
Guido van Rossum6a00cd81995-01-07 12:39:01 +0000484 return NULL;
485 return do_divmod(v, w);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000486}
487
Guido van Rossum79f25d91997-04-29 20:08:16 +0000488static PyObject *
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000489builtin_eval(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000490 PyObject *self;
491 PyObject *args;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000492{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000493 PyObject *cmd;
494 PyObject *globals = Py_None, *locals = Py_None;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000495 char *str;
Guido van Rossum590baa41993-11-30 13:40:46 +0000496
Guido van Rossum79f25d91997-04-29 20:08:16 +0000497 if (!PyArg_ParseTuple(args, "O|O!O!:eval",
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000498 &cmd,
Guido van Rossum79f25d91997-04-29 20:08:16 +0000499 &PyDict_Type, &globals,
500 &PyDict_Type, &locals))
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000501 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000502 if (globals == Py_None) {
503 globals = PyEval_GetGlobals();
504 if (locals == Py_None)
505 locals = PyEval_GetLocals();
Guido van Rossum6135a871995-01-09 17:53:26 +0000506 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000507 else if (locals == Py_None)
Guido van Rossum6135a871995-01-09 17:53:26 +0000508 locals = globals;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000509 if (PyDict_GetItemString(globals, "__builtins__") == NULL) {
510 if (PyDict_SetItemString(globals, "__builtins__",
511 PyEval_GetBuiltins()) != 0)
Guido van Rossum6135a871995-01-09 17:53:26 +0000512 return NULL;
513 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000514 if (PyCode_Check(cmd))
515 return PyEval_EvalCode((PyCodeObject *) cmd, globals, locals);
516 if (!PyString_Check(cmd)) {
517 PyErr_SetString(PyExc_TypeError,
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000518 "eval() argument 1 must be string or code object");
Guido van Rossum94390a41992-08-14 15:14:30 +0000519 return NULL;
520 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000521 str = PyString_AsString(cmd);
522 if ((int)strlen(str) != PyString_Size(cmd)) {
523 PyErr_SetString(PyExc_ValueError,
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000524 "embedded '\\0' in string arg");
525 return NULL;
Guido van Rossumf08ab0a1992-03-04 16:41:41 +0000526 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000527 while (*str == ' ' || *str == '\t')
528 str++;
Guido van Rossumb05a5c71997-05-07 17:46:13 +0000529 return PyRun_String(str, Py_eval_input, globals, locals);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000530}
531
Guido van Rossum79f25d91997-04-29 20:08:16 +0000532static PyObject *
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000533builtin_execfile(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000534 PyObject *self;
535 PyObject *args;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000536{
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000537 char *filename;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000538 PyObject *globals = Py_None, *locals = Py_None;
539 PyObject *res;
Guido van Rossum0f61f8a1992-02-25 18:55:05 +0000540 FILE* fp;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000541
Guido van Rossum79f25d91997-04-29 20:08:16 +0000542 if (!PyArg_ParseTuple(args, "s|O!O!:execfile",
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000543 &filename,
Guido van Rossum79f25d91997-04-29 20:08:16 +0000544 &PyDict_Type, &globals,
545 &PyDict_Type, &locals))
Guido van Rossum0f61f8a1992-02-25 18:55:05 +0000546 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000547 if (globals == Py_None) {
548 globals = PyEval_GetGlobals();
549 if (locals == Py_None)
550 locals = PyEval_GetLocals();
Guido van Rossum6135a871995-01-09 17:53:26 +0000551 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000552 else if (locals == Py_None)
Guido van Rossum6135a871995-01-09 17:53:26 +0000553 locals = globals;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000554 if (PyDict_GetItemString(globals, "__builtins__") == NULL) {
555 if (PyDict_SetItemString(globals, "__builtins__",
556 PyEval_GetBuiltins()) != 0)
Guido van Rossum6135a871995-01-09 17:53:26 +0000557 return NULL;
558 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000559 Py_BEGIN_ALLOW_THREADS
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000560 fp = fopen(filename, "r");
Guido van Rossum79f25d91997-04-29 20:08:16 +0000561 Py_END_ALLOW_THREADS
Guido van Rossum0f61f8a1992-02-25 18:55:05 +0000562 if (fp == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000563 PyErr_SetFromErrno(PyExc_IOError);
Guido van Rossum0f61f8a1992-02-25 18:55:05 +0000564 return NULL;
565 }
Guido van Rossumb05a5c71997-05-07 17:46:13 +0000566 res = PyRun_File(fp, filename, Py_file_input, globals, locals);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000567 Py_BEGIN_ALLOW_THREADS
Guido van Rossum0f61f8a1992-02-25 18:55:05 +0000568 fclose(fp);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000569 Py_END_ALLOW_THREADS
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000570 return res;
Guido van Rossum0f61f8a1992-02-25 18:55:05 +0000571}
572
Guido van Rossum79f25d91997-04-29 20:08:16 +0000573static PyObject *
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000574builtin_float(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000575 PyObject *self;
576 PyObject *args;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000577{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000578 PyObject *v;
579 PyNumberMethods *nb;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000580
Guido van Rossum79f25d91997-04-29 20:08:16 +0000581 if (!PyArg_ParseTuple(args, "O:float", &v))
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000582 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000583 if (PyString_Check(v))
Guido van Rossumc6472e91997-03-31 17:15:43 +0000584 return float_from_string(v);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000585 if ((nb = v->ob_type->tp_as_number) == NULL ||
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000586 nb->nb_float == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000587 PyErr_SetString(PyExc_TypeError,
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000588 "float() argument can't be converted to float");
589 return NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000590 }
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000591 return (*nb->nb_float)(v);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000592}
593
Guido van Rossum79f25d91997-04-29 20:08:16 +0000594static PyObject *
Guido van Rossum94390a41992-08-14 15:14:30 +0000595builtin_getattr(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000596 PyObject *self;
597 PyObject *args;
Guido van Rossum33894be1992-01-27 16:53:09 +0000598{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000599 PyObject *v;
600 PyObject *name;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000601
Guido van Rossum79f25d91997-04-29 20:08:16 +0000602 if (!PyArg_ParseTuple(args, "OS:getattr", &v, &name))
Guido van Rossum33894be1992-01-27 16:53:09 +0000603 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000604 return PyObject_GetAttr(v, name);
Guido van Rossum9bfef441993-03-29 10:43:31 +0000605}
606
Guido van Rossum79f25d91997-04-29 20:08:16 +0000607static PyObject *
Guido van Rossum872537c1995-07-07 22:43:42 +0000608builtin_globals(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000609 PyObject *self;
610 PyObject *args;
Guido van Rossum872537c1995-07-07 22:43:42 +0000611{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000612 PyObject *d;
Guido van Rossum872537c1995-07-07 22:43:42 +0000613
Guido van Rossum79f25d91997-04-29 20:08:16 +0000614 if (!PyArg_ParseTuple(args, ""))
Guido van Rossum872537c1995-07-07 22:43:42 +0000615 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000616 d = PyEval_GetGlobals();
617 Py_INCREF(d);
Guido van Rossum872537c1995-07-07 22:43:42 +0000618 return d;
619}
620
Guido van Rossum79f25d91997-04-29 20:08:16 +0000621static PyObject *
Guido van Rossum9bfef441993-03-29 10:43:31 +0000622builtin_hasattr(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000623 PyObject *self;
624 PyObject *args;
Guido van Rossum9bfef441993-03-29 10:43:31 +0000625{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000626 PyObject *v;
627 PyObject *name;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000628
Guido van Rossum79f25d91997-04-29 20:08:16 +0000629 if (!PyArg_ParseTuple(args, "OS:hasattr", &v, &name))
Guido van Rossum9bfef441993-03-29 10:43:31 +0000630 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000631 v = PyObject_GetAttr(v, name);
Guido van Rossum9bfef441993-03-29 10:43:31 +0000632 if (v == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000633 PyErr_Clear();
634 return PyInt_FromLong(0L);
Guido van Rossum9bfef441993-03-29 10:43:31 +0000635 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000636 Py_DECREF(v);
637 return PyInt_FromLong(1L);
Guido van Rossum33894be1992-01-27 16:53:09 +0000638}
639
Guido van Rossum79f25d91997-04-29 20:08:16 +0000640static PyObject *
Guido van Rossum5b722181993-03-30 17:46:03 +0000641builtin_id(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000642 PyObject *self;
643 PyObject *args;
Guido van Rossum5b722181993-03-30 17:46:03 +0000644{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000645 PyObject *v;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000646
Guido van Rossum79f25d91997-04-29 20:08:16 +0000647 if (!PyArg_ParseTuple(args, "O:id", &v))
Guido van Rossum5b722181993-03-30 17:46:03 +0000648 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000649 return PyInt_FromLong((long)v);
Guido van Rossum5b722181993-03-30 17:46:03 +0000650}
651
Guido van Rossum79f25d91997-04-29 20:08:16 +0000652static PyObject *
Guido van Rossum12d12c51993-10-26 17:58:25 +0000653builtin_map(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000654 PyObject *self;
655 PyObject *args;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000656{
657 typedef struct {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000658 PyObject *seq;
659 PySequenceMethods *sqf;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000660 int len;
661 } sequence;
662
Guido van Rossum79f25d91997-04-29 20:08:16 +0000663 PyObject *func, *result;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000664 sequence *seqs = NULL, *sqp;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000665 int n, len;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000666 register int i, j;
667
Guido van Rossum79f25d91997-04-29 20:08:16 +0000668 n = PyTuple_Size(args);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000669 if (n < 2) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000670 PyErr_SetString(PyExc_TypeError,
671 "map() requires at least two args");
Guido van Rossum12d12c51993-10-26 17:58:25 +0000672 return NULL;
673 }
674
Guido van Rossum79f25d91997-04-29 20:08:16 +0000675 func = PyTuple_GetItem(args, 0);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000676 n--;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000677
Guido van Rossum79f25d91997-04-29 20:08:16 +0000678 if ((seqs = PyMem_NEW(sequence, n)) == NULL) {
679 PyErr_NoMemory();
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000680 goto Fail_2;
681 }
Guido van Rossum12d12c51993-10-26 17:58:25 +0000682
Guido van Rossum2d951851994-08-29 12:52:16 +0000683 for (len = 0, i = 0, sqp = seqs; i < n; ++i, ++sqp) {
Guido van Rossum12d12c51993-10-26 17:58:25 +0000684 int curlen;
685
Guido van Rossum79f25d91997-04-29 20:08:16 +0000686 if ((sqp->seq = PyTuple_GetItem(args, i + 1)) == NULL)
Guido van Rossum12d12c51993-10-26 17:58:25 +0000687 goto Fail_2;
688
689 if (! (sqp->sqf = sqp->seq->ob_type->tp_as_sequence)) {
690 static char errmsg[] =
691 "argument %d to map() must be a sequence object";
Guido van Rossum15e33a41997-04-30 19:00:27 +0000692 char errbuf[sizeof(errmsg) + 25];
Guido van Rossum12d12c51993-10-26 17:58:25 +0000693
694 sprintf(errbuf, errmsg, i+2);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000695 PyErr_SetString(PyExc_TypeError, errbuf);
Guido van Rossum12d12c51993-10-26 17:58:25 +0000696 goto Fail_2;
697 }
698
699 if ((curlen = sqp->len = (*sqp->sqf->sq_length)(sqp->seq)) < 0)
700 goto Fail_2;
701
702 if (curlen > len)
703 len = curlen;
704 }
705
Guido van Rossum79f25d91997-04-29 20:08:16 +0000706 if ((result = (PyObject *) PyList_New(len)) == NULL)
Guido van Rossum12d12c51993-10-26 17:58:25 +0000707 goto Fail_2;
708
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000709 /* XXX Special case map(None, single_list) could be more efficient */
Guido van Rossum2d951851994-08-29 12:52:16 +0000710 for (i = 0; ; ++i) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000711 PyObject *alist, *item=NULL, *value;
Guido van Rossum2d951851994-08-29 12:52:16 +0000712 int any = 0;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000713
Guido van Rossum79f25d91997-04-29 20:08:16 +0000714 if (func == Py_None && n == 1)
Guido van Rossum32120311995-07-10 13:52:21 +0000715 alist = NULL;
Guido van Rossum2d951851994-08-29 12:52:16 +0000716 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000717 if ((alist = PyTuple_New(n)) == NULL)
Guido van Rossum2d951851994-08-29 12:52:16 +0000718 goto Fail_1;
719 }
Guido van Rossum12d12c51993-10-26 17:58:25 +0000720
721 for (j = 0, sqp = seqs; j < n; ++j, ++sqp) {
Guido van Rossum2d951851994-08-29 12:52:16 +0000722 if (sqp->len < 0) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000723 Py_INCREF(Py_None);
724 item = Py_None;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000725 }
Guido van Rossum12d12c51993-10-26 17:58:25 +0000726 else {
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000727 item = (*sqp->sqf->sq_item)(sqp->seq, i);
Guido van Rossum2d951851994-08-29 12:52:16 +0000728 if (item == NULL) {
729 if (i < sqp->len)
730 goto Fail_0;
Barry Warsawcde8b1b1997-08-22 21:14:38 +0000731 if (PyErr_ExceptionMatches(
732 PyExc_IndexError))
733 {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000734 PyErr_Clear();
735 Py_INCREF(Py_None);
736 item = Py_None;
Guido van Rossum2d951851994-08-29 12:52:16 +0000737 sqp->len = -1;
738 }
739 else {
740 goto Fail_0;
741 }
742 }
743 else
744 any = 1;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000745
Guido van Rossum12d12c51993-10-26 17:58:25 +0000746 }
Guido van Rossum32120311995-07-10 13:52:21 +0000747 if (!alist)
Guido van Rossum2d951851994-08-29 12:52:16 +0000748 break;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000749 if (PyTuple_SetItem(alist, j, item) < 0) {
750 Py_DECREF(item);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000751 goto Fail_0;
Guido van Rossum2d951851994-08-29 12:52:16 +0000752 }
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000753 continue;
754
755 Fail_0:
Guido van Rossum79f25d91997-04-29 20:08:16 +0000756 Py_XDECREF(alist);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000757 goto Fail_1;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000758 }
759
Guido van Rossum32120311995-07-10 13:52:21 +0000760 if (!alist)
761 alist = item;
Guido van Rossum2d951851994-08-29 12:52:16 +0000762
763 if (!any) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000764 Py_DECREF(alist);
Guido van Rossum2d951851994-08-29 12:52:16 +0000765 break;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000766 }
Guido van Rossum2d951851994-08-29 12:52:16 +0000767
Guido van Rossum79f25d91997-04-29 20:08:16 +0000768 if (func == Py_None)
Guido van Rossum32120311995-07-10 13:52:21 +0000769 value = alist;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000770 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000771 value = PyEval_CallObject(func, alist);
772 Py_DECREF(alist);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000773 if (value == NULL)
774 goto Fail_1;
Guido van Rossum2d951851994-08-29 12:52:16 +0000775 }
776 if (i >= len) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000777 if (PyList_Append(result, value) < 0)
Guido van Rossum2d951851994-08-29 12:52:16 +0000778 goto Fail_1;
779 }
780 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000781 if (PyList_SetItem(result, i, value) < 0)
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000782 goto Fail_1;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000783 }
784 }
785
Guido van Rossum79f25d91997-04-29 20:08:16 +0000786 PyMem_DEL(seqs);
Guido van Rossum12d12c51993-10-26 17:58:25 +0000787 return result;
788
Guido van Rossum12d12c51993-10-26 17:58:25 +0000789Fail_1:
Guido van Rossum79f25d91997-04-29 20:08:16 +0000790 Py_DECREF(result);
Guido van Rossum12d12c51993-10-26 17:58:25 +0000791Fail_2:
Guido van Rossum79f25d91997-04-29 20:08:16 +0000792 if (seqs) PyMem_DEL(seqs);
Guido van Rossum12d12c51993-10-26 17:58:25 +0000793 return NULL;
794}
795
Guido van Rossum79f25d91997-04-29 20:08:16 +0000796static PyObject *
Guido van Rossum94390a41992-08-14 15:14:30 +0000797builtin_setattr(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000798 PyObject *self;
799 PyObject *args;
Guido van Rossum33894be1992-01-27 16:53:09 +0000800{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000801 PyObject *v;
802 PyObject *name;
803 PyObject *value;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000804
Guido van Rossum79f25d91997-04-29 20:08:16 +0000805 if (!PyArg_ParseTuple(args, "OSO:setattr", &v, &name, &value))
Guido van Rossum33894be1992-01-27 16:53:09 +0000806 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000807 if (PyObject_SetAttr(v, name, value) != 0)
Guido van Rossum33894be1992-01-27 16:53:09 +0000808 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000809 Py_INCREF(Py_None);
810 return Py_None;
Guido van Rossum33894be1992-01-27 16:53:09 +0000811}
812
Guido van Rossum79f25d91997-04-29 20:08:16 +0000813static PyObject *
Guido van Rossum14144fc1994-08-29 12:53:40 +0000814builtin_delattr(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000815 PyObject *self;
816 PyObject *args;
Guido van Rossum14144fc1994-08-29 12:53:40 +0000817{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000818 PyObject *v;
819 PyObject *name;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000820
Guido van Rossum79f25d91997-04-29 20:08:16 +0000821 if (!PyArg_ParseTuple(args, "OS:delattr", &v, &name))
Guido van Rossum14144fc1994-08-29 12:53:40 +0000822 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000823 if (PyObject_SetAttr(v, name, (PyObject *)NULL) != 0)
Guido van Rossum14144fc1994-08-29 12:53:40 +0000824 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000825 Py_INCREF(Py_None);
826 return Py_None;
Guido van Rossum14144fc1994-08-29 12:53:40 +0000827}
828
Guido van Rossum79f25d91997-04-29 20:08:16 +0000829static PyObject *
Guido van Rossum9bfef441993-03-29 10:43:31 +0000830builtin_hash(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000831 PyObject *self;
832 PyObject *args;
Guido van Rossum9bfef441993-03-29 10:43:31 +0000833{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000834 PyObject *v;
Guido van Rossum9bfef441993-03-29 10:43:31 +0000835 long x;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000836
Guido van Rossum79f25d91997-04-29 20:08:16 +0000837 if (!PyArg_ParseTuple(args, "O:hash", &v))
Guido van Rossum9bfef441993-03-29 10:43:31 +0000838 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000839 x = PyObject_Hash(v);
Guido van Rossum9bfef441993-03-29 10:43:31 +0000840 if (x == -1)
841 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000842 return PyInt_FromLong(x);
Guido van Rossum9bfef441993-03-29 10:43:31 +0000843}
844
Guido van Rossum79f25d91997-04-29 20:08:16 +0000845static PyObject *
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000846builtin_hex(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000847 PyObject *self;
848 PyObject *args;
Guido van Rossum006bcd41991-10-24 14:54:44 +0000849{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000850 PyObject *v;
851 PyNumberMethods *nb;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000852
Guido van Rossum79f25d91997-04-29 20:08:16 +0000853 if (!PyArg_ParseTuple(args, "O:hex", &v))
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000854 return NULL;
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000855
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000856 if ((nb = v->ob_type->tp_as_number) == NULL ||
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000857 nb->nb_hex == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000858 PyErr_SetString(PyExc_TypeError,
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000859 "hex() argument can't be converted to hex");
860 return NULL;
Guido van Rossum006bcd41991-10-24 14:54:44 +0000861 }
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000862 return (*nb->nb_hex)(v);
Guido van Rossum006bcd41991-10-24 14:54:44 +0000863}
864
Guido van Rossum79f25d91997-04-29 20:08:16 +0000865static PyObject *builtin_raw_input Py_PROTO((PyObject *, PyObject *));
Guido van Rossum3165fe61992-09-25 21:59:05 +0000866
Guido van Rossum79f25d91997-04-29 20:08:16 +0000867static PyObject *
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000868builtin_input(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000869 PyObject *self;
870 PyObject *args;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000871{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000872 PyObject *line;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000873 char *str;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000874 PyObject *res;
875 PyObject *globals, *locals;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000876
877 line = builtin_raw_input(self, args);
Guido van Rossum3165fe61992-09-25 21:59:05 +0000878 if (line == NULL)
879 return line;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000880 if (!PyArg_Parse(line, "s;embedded '\\0' in input line", &str))
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000881 return NULL;
882 while (*str == ' ' || *str == '\t')
883 str++;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000884 globals = PyEval_GetGlobals();
885 locals = PyEval_GetLocals();
886 if (PyDict_GetItemString(globals, "__builtins__") == NULL) {
887 if (PyDict_SetItemString(globals, "__builtins__",
888 PyEval_GetBuiltins()) != 0)
Guido van Rossum6135a871995-01-09 17:53:26 +0000889 return NULL;
890 }
Guido van Rossumb05a5c71997-05-07 17:46:13 +0000891 res = PyRun_String(str, Py_eval_input, globals, locals);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000892 Py_DECREF(line);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000893 return res;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000894}
895
Guido van Rossume8811f81997-02-14 15:48:05 +0000896static PyObject *
897builtin_intern(self, args)
898 PyObject *self;
899 PyObject *args;
900{
901 PyObject *s;
902 if (!PyArg_ParseTuple(args, "S", &s))
903 return NULL;
904 Py_INCREF(s);
905 PyString_InternInPlace(&s);
906 return s;
907}
908
Guido van Rossum79f25d91997-04-29 20:08:16 +0000909static PyObject *
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000910builtin_int(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000911 PyObject *self;
912 PyObject *args;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000913{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000914 PyObject *v;
915 PyNumberMethods *nb;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000916
Guido van Rossum79f25d91997-04-29 20:08:16 +0000917 if (!PyArg_ParseTuple(args, "O:int", &v))
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000918 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000919 if (PyString_Check(v))
Guido van Rossumc6472e91997-03-31 17:15:43 +0000920 return int_from_string(v);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000921 if ((nb = v->ob_type->tp_as_number) == NULL ||
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000922 nb->nb_int == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000923 PyErr_SetString(PyExc_TypeError,
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000924 "int() argument can't be converted to int");
925 return NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000926 }
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000927 return (*nb->nb_int)(v);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000928}
929
Guido van Rossum79f25d91997-04-29 20:08:16 +0000930static PyObject *
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000931builtin_len(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000932 PyObject *self;
933 PyObject *args;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000934{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000935 PyObject *v;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000936 long len;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000937 PyTypeObject *tp;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000938
Guido van Rossum79f25d91997-04-29 20:08:16 +0000939 if (!PyArg_ParseTuple(args, "O:len", &v))
Guido van Rossum3f5da241990-12-20 15:06:42 +0000940 return NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000941 tp = v->ob_type;
942 if (tp->tp_as_sequence != NULL) {
943 len = (*tp->tp_as_sequence->sq_length)(v);
944 }
945 else if (tp->tp_as_mapping != NULL) {
946 len = (*tp->tp_as_mapping->mp_length)(v);
947 }
948 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000949 PyErr_SetString(PyExc_TypeError, "len() of unsized object");
Guido van Rossum3f5da241990-12-20 15:06:42 +0000950 return NULL;
951 }
Guido van Rossum04691fc1992-08-12 15:35:34 +0000952 if (len < 0)
953 return NULL;
954 else
Guido van Rossum79f25d91997-04-29 20:08:16 +0000955 return PyInt_FromLong(len);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000956}
957
Guido van Rossum79f25d91997-04-29 20:08:16 +0000958static PyObject *
Guido van Rossumd1705771996-04-09 02:41:06 +0000959builtin_list(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000960 PyObject *self;
961 PyObject *args;
Guido van Rossumd1705771996-04-09 02:41:06 +0000962{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000963 PyObject *v;
964 PySequenceMethods *sqf;
Guido van Rossumd1705771996-04-09 02:41:06 +0000965
Guido van Rossum79f25d91997-04-29 20:08:16 +0000966 if (!PyArg_ParseTuple(args, "O:list", &v))
Guido van Rossumd1705771996-04-09 02:41:06 +0000967 return NULL;
968 if ((sqf = v->ob_type->tp_as_sequence) != NULL) {
969 int n = (*sqf->sq_length)(v);
970 int i;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000971 PyObject *l;
Guido van Rossumd1705771996-04-09 02:41:06 +0000972 if (n < 0)
973 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000974 l = PyList_New(n);
Guido van Rossumd1705771996-04-09 02:41:06 +0000975 if (l == NULL)
976 return NULL;
977 for (i = 0; i < n; i++) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000978 PyObject *item = (*sqf->sq_item)(v, i);
Guido van Rossumd1705771996-04-09 02:41:06 +0000979 if (item == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000980 Py_DECREF(l);
Guido van Rossumd1705771996-04-09 02:41:06 +0000981 l = NULL;
982 break;
983 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000984 PyList_SetItem(l, i, item);
Guido van Rossumd1705771996-04-09 02:41:06 +0000985 }
986 /* XXX Should support indefinite-length sequences */
987 return l;
988 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000989 PyErr_SetString(PyExc_TypeError, "list() argument must be a sequence");
Guido van Rossumd1705771996-04-09 02:41:06 +0000990 return NULL;
991}
992
Guido van Rossum8861b741996-07-30 16:49:37 +0000993
994static PyObject *
995builtin_slice(self, args)
996 PyObject *self;
997 PyObject *args;
998{
999 PyObject *start, *stop, *step;
1000
1001 start = stop = step = NULL;
1002
1003 if (!PyArg_ParseTuple(args, "O|OO:slice", &start, &stop, &step))
1004 return NULL;
1005
1006 /*This swapping of stop and start is to maintain compatibility with
1007 the range builtin.*/
1008 if (stop == NULL) {
1009 stop = start;
1010 start = NULL;
1011 }
1012 return PySlice_New(start, stop, step);
1013}
1014
Guido van Rossum79f25d91997-04-29 20:08:16 +00001015static PyObject *
Guido van Rossum872537c1995-07-07 22:43:42 +00001016builtin_locals(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001017 PyObject *self;
1018 PyObject *args;
Guido van Rossum872537c1995-07-07 22:43:42 +00001019{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001020 PyObject *d;
Guido van Rossum872537c1995-07-07 22:43:42 +00001021
Guido van Rossum79f25d91997-04-29 20:08:16 +00001022 if (!PyArg_ParseTuple(args, ""))
Guido van Rossum872537c1995-07-07 22:43:42 +00001023 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001024 d = PyEval_GetLocals();
1025 Py_INCREF(d);
Guido van Rossum872537c1995-07-07 22:43:42 +00001026 return d;
1027}
1028
Guido van Rossum79f25d91997-04-29 20:08:16 +00001029static PyObject *
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001030builtin_long(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001031 PyObject *self;
1032 PyObject *args;
Guido van Rossumd4905451991-05-05 20:00:36 +00001033{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001034 PyObject *v;
1035 PyNumberMethods *nb;
Guido van Rossum1899c2e1992-09-12 11:09:23 +00001036
Guido van Rossum79f25d91997-04-29 20:08:16 +00001037 if (!PyArg_ParseTuple(args, "O:long", &v))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001038 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001039 if (PyString_Check(v))
Guido van Rossumc6472e91997-03-31 17:15:43 +00001040 return long_from_string(v);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001041 if ((nb = v->ob_type->tp_as_number) == NULL ||
Guido van Rossum1899c2e1992-09-12 11:09:23 +00001042 nb->nb_long == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001043 PyErr_SetString(PyExc_TypeError,
Guido van Rossum1899c2e1992-09-12 11:09:23 +00001044 "long() argument can't be converted to long");
1045 return NULL;
Guido van Rossumd4905451991-05-05 20:00:36 +00001046 }
Guido van Rossum1899c2e1992-09-12 11:09:23 +00001047 return (*nb->nb_long)(v);
Guido van Rossumd4905451991-05-05 20:00:36 +00001048}
1049
Guido van Rossum79f25d91997-04-29 20:08:16 +00001050static PyObject *
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001051min_max(args, sign)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001052 PyObject *args;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001053 int sign;
1054{
Guido van Rossum2d951851994-08-29 12:52:16 +00001055 int i;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001056 PyObject *v, *w, *x;
1057 PySequenceMethods *sq;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001058
Guido van Rossum79f25d91997-04-29 20:08:16 +00001059 if (PyTuple_Size(args) > 1)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001060 v = args;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001061 else if (!PyArg_ParseTuple(args, "O:min/max", &v))
Guido van Rossum3f5da241990-12-20 15:06:42 +00001062 return NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001063 sq = v->ob_type->tp_as_sequence;
1064 if (sq == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001065 PyErr_SetString(PyExc_TypeError,
1066 "min() or max() of non-sequence");
Guido van Rossum3f5da241990-12-20 15:06:42 +00001067 return NULL;
1068 }
Guido van Rossum2d951851994-08-29 12:52:16 +00001069 w = NULL;
1070 for (i = 0; ; i++) {
Guido van Rossum3f5da241990-12-20 15:06:42 +00001071 x = (*sq->sq_item)(v, i); /* Implies INCREF */
Guido van Rossum2d951851994-08-29 12:52:16 +00001072 if (x == NULL) {
Barry Warsawcde8b1b1997-08-22 21:14:38 +00001073 if (PyErr_ExceptionMatches(PyExc_IndexError)) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001074 PyErr_Clear();
Guido van Rossum2d951851994-08-29 12:52:16 +00001075 break;
1076 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00001077 Py_XDECREF(w);
Guido van Rossum2d951851994-08-29 12:52:16 +00001078 return NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001079 }
Guido van Rossum2d951851994-08-29 12:52:16 +00001080 if (w == NULL)
1081 w = x;
1082 else {
Guido van Rossumc8b6df91997-05-23 00:06:51 +00001083 int c = PyObject_Compare(x, w);
1084 if (c && PyErr_Occurred()) {
1085 Py_DECREF(x);
1086 Py_XDECREF(w);
1087 return NULL;
1088 }
1089 if (c * sign > 0) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001090 Py_DECREF(w);
Guido van Rossum2d951851994-08-29 12:52:16 +00001091 w = x;
1092 }
1093 else
Guido van Rossum79f25d91997-04-29 20:08:16 +00001094 Py_DECREF(x);
Guido van Rossum2d951851994-08-29 12:52:16 +00001095 }
Guido van Rossum3f5da241990-12-20 15:06:42 +00001096 }
Guido van Rossum2d951851994-08-29 12:52:16 +00001097 if (w == NULL)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001098 PyErr_SetString(PyExc_ValueError,
1099 "min() or max() of empty sequence");
Guido van Rossum3f5da241990-12-20 15:06:42 +00001100 return w;
1101}
1102
Guido van Rossum79f25d91997-04-29 20:08:16 +00001103static PyObject *
Guido van Rossum3f5da241990-12-20 15:06:42 +00001104builtin_min(self, v)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001105 PyObject *self;
1106 PyObject *v;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001107{
1108 return min_max(v, -1);
1109}
1110
Guido van Rossum79f25d91997-04-29 20:08:16 +00001111static PyObject *
Guido van Rossum3f5da241990-12-20 15:06:42 +00001112builtin_max(self, v)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001113 PyObject *self;
1114 PyObject *v;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001115{
1116 return min_max(v, 1);
1117}
1118
Guido van Rossum79f25d91997-04-29 20:08:16 +00001119static PyObject *
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001120builtin_oct(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001121 PyObject *self;
1122 PyObject *args;
Guido van Rossum006bcd41991-10-24 14:54:44 +00001123{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001124 PyObject *v;
1125 PyNumberMethods *nb;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001126
Guido van Rossum79f25d91997-04-29 20:08:16 +00001127 if (!PyArg_ParseTuple(args, "O:oct", &v))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001128 return NULL;
Guido van Rossum1899c2e1992-09-12 11:09:23 +00001129 if (v == NULL || (nb = v->ob_type->tp_as_number) == NULL ||
1130 nb->nb_oct == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001131 PyErr_SetString(PyExc_TypeError,
Guido van Rossum1899c2e1992-09-12 11:09:23 +00001132 "oct() argument can't be converted to oct");
1133 return NULL;
Guido van Rossum006bcd41991-10-24 14:54:44 +00001134 }
Guido van Rossum1899c2e1992-09-12 11:09:23 +00001135 return (*nb->nb_oct)(v);
Guido van Rossum006bcd41991-10-24 14:54:44 +00001136}
1137
Guido van Rossum79f25d91997-04-29 20:08:16 +00001138static PyObject *
Guido van Rossum94390a41992-08-14 15:14:30 +00001139builtin_open(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001140 PyObject *self;
1141 PyObject *args;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001142{
Guido van Rossum2d951851994-08-29 12:52:16 +00001143 char *name;
1144 char *mode = "r";
1145 int bufsize = -1;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001146 PyObject *f;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001147
Guido van Rossum79f25d91997-04-29 20:08:16 +00001148 if (!PyArg_ParseTuple(args, "s|si:open", &name, &mode, &bufsize))
Guido van Rossum3f5da241990-12-20 15:06:42 +00001149 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001150 f = PyFile_FromString(name, mode);
Guido van Rossum2d951851994-08-29 12:52:16 +00001151 if (f != NULL)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001152 PyFile_SetBufSize(f, bufsize);
Guido van Rossum2d951851994-08-29 12:52:16 +00001153 return f;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001154}
1155
Guido van Rossum79f25d91997-04-29 20:08:16 +00001156static PyObject *
Guido van Rossum94390a41992-08-14 15:14:30 +00001157builtin_ord(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001158 PyObject *self;
1159 PyObject *args;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001160{
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001161 char c;
1162
Guido van Rossum79f25d91997-04-29 20:08:16 +00001163 if (!PyArg_ParseTuple(args, "c:ord", &c))
Guido van Rossum3f5da241990-12-20 15:06:42 +00001164 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001165 return PyInt_FromLong((long)(c & 0xff));
Guido van Rossum3f5da241990-12-20 15:06:42 +00001166}
1167
Guido van Rossum79f25d91997-04-29 20:08:16 +00001168static PyObject *
Guido van Rossum6a00cd81995-01-07 12:39:01 +00001169do_pow(v, w)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001170 PyObject *v, *w;
Guido van Rossumd4905451991-05-05 20:00:36 +00001171{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001172 PyObject *res;
1173 if (PyInstance_Check(v) || PyInstance_Check(w))
1174 return PyInstance_DoBinOp(v, w, "__pow__", "__rpow__", do_pow);
Guido van Rossumc6bb8f71991-07-01 18:42:41 +00001175 if (v->ob_type->tp_as_number == NULL ||
Guido van Rossumdf05ac61994-08-29 12:52:37 +00001176 w->ob_type->tp_as_number == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001177 PyErr_SetString(PyExc_TypeError,
1178 "pow() requires numeric arguments");
Guido van Rossumd4905451991-05-05 20:00:36 +00001179 return NULL;
1180 }
Guido van Rossum8a5c5d21996-01-12 01:09:56 +00001181 if (
1182#ifndef WITHOUT_COMPLEX
Guido van Rossum79f25d91997-04-29 20:08:16 +00001183 !PyComplex_Check(v) &&
Guido van Rossum8a5c5d21996-01-12 01:09:56 +00001184#endif
Guido van Rossum79f25d91997-04-29 20:08:16 +00001185 PyFloat_Check(w) && PyFloat_AsDouble(v) < 0.0) {
1186 if (!PyErr_Occurred())
1187 PyErr_SetString(PyExc_ValueError,
1188 "negative number to float power");
Guido van Rossum21651581995-02-10 16:57:16 +00001189 return NULL;
1190 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00001191 if (PyNumber_Coerce(&v, &w) != 0)
Guido van Rossumc6bb8f71991-07-01 18:42:41 +00001192 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001193 res = (*v->ob_type->tp_as_number->nb_power)(v, w, Py_None);
1194 Py_DECREF(v);
1195 Py_DECREF(w);
Guido van Rossum6a00cd81995-01-07 12:39:01 +00001196 return res;
1197}
1198
Guido van Rossum79f25d91997-04-29 20:08:16 +00001199static PyObject *
Guido van Rossum6a00cd81995-01-07 12:39:01 +00001200builtin_pow(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001201 PyObject *self;
1202 PyObject *args;
Guido van Rossum6a00cd81995-01-07 12:39:01 +00001203{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001204 PyObject *v, *w, *z = Py_None, *res;
1205 PyObject *v1, *z1, *w2, *z2;
Guido van Rossum6a00cd81995-01-07 12:39:01 +00001206
Guido van Rossum79f25d91997-04-29 20:08:16 +00001207 if (!PyArg_ParseTuple(args, "OO|O:pow", &v, &w, &z))
Guido van Rossum6a00cd81995-01-07 12:39:01 +00001208 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001209 if (z == Py_None)
Guido van Rossum6a00cd81995-01-07 12:39:01 +00001210 return do_pow(v, w);
1211 /* XXX The ternary version doesn't do class instance coercions */
Guido van Rossum79f25d91997-04-29 20:08:16 +00001212 if (PyInstance_Check(v))
Guido van Rossum6a00cd81995-01-07 12:39:01 +00001213 return v->ob_type->tp_as_number->nb_power(v, w, z);
1214 if (v->ob_type->tp_as_number == NULL ||
1215 z->ob_type->tp_as_number == NULL ||
1216 w->ob_type->tp_as_number == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001217 PyErr_SetString(PyExc_TypeError,
1218 "pow() requires numeric arguments");
Guido van Rossum6a00cd81995-01-07 12:39:01 +00001219 return NULL;
1220 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00001221 if (PyNumber_Coerce(&v, &w) != 0)
Guido van Rossum6a00cd81995-01-07 12:39:01 +00001222 return NULL;
1223 res = NULL;
1224 v1 = v;
1225 z1 = z;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001226 if (PyNumber_Coerce(&v1, &z1) != 0)
Guido van Rossum6a00cd81995-01-07 12:39:01 +00001227 goto error2;
1228 w2 = w;
1229 z2 = z1;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001230 if (PyNumber_Coerce(&w2, &z2) != 0)
Guido van Rossum6a00cd81995-01-07 12:39:01 +00001231 goto error1;
1232 res = (*v1->ob_type->tp_as_number->nb_power)(v1, w2, z2);
Guido van Rossum79f25d91997-04-29 20:08:16 +00001233 Py_DECREF(w2);
1234 Py_DECREF(z2);
Guido van Rossum6a00cd81995-01-07 12:39:01 +00001235 error1:
Guido van Rossum79f25d91997-04-29 20:08:16 +00001236 Py_DECREF(v1);
1237 Py_DECREF(z1);
Guido van Rossum6a00cd81995-01-07 12:39:01 +00001238 error2:
Guido van Rossum79f25d91997-04-29 20:08:16 +00001239 Py_DECREF(v);
1240 Py_DECREF(w);
Guido van Rossum6a00cd81995-01-07 12:39:01 +00001241 return res;
Guido van Rossumd4905451991-05-05 20:00:36 +00001242}
1243
Guido van Rossum79f25d91997-04-29 20:08:16 +00001244static PyObject *
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001245builtin_range(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001246 PyObject *self;
1247 PyObject *args;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001248{
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001249 long ilow = 0, ihigh = 0, istep = 1;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001250 int i, n;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001251 PyObject *v;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001252
Guido van Rossum79f25d91997-04-29 20:08:16 +00001253 if (PyTuple_Size(args) <= 1) {
1254 if (!PyArg_ParseTuple(args,
Guido van Rossum0865dd91995-01-17 16:30:22 +00001255 "l;range() requires 1-3 int arguments",
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001256 &ihigh))
1257 return NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001258 }
1259 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001260 if (!PyArg_ParseTuple(args,
Guido van Rossum0865dd91995-01-17 16:30:22 +00001261 "ll|l;range() requires 1-3 int arguments",
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001262 &ilow, &ihigh, &istep))
Guido van Rossum3f5da241990-12-20 15:06:42 +00001263 return NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001264 }
1265 if (istep == 0) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001266 PyErr_SetString(PyExc_ValueError, "zero step for range()");
Guido van Rossum3f5da241990-12-20 15:06:42 +00001267 return NULL;
1268 }
1269 /* XXX ought to check overflow of subtraction */
1270 if (istep > 0)
1271 n = (ihigh - ilow + istep - 1) / istep;
1272 else
1273 n = (ihigh - ilow + istep + 1) / istep;
1274 if (n < 0)
1275 n = 0;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001276 v = PyList_New(n);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001277 if (v == NULL)
1278 return NULL;
1279 for (i = 0; i < n; i++) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001280 PyObject *w = PyInt_FromLong(ilow);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001281 if (w == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001282 Py_DECREF(v);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001283 return NULL;
1284 }
Guido van Rossuma937d141998-04-24 18:22:02 +00001285 PyList_SET_ITEM(v, i, w);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001286 ilow += istep;
1287 }
1288 return v;
1289}
1290
Guido van Rossum79f25d91997-04-29 20:08:16 +00001291static PyObject *
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001292builtin_xrange(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001293 PyObject *self;
1294 PyObject *args;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001295{
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001296 long ilow = 0, ihigh = 0, istep = 1;
Guido van Rossum0865dd91995-01-17 16:30:22 +00001297 long n;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001298
Guido van Rossum79f25d91997-04-29 20:08:16 +00001299 if (PyTuple_Size(args) <= 1) {
1300 if (!PyArg_ParseTuple(args,
Guido van Rossum0865dd91995-01-17 16:30:22 +00001301 "l;xrange() requires 1-3 int arguments",
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001302 &ihigh))
1303 return NULL;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001304 }
1305 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001306 if (!PyArg_ParseTuple(args,
Guido van Rossum0865dd91995-01-17 16:30:22 +00001307 "ll|l;xrange() requires 1-3 int arguments",
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001308 &ilow, &ihigh, &istep))
Guido van Rossum12d12c51993-10-26 17:58:25 +00001309 return NULL;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001310 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001311 if (istep == 0) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001312 PyErr_SetString(PyExc_ValueError, "zero step for xrange()");
Guido van Rossum12d12c51993-10-26 17:58:25 +00001313 return NULL;
1314 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001315 /* XXX ought to check overflow of subtraction */
1316 if (istep > 0)
1317 n = (ihigh - ilow + istep - 1) / istep;
1318 else
1319 n = (ihigh - ilow + istep + 1) / istep;
1320 if (n < 0)
1321 n = 0;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001322 return PyRange_New(ilow, n, istep, 1);
Guido van Rossum12d12c51993-10-26 17:58:25 +00001323}
1324
Guido van Rossum79f25d91997-04-29 20:08:16 +00001325extern char *PyOS_Readline Py_PROTO((char *));
Guido van Rossum872537c1995-07-07 22:43:42 +00001326
Guido van Rossum79f25d91997-04-29 20:08:16 +00001327static PyObject *
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001328builtin_raw_input(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001329 PyObject *self;
1330 PyObject *args;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001331{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001332 PyObject *v = NULL;
1333 PyObject *f;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001334
Guido van Rossum79f25d91997-04-29 20:08:16 +00001335 if (!PyArg_ParseTuple(args, "|O:[raw_]input", &v))
Guido van Rossum3165fe61992-09-25 21:59:05 +00001336 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001337 if (PyFile_AsFile(PySys_GetObject("stdin")) == stdin &&
1338 PyFile_AsFile(PySys_GetObject("stdout")) == stdout &&
Guido van Rossum53bb7ff1995-07-26 16:26:31 +00001339 isatty(fileno(stdin)) && isatty(fileno(stdout))) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001340 PyObject *po;
Guido van Rossum872537c1995-07-07 22:43:42 +00001341 char *prompt;
1342 char *s;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001343 PyObject *result;
Guido van Rossum872537c1995-07-07 22:43:42 +00001344 if (v != NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001345 po = PyObject_Str(v);
Guido van Rossum872537c1995-07-07 22:43:42 +00001346 if (po == NULL)
1347 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001348 prompt = PyString_AsString(po);
Guido van Rossum872537c1995-07-07 22:43:42 +00001349 }
1350 else {
1351 po = NULL;
1352 prompt = "";
1353 }
Guido van Rossumee81af81997-09-26 21:47:43 +00001354 Py_BEGIN_ALLOW_THREADS
Guido van Rossum79f25d91997-04-29 20:08:16 +00001355 s = PyOS_Readline(prompt);
Guido van Rossumee81af81997-09-26 21:47:43 +00001356 Py_END_ALLOW_THREADS
Guido van Rossum79f25d91997-04-29 20:08:16 +00001357 Py_XDECREF(po);
Guido van Rossum872537c1995-07-07 22:43:42 +00001358 if (s == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001359 PyErr_SetNone(PyExc_KeyboardInterrupt);
Guido van Rossum872537c1995-07-07 22:43:42 +00001360 return NULL;
1361 }
1362 if (*s == '\0') {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001363 PyErr_SetNone(PyExc_EOFError);
Guido van Rossum872537c1995-07-07 22:43:42 +00001364 result = NULL;
1365 }
1366 else { /* strip trailing '\n' */
Guido van Rossum79f25d91997-04-29 20:08:16 +00001367 result = PyString_FromStringAndSize(s, strlen(s)-1);
Guido van Rossum872537c1995-07-07 22:43:42 +00001368 }
1369 free(s);
1370 return result;
1371 }
Guido van Rossum90933611991-06-07 16:10:43 +00001372 if (v != NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001373 f = PySys_GetObject("stdout");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001374 if (f == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001375 PyErr_SetString(PyExc_RuntimeError, "lost sys.stdout");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001376 return NULL;
1377 }
Guido van Rossumc8b6df91997-05-23 00:06:51 +00001378 if (Py_FlushLine() != 0 ||
1379 PyFile_WriteObject(v, f, Py_PRINT_RAW) != 0)
Guido van Rossum90933611991-06-07 16:10:43 +00001380 return NULL;
1381 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00001382 f = PySys_GetObject("stdin");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001383 if (f == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001384 PyErr_SetString(PyExc_RuntimeError, "lost sys.stdin");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001385 return NULL;
1386 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00001387 return PyFile_GetLine(f, -1);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001388}
1389
Guido van Rossum79f25d91997-04-29 20:08:16 +00001390static PyObject *
Guido van Rossum12d12c51993-10-26 17:58:25 +00001391builtin_reduce(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001392 PyObject *self;
1393 PyObject *args;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001394{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001395 PyObject *seq, *func, *result = NULL;
1396 PySequenceMethods *sqf;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001397 register int i;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001398
Guido van Rossum79f25d91997-04-29 20:08:16 +00001399 if (!PyArg_ParseTuple(args, "OO|O:reduce", &func, &seq, &result))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001400 return NULL;
1401 if (result != NULL)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001402 Py_INCREF(result);
Guido van Rossum12d12c51993-10-26 17:58:25 +00001403
1404 if ((sqf = seq->ob_type->tp_as_sequence) == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001405 PyErr_SetString(PyExc_TypeError,
Guido van Rossum12d12c51993-10-26 17:58:25 +00001406 "2nd argument to reduce() must be a sequence object");
1407 return NULL;
1408 }
1409
Guido van Rossum79f25d91997-04-29 20:08:16 +00001410 if ((args = PyTuple_New(2)) == NULL)
Guido van Rossum2d951851994-08-29 12:52:16 +00001411 goto Fail;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001412
Guido van Rossum2d951851994-08-29 12:52:16 +00001413 for (i = 0; ; ++i) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001414 PyObject *op2;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001415
1416 if (args->ob_refcnt > 1) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001417 Py_DECREF(args);
1418 if ((args = PyTuple_New(2)) == NULL)
Guido van Rossum2d951851994-08-29 12:52:16 +00001419 goto Fail;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001420 }
1421
Guido van Rossum2d951851994-08-29 12:52:16 +00001422 if ((op2 = (*sqf->sq_item)(seq, i)) == NULL) {
Barry Warsawcde8b1b1997-08-22 21:14:38 +00001423 if (PyErr_ExceptionMatches(PyExc_IndexError)) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001424 PyErr_Clear();
Guido van Rossum2d951851994-08-29 12:52:16 +00001425 break;
1426 }
1427 goto Fail;
1428 }
Guido van Rossum12d12c51993-10-26 17:58:25 +00001429
Guido van Rossum2d951851994-08-29 12:52:16 +00001430 if (result == NULL)
1431 result = op2;
1432 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001433 PyTuple_SetItem(args, 0, result);
1434 PyTuple_SetItem(args, 1, op2);
1435 if ((result = PyEval_CallObject(func, args)) == NULL)
Guido van Rossum2d951851994-08-29 12:52:16 +00001436 goto Fail;
1437 }
Guido van Rossum12d12c51993-10-26 17:58:25 +00001438 }
1439
Guido van Rossum79f25d91997-04-29 20:08:16 +00001440 Py_DECREF(args);
Guido van Rossum12d12c51993-10-26 17:58:25 +00001441
Guido van Rossum2d951851994-08-29 12:52:16 +00001442 if (result == NULL)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001443 PyErr_SetString(PyExc_TypeError,
Guido van Rossum2d951851994-08-29 12:52:16 +00001444 "reduce of empty sequence with no initial value");
1445
Guido van Rossum12d12c51993-10-26 17:58:25 +00001446 return result;
1447
Guido van Rossum2d951851994-08-29 12:52:16 +00001448Fail:
Guido van Rossum79f25d91997-04-29 20:08:16 +00001449 Py_XDECREF(args);
1450 Py_XDECREF(result);
Guido van Rossum12d12c51993-10-26 17:58:25 +00001451 return NULL;
1452}
1453
Guido van Rossum79f25d91997-04-29 20:08:16 +00001454static PyObject *
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001455builtin_reload(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001456 PyObject *self;
1457 PyObject *args;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001458{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001459 PyObject *v;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001460
Guido van Rossum79f25d91997-04-29 20:08:16 +00001461 if (!PyArg_ParseTuple(args, "O:reload", &v))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001462 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001463 return PyImport_ReloadModule(v);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001464}
1465
Guido van Rossum79f25d91997-04-29 20:08:16 +00001466static PyObject *
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001467builtin_repr(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001468 PyObject *self;
1469 PyObject *args;
Guido van Rossumc89705d1992-11-26 08:54:07 +00001470{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001471 PyObject *v;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001472
Guido van Rossum79f25d91997-04-29 20:08:16 +00001473 if (!PyArg_ParseTuple(args, "O:repr", &v))
Guido van Rossumc89705d1992-11-26 08:54:07 +00001474 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001475 return PyObject_Repr(v);
Guido van Rossumc89705d1992-11-26 08:54:07 +00001476}
1477
Guido van Rossum79f25d91997-04-29 20:08:16 +00001478static PyObject *
Guido van Rossum9e51f9b1993-02-12 16:29:05 +00001479builtin_round(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001480 PyObject *self;
1481 PyObject *args;
Guido van Rossum9e51f9b1993-02-12 16:29:05 +00001482{
Guido van Rossum9e51f9b1993-02-12 16:29:05 +00001483 double x;
1484 double f;
1485 int ndigits = 0;
Guido van Rossum9e51f9b1993-02-12 16:29:05 +00001486 int i;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001487
Guido van Rossum79f25d91997-04-29 20:08:16 +00001488 if (!PyArg_ParseTuple(args, "d|i:round", &x, &ndigits))
Guido van Rossum9e51f9b1993-02-12 16:29:05 +00001489 return NULL;
Guido van Rossum9e51f9b1993-02-12 16:29:05 +00001490 f = 1.0;
1491 for (i = ndigits; --i >= 0; )
1492 f = f*10.0;
1493 for (i = ndigits; ++i <= 0; )
1494 f = f*0.1;
1495 if (x >= 0.0)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001496 return PyFloat_FromDouble(floor(x*f + 0.5) / f);
Guido van Rossum9e51f9b1993-02-12 16:29:05 +00001497 else
Guido van Rossum79f25d91997-04-29 20:08:16 +00001498 return PyFloat_FromDouble(ceil(x*f - 0.5) / f);
Guido van Rossum9e51f9b1993-02-12 16:29:05 +00001499}
1500
Guido van Rossum79f25d91997-04-29 20:08:16 +00001501static PyObject *
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001502builtin_str(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001503 PyObject *self;
1504 PyObject *args;
Guido van Rossumc89705d1992-11-26 08:54:07 +00001505{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001506 PyObject *v;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001507
Guido van Rossum79f25d91997-04-29 20:08:16 +00001508 if (!PyArg_ParseTuple(args, "O:str", &v))
Guido van Rossumc89705d1992-11-26 08:54:07 +00001509 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001510 return PyObject_Str(v);
Guido van Rossumc89705d1992-11-26 08:54:07 +00001511}
1512
Guido van Rossum79f25d91997-04-29 20:08:16 +00001513static PyObject *
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001514builtin_tuple(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001515 PyObject *self;
1516 PyObject *args;
Guido van Rossumcae027b1994-08-29 12:53:11 +00001517{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001518 PyObject *v;
1519 PySequenceMethods *sqf;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001520
Guido van Rossum79f25d91997-04-29 20:08:16 +00001521 if (!PyArg_ParseTuple(args, "O:tuple", &v))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001522 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001523 if (PyTuple_Check(v)) {
1524 Py_INCREF(v);
Guido van Rossumcae027b1994-08-29 12:53:11 +00001525 return v;
1526 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00001527 if (PyList_Check(v))
1528 return PyList_AsTuple(v);
1529 if (PyString_Check(v)) {
1530 int n = PyString_Size(v);
1531 PyObject *t = PyTuple_New(n);
Guido van Rossumcae027b1994-08-29 12:53:11 +00001532 if (t != NULL) {
1533 int i;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001534 char *p = PyString_AsString(v);
Guido van Rossumcae027b1994-08-29 12:53:11 +00001535 for (i = 0; i < n; i++) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001536 PyObject *item =
1537 PyString_FromStringAndSize(p+i, 1);
Guido van Rossumcae027b1994-08-29 12:53:11 +00001538 if (item == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001539 Py_DECREF(t);
Guido van Rossumcae027b1994-08-29 12:53:11 +00001540 t = NULL;
1541 break;
1542 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00001543 PyTuple_SetItem(t, i, item);
Guido van Rossumcae027b1994-08-29 12:53:11 +00001544 }
1545 }
1546 return t;
1547 }
1548 /* Generic sequence object */
1549 if ((sqf = v->ob_type->tp_as_sequence) != NULL) {
1550 int n = (*sqf->sq_length)(v);
1551 int i;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001552 PyObject *t;
Guido van Rossumcae027b1994-08-29 12:53:11 +00001553 if (n < 0)
1554 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001555 t = PyTuple_New(n);
Guido van Rossumcae027b1994-08-29 12:53:11 +00001556 if (t == NULL)
1557 return NULL;
1558 for (i = 0; i < n; i++) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001559 PyObject *item = (*sqf->sq_item)(v, i);
Guido van Rossumcae027b1994-08-29 12:53:11 +00001560 if (item == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001561 Py_DECREF(t);
Guido van Rossumcae027b1994-08-29 12:53:11 +00001562 t = NULL;
1563 break;
1564 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00001565 PyTuple_SetItem(t, i, item);
Guido van Rossumcae027b1994-08-29 12:53:11 +00001566 }
1567 /* XXX Should support indefinite-length sequences */
1568 return t;
1569 }
1570 /* None of the above */
Guido van Rossum79f25d91997-04-29 20:08:16 +00001571 PyErr_SetString(PyExc_TypeError,
1572 "tuple() argument must be a sequence");
Guido van Rossumcae027b1994-08-29 12:53:11 +00001573 return NULL;
1574}
1575
Guido van Rossum79f25d91997-04-29 20:08:16 +00001576static PyObject *
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001577builtin_type(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001578 PyObject *self;
1579 PyObject *args;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001580{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001581 PyObject *v;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001582
Guido van Rossum79f25d91997-04-29 20:08:16 +00001583 if (!PyArg_ParseTuple(args, "O:type", &v))
Guido van Rossum3f5da241990-12-20 15:06:42 +00001584 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001585 v = (PyObject *)v->ob_type;
1586 Py_INCREF(v);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001587 return v;
1588}
1589
Guido van Rossum79f25d91997-04-29 20:08:16 +00001590static PyObject *
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001591builtin_vars(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001592 PyObject *self;
1593 PyObject *args;
Guido van Rossum2d951851994-08-29 12:52:16 +00001594{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001595 PyObject *v = NULL;
1596 PyObject *d;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001597
Guido van Rossum79f25d91997-04-29 20:08:16 +00001598 if (!PyArg_ParseTuple(args, "|O:vars", &v))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001599 return NULL;
Guido van Rossum2d951851994-08-29 12:52:16 +00001600 if (v == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001601 d = PyEval_GetLocals();
Guido van Rossum53bb7ff1995-07-26 16:26:31 +00001602 if (d == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001603 if (!PyErr_Occurred())
1604 PyErr_SetString(PyExc_SystemError,
1605 "no locals!?");
Guido van Rossum53bb7ff1995-07-26 16:26:31 +00001606 }
1607 else
Guido van Rossum79f25d91997-04-29 20:08:16 +00001608 Py_INCREF(d);
Guido van Rossum2d951851994-08-29 12:52:16 +00001609 }
1610 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001611 d = PyObject_GetAttrString(v, "__dict__");
Guido van Rossum2d951851994-08-29 12:52:16 +00001612 if (d == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001613 PyErr_SetString(PyExc_TypeError,
Guido van Rossum2d951851994-08-29 12:52:16 +00001614 "vars() argument must have __dict__ attribute");
1615 return NULL;
1616 }
1617 }
1618 return d;
1619}
1620
Barry Warsawcde8b1b1997-08-22 21:14:38 +00001621static PyObject *
1622builtin_isinstance(self, args)
1623 PyObject *self;
1624 PyObject *args;
1625{
1626 PyObject *inst;
1627 PyObject *cls;
1628 int retval;
1629
1630 if (!PyArg_ParseTuple(args, "OO", &inst, &cls))
1631 return NULL;
Guido van Rossumf5dd9141997-12-02 19:11:45 +00001632 if (PyType_Check(cls)) {
Guido van Rossumd6af46d1997-12-10 05:51:47 +00001633 retval = ((PyObject *)(inst->ob_type) == cls);
Barry Warsawcde8b1b1997-08-22 21:14:38 +00001634 }
Barry Warsawcde8b1b1997-08-22 21:14:38 +00001635 else {
Guido van Rossumf5dd9141997-12-02 19:11:45 +00001636 if (!PyClass_Check(cls)) {
1637 PyErr_SetString(PyExc_TypeError,
1638 "second argument must be a class");
1639 return NULL;
1640 }
1641
1642 if (!PyInstance_Check(inst))
1643 retval = 0;
1644 else {
1645 PyObject *inclass =
1646 (PyObject*)((PyInstanceObject*)inst)->in_class;
1647 retval = PyClass_IsSubclass(inclass, cls);
1648 }
Barry Warsawcde8b1b1997-08-22 21:14:38 +00001649 }
1650 return PyInt_FromLong(retval);
1651}
1652
1653
1654static PyObject *
1655builtin_issubclass(self, args)
1656 PyObject *self;
1657 PyObject *args;
1658{
1659 PyObject *derived;
1660 PyObject *cls;
1661 int retval;
1662
1663 if (!PyArg_ParseTuple(args, "OO", &derived, &cls))
1664 return NULL;
1665 if (!PyClass_Check(derived) || !PyClass_Check(cls)) {
1666 PyErr_SetString(PyExc_TypeError, "arguments must be classes");
1667 return NULL;
1668 }
1669 /* shortcut */
1670 if (!(retval = (derived == cls)))
1671 retval = PyClass_IsSubclass(derived, cls);
1672
1673 return PyInt_FromLong(retval);
1674}
1675
1676
Guido van Rossum79f25d91997-04-29 20:08:16 +00001677static PyMethodDef builtin_methods[] = {
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001678 {"__import__", builtin___import__, 1},
1679 {"abs", builtin_abs, 1},
1680 {"apply", builtin_apply, 1},
1681 {"callable", builtin_callable, 1},
1682 {"chr", builtin_chr, 1},
1683 {"cmp", builtin_cmp, 1},
1684 {"coerce", builtin_coerce, 1},
1685 {"compile", builtin_compile, 1},
Guido van Rossum8a5c5d21996-01-12 01:09:56 +00001686#ifndef WITHOUT_COMPLEX
1687 {"complex", builtin_complex, 1},
1688#endif
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001689 {"delattr", builtin_delattr, 1},
1690 {"dir", builtin_dir, 1},
1691 {"divmod", builtin_divmod, 1},
1692 {"eval", builtin_eval, 1},
1693 {"execfile", builtin_execfile, 1},
1694 {"filter", builtin_filter, 1},
1695 {"float", builtin_float, 1},
1696 {"getattr", builtin_getattr, 1},
Guido van Rossum872537c1995-07-07 22:43:42 +00001697 {"globals", builtin_globals, 1},
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001698 {"hasattr", builtin_hasattr, 1},
1699 {"hash", builtin_hash, 1},
1700 {"hex", builtin_hex, 1},
1701 {"id", builtin_id, 1},
1702 {"input", builtin_input, 1},
Guido van Rossume8811f81997-02-14 15:48:05 +00001703 {"intern", builtin_intern, 1},
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001704 {"int", builtin_int, 1},
Barry Warsawcde8b1b1997-08-22 21:14:38 +00001705 {"isinstance", builtin_isinstance, 1},
1706 {"issubclass", builtin_issubclass, 1},
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001707 {"len", builtin_len, 1},
Guido van Rossumd1705771996-04-09 02:41:06 +00001708 {"list", builtin_list, 1},
Guido van Rossum872537c1995-07-07 22:43:42 +00001709 {"locals", builtin_locals, 1},
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001710 {"long", builtin_long, 1},
1711 {"map", builtin_map, 1},
1712 {"max", builtin_max, 1},
1713 {"min", builtin_min, 1},
1714 {"oct", builtin_oct, 1},
1715 {"open", builtin_open, 1},
1716 {"ord", builtin_ord, 1},
Guido van Rossum030ae171994-11-10 22:33:19 +00001717 {"pow", builtin_pow, 1},
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001718 {"range", builtin_range, 1},
1719 {"raw_input", builtin_raw_input, 1},
1720 {"reduce", builtin_reduce, 1},
1721 {"reload", builtin_reload, 1},
1722 {"repr", builtin_repr, 1},
1723 {"round", builtin_round, 1},
1724 {"setattr", builtin_setattr, 1},
Guido van Rossum8861b741996-07-30 16:49:37 +00001725 {"slice", builtin_slice, 1},
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001726 {"str", builtin_str, 1},
1727 {"tuple", builtin_tuple, 1},
1728 {"type", builtin_type, 1},
1729 {"vars", builtin_vars, 1},
1730 {"xrange", builtin_xrange, 1},
Guido van Rossumc02e15c1991-12-16 13:03:00 +00001731 {NULL, NULL},
Guido van Rossum3f5da241990-12-20 15:06:42 +00001732};
1733
Guido van Rossum3f5da241990-12-20 15:06:42 +00001734/* Predefined exceptions */
1735
Guido van Rossum04748321997-09-16 18:43:15 +00001736PyObject *PyExc_Exception;
Barry Warsaw757af0e1997-08-29 22:13:51 +00001737PyObject *PyExc_StandardError;
Barry Warsaw412cdc21997-09-16 21:51:14 +00001738PyObject *PyExc_ArithmeticError;
Barry Warsaw757af0e1997-08-29 22:13:51 +00001739PyObject *PyExc_LookupError;
1740
Guido van Rossum79f25d91997-04-29 20:08:16 +00001741PyObject *PyExc_AssertionError;
1742PyObject *PyExc_AttributeError;
1743PyObject *PyExc_EOFError;
Guido van Rossumb6a7f771997-05-09 03:03:23 +00001744PyObject *PyExc_FloatingPointError;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001745PyObject *PyExc_IOError;
1746PyObject *PyExc_ImportError;
1747PyObject *PyExc_IndexError;
1748PyObject *PyExc_KeyError;
1749PyObject *PyExc_KeyboardInterrupt;
1750PyObject *PyExc_MemoryError;
1751PyObject *PyExc_NameError;
1752PyObject *PyExc_OverflowError;
1753PyObject *PyExc_RuntimeError;
1754PyObject *PyExc_SyntaxError;
1755PyObject *PyExc_SystemError;
1756PyObject *PyExc_SystemExit;
1757PyObject *PyExc_TypeError;
1758PyObject *PyExc_ValueError;
1759PyObject *PyExc_ZeroDivisionError;
Guido van Rossum50afb7a1991-12-10 13:52:31 +00001760
Barry Warsaw757af0e1997-08-29 22:13:51 +00001761PyObject *PyExc_MemoryErrorInst;
1762
1763static struct
1764{
1765 char* name;
1766 PyObject** exc;
1767 int leaf_exc;
1768}
1769bltin_exc[] = {
Guido van Rossum04748321997-09-16 18:43:15 +00001770 {"Exception", &PyExc_Exception, 0},
Barry Warsaw757af0e1997-08-29 22:13:51 +00001771 {"StandardError", &PyExc_StandardError, 0},
Barry Warsaw412cdc21997-09-16 21:51:14 +00001772 {"ArithmeticError", &PyExc_ArithmeticError, 0},
Barry Warsaw757af0e1997-08-29 22:13:51 +00001773 {"LookupError", &PyExc_LookupError, 0},
1774 {"AssertionError", &PyExc_AssertionError, 1},
1775 {"AttributeError", &PyExc_AttributeError, 1},
1776 {"EOFError", &PyExc_EOFError, 1},
1777 {"FloatingPointError", &PyExc_FloatingPointError, 1},
1778 {"IOError", &PyExc_IOError, 1},
1779 {"ImportError", &PyExc_ImportError, 1},
1780 {"IndexError", &PyExc_IndexError, 1},
1781 {"KeyError", &PyExc_KeyError, 1},
1782 {"KeyboardInterrupt", &PyExc_KeyboardInterrupt, 1},
1783 {"MemoryError", &PyExc_MemoryError, 1},
1784 {"NameError", &PyExc_NameError, 1},
1785 {"OverflowError", &PyExc_OverflowError, 1},
1786 {"RuntimeError", &PyExc_RuntimeError, 1},
1787 {"SyntaxError", &PyExc_SyntaxError, 1},
1788 {"SystemError", &PyExc_SystemError, 1},
1789 {"SystemExit", &PyExc_SystemExit, 1},
1790 {"TypeError", &PyExc_TypeError, 1},
1791 {"ValueError", &PyExc_ValueError, 1},
1792 {"ZeroDivisionError", &PyExc_ZeroDivisionError, 1},
1793 {NULL, NULL}
1794};
1795
1796
1797/* import exceptions module to extract class exceptions */
1798static void
1799init_class_exc(dict)
1800 PyObject *dict;
1801{
1802 int i;
1803 PyObject *m = PyImport_ImportModule("exceptions");
1804 PyObject *d;
1805 PyObject *args;
1806
1807 if (m == NULL ||
1808 (d = PyModule_GetDict(m)) == NULL)
1809 {
1810 PyObject *f = PySys_GetObject("stderr");
1811 if (Py_VerboseFlag) {
1812 PyFile_WriteString(
1813 "'import exceptions' failed; traceback:\n", f);
1814 PyErr_Print();
1815 }
1816 else {
1817 PyFile_WriteString(
1818 "'import exceptions' failed; use -v for traceback\n", f);
1819 PyErr_Clear();
1820 }
1821 PyFile_WriteString("defaulting to old style exceptions\n", f);
1822 return;
1823 }
1824 for (i = 0; bltin_exc[i].name; i++) {
1825 /* dig the exception out of the module */
1826 PyObject *exc = PyDict_GetItemString(d, bltin_exc[i].name);
1827 if (!exc)
1828 Py_FatalError("built-in exception cannot be initialized");
1829
1830 Py_XDECREF(*bltin_exc[i].exc);
1831
1832 /* squirrel away a pointer to the exception */
1833 Py_INCREF(exc);
1834 *bltin_exc[i].exc = exc;
1835
1836 /* and insert the name in the __builtin__ module */
1837 PyDict_SetItemString(dict, bltin_exc[i].name, exc);
1838 }
1839
1840 /* we need one pre-allocated instance */
1841 args = Py_BuildValue("()");
1842 if (args) {
1843 PyExc_MemoryErrorInst =
1844 PyEval_CallObject(PyExc_MemoryError, args);
1845 Py_DECREF(args);
1846 }
1847
1848 /* we're done with the exceptions module */
1849 Py_DECREF(m);
1850
1851 if (PyErr_Occurred())
1852 Py_FatalError("can't instantiate standard exceptions");
1853}
1854
1855
1856static void
1857fini_instances()
1858{
1859 Py_XDECREF(PyExc_MemoryErrorInst);
1860 PyExc_MemoryErrorInst = NULL;
1861}
1862
1863
Guido van Rossum79f25d91997-04-29 20:08:16 +00001864static PyObject *
Guido van Rossum25ce5661997-08-02 03:10:38 +00001865newstdexception(dict, name)
1866 PyObject *dict;
Guido van Rossumfb905c31991-12-16 15:42:38 +00001867 char *name;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001868{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001869 PyObject *v = PyString_FromString(name);
Guido van Rossum25ce5661997-08-02 03:10:38 +00001870 if (v == NULL || PyDict_SetItemString(dict, name, v) != 0)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001871 Py_FatalError("no mem for new standard exception");
Guido van Rossum3f5da241990-12-20 15:06:42 +00001872 return v;
1873}
1874
1875static void
Guido van Rossum25ce5661997-08-02 03:10:38 +00001876initerrors(dict)
1877 PyObject *dict;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001878{
Barry Warsaw757af0e1997-08-29 22:13:51 +00001879 int i;
1880 int exccnt = 0;
1881 for (i = 0; bltin_exc[i].name; i++, exccnt++) {
1882 if (bltin_exc[i].leaf_exc)
1883 *bltin_exc[i].exc =
1884 newstdexception(dict, bltin_exc[i].name);
1885 }
1886
1887 /* This is kind of bogus because we special case the three new
1888 exceptions to be nearly forward compatible. But this means we
1889 hard code knowledge about exceptions.py into C here. I don't
1890 have a better solution, though
1891 */
1892 PyExc_LookupError = PyTuple_New(2);
1893 Py_INCREF(PyExc_IndexError);
1894 PyTuple_SET_ITEM(PyExc_LookupError, 0, PyExc_IndexError);
1895 Py_INCREF(PyExc_KeyError);
1896 PyTuple_SET_ITEM(PyExc_LookupError, 1, PyExc_KeyError);
1897 PyDict_SetItemString(dict, "LookupError", PyExc_LookupError);
1898
Barry Warsaw412cdc21997-09-16 21:51:14 +00001899 PyExc_ArithmeticError = PyTuple_New(3);
Barry Warsaw757af0e1997-08-29 22:13:51 +00001900 Py_INCREF(PyExc_OverflowError);
Barry Warsaw412cdc21997-09-16 21:51:14 +00001901 PyTuple_SET_ITEM(PyExc_ArithmeticError, 0, PyExc_OverflowError);
Barry Warsaw757af0e1997-08-29 22:13:51 +00001902 Py_INCREF(PyExc_ZeroDivisionError);
Barry Warsaw412cdc21997-09-16 21:51:14 +00001903 PyTuple_SET_ITEM(PyExc_ArithmeticError, 1, PyExc_ZeroDivisionError);
Barry Warsaw757af0e1997-08-29 22:13:51 +00001904 Py_INCREF(PyExc_FloatingPointError);
Barry Warsaw412cdc21997-09-16 21:51:14 +00001905 PyTuple_SET_ITEM(PyExc_ArithmeticError, 2, PyExc_FloatingPointError);
1906 PyDict_SetItemString(dict, "ArithmeticError", PyExc_ArithmeticError);
Barry Warsaw757af0e1997-08-29 22:13:51 +00001907
Barry Warsawb01a7fa1997-09-18 03:44:38 +00001908 PyExc_StandardError = PyTuple_New(exccnt-2);
1909 for (i = 2; bltin_exc[i].name; i++) {
Barry Warsaw757af0e1997-08-29 22:13:51 +00001910 PyObject *exc = *bltin_exc[i].exc;
1911 Py_INCREF(exc);
Barry Warsawb01a7fa1997-09-18 03:44:38 +00001912 PyTuple_SET_ITEM(PyExc_StandardError, i-2, exc);
Barry Warsaw757af0e1997-08-29 22:13:51 +00001913 }
1914 PyDict_SetItemString(dict, "StandardError", PyExc_StandardError);
Guido van Rossum04748321997-09-16 18:43:15 +00001915
1916 /* Exception is treated differently; for now, it's == StandardError */
1917 PyExc_Exception = PyExc_StandardError;
1918 Py_INCREF(PyExc_Exception);
1919 PyDict_SetItemString(dict, "Exception", PyExc_Exception);
Barry Warsaw757af0e1997-08-29 22:13:51 +00001920
1921 if (PyErr_Occurred())
1922 Py_FatalError("Could not initialize built-in string exceptions");
Guido van Rossum25ce5661997-08-02 03:10:38 +00001923}
1924
1925static void
1926finierrors()
1927{
Barry Warsaw757af0e1997-08-29 22:13:51 +00001928 int i;
1929 for (i = 0; bltin_exc[i].name; i++) {
1930 PyObject *exc = *bltin_exc[i].exc;
1931 Py_XDECREF(exc);
1932 *bltin_exc[i].exc = NULL;
1933 }
Guido van Rossum25ce5661997-08-02 03:10:38 +00001934}
1935
1936PyObject *
Barry Warsaw757af0e1997-08-29 22:13:51 +00001937_PyBuiltin_Init_1()
Guido van Rossum25ce5661997-08-02 03:10:38 +00001938{
1939 PyObject *mod, *dict;
1940 mod = Py_InitModule("__builtin__", builtin_methods);
1941 if (mod == NULL)
1942 return NULL;
1943 dict = PyModule_GetDict(mod);
1944 initerrors(dict);
1945 if (PyDict_SetItemString(dict, "None", Py_None) < 0)
1946 return NULL;
1947 if (PyDict_SetItemString(dict, "Ellipsis", Py_Ellipsis) < 0)
1948 return NULL;
1949 if (PyDict_SetItemString(dict, "__debug__",
1950 PyInt_FromLong(Py_OptimizeFlag == 0)) < 0)
1951 return NULL;
Barry Warsaw757af0e1997-08-29 22:13:51 +00001952
Guido van Rossum25ce5661997-08-02 03:10:38 +00001953 return mod;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001954}
1955
1956void
Barry Warsaw757af0e1997-08-29 22:13:51 +00001957_PyBuiltin_Init_2(dict)
1958 PyObject *dict;
1959{
1960 /* if Python was started with -X, initialize the class exceptions */
1961 if (Py_UseClassExceptionsFlag)
1962 init_class_exc(dict);
1963}
1964
1965
1966void
1967_PyBuiltin_Fini_1()
1968{
1969 fini_instances();
1970}
1971
1972
1973void
1974_PyBuiltin_Fini_2()
Guido van Rossum3f5da241990-12-20 15:06:42 +00001975{
Guido van Rossum25ce5661997-08-02 03:10:38 +00001976 finierrors();
Guido van Rossum3f5da241990-12-20 15:06:42 +00001977}
Guido van Rossumc6bb8f71991-07-01 18:42:41 +00001978
Guido van Rossum12d12c51993-10-26 17:58:25 +00001979
Guido van Rossume77a7571993-11-03 15:01:26 +00001980/* Helper for filter(): filter a tuple through a function */
Guido van Rossum12d12c51993-10-26 17:58:25 +00001981
Guido van Rossum79f25d91997-04-29 20:08:16 +00001982static PyObject *
Guido van Rossum12d12c51993-10-26 17:58:25 +00001983filtertuple(func, tuple)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001984 PyObject *func;
1985 PyObject *tuple;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001986{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001987 PyObject *result;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001988 register int i, j;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001989 int len = PyTuple_Size(tuple);
Guido van Rossum12d12c51993-10-26 17:58:25 +00001990
Guido van Rossumb7b45621995-08-04 04:07:45 +00001991 if (len == 0) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001992 Py_INCREF(tuple);
Guido van Rossumb7b45621995-08-04 04:07:45 +00001993 return tuple;
1994 }
1995
Guido van Rossum79f25d91997-04-29 20:08:16 +00001996 if ((result = PyTuple_New(len)) == NULL)
Guido van Rossum2586bf01993-11-01 16:21:44 +00001997 return NULL;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001998
Guido van Rossum12d12c51993-10-26 17:58:25 +00001999 for (i = j = 0; i < len; ++i) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002000 PyObject *item, *good;
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002001 int ok;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002002
Guido van Rossum79f25d91997-04-29 20:08:16 +00002003 if ((item = PyTuple_GetItem(tuple, i)) == NULL)
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002004 goto Fail_1;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002005 if (func == Py_None) {
2006 Py_INCREF(item);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002007 good = item;
2008 }
2009 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002010 PyObject *arg = Py_BuildValue("(O)", item);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002011 if (arg == NULL)
2012 goto Fail_1;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002013 good = PyEval_CallObject(func, arg);
2014 Py_DECREF(arg);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002015 if (good == NULL)
Guido van Rossum12d12c51993-10-26 17:58:25 +00002016 goto Fail_1;
2017 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002018 ok = PyObject_IsTrue(good);
2019 Py_DECREF(good);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002020 if (ok) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002021 Py_INCREF(item);
2022 if (PyTuple_SetItem(result, j++, item) < 0)
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002023 goto Fail_1;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002024 }
Guido van Rossum12d12c51993-10-26 17:58:25 +00002025 }
2026
Guido van Rossum79f25d91997-04-29 20:08:16 +00002027 if (_PyTuple_Resize(&result, j, 0) < 0)
Guido van Rossum12d12c51993-10-26 17:58:25 +00002028 return NULL;
2029
Guido van Rossum12d12c51993-10-26 17:58:25 +00002030 return result;
2031
Guido van Rossum12d12c51993-10-26 17:58:25 +00002032Fail_1:
Guido van Rossum79f25d91997-04-29 20:08:16 +00002033 Py_DECREF(result);
Guido van Rossum12d12c51993-10-26 17:58:25 +00002034 return NULL;
2035}
2036
2037
Guido van Rossume77a7571993-11-03 15:01:26 +00002038/* Helper for filter(): filter a string through a function */
Guido van Rossum12d12c51993-10-26 17:58:25 +00002039
Guido van Rossum79f25d91997-04-29 20:08:16 +00002040static PyObject *
Guido van Rossum12d12c51993-10-26 17:58:25 +00002041filterstring(func, strobj)
Guido van Rossum79f25d91997-04-29 20:08:16 +00002042 PyObject *func;
2043 PyObject *strobj;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002044{
Guido van Rossum79f25d91997-04-29 20:08:16 +00002045 PyObject *result;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002046 register int i, j;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002047 int len = PyString_Size(strobj);
Guido van Rossum12d12c51993-10-26 17:58:25 +00002048
Guido van Rossum79f25d91997-04-29 20:08:16 +00002049 if (func == Py_None) {
Guido van Rossum2586bf01993-11-01 16:21:44 +00002050 /* No character is ever false -- share input string */
Guido van Rossum79f25d91997-04-29 20:08:16 +00002051 Py_INCREF(strobj);
Guido van Rossum2d951851994-08-29 12:52:16 +00002052 return strobj;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002053 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002054 if ((result = PyString_FromStringAndSize(NULL, len)) == NULL)
Guido van Rossum2586bf01993-11-01 16:21:44 +00002055 return NULL;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002056
Guido van Rossum12d12c51993-10-26 17:58:25 +00002057 for (i = j = 0; i < len; ++i) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002058 PyObject *item, *arg, *good;
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002059 int ok;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002060
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002061 item = (*strobj->ob_type->tp_as_sequence->sq_item)(strobj, i);
2062 if (item == NULL)
2063 goto Fail_1;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002064 arg = Py_BuildValue("(O)", item);
2065 Py_DECREF(item);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002066 if (arg == NULL)
2067 goto Fail_1;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002068 good = PyEval_CallObject(func, arg);
2069 Py_DECREF(arg);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002070 if (good == NULL)
2071 goto Fail_1;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002072 ok = PyObject_IsTrue(good);
2073 Py_DECREF(good);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002074 if (ok)
Guido van Rossum79f25d91997-04-29 20:08:16 +00002075 PyString_AS_STRING((PyStringObject *)result)[j++] =
2076 PyString_AS_STRING((PyStringObject *)item)[0];
Guido van Rossum12d12c51993-10-26 17:58:25 +00002077 }
2078
Guido van Rossum79f25d91997-04-29 20:08:16 +00002079 if (j < len && _PyString_Resize(&result, j) < 0)
Guido van Rossum12d12c51993-10-26 17:58:25 +00002080 return NULL;
2081
Guido van Rossum12d12c51993-10-26 17:58:25 +00002082 return result;
2083
Guido van Rossum12d12c51993-10-26 17:58:25 +00002084Fail_1:
Guido van Rossum79f25d91997-04-29 20:08:16 +00002085 Py_DECREF(result);
Guido van Rossum12d12c51993-10-26 17:58:25 +00002086 return NULL;
2087}
Guido van Rossumc6472e91997-03-31 17:15:43 +00002088
2089/* Copied with modifications from stropmodule.c: atoi,atof.atol */
2090
2091static PyObject *
2092int_from_string(v)
2093 PyObject *v;
2094{
2095 extern long PyOS_strtol Py_PROTO((const char *, char **, int));
2096 char *s, *end;
2097 long x;
2098 char buffer[256]; /* For errors */
2099
Guido van Rossum8f745711998-03-13 21:30:14 +00002100 s = PyString_AS_STRING(v);
Guido van Rossumc6472e91997-03-31 17:15:43 +00002101 while (*s && isspace(Py_CHARMASK(*s)))
2102 s++;
2103 if (s[0] == '\0') {
2104 PyErr_SetString(PyExc_ValueError, "empty string for int()");
2105 return NULL;
2106 }
2107 errno = 0;
2108 x = PyOS_strtol(s, &end, 10);
2109 while (*end && isspace(Py_CHARMASK(*end)))
2110 end++;
2111 if (*end != '\0') {
2112 sprintf(buffer, "invalid literal for int(): %.200s", s);
2113 PyErr_SetString(PyExc_ValueError, buffer);
2114 return NULL;
2115 }
Guido van Rossum8f745711998-03-13 21:30:14 +00002116 else if (end-s != PyString_GET_SIZE(v)) {
2117 PyErr_SetString(PyExc_ValueError,
2118 "null byte in argument for int()");
2119 return NULL;
2120 }
Guido van Rossumc6472e91997-03-31 17:15:43 +00002121 else if (errno != 0) {
2122 sprintf(buffer, "int() literal too large: %.200s", s);
2123 PyErr_SetString(PyExc_ValueError, buffer);
2124 return NULL;
2125 }
2126 return PyInt_FromLong(x);
2127}
2128
2129static PyObject *
2130long_from_string(v)
2131 PyObject *v;
2132{
2133 char *s, *end;
2134 PyObject *x;
2135 char buffer[256]; /* For errors */
2136
Guido van Rossum8f745711998-03-13 21:30:14 +00002137 s = PyString_AS_STRING(v);
Guido van Rossumc6472e91997-03-31 17:15:43 +00002138 while (*s && isspace(Py_CHARMASK(*s)))
2139 s++;
2140 if (s[0] == '\0') {
2141 PyErr_SetString(PyExc_ValueError, "empty string for long()");
2142 return NULL;
2143 }
2144 x = PyLong_FromString(s, &end, 10);
2145 if (x == NULL)
2146 return NULL;
2147 while (*end && isspace(Py_CHARMASK(*end)))
2148 end++;
2149 if (*end != '\0') {
2150 sprintf(buffer, "invalid literal for long(): %.200s", s);
2151 PyErr_SetString(PyExc_ValueError, buffer);
2152 Py_DECREF(x);
2153 return NULL;
2154 }
Guido van Rossum8f745711998-03-13 21:30:14 +00002155 else if (end-s != PyString_GET_SIZE(v)) {
2156 PyErr_SetString(PyExc_ValueError,
2157 "null byte in argument for float()");
2158 return NULL;
2159 }
Guido van Rossumc6472e91997-03-31 17:15:43 +00002160 return x;
2161}
2162
2163static PyObject *
2164float_from_string(v)
2165 PyObject *v;
2166{
2167 extern double strtod Py_PROTO((const char *, char **));
2168 char *s, *end;
2169 double x;
2170 char buffer[256]; /* For errors */
2171
Guido van Rossum8f745711998-03-13 21:30:14 +00002172 s = PyString_AS_STRING(v);
Guido van Rossumc6472e91997-03-31 17:15:43 +00002173 while (*s && isspace(Py_CHARMASK(*s)))
2174 s++;
2175 if (s[0] == '\0') {
2176 PyErr_SetString(PyExc_ValueError, "empty string for float()");
2177 return NULL;
2178 }
2179 errno = 0;
2180 PyFPE_START_PROTECT("float_from_string", return 0)
2181 x = strtod(s, &end);
2182 PyFPE_END_PROTECT(x)
2183 while (*end && isspace(Py_CHARMASK(*end)))
2184 end++;
2185 if (*end != '\0') {
2186 sprintf(buffer, "invalid literal for float(): %.200s", s);
2187 PyErr_SetString(PyExc_ValueError, buffer);
2188 return NULL;
2189 }
Guido van Rossum8f745711998-03-13 21:30:14 +00002190 else if (end-s != PyString_GET_SIZE(v)) {
2191 PyErr_SetString(PyExc_ValueError,
2192 "null byte in argument for float()");
2193 return NULL;
2194 }
Guido van Rossumc6472e91997-03-31 17:15:43 +00002195 else if (errno != 0) {
2196 sprintf(buffer, "float() literal too large: %.200s", s);
2197 PyErr_SetString(PyExc_ValueError, buffer);
2198 return NULL;
2199 }
2200 return PyFloat_FromDouble(x);
2201}