blob: 1d23281f9f90ebbfb9320f7040e4bf7d19e21bc5 [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[] =
Fred Drake7b912121999-12-23 14:16:55 +0000133"apply(object, args[, kwargs]) -> value\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000134\n\
Fred Drake7b912121999-12-23 14:16:55 +0000135Call a callable object with positional arguments taken from the tuple args,\n\
136and keyword arguments taken from the optional dictionary kwargs.\n\
137Note that classes are callable, as are instances with a __call__() method.";
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000138
139
Guido van Rossum79f25d91997-04-29 20:08:16 +0000140static PyObject *
Guido van Rossum0daf0221999-03-19 19:07:19 +0000141builtin_buffer(self, args)
142 PyObject *self;
143 PyObject *args;
144{
145 PyObject *ob;
146 int offset = 0;
147 int size = Py_END_OF_BUFFER;
148
149 if ( !PyArg_ParseTuple(args, "O|ii:buffer", &ob, &offset, &size) )
150 return NULL;
151 return PyBuffer_FromObject(ob, offset, size);
152}
153
154static char buffer_doc[] =
155"buffer(object [, offset[, size]) -> object\n\
156\n\
157Creates a new buffer object which references the given object.\n\
158The buffer will reference a slice of the target object from the\n\
159start of the object (or at the specified offset). The slice will\n\
160extend to the end of the target object (or with the specified size).";
161
162
163static PyObject *
Guido van Rossum2d951851994-08-29 12:52:16 +0000164builtin_callable(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000165 PyObject *self;
166 PyObject *args;
Guido van Rossum2d951851994-08-29 12:52:16 +0000167{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000168 PyObject *v;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000169
Guido van Rossum79f25d91997-04-29 20:08:16 +0000170 if (!PyArg_ParseTuple(args, "O:callable", &v))
Guido van Rossum2d951851994-08-29 12:52:16 +0000171 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000172 return PyInt_FromLong((long)PyCallable_Check(v));
Guido van Rossum2d951851994-08-29 12:52:16 +0000173}
174
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000175static char callable_doc[] =
176"callable(object) -> Boolean\n\
177\n\
178Return whether the object is callable (i.e., some kind of function).\n\
179Note that classes are callable, as are instances with a __call__() method.";
180
181
Guido van Rossum79f25d91997-04-29 20:08:16 +0000182static PyObject *
Guido van Rossume77a7571993-11-03 15:01:26 +0000183builtin_filter(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000184 PyObject *self;
185 PyObject *args;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000186{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000187 PyObject *func, *seq, *result;
188 PySequenceMethods *sqf;
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000189 int len;
190 register int i, j;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000191
Guido van Rossum79f25d91997-04-29 20:08:16 +0000192 if (!PyArg_ParseTuple(args, "OO:filter", &func, &seq))
Guido van Rossum12d12c51993-10-26 17:58:25 +0000193 return NULL;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000194
Guido van Rossum79f25d91997-04-29 20:08:16 +0000195 if (PyString_Check(seq)) {
196 PyObject *r = filterstring(func, seq);
Guido van Rossum12d12c51993-10-26 17:58:25 +0000197 return r;
198 }
199
Guido van Rossum79f25d91997-04-29 20:08:16 +0000200 if (PyTuple_Check(seq)) {
201 PyObject *r = filtertuple(func, seq);
Guido van Rossum12d12c51993-10-26 17:58:25 +0000202 return r;
203 }
204
Guido van Rossum09df08a1998-05-22 00:51:39 +0000205 sqf = seq->ob_type->tp_as_sequence;
206 if (sqf == NULL || sqf->sq_length == NULL || sqf->sq_item == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000207 PyErr_SetString(PyExc_TypeError,
Guido van Rossume77a7571993-11-03 15:01:26 +0000208 "argument 2 to filter() must be a sequence type");
Guido van Rossum12d12c51993-10-26 17:58:25 +0000209 goto Fail_2;
210 }
211
212 if ((len = (*sqf->sq_length)(seq)) < 0)
213 goto Fail_2;
214
Guido van Rossum79f25d91997-04-29 20:08:16 +0000215 if (PyList_Check(seq) && seq->ob_refcnt == 1) {
216 Py_INCREF(seq);
Guido van Rossum12d12c51993-10-26 17:58:25 +0000217 result = seq;
218 }
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000219 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000220 if ((result = PyList_New(len)) == NULL)
Guido van Rossum12d12c51993-10-26 17:58:25 +0000221 goto Fail_2;
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000222 }
Guido van Rossum12d12c51993-10-26 17:58:25 +0000223
Guido van Rossum2d951851994-08-29 12:52:16 +0000224 for (i = j = 0; ; ++i) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000225 PyObject *item, *good;
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000226 int ok;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000227
Guido van Rossum2d951851994-08-29 12:52:16 +0000228 if ((item = (*sqf->sq_item)(seq, i)) == NULL) {
Barry Warsawcde8b1b1997-08-22 21:14:38 +0000229 if (PyErr_ExceptionMatches(PyExc_IndexError)) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000230 PyErr_Clear();
Guido van Rossum2d951851994-08-29 12:52:16 +0000231 break;
232 }
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000233 goto Fail_1;
Guido van Rossum2d951851994-08-29 12:52:16 +0000234 }
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000235
Guido van Rossum79f25d91997-04-29 20:08:16 +0000236 if (func == Py_None) {
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000237 good = item;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000238 Py_INCREF(good);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000239 }
240 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000241 PyObject *arg = Py_BuildValue("(O)", item);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000242 if (arg == NULL)
243 goto Fail_1;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000244 good = PyEval_CallObject(func, arg);
245 Py_DECREF(arg);
Guido van Rossum58b68731995-01-10 17:40:55 +0000246 if (good == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000247 Py_DECREF(item);
Guido van Rossum12d12c51993-10-26 17:58:25 +0000248 goto Fail_1;
Guido van Rossum58b68731995-01-10 17:40:55 +0000249 }
Guido van Rossum12d12c51993-10-26 17:58:25 +0000250 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000251 ok = PyObject_IsTrue(good);
252 Py_DECREF(good);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000253 if (ok) {
Guido van Rossum2d951851994-08-29 12:52:16 +0000254 if (j < len) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000255 if (PyList_SetItem(result, j++, item) < 0)
Guido van Rossum2d951851994-08-29 12:52:16 +0000256 goto Fail_1;
257 }
258 else {
Barry Warsawfa77e091999-01-28 18:49:12 +0000259 int status = PyList_Append(result, item);
Guido van Rossum2d951851994-08-29 12:52:16 +0000260 j++;
Barry Warsawfa77e091999-01-28 18:49:12 +0000261 Py_DECREF(item);
262 if (status < 0)
Guido van Rossum2d951851994-08-29 12:52:16 +0000263 goto Fail_1;
264 }
Guido van Rossum58b68731995-01-10 17:40:55 +0000265 } else {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000266 Py_DECREF(item);
Guido van Rossum12d12c51993-10-26 17:58:25 +0000267 }
Guido van Rossum12d12c51993-10-26 17:58:25 +0000268 }
269
Guido van Rossum12d12c51993-10-26 17:58:25 +0000270
Guido van Rossum79f25d91997-04-29 20:08:16 +0000271 if (j < len && PyList_SetSlice(result, j, len, NULL) < 0)
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000272 goto Fail_1;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000273
Guido van Rossum12d12c51993-10-26 17:58:25 +0000274 return result;
275
Guido van Rossum12d12c51993-10-26 17:58:25 +0000276Fail_1:
Guido van Rossum79f25d91997-04-29 20:08:16 +0000277 Py_DECREF(result);
Guido van Rossum12d12c51993-10-26 17:58:25 +0000278Fail_2:
Guido van Rossum12d12c51993-10-26 17:58:25 +0000279 return NULL;
280}
281
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000282static char filter_doc[] =
283"filter(function, sequence) -> list\n\
284\n\
285Return a list containing those items of sequence for which function(item)\n\
286is true. If function is None, return a list of items that are true.";
287
288
Guido van Rossum79f25d91997-04-29 20:08:16 +0000289static PyObject *
Guido van Rossum94390a41992-08-14 15:14:30 +0000290builtin_chr(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000291 PyObject *self;
292 PyObject *args;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000293{
294 long x;
295 char s[1];
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000296
Guido van Rossum79f25d91997-04-29 20:08:16 +0000297 if (!PyArg_ParseTuple(args, "l:chr", &x))
Guido van Rossum3f5da241990-12-20 15:06:42 +0000298 return NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000299 if (x < 0 || x >= 256) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000300 PyErr_SetString(PyExc_ValueError,
301 "chr() arg not in range(256)");
Guido van Rossum3f5da241990-12-20 15:06:42 +0000302 return NULL;
303 }
Guido van Rossum6bf62da1997-04-11 20:37:35 +0000304 s[0] = (char)x;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000305 return PyString_FromStringAndSize(s, 1);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000306}
307
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000308static char chr_doc[] =
309"chr(i) -> character\n\
310\n\
311Return a string of one character with ordinal i; 0 <= i < 256.";
312
313
Guido van Rossum79f25d91997-04-29 20:08:16 +0000314static PyObject *
Guido van Rossuma9e7dc11992-10-18 18:53:57 +0000315builtin_cmp(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000316 PyObject *self;
317 PyObject *args;
Guido van Rossuma9e7dc11992-10-18 18:53:57 +0000318{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000319 PyObject *a, *b;
Guido van Rossum09df08a1998-05-22 00:51:39 +0000320 int c;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000321
Guido van Rossum79f25d91997-04-29 20:08:16 +0000322 if (!PyArg_ParseTuple(args, "OO:cmp", &a, &b))
Guido van Rossuma9e7dc11992-10-18 18:53:57 +0000323 return NULL;
Guido van Rossum09df08a1998-05-22 00:51:39 +0000324 if (PyObject_Cmp(a, b, &c) < 0)
Guido van Rossumc8b6df91997-05-23 00:06:51 +0000325 return NULL;
Guido van Rossum09df08a1998-05-22 00:51:39 +0000326 return PyInt_FromLong((long)c);
Guido van Rossuma9e7dc11992-10-18 18:53:57 +0000327}
328
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000329static char cmp_doc[] =
330"cmp(x, y) -> integer\n\
331\n\
332Return negative if x<y, zero if x==y, positive if x>y.";
333
334
Guido van Rossum79f25d91997-04-29 20:08:16 +0000335static PyObject *
Guido van Rossum5524a591995-01-10 15:26:20 +0000336builtin_coerce(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000337 PyObject *self;
338 PyObject *args;
Guido van Rossum6a00cd81995-01-07 12:39:01 +0000339{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000340 PyObject *v, *w;
341 PyObject *res;
Guido van Rossum5524a591995-01-10 15:26:20 +0000342
Guido van Rossum79f25d91997-04-29 20:08:16 +0000343 if (!PyArg_ParseTuple(args, "OO:coerce", &v, &w))
Guido van Rossum5524a591995-01-10 15:26:20 +0000344 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000345 if (PyNumber_Coerce(&v, &w) < 0)
Guido van Rossum04691fc1992-08-12 15:35:34 +0000346 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000347 res = Py_BuildValue("(OO)", v, w);
348 Py_DECREF(v);
349 Py_DECREF(w);
Guido van Rossum04691fc1992-08-12 15:35:34 +0000350 return res;
351}
352
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000353static char coerce_doc[] =
354"coerce(x, y) -> None or (x1, y1)\n\
355\n\
356When x and y can be coerced to values of the same type, return a tuple\n\
357containing the coerced values. When they can't be coerced, return None.";
358
359
Guido van Rossum79f25d91997-04-29 20:08:16 +0000360static PyObject *
Guido van Rossum5b722181993-03-30 17:46:03 +0000361builtin_compile(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000362 PyObject *self;
363 PyObject *args;
Guido van Rossum5b722181993-03-30 17:46:03 +0000364{
365 char *str;
366 char *filename;
367 char *startstr;
368 int start;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000369
Guido van Rossum79f25d91997-04-29 20:08:16 +0000370 if (!PyArg_ParseTuple(args, "sss:compile", &str, &filename, &startstr))
Guido van Rossum5b722181993-03-30 17:46:03 +0000371 return NULL;
372 if (strcmp(startstr, "exec") == 0)
Guido van Rossumb05a5c71997-05-07 17:46:13 +0000373 start = Py_file_input;
Guido van Rossum5b722181993-03-30 17:46:03 +0000374 else if (strcmp(startstr, "eval") == 0)
Guido van Rossumb05a5c71997-05-07 17:46:13 +0000375 start = Py_eval_input;
Guido van Rossum872537c1995-07-07 22:43:42 +0000376 else if (strcmp(startstr, "single") == 0)
Guido van Rossumb05a5c71997-05-07 17:46:13 +0000377 start = Py_single_input;
Guido van Rossum5b722181993-03-30 17:46:03 +0000378 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000379 PyErr_SetString(PyExc_ValueError,
Guido van Rossum872537c1995-07-07 22:43:42 +0000380 "compile() mode must be 'exec' or 'eval' or 'single'");
Guido van Rossum5b722181993-03-30 17:46:03 +0000381 return NULL;
382 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000383 return Py_CompileString(str, filename, start);
Guido van Rossum5b722181993-03-30 17:46:03 +0000384}
385
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000386static char compile_doc[] =
387"compile(source, filename, mode) -> code object\n\
388\n\
389Compile the source string (a Python module, statement or expression)\n\
390into a code object that can be executed by the exec statement or eval().\n\
391The filename will be used for run-time error messages.\n\
392The mode must be 'exec' to compile a module, 'single' to compile a\n\
393single (interactive) statement, or 'eval' to compile an expression.";
394
395
Guido van Rossum8a5c5d21996-01-12 01:09:56 +0000396#ifndef WITHOUT_COMPLEX
397
Guido van Rossum79f25d91997-04-29 20:08:16 +0000398static PyObject *
Guido van Rossum11950231999-03-25 21:16:07 +0000399complex_from_string(v)
400 PyObject *v;
401{
402 extern double strtod Py_PROTO((const char *, char **));
Guido van Rossum99fb7c71999-04-07 16:05:47 +0000403 char *s, *start, *end;
Guido van Rossum11950231999-03-25 21:16:07 +0000404 double x=0.0, y=0.0, z;
405 int got_re=0, got_im=0, done=0;
406 int digit_or_dot;
407 int sw_error=0;
408 int sign;
409 char buffer[256]; /* For errors */
410
411 start = s = PyString_AS_STRING(v);
412
413 /* position on first nonblank */
414 while (*s && isspace(Py_CHARMASK(*s)))
415 s++;
416 if (s[0] == '\0') {
417 PyErr_SetString(PyExc_ValueError,
418 "empty string for complex()");
419 return NULL;
420 }
421
422 z = -1.0;
423 sign = 1;
424 do {
425
426 switch (*s) {
427
428 case '\0':
429 if (s-start != PyString_GET_SIZE(v)) {
430 PyErr_SetString(
431 PyExc_ValueError,
432 "null byte in argument for complex()");
433 return NULL;
434 }
435 if(!done) sw_error=1;
436 break;
437
438 case '-':
439 sign = -1;
440 /* Fallthrough */
441 case '+':
442 if (done) sw_error=1;
443 s++;
444 if ( *s=='\0'||*s=='+'||*s=='-' ||
445 isspace(Py_CHARMASK(*s)) ) sw_error=1;
446 break;
447
448 case 'J':
449 case 'j':
450 if (got_im || done) {
451 sw_error = 1;
452 break;
453 }
454 if (z<0.0) {
455 y=sign;
456 }
457 else{
458 y=sign*z;
459 }
460 got_im=1;
461 s++;
462 if (*s!='+' && *s!='-' )
463 done=1;
464 break;
465
466 default:
467 if (isspace(Py_CHARMASK(*s))) {
468 while (*s && isspace(Py_CHARMASK(*s)))
469 s++;
470 if (s[0] != '\0')
471 sw_error=1;
472 else
473 done = 1;
474 break;
475 }
476 digit_or_dot =
477 (*s=='.' || isdigit(Py_CHARMASK(*s)));
478 if (done||!digit_or_dot) {
479 sw_error=1;
480 break;
481 }
482 errno = 0;
483 PyFPE_START_PROTECT("strtod", return 0)
484 z = strtod(s, &end) ;
485 PyFPE_END_PROTECT(z)
486 if (errno != 0) {
487 sprintf(buffer,
488 "float() out of range: %.150s", s);
489 PyErr_SetString(
490 PyExc_ValueError,
491 buffer);
492 return NULL;
493 }
494 s=end;
495 if (*s=='J' || *s=='j') {
496
497 break;
498 }
499 if (got_re) {
500 sw_error=1;
501 break;
502 }
503
504 /* accept a real part */
505 x=sign*z;
506 got_re=1;
507 if (got_im) done=1;
508 z = -1.0;
509 sign = 1;
510 break;
511
512 } /* end of switch */
513
514 } while (*s!='\0' && !sw_error);
515
516 if (sw_error) {
517 PyErr_SetString(PyExc_ValueError,
518 "malformed string for complex()");
519 return NULL;
520 }
521
522 return PyComplex_FromDoubles(x,y);
523}
524
525static PyObject *
Guido van Rossum8a5c5d21996-01-12 01:09:56 +0000526builtin_complex(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000527 PyObject *self;
528 PyObject *args;
Guido van Rossum8a5c5d21996-01-12 01:09:56 +0000529{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000530 PyObject *r, *i, *tmp;
531 PyNumberMethods *nbr, *nbi = NULL;
Guido van Rossum530956d1996-07-21 02:27:43 +0000532 Py_complex cr, ci;
Guido van Rossumc6472e91997-03-31 17:15:43 +0000533 int own_r = 0;
Guido van Rossum8a5c5d21996-01-12 01:09:56 +0000534
535 i = NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000536 if (!PyArg_ParseTuple(args, "O|O:complex", &r, &i))
Guido van Rossum8a5c5d21996-01-12 01:09:56 +0000537 return NULL;
Guido van Rossum11950231999-03-25 21:16:07 +0000538 if (PyString_Check(r))
539 return complex_from_string(r);
Guido van Rossum8a5c5d21996-01-12 01:09:56 +0000540 if ((nbr = r->ob_type->tp_as_number) == NULL ||
Guido van Rossumc6472e91997-03-31 17:15:43 +0000541 nbr->nb_float == NULL ||
542 (i != NULL &&
543 ((nbi = i->ob_type->tp_as_number) == NULL ||
544 nbi->nb_float == NULL))) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000545 PyErr_SetString(PyExc_TypeError,
Guido van Rossum8a5c5d21996-01-12 01:09:56 +0000546 "complex() argument can't be converted to complex");
547 return NULL;
548 }
Guido van Rossumed0af8f1996-12-05 23:18:18 +0000549 /* XXX Hack to support classes with __complex__ method */
Guido van Rossum79f25d91997-04-29 20:08:16 +0000550 if (PyInstance_Check(r)) {
551 static PyObject *complexstr;
552 PyObject *f;
Guido van Rossumed0af8f1996-12-05 23:18:18 +0000553 if (complexstr == NULL) {
Guido van Rossum8d751611997-01-18 08:04:16 +0000554 complexstr = PyString_InternFromString("__complex__");
Guido van Rossumed0af8f1996-12-05 23:18:18 +0000555 if (complexstr == NULL)
556 return NULL;
557 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000558 f = PyObject_GetAttr(r, complexstr);
Guido van Rossumed0af8f1996-12-05 23:18:18 +0000559 if (f == NULL)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000560 PyErr_Clear();
Guido van Rossumed0af8f1996-12-05 23:18:18 +0000561 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000562 PyObject *args = Py_BuildValue("()");
Guido van Rossumed0af8f1996-12-05 23:18:18 +0000563 if (args == NULL)
564 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000565 r = PyEval_CallObject(f, args);
566 Py_DECREF(args);
Barry Warsawf988e681999-01-27 23:13:59 +0000567 Py_DECREF(f);
Guido van Rossumed0af8f1996-12-05 23:18:18 +0000568 if (r == NULL)
569 return NULL;
Guido van Rossumc6472e91997-03-31 17:15:43 +0000570 own_r = 1;
Guido van Rossumed0af8f1996-12-05 23:18:18 +0000571 }
572 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000573 if (PyComplex_Check(r)) {
574 cr = ((PyComplexObject*)r)->cval;
Guido van Rossum730806d1998-04-10 22:27:42 +0000575 if (own_r) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000576 Py_DECREF(r);
Guido van Rossum730806d1998-04-10 22:27:42 +0000577 }
Guido van Rossumc6472e91997-03-31 17:15:43 +0000578 }
Guido van Rossum8a5c5d21996-01-12 01:09:56 +0000579 else {
Guido van Rossumfe4b6ee1996-08-08 18:49:41 +0000580 tmp = (*nbr->nb_float)(r);
Guido van Rossum730806d1998-04-10 22:27:42 +0000581 if (own_r) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000582 Py_DECREF(r);
Guido van Rossum730806d1998-04-10 22:27:42 +0000583 }
Guido van Rossumfe4b6ee1996-08-08 18:49:41 +0000584 if (tmp == NULL)
585 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000586 cr.real = PyFloat_AsDouble(tmp);
587 Py_DECREF(tmp);
Guido van Rossum11950231999-03-25 21:16:07 +0000588 cr.imag = 0.0;
Guido van Rossum8a5c5d21996-01-12 01:09:56 +0000589 }
590 if (i == NULL) {
Guido van Rossum11950231999-03-25 21:16:07 +0000591 ci.real = 0.0;
592 ci.imag = 0.0;
Guido van Rossum8a5c5d21996-01-12 01:09:56 +0000593 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000594 else if (PyComplex_Check(i))
595 ci = ((PyComplexObject*)i)->cval;
Guido van Rossum8a5c5d21996-01-12 01:09:56 +0000596 else {
Guido van Rossumc6472e91997-03-31 17:15:43 +0000597 tmp = (*nbi->nb_float)(i);
Guido van Rossumfe4b6ee1996-08-08 18:49:41 +0000598 if (tmp == NULL)
599 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000600 ci.real = PyFloat_AsDouble(tmp);
601 Py_DECREF(tmp);
Guido van Rossum8a5c5d21996-01-12 01:09:56 +0000602 ci.imag = 0.;
603 }
604 cr.real -= ci.imag;
605 cr.imag += ci.real;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000606 return PyComplex_FromCComplex(cr);
Guido van Rossum8a5c5d21996-01-12 01:09:56 +0000607}
608
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000609static char complex_doc[] =
610"complex(real[, imag]) -> complex number\n\
611\n\
612Create a complex number from a real part and an optional imaginary part.\n\
613This is equivalent to (real + imag*1j) where imag defaults to 0.";
614
615
Guido van Rossum8a5c5d21996-01-12 01:09:56 +0000616#endif
617
Guido van Rossum79f25d91997-04-29 20:08:16 +0000618static PyObject *
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000619builtin_dir(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000620 PyObject *self;
621 PyObject *args;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000622{
Guido van Rossum666b17a1997-05-06 16:36:57 +0000623 static char *attrlist[] = {"__members__", "__methods__", NULL};
624 PyObject *v = NULL, *l = NULL, *m = NULL;
625 PyObject *d, *x;
626 int i;
627 char **s;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000628
Guido van Rossum79f25d91997-04-29 20:08:16 +0000629 if (!PyArg_ParseTuple(args, "|O:dir", &v))
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000630 return NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000631 if (v == NULL) {
Guido van Rossum666b17a1997-05-06 16:36:57 +0000632 x = PyEval_GetLocals();
633 if (x == NULL)
634 goto error;
635 l = PyMapping_Keys(x);
636 if (l == NULL)
637 goto error;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000638 }
639 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000640 d = PyObject_GetAttrString(v, "__dict__");
Guido van Rossum666b17a1997-05-06 16:36:57 +0000641 if (d == NULL)
Guido van Rossum795ba581996-05-23 22:49:07 +0000642 PyErr_Clear();
Guido van Rossum666b17a1997-05-06 16:36:57 +0000643 else {
644 l = PyMapping_Keys(d);
645 if (l == NULL)
646 PyErr_Clear();
647 Py_DECREF(d);
648 }
649 if (l == NULL) {
650 l = PyList_New(0);
651 if (l == NULL)
652 goto error;
653 }
654 for (s = attrlist; *s != NULL; s++) {
655 m = PyObject_GetAttrString(v, *s);
656 if (m == NULL) {
657 PyErr_Clear();
658 continue;
659 }
660 for (i = 0; ; i++) {
661 x = PySequence_GetItem(m, i);
662 if (x == NULL) {
663 PyErr_Clear();
664 break;
665 }
666 if (PyList_Append(l, x) != 0) {
667 Py_DECREF(x);
668 Py_DECREF(m);
669 goto error;
670 }
671 Py_DECREF(x);
672 }
673 Py_DECREF(m);
Guido van Rossum795ba581996-05-23 22:49:07 +0000674 }
Guido van Rossum006bcd41991-10-24 14:54:44 +0000675 }
Guido van Rossum666b17a1997-05-06 16:36:57 +0000676 if (PyList_Sort(l) != 0)
677 goto error;
678 return l;
679 error:
680 Py_XDECREF(l);
681 return NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000682}
683
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000684static char dir_doc[] =
685"dir([object]) -> list of strings\n\
686\n\
687Return an alphabetized list of names comprising (some of) the attributes\n\
688of the given object. Without an argument, the names in the current scope\n\
689are listed. With an instance argument, only the instance attributes are\n\
690returned. With a class argument, attributes of the base class are not\n\
691returned. For other types or arguments, this may list members or methods.";
692
693
Guido van Rossum79f25d91997-04-29 20:08:16 +0000694static PyObject *
Guido van Rossum6a00cd81995-01-07 12:39:01 +0000695builtin_divmod(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000696 PyObject *self;
697 PyObject *args;
Guido van Rossum6a00cd81995-01-07 12:39:01 +0000698{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000699 PyObject *v, *w;
Guido van Rossum6a00cd81995-01-07 12:39:01 +0000700
Guido van Rossum79f25d91997-04-29 20:08:16 +0000701 if (!PyArg_ParseTuple(args, "OO:divmod", &v, &w))
Guido van Rossum6a00cd81995-01-07 12:39:01 +0000702 return NULL;
Guido van Rossum09df08a1998-05-22 00:51:39 +0000703 return PyNumber_Divmod(v, w);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000704}
705
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000706static char divmod_doc[] =
707"divmod(x, y) -> (div, mod)\n\
708\n\
709Return the tuple ((x-x%y)/y, x%y). Invariant: div*y + mod == x.";
710
711
Guido van Rossum79f25d91997-04-29 20:08:16 +0000712static PyObject *
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000713builtin_eval(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000714 PyObject *self;
715 PyObject *args;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000716{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000717 PyObject *cmd;
718 PyObject *globals = Py_None, *locals = Py_None;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000719 char *str;
Guido van Rossum590baa41993-11-30 13:40:46 +0000720
Guido van Rossum79f25d91997-04-29 20:08:16 +0000721 if (!PyArg_ParseTuple(args, "O|O!O!:eval",
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000722 &cmd,
Guido van Rossum79f25d91997-04-29 20:08:16 +0000723 &PyDict_Type, &globals,
724 &PyDict_Type, &locals))
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000725 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000726 if (globals == Py_None) {
727 globals = PyEval_GetGlobals();
728 if (locals == Py_None)
729 locals = PyEval_GetLocals();
Guido van Rossum6135a871995-01-09 17:53:26 +0000730 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000731 else if (locals == Py_None)
Guido van Rossum6135a871995-01-09 17:53:26 +0000732 locals = globals;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000733 if (PyDict_GetItemString(globals, "__builtins__") == NULL) {
734 if (PyDict_SetItemString(globals, "__builtins__",
735 PyEval_GetBuiltins()) != 0)
Guido van Rossum6135a871995-01-09 17:53:26 +0000736 return NULL;
737 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000738 if (PyCode_Check(cmd))
739 return PyEval_EvalCode((PyCodeObject *) cmd, globals, locals);
740 if (!PyString_Check(cmd)) {
741 PyErr_SetString(PyExc_TypeError,
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000742 "eval() argument 1 must be string or code object");
Guido van Rossum94390a41992-08-14 15:14:30 +0000743 return NULL;
744 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000745 str = PyString_AsString(cmd);
746 if ((int)strlen(str) != PyString_Size(cmd)) {
747 PyErr_SetString(PyExc_ValueError,
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000748 "embedded '\\0' in string arg");
749 return NULL;
Guido van Rossumf08ab0a1992-03-04 16:41:41 +0000750 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000751 while (*str == ' ' || *str == '\t')
752 str++;
Guido van Rossumb05a5c71997-05-07 17:46:13 +0000753 return PyRun_String(str, Py_eval_input, globals, locals);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000754}
755
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000756static char eval_doc[] =
757"eval(source[, globals[, locals]]) -> value\n\
758\n\
759Evaluate the source in the context of globals and locals.\n\
760The source may be a string representing a Python expression\n\
761or a code object as returned by compile().\n\
762The globals and locals are dictionaries, defaulting to the current\n\
763globals and locals. If only globals is given, locals defaults to it.";
764
765
Guido van Rossum79f25d91997-04-29 20:08:16 +0000766static PyObject *
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000767builtin_execfile(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000768 PyObject *self;
769 PyObject *args;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000770{
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000771 char *filename;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000772 PyObject *globals = Py_None, *locals = Py_None;
773 PyObject *res;
Guido van Rossum0f61f8a1992-02-25 18:55:05 +0000774 FILE* fp;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000775
Guido van Rossum79f25d91997-04-29 20:08:16 +0000776 if (!PyArg_ParseTuple(args, "s|O!O!:execfile",
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000777 &filename,
Guido van Rossum79f25d91997-04-29 20:08:16 +0000778 &PyDict_Type, &globals,
779 &PyDict_Type, &locals))
Guido van Rossum0f61f8a1992-02-25 18:55:05 +0000780 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000781 if (globals == Py_None) {
782 globals = PyEval_GetGlobals();
783 if (locals == Py_None)
784 locals = PyEval_GetLocals();
Guido van Rossum6135a871995-01-09 17:53:26 +0000785 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000786 else if (locals == Py_None)
Guido van Rossum6135a871995-01-09 17:53:26 +0000787 locals = globals;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000788 if (PyDict_GetItemString(globals, "__builtins__") == NULL) {
789 if (PyDict_SetItemString(globals, "__builtins__",
790 PyEval_GetBuiltins()) != 0)
Guido van Rossum6135a871995-01-09 17:53:26 +0000791 return NULL;
792 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000793 Py_BEGIN_ALLOW_THREADS
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000794 fp = fopen(filename, "r");
Guido van Rossum79f25d91997-04-29 20:08:16 +0000795 Py_END_ALLOW_THREADS
Guido van Rossum0f61f8a1992-02-25 18:55:05 +0000796 if (fp == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000797 PyErr_SetFromErrno(PyExc_IOError);
Guido van Rossum0f61f8a1992-02-25 18:55:05 +0000798 return NULL;
799 }
Guido van Rossumb05a5c71997-05-07 17:46:13 +0000800 res = PyRun_File(fp, filename, Py_file_input, globals, locals);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000801 Py_BEGIN_ALLOW_THREADS
Guido van Rossum0f61f8a1992-02-25 18:55:05 +0000802 fclose(fp);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000803 Py_END_ALLOW_THREADS
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000804 return res;
Guido van Rossum0f61f8a1992-02-25 18:55:05 +0000805}
806
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000807static char execfile_doc[] =
808"execfile(filename[, globals[, locals]])\n\
809\n\
810Read and execute a Python script from a file.\n\
811The globals and locals are dictionaries, defaulting to the current\n\
812globals and locals. If only globals is given, locals defaults to it.";
813
814
Guido van Rossum79f25d91997-04-29 20:08:16 +0000815static PyObject *
Guido van Rossum94390a41992-08-14 15:14:30 +0000816builtin_getattr(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000817 PyObject *self;
818 PyObject *args;
Guido van Rossum33894be1992-01-27 16:53:09 +0000819{
Guido van Rossum950ff291998-06-29 13:38:57 +0000820 PyObject *v, *result, *dflt = NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000821 PyObject *name;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000822
Guido van Rossum950ff291998-06-29 13:38:57 +0000823 if (!PyArg_ParseTuple(args, "OS|O:getattr", &v, &name, &dflt))
Guido van Rossum33894be1992-01-27 16:53:09 +0000824 return NULL;
Guido van Rossum950ff291998-06-29 13:38:57 +0000825 result = PyObject_GetAttr(v, name);
826 if (result == NULL && dflt != NULL) {
827 PyErr_Clear();
828 Py_INCREF(dflt);
829 result = dflt;
830 }
831 return result;
Guido van Rossum9bfef441993-03-29 10:43:31 +0000832}
833
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000834static char getattr_doc[] =
Guido van Rossum950ff291998-06-29 13:38:57 +0000835"getattr(object, name[, default]) -> value\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000836\n\
Guido van Rossum950ff291998-06-29 13:38:57 +0000837Get a named attribute from an object; getattr(x, 'y') is equivalent to x.y.\n\
838When a default argument is given, it is returned when the attribute doesn't\n\
839exist; without it, an exception is raised in that case.";
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000840
841
Guido van Rossum79f25d91997-04-29 20:08:16 +0000842static PyObject *
Guido van Rossum872537c1995-07-07 22:43:42 +0000843builtin_globals(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000844 PyObject *self;
845 PyObject *args;
Guido van Rossum872537c1995-07-07 22:43:42 +0000846{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000847 PyObject *d;
Guido van Rossum872537c1995-07-07 22:43:42 +0000848
Guido van Rossum79f25d91997-04-29 20:08:16 +0000849 if (!PyArg_ParseTuple(args, ""))
Guido van Rossum872537c1995-07-07 22:43:42 +0000850 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000851 d = PyEval_GetGlobals();
852 Py_INCREF(d);
Guido van Rossum872537c1995-07-07 22:43:42 +0000853 return d;
854}
855
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000856static char globals_doc[] =
857"globals() -> dictionary\n\
858\n\
859Return the dictionary containing the current scope's global variables.";
860
861
Guido van Rossum79f25d91997-04-29 20:08:16 +0000862static PyObject *
Guido van Rossum9bfef441993-03-29 10:43:31 +0000863builtin_hasattr(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000864 PyObject *self;
865 PyObject *args;
Guido van Rossum9bfef441993-03-29 10:43:31 +0000866{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000867 PyObject *v;
868 PyObject *name;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000869
Guido van Rossum79f25d91997-04-29 20:08:16 +0000870 if (!PyArg_ParseTuple(args, "OS:hasattr", &v, &name))
Guido van Rossum9bfef441993-03-29 10:43:31 +0000871 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000872 v = PyObject_GetAttr(v, name);
Guido van Rossum9bfef441993-03-29 10:43:31 +0000873 if (v == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000874 PyErr_Clear();
Guido van Rossum09df08a1998-05-22 00:51:39 +0000875 Py_INCREF(Py_False);
876 return Py_False;
Guido van Rossum9bfef441993-03-29 10:43:31 +0000877 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000878 Py_DECREF(v);
Guido van Rossum09df08a1998-05-22 00:51:39 +0000879 Py_INCREF(Py_True);
880 return Py_True;
Guido van Rossum33894be1992-01-27 16:53:09 +0000881}
882
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000883static char hasattr_doc[] =
884"hasattr(object, name) -> Boolean\n\
885\n\
886Return whether the object has an attribute with the given name.\n\
887(This is done by calling getattr(object, name) and catching exceptions.)";
888
889
Guido van Rossum79f25d91997-04-29 20:08:16 +0000890static PyObject *
Guido van Rossum5b722181993-03-30 17:46:03 +0000891builtin_id(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000892 PyObject *self;
893 PyObject *args;
Guido van Rossum5b722181993-03-30 17:46:03 +0000894{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000895 PyObject *v;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000896
Guido van Rossum79f25d91997-04-29 20:08:16 +0000897 if (!PyArg_ParseTuple(args, "O:id", &v))
Guido van Rossum5b722181993-03-30 17:46:03 +0000898 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000899 return PyInt_FromLong((long)v);
Guido van Rossum5b722181993-03-30 17:46:03 +0000900}
901
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000902static char id_doc[] =
903"id(object) -> integer\n\
904\n\
905Return the identity of an object. This is guaranteed to be unique among\n\
906simultaneously existing objects. (Hint: it's the object's memory address.)";
907
908
Guido van Rossum79f25d91997-04-29 20:08:16 +0000909static PyObject *
Guido van Rossum12d12c51993-10-26 17:58:25 +0000910builtin_map(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000911 PyObject *self;
912 PyObject *args;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000913{
914 typedef struct {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000915 PyObject *seq;
916 PySequenceMethods *sqf;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000917 int len;
918 } sequence;
919
Guido van Rossum79f25d91997-04-29 20:08:16 +0000920 PyObject *func, *result;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000921 sequence *seqs = NULL, *sqp;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000922 int n, len;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000923 register int i, j;
924
Guido van Rossum79f25d91997-04-29 20:08:16 +0000925 n = PyTuple_Size(args);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000926 if (n < 2) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000927 PyErr_SetString(PyExc_TypeError,
928 "map() requires at least two args");
Guido van Rossum12d12c51993-10-26 17:58:25 +0000929 return NULL;
930 }
931
Guido van Rossum79f25d91997-04-29 20:08:16 +0000932 func = PyTuple_GetItem(args, 0);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000933 n--;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000934
Guido van Rossumfa4ac711998-07-10 17:37:30 +0000935 if (func == Py_None && n == 1) {
936 /* map(None, S) is the same as list(S). */
937 return PySequence_List(PyTuple_GetItem(args, 1));
938 }
939
Guido van Rossum79f25d91997-04-29 20:08:16 +0000940 if ((seqs = PyMem_NEW(sequence, n)) == NULL) {
941 PyErr_NoMemory();
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000942 goto Fail_2;
943 }
Guido van Rossum12d12c51993-10-26 17:58:25 +0000944
Guido van Rossum2d951851994-08-29 12:52:16 +0000945 for (len = 0, i = 0, sqp = seqs; i < n; ++i, ++sqp) {
Guido van Rossum12d12c51993-10-26 17:58:25 +0000946 int curlen;
Guido van Rossum09df08a1998-05-22 00:51:39 +0000947 PySequenceMethods *sqf;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000948
Guido van Rossum79f25d91997-04-29 20:08:16 +0000949 if ((sqp->seq = PyTuple_GetItem(args, i + 1)) == NULL)
Guido van Rossum12d12c51993-10-26 17:58:25 +0000950 goto Fail_2;
951
Guido van Rossum09df08a1998-05-22 00:51:39 +0000952 sqp->sqf = sqf = sqp->seq->ob_type->tp_as_sequence;
953 if (sqf == NULL ||
954 sqf->sq_length == NULL ||
955 sqf->sq_item == NULL)
956 {
Guido van Rossum12d12c51993-10-26 17:58:25 +0000957 static char errmsg[] =
958 "argument %d to map() must be a sequence object";
Guido van Rossum15e33a41997-04-30 19:00:27 +0000959 char errbuf[sizeof(errmsg) + 25];
Guido van Rossum12d12c51993-10-26 17:58:25 +0000960
961 sprintf(errbuf, errmsg, i+2);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000962 PyErr_SetString(PyExc_TypeError, errbuf);
Guido van Rossum12d12c51993-10-26 17:58:25 +0000963 goto Fail_2;
964 }
965
966 if ((curlen = sqp->len = (*sqp->sqf->sq_length)(sqp->seq)) < 0)
967 goto Fail_2;
968
969 if (curlen > len)
970 len = curlen;
971 }
972
Guido van Rossum79f25d91997-04-29 20:08:16 +0000973 if ((result = (PyObject *) PyList_New(len)) == NULL)
Guido van Rossum12d12c51993-10-26 17:58:25 +0000974 goto Fail_2;
975
Guido van Rossum2d951851994-08-29 12:52:16 +0000976 for (i = 0; ; ++i) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000977 PyObject *alist, *item=NULL, *value;
Guido van Rossum2d951851994-08-29 12:52:16 +0000978 int any = 0;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000979
Guido van Rossum79f25d91997-04-29 20:08:16 +0000980 if (func == Py_None && n == 1)
Guido van Rossum32120311995-07-10 13:52:21 +0000981 alist = NULL;
Guido van Rossum2d951851994-08-29 12:52:16 +0000982 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000983 if ((alist = PyTuple_New(n)) == NULL)
Guido van Rossum2d951851994-08-29 12:52:16 +0000984 goto Fail_1;
985 }
Guido van Rossum12d12c51993-10-26 17:58:25 +0000986
987 for (j = 0, sqp = seqs; j < n; ++j, ++sqp) {
Guido van Rossum2d951851994-08-29 12:52:16 +0000988 if (sqp->len < 0) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000989 Py_INCREF(Py_None);
990 item = Py_None;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000991 }
Guido van Rossum12d12c51993-10-26 17:58:25 +0000992 else {
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000993 item = (*sqp->sqf->sq_item)(sqp->seq, i);
Guido van Rossum2d951851994-08-29 12:52:16 +0000994 if (item == NULL) {
Barry Warsawcde8b1b1997-08-22 21:14:38 +0000995 if (PyErr_ExceptionMatches(
996 PyExc_IndexError))
997 {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000998 PyErr_Clear();
999 Py_INCREF(Py_None);
1000 item = Py_None;
Guido van Rossum2d951851994-08-29 12:52:16 +00001001 sqp->len = -1;
1002 }
1003 else {
1004 goto Fail_0;
1005 }
1006 }
1007 else
1008 any = 1;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001009
Guido van Rossum12d12c51993-10-26 17:58:25 +00001010 }
Guido van Rossum32120311995-07-10 13:52:21 +00001011 if (!alist)
Guido van Rossum2d951851994-08-29 12:52:16 +00001012 break;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001013 if (PyTuple_SetItem(alist, j, item) < 0) {
1014 Py_DECREF(item);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00001015 goto Fail_0;
Guido van Rossum2d951851994-08-29 12:52:16 +00001016 }
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00001017 continue;
1018
1019 Fail_0:
Guido van Rossum79f25d91997-04-29 20:08:16 +00001020 Py_XDECREF(alist);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00001021 goto Fail_1;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001022 }
1023
Guido van Rossum32120311995-07-10 13:52:21 +00001024 if (!alist)
1025 alist = item;
Guido van Rossum2d951851994-08-29 12:52:16 +00001026
1027 if (!any) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001028 Py_DECREF(alist);
Guido van Rossum2d951851994-08-29 12:52:16 +00001029 break;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001030 }
Guido van Rossum2d951851994-08-29 12:52:16 +00001031
Guido van Rossum79f25d91997-04-29 20:08:16 +00001032 if (func == Py_None)
Guido van Rossum32120311995-07-10 13:52:21 +00001033 value = alist;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001034 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001035 value = PyEval_CallObject(func, alist);
1036 Py_DECREF(alist);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00001037 if (value == NULL)
1038 goto Fail_1;
Guido van Rossum2d951851994-08-29 12:52:16 +00001039 }
1040 if (i >= len) {
Barry Warsawfa77e091999-01-28 18:49:12 +00001041 int status = PyList_Append(result, value);
Barry Warsaw21332871999-01-28 04:21:35 +00001042 Py_DECREF(value);
Barry Warsawfa77e091999-01-28 18:49:12 +00001043 if (status < 0)
1044 goto Fail_1;
Guido van Rossum2d951851994-08-29 12:52:16 +00001045 }
1046 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001047 if (PyList_SetItem(result, i, value) < 0)
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00001048 goto Fail_1;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001049 }
1050 }
1051
Guido van Rossumfa4ac711998-07-10 17:37:30 +00001052 if (i < len && PyList_SetSlice(result, i, len, NULL) < 0)
1053 goto Fail_1;
1054
Guido van Rossum79f25d91997-04-29 20:08:16 +00001055 PyMem_DEL(seqs);
Guido van Rossum12d12c51993-10-26 17:58:25 +00001056 return result;
1057
Guido van Rossum12d12c51993-10-26 17:58:25 +00001058Fail_1:
Guido van Rossum79f25d91997-04-29 20:08:16 +00001059 Py_DECREF(result);
Guido van Rossum12d12c51993-10-26 17:58:25 +00001060Fail_2:
Guido van Rossum79f25d91997-04-29 20:08:16 +00001061 if (seqs) PyMem_DEL(seqs);
Guido van Rossum12d12c51993-10-26 17:58:25 +00001062 return NULL;
1063}
1064
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001065static char map_doc[] =
1066"map(function, sequence[, sequence, ...]) -> list\n\
1067\n\
1068Return a list of the results of applying the function to the items of\n\
1069the argument sequence(s). If more than one sequence is given, the\n\
1070function is called with an argument list consisting of the corresponding\n\
1071item of each sequence, substituting None for missing values when not all\n\
1072sequences have the same length. If the function is None, return a list of\n\
1073the items of the sequence (or a list of tuples if more than one sequence).";
1074
1075
Guido van Rossum79f25d91997-04-29 20:08:16 +00001076static PyObject *
Guido van Rossum94390a41992-08-14 15:14:30 +00001077builtin_setattr(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001078 PyObject *self;
1079 PyObject *args;
Guido van Rossum33894be1992-01-27 16:53:09 +00001080{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001081 PyObject *v;
1082 PyObject *name;
1083 PyObject *value;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001084
Guido van Rossum79f25d91997-04-29 20:08:16 +00001085 if (!PyArg_ParseTuple(args, "OSO:setattr", &v, &name, &value))
Guido van Rossum33894be1992-01-27 16:53:09 +00001086 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001087 if (PyObject_SetAttr(v, name, value) != 0)
Guido van Rossum33894be1992-01-27 16:53:09 +00001088 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001089 Py_INCREF(Py_None);
1090 return Py_None;
Guido van Rossum33894be1992-01-27 16:53:09 +00001091}
1092
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001093static char setattr_doc[] =
1094"setattr(object, name, value)\n\
1095\n\
1096Set a named attribute on an object; setattr(x, 'y', v) is equivalent to\n\
1097``x.y = v''.";
1098
1099
Guido van Rossum79f25d91997-04-29 20:08:16 +00001100static PyObject *
Guido van Rossum14144fc1994-08-29 12:53:40 +00001101builtin_delattr(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001102 PyObject *self;
1103 PyObject *args;
Guido van Rossum14144fc1994-08-29 12:53:40 +00001104{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001105 PyObject *v;
1106 PyObject *name;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001107
Guido van Rossum79f25d91997-04-29 20:08:16 +00001108 if (!PyArg_ParseTuple(args, "OS:delattr", &v, &name))
Guido van Rossum14144fc1994-08-29 12:53:40 +00001109 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001110 if (PyObject_SetAttr(v, name, (PyObject *)NULL) != 0)
Guido van Rossum14144fc1994-08-29 12:53:40 +00001111 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001112 Py_INCREF(Py_None);
1113 return Py_None;
Guido van Rossum14144fc1994-08-29 12:53:40 +00001114}
1115
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001116static char delattr_doc[] =
Guido van Rossumdf12a591998-11-23 22:13:04 +00001117"delattr(object, name)\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001118\n\
1119Delete a named attribute on an object; delattr(x, 'y') is equivalent to\n\
1120``del x.y''.";
1121
1122
Guido van Rossum79f25d91997-04-29 20:08:16 +00001123static PyObject *
Guido van Rossum9bfef441993-03-29 10:43:31 +00001124builtin_hash(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001125 PyObject *self;
1126 PyObject *args;
Guido van Rossum9bfef441993-03-29 10:43:31 +00001127{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001128 PyObject *v;
Guido van Rossum9bfef441993-03-29 10:43:31 +00001129 long x;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001130
Guido van Rossum79f25d91997-04-29 20:08:16 +00001131 if (!PyArg_ParseTuple(args, "O:hash", &v))
Guido van Rossum9bfef441993-03-29 10:43:31 +00001132 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001133 x = PyObject_Hash(v);
Guido van Rossum9bfef441993-03-29 10:43:31 +00001134 if (x == -1)
1135 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001136 return PyInt_FromLong(x);
Guido van Rossum9bfef441993-03-29 10:43:31 +00001137}
1138
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001139static char hash_doc[] =
1140"hash(object) -> integer\n\
1141\n\
1142Return a hash value for the object. Two objects with the same value have\n\
1143the same hash value. The reverse is not necessarily true, but likely.";
1144
1145
Guido van Rossum79f25d91997-04-29 20:08:16 +00001146static PyObject *
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001147builtin_hex(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001148 PyObject *self;
1149 PyObject *args;
Guido van Rossum006bcd41991-10-24 14:54:44 +00001150{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001151 PyObject *v;
1152 PyNumberMethods *nb;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001153
Guido van Rossum79f25d91997-04-29 20:08:16 +00001154 if (!PyArg_ParseTuple(args, "O:hex", &v))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001155 return NULL;
Guido van Rossum1899c2e1992-09-12 11:09:23 +00001156
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001157 if ((nb = v->ob_type->tp_as_number) == NULL ||
Guido van Rossum1899c2e1992-09-12 11:09:23 +00001158 nb->nb_hex == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001159 PyErr_SetString(PyExc_TypeError,
Guido van Rossum1899c2e1992-09-12 11:09:23 +00001160 "hex() argument can't be converted to hex");
1161 return NULL;
Guido van Rossum006bcd41991-10-24 14:54:44 +00001162 }
Guido van Rossum1899c2e1992-09-12 11:09:23 +00001163 return (*nb->nb_hex)(v);
Guido van Rossum006bcd41991-10-24 14:54:44 +00001164}
1165
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001166static char hex_doc[] =
1167"hex(number) -> string\n\
1168\n\
1169Return the hexadecimal representation of an integer or long integer.";
1170
1171
Guido van Rossum79f25d91997-04-29 20:08:16 +00001172static PyObject *builtin_raw_input Py_PROTO((PyObject *, PyObject *));
Guido van Rossum3165fe61992-09-25 21:59:05 +00001173
Guido van Rossum79f25d91997-04-29 20:08:16 +00001174static PyObject *
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001175builtin_input(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001176 PyObject *self;
1177 PyObject *args;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001178{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001179 PyObject *line;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001180 char *str;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001181 PyObject *res;
1182 PyObject *globals, *locals;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001183
1184 line = builtin_raw_input(self, args);
Guido van Rossum3165fe61992-09-25 21:59:05 +00001185 if (line == NULL)
1186 return line;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001187 if (!PyArg_Parse(line, "s;embedded '\\0' in input line", &str))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001188 return NULL;
1189 while (*str == ' ' || *str == '\t')
1190 str++;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001191 globals = PyEval_GetGlobals();
1192 locals = PyEval_GetLocals();
1193 if (PyDict_GetItemString(globals, "__builtins__") == NULL) {
1194 if (PyDict_SetItemString(globals, "__builtins__",
1195 PyEval_GetBuiltins()) != 0)
Guido van Rossum6135a871995-01-09 17:53:26 +00001196 return NULL;
1197 }
Guido van Rossumb05a5c71997-05-07 17:46:13 +00001198 res = PyRun_String(str, Py_eval_input, globals, locals);
Guido van Rossum79f25d91997-04-29 20:08:16 +00001199 Py_DECREF(line);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001200 return res;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001201}
1202
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001203static char input_doc[] =
1204"input([prompt]) -> value\n\
1205\n\
1206Equivalent to eval(raw_input(prompt)).";
1207
1208
Guido van Rossume8811f81997-02-14 15:48:05 +00001209static PyObject *
1210builtin_intern(self, args)
1211 PyObject *self;
1212 PyObject *args;
1213{
1214 PyObject *s;
1215 if (!PyArg_ParseTuple(args, "S", &s))
1216 return NULL;
1217 Py_INCREF(s);
1218 PyString_InternInPlace(&s);
1219 return s;
1220}
1221
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001222static char intern_doc[] =
1223"intern(string) -> string\n\
1224\n\
1225``Intern'' the given string. This enters the string in the (global)\n\
1226table of interned strings whose purpose is to speed up dictionary lookups.\n\
1227Return the string itself or the previously interned string object with the\n\
1228same value.";
1229
1230
Guido van Rossum79f25d91997-04-29 20:08:16 +00001231static PyObject *
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001232builtin_int(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001233 PyObject *self;
1234 PyObject *args;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001235{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001236 PyObject *v;
Barry Warsaw226ae6c1999-10-12 19:54:53 +00001237 int base = -909; /* unlikely! */
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001238
Barry Warsaw226ae6c1999-10-12 19:54:53 +00001239 if (!PyArg_ParseTuple(args, "O|i:int", &v, &base))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001240 return NULL;
Barry Warsaw226ae6c1999-10-12 19:54:53 +00001241 if (base == -909)
1242 return PyNumber_Int(v);
1243 else if (!PyString_Check(v)) {
1244 PyErr_SetString(PyExc_TypeError,
1245 "can't convert non-string with explicit base");
1246 return NULL;
1247 }
1248 return PyInt_FromString(PyString_AS_STRING(v), NULL, base);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001249}
1250
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001251static char int_doc[] =
Barry Warsaw226ae6c1999-10-12 19:54:53 +00001252"int(x[, base]) -> integer\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001253\n\
Barry Warsaw226ae6c1999-10-12 19:54:53 +00001254Convert a string or number to an integer, if possible. A floating point\n\
1255argument will be truncated towards zero (this does not include a string\n\
1256representation of a floating point number!) When converting a string, use\n\
1257the optional base. It is an error to supply a base when converting a\n\
1258non-string.";
1259
1260
1261static PyObject *
1262builtin_long(self, args)
1263 PyObject *self;
1264 PyObject *args;
1265{
1266 PyObject *v;
1267 int base = -909; /* unlikely! */
1268
1269 if (!PyArg_ParseTuple(args, "O|i:long", &v, &base))
1270 return NULL;
1271 if (base == -909)
1272 return PyNumber_Long(v);
1273 else if (!PyString_Check(v)) {
1274 PyErr_SetString(PyExc_TypeError,
1275 "can't convert non-string with explicit base");
1276 return NULL;
1277 }
1278 return PyLong_FromString(PyString_AS_STRING(v), NULL, base);
1279}
1280
1281static char long_doc[] =
1282"long(x) -> long integer\n\
1283long(x, base) -> long integer\n\
1284\n\
1285Convert a string or number to a long integer, if possible. A floating\n\
1286point argument will be truncated towards zero (this does not include a\n\
1287string representation of a floating point number!) When converting a\n\
1288string, use the given base. It is an error to supply a base when\n\
1289converting a non-string.";
1290
1291
1292static PyObject *
1293builtin_float(self, args)
1294 PyObject *self;
1295 PyObject *args;
1296{
1297 PyObject *v;
1298
1299 if (!PyArg_ParseTuple(args, "O:float", &v))
1300 return NULL;
1301 if (PyString_Check(v))
1302 return PyFloat_FromString(v, NULL);
1303 return PyNumber_Float(v);
1304}
1305
1306static char float_doc[] =
1307"float(x) -> floating point number\n\
1308\n\
1309Convert a string or number to a floating point number, if possible.";
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001310
1311
Guido van Rossum79f25d91997-04-29 20:08:16 +00001312static PyObject *
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001313builtin_len(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001314 PyObject *self;
1315 PyObject *args;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001316{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001317 PyObject *v;
Guido van Rossum8ea9f4d1998-06-29 22:26:50 +00001318 long res;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001319
Guido van Rossum79f25d91997-04-29 20:08:16 +00001320 if (!PyArg_ParseTuple(args, "O:len", &v))
Guido van Rossum3f5da241990-12-20 15:06:42 +00001321 return NULL;
Guido van Rossum8ea9f4d1998-06-29 22:26:50 +00001322 res = PyObject_Length(v);
1323 if (res < 0 && PyErr_Occurred())
1324 return NULL;
1325 return PyInt_FromLong(res);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001326}
1327
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001328static char len_doc[] =
1329"len(object) -> integer\n\
1330\n\
1331Return the number of items of a sequence or mapping.";
1332
1333
Guido van Rossum79f25d91997-04-29 20:08:16 +00001334static PyObject *
Guido van Rossumd1705771996-04-09 02:41:06 +00001335builtin_list(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001336 PyObject *self;
1337 PyObject *args;
Guido van Rossumd1705771996-04-09 02:41:06 +00001338{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001339 PyObject *v;
Guido van Rossumd1705771996-04-09 02:41:06 +00001340
Guido van Rossum79f25d91997-04-29 20:08:16 +00001341 if (!PyArg_ParseTuple(args, "O:list", &v))
Guido van Rossumd1705771996-04-09 02:41:06 +00001342 return NULL;
Guido van Rossum09df08a1998-05-22 00:51:39 +00001343 return PySequence_List(v);
Guido van Rossumd1705771996-04-09 02:41:06 +00001344}
1345
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001346static char list_doc[] =
1347"list(sequence) -> list\n\
1348\n\
1349Return a new list whose items are the same as those of the argument sequence.";
1350
Guido van Rossum8861b741996-07-30 16:49:37 +00001351
1352static PyObject *
1353builtin_slice(self, args)
1354 PyObject *self;
1355 PyObject *args;
1356{
Guido van Rossum09df08a1998-05-22 00:51:39 +00001357 PyObject *start, *stop, *step;
Guido van Rossum8861b741996-07-30 16:49:37 +00001358
Guido van Rossum09df08a1998-05-22 00:51:39 +00001359 start = stop = step = NULL;
Guido van Rossum8861b741996-07-30 16:49:37 +00001360
Guido van Rossum09df08a1998-05-22 00:51:39 +00001361 if (!PyArg_ParseTuple(args, "O|OO:slice", &start, &stop, &step))
1362 return NULL;
Guido van Rossum8861b741996-07-30 16:49:37 +00001363
Guido van Rossum09df08a1998-05-22 00:51:39 +00001364 /* This swapping of stop and start is to maintain similarity with
1365 range(). */
1366 if (stop == NULL) {
1367 stop = start;
1368 start = NULL;
1369 }
1370 return PySlice_New(start, stop, step);
Guido van Rossum8861b741996-07-30 16:49:37 +00001371}
1372
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001373static char slice_doc[] =
Fred Drake3d587441999-07-19 15:21:16 +00001374"slice([start,] stop[, step]) -> slice object\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001375\n\
1376Create a slice object. This is used for slicing by the Numeric extensions.";
1377
1378
Guido van Rossum79f25d91997-04-29 20:08:16 +00001379static PyObject *
Guido van Rossum872537c1995-07-07 22:43:42 +00001380builtin_locals(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001381 PyObject *self;
1382 PyObject *args;
Guido van Rossum872537c1995-07-07 22:43:42 +00001383{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001384 PyObject *d;
Guido van Rossum872537c1995-07-07 22:43:42 +00001385
Guido van Rossum79f25d91997-04-29 20:08:16 +00001386 if (!PyArg_ParseTuple(args, ""))
Guido van Rossum872537c1995-07-07 22:43:42 +00001387 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001388 d = PyEval_GetLocals();
1389 Py_INCREF(d);
Guido van Rossum872537c1995-07-07 22:43:42 +00001390 return d;
1391}
1392
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001393static char locals_doc[] =
1394"locals() -> dictionary\n\
1395\n\
1396Return the dictionary containing the current scope's local variables.";
1397
1398
Guido van Rossum79f25d91997-04-29 20:08:16 +00001399static PyObject *
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001400min_max(args, sign)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001401 PyObject *args;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001402 int sign;
1403{
Guido van Rossum2d951851994-08-29 12:52:16 +00001404 int i;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001405 PyObject *v, *w, *x;
1406 PySequenceMethods *sq;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001407
Guido van Rossum79f25d91997-04-29 20:08:16 +00001408 if (PyTuple_Size(args) > 1)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001409 v = args;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001410 else if (!PyArg_ParseTuple(args, "O:min/max", &v))
Guido van Rossum3f5da241990-12-20 15:06:42 +00001411 return NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001412 sq = v->ob_type->tp_as_sequence;
Guido van Rossum09df08a1998-05-22 00:51:39 +00001413 if (sq == NULL || sq->sq_item == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001414 PyErr_SetString(PyExc_TypeError,
1415 "min() or max() of non-sequence");
Guido van Rossum3f5da241990-12-20 15:06:42 +00001416 return NULL;
1417 }
Guido van Rossum2d951851994-08-29 12:52:16 +00001418 w = NULL;
1419 for (i = 0; ; i++) {
Guido van Rossum3f5da241990-12-20 15:06:42 +00001420 x = (*sq->sq_item)(v, i); /* Implies INCREF */
Guido van Rossum2d951851994-08-29 12:52:16 +00001421 if (x == NULL) {
Barry Warsawcde8b1b1997-08-22 21:14:38 +00001422 if (PyErr_ExceptionMatches(PyExc_IndexError)) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001423 PyErr_Clear();
Guido van Rossum2d951851994-08-29 12:52:16 +00001424 break;
1425 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00001426 Py_XDECREF(w);
Guido van Rossum2d951851994-08-29 12:52:16 +00001427 return NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001428 }
Guido van Rossum2d951851994-08-29 12:52:16 +00001429 if (w == NULL)
1430 w = x;
1431 else {
Guido van Rossumc8b6df91997-05-23 00:06:51 +00001432 int c = PyObject_Compare(x, w);
1433 if (c && PyErr_Occurred()) {
1434 Py_DECREF(x);
1435 Py_XDECREF(w);
1436 return NULL;
1437 }
1438 if (c * sign > 0) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001439 Py_DECREF(w);
Guido van Rossum2d951851994-08-29 12:52:16 +00001440 w = x;
1441 }
1442 else
Guido van Rossum79f25d91997-04-29 20:08:16 +00001443 Py_DECREF(x);
Guido van Rossum2d951851994-08-29 12:52:16 +00001444 }
Guido van Rossum3f5da241990-12-20 15:06:42 +00001445 }
Guido van Rossum2d951851994-08-29 12:52:16 +00001446 if (w == NULL)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001447 PyErr_SetString(PyExc_ValueError,
1448 "min() or max() of empty sequence");
Guido van Rossum3f5da241990-12-20 15:06:42 +00001449 return w;
1450}
1451
Guido van Rossum79f25d91997-04-29 20:08:16 +00001452static PyObject *
Guido van Rossum3f5da241990-12-20 15:06:42 +00001453builtin_min(self, v)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001454 PyObject *self;
1455 PyObject *v;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001456{
1457 return min_max(v, -1);
1458}
1459
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001460static char min_doc[] =
1461"min(sequence) -> value\n\
1462min(a, b, c, ...) -> value\n\
1463\n\
1464With a single sequence argument, return its smallest item.\n\
1465With two or more arguments, return the smallest argument.";
1466
1467
Guido van Rossum79f25d91997-04-29 20:08:16 +00001468static PyObject *
Guido van Rossum3f5da241990-12-20 15:06:42 +00001469builtin_max(self, v)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001470 PyObject *self;
1471 PyObject *v;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001472{
1473 return min_max(v, 1);
1474}
1475
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001476static char max_doc[] =
1477"max(sequence) -> value\n\
1478max(a, b, c, ...) -> value\n\
1479\n\
1480With a single sequence argument, return its largest item.\n\
1481With two or more arguments, return the largest argument.";
1482
1483
Guido van Rossum79f25d91997-04-29 20:08:16 +00001484static PyObject *
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001485builtin_oct(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001486 PyObject *self;
1487 PyObject *args;
Guido van Rossum006bcd41991-10-24 14:54:44 +00001488{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001489 PyObject *v;
1490 PyNumberMethods *nb;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001491
Guido van Rossum79f25d91997-04-29 20:08:16 +00001492 if (!PyArg_ParseTuple(args, "O:oct", &v))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001493 return NULL;
Guido van Rossum1899c2e1992-09-12 11:09:23 +00001494 if (v == NULL || (nb = v->ob_type->tp_as_number) == NULL ||
1495 nb->nb_oct == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001496 PyErr_SetString(PyExc_TypeError,
Guido van Rossum1899c2e1992-09-12 11:09:23 +00001497 "oct() argument can't be converted to oct");
1498 return NULL;
Guido van Rossum006bcd41991-10-24 14:54:44 +00001499 }
Guido van Rossum1899c2e1992-09-12 11:09:23 +00001500 return (*nb->nb_oct)(v);
Guido van Rossum006bcd41991-10-24 14:54:44 +00001501}
1502
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001503static char oct_doc[] =
1504"oct(number) -> string\n\
1505\n\
1506Return the octal representation of an integer or long integer.";
1507
1508
Guido van Rossum79f25d91997-04-29 20:08:16 +00001509static PyObject *
Guido van Rossum94390a41992-08-14 15:14:30 +00001510builtin_open(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001511 PyObject *self;
1512 PyObject *args;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001513{
Guido van Rossum2d951851994-08-29 12:52:16 +00001514 char *name;
1515 char *mode = "r";
1516 int bufsize = -1;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001517 PyObject *f;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001518
Guido van Rossum79f25d91997-04-29 20:08:16 +00001519 if (!PyArg_ParseTuple(args, "s|si:open", &name, &mode, &bufsize))
Guido van Rossum3f5da241990-12-20 15:06:42 +00001520 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001521 f = PyFile_FromString(name, mode);
Guido van Rossum2d951851994-08-29 12:52:16 +00001522 if (f != NULL)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001523 PyFile_SetBufSize(f, bufsize);
Guido van Rossum2d951851994-08-29 12:52:16 +00001524 return f;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001525}
1526
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001527static char open_doc[] =
1528"open(filename[, mode[, buffering]]) -> file object\n\
1529\n\
1530Open a file. The mode can be 'r', 'w' or 'a' for reading (default),\n\
1531writing or appending. The file will be created if it doesn't exist\n\
1532when opened for writing or appending; it will be truncated when\n\
1533opened for writing. Add a 'b' to the mode for binary files.\n\
1534Add a '+' to the mode to allow simultaneous reading and writing.\n\
1535If the buffering argument is given, 0 means unbuffered, 1 means line\n\
1536buffered, and larger numbers specify the buffer size.";
1537
1538
Guido van Rossum79f25d91997-04-29 20:08:16 +00001539static PyObject *
Guido van Rossum94390a41992-08-14 15:14:30 +00001540builtin_ord(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001541 PyObject *self;
1542 PyObject *args;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001543{
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001544 char c;
1545
Guido van Rossum79f25d91997-04-29 20:08:16 +00001546 if (!PyArg_ParseTuple(args, "c:ord", &c))
Guido van Rossum3f5da241990-12-20 15:06:42 +00001547 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001548 return PyInt_FromLong((long)(c & 0xff));
Guido van Rossum3f5da241990-12-20 15:06:42 +00001549}
1550
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001551static char ord_doc[] =
1552"ord(c) -> integer\n\
1553\n\
1554Return the integer ordinal of a one character string.";
1555
1556
Guido van Rossum79f25d91997-04-29 20:08:16 +00001557static PyObject *
Guido van Rossum6a00cd81995-01-07 12:39:01 +00001558builtin_pow(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001559 PyObject *self;
1560 PyObject *args;
Guido van Rossum6a00cd81995-01-07 12:39:01 +00001561{
Guido van Rossum09df08a1998-05-22 00:51:39 +00001562 PyObject *v, *w, *z = Py_None;
Guido van Rossum6a00cd81995-01-07 12:39:01 +00001563
Guido van Rossum79f25d91997-04-29 20:08:16 +00001564 if (!PyArg_ParseTuple(args, "OO|O:pow", &v, &w, &z))
Guido van Rossum6a00cd81995-01-07 12:39:01 +00001565 return NULL;
Guido van Rossum09df08a1998-05-22 00:51:39 +00001566 return PyNumber_Power(v, w, z);
Guido van Rossumd4905451991-05-05 20:00:36 +00001567}
1568
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001569static char pow_doc[] =
1570"pow(x, y[, z]) -> number\n\
1571\n\
1572With two arguments, equivalent to x**y. With three arguments,\n\
1573equivalent to (x**y) % z, but may be more efficient (e.g. for longs).";
1574
1575
Guido van Rossum124eff01999-02-23 16:11:01 +00001576/* Return number of items in range/xrange (lo, hi, step). step > 0
1577 * required. Return a value < 0 if & only if the true value is too
1578 * large to fit in a signed long.
1579 */
1580static long
1581get_len_of_range(lo, hi, step)
1582 long lo;
1583 long hi;
1584 long step; /* must be > 0 */
1585{
1586 /* -------------------------------------------------------------
1587 If lo >= hi, the range is empty.
1588 Else if n values are in the range, the last one is
1589 lo + (n-1)*step, which must be <= hi-1. Rearranging,
1590 n <= (hi - lo - 1)/step + 1, so taking the floor of the RHS gives
1591 the proper value. Since lo < hi in this case, hi-lo-1 >= 0, so
1592 the RHS is non-negative and so truncation is the same as the
1593 floor. Letting M be the largest positive long, the worst case
1594 for the RHS numerator is hi=M, lo=-M-1, and then
1595 hi-lo-1 = M-(-M-1)-1 = 2*M. Therefore unsigned long has enough
1596 precision to compute the RHS exactly.
1597 ---------------------------------------------------------------*/
1598 long n = 0;
1599 if (lo < hi) {
1600 unsigned long uhi = (unsigned long)hi;
1601 unsigned long ulo = (unsigned long)lo;
1602 unsigned long diff = uhi - ulo - 1;
1603 n = (long)(diff / (unsigned long)step + 1);
1604 }
1605 return n;
1606}
1607
Guido van Rossum79f25d91997-04-29 20:08:16 +00001608static PyObject *
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001609builtin_range(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001610 PyObject *self;
1611 PyObject *args;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001612{
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001613 long ilow = 0, ihigh = 0, istep = 1;
Guido van Rossum124eff01999-02-23 16:11:01 +00001614 long bign;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001615 int i, n;
Guido van Rossum124eff01999-02-23 16:11:01 +00001616
Guido van Rossum79f25d91997-04-29 20:08:16 +00001617 PyObject *v;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001618
Guido van Rossum79f25d91997-04-29 20:08:16 +00001619 if (PyTuple_Size(args) <= 1) {
1620 if (!PyArg_ParseTuple(args,
Guido van Rossum0865dd91995-01-17 16:30:22 +00001621 "l;range() requires 1-3 int arguments",
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001622 &ihigh))
1623 return NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001624 }
1625 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001626 if (!PyArg_ParseTuple(args,
Guido van Rossum0865dd91995-01-17 16:30:22 +00001627 "ll|l;range() requires 1-3 int arguments",
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001628 &ilow, &ihigh, &istep))
Guido van Rossum3f5da241990-12-20 15:06:42 +00001629 return NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001630 }
1631 if (istep == 0) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001632 PyErr_SetString(PyExc_ValueError, "zero step for range()");
Guido van Rossum3f5da241990-12-20 15:06:42 +00001633 return NULL;
1634 }
Guido van Rossum124eff01999-02-23 16:11:01 +00001635 if (istep > 0)
1636 bign = get_len_of_range(ilow, ihigh, istep);
1637 else
1638 bign = get_len_of_range(ihigh, ilow, -istep);
1639 n = (int)bign;
1640 if (bign < 0 || (long)n != bign) {
Guido van Rossume23cde21999-01-12 05:07:47 +00001641 PyErr_SetString(PyExc_OverflowError,
Guido van Rossum124eff01999-02-23 16:11:01 +00001642 "range() has too many items");
Guido van Rossume23cde21999-01-12 05:07:47 +00001643 return NULL;
1644 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00001645 v = PyList_New(n);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001646 if (v == NULL)
1647 return NULL;
1648 for (i = 0; i < n; i++) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001649 PyObject *w = PyInt_FromLong(ilow);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001650 if (w == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001651 Py_DECREF(v);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001652 return NULL;
1653 }
Guido van Rossuma937d141998-04-24 18:22:02 +00001654 PyList_SET_ITEM(v, i, w);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001655 ilow += istep;
1656 }
1657 return v;
1658}
1659
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001660static char range_doc[] =
1661"range([start,] stop[, step]) -> list of integers\n\
1662\n\
1663Return a list containing an arithmetic progression of integers.\n\
1664range(i, j) returns [i, i+1, i+2, ..., j-1]; start (!) defaults to 0.\n\
1665When step is given, it specifies the increment (or decrement).\n\
1666For example, range(4) returns [0, 1, 2, 3]. The end point is omitted!\n\
1667These are exactly the valid indices for a list of 4 elements.";
1668
1669
Guido van Rossum79f25d91997-04-29 20:08:16 +00001670static PyObject *
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001671builtin_xrange(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001672 PyObject *self;
1673 PyObject *args;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001674{
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001675 long ilow = 0, ihigh = 0, istep = 1;
Guido van Rossum0865dd91995-01-17 16:30:22 +00001676 long n;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001677
Guido van Rossum79f25d91997-04-29 20:08:16 +00001678 if (PyTuple_Size(args) <= 1) {
1679 if (!PyArg_ParseTuple(args,
Guido van Rossum0865dd91995-01-17 16:30:22 +00001680 "l;xrange() requires 1-3 int arguments",
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001681 &ihigh))
1682 return NULL;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001683 }
1684 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001685 if (!PyArg_ParseTuple(args,
Guido van Rossum0865dd91995-01-17 16:30:22 +00001686 "ll|l;xrange() requires 1-3 int arguments",
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001687 &ilow, &ihigh, &istep))
Guido van Rossum12d12c51993-10-26 17:58:25 +00001688 return NULL;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001689 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001690 if (istep == 0) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001691 PyErr_SetString(PyExc_ValueError, "zero step for xrange()");
Guido van Rossum12d12c51993-10-26 17:58:25 +00001692 return NULL;
1693 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001694 if (istep > 0)
Guido van Rossum124eff01999-02-23 16:11:01 +00001695 n = get_len_of_range(ilow, ihigh, istep);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001696 else
Guido van Rossum124eff01999-02-23 16:11:01 +00001697 n = get_len_of_range(ihigh, ilow, -istep);
1698 if (n < 0) {
1699 PyErr_SetString(PyExc_OverflowError,
1700 "xrange() has more than sys.maxint items");
1701 return NULL;
1702 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00001703 return PyRange_New(ilow, n, istep, 1);
Guido van Rossum12d12c51993-10-26 17:58:25 +00001704}
1705
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001706static char xrange_doc[] =
1707"xrange([start,] stop[, step]) -> xrange object\n\
1708\n\
1709Like range(), but instead of returning a list, returns an object that\n\
1710generates the numbers in the range on demand. This is slightly slower\n\
1711than range() but more memory efficient.";
1712
1713
Guido van Rossum79f25d91997-04-29 20:08:16 +00001714static PyObject *
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001715builtin_raw_input(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001716 PyObject *self;
1717 PyObject *args;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001718{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001719 PyObject *v = NULL;
1720 PyObject *f;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001721
Guido van Rossum79f25d91997-04-29 20:08:16 +00001722 if (!PyArg_ParseTuple(args, "|O:[raw_]input", &v))
Guido van Rossum3165fe61992-09-25 21:59:05 +00001723 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001724 if (PyFile_AsFile(PySys_GetObject("stdin")) == stdin &&
1725 PyFile_AsFile(PySys_GetObject("stdout")) == stdout &&
Guido van Rossum53bb7ff1995-07-26 16:26:31 +00001726 isatty(fileno(stdin)) && isatty(fileno(stdout))) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001727 PyObject *po;
Guido van Rossum872537c1995-07-07 22:43:42 +00001728 char *prompt;
1729 char *s;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001730 PyObject *result;
Guido van Rossum872537c1995-07-07 22:43:42 +00001731 if (v != NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001732 po = PyObject_Str(v);
Guido van Rossum872537c1995-07-07 22:43:42 +00001733 if (po == NULL)
1734 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001735 prompt = PyString_AsString(po);
Guido van Rossumd9b52081998-06-26 18:25:38 +00001736 if (prompt == NULL)
1737 return NULL;
Guido van Rossum872537c1995-07-07 22:43:42 +00001738 }
1739 else {
1740 po = NULL;
1741 prompt = "";
1742 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00001743 s = PyOS_Readline(prompt);
1744 Py_XDECREF(po);
Guido van Rossum872537c1995-07-07 22:43:42 +00001745 if (s == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001746 PyErr_SetNone(PyExc_KeyboardInterrupt);
Guido van Rossum872537c1995-07-07 22:43:42 +00001747 return NULL;
1748 }
1749 if (*s == '\0') {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001750 PyErr_SetNone(PyExc_EOFError);
Guido van Rossum872537c1995-07-07 22:43:42 +00001751 result = NULL;
1752 }
1753 else { /* strip trailing '\n' */
Guido van Rossum79f25d91997-04-29 20:08:16 +00001754 result = PyString_FromStringAndSize(s, strlen(s)-1);
Guido van Rossum872537c1995-07-07 22:43:42 +00001755 }
1756 free(s);
1757 return result;
1758 }
Guido van Rossum90933611991-06-07 16:10:43 +00001759 if (v != NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001760 f = PySys_GetObject("stdout");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001761 if (f == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001762 PyErr_SetString(PyExc_RuntimeError, "lost sys.stdout");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001763 return NULL;
1764 }
Guido van Rossumc8b6df91997-05-23 00:06:51 +00001765 if (Py_FlushLine() != 0 ||
1766 PyFile_WriteObject(v, f, Py_PRINT_RAW) != 0)
Guido van Rossum90933611991-06-07 16:10:43 +00001767 return NULL;
1768 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00001769 f = PySys_GetObject("stdin");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001770 if (f == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001771 PyErr_SetString(PyExc_RuntimeError, "lost sys.stdin");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001772 return NULL;
1773 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00001774 return PyFile_GetLine(f, -1);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001775}
1776
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001777static char raw_input_doc[] =
1778"raw_input([prompt]) -> string\n\
1779\n\
1780Read a string from standard input. The trailing newline is stripped.\n\
1781If the user hits EOF (Unix: Ctl-D, Windows: Ctl-Z+Return), raise EOFError.\n\
1782On Unix, GNU readline is used if enabled. The prompt string, if given,\n\
1783is printed without a trailing newline before reading.";
1784
1785
Guido van Rossum79f25d91997-04-29 20:08:16 +00001786static PyObject *
Guido van Rossum12d12c51993-10-26 17:58:25 +00001787builtin_reduce(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001788 PyObject *self;
1789 PyObject *args;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001790{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001791 PyObject *seq, *func, *result = NULL;
1792 PySequenceMethods *sqf;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001793 register int i;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001794
Guido van Rossum79f25d91997-04-29 20:08:16 +00001795 if (!PyArg_ParseTuple(args, "OO|O:reduce", &func, &seq, &result))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001796 return NULL;
1797 if (result != NULL)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001798 Py_INCREF(result);
Guido van Rossum12d12c51993-10-26 17:58:25 +00001799
Guido van Rossum09df08a1998-05-22 00:51:39 +00001800 sqf = seq->ob_type->tp_as_sequence;
1801 if (sqf == NULL || sqf->sq_item == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001802 PyErr_SetString(PyExc_TypeError,
Guido van Rossum12d12c51993-10-26 17:58:25 +00001803 "2nd argument to reduce() must be a sequence object");
1804 return NULL;
1805 }
1806
Guido van Rossum79f25d91997-04-29 20:08:16 +00001807 if ((args = PyTuple_New(2)) == NULL)
Guido van Rossum2d951851994-08-29 12:52:16 +00001808 goto Fail;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001809
Guido van Rossum2d951851994-08-29 12:52:16 +00001810 for (i = 0; ; ++i) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001811 PyObject *op2;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001812
1813 if (args->ob_refcnt > 1) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001814 Py_DECREF(args);
1815 if ((args = PyTuple_New(2)) == NULL)
Guido van Rossum2d951851994-08-29 12:52:16 +00001816 goto Fail;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001817 }
1818
Guido van Rossum2d951851994-08-29 12:52:16 +00001819 if ((op2 = (*sqf->sq_item)(seq, i)) == NULL) {
Barry Warsawcde8b1b1997-08-22 21:14:38 +00001820 if (PyErr_ExceptionMatches(PyExc_IndexError)) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001821 PyErr_Clear();
Guido van Rossum2d951851994-08-29 12:52:16 +00001822 break;
1823 }
1824 goto Fail;
1825 }
Guido van Rossum12d12c51993-10-26 17:58:25 +00001826
Guido van Rossum2d951851994-08-29 12:52:16 +00001827 if (result == NULL)
1828 result = op2;
1829 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001830 PyTuple_SetItem(args, 0, result);
1831 PyTuple_SetItem(args, 1, op2);
1832 if ((result = PyEval_CallObject(func, args)) == NULL)
Guido van Rossum2d951851994-08-29 12:52:16 +00001833 goto Fail;
1834 }
Guido van Rossum12d12c51993-10-26 17:58:25 +00001835 }
1836
Guido van Rossum79f25d91997-04-29 20:08:16 +00001837 Py_DECREF(args);
Guido van Rossum12d12c51993-10-26 17:58:25 +00001838
Guido van Rossum2d951851994-08-29 12:52:16 +00001839 if (result == NULL)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001840 PyErr_SetString(PyExc_TypeError,
Guido van Rossum2d951851994-08-29 12:52:16 +00001841 "reduce of empty sequence with no initial value");
1842
Guido van Rossum12d12c51993-10-26 17:58:25 +00001843 return result;
1844
Guido van Rossum2d951851994-08-29 12:52:16 +00001845Fail:
Guido van Rossum79f25d91997-04-29 20:08:16 +00001846 Py_XDECREF(args);
1847 Py_XDECREF(result);
Guido van Rossum12d12c51993-10-26 17:58:25 +00001848 return NULL;
1849}
1850
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001851static char reduce_doc[] =
1852"reduce(function, sequence[, initial]) -> value\n\
1853\n\
1854Apply a function of two arguments cumulatively to the items of a sequence,\n\
1855from left to right, so as to reduce the sequence to a single value.\n\
1856For example, reduce(lambda x, y: x+y, [1, 2, 3, 4, 5]) calculates\n\
1857((((1+2)+3)+4)+5). If initial is present, it is placed before the items\n\
1858of the sequence in the calculation, and serves as a default when the\n\
1859sequence is empty.";
1860
1861
Guido van Rossum79f25d91997-04-29 20:08:16 +00001862static PyObject *
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001863builtin_reload(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001864 PyObject *self;
1865 PyObject *args;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001866{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001867 PyObject *v;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001868
Guido van Rossum79f25d91997-04-29 20:08:16 +00001869 if (!PyArg_ParseTuple(args, "O:reload", &v))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001870 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001871 return PyImport_ReloadModule(v);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001872}
1873
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001874static char reload_doc[] =
1875"reload(module) -> module\n\
1876\n\
1877Reload the module. The module must have been successfully imported before.";
1878
1879
Guido van Rossum79f25d91997-04-29 20:08:16 +00001880static PyObject *
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001881builtin_repr(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001882 PyObject *self;
1883 PyObject *args;
Guido van Rossumc89705d1992-11-26 08:54:07 +00001884{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001885 PyObject *v;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001886
Guido van Rossum79f25d91997-04-29 20:08:16 +00001887 if (!PyArg_ParseTuple(args, "O:repr", &v))
Guido van Rossumc89705d1992-11-26 08:54:07 +00001888 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001889 return PyObject_Repr(v);
Guido van Rossumc89705d1992-11-26 08:54:07 +00001890}
1891
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001892static char repr_doc[] =
1893"repr(object) -> string\n\
1894\n\
1895Return the canonical string representation of the object.\n\
1896For most object types, eval(repr(object)) == object.";
1897
1898
Guido van Rossum79f25d91997-04-29 20:08:16 +00001899static PyObject *
Guido van Rossum9e51f9b1993-02-12 16:29:05 +00001900builtin_round(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001901 PyObject *self;
1902 PyObject *args;
Guido van Rossum9e51f9b1993-02-12 16:29:05 +00001903{
Guido van Rossum9e51f9b1993-02-12 16:29:05 +00001904 double x;
1905 double f;
1906 int ndigits = 0;
Guido van Rossum9e51f9b1993-02-12 16:29:05 +00001907 int i;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001908
Guido van Rossum79f25d91997-04-29 20:08:16 +00001909 if (!PyArg_ParseTuple(args, "d|i:round", &x, &ndigits))
Guido van Rossum9e51f9b1993-02-12 16:29:05 +00001910 return NULL;
Guido van Rossum9e51f9b1993-02-12 16:29:05 +00001911 f = 1.0;
Guido van Rossum1e162d31998-05-09 14:42:25 +00001912 i = abs(ndigits);
1913 while (--i >= 0)
Guido van Rossum9e51f9b1993-02-12 16:29:05 +00001914 f = f*10.0;
Guido van Rossum1e162d31998-05-09 14:42:25 +00001915 if (ndigits < 0)
1916 x /= f;
Guido van Rossum9e51f9b1993-02-12 16:29:05 +00001917 else
Guido van Rossum1e162d31998-05-09 14:42:25 +00001918 x *= f;
1919 if (x >= 0.0)
1920 x = floor(x + 0.5);
1921 else
1922 x = ceil(x - 0.5);
1923 if (ndigits < 0)
1924 x *= f;
1925 else
1926 x /= f;
1927 return PyFloat_FromDouble(x);
Guido van Rossum9e51f9b1993-02-12 16:29:05 +00001928}
1929
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001930static char round_doc[] =
1931"round(number[, ndigits]) -> floating point number\n\
1932\n\
1933Round a number to a given precision in decimal digits (default 0 digits).\n\
1934This always returns a floating point number. Precision may be negative.";
1935
1936
Guido van Rossum79f25d91997-04-29 20:08:16 +00001937static PyObject *
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001938builtin_str(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001939 PyObject *self;
1940 PyObject *args;
Guido van Rossumc89705d1992-11-26 08:54:07 +00001941{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001942 PyObject *v;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001943
Guido van Rossum79f25d91997-04-29 20:08:16 +00001944 if (!PyArg_ParseTuple(args, "O:str", &v))
Guido van Rossumc89705d1992-11-26 08:54:07 +00001945 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001946 return PyObject_Str(v);
Guido van Rossumc89705d1992-11-26 08:54:07 +00001947}
1948
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001949static char str_doc[] =
1950"str(object) -> string\n\
1951\n\
1952Return a nice string representation of the object.\n\
1953If the argument is a string, the return value is the same object.";
1954
1955
Guido van Rossum79f25d91997-04-29 20:08:16 +00001956static PyObject *
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001957builtin_tuple(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001958 PyObject *self;
1959 PyObject *args;
Guido van Rossumcae027b1994-08-29 12:53:11 +00001960{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001961 PyObject *v;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001962
Guido van Rossum79f25d91997-04-29 20:08:16 +00001963 if (!PyArg_ParseTuple(args, "O:tuple", &v))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001964 return NULL;
Guido van Rossum09df08a1998-05-22 00:51:39 +00001965 return PySequence_Tuple(v);
Guido van Rossumcae027b1994-08-29 12:53:11 +00001966}
1967
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001968static char tuple_doc[] =
1969"tuple(sequence) -> list\n\
1970\n\
1971Return a tuple whose items are the same as those of the argument sequence.\n\
1972If the argument is a tuple, the return value is the same object.";
1973
1974
Guido van Rossum79f25d91997-04-29 20:08:16 +00001975static PyObject *
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001976builtin_type(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001977 PyObject *self;
1978 PyObject *args;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001979{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001980 PyObject *v;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001981
Guido van Rossum79f25d91997-04-29 20:08:16 +00001982 if (!PyArg_ParseTuple(args, "O:type", &v))
Guido van Rossum3f5da241990-12-20 15:06:42 +00001983 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001984 v = (PyObject *)v->ob_type;
1985 Py_INCREF(v);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001986 return v;
1987}
1988
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001989static char type_doc[] =
1990"type(object) -> type object\n\
1991\n\
1992Return the type of the object.";
1993
1994
Guido van Rossum79f25d91997-04-29 20:08:16 +00001995static PyObject *
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001996builtin_vars(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001997 PyObject *self;
1998 PyObject *args;
Guido van Rossum2d951851994-08-29 12:52:16 +00001999{
Guido van Rossum79f25d91997-04-29 20:08:16 +00002000 PyObject *v = NULL;
2001 PyObject *d;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002002
Guido van Rossum79f25d91997-04-29 20:08:16 +00002003 if (!PyArg_ParseTuple(args, "|O:vars", &v))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002004 return NULL;
Guido van Rossum2d951851994-08-29 12:52:16 +00002005 if (v == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002006 d = PyEval_GetLocals();
Guido van Rossum53bb7ff1995-07-26 16:26:31 +00002007 if (d == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002008 if (!PyErr_Occurred())
2009 PyErr_SetString(PyExc_SystemError,
2010 "no locals!?");
Guido van Rossum53bb7ff1995-07-26 16:26:31 +00002011 }
2012 else
Guido van Rossum79f25d91997-04-29 20:08:16 +00002013 Py_INCREF(d);
Guido van Rossum2d951851994-08-29 12:52:16 +00002014 }
2015 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002016 d = PyObject_GetAttrString(v, "__dict__");
Guido van Rossum2d951851994-08-29 12:52:16 +00002017 if (d == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002018 PyErr_SetString(PyExc_TypeError,
Guido van Rossum2d951851994-08-29 12:52:16 +00002019 "vars() argument must have __dict__ attribute");
2020 return NULL;
2021 }
2022 }
2023 return d;
2024}
2025
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002026static char vars_doc[] =
2027"vars([object]) -> dictionary\n\
2028\n\
2029Without arguments, equivalent to locals().\n\
2030With an argument, equivalent to object.__dict__.";
2031
Guido van Rossum668213d1999-06-16 17:28:37 +00002032static int
2033abstract_issubclass(derived, cls, err, first)
2034 PyObject *derived;
2035 PyObject *cls;
2036 char *err;
2037 int first;
2038{
2039 static PyObject *__bases__ = NULL;
2040 PyObject *bases;
Guido van Rossum7f851861999-06-17 19:12:39 +00002041 int i, n;
Guido van Rossum668213d1999-06-16 17:28:37 +00002042 int r = 0;
2043
2044 if (__bases__ == NULL) {
2045 __bases__ = PyString_FromString("__bases__");
2046 if (__bases__ == NULL)
2047 return -1;
2048 }
2049
2050 if (first) {
2051 bases = PyObject_GetAttr(cls, __bases__);
2052 if (bases == NULL || !PyTuple_Check(bases)) {
2053 Py_XDECREF(bases);
2054 PyErr_SetString(PyExc_TypeError, err);
2055 return -1;
2056 }
2057 Py_DECREF(bases);
2058 }
2059
2060 if (derived == cls)
2061 return 1;
2062
2063 bases = PyObject_GetAttr(derived, __bases__);
2064 if (bases == NULL || !PyTuple_Check(bases)) {
2065 Py_XDECREF(bases);
2066 PyErr_SetString(PyExc_TypeError, err);
2067 return -1;
2068 }
2069
2070 n = PyTuple_GET_SIZE(bases);
2071 for (i = 0; i < n; i++) {
2072 r = abstract_issubclass(PyTuple_GET_ITEM(bases, i),
2073 cls, err, 0);
2074 if (r != 0)
2075 break;
2076 }
2077
2078 Py_DECREF(bases);
2079
2080 return r;
2081}
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002082
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002083static PyObject *
2084builtin_isinstance(self, args)
2085 PyObject *self;
2086 PyObject *args;
2087{
2088 PyObject *inst;
2089 PyObject *cls;
Guido van Rossum668213d1999-06-16 17:28:37 +00002090 PyObject *icls;
2091 static PyObject *__class__ = NULL;
2092 int retval = 0;
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002093
2094 if (!PyArg_ParseTuple(args, "OO", &inst, &cls))
2095 return NULL;
Guido van Rossumf5dd9141997-12-02 19:11:45 +00002096
Guido van Rossum668213d1999-06-16 17:28:37 +00002097 if (PyClass_Check(cls)) {
2098 if (PyInstance_Check(inst)) {
Guido van Rossumf5dd9141997-12-02 19:11:45 +00002099 PyObject *inclass =
2100 (PyObject*)((PyInstanceObject*)inst)->in_class;
2101 retval = PyClass_IsSubclass(inclass, cls);
2102 }
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002103 }
Guido van Rossum668213d1999-06-16 17:28:37 +00002104 else if (PyType_Check(cls)) {
2105 retval = ((PyObject *)(inst->ob_type) == cls);
2106 }
2107 else if (!PyInstance_Check(inst)) {
2108 if (__class__ == NULL) {
2109 __class__ = PyString_FromString("__class__");
2110 if (__class__ == NULL)
2111 return NULL;
2112 }
2113 icls = PyObject_GetAttr(inst, __class__);
2114 if (icls != NULL) {
2115 retval = abstract_issubclass(
2116 icls, cls,
2117 "second argument must be a class",
2118 1);
2119 Py_DECREF(icls);
2120 if (retval < 0)
2121 return NULL;
2122 }
2123 else {
2124 PyErr_SetString(PyExc_TypeError,
2125 "second argument must be a class");
2126 return NULL;
2127 }
2128 }
2129 else {
2130 PyErr_SetString(PyExc_TypeError,
2131 "second argument must be a class");
2132 return NULL;
2133 }
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002134 return PyInt_FromLong(retval);
2135}
2136
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002137static char isinstance_doc[] =
2138"isinstance(object, class-or-type) -> Boolean\n\
2139\n\
2140Return whether an object is an instance of a class or of a subclass thereof.\n\
2141With a type as second argument, return whether that is the object's type.";
2142
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002143
2144static PyObject *
2145builtin_issubclass(self, args)
2146 PyObject *self;
2147 PyObject *args;
2148{
2149 PyObject *derived;
2150 PyObject *cls;
2151 int retval;
2152
2153 if (!PyArg_ParseTuple(args, "OO", &derived, &cls))
2154 return NULL;
Guido van Rossum668213d1999-06-16 17:28:37 +00002155
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002156 if (!PyClass_Check(derived) || !PyClass_Check(cls)) {
Guido van Rossum668213d1999-06-16 17:28:37 +00002157 retval = abstract_issubclass(
2158 derived, cls, "arguments must be classes", 1);
2159 if (retval < 0)
2160 return NULL;
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002161 }
Guido van Rossum668213d1999-06-16 17:28:37 +00002162 else {
2163 /* shortcut */
2164 if (!(retval = (derived == cls)))
2165 retval = PyClass_IsSubclass(derived, cls);
2166 }
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002167
2168 return PyInt_FromLong(retval);
2169}
2170
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002171static char issubclass_doc[] =
2172"issubclass(C, B) -> Boolean\n\
2173\n\
2174Return whether class C is a subclass (i.e., a derived class) of class B.";
2175
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002176
Guido van Rossum79f25d91997-04-29 20:08:16 +00002177static PyMethodDef builtin_methods[] = {
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002178 {"__import__", builtin___import__, 1, import_doc},
2179 {"abs", builtin_abs, 1, abs_doc},
2180 {"apply", builtin_apply, 1, apply_doc},
Guido van Rossum0daf0221999-03-19 19:07:19 +00002181 {"buffer", builtin_buffer, 1, buffer_doc},
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002182 {"callable", builtin_callable, 1, callable_doc},
2183 {"chr", builtin_chr, 1, chr_doc},
2184 {"cmp", builtin_cmp, 1, cmp_doc},
2185 {"coerce", builtin_coerce, 1, coerce_doc},
2186 {"compile", builtin_compile, 1, compile_doc},
Guido van Rossum8a5c5d21996-01-12 01:09:56 +00002187#ifndef WITHOUT_COMPLEX
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002188 {"complex", builtin_complex, 1, complex_doc},
Guido van Rossum8a5c5d21996-01-12 01:09:56 +00002189#endif
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002190 {"delattr", builtin_delattr, 1, delattr_doc},
2191 {"dir", builtin_dir, 1, dir_doc},
2192 {"divmod", builtin_divmod, 1, divmod_doc},
2193 {"eval", builtin_eval, 1, eval_doc},
2194 {"execfile", builtin_execfile, 1, execfile_doc},
2195 {"filter", builtin_filter, 1, filter_doc},
2196 {"float", builtin_float, 1, float_doc},
2197 {"getattr", builtin_getattr, 1, getattr_doc},
2198 {"globals", builtin_globals, 1, globals_doc},
2199 {"hasattr", builtin_hasattr, 1, hasattr_doc},
2200 {"hash", builtin_hash, 1, hash_doc},
2201 {"hex", builtin_hex, 1, hex_doc},
2202 {"id", builtin_id, 1, id_doc},
2203 {"input", builtin_input, 1, input_doc},
2204 {"intern", builtin_intern, 1, intern_doc},
2205 {"int", builtin_int, 1, int_doc},
2206 {"isinstance", builtin_isinstance, 1, isinstance_doc},
2207 {"issubclass", builtin_issubclass, 1, issubclass_doc},
2208 {"len", builtin_len, 1, len_doc},
2209 {"list", builtin_list, 1, list_doc},
2210 {"locals", builtin_locals, 1, locals_doc},
2211 {"long", builtin_long, 1, long_doc},
2212 {"map", builtin_map, 1, map_doc},
2213 {"max", builtin_max, 1, max_doc},
2214 {"min", builtin_min, 1, min_doc},
2215 {"oct", builtin_oct, 1, oct_doc},
2216 {"open", builtin_open, 1, open_doc},
2217 {"ord", builtin_ord, 1, ord_doc},
2218 {"pow", builtin_pow, 1, pow_doc},
2219 {"range", builtin_range, 1, range_doc},
2220 {"raw_input", builtin_raw_input, 1, raw_input_doc},
2221 {"reduce", builtin_reduce, 1, reduce_doc},
2222 {"reload", builtin_reload, 1, reload_doc},
2223 {"repr", builtin_repr, 1, repr_doc},
2224 {"round", builtin_round, 1, round_doc},
2225 {"setattr", builtin_setattr, 1, setattr_doc},
2226 {"slice", builtin_slice, 1, slice_doc},
2227 {"str", builtin_str, 1, str_doc},
2228 {"tuple", builtin_tuple, 1, tuple_doc},
2229 {"type", builtin_type, 1, type_doc},
2230 {"vars", builtin_vars, 1, vars_doc},
2231 {"xrange", builtin_xrange, 1, xrange_doc},
Guido van Rossumc02e15c1991-12-16 13:03:00 +00002232 {NULL, NULL},
Guido van Rossum3f5da241990-12-20 15:06:42 +00002233};
2234
Guido van Rossum3f5da241990-12-20 15:06:42 +00002235/* Predefined exceptions */
2236
Guido van Rossum04748321997-09-16 18:43:15 +00002237PyObject *PyExc_Exception;
Barry Warsaw757af0e1997-08-29 22:13:51 +00002238PyObject *PyExc_StandardError;
Barry Warsaw412cdc21997-09-16 21:51:14 +00002239PyObject *PyExc_ArithmeticError;
Barry Warsaw757af0e1997-08-29 22:13:51 +00002240PyObject *PyExc_LookupError;
2241
Guido van Rossum79f25d91997-04-29 20:08:16 +00002242PyObject *PyExc_AssertionError;
2243PyObject *PyExc_AttributeError;
2244PyObject *PyExc_EOFError;
Guido van Rossumb6a7f771997-05-09 03:03:23 +00002245PyObject *PyExc_FloatingPointError;
Barry Warsawd086a1a1998-07-23 15:59:57 +00002246PyObject *PyExc_EnvironmentError;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002247PyObject *PyExc_IOError;
Barry Warsawd086a1a1998-07-23 15:59:57 +00002248PyObject *PyExc_OSError;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002249PyObject *PyExc_ImportError;
2250PyObject *PyExc_IndexError;
2251PyObject *PyExc_KeyError;
2252PyObject *PyExc_KeyboardInterrupt;
2253PyObject *PyExc_MemoryError;
2254PyObject *PyExc_NameError;
2255PyObject *PyExc_OverflowError;
2256PyObject *PyExc_RuntimeError;
Barry Warsaw344864f1998-12-01 18:52:06 +00002257PyObject *PyExc_NotImplementedError;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002258PyObject *PyExc_SyntaxError;
2259PyObject *PyExc_SystemError;
2260PyObject *PyExc_SystemExit;
Guido van Rossum87460821999-06-22 14:47:32 +00002261PyObject *PyExc_UnboundLocalError;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002262PyObject *PyExc_TypeError;
2263PyObject *PyExc_ValueError;
2264PyObject *PyExc_ZeroDivisionError;
Guido van Rossum50afb7a1991-12-10 13:52:31 +00002265
Barry Warsaw757af0e1997-08-29 22:13:51 +00002266PyObject *PyExc_MemoryErrorInst;
2267
Guido van Rossum11950231999-03-25 21:16:07 +00002268static struct
Barry Warsaw757af0e1997-08-29 22:13:51 +00002269{
2270 char* name;
2271 PyObject** exc;
2272 int leaf_exc;
2273}
2274bltin_exc[] = {
Guido van Rossum04748321997-09-16 18:43:15 +00002275 {"Exception", &PyExc_Exception, 0},
Barry Warsaw757af0e1997-08-29 22:13:51 +00002276 {"StandardError", &PyExc_StandardError, 0},
Barry Warsaw412cdc21997-09-16 21:51:14 +00002277 {"ArithmeticError", &PyExc_ArithmeticError, 0},
Barry Warsaw757af0e1997-08-29 22:13:51 +00002278 {"LookupError", &PyExc_LookupError, 0},
2279 {"AssertionError", &PyExc_AssertionError, 1},
2280 {"AttributeError", &PyExc_AttributeError, 1},
2281 {"EOFError", &PyExc_EOFError, 1},
2282 {"FloatingPointError", &PyExc_FloatingPointError, 1},
Barry Warsaw78902031999-01-29 20:29:49 +00002283 {"EnvironmentError", &PyExc_EnvironmentError, 0},
Barry Warsaw757af0e1997-08-29 22:13:51 +00002284 {"IOError", &PyExc_IOError, 1},
Barry Warsawd086a1a1998-07-23 15:59:57 +00002285 {"OSError", &PyExc_OSError, 1},
Barry Warsaw757af0e1997-08-29 22:13:51 +00002286 {"ImportError", &PyExc_ImportError, 1},
2287 {"IndexError", &PyExc_IndexError, 1},
2288 {"KeyError", &PyExc_KeyError, 1},
2289 {"KeyboardInterrupt", &PyExc_KeyboardInterrupt, 1},
2290 {"MemoryError", &PyExc_MemoryError, 1},
Guido van Rossum87460821999-06-22 14:47:32 +00002291 /* Note: NameError is not a leaf in exceptions.py, but unlike
2292 the other non-leafs NameError is meant to be raised directly
2293 at times -- the leaf_exc member really seems to mean something
2294 like "this is an abstract base class" when false.
2295 */
Barry Warsaw757af0e1997-08-29 22:13:51 +00002296 {"NameError", &PyExc_NameError, 1},
2297 {"OverflowError", &PyExc_OverflowError, 1},
2298 {"RuntimeError", &PyExc_RuntimeError, 1},
Barry Warsaw344864f1998-12-01 18:52:06 +00002299 {"NotImplementedError",&PyExc_NotImplementedError,1},
Barry Warsaw757af0e1997-08-29 22:13:51 +00002300 {"SyntaxError", &PyExc_SyntaxError, 1},
2301 {"SystemError", &PyExc_SystemError, 1},
2302 {"SystemExit", &PyExc_SystemExit, 1},
Guido van Rossum87460821999-06-22 14:47:32 +00002303 {"UnboundLocalError", &PyExc_UnboundLocalError, 1},
Barry Warsaw757af0e1997-08-29 22:13:51 +00002304 {"TypeError", &PyExc_TypeError, 1},
2305 {"ValueError", &PyExc_ValueError, 1},
2306 {"ZeroDivisionError", &PyExc_ZeroDivisionError, 1},
2307 {NULL, NULL}
2308};
2309
2310
Barry Warsaw98b62461998-09-14 18:51:11 +00002311/* import exceptions module to extract class exceptions. on success,
2312 * return 1. on failure return 0 which signals _PyBuiltin_Init_2 to fall
2313 * back to using old-style string based exceptions.
2314 */
2315static int
Barry Warsaw757af0e1997-08-29 22:13:51 +00002316init_class_exc(dict)
2317 PyObject *dict;
2318{
2319 int i;
2320 PyObject *m = PyImport_ImportModule("exceptions");
Barry Warsaw98b62461998-09-14 18:51:11 +00002321 PyObject *args = NULL;
2322 PyObject *d = NULL;
Barry Warsaw757af0e1997-08-29 22:13:51 +00002323
Barry Warsaw98b62461998-09-14 18:51:11 +00002324 /* make sure we got the module and its dictionary */
Barry Warsaw757af0e1997-08-29 22:13:51 +00002325 if (m == NULL ||
2326 (d = PyModule_GetDict(m)) == NULL)
2327 {
Barry Warsaw98b62461998-09-14 18:51:11 +00002328 PySys_WriteStderr("'import exceptions' failed; ");
Barry Warsaw757af0e1997-08-29 22:13:51 +00002329 if (Py_VerboseFlag) {
Barry Warsaw98b62461998-09-14 18:51:11 +00002330 PySys_WriteStderr("traceback:\n");
Barry Warsaw757af0e1997-08-29 22:13:51 +00002331 PyErr_Print();
2332 }
2333 else {
Barry Warsaw98b62461998-09-14 18:51:11 +00002334 PySys_WriteStderr("use -v for traceback\n");
Barry Warsaw757af0e1997-08-29 22:13:51 +00002335 }
Barry Warsaw98b62461998-09-14 18:51:11 +00002336 goto finally;
Barry Warsaw757af0e1997-08-29 22:13:51 +00002337 }
2338 for (i = 0; bltin_exc[i].name; i++) {
2339 /* dig the exception out of the module */
2340 PyObject *exc = PyDict_GetItemString(d, bltin_exc[i].name);
Barry Warsaw98b62461998-09-14 18:51:11 +00002341 if (!exc) {
2342 PySys_WriteStderr(
2343 "Built-in exception class not found: %s. Library mismatch?\n",
2344 bltin_exc[i].name);
2345 goto finally;
2346 }
2347 /* free the old-style exception string object */
Barry Warsaw757af0e1997-08-29 22:13:51 +00002348 Py_XDECREF(*bltin_exc[i].exc);
2349
2350 /* squirrel away a pointer to the exception */
2351 Py_INCREF(exc);
2352 *bltin_exc[i].exc = exc;
2353
2354 /* and insert the name in the __builtin__ module */
Barry Warsaw98b62461998-09-14 18:51:11 +00002355 if (PyDict_SetItemString(dict, bltin_exc[i].name, exc)) {
2356 PySys_WriteStderr(
2357 "Cannot insert exception into __builtin__: %s\n",
2358 bltin_exc[i].name);
2359 goto finally;
2360 }
Barry Warsaw757af0e1997-08-29 22:13:51 +00002361 }
2362
2363 /* we need one pre-allocated instance */
2364 args = Py_BuildValue("()");
Barry Warsaw98b62461998-09-14 18:51:11 +00002365 if (!args ||
2366 !(PyExc_MemoryErrorInst =
2367 PyEval_CallObject(PyExc_MemoryError, args)))
2368 {
2369 PySys_WriteStderr("Cannot pre-allocate MemoryError instance\n");
2370 goto finally;
Barry Warsaw757af0e1997-08-29 22:13:51 +00002371 }
Barry Warsaw98b62461998-09-14 18:51:11 +00002372 Py_DECREF(args);
Barry Warsaw757af0e1997-08-29 22:13:51 +00002373
2374 /* we're done with the exceptions module */
2375 Py_DECREF(m);
2376
Barry Warsaw98b62461998-09-14 18:51:11 +00002377 if (PyErr_Occurred()) {
2378 PySys_WriteStderr("Cannot initialize standard class exceptions; ");
2379 if (Py_VerboseFlag) {
2380 PySys_WriteStderr("traceback:\n");
2381 PyErr_Print();
2382 }
2383 else
2384 PySys_WriteStderr("use -v for traceback\n");
2385 goto finally;
2386 }
2387 return 1;
2388 finally:
2389 Py_XDECREF(m);
2390 Py_XDECREF(args);
2391 PyErr_Clear();
2392 return 0;
Barry Warsaw757af0e1997-08-29 22:13:51 +00002393}
2394
2395
2396static void
2397fini_instances()
2398{
2399 Py_XDECREF(PyExc_MemoryErrorInst);
2400 PyExc_MemoryErrorInst = NULL;
2401}
2402
2403
Guido van Rossum79f25d91997-04-29 20:08:16 +00002404static PyObject *
Guido van Rossum25ce5661997-08-02 03:10:38 +00002405newstdexception(dict, name)
2406 PyObject *dict;
Guido van Rossumfb905c31991-12-16 15:42:38 +00002407 char *name;
Guido van Rossum3f5da241990-12-20 15:06:42 +00002408{
Guido van Rossum79f25d91997-04-29 20:08:16 +00002409 PyObject *v = PyString_FromString(name);
Guido van Rossum25ce5661997-08-02 03:10:38 +00002410 if (v == NULL || PyDict_SetItemString(dict, name, v) != 0)
Barry Warsaw98b62461998-09-14 18:51:11 +00002411 Py_FatalError("Cannot create string-based exceptions");
Guido van Rossum3f5da241990-12-20 15:06:42 +00002412 return v;
2413}
2414
2415static void
Guido van Rossum25ce5661997-08-02 03:10:38 +00002416initerrors(dict)
2417 PyObject *dict;
Guido van Rossum3f5da241990-12-20 15:06:42 +00002418{
Barry Warsaw72b715d1999-02-24 00:35:43 +00002419 int i, j;
Barry Warsaw757af0e1997-08-29 22:13:51 +00002420 int exccnt = 0;
2421 for (i = 0; bltin_exc[i].name; i++, exccnt++) {
Barry Warsaw98b62461998-09-14 18:51:11 +00002422 Py_XDECREF(*bltin_exc[i].exc);
Barry Warsaw757af0e1997-08-29 22:13:51 +00002423 if (bltin_exc[i].leaf_exc)
2424 *bltin_exc[i].exc =
2425 newstdexception(dict, bltin_exc[i].name);
2426 }
2427
Barry Warsawd086a1a1998-07-23 15:59:57 +00002428 /* This is kind of bogus because we special case the some of the
2429 * new exceptions to be nearly forward compatible. But this means
2430 * we hard code knowledge about exceptions.py into C here. I don't
2431 * have a better solution, though.
2432 */
Barry Warsaw757af0e1997-08-29 22:13:51 +00002433 PyExc_LookupError = PyTuple_New(2);
2434 Py_INCREF(PyExc_IndexError);
2435 PyTuple_SET_ITEM(PyExc_LookupError, 0, PyExc_IndexError);
2436 Py_INCREF(PyExc_KeyError);
2437 PyTuple_SET_ITEM(PyExc_LookupError, 1, PyExc_KeyError);
2438 PyDict_SetItemString(dict, "LookupError", PyExc_LookupError);
2439
Barry Warsaw412cdc21997-09-16 21:51:14 +00002440 PyExc_ArithmeticError = PyTuple_New(3);
Barry Warsaw757af0e1997-08-29 22:13:51 +00002441 Py_INCREF(PyExc_OverflowError);
Barry Warsaw412cdc21997-09-16 21:51:14 +00002442 PyTuple_SET_ITEM(PyExc_ArithmeticError, 0, PyExc_OverflowError);
Barry Warsaw757af0e1997-08-29 22:13:51 +00002443 Py_INCREF(PyExc_ZeroDivisionError);
Barry Warsaw412cdc21997-09-16 21:51:14 +00002444 PyTuple_SET_ITEM(PyExc_ArithmeticError, 1, PyExc_ZeroDivisionError);
Barry Warsaw757af0e1997-08-29 22:13:51 +00002445 Py_INCREF(PyExc_FloatingPointError);
Barry Warsaw412cdc21997-09-16 21:51:14 +00002446 PyTuple_SET_ITEM(PyExc_ArithmeticError, 2, PyExc_FloatingPointError);
2447 PyDict_SetItemString(dict, "ArithmeticError", PyExc_ArithmeticError);
Barry Warsaw757af0e1997-08-29 22:13:51 +00002448
Barry Warsawd086a1a1998-07-23 15:59:57 +00002449 PyExc_EnvironmentError = PyTuple_New(2);
2450 Py_INCREF(PyExc_IOError);
2451 PyTuple_SET_ITEM(PyExc_EnvironmentError, 0, PyExc_IOError);
2452 Py_INCREF(PyExc_OSError);
2453 PyTuple_SET_ITEM(PyExc_EnvironmentError, 1, PyExc_OSError);
2454 PyDict_SetItemString(dict, "EnvironmentError", PyExc_EnvironmentError);
2455
Guido van Rossum87460821999-06-22 14:47:32 +00002456 /* Make UnboundLocalError an alias for NameError */
2457 Py_INCREF(PyExc_NameError);
2458 Py_DECREF(PyExc_UnboundLocalError);
2459 PyExc_UnboundLocalError = PyExc_NameError;
2460 if (PyDict_SetItemString(dict, "UnboundLocalError",
2461 PyExc_NameError) != 0)
2462 Py_FatalError("Cannot create string-based exceptions");
2463
Barry Warsaw72b715d1999-02-24 00:35:43 +00002464 /* missing from the StandardError tuple: Exception, StandardError,
2465 * and SystemExit
2466 */
2467 PyExc_StandardError = PyTuple_New(exccnt-3);
2468 for (i = 2, j = 0; bltin_exc[i].name; i++) {
Barry Warsaw757af0e1997-08-29 22:13:51 +00002469 PyObject *exc = *bltin_exc[i].exc;
Barry Warsaw72b715d1999-02-24 00:35:43 +00002470 /* SystemExit is not an error, but it is an exception */
2471 if (exc != PyExc_SystemExit) {
2472 Py_INCREF(exc);
2473 PyTuple_SET_ITEM(PyExc_StandardError, j++, exc);
2474 }
Barry Warsaw757af0e1997-08-29 22:13:51 +00002475 }
2476 PyDict_SetItemString(dict, "StandardError", PyExc_StandardError);
Guido van Rossum04748321997-09-16 18:43:15 +00002477
Barry Warsaw72b715d1999-02-24 00:35:43 +00002478 /* Exception is a 2-tuple */
2479 PyExc_Exception = PyTuple_New(2);
2480 Py_INCREF(PyExc_SystemExit);
2481 PyTuple_SET_ITEM(PyExc_Exception, 0, PyExc_SystemExit);
2482 Py_INCREF(PyExc_StandardError);
2483 PyTuple_SET_ITEM(PyExc_Exception, 1, PyExc_StandardError);
Guido van Rossum04748321997-09-16 18:43:15 +00002484 PyDict_SetItemString(dict, "Exception", PyExc_Exception);
Barry Warsaw757af0e1997-08-29 22:13:51 +00002485
2486 if (PyErr_Occurred())
2487 Py_FatalError("Could not initialize built-in string exceptions");
Guido van Rossum25ce5661997-08-02 03:10:38 +00002488}
2489
Barry Warsaw72b715d1999-02-24 00:35:43 +00002490
Guido van Rossum25ce5661997-08-02 03:10:38 +00002491static void
2492finierrors()
2493{
Barry Warsaw757af0e1997-08-29 22:13:51 +00002494 int i;
2495 for (i = 0; bltin_exc[i].name; i++) {
2496 PyObject *exc = *bltin_exc[i].exc;
2497 Py_XDECREF(exc);
2498 *bltin_exc[i].exc = NULL;
2499 }
Guido van Rossum25ce5661997-08-02 03:10:38 +00002500}
2501
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002502static char builtin_doc[] =
2503"Built-in functions, exceptions, and other objects.\n\
2504\n\
2505Noteworthy: None is the `nil' object; Ellipsis represents `...' in slices.";
2506
Guido van Rossum25ce5661997-08-02 03:10:38 +00002507PyObject *
Barry Warsaw757af0e1997-08-29 22:13:51 +00002508_PyBuiltin_Init_1()
Guido van Rossum25ce5661997-08-02 03:10:38 +00002509{
2510 PyObject *mod, *dict;
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002511 mod = Py_InitModule4("__builtin__", builtin_methods,
2512 builtin_doc, (PyObject *)NULL,
2513 PYTHON_API_VERSION);
Guido van Rossum25ce5661997-08-02 03:10:38 +00002514 if (mod == NULL)
2515 return NULL;
2516 dict = PyModule_GetDict(mod);
2517 initerrors(dict);
2518 if (PyDict_SetItemString(dict, "None", Py_None) < 0)
2519 return NULL;
2520 if (PyDict_SetItemString(dict, "Ellipsis", Py_Ellipsis) < 0)
2521 return NULL;
2522 if (PyDict_SetItemString(dict, "__debug__",
2523 PyInt_FromLong(Py_OptimizeFlag == 0)) < 0)
2524 return NULL;
Barry Warsaw757af0e1997-08-29 22:13:51 +00002525
Guido van Rossum25ce5661997-08-02 03:10:38 +00002526 return mod;
Guido van Rossum3f5da241990-12-20 15:06:42 +00002527}
2528
2529void
Barry Warsaw757af0e1997-08-29 22:13:51 +00002530_PyBuiltin_Init_2(dict)
2531 PyObject *dict;
2532{
2533 /* if Python was started with -X, initialize the class exceptions */
Barry Warsaw98b62461998-09-14 18:51:11 +00002534 if (Py_UseClassExceptionsFlag) {
2535 if (!init_class_exc(dict)) {
2536 /* class based exceptions could not be
2537 * initialized. Fall back to using string based
2538 * exceptions.
2539 */
2540 PySys_WriteStderr(
2541 "Warning! Falling back to string-based exceptions\n");
2542 initerrors(dict);
2543 }
2544 }
Barry Warsaw757af0e1997-08-29 22:13:51 +00002545}
2546
2547
2548void
2549_PyBuiltin_Fini_1()
2550{
2551 fini_instances();
2552}
2553
2554
2555void
2556_PyBuiltin_Fini_2()
Guido van Rossum3f5da241990-12-20 15:06:42 +00002557{
Guido van Rossum25ce5661997-08-02 03:10:38 +00002558 finierrors();
Guido van Rossum3f5da241990-12-20 15:06:42 +00002559}
Guido van Rossumc6bb8f71991-07-01 18:42:41 +00002560
Guido van Rossum12d12c51993-10-26 17:58:25 +00002561
Guido van Rossume77a7571993-11-03 15:01:26 +00002562/* Helper for filter(): filter a tuple through a function */
Guido van Rossum12d12c51993-10-26 17:58:25 +00002563
Guido van Rossum79f25d91997-04-29 20:08:16 +00002564static PyObject *
Guido van Rossum12d12c51993-10-26 17:58:25 +00002565filtertuple(func, tuple)
Guido van Rossum79f25d91997-04-29 20:08:16 +00002566 PyObject *func;
2567 PyObject *tuple;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002568{
Guido van Rossum79f25d91997-04-29 20:08:16 +00002569 PyObject *result;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002570 register int i, j;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002571 int len = PyTuple_Size(tuple);
Guido van Rossum12d12c51993-10-26 17:58:25 +00002572
Guido van Rossumb7b45621995-08-04 04:07:45 +00002573 if (len == 0) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002574 Py_INCREF(tuple);
Guido van Rossumb7b45621995-08-04 04:07:45 +00002575 return tuple;
2576 }
2577
Guido van Rossum79f25d91997-04-29 20:08:16 +00002578 if ((result = PyTuple_New(len)) == NULL)
Guido van Rossum2586bf01993-11-01 16:21:44 +00002579 return NULL;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002580
Guido van Rossum12d12c51993-10-26 17:58:25 +00002581 for (i = j = 0; i < len; ++i) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002582 PyObject *item, *good;
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002583 int ok;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002584
Guido van Rossum79f25d91997-04-29 20:08:16 +00002585 if ((item = PyTuple_GetItem(tuple, i)) == NULL)
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002586 goto Fail_1;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002587 if (func == Py_None) {
2588 Py_INCREF(item);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002589 good = item;
2590 }
2591 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002592 PyObject *arg = Py_BuildValue("(O)", item);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002593 if (arg == NULL)
2594 goto Fail_1;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002595 good = PyEval_CallObject(func, arg);
2596 Py_DECREF(arg);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002597 if (good == NULL)
Guido van Rossum12d12c51993-10-26 17:58:25 +00002598 goto Fail_1;
2599 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002600 ok = PyObject_IsTrue(good);
2601 Py_DECREF(good);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002602 if (ok) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002603 Py_INCREF(item);
2604 if (PyTuple_SetItem(result, j++, item) < 0)
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002605 goto Fail_1;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002606 }
Guido van Rossum12d12c51993-10-26 17:58:25 +00002607 }
2608
Guido van Rossum79f25d91997-04-29 20:08:16 +00002609 if (_PyTuple_Resize(&result, j, 0) < 0)
Guido van Rossum12d12c51993-10-26 17:58:25 +00002610 return NULL;
2611
Guido van Rossum12d12c51993-10-26 17:58:25 +00002612 return result;
2613
Guido van Rossum12d12c51993-10-26 17:58:25 +00002614Fail_1:
Guido van Rossum79f25d91997-04-29 20:08:16 +00002615 Py_DECREF(result);
Guido van Rossum12d12c51993-10-26 17:58:25 +00002616 return NULL;
2617}
2618
2619
Guido van Rossume77a7571993-11-03 15:01:26 +00002620/* Helper for filter(): filter a string through a function */
Guido van Rossum12d12c51993-10-26 17:58:25 +00002621
Guido van Rossum79f25d91997-04-29 20:08:16 +00002622static PyObject *
Guido van Rossum12d12c51993-10-26 17:58:25 +00002623filterstring(func, strobj)
Guido van Rossum79f25d91997-04-29 20:08:16 +00002624 PyObject *func;
2625 PyObject *strobj;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002626{
Guido van Rossum79f25d91997-04-29 20:08:16 +00002627 PyObject *result;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002628 register int i, j;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002629 int len = PyString_Size(strobj);
Guido van Rossum12d12c51993-10-26 17:58:25 +00002630
Guido van Rossum79f25d91997-04-29 20:08:16 +00002631 if (func == Py_None) {
Guido van Rossum2586bf01993-11-01 16:21:44 +00002632 /* No character is ever false -- share input string */
Guido van Rossum79f25d91997-04-29 20:08:16 +00002633 Py_INCREF(strobj);
Guido van Rossum2d951851994-08-29 12:52:16 +00002634 return strobj;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002635 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002636 if ((result = PyString_FromStringAndSize(NULL, len)) == NULL)
Guido van Rossum2586bf01993-11-01 16:21:44 +00002637 return NULL;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002638
Guido van Rossum12d12c51993-10-26 17:58:25 +00002639 for (i = j = 0; i < len; ++i) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002640 PyObject *item, *arg, *good;
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002641 int ok;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002642
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002643 item = (*strobj->ob_type->tp_as_sequence->sq_item)(strobj, i);
2644 if (item == NULL)
2645 goto Fail_1;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002646 arg = Py_BuildValue("(O)", item);
2647 Py_DECREF(item);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002648 if (arg == NULL)
2649 goto Fail_1;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002650 good = PyEval_CallObject(func, arg);
2651 Py_DECREF(arg);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002652 if (good == NULL)
2653 goto Fail_1;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002654 ok = PyObject_IsTrue(good);
2655 Py_DECREF(good);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002656 if (ok)
Guido van Rossum79f25d91997-04-29 20:08:16 +00002657 PyString_AS_STRING((PyStringObject *)result)[j++] =
2658 PyString_AS_STRING((PyStringObject *)item)[0];
Guido van Rossum12d12c51993-10-26 17:58:25 +00002659 }
2660
Guido van Rossum79f25d91997-04-29 20:08:16 +00002661 if (j < len && _PyString_Resize(&result, j) < 0)
Guido van Rossum12d12c51993-10-26 17:58:25 +00002662 return NULL;
2663
Guido van Rossum12d12c51993-10-26 17:58:25 +00002664 return result;
2665
Guido van Rossum12d12c51993-10-26 17:58:25 +00002666Fail_1:
Guido van Rossum79f25d91997-04-29 20:08:16 +00002667 Py_DECREF(result);
Guido van Rossum12d12c51993-10-26 17:58:25 +00002668 return NULL;
2669}