blob: ec87492f34e2ab0649566ed31a9d6409deb2636d [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;
Barry Warsaw968f8cb1998-10-01 15:33:12 +0000104 PyObject *t = NULL, *retval = NULL;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000105
Guido van Rossum79f25d91997-04-29 20:08:16 +0000106 if (!PyArg_ParseTuple(args, "O|OO:apply", &func, &alist, &kwdict))
Guido van Rossumc02e15c1991-12-16 13:03:00 +0000107 return NULL;
Barry Warsaw968f8cb1998-10-01 15:33:12 +0000108 if (alist != NULL) {
109 if (!PyTuple_Check(alist)) {
110 if (!PySequence_Check(alist)) {
111 PyErr_SetString(PyExc_TypeError,
112 "apply() 2nd argument must be a sequence");
113 return NULL;
114 }
115 t = PySequence_Tuple(alist);
116 if (t == NULL)
117 return NULL;
118 alist = t;
119 }
Guido van Rossum2d951851994-08-29 12:52:16 +0000120 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000121 if (kwdict != NULL && !PyDict_Check(kwdict)) {
122 PyErr_SetString(PyExc_TypeError,
Guido van Rossum681d79a1995-07-18 14:51:37 +0000123 "apply() 3rd argument must be dictionary");
Barry Warsaw968f8cb1998-10-01 15:33:12 +0000124 goto finally;
Guido van Rossum681d79a1995-07-18 14:51:37 +0000125 }
Barry Warsaw968f8cb1998-10-01 15:33:12 +0000126 retval = PyEval_CallObjectWithKeywords(func, alist, kwdict);
127 finally:
128 Py_XDECREF(t);
129 return retval;
Guido van Rossumc02e15c1991-12-16 13:03:00 +0000130}
131
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000132static char apply_doc[] =
133"apply(function, args[, kwargs]) -> value\n\
134\n\
135Call a function with positional arguments taken from the tuple args,\n\
136and keyword arguments taken from the optional dictionary kwargs.";
137
138
Guido van Rossum79f25d91997-04-29 20:08:16 +0000139static PyObject *
Guido van Rossum0daf0221999-03-19 19:07:19 +0000140builtin_buffer(self, args)
141 PyObject *self;
142 PyObject *args;
143{
144 PyObject *ob;
145 int offset = 0;
146 int size = Py_END_OF_BUFFER;
147
148 if ( !PyArg_ParseTuple(args, "O|ii:buffer", &ob, &offset, &size) )
149 return NULL;
150 return PyBuffer_FromObject(ob, offset, size);
151}
152
153static char buffer_doc[] =
154"buffer(object [, offset[, size]) -> object\n\
155\n\
156Creates a new buffer object which references the given object.\n\
157The buffer will reference a slice of the target object from the\n\
158start of the object (or at the specified offset). The slice will\n\
159extend to the end of the target object (or with the specified size).";
160
161
162static PyObject *
Guido van Rossum2d951851994-08-29 12:52:16 +0000163builtin_callable(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000164 PyObject *self;
165 PyObject *args;
Guido van Rossum2d951851994-08-29 12:52:16 +0000166{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000167 PyObject *v;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000168
Guido van Rossum79f25d91997-04-29 20:08:16 +0000169 if (!PyArg_ParseTuple(args, "O:callable", &v))
Guido van Rossum2d951851994-08-29 12:52:16 +0000170 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000171 return PyInt_FromLong((long)PyCallable_Check(v));
Guido van Rossum2d951851994-08-29 12:52:16 +0000172}
173
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000174static char callable_doc[] =
175"callable(object) -> Boolean\n\
176\n\
177Return whether the object is callable (i.e., some kind of function).\n\
178Note that classes are callable, as are instances with a __call__() method.";
179
180
Guido van Rossum79f25d91997-04-29 20:08:16 +0000181static PyObject *
Guido van Rossume77a7571993-11-03 15:01:26 +0000182builtin_filter(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000183 PyObject *self;
184 PyObject *args;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000185{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000186 PyObject *func, *seq, *result;
187 PySequenceMethods *sqf;
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000188 int len;
189 register int i, j;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000190
Guido van Rossum79f25d91997-04-29 20:08:16 +0000191 if (!PyArg_ParseTuple(args, "OO:filter", &func, &seq))
Guido van Rossum12d12c51993-10-26 17:58:25 +0000192 return NULL;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000193
Guido van Rossum79f25d91997-04-29 20:08:16 +0000194 if (PyString_Check(seq)) {
195 PyObject *r = filterstring(func, seq);
Guido van Rossum12d12c51993-10-26 17:58:25 +0000196 return r;
197 }
198
Guido van Rossum79f25d91997-04-29 20:08:16 +0000199 if (PyTuple_Check(seq)) {
200 PyObject *r = filtertuple(func, seq);
Guido van Rossum12d12c51993-10-26 17:58:25 +0000201 return r;
202 }
203
Guido van Rossum09df08a1998-05-22 00:51:39 +0000204 sqf = seq->ob_type->tp_as_sequence;
205 if (sqf == NULL || sqf->sq_length == NULL || sqf->sq_item == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000206 PyErr_SetString(PyExc_TypeError,
Guido van Rossume77a7571993-11-03 15:01:26 +0000207 "argument 2 to filter() must be a sequence type");
Guido van Rossum12d12c51993-10-26 17:58:25 +0000208 goto Fail_2;
209 }
210
211 if ((len = (*sqf->sq_length)(seq)) < 0)
212 goto Fail_2;
213
Guido van Rossum79f25d91997-04-29 20:08:16 +0000214 if (PyList_Check(seq) && seq->ob_refcnt == 1) {
215 Py_INCREF(seq);
Guido van Rossum12d12c51993-10-26 17:58:25 +0000216 result = seq;
217 }
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000218 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000219 if ((result = PyList_New(len)) == NULL)
Guido van Rossum12d12c51993-10-26 17:58:25 +0000220 goto Fail_2;
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000221 }
Guido van Rossum12d12c51993-10-26 17:58:25 +0000222
Guido van Rossum2d951851994-08-29 12:52:16 +0000223 for (i = j = 0; ; ++i) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000224 PyObject *item, *good;
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000225 int ok;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000226
Guido van Rossum2d951851994-08-29 12:52:16 +0000227 if ((item = (*sqf->sq_item)(seq, i)) == NULL) {
Barry Warsawcde8b1b1997-08-22 21:14:38 +0000228 if (PyErr_ExceptionMatches(PyExc_IndexError)) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000229 PyErr_Clear();
Guido van Rossum2d951851994-08-29 12:52:16 +0000230 break;
231 }
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000232 goto Fail_1;
Guido van Rossum2d951851994-08-29 12:52:16 +0000233 }
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000234
Guido van Rossum79f25d91997-04-29 20:08:16 +0000235 if (func == Py_None) {
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000236 good = item;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000237 Py_INCREF(good);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000238 }
239 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000240 PyObject *arg = Py_BuildValue("(O)", item);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000241 if (arg == NULL)
242 goto Fail_1;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000243 good = PyEval_CallObject(func, arg);
244 Py_DECREF(arg);
Guido van Rossum58b68731995-01-10 17:40:55 +0000245 if (good == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000246 Py_DECREF(item);
Guido van Rossum12d12c51993-10-26 17:58:25 +0000247 goto Fail_1;
Guido van Rossum58b68731995-01-10 17:40:55 +0000248 }
Guido van Rossum12d12c51993-10-26 17:58:25 +0000249 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000250 ok = PyObject_IsTrue(good);
251 Py_DECREF(good);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000252 if (ok) {
Guido van Rossum2d951851994-08-29 12:52:16 +0000253 if (j < len) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000254 if (PyList_SetItem(result, j++, item) < 0)
Guido van Rossum2d951851994-08-29 12:52:16 +0000255 goto Fail_1;
256 }
257 else {
Barry Warsawfa77e091999-01-28 18:49:12 +0000258 int status = PyList_Append(result, item);
Guido van Rossum2d951851994-08-29 12:52:16 +0000259 j++;
Barry Warsawfa77e091999-01-28 18:49:12 +0000260 Py_DECREF(item);
261 if (status < 0)
Guido van Rossum2d951851994-08-29 12:52:16 +0000262 goto Fail_1;
263 }
Guido van Rossum58b68731995-01-10 17:40:55 +0000264 } else {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000265 Py_DECREF(item);
Guido van Rossum12d12c51993-10-26 17:58:25 +0000266 }
Guido van Rossum12d12c51993-10-26 17:58:25 +0000267 }
268
Guido van Rossum12d12c51993-10-26 17:58:25 +0000269
Guido van Rossum79f25d91997-04-29 20:08:16 +0000270 if (j < len && PyList_SetSlice(result, j, len, NULL) < 0)
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000271 goto Fail_1;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000272
Guido van Rossum12d12c51993-10-26 17:58:25 +0000273 return result;
274
Guido van Rossum12d12c51993-10-26 17:58:25 +0000275Fail_1:
Guido van Rossum79f25d91997-04-29 20:08:16 +0000276 Py_DECREF(result);
Guido van Rossum12d12c51993-10-26 17:58:25 +0000277Fail_2:
Guido van Rossum12d12c51993-10-26 17:58:25 +0000278 return NULL;
279}
280
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000281static char filter_doc[] =
282"filter(function, sequence) -> list\n\
283\n\
284Return a list containing those items of sequence for which function(item)\n\
285is true. If function is None, return a list of items that are true.";
286
287
Guido van Rossum79f25d91997-04-29 20:08:16 +0000288static PyObject *
Guido van Rossum94390a41992-08-14 15:14:30 +0000289builtin_chr(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000290 PyObject *self;
291 PyObject *args;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000292{
293 long x;
294 char s[1];
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000295
Guido van Rossum79f25d91997-04-29 20:08:16 +0000296 if (!PyArg_ParseTuple(args, "l:chr", &x))
Guido van Rossum3f5da241990-12-20 15:06:42 +0000297 return NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000298 if (x < 0 || x >= 256) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000299 PyErr_SetString(PyExc_ValueError,
300 "chr() arg not in range(256)");
Guido van Rossum3f5da241990-12-20 15:06:42 +0000301 return NULL;
302 }
Guido van Rossum6bf62da1997-04-11 20:37:35 +0000303 s[0] = (char)x;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000304 return PyString_FromStringAndSize(s, 1);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000305}
306
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000307static char chr_doc[] =
308"chr(i) -> character\n\
309\n\
310Return a string of one character with ordinal i; 0 <= i < 256.";
311
312
Guido van Rossum79f25d91997-04-29 20:08:16 +0000313static PyObject *
Guido van Rossuma9e7dc11992-10-18 18:53:57 +0000314builtin_cmp(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000315 PyObject *self;
316 PyObject *args;
Guido van Rossuma9e7dc11992-10-18 18:53:57 +0000317{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000318 PyObject *a, *b;
Guido van Rossum09df08a1998-05-22 00:51:39 +0000319 int c;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000320
Guido van Rossum79f25d91997-04-29 20:08:16 +0000321 if (!PyArg_ParseTuple(args, "OO:cmp", &a, &b))
Guido van Rossuma9e7dc11992-10-18 18:53:57 +0000322 return NULL;
Guido van Rossum09df08a1998-05-22 00:51:39 +0000323 if (PyObject_Cmp(a, b, &c) < 0)
Guido van Rossumc8b6df91997-05-23 00:06:51 +0000324 return NULL;
Guido van Rossum09df08a1998-05-22 00:51:39 +0000325 return PyInt_FromLong((long)c);
Guido van Rossuma9e7dc11992-10-18 18:53:57 +0000326}
327
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000328static char cmp_doc[] =
329"cmp(x, y) -> integer\n\
330\n\
331Return negative if x<y, zero if x==y, positive if x>y.";
332
333
Guido van Rossum79f25d91997-04-29 20:08:16 +0000334static PyObject *
Guido van Rossum5524a591995-01-10 15:26:20 +0000335builtin_coerce(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000336 PyObject *self;
337 PyObject *args;
Guido van Rossum6a00cd81995-01-07 12:39:01 +0000338{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000339 PyObject *v, *w;
340 PyObject *res;
Guido van Rossum5524a591995-01-10 15:26:20 +0000341
Guido van Rossum79f25d91997-04-29 20:08:16 +0000342 if (!PyArg_ParseTuple(args, "OO:coerce", &v, &w))
Guido van Rossum5524a591995-01-10 15:26:20 +0000343 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000344 if (PyNumber_Coerce(&v, &w) < 0)
Guido van Rossum04691fc1992-08-12 15:35:34 +0000345 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000346 res = Py_BuildValue("(OO)", v, w);
347 Py_DECREF(v);
348 Py_DECREF(w);
Guido van Rossum04691fc1992-08-12 15:35:34 +0000349 return res;
350}
351
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000352static char coerce_doc[] =
353"coerce(x, y) -> None or (x1, y1)\n\
354\n\
355When x and y can be coerced to values of the same type, return a tuple\n\
356containing the coerced values. When they can't be coerced, return None.";
357
358
Guido van Rossum79f25d91997-04-29 20:08:16 +0000359static PyObject *
Guido van Rossum5b722181993-03-30 17:46:03 +0000360builtin_compile(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000361 PyObject *self;
362 PyObject *args;
Guido van Rossum5b722181993-03-30 17:46:03 +0000363{
364 char *str;
365 char *filename;
366 char *startstr;
367 int start;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000368
Guido van Rossum79f25d91997-04-29 20:08:16 +0000369 if (!PyArg_ParseTuple(args, "sss:compile", &str, &filename, &startstr))
Guido van Rossum5b722181993-03-30 17:46:03 +0000370 return NULL;
371 if (strcmp(startstr, "exec") == 0)
Guido van Rossumb05a5c71997-05-07 17:46:13 +0000372 start = Py_file_input;
Guido van Rossum5b722181993-03-30 17:46:03 +0000373 else if (strcmp(startstr, "eval") == 0)
Guido van Rossumb05a5c71997-05-07 17:46:13 +0000374 start = Py_eval_input;
Guido van Rossum872537c1995-07-07 22:43:42 +0000375 else if (strcmp(startstr, "single") == 0)
Guido van Rossumb05a5c71997-05-07 17:46:13 +0000376 start = Py_single_input;
Guido van Rossum5b722181993-03-30 17:46:03 +0000377 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000378 PyErr_SetString(PyExc_ValueError,
Guido van Rossum872537c1995-07-07 22:43:42 +0000379 "compile() mode must be 'exec' or 'eval' or 'single'");
Guido van Rossum5b722181993-03-30 17:46:03 +0000380 return NULL;
381 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000382 return Py_CompileString(str, filename, start);
Guido van Rossum5b722181993-03-30 17:46:03 +0000383}
384
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000385static char compile_doc[] =
386"compile(source, filename, mode) -> code object\n\
387\n\
388Compile the source string (a Python module, statement or expression)\n\
389into a code object that can be executed by the exec statement or eval().\n\
390The filename will be used for run-time error messages.\n\
391The mode must be 'exec' to compile a module, 'single' to compile a\n\
392single (interactive) statement, or 'eval' to compile an expression.";
393
394
Guido van Rossum8a5c5d21996-01-12 01:09:56 +0000395#ifndef WITHOUT_COMPLEX
396
Guido van Rossum79f25d91997-04-29 20:08:16 +0000397static PyObject *
Guido van Rossum11950231999-03-25 21:16:07 +0000398complex_from_string(v)
399 PyObject *v;
400{
401 extern double strtod Py_PROTO((const char *, char **));
402 char a, *s, *start, *end;
403 double x=0.0, y=0.0, z;
404 int got_re=0, got_im=0, done=0;
405 int digit_or_dot;
406 int sw_error=0;
407 int sign;
408 char buffer[256]; /* For errors */
409
410 start = s = PyString_AS_STRING(v);
411
412 /* position on first nonblank */
413 while (*s && isspace(Py_CHARMASK(*s)))
414 s++;
415 if (s[0] == '\0') {
416 PyErr_SetString(PyExc_ValueError,
417 "empty string for complex()");
418 return NULL;
419 }
420
421 z = -1.0;
422 sign = 1;
423 do {
424
425 switch (*s) {
426
427 case '\0':
428 if (s-start != PyString_GET_SIZE(v)) {
429 PyErr_SetString(
430 PyExc_ValueError,
431 "null byte in argument for complex()");
432 return NULL;
433 }
434 if(!done) sw_error=1;
435 break;
436
437 case '-':
438 sign = -1;
439 /* Fallthrough */
440 case '+':
441 if (done) sw_error=1;
442 s++;
443 if ( *s=='\0'||*s=='+'||*s=='-' ||
444 isspace(Py_CHARMASK(*s)) ) sw_error=1;
445 break;
446
447 case 'J':
448 case 'j':
449 if (got_im || done) {
450 sw_error = 1;
451 break;
452 }
453 if (z<0.0) {
454 y=sign;
455 }
456 else{
457 y=sign*z;
458 }
459 got_im=1;
460 s++;
461 if (*s!='+' && *s!='-' )
462 done=1;
463 break;
464
465 default:
466 if (isspace(Py_CHARMASK(*s))) {
467 while (*s && isspace(Py_CHARMASK(*s)))
468 s++;
469 if (s[0] != '\0')
470 sw_error=1;
471 else
472 done = 1;
473 break;
474 }
475 digit_or_dot =
476 (*s=='.' || isdigit(Py_CHARMASK(*s)));
477 if (done||!digit_or_dot) {
478 sw_error=1;
479 break;
480 }
481 errno = 0;
482 PyFPE_START_PROTECT("strtod", return 0)
483 z = strtod(s, &end) ;
484 PyFPE_END_PROTECT(z)
485 if (errno != 0) {
486 sprintf(buffer,
487 "float() out of range: %.150s", s);
488 PyErr_SetString(
489 PyExc_ValueError,
490 buffer);
491 return NULL;
492 }
493 s=end;
494 if (*s=='J' || *s=='j') {
495
496 break;
497 }
498 if (got_re) {
499 sw_error=1;
500 break;
501 }
502
503 /* accept a real part */
504 x=sign*z;
505 got_re=1;
506 if (got_im) done=1;
507 z = -1.0;
508 sign = 1;
509 break;
510
511 } /* end of switch */
512
513 } while (*s!='\0' && !sw_error);
514
515 if (sw_error) {
516 PyErr_SetString(PyExc_ValueError,
517 "malformed string for complex()");
518 return NULL;
519 }
520
521 return PyComplex_FromDoubles(x,y);
522}
523
524static PyObject *
Guido van Rossum8a5c5d21996-01-12 01:09:56 +0000525builtin_complex(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000526 PyObject *self;
527 PyObject *args;
Guido van Rossum8a5c5d21996-01-12 01:09:56 +0000528{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000529 PyObject *r, *i, *tmp;
530 PyNumberMethods *nbr, *nbi = NULL;
Guido van Rossum530956d1996-07-21 02:27:43 +0000531 Py_complex cr, ci;
Guido van Rossumc6472e91997-03-31 17:15:43 +0000532 int own_r = 0;
Guido van Rossum8a5c5d21996-01-12 01:09:56 +0000533
534 i = NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000535 if (!PyArg_ParseTuple(args, "O|O:complex", &r, &i))
Guido van Rossum8a5c5d21996-01-12 01:09:56 +0000536 return NULL;
Guido van Rossum11950231999-03-25 21:16:07 +0000537 if (PyString_Check(r))
538 return complex_from_string(r);
Guido van Rossum8a5c5d21996-01-12 01:09:56 +0000539 if ((nbr = r->ob_type->tp_as_number) == NULL ||
Guido van Rossumc6472e91997-03-31 17:15:43 +0000540 nbr->nb_float == NULL ||
541 (i != NULL &&
542 ((nbi = i->ob_type->tp_as_number) == NULL ||
543 nbi->nb_float == NULL))) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000544 PyErr_SetString(PyExc_TypeError,
Guido van Rossum8a5c5d21996-01-12 01:09:56 +0000545 "complex() argument can't be converted to complex");
546 return NULL;
547 }
Guido van Rossumed0af8f1996-12-05 23:18:18 +0000548 /* XXX Hack to support classes with __complex__ method */
Guido van Rossum79f25d91997-04-29 20:08:16 +0000549 if (PyInstance_Check(r)) {
550 static PyObject *complexstr;
551 PyObject *f;
Guido van Rossumed0af8f1996-12-05 23:18:18 +0000552 if (complexstr == NULL) {
Guido van Rossum8d751611997-01-18 08:04:16 +0000553 complexstr = PyString_InternFromString("__complex__");
Guido van Rossumed0af8f1996-12-05 23:18:18 +0000554 if (complexstr == NULL)
555 return NULL;
556 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000557 f = PyObject_GetAttr(r, complexstr);
Guido van Rossumed0af8f1996-12-05 23:18:18 +0000558 if (f == NULL)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000559 PyErr_Clear();
Guido van Rossumed0af8f1996-12-05 23:18:18 +0000560 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000561 PyObject *args = Py_BuildValue("()");
Guido van Rossumed0af8f1996-12-05 23:18:18 +0000562 if (args == NULL)
563 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000564 r = PyEval_CallObject(f, args);
565 Py_DECREF(args);
Barry Warsawf988e681999-01-27 23:13:59 +0000566 Py_DECREF(f);
Guido van Rossumed0af8f1996-12-05 23:18:18 +0000567 if (r == NULL)
568 return NULL;
Guido van Rossumc6472e91997-03-31 17:15:43 +0000569 own_r = 1;
Guido van Rossumed0af8f1996-12-05 23:18:18 +0000570 }
571 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000572 if (PyComplex_Check(r)) {
573 cr = ((PyComplexObject*)r)->cval;
Guido van Rossum730806d1998-04-10 22:27:42 +0000574 if (own_r) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000575 Py_DECREF(r);
Guido van Rossum730806d1998-04-10 22:27:42 +0000576 }
Guido van Rossumc6472e91997-03-31 17:15:43 +0000577 }
Guido van Rossum8a5c5d21996-01-12 01:09:56 +0000578 else {
Guido van Rossumfe4b6ee1996-08-08 18:49:41 +0000579 tmp = (*nbr->nb_float)(r);
Guido van Rossum730806d1998-04-10 22:27:42 +0000580 if (own_r) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000581 Py_DECREF(r);
Guido van Rossum730806d1998-04-10 22:27:42 +0000582 }
Guido van Rossumfe4b6ee1996-08-08 18:49:41 +0000583 if (tmp == NULL)
584 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000585 cr.real = PyFloat_AsDouble(tmp);
586 Py_DECREF(tmp);
Guido van Rossum11950231999-03-25 21:16:07 +0000587 cr.imag = 0.0;
Guido van Rossum8a5c5d21996-01-12 01:09:56 +0000588 }
589 if (i == NULL) {
Guido van Rossum11950231999-03-25 21:16:07 +0000590 ci.real = 0.0;
591 ci.imag = 0.0;
Guido van Rossum8a5c5d21996-01-12 01:09:56 +0000592 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000593 else if (PyComplex_Check(i))
594 ci = ((PyComplexObject*)i)->cval;
Guido van Rossum8a5c5d21996-01-12 01:09:56 +0000595 else {
Guido van Rossumc6472e91997-03-31 17:15:43 +0000596 tmp = (*nbi->nb_float)(i);
Guido van Rossumfe4b6ee1996-08-08 18:49:41 +0000597 if (tmp == NULL)
598 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000599 ci.real = PyFloat_AsDouble(tmp);
600 Py_DECREF(tmp);
Guido van Rossum8a5c5d21996-01-12 01:09:56 +0000601 ci.imag = 0.;
602 }
603 cr.real -= ci.imag;
604 cr.imag += ci.real;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000605 return PyComplex_FromCComplex(cr);
Guido van Rossum8a5c5d21996-01-12 01:09:56 +0000606}
607
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000608static char complex_doc[] =
609"complex(real[, imag]) -> complex number\n\
610\n\
611Create a complex number from a real part and an optional imaginary part.\n\
612This is equivalent to (real + imag*1j) where imag defaults to 0.";
613
614
Guido van Rossum8a5c5d21996-01-12 01:09:56 +0000615#endif
616
Guido van Rossum79f25d91997-04-29 20:08:16 +0000617static PyObject *
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000618builtin_dir(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000619 PyObject *self;
620 PyObject *args;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000621{
Guido van Rossum666b17a1997-05-06 16:36:57 +0000622 static char *attrlist[] = {"__members__", "__methods__", NULL};
623 PyObject *v = NULL, *l = NULL, *m = NULL;
624 PyObject *d, *x;
625 int i;
626 char **s;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000627
Guido van Rossum79f25d91997-04-29 20:08:16 +0000628 if (!PyArg_ParseTuple(args, "|O:dir", &v))
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000629 return NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000630 if (v == NULL) {
Guido van Rossum666b17a1997-05-06 16:36:57 +0000631 x = PyEval_GetLocals();
632 if (x == NULL)
633 goto error;
634 l = PyMapping_Keys(x);
635 if (l == NULL)
636 goto error;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000637 }
638 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000639 d = PyObject_GetAttrString(v, "__dict__");
Guido van Rossum666b17a1997-05-06 16:36:57 +0000640 if (d == NULL)
Guido van Rossum795ba581996-05-23 22:49:07 +0000641 PyErr_Clear();
Guido van Rossum666b17a1997-05-06 16:36:57 +0000642 else {
643 l = PyMapping_Keys(d);
644 if (l == NULL)
645 PyErr_Clear();
646 Py_DECREF(d);
647 }
648 if (l == NULL) {
649 l = PyList_New(0);
650 if (l == NULL)
651 goto error;
652 }
653 for (s = attrlist; *s != NULL; s++) {
654 m = PyObject_GetAttrString(v, *s);
655 if (m == NULL) {
656 PyErr_Clear();
657 continue;
658 }
659 for (i = 0; ; i++) {
660 x = PySequence_GetItem(m, i);
661 if (x == NULL) {
662 PyErr_Clear();
663 break;
664 }
665 if (PyList_Append(l, x) != 0) {
666 Py_DECREF(x);
667 Py_DECREF(m);
668 goto error;
669 }
670 Py_DECREF(x);
671 }
672 Py_DECREF(m);
Guido van Rossum795ba581996-05-23 22:49:07 +0000673 }
Guido van Rossum006bcd41991-10-24 14:54:44 +0000674 }
Guido van Rossum666b17a1997-05-06 16:36:57 +0000675 if (PyList_Sort(l) != 0)
676 goto error;
677 return l;
678 error:
679 Py_XDECREF(l);
680 return NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000681}
682
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000683static char dir_doc[] =
684"dir([object]) -> list of strings\n\
685\n\
686Return an alphabetized list of names comprising (some of) the attributes\n\
687of the given object. Without an argument, the names in the current scope\n\
688are listed. With an instance argument, only the instance attributes are\n\
689returned. With a class argument, attributes of the base class are not\n\
690returned. For other types or arguments, this may list members or methods.";
691
692
Guido van Rossum79f25d91997-04-29 20:08:16 +0000693static PyObject *
Guido van Rossum6a00cd81995-01-07 12:39:01 +0000694builtin_divmod(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000695 PyObject *self;
696 PyObject *args;
Guido van Rossum6a00cd81995-01-07 12:39:01 +0000697{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000698 PyObject *v, *w;
Guido van Rossum6a00cd81995-01-07 12:39:01 +0000699
Guido van Rossum79f25d91997-04-29 20:08:16 +0000700 if (!PyArg_ParseTuple(args, "OO:divmod", &v, &w))
Guido van Rossum6a00cd81995-01-07 12:39:01 +0000701 return NULL;
Guido van Rossum09df08a1998-05-22 00:51:39 +0000702 return PyNumber_Divmod(v, w);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000703}
704
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000705static char divmod_doc[] =
706"divmod(x, y) -> (div, mod)\n\
707\n\
708Return the tuple ((x-x%y)/y, x%y). Invariant: div*y + mod == x.";
709
710
Guido van Rossum79f25d91997-04-29 20:08:16 +0000711static PyObject *
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000712builtin_eval(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000713 PyObject *self;
714 PyObject *args;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000715{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000716 PyObject *cmd;
717 PyObject *globals = Py_None, *locals = Py_None;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000718 char *str;
Guido van Rossum590baa41993-11-30 13:40:46 +0000719
Guido van Rossum79f25d91997-04-29 20:08:16 +0000720 if (!PyArg_ParseTuple(args, "O|O!O!:eval",
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000721 &cmd,
Guido van Rossum79f25d91997-04-29 20:08:16 +0000722 &PyDict_Type, &globals,
723 &PyDict_Type, &locals))
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000724 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000725 if (globals == Py_None) {
726 globals = PyEval_GetGlobals();
727 if (locals == Py_None)
728 locals = PyEval_GetLocals();
Guido van Rossum6135a871995-01-09 17:53:26 +0000729 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000730 else if (locals == Py_None)
Guido van Rossum6135a871995-01-09 17:53:26 +0000731 locals = globals;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000732 if (PyDict_GetItemString(globals, "__builtins__") == NULL) {
733 if (PyDict_SetItemString(globals, "__builtins__",
734 PyEval_GetBuiltins()) != 0)
Guido van Rossum6135a871995-01-09 17:53:26 +0000735 return NULL;
736 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000737 if (PyCode_Check(cmd))
738 return PyEval_EvalCode((PyCodeObject *) cmd, globals, locals);
739 if (!PyString_Check(cmd)) {
740 PyErr_SetString(PyExc_TypeError,
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000741 "eval() argument 1 must be string or code object");
Guido van Rossum94390a41992-08-14 15:14:30 +0000742 return NULL;
743 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000744 str = PyString_AsString(cmd);
745 if ((int)strlen(str) != PyString_Size(cmd)) {
746 PyErr_SetString(PyExc_ValueError,
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000747 "embedded '\\0' in string arg");
748 return NULL;
Guido van Rossumf08ab0a1992-03-04 16:41:41 +0000749 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000750 while (*str == ' ' || *str == '\t')
751 str++;
Guido van Rossumb05a5c71997-05-07 17:46:13 +0000752 return PyRun_String(str, Py_eval_input, globals, locals);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000753}
754
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000755static char eval_doc[] =
756"eval(source[, globals[, locals]]) -> value\n\
757\n\
758Evaluate the source in the context of globals and locals.\n\
759The source may be a string representing a Python expression\n\
760or a code object as returned by compile().\n\
761The globals and locals are dictionaries, defaulting to the current\n\
762globals and locals. If only globals is given, locals defaults to it.";
763
764
Guido van Rossum79f25d91997-04-29 20:08:16 +0000765static PyObject *
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000766builtin_execfile(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000767 PyObject *self;
768 PyObject *args;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000769{
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000770 char *filename;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000771 PyObject *globals = Py_None, *locals = Py_None;
772 PyObject *res;
Guido van Rossum0f61f8a1992-02-25 18:55:05 +0000773 FILE* fp;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000774
Guido van Rossum79f25d91997-04-29 20:08:16 +0000775 if (!PyArg_ParseTuple(args, "s|O!O!:execfile",
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000776 &filename,
Guido van Rossum79f25d91997-04-29 20:08:16 +0000777 &PyDict_Type, &globals,
778 &PyDict_Type, &locals))
Guido van Rossum0f61f8a1992-02-25 18:55:05 +0000779 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000780 if (globals == Py_None) {
781 globals = PyEval_GetGlobals();
782 if (locals == Py_None)
783 locals = PyEval_GetLocals();
Guido van Rossum6135a871995-01-09 17:53:26 +0000784 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000785 else if (locals == Py_None)
Guido van Rossum6135a871995-01-09 17:53:26 +0000786 locals = globals;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000787 if (PyDict_GetItemString(globals, "__builtins__") == NULL) {
788 if (PyDict_SetItemString(globals, "__builtins__",
789 PyEval_GetBuiltins()) != 0)
Guido van Rossum6135a871995-01-09 17:53:26 +0000790 return NULL;
791 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000792 Py_BEGIN_ALLOW_THREADS
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000793 fp = fopen(filename, "r");
Guido van Rossum79f25d91997-04-29 20:08:16 +0000794 Py_END_ALLOW_THREADS
Guido van Rossum0f61f8a1992-02-25 18:55:05 +0000795 if (fp == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000796 PyErr_SetFromErrno(PyExc_IOError);
Guido van Rossum0f61f8a1992-02-25 18:55:05 +0000797 return NULL;
798 }
Guido van Rossumb05a5c71997-05-07 17:46:13 +0000799 res = PyRun_File(fp, filename, Py_file_input, globals, locals);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000800 Py_BEGIN_ALLOW_THREADS
Guido van Rossum0f61f8a1992-02-25 18:55:05 +0000801 fclose(fp);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000802 Py_END_ALLOW_THREADS
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000803 return res;
Guido van Rossum0f61f8a1992-02-25 18:55:05 +0000804}
805
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000806static char execfile_doc[] =
807"execfile(filename[, globals[, locals]])\n\
808\n\
809Read and execute a Python script from a file.\n\
810The globals and locals are dictionaries, defaulting to the current\n\
811globals and locals. If only globals is given, locals defaults to it.";
812
813
Guido van Rossum79f25d91997-04-29 20:08:16 +0000814static PyObject *
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000815builtin_float(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000816 PyObject *self;
817 PyObject *args;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000818{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000819 PyObject *v;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000820
Guido van Rossum79f25d91997-04-29 20:08:16 +0000821 if (!PyArg_ParseTuple(args, "O:float", &v))
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000822 return NULL;
Guido van Rossum09df08a1998-05-22 00:51:39 +0000823 return PyNumber_Float(v);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000824}
825
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000826static char float_doc[] =
827"float(x) -> floating point number\n\
828\n\
829Convert a string or number to a floating point number, if possible.";
830
831
Guido van Rossum79f25d91997-04-29 20:08:16 +0000832static PyObject *
Guido van Rossum94390a41992-08-14 15:14:30 +0000833builtin_getattr(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000834 PyObject *self;
835 PyObject *args;
Guido van Rossum33894be1992-01-27 16:53:09 +0000836{
Guido van Rossum950ff291998-06-29 13:38:57 +0000837 PyObject *v, *result, *dflt = NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000838 PyObject *name;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000839
Guido van Rossum950ff291998-06-29 13:38:57 +0000840 if (!PyArg_ParseTuple(args, "OS|O:getattr", &v, &name, &dflt))
Guido van Rossum33894be1992-01-27 16:53:09 +0000841 return NULL;
Guido van Rossum950ff291998-06-29 13:38:57 +0000842 result = PyObject_GetAttr(v, name);
843 if (result == NULL && dflt != NULL) {
844 PyErr_Clear();
845 Py_INCREF(dflt);
846 result = dflt;
847 }
848 return result;
Guido van Rossum9bfef441993-03-29 10:43:31 +0000849}
850
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000851static char getattr_doc[] =
Guido van Rossum950ff291998-06-29 13:38:57 +0000852"getattr(object, name[, default]) -> value\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000853\n\
Guido van Rossum950ff291998-06-29 13:38:57 +0000854Get a named attribute from an object; getattr(x, 'y') is equivalent to x.y.\n\
855When a default argument is given, it is returned when the attribute doesn't\n\
856exist; without it, an exception is raised in that case.";
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000857
858
Guido van Rossum79f25d91997-04-29 20:08:16 +0000859static PyObject *
Guido van Rossum872537c1995-07-07 22:43:42 +0000860builtin_globals(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000861 PyObject *self;
862 PyObject *args;
Guido van Rossum872537c1995-07-07 22:43:42 +0000863{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000864 PyObject *d;
Guido van Rossum872537c1995-07-07 22:43:42 +0000865
Guido van Rossum79f25d91997-04-29 20:08:16 +0000866 if (!PyArg_ParseTuple(args, ""))
Guido van Rossum872537c1995-07-07 22:43:42 +0000867 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000868 d = PyEval_GetGlobals();
869 Py_INCREF(d);
Guido van Rossum872537c1995-07-07 22:43:42 +0000870 return d;
871}
872
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000873static char globals_doc[] =
874"globals() -> dictionary\n\
875\n\
876Return the dictionary containing the current scope's global variables.";
877
878
Guido van Rossum79f25d91997-04-29 20:08:16 +0000879static PyObject *
Guido van Rossum9bfef441993-03-29 10:43:31 +0000880builtin_hasattr(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000881 PyObject *self;
882 PyObject *args;
Guido van Rossum9bfef441993-03-29 10:43:31 +0000883{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000884 PyObject *v;
885 PyObject *name;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000886
Guido van Rossum79f25d91997-04-29 20:08:16 +0000887 if (!PyArg_ParseTuple(args, "OS:hasattr", &v, &name))
Guido van Rossum9bfef441993-03-29 10:43:31 +0000888 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000889 v = PyObject_GetAttr(v, name);
Guido van Rossum9bfef441993-03-29 10:43:31 +0000890 if (v == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000891 PyErr_Clear();
Guido van Rossum09df08a1998-05-22 00:51:39 +0000892 Py_INCREF(Py_False);
893 return Py_False;
Guido van Rossum9bfef441993-03-29 10:43:31 +0000894 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000895 Py_DECREF(v);
Guido van Rossum09df08a1998-05-22 00:51:39 +0000896 Py_INCREF(Py_True);
897 return Py_True;
Guido van Rossum33894be1992-01-27 16:53:09 +0000898}
899
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000900static char hasattr_doc[] =
901"hasattr(object, name) -> Boolean\n\
902\n\
903Return whether the object has an attribute with the given name.\n\
904(This is done by calling getattr(object, name) and catching exceptions.)";
905
906
Guido van Rossum79f25d91997-04-29 20:08:16 +0000907static PyObject *
Guido van Rossum5b722181993-03-30 17:46:03 +0000908builtin_id(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000909 PyObject *self;
910 PyObject *args;
Guido van Rossum5b722181993-03-30 17:46:03 +0000911{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000912 PyObject *v;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000913
Guido van Rossum79f25d91997-04-29 20:08:16 +0000914 if (!PyArg_ParseTuple(args, "O:id", &v))
Guido van Rossum5b722181993-03-30 17:46:03 +0000915 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000916 return PyInt_FromLong((long)v);
Guido van Rossum5b722181993-03-30 17:46:03 +0000917}
918
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000919static char id_doc[] =
920"id(object) -> integer\n\
921\n\
922Return the identity of an object. This is guaranteed to be unique among\n\
923simultaneously existing objects. (Hint: it's the object's memory address.)";
924
925
Guido van Rossum79f25d91997-04-29 20:08:16 +0000926static PyObject *
Guido van Rossum12d12c51993-10-26 17:58:25 +0000927builtin_map(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000928 PyObject *self;
929 PyObject *args;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000930{
931 typedef struct {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000932 PyObject *seq;
933 PySequenceMethods *sqf;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000934 int len;
935 } sequence;
936
Guido van Rossum79f25d91997-04-29 20:08:16 +0000937 PyObject *func, *result;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000938 sequence *seqs = NULL, *sqp;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000939 int n, len;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000940 register int i, j;
941
Guido van Rossum79f25d91997-04-29 20:08:16 +0000942 n = PyTuple_Size(args);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000943 if (n < 2) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000944 PyErr_SetString(PyExc_TypeError,
945 "map() requires at least two args");
Guido van Rossum12d12c51993-10-26 17:58:25 +0000946 return NULL;
947 }
948
Guido van Rossum79f25d91997-04-29 20:08:16 +0000949 func = PyTuple_GetItem(args, 0);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000950 n--;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000951
Guido van Rossumfa4ac711998-07-10 17:37:30 +0000952 if (func == Py_None && n == 1) {
953 /* map(None, S) is the same as list(S). */
954 return PySequence_List(PyTuple_GetItem(args, 1));
955 }
956
Guido van Rossum79f25d91997-04-29 20:08:16 +0000957 if ((seqs = PyMem_NEW(sequence, n)) == NULL) {
958 PyErr_NoMemory();
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000959 goto Fail_2;
960 }
Guido van Rossum12d12c51993-10-26 17:58:25 +0000961
Guido van Rossum2d951851994-08-29 12:52:16 +0000962 for (len = 0, i = 0, sqp = seqs; i < n; ++i, ++sqp) {
Guido van Rossum12d12c51993-10-26 17:58:25 +0000963 int curlen;
Guido van Rossum09df08a1998-05-22 00:51:39 +0000964 PySequenceMethods *sqf;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000965
Guido van Rossum79f25d91997-04-29 20:08:16 +0000966 if ((sqp->seq = PyTuple_GetItem(args, i + 1)) == NULL)
Guido van Rossum12d12c51993-10-26 17:58:25 +0000967 goto Fail_2;
968
Guido van Rossum09df08a1998-05-22 00:51:39 +0000969 sqp->sqf = sqf = sqp->seq->ob_type->tp_as_sequence;
970 if (sqf == NULL ||
971 sqf->sq_length == NULL ||
972 sqf->sq_item == NULL)
973 {
Guido van Rossum12d12c51993-10-26 17:58:25 +0000974 static char errmsg[] =
975 "argument %d to map() must be a sequence object";
Guido van Rossum15e33a41997-04-30 19:00:27 +0000976 char errbuf[sizeof(errmsg) + 25];
Guido van Rossum12d12c51993-10-26 17:58:25 +0000977
978 sprintf(errbuf, errmsg, i+2);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000979 PyErr_SetString(PyExc_TypeError, errbuf);
Guido van Rossum12d12c51993-10-26 17:58:25 +0000980 goto Fail_2;
981 }
982
983 if ((curlen = sqp->len = (*sqp->sqf->sq_length)(sqp->seq)) < 0)
984 goto Fail_2;
985
986 if (curlen > len)
987 len = curlen;
988 }
989
Guido van Rossum79f25d91997-04-29 20:08:16 +0000990 if ((result = (PyObject *) PyList_New(len)) == NULL)
Guido van Rossum12d12c51993-10-26 17:58:25 +0000991 goto Fail_2;
992
Guido van Rossum2d951851994-08-29 12:52:16 +0000993 for (i = 0; ; ++i) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000994 PyObject *alist, *item=NULL, *value;
Guido van Rossum2d951851994-08-29 12:52:16 +0000995 int any = 0;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000996
Guido van Rossum79f25d91997-04-29 20:08:16 +0000997 if (func == Py_None && n == 1)
Guido van Rossum32120311995-07-10 13:52:21 +0000998 alist = NULL;
Guido van Rossum2d951851994-08-29 12:52:16 +0000999 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001000 if ((alist = PyTuple_New(n)) == NULL)
Guido van Rossum2d951851994-08-29 12:52:16 +00001001 goto Fail_1;
1002 }
Guido van Rossum12d12c51993-10-26 17:58:25 +00001003
1004 for (j = 0, sqp = seqs; j < n; ++j, ++sqp) {
Guido van Rossum2d951851994-08-29 12:52:16 +00001005 if (sqp->len < 0) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001006 Py_INCREF(Py_None);
1007 item = Py_None;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001008 }
Guido van Rossum12d12c51993-10-26 17:58:25 +00001009 else {
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00001010 item = (*sqp->sqf->sq_item)(sqp->seq, i);
Guido van Rossum2d951851994-08-29 12:52:16 +00001011 if (item == NULL) {
Barry Warsawcde8b1b1997-08-22 21:14:38 +00001012 if (PyErr_ExceptionMatches(
1013 PyExc_IndexError))
1014 {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001015 PyErr_Clear();
1016 Py_INCREF(Py_None);
1017 item = Py_None;
Guido van Rossum2d951851994-08-29 12:52:16 +00001018 sqp->len = -1;
1019 }
1020 else {
1021 goto Fail_0;
1022 }
1023 }
1024 else
1025 any = 1;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001026
Guido van Rossum12d12c51993-10-26 17:58:25 +00001027 }
Guido van Rossum32120311995-07-10 13:52:21 +00001028 if (!alist)
Guido van Rossum2d951851994-08-29 12:52:16 +00001029 break;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001030 if (PyTuple_SetItem(alist, j, item) < 0) {
1031 Py_DECREF(item);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00001032 goto Fail_0;
Guido van Rossum2d951851994-08-29 12:52:16 +00001033 }
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00001034 continue;
1035
1036 Fail_0:
Guido van Rossum79f25d91997-04-29 20:08:16 +00001037 Py_XDECREF(alist);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00001038 goto Fail_1;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001039 }
1040
Guido van Rossum32120311995-07-10 13:52:21 +00001041 if (!alist)
1042 alist = item;
Guido van Rossum2d951851994-08-29 12:52:16 +00001043
1044 if (!any) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001045 Py_DECREF(alist);
Guido van Rossum2d951851994-08-29 12:52:16 +00001046 break;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001047 }
Guido van Rossum2d951851994-08-29 12:52:16 +00001048
Guido van Rossum79f25d91997-04-29 20:08:16 +00001049 if (func == Py_None)
Guido van Rossum32120311995-07-10 13:52:21 +00001050 value = alist;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001051 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001052 value = PyEval_CallObject(func, alist);
1053 Py_DECREF(alist);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00001054 if (value == NULL)
1055 goto Fail_1;
Guido van Rossum2d951851994-08-29 12:52:16 +00001056 }
1057 if (i >= len) {
Barry Warsawfa77e091999-01-28 18:49:12 +00001058 int status = PyList_Append(result, value);
Barry Warsaw21332871999-01-28 04:21:35 +00001059 Py_DECREF(value);
Barry Warsawfa77e091999-01-28 18:49:12 +00001060 if (status < 0)
1061 goto Fail_1;
Guido van Rossum2d951851994-08-29 12:52:16 +00001062 }
1063 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001064 if (PyList_SetItem(result, i, value) < 0)
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00001065 goto Fail_1;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001066 }
1067 }
1068
Guido van Rossumfa4ac711998-07-10 17:37:30 +00001069 if (i < len && PyList_SetSlice(result, i, len, NULL) < 0)
1070 goto Fail_1;
1071
Guido van Rossum79f25d91997-04-29 20:08:16 +00001072 PyMem_DEL(seqs);
Guido van Rossum12d12c51993-10-26 17:58:25 +00001073 return result;
1074
Guido van Rossum12d12c51993-10-26 17:58:25 +00001075Fail_1:
Guido van Rossum79f25d91997-04-29 20:08:16 +00001076 Py_DECREF(result);
Guido van Rossum12d12c51993-10-26 17:58:25 +00001077Fail_2:
Guido van Rossum79f25d91997-04-29 20:08:16 +00001078 if (seqs) PyMem_DEL(seqs);
Guido van Rossum12d12c51993-10-26 17:58:25 +00001079 return NULL;
1080}
1081
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001082static char map_doc[] =
1083"map(function, sequence[, sequence, ...]) -> list\n\
1084\n\
1085Return a list of the results of applying the function to the items of\n\
1086the argument sequence(s). If more than one sequence is given, the\n\
1087function is called with an argument list consisting of the corresponding\n\
1088item of each sequence, substituting None for missing values when not all\n\
1089sequences have the same length. If the function is None, return a list of\n\
1090the items of the sequence (or a list of tuples if more than one sequence).";
1091
1092
Guido van Rossum79f25d91997-04-29 20:08:16 +00001093static PyObject *
Guido van Rossum94390a41992-08-14 15:14:30 +00001094builtin_setattr(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001095 PyObject *self;
1096 PyObject *args;
Guido van Rossum33894be1992-01-27 16:53:09 +00001097{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001098 PyObject *v;
1099 PyObject *name;
1100 PyObject *value;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001101
Guido van Rossum79f25d91997-04-29 20:08:16 +00001102 if (!PyArg_ParseTuple(args, "OSO:setattr", &v, &name, &value))
Guido van Rossum33894be1992-01-27 16:53:09 +00001103 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001104 if (PyObject_SetAttr(v, name, value) != 0)
Guido van Rossum33894be1992-01-27 16:53:09 +00001105 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001106 Py_INCREF(Py_None);
1107 return Py_None;
Guido van Rossum33894be1992-01-27 16:53:09 +00001108}
1109
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001110static char setattr_doc[] =
1111"setattr(object, name, value)\n\
1112\n\
1113Set a named attribute on an object; setattr(x, 'y', v) is equivalent to\n\
1114``x.y = v''.";
1115
1116
Guido van Rossum79f25d91997-04-29 20:08:16 +00001117static PyObject *
Guido van Rossum14144fc1994-08-29 12:53:40 +00001118builtin_delattr(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001119 PyObject *self;
1120 PyObject *args;
Guido van Rossum14144fc1994-08-29 12:53:40 +00001121{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001122 PyObject *v;
1123 PyObject *name;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001124
Guido van Rossum79f25d91997-04-29 20:08:16 +00001125 if (!PyArg_ParseTuple(args, "OS:delattr", &v, &name))
Guido van Rossum14144fc1994-08-29 12:53:40 +00001126 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001127 if (PyObject_SetAttr(v, name, (PyObject *)NULL) != 0)
Guido van Rossum14144fc1994-08-29 12:53:40 +00001128 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001129 Py_INCREF(Py_None);
1130 return Py_None;
Guido van Rossum14144fc1994-08-29 12:53:40 +00001131}
1132
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001133static char delattr_doc[] =
Guido van Rossumdf12a591998-11-23 22:13:04 +00001134"delattr(object, name)\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001135\n\
1136Delete a named attribute on an object; delattr(x, 'y') is equivalent to\n\
1137``del x.y''.";
1138
1139
Guido van Rossum79f25d91997-04-29 20:08:16 +00001140static PyObject *
Guido van Rossum9bfef441993-03-29 10:43:31 +00001141builtin_hash(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001142 PyObject *self;
1143 PyObject *args;
Guido van Rossum9bfef441993-03-29 10:43:31 +00001144{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001145 PyObject *v;
Guido van Rossum9bfef441993-03-29 10:43:31 +00001146 long x;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001147
Guido van Rossum79f25d91997-04-29 20:08:16 +00001148 if (!PyArg_ParseTuple(args, "O:hash", &v))
Guido van Rossum9bfef441993-03-29 10:43:31 +00001149 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001150 x = PyObject_Hash(v);
Guido van Rossum9bfef441993-03-29 10:43:31 +00001151 if (x == -1)
1152 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001153 return PyInt_FromLong(x);
Guido van Rossum9bfef441993-03-29 10:43:31 +00001154}
1155
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001156static char hash_doc[] =
1157"hash(object) -> integer\n\
1158\n\
1159Return a hash value for the object. Two objects with the same value have\n\
1160the same hash value. The reverse is not necessarily true, but likely.";
1161
1162
Guido van Rossum79f25d91997-04-29 20:08:16 +00001163static PyObject *
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001164builtin_hex(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001165 PyObject *self;
1166 PyObject *args;
Guido van Rossum006bcd41991-10-24 14:54:44 +00001167{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001168 PyObject *v;
1169 PyNumberMethods *nb;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001170
Guido van Rossum79f25d91997-04-29 20:08:16 +00001171 if (!PyArg_ParseTuple(args, "O:hex", &v))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001172 return NULL;
Guido van Rossum1899c2e1992-09-12 11:09:23 +00001173
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001174 if ((nb = v->ob_type->tp_as_number) == NULL ||
Guido van Rossum1899c2e1992-09-12 11:09:23 +00001175 nb->nb_hex == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001176 PyErr_SetString(PyExc_TypeError,
Guido van Rossum1899c2e1992-09-12 11:09:23 +00001177 "hex() argument can't be converted to hex");
1178 return NULL;
Guido van Rossum006bcd41991-10-24 14:54:44 +00001179 }
Guido van Rossum1899c2e1992-09-12 11:09:23 +00001180 return (*nb->nb_hex)(v);
Guido van Rossum006bcd41991-10-24 14:54:44 +00001181}
1182
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001183static char hex_doc[] =
1184"hex(number) -> string\n\
1185\n\
1186Return the hexadecimal representation of an integer or long integer.";
1187
1188
Guido van Rossum79f25d91997-04-29 20:08:16 +00001189static PyObject *builtin_raw_input Py_PROTO((PyObject *, PyObject *));
Guido van Rossum3165fe61992-09-25 21:59:05 +00001190
Guido van Rossum79f25d91997-04-29 20:08:16 +00001191static PyObject *
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001192builtin_input(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001193 PyObject *self;
1194 PyObject *args;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001195{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001196 PyObject *line;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001197 char *str;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001198 PyObject *res;
1199 PyObject *globals, *locals;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001200
1201 line = builtin_raw_input(self, args);
Guido van Rossum3165fe61992-09-25 21:59:05 +00001202 if (line == NULL)
1203 return line;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001204 if (!PyArg_Parse(line, "s;embedded '\\0' in input line", &str))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001205 return NULL;
1206 while (*str == ' ' || *str == '\t')
1207 str++;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001208 globals = PyEval_GetGlobals();
1209 locals = PyEval_GetLocals();
1210 if (PyDict_GetItemString(globals, "__builtins__") == NULL) {
1211 if (PyDict_SetItemString(globals, "__builtins__",
1212 PyEval_GetBuiltins()) != 0)
Guido van Rossum6135a871995-01-09 17:53:26 +00001213 return NULL;
1214 }
Guido van Rossumb05a5c71997-05-07 17:46:13 +00001215 res = PyRun_String(str, Py_eval_input, globals, locals);
Guido van Rossum79f25d91997-04-29 20:08:16 +00001216 Py_DECREF(line);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001217 return res;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001218}
1219
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001220static char input_doc[] =
1221"input([prompt]) -> value\n\
1222\n\
1223Equivalent to eval(raw_input(prompt)).";
1224
1225
Guido van Rossume8811f81997-02-14 15:48:05 +00001226static PyObject *
1227builtin_intern(self, args)
1228 PyObject *self;
1229 PyObject *args;
1230{
1231 PyObject *s;
1232 if (!PyArg_ParseTuple(args, "S", &s))
1233 return NULL;
1234 Py_INCREF(s);
1235 PyString_InternInPlace(&s);
1236 return s;
1237}
1238
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001239static char intern_doc[] =
1240"intern(string) -> string\n\
1241\n\
1242``Intern'' the given string. This enters the string in the (global)\n\
1243table of interned strings whose purpose is to speed up dictionary lookups.\n\
1244Return the string itself or the previously interned string object with the\n\
1245same value.";
1246
1247
Guido van Rossum79f25d91997-04-29 20:08:16 +00001248static PyObject *
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001249builtin_int(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001250 PyObject *self;
1251 PyObject *args;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001252{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001253 PyObject *v;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001254
Guido van Rossum79f25d91997-04-29 20:08:16 +00001255 if (!PyArg_ParseTuple(args, "O:int", &v))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001256 return NULL;
Guido van Rossum09df08a1998-05-22 00:51:39 +00001257 return PyNumber_Int(v);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001258}
1259
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001260static char int_doc[] =
1261"int(x) -> integer\n\
1262\n\
1263Convert a string or number to an integer, if possible.\n\
1264A floating point argument will be truncated towards zero.";
1265
1266
Guido van Rossum79f25d91997-04-29 20:08:16 +00001267static PyObject *
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001268builtin_len(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001269 PyObject *self;
1270 PyObject *args;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001271{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001272 PyObject *v;
Guido van Rossum8ea9f4d1998-06-29 22:26:50 +00001273 long res;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001274
Guido van Rossum79f25d91997-04-29 20:08:16 +00001275 if (!PyArg_ParseTuple(args, "O:len", &v))
Guido van Rossum3f5da241990-12-20 15:06:42 +00001276 return NULL;
Guido van Rossum8ea9f4d1998-06-29 22:26:50 +00001277 res = PyObject_Length(v);
1278 if (res < 0 && PyErr_Occurred())
1279 return NULL;
1280 return PyInt_FromLong(res);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001281}
1282
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001283static char len_doc[] =
1284"len(object) -> integer\n\
1285\n\
1286Return the number of items of a sequence or mapping.";
1287
1288
Guido van Rossum79f25d91997-04-29 20:08:16 +00001289static PyObject *
Guido van Rossumd1705771996-04-09 02:41:06 +00001290builtin_list(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001291 PyObject *self;
1292 PyObject *args;
Guido van Rossumd1705771996-04-09 02:41:06 +00001293{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001294 PyObject *v;
Guido van Rossumd1705771996-04-09 02:41:06 +00001295
Guido van Rossum79f25d91997-04-29 20:08:16 +00001296 if (!PyArg_ParseTuple(args, "O:list", &v))
Guido van Rossumd1705771996-04-09 02:41:06 +00001297 return NULL;
Guido van Rossum09df08a1998-05-22 00:51:39 +00001298 return PySequence_List(v);
Guido van Rossumd1705771996-04-09 02:41:06 +00001299}
1300
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001301static char list_doc[] =
1302"list(sequence) -> list\n\
1303\n\
1304Return a new list whose items are the same as those of the argument sequence.";
1305
Guido van Rossum8861b741996-07-30 16:49:37 +00001306
1307static PyObject *
1308builtin_slice(self, args)
1309 PyObject *self;
1310 PyObject *args;
1311{
Guido van Rossum09df08a1998-05-22 00:51:39 +00001312 PyObject *start, *stop, *step;
Guido van Rossum8861b741996-07-30 16:49:37 +00001313
Guido van Rossum09df08a1998-05-22 00:51:39 +00001314 start = stop = step = NULL;
Guido van Rossum8861b741996-07-30 16:49:37 +00001315
Guido van Rossum09df08a1998-05-22 00:51:39 +00001316 if (!PyArg_ParseTuple(args, "O|OO:slice", &start, &stop, &step))
1317 return NULL;
Guido van Rossum8861b741996-07-30 16:49:37 +00001318
Guido van Rossum09df08a1998-05-22 00:51:39 +00001319 /* This swapping of stop and start is to maintain similarity with
1320 range(). */
1321 if (stop == NULL) {
1322 stop = start;
1323 start = NULL;
1324 }
1325 return PySlice_New(start, stop, step);
Guido van Rossum8861b741996-07-30 16:49:37 +00001326}
1327
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001328static char slice_doc[] =
1329"slice([start,] step[, stop]) -> slice object\n\
1330\n\
1331Create a slice object. This is used for slicing by the Numeric extensions.";
1332
1333
Guido van Rossum79f25d91997-04-29 20:08:16 +00001334static PyObject *
Guido van Rossum872537c1995-07-07 22:43:42 +00001335builtin_locals(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001336 PyObject *self;
1337 PyObject *args;
Guido van Rossum872537c1995-07-07 22:43:42 +00001338{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001339 PyObject *d;
Guido van Rossum872537c1995-07-07 22:43:42 +00001340
Guido van Rossum79f25d91997-04-29 20:08:16 +00001341 if (!PyArg_ParseTuple(args, ""))
Guido van Rossum872537c1995-07-07 22:43:42 +00001342 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001343 d = PyEval_GetLocals();
1344 Py_INCREF(d);
Guido van Rossum872537c1995-07-07 22:43:42 +00001345 return d;
1346}
1347
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001348static char locals_doc[] =
1349"locals() -> dictionary\n\
1350\n\
1351Return the dictionary containing the current scope's local variables.";
1352
1353
Guido van Rossum79f25d91997-04-29 20:08:16 +00001354static PyObject *
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001355builtin_long(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001356 PyObject *self;
1357 PyObject *args;
Guido van Rossumd4905451991-05-05 20:00:36 +00001358{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001359 PyObject *v;
Guido van Rossum1899c2e1992-09-12 11:09:23 +00001360
Guido van Rossum79f25d91997-04-29 20:08:16 +00001361 if (!PyArg_ParseTuple(args, "O:long", &v))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001362 return NULL;
Guido van Rossum09df08a1998-05-22 00:51:39 +00001363 return PyNumber_Long(v);
Guido van Rossumd4905451991-05-05 20:00:36 +00001364}
1365
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001366static char long_doc[] =
1367"long(x) -> long integer\n\
1368\n\
1369Convert a string or number to a long integer, if possible.\n\
1370A floating point argument will be truncated towards zero.";
1371
1372
Guido van Rossum79f25d91997-04-29 20:08:16 +00001373static PyObject *
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001374min_max(args, sign)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001375 PyObject *args;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001376 int sign;
1377{
Guido van Rossum2d951851994-08-29 12:52:16 +00001378 int i;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001379 PyObject *v, *w, *x;
1380 PySequenceMethods *sq;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001381
Guido van Rossum79f25d91997-04-29 20:08:16 +00001382 if (PyTuple_Size(args) > 1)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001383 v = args;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001384 else if (!PyArg_ParseTuple(args, "O:min/max", &v))
Guido van Rossum3f5da241990-12-20 15:06:42 +00001385 return NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001386 sq = v->ob_type->tp_as_sequence;
Guido van Rossum09df08a1998-05-22 00:51:39 +00001387 if (sq == NULL || sq->sq_item == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001388 PyErr_SetString(PyExc_TypeError,
1389 "min() or max() of non-sequence");
Guido van Rossum3f5da241990-12-20 15:06:42 +00001390 return NULL;
1391 }
Guido van Rossum2d951851994-08-29 12:52:16 +00001392 w = NULL;
1393 for (i = 0; ; i++) {
Guido van Rossum3f5da241990-12-20 15:06:42 +00001394 x = (*sq->sq_item)(v, i); /* Implies INCREF */
Guido van Rossum2d951851994-08-29 12:52:16 +00001395 if (x == NULL) {
Barry Warsawcde8b1b1997-08-22 21:14:38 +00001396 if (PyErr_ExceptionMatches(PyExc_IndexError)) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001397 PyErr_Clear();
Guido van Rossum2d951851994-08-29 12:52:16 +00001398 break;
1399 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00001400 Py_XDECREF(w);
Guido van Rossum2d951851994-08-29 12:52:16 +00001401 return NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001402 }
Guido van Rossum2d951851994-08-29 12:52:16 +00001403 if (w == NULL)
1404 w = x;
1405 else {
Guido van Rossumc8b6df91997-05-23 00:06:51 +00001406 int c = PyObject_Compare(x, w);
1407 if (c && PyErr_Occurred()) {
1408 Py_DECREF(x);
1409 Py_XDECREF(w);
1410 return NULL;
1411 }
1412 if (c * sign > 0) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001413 Py_DECREF(w);
Guido van Rossum2d951851994-08-29 12:52:16 +00001414 w = x;
1415 }
1416 else
Guido van Rossum79f25d91997-04-29 20:08:16 +00001417 Py_DECREF(x);
Guido van Rossum2d951851994-08-29 12:52:16 +00001418 }
Guido van Rossum3f5da241990-12-20 15:06:42 +00001419 }
Guido van Rossum2d951851994-08-29 12:52:16 +00001420 if (w == NULL)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001421 PyErr_SetString(PyExc_ValueError,
1422 "min() or max() of empty sequence");
Guido van Rossum3f5da241990-12-20 15:06:42 +00001423 return w;
1424}
1425
Guido van Rossum79f25d91997-04-29 20:08:16 +00001426static PyObject *
Guido van Rossum3f5da241990-12-20 15:06:42 +00001427builtin_min(self, v)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001428 PyObject *self;
1429 PyObject *v;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001430{
1431 return min_max(v, -1);
1432}
1433
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001434static char min_doc[] =
1435"min(sequence) -> value\n\
1436min(a, b, c, ...) -> value\n\
1437\n\
1438With a single sequence argument, return its smallest item.\n\
1439With two or more arguments, return the smallest argument.";
1440
1441
Guido van Rossum79f25d91997-04-29 20:08:16 +00001442static PyObject *
Guido van Rossum3f5da241990-12-20 15:06:42 +00001443builtin_max(self, v)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001444 PyObject *self;
1445 PyObject *v;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001446{
1447 return min_max(v, 1);
1448}
1449
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001450static char max_doc[] =
1451"max(sequence) -> value\n\
1452max(a, b, c, ...) -> value\n\
1453\n\
1454With a single sequence argument, return its largest item.\n\
1455With two or more arguments, return the largest argument.";
1456
1457
Guido van Rossum79f25d91997-04-29 20:08:16 +00001458static PyObject *
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001459builtin_oct(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001460 PyObject *self;
1461 PyObject *args;
Guido van Rossum006bcd41991-10-24 14:54:44 +00001462{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001463 PyObject *v;
1464 PyNumberMethods *nb;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001465
Guido van Rossum79f25d91997-04-29 20:08:16 +00001466 if (!PyArg_ParseTuple(args, "O:oct", &v))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001467 return NULL;
Guido van Rossum1899c2e1992-09-12 11:09:23 +00001468 if (v == NULL || (nb = v->ob_type->tp_as_number) == NULL ||
1469 nb->nb_oct == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001470 PyErr_SetString(PyExc_TypeError,
Guido van Rossum1899c2e1992-09-12 11:09:23 +00001471 "oct() argument can't be converted to oct");
1472 return NULL;
Guido van Rossum006bcd41991-10-24 14:54:44 +00001473 }
Guido van Rossum1899c2e1992-09-12 11:09:23 +00001474 return (*nb->nb_oct)(v);
Guido van Rossum006bcd41991-10-24 14:54:44 +00001475}
1476
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001477static char oct_doc[] =
1478"oct(number) -> string\n\
1479\n\
1480Return the octal representation of an integer or long integer.";
1481
1482
Guido van Rossum79f25d91997-04-29 20:08:16 +00001483static PyObject *
Guido van Rossum94390a41992-08-14 15:14:30 +00001484builtin_open(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001485 PyObject *self;
1486 PyObject *args;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001487{
Guido van Rossum2d951851994-08-29 12:52:16 +00001488 char *name;
1489 char *mode = "r";
1490 int bufsize = -1;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001491 PyObject *f;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001492
Guido van Rossum79f25d91997-04-29 20:08:16 +00001493 if (!PyArg_ParseTuple(args, "s|si:open", &name, &mode, &bufsize))
Guido van Rossum3f5da241990-12-20 15:06:42 +00001494 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001495 f = PyFile_FromString(name, mode);
Guido van Rossum2d951851994-08-29 12:52:16 +00001496 if (f != NULL)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001497 PyFile_SetBufSize(f, bufsize);
Guido van Rossum2d951851994-08-29 12:52:16 +00001498 return f;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001499}
1500
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001501static char open_doc[] =
1502"open(filename[, mode[, buffering]]) -> file object\n\
1503\n\
1504Open a file. The mode can be 'r', 'w' or 'a' for reading (default),\n\
1505writing or appending. The file will be created if it doesn't exist\n\
1506when opened for writing or appending; it will be truncated when\n\
1507opened for writing. Add a 'b' to the mode for binary files.\n\
1508Add a '+' to the mode to allow simultaneous reading and writing.\n\
1509If the buffering argument is given, 0 means unbuffered, 1 means line\n\
1510buffered, and larger numbers specify the buffer size.";
1511
1512
Guido van Rossum79f25d91997-04-29 20:08:16 +00001513static PyObject *
Guido van Rossum94390a41992-08-14 15:14:30 +00001514builtin_ord(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001515 PyObject *self;
1516 PyObject *args;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001517{
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001518 char c;
1519
Guido van Rossum79f25d91997-04-29 20:08:16 +00001520 if (!PyArg_ParseTuple(args, "c:ord", &c))
Guido van Rossum3f5da241990-12-20 15:06:42 +00001521 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001522 return PyInt_FromLong((long)(c & 0xff));
Guido van Rossum3f5da241990-12-20 15:06:42 +00001523}
1524
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001525static char ord_doc[] =
1526"ord(c) -> integer\n\
1527\n\
1528Return the integer ordinal of a one character string.";
1529
1530
Guido van Rossum79f25d91997-04-29 20:08:16 +00001531static PyObject *
Guido van Rossum6a00cd81995-01-07 12:39:01 +00001532builtin_pow(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001533 PyObject *self;
1534 PyObject *args;
Guido van Rossum6a00cd81995-01-07 12:39:01 +00001535{
Guido van Rossum09df08a1998-05-22 00:51:39 +00001536 PyObject *v, *w, *z = Py_None;
Guido van Rossum6a00cd81995-01-07 12:39:01 +00001537
Guido van Rossum79f25d91997-04-29 20:08:16 +00001538 if (!PyArg_ParseTuple(args, "OO|O:pow", &v, &w, &z))
Guido van Rossum6a00cd81995-01-07 12:39:01 +00001539 return NULL;
Guido van Rossum09df08a1998-05-22 00:51:39 +00001540 return PyNumber_Power(v, w, z);
Guido van Rossumd4905451991-05-05 20:00:36 +00001541}
1542
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001543static char pow_doc[] =
1544"pow(x, y[, z]) -> number\n\
1545\n\
1546With two arguments, equivalent to x**y. With three arguments,\n\
1547equivalent to (x**y) % z, but may be more efficient (e.g. for longs).";
1548
1549
Guido van Rossum124eff01999-02-23 16:11:01 +00001550/* Return number of items in range/xrange (lo, hi, step). step > 0
1551 * required. Return a value < 0 if & only if the true value is too
1552 * large to fit in a signed long.
1553 */
1554static long
1555get_len_of_range(lo, hi, step)
1556 long lo;
1557 long hi;
1558 long step; /* must be > 0 */
1559{
1560 /* -------------------------------------------------------------
1561 If lo >= hi, the range is empty.
1562 Else if n values are in the range, the last one is
1563 lo + (n-1)*step, which must be <= hi-1. Rearranging,
1564 n <= (hi - lo - 1)/step + 1, so taking the floor of the RHS gives
1565 the proper value. Since lo < hi in this case, hi-lo-1 >= 0, so
1566 the RHS is non-negative and so truncation is the same as the
1567 floor. Letting M be the largest positive long, the worst case
1568 for the RHS numerator is hi=M, lo=-M-1, and then
1569 hi-lo-1 = M-(-M-1)-1 = 2*M. Therefore unsigned long has enough
1570 precision to compute the RHS exactly.
1571 ---------------------------------------------------------------*/
1572 long n = 0;
1573 if (lo < hi) {
1574 unsigned long uhi = (unsigned long)hi;
1575 unsigned long ulo = (unsigned long)lo;
1576 unsigned long diff = uhi - ulo - 1;
1577 n = (long)(diff / (unsigned long)step + 1);
1578 }
1579 return n;
1580}
1581
Guido van Rossum79f25d91997-04-29 20:08:16 +00001582static PyObject *
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001583builtin_range(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001584 PyObject *self;
1585 PyObject *args;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001586{
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001587 long ilow = 0, ihigh = 0, istep = 1;
Guido van Rossum124eff01999-02-23 16:11:01 +00001588 long bign;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001589 int i, n;
Guido van Rossum124eff01999-02-23 16:11:01 +00001590
Guido van Rossum79f25d91997-04-29 20:08:16 +00001591 PyObject *v;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001592
Guido van Rossum79f25d91997-04-29 20:08:16 +00001593 if (PyTuple_Size(args) <= 1) {
1594 if (!PyArg_ParseTuple(args,
Guido van Rossum0865dd91995-01-17 16:30:22 +00001595 "l;range() requires 1-3 int arguments",
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001596 &ihigh))
1597 return NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001598 }
1599 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001600 if (!PyArg_ParseTuple(args,
Guido van Rossum0865dd91995-01-17 16:30:22 +00001601 "ll|l;range() requires 1-3 int arguments",
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001602 &ilow, &ihigh, &istep))
Guido van Rossum3f5da241990-12-20 15:06:42 +00001603 return NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001604 }
1605 if (istep == 0) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001606 PyErr_SetString(PyExc_ValueError, "zero step for range()");
Guido van Rossum3f5da241990-12-20 15:06:42 +00001607 return NULL;
1608 }
Guido van Rossum124eff01999-02-23 16:11:01 +00001609 if (istep > 0)
1610 bign = get_len_of_range(ilow, ihigh, istep);
1611 else
1612 bign = get_len_of_range(ihigh, ilow, -istep);
1613 n = (int)bign;
1614 if (bign < 0 || (long)n != bign) {
Guido van Rossume23cde21999-01-12 05:07:47 +00001615 PyErr_SetString(PyExc_OverflowError,
Guido van Rossum124eff01999-02-23 16:11:01 +00001616 "range() has too many items");
Guido van Rossume23cde21999-01-12 05:07:47 +00001617 return NULL;
1618 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00001619 v = PyList_New(n);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001620 if (v == NULL)
1621 return NULL;
1622 for (i = 0; i < n; i++) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001623 PyObject *w = PyInt_FromLong(ilow);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001624 if (w == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001625 Py_DECREF(v);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001626 return NULL;
1627 }
Guido van Rossuma937d141998-04-24 18:22:02 +00001628 PyList_SET_ITEM(v, i, w);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001629 ilow += istep;
1630 }
1631 return v;
1632}
1633
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001634static char range_doc[] =
1635"range([start,] stop[, step]) -> list of integers\n\
1636\n\
1637Return a list containing an arithmetic progression of integers.\n\
1638range(i, j) returns [i, i+1, i+2, ..., j-1]; start (!) defaults to 0.\n\
1639When step is given, it specifies the increment (or decrement).\n\
1640For example, range(4) returns [0, 1, 2, 3]. The end point is omitted!\n\
1641These are exactly the valid indices for a list of 4 elements.";
1642
1643
Guido van Rossum79f25d91997-04-29 20:08:16 +00001644static PyObject *
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001645builtin_xrange(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001646 PyObject *self;
1647 PyObject *args;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001648{
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001649 long ilow = 0, ihigh = 0, istep = 1;
Guido van Rossum0865dd91995-01-17 16:30:22 +00001650 long n;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001651
Guido van Rossum79f25d91997-04-29 20:08:16 +00001652 if (PyTuple_Size(args) <= 1) {
1653 if (!PyArg_ParseTuple(args,
Guido van Rossum0865dd91995-01-17 16:30:22 +00001654 "l;xrange() requires 1-3 int arguments",
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001655 &ihigh))
1656 return NULL;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001657 }
1658 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001659 if (!PyArg_ParseTuple(args,
Guido van Rossum0865dd91995-01-17 16:30:22 +00001660 "ll|l;xrange() requires 1-3 int arguments",
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001661 &ilow, &ihigh, &istep))
Guido van Rossum12d12c51993-10-26 17:58:25 +00001662 return NULL;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001663 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001664 if (istep == 0) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001665 PyErr_SetString(PyExc_ValueError, "zero step for xrange()");
Guido van Rossum12d12c51993-10-26 17:58:25 +00001666 return NULL;
1667 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001668 if (istep > 0)
Guido van Rossum124eff01999-02-23 16:11:01 +00001669 n = get_len_of_range(ilow, ihigh, istep);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001670 else
Guido van Rossum124eff01999-02-23 16:11:01 +00001671 n = get_len_of_range(ihigh, ilow, -istep);
1672 if (n < 0) {
1673 PyErr_SetString(PyExc_OverflowError,
1674 "xrange() has more than sys.maxint items");
1675 return NULL;
1676 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00001677 return PyRange_New(ilow, n, istep, 1);
Guido van Rossum12d12c51993-10-26 17:58:25 +00001678}
1679
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001680static char xrange_doc[] =
1681"xrange([start,] stop[, step]) -> xrange object\n\
1682\n\
1683Like range(), but instead of returning a list, returns an object that\n\
1684generates the numbers in the range on demand. This is slightly slower\n\
1685than range() but more memory efficient.";
1686
1687
Guido van Rossum79f25d91997-04-29 20:08:16 +00001688static PyObject *
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001689builtin_raw_input(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001690 PyObject *self;
1691 PyObject *args;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001692{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001693 PyObject *v = NULL;
1694 PyObject *f;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001695
Guido van Rossum79f25d91997-04-29 20:08:16 +00001696 if (!PyArg_ParseTuple(args, "|O:[raw_]input", &v))
Guido van Rossum3165fe61992-09-25 21:59:05 +00001697 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001698 if (PyFile_AsFile(PySys_GetObject("stdin")) == stdin &&
1699 PyFile_AsFile(PySys_GetObject("stdout")) == stdout &&
Guido van Rossum53bb7ff1995-07-26 16:26:31 +00001700 isatty(fileno(stdin)) && isatty(fileno(stdout))) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001701 PyObject *po;
Guido van Rossum872537c1995-07-07 22:43:42 +00001702 char *prompt;
1703 char *s;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001704 PyObject *result;
Guido van Rossum872537c1995-07-07 22:43:42 +00001705 if (v != NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001706 po = PyObject_Str(v);
Guido van Rossum872537c1995-07-07 22:43:42 +00001707 if (po == NULL)
1708 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001709 prompt = PyString_AsString(po);
Guido van Rossumd9b52081998-06-26 18:25:38 +00001710 if (prompt == NULL)
1711 return NULL;
Guido van Rossum872537c1995-07-07 22:43:42 +00001712 }
1713 else {
1714 po = NULL;
1715 prompt = "";
1716 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00001717 s = PyOS_Readline(prompt);
1718 Py_XDECREF(po);
Guido van Rossum872537c1995-07-07 22:43:42 +00001719 if (s == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001720 PyErr_SetNone(PyExc_KeyboardInterrupt);
Guido van Rossum872537c1995-07-07 22:43:42 +00001721 return NULL;
1722 }
1723 if (*s == '\0') {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001724 PyErr_SetNone(PyExc_EOFError);
Guido van Rossum872537c1995-07-07 22:43:42 +00001725 result = NULL;
1726 }
1727 else { /* strip trailing '\n' */
Guido van Rossum79f25d91997-04-29 20:08:16 +00001728 result = PyString_FromStringAndSize(s, strlen(s)-1);
Guido van Rossum872537c1995-07-07 22:43:42 +00001729 }
1730 free(s);
1731 return result;
1732 }
Guido van Rossum90933611991-06-07 16:10:43 +00001733 if (v != NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001734 f = PySys_GetObject("stdout");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001735 if (f == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001736 PyErr_SetString(PyExc_RuntimeError, "lost sys.stdout");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001737 return NULL;
1738 }
Guido van Rossumc8b6df91997-05-23 00:06:51 +00001739 if (Py_FlushLine() != 0 ||
1740 PyFile_WriteObject(v, f, Py_PRINT_RAW) != 0)
Guido van Rossum90933611991-06-07 16:10:43 +00001741 return NULL;
1742 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00001743 f = PySys_GetObject("stdin");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001744 if (f == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001745 PyErr_SetString(PyExc_RuntimeError, "lost sys.stdin");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001746 return NULL;
1747 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00001748 return PyFile_GetLine(f, -1);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001749}
1750
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001751static char raw_input_doc[] =
1752"raw_input([prompt]) -> string\n\
1753\n\
1754Read a string from standard input. The trailing newline is stripped.\n\
1755If the user hits EOF (Unix: Ctl-D, Windows: Ctl-Z+Return), raise EOFError.\n\
1756On Unix, GNU readline is used if enabled. The prompt string, if given,\n\
1757is printed without a trailing newline before reading.";
1758
1759
Guido van Rossum79f25d91997-04-29 20:08:16 +00001760static PyObject *
Guido van Rossum12d12c51993-10-26 17:58:25 +00001761builtin_reduce(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001762 PyObject *self;
1763 PyObject *args;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001764{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001765 PyObject *seq, *func, *result = NULL;
1766 PySequenceMethods *sqf;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001767 register int i;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001768
Guido van Rossum79f25d91997-04-29 20:08:16 +00001769 if (!PyArg_ParseTuple(args, "OO|O:reduce", &func, &seq, &result))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001770 return NULL;
1771 if (result != NULL)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001772 Py_INCREF(result);
Guido van Rossum12d12c51993-10-26 17:58:25 +00001773
Guido van Rossum09df08a1998-05-22 00:51:39 +00001774 sqf = seq->ob_type->tp_as_sequence;
1775 if (sqf == NULL || sqf->sq_item == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001776 PyErr_SetString(PyExc_TypeError,
Guido van Rossum12d12c51993-10-26 17:58:25 +00001777 "2nd argument to reduce() must be a sequence object");
1778 return NULL;
1779 }
1780
Guido van Rossum79f25d91997-04-29 20:08:16 +00001781 if ((args = PyTuple_New(2)) == NULL)
Guido van Rossum2d951851994-08-29 12:52:16 +00001782 goto Fail;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001783
Guido van Rossum2d951851994-08-29 12:52:16 +00001784 for (i = 0; ; ++i) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001785 PyObject *op2;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001786
1787 if (args->ob_refcnt > 1) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001788 Py_DECREF(args);
1789 if ((args = PyTuple_New(2)) == NULL)
Guido van Rossum2d951851994-08-29 12:52:16 +00001790 goto Fail;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001791 }
1792
Guido van Rossum2d951851994-08-29 12:52:16 +00001793 if ((op2 = (*sqf->sq_item)(seq, i)) == NULL) {
Barry Warsawcde8b1b1997-08-22 21:14:38 +00001794 if (PyErr_ExceptionMatches(PyExc_IndexError)) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001795 PyErr_Clear();
Guido van Rossum2d951851994-08-29 12:52:16 +00001796 break;
1797 }
1798 goto Fail;
1799 }
Guido van Rossum12d12c51993-10-26 17:58:25 +00001800
Guido van Rossum2d951851994-08-29 12:52:16 +00001801 if (result == NULL)
1802 result = op2;
1803 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001804 PyTuple_SetItem(args, 0, result);
1805 PyTuple_SetItem(args, 1, op2);
1806 if ((result = PyEval_CallObject(func, args)) == NULL)
Guido van Rossum2d951851994-08-29 12:52:16 +00001807 goto Fail;
1808 }
Guido van Rossum12d12c51993-10-26 17:58:25 +00001809 }
1810
Guido van Rossum79f25d91997-04-29 20:08:16 +00001811 Py_DECREF(args);
Guido van Rossum12d12c51993-10-26 17:58:25 +00001812
Guido van Rossum2d951851994-08-29 12:52:16 +00001813 if (result == NULL)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001814 PyErr_SetString(PyExc_TypeError,
Guido van Rossum2d951851994-08-29 12:52:16 +00001815 "reduce of empty sequence with no initial value");
1816
Guido van Rossum12d12c51993-10-26 17:58:25 +00001817 return result;
1818
Guido van Rossum2d951851994-08-29 12:52:16 +00001819Fail:
Guido van Rossum79f25d91997-04-29 20:08:16 +00001820 Py_XDECREF(args);
1821 Py_XDECREF(result);
Guido van Rossum12d12c51993-10-26 17:58:25 +00001822 return NULL;
1823}
1824
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001825static char reduce_doc[] =
1826"reduce(function, sequence[, initial]) -> value\n\
1827\n\
1828Apply a function of two arguments cumulatively to the items of a sequence,\n\
1829from left to right, so as to reduce the sequence to a single value.\n\
1830For example, reduce(lambda x, y: x+y, [1, 2, 3, 4, 5]) calculates\n\
1831((((1+2)+3)+4)+5). If initial is present, it is placed before the items\n\
1832of the sequence in the calculation, and serves as a default when the\n\
1833sequence is empty.";
1834
1835
Guido van Rossum79f25d91997-04-29 20:08:16 +00001836static PyObject *
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001837builtin_reload(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001838 PyObject *self;
1839 PyObject *args;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001840{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001841 PyObject *v;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001842
Guido van Rossum79f25d91997-04-29 20:08:16 +00001843 if (!PyArg_ParseTuple(args, "O:reload", &v))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001844 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001845 return PyImport_ReloadModule(v);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001846}
1847
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001848static char reload_doc[] =
1849"reload(module) -> module\n\
1850\n\
1851Reload the module. The module must have been successfully imported before.";
1852
1853
Guido van Rossum79f25d91997-04-29 20:08:16 +00001854static PyObject *
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001855builtin_repr(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001856 PyObject *self;
1857 PyObject *args;
Guido van Rossumc89705d1992-11-26 08:54:07 +00001858{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001859 PyObject *v;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001860
Guido van Rossum79f25d91997-04-29 20:08:16 +00001861 if (!PyArg_ParseTuple(args, "O:repr", &v))
Guido van Rossumc89705d1992-11-26 08:54:07 +00001862 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001863 return PyObject_Repr(v);
Guido van Rossumc89705d1992-11-26 08:54:07 +00001864}
1865
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001866static char repr_doc[] =
1867"repr(object) -> string\n\
1868\n\
1869Return the canonical string representation of the object.\n\
1870For most object types, eval(repr(object)) == object.";
1871
1872
Guido van Rossum79f25d91997-04-29 20:08:16 +00001873static PyObject *
Guido van Rossum9e51f9b1993-02-12 16:29:05 +00001874builtin_round(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001875 PyObject *self;
1876 PyObject *args;
Guido van Rossum9e51f9b1993-02-12 16:29:05 +00001877{
Guido van Rossum9e51f9b1993-02-12 16:29:05 +00001878 double x;
1879 double f;
1880 int ndigits = 0;
Guido van Rossum9e51f9b1993-02-12 16:29:05 +00001881 int i;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001882
Guido van Rossum79f25d91997-04-29 20:08:16 +00001883 if (!PyArg_ParseTuple(args, "d|i:round", &x, &ndigits))
Guido van Rossum9e51f9b1993-02-12 16:29:05 +00001884 return NULL;
Guido van Rossum9e51f9b1993-02-12 16:29:05 +00001885 f = 1.0;
Guido van Rossum1e162d31998-05-09 14:42:25 +00001886 i = abs(ndigits);
1887 while (--i >= 0)
Guido van Rossum9e51f9b1993-02-12 16:29:05 +00001888 f = f*10.0;
Guido van Rossum1e162d31998-05-09 14:42:25 +00001889 if (ndigits < 0)
1890 x /= f;
Guido van Rossum9e51f9b1993-02-12 16:29:05 +00001891 else
Guido van Rossum1e162d31998-05-09 14:42:25 +00001892 x *= f;
1893 if (x >= 0.0)
1894 x = floor(x + 0.5);
1895 else
1896 x = ceil(x - 0.5);
1897 if (ndigits < 0)
1898 x *= f;
1899 else
1900 x /= f;
1901 return PyFloat_FromDouble(x);
Guido van Rossum9e51f9b1993-02-12 16:29:05 +00001902}
1903
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001904static char round_doc[] =
1905"round(number[, ndigits]) -> floating point number\n\
1906\n\
1907Round a number to a given precision in decimal digits (default 0 digits).\n\
1908This always returns a floating point number. Precision may be negative.";
1909
1910
Guido van Rossum79f25d91997-04-29 20:08:16 +00001911static PyObject *
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001912builtin_str(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001913 PyObject *self;
1914 PyObject *args;
Guido van Rossumc89705d1992-11-26 08:54:07 +00001915{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001916 PyObject *v;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001917
Guido van Rossum79f25d91997-04-29 20:08:16 +00001918 if (!PyArg_ParseTuple(args, "O:str", &v))
Guido van Rossumc89705d1992-11-26 08:54:07 +00001919 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001920 return PyObject_Str(v);
Guido van Rossumc89705d1992-11-26 08:54:07 +00001921}
1922
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001923static char str_doc[] =
1924"str(object) -> string\n\
1925\n\
1926Return a nice string representation of the object.\n\
1927If the argument is a string, the return value is the same object.";
1928
1929
Guido van Rossum79f25d91997-04-29 20:08:16 +00001930static PyObject *
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001931builtin_tuple(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001932 PyObject *self;
1933 PyObject *args;
Guido van Rossumcae027b1994-08-29 12:53:11 +00001934{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001935 PyObject *v;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001936
Guido van Rossum79f25d91997-04-29 20:08:16 +00001937 if (!PyArg_ParseTuple(args, "O:tuple", &v))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001938 return NULL;
Guido van Rossum09df08a1998-05-22 00:51:39 +00001939 return PySequence_Tuple(v);
Guido van Rossumcae027b1994-08-29 12:53:11 +00001940}
1941
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001942static char tuple_doc[] =
1943"tuple(sequence) -> list\n\
1944\n\
1945Return a tuple whose items are the same as those of the argument sequence.\n\
1946If the argument is a tuple, the return value is the same object.";
1947
1948
Guido van Rossum79f25d91997-04-29 20:08:16 +00001949static PyObject *
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001950builtin_type(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001951 PyObject *self;
1952 PyObject *args;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001953{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001954 PyObject *v;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001955
Guido van Rossum79f25d91997-04-29 20:08:16 +00001956 if (!PyArg_ParseTuple(args, "O:type", &v))
Guido van Rossum3f5da241990-12-20 15:06:42 +00001957 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001958 v = (PyObject *)v->ob_type;
1959 Py_INCREF(v);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001960 return v;
1961}
1962
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001963static char type_doc[] =
1964"type(object) -> type object\n\
1965\n\
1966Return the type of the object.";
1967
1968
Guido van Rossum79f25d91997-04-29 20:08:16 +00001969static PyObject *
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001970builtin_vars(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001971 PyObject *self;
1972 PyObject *args;
Guido van Rossum2d951851994-08-29 12:52:16 +00001973{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001974 PyObject *v = NULL;
1975 PyObject *d;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001976
Guido van Rossum79f25d91997-04-29 20:08:16 +00001977 if (!PyArg_ParseTuple(args, "|O:vars", &v))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001978 return NULL;
Guido van Rossum2d951851994-08-29 12:52:16 +00001979 if (v == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001980 d = PyEval_GetLocals();
Guido van Rossum53bb7ff1995-07-26 16:26:31 +00001981 if (d == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001982 if (!PyErr_Occurred())
1983 PyErr_SetString(PyExc_SystemError,
1984 "no locals!?");
Guido van Rossum53bb7ff1995-07-26 16:26:31 +00001985 }
1986 else
Guido van Rossum79f25d91997-04-29 20:08:16 +00001987 Py_INCREF(d);
Guido van Rossum2d951851994-08-29 12:52:16 +00001988 }
1989 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001990 d = PyObject_GetAttrString(v, "__dict__");
Guido van Rossum2d951851994-08-29 12:52:16 +00001991 if (d == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001992 PyErr_SetString(PyExc_TypeError,
Guido van Rossum2d951851994-08-29 12:52:16 +00001993 "vars() argument must have __dict__ attribute");
1994 return NULL;
1995 }
1996 }
1997 return d;
1998}
1999
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002000static char vars_doc[] =
2001"vars([object]) -> dictionary\n\
2002\n\
2003Without arguments, equivalent to locals().\n\
2004With an argument, equivalent to object.__dict__.";
2005
2006
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002007static PyObject *
2008builtin_isinstance(self, args)
2009 PyObject *self;
2010 PyObject *args;
2011{
2012 PyObject *inst;
2013 PyObject *cls;
2014 int retval;
2015
2016 if (!PyArg_ParseTuple(args, "OO", &inst, &cls))
2017 return NULL;
Guido van Rossumf5dd9141997-12-02 19:11:45 +00002018 if (PyType_Check(cls)) {
Guido van Rossumd6af46d1997-12-10 05:51:47 +00002019 retval = ((PyObject *)(inst->ob_type) == cls);
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002020 }
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002021 else {
Guido van Rossumf5dd9141997-12-02 19:11:45 +00002022 if (!PyClass_Check(cls)) {
2023 PyErr_SetString(PyExc_TypeError,
2024 "second argument must be a class");
2025 return NULL;
2026 }
2027
2028 if (!PyInstance_Check(inst))
2029 retval = 0;
2030 else {
2031 PyObject *inclass =
2032 (PyObject*)((PyInstanceObject*)inst)->in_class;
2033 retval = PyClass_IsSubclass(inclass, cls);
2034 }
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002035 }
2036 return PyInt_FromLong(retval);
2037}
2038
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002039static char isinstance_doc[] =
2040"isinstance(object, class-or-type) -> Boolean\n\
2041\n\
2042Return whether an object is an instance of a class or of a subclass thereof.\n\
2043With a type as second argument, return whether that is the object's type.";
2044
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002045
2046static PyObject *
2047builtin_issubclass(self, args)
2048 PyObject *self;
2049 PyObject *args;
2050{
2051 PyObject *derived;
2052 PyObject *cls;
2053 int retval;
2054
2055 if (!PyArg_ParseTuple(args, "OO", &derived, &cls))
2056 return NULL;
2057 if (!PyClass_Check(derived) || !PyClass_Check(cls)) {
2058 PyErr_SetString(PyExc_TypeError, "arguments must be classes");
2059 return NULL;
2060 }
2061 /* shortcut */
2062 if (!(retval = (derived == cls)))
2063 retval = PyClass_IsSubclass(derived, cls);
2064
2065 return PyInt_FromLong(retval);
2066}
2067
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002068static char issubclass_doc[] =
2069"issubclass(C, B) -> Boolean\n\
2070\n\
2071Return whether class C is a subclass (i.e., a derived class) of class B.";
2072
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002073
Guido van Rossum79f25d91997-04-29 20:08:16 +00002074static PyMethodDef builtin_methods[] = {
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002075 {"__import__", builtin___import__, 1, import_doc},
2076 {"abs", builtin_abs, 1, abs_doc},
2077 {"apply", builtin_apply, 1, apply_doc},
Guido van Rossum0daf0221999-03-19 19:07:19 +00002078 {"buffer", builtin_buffer, 1, buffer_doc},
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002079 {"callable", builtin_callable, 1, callable_doc},
2080 {"chr", builtin_chr, 1, chr_doc},
2081 {"cmp", builtin_cmp, 1, cmp_doc},
2082 {"coerce", builtin_coerce, 1, coerce_doc},
2083 {"compile", builtin_compile, 1, compile_doc},
Guido van Rossum8a5c5d21996-01-12 01:09:56 +00002084#ifndef WITHOUT_COMPLEX
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002085 {"complex", builtin_complex, 1, complex_doc},
Guido van Rossum8a5c5d21996-01-12 01:09:56 +00002086#endif
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002087 {"delattr", builtin_delattr, 1, delattr_doc},
2088 {"dir", builtin_dir, 1, dir_doc},
2089 {"divmod", builtin_divmod, 1, divmod_doc},
2090 {"eval", builtin_eval, 1, eval_doc},
2091 {"execfile", builtin_execfile, 1, execfile_doc},
2092 {"filter", builtin_filter, 1, filter_doc},
2093 {"float", builtin_float, 1, float_doc},
2094 {"getattr", builtin_getattr, 1, getattr_doc},
2095 {"globals", builtin_globals, 1, globals_doc},
2096 {"hasattr", builtin_hasattr, 1, hasattr_doc},
2097 {"hash", builtin_hash, 1, hash_doc},
2098 {"hex", builtin_hex, 1, hex_doc},
2099 {"id", builtin_id, 1, id_doc},
2100 {"input", builtin_input, 1, input_doc},
2101 {"intern", builtin_intern, 1, intern_doc},
2102 {"int", builtin_int, 1, int_doc},
2103 {"isinstance", builtin_isinstance, 1, isinstance_doc},
2104 {"issubclass", builtin_issubclass, 1, issubclass_doc},
2105 {"len", builtin_len, 1, len_doc},
2106 {"list", builtin_list, 1, list_doc},
2107 {"locals", builtin_locals, 1, locals_doc},
2108 {"long", builtin_long, 1, long_doc},
2109 {"map", builtin_map, 1, map_doc},
2110 {"max", builtin_max, 1, max_doc},
2111 {"min", builtin_min, 1, min_doc},
2112 {"oct", builtin_oct, 1, oct_doc},
2113 {"open", builtin_open, 1, open_doc},
2114 {"ord", builtin_ord, 1, ord_doc},
2115 {"pow", builtin_pow, 1, pow_doc},
2116 {"range", builtin_range, 1, range_doc},
2117 {"raw_input", builtin_raw_input, 1, raw_input_doc},
2118 {"reduce", builtin_reduce, 1, reduce_doc},
2119 {"reload", builtin_reload, 1, reload_doc},
2120 {"repr", builtin_repr, 1, repr_doc},
2121 {"round", builtin_round, 1, round_doc},
2122 {"setattr", builtin_setattr, 1, setattr_doc},
2123 {"slice", builtin_slice, 1, slice_doc},
2124 {"str", builtin_str, 1, str_doc},
2125 {"tuple", builtin_tuple, 1, tuple_doc},
2126 {"type", builtin_type, 1, type_doc},
2127 {"vars", builtin_vars, 1, vars_doc},
2128 {"xrange", builtin_xrange, 1, xrange_doc},
Guido van Rossumc02e15c1991-12-16 13:03:00 +00002129 {NULL, NULL},
Guido van Rossum3f5da241990-12-20 15:06:42 +00002130};
2131
Guido van Rossum3f5da241990-12-20 15:06:42 +00002132/* Predefined exceptions */
2133
Guido van Rossum04748321997-09-16 18:43:15 +00002134PyObject *PyExc_Exception;
Barry Warsaw757af0e1997-08-29 22:13:51 +00002135PyObject *PyExc_StandardError;
Barry Warsaw412cdc21997-09-16 21:51:14 +00002136PyObject *PyExc_ArithmeticError;
Barry Warsaw757af0e1997-08-29 22:13:51 +00002137PyObject *PyExc_LookupError;
2138
Guido van Rossum79f25d91997-04-29 20:08:16 +00002139PyObject *PyExc_AssertionError;
2140PyObject *PyExc_AttributeError;
2141PyObject *PyExc_EOFError;
Guido van Rossumb6a7f771997-05-09 03:03:23 +00002142PyObject *PyExc_FloatingPointError;
Barry Warsawd086a1a1998-07-23 15:59:57 +00002143PyObject *PyExc_EnvironmentError;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002144PyObject *PyExc_IOError;
Barry Warsawd086a1a1998-07-23 15:59:57 +00002145PyObject *PyExc_OSError;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002146PyObject *PyExc_ImportError;
2147PyObject *PyExc_IndexError;
2148PyObject *PyExc_KeyError;
2149PyObject *PyExc_KeyboardInterrupt;
2150PyObject *PyExc_MemoryError;
2151PyObject *PyExc_NameError;
2152PyObject *PyExc_OverflowError;
2153PyObject *PyExc_RuntimeError;
Barry Warsaw344864f1998-12-01 18:52:06 +00002154PyObject *PyExc_NotImplementedError;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002155PyObject *PyExc_SyntaxError;
2156PyObject *PyExc_SystemError;
2157PyObject *PyExc_SystemExit;
2158PyObject *PyExc_TypeError;
2159PyObject *PyExc_ValueError;
2160PyObject *PyExc_ZeroDivisionError;
Guido van Rossum50afb7a1991-12-10 13:52:31 +00002161
Barry Warsaw757af0e1997-08-29 22:13:51 +00002162PyObject *PyExc_MemoryErrorInst;
2163
Guido van Rossum11950231999-03-25 21:16:07 +00002164static struct
Barry Warsaw757af0e1997-08-29 22:13:51 +00002165{
2166 char* name;
2167 PyObject** exc;
2168 int leaf_exc;
2169}
2170bltin_exc[] = {
Guido van Rossum04748321997-09-16 18:43:15 +00002171 {"Exception", &PyExc_Exception, 0},
Barry Warsaw757af0e1997-08-29 22:13:51 +00002172 {"StandardError", &PyExc_StandardError, 0},
Barry Warsaw412cdc21997-09-16 21:51:14 +00002173 {"ArithmeticError", &PyExc_ArithmeticError, 0},
Barry Warsaw757af0e1997-08-29 22:13:51 +00002174 {"LookupError", &PyExc_LookupError, 0},
2175 {"AssertionError", &PyExc_AssertionError, 1},
2176 {"AttributeError", &PyExc_AttributeError, 1},
2177 {"EOFError", &PyExc_EOFError, 1},
2178 {"FloatingPointError", &PyExc_FloatingPointError, 1},
Barry Warsaw78902031999-01-29 20:29:49 +00002179 {"EnvironmentError", &PyExc_EnvironmentError, 0},
Barry Warsaw757af0e1997-08-29 22:13:51 +00002180 {"IOError", &PyExc_IOError, 1},
Barry Warsawd086a1a1998-07-23 15:59:57 +00002181 {"OSError", &PyExc_OSError, 1},
Barry Warsaw757af0e1997-08-29 22:13:51 +00002182 {"ImportError", &PyExc_ImportError, 1},
2183 {"IndexError", &PyExc_IndexError, 1},
2184 {"KeyError", &PyExc_KeyError, 1},
2185 {"KeyboardInterrupt", &PyExc_KeyboardInterrupt, 1},
2186 {"MemoryError", &PyExc_MemoryError, 1},
2187 {"NameError", &PyExc_NameError, 1},
2188 {"OverflowError", &PyExc_OverflowError, 1},
2189 {"RuntimeError", &PyExc_RuntimeError, 1},
Barry Warsaw344864f1998-12-01 18:52:06 +00002190 {"NotImplementedError",&PyExc_NotImplementedError,1},
Barry Warsaw757af0e1997-08-29 22:13:51 +00002191 {"SyntaxError", &PyExc_SyntaxError, 1},
2192 {"SystemError", &PyExc_SystemError, 1},
2193 {"SystemExit", &PyExc_SystemExit, 1},
2194 {"TypeError", &PyExc_TypeError, 1},
2195 {"ValueError", &PyExc_ValueError, 1},
2196 {"ZeroDivisionError", &PyExc_ZeroDivisionError, 1},
2197 {NULL, NULL}
2198};
2199
2200
Barry Warsaw98b62461998-09-14 18:51:11 +00002201/* import exceptions module to extract class exceptions. on success,
2202 * return 1. on failure return 0 which signals _PyBuiltin_Init_2 to fall
2203 * back to using old-style string based exceptions.
2204 */
2205static int
Barry Warsaw757af0e1997-08-29 22:13:51 +00002206init_class_exc(dict)
2207 PyObject *dict;
2208{
2209 int i;
2210 PyObject *m = PyImport_ImportModule("exceptions");
Barry Warsaw98b62461998-09-14 18:51:11 +00002211 PyObject *args = NULL;
2212 PyObject *d = NULL;
Barry Warsaw757af0e1997-08-29 22:13:51 +00002213
Barry Warsaw98b62461998-09-14 18:51:11 +00002214 /* make sure we got the module and its dictionary */
Barry Warsaw757af0e1997-08-29 22:13:51 +00002215 if (m == NULL ||
2216 (d = PyModule_GetDict(m)) == NULL)
2217 {
Barry Warsaw98b62461998-09-14 18:51:11 +00002218 PySys_WriteStderr("'import exceptions' failed; ");
Barry Warsaw757af0e1997-08-29 22:13:51 +00002219 if (Py_VerboseFlag) {
Barry Warsaw98b62461998-09-14 18:51:11 +00002220 PySys_WriteStderr("traceback:\n");
Barry Warsaw757af0e1997-08-29 22:13:51 +00002221 PyErr_Print();
2222 }
2223 else {
Barry Warsaw98b62461998-09-14 18:51:11 +00002224 PySys_WriteStderr("use -v for traceback\n");
Barry Warsaw757af0e1997-08-29 22:13:51 +00002225 }
Barry Warsaw98b62461998-09-14 18:51:11 +00002226 goto finally;
Barry Warsaw757af0e1997-08-29 22:13:51 +00002227 }
2228 for (i = 0; bltin_exc[i].name; i++) {
2229 /* dig the exception out of the module */
2230 PyObject *exc = PyDict_GetItemString(d, bltin_exc[i].name);
Barry Warsaw98b62461998-09-14 18:51:11 +00002231 if (!exc) {
2232 PySys_WriteStderr(
2233 "Built-in exception class not found: %s. Library mismatch?\n",
2234 bltin_exc[i].name);
2235 goto finally;
2236 }
2237 /* free the old-style exception string object */
Barry Warsaw757af0e1997-08-29 22:13:51 +00002238 Py_XDECREF(*bltin_exc[i].exc);
2239
2240 /* squirrel away a pointer to the exception */
2241 Py_INCREF(exc);
2242 *bltin_exc[i].exc = exc;
2243
2244 /* and insert the name in the __builtin__ module */
Barry Warsaw98b62461998-09-14 18:51:11 +00002245 if (PyDict_SetItemString(dict, bltin_exc[i].name, exc)) {
2246 PySys_WriteStderr(
2247 "Cannot insert exception into __builtin__: %s\n",
2248 bltin_exc[i].name);
2249 goto finally;
2250 }
Barry Warsaw757af0e1997-08-29 22:13:51 +00002251 }
2252
2253 /* we need one pre-allocated instance */
2254 args = Py_BuildValue("()");
Barry Warsaw98b62461998-09-14 18:51:11 +00002255 if (!args ||
2256 !(PyExc_MemoryErrorInst =
2257 PyEval_CallObject(PyExc_MemoryError, args)))
2258 {
2259 PySys_WriteStderr("Cannot pre-allocate MemoryError instance\n");
2260 goto finally;
Barry Warsaw757af0e1997-08-29 22:13:51 +00002261 }
Barry Warsaw98b62461998-09-14 18:51:11 +00002262 Py_DECREF(args);
Barry Warsaw757af0e1997-08-29 22:13:51 +00002263
2264 /* we're done with the exceptions module */
2265 Py_DECREF(m);
2266
Barry Warsaw98b62461998-09-14 18:51:11 +00002267 if (PyErr_Occurred()) {
2268 PySys_WriteStderr("Cannot initialize standard class exceptions; ");
2269 if (Py_VerboseFlag) {
2270 PySys_WriteStderr("traceback:\n");
2271 PyErr_Print();
2272 }
2273 else
2274 PySys_WriteStderr("use -v for traceback\n");
2275 goto finally;
2276 }
2277 return 1;
2278 finally:
2279 Py_XDECREF(m);
2280 Py_XDECREF(args);
2281 PyErr_Clear();
2282 return 0;
Barry Warsaw757af0e1997-08-29 22:13:51 +00002283}
2284
2285
2286static void
2287fini_instances()
2288{
2289 Py_XDECREF(PyExc_MemoryErrorInst);
2290 PyExc_MemoryErrorInst = NULL;
2291}
2292
2293
Guido van Rossum79f25d91997-04-29 20:08:16 +00002294static PyObject *
Guido van Rossum25ce5661997-08-02 03:10:38 +00002295newstdexception(dict, name)
2296 PyObject *dict;
Guido van Rossumfb905c31991-12-16 15:42:38 +00002297 char *name;
Guido van Rossum3f5da241990-12-20 15:06:42 +00002298{
Guido van Rossum79f25d91997-04-29 20:08:16 +00002299 PyObject *v = PyString_FromString(name);
Guido van Rossum25ce5661997-08-02 03:10:38 +00002300 if (v == NULL || PyDict_SetItemString(dict, name, v) != 0)
Barry Warsaw98b62461998-09-14 18:51:11 +00002301 Py_FatalError("Cannot create string-based exceptions");
Guido van Rossum3f5da241990-12-20 15:06:42 +00002302 return v;
2303}
2304
2305static void
Guido van Rossum25ce5661997-08-02 03:10:38 +00002306initerrors(dict)
2307 PyObject *dict;
Guido van Rossum3f5da241990-12-20 15:06:42 +00002308{
Barry Warsaw72b715d1999-02-24 00:35:43 +00002309 int i, j;
Barry Warsaw757af0e1997-08-29 22:13:51 +00002310 int exccnt = 0;
2311 for (i = 0; bltin_exc[i].name; i++, exccnt++) {
Barry Warsaw98b62461998-09-14 18:51:11 +00002312 Py_XDECREF(*bltin_exc[i].exc);
Barry Warsaw757af0e1997-08-29 22:13:51 +00002313 if (bltin_exc[i].leaf_exc)
2314 *bltin_exc[i].exc =
2315 newstdexception(dict, bltin_exc[i].name);
2316 }
2317
Barry Warsawd086a1a1998-07-23 15:59:57 +00002318 /* This is kind of bogus because we special case the some of the
2319 * new exceptions to be nearly forward compatible. But this means
2320 * we hard code knowledge about exceptions.py into C here. I don't
2321 * have a better solution, though.
2322 */
Barry Warsaw757af0e1997-08-29 22:13:51 +00002323 PyExc_LookupError = PyTuple_New(2);
2324 Py_INCREF(PyExc_IndexError);
2325 PyTuple_SET_ITEM(PyExc_LookupError, 0, PyExc_IndexError);
2326 Py_INCREF(PyExc_KeyError);
2327 PyTuple_SET_ITEM(PyExc_LookupError, 1, PyExc_KeyError);
2328 PyDict_SetItemString(dict, "LookupError", PyExc_LookupError);
2329
Barry Warsaw412cdc21997-09-16 21:51:14 +00002330 PyExc_ArithmeticError = PyTuple_New(3);
Barry Warsaw757af0e1997-08-29 22:13:51 +00002331 Py_INCREF(PyExc_OverflowError);
Barry Warsaw412cdc21997-09-16 21:51:14 +00002332 PyTuple_SET_ITEM(PyExc_ArithmeticError, 0, PyExc_OverflowError);
Barry Warsaw757af0e1997-08-29 22:13:51 +00002333 Py_INCREF(PyExc_ZeroDivisionError);
Barry Warsaw412cdc21997-09-16 21:51:14 +00002334 PyTuple_SET_ITEM(PyExc_ArithmeticError, 1, PyExc_ZeroDivisionError);
Barry Warsaw757af0e1997-08-29 22:13:51 +00002335 Py_INCREF(PyExc_FloatingPointError);
Barry Warsaw412cdc21997-09-16 21:51:14 +00002336 PyTuple_SET_ITEM(PyExc_ArithmeticError, 2, PyExc_FloatingPointError);
2337 PyDict_SetItemString(dict, "ArithmeticError", PyExc_ArithmeticError);
Barry Warsaw757af0e1997-08-29 22:13:51 +00002338
Barry Warsawd086a1a1998-07-23 15:59:57 +00002339 PyExc_EnvironmentError = PyTuple_New(2);
2340 Py_INCREF(PyExc_IOError);
2341 PyTuple_SET_ITEM(PyExc_EnvironmentError, 0, PyExc_IOError);
2342 Py_INCREF(PyExc_OSError);
2343 PyTuple_SET_ITEM(PyExc_EnvironmentError, 1, PyExc_OSError);
2344 PyDict_SetItemString(dict, "EnvironmentError", PyExc_EnvironmentError);
2345
Barry Warsaw72b715d1999-02-24 00:35:43 +00002346 /* missing from the StandardError tuple: Exception, StandardError,
2347 * and SystemExit
2348 */
2349 PyExc_StandardError = PyTuple_New(exccnt-3);
2350 for (i = 2, j = 0; bltin_exc[i].name; i++) {
Barry Warsaw757af0e1997-08-29 22:13:51 +00002351 PyObject *exc = *bltin_exc[i].exc;
Barry Warsaw72b715d1999-02-24 00:35:43 +00002352 /* SystemExit is not an error, but it is an exception */
2353 if (exc != PyExc_SystemExit) {
2354 Py_INCREF(exc);
2355 PyTuple_SET_ITEM(PyExc_StandardError, j++, exc);
2356 }
Barry Warsaw757af0e1997-08-29 22:13:51 +00002357 }
2358 PyDict_SetItemString(dict, "StandardError", PyExc_StandardError);
Guido van Rossum04748321997-09-16 18:43:15 +00002359
Barry Warsaw72b715d1999-02-24 00:35:43 +00002360 /* Exception is a 2-tuple */
2361 PyExc_Exception = PyTuple_New(2);
2362 Py_INCREF(PyExc_SystemExit);
2363 PyTuple_SET_ITEM(PyExc_Exception, 0, PyExc_SystemExit);
2364 Py_INCREF(PyExc_StandardError);
2365 PyTuple_SET_ITEM(PyExc_Exception, 1, PyExc_StandardError);
Guido van Rossum04748321997-09-16 18:43:15 +00002366 PyDict_SetItemString(dict, "Exception", PyExc_Exception);
Barry Warsaw757af0e1997-08-29 22:13:51 +00002367
2368 if (PyErr_Occurred())
2369 Py_FatalError("Could not initialize built-in string exceptions");
Guido van Rossum25ce5661997-08-02 03:10:38 +00002370}
2371
Barry Warsaw72b715d1999-02-24 00:35:43 +00002372
Guido van Rossum25ce5661997-08-02 03:10:38 +00002373static void
2374finierrors()
2375{
Barry Warsaw757af0e1997-08-29 22:13:51 +00002376 int i;
2377 for (i = 0; bltin_exc[i].name; i++) {
2378 PyObject *exc = *bltin_exc[i].exc;
2379 Py_XDECREF(exc);
2380 *bltin_exc[i].exc = NULL;
2381 }
Guido van Rossum25ce5661997-08-02 03:10:38 +00002382}
2383
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002384static char builtin_doc[] =
2385"Built-in functions, exceptions, and other objects.\n\
2386\n\
2387Noteworthy: None is the `nil' object; Ellipsis represents `...' in slices.";
2388
Guido van Rossum25ce5661997-08-02 03:10:38 +00002389PyObject *
Barry Warsaw757af0e1997-08-29 22:13:51 +00002390_PyBuiltin_Init_1()
Guido van Rossum25ce5661997-08-02 03:10:38 +00002391{
2392 PyObject *mod, *dict;
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002393 mod = Py_InitModule4("__builtin__", builtin_methods,
2394 builtin_doc, (PyObject *)NULL,
2395 PYTHON_API_VERSION);
Guido van Rossum25ce5661997-08-02 03:10:38 +00002396 if (mod == NULL)
2397 return NULL;
2398 dict = PyModule_GetDict(mod);
2399 initerrors(dict);
2400 if (PyDict_SetItemString(dict, "None", Py_None) < 0)
2401 return NULL;
2402 if (PyDict_SetItemString(dict, "Ellipsis", Py_Ellipsis) < 0)
2403 return NULL;
2404 if (PyDict_SetItemString(dict, "__debug__",
2405 PyInt_FromLong(Py_OptimizeFlag == 0)) < 0)
2406 return NULL;
Barry Warsaw757af0e1997-08-29 22:13:51 +00002407
Guido van Rossum25ce5661997-08-02 03:10:38 +00002408 return mod;
Guido van Rossum3f5da241990-12-20 15:06:42 +00002409}
2410
2411void
Barry Warsaw757af0e1997-08-29 22:13:51 +00002412_PyBuiltin_Init_2(dict)
2413 PyObject *dict;
2414{
2415 /* if Python was started with -X, initialize the class exceptions */
Barry Warsaw98b62461998-09-14 18:51:11 +00002416 if (Py_UseClassExceptionsFlag) {
2417 if (!init_class_exc(dict)) {
2418 /* class based exceptions could not be
2419 * initialized. Fall back to using string based
2420 * exceptions.
2421 */
2422 PySys_WriteStderr(
2423 "Warning! Falling back to string-based exceptions\n");
2424 initerrors(dict);
2425 }
2426 }
Barry Warsaw757af0e1997-08-29 22:13:51 +00002427}
2428
2429
2430void
2431_PyBuiltin_Fini_1()
2432{
2433 fini_instances();
2434}
2435
2436
2437void
2438_PyBuiltin_Fini_2()
Guido van Rossum3f5da241990-12-20 15:06:42 +00002439{
Guido van Rossum25ce5661997-08-02 03:10:38 +00002440 finierrors();
Guido van Rossum3f5da241990-12-20 15:06:42 +00002441}
Guido van Rossumc6bb8f71991-07-01 18:42:41 +00002442
Guido van Rossum12d12c51993-10-26 17:58:25 +00002443
Guido van Rossume77a7571993-11-03 15:01:26 +00002444/* Helper for filter(): filter a tuple through a function */
Guido van Rossum12d12c51993-10-26 17:58:25 +00002445
Guido van Rossum79f25d91997-04-29 20:08:16 +00002446static PyObject *
Guido van Rossum12d12c51993-10-26 17:58:25 +00002447filtertuple(func, tuple)
Guido van Rossum79f25d91997-04-29 20:08:16 +00002448 PyObject *func;
2449 PyObject *tuple;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002450{
Guido van Rossum79f25d91997-04-29 20:08:16 +00002451 PyObject *result;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002452 register int i, j;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002453 int len = PyTuple_Size(tuple);
Guido van Rossum12d12c51993-10-26 17:58:25 +00002454
Guido van Rossumb7b45621995-08-04 04:07:45 +00002455 if (len == 0) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002456 Py_INCREF(tuple);
Guido van Rossumb7b45621995-08-04 04:07:45 +00002457 return tuple;
2458 }
2459
Guido van Rossum79f25d91997-04-29 20:08:16 +00002460 if ((result = PyTuple_New(len)) == NULL)
Guido van Rossum2586bf01993-11-01 16:21:44 +00002461 return NULL;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002462
Guido van Rossum12d12c51993-10-26 17:58:25 +00002463 for (i = j = 0; i < len; ++i) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002464 PyObject *item, *good;
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002465 int ok;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002466
Guido van Rossum79f25d91997-04-29 20:08:16 +00002467 if ((item = PyTuple_GetItem(tuple, i)) == NULL)
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002468 goto Fail_1;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002469 if (func == Py_None) {
2470 Py_INCREF(item);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002471 good = item;
2472 }
2473 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002474 PyObject *arg = Py_BuildValue("(O)", item);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002475 if (arg == NULL)
2476 goto Fail_1;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002477 good = PyEval_CallObject(func, arg);
2478 Py_DECREF(arg);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002479 if (good == NULL)
Guido van Rossum12d12c51993-10-26 17:58:25 +00002480 goto Fail_1;
2481 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002482 ok = PyObject_IsTrue(good);
2483 Py_DECREF(good);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002484 if (ok) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002485 Py_INCREF(item);
2486 if (PyTuple_SetItem(result, j++, item) < 0)
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002487 goto Fail_1;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002488 }
Guido van Rossum12d12c51993-10-26 17:58:25 +00002489 }
2490
Guido van Rossum79f25d91997-04-29 20:08:16 +00002491 if (_PyTuple_Resize(&result, j, 0) < 0)
Guido van Rossum12d12c51993-10-26 17:58:25 +00002492 return NULL;
2493
Guido van Rossum12d12c51993-10-26 17:58:25 +00002494 return result;
2495
Guido van Rossum12d12c51993-10-26 17:58:25 +00002496Fail_1:
Guido van Rossum79f25d91997-04-29 20:08:16 +00002497 Py_DECREF(result);
Guido van Rossum12d12c51993-10-26 17:58:25 +00002498 return NULL;
2499}
2500
2501
Guido van Rossume77a7571993-11-03 15:01:26 +00002502/* Helper for filter(): filter a string through a function */
Guido van Rossum12d12c51993-10-26 17:58:25 +00002503
Guido van Rossum79f25d91997-04-29 20:08:16 +00002504static PyObject *
Guido van Rossum12d12c51993-10-26 17:58:25 +00002505filterstring(func, strobj)
Guido van Rossum79f25d91997-04-29 20:08:16 +00002506 PyObject *func;
2507 PyObject *strobj;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002508{
Guido van Rossum79f25d91997-04-29 20:08:16 +00002509 PyObject *result;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002510 register int i, j;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002511 int len = PyString_Size(strobj);
Guido van Rossum12d12c51993-10-26 17:58:25 +00002512
Guido van Rossum79f25d91997-04-29 20:08:16 +00002513 if (func == Py_None) {
Guido van Rossum2586bf01993-11-01 16:21:44 +00002514 /* No character is ever false -- share input string */
Guido van Rossum79f25d91997-04-29 20:08:16 +00002515 Py_INCREF(strobj);
Guido van Rossum2d951851994-08-29 12:52:16 +00002516 return strobj;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002517 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002518 if ((result = PyString_FromStringAndSize(NULL, len)) == NULL)
Guido van Rossum2586bf01993-11-01 16:21:44 +00002519 return NULL;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002520
Guido van Rossum12d12c51993-10-26 17:58:25 +00002521 for (i = j = 0; i < len; ++i) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002522 PyObject *item, *arg, *good;
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002523 int ok;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002524
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002525 item = (*strobj->ob_type->tp_as_sequence->sq_item)(strobj, i);
2526 if (item == NULL)
2527 goto Fail_1;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002528 arg = Py_BuildValue("(O)", item);
2529 Py_DECREF(item);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002530 if (arg == NULL)
2531 goto Fail_1;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002532 good = PyEval_CallObject(func, arg);
2533 Py_DECREF(arg);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002534 if (good == NULL)
2535 goto Fail_1;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002536 ok = PyObject_IsTrue(good);
2537 Py_DECREF(good);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002538 if (ok)
Guido van Rossum79f25d91997-04-29 20:08:16 +00002539 PyString_AS_STRING((PyStringObject *)result)[j++] =
2540 PyString_AS_STRING((PyStringObject *)item)[0];
Guido van Rossum12d12c51993-10-26 17:58:25 +00002541 }
2542
Guido van Rossum79f25d91997-04-29 20:08:16 +00002543 if (j < len && _PyString_Resize(&result, j) < 0)
Guido van Rossum12d12c51993-10-26 17:58:25 +00002544 return NULL;
2545
Guido van Rossum12d12c51993-10-26 17:58:25 +00002546 return result;
2547
Guido van Rossum12d12c51993-10-26 17:58:25 +00002548Fail_1:
Guido van Rossum79f25d91997-04-29 20:08:16 +00002549 Py_DECREF(result);
Guido van Rossum12d12c51993-10-26 17:58:25 +00002550 return NULL;
2551}