blob: c220d84100d3b829e1ca814ae4e7782dfdb29364 [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 **));
Guido van Rossum99fb7c71999-04-07 16:05:47 +0000402 char *s, *start, *end;
Guido van Rossum11950231999-03-25 21:16:07 +0000403 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 Rossum94390a41992-08-14 15:14:30 +0000815builtin_getattr(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000816 PyObject *self;
817 PyObject *args;
Guido van Rossum33894be1992-01-27 16:53:09 +0000818{
Guido van Rossum950ff291998-06-29 13:38:57 +0000819 PyObject *v, *result, *dflt = NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000820 PyObject *name;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000821
Guido van Rossum950ff291998-06-29 13:38:57 +0000822 if (!PyArg_ParseTuple(args, "OS|O:getattr", &v, &name, &dflt))
Guido van Rossum33894be1992-01-27 16:53:09 +0000823 return NULL;
Guido van Rossum950ff291998-06-29 13:38:57 +0000824 result = PyObject_GetAttr(v, name);
825 if (result == NULL && dflt != NULL) {
826 PyErr_Clear();
827 Py_INCREF(dflt);
828 result = dflt;
829 }
830 return result;
Guido van Rossum9bfef441993-03-29 10:43:31 +0000831}
832
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000833static char getattr_doc[] =
Guido van Rossum950ff291998-06-29 13:38:57 +0000834"getattr(object, name[, default]) -> value\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000835\n\
Guido van Rossum950ff291998-06-29 13:38:57 +0000836Get a named attribute from an object; getattr(x, 'y') is equivalent to x.y.\n\
837When a default argument is given, it is returned when the attribute doesn't\n\
838exist; without it, an exception is raised in that case.";
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000839
840
Guido van Rossum79f25d91997-04-29 20:08:16 +0000841static PyObject *
Guido van Rossum872537c1995-07-07 22:43:42 +0000842builtin_globals(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000843 PyObject *self;
844 PyObject *args;
Guido van Rossum872537c1995-07-07 22:43:42 +0000845{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000846 PyObject *d;
Guido van Rossum872537c1995-07-07 22:43:42 +0000847
Guido van Rossum79f25d91997-04-29 20:08:16 +0000848 if (!PyArg_ParseTuple(args, ""))
Guido van Rossum872537c1995-07-07 22:43:42 +0000849 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000850 d = PyEval_GetGlobals();
851 Py_INCREF(d);
Guido van Rossum872537c1995-07-07 22:43:42 +0000852 return d;
853}
854
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000855static char globals_doc[] =
856"globals() -> dictionary\n\
857\n\
858Return the dictionary containing the current scope's global variables.";
859
860
Guido van Rossum79f25d91997-04-29 20:08:16 +0000861static PyObject *
Guido van Rossum9bfef441993-03-29 10:43:31 +0000862builtin_hasattr(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000863 PyObject *self;
864 PyObject *args;
Guido van Rossum9bfef441993-03-29 10:43:31 +0000865{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000866 PyObject *v;
867 PyObject *name;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000868
Guido van Rossum79f25d91997-04-29 20:08:16 +0000869 if (!PyArg_ParseTuple(args, "OS:hasattr", &v, &name))
Guido van Rossum9bfef441993-03-29 10:43:31 +0000870 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000871 v = PyObject_GetAttr(v, name);
Guido van Rossum9bfef441993-03-29 10:43:31 +0000872 if (v == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000873 PyErr_Clear();
Guido van Rossum09df08a1998-05-22 00:51:39 +0000874 Py_INCREF(Py_False);
875 return Py_False;
Guido van Rossum9bfef441993-03-29 10:43:31 +0000876 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000877 Py_DECREF(v);
Guido van Rossum09df08a1998-05-22 00:51:39 +0000878 Py_INCREF(Py_True);
879 return Py_True;
Guido van Rossum33894be1992-01-27 16:53:09 +0000880}
881
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000882static char hasattr_doc[] =
883"hasattr(object, name) -> Boolean\n\
884\n\
885Return whether the object has an attribute with the given name.\n\
886(This is done by calling getattr(object, name) and catching exceptions.)";
887
888
Guido van Rossum79f25d91997-04-29 20:08:16 +0000889static PyObject *
Guido van Rossum5b722181993-03-30 17:46:03 +0000890builtin_id(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000891 PyObject *self;
892 PyObject *args;
Guido van Rossum5b722181993-03-30 17:46:03 +0000893{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000894 PyObject *v;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000895
Guido van Rossum79f25d91997-04-29 20:08:16 +0000896 if (!PyArg_ParseTuple(args, "O:id", &v))
Guido van Rossum5b722181993-03-30 17:46:03 +0000897 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000898 return PyInt_FromLong((long)v);
Guido van Rossum5b722181993-03-30 17:46:03 +0000899}
900
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000901static char id_doc[] =
902"id(object) -> integer\n\
903\n\
904Return the identity of an object. This is guaranteed to be unique among\n\
905simultaneously existing objects. (Hint: it's the object's memory address.)";
906
907
Guido van Rossum79f25d91997-04-29 20:08:16 +0000908static PyObject *
Guido van Rossum12d12c51993-10-26 17:58:25 +0000909builtin_map(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000910 PyObject *self;
911 PyObject *args;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000912{
913 typedef struct {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000914 PyObject *seq;
915 PySequenceMethods *sqf;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000916 int len;
917 } sequence;
918
Guido van Rossum79f25d91997-04-29 20:08:16 +0000919 PyObject *func, *result;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000920 sequence *seqs = NULL, *sqp;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000921 int n, len;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000922 register int i, j;
923
Guido van Rossum79f25d91997-04-29 20:08:16 +0000924 n = PyTuple_Size(args);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000925 if (n < 2) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000926 PyErr_SetString(PyExc_TypeError,
927 "map() requires at least two args");
Guido van Rossum12d12c51993-10-26 17:58:25 +0000928 return NULL;
929 }
930
Guido van Rossum79f25d91997-04-29 20:08:16 +0000931 func = PyTuple_GetItem(args, 0);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000932 n--;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000933
Guido van Rossumfa4ac711998-07-10 17:37:30 +0000934 if (func == Py_None && n == 1) {
935 /* map(None, S) is the same as list(S). */
936 return PySequence_List(PyTuple_GetItem(args, 1));
937 }
938
Guido van Rossum79f25d91997-04-29 20:08:16 +0000939 if ((seqs = PyMem_NEW(sequence, n)) == NULL) {
940 PyErr_NoMemory();
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000941 goto Fail_2;
942 }
Guido van Rossum12d12c51993-10-26 17:58:25 +0000943
Guido van Rossum2d951851994-08-29 12:52:16 +0000944 for (len = 0, i = 0, sqp = seqs; i < n; ++i, ++sqp) {
Guido van Rossum12d12c51993-10-26 17:58:25 +0000945 int curlen;
Guido van Rossum09df08a1998-05-22 00:51:39 +0000946 PySequenceMethods *sqf;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000947
Guido van Rossum79f25d91997-04-29 20:08:16 +0000948 if ((sqp->seq = PyTuple_GetItem(args, i + 1)) == NULL)
Guido van Rossum12d12c51993-10-26 17:58:25 +0000949 goto Fail_2;
950
Guido van Rossum09df08a1998-05-22 00:51:39 +0000951 sqp->sqf = sqf = sqp->seq->ob_type->tp_as_sequence;
952 if (sqf == NULL ||
953 sqf->sq_length == NULL ||
954 sqf->sq_item == NULL)
955 {
Guido van Rossum12d12c51993-10-26 17:58:25 +0000956 static char errmsg[] =
957 "argument %d to map() must be a sequence object";
Guido van Rossum15e33a41997-04-30 19:00:27 +0000958 char errbuf[sizeof(errmsg) + 25];
Guido van Rossum12d12c51993-10-26 17:58:25 +0000959
960 sprintf(errbuf, errmsg, i+2);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000961 PyErr_SetString(PyExc_TypeError, errbuf);
Guido van Rossum12d12c51993-10-26 17:58:25 +0000962 goto Fail_2;
963 }
964
965 if ((curlen = sqp->len = (*sqp->sqf->sq_length)(sqp->seq)) < 0)
966 goto Fail_2;
967
968 if (curlen > len)
969 len = curlen;
970 }
971
Guido van Rossum79f25d91997-04-29 20:08:16 +0000972 if ((result = (PyObject *) PyList_New(len)) == NULL)
Guido van Rossum12d12c51993-10-26 17:58:25 +0000973 goto Fail_2;
974
Guido van Rossum2d951851994-08-29 12:52:16 +0000975 for (i = 0; ; ++i) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000976 PyObject *alist, *item=NULL, *value;
Guido van Rossum2d951851994-08-29 12:52:16 +0000977 int any = 0;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000978
Guido van Rossum79f25d91997-04-29 20:08:16 +0000979 if (func == Py_None && n == 1)
Guido van Rossum32120311995-07-10 13:52:21 +0000980 alist = NULL;
Guido van Rossum2d951851994-08-29 12:52:16 +0000981 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000982 if ((alist = PyTuple_New(n)) == NULL)
Guido van Rossum2d951851994-08-29 12:52:16 +0000983 goto Fail_1;
984 }
Guido van Rossum12d12c51993-10-26 17:58:25 +0000985
986 for (j = 0, sqp = seqs; j < n; ++j, ++sqp) {
Guido van Rossum2d951851994-08-29 12:52:16 +0000987 if (sqp->len < 0) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000988 Py_INCREF(Py_None);
989 item = Py_None;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000990 }
Guido van Rossum12d12c51993-10-26 17:58:25 +0000991 else {
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000992 item = (*sqp->sqf->sq_item)(sqp->seq, i);
Guido van Rossum2d951851994-08-29 12:52:16 +0000993 if (item == NULL) {
Barry Warsawcde8b1b1997-08-22 21:14:38 +0000994 if (PyErr_ExceptionMatches(
995 PyExc_IndexError))
996 {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000997 PyErr_Clear();
998 Py_INCREF(Py_None);
999 item = Py_None;
Guido van Rossum2d951851994-08-29 12:52:16 +00001000 sqp->len = -1;
1001 }
1002 else {
1003 goto Fail_0;
1004 }
1005 }
1006 else
1007 any = 1;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001008
Guido van Rossum12d12c51993-10-26 17:58:25 +00001009 }
Guido van Rossum32120311995-07-10 13:52:21 +00001010 if (!alist)
Guido van Rossum2d951851994-08-29 12:52:16 +00001011 break;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001012 if (PyTuple_SetItem(alist, j, item) < 0) {
1013 Py_DECREF(item);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00001014 goto Fail_0;
Guido van Rossum2d951851994-08-29 12:52:16 +00001015 }
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00001016 continue;
1017
1018 Fail_0:
Guido van Rossum79f25d91997-04-29 20:08:16 +00001019 Py_XDECREF(alist);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00001020 goto Fail_1;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001021 }
1022
Guido van Rossum32120311995-07-10 13:52:21 +00001023 if (!alist)
1024 alist = item;
Guido van Rossum2d951851994-08-29 12:52:16 +00001025
1026 if (!any) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001027 Py_DECREF(alist);
Guido van Rossum2d951851994-08-29 12:52:16 +00001028 break;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001029 }
Guido van Rossum2d951851994-08-29 12:52:16 +00001030
Guido van Rossum79f25d91997-04-29 20:08:16 +00001031 if (func == Py_None)
Guido van Rossum32120311995-07-10 13:52:21 +00001032 value = alist;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001033 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001034 value = PyEval_CallObject(func, alist);
1035 Py_DECREF(alist);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00001036 if (value == NULL)
1037 goto Fail_1;
Guido van Rossum2d951851994-08-29 12:52:16 +00001038 }
1039 if (i >= len) {
Barry Warsawfa77e091999-01-28 18:49:12 +00001040 int status = PyList_Append(result, value);
Barry Warsaw21332871999-01-28 04:21:35 +00001041 Py_DECREF(value);
Barry Warsawfa77e091999-01-28 18:49:12 +00001042 if (status < 0)
1043 goto Fail_1;
Guido van Rossum2d951851994-08-29 12:52:16 +00001044 }
1045 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001046 if (PyList_SetItem(result, i, value) < 0)
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00001047 goto Fail_1;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001048 }
1049 }
1050
Guido van Rossumfa4ac711998-07-10 17:37:30 +00001051 if (i < len && PyList_SetSlice(result, i, len, NULL) < 0)
1052 goto Fail_1;
1053
Guido van Rossum79f25d91997-04-29 20:08:16 +00001054 PyMem_DEL(seqs);
Guido van Rossum12d12c51993-10-26 17:58:25 +00001055 return result;
1056
Guido van Rossum12d12c51993-10-26 17:58:25 +00001057Fail_1:
Guido van Rossum79f25d91997-04-29 20:08:16 +00001058 Py_DECREF(result);
Guido van Rossum12d12c51993-10-26 17:58:25 +00001059Fail_2:
Guido van Rossum79f25d91997-04-29 20:08:16 +00001060 if (seqs) PyMem_DEL(seqs);
Guido van Rossum12d12c51993-10-26 17:58:25 +00001061 return NULL;
1062}
1063
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001064static char map_doc[] =
1065"map(function, sequence[, sequence, ...]) -> list\n\
1066\n\
1067Return a list of the results of applying the function to the items of\n\
1068the argument sequence(s). If more than one sequence is given, the\n\
1069function is called with an argument list consisting of the corresponding\n\
1070item of each sequence, substituting None for missing values when not all\n\
1071sequences have the same length. If the function is None, return a list of\n\
1072the items of the sequence (or a list of tuples if more than one sequence).";
1073
1074
Guido van Rossum79f25d91997-04-29 20:08:16 +00001075static PyObject *
Guido van Rossum94390a41992-08-14 15:14:30 +00001076builtin_setattr(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001077 PyObject *self;
1078 PyObject *args;
Guido van Rossum33894be1992-01-27 16:53:09 +00001079{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001080 PyObject *v;
1081 PyObject *name;
1082 PyObject *value;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001083
Guido van Rossum79f25d91997-04-29 20:08:16 +00001084 if (!PyArg_ParseTuple(args, "OSO:setattr", &v, &name, &value))
Guido van Rossum33894be1992-01-27 16:53:09 +00001085 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001086 if (PyObject_SetAttr(v, name, value) != 0)
Guido van Rossum33894be1992-01-27 16:53:09 +00001087 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001088 Py_INCREF(Py_None);
1089 return Py_None;
Guido van Rossum33894be1992-01-27 16:53:09 +00001090}
1091
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001092static char setattr_doc[] =
1093"setattr(object, name, value)\n\
1094\n\
1095Set a named attribute on an object; setattr(x, 'y', v) is equivalent to\n\
1096``x.y = v''.";
1097
1098
Guido van Rossum79f25d91997-04-29 20:08:16 +00001099static PyObject *
Guido van Rossum14144fc1994-08-29 12:53:40 +00001100builtin_delattr(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001101 PyObject *self;
1102 PyObject *args;
Guido van Rossum14144fc1994-08-29 12:53:40 +00001103{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001104 PyObject *v;
1105 PyObject *name;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001106
Guido van Rossum79f25d91997-04-29 20:08:16 +00001107 if (!PyArg_ParseTuple(args, "OS:delattr", &v, &name))
Guido van Rossum14144fc1994-08-29 12:53:40 +00001108 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001109 if (PyObject_SetAttr(v, name, (PyObject *)NULL) != 0)
Guido van Rossum14144fc1994-08-29 12:53:40 +00001110 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001111 Py_INCREF(Py_None);
1112 return Py_None;
Guido van Rossum14144fc1994-08-29 12:53:40 +00001113}
1114
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001115static char delattr_doc[] =
Guido van Rossumdf12a591998-11-23 22:13:04 +00001116"delattr(object, name)\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001117\n\
1118Delete a named attribute on an object; delattr(x, 'y') is equivalent to\n\
1119``del x.y''.";
1120
1121
Guido van Rossum79f25d91997-04-29 20:08:16 +00001122static PyObject *
Guido van Rossum9bfef441993-03-29 10:43:31 +00001123builtin_hash(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001124 PyObject *self;
1125 PyObject *args;
Guido van Rossum9bfef441993-03-29 10:43:31 +00001126{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001127 PyObject *v;
Guido van Rossum9bfef441993-03-29 10:43:31 +00001128 long x;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001129
Guido van Rossum79f25d91997-04-29 20:08:16 +00001130 if (!PyArg_ParseTuple(args, "O:hash", &v))
Guido van Rossum9bfef441993-03-29 10:43:31 +00001131 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001132 x = PyObject_Hash(v);
Guido van Rossum9bfef441993-03-29 10:43:31 +00001133 if (x == -1)
1134 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001135 return PyInt_FromLong(x);
Guido van Rossum9bfef441993-03-29 10:43:31 +00001136}
1137
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001138static char hash_doc[] =
1139"hash(object) -> integer\n\
1140\n\
1141Return a hash value for the object. Two objects with the same value have\n\
1142the same hash value. The reverse is not necessarily true, but likely.";
1143
1144
Guido van Rossum79f25d91997-04-29 20:08:16 +00001145static PyObject *
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001146builtin_hex(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001147 PyObject *self;
1148 PyObject *args;
Guido van Rossum006bcd41991-10-24 14:54:44 +00001149{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001150 PyObject *v;
1151 PyNumberMethods *nb;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001152
Guido van Rossum79f25d91997-04-29 20:08:16 +00001153 if (!PyArg_ParseTuple(args, "O:hex", &v))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001154 return NULL;
Guido van Rossum1899c2e1992-09-12 11:09:23 +00001155
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001156 if ((nb = v->ob_type->tp_as_number) == NULL ||
Guido van Rossum1899c2e1992-09-12 11:09:23 +00001157 nb->nb_hex == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001158 PyErr_SetString(PyExc_TypeError,
Guido van Rossum1899c2e1992-09-12 11:09:23 +00001159 "hex() argument can't be converted to hex");
1160 return NULL;
Guido van Rossum006bcd41991-10-24 14:54:44 +00001161 }
Guido van Rossum1899c2e1992-09-12 11:09:23 +00001162 return (*nb->nb_hex)(v);
Guido van Rossum006bcd41991-10-24 14:54:44 +00001163}
1164
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001165static char hex_doc[] =
1166"hex(number) -> string\n\
1167\n\
1168Return the hexadecimal representation of an integer or long integer.";
1169
1170
Guido van Rossum79f25d91997-04-29 20:08:16 +00001171static PyObject *builtin_raw_input Py_PROTO((PyObject *, PyObject *));
Guido van Rossum3165fe61992-09-25 21:59:05 +00001172
Guido van Rossum79f25d91997-04-29 20:08:16 +00001173static PyObject *
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001174builtin_input(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001175 PyObject *self;
1176 PyObject *args;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001177{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001178 PyObject *line;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001179 char *str;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001180 PyObject *res;
1181 PyObject *globals, *locals;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001182
1183 line = builtin_raw_input(self, args);
Guido van Rossum3165fe61992-09-25 21:59:05 +00001184 if (line == NULL)
1185 return line;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001186 if (!PyArg_Parse(line, "s;embedded '\\0' in input line", &str))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001187 return NULL;
1188 while (*str == ' ' || *str == '\t')
1189 str++;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001190 globals = PyEval_GetGlobals();
1191 locals = PyEval_GetLocals();
1192 if (PyDict_GetItemString(globals, "__builtins__") == NULL) {
1193 if (PyDict_SetItemString(globals, "__builtins__",
1194 PyEval_GetBuiltins()) != 0)
Guido van Rossum6135a871995-01-09 17:53:26 +00001195 return NULL;
1196 }
Guido van Rossumb05a5c71997-05-07 17:46:13 +00001197 res = PyRun_String(str, Py_eval_input, globals, locals);
Guido van Rossum79f25d91997-04-29 20:08:16 +00001198 Py_DECREF(line);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001199 return res;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001200}
1201
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001202static char input_doc[] =
1203"input([prompt]) -> value\n\
1204\n\
1205Equivalent to eval(raw_input(prompt)).";
1206
1207
Guido van Rossume8811f81997-02-14 15:48:05 +00001208static PyObject *
1209builtin_intern(self, args)
1210 PyObject *self;
1211 PyObject *args;
1212{
1213 PyObject *s;
1214 if (!PyArg_ParseTuple(args, "S", &s))
1215 return NULL;
1216 Py_INCREF(s);
1217 PyString_InternInPlace(&s);
1218 return s;
1219}
1220
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001221static char intern_doc[] =
1222"intern(string) -> string\n\
1223\n\
1224``Intern'' the given string. This enters the string in the (global)\n\
1225table of interned strings whose purpose is to speed up dictionary lookups.\n\
1226Return the string itself or the previously interned string object with the\n\
1227same value.";
1228
1229
Guido van Rossum79f25d91997-04-29 20:08:16 +00001230static PyObject *
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001231builtin_int(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001232 PyObject *self;
1233 PyObject *args;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001234{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001235 PyObject *v;
Barry Warsaw226ae6c1999-10-12 19:54:53 +00001236 int base = -909; /* unlikely! */
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001237
Barry Warsaw226ae6c1999-10-12 19:54:53 +00001238 if (!PyArg_ParseTuple(args, "O|i:int", &v, &base))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001239 return NULL;
Barry Warsaw226ae6c1999-10-12 19:54:53 +00001240 if (base == -909)
1241 return PyNumber_Int(v);
1242 else if (!PyString_Check(v)) {
1243 PyErr_SetString(PyExc_TypeError,
1244 "can't convert non-string with explicit base");
1245 return NULL;
1246 }
1247 return PyInt_FromString(PyString_AS_STRING(v), NULL, base);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001248}
1249
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001250static char int_doc[] =
Barry Warsaw226ae6c1999-10-12 19:54:53 +00001251"int(x[, base]) -> integer\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001252\n\
Barry Warsaw226ae6c1999-10-12 19:54:53 +00001253Convert a string or number to an integer, if possible. A floating point\n\
1254argument will be truncated towards zero (this does not include a string\n\
1255representation of a floating point number!) When converting a string, use\n\
1256the optional base. It is an error to supply a base when converting a\n\
1257non-string.";
1258
1259
1260static PyObject *
1261builtin_long(self, args)
1262 PyObject *self;
1263 PyObject *args;
1264{
1265 PyObject *v;
1266 int base = -909; /* unlikely! */
1267
1268 if (!PyArg_ParseTuple(args, "O|i:long", &v, &base))
1269 return NULL;
1270 if (base == -909)
1271 return PyNumber_Long(v);
1272 else if (!PyString_Check(v)) {
1273 PyErr_SetString(PyExc_TypeError,
1274 "can't convert non-string with explicit base");
1275 return NULL;
1276 }
1277 return PyLong_FromString(PyString_AS_STRING(v), NULL, base);
1278}
1279
1280static char long_doc[] =
1281"long(x) -> long integer\n\
1282long(x, base) -> long integer\n\
1283\n\
1284Convert a string or number to a long integer, if possible. A floating\n\
1285point argument will be truncated towards zero (this does not include a\n\
1286string representation of a floating point number!) When converting a\n\
1287string, use the given base. It is an error to supply a base when\n\
1288converting a non-string.";
1289
1290
1291static PyObject *
1292builtin_float(self, args)
1293 PyObject *self;
1294 PyObject *args;
1295{
1296 PyObject *v;
1297
1298 if (!PyArg_ParseTuple(args, "O:float", &v))
1299 return NULL;
1300 if (PyString_Check(v))
1301 return PyFloat_FromString(v, NULL);
1302 return PyNumber_Float(v);
1303}
1304
1305static char float_doc[] =
1306"float(x) -> floating point number\n\
1307\n\
1308Convert a string or number to a floating point number, if possible.";
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001309
1310
Guido van Rossum79f25d91997-04-29 20:08:16 +00001311static PyObject *
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001312builtin_len(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001313 PyObject *self;
1314 PyObject *args;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001315{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001316 PyObject *v;
Guido van Rossum8ea9f4d1998-06-29 22:26:50 +00001317 long res;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001318
Guido van Rossum79f25d91997-04-29 20:08:16 +00001319 if (!PyArg_ParseTuple(args, "O:len", &v))
Guido van Rossum3f5da241990-12-20 15:06:42 +00001320 return NULL;
Guido van Rossum8ea9f4d1998-06-29 22:26:50 +00001321 res = PyObject_Length(v);
1322 if (res < 0 && PyErr_Occurred())
1323 return NULL;
1324 return PyInt_FromLong(res);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001325}
1326
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001327static char len_doc[] =
1328"len(object) -> integer\n\
1329\n\
1330Return the number of items of a sequence or mapping.";
1331
1332
Guido van Rossum79f25d91997-04-29 20:08:16 +00001333static PyObject *
Guido van Rossumd1705771996-04-09 02:41:06 +00001334builtin_list(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001335 PyObject *self;
1336 PyObject *args;
Guido van Rossumd1705771996-04-09 02:41:06 +00001337{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001338 PyObject *v;
Guido van Rossumd1705771996-04-09 02:41:06 +00001339
Guido van Rossum79f25d91997-04-29 20:08:16 +00001340 if (!PyArg_ParseTuple(args, "O:list", &v))
Guido van Rossumd1705771996-04-09 02:41:06 +00001341 return NULL;
Guido van Rossum09df08a1998-05-22 00:51:39 +00001342 return PySequence_List(v);
Guido van Rossumd1705771996-04-09 02:41:06 +00001343}
1344
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001345static char list_doc[] =
1346"list(sequence) -> list\n\
1347\n\
1348Return a new list whose items are the same as those of the argument sequence.";
1349
Guido van Rossum8861b741996-07-30 16:49:37 +00001350
1351static PyObject *
1352builtin_slice(self, args)
1353 PyObject *self;
1354 PyObject *args;
1355{
Guido van Rossum09df08a1998-05-22 00:51:39 +00001356 PyObject *start, *stop, *step;
Guido van Rossum8861b741996-07-30 16:49:37 +00001357
Guido van Rossum09df08a1998-05-22 00:51:39 +00001358 start = stop = step = NULL;
Guido van Rossum8861b741996-07-30 16:49:37 +00001359
Guido van Rossum09df08a1998-05-22 00:51:39 +00001360 if (!PyArg_ParseTuple(args, "O|OO:slice", &start, &stop, &step))
1361 return NULL;
Guido van Rossum8861b741996-07-30 16:49:37 +00001362
Guido van Rossum09df08a1998-05-22 00:51:39 +00001363 /* This swapping of stop and start is to maintain similarity with
1364 range(). */
1365 if (stop == NULL) {
1366 stop = start;
1367 start = NULL;
1368 }
1369 return PySlice_New(start, stop, step);
Guido van Rossum8861b741996-07-30 16:49:37 +00001370}
1371
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001372static char slice_doc[] =
Fred Drake3d587441999-07-19 15:21:16 +00001373"slice([start,] stop[, step]) -> slice object\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001374\n\
1375Create a slice object. This is used for slicing by the Numeric extensions.";
1376
1377
Guido van Rossum79f25d91997-04-29 20:08:16 +00001378static PyObject *
Guido van Rossum872537c1995-07-07 22:43:42 +00001379builtin_locals(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001380 PyObject *self;
1381 PyObject *args;
Guido van Rossum872537c1995-07-07 22:43:42 +00001382{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001383 PyObject *d;
Guido van Rossum872537c1995-07-07 22:43:42 +00001384
Guido van Rossum79f25d91997-04-29 20:08:16 +00001385 if (!PyArg_ParseTuple(args, ""))
Guido van Rossum872537c1995-07-07 22:43:42 +00001386 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001387 d = PyEval_GetLocals();
1388 Py_INCREF(d);
Guido van Rossum872537c1995-07-07 22:43:42 +00001389 return d;
1390}
1391
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001392static char locals_doc[] =
1393"locals() -> dictionary\n\
1394\n\
1395Return the dictionary containing the current scope's local variables.";
1396
1397
Guido van Rossum79f25d91997-04-29 20:08:16 +00001398static PyObject *
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001399min_max(args, sign)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001400 PyObject *args;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001401 int sign;
1402{
Guido van Rossum2d951851994-08-29 12:52:16 +00001403 int i;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001404 PyObject *v, *w, *x;
1405 PySequenceMethods *sq;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001406
Guido van Rossum79f25d91997-04-29 20:08:16 +00001407 if (PyTuple_Size(args) > 1)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001408 v = args;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001409 else if (!PyArg_ParseTuple(args, "O:min/max", &v))
Guido van Rossum3f5da241990-12-20 15:06:42 +00001410 return NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001411 sq = v->ob_type->tp_as_sequence;
Guido van Rossum09df08a1998-05-22 00:51:39 +00001412 if (sq == NULL || sq->sq_item == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001413 PyErr_SetString(PyExc_TypeError,
1414 "min() or max() of non-sequence");
Guido van Rossum3f5da241990-12-20 15:06:42 +00001415 return NULL;
1416 }
Guido van Rossum2d951851994-08-29 12:52:16 +00001417 w = NULL;
1418 for (i = 0; ; i++) {
Guido van Rossum3f5da241990-12-20 15:06:42 +00001419 x = (*sq->sq_item)(v, i); /* Implies INCREF */
Guido van Rossum2d951851994-08-29 12:52:16 +00001420 if (x == NULL) {
Barry Warsawcde8b1b1997-08-22 21:14:38 +00001421 if (PyErr_ExceptionMatches(PyExc_IndexError)) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001422 PyErr_Clear();
Guido van Rossum2d951851994-08-29 12:52:16 +00001423 break;
1424 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00001425 Py_XDECREF(w);
Guido van Rossum2d951851994-08-29 12:52:16 +00001426 return NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001427 }
Guido van Rossum2d951851994-08-29 12:52:16 +00001428 if (w == NULL)
1429 w = x;
1430 else {
Guido van Rossumc8b6df91997-05-23 00:06:51 +00001431 int c = PyObject_Compare(x, w);
1432 if (c && PyErr_Occurred()) {
1433 Py_DECREF(x);
1434 Py_XDECREF(w);
1435 return NULL;
1436 }
1437 if (c * sign > 0) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001438 Py_DECREF(w);
Guido van Rossum2d951851994-08-29 12:52:16 +00001439 w = x;
1440 }
1441 else
Guido van Rossum79f25d91997-04-29 20:08:16 +00001442 Py_DECREF(x);
Guido van Rossum2d951851994-08-29 12:52:16 +00001443 }
Guido van Rossum3f5da241990-12-20 15:06:42 +00001444 }
Guido van Rossum2d951851994-08-29 12:52:16 +00001445 if (w == NULL)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001446 PyErr_SetString(PyExc_ValueError,
1447 "min() or max() of empty sequence");
Guido van Rossum3f5da241990-12-20 15:06:42 +00001448 return w;
1449}
1450
Guido van Rossum79f25d91997-04-29 20:08:16 +00001451static PyObject *
Guido van Rossum3f5da241990-12-20 15:06:42 +00001452builtin_min(self, v)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001453 PyObject *self;
1454 PyObject *v;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001455{
1456 return min_max(v, -1);
1457}
1458
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001459static char min_doc[] =
1460"min(sequence) -> value\n\
1461min(a, b, c, ...) -> value\n\
1462\n\
1463With a single sequence argument, return its smallest item.\n\
1464With two or more arguments, return the smallest argument.";
1465
1466
Guido van Rossum79f25d91997-04-29 20:08:16 +00001467static PyObject *
Guido van Rossum3f5da241990-12-20 15:06:42 +00001468builtin_max(self, v)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001469 PyObject *self;
1470 PyObject *v;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001471{
1472 return min_max(v, 1);
1473}
1474
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001475static char max_doc[] =
1476"max(sequence) -> value\n\
1477max(a, b, c, ...) -> value\n\
1478\n\
1479With a single sequence argument, return its largest item.\n\
1480With two or more arguments, return the largest argument.";
1481
1482
Guido van Rossum79f25d91997-04-29 20:08:16 +00001483static PyObject *
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001484builtin_oct(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001485 PyObject *self;
1486 PyObject *args;
Guido van Rossum006bcd41991-10-24 14:54:44 +00001487{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001488 PyObject *v;
1489 PyNumberMethods *nb;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001490
Guido van Rossum79f25d91997-04-29 20:08:16 +00001491 if (!PyArg_ParseTuple(args, "O:oct", &v))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001492 return NULL;
Guido van Rossum1899c2e1992-09-12 11:09:23 +00001493 if (v == NULL || (nb = v->ob_type->tp_as_number) == NULL ||
1494 nb->nb_oct == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001495 PyErr_SetString(PyExc_TypeError,
Guido van Rossum1899c2e1992-09-12 11:09:23 +00001496 "oct() argument can't be converted to oct");
1497 return NULL;
Guido van Rossum006bcd41991-10-24 14:54:44 +00001498 }
Guido van Rossum1899c2e1992-09-12 11:09:23 +00001499 return (*nb->nb_oct)(v);
Guido van Rossum006bcd41991-10-24 14:54:44 +00001500}
1501
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001502static char oct_doc[] =
1503"oct(number) -> string\n\
1504\n\
1505Return the octal representation of an integer or long integer.";
1506
1507
Guido van Rossum79f25d91997-04-29 20:08:16 +00001508static PyObject *
Guido van Rossum94390a41992-08-14 15:14:30 +00001509builtin_open(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001510 PyObject *self;
1511 PyObject *args;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001512{
Guido van Rossum2d951851994-08-29 12:52:16 +00001513 char *name;
1514 char *mode = "r";
1515 int bufsize = -1;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001516 PyObject *f;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001517
Guido van Rossum79f25d91997-04-29 20:08:16 +00001518 if (!PyArg_ParseTuple(args, "s|si:open", &name, &mode, &bufsize))
Guido van Rossum3f5da241990-12-20 15:06:42 +00001519 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001520 f = PyFile_FromString(name, mode);
Guido van Rossum2d951851994-08-29 12:52:16 +00001521 if (f != NULL)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001522 PyFile_SetBufSize(f, bufsize);
Guido van Rossum2d951851994-08-29 12:52:16 +00001523 return f;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001524}
1525
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001526static char open_doc[] =
1527"open(filename[, mode[, buffering]]) -> file object\n\
1528\n\
1529Open a file. The mode can be 'r', 'w' or 'a' for reading (default),\n\
1530writing or appending. The file will be created if it doesn't exist\n\
1531when opened for writing or appending; it will be truncated when\n\
1532opened for writing. Add a 'b' to the mode for binary files.\n\
1533Add a '+' to the mode to allow simultaneous reading and writing.\n\
1534If the buffering argument is given, 0 means unbuffered, 1 means line\n\
1535buffered, and larger numbers specify the buffer size.";
1536
1537
Guido van Rossum79f25d91997-04-29 20:08:16 +00001538static PyObject *
Guido van Rossum94390a41992-08-14 15:14:30 +00001539builtin_ord(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001540 PyObject *self;
1541 PyObject *args;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001542{
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001543 char c;
1544
Guido van Rossum79f25d91997-04-29 20:08:16 +00001545 if (!PyArg_ParseTuple(args, "c:ord", &c))
Guido van Rossum3f5da241990-12-20 15:06:42 +00001546 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001547 return PyInt_FromLong((long)(c & 0xff));
Guido van Rossum3f5da241990-12-20 15:06:42 +00001548}
1549
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001550static char ord_doc[] =
1551"ord(c) -> integer\n\
1552\n\
1553Return the integer ordinal of a one character string.";
1554
1555
Guido van Rossum79f25d91997-04-29 20:08:16 +00001556static PyObject *
Guido van Rossum6a00cd81995-01-07 12:39:01 +00001557builtin_pow(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001558 PyObject *self;
1559 PyObject *args;
Guido van Rossum6a00cd81995-01-07 12:39:01 +00001560{
Guido van Rossum09df08a1998-05-22 00:51:39 +00001561 PyObject *v, *w, *z = Py_None;
Guido van Rossum6a00cd81995-01-07 12:39:01 +00001562
Guido van Rossum79f25d91997-04-29 20:08:16 +00001563 if (!PyArg_ParseTuple(args, "OO|O:pow", &v, &w, &z))
Guido van Rossum6a00cd81995-01-07 12:39:01 +00001564 return NULL;
Guido van Rossum09df08a1998-05-22 00:51:39 +00001565 return PyNumber_Power(v, w, z);
Guido van Rossumd4905451991-05-05 20:00:36 +00001566}
1567
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001568static char pow_doc[] =
1569"pow(x, y[, z]) -> number\n\
1570\n\
1571With two arguments, equivalent to x**y. With three arguments,\n\
1572equivalent to (x**y) % z, but may be more efficient (e.g. for longs).";
1573
1574
Guido van Rossum124eff01999-02-23 16:11:01 +00001575/* Return number of items in range/xrange (lo, hi, step). step > 0
1576 * required. Return a value < 0 if & only if the true value is too
1577 * large to fit in a signed long.
1578 */
1579static long
1580get_len_of_range(lo, hi, step)
1581 long lo;
1582 long hi;
1583 long step; /* must be > 0 */
1584{
1585 /* -------------------------------------------------------------
1586 If lo >= hi, the range is empty.
1587 Else if n values are in the range, the last one is
1588 lo + (n-1)*step, which must be <= hi-1. Rearranging,
1589 n <= (hi - lo - 1)/step + 1, so taking the floor of the RHS gives
1590 the proper value. Since lo < hi in this case, hi-lo-1 >= 0, so
1591 the RHS is non-negative and so truncation is the same as the
1592 floor. Letting M be the largest positive long, the worst case
1593 for the RHS numerator is hi=M, lo=-M-1, and then
1594 hi-lo-1 = M-(-M-1)-1 = 2*M. Therefore unsigned long has enough
1595 precision to compute the RHS exactly.
1596 ---------------------------------------------------------------*/
1597 long n = 0;
1598 if (lo < hi) {
1599 unsigned long uhi = (unsigned long)hi;
1600 unsigned long ulo = (unsigned long)lo;
1601 unsigned long diff = uhi - ulo - 1;
1602 n = (long)(diff / (unsigned long)step + 1);
1603 }
1604 return n;
1605}
1606
Guido van Rossum79f25d91997-04-29 20:08:16 +00001607static PyObject *
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001608builtin_range(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001609 PyObject *self;
1610 PyObject *args;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001611{
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001612 long ilow = 0, ihigh = 0, istep = 1;
Guido van Rossum124eff01999-02-23 16:11:01 +00001613 long bign;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001614 int i, n;
Guido van Rossum124eff01999-02-23 16:11:01 +00001615
Guido van Rossum79f25d91997-04-29 20:08:16 +00001616 PyObject *v;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001617
Guido van Rossum79f25d91997-04-29 20:08:16 +00001618 if (PyTuple_Size(args) <= 1) {
1619 if (!PyArg_ParseTuple(args,
Guido van Rossum0865dd91995-01-17 16:30:22 +00001620 "l;range() requires 1-3 int arguments",
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001621 &ihigh))
1622 return NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001623 }
1624 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001625 if (!PyArg_ParseTuple(args,
Guido van Rossum0865dd91995-01-17 16:30:22 +00001626 "ll|l;range() requires 1-3 int arguments",
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001627 &ilow, &ihigh, &istep))
Guido van Rossum3f5da241990-12-20 15:06:42 +00001628 return NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001629 }
1630 if (istep == 0) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001631 PyErr_SetString(PyExc_ValueError, "zero step for range()");
Guido van Rossum3f5da241990-12-20 15:06:42 +00001632 return NULL;
1633 }
Guido van Rossum124eff01999-02-23 16:11:01 +00001634 if (istep > 0)
1635 bign = get_len_of_range(ilow, ihigh, istep);
1636 else
1637 bign = get_len_of_range(ihigh, ilow, -istep);
1638 n = (int)bign;
1639 if (bign < 0 || (long)n != bign) {
Guido van Rossume23cde21999-01-12 05:07:47 +00001640 PyErr_SetString(PyExc_OverflowError,
Guido van Rossum124eff01999-02-23 16:11:01 +00001641 "range() has too many items");
Guido van Rossume23cde21999-01-12 05:07:47 +00001642 return NULL;
1643 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00001644 v = PyList_New(n);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001645 if (v == NULL)
1646 return NULL;
1647 for (i = 0; i < n; i++) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001648 PyObject *w = PyInt_FromLong(ilow);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001649 if (w == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001650 Py_DECREF(v);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001651 return NULL;
1652 }
Guido van Rossuma937d141998-04-24 18:22:02 +00001653 PyList_SET_ITEM(v, i, w);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001654 ilow += istep;
1655 }
1656 return v;
1657}
1658
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001659static char range_doc[] =
1660"range([start,] stop[, step]) -> list of integers\n\
1661\n\
1662Return a list containing an arithmetic progression of integers.\n\
1663range(i, j) returns [i, i+1, i+2, ..., j-1]; start (!) defaults to 0.\n\
1664When step is given, it specifies the increment (or decrement).\n\
1665For example, range(4) returns [0, 1, 2, 3]. The end point is omitted!\n\
1666These are exactly the valid indices for a list of 4 elements.";
1667
1668
Guido van Rossum79f25d91997-04-29 20:08:16 +00001669static PyObject *
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001670builtin_xrange(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001671 PyObject *self;
1672 PyObject *args;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001673{
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001674 long ilow = 0, ihigh = 0, istep = 1;
Guido van Rossum0865dd91995-01-17 16:30:22 +00001675 long n;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001676
Guido van Rossum79f25d91997-04-29 20:08:16 +00001677 if (PyTuple_Size(args) <= 1) {
1678 if (!PyArg_ParseTuple(args,
Guido van Rossum0865dd91995-01-17 16:30:22 +00001679 "l;xrange() requires 1-3 int arguments",
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001680 &ihigh))
1681 return NULL;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001682 }
1683 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001684 if (!PyArg_ParseTuple(args,
Guido van Rossum0865dd91995-01-17 16:30:22 +00001685 "ll|l;xrange() requires 1-3 int arguments",
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001686 &ilow, &ihigh, &istep))
Guido van Rossum12d12c51993-10-26 17:58:25 +00001687 return NULL;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001688 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001689 if (istep == 0) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001690 PyErr_SetString(PyExc_ValueError, "zero step for xrange()");
Guido van Rossum12d12c51993-10-26 17:58:25 +00001691 return NULL;
1692 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001693 if (istep > 0)
Guido van Rossum124eff01999-02-23 16:11:01 +00001694 n = get_len_of_range(ilow, ihigh, istep);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001695 else
Guido van Rossum124eff01999-02-23 16:11:01 +00001696 n = get_len_of_range(ihigh, ilow, -istep);
1697 if (n < 0) {
1698 PyErr_SetString(PyExc_OverflowError,
1699 "xrange() has more than sys.maxint items");
1700 return NULL;
1701 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00001702 return PyRange_New(ilow, n, istep, 1);
Guido van Rossum12d12c51993-10-26 17:58:25 +00001703}
1704
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001705static char xrange_doc[] =
1706"xrange([start,] stop[, step]) -> xrange object\n\
1707\n\
1708Like range(), but instead of returning a list, returns an object that\n\
1709generates the numbers in the range on demand. This is slightly slower\n\
1710than range() but more memory efficient.";
1711
1712
Guido van Rossum79f25d91997-04-29 20:08:16 +00001713static PyObject *
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001714builtin_raw_input(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001715 PyObject *self;
1716 PyObject *args;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001717{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001718 PyObject *v = NULL;
1719 PyObject *f;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001720
Guido van Rossum79f25d91997-04-29 20:08:16 +00001721 if (!PyArg_ParseTuple(args, "|O:[raw_]input", &v))
Guido van Rossum3165fe61992-09-25 21:59:05 +00001722 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001723 if (PyFile_AsFile(PySys_GetObject("stdin")) == stdin &&
1724 PyFile_AsFile(PySys_GetObject("stdout")) == stdout &&
Guido van Rossum53bb7ff1995-07-26 16:26:31 +00001725 isatty(fileno(stdin)) && isatty(fileno(stdout))) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001726 PyObject *po;
Guido van Rossum872537c1995-07-07 22:43:42 +00001727 char *prompt;
1728 char *s;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001729 PyObject *result;
Guido van Rossum872537c1995-07-07 22:43:42 +00001730 if (v != NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001731 po = PyObject_Str(v);
Guido van Rossum872537c1995-07-07 22:43:42 +00001732 if (po == NULL)
1733 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001734 prompt = PyString_AsString(po);
Guido van Rossumd9b52081998-06-26 18:25:38 +00001735 if (prompt == NULL)
1736 return NULL;
Guido van Rossum872537c1995-07-07 22:43:42 +00001737 }
1738 else {
1739 po = NULL;
1740 prompt = "";
1741 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00001742 s = PyOS_Readline(prompt);
1743 Py_XDECREF(po);
Guido van Rossum872537c1995-07-07 22:43:42 +00001744 if (s == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001745 PyErr_SetNone(PyExc_KeyboardInterrupt);
Guido van Rossum872537c1995-07-07 22:43:42 +00001746 return NULL;
1747 }
1748 if (*s == '\0') {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001749 PyErr_SetNone(PyExc_EOFError);
Guido van Rossum872537c1995-07-07 22:43:42 +00001750 result = NULL;
1751 }
1752 else { /* strip trailing '\n' */
Guido van Rossum79f25d91997-04-29 20:08:16 +00001753 result = PyString_FromStringAndSize(s, strlen(s)-1);
Guido van Rossum872537c1995-07-07 22:43:42 +00001754 }
1755 free(s);
1756 return result;
1757 }
Guido van Rossum90933611991-06-07 16:10:43 +00001758 if (v != NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001759 f = PySys_GetObject("stdout");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001760 if (f == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001761 PyErr_SetString(PyExc_RuntimeError, "lost sys.stdout");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001762 return NULL;
1763 }
Guido van Rossumc8b6df91997-05-23 00:06:51 +00001764 if (Py_FlushLine() != 0 ||
1765 PyFile_WriteObject(v, f, Py_PRINT_RAW) != 0)
Guido van Rossum90933611991-06-07 16:10:43 +00001766 return NULL;
1767 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00001768 f = PySys_GetObject("stdin");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001769 if (f == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001770 PyErr_SetString(PyExc_RuntimeError, "lost sys.stdin");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001771 return NULL;
1772 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00001773 return PyFile_GetLine(f, -1);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001774}
1775
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001776static char raw_input_doc[] =
1777"raw_input([prompt]) -> string\n\
1778\n\
1779Read a string from standard input. The trailing newline is stripped.\n\
1780If the user hits EOF (Unix: Ctl-D, Windows: Ctl-Z+Return), raise EOFError.\n\
1781On Unix, GNU readline is used if enabled. The prompt string, if given,\n\
1782is printed without a trailing newline before reading.";
1783
1784
Guido van Rossum79f25d91997-04-29 20:08:16 +00001785static PyObject *
Guido van Rossum12d12c51993-10-26 17:58:25 +00001786builtin_reduce(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001787 PyObject *self;
1788 PyObject *args;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001789{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001790 PyObject *seq, *func, *result = NULL;
1791 PySequenceMethods *sqf;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001792 register int i;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001793
Guido van Rossum79f25d91997-04-29 20:08:16 +00001794 if (!PyArg_ParseTuple(args, "OO|O:reduce", &func, &seq, &result))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001795 return NULL;
1796 if (result != NULL)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001797 Py_INCREF(result);
Guido van Rossum12d12c51993-10-26 17:58:25 +00001798
Guido van Rossum09df08a1998-05-22 00:51:39 +00001799 sqf = seq->ob_type->tp_as_sequence;
1800 if (sqf == NULL || sqf->sq_item == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001801 PyErr_SetString(PyExc_TypeError,
Guido van Rossum12d12c51993-10-26 17:58:25 +00001802 "2nd argument to reduce() must be a sequence object");
1803 return NULL;
1804 }
1805
Guido van Rossum79f25d91997-04-29 20:08:16 +00001806 if ((args = PyTuple_New(2)) == NULL)
Guido van Rossum2d951851994-08-29 12:52:16 +00001807 goto Fail;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001808
Guido van Rossum2d951851994-08-29 12:52:16 +00001809 for (i = 0; ; ++i) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001810 PyObject *op2;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001811
1812 if (args->ob_refcnt > 1) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001813 Py_DECREF(args);
1814 if ((args = PyTuple_New(2)) == NULL)
Guido van Rossum2d951851994-08-29 12:52:16 +00001815 goto Fail;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001816 }
1817
Guido van Rossum2d951851994-08-29 12:52:16 +00001818 if ((op2 = (*sqf->sq_item)(seq, i)) == NULL) {
Barry Warsawcde8b1b1997-08-22 21:14:38 +00001819 if (PyErr_ExceptionMatches(PyExc_IndexError)) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001820 PyErr_Clear();
Guido van Rossum2d951851994-08-29 12:52:16 +00001821 break;
1822 }
1823 goto Fail;
1824 }
Guido van Rossum12d12c51993-10-26 17:58:25 +00001825
Guido van Rossum2d951851994-08-29 12:52:16 +00001826 if (result == NULL)
1827 result = op2;
1828 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001829 PyTuple_SetItem(args, 0, result);
1830 PyTuple_SetItem(args, 1, op2);
1831 if ((result = PyEval_CallObject(func, args)) == NULL)
Guido van Rossum2d951851994-08-29 12:52:16 +00001832 goto Fail;
1833 }
Guido van Rossum12d12c51993-10-26 17:58:25 +00001834 }
1835
Guido van Rossum79f25d91997-04-29 20:08:16 +00001836 Py_DECREF(args);
Guido van Rossum12d12c51993-10-26 17:58:25 +00001837
Guido van Rossum2d951851994-08-29 12:52:16 +00001838 if (result == NULL)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001839 PyErr_SetString(PyExc_TypeError,
Guido van Rossum2d951851994-08-29 12:52:16 +00001840 "reduce of empty sequence with no initial value");
1841
Guido van Rossum12d12c51993-10-26 17:58:25 +00001842 return result;
1843
Guido van Rossum2d951851994-08-29 12:52:16 +00001844Fail:
Guido van Rossum79f25d91997-04-29 20:08:16 +00001845 Py_XDECREF(args);
1846 Py_XDECREF(result);
Guido van Rossum12d12c51993-10-26 17:58:25 +00001847 return NULL;
1848}
1849
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001850static char reduce_doc[] =
1851"reduce(function, sequence[, initial]) -> value\n\
1852\n\
1853Apply a function of two arguments cumulatively to the items of a sequence,\n\
1854from left to right, so as to reduce the sequence to a single value.\n\
1855For example, reduce(lambda x, y: x+y, [1, 2, 3, 4, 5]) calculates\n\
1856((((1+2)+3)+4)+5). If initial is present, it is placed before the items\n\
1857of the sequence in the calculation, and serves as a default when the\n\
1858sequence is empty.";
1859
1860
Guido van Rossum79f25d91997-04-29 20:08:16 +00001861static PyObject *
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001862builtin_reload(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001863 PyObject *self;
1864 PyObject *args;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001865{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001866 PyObject *v;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001867
Guido van Rossum79f25d91997-04-29 20:08:16 +00001868 if (!PyArg_ParseTuple(args, "O:reload", &v))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001869 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001870 return PyImport_ReloadModule(v);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001871}
1872
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001873static char reload_doc[] =
1874"reload(module) -> module\n\
1875\n\
1876Reload the module. The module must have been successfully imported before.";
1877
1878
Guido van Rossum79f25d91997-04-29 20:08:16 +00001879static PyObject *
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001880builtin_repr(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001881 PyObject *self;
1882 PyObject *args;
Guido van Rossumc89705d1992-11-26 08:54:07 +00001883{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001884 PyObject *v;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001885
Guido van Rossum79f25d91997-04-29 20:08:16 +00001886 if (!PyArg_ParseTuple(args, "O:repr", &v))
Guido van Rossumc89705d1992-11-26 08:54:07 +00001887 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001888 return PyObject_Repr(v);
Guido van Rossumc89705d1992-11-26 08:54:07 +00001889}
1890
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001891static char repr_doc[] =
1892"repr(object) -> string\n\
1893\n\
1894Return the canonical string representation of the object.\n\
1895For most object types, eval(repr(object)) == object.";
1896
1897
Guido van Rossum79f25d91997-04-29 20:08:16 +00001898static PyObject *
Guido van Rossum9e51f9b1993-02-12 16:29:05 +00001899builtin_round(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001900 PyObject *self;
1901 PyObject *args;
Guido van Rossum9e51f9b1993-02-12 16:29:05 +00001902{
Guido van Rossum9e51f9b1993-02-12 16:29:05 +00001903 double x;
1904 double f;
1905 int ndigits = 0;
Guido van Rossum9e51f9b1993-02-12 16:29:05 +00001906 int i;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001907
Guido van Rossum79f25d91997-04-29 20:08:16 +00001908 if (!PyArg_ParseTuple(args, "d|i:round", &x, &ndigits))
Guido van Rossum9e51f9b1993-02-12 16:29:05 +00001909 return NULL;
Guido van Rossum9e51f9b1993-02-12 16:29:05 +00001910 f = 1.0;
Guido van Rossum1e162d31998-05-09 14:42:25 +00001911 i = abs(ndigits);
1912 while (--i >= 0)
Guido van Rossum9e51f9b1993-02-12 16:29:05 +00001913 f = f*10.0;
Guido van Rossum1e162d31998-05-09 14:42:25 +00001914 if (ndigits < 0)
1915 x /= f;
Guido van Rossum9e51f9b1993-02-12 16:29:05 +00001916 else
Guido van Rossum1e162d31998-05-09 14:42:25 +00001917 x *= f;
1918 if (x >= 0.0)
1919 x = floor(x + 0.5);
1920 else
1921 x = ceil(x - 0.5);
1922 if (ndigits < 0)
1923 x *= f;
1924 else
1925 x /= f;
1926 return PyFloat_FromDouble(x);
Guido van Rossum9e51f9b1993-02-12 16:29:05 +00001927}
1928
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001929static char round_doc[] =
1930"round(number[, ndigits]) -> floating point number\n\
1931\n\
1932Round a number to a given precision in decimal digits (default 0 digits).\n\
1933This always returns a floating point number. Precision may be negative.";
1934
1935
Guido van Rossum79f25d91997-04-29 20:08:16 +00001936static PyObject *
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001937builtin_str(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001938 PyObject *self;
1939 PyObject *args;
Guido van Rossumc89705d1992-11-26 08:54:07 +00001940{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001941 PyObject *v;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001942
Guido van Rossum79f25d91997-04-29 20:08:16 +00001943 if (!PyArg_ParseTuple(args, "O:str", &v))
Guido van Rossumc89705d1992-11-26 08:54:07 +00001944 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001945 return PyObject_Str(v);
Guido van Rossumc89705d1992-11-26 08:54:07 +00001946}
1947
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001948static char str_doc[] =
1949"str(object) -> string\n\
1950\n\
1951Return a nice string representation of the object.\n\
1952If the argument is a string, the return value is the same object.";
1953
1954
Guido van Rossum79f25d91997-04-29 20:08:16 +00001955static PyObject *
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001956builtin_tuple(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001957 PyObject *self;
1958 PyObject *args;
Guido van Rossumcae027b1994-08-29 12:53:11 +00001959{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001960 PyObject *v;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001961
Guido van Rossum79f25d91997-04-29 20:08:16 +00001962 if (!PyArg_ParseTuple(args, "O:tuple", &v))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001963 return NULL;
Guido van Rossum09df08a1998-05-22 00:51:39 +00001964 return PySequence_Tuple(v);
Guido van Rossumcae027b1994-08-29 12:53:11 +00001965}
1966
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001967static char tuple_doc[] =
1968"tuple(sequence) -> list\n\
1969\n\
1970Return a tuple whose items are the same as those of the argument sequence.\n\
1971If the argument is a tuple, the return value is the same object.";
1972
1973
Guido van Rossum79f25d91997-04-29 20:08:16 +00001974static PyObject *
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001975builtin_type(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001976 PyObject *self;
1977 PyObject *args;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001978{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001979 PyObject *v;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001980
Guido van Rossum79f25d91997-04-29 20:08:16 +00001981 if (!PyArg_ParseTuple(args, "O:type", &v))
Guido van Rossum3f5da241990-12-20 15:06:42 +00001982 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001983 v = (PyObject *)v->ob_type;
1984 Py_INCREF(v);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001985 return v;
1986}
1987
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001988static char type_doc[] =
1989"type(object) -> type object\n\
1990\n\
1991Return the type of the object.";
1992
1993
Guido van Rossum79f25d91997-04-29 20:08:16 +00001994static PyObject *
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001995builtin_vars(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001996 PyObject *self;
1997 PyObject *args;
Guido van Rossum2d951851994-08-29 12:52:16 +00001998{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001999 PyObject *v = NULL;
2000 PyObject *d;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002001
Guido van Rossum79f25d91997-04-29 20:08:16 +00002002 if (!PyArg_ParseTuple(args, "|O:vars", &v))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002003 return NULL;
Guido van Rossum2d951851994-08-29 12:52:16 +00002004 if (v == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002005 d = PyEval_GetLocals();
Guido van Rossum53bb7ff1995-07-26 16:26:31 +00002006 if (d == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002007 if (!PyErr_Occurred())
2008 PyErr_SetString(PyExc_SystemError,
2009 "no locals!?");
Guido van Rossum53bb7ff1995-07-26 16:26:31 +00002010 }
2011 else
Guido van Rossum79f25d91997-04-29 20:08:16 +00002012 Py_INCREF(d);
Guido van Rossum2d951851994-08-29 12:52:16 +00002013 }
2014 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002015 d = PyObject_GetAttrString(v, "__dict__");
Guido van Rossum2d951851994-08-29 12:52:16 +00002016 if (d == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002017 PyErr_SetString(PyExc_TypeError,
Guido van Rossum2d951851994-08-29 12:52:16 +00002018 "vars() argument must have __dict__ attribute");
2019 return NULL;
2020 }
2021 }
2022 return d;
2023}
2024
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002025static char vars_doc[] =
2026"vars([object]) -> dictionary\n\
2027\n\
2028Without arguments, equivalent to locals().\n\
2029With an argument, equivalent to object.__dict__.";
2030
Guido van Rossum668213d1999-06-16 17:28:37 +00002031static int
2032abstract_issubclass(derived, cls, err, first)
2033 PyObject *derived;
2034 PyObject *cls;
2035 char *err;
2036 int first;
2037{
2038 static PyObject *__bases__ = NULL;
2039 PyObject *bases;
Guido van Rossum7f851861999-06-17 19:12:39 +00002040 int i, n;
Guido van Rossum668213d1999-06-16 17:28:37 +00002041 int r = 0;
2042
2043 if (__bases__ == NULL) {
2044 __bases__ = PyString_FromString("__bases__");
2045 if (__bases__ == NULL)
2046 return -1;
2047 }
2048
2049 if (first) {
2050 bases = PyObject_GetAttr(cls, __bases__);
2051 if (bases == NULL || !PyTuple_Check(bases)) {
2052 Py_XDECREF(bases);
2053 PyErr_SetString(PyExc_TypeError, err);
2054 return -1;
2055 }
2056 Py_DECREF(bases);
2057 }
2058
2059 if (derived == cls)
2060 return 1;
2061
2062 bases = PyObject_GetAttr(derived, __bases__);
2063 if (bases == NULL || !PyTuple_Check(bases)) {
2064 Py_XDECREF(bases);
2065 PyErr_SetString(PyExc_TypeError, err);
2066 return -1;
2067 }
2068
2069 n = PyTuple_GET_SIZE(bases);
2070 for (i = 0; i < n; i++) {
2071 r = abstract_issubclass(PyTuple_GET_ITEM(bases, i),
2072 cls, err, 0);
2073 if (r != 0)
2074 break;
2075 }
2076
2077 Py_DECREF(bases);
2078
2079 return r;
2080}
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002081
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002082static PyObject *
2083builtin_isinstance(self, args)
2084 PyObject *self;
2085 PyObject *args;
2086{
2087 PyObject *inst;
2088 PyObject *cls;
Guido van Rossum668213d1999-06-16 17:28:37 +00002089 PyObject *icls;
2090 static PyObject *__class__ = NULL;
2091 int retval = 0;
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002092
2093 if (!PyArg_ParseTuple(args, "OO", &inst, &cls))
2094 return NULL;
Guido van Rossumf5dd9141997-12-02 19:11:45 +00002095
Guido van Rossum668213d1999-06-16 17:28:37 +00002096 if (PyClass_Check(cls)) {
2097 if (PyInstance_Check(inst)) {
Guido van Rossumf5dd9141997-12-02 19:11:45 +00002098 PyObject *inclass =
2099 (PyObject*)((PyInstanceObject*)inst)->in_class;
2100 retval = PyClass_IsSubclass(inclass, cls);
2101 }
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002102 }
Guido van Rossum668213d1999-06-16 17:28:37 +00002103 else if (PyType_Check(cls)) {
2104 retval = ((PyObject *)(inst->ob_type) == cls);
2105 }
2106 else if (!PyInstance_Check(inst)) {
2107 if (__class__ == NULL) {
2108 __class__ = PyString_FromString("__class__");
2109 if (__class__ == NULL)
2110 return NULL;
2111 }
2112 icls = PyObject_GetAttr(inst, __class__);
2113 if (icls != NULL) {
2114 retval = abstract_issubclass(
2115 icls, cls,
2116 "second argument must be a class",
2117 1);
2118 Py_DECREF(icls);
2119 if (retval < 0)
2120 return NULL;
2121 }
2122 else {
2123 PyErr_SetString(PyExc_TypeError,
2124 "second argument must be a class");
2125 return NULL;
2126 }
2127 }
2128 else {
2129 PyErr_SetString(PyExc_TypeError,
2130 "second argument must be a class");
2131 return NULL;
2132 }
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002133 return PyInt_FromLong(retval);
2134}
2135
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002136static char isinstance_doc[] =
2137"isinstance(object, class-or-type) -> Boolean\n\
2138\n\
2139Return whether an object is an instance of a class or of a subclass thereof.\n\
2140With a type as second argument, return whether that is the object's type.";
2141
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002142
2143static PyObject *
2144builtin_issubclass(self, args)
2145 PyObject *self;
2146 PyObject *args;
2147{
2148 PyObject *derived;
2149 PyObject *cls;
2150 int retval;
2151
2152 if (!PyArg_ParseTuple(args, "OO", &derived, &cls))
2153 return NULL;
Guido van Rossum668213d1999-06-16 17:28:37 +00002154
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002155 if (!PyClass_Check(derived) || !PyClass_Check(cls)) {
Guido van Rossum668213d1999-06-16 17:28:37 +00002156 retval = abstract_issubclass(
2157 derived, cls, "arguments must be classes", 1);
2158 if (retval < 0)
2159 return NULL;
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002160 }
Guido van Rossum668213d1999-06-16 17:28:37 +00002161 else {
2162 /* shortcut */
2163 if (!(retval = (derived == cls)))
2164 retval = PyClass_IsSubclass(derived, cls);
2165 }
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002166
2167 return PyInt_FromLong(retval);
2168}
2169
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002170static char issubclass_doc[] =
2171"issubclass(C, B) -> Boolean\n\
2172\n\
2173Return whether class C is a subclass (i.e., a derived class) of class B.";
2174
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002175
Guido van Rossum79f25d91997-04-29 20:08:16 +00002176static PyMethodDef builtin_methods[] = {
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002177 {"__import__", builtin___import__, 1, import_doc},
2178 {"abs", builtin_abs, 1, abs_doc},
2179 {"apply", builtin_apply, 1, apply_doc},
Guido van Rossum0daf0221999-03-19 19:07:19 +00002180 {"buffer", builtin_buffer, 1, buffer_doc},
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002181 {"callable", builtin_callable, 1, callable_doc},
2182 {"chr", builtin_chr, 1, chr_doc},
2183 {"cmp", builtin_cmp, 1, cmp_doc},
2184 {"coerce", builtin_coerce, 1, coerce_doc},
2185 {"compile", builtin_compile, 1, compile_doc},
Guido van Rossum8a5c5d21996-01-12 01:09:56 +00002186#ifndef WITHOUT_COMPLEX
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002187 {"complex", builtin_complex, 1, complex_doc},
Guido van Rossum8a5c5d21996-01-12 01:09:56 +00002188#endif
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002189 {"delattr", builtin_delattr, 1, delattr_doc},
2190 {"dir", builtin_dir, 1, dir_doc},
2191 {"divmod", builtin_divmod, 1, divmod_doc},
2192 {"eval", builtin_eval, 1, eval_doc},
2193 {"execfile", builtin_execfile, 1, execfile_doc},
2194 {"filter", builtin_filter, 1, filter_doc},
2195 {"float", builtin_float, 1, float_doc},
2196 {"getattr", builtin_getattr, 1, getattr_doc},
2197 {"globals", builtin_globals, 1, globals_doc},
2198 {"hasattr", builtin_hasattr, 1, hasattr_doc},
2199 {"hash", builtin_hash, 1, hash_doc},
2200 {"hex", builtin_hex, 1, hex_doc},
2201 {"id", builtin_id, 1, id_doc},
2202 {"input", builtin_input, 1, input_doc},
2203 {"intern", builtin_intern, 1, intern_doc},
2204 {"int", builtin_int, 1, int_doc},
2205 {"isinstance", builtin_isinstance, 1, isinstance_doc},
2206 {"issubclass", builtin_issubclass, 1, issubclass_doc},
2207 {"len", builtin_len, 1, len_doc},
2208 {"list", builtin_list, 1, list_doc},
2209 {"locals", builtin_locals, 1, locals_doc},
2210 {"long", builtin_long, 1, long_doc},
2211 {"map", builtin_map, 1, map_doc},
2212 {"max", builtin_max, 1, max_doc},
2213 {"min", builtin_min, 1, min_doc},
2214 {"oct", builtin_oct, 1, oct_doc},
2215 {"open", builtin_open, 1, open_doc},
2216 {"ord", builtin_ord, 1, ord_doc},
2217 {"pow", builtin_pow, 1, pow_doc},
2218 {"range", builtin_range, 1, range_doc},
2219 {"raw_input", builtin_raw_input, 1, raw_input_doc},
2220 {"reduce", builtin_reduce, 1, reduce_doc},
2221 {"reload", builtin_reload, 1, reload_doc},
2222 {"repr", builtin_repr, 1, repr_doc},
2223 {"round", builtin_round, 1, round_doc},
2224 {"setattr", builtin_setattr, 1, setattr_doc},
2225 {"slice", builtin_slice, 1, slice_doc},
2226 {"str", builtin_str, 1, str_doc},
2227 {"tuple", builtin_tuple, 1, tuple_doc},
2228 {"type", builtin_type, 1, type_doc},
2229 {"vars", builtin_vars, 1, vars_doc},
2230 {"xrange", builtin_xrange, 1, xrange_doc},
Guido van Rossumc02e15c1991-12-16 13:03:00 +00002231 {NULL, NULL},
Guido van Rossum3f5da241990-12-20 15:06:42 +00002232};
2233
Guido van Rossum3f5da241990-12-20 15:06:42 +00002234/* Predefined exceptions */
2235
Guido van Rossum04748321997-09-16 18:43:15 +00002236PyObject *PyExc_Exception;
Barry Warsaw757af0e1997-08-29 22:13:51 +00002237PyObject *PyExc_StandardError;
Barry Warsaw412cdc21997-09-16 21:51:14 +00002238PyObject *PyExc_ArithmeticError;
Barry Warsaw757af0e1997-08-29 22:13:51 +00002239PyObject *PyExc_LookupError;
2240
Guido van Rossum79f25d91997-04-29 20:08:16 +00002241PyObject *PyExc_AssertionError;
2242PyObject *PyExc_AttributeError;
2243PyObject *PyExc_EOFError;
Guido van Rossumb6a7f771997-05-09 03:03:23 +00002244PyObject *PyExc_FloatingPointError;
Barry Warsawd086a1a1998-07-23 15:59:57 +00002245PyObject *PyExc_EnvironmentError;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002246PyObject *PyExc_IOError;
Barry Warsawd086a1a1998-07-23 15:59:57 +00002247PyObject *PyExc_OSError;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002248PyObject *PyExc_ImportError;
2249PyObject *PyExc_IndexError;
2250PyObject *PyExc_KeyError;
2251PyObject *PyExc_KeyboardInterrupt;
2252PyObject *PyExc_MemoryError;
2253PyObject *PyExc_NameError;
2254PyObject *PyExc_OverflowError;
2255PyObject *PyExc_RuntimeError;
Barry Warsaw344864f1998-12-01 18:52:06 +00002256PyObject *PyExc_NotImplementedError;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002257PyObject *PyExc_SyntaxError;
2258PyObject *PyExc_SystemError;
2259PyObject *PyExc_SystemExit;
Guido van Rossum87460821999-06-22 14:47:32 +00002260PyObject *PyExc_UnboundLocalError;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002261PyObject *PyExc_TypeError;
2262PyObject *PyExc_ValueError;
2263PyObject *PyExc_ZeroDivisionError;
Guido van Rossum50afb7a1991-12-10 13:52:31 +00002264
Barry Warsaw757af0e1997-08-29 22:13:51 +00002265PyObject *PyExc_MemoryErrorInst;
2266
Guido van Rossum11950231999-03-25 21:16:07 +00002267static struct
Barry Warsaw757af0e1997-08-29 22:13:51 +00002268{
2269 char* name;
2270 PyObject** exc;
2271 int leaf_exc;
2272}
2273bltin_exc[] = {
Guido van Rossum04748321997-09-16 18:43:15 +00002274 {"Exception", &PyExc_Exception, 0},
Barry Warsaw757af0e1997-08-29 22:13:51 +00002275 {"StandardError", &PyExc_StandardError, 0},
Barry Warsaw412cdc21997-09-16 21:51:14 +00002276 {"ArithmeticError", &PyExc_ArithmeticError, 0},
Barry Warsaw757af0e1997-08-29 22:13:51 +00002277 {"LookupError", &PyExc_LookupError, 0},
2278 {"AssertionError", &PyExc_AssertionError, 1},
2279 {"AttributeError", &PyExc_AttributeError, 1},
2280 {"EOFError", &PyExc_EOFError, 1},
2281 {"FloatingPointError", &PyExc_FloatingPointError, 1},
Barry Warsaw78902031999-01-29 20:29:49 +00002282 {"EnvironmentError", &PyExc_EnvironmentError, 0},
Barry Warsaw757af0e1997-08-29 22:13:51 +00002283 {"IOError", &PyExc_IOError, 1},
Barry Warsawd086a1a1998-07-23 15:59:57 +00002284 {"OSError", &PyExc_OSError, 1},
Barry Warsaw757af0e1997-08-29 22:13:51 +00002285 {"ImportError", &PyExc_ImportError, 1},
2286 {"IndexError", &PyExc_IndexError, 1},
2287 {"KeyError", &PyExc_KeyError, 1},
2288 {"KeyboardInterrupt", &PyExc_KeyboardInterrupt, 1},
2289 {"MemoryError", &PyExc_MemoryError, 1},
Guido van Rossum87460821999-06-22 14:47:32 +00002290 /* Note: NameError is not a leaf in exceptions.py, but unlike
2291 the other non-leafs NameError is meant to be raised directly
2292 at times -- the leaf_exc member really seems to mean something
2293 like "this is an abstract base class" when false.
2294 */
Barry Warsaw757af0e1997-08-29 22:13:51 +00002295 {"NameError", &PyExc_NameError, 1},
2296 {"OverflowError", &PyExc_OverflowError, 1},
2297 {"RuntimeError", &PyExc_RuntimeError, 1},
Barry Warsaw344864f1998-12-01 18:52:06 +00002298 {"NotImplementedError",&PyExc_NotImplementedError,1},
Barry Warsaw757af0e1997-08-29 22:13:51 +00002299 {"SyntaxError", &PyExc_SyntaxError, 1},
2300 {"SystemError", &PyExc_SystemError, 1},
2301 {"SystemExit", &PyExc_SystemExit, 1},
Guido van Rossum87460821999-06-22 14:47:32 +00002302 {"UnboundLocalError", &PyExc_UnboundLocalError, 1},
Barry Warsaw757af0e1997-08-29 22:13:51 +00002303 {"TypeError", &PyExc_TypeError, 1},
2304 {"ValueError", &PyExc_ValueError, 1},
2305 {"ZeroDivisionError", &PyExc_ZeroDivisionError, 1},
2306 {NULL, NULL}
2307};
2308
2309
Barry Warsaw98b62461998-09-14 18:51:11 +00002310/* import exceptions module to extract class exceptions. on success,
2311 * return 1. on failure return 0 which signals _PyBuiltin_Init_2 to fall
2312 * back to using old-style string based exceptions.
2313 */
2314static int
Barry Warsaw757af0e1997-08-29 22:13:51 +00002315init_class_exc(dict)
2316 PyObject *dict;
2317{
2318 int i;
2319 PyObject *m = PyImport_ImportModule("exceptions");
Barry Warsaw98b62461998-09-14 18:51:11 +00002320 PyObject *args = NULL;
2321 PyObject *d = NULL;
Barry Warsaw757af0e1997-08-29 22:13:51 +00002322
Barry Warsaw98b62461998-09-14 18:51:11 +00002323 /* make sure we got the module and its dictionary */
Barry Warsaw757af0e1997-08-29 22:13:51 +00002324 if (m == NULL ||
2325 (d = PyModule_GetDict(m)) == NULL)
2326 {
Barry Warsaw98b62461998-09-14 18:51:11 +00002327 PySys_WriteStderr("'import exceptions' failed; ");
Barry Warsaw757af0e1997-08-29 22:13:51 +00002328 if (Py_VerboseFlag) {
Barry Warsaw98b62461998-09-14 18:51:11 +00002329 PySys_WriteStderr("traceback:\n");
Barry Warsaw757af0e1997-08-29 22:13:51 +00002330 PyErr_Print();
2331 }
2332 else {
Barry Warsaw98b62461998-09-14 18:51:11 +00002333 PySys_WriteStderr("use -v for traceback\n");
Barry Warsaw757af0e1997-08-29 22:13:51 +00002334 }
Barry Warsaw98b62461998-09-14 18:51:11 +00002335 goto finally;
Barry Warsaw757af0e1997-08-29 22:13:51 +00002336 }
2337 for (i = 0; bltin_exc[i].name; i++) {
2338 /* dig the exception out of the module */
2339 PyObject *exc = PyDict_GetItemString(d, bltin_exc[i].name);
Barry Warsaw98b62461998-09-14 18:51:11 +00002340 if (!exc) {
2341 PySys_WriteStderr(
2342 "Built-in exception class not found: %s. Library mismatch?\n",
2343 bltin_exc[i].name);
2344 goto finally;
2345 }
2346 /* free the old-style exception string object */
Barry Warsaw757af0e1997-08-29 22:13:51 +00002347 Py_XDECREF(*bltin_exc[i].exc);
2348
2349 /* squirrel away a pointer to the exception */
2350 Py_INCREF(exc);
2351 *bltin_exc[i].exc = exc;
2352
2353 /* and insert the name in the __builtin__ module */
Barry Warsaw98b62461998-09-14 18:51:11 +00002354 if (PyDict_SetItemString(dict, bltin_exc[i].name, exc)) {
2355 PySys_WriteStderr(
2356 "Cannot insert exception into __builtin__: %s\n",
2357 bltin_exc[i].name);
2358 goto finally;
2359 }
Barry Warsaw757af0e1997-08-29 22:13:51 +00002360 }
2361
2362 /* we need one pre-allocated instance */
2363 args = Py_BuildValue("()");
Barry Warsaw98b62461998-09-14 18:51:11 +00002364 if (!args ||
2365 !(PyExc_MemoryErrorInst =
2366 PyEval_CallObject(PyExc_MemoryError, args)))
2367 {
2368 PySys_WriteStderr("Cannot pre-allocate MemoryError instance\n");
2369 goto finally;
Barry Warsaw757af0e1997-08-29 22:13:51 +00002370 }
Barry Warsaw98b62461998-09-14 18:51:11 +00002371 Py_DECREF(args);
Barry Warsaw757af0e1997-08-29 22:13:51 +00002372
2373 /* we're done with the exceptions module */
2374 Py_DECREF(m);
2375
Barry Warsaw98b62461998-09-14 18:51:11 +00002376 if (PyErr_Occurred()) {
2377 PySys_WriteStderr("Cannot initialize standard class exceptions; ");
2378 if (Py_VerboseFlag) {
2379 PySys_WriteStderr("traceback:\n");
2380 PyErr_Print();
2381 }
2382 else
2383 PySys_WriteStderr("use -v for traceback\n");
2384 goto finally;
2385 }
2386 return 1;
2387 finally:
2388 Py_XDECREF(m);
2389 Py_XDECREF(args);
2390 PyErr_Clear();
2391 return 0;
Barry Warsaw757af0e1997-08-29 22:13:51 +00002392}
2393
2394
2395static void
2396fini_instances()
2397{
2398 Py_XDECREF(PyExc_MemoryErrorInst);
2399 PyExc_MemoryErrorInst = NULL;
2400}
2401
2402
Guido van Rossum79f25d91997-04-29 20:08:16 +00002403static PyObject *
Guido van Rossum25ce5661997-08-02 03:10:38 +00002404newstdexception(dict, name)
2405 PyObject *dict;
Guido van Rossumfb905c31991-12-16 15:42:38 +00002406 char *name;
Guido van Rossum3f5da241990-12-20 15:06:42 +00002407{
Guido van Rossum79f25d91997-04-29 20:08:16 +00002408 PyObject *v = PyString_FromString(name);
Guido van Rossum25ce5661997-08-02 03:10:38 +00002409 if (v == NULL || PyDict_SetItemString(dict, name, v) != 0)
Barry Warsaw98b62461998-09-14 18:51:11 +00002410 Py_FatalError("Cannot create string-based exceptions");
Guido van Rossum3f5da241990-12-20 15:06:42 +00002411 return v;
2412}
2413
2414static void
Guido van Rossum25ce5661997-08-02 03:10:38 +00002415initerrors(dict)
2416 PyObject *dict;
Guido van Rossum3f5da241990-12-20 15:06:42 +00002417{
Barry Warsaw72b715d1999-02-24 00:35:43 +00002418 int i, j;
Barry Warsaw757af0e1997-08-29 22:13:51 +00002419 int exccnt = 0;
2420 for (i = 0; bltin_exc[i].name; i++, exccnt++) {
Barry Warsaw98b62461998-09-14 18:51:11 +00002421 Py_XDECREF(*bltin_exc[i].exc);
Barry Warsaw757af0e1997-08-29 22:13:51 +00002422 if (bltin_exc[i].leaf_exc)
2423 *bltin_exc[i].exc =
2424 newstdexception(dict, bltin_exc[i].name);
2425 }
2426
Barry Warsawd086a1a1998-07-23 15:59:57 +00002427 /* This is kind of bogus because we special case the some of the
2428 * new exceptions to be nearly forward compatible. But this means
2429 * we hard code knowledge about exceptions.py into C here. I don't
2430 * have a better solution, though.
2431 */
Barry Warsaw757af0e1997-08-29 22:13:51 +00002432 PyExc_LookupError = PyTuple_New(2);
2433 Py_INCREF(PyExc_IndexError);
2434 PyTuple_SET_ITEM(PyExc_LookupError, 0, PyExc_IndexError);
2435 Py_INCREF(PyExc_KeyError);
2436 PyTuple_SET_ITEM(PyExc_LookupError, 1, PyExc_KeyError);
2437 PyDict_SetItemString(dict, "LookupError", PyExc_LookupError);
2438
Barry Warsaw412cdc21997-09-16 21:51:14 +00002439 PyExc_ArithmeticError = PyTuple_New(3);
Barry Warsaw757af0e1997-08-29 22:13:51 +00002440 Py_INCREF(PyExc_OverflowError);
Barry Warsaw412cdc21997-09-16 21:51:14 +00002441 PyTuple_SET_ITEM(PyExc_ArithmeticError, 0, PyExc_OverflowError);
Barry Warsaw757af0e1997-08-29 22:13:51 +00002442 Py_INCREF(PyExc_ZeroDivisionError);
Barry Warsaw412cdc21997-09-16 21:51:14 +00002443 PyTuple_SET_ITEM(PyExc_ArithmeticError, 1, PyExc_ZeroDivisionError);
Barry Warsaw757af0e1997-08-29 22:13:51 +00002444 Py_INCREF(PyExc_FloatingPointError);
Barry Warsaw412cdc21997-09-16 21:51:14 +00002445 PyTuple_SET_ITEM(PyExc_ArithmeticError, 2, PyExc_FloatingPointError);
2446 PyDict_SetItemString(dict, "ArithmeticError", PyExc_ArithmeticError);
Barry Warsaw757af0e1997-08-29 22:13:51 +00002447
Barry Warsawd086a1a1998-07-23 15:59:57 +00002448 PyExc_EnvironmentError = PyTuple_New(2);
2449 Py_INCREF(PyExc_IOError);
2450 PyTuple_SET_ITEM(PyExc_EnvironmentError, 0, PyExc_IOError);
2451 Py_INCREF(PyExc_OSError);
2452 PyTuple_SET_ITEM(PyExc_EnvironmentError, 1, PyExc_OSError);
2453 PyDict_SetItemString(dict, "EnvironmentError", PyExc_EnvironmentError);
2454
Guido van Rossum87460821999-06-22 14:47:32 +00002455 /* Make UnboundLocalError an alias for NameError */
2456 Py_INCREF(PyExc_NameError);
2457 Py_DECREF(PyExc_UnboundLocalError);
2458 PyExc_UnboundLocalError = PyExc_NameError;
2459 if (PyDict_SetItemString(dict, "UnboundLocalError",
2460 PyExc_NameError) != 0)
2461 Py_FatalError("Cannot create string-based exceptions");
2462
Barry Warsaw72b715d1999-02-24 00:35:43 +00002463 /* missing from the StandardError tuple: Exception, StandardError,
2464 * and SystemExit
2465 */
2466 PyExc_StandardError = PyTuple_New(exccnt-3);
2467 for (i = 2, j = 0; bltin_exc[i].name; i++) {
Barry Warsaw757af0e1997-08-29 22:13:51 +00002468 PyObject *exc = *bltin_exc[i].exc;
Barry Warsaw72b715d1999-02-24 00:35:43 +00002469 /* SystemExit is not an error, but it is an exception */
2470 if (exc != PyExc_SystemExit) {
2471 Py_INCREF(exc);
2472 PyTuple_SET_ITEM(PyExc_StandardError, j++, exc);
2473 }
Barry Warsaw757af0e1997-08-29 22:13:51 +00002474 }
2475 PyDict_SetItemString(dict, "StandardError", PyExc_StandardError);
Guido van Rossum04748321997-09-16 18:43:15 +00002476
Barry Warsaw72b715d1999-02-24 00:35:43 +00002477 /* Exception is a 2-tuple */
2478 PyExc_Exception = PyTuple_New(2);
2479 Py_INCREF(PyExc_SystemExit);
2480 PyTuple_SET_ITEM(PyExc_Exception, 0, PyExc_SystemExit);
2481 Py_INCREF(PyExc_StandardError);
2482 PyTuple_SET_ITEM(PyExc_Exception, 1, PyExc_StandardError);
Guido van Rossum04748321997-09-16 18:43:15 +00002483 PyDict_SetItemString(dict, "Exception", PyExc_Exception);
Barry Warsaw757af0e1997-08-29 22:13:51 +00002484
2485 if (PyErr_Occurred())
2486 Py_FatalError("Could not initialize built-in string exceptions");
Guido van Rossum25ce5661997-08-02 03:10:38 +00002487}
2488
Barry Warsaw72b715d1999-02-24 00:35:43 +00002489
Guido van Rossum25ce5661997-08-02 03:10:38 +00002490static void
2491finierrors()
2492{
Barry Warsaw757af0e1997-08-29 22:13:51 +00002493 int i;
2494 for (i = 0; bltin_exc[i].name; i++) {
2495 PyObject *exc = *bltin_exc[i].exc;
2496 Py_XDECREF(exc);
2497 *bltin_exc[i].exc = NULL;
2498 }
Guido van Rossum25ce5661997-08-02 03:10:38 +00002499}
2500
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002501static char builtin_doc[] =
2502"Built-in functions, exceptions, and other objects.\n\
2503\n\
2504Noteworthy: None is the `nil' object; Ellipsis represents `...' in slices.";
2505
Guido van Rossum25ce5661997-08-02 03:10:38 +00002506PyObject *
Barry Warsaw757af0e1997-08-29 22:13:51 +00002507_PyBuiltin_Init_1()
Guido van Rossum25ce5661997-08-02 03:10:38 +00002508{
2509 PyObject *mod, *dict;
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002510 mod = Py_InitModule4("__builtin__", builtin_methods,
2511 builtin_doc, (PyObject *)NULL,
2512 PYTHON_API_VERSION);
Guido van Rossum25ce5661997-08-02 03:10:38 +00002513 if (mod == NULL)
2514 return NULL;
2515 dict = PyModule_GetDict(mod);
2516 initerrors(dict);
2517 if (PyDict_SetItemString(dict, "None", Py_None) < 0)
2518 return NULL;
2519 if (PyDict_SetItemString(dict, "Ellipsis", Py_Ellipsis) < 0)
2520 return NULL;
2521 if (PyDict_SetItemString(dict, "__debug__",
2522 PyInt_FromLong(Py_OptimizeFlag == 0)) < 0)
2523 return NULL;
Barry Warsaw757af0e1997-08-29 22:13:51 +00002524
Guido van Rossum25ce5661997-08-02 03:10:38 +00002525 return mod;
Guido van Rossum3f5da241990-12-20 15:06:42 +00002526}
2527
2528void
Barry Warsaw757af0e1997-08-29 22:13:51 +00002529_PyBuiltin_Init_2(dict)
2530 PyObject *dict;
2531{
2532 /* if Python was started with -X, initialize the class exceptions */
Barry Warsaw98b62461998-09-14 18:51:11 +00002533 if (Py_UseClassExceptionsFlag) {
2534 if (!init_class_exc(dict)) {
2535 /* class based exceptions could not be
2536 * initialized. Fall back to using string based
2537 * exceptions.
2538 */
2539 PySys_WriteStderr(
2540 "Warning! Falling back to string-based exceptions\n");
2541 initerrors(dict);
2542 }
2543 }
Barry Warsaw757af0e1997-08-29 22:13:51 +00002544}
2545
2546
2547void
2548_PyBuiltin_Fini_1()
2549{
2550 fini_instances();
2551}
2552
2553
2554void
2555_PyBuiltin_Fini_2()
Guido van Rossum3f5da241990-12-20 15:06:42 +00002556{
Guido van Rossum25ce5661997-08-02 03:10:38 +00002557 finierrors();
Guido van Rossum3f5da241990-12-20 15:06:42 +00002558}
Guido van Rossumc6bb8f71991-07-01 18:42:41 +00002559
Guido van Rossum12d12c51993-10-26 17:58:25 +00002560
Guido van Rossume77a7571993-11-03 15:01:26 +00002561/* Helper for filter(): filter a tuple through a function */
Guido van Rossum12d12c51993-10-26 17:58:25 +00002562
Guido van Rossum79f25d91997-04-29 20:08:16 +00002563static PyObject *
Guido van Rossum12d12c51993-10-26 17:58:25 +00002564filtertuple(func, tuple)
Guido van Rossum79f25d91997-04-29 20:08:16 +00002565 PyObject *func;
2566 PyObject *tuple;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002567{
Guido van Rossum79f25d91997-04-29 20:08:16 +00002568 PyObject *result;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002569 register int i, j;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002570 int len = PyTuple_Size(tuple);
Guido van Rossum12d12c51993-10-26 17:58:25 +00002571
Guido van Rossumb7b45621995-08-04 04:07:45 +00002572 if (len == 0) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002573 Py_INCREF(tuple);
Guido van Rossumb7b45621995-08-04 04:07:45 +00002574 return tuple;
2575 }
2576
Guido van Rossum79f25d91997-04-29 20:08:16 +00002577 if ((result = PyTuple_New(len)) == NULL)
Guido van Rossum2586bf01993-11-01 16:21:44 +00002578 return NULL;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002579
Guido van Rossum12d12c51993-10-26 17:58:25 +00002580 for (i = j = 0; i < len; ++i) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002581 PyObject *item, *good;
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002582 int ok;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002583
Guido van Rossum79f25d91997-04-29 20:08:16 +00002584 if ((item = PyTuple_GetItem(tuple, i)) == NULL)
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002585 goto Fail_1;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002586 if (func == Py_None) {
2587 Py_INCREF(item);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002588 good = item;
2589 }
2590 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002591 PyObject *arg = Py_BuildValue("(O)", item);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002592 if (arg == NULL)
2593 goto Fail_1;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002594 good = PyEval_CallObject(func, arg);
2595 Py_DECREF(arg);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002596 if (good == NULL)
Guido van Rossum12d12c51993-10-26 17:58:25 +00002597 goto Fail_1;
2598 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002599 ok = PyObject_IsTrue(good);
2600 Py_DECREF(good);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002601 if (ok) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002602 Py_INCREF(item);
2603 if (PyTuple_SetItem(result, j++, item) < 0)
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002604 goto Fail_1;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002605 }
Guido van Rossum12d12c51993-10-26 17:58:25 +00002606 }
2607
Guido van Rossum79f25d91997-04-29 20:08:16 +00002608 if (_PyTuple_Resize(&result, j, 0) < 0)
Guido van Rossum12d12c51993-10-26 17:58:25 +00002609 return NULL;
2610
Guido van Rossum12d12c51993-10-26 17:58:25 +00002611 return result;
2612
Guido van Rossum12d12c51993-10-26 17:58:25 +00002613Fail_1:
Guido van Rossum79f25d91997-04-29 20:08:16 +00002614 Py_DECREF(result);
Guido van Rossum12d12c51993-10-26 17:58:25 +00002615 return NULL;
2616}
2617
2618
Guido van Rossume77a7571993-11-03 15:01:26 +00002619/* Helper for filter(): filter a string through a function */
Guido van Rossum12d12c51993-10-26 17:58:25 +00002620
Guido van Rossum79f25d91997-04-29 20:08:16 +00002621static PyObject *
Guido van Rossum12d12c51993-10-26 17:58:25 +00002622filterstring(func, strobj)
Guido van Rossum79f25d91997-04-29 20:08:16 +00002623 PyObject *func;
2624 PyObject *strobj;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002625{
Guido van Rossum79f25d91997-04-29 20:08:16 +00002626 PyObject *result;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002627 register int i, j;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002628 int len = PyString_Size(strobj);
Guido van Rossum12d12c51993-10-26 17:58:25 +00002629
Guido van Rossum79f25d91997-04-29 20:08:16 +00002630 if (func == Py_None) {
Guido van Rossum2586bf01993-11-01 16:21:44 +00002631 /* No character is ever false -- share input string */
Guido van Rossum79f25d91997-04-29 20:08:16 +00002632 Py_INCREF(strobj);
Guido van Rossum2d951851994-08-29 12:52:16 +00002633 return strobj;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002634 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002635 if ((result = PyString_FromStringAndSize(NULL, len)) == NULL)
Guido van Rossum2586bf01993-11-01 16:21:44 +00002636 return NULL;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002637
Guido van Rossum12d12c51993-10-26 17:58:25 +00002638 for (i = j = 0; i < len; ++i) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002639 PyObject *item, *arg, *good;
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002640 int ok;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002641
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002642 item = (*strobj->ob_type->tp_as_sequence->sq_item)(strobj, i);
2643 if (item == NULL)
2644 goto Fail_1;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002645 arg = Py_BuildValue("(O)", item);
2646 Py_DECREF(item);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002647 if (arg == NULL)
2648 goto Fail_1;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002649 good = PyEval_CallObject(func, arg);
2650 Py_DECREF(arg);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002651 if (good == NULL)
2652 goto Fail_1;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002653 ok = PyObject_IsTrue(good);
2654 Py_DECREF(good);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002655 if (ok)
Guido van Rossum79f25d91997-04-29 20:08:16 +00002656 PyString_AS_STRING((PyStringObject *)result)[j++] =
2657 PyString_AS_STRING((PyStringObject *)item)[0];
Guido van Rossum12d12c51993-10-26 17:58:25 +00002658 }
2659
Guido van Rossum79f25d91997-04-29 20:08:16 +00002660 if (j < len && _PyString_Resize(&result, j) < 0)
Guido van Rossum12d12c51993-10-26 17:58:25 +00002661 return NULL;
2662
Guido van Rossum12d12c51993-10-26 17:58:25 +00002663 return result;
2664
Guido van Rossum12d12c51993-10-26 17:58:25 +00002665Fail_1:
Guido van Rossum79f25d91997-04-29 20:08:16 +00002666 Py_DECREF(result);
Guido van Rossum12d12c51993-10-26 17:58:25 +00002667 return NULL;
2668}