blob: 775c318becae69217d677efc68a9c5e1297ded33 [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) {
193 if (i < len)
194 goto Fail_1;
Barry Warsawcde8b1b1997-08-22 21:14:38 +0000195 if (PyErr_ExceptionMatches(PyExc_IndexError)) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000196 PyErr_Clear();
Guido van Rossum2d951851994-08-29 12:52:16 +0000197 break;
198 }
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000199 goto Fail_1;
Guido van Rossum2d951851994-08-29 12:52:16 +0000200 }
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000201
Guido van Rossum79f25d91997-04-29 20:08:16 +0000202 if (func == Py_None) {
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000203 good = item;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000204 Py_INCREF(good);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000205 }
206 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000207 PyObject *arg = Py_BuildValue("(O)", item);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000208 if (arg == NULL)
209 goto Fail_1;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000210 good = PyEval_CallObject(func, arg);
211 Py_DECREF(arg);
Guido van Rossum58b68731995-01-10 17:40:55 +0000212 if (good == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000213 Py_DECREF(item);
Guido van Rossum12d12c51993-10-26 17:58:25 +0000214 goto Fail_1;
Guido van Rossum58b68731995-01-10 17:40:55 +0000215 }
Guido van Rossum12d12c51993-10-26 17:58:25 +0000216 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000217 ok = PyObject_IsTrue(good);
218 Py_DECREF(good);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000219 if (ok) {
Guido van Rossum2d951851994-08-29 12:52:16 +0000220 if (j < len) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000221 if (PyList_SetItem(result, j++, item) < 0)
Guido van Rossum2d951851994-08-29 12:52:16 +0000222 goto Fail_1;
223 }
224 else {
225 j++;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000226 if (PyList_Append(result, item) < 0)
Guido van Rossum2d951851994-08-29 12:52:16 +0000227 goto Fail_1;
228 }
Guido van Rossum58b68731995-01-10 17:40:55 +0000229 } else {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000230 Py_DECREF(item);
Guido van Rossum12d12c51993-10-26 17:58:25 +0000231 }
Guido van Rossum12d12c51993-10-26 17:58:25 +0000232 }
233
Guido van Rossum12d12c51993-10-26 17:58:25 +0000234
Guido van Rossum79f25d91997-04-29 20:08:16 +0000235 if (j < len && PyList_SetSlice(result, j, len, NULL) < 0)
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000236 goto Fail_1;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000237
Guido van Rossum12d12c51993-10-26 17:58:25 +0000238 return result;
239
Guido van Rossum12d12c51993-10-26 17:58:25 +0000240Fail_1:
Guido van Rossum79f25d91997-04-29 20:08:16 +0000241 Py_DECREF(result);
Guido van Rossum12d12c51993-10-26 17:58:25 +0000242Fail_2:
Guido van Rossum12d12c51993-10-26 17:58:25 +0000243 return NULL;
244}
245
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000246static char filter_doc[] =
247"filter(function, sequence) -> list\n\
248\n\
249Return a list containing those items of sequence for which function(item)\n\
250is true. If function is None, return a list of items that are true.";
251
252
Guido van Rossum79f25d91997-04-29 20:08:16 +0000253static PyObject *
Guido van Rossum94390a41992-08-14 15:14:30 +0000254builtin_chr(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000255 PyObject *self;
256 PyObject *args;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000257{
258 long x;
259 char s[1];
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000260
Guido van Rossum79f25d91997-04-29 20:08:16 +0000261 if (!PyArg_ParseTuple(args, "l:chr", &x))
Guido van Rossum3f5da241990-12-20 15:06:42 +0000262 return NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000263 if (x < 0 || x >= 256) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000264 PyErr_SetString(PyExc_ValueError,
265 "chr() arg not in range(256)");
Guido van Rossum3f5da241990-12-20 15:06:42 +0000266 return NULL;
267 }
Guido van Rossum6bf62da1997-04-11 20:37:35 +0000268 s[0] = (char)x;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000269 return PyString_FromStringAndSize(s, 1);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000270}
271
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000272static char chr_doc[] =
273"chr(i) -> character\n\
274\n\
275Return a string of one character with ordinal i; 0 <= i < 256.";
276
277
Guido van Rossum79f25d91997-04-29 20:08:16 +0000278static PyObject *
Guido van Rossuma9e7dc11992-10-18 18:53:57 +0000279builtin_cmp(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000280 PyObject *self;
281 PyObject *args;
Guido van Rossuma9e7dc11992-10-18 18:53:57 +0000282{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000283 PyObject *a, *b;
Guido van Rossum09df08a1998-05-22 00:51:39 +0000284 int c;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000285
Guido van Rossum79f25d91997-04-29 20:08:16 +0000286 if (!PyArg_ParseTuple(args, "OO:cmp", &a, &b))
Guido van Rossuma9e7dc11992-10-18 18:53:57 +0000287 return NULL;
Guido van Rossum09df08a1998-05-22 00:51:39 +0000288 if (PyObject_Cmp(a, b, &c) < 0)
Guido van Rossumc8b6df91997-05-23 00:06:51 +0000289 return NULL;
Guido van Rossum09df08a1998-05-22 00:51:39 +0000290 return PyInt_FromLong((long)c);
Guido van Rossuma9e7dc11992-10-18 18:53:57 +0000291}
292
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000293static char cmp_doc[] =
294"cmp(x, y) -> integer\n\
295\n\
296Return negative if x<y, zero if x==y, positive if x>y.";
297
298
Guido van Rossum79f25d91997-04-29 20:08:16 +0000299static PyObject *
Guido van Rossum5524a591995-01-10 15:26:20 +0000300builtin_coerce(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000301 PyObject *self;
302 PyObject *args;
Guido van Rossum6a00cd81995-01-07 12:39:01 +0000303{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000304 PyObject *v, *w;
305 PyObject *res;
Guido van Rossum5524a591995-01-10 15:26:20 +0000306
Guido van Rossum79f25d91997-04-29 20:08:16 +0000307 if (!PyArg_ParseTuple(args, "OO:coerce", &v, &w))
Guido van Rossum5524a591995-01-10 15:26:20 +0000308 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000309 if (PyNumber_Coerce(&v, &w) < 0)
Guido van Rossum04691fc1992-08-12 15:35:34 +0000310 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000311 res = Py_BuildValue("(OO)", v, w);
312 Py_DECREF(v);
313 Py_DECREF(w);
Guido van Rossum04691fc1992-08-12 15:35:34 +0000314 return res;
315}
316
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000317static char coerce_doc[] =
318"coerce(x, y) -> None or (x1, y1)\n\
319\n\
320When x and y can be coerced to values of the same type, return a tuple\n\
321containing the coerced values. When they can't be coerced, return None.";
322
323
Guido van Rossum79f25d91997-04-29 20:08:16 +0000324static PyObject *
Guido van Rossum5b722181993-03-30 17:46:03 +0000325builtin_compile(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000326 PyObject *self;
327 PyObject *args;
Guido van Rossum5b722181993-03-30 17:46:03 +0000328{
329 char *str;
330 char *filename;
331 char *startstr;
332 int start;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000333
Guido van Rossum79f25d91997-04-29 20:08:16 +0000334 if (!PyArg_ParseTuple(args, "sss:compile", &str, &filename, &startstr))
Guido van Rossum5b722181993-03-30 17:46:03 +0000335 return NULL;
336 if (strcmp(startstr, "exec") == 0)
Guido van Rossumb05a5c71997-05-07 17:46:13 +0000337 start = Py_file_input;
Guido van Rossum5b722181993-03-30 17:46:03 +0000338 else if (strcmp(startstr, "eval") == 0)
Guido van Rossumb05a5c71997-05-07 17:46:13 +0000339 start = Py_eval_input;
Guido van Rossum872537c1995-07-07 22:43:42 +0000340 else if (strcmp(startstr, "single") == 0)
Guido van Rossumb05a5c71997-05-07 17:46:13 +0000341 start = Py_single_input;
Guido van Rossum5b722181993-03-30 17:46:03 +0000342 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000343 PyErr_SetString(PyExc_ValueError,
Guido van Rossum872537c1995-07-07 22:43:42 +0000344 "compile() mode must be 'exec' or 'eval' or 'single'");
Guido van Rossum5b722181993-03-30 17:46:03 +0000345 return NULL;
346 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000347 return Py_CompileString(str, filename, start);
Guido van Rossum5b722181993-03-30 17:46:03 +0000348}
349
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000350static char compile_doc[] =
351"compile(source, filename, mode) -> code object\n\
352\n\
353Compile the source string (a Python module, statement or expression)\n\
354into a code object that can be executed by the exec statement or eval().\n\
355The filename will be used for run-time error messages.\n\
356The mode must be 'exec' to compile a module, 'single' to compile a\n\
357single (interactive) statement, or 'eval' to compile an expression.";
358
359
Guido van Rossum8a5c5d21996-01-12 01:09:56 +0000360#ifndef WITHOUT_COMPLEX
361
Guido van Rossum79f25d91997-04-29 20:08:16 +0000362static PyObject *
Guido van Rossum8a5c5d21996-01-12 01:09:56 +0000363builtin_complex(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000364 PyObject *self;
365 PyObject *args;
Guido van Rossum8a5c5d21996-01-12 01:09:56 +0000366{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000367 PyObject *r, *i, *tmp;
368 PyNumberMethods *nbr, *nbi = NULL;
Guido van Rossum530956d1996-07-21 02:27:43 +0000369 Py_complex cr, ci;
Guido van Rossumc6472e91997-03-31 17:15:43 +0000370 int own_r = 0;
Guido van Rossum8a5c5d21996-01-12 01:09:56 +0000371
372 i = NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000373 if (!PyArg_ParseTuple(args, "O|O:complex", &r, &i))
Guido van Rossum8a5c5d21996-01-12 01:09:56 +0000374 return NULL;
375 if ((nbr = r->ob_type->tp_as_number) == NULL ||
Guido van Rossumc6472e91997-03-31 17:15:43 +0000376 nbr->nb_float == NULL ||
377 (i != NULL &&
378 ((nbi = i->ob_type->tp_as_number) == NULL ||
379 nbi->nb_float == NULL))) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000380 PyErr_SetString(PyExc_TypeError,
Guido van Rossum8a5c5d21996-01-12 01:09:56 +0000381 "complex() argument can't be converted to complex");
382 return NULL;
383 }
Guido van Rossumed0af8f1996-12-05 23:18:18 +0000384 /* XXX Hack to support classes with __complex__ method */
Guido van Rossum79f25d91997-04-29 20:08:16 +0000385 if (PyInstance_Check(r)) {
386 static PyObject *complexstr;
387 PyObject *f;
Guido van Rossumed0af8f1996-12-05 23:18:18 +0000388 if (complexstr == NULL) {
Guido van Rossum8d751611997-01-18 08:04:16 +0000389 complexstr = PyString_InternFromString("__complex__");
Guido van Rossumed0af8f1996-12-05 23:18:18 +0000390 if (complexstr == NULL)
391 return NULL;
392 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000393 f = PyObject_GetAttr(r, complexstr);
Guido van Rossumed0af8f1996-12-05 23:18:18 +0000394 if (f == NULL)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000395 PyErr_Clear();
Guido van Rossumed0af8f1996-12-05 23:18:18 +0000396 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000397 PyObject *args = Py_BuildValue("()");
Guido van Rossumed0af8f1996-12-05 23:18:18 +0000398 if (args == NULL)
399 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000400 r = PyEval_CallObject(f, args);
401 Py_DECREF(args);
Guido van Rossumed0af8f1996-12-05 23:18:18 +0000402 if (r == NULL)
403 return NULL;
Guido van Rossumc6472e91997-03-31 17:15:43 +0000404 own_r = 1;
Guido van Rossumed0af8f1996-12-05 23:18:18 +0000405 }
406 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000407 if (PyComplex_Check(r)) {
408 cr = ((PyComplexObject*)r)->cval;
Guido van Rossum730806d1998-04-10 22:27:42 +0000409 if (own_r) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000410 Py_DECREF(r);
Guido van Rossum730806d1998-04-10 22:27:42 +0000411 }
Guido van Rossumc6472e91997-03-31 17:15:43 +0000412 }
Guido van Rossum8a5c5d21996-01-12 01:09:56 +0000413 else {
Guido van Rossumfe4b6ee1996-08-08 18:49:41 +0000414 tmp = (*nbr->nb_float)(r);
Guido van Rossum730806d1998-04-10 22:27:42 +0000415 if (own_r) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000416 Py_DECREF(r);
Guido van Rossum730806d1998-04-10 22:27:42 +0000417 }
Guido van Rossumfe4b6ee1996-08-08 18:49:41 +0000418 if (tmp == NULL)
419 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000420 cr.real = PyFloat_AsDouble(tmp);
421 Py_DECREF(tmp);
Guido van Rossum8a5c5d21996-01-12 01:09:56 +0000422 cr.imag = 0.;
423 }
424 if (i == NULL) {
425 ci.real = 0.;
426 ci.imag = 0.;
427 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000428 else if (PyComplex_Check(i))
429 ci = ((PyComplexObject*)i)->cval;
Guido van Rossum8a5c5d21996-01-12 01:09:56 +0000430 else {
Guido van Rossumc6472e91997-03-31 17:15:43 +0000431 tmp = (*nbi->nb_float)(i);
Guido van Rossumfe4b6ee1996-08-08 18:49:41 +0000432 if (tmp == NULL)
433 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000434 ci.real = PyFloat_AsDouble(tmp);
435 Py_DECREF(tmp);
Guido van Rossum8a5c5d21996-01-12 01:09:56 +0000436 ci.imag = 0.;
437 }
438 cr.real -= ci.imag;
439 cr.imag += ci.real;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000440 return PyComplex_FromCComplex(cr);
Guido van Rossum8a5c5d21996-01-12 01:09:56 +0000441}
442
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000443static char complex_doc[] =
444"complex(real[, imag]) -> complex number\n\
445\n\
446Create a complex number from a real part and an optional imaginary part.\n\
447This is equivalent to (real + imag*1j) where imag defaults to 0.";
448
449
Guido van Rossum8a5c5d21996-01-12 01:09:56 +0000450#endif
451
Guido van Rossum79f25d91997-04-29 20:08:16 +0000452static PyObject *
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000453builtin_dir(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000454 PyObject *self;
455 PyObject *args;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000456{
Guido van Rossum666b17a1997-05-06 16:36:57 +0000457 static char *attrlist[] = {"__members__", "__methods__", NULL};
458 PyObject *v = NULL, *l = NULL, *m = NULL;
459 PyObject *d, *x;
460 int i;
461 char **s;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000462
Guido van Rossum79f25d91997-04-29 20:08:16 +0000463 if (!PyArg_ParseTuple(args, "|O:dir", &v))
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000464 return NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000465 if (v == NULL) {
Guido van Rossum666b17a1997-05-06 16:36:57 +0000466 x = PyEval_GetLocals();
467 if (x == NULL)
468 goto error;
469 l = PyMapping_Keys(x);
470 if (l == NULL)
471 goto error;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000472 }
473 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000474 d = PyObject_GetAttrString(v, "__dict__");
Guido van Rossum666b17a1997-05-06 16:36:57 +0000475 if (d == NULL)
Guido van Rossum795ba581996-05-23 22:49:07 +0000476 PyErr_Clear();
Guido van Rossum666b17a1997-05-06 16:36:57 +0000477 else {
478 l = PyMapping_Keys(d);
479 if (l == NULL)
480 PyErr_Clear();
481 Py_DECREF(d);
482 }
483 if (l == NULL) {
484 l = PyList_New(0);
485 if (l == NULL)
486 goto error;
487 }
488 for (s = attrlist; *s != NULL; s++) {
489 m = PyObject_GetAttrString(v, *s);
490 if (m == NULL) {
491 PyErr_Clear();
492 continue;
493 }
494 for (i = 0; ; i++) {
495 x = PySequence_GetItem(m, i);
496 if (x == NULL) {
497 PyErr_Clear();
498 break;
499 }
500 if (PyList_Append(l, x) != 0) {
501 Py_DECREF(x);
502 Py_DECREF(m);
503 goto error;
504 }
505 Py_DECREF(x);
506 }
507 Py_DECREF(m);
Guido van Rossum795ba581996-05-23 22:49:07 +0000508 }
Guido van Rossum006bcd41991-10-24 14:54:44 +0000509 }
Guido van Rossum666b17a1997-05-06 16:36:57 +0000510 if (PyList_Sort(l) != 0)
511 goto error;
512 return l;
513 error:
514 Py_XDECREF(l);
515 return NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000516}
517
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000518static char dir_doc[] =
519"dir([object]) -> list of strings\n\
520\n\
521Return an alphabetized list of names comprising (some of) the attributes\n\
522of the given object. Without an argument, the names in the current scope\n\
523are listed. With an instance argument, only the instance attributes are\n\
524returned. With a class argument, attributes of the base class are not\n\
525returned. For other types or arguments, this may list members or methods.";
526
527
Guido van Rossum79f25d91997-04-29 20:08:16 +0000528static PyObject *
Guido van Rossum6a00cd81995-01-07 12:39:01 +0000529builtin_divmod(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000530 PyObject *self;
531 PyObject *args;
Guido van Rossum6a00cd81995-01-07 12:39:01 +0000532{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000533 PyObject *v, *w;
Guido van Rossum6a00cd81995-01-07 12:39:01 +0000534
Guido van Rossum79f25d91997-04-29 20:08:16 +0000535 if (!PyArg_ParseTuple(args, "OO:divmod", &v, &w))
Guido van Rossum6a00cd81995-01-07 12:39:01 +0000536 return NULL;
Guido van Rossum09df08a1998-05-22 00:51:39 +0000537 return PyNumber_Divmod(v, w);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000538}
539
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000540static char divmod_doc[] =
541"divmod(x, y) -> (div, mod)\n\
542\n\
543Return the tuple ((x-x%y)/y, x%y). Invariant: div*y + mod == x.";
544
545
Guido van Rossum79f25d91997-04-29 20:08:16 +0000546static PyObject *
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000547builtin_eval(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000548 PyObject *self;
549 PyObject *args;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000550{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000551 PyObject *cmd;
552 PyObject *globals = Py_None, *locals = Py_None;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000553 char *str;
Guido van Rossum590baa41993-11-30 13:40:46 +0000554
Guido van Rossum79f25d91997-04-29 20:08:16 +0000555 if (!PyArg_ParseTuple(args, "O|O!O!:eval",
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000556 &cmd,
Guido van Rossum79f25d91997-04-29 20:08:16 +0000557 &PyDict_Type, &globals,
558 &PyDict_Type, &locals))
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000559 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000560 if (globals == Py_None) {
561 globals = PyEval_GetGlobals();
562 if (locals == Py_None)
563 locals = PyEval_GetLocals();
Guido van Rossum6135a871995-01-09 17:53:26 +0000564 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000565 else if (locals == Py_None)
Guido van Rossum6135a871995-01-09 17:53:26 +0000566 locals = globals;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000567 if (PyDict_GetItemString(globals, "__builtins__") == NULL) {
568 if (PyDict_SetItemString(globals, "__builtins__",
569 PyEval_GetBuiltins()) != 0)
Guido van Rossum6135a871995-01-09 17:53:26 +0000570 return NULL;
571 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000572 if (PyCode_Check(cmd))
573 return PyEval_EvalCode((PyCodeObject *) cmd, globals, locals);
574 if (!PyString_Check(cmd)) {
575 PyErr_SetString(PyExc_TypeError,
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000576 "eval() argument 1 must be string or code object");
Guido van Rossum94390a41992-08-14 15:14:30 +0000577 return NULL;
578 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000579 str = PyString_AsString(cmd);
580 if ((int)strlen(str) != PyString_Size(cmd)) {
581 PyErr_SetString(PyExc_ValueError,
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000582 "embedded '\\0' in string arg");
583 return NULL;
Guido van Rossumf08ab0a1992-03-04 16:41:41 +0000584 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000585 while (*str == ' ' || *str == '\t')
586 str++;
Guido van Rossumb05a5c71997-05-07 17:46:13 +0000587 return PyRun_String(str, Py_eval_input, globals, locals);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000588}
589
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000590static char eval_doc[] =
591"eval(source[, globals[, locals]]) -> value\n\
592\n\
593Evaluate the source in the context of globals and locals.\n\
594The source may be a string representing a Python expression\n\
595or a code object as returned by compile().\n\
596The globals and locals are dictionaries, defaulting to the current\n\
597globals and locals. If only globals is given, locals defaults to it.";
598
599
Guido van Rossum79f25d91997-04-29 20:08:16 +0000600static PyObject *
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000601builtin_execfile(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000602 PyObject *self;
603 PyObject *args;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000604{
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000605 char *filename;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000606 PyObject *globals = Py_None, *locals = Py_None;
607 PyObject *res;
Guido van Rossum0f61f8a1992-02-25 18:55:05 +0000608 FILE* fp;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000609
Guido van Rossum79f25d91997-04-29 20:08:16 +0000610 if (!PyArg_ParseTuple(args, "s|O!O!:execfile",
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000611 &filename,
Guido van Rossum79f25d91997-04-29 20:08:16 +0000612 &PyDict_Type, &globals,
613 &PyDict_Type, &locals))
Guido van Rossum0f61f8a1992-02-25 18:55:05 +0000614 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000615 if (globals == Py_None) {
616 globals = PyEval_GetGlobals();
617 if (locals == Py_None)
618 locals = PyEval_GetLocals();
Guido van Rossum6135a871995-01-09 17:53:26 +0000619 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000620 else if (locals == Py_None)
Guido van Rossum6135a871995-01-09 17:53:26 +0000621 locals = globals;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000622 if (PyDict_GetItemString(globals, "__builtins__") == NULL) {
623 if (PyDict_SetItemString(globals, "__builtins__",
624 PyEval_GetBuiltins()) != 0)
Guido van Rossum6135a871995-01-09 17:53:26 +0000625 return NULL;
626 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000627 Py_BEGIN_ALLOW_THREADS
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000628 fp = fopen(filename, "r");
Guido van Rossum79f25d91997-04-29 20:08:16 +0000629 Py_END_ALLOW_THREADS
Guido van Rossum0f61f8a1992-02-25 18:55:05 +0000630 if (fp == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000631 PyErr_SetFromErrno(PyExc_IOError);
Guido van Rossum0f61f8a1992-02-25 18:55:05 +0000632 return NULL;
633 }
Guido van Rossumb05a5c71997-05-07 17:46:13 +0000634 res = PyRun_File(fp, filename, Py_file_input, globals, locals);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000635 Py_BEGIN_ALLOW_THREADS
Guido van Rossum0f61f8a1992-02-25 18:55:05 +0000636 fclose(fp);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000637 Py_END_ALLOW_THREADS
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000638 return res;
Guido van Rossum0f61f8a1992-02-25 18:55:05 +0000639}
640
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000641static char execfile_doc[] =
642"execfile(filename[, globals[, locals]])\n\
643\n\
644Read and execute a Python script from a file.\n\
645The globals and locals are dictionaries, defaulting to the current\n\
646globals and locals. If only globals is given, locals defaults to it.";
647
648
Guido van Rossum79f25d91997-04-29 20:08:16 +0000649static PyObject *
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000650builtin_float(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000651 PyObject *self;
652 PyObject *args;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000653{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000654 PyObject *v;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000655
Guido van Rossum79f25d91997-04-29 20:08:16 +0000656 if (!PyArg_ParseTuple(args, "O:float", &v))
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000657 return NULL;
Guido van Rossum09df08a1998-05-22 00:51:39 +0000658 return PyNumber_Float(v);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000659}
660
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000661static char float_doc[] =
662"float(x) -> floating point number\n\
663\n\
664Convert a string or number to a floating point number, if possible.";
665
666
Guido van Rossum79f25d91997-04-29 20:08:16 +0000667static PyObject *
Guido van Rossum94390a41992-08-14 15:14:30 +0000668builtin_getattr(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000669 PyObject *self;
670 PyObject *args;
Guido van Rossum33894be1992-01-27 16:53:09 +0000671{
Guido van Rossum950ff291998-06-29 13:38:57 +0000672 PyObject *v, *result, *dflt = NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000673 PyObject *name;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000674
Guido van Rossum950ff291998-06-29 13:38:57 +0000675 if (!PyArg_ParseTuple(args, "OS|O:getattr", &v, &name, &dflt))
Guido van Rossum33894be1992-01-27 16:53:09 +0000676 return NULL;
Guido van Rossum950ff291998-06-29 13:38:57 +0000677 result = PyObject_GetAttr(v, name);
678 if (result == NULL && dflt != NULL) {
679 PyErr_Clear();
680 Py_INCREF(dflt);
681 result = dflt;
682 }
683 return result;
Guido van Rossum9bfef441993-03-29 10:43:31 +0000684}
685
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000686static char getattr_doc[] =
Guido van Rossum950ff291998-06-29 13:38:57 +0000687"getattr(object, name[, default]) -> value\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000688\n\
Guido van Rossum950ff291998-06-29 13:38:57 +0000689Get a named attribute from an object; getattr(x, 'y') is equivalent to x.y.\n\
690When a default argument is given, it is returned when the attribute doesn't\n\
691exist; without it, an exception is raised in that case.";
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000692
693
Guido van Rossum79f25d91997-04-29 20:08:16 +0000694static PyObject *
Guido van Rossum872537c1995-07-07 22:43:42 +0000695builtin_globals(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000696 PyObject *self;
697 PyObject *args;
Guido van Rossum872537c1995-07-07 22:43:42 +0000698{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000699 PyObject *d;
Guido van Rossum872537c1995-07-07 22:43:42 +0000700
Guido van Rossum79f25d91997-04-29 20:08:16 +0000701 if (!PyArg_ParseTuple(args, ""))
Guido van Rossum872537c1995-07-07 22:43:42 +0000702 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000703 d = PyEval_GetGlobals();
704 Py_INCREF(d);
Guido van Rossum872537c1995-07-07 22:43:42 +0000705 return d;
706}
707
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000708static char globals_doc[] =
709"globals() -> dictionary\n\
710\n\
711Return the dictionary containing the current scope's global variables.";
712
713
Guido van Rossum79f25d91997-04-29 20:08:16 +0000714static PyObject *
Guido van Rossum9bfef441993-03-29 10:43:31 +0000715builtin_hasattr(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000716 PyObject *self;
717 PyObject *args;
Guido van Rossum9bfef441993-03-29 10:43:31 +0000718{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000719 PyObject *v;
720 PyObject *name;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000721
Guido van Rossum79f25d91997-04-29 20:08:16 +0000722 if (!PyArg_ParseTuple(args, "OS:hasattr", &v, &name))
Guido van Rossum9bfef441993-03-29 10:43:31 +0000723 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000724 v = PyObject_GetAttr(v, name);
Guido van Rossum9bfef441993-03-29 10:43:31 +0000725 if (v == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000726 PyErr_Clear();
Guido van Rossum09df08a1998-05-22 00:51:39 +0000727 Py_INCREF(Py_False);
728 return Py_False;
Guido van Rossum9bfef441993-03-29 10:43:31 +0000729 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000730 Py_DECREF(v);
Guido van Rossum09df08a1998-05-22 00:51:39 +0000731 Py_INCREF(Py_True);
732 return Py_True;
Guido van Rossum33894be1992-01-27 16:53:09 +0000733}
734
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000735static char hasattr_doc[] =
736"hasattr(object, name) -> Boolean\n\
737\n\
738Return whether the object has an attribute with the given name.\n\
739(This is done by calling getattr(object, name) and catching exceptions.)";
740
741
Guido van Rossum79f25d91997-04-29 20:08:16 +0000742static PyObject *
Guido van Rossum5b722181993-03-30 17:46:03 +0000743builtin_id(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000744 PyObject *self;
745 PyObject *args;
Guido van Rossum5b722181993-03-30 17:46:03 +0000746{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000747 PyObject *v;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000748
Guido van Rossum79f25d91997-04-29 20:08:16 +0000749 if (!PyArg_ParseTuple(args, "O:id", &v))
Guido van Rossum5b722181993-03-30 17:46:03 +0000750 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000751 return PyInt_FromLong((long)v);
Guido van Rossum5b722181993-03-30 17:46:03 +0000752}
753
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000754static char id_doc[] =
755"id(object) -> integer\n\
756\n\
757Return the identity of an object. This is guaranteed to be unique among\n\
758simultaneously existing objects. (Hint: it's the object's memory address.)";
759
760
Guido van Rossum79f25d91997-04-29 20:08:16 +0000761static PyObject *
Guido van Rossum12d12c51993-10-26 17:58:25 +0000762builtin_map(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000763 PyObject *self;
764 PyObject *args;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000765{
766 typedef struct {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000767 PyObject *seq;
768 PySequenceMethods *sqf;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000769 int len;
770 } sequence;
771
Guido van Rossum79f25d91997-04-29 20:08:16 +0000772 PyObject *func, *result;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000773 sequence *seqs = NULL, *sqp;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000774 int n, len;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000775 register int i, j;
776
Guido van Rossum79f25d91997-04-29 20:08:16 +0000777 n = PyTuple_Size(args);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000778 if (n < 2) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000779 PyErr_SetString(PyExc_TypeError,
780 "map() requires at least two args");
Guido van Rossum12d12c51993-10-26 17:58:25 +0000781 return NULL;
782 }
783
Guido van Rossum79f25d91997-04-29 20:08:16 +0000784 func = PyTuple_GetItem(args, 0);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000785 n--;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000786
Guido van Rossum79f25d91997-04-29 20:08:16 +0000787 if ((seqs = PyMem_NEW(sequence, n)) == NULL) {
788 PyErr_NoMemory();
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000789 goto Fail_2;
790 }
Guido van Rossum12d12c51993-10-26 17:58:25 +0000791
Guido van Rossum2d951851994-08-29 12:52:16 +0000792 for (len = 0, i = 0, sqp = seqs; i < n; ++i, ++sqp) {
Guido van Rossum12d12c51993-10-26 17:58:25 +0000793 int curlen;
Guido van Rossum09df08a1998-05-22 00:51:39 +0000794 PySequenceMethods *sqf;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000795
Guido van Rossum79f25d91997-04-29 20:08:16 +0000796 if ((sqp->seq = PyTuple_GetItem(args, i + 1)) == NULL)
Guido van Rossum12d12c51993-10-26 17:58:25 +0000797 goto Fail_2;
798
Guido van Rossum09df08a1998-05-22 00:51:39 +0000799 sqp->sqf = sqf = sqp->seq->ob_type->tp_as_sequence;
800 if (sqf == NULL ||
801 sqf->sq_length == NULL ||
802 sqf->sq_item == NULL)
803 {
Guido van Rossum12d12c51993-10-26 17:58:25 +0000804 static char errmsg[] =
805 "argument %d to map() must be a sequence object";
Guido van Rossum15e33a41997-04-30 19:00:27 +0000806 char errbuf[sizeof(errmsg) + 25];
Guido van Rossum12d12c51993-10-26 17:58:25 +0000807
808 sprintf(errbuf, errmsg, i+2);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000809 PyErr_SetString(PyExc_TypeError, errbuf);
Guido van Rossum12d12c51993-10-26 17:58:25 +0000810 goto Fail_2;
811 }
812
813 if ((curlen = sqp->len = (*sqp->sqf->sq_length)(sqp->seq)) < 0)
814 goto Fail_2;
815
816 if (curlen > len)
817 len = curlen;
818 }
819
Guido van Rossum79f25d91997-04-29 20:08:16 +0000820 if ((result = (PyObject *) PyList_New(len)) == NULL)
Guido van Rossum12d12c51993-10-26 17:58:25 +0000821 goto Fail_2;
822
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000823 /* XXX Special case map(None, single_list) could be more efficient */
Guido van Rossum2d951851994-08-29 12:52:16 +0000824 for (i = 0; ; ++i) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000825 PyObject *alist, *item=NULL, *value;
Guido van Rossum2d951851994-08-29 12:52:16 +0000826 int any = 0;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000827
Guido van Rossum79f25d91997-04-29 20:08:16 +0000828 if (func == Py_None && n == 1)
Guido van Rossum32120311995-07-10 13:52:21 +0000829 alist = NULL;
Guido van Rossum2d951851994-08-29 12:52:16 +0000830 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000831 if ((alist = PyTuple_New(n)) == NULL)
Guido van Rossum2d951851994-08-29 12:52:16 +0000832 goto Fail_1;
833 }
Guido van Rossum12d12c51993-10-26 17:58:25 +0000834
835 for (j = 0, sqp = seqs; j < n; ++j, ++sqp) {
Guido van Rossum2d951851994-08-29 12:52:16 +0000836 if (sqp->len < 0) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000837 Py_INCREF(Py_None);
838 item = Py_None;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000839 }
Guido van Rossum12d12c51993-10-26 17:58:25 +0000840 else {
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000841 item = (*sqp->sqf->sq_item)(sqp->seq, i);
Guido van Rossum2d951851994-08-29 12:52:16 +0000842 if (item == NULL) {
843 if (i < sqp->len)
844 goto Fail_0;
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 Rossum79f25d91997-04-29 20:08:16 +0000900 PyMem_DEL(seqs);
Guido van Rossum12d12c51993-10-26 17:58:25 +0000901 return result;
902
Guido van Rossum12d12c51993-10-26 17:58:25 +0000903Fail_1:
Guido van Rossum79f25d91997-04-29 20:08:16 +0000904 Py_DECREF(result);
Guido van Rossum12d12c51993-10-26 17:58:25 +0000905Fail_2:
Guido van Rossum79f25d91997-04-29 20:08:16 +0000906 if (seqs) PyMem_DEL(seqs);
Guido van Rossum12d12c51993-10-26 17:58:25 +0000907 return NULL;
908}
909
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000910static char map_doc[] =
911"map(function, sequence[, sequence, ...]) -> list\n\
912\n\
913Return a list of the results of applying the function to the items of\n\
914the argument sequence(s). If more than one sequence is given, the\n\
915function is called with an argument list consisting of the corresponding\n\
916item of each sequence, substituting None for missing values when not all\n\
917sequences have the same length. If the function is None, return a list of\n\
918the items of the sequence (or a list of tuples if more than one sequence).";
919
920
Guido van Rossum79f25d91997-04-29 20:08:16 +0000921static PyObject *
Guido van Rossum94390a41992-08-14 15:14:30 +0000922builtin_setattr(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000923 PyObject *self;
924 PyObject *args;
Guido van Rossum33894be1992-01-27 16:53:09 +0000925{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000926 PyObject *v;
927 PyObject *name;
928 PyObject *value;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000929
Guido van Rossum79f25d91997-04-29 20:08:16 +0000930 if (!PyArg_ParseTuple(args, "OSO:setattr", &v, &name, &value))
Guido van Rossum33894be1992-01-27 16:53:09 +0000931 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000932 if (PyObject_SetAttr(v, name, value) != 0)
Guido van Rossum33894be1992-01-27 16:53:09 +0000933 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000934 Py_INCREF(Py_None);
935 return Py_None;
Guido van Rossum33894be1992-01-27 16:53:09 +0000936}
937
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000938static char setattr_doc[] =
939"setattr(object, name, value)\n\
940\n\
941Set a named attribute on an object; setattr(x, 'y', v) is equivalent to\n\
942``x.y = v''.";
943
944
Guido van Rossum79f25d91997-04-29 20:08:16 +0000945static PyObject *
Guido van Rossum14144fc1994-08-29 12:53:40 +0000946builtin_delattr(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000947 PyObject *self;
948 PyObject *args;
Guido van Rossum14144fc1994-08-29 12:53:40 +0000949{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000950 PyObject *v;
951 PyObject *name;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000952
Guido van Rossum79f25d91997-04-29 20:08:16 +0000953 if (!PyArg_ParseTuple(args, "OS:delattr", &v, &name))
Guido van Rossum14144fc1994-08-29 12:53:40 +0000954 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000955 if (PyObject_SetAttr(v, name, (PyObject *)NULL) != 0)
Guido van Rossum14144fc1994-08-29 12:53:40 +0000956 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000957 Py_INCREF(Py_None);
958 return Py_None;
Guido van Rossum14144fc1994-08-29 12:53:40 +0000959}
960
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000961static char delattr_doc[] =
962"setattr(object, name, value)\n\
963\n\
964Delete a named attribute on an object; delattr(x, 'y') is equivalent to\n\
965``del x.y''.";
966
967
Guido van Rossum79f25d91997-04-29 20:08:16 +0000968static PyObject *
Guido van Rossum9bfef441993-03-29 10:43:31 +0000969builtin_hash(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000970 PyObject *self;
971 PyObject *args;
Guido van Rossum9bfef441993-03-29 10:43:31 +0000972{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000973 PyObject *v;
Guido van Rossum9bfef441993-03-29 10:43:31 +0000974 long x;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000975
Guido van Rossum79f25d91997-04-29 20:08:16 +0000976 if (!PyArg_ParseTuple(args, "O:hash", &v))
Guido van Rossum9bfef441993-03-29 10:43:31 +0000977 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000978 x = PyObject_Hash(v);
Guido van Rossum9bfef441993-03-29 10:43:31 +0000979 if (x == -1)
980 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000981 return PyInt_FromLong(x);
Guido van Rossum9bfef441993-03-29 10:43:31 +0000982}
983
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000984static char hash_doc[] =
985"hash(object) -> integer\n\
986\n\
987Return a hash value for the object. Two objects with the same value have\n\
988the same hash value. The reverse is not necessarily true, but likely.";
989
990
Guido van Rossum79f25d91997-04-29 20:08:16 +0000991static PyObject *
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000992builtin_hex(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000993 PyObject *self;
994 PyObject *args;
Guido van Rossum006bcd41991-10-24 14:54:44 +0000995{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000996 PyObject *v;
997 PyNumberMethods *nb;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000998
Guido van Rossum79f25d91997-04-29 20:08:16 +0000999 if (!PyArg_ParseTuple(args, "O:hex", &v))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001000 return NULL;
Guido van Rossum1899c2e1992-09-12 11:09:23 +00001001
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001002 if ((nb = v->ob_type->tp_as_number) == NULL ||
Guido van Rossum1899c2e1992-09-12 11:09:23 +00001003 nb->nb_hex == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001004 PyErr_SetString(PyExc_TypeError,
Guido van Rossum1899c2e1992-09-12 11:09:23 +00001005 "hex() argument can't be converted to hex");
1006 return NULL;
Guido van Rossum006bcd41991-10-24 14:54:44 +00001007 }
Guido van Rossum1899c2e1992-09-12 11:09:23 +00001008 return (*nb->nb_hex)(v);
Guido van Rossum006bcd41991-10-24 14:54:44 +00001009}
1010
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001011static char hex_doc[] =
1012"hex(number) -> string\n\
1013\n\
1014Return the hexadecimal representation of an integer or long integer.";
1015
1016
Guido van Rossum79f25d91997-04-29 20:08:16 +00001017static PyObject *builtin_raw_input Py_PROTO((PyObject *, PyObject *));
Guido van Rossum3165fe61992-09-25 21:59:05 +00001018
Guido van Rossum79f25d91997-04-29 20:08:16 +00001019static PyObject *
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001020builtin_input(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001021 PyObject *self;
1022 PyObject *args;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001023{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001024 PyObject *line;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001025 char *str;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001026 PyObject *res;
1027 PyObject *globals, *locals;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001028
1029 line = builtin_raw_input(self, args);
Guido van Rossum3165fe61992-09-25 21:59:05 +00001030 if (line == NULL)
1031 return line;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001032 if (!PyArg_Parse(line, "s;embedded '\\0' in input line", &str))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001033 return NULL;
1034 while (*str == ' ' || *str == '\t')
1035 str++;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001036 globals = PyEval_GetGlobals();
1037 locals = PyEval_GetLocals();
1038 if (PyDict_GetItemString(globals, "__builtins__") == NULL) {
1039 if (PyDict_SetItemString(globals, "__builtins__",
1040 PyEval_GetBuiltins()) != 0)
Guido van Rossum6135a871995-01-09 17:53:26 +00001041 return NULL;
1042 }
Guido van Rossumb05a5c71997-05-07 17:46:13 +00001043 res = PyRun_String(str, Py_eval_input, globals, locals);
Guido van Rossum79f25d91997-04-29 20:08:16 +00001044 Py_DECREF(line);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001045 return res;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001046}
1047
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001048static char input_doc[] =
1049"input([prompt]) -> value\n\
1050\n\
1051Equivalent to eval(raw_input(prompt)).";
1052
1053
Guido van Rossume8811f81997-02-14 15:48:05 +00001054static PyObject *
1055builtin_intern(self, args)
1056 PyObject *self;
1057 PyObject *args;
1058{
1059 PyObject *s;
1060 if (!PyArg_ParseTuple(args, "S", &s))
1061 return NULL;
1062 Py_INCREF(s);
1063 PyString_InternInPlace(&s);
1064 return s;
1065}
1066
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001067static char intern_doc[] =
1068"intern(string) -> string\n\
1069\n\
1070``Intern'' the given string. This enters the string in the (global)\n\
1071table of interned strings whose purpose is to speed up dictionary lookups.\n\
1072Return the string itself or the previously interned string object with the\n\
1073same value.";
1074
1075
Guido van Rossum79f25d91997-04-29 20:08:16 +00001076static PyObject *
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001077builtin_int(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001078 PyObject *self;
1079 PyObject *args;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001080{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001081 PyObject *v;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001082
Guido van Rossum79f25d91997-04-29 20:08:16 +00001083 if (!PyArg_ParseTuple(args, "O:int", &v))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001084 return NULL;
Guido van Rossum09df08a1998-05-22 00:51:39 +00001085 return PyNumber_Int(v);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001086}
1087
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001088static char int_doc[] =
1089"int(x) -> integer\n\
1090\n\
1091Convert a string or number to an integer, if possible.\n\
1092A floating point argument will be truncated towards zero.";
1093
1094
Guido van Rossum79f25d91997-04-29 20:08:16 +00001095static PyObject *
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001096builtin_len(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001097 PyObject *self;
1098 PyObject *args;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001099{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001100 PyObject *v;
Guido van Rossum8ea9f4d1998-06-29 22:26:50 +00001101 long res;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001102
Guido van Rossum79f25d91997-04-29 20:08:16 +00001103 if (!PyArg_ParseTuple(args, "O:len", &v))
Guido van Rossum3f5da241990-12-20 15:06:42 +00001104 return NULL;
Guido van Rossum8ea9f4d1998-06-29 22:26:50 +00001105 res = PyObject_Length(v);
1106 if (res < 0 && PyErr_Occurred())
1107 return NULL;
1108 return PyInt_FromLong(res);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001109}
1110
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001111static char len_doc[] =
1112"len(object) -> integer\n\
1113\n\
1114Return the number of items of a sequence or mapping.";
1115
1116
Guido van Rossum79f25d91997-04-29 20:08:16 +00001117static PyObject *
Guido van Rossumd1705771996-04-09 02:41:06 +00001118builtin_list(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001119 PyObject *self;
1120 PyObject *args;
Guido van Rossumd1705771996-04-09 02:41:06 +00001121{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001122 PyObject *v;
Guido van Rossumd1705771996-04-09 02:41:06 +00001123
Guido van Rossum79f25d91997-04-29 20:08:16 +00001124 if (!PyArg_ParseTuple(args, "O:list", &v))
Guido van Rossumd1705771996-04-09 02:41:06 +00001125 return NULL;
Guido van Rossum09df08a1998-05-22 00:51:39 +00001126 return PySequence_List(v);
Guido van Rossumd1705771996-04-09 02:41:06 +00001127}
1128
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001129static char list_doc[] =
1130"list(sequence) -> list\n\
1131\n\
1132Return a new list whose items are the same as those of the argument sequence.";
1133
Guido van Rossum8861b741996-07-30 16:49:37 +00001134
1135static PyObject *
1136builtin_slice(self, args)
1137 PyObject *self;
1138 PyObject *args;
1139{
Guido van Rossum09df08a1998-05-22 00:51:39 +00001140 PyObject *start, *stop, *step;
Guido van Rossum8861b741996-07-30 16:49:37 +00001141
Guido van Rossum09df08a1998-05-22 00:51:39 +00001142 start = stop = step = NULL;
Guido van Rossum8861b741996-07-30 16:49:37 +00001143
Guido van Rossum09df08a1998-05-22 00:51:39 +00001144 if (!PyArg_ParseTuple(args, "O|OO:slice", &start, &stop, &step))
1145 return NULL;
Guido van Rossum8861b741996-07-30 16:49:37 +00001146
Guido van Rossum09df08a1998-05-22 00:51:39 +00001147 /* This swapping of stop and start is to maintain similarity with
1148 range(). */
1149 if (stop == NULL) {
1150 stop = start;
1151 start = NULL;
1152 }
1153 return PySlice_New(start, stop, step);
Guido van Rossum8861b741996-07-30 16:49:37 +00001154}
1155
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001156static char slice_doc[] =
1157"slice([start,] step[, stop]) -> slice object\n\
1158\n\
1159Create a slice object. This is used for slicing by the Numeric extensions.";
1160
1161
Guido van Rossum79f25d91997-04-29 20:08:16 +00001162static PyObject *
Guido van Rossum872537c1995-07-07 22:43:42 +00001163builtin_locals(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001164 PyObject *self;
1165 PyObject *args;
Guido van Rossum872537c1995-07-07 22:43:42 +00001166{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001167 PyObject *d;
Guido van Rossum872537c1995-07-07 22:43:42 +00001168
Guido van Rossum79f25d91997-04-29 20:08:16 +00001169 if (!PyArg_ParseTuple(args, ""))
Guido van Rossum872537c1995-07-07 22:43:42 +00001170 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001171 d = PyEval_GetLocals();
1172 Py_INCREF(d);
Guido van Rossum872537c1995-07-07 22:43:42 +00001173 return d;
1174}
1175
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001176static char locals_doc[] =
1177"locals() -> dictionary\n\
1178\n\
1179Return the dictionary containing the current scope's local variables.";
1180
1181
Guido van Rossum79f25d91997-04-29 20:08:16 +00001182static PyObject *
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001183builtin_long(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001184 PyObject *self;
1185 PyObject *args;
Guido van Rossumd4905451991-05-05 20:00:36 +00001186{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001187 PyObject *v;
Guido van Rossum1899c2e1992-09-12 11:09:23 +00001188
Guido van Rossum79f25d91997-04-29 20:08:16 +00001189 if (!PyArg_ParseTuple(args, "O:long", &v))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001190 return NULL;
Guido van Rossum09df08a1998-05-22 00:51:39 +00001191 return PyNumber_Long(v);
Guido van Rossumd4905451991-05-05 20:00:36 +00001192}
1193
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001194static char long_doc[] =
1195"long(x) -> long integer\n\
1196\n\
1197Convert a string or number to a long integer, if possible.\n\
1198A floating point argument will be truncated towards zero.";
1199
1200
Guido van Rossum79f25d91997-04-29 20:08:16 +00001201static PyObject *
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001202min_max(args, sign)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001203 PyObject *args;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001204 int sign;
1205{
Guido van Rossum2d951851994-08-29 12:52:16 +00001206 int i;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001207 PyObject *v, *w, *x;
1208 PySequenceMethods *sq;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001209
Guido van Rossum79f25d91997-04-29 20:08:16 +00001210 if (PyTuple_Size(args) > 1)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001211 v = args;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001212 else if (!PyArg_ParseTuple(args, "O:min/max", &v))
Guido van Rossum3f5da241990-12-20 15:06:42 +00001213 return NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001214 sq = v->ob_type->tp_as_sequence;
Guido van Rossum09df08a1998-05-22 00:51:39 +00001215 if (sq == NULL || sq->sq_item == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001216 PyErr_SetString(PyExc_TypeError,
1217 "min() or max() of non-sequence");
Guido van Rossum3f5da241990-12-20 15:06:42 +00001218 return NULL;
1219 }
Guido van Rossum2d951851994-08-29 12:52:16 +00001220 w = NULL;
1221 for (i = 0; ; i++) {
Guido van Rossum3f5da241990-12-20 15:06:42 +00001222 x = (*sq->sq_item)(v, i); /* Implies INCREF */
Guido van Rossum2d951851994-08-29 12:52:16 +00001223 if (x == NULL) {
Barry Warsawcde8b1b1997-08-22 21:14:38 +00001224 if (PyErr_ExceptionMatches(PyExc_IndexError)) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001225 PyErr_Clear();
Guido van Rossum2d951851994-08-29 12:52:16 +00001226 break;
1227 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00001228 Py_XDECREF(w);
Guido van Rossum2d951851994-08-29 12:52:16 +00001229 return NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001230 }
Guido van Rossum2d951851994-08-29 12:52:16 +00001231 if (w == NULL)
1232 w = x;
1233 else {
Guido van Rossumc8b6df91997-05-23 00:06:51 +00001234 int c = PyObject_Compare(x, w);
1235 if (c && PyErr_Occurred()) {
1236 Py_DECREF(x);
1237 Py_XDECREF(w);
1238 return NULL;
1239 }
1240 if (c * sign > 0) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001241 Py_DECREF(w);
Guido van Rossum2d951851994-08-29 12:52:16 +00001242 w = x;
1243 }
1244 else
Guido van Rossum79f25d91997-04-29 20:08:16 +00001245 Py_DECREF(x);
Guido van Rossum2d951851994-08-29 12:52:16 +00001246 }
Guido van Rossum3f5da241990-12-20 15:06:42 +00001247 }
Guido van Rossum2d951851994-08-29 12:52:16 +00001248 if (w == NULL)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001249 PyErr_SetString(PyExc_ValueError,
1250 "min() or max() of empty sequence");
Guido van Rossum3f5da241990-12-20 15:06:42 +00001251 return w;
1252}
1253
Guido van Rossum79f25d91997-04-29 20:08:16 +00001254static PyObject *
Guido van Rossum3f5da241990-12-20 15:06:42 +00001255builtin_min(self, v)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001256 PyObject *self;
1257 PyObject *v;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001258{
1259 return min_max(v, -1);
1260}
1261
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001262static char min_doc[] =
1263"min(sequence) -> value\n\
1264min(a, b, c, ...) -> value\n\
1265\n\
1266With a single sequence argument, return its smallest item.\n\
1267With two or more arguments, return the smallest argument.";
1268
1269
Guido van Rossum79f25d91997-04-29 20:08:16 +00001270static PyObject *
Guido van Rossum3f5da241990-12-20 15:06:42 +00001271builtin_max(self, v)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001272 PyObject *self;
1273 PyObject *v;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001274{
1275 return min_max(v, 1);
1276}
1277
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001278static char max_doc[] =
1279"max(sequence) -> value\n\
1280max(a, b, c, ...) -> value\n\
1281\n\
1282With a single sequence argument, return its largest item.\n\
1283With two or more arguments, return the largest argument.";
1284
1285
Guido van Rossum79f25d91997-04-29 20:08:16 +00001286static PyObject *
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001287builtin_oct(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001288 PyObject *self;
1289 PyObject *args;
Guido van Rossum006bcd41991-10-24 14:54:44 +00001290{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001291 PyObject *v;
1292 PyNumberMethods *nb;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001293
Guido van Rossum79f25d91997-04-29 20:08:16 +00001294 if (!PyArg_ParseTuple(args, "O:oct", &v))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001295 return NULL;
Guido van Rossum1899c2e1992-09-12 11:09:23 +00001296 if (v == NULL || (nb = v->ob_type->tp_as_number) == NULL ||
1297 nb->nb_oct == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001298 PyErr_SetString(PyExc_TypeError,
Guido van Rossum1899c2e1992-09-12 11:09:23 +00001299 "oct() argument can't be converted to oct");
1300 return NULL;
Guido van Rossum006bcd41991-10-24 14:54:44 +00001301 }
Guido van Rossum1899c2e1992-09-12 11:09:23 +00001302 return (*nb->nb_oct)(v);
Guido van Rossum006bcd41991-10-24 14:54:44 +00001303}
1304
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001305static char oct_doc[] =
1306"oct(number) -> string\n\
1307\n\
1308Return the octal representation of an integer or long integer.";
1309
1310
Guido van Rossum79f25d91997-04-29 20:08:16 +00001311static PyObject *
Guido van Rossum94390a41992-08-14 15:14:30 +00001312builtin_open(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001313 PyObject *self;
1314 PyObject *args;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001315{
Guido van Rossum2d951851994-08-29 12:52:16 +00001316 char *name;
1317 char *mode = "r";
1318 int bufsize = -1;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001319 PyObject *f;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001320
Guido van Rossum79f25d91997-04-29 20:08:16 +00001321 if (!PyArg_ParseTuple(args, "s|si:open", &name, &mode, &bufsize))
Guido van Rossum3f5da241990-12-20 15:06:42 +00001322 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001323 f = PyFile_FromString(name, mode);
Guido van Rossum2d951851994-08-29 12:52:16 +00001324 if (f != NULL)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001325 PyFile_SetBufSize(f, bufsize);
Guido van Rossum2d951851994-08-29 12:52:16 +00001326 return f;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001327}
1328
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001329static char open_doc[] =
1330"open(filename[, mode[, buffering]]) -> file object\n\
1331\n\
1332Open a file. The mode can be 'r', 'w' or 'a' for reading (default),\n\
1333writing or appending. The file will be created if it doesn't exist\n\
1334when opened for writing or appending; it will be truncated when\n\
1335opened for writing. Add a 'b' to the mode for binary files.\n\
1336Add a '+' to the mode to allow simultaneous reading and writing.\n\
1337If the buffering argument is given, 0 means unbuffered, 1 means line\n\
1338buffered, and larger numbers specify the buffer size.";
1339
1340
Guido van Rossum79f25d91997-04-29 20:08:16 +00001341static PyObject *
Guido van Rossum94390a41992-08-14 15:14:30 +00001342builtin_ord(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001343 PyObject *self;
1344 PyObject *args;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001345{
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001346 char c;
1347
Guido van Rossum79f25d91997-04-29 20:08:16 +00001348 if (!PyArg_ParseTuple(args, "c:ord", &c))
Guido van Rossum3f5da241990-12-20 15:06:42 +00001349 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001350 return PyInt_FromLong((long)(c & 0xff));
Guido van Rossum3f5da241990-12-20 15:06:42 +00001351}
1352
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001353static char ord_doc[] =
1354"ord(c) -> integer\n\
1355\n\
1356Return the integer ordinal of a one character string.";
1357
1358
Guido van Rossum79f25d91997-04-29 20:08:16 +00001359static PyObject *
Guido van Rossum6a00cd81995-01-07 12:39:01 +00001360builtin_pow(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001361 PyObject *self;
1362 PyObject *args;
Guido van Rossum6a00cd81995-01-07 12:39:01 +00001363{
Guido van Rossum09df08a1998-05-22 00:51:39 +00001364 PyObject *v, *w, *z = Py_None;
Guido van Rossum6a00cd81995-01-07 12:39:01 +00001365
Guido van Rossum79f25d91997-04-29 20:08:16 +00001366 if (!PyArg_ParseTuple(args, "OO|O:pow", &v, &w, &z))
Guido van Rossum6a00cd81995-01-07 12:39:01 +00001367 return NULL;
Guido van Rossum09df08a1998-05-22 00:51:39 +00001368 return PyNumber_Power(v, w, z);
Guido van Rossumd4905451991-05-05 20:00:36 +00001369}
1370
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001371static char pow_doc[] =
1372"pow(x, y[, z]) -> number\n\
1373\n\
1374With two arguments, equivalent to x**y. With three arguments,\n\
1375equivalent to (x**y) % z, but may be more efficient (e.g. for longs).";
1376
1377
Guido van Rossum79f25d91997-04-29 20:08:16 +00001378static PyObject *
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001379builtin_range(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001380 PyObject *self;
1381 PyObject *args;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001382{
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001383 long ilow = 0, ihigh = 0, istep = 1;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001384 int i, n;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001385 PyObject *v;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001386
Guido van Rossum79f25d91997-04-29 20:08:16 +00001387 if (PyTuple_Size(args) <= 1) {
1388 if (!PyArg_ParseTuple(args,
Guido van Rossum0865dd91995-01-17 16:30:22 +00001389 "l;range() requires 1-3 int arguments",
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001390 &ihigh))
1391 return NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001392 }
1393 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001394 if (!PyArg_ParseTuple(args,
Guido van Rossum0865dd91995-01-17 16:30:22 +00001395 "ll|l;range() requires 1-3 int arguments",
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001396 &ilow, &ihigh, &istep))
Guido van Rossum3f5da241990-12-20 15:06:42 +00001397 return NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001398 }
1399 if (istep == 0) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001400 PyErr_SetString(PyExc_ValueError, "zero step for range()");
Guido van Rossum3f5da241990-12-20 15:06:42 +00001401 return NULL;
1402 }
1403 /* XXX ought to check overflow of subtraction */
1404 if (istep > 0)
1405 n = (ihigh - ilow + istep - 1) / istep;
1406 else
1407 n = (ihigh - ilow + istep + 1) / istep;
1408 if (n < 0)
1409 n = 0;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001410 v = PyList_New(n);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001411 if (v == NULL)
1412 return NULL;
1413 for (i = 0; i < n; i++) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001414 PyObject *w = PyInt_FromLong(ilow);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001415 if (w == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001416 Py_DECREF(v);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001417 return NULL;
1418 }
Guido van Rossuma937d141998-04-24 18:22:02 +00001419 PyList_SET_ITEM(v, i, w);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001420 ilow += istep;
1421 }
1422 return v;
1423}
1424
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001425static char range_doc[] =
1426"range([start,] stop[, step]) -> list of integers\n\
1427\n\
1428Return a list containing an arithmetic progression of integers.\n\
1429range(i, j) returns [i, i+1, i+2, ..., j-1]; start (!) defaults to 0.\n\
1430When step is given, it specifies the increment (or decrement).\n\
1431For example, range(4) returns [0, 1, 2, 3]. The end point is omitted!\n\
1432These are exactly the valid indices for a list of 4 elements.";
1433
1434
Guido van Rossum79f25d91997-04-29 20:08:16 +00001435static PyObject *
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001436builtin_xrange(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001437 PyObject *self;
1438 PyObject *args;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001439{
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001440 long ilow = 0, ihigh = 0, istep = 1;
Guido van Rossum0865dd91995-01-17 16:30:22 +00001441 long n;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001442
Guido van Rossum79f25d91997-04-29 20:08:16 +00001443 if (PyTuple_Size(args) <= 1) {
1444 if (!PyArg_ParseTuple(args,
Guido van Rossum0865dd91995-01-17 16:30:22 +00001445 "l;xrange() requires 1-3 int arguments",
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001446 &ihigh))
1447 return NULL;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001448 }
1449 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001450 if (!PyArg_ParseTuple(args,
Guido van Rossum0865dd91995-01-17 16:30:22 +00001451 "ll|l;xrange() requires 1-3 int arguments",
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001452 &ilow, &ihigh, &istep))
Guido van Rossum12d12c51993-10-26 17:58:25 +00001453 return NULL;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001454 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001455 if (istep == 0) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001456 PyErr_SetString(PyExc_ValueError, "zero step for xrange()");
Guido van Rossum12d12c51993-10-26 17:58:25 +00001457 return NULL;
1458 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001459 /* XXX ought to check overflow of subtraction */
1460 if (istep > 0)
1461 n = (ihigh - ilow + istep - 1) / istep;
1462 else
1463 n = (ihigh - ilow + istep + 1) / istep;
1464 if (n < 0)
1465 n = 0;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001466 return PyRange_New(ilow, n, istep, 1);
Guido van Rossum12d12c51993-10-26 17:58:25 +00001467}
1468
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001469static char xrange_doc[] =
1470"xrange([start,] stop[, step]) -> xrange object\n\
1471\n\
1472Like range(), but instead of returning a list, returns an object that\n\
1473generates the numbers in the range on demand. This is slightly slower\n\
1474than range() but more memory efficient.";
1475
1476
Guido van Rossum79f25d91997-04-29 20:08:16 +00001477extern char *PyOS_Readline Py_PROTO((char *));
Guido van Rossum872537c1995-07-07 22:43:42 +00001478
Guido van Rossum79f25d91997-04-29 20:08:16 +00001479static PyObject *
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001480builtin_raw_input(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001481 PyObject *self;
1482 PyObject *args;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001483{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001484 PyObject *v = NULL;
1485 PyObject *f;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001486
Guido van Rossum79f25d91997-04-29 20:08:16 +00001487 if (!PyArg_ParseTuple(args, "|O:[raw_]input", &v))
Guido van Rossum3165fe61992-09-25 21:59:05 +00001488 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001489 if (PyFile_AsFile(PySys_GetObject("stdin")) == stdin &&
1490 PyFile_AsFile(PySys_GetObject("stdout")) == stdout &&
Guido van Rossum53bb7ff1995-07-26 16:26:31 +00001491 isatty(fileno(stdin)) && isatty(fileno(stdout))) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001492 PyObject *po;
Guido van Rossum872537c1995-07-07 22:43:42 +00001493 char *prompt;
1494 char *s;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001495 PyObject *result;
Guido van Rossum872537c1995-07-07 22:43:42 +00001496 if (v != NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001497 po = PyObject_Str(v);
Guido van Rossum872537c1995-07-07 22:43:42 +00001498 if (po == NULL)
1499 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001500 prompt = PyString_AsString(po);
Guido van Rossumd9b52081998-06-26 18:25:38 +00001501 if (prompt == NULL)
1502 return NULL;
Guido van Rossum872537c1995-07-07 22:43:42 +00001503 }
1504 else {
1505 po = NULL;
1506 prompt = "";
1507 }
Guido van Rossumee81af81997-09-26 21:47:43 +00001508 Py_BEGIN_ALLOW_THREADS
Guido van Rossum79f25d91997-04-29 20:08:16 +00001509 s = PyOS_Readline(prompt);
Guido van Rossumee81af81997-09-26 21:47:43 +00001510 Py_END_ALLOW_THREADS
Guido van Rossum79f25d91997-04-29 20:08:16 +00001511 Py_XDECREF(po);
Guido van Rossum872537c1995-07-07 22:43:42 +00001512 if (s == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001513 PyErr_SetNone(PyExc_KeyboardInterrupt);
Guido van Rossum872537c1995-07-07 22:43:42 +00001514 return NULL;
1515 }
1516 if (*s == '\0') {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001517 PyErr_SetNone(PyExc_EOFError);
Guido van Rossum872537c1995-07-07 22:43:42 +00001518 result = NULL;
1519 }
1520 else { /* strip trailing '\n' */
Guido van Rossum79f25d91997-04-29 20:08:16 +00001521 result = PyString_FromStringAndSize(s, strlen(s)-1);
Guido van Rossum872537c1995-07-07 22:43:42 +00001522 }
1523 free(s);
1524 return result;
1525 }
Guido van Rossum90933611991-06-07 16:10:43 +00001526 if (v != NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001527 f = PySys_GetObject("stdout");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001528 if (f == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001529 PyErr_SetString(PyExc_RuntimeError, "lost sys.stdout");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001530 return NULL;
1531 }
Guido van Rossumc8b6df91997-05-23 00:06:51 +00001532 if (Py_FlushLine() != 0 ||
1533 PyFile_WriteObject(v, f, Py_PRINT_RAW) != 0)
Guido van Rossum90933611991-06-07 16:10:43 +00001534 return NULL;
1535 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00001536 f = PySys_GetObject("stdin");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001537 if (f == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001538 PyErr_SetString(PyExc_RuntimeError, "lost sys.stdin");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001539 return NULL;
1540 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00001541 return PyFile_GetLine(f, -1);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001542}
1543
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001544static char raw_input_doc[] =
1545"raw_input([prompt]) -> string\n\
1546\n\
1547Read a string from standard input. The trailing newline is stripped.\n\
1548If the user hits EOF (Unix: Ctl-D, Windows: Ctl-Z+Return), raise EOFError.\n\
1549On Unix, GNU readline is used if enabled. The prompt string, if given,\n\
1550is printed without a trailing newline before reading.";
1551
1552
Guido van Rossum79f25d91997-04-29 20:08:16 +00001553static PyObject *
Guido van Rossum12d12c51993-10-26 17:58:25 +00001554builtin_reduce(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001555 PyObject *self;
1556 PyObject *args;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001557{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001558 PyObject *seq, *func, *result = NULL;
1559 PySequenceMethods *sqf;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001560 register int i;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001561
Guido van Rossum79f25d91997-04-29 20:08:16 +00001562 if (!PyArg_ParseTuple(args, "OO|O:reduce", &func, &seq, &result))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001563 return NULL;
1564 if (result != NULL)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001565 Py_INCREF(result);
Guido van Rossum12d12c51993-10-26 17:58:25 +00001566
Guido van Rossum09df08a1998-05-22 00:51:39 +00001567 sqf = seq->ob_type->tp_as_sequence;
1568 if (sqf == NULL || sqf->sq_item == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001569 PyErr_SetString(PyExc_TypeError,
Guido van Rossum12d12c51993-10-26 17:58:25 +00001570 "2nd argument to reduce() must be a sequence object");
1571 return NULL;
1572 }
1573
Guido van Rossum79f25d91997-04-29 20:08:16 +00001574 if ((args = PyTuple_New(2)) == NULL)
Guido van Rossum2d951851994-08-29 12:52:16 +00001575 goto Fail;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001576
Guido van Rossum2d951851994-08-29 12:52:16 +00001577 for (i = 0; ; ++i) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001578 PyObject *op2;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001579
1580 if (args->ob_refcnt > 1) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001581 Py_DECREF(args);
1582 if ((args = PyTuple_New(2)) == NULL)
Guido van Rossum2d951851994-08-29 12:52:16 +00001583 goto Fail;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001584 }
1585
Guido van Rossum2d951851994-08-29 12:52:16 +00001586 if ((op2 = (*sqf->sq_item)(seq, i)) == NULL) {
Barry Warsawcde8b1b1997-08-22 21:14:38 +00001587 if (PyErr_ExceptionMatches(PyExc_IndexError)) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001588 PyErr_Clear();
Guido van Rossum2d951851994-08-29 12:52:16 +00001589 break;
1590 }
1591 goto Fail;
1592 }
Guido van Rossum12d12c51993-10-26 17:58:25 +00001593
Guido van Rossum2d951851994-08-29 12:52:16 +00001594 if (result == NULL)
1595 result = op2;
1596 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001597 PyTuple_SetItem(args, 0, result);
1598 PyTuple_SetItem(args, 1, op2);
1599 if ((result = PyEval_CallObject(func, args)) == NULL)
Guido van Rossum2d951851994-08-29 12:52:16 +00001600 goto Fail;
1601 }
Guido van Rossum12d12c51993-10-26 17:58:25 +00001602 }
1603
Guido van Rossum79f25d91997-04-29 20:08:16 +00001604 Py_DECREF(args);
Guido van Rossum12d12c51993-10-26 17:58:25 +00001605
Guido van Rossum2d951851994-08-29 12:52:16 +00001606 if (result == NULL)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001607 PyErr_SetString(PyExc_TypeError,
Guido van Rossum2d951851994-08-29 12:52:16 +00001608 "reduce of empty sequence with no initial value");
1609
Guido van Rossum12d12c51993-10-26 17:58:25 +00001610 return result;
1611
Guido van Rossum2d951851994-08-29 12:52:16 +00001612Fail:
Guido van Rossum79f25d91997-04-29 20:08:16 +00001613 Py_XDECREF(args);
1614 Py_XDECREF(result);
Guido van Rossum12d12c51993-10-26 17:58:25 +00001615 return NULL;
1616}
1617
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001618static char reduce_doc[] =
1619"reduce(function, sequence[, initial]) -> value\n\
1620\n\
1621Apply a function of two arguments cumulatively to the items of a sequence,\n\
1622from left to right, so as to reduce the sequence to a single value.\n\
1623For example, reduce(lambda x, y: x+y, [1, 2, 3, 4, 5]) calculates\n\
1624((((1+2)+3)+4)+5). If initial is present, it is placed before the items\n\
1625of the sequence in the calculation, and serves as a default when the\n\
1626sequence is empty.";
1627
1628
Guido van Rossum79f25d91997-04-29 20:08:16 +00001629static PyObject *
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001630builtin_reload(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001631 PyObject *self;
1632 PyObject *args;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001633{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001634 PyObject *v;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001635
Guido van Rossum79f25d91997-04-29 20:08:16 +00001636 if (!PyArg_ParseTuple(args, "O:reload", &v))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001637 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001638 return PyImport_ReloadModule(v);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001639}
1640
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001641static char reload_doc[] =
1642"reload(module) -> module\n\
1643\n\
1644Reload the module. The module must have been successfully imported before.";
1645
1646
Guido van Rossum79f25d91997-04-29 20:08:16 +00001647static PyObject *
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001648builtin_repr(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001649 PyObject *self;
1650 PyObject *args;
Guido van Rossumc89705d1992-11-26 08:54:07 +00001651{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001652 PyObject *v;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001653
Guido van Rossum79f25d91997-04-29 20:08:16 +00001654 if (!PyArg_ParseTuple(args, "O:repr", &v))
Guido van Rossumc89705d1992-11-26 08:54:07 +00001655 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001656 return PyObject_Repr(v);
Guido van Rossumc89705d1992-11-26 08:54:07 +00001657}
1658
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001659static char repr_doc[] =
1660"repr(object) -> string\n\
1661\n\
1662Return the canonical string representation of the object.\n\
1663For most object types, eval(repr(object)) == object.";
1664
1665
Guido van Rossum79f25d91997-04-29 20:08:16 +00001666static PyObject *
Guido van Rossum9e51f9b1993-02-12 16:29:05 +00001667builtin_round(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001668 PyObject *self;
1669 PyObject *args;
Guido van Rossum9e51f9b1993-02-12 16:29:05 +00001670{
Guido van Rossum9e51f9b1993-02-12 16:29:05 +00001671 double x;
1672 double f;
1673 int ndigits = 0;
Guido van Rossum9e51f9b1993-02-12 16:29:05 +00001674 int i;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001675
Guido van Rossum79f25d91997-04-29 20:08:16 +00001676 if (!PyArg_ParseTuple(args, "d|i:round", &x, &ndigits))
Guido van Rossum9e51f9b1993-02-12 16:29:05 +00001677 return NULL;
Guido van Rossum9e51f9b1993-02-12 16:29:05 +00001678 f = 1.0;
Guido van Rossum1e162d31998-05-09 14:42:25 +00001679 i = abs(ndigits);
1680 while (--i >= 0)
Guido van Rossum9e51f9b1993-02-12 16:29:05 +00001681 f = f*10.0;
Guido van Rossum1e162d31998-05-09 14:42:25 +00001682 if (ndigits < 0)
1683 x /= f;
Guido van Rossum9e51f9b1993-02-12 16:29:05 +00001684 else
Guido van Rossum1e162d31998-05-09 14:42:25 +00001685 x *= f;
1686 if (x >= 0.0)
1687 x = floor(x + 0.5);
1688 else
1689 x = ceil(x - 0.5);
1690 if (ndigits < 0)
1691 x *= f;
1692 else
1693 x /= f;
1694 return PyFloat_FromDouble(x);
Guido van Rossum9e51f9b1993-02-12 16:29:05 +00001695}
1696
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001697static char round_doc[] =
1698"round(number[, ndigits]) -> floating point number\n\
1699\n\
1700Round a number to a given precision in decimal digits (default 0 digits).\n\
1701This always returns a floating point number. Precision may be negative.";
1702
1703
Guido van Rossum79f25d91997-04-29 20:08:16 +00001704static PyObject *
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001705builtin_str(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001706 PyObject *self;
1707 PyObject *args;
Guido van Rossumc89705d1992-11-26 08:54:07 +00001708{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001709 PyObject *v;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001710
Guido van Rossum79f25d91997-04-29 20:08:16 +00001711 if (!PyArg_ParseTuple(args, "O:str", &v))
Guido van Rossumc89705d1992-11-26 08:54:07 +00001712 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001713 return PyObject_Str(v);
Guido van Rossumc89705d1992-11-26 08:54:07 +00001714}
1715
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001716static char str_doc[] =
1717"str(object) -> string\n\
1718\n\
1719Return a nice string representation of the object.\n\
1720If the argument is a string, the return value is the same object.";
1721
1722
Guido van Rossum79f25d91997-04-29 20:08:16 +00001723static PyObject *
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001724builtin_tuple(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001725 PyObject *self;
1726 PyObject *args;
Guido van Rossumcae027b1994-08-29 12:53:11 +00001727{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001728 PyObject *v;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001729
Guido van Rossum79f25d91997-04-29 20:08:16 +00001730 if (!PyArg_ParseTuple(args, "O:tuple", &v))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001731 return NULL;
Guido van Rossum09df08a1998-05-22 00:51:39 +00001732 return PySequence_Tuple(v);
Guido van Rossumcae027b1994-08-29 12:53:11 +00001733}
1734
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001735static char tuple_doc[] =
1736"tuple(sequence) -> list\n\
1737\n\
1738Return a tuple whose items are the same as those of the argument sequence.\n\
1739If the argument is a tuple, the return value is the same object.";
1740
1741
Guido van Rossum79f25d91997-04-29 20:08:16 +00001742static PyObject *
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001743builtin_type(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001744 PyObject *self;
1745 PyObject *args;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001746{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001747 PyObject *v;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001748
Guido van Rossum79f25d91997-04-29 20:08:16 +00001749 if (!PyArg_ParseTuple(args, "O:type", &v))
Guido van Rossum3f5da241990-12-20 15:06:42 +00001750 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001751 v = (PyObject *)v->ob_type;
1752 Py_INCREF(v);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001753 return v;
1754}
1755
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001756static char type_doc[] =
1757"type(object) -> type object\n\
1758\n\
1759Return the type of the object.";
1760
1761
Guido van Rossum79f25d91997-04-29 20:08:16 +00001762static PyObject *
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001763builtin_vars(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001764 PyObject *self;
1765 PyObject *args;
Guido van Rossum2d951851994-08-29 12:52:16 +00001766{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001767 PyObject *v = NULL;
1768 PyObject *d;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001769
Guido van Rossum79f25d91997-04-29 20:08:16 +00001770 if (!PyArg_ParseTuple(args, "|O:vars", &v))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001771 return NULL;
Guido van Rossum2d951851994-08-29 12:52:16 +00001772 if (v == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001773 d = PyEval_GetLocals();
Guido van Rossum53bb7ff1995-07-26 16:26:31 +00001774 if (d == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001775 if (!PyErr_Occurred())
1776 PyErr_SetString(PyExc_SystemError,
1777 "no locals!?");
Guido van Rossum53bb7ff1995-07-26 16:26:31 +00001778 }
1779 else
Guido van Rossum79f25d91997-04-29 20:08:16 +00001780 Py_INCREF(d);
Guido van Rossum2d951851994-08-29 12:52:16 +00001781 }
1782 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001783 d = PyObject_GetAttrString(v, "__dict__");
Guido van Rossum2d951851994-08-29 12:52:16 +00001784 if (d == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001785 PyErr_SetString(PyExc_TypeError,
Guido van Rossum2d951851994-08-29 12:52:16 +00001786 "vars() argument must have __dict__ attribute");
1787 return NULL;
1788 }
1789 }
1790 return d;
1791}
1792
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001793static char vars_doc[] =
1794"vars([object]) -> dictionary\n\
1795\n\
1796Without arguments, equivalent to locals().\n\
1797With an argument, equivalent to object.__dict__.";
1798
1799
Barry Warsawcde8b1b1997-08-22 21:14:38 +00001800static PyObject *
1801builtin_isinstance(self, args)
1802 PyObject *self;
1803 PyObject *args;
1804{
1805 PyObject *inst;
1806 PyObject *cls;
1807 int retval;
1808
1809 if (!PyArg_ParseTuple(args, "OO", &inst, &cls))
1810 return NULL;
Guido van Rossumf5dd9141997-12-02 19:11:45 +00001811 if (PyType_Check(cls)) {
Guido van Rossumd6af46d1997-12-10 05:51:47 +00001812 retval = ((PyObject *)(inst->ob_type) == cls);
Barry Warsawcde8b1b1997-08-22 21:14:38 +00001813 }
Barry Warsawcde8b1b1997-08-22 21:14:38 +00001814 else {
Guido van Rossumf5dd9141997-12-02 19:11:45 +00001815 if (!PyClass_Check(cls)) {
1816 PyErr_SetString(PyExc_TypeError,
1817 "second argument must be a class");
1818 return NULL;
1819 }
1820
1821 if (!PyInstance_Check(inst))
1822 retval = 0;
1823 else {
1824 PyObject *inclass =
1825 (PyObject*)((PyInstanceObject*)inst)->in_class;
1826 retval = PyClass_IsSubclass(inclass, cls);
1827 }
Barry Warsawcde8b1b1997-08-22 21:14:38 +00001828 }
1829 return PyInt_FromLong(retval);
1830}
1831
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001832static char isinstance_doc[] =
1833"isinstance(object, class-or-type) -> Boolean\n\
1834\n\
1835Return whether an object is an instance of a class or of a subclass thereof.\n\
1836With a type as second argument, return whether that is the object's type.";
1837
Barry Warsawcde8b1b1997-08-22 21:14:38 +00001838
1839static PyObject *
1840builtin_issubclass(self, args)
1841 PyObject *self;
1842 PyObject *args;
1843{
1844 PyObject *derived;
1845 PyObject *cls;
1846 int retval;
1847
1848 if (!PyArg_ParseTuple(args, "OO", &derived, &cls))
1849 return NULL;
1850 if (!PyClass_Check(derived) || !PyClass_Check(cls)) {
1851 PyErr_SetString(PyExc_TypeError, "arguments must be classes");
1852 return NULL;
1853 }
1854 /* shortcut */
1855 if (!(retval = (derived == cls)))
1856 retval = PyClass_IsSubclass(derived, cls);
1857
1858 return PyInt_FromLong(retval);
1859}
1860
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001861static char issubclass_doc[] =
1862"issubclass(C, B) -> Boolean\n\
1863\n\
1864Return whether class C is a subclass (i.e., a derived class) of class B.";
1865
Barry Warsawcde8b1b1997-08-22 21:14:38 +00001866
Guido van Rossum79f25d91997-04-29 20:08:16 +00001867static PyMethodDef builtin_methods[] = {
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001868 {"__import__", builtin___import__, 1, import_doc},
1869 {"abs", builtin_abs, 1, abs_doc},
1870 {"apply", builtin_apply, 1, apply_doc},
1871 {"callable", builtin_callable, 1, callable_doc},
1872 {"chr", builtin_chr, 1, chr_doc},
1873 {"cmp", builtin_cmp, 1, cmp_doc},
1874 {"coerce", builtin_coerce, 1, coerce_doc},
1875 {"compile", builtin_compile, 1, compile_doc},
Guido van Rossum8a5c5d21996-01-12 01:09:56 +00001876#ifndef WITHOUT_COMPLEX
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001877 {"complex", builtin_complex, 1, complex_doc},
Guido van Rossum8a5c5d21996-01-12 01:09:56 +00001878#endif
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001879 {"delattr", builtin_delattr, 1, delattr_doc},
1880 {"dir", builtin_dir, 1, dir_doc},
1881 {"divmod", builtin_divmod, 1, divmod_doc},
1882 {"eval", builtin_eval, 1, eval_doc},
1883 {"execfile", builtin_execfile, 1, execfile_doc},
1884 {"filter", builtin_filter, 1, filter_doc},
1885 {"float", builtin_float, 1, float_doc},
1886 {"getattr", builtin_getattr, 1, getattr_doc},
1887 {"globals", builtin_globals, 1, globals_doc},
1888 {"hasattr", builtin_hasattr, 1, hasattr_doc},
1889 {"hash", builtin_hash, 1, hash_doc},
1890 {"hex", builtin_hex, 1, hex_doc},
1891 {"id", builtin_id, 1, id_doc},
1892 {"input", builtin_input, 1, input_doc},
1893 {"intern", builtin_intern, 1, intern_doc},
1894 {"int", builtin_int, 1, int_doc},
1895 {"isinstance", builtin_isinstance, 1, isinstance_doc},
1896 {"issubclass", builtin_issubclass, 1, issubclass_doc},
1897 {"len", builtin_len, 1, len_doc},
1898 {"list", builtin_list, 1, list_doc},
1899 {"locals", builtin_locals, 1, locals_doc},
1900 {"long", builtin_long, 1, long_doc},
1901 {"map", builtin_map, 1, map_doc},
1902 {"max", builtin_max, 1, max_doc},
1903 {"min", builtin_min, 1, min_doc},
1904 {"oct", builtin_oct, 1, oct_doc},
1905 {"open", builtin_open, 1, open_doc},
1906 {"ord", builtin_ord, 1, ord_doc},
1907 {"pow", builtin_pow, 1, pow_doc},
1908 {"range", builtin_range, 1, range_doc},
1909 {"raw_input", builtin_raw_input, 1, raw_input_doc},
1910 {"reduce", builtin_reduce, 1, reduce_doc},
1911 {"reload", builtin_reload, 1, reload_doc},
1912 {"repr", builtin_repr, 1, repr_doc},
1913 {"round", builtin_round, 1, round_doc},
1914 {"setattr", builtin_setattr, 1, setattr_doc},
1915 {"slice", builtin_slice, 1, slice_doc},
1916 {"str", builtin_str, 1, str_doc},
1917 {"tuple", builtin_tuple, 1, tuple_doc},
1918 {"type", builtin_type, 1, type_doc},
1919 {"vars", builtin_vars, 1, vars_doc},
1920 {"xrange", builtin_xrange, 1, xrange_doc},
Guido van Rossumc02e15c1991-12-16 13:03:00 +00001921 {NULL, NULL},
Guido van Rossum3f5da241990-12-20 15:06:42 +00001922};
1923
Guido van Rossum3f5da241990-12-20 15:06:42 +00001924/* Predefined exceptions */
1925
Guido van Rossum04748321997-09-16 18:43:15 +00001926PyObject *PyExc_Exception;
Barry Warsaw757af0e1997-08-29 22:13:51 +00001927PyObject *PyExc_StandardError;
Barry Warsaw412cdc21997-09-16 21:51:14 +00001928PyObject *PyExc_ArithmeticError;
Barry Warsaw757af0e1997-08-29 22:13:51 +00001929PyObject *PyExc_LookupError;
1930
Guido van Rossum79f25d91997-04-29 20:08:16 +00001931PyObject *PyExc_AssertionError;
1932PyObject *PyExc_AttributeError;
1933PyObject *PyExc_EOFError;
Guido van Rossumb6a7f771997-05-09 03:03:23 +00001934PyObject *PyExc_FloatingPointError;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001935PyObject *PyExc_IOError;
1936PyObject *PyExc_ImportError;
1937PyObject *PyExc_IndexError;
1938PyObject *PyExc_KeyError;
1939PyObject *PyExc_KeyboardInterrupt;
1940PyObject *PyExc_MemoryError;
1941PyObject *PyExc_NameError;
1942PyObject *PyExc_OverflowError;
1943PyObject *PyExc_RuntimeError;
1944PyObject *PyExc_SyntaxError;
1945PyObject *PyExc_SystemError;
1946PyObject *PyExc_SystemExit;
1947PyObject *PyExc_TypeError;
1948PyObject *PyExc_ValueError;
1949PyObject *PyExc_ZeroDivisionError;
Guido van Rossum50afb7a1991-12-10 13:52:31 +00001950
Barry Warsaw757af0e1997-08-29 22:13:51 +00001951PyObject *PyExc_MemoryErrorInst;
1952
1953static struct
1954{
1955 char* name;
1956 PyObject** exc;
1957 int leaf_exc;
1958}
1959bltin_exc[] = {
Guido van Rossum04748321997-09-16 18:43:15 +00001960 {"Exception", &PyExc_Exception, 0},
Barry Warsaw757af0e1997-08-29 22:13:51 +00001961 {"StandardError", &PyExc_StandardError, 0},
Barry Warsaw412cdc21997-09-16 21:51:14 +00001962 {"ArithmeticError", &PyExc_ArithmeticError, 0},
Barry Warsaw757af0e1997-08-29 22:13:51 +00001963 {"LookupError", &PyExc_LookupError, 0},
1964 {"AssertionError", &PyExc_AssertionError, 1},
1965 {"AttributeError", &PyExc_AttributeError, 1},
1966 {"EOFError", &PyExc_EOFError, 1},
1967 {"FloatingPointError", &PyExc_FloatingPointError, 1},
1968 {"IOError", &PyExc_IOError, 1},
1969 {"ImportError", &PyExc_ImportError, 1},
1970 {"IndexError", &PyExc_IndexError, 1},
1971 {"KeyError", &PyExc_KeyError, 1},
1972 {"KeyboardInterrupt", &PyExc_KeyboardInterrupt, 1},
1973 {"MemoryError", &PyExc_MemoryError, 1},
1974 {"NameError", &PyExc_NameError, 1},
1975 {"OverflowError", &PyExc_OverflowError, 1},
1976 {"RuntimeError", &PyExc_RuntimeError, 1},
1977 {"SyntaxError", &PyExc_SyntaxError, 1},
1978 {"SystemError", &PyExc_SystemError, 1},
1979 {"SystemExit", &PyExc_SystemExit, 1},
1980 {"TypeError", &PyExc_TypeError, 1},
1981 {"ValueError", &PyExc_ValueError, 1},
1982 {"ZeroDivisionError", &PyExc_ZeroDivisionError, 1},
1983 {NULL, NULL}
1984};
1985
1986
1987/* import exceptions module to extract class exceptions */
1988static void
1989init_class_exc(dict)
1990 PyObject *dict;
1991{
1992 int i;
1993 PyObject *m = PyImport_ImportModule("exceptions");
1994 PyObject *d;
1995 PyObject *args;
1996
1997 if (m == NULL ||
1998 (d = PyModule_GetDict(m)) == NULL)
1999 {
Guido van Rossum09df08a1998-05-22 00:51:39 +00002000 /* XXX Should use PySys_WriteStderr here */
Barry Warsaw757af0e1997-08-29 22:13:51 +00002001 PyObject *f = PySys_GetObject("stderr");
2002 if (Py_VerboseFlag) {
2003 PyFile_WriteString(
2004 "'import exceptions' failed; traceback:\n", f);
2005 PyErr_Print();
2006 }
2007 else {
2008 PyFile_WriteString(
2009 "'import exceptions' failed; use -v for traceback\n", f);
2010 PyErr_Clear();
2011 }
2012 PyFile_WriteString("defaulting to old style exceptions\n", f);
2013 return;
2014 }
2015 for (i = 0; bltin_exc[i].name; i++) {
2016 /* dig the exception out of the module */
2017 PyObject *exc = PyDict_GetItemString(d, bltin_exc[i].name);
2018 if (!exc)
2019 Py_FatalError("built-in exception cannot be initialized");
2020
2021 Py_XDECREF(*bltin_exc[i].exc);
2022
2023 /* squirrel away a pointer to the exception */
2024 Py_INCREF(exc);
2025 *bltin_exc[i].exc = exc;
2026
2027 /* and insert the name in the __builtin__ module */
2028 PyDict_SetItemString(dict, bltin_exc[i].name, exc);
2029 }
2030
2031 /* we need one pre-allocated instance */
2032 args = Py_BuildValue("()");
2033 if (args) {
2034 PyExc_MemoryErrorInst =
2035 PyEval_CallObject(PyExc_MemoryError, args);
2036 Py_DECREF(args);
2037 }
2038
2039 /* we're done with the exceptions module */
2040 Py_DECREF(m);
2041
2042 if (PyErr_Occurred())
2043 Py_FatalError("can't instantiate standard exceptions");
2044}
2045
2046
2047static void
2048fini_instances()
2049{
2050 Py_XDECREF(PyExc_MemoryErrorInst);
2051 PyExc_MemoryErrorInst = NULL;
2052}
2053
2054
Guido van Rossum79f25d91997-04-29 20:08:16 +00002055static PyObject *
Guido van Rossum25ce5661997-08-02 03:10:38 +00002056newstdexception(dict, name)
2057 PyObject *dict;
Guido van Rossumfb905c31991-12-16 15:42:38 +00002058 char *name;
Guido van Rossum3f5da241990-12-20 15:06:42 +00002059{
Guido van Rossum79f25d91997-04-29 20:08:16 +00002060 PyObject *v = PyString_FromString(name);
Guido van Rossum25ce5661997-08-02 03:10:38 +00002061 if (v == NULL || PyDict_SetItemString(dict, name, v) != 0)
Guido van Rossum79f25d91997-04-29 20:08:16 +00002062 Py_FatalError("no mem for new standard exception");
Guido van Rossum3f5da241990-12-20 15:06:42 +00002063 return v;
2064}
2065
2066static void
Guido van Rossum25ce5661997-08-02 03:10:38 +00002067initerrors(dict)
2068 PyObject *dict;
Guido van Rossum3f5da241990-12-20 15:06:42 +00002069{
Barry Warsaw757af0e1997-08-29 22:13:51 +00002070 int i;
2071 int exccnt = 0;
2072 for (i = 0; bltin_exc[i].name; i++, exccnt++) {
2073 if (bltin_exc[i].leaf_exc)
2074 *bltin_exc[i].exc =
2075 newstdexception(dict, bltin_exc[i].name);
2076 }
2077
2078 /* This is kind of bogus because we special case the three new
2079 exceptions to be nearly forward compatible. But this means we
2080 hard code knowledge about exceptions.py into C here. I don't
2081 have a better solution, though
2082 */
2083 PyExc_LookupError = PyTuple_New(2);
2084 Py_INCREF(PyExc_IndexError);
2085 PyTuple_SET_ITEM(PyExc_LookupError, 0, PyExc_IndexError);
2086 Py_INCREF(PyExc_KeyError);
2087 PyTuple_SET_ITEM(PyExc_LookupError, 1, PyExc_KeyError);
2088 PyDict_SetItemString(dict, "LookupError", PyExc_LookupError);
2089
Barry Warsaw412cdc21997-09-16 21:51:14 +00002090 PyExc_ArithmeticError = PyTuple_New(3);
Barry Warsaw757af0e1997-08-29 22:13:51 +00002091 Py_INCREF(PyExc_OverflowError);
Barry Warsaw412cdc21997-09-16 21:51:14 +00002092 PyTuple_SET_ITEM(PyExc_ArithmeticError, 0, PyExc_OverflowError);
Barry Warsaw757af0e1997-08-29 22:13:51 +00002093 Py_INCREF(PyExc_ZeroDivisionError);
Barry Warsaw412cdc21997-09-16 21:51:14 +00002094 PyTuple_SET_ITEM(PyExc_ArithmeticError, 1, PyExc_ZeroDivisionError);
Barry Warsaw757af0e1997-08-29 22:13:51 +00002095 Py_INCREF(PyExc_FloatingPointError);
Barry Warsaw412cdc21997-09-16 21:51:14 +00002096 PyTuple_SET_ITEM(PyExc_ArithmeticError, 2, PyExc_FloatingPointError);
2097 PyDict_SetItemString(dict, "ArithmeticError", PyExc_ArithmeticError);
Barry Warsaw757af0e1997-08-29 22:13:51 +00002098
Barry Warsawb01a7fa1997-09-18 03:44:38 +00002099 PyExc_StandardError = PyTuple_New(exccnt-2);
2100 for (i = 2; bltin_exc[i].name; i++) {
Barry Warsaw757af0e1997-08-29 22:13:51 +00002101 PyObject *exc = *bltin_exc[i].exc;
2102 Py_INCREF(exc);
Barry Warsawb01a7fa1997-09-18 03:44:38 +00002103 PyTuple_SET_ITEM(PyExc_StandardError, i-2, exc);
Barry Warsaw757af0e1997-08-29 22:13:51 +00002104 }
2105 PyDict_SetItemString(dict, "StandardError", PyExc_StandardError);
Guido van Rossum04748321997-09-16 18:43:15 +00002106
2107 /* Exception is treated differently; for now, it's == StandardError */
2108 PyExc_Exception = PyExc_StandardError;
2109 Py_INCREF(PyExc_Exception);
2110 PyDict_SetItemString(dict, "Exception", PyExc_Exception);
Barry Warsaw757af0e1997-08-29 22:13:51 +00002111
2112 if (PyErr_Occurred())
2113 Py_FatalError("Could not initialize built-in string exceptions");
Guido van Rossum25ce5661997-08-02 03:10:38 +00002114}
2115
2116static void
2117finierrors()
2118{
Barry Warsaw757af0e1997-08-29 22:13:51 +00002119 int i;
2120 for (i = 0; bltin_exc[i].name; i++) {
2121 PyObject *exc = *bltin_exc[i].exc;
2122 Py_XDECREF(exc);
2123 *bltin_exc[i].exc = NULL;
2124 }
Guido van Rossum25ce5661997-08-02 03:10:38 +00002125}
2126
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002127static char builtin_doc[] =
2128"Built-in functions, exceptions, and other objects.\n\
2129\n\
2130Noteworthy: None is the `nil' object; Ellipsis represents `...' in slices.";
2131
Guido van Rossum25ce5661997-08-02 03:10:38 +00002132PyObject *
Barry Warsaw757af0e1997-08-29 22:13:51 +00002133_PyBuiltin_Init_1()
Guido van Rossum25ce5661997-08-02 03:10:38 +00002134{
2135 PyObject *mod, *dict;
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002136 mod = Py_InitModule4("__builtin__", builtin_methods,
2137 builtin_doc, (PyObject *)NULL,
2138 PYTHON_API_VERSION);
Guido van Rossum25ce5661997-08-02 03:10:38 +00002139 if (mod == NULL)
2140 return NULL;
2141 dict = PyModule_GetDict(mod);
2142 initerrors(dict);
2143 if (PyDict_SetItemString(dict, "None", Py_None) < 0)
2144 return NULL;
2145 if (PyDict_SetItemString(dict, "Ellipsis", Py_Ellipsis) < 0)
2146 return NULL;
2147 if (PyDict_SetItemString(dict, "__debug__",
2148 PyInt_FromLong(Py_OptimizeFlag == 0)) < 0)
2149 return NULL;
Barry Warsaw757af0e1997-08-29 22:13:51 +00002150
Guido van Rossum25ce5661997-08-02 03:10:38 +00002151 return mod;
Guido van Rossum3f5da241990-12-20 15:06:42 +00002152}
2153
2154void
Barry Warsaw757af0e1997-08-29 22:13:51 +00002155_PyBuiltin_Init_2(dict)
2156 PyObject *dict;
2157{
2158 /* if Python was started with -X, initialize the class exceptions */
2159 if (Py_UseClassExceptionsFlag)
2160 init_class_exc(dict);
2161}
2162
2163
2164void
2165_PyBuiltin_Fini_1()
2166{
2167 fini_instances();
2168}
2169
2170
2171void
2172_PyBuiltin_Fini_2()
Guido van Rossum3f5da241990-12-20 15:06:42 +00002173{
Guido van Rossum25ce5661997-08-02 03:10:38 +00002174 finierrors();
Guido van Rossum3f5da241990-12-20 15:06:42 +00002175}
Guido van Rossumc6bb8f71991-07-01 18:42:41 +00002176
Guido van Rossum12d12c51993-10-26 17:58:25 +00002177
Guido van Rossume77a7571993-11-03 15:01:26 +00002178/* Helper for filter(): filter a tuple through a function */
Guido van Rossum12d12c51993-10-26 17:58:25 +00002179
Guido van Rossum79f25d91997-04-29 20:08:16 +00002180static PyObject *
Guido van Rossum12d12c51993-10-26 17:58:25 +00002181filtertuple(func, tuple)
Guido van Rossum79f25d91997-04-29 20:08:16 +00002182 PyObject *func;
2183 PyObject *tuple;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002184{
Guido van Rossum79f25d91997-04-29 20:08:16 +00002185 PyObject *result;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002186 register int i, j;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002187 int len = PyTuple_Size(tuple);
Guido van Rossum12d12c51993-10-26 17:58:25 +00002188
Guido van Rossumb7b45621995-08-04 04:07:45 +00002189 if (len == 0) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002190 Py_INCREF(tuple);
Guido van Rossumb7b45621995-08-04 04:07:45 +00002191 return tuple;
2192 }
2193
Guido van Rossum79f25d91997-04-29 20:08:16 +00002194 if ((result = PyTuple_New(len)) == NULL)
Guido van Rossum2586bf01993-11-01 16:21:44 +00002195 return NULL;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002196
Guido van Rossum12d12c51993-10-26 17:58:25 +00002197 for (i = j = 0; i < len; ++i) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002198 PyObject *item, *good;
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002199 int ok;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002200
Guido van Rossum79f25d91997-04-29 20:08:16 +00002201 if ((item = PyTuple_GetItem(tuple, i)) == NULL)
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002202 goto Fail_1;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002203 if (func == Py_None) {
2204 Py_INCREF(item);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002205 good = item;
2206 }
2207 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002208 PyObject *arg = Py_BuildValue("(O)", item);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002209 if (arg == NULL)
2210 goto Fail_1;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002211 good = PyEval_CallObject(func, arg);
2212 Py_DECREF(arg);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002213 if (good == NULL)
Guido van Rossum12d12c51993-10-26 17:58:25 +00002214 goto Fail_1;
2215 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002216 ok = PyObject_IsTrue(good);
2217 Py_DECREF(good);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002218 if (ok) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002219 Py_INCREF(item);
2220 if (PyTuple_SetItem(result, j++, item) < 0)
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002221 goto Fail_1;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002222 }
Guido van Rossum12d12c51993-10-26 17:58:25 +00002223 }
2224
Guido van Rossum79f25d91997-04-29 20:08:16 +00002225 if (_PyTuple_Resize(&result, j, 0) < 0)
Guido van Rossum12d12c51993-10-26 17:58:25 +00002226 return NULL;
2227
Guido van Rossum12d12c51993-10-26 17:58:25 +00002228 return result;
2229
Guido van Rossum12d12c51993-10-26 17:58:25 +00002230Fail_1:
Guido van Rossum79f25d91997-04-29 20:08:16 +00002231 Py_DECREF(result);
Guido van Rossum12d12c51993-10-26 17:58:25 +00002232 return NULL;
2233}
2234
2235
Guido van Rossume77a7571993-11-03 15:01:26 +00002236/* Helper for filter(): filter a string through a function */
Guido van Rossum12d12c51993-10-26 17:58:25 +00002237
Guido van Rossum79f25d91997-04-29 20:08:16 +00002238static PyObject *
Guido van Rossum12d12c51993-10-26 17:58:25 +00002239filterstring(func, strobj)
Guido van Rossum79f25d91997-04-29 20:08:16 +00002240 PyObject *func;
2241 PyObject *strobj;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002242{
Guido van Rossum79f25d91997-04-29 20:08:16 +00002243 PyObject *result;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002244 register int i, j;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002245 int len = PyString_Size(strobj);
Guido van Rossum12d12c51993-10-26 17:58:25 +00002246
Guido van Rossum79f25d91997-04-29 20:08:16 +00002247 if (func == Py_None) {
Guido van Rossum2586bf01993-11-01 16:21:44 +00002248 /* No character is ever false -- share input string */
Guido van Rossum79f25d91997-04-29 20:08:16 +00002249 Py_INCREF(strobj);
Guido van Rossum2d951851994-08-29 12:52:16 +00002250 return strobj;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002251 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002252 if ((result = PyString_FromStringAndSize(NULL, len)) == NULL)
Guido van Rossum2586bf01993-11-01 16:21:44 +00002253 return NULL;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002254
Guido van Rossum12d12c51993-10-26 17:58:25 +00002255 for (i = j = 0; i < len; ++i) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002256 PyObject *item, *arg, *good;
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002257 int ok;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002258
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002259 item = (*strobj->ob_type->tp_as_sequence->sq_item)(strobj, i);
2260 if (item == NULL)
2261 goto Fail_1;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002262 arg = Py_BuildValue("(O)", item);
2263 Py_DECREF(item);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002264 if (arg == NULL)
2265 goto Fail_1;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002266 good = PyEval_CallObject(func, arg);
2267 Py_DECREF(arg);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002268 if (good == NULL)
2269 goto Fail_1;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002270 ok = PyObject_IsTrue(good);
2271 Py_DECREF(good);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002272 if (ok)
Guido van Rossum79f25d91997-04-29 20:08:16 +00002273 PyString_AS_STRING((PyStringObject *)result)[j++] =
2274 PyString_AS_STRING((PyStringObject *)item)[0];
Guido van Rossum12d12c51993-10-26 17:58:25 +00002275 }
2276
Guido van Rossum79f25d91997-04-29 20:08:16 +00002277 if (j < len && _PyString_Resize(&result, j) < 0)
Guido van Rossum12d12c51993-10-26 17:58:25 +00002278 return NULL;
2279
Guido van Rossum12d12c51993-10-26 17:58:25 +00002280 return result;
2281
Guido van Rossum12d12c51993-10-26 17:58:25 +00002282Fail_1:
Guido van Rossum79f25d91997-04-29 20:08:16 +00002283 Py_DECREF(result);
Guido van Rossum12d12c51993-10-26 17:58:25 +00002284 return NULL;
2285}