blob: 63531db2ea6fdad47d4df53f04d15112d21047cf [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 *));
Guido van Rossum12d12c51993-10-26 17:58:25 +000051
Guido van Rossum79f25d91997-04-29 20:08:16 +000052static PyObject *
Guido van Rossum1ae940a1995-01-02 19:04:15 +000053builtin___import__(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +000054 PyObject *self;
55 PyObject *args;
Guido van Rossum3f5da241990-12-20 15:06:42 +000056{
Guido van Rossum1ae940a1995-01-02 19:04:15 +000057 char *name;
Guido van Rossum79f25d91997-04-29 20:08:16 +000058 PyObject *globals = NULL;
59 PyObject *locals = NULL;
60 PyObject *fromlist = NULL;
Guido van Rossum1ae940a1995-01-02 19:04:15 +000061
Guido van Rossum79f25d91997-04-29 20:08:16 +000062 if (!PyArg_ParseTuple(args, "s|OOO:__import__",
Guido van Rossum24c13741995-02-14 09:42:43 +000063 &name, &globals, &locals, &fromlist))
Guido van Rossum1ae940a1995-01-02 19:04:15 +000064 return NULL;
Guido van Rossumaee0bad1997-09-05 07:33:22 +000065 return PyImport_ImportModuleEx(name, globals, locals, fromlist);
Guido van Rossum1ae940a1995-01-02 19:04:15 +000066}
67
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +000068static char import_doc[] =
69"__import__(name, globals, locals, fromlist) -> module\n\
70\n\
71Import a module. The globals are only used to determine the context;\n\
72they are not modified. The locals are currently unused. The fromlist\n\
73should be a list of names to emulate ``from name import ...'', or an\n\
74empty list to emulate ``import name''.\n\
75When importing a module from a package, note that __import__('A.B', ...)\n\
76returns package A when fromlist is empty, but its submodule B when\n\
77fromlist is not empty.";
78
Guido van Rossum1ae940a1995-01-02 19:04:15 +000079
Guido van Rossum79f25d91997-04-29 20:08:16 +000080static PyObject *
Guido van Rossum1ae940a1995-01-02 19:04:15 +000081builtin_abs(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +000082 PyObject *self;
83 PyObject *args;
Guido van Rossum1ae940a1995-01-02 19:04:15 +000084{
Guido van Rossum79f25d91997-04-29 20:08:16 +000085 PyObject *v;
Guido van Rossum1ae940a1995-01-02 19:04:15 +000086
Guido van Rossum79f25d91997-04-29 20:08:16 +000087 if (!PyArg_ParseTuple(args, "O:abs", &v))
Guido van Rossum1ae940a1995-01-02 19:04:15 +000088 return NULL;
Guido van Rossum09df08a1998-05-22 00:51:39 +000089 return PyNumber_Absolute(v);
Guido van Rossum3f5da241990-12-20 15:06:42 +000090}
91
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +000092static char abs_doc[] =
93"abs(number) -> number\n\
94\n\
95Return the absolute value of the argument.";
96
97
Guido van Rossum79f25d91997-04-29 20:08:16 +000098static PyObject *
Guido van Rossum94390a41992-08-14 15:14:30 +000099builtin_apply(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000100 PyObject *self;
101 PyObject *args;
Guido van Rossumc02e15c1991-12-16 13:03:00 +0000102{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000103 PyObject *func, *alist = NULL, *kwdict = NULL;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000104
Guido van Rossum79f25d91997-04-29 20:08:16 +0000105 if (!PyArg_ParseTuple(args, "O|OO:apply", &func, &alist, &kwdict))
Guido van Rossumc02e15c1991-12-16 13:03:00 +0000106 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000107 if (alist != NULL && !PyTuple_Check(alist)) {
108 PyErr_SetString(PyExc_TypeError,
109 "apply() 2nd argument must be tuple");
Guido van Rossum2d951851994-08-29 12:52:16 +0000110 return NULL;
111 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000112 if (kwdict != NULL && !PyDict_Check(kwdict)) {
113 PyErr_SetString(PyExc_TypeError,
Guido van Rossum681d79a1995-07-18 14:51:37 +0000114 "apply() 3rd argument must be dictionary");
115 return NULL;
116 }
117 return PyEval_CallObjectWithKeywords(func, alist, kwdict);
Guido van Rossumc02e15c1991-12-16 13:03:00 +0000118}
119
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000120static char apply_doc[] =
121"apply(function, args[, kwargs]) -> value\n\
122\n\
123Call a function with positional arguments taken from the tuple args,\n\
124and keyword arguments taken from the optional dictionary kwargs.";
125
126
Guido van Rossum79f25d91997-04-29 20:08:16 +0000127static PyObject *
Guido van Rossum2d951851994-08-29 12:52:16 +0000128builtin_callable(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000129 PyObject *self;
130 PyObject *args;
Guido van Rossum2d951851994-08-29 12:52:16 +0000131{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000132 PyObject *v;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000133
Guido van Rossum79f25d91997-04-29 20:08:16 +0000134 if (!PyArg_ParseTuple(args, "O:callable", &v))
Guido van Rossum2d951851994-08-29 12:52:16 +0000135 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000136 return PyInt_FromLong((long)PyCallable_Check(v));
Guido van Rossum2d951851994-08-29 12:52:16 +0000137}
138
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000139static char callable_doc[] =
140"callable(object) -> Boolean\n\
141\n\
142Return whether the object is callable (i.e., some kind of function).\n\
143Note that classes are callable, as are instances with a __call__() method.";
144
145
Guido van Rossum79f25d91997-04-29 20:08:16 +0000146static PyObject *
Guido van Rossume77a7571993-11-03 15:01:26 +0000147builtin_filter(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000148 PyObject *self;
149 PyObject *args;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000150{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000151 PyObject *func, *seq, *result;
152 PySequenceMethods *sqf;
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000153 int len;
154 register int i, j;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000155
Guido van Rossum79f25d91997-04-29 20:08:16 +0000156 if (!PyArg_ParseTuple(args, "OO:filter", &func, &seq))
Guido van Rossum12d12c51993-10-26 17:58:25 +0000157 return NULL;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000158
Guido van Rossum79f25d91997-04-29 20:08:16 +0000159 if (PyString_Check(seq)) {
160 PyObject *r = filterstring(func, seq);
Guido van Rossum12d12c51993-10-26 17:58:25 +0000161 return r;
162 }
163
Guido van Rossum79f25d91997-04-29 20:08:16 +0000164 if (PyTuple_Check(seq)) {
165 PyObject *r = filtertuple(func, seq);
Guido van Rossum12d12c51993-10-26 17:58:25 +0000166 return r;
167 }
168
Guido van Rossum09df08a1998-05-22 00:51:39 +0000169 sqf = seq->ob_type->tp_as_sequence;
170 if (sqf == NULL || sqf->sq_length == NULL || sqf->sq_item == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000171 PyErr_SetString(PyExc_TypeError,
Guido van Rossume77a7571993-11-03 15:01:26 +0000172 "argument 2 to filter() must be a sequence type");
Guido van Rossum12d12c51993-10-26 17:58:25 +0000173 goto Fail_2;
174 }
175
176 if ((len = (*sqf->sq_length)(seq)) < 0)
177 goto Fail_2;
178
Guido van Rossum79f25d91997-04-29 20:08:16 +0000179 if (PyList_Check(seq) && seq->ob_refcnt == 1) {
180 Py_INCREF(seq);
Guido van Rossum12d12c51993-10-26 17:58:25 +0000181 result = seq;
182 }
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000183 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000184 if ((result = PyList_New(len)) == NULL)
Guido van Rossum12d12c51993-10-26 17:58:25 +0000185 goto Fail_2;
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000186 }
Guido van Rossum12d12c51993-10-26 17:58:25 +0000187
Guido van Rossum2d951851994-08-29 12:52:16 +0000188 for (i = j = 0; ; ++i) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000189 PyObject *item, *good;
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000190 int ok;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000191
Guido van Rossum2d951851994-08-29 12:52:16 +0000192 if ((item = (*sqf->sq_item)(seq, i)) == NULL) {
Barry Warsawcde8b1b1997-08-22 21:14:38 +0000193 if (PyErr_ExceptionMatches(PyExc_IndexError)) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000194 PyErr_Clear();
Guido van Rossum2d951851994-08-29 12:52:16 +0000195 break;
196 }
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000197 goto Fail_1;
Guido van Rossum2d951851994-08-29 12:52:16 +0000198 }
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000199
Guido van Rossum79f25d91997-04-29 20:08:16 +0000200 if (func == Py_None) {
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000201 good = item;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000202 Py_INCREF(good);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000203 }
204 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000205 PyObject *arg = Py_BuildValue("(O)", item);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000206 if (arg == NULL)
207 goto Fail_1;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000208 good = PyEval_CallObject(func, arg);
209 Py_DECREF(arg);
Guido van Rossum58b68731995-01-10 17:40:55 +0000210 if (good == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000211 Py_DECREF(item);
Guido van Rossum12d12c51993-10-26 17:58:25 +0000212 goto Fail_1;
Guido van Rossum58b68731995-01-10 17:40:55 +0000213 }
Guido van Rossum12d12c51993-10-26 17:58:25 +0000214 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000215 ok = PyObject_IsTrue(good);
216 Py_DECREF(good);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000217 if (ok) {
Guido van Rossum2d951851994-08-29 12:52:16 +0000218 if (j < len) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000219 if (PyList_SetItem(result, j++, item) < 0)
Guido van Rossum2d951851994-08-29 12:52:16 +0000220 goto Fail_1;
221 }
222 else {
223 j++;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000224 if (PyList_Append(result, item) < 0)
Guido van Rossum2d951851994-08-29 12:52:16 +0000225 goto Fail_1;
226 }
Guido van Rossum58b68731995-01-10 17:40:55 +0000227 } else {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000228 Py_DECREF(item);
Guido van Rossum12d12c51993-10-26 17:58:25 +0000229 }
Guido van Rossum12d12c51993-10-26 17:58:25 +0000230 }
231
Guido van Rossum12d12c51993-10-26 17:58:25 +0000232
Guido van Rossum79f25d91997-04-29 20:08:16 +0000233 if (j < len && PyList_SetSlice(result, j, len, NULL) < 0)
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000234 goto Fail_1;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000235
Guido van Rossum12d12c51993-10-26 17:58:25 +0000236 return result;
237
Guido van Rossum12d12c51993-10-26 17:58:25 +0000238Fail_1:
Guido van Rossum79f25d91997-04-29 20:08:16 +0000239 Py_DECREF(result);
Guido van Rossum12d12c51993-10-26 17:58:25 +0000240Fail_2:
Guido van Rossum12d12c51993-10-26 17:58:25 +0000241 return NULL;
242}
243
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000244static char filter_doc[] =
245"filter(function, sequence) -> list\n\
246\n\
247Return a list containing those items of sequence for which function(item)\n\
248is true. If function is None, return a list of items that are true.";
249
250
Guido van Rossum79f25d91997-04-29 20:08:16 +0000251static PyObject *
Guido van Rossum94390a41992-08-14 15:14:30 +0000252builtin_chr(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000253 PyObject *self;
254 PyObject *args;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000255{
256 long x;
257 char s[1];
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000258
Guido van Rossum79f25d91997-04-29 20:08:16 +0000259 if (!PyArg_ParseTuple(args, "l:chr", &x))
Guido van Rossum3f5da241990-12-20 15:06:42 +0000260 return NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000261 if (x < 0 || x >= 256) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000262 PyErr_SetString(PyExc_ValueError,
263 "chr() arg not in range(256)");
Guido van Rossum3f5da241990-12-20 15:06:42 +0000264 return NULL;
265 }
Guido van Rossum6bf62da1997-04-11 20:37:35 +0000266 s[0] = (char)x;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000267 return PyString_FromStringAndSize(s, 1);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000268}
269
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000270static char chr_doc[] =
271"chr(i) -> character\n\
272\n\
273Return a string of one character with ordinal i; 0 <= i < 256.";
274
275
Guido van Rossum79f25d91997-04-29 20:08:16 +0000276static PyObject *
Guido van Rossuma9e7dc11992-10-18 18:53:57 +0000277builtin_cmp(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000278 PyObject *self;
279 PyObject *args;
Guido van Rossuma9e7dc11992-10-18 18:53:57 +0000280{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000281 PyObject *a, *b;
Guido van Rossum09df08a1998-05-22 00:51:39 +0000282 int c;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000283
Guido van Rossum79f25d91997-04-29 20:08:16 +0000284 if (!PyArg_ParseTuple(args, "OO:cmp", &a, &b))
Guido van Rossuma9e7dc11992-10-18 18:53:57 +0000285 return NULL;
Guido van Rossum09df08a1998-05-22 00:51:39 +0000286 if (PyObject_Cmp(a, b, &c) < 0)
Guido van Rossumc8b6df91997-05-23 00:06:51 +0000287 return NULL;
Guido van Rossum09df08a1998-05-22 00:51:39 +0000288 return PyInt_FromLong((long)c);
Guido van Rossuma9e7dc11992-10-18 18:53:57 +0000289}
290
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000291static char cmp_doc[] =
292"cmp(x, y) -> integer\n\
293\n\
294Return negative if x<y, zero if x==y, positive if x>y.";
295
296
Guido van Rossum79f25d91997-04-29 20:08:16 +0000297static PyObject *
Guido van Rossum5524a591995-01-10 15:26:20 +0000298builtin_coerce(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000299 PyObject *self;
300 PyObject *args;
Guido van Rossum6a00cd81995-01-07 12:39:01 +0000301{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000302 PyObject *v, *w;
303 PyObject *res;
Guido van Rossum5524a591995-01-10 15:26:20 +0000304
Guido van Rossum79f25d91997-04-29 20:08:16 +0000305 if (!PyArg_ParseTuple(args, "OO:coerce", &v, &w))
Guido van Rossum5524a591995-01-10 15:26:20 +0000306 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000307 if (PyNumber_Coerce(&v, &w) < 0)
Guido van Rossum04691fc1992-08-12 15:35:34 +0000308 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000309 res = Py_BuildValue("(OO)", v, w);
310 Py_DECREF(v);
311 Py_DECREF(w);
Guido van Rossum04691fc1992-08-12 15:35:34 +0000312 return res;
313}
314
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000315static char coerce_doc[] =
316"coerce(x, y) -> None or (x1, y1)\n\
317\n\
318When x and y can be coerced to values of the same type, return a tuple\n\
319containing the coerced values. When they can't be coerced, return None.";
320
321
Guido van Rossum79f25d91997-04-29 20:08:16 +0000322static PyObject *
Guido van Rossum5b722181993-03-30 17:46:03 +0000323builtin_compile(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000324 PyObject *self;
325 PyObject *args;
Guido van Rossum5b722181993-03-30 17:46:03 +0000326{
327 char *str;
328 char *filename;
329 char *startstr;
330 int start;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000331
Guido van Rossum79f25d91997-04-29 20:08:16 +0000332 if (!PyArg_ParseTuple(args, "sss:compile", &str, &filename, &startstr))
Guido van Rossum5b722181993-03-30 17:46:03 +0000333 return NULL;
334 if (strcmp(startstr, "exec") == 0)
Guido van Rossumb05a5c71997-05-07 17:46:13 +0000335 start = Py_file_input;
Guido van Rossum5b722181993-03-30 17:46:03 +0000336 else if (strcmp(startstr, "eval") == 0)
Guido van Rossumb05a5c71997-05-07 17:46:13 +0000337 start = Py_eval_input;
Guido van Rossum872537c1995-07-07 22:43:42 +0000338 else if (strcmp(startstr, "single") == 0)
Guido van Rossumb05a5c71997-05-07 17:46:13 +0000339 start = Py_single_input;
Guido van Rossum5b722181993-03-30 17:46:03 +0000340 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000341 PyErr_SetString(PyExc_ValueError,
Guido van Rossum872537c1995-07-07 22:43:42 +0000342 "compile() mode must be 'exec' or 'eval' or 'single'");
Guido van Rossum5b722181993-03-30 17:46:03 +0000343 return NULL;
344 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000345 return Py_CompileString(str, filename, start);
Guido van Rossum5b722181993-03-30 17:46:03 +0000346}
347
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000348static char compile_doc[] =
349"compile(source, filename, mode) -> code object\n\
350\n\
351Compile the source string (a Python module, statement or expression)\n\
352into a code object that can be executed by the exec statement or eval().\n\
353The filename will be used for run-time error messages.\n\
354The mode must be 'exec' to compile a module, 'single' to compile a\n\
355single (interactive) statement, or 'eval' to compile an expression.";
356
357
Guido van Rossum8a5c5d21996-01-12 01:09:56 +0000358#ifndef WITHOUT_COMPLEX
359
Guido van Rossum79f25d91997-04-29 20:08:16 +0000360static PyObject *
Guido van Rossum8a5c5d21996-01-12 01:09:56 +0000361builtin_complex(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000362 PyObject *self;
363 PyObject *args;
Guido van Rossum8a5c5d21996-01-12 01:09:56 +0000364{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000365 PyObject *r, *i, *tmp;
366 PyNumberMethods *nbr, *nbi = NULL;
Guido van Rossum530956d1996-07-21 02:27:43 +0000367 Py_complex cr, ci;
Guido van Rossumc6472e91997-03-31 17:15:43 +0000368 int own_r = 0;
Guido van Rossum8a5c5d21996-01-12 01:09:56 +0000369
370 i = NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000371 if (!PyArg_ParseTuple(args, "O|O:complex", &r, &i))
Guido van Rossum8a5c5d21996-01-12 01:09:56 +0000372 return NULL;
373 if ((nbr = r->ob_type->tp_as_number) == NULL ||
Guido van Rossumc6472e91997-03-31 17:15:43 +0000374 nbr->nb_float == NULL ||
375 (i != NULL &&
376 ((nbi = i->ob_type->tp_as_number) == NULL ||
377 nbi->nb_float == NULL))) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000378 PyErr_SetString(PyExc_TypeError,
Guido van Rossum8a5c5d21996-01-12 01:09:56 +0000379 "complex() argument can't be converted to complex");
380 return NULL;
381 }
Guido van Rossumed0af8f1996-12-05 23:18:18 +0000382 /* XXX Hack to support classes with __complex__ method */
Guido van Rossum79f25d91997-04-29 20:08:16 +0000383 if (PyInstance_Check(r)) {
384 static PyObject *complexstr;
385 PyObject *f;
Guido van Rossumed0af8f1996-12-05 23:18:18 +0000386 if (complexstr == NULL) {
Guido van Rossum8d751611997-01-18 08:04:16 +0000387 complexstr = PyString_InternFromString("__complex__");
Guido van Rossumed0af8f1996-12-05 23:18:18 +0000388 if (complexstr == NULL)
389 return NULL;
390 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000391 f = PyObject_GetAttr(r, complexstr);
Guido van Rossumed0af8f1996-12-05 23:18:18 +0000392 if (f == NULL)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000393 PyErr_Clear();
Guido van Rossumed0af8f1996-12-05 23:18:18 +0000394 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000395 PyObject *args = Py_BuildValue("()");
Guido van Rossumed0af8f1996-12-05 23:18:18 +0000396 if (args == NULL)
397 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000398 r = PyEval_CallObject(f, args);
399 Py_DECREF(args);
Guido van Rossumed0af8f1996-12-05 23:18:18 +0000400 if (r == NULL)
401 return NULL;
Guido van Rossumc6472e91997-03-31 17:15:43 +0000402 own_r = 1;
Guido van Rossumed0af8f1996-12-05 23:18:18 +0000403 }
404 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000405 if (PyComplex_Check(r)) {
406 cr = ((PyComplexObject*)r)->cval;
Guido van Rossum730806d1998-04-10 22:27:42 +0000407 if (own_r) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000408 Py_DECREF(r);
Guido van Rossum730806d1998-04-10 22:27:42 +0000409 }
Guido van Rossumc6472e91997-03-31 17:15:43 +0000410 }
Guido van Rossum8a5c5d21996-01-12 01:09:56 +0000411 else {
Guido van Rossumfe4b6ee1996-08-08 18:49:41 +0000412 tmp = (*nbr->nb_float)(r);
Guido van Rossum730806d1998-04-10 22:27:42 +0000413 if (own_r) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000414 Py_DECREF(r);
Guido van Rossum730806d1998-04-10 22:27:42 +0000415 }
Guido van Rossumfe4b6ee1996-08-08 18:49:41 +0000416 if (tmp == NULL)
417 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000418 cr.real = PyFloat_AsDouble(tmp);
419 Py_DECREF(tmp);
Guido van Rossum8a5c5d21996-01-12 01:09:56 +0000420 cr.imag = 0.;
421 }
422 if (i == NULL) {
423 ci.real = 0.;
424 ci.imag = 0.;
425 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000426 else if (PyComplex_Check(i))
427 ci = ((PyComplexObject*)i)->cval;
Guido van Rossum8a5c5d21996-01-12 01:09:56 +0000428 else {
Guido van Rossumc6472e91997-03-31 17:15:43 +0000429 tmp = (*nbi->nb_float)(i);
Guido van Rossumfe4b6ee1996-08-08 18:49:41 +0000430 if (tmp == NULL)
431 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000432 ci.real = PyFloat_AsDouble(tmp);
433 Py_DECREF(tmp);
Guido van Rossum8a5c5d21996-01-12 01:09:56 +0000434 ci.imag = 0.;
435 }
436 cr.real -= ci.imag;
437 cr.imag += ci.real;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000438 return PyComplex_FromCComplex(cr);
Guido van Rossum8a5c5d21996-01-12 01:09:56 +0000439}
440
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000441static char complex_doc[] =
442"complex(real[, imag]) -> complex number\n\
443\n\
444Create a complex number from a real part and an optional imaginary part.\n\
445This is equivalent to (real + imag*1j) where imag defaults to 0.";
446
447
Guido van Rossum8a5c5d21996-01-12 01:09:56 +0000448#endif
449
Guido van Rossum79f25d91997-04-29 20:08:16 +0000450static PyObject *
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000451builtin_dir(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000452 PyObject *self;
453 PyObject *args;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000454{
Guido van Rossum666b17a1997-05-06 16:36:57 +0000455 static char *attrlist[] = {"__members__", "__methods__", NULL};
456 PyObject *v = NULL, *l = NULL, *m = NULL;
457 PyObject *d, *x;
458 int i;
459 char **s;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000460
Guido van Rossum79f25d91997-04-29 20:08:16 +0000461 if (!PyArg_ParseTuple(args, "|O:dir", &v))
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000462 return NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000463 if (v == NULL) {
Guido van Rossum666b17a1997-05-06 16:36:57 +0000464 x = PyEval_GetLocals();
465 if (x == NULL)
466 goto error;
467 l = PyMapping_Keys(x);
468 if (l == NULL)
469 goto error;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000470 }
471 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000472 d = PyObject_GetAttrString(v, "__dict__");
Guido van Rossum666b17a1997-05-06 16:36:57 +0000473 if (d == NULL)
Guido van Rossum795ba581996-05-23 22:49:07 +0000474 PyErr_Clear();
Guido van Rossum666b17a1997-05-06 16:36:57 +0000475 else {
476 l = PyMapping_Keys(d);
477 if (l == NULL)
478 PyErr_Clear();
479 Py_DECREF(d);
480 }
481 if (l == NULL) {
482 l = PyList_New(0);
483 if (l == NULL)
484 goto error;
485 }
486 for (s = attrlist; *s != NULL; s++) {
487 m = PyObject_GetAttrString(v, *s);
488 if (m == NULL) {
489 PyErr_Clear();
490 continue;
491 }
492 for (i = 0; ; i++) {
493 x = PySequence_GetItem(m, i);
494 if (x == NULL) {
495 PyErr_Clear();
496 break;
497 }
498 if (PyList_Append(l, x) != 0) {
499 Py_DECREF(x);
500 Py_DECREF(m);
501 goto error;
502 }
503 Py_DECREF(x);
504 }
505 Py_DECREF(m);
Guido van Rossum795ba581996-05-23 22:49:07 +0000506 }
Guido van Rossum006bcd41991-10-24 14:54:44 +0000507 }
Guido van Rossum666b17a1997-05-06 16:36:57 +0000508 if (PyList_Sort(l) != 0)
509 goto error;
510 return l;
511 error:
512 Py_XDECREF(l);
513 return NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000514}
515
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000516static char dir_doc[] =
517"dir([object]) -> list of strings\n\
518\n\
519Return an alphabetized list of names comprising (some of) the attributes\n\
520of the given object. Without an argument, the names in the current scope\n\
521are listed. With an instance argument, only the instance attributes are\n\
522returned. With a class argument, attributes of the base class are not\n\
523returned. For other types or arguments, this may list members or methods.";
524
525
Guido van Rossum79f25d91997-04-29 20:08:16 +0000526static PyObject *
Guido van Rossum6a00cd81995-01-07 12:39:01 +0000527builtin_divmod(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000528 PyObject *self;
529 PyObject *args;
Guido van Rossum6a00cd81995-01-07 12:39:01 +0000530{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000531 PyObject *v, *w;
Guido van Rossum6a00cd81995-01-07 12:39:01 +0000532
Guido van Rossum79f25d91997-04-29 20:08:16 +0000533 if (!PyArg_ParseTuple(args, "OO:divmod", &v, &w))
Guido van Rossum6a00cd81995-01-07 12:39:01 +0000534 return NULL;
Guido van Rossum09df08a1998-05-22 00:51:39 +0000535 return PyNumber_Divmod(v, w);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000536}
537
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000538static char divmod_doc[] =
539"divmod(x, y) -> (div, mod)\n\
540\n\
541Return the tuple ((x-x%y)/y, x%y). Invariant: div*y + mod == x.";
542
543
Guido van Rossum79f25d91997-04-29 20:08:16 +0000544static PyObject *
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000545builtin_eval(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000546 PyObject *self;
547 PyObject *args;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000548{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000549 PyObject *cmd;
550 PyObject *globals = Py_None, *locals = Py_None;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000551 char *str;
Guido van Rossum590baa41993-11-30 13:40:46 +0000552
Guido van Rossum79f25d91997-04-29 20:08:16 +0000553 if (!PyArg_ParseTuple(args, "O|O!O!:eval",
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000554 &cmd,
Guido van Rossum79f25d91997-04-29 20:08:16 +0000555 &PyDict_Type, &globals,
556 &PyDict_Type, &locals))
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000557 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000558 if (globals == Py_None) {
559 globals = PyEval_GetGlobals();
560 if (locals == Py_None)
561 locals = PyEval_GetLocals();
Guido van Rossum6135a871995-01-09 17:53:26 +0000562 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000563 else if (locals == Py_None)
Guido van Rossum6135a871995-01-09 17:53:26 +0000564 locals = globals;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000565 if (PyDict_GetItemString(globals, "__builtins__") == NULL) {
566 if (PyDict_SetItemString(globals, "__builtins__",
567 PyEval_GetBuiltins()) != 0)
Guido van Rossum6135a871995-01-09 17:53:26 +0000568 return NULL;
569 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000570 if (PyCode_Check(cmd))
571 return PyEval_EvalCode((PyCodeObject *) cmd, globals, locals);
572 if (!PyString_Check(cmd)) {
573 PyErr_SetString(PyExc_TypeError,
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000574 "eval() argument 1 must be string or code object");
Guido van Rossum94390a41992-08-14 15:14:30 +0000575 return NULL;
576 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000577 str = PyString_AsString(cmd);
578 if ((int)strlen(str) != PyString_Size(cmd)) {
579 PyErr_SetString(PyExc_ValueError,
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000580 "embedded '\\0' in string arg");
581 return NULL;
Guido van Rossumf08ab0a1992-03-04 16:41:41 +0000582 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000583 while (*str == ' ' || *str == '\t')
584 str++;
Guido van Rossumb05a5c71997-05-07 17:46:13 +0000585 return PyRun_String(str, Py_eval_input, globals, locals);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000586}
587
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000588static char eval_doc[] =
589"eval(source[, globals[, locals]]) -> value\n\
590\n\
591Evaluate the source in the context of globals and locals.\n\
592The source may be a string representing a Python expression\n\
593or a code object as returned by compile().\n\
594The globals and locals are dictionaries, defaulting to the current\n\
595globals and locals. If only globals is given, locals defaults to it.";
596
597
Guido van Rossum79f25d91997-04-29 20:08:16 +0000598static PyObject *
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000599builtin_execfile(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000600 PyObject *self;
601 PyObject *args;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000602{
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000603 char *filename;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000604 PyObject *globals = Py_None, *locals = Py_None;
605 PyObject *res;
Guido van Rossum0f61f8a1992-02-25 18:55:05 +0000606 FILE* fp;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000607
Guido van Rossum79f25d91997-04-29 20:08:16 +0000608 if (!PyArg_ParseTuple(args, "s|O!O!:execfile",
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000609 &filename,
Guido van Rossum79f25d91997-04-29 20:08:16 +0000610 &PyDict_Type, &globals,
611 &PyDict_Type, &locals))
Guido van Rossum0f61f8a1992-02-25 18:55:05 +0000612 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000613 if (globals == Py_None) {
614 globals = PyEval_GetGlobals();
615 if (locals == Py_None)
616 locals = PyEval_GetLocals();
Guido van Rossum6135a871995-01-09 17:53:26 +0000617 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000618 else if (locals == Py_None)
Guido van Rossum6135a871995-01-09 17:53:26 +0000619 locals = globals;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000620 if (PyDict_GetItemString(globals, "__builtins__") == NULL) {
621 if (PyDict_SetItemString(globals, "__builtins__",
622 PyEval_GetBuiltins()) != 0)
Guido van Rossum6135a871995-01-09 17:53:26 +0000623 return NULL;
624 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000625 Py_BEGIN_ALLOW_THREADS
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000626 fp = fopen(filename, "r");
Guido van Rossum79f25d91997-04-29 20:08:16 +0000627 Py_END_ALLOW_THREADS
Guido van Rossum0f61f8a1992-02-25 18:55:05 +0000628 if (fp == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000629 PyErr_SetFromErrno(PyExc_IOError);
Guido van Rossum0f61f8a1992-02-25 18:55:05 +0000630 return NULL;
631 }
Guido van Rossumb05a5c71997-05-07 17:46:13 +0000632 res = PyRun_File(fp, filename, Py_file_input, globals, locals);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000633 Py_BEGIN_ALLOW_THREADS
Guido van Rossum0f61f8a1992-02-25 18:55:05 +0000634 fclose(fp);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000635 Py_END_ALLOW_THREADS
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000636 return res;
Guido van Rossum0f61f8a1992-02-25 18:55:05 +0000637}
638
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000639static char execfile_doc[] =
640"execfile(filename[, globals[, locals]])\n\
641\n\
642Read and execute a Python script from a file.\n\
643The globals and locals are dictionaries, defaulting to the current\n\
644globals and locals. If only globals is given, locals defaults to it.";
645
646
Guido van Rossum79f25d91997-04-29 20:08:16 +0000647static PyObject *
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000648builtin_float(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000649 PyObject *self;
650 PyObject *args;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000651{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000652 PyObject *v;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000653
Guido van Rossum79f25d91997-04-29 20:08:16 +0000654 if (!PyArg_ParseTuple(args, "O:float", &v))
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000655 return NULL;
Guido van Rossum09df08a1998-05-22 00:51:39 +0000656 return PyNumber_Float(v);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000657}
658
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000659static char float_doc[] =
660"float(x) -> floating point number\n\
661\n\
662Convert a string or number to a floating point number, if possible.";
663
664
Guido van Rossum79f25d91997-04-29 20:08:16 +0000665static PyObject *
Guido van Rossum94390a41992-08-14 15:14:30 +0000666builtin_getattr(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000667 PyObject *self;
668 PyObject *args;
Guido van Rossum33894be1992-01-27 16:53:09 +0000669{
Guido van Rossum950ff291998-06-29 13:38:57 +0000670 PyObject *v, *result, *dflt = NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000671 PyObject *name;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000672
Guido van Rossum950ff291998-06-29 13:38:57 +0000673 if (!PyArg_ParseTuple(args, "OS|O:getattr", &v, &name, &dflt))
Guido van Rossum33894be1992-01-27 16:53:09 +0000674 return NULL;
Guido van Rossum950ff291998-06-29 13:38:57 +0000675 result = PyObject_GetAttr(v, name);
676 if (result == NULL && dflt != NULL) {
677 PyErr_Clear();
678 Py_INCREF(dflt);
679 result = dflt;
680 }
681 return result;
Guido van Rossum9bfef441993-03-29 10:43:31 +0000682}
683
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000684static char getattr_doc[] =
Guido van Rossum950ff291998-06-29 13:38:57 +0000685"getattr(object, name[, default]) -> value\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000686\n\
Guido van Rossum950ff291998-06-29 13:38:57 +0000687Get a named attribute from an object; getattr(x, 'y') is equivalent to x.y.\n\
688When a default argument is given, it is returned when the attribute doesn't\n\
689exist; without it, an exception is raised in that case.";
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000690
691
Guido van Rossum79f25d91997-04-29 20:08:16 +0000692static PyObject *
Guido van Rossum872537c1995-07-07 22:43:42 +0000693builtin_globals(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000694 PyObject *self;
695 PyObject *args;
Guido van Rossum872537c1995-07-07 22:43:42 +0000696{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000697 PyObject *d;
Guido van Rossum872537c1995-07-07 22:43:42 +0000698
Guido van Rossum79f25d91997-04-29 20:08:16 +0000699 if (!PyArg_ParseTuple(args, ""))
Guido van Rossum872537c1995-07-07 22:43:42 +0000700 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000701 d = PyEval_GetGlobals();
702 Py_INCREF(d);
Guido van Rossum872537c1995-07-07 22:43:42 +0000703 return d;
704}
705
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000706static char globals_doc[] =
707"globals() -> dictionary\n\
708\n\
709Return the dictionary containing the current scope's global variables.";
710
711
Guido van Rossum79f25d91997-04-29 20:08:16 +0000712static PyObject *
Guido van Rossum9bfef441993-03-29 10:43:31 +0000713builtin_hasattr(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000714 PyObject *self;
715 PyObject *args;
Guido van Rossum9bfef441993-03-29 10:43:31 +0000716{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000717 PyObject *v;
718 PyObject *name;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000719
Guido van Rossum79f25d91997-04-29 20:08:16 +0000720 if (!PyArg_ParseTuple(args, "OS:hasattr", &v, &name))
Guido van Rossum9bfef441993-03-29 10:43:31 +0000721 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000722 v = PyObject_GetAttr(v, name);
Guido van Rossum9bfef441993-03-29 10:43:31 +0000723 if (v == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000724 PyErr_Clear();
Guido van Rossum09df08a1998-05-22 00:51:39 +0000725 Py_INCREF(Py_False);
726 return Py_False;
Guido van Rossum9bfef441993-03-29 10:43:31 +0000727 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000728 Py_DECREF(v);
Guido van Rossum09df08a1998-05-22 00:51:39 +0000729 Py_INCREF(Py_True);
730 return Py_True;
Guido van Rossum33894be1992-01-27 16:53:09 +0000731}
732
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000733static char hasattr_doc[] =
734"hasattr(object, name) -> Boolean\n\
735\n\
736Return whether the object has an attribute with the given name.\n\
737(This is done by calling getattr(object, name) and catching exceptions.)";
738
739
Guido van Rossum79f25d91997-04-29 20:08:16 +0000740static PyObject *
Guido van Rossum5b722181993-03-30 17:46:03 +0000741builtin_id(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000742 PyObject *self;
743 PyObject *args;
Guido van Rossum5b722181993-03-30 17:46:03 +0000744{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000745 PyObject *v;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000746
Guido van Rossum79f25d91997-04-29 20:08:16 +0000747 if (!PyArg_ParseTuple(args, "O:id", &v))
Guido van Rossum5b722181993-03-30 17:46:03 +0000748 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000749 return PyInt_FromLong((long)v);
Guido van Rossum5b722181993-03-30 17:46:03 +0000750}
751
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000752static char id_doc[] =
753"id(object) -> integer\n\
754\n\
755Return the identity of an object. This is guaranteed to be unique among\n\
756simultaneously existing objects. (Hint: it's the object's memory address.)";
757
758
Guido van Rossum79f25d91997-04-29 20:08:16 +0000759static PyObject *
Guido van Rossum12d12c51993-10-26 17:58:25 +0000760builtin_map(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000761 PyObject *self;
762 PyObject *args;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000763{
764 typedef struct {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000765 PyObject *seq;
766 PySequenceMethods *sqf;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000767 int len;
768 } sequence;
769
Guido van Rossum79f25d91997-04-29 20:08:16 +0000770 PyObject *func, *result;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000771 sequence *seqs = NULL, *sqp;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000772 int n, len;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000773 register int i, j;
774
Guido van Rossum79f25d91997-04-29 20:08:16 +0000775 n = PyTuple_Size(args);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000776 if (n < 2) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000777 PyErr_SetString(PyExc_TypeError,
778 "map() requires at least two args");
Guido van Rossum12d12c51993-10-26 17:58:25 +0000779 return NULL;
780 }
781
Guido van Rossum79f25d91997-04-29 20:08:16 +0000782 func = PyTuple_GetItem(args, 0);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000783 n--;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000784
Guido van Rossumfa4ac711998-07-10 17:37:30 +0000785 if (func == Py_None && n == 1) {
786 /* map(None, S) is the same as list(S). */
787 return PySequence_List(PyTuple_GetItem(args, 1));
788 }
789
Guido van Rossum79f25d91997-04-29 20:08:16 +0000790 if ((seqs = PyMem_NEW(sequence, n)) == NULL) {
791 PyErr_NoMemory();
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000792 goto Fail_2;
793 }
Guido van Rossum12d12c51993-10-26 17:58:25 +0000794
Guido van Rossum2d951851994-08-29 12:52:16 +0000795 for (len = 0, i = 0, sqp = seqs; i < n; ++i, ++sqp) {
Guido van Rossum12d12c51993-10-26 17:58:25 +0000796 int curlen;
Guido van Rossum09df08a1998-05-22 00:51:39 +0000797 PySequenceMethods *sqf;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000798
Guido van Rossum79f25d91997-04-29 20:08:16 +0000799 if ((sqp->seq = PyTuple_GetItem(args, i + 1)) == NULL)
Guido van Rossum12d12c51993-10-26 17:58:25 +0000800 goto Fail_2;
801
Guido van Rossum09df08a1998-05-22 00:51:39 +0000802 sqp->sqf = sqf = sqp->seq->ob_type->tp_as_sequence;
803 if (sqf == NULL ||
804 sqf->sq_length == NULL ||
805 sqf->sq_item == NULL)
806 {
Guido van Rossum12d12c51993-10-26 17:58:25 +0000807 static char errmsg[] =
808 "argument %d to map() must be a sequence object";
Guido van Rossum15e33a41997-04-30 19:00:27 +0000809 char errbuf[sizeof(errmsg) + 25];
Guido van Rossum12d12c51993-10-26 17:58:25 +0000810
811 sprintf(errbuf, errmsg, i+2);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000812 PyErr_SetString(PyExc_TypeError, errbuf);
Guido van Rossum12d12c51993-10-26 17:58:25 +0000813 goto Fail_2;
814 }
815
816 if ((curlen = sqp->len = (*sqp->sqf->sq_length)(sqp->seq)) < 0)
817 goto Fail_2;
818
819 if (curlen > len)
820 len = curlen;
821 }
822
Guido van Rossum79f25d91997-04-29 20:08:16 +0000823 if ((result = (PyObject *) PyList_New(len)) == NULL)
Guido van Rossum12d12c51993-10-26 17:58:25 +0000824 goto Fail_2;
825
Guido van Rossum2d951851994-08-29 12:52:16 +0000826 for (i = 0; ; ++i) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000827 PyObject *alist, *item=NULL, *value;
Guido van Rossum2d951851994-08-29 12:52:16 +0000828 int any = 0;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000829
Guido van Rossum79f25d91997-04-29 20:08:16 +0000830 if (func == Py_None && n == 1)
Guido van Rossum32120311995-07-10 13:52:21 +0000831 alist = NULL;
Guido van Rossum2d951851994-08-29 12:52:16 +0000832 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000833 if ((alist = PyTuple_New(n)) == NULL)
Guido van Rossum2d951851994-08-29 12:52:16 +0000834 goto Fail_1;
835 }
Guido van Rossum12d12c51993-10-26 17:58:25 +0000836
837 for (j = 0, sqp = seqs; j < n; ++j, ++sqp) {
Guido van Rossum2d951851994-08-29 12:52:16 +0000838 if (sqp->len < 0) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000839 Py_INCREF(Py_None);
840 item = Py_None;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000841 }
Guido van Rossum12d12c51993-10-26 17:58:25 +0000842 else {
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000843 item = (*sqp->sqf->sq_item)(sqp->seq, i);
Guido van Rossum2d951851994-08-29 12:52:16 +0000844 if (item == NULL) {
Barry Warsawcde8b1b1997-08-22 21:14:38 +0000845 if (PyErr_ExceptionMatches(
846 PyExc_IndexError))
847 {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000848 PyErr_Clear();
849 Py_INCREF(Py_None);
850 item = Py_None;
Guido van Rossum2d951851994-08-29 12:52:16 +0000851 sqp->len = -1;
852 }
853 else {
854 goto Fail_0;
855 }
856 }
857 else
858 any = 1;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000859
Guido van Rossum12d12c51993-10-26 17:58:25 +0000860 }
Guido van Rossum32120311995-07-10 13:52:21 +0000861 if (!alist)
Guido van Rossum2d951851994-08-29 12:52:16 +0000862 break;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000863 if (PyTuple_SetItem(alist, j, item) < 0) {
864 Py_DECREF(item);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000865 goto Fail_0;
Guido van Rossum2d951851994-08-29 12:52:16 +0000866 }
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000867 continue;
868
869 Fail_0:
Guido van Rossum79f25d91997-04-29 20:08:16 +0000870 Py_XDECREF(alist);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000871 goto Fail_1;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000872 }
873
Guido van Rossum32120311995-07-10 13:52:21 +0000874 if (!alist)
875 alist = item;
Guido van Rossum2d951851994-08-29 12:52:16 +0000876
877 if (!any) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000878 Py_DECREF(alist);
Guido van Rossum2d951851994-08-29 12:52:16 +0000879 break;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000880 }
Guido van Rossum2d951851994-08-29 12:52:16 +0000881
Guido van Rossum79f25d91997-04-29 20:08:16 +0000882 if (func == Py_None)
Guido van Rossum32120311995-07-10 13:52:21 +0000883 value = alist;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000884 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000885 value = PyEval_CallObject(func, alist);
886 Py_DECREF(alist);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000887 if (value == NULL)
888 goto Fail_1;
Guido van Rossum2d951851994-08-29 12:52:16 +0000889 }
890 if (i >= len) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000891 if (PyList_Append(result, value) < 0)
Guido van Rossum2d951851994-08-29 12:52:16 +0000892 goto Fail_1;
893 }
894 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000895 if (PyList_SetItem(result, i, value) < 0)
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000896 goto Fail_1;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000897 }
898 }
899
Guido van Rossumfa4ac711998-07-10 17:37:30 +0000900 if (i < len && PyList_SetSlice(result, i, len, NULL) < 0)
901 goto Fail_1;
902
Guido van Rossum79f25d91997-04-29 20:08:16 +0000903 PyMem_DEL(seqs);
Guido van Rossum12d12c51993-10-26 17:58:25 +0000904 return result;
905
Guido van Rossum12d12c51993-10-26 17:58:25 +0000906Fail_1:
Guido van Rossum79f25d91997-04-29 20:08:16 +0000907 Py_DECREF(result);
Guido van Rossum12d12c51993-10-26 17:58:25 +0000908Fail_2:
Guido van Rossum79f25d91997-04-29 20:08:16 +0000909 if (seqs) PyMem_DEL(seqs);
Guido van Rossum12d12c51993-10-26 17:58:25 +0000910 return NULL;
911}
912
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000913static char map_doc[] =
914"map(function, sequence[, sequence, ...]) -> list\n\
915\n\
916Return a list of the results of applying the function to the items of\n\
917the argument sequence(s). If more than one sequence is given, the\n\
918function is called with an argument list consisting of the corresponding\n\
919item of each sequence, substituting None for missing values when not all\n\
920sequences have the same length. If the function is None, return a list of\n\
921the items of the sequence (or a list of tuples if more than one sequence).";
922
923
Guido van Rossum79f25d91997-04-29 20:08:16 +0000924static PyObject *
Guido van Rossum94390a41992-08-14 15:14:30 +0000925builtin_setattr(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000926 PyObject *self;
927 PyObject *args;
Guido van Rossum33894be1992-01-27 16:53:09 +0000928{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000929 PyObject *v;
930 PyObject *name;
931 PyObject *value;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000932
Guido van Rossum79f25d91997-04-29 20:08:16 +0000933 if (!PyArg_ParseTuple(args, "OSO:setattr", &v, &name, &value))
Guido van Rossum33894be1992-01-27 16:53:09 +0000934 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000935 if (PyObject_SetAttr(v, name, value) != 0)
Guido van Rossum33894be1992-01-27 16:53:09 +0000936 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000937 Py_INCREF(Py_None);
938 return Py_None;
Guido van Rossum33894be1992-01-27 16:53:09 +0000939}
940
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000941static char setattr_doc[] =
942"setattr(object, name, value)\n\
943\n\
944Set a named attribute on an object; setattr(x, 'y', v) is equivalent to\n\
945``x.y = v''.";
946
947
Guido van Rossum79f25d91997-04-29 20:08:16 +0000948static PyObject *
Guido van Rossum14144fc1994-08-29 12:53:40 +0000949builtin_delattr(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000950 PyObject *self;
951 PyObject *args;
Guido van Rossum14144fc1994-08-29 12:53:40 +0000952{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000953 PyObject *v;
954 PyObject *name;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000955
Guido van Rossum79f25d91997-04-29 20:08:16 +0000956 if (!PyArg_ParseTuple(args, "OS:delattr", &v, &name))
Guido van Rossum14144fc1994-08-29 12:53:40 +0000957 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000958 if (PyObject_SetAttr(v, name, (PyObject *)NULL) != 0)
Guido van Rossum14144fc1994-08-29 12:53:40 +0000959 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000960 Py_INCREF(Py_None);
961 return Py_None;
Guido van Rossum14144fc1994-08-29 12:53:40 +0000962}
963
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000964static char delattr_doc[] =
965"setattr(object, name, value)\n\
966\n\
967Delete a named attribute on an object; delattr(x, 'y') is equivalent to\n\
968``del x.y''.";
969
970
Guido van Rossum79f25d91997-04-29 20:08:16 +0000971static PyObject *
Guido van Rossum9bfef441993-03-29 10:43:31 +0000972builtin_hash(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000973 PyObject *self;
974 PyObject *args;
Guido van Rossum9bfef441993-03-29 10:43:31 +0000975{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000976 PyObject *v;
Guido van Rossum9bfef441993-03-29 10:43:31 +0000977 long x;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000978
Guido van Rossum79f25d91997-04-29 20:08:16 +0000979 if (!PyArg_ParseTuple(args, "O:hash", &v))
Guido van Rossum9bfef441993-03-29 10:43:31 +0000980 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000981 x = PyObject_Hash(v);
Guido van Rossum9bfef441993-03-29 10:43:31 +0000982 if (x == -1)
983 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000984 return PyInt_FromLong(x);
Guido van Rossum9bfef441993-03-29 10:43:31 +0000985}
986
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000987static char hash_doc[] =
988"hash(object) -> integer\n\
989\n\
990Return a hash value for the object. Two objects with the same value have\n\
991the same hash value. The reverse is not necessarily true, but likely.";
992
993
Guido van Rossum79f25d91997-04-29 20:08:16 +0000994static PyObject *
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000995builtin_hex(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000996 PyObject *self;
997 PyObject *args;
Guido van Rossum006bcd41991-10-24 14:54:44 +0000998{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000999 PyObject *v;
1000 PyNumberMethods *nb;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001001
Guido van Rossum79f25d91997-04-29 20:08:16 +00001002 if (!PyArg_ParseTuple(args, "O:hex", &v))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001003 return NULL;
Guido van Rossum1899c2e1992-09-12 11:09:23 +00001004
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001005 if ((nb = v->ob_type->tp_as_number) == NULL ||
Guido van Rossum1899c2e1992-09-12 11:09:23 +00001006 nb->nb_hex == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001007 PyErr_SetString(PyExc_TypeError,
Guido van Rossum1899c2e1992-09-12 11:09:23 +00001008 "hex() argument can't be converted to hex");
1009 return NULL;
Guido van Rossum006bcd41991-10-24 14:54:44 +00001010 }
Guido van Rossum1899c2e1992-09-12 11:09:23 +00001011 return (*nb->nb_hex)(v);
Guido van Rossum006bcd41991-10-24 14:54:44 +00001012}
1013
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001014static char hex_doc[] =
1015"hex(number) -> string\n\
1016\n\
1017Return the hexadecimal representation of an integer or long integer.";
1018
1019
Guido van Rossum79f25d91997-04-29 20:08:16 +00001020static PyObject *builtin_raw_input Py_PROTO((PyObject *, PyObject *));
Guido van Rossum3165fe61992-09-25 21:59:05 +00001021
Guido van Rossum79f25d91997-04-29 20:08:16 +00001022static PyObject *
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001023builtin_input(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001024 PyObject *self;
1025 PyObject *args;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001026{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001027 PyObject *line;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001028 char *str;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001029 PyObject *res;
1030 PyObject *globals, *locals;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001031
1032 line = builtin_raw_input(self, args);
Guido van Rossum3165fe61992-09-25 21:59:05 +00001033 if (line == NULL)
1034 return line;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001035 if (!PyArg_Parse(line, "s;embedded '\\0' in input line", &str))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001036 return NULL;
1037 while (*str == ' ' || *str == '\t')
1038 str++;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001039 globals = PyEval_GetGlobals();
1040 locals = PyEval_GetLocals();
1041 if (PyDict_GetItemString(globals, "__builtins__") == NULL) {
1042 if (PyDict_SetItemString(globals, "__builtins__",
1043 PyEval_GetBuiltins()) != 0)
Guido van Rossum6135a871995-01-09 17:53:26 +00001044 return NULL;
1045 }
Guido van Rossumb05a5c71997-05-07 17:46:13 +00001046 res = PyRun_String(str, Py_eval_input, globals, locals);
Guido van Rossum79f25d91997-04-29 20:08:16 +00001047 Py_DECREF(line);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001048 return res;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001049}
1050
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001051static char input_doc[] =
1052"input([prompt]) -> value\n\
1053\n\
1054Equivalent to eval(raw_input(prompt)).";
1055
1056
Guido van Rossume8811f81997-02-14 15:48:05 +00001057static PyObject *
1058builtin_intern(self, args)
1059 PyObject *self;
1060 PyObject *args;
1061{
1062 PyObject *s;
1063 if (!PyArg_ParseTuple(args, "S", &s))
1064 return NULL;
1065 Py_INCREF(s);
1066 PyString_InternInPlace(&s);
1067 return s;
1068}
1069
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001070static char intern_doc[] =
1071"intern(string) -> string\n\
1072\n\
1073``Intern'' the given string. This enters the string in the (global)\n\
1074table of interned strings whose purpose is to speed up dictionary lookups.\n\
1075Return the string itself or the previously interned string object with the\n\
1076same value.";
1077
1078
Guido van Rossum79f25d91997-04-29 20:08:16 +00001079static PyObject *
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001080builtin_int(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001081 PyObject *self;
1082 PyObject *args;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001083{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001084 PyObject *v;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001085
Guido van Rossum79f25d91997-04-29 20:08:16 +00001086 if (!PyArg_ParseTuple(args, "O:int", &v))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001087 return NULL;
Guido van Rossum09df08a1998-05-22 00:51:39 +00001088 return PyNumber_Int(v);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001089}
1090
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001091static char int_doc[] =
1092"int(x) -> integer\n\
1093\n\
1094Convert a string or number to an integer, if possible.\n\
1095A floating point argument will be truncated towards zero.";
1096
1097
Guido van Rossum79f25d91997-04-29 20:08:16 +00001098static PyObject *
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001099builtin_len(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001100 PyObject *self;
1101 PyObject *args;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001102{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001103 PyObject *v;
Guido van Rossum8ea9f4d1998-06-29 22:26:50 +00001104 long res;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001105
Guido van Rossum79f25d91997-04-29 20:08:16 +00001106 if (!PyArg_ParseTuple(args, "O:len", &v))
Guido van Rossum3f5da241990-12-20 15:06:42 +00001107 return NULL;
Guido van Rossum8ea9f4d1998-06-29 22:26:50 +00001108 res = PyObject_Length(v);
1109 if (res < 0 && PyErr_Occurred())
1110 return NULL;
1111 return PyInt_FromLong(res);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001112}
1113
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001114static char len_doc[] =
1115"len(object) -> integer\n\
1116\n\
1117Return the number of items of a sequence or mapping.";
1118
1119
Guido van Rossum79f25d91997-04-29 20:08:16 +00001120static PyObject *
Guido van Rossumd1705771996-04-09 02:41:06 +00001121builtin_list(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001122 PyObject *self;
1123 PyObject *args;
Guido van Rossumd1705771996-04-09 02:41:06 +00001124{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001125 PyObject *v;
Guido van Rossumd1705771996-04-09 02:41:06 +00001126
Guido van Rossum79f25d91997-04-29 20:08:16 +00001127 if (!PyArg_ParseTuple(args, "O:list", &v))
Guido van Rossumd1705771996-04-09 02:41:06 +00001128 return NULL;
Guido van Rossum09df08a1998-05-22 00:51:39 +00001129 return PySequence_List(v);
Guido van Rossumd1705771996-04-09 02:41:06 +00001130}
1131
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001132static char list_doc[] =
1133"list(sequence) -> list\n\
1134\n\
1135Return a new list whose items are the same as those of the argument sequence.";
1136
Guido van Rossum8861b741996-07-30 16:49:37 +00001137
1138static PyObject *
1139builtin_slice(self, args)
1140 PyObject *self;
1141 PyObject *args;
1142{
Guido van Rossum09df08a1998-05-22 00:51:39 +00001143 PyObject *start, *stop, *step;
Guido van Rossum8861b741996-07-30 16:49:37 +00001144
Guido van Rossum09df08a1998-05-22 00:51:39 +00001145 start = stop = step = NULL;
Guido van Rossum8861b741996-07-30 16:49:37 +00001146
Guido van Rossum09df08a1998-05-22 00:51:39 +00001147 if (!PyArg_ParseTuple(args, "O|OO:slice", &start, &stop, &step))
1148 return NULL;
Guido van Rossum8861b741996-07-30 16:49:37 +00001149
Guido van Rossum09df08a1998-05-22 00:51:39 +00001150 /* This swapping of stop and start is to maintain similarity with
1151 range(). */
1152 if (stop == NULL) {
1153 stop = start;
1154 start = NULL;
1155 }
1156 return PySlice_New(start, stop, step);
Guido van Rossum8861b741996-07-30 16:49:37 +00001157}
1158
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001159static char slice_doc[] =
1160"slice([start,] step[, stop]) -> slice object\n\
1161\n\
1162Create a slice object. This is used for slicing by the Numeric extensions.";
1163
1164
Guido van Rossum79f25d91997-04-29 20:08:16 +00001165static PyObject *
Guido van Rossum872537c1995-07-07 22:43:42 +00001166builtin_locals(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001167 PyObject *self;
1168 PyObject *args;
Guido van Rossum872537c1995-07-07 22:43:42 +00001169{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001170 PyObject *d;
Guido van Rossum872537c1995-07-07 22:43:42 +00001171
Guido van Rossum79f25d91997-04-29 20:08:16 +00001172 if (!PyArg_ParseTuple(args, ""))
Guido van Rossum872537c1995-07-07 22:43:42 +00001173 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001174 d = PyEval_GetLocals();
1175 Py_INCREF(d);
Guido van Rossum872537c1995-07-07 22:43:42 +00001176 return d;
1177}
1178
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001179static char locals_doc[] =
1180"locals() -> dictionary\n\
1181\n\
1182Return the dictionary containing the current scope's local variables.";
1183
1184
Guido van Rossum79f25d91997-04-29 20:08:16 +00001185static PyObject *
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001186builtin_long(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001187 PyObject *self;
1188 PyObject *args;
Guido van Rossumd4905451991-05-05 20:00:36 +00001189{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001190 PyObject *v;
Guido van Rossum1899c2e1992-09-12 11:09:23 +00001191
Guido van Rossum79f25d91997-04-29 20:08:16 +00001192 if (!PyArg_ParseTuple(args, "O:long", &v))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001193 return NULL;
Guido van Rossum09df08a1998-05-22 00:51:39 +00001194 return PyNumber_Long(v);
Guido van Rossumd4905451991-05-05 20:00:36 +00001195}
1196
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001197static char long_doc[] =
1198"long(x) -> long integer\n\
1199\n\
1200Convert a string or number to a long integer, if possible.\n\
1201A floating point argument will be truncated towards zero.";
1202
1203
Guido van Rossum79f25d91997-04-29 20:08:16 +00001204static PyObject *
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001205min_max(args, sign)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001206 PyObject *args;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001207 int sign;
1208{
Guido van Rossum2d951851994-08-29 12:52:16 +00001209 int i;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001210 PyObject *v, *w, *x;
1211 PySequenceMethods *sq;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001212
Guido van Rossum79f25d91997-04-29 20:08:16 +00001213 if (PyTuple_Size(args) > 1)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001214 v = args;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001215 else if (!PyArg_ParseTuple(args, "O:min/max", &v))
Guido van Rossum3f5da241990-12-20 15:06:42 +00001216 return NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001217 sq = v->ob_type->tp_as_sequence;
Guido van Rossum09df08a1998-05-22 00:51:39 +00001218 if (sq == NULL || sq->sq_item == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001219 PyErr_SetString(PyExc_TypeError,
1220 "min() or max() of non-sequence");
Guido van Rossum3f5da241990-12-20 15:06:42 +00001221 return NULL;
1222 }
Guido van Rossum2d951851994-08-29 12:52:16 +00001223 w = NULL;
1224 for (i = 0; ; i++) {
Guido van Rossum3f5da241990-12-20 15:06:42 +00001225 x = (*sq->sq_item)(v, i); /* Implies INCREF */
Guido van Rossum2d951851994-08-29 12:52:16 +00001226 if (x == NULL) {
Barry Warsawcde8b1b1997-08-22 21:14:38 +00001227 if (PyErr_ExceptionMatches(PyExc_IndexError)) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001228 PyErr_Clear();
Guido van Rossum2d951851994-08-29 12:52:16 +00001229 break;
1230 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00001231 Py_XDECREF(w);
Guido van Rossum2d951851994-08-29 12:52:16 +00001232 return NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001233 }
Guido van Rossum2d951851994-08-29 12:52:16 +00001234 if (w == NULL)
1235 w = x;
1236 else {
Guido van Rossumc8b6df91997-05-23 00:06:51 +00001237 int c = PyObject_Compare(x, w);
1238 if (c && PyErr_Occurred()) {
1239 Py_DECREF(x);
1240 Py_XDECREF(w);
1241 return NULL;
1242 }
1243 if (c * sign > 0) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001244 Py_DECREF(w);
Guido van Rossum2d951851994-08-29 12:52:16 +00001245 w = x;
1246 }
1247 else
Guido van Rossum79f25d91997-04-29 20:08:16 +00001248 Py_DECREF(x);
Guido van Rossum2d951851994-08-29 12:52:16 +00001249 }
Guido van Rossum3f5da241990-12-20 15:06:42 +00001250 }
Guido van Rossum2d951851994-08-29 12:52:16 +00001251 if (w == NULL)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001252 PyErr_SetString(PyExc_ValueError,
1253 "min() or max() of empty sequence");
Guido van Rossum3f5da241990-12-20 15:06:42 +00001254 return w;
1255}
1256
Guido van Rossum79f25d91997-04-29 20:08:16 +00001257static PyObject *
Guido van Rossum3f5da241990-12-20 15:06:42 +00001258builtin_min(self, v)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001259 PyObject *self;
1260 PyObject *v;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001261{
1262 return min_max(v, -1);
1263}
1264
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001265static char min_doc[] =
1266"min(sequence) -> value\n\
1267min(a, b, c, ...) -> value\n\
1268\n\
1269With a single sequence argument, return its smallest item.\n\
1270With two or more arguments, return the smallest argument.";
1271
1272
Guido van Rossum79f25d91997-04-29 20:08:16 +00001273static PyObject *
Guido van Rossum3f5da241990-12-20 15:06:42 +00001274builtin_max(self, v)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001275 PyObject *self;
1276 PyObject *v;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001277{
1278 return min_max(v, 1);
1279}
1280
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001281static char max_doc[] =
1282"max(sequence) -> value\n\
1283max(a, b, c, ...) -> value\n\
1284\n\
1285With a single sequence argument, return its largest item.\n\
1286With two or more arguments, return the largest argument.";
1287
1288
Guido van Rossum79f25d91997-04-29 20:08:16 +00001289static PyObject *
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001290builtin_oct(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001291 PyObject *self;
1292 PyObject *args;
Guido van Rossum006bcd41991-10-24 14:54:44 +00001293{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001294 PyObject *v;
1295 PyNumberMethods *nb;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001296
Guido van Rossum79f25d91997-04-29 20:08:16 +00001297 if (!PyArg_ParseTuple(args, "O:oct", &v))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001298 return NULL;
Guido van Rossum1899c2e1992-09-12 11:09:23 +00001299 if (v == NULL || (nb = v->ob_type->tp_as_number) == NULL ||
1300 nb->nb_oct == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001301 PyErr_SetString(PyExc_TypeError,
Guido van Rossum1899c2e1992-09-12 11:09:23 +00001302 "oct() argument can't be converted to oct");
1303 return NULL;
Guido van Rossum006bcd41991-10-24 14:54:44 +00001304 }
Guido van Rossum1899c2e1992-09-12 11:09:23 +00001305 return (*nb->nb_oct)(v);
Guido van Rossum006bcd41991-10-24 14:54:44 +00001306}
1307
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001308static char oct_doc[] =
1309"oct(number) -> string\n\
1310\n\
1311Return the octal representation of an integer or long integer.";
1312
1313
Guido van Rossum79f25d91997-04-29 20:08:16 +00001314static PyObject *
Guido van Rossum94390a41992-08-14 15:14:30 +00001315builtin_open(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001316 PyObject *self;
1317 PyObject *args;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001318{
Guido van Rossum2d951851994-08-29 12:52:16 +00001319 char *name;
1320 char *mode = "r";
1321 int bufsize = -1;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001322 PyObject *f;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001323
Guido van Rossum79f25d91997-04-29 20:08:16 +00001324 if (!PyArg_ParseTuple(args, "s|si:open", &name, &mode, &bufsize))
Guido van Rossum3f5da241990-12-20 15:06:42 +00001325 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001326 f = PyFile_FromString(name, mode);
Guido van Rossum2d951851994-08-29 12:52:16 +00001327 if (f != NULL)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001328 PyFile_SetBufSize(f, bufsize);
Guido van Rossum2d951851994-08-29 12:52:16 +00001329 return f;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001330}
1331
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001332static char open_doc[] =
1333"open(filename[, mode[, buffering]]) -> file object\n\
1334\n\
1335Open a file. The mode can be 'r', 'w' or 'a' for reading (default),\n\
1336writing or appending. The file will be created if it doesn't exist\n\
1337when opened for writing or appending; it will be truncated when\n\
1338opened for writing. Add a 'b' to the mode for binary files.\n\
1339Add a '+' to the mode to allow simultaneous reading and writing.\n\
1340If the buffering argument is given, 0 means unbuffered, 1 means line\n\
1341buffered, and larger numbers specify the buffer size.";
1342
1343
Guido van Rossum79f25d91997-04-29 20:08:16 +00001344static PyObject *
Guido van Rossum94390a41992-08-14 15:14:30 +00001345builtin_ord(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001346 PyObject *self;
1347 PyObject *args;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001348{
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001349 char c;
1350
Guido van Rossum79f25d91997-04-29 20:08:16 +00001351 if (!PyArg_ParseTuple(args, "c:ord", &c))
Guido van Rossum3f5da241990-12-20 15:06:42 +00001352 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001353 return PyInt_FromLong((long)(c & 0xff));
Guido van Rossum3f5da241990-12-20 15:06:42 +00001354}
1355
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001356static char ord_doc[] =
1357"ord(c) -> integer\n\
1358\n\
1359Return the integer ordinal of a one character string.";
1360
1361
Guido van Rossum79f25d91997-04-29 20:08:16 +00001362static PyObject *
Guido van Rossum6a00cd81995-01-07 12:39:01 +00001363builtin_pow(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001364 PyObject *self;
1365 PyObject *args;
Guido van Rossum6a00cd81995-01-07 12:39:01 +00001366{
Guido van Rossum09df08a1998-05-22 00:51:39 +00001367 PyObject *v, *w, *z = Py_None;
Guido van Rossum6a00cd81995-01-07 12:39:01 +00001368
Guido van Rossum79f25d91997-04-29 20:08:16 +00001369 if (!PyArg_ParseTuple(args, "OO|O:pow", &v, &w, &z))
Guido van Rossum6a00cd81995-01-07 12:39:01 +00001370 return NULL;
Guido van Rossum09df08a1998-05-22 00:51:39 +00001371 return PyNumber_Power(v, w, z);
Guido van Rossumd4905451991-05-05 20:00:36 +00001372}
1373
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001374static char pow_doc[] =
1375"pow(x, y[, z]) -> number\n\
1376\n\
1377With two arguments, equivalent to x**y. With three arguments,\n\
1378equivalent to (x**y) % z, but may be more efficient (e.g. for longs).";
1379
1380
Guido van Rossum79f25d91997-04-29 20:08:16 +00001381static PyObject *
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001382builtin_range(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001383 PyObject *self;
1384 PyObject *args;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001385{
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001386 long ilow = 0, ihigh = 0, istep = 1;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001387 int i, n;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001388 PyObject *v;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001389
Guido van Rossum79f25d91997-04-29 20:08:16 +00001390 if (PyTuple_Size(args) <= 1) {
1391 if (!PyArg_ParseTuple(args,
Guido van Rossum0865dd91995-01-17 16:30:22 +00001392 "l;range() requires 1-3 int arguments",
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001393 &ihigh))
1394 return NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001395 }
1396 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001397 if (!PyArg_ParseTuple(args,
Guido van Rossum0865dd91995-01-17 16:30:22 +00001398 "ll|l;range() requires 1-3 int arguments",
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001399 &ilow, &ihigh, &istep))
Guido van Rossum3f5da241990-12-20 15:06:42 +00001400 return NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001401 }
1402 if (istep == 0) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001403 PyErr_SetString(PyExc_ValueError, "zero step for range()");
Guido van Rossum3f5da241990-12-20 15:06:42 +00001404 return NULL;
1405 }
1406 /* XXX ought to check overflow of subtraction */
1407 if (istep > 0)
1408 n = (ihigh - ilow + istep - 1) / istep;
1409 else
1410 n = (ihigh - ilow + istep + 1) / istep;
1411 if (n < 0)
1412 n = 0;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001413 v = PyList_New(n);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001414 if (v == NULL)
1415 return NULL;
1416 for (i = 0; i < n; i++) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001417 PyObject *w = PyInt_FromLong(ilow);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001418 if (w == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001419 Py_DECREF(v);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001420 return NULL;
1421 }
Guido van Rossuma937d141998-04-24 18:22:02 +00001422 PyList_SET_ITEM(v, i, w);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001423 ilow += istep;
1424 }
1425 return v;
1426}
1427
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001428static char range_doc[] =
1429"range([start,] stop[, step]) -> list of integers\n\
1430\n\
1431Return a list containing an arithmetic progression of integers.\n\
1432range(i, j) returns [i, i+1, i+2, ..., j-1]; start (!) defaults to 0.\n\
1433When step is given, it specifies the increment (or decrement).\n\
1434For example, range(4) returns [0, 1, 2, 3]. The end point is omitted!\n\
1435These are exactly the valid indices for a list of 4 elements.";
1436
1437
Guido van Rossum79f25d91997-04-29 20:08:16 +00001438static PyObject *
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001439builtin_xrange(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001440 PyObject *self;
1441 PyObject *args;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001442{
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001443 long ilow = 0, ihigh = 0, istep = 1;
Guido van Rossum0865dd91995-01-17 16:30:22 +00001444 long n;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001445
Guido van Rossum79f25d91997-04-29 20:08:16 +00001446 if (PyTuple_Size(args) <= 1) {
1447 if (!PyArg_ParseTuple(args,
Guido van Rossum0865dd91995-01-17 16:30:22 +00001448 "l;xrange() requires 1-3 int arguments",
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001449 &ihigh))
1450 return NULL;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001451 }
1452 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001453 if (!PyArg_ParseTuple(args,
Guido van Rossum0865dd91995-01-17 16:30:22 +00001454 "ll|l;xrange() requires 1-3 int arguments",
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001455 &ilow, &ihigh, &istep))
Guido van Rossum12d12c51993-10-26 17:58:25 +00001456 return NULL;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001457 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001458 if (istep == 0) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001459 PyErr_SetString(PyExc_ValueError, "zero step for xrange()");
Guido van Rossum12d12c51993-10-26 17:58:25 +00001460 return NULL;
1461 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001462 /* XXX ought to check overflow of subtraction */
1463 if (istep > 0)
1464 n = (ihigh - ilow + istep - 1) / istep;
1465 else
1466 n = (ihigh - ilow + istep + 1) / istep;
1467 if (n < 0)
1468 n = 0;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001469 return PyRange_New(ilow, n, istep, 1);
Guido van Rossum12d12c51993-10-26 17:58:25 +00001470}
1471
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001472static char xrange_doc[] =
1473"xrange([start,] stop[, step]) -> xrange object\n\
1474\n\
1475Like range(), but instead of returning a list, returns an object that\n\
1476generates the numbers in the range on demand. This is slightly slower\n\
1477than range() but more memory efficient.";
1478
1479
Guido van Rossum79f25d91997-04-29 20:08:16 +00001480extern char *PyOS_Readline Py_PROTO((char *));
Guido van Rossum872537c1995-07-07 22:43:42 +00001481
Guido van Rossum79f25d91997-04-29 20:08:16 +00001482static PyObject *
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001483builtin_raw_input(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001484 PyObject *self;
1485 PyObject *args;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001486{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001487 PyObject *v = NULL;
1488 PyObject *f;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001489
Guido van Rossum79f25d91997-04-29 20:08:16 +00001490 if (!PyArg_ParseTuple(args, "|O:[raw_]input", &v))
Guido van Rossum3165fe61992-09-25 21:59:05 +00001491 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001492 if (PyFile_AsFile(PySys_GetObject("stdin")) == stdin &&
1493 PyFile_AsFile(PySys_GetObject("stdout")) == stdout &&
Guido van Rossum53bb7ff1995-07-26 16:26:31 +00001494 isatty(fileno(stdin)) && isatty(fileno(stdout))) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001495 PyObject *po;
Guido van Rossum872537c1995-07-07 22:43:42 +00001496 char *prompt;
1497 char *s;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001498 PyObject *result;
Guido van Rossum872537c1995-07-07 22:43:42 +00001499 if (v != NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001500 po = PyObject_Str(v);
Guido van Rossum872537c1995-07-07 22:43:42 +00001501 if (po == NULL)
1502 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001503 prompt = PyString_AsString(po);
Guido van Rossumd9b52081998-06-26 18:25:38 +00001504 if (prompt == NULL)
1505 return NULL;
Guido van Rossum872537c1995-07-07 22:43:42 +00001506 }
1507 else {
1508 po = NULL;
1509 prompt = "";
1510 }
Guido van Rossumee81af81997-09-26 21:47:43 +00001511 Py_BEGIN_ALLOW_THREADS
Guido van Rossum79f25d91997-04-29 20:08:16 +00001512 s = PyOS_Readline(prompt);
Guido van Rossumee81af81997-09-26 21:47:43 +00001513 Py_END_ALLOW_THREADS
Guido van Rossum79f25d91997-04-29 20:08:16 +00001514 Py_XDECREF(po);
Guido van Rossum872537c1995-07-07 22:43:42 +00001515 if (s == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001516 PyErr_SetNone(PyExc_KeyboardInterrupt);
Guido van Rossum872537c1995-07-07 22:43:42 +00001517 return NULL;
1518 }
1519 if (*s == '\0') {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001520 PyErr_SetNone(PyExc_EOFError);
Guido van Rossum872537c1995-07-07 22:43:42 +00001521 result = NULL;
1522 }
1523 else { /* strip trailing '\n' */
Guido van Rossum79f25d91997-04-29 20:08:16 +00001524 result = PyString_FromStringAndSize(s, strlen(s)-1);
Guido van Rossum872537c1995-07-07 22:43:42 +00001525 }
1526 free(s);
1527 return result;
1528 }
Guido van Rossum90933611991-06-07 16:10:43 +00001529 if (v != NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001530 f = PySys_GetObject("stdout");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001531 if (f == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001532 PyErr_SetString(PyExc_RuntimeError, "lost sys.stdout");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001533 return NULL;
1534 }
Guido van Rossumc8b6df91997-05-23 00:06:51 +00001535 if (Py_FlushLine() != 0 ||
1536 PyFile_WriteObject(v, f, Py_PRINT_RAW) != 0)
Guido van Rossum90933611991-06-07 16:10:43 +00001537 return NULL;
1538 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00001539 f = PySys_GetObject("stdin");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001540 if (f == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001541 PyErr_SetString(PyExc_RuntimeError, "lost sys.stdin");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001542 return NULL;
1543 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00001544 return PyFile_GetLine(f, -1);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001545}
1546
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001547static char raw_input_doc[] =
1548"raw_input([prompt]) -> string\n\
1549\n\
1550Read a string from standard input. The trailing newline is stripped.\n\
1551If the user hits EOF (Unix: Ctl-D, Windows: Ctl-Z+Return), raise EOFError.\n\
1552On Unix, GNU readline is used if enabled. The prompt string, if given,\n\
1553is printed without a trailing newline before reading.";
1554
1555
Guido van Rossum79f25d91997-04-29 20:08:16 +00001556static PyObject *
Guido van Rossum12d12c51993-10-26 17:58:25 +00001557builtin_reduce(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001558 PyObject *self;
1559 PyObject *args;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001560{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001561 PyObject *seq, *func, *result = NULL;
1562 PySequenceMethods *sqf;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001563 register int i;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001564
Guido van Rossum79f25d91997-04-29 20:08:16 +00001565 if (!PyArg_ParseTuple(args, "OO|O:reduce", &func, &seq, &result))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001566 return NULL;
1567 if (result != NULL)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001568 Py_INCREF(result);
Guido van Rossum12d12c51993-10-26 17:58:25 +00001569
Guido van Rossum09df08a1998-05-22 00:51:39 +00001570 sqf = seq->ob_type->tp_as_sequence;
1571 if (sqf == NULL || sqf->sq_item == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001572 PyErr_SetString(PyExc_TypeError,
Guido van Rossum12d12c51993-10-26 17:58:25 +00001573 "2nd argument to reduce() must be a sequence object");
1574 return NULL;
1575 }
1576
Guido van Rossum79f25d91997-04-29 20:08:16 +00001577 if ((args = PyTuple_New(2)) == NULL)
Guido van Rossum2d951851994-08-29 12:52:16 +00001578 goto Fail;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001579
Guido van Rossum2d951851994-08-29 12:52:16 +00001580 for (i = 0; ; ++i) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001581 PyObject *op2;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001582
1583 if (args->ob_refcnt > 1) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001584 Py_DECREF(args);
1585 if ((args = PyTuple_New(2)) == NULL)
Guido van Rossum2d951851994-08-29 12:52:16 +00001586 goto Fail;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001587 }
1588
Guido van Rossum2d951851994-08-29 12:52:16 +00001589 if ((op2 = (*sqf->sq_item)(seq, i)) == NULL) {
Barry Warsawcde8b1b1997-08-22 21:14:38 +00001590 if (PyErr_ExceptionMatches(PyExc_IndexError)) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001591 PyErr_Clear();
Guido van Rossum2d951851994-08-29 12:52:16 +00001592 break;
1593 }
1594 goto Fail;
1595 }
Guido van Rossum12d12c51993-10-26 17:58:25 +00001596
Guido van Rossum2d951851994-08-29 12:52:16 +00001597 if (result == NULL)
1598 result = op2;
1599 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001600 PyTuple_SetItem(args, 0, result);
1601 PyTuple_SetItem(args, 1, op2);
1602 if ((result = PyEval_CallObject(func, args)) == NULL)
Guido van Rossum2d951851994-08-29 12:52:16 +00001603 goto Fail;
1604 }
Guido van Rossum12d12c51993-10-26 17:58:25 +00001605 }
1606
Guido van Rossum79f25d91997-04-29 20:08:16 +00001607 Py_DECREF(args);
Guido van Rossum12d12c51993-10-26 17:58:25 +00001608
Guido van Rossum2d951851994-08-29 12:52:16 +00001609 if (result == NULL)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001610 PyErr_SetString(PyExc_TypeError,
Guido van Rossum2d951851994-08-29 12:52:16 +00001611 "reduce of empty sequence with no initial value");
1612
Guido van Rossum12d12c51993-10-26 17:58:25 +00001613 return result;
1614
Guido van Rossum2d951851994-08-29 12:52:16 +00001615Fail:
Guido van Rossum79f25d91997-04-29 20:08:16 +00001616 Py_XDECREF(args);
1617 Py_XDECREF(result);
Guido van Rossum12d12c51993-10-26 17:58:25 +00001618 return NULL;
1619}
1620
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001621static char reduce_doc[] =
1622"reduce(function, sequence[, initial]) -> value\n\
1623\n\
1624Apply a function of two arguments cumulatively to the items of a sequence,\n\
1625from left to right, so as to reduce the sequence to a single value.\n\
1626For example, reduce(lambda x, y: x+y, [1, 2, 3, 4, 5]) calculates\n\
1627((((1+2)+3)+4)+5). If initial is present, it is placed before the items\n\
1628of the sequence in the calculation, and serves as a default when the\n\
1629sequence is empty.";
1630
1631
Guido van Rossum79f25d91997-04-29 20:08:16 +00001632static PyObject *
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001633builtin_reload(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001634 PyObject *self;
1635 PyObject *args;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001636{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001637 PyObject *v;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001638
Guido van Rossum79f25d91997-04-29 20:08:16 +00001639 if (!PyArg_ParseTuple(args, "O:reload", &v))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001640 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001641 return PyImport_ReloadModule(v);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001642}
1643
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001644static char reload_doc[] =
1645"reload(module) -> module\n\
1646\n\
1647Reload the module. The module must have been successfully imported before.";
1648
1649
Guido van Rossum79f25d91997-04-29 20:08:16 +00001650static PyObject *
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001651builtin_repr(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001652 PyObject *self;
1653 PyObject *args;
Guido van Rossumc89705d1992-11-26 08:54:07 +00001654{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001655 PyObject *v;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001656
Guido van Rossum79f25d91997-04-29 20:08:16 +00001657 if (!PyArg_ParseTuple(args, "O:repr", &v))
Guido van Rossumc89705d1992-11-26 08:54:07 +00001658 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001659 return PyObject_Repr(v);
Guido van Rossumc89705d1992-11-26 08:54:07 +00001660}
1661
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001662static char repr_doc[] =
1663"repr(object) -> string\n\
1664\n\
1665Return the canonical string representation of the object.\n\
1666For most object types, eval(repr(object)) == object.";
1667
1668
Guido van Rossum79f25d91997-04-29 20:08:16 +00001669static PyObject *
Guido van Rossum9e51f9b1993-02-12 16:29:05 +00001670builtin_round(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001671 PyObject *self;
1672 PyObject *args;
Guido van Rossum9e51f9b1993-02-12 16:29:05 +00001673{
Guido van Rossum9e51f9b1993-02-12 16:29:05 +00001674 double x;
1675 double f;
1676 int ndigits = 0;
Guido van Rossum9e51f9b1993-02-12 16:29:05 +00001677 int i;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001678
Guido van Rossum79f25d91997-04-29 20:08:16 +00001679 if (!PyArg_ParseTuple(args, "d|i:round", &x, &ndigits))
Guido van Rossum9e51f9b1993-02-12 16:29:05 +00001680 return NULL;
Guido van Rossum9e51f9b1993-02-12 16:29:05 +00001681 f = 1.0;
Guido van Rossum1e162d31998-05-09 14:42:25 +00001682 i = abs(ndigits);
1683 while (--i >= 0)
Guido van Rossum9e51f9b1993-02-12 16:29:05 +00001684 f = f*10.0;
Guido van Rossum1e162d31998-05-09 14:42:25 +00001685 if (ndigits < 0)
1686 x /= f;
Guido van Rossum9e51f9b1993-02-12 16:29:05 +00001687 else
Guido van Rossum1e162d31998-05-09 14:42:25 +00001688 x *= f;
1689 if (x >= 0.0)
1690 x = floor(x + 0.5);
1691 else
1692 x = ceil(x - 0.5);
1693 if (ndigits < 0)
1694 x *= f;
1695 else
1696 x /= f;
1697 return PyFloat_FromDouble(x);
Guido van Rossum9e51f9b1993-02-12 16:29:05 +00001698}
1699
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001700static char round_doc[] =
1701"round(number[, ndigits]) -> floating point number\n\
1702\n\
1703Round a number to a given precision in decimal digits (default 0 digits).\n\
1704This always returns a floating point number. Precision may be negative.";
1705
1706
Guido van Rossum79f25d91997-04-29 20:08:16 +00001707static PyObject *
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001708builtin_str(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001709 PyObject *self;
1710 PyObject *args;
Guido van Rossumc89705d1992-11-26 08:54:07 +00001711{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001712 PyObject *v;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001713
Guido van Rossum79f25d91997-04-29 20:08:16 +00001714 if (!PyArg_ParseTuple(args, "O:str", &v))
Guido van Rossumc89705d1992-11-26 08:54:07 +00001715 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001716 return PyObject_Str(v);
Guido van Rossumc89705d1992-11-26 08:54:07 +00001717}
1718
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001719static char str_doc[] =
1720"str(object) -> string\n\
1721\n\
1722Return a nice string representation of the object.\n\
1723If the argument is a string, the return value is the same object.";
1724
1725
Guido van Rossum79f25d91997-04-29 20:08:16 +00001726static PyObject *
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001727builtin_tuple(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001728 PyObject *self;
1729 PyObject *args;
Guido van Rossumcae027b1994-08-29 12:53:11 +00001730{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001731 PyObject *v;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001732
Guido van Rossum79f25d91997-04-29 20:08:16 +00001733 if (!PyArg_ParseTuple(args, "O:tuple", &v))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001734 return NULL;
Guido van Rossum09df08a1998-05-22 00:51:39 +00001735 return PySequence_Tuple(v);
Guido van Rossumcae027b1994-08-29 12:53:11 +00001736}
1737
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001738static char tuple_doc[] =
1739"tuple(sequence) -> list\n\
1740\n\
1741Return a tuple whose items are the same as those of the argument sequence.\n\
1742If the argument is a tuple, the return value is the same object.";
1743
1744
Guido van Rossum79f25d91997-04-29 20:08:16 +00001745static PyObject *
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001746builtin_type(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001747 PyObject *self;
1748 PyObject *args;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001749{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001750 PyObject *v;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001751
Guido van Rossum79f25d91997-04-29 20:08:16 +00001752 if (!PyArg_ParseTuple(args, "O:type", &v))
Guido van Rossum3f5da241990-12-20 15:06:42 +00001753 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001754 v = (PyObject *)v->ob_type;
1755 Py_INCREF(v);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001756 return v;
1757}
1758
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001759static char type_doc[] =
1760"type(object) -> type object\n\
1761\n\
1762Return the type of the object.";
1763
1764
Guido van Rossum79f25d91997-04-29 20:08:16 +00001765static PyObject *
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001766builtin_vars(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001767 PyObject *self;
1768 PyObject *args;
Guido van Rossum2d951851994-08-29 12:52:16 +00001769{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001770 PyObject *v = NULL;
1771 PyObject *d;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001772
Guido van Rossum79f25d91997-04-29 20:08:16 +00001773 if (!PyArg_ParseTuple(args, "|O:vars", &v))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001774 return NULL;
Guido van Rossum2d951851994-08-29 12:52:16 +00001775 if (v == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001776 d = PyEval_GetLocals();
Guido van Rossum53bb7ff1995-07-26 16:26:31 +00001777 if (d == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001778 if (!PyErr_Occurred())
1779 PyErr_SetString(PyExc_SystemError,
1780 "no locals!?");
Guido van Rossum53bb7ff1995-07-26 16:26:31 +00001781 }
1782 else
Guido van Rossum79f25d91997-04-29 20:08:16 +00001783 Py_INCREF(d);
Guido van Rossum2d951851994-08-29 12:52:16 +00001784 }
1785 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001786 d = PyObject_GetAttrString(v, "__dict__");
Guido van Rossum2d951851994-08-29 12:52:16 +00001787 if (d == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001788 PyErr_SetString(PyExc_TypeError,
Guido van Rossum2d951851994-08-29 12:52:16 +00001789 "vars() argument must have __dict__ attribute");
1790 return NULL;
1791 }
1792 }
1793 return d;
1794}
1795
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001796static char vars_doc[] =
1797"vars([object]) -> dictionary\n\
1798\n\
1799Without arguments, equivalent to locals().\n\
1800With an argument, equivalent to object.__dict__.";
1801
1802
Barry Warsawcde8b1b1997-08-22 21:14:38 +00001803static PyObject *
1804builtin_isinstance(self, args)
1805 PyObject *self;
1806 PyObject *args;
1807{
1808 PyObject *inst;
1809 PyObject *cls;
1810 int retval;
1811
1812 if (!PyArg_ParseTuple(args, "OO", &inst, &cls))
1813 return NULL;
Guido van Rossumf5dd9141997-12-02 19:11:45 +00001814 if (PyType_Check(cls)) {
Guido van Rossumd6af46d1997-12-10 05:51:47 +00001815 retval = ((PyObject *)(inst->ob_type) == cls);
Barry Warsawcde8b1b1997-08-22 21:14:38 +00001816 }
Barry Warsawcde8b1b1997-08-22 21:14:38 +00001817 else {
Guido van Rossumf5dd9141997-12-02 19:11:45 +00001818 if (!PyClass_Check(cls)) {
1819 PyErr_SetString(PyExc_TypeError,
1820 "second argument must be a class");
1821 return NULL;
1822 }
1823
1824 if (!PyInstance_Check(inst))
1825 retval = 0;
1826 else {
1827 PyObject *inclass =
1828 (PyObject*)((PyInstanceObject*)inst)->in_class;
1829 retval = PyClass_IsSubclass(inclass, cls);
1830 }
Barry Warsawcde8b1b1997-08-22 21:14:38 +00001831 }
1832 return PyInt_FromLong(retval);
1833}
1834
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001835static char isinstance_doc[] =
1836"isinstance(object, class-or-type) -> Boolean\n\
1837\n\
1838Return whether an object is an instance of a class or of a subclass thereof.\n\
1839With a type as second argument, return whether that is the object's type.";
1840
Barry Warsawcde8b1b1997-08-22 21:14:38 +00001841
1842static PyObject *
1843builtin_issubclass(self, args)
1844 PyObject *self;
1845 PyObject *args;
1846{
1847 PyObject *derived;
1848 PyObject *cls;
1849 int retval;
1850
1851 if (!PyArg_ParseTuple(args, "OO", &derived, &cls))
1852 return NULL;
1853 if (!PyClass_Check(derived) || !PyClass_Check(cls)) {
1854 PyErr_SetString(PyExc_TypeError, "arguments must be classes");
1855 return NULL;
1856 }
1857 /* shortcut */
1858 if (!(retval = (derived == cls)))
1859 retval = PyClass_IsSubclass(derived, cls);
1860
1861 return PyInt_FromLong(retval);
1862}
1863
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001864static char issubclass_doc[] =
1865"issubclass(C, B) -> Boolean\n\
1866\n\
1867Return whether class C is a subclass (i.e., a derived class) of class B.";
1868
Barry Warsawcde8b1b1997-08-22 21:14:38 +00001869
Guido van Rossum79f25d91997-04-29 20:08:16 +00001870static PyMethodDef builtin_methods[] = {
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001871 {"__import__", builtin___import__, 1, import_doc},
1872 {"abs", builtin_abs, 1, abs_doc},
1873 {"apply", builtin_apply, 1, apply_doc},
1874 {"callable", builtin_callable, 1, callable_doc},
1875 {"chr", builtin_chr, 1, chr_doc},
1876 {"cmp", builtin_cmp, 1, cmp_doc},
1877 {"coerce", builtin_coerce, 1, coerce_doc},
1878 {"compile", builtin_compile, 1, compile_doc},
Guido van Rossum8a5c5d21996-01-12 01:09:56 +00001879#ifndef WITHOUT_COMPLEX
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001880 {"complex", builtin_complex, 1, complex_doc},
Guido van Rossum8a5c5d21996-01-12 01:09:56 +00001881#endif
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001882 {"delattr", builtin_delattr, 1, delattr_doc},
1883 {"dir", builtin_dir, 1, dir_doc},
1884 {"divmod", builtin_divmod, 1, divmod_doc},
1885 {"eval", builtin_eval, 1, eval_doc},
1886 {"execfile", builtin_execfile, 1, execfile_doc},
1887 {"filter", builtin_filter, 1, filter_doc},
1888 {"float", builtin_float, 1, float_doc},
1889 {"getattr", builtin_getattr, 1, getattr_doc},
1890 {"globals", builtin_globals, 1, globals_doc},
1891 {"hasattr", builtin_hasattr, 1, hasattr_doc},
1892 {"hash", builtin_hash, 1, hash_doc},
1893 {"hex", builtin_hex, 1, hex_doc},
1894 {"id", builtin_id, 1, id_doc},
1895 {"input", builtin_input, 1, input_doc},
1896 {"intern", builtin_intern, 1, intern_doc},
1897 {"int", builtin_int, 1, int_doc},
1898 {"isinstance", builtin_isinstance, 1, isinstance_doc},
1899 {"issubclass", builtin_issubclass, 1, issubclass_doc},
1900 {"len", builtin_len, 1, len_doc},
1901 {"list", builtin_list, 1, list_doc},
1902 {"locals", builtin_locals, 1, locals_doc},
1903 {"long", builtin_long, 1, long_doc},
1904 {"map", builtin_map, 1, map_doc},
1905 {"max", builtin_max, 1, max_doc},
1906 {"min", builtin_min, 1, min_doc},
1907 {"oct", builtin_oct, 1, oct_doc},
1908 {"open", builtin_open, 1, open_doc},
1909 {"ord", builtin_ord, 1, ord_doc},
1910 {"pow", builtin_pow, 1, pow_doc},
1911 {"range", builtin_range, 1, range_doc},
1912 {"raw_input", builtin_raw_input, 1, raw_input_doc},
1913 {"reduce", builtin_reduce, 1, reduce_doc},
1914 {"reload", builtin_reload, 1, reload_doc},
1915 {"repr", builtin_repr, 1, repr_doc},
1916 {"round", builtin_round, 1, round_doc},
1917 {"setattr", builtin_setattr, 1, setattr_doc},
1918 {"slice", builtin_slice, 1, slice_doc},
1919 {"str", builtin_str, 1, str_doc},
1920 {"tuple", builtin_tuple, 1, tuple_doc},
1921 {"type", builtin_type, 1, type_doc},
1922 {"vars", builtin_vars, 1, vars_doc},
1923 {"xrange", builtin_xrange, 1, xrange_doc},
Guido van Rossumc02e15c1991-12-16 13:03:00 +00001924 {NULL, NULL},
Guido van Rossum3f5da241990-12-20 15:06:42 +00001925};
1926
Guido van Rossum3f5da241990-12-20 15:06:42 +00001927/* Predefined exceptions */
1928
Guido van Rossum04748321997-09-16 18:43:15 +00001929PyObject *PyExc_Exception;
Barry Warsaw757af0e1997-08-29 22:13:51 +00001930PyObject *PyExc_StandardError;
Barry Warsaw412cdc21997-09-16 21:51:14 +00001931PyObject *PyExc_ArithmeticError;
Barry Warsaw757af0e1997-08-29 22:13:51 +00001932PyObject *PyExc_LookupError;
1933
Guido van Rossum79f25d91997-04-29 20:08:16 +00001934PyObject *PyExc_AssertionError;
1935PyObject *PyExc_AttributeError;
1936PyObject *PyExc_EOFError;
Guido van Rossumb6a7f771997-05-09 03:03:23 +00001937PyObject *PyExc_FloatingPointError;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001938PyObject *PyExc_IOError;
1939PyObject *PyExc_ImportError;
1940PyObject *PyExc_IndexError;
1941PyObject *PyExc_KeyError;
1942PyObject *PyExc_KeyboardInterrupt;
1943PyObject *PyExc_MemoryError;
1944PyObject *PyExc_NameError;
1945PyObject *PyExc_OverflowError;
1946PyObject *PyExc_RuntimeError;
1947PyObject *PyExc_SyntaxError;
1948PyObject *PyExc_SystemError;
1949PyObject *PyExc_SystemExit;
1950PyObject *PyExc_TypeError;
1951PyObject *PyExc_ValueError;
1952PyObject *PyExc_ZeroDivisionError;
Guido van Rossum50afb7a1991-12-10 13:52:31 +00001953
Barry Warsaw757af0e1997-08-29 22:13:51 +00001954PyObject *PyExc_MemoryErrorInst;
1955
1956static struct
1957{
1958 char* name;
1959 PyObject** exc;
1960 int leaf_exc;
1961}
1962bltin_exc[] = {
Guido van Rossum04748321997-09-16 18:43:15 +00001963 {"Exception", &PyExc_Exception, 0},
Barry Warsaw757af0e1997-08-29 22:13:51 +00001964 {"StandardError", &PyExc_StandardError, 0},
Barry Warsaw412cdc21997-09-16 21:51:14 +00001965 {"ArithmeticError", &PyExc_ArithmeticError, 0},
Barry Warsaw757af0e1997-08-29 22:13:51 +00001966 {"LookupError", &PyExc_LookupError, 0},
1967 {"AssertionError", &PyExc_AssertionError, 1},
1968 {"AttributeError", &PyExc_AttributeError, 1},
1969 {"EOFError", &PyExc_EOFError, 1},
1970 {"FloatingPointError", &PyExc_FloatingPointError, 1},
1971 {"IOError", &PyExc_IOError, 1},
1972 {"ImportError", &PyExc_ImportError, 1},
1973 {"IndexError", &PyExc_IndexError, 1},
1974 {"KeyError", &PyExc_KeyError, 1},
1975 {"KeyboardInterrupt", &PyExc_KeyboardInterrupt, 1},
1976 {"MemoryError", &PyExc_MemoryError, 1},
1977 {"NameError", &PyExc_NameError, 1},
1978 {"OverflowError", &PyExc_OverflowError, 1},
1979 {"RuntimeError", &PyExc_RuntimeError, 1},
1980 {"SyntaxError", &PyExc_SyntaxError, 1},
1981 {"SystemError", &PyExc_SystemError, 1},
1982 {"SystemExit", &PyExc_SystemExit, 1},
1983 {"TypeError", &PyExc_TypeError, 1},
1984 {"ValueError", &PyExc_ValueError, 1},
1985 {"ZeroDivisionError", &PyExc_ZeroDivisionError, 1},
1986 {NULL, NULL}
1987};
1988
1989
1990/* import exceptions module to extract class exceptions */
1991static void
1992init_class_exc(dict)
1993 PyObject *dict;
1994{
1995 int i;
1996 PyObject *m = PyImport_ImportModule("exceptions");
1997 PyObject *d;
1998 PyObject *args;
1999
2000 if (m == NULL ||
2001 (d = PyModule_GetDict(m)) == NULL)
2002 {
Guido van Rossum09df08a1998-05-22 00:51:39 +00002003 /* XXX Should use PySys_WriteStderr here */
Barry Warsaw757af0e1997-08-29 22:13:51 +00002004 PyObject *f = PySys_GetObject("stderr");
2005 if (Py_VerboseFlag) {
2006 PyFile_WriteString(
2007 "'import exceptions' failed; traceback:\n", f);
2008 PyErr_Print();
2009 }
2010 else {
2011 PyFile_WriteString(
2012 "'import exceptions' failed; use -v for traceback\n", f);
2013 PyErr_Clear();
2014 }
2015 PyFile_WriteString("defaulting to old style exceptions\n", f);
2016 return;
2017 }
2018 for (i = 0; bltin_exc[i].name; i++) {
2019 /* dig the exception out of the module */
2020 PyObject *exc = PyDict_GetItemString(d, bltin_exc[i].name);
2021 if (!exc)
2022 Py_FatalError("built-in exception cannot be initialized");
2023
2024 Py_XDECREF(*bltin_exc[i].exc);
2025
2026 /* squirrel away a pointer to the exception */
2027 Py_INCREF(exc);
2028 *bltin_exc[i].exc = exc;
2029
2030 /* and insert the name in the __builtin__ module */
2031 PyDict_SetItemString(dict, bltin_exc[i].name, exc);
2032 }
2033
2034 /* we need one pre-allocated instance */
2035 args = Py_BuildValue("()");
2036 if (args) {
2037 PyExc_MemoryErrorInst =
2038 PyEval_CallObject(PyExc_MemoryError, args);
2039 Py_DECREF(args);
2040 }
2041
2042 /* we're done with the exceptions module */
2043 Py_DECREF(m);
2044
2045 if (PyErr_Occurred())
2046 Py_FatalError("can't instantiate standard exceptions");
2047}
2048
2049
2050static void
2051fini_instances()
2052{
2053 Py_XDECREF(PyExc_MemoryErrorInst);
2054 PyExc_MemoryErrorInst = NULL;
2055}
2056
2057
Guido van Rossum79f25d91997-04-29 20:08:16 +00002058static PyObject *
Guido van Rossum25ce5661997-08-02 03:10:38 +00002059newstdexception(dict, name)
2060 PyObject *dict;
Guido van Rossumfb905c31991-12-16 15:42:38 +00002061 char *name;
Guido van Rossum3f5da241990-12-20 15:06:42 +00002062{
Guido van Rossum79f25d91997-04-29 20:08:16 +00002063 PyObject *v = PyString_FromString(name);
Guido van Rossum25ce5661997-08-02 03:10:38 +00002064 if (v == NULL || PyDict_SetItemString(dict, name, v) != 0)
Guido van Rossum79f25d91997-04-29 20:08:16 +00002065 Py_FatalError("no mem for new standard exception");
Guido van Rossum3f5da241990-12-20 15:06:42 +00002066 return v;
2067}
2068
2069static void
Guido van Rossum25ce5661997-08-02 03:10:38 +00002070initerrors(dict)
2071 PyObject *dict;
Guido van Rossum3f5da241990-12-20 15:06:42 +00002072{
Barry Warsaw757af0e1997-08-29 22:13:51 +00002073 int i;
2074 int exccnt = 0;
2075 for (i = 0; bltin_exc[i].name; i++, exccnt++) {
2076 if (bltin_exc[i].leaf_exc)
2077 *bltin_exc[i].exc =
2078 newstdexception(dict, bltin_exc[i].name);
2079 }
2080
2081 /* This is kind of bogus because we special case the three new
2082 exceptions to be nearly forward compatible. But this means we
2083 hard code knowledge about exceptions.py into C here. I don't
2084 have a better solution, though
2085 */
2086 PyExc_LookupError = PyTuple_New(2);
2087 Py_INCREF(PyExc_IndexError);
2088 PyTuple_SET_ITEM(PyExc_LookupError, 0, PyExc_IndexError);
2089 Py_INCREF(PyExc_KeyError);
2090 PyTuple_SET_ITEM(PyExc_LookupError, 1, PyExc_KeyError);
2091 PyDict_SetItemString(dict, "LookupError", PyExc_LookupError);
2092
Barry Warsaw412cdc21997-09-16 21:51:14 +00002093 PyExc_ArithmeticError = PyTuple_New(3);
Barry Warsaw757af0e1997-08-29 22:13:51 +00002094 Py_INCREF(PyExc_OverflowError);
Barry Warsaw412cdc21997-09-16 21:51:14 +00002095 PyTuple_SET_ITEM(PyExc_ArithmeticError, 0, PyExc_OverflowError);
Barry Warsaw757af0e1997-08-29 22:13:51 +00002096 Py_INCREF(PyExc_ZeroDivisionError);
Barry Warsaw412cdc21997-09-16 21:51:14 +00002097 PyTuple_SET_ITEM(PyExc_ArithmeticError, 1, PyExc_ZeroDivisionError);
Barry Warsaw757af0e1997-08-29 22:13:51 +00002098 Py_INCREF(PyExc_FloatingPointError);
Barry Warsaw412cdc21997-09-16 21:51:14 +00002099 PyTuple_SET_ITEM(PyExc_ArithmeticError, 2, PyExc_FloatingPointError);
2100 PyDict_SetItemString(dict, "ArithmeticError", PyExc_ArithmeticError);
Barry Warsaw757af0e1997-08-29 22:13:51 +00002101
Barry Warsawb01a7fa1997-09-18 03:44:38 +00002102 PyExc_StandardError = PyTuple_New(exccnt-2);
2103 for (i = 2; bltin_exc[i].name; i++) {
Barry Warsaw757af0e1997-08-29 22:13:51 +00002104 PyObject *exc = *bltin_exc[i].exc;
2105 Py_INCREF(exc);
Barry Warsawb01a7fa1997-09-18 03:44:38 +00002106 PyTuple_SET_ITEM(PyExc_StandardError, i-2, exc);
Barry Warsaw757af0e1997-08-29 22:13:51 +00002107 }
2108 PyDict_SetItemString(dict, "StandardError", PyExc_StandardError);
Guido van Rossum04748321997-09-16 18:43:15 +00002109
2110 /* Exception is treated differently; for now, it's == StandardError */
2111 PyExc_Exception = PyExc_StandardError;
2112 Py_INCREF(PyExc_Exception);
2113 PyDict_SetItemString(dict, "Exception", PyExc_Exception);
Barry Warsaw757af0e1997-08-29 22:13:51 +00002114
2115 if (PyErr_Occurred())
2116 Py_FatalError("Could not initialize built-in string exceptions");
Guido van Rossum25ce5661997-08-02 03:10:38 +00002117}
2118
2119static void
2120finierrors()
2121{
Barry Warsaw757af0e1997-08-29 22:13:51 +00002122 int i;
2123 for (i = 0; bltin_exc[i].name; i++) {
2124 PyObject *exc = *bltin_exc[i].exc;
2125 Py_XDECREF(exc);
2126 *bltin_exc[i].exc = NULL;
2127 }
Guido van Rossum25ce5661997-08-02 03:10:38 +00002128}
2129
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002130static char builtin_doc[] =
2131"Built-in functions, exceptions, and other objects.\n\
2132\n\
2133Noteworthy: None is the `nil' object; Ellipsis represents `...' in slices.";
2134
Guido van Rossum25ce5661997-08-02 03:10:38 +00002135PyObject *
Barry Warsaw757af0e1997-08-29 22:13:51 +00002136_PyBuiltin_Init_1()
Guido van Rossum25ce5661997-08-02 03:10:38 +00002137{
2138 PyObject *mod, *dict;
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002139 mod = Py_InitModule4("__builtin__", builtin_methods,
2140 builtin_doc, (PyObject *)NULL,
2141 PYTHON_API_VERSION);
Guido van Rossum25ce5661997-08-02 03:10:38 +00002142 if (mod == NULL)
2143 return NULL;
2144 dict = PyModule_GetDict(mod);
2145 initerrors(dict);
2146 if (PyDict_SetItemString(dict, "None", Py_None) < 0)
2147 return NULL;
2148 if (PyDict_SetItemString(dict, "Ellipsis", Py_Ellipsis) < 0)
2149 return NULL;
2150 if (PyDict_SetItemString(dict, "__debug__",
2151 PyInt_FromLong(Py_OptimizeFlag == 0)) < 0)
2152 return NULL;
Barry Warsaw757af0e1997-08-29 22:13:51 +00002153
Guido van Rossum25ce5661997-08-02 03:10:38 +00002154 return mod;
Guido van Rossum3f5da241990-12-20 15:06:42 +00002155}
2156
2157void
Barry Warsaw757af0e1997-08-29 22:13:51 +00002158_PyBuiltin_Init_2(dict)
2159 PyObject *dict;
2160{
2161 /* if Python was started with -X, initialize the class exceptions */
2162 if (Py_UseClassExceptionsFlag)
2163 init_class_exc(dict);
2164}
2165
2166
2167void
2168_PyBuiltin_Fini_1()
2169{
2170 fini_instances();
2171}
2172
2173
2174void
2175_PyBuiltin_Fini_2()
Guido van Rossum3f5da241990-12-20 15:06:42 +00002176{
Guido van Rossum25ce5661997-08-02 03:10:38 +00002177 finierrors();
Guido van Rossum3f5da241990-12-20 15:06:42 +00002178}
Guido van Rossumc6bb8f71991-07-01 18:42:41 +00002179
Guido van Rossum12d12c51993-10-26 17:58:25 +00002180
Guido van Rossume77a7571993-11-03 15:01:26 +00002181/* Helper for filter(): filter a tuple through a function */
Guido van Rossum12d12c51993-10-26 17:58:25 +00002182
Guido van Rossum79f25d91997-04-29 20:08:16 +00002183static PyObject *
Guido van Rossum12d12c51993-10-26 17:58:25 +00002184filtertuple(func, tuple)
Guido van Rossum79f25d91997-04-29 20:08:16 +00002185 PyObject *func;
2186 PyObject *tuple;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002187{
Guido van Rossum79f25d91997-04-29 20:08:16 +00002188 PyObject *result;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002189 register int i, j;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002190 int len = PyTuple_Size(tuple);
Guido van Rossum12d12c51993-10-26 17:58:25 +00002191
Guido van Rossumb7b45621995-08-04 04:07:45 +00002192 if (len == 0) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002193 Py_INCREF(tuple);
Guido van Rossumb7b45621995-08-04 04:07:45 +00002194 return tuple;
2195 }
2196
Guido van Rossum79f25d91997-04-29 20:08:16 +00002197 if ((result = PyTuple_New(len)) == NULL)
Guido van Rossum2586bf01993-11-01 16:21:44 +00002198 return NULL;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002199
Guido van Rossum12d12c51993-10-26 17:58:25 +00002200 for (i = j = 0; i < len; ++i) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002201 PyObject *item, *good;
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002202 int ok;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002203
Guido van Rossum79f25d91997-04-29 20:08:16 +00002204 if ((item = PyTuple_GetItem(tuple, i)) == NULL)
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002205 goto Fail_1;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002206 if (func == Py_None) {
2207 Py_INCREF(item);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002208 good = item;
2209 }
2210 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002211 PyObject *arg = Py_BuildValue("(O)", item);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002212 if (arg == NULL)
2213 goto Fail_1;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002214 good = PyEval_CallObject(func, arg);
2215 Py_DECREF(arg);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002216 if (good == NULL)
Guido van Rossum12d12c51993-10-26 17:58:25 +00002217 goto Fail_1;
2218 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002219 ok = PyObject_IsTrue(good);
2220 Py_DECREF(good);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002221 if (ok) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002222 Py_INCREF(item);
2223 if (PyTuple_SetItem(result, j++, item) < 0)
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002224 goto Fail_1;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002225 }
Guido van Rossum12d12c51993-10-26 17:58:25 +00002226 }
2227
Guido van Rossum79f25d91997-04-29 20:08:16 +00002228 if (_PyTuple_Resize(&result, j, 0) < 0)
Guido van Rossum12d12c51993-10-26 17:58:25 +00002229 return NULL;
2230
Guido van Rossum12d12c51993-10-26 17:58:25 +00002231 return result;
2232
Guido van Rossum12d12c51993-10-26 17:58:25 +00002233Fail_1:
Guido van Rossum79f25d91997-04-29 20:08:16 +00002234 Py_DECREF(result);
Guido van Rossum12d12c51993-10-26 17:58:25 +00002235 return NULL;
2236}
2237
2238
Guido van Rossume77a7571993-11-03 15:01:26 +00002239/* Helper for filter(): filter a string through a function */
Guido van Rossum12d12c51993-10-26 17:58:25 +00002240
Guido van Rossum79f25d91997-04-29 20:08:16 +00002241static PyObject *
Guido van Rossum12d12c51993-10-26 17:58:25 +00002242filterstring(func, strobj)
Guido van Rossum79f25d91997-04-29 20:08:16 +00002243 PyObject *func;
2244 PyObject *strobj;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002245{
Guido van Rossum79f25d91997-04-29 20:08:16 +00002246 PyObject *result;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002247 register int i, j;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002248 int len = PyString_Size(strobj);
Guido van Rossum12d12c51993-10-26 17:58:25 +00002249
Guido van Rossum79f25d91997-04-29 20:08:16 +00002250 if (func == Py_None) {
Guido van Rossum2586bf01993-11-01 16:21:44 +00002251 /* No character is ever false -- share input string */
Guido van Rossum79f25d91997-04-29 20:08:16 +00002252 Py_INCREF(strobj);
Guido van Rossum2d951851994-08-29 12:52:16 +00002253 return strobj;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002254 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002255 if ((result = PyString_FromStringAndSize(NULL, len)) == NULL)
Guido van Rossum2586bf01993-11-01 16:21:44 +00002256 return NULL;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002257
Guido van Rossum12d12c51993-10-26 17:58:25 +00002258 for (i = j = 0; i < len; ++i) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002259 PyObject *item, *arg, *good;
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002260 int ok;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002261
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002262 item = (*strobj->ob_type->tp_as_sequence->sq_item)(strobj, i);
2263 if (item == NULL)
2264 goto Fail_1;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002265 arg = Py_BuildValue("(O)", item);
2266 Py_DECREF(item);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002267 if (arg == NULL)
2268 goto Fail_1;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002269 good = PyEval_CallObject(func, arg);
2270 Py_DECREF(arg);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002271 if (good == NULL)
2272 goto Fail_1;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002273 ok = PyObject_IsTrue(good);
2274 Py_DECREF(good);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002275 if (ok)
Guido van Rossum79f25d91997-04-29 20:08:16 +00002276 PyString_AS_STRING((PyStringObject *)result)[j++] =
2277 PyString_AS_STRING((PyStringObject *)item)[0];
Guido van Rossum12d12c51993-10-26 17:58:25 +00002278 }
2279
Guido van Rossum79f25d91997-04-29 20:08:16 +00002280 if (j < len && _PyString_Resize(&result, j) < 0)
Guido van Rossum12d12c51993-10-26 17:58:25 +00002281 return NULL;
2282
Guido van Rossum12d12c51993-10-26 17:58:25 +00002283 return result;
2284
Guido van Rossum12d12c51993-10-26 17:58:25 +00002285Fail_1:
Guido van Rossum79f25d91997-04-29 20:08:16 +00002286 Py_DECREF(result);
Guido van Rossum12d12c51993-10-26 17:58:25 +00002287 return NULL;
2288}