blob: 90af88854b9499ffc5df4c5a0ceed445abd412a2 [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[] =
Guido van Rossum09095f32000-03-10 23:00:52 +0000155"buffer(object [, offset[, size]]) -> object\n\
Guido van Rossum0daf0221999-03-19 19:07:19 +0000156\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 Rossum09095f32000-03-10 23:00:52 +0000164builtin_unicode(self, args)
165 PyObject *self;
166 PyObject *args;
167{
168 char *s;
169 int len;
170 char *encoding = NULL;
171 char *errors = NULL;
172
173 if ( !PyArg_ParseTuple(args, "s#|ss:unicode", &s, &len,
174 &encoding, &errors) )
175 return NULL;
176 return PyUnicode_Decode(s, len, encoding, errors);
177}
178
179static char unicode_doc[] =
180"unicode(string [, encoding[, errors]]) -> object\n\
181\n\
182Creates a new unicode object from the given encoded string.\n\
183encoding defaults to 'utf-8' and errors, defining the error handling,\n\
184to 'strict'.";
185
186
187static PyObject *
Guido van Rossum2d951851994-08-29 12:52:16 +0000188builtin_callable(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000189 PyObject *self;
190 PyObject *args;
Guido van Rossum2d951851994-08-29 12:52:16 +0000191{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000192 PyObject *v;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000193
Guido van Rossum79f25d91997-04-29 20:08:16 +0000194 if (!PyArg_ParseTuple(args, "O:callable", &v))
Guido van Rossum2d951851994-08-29 12:52:16 +0000195 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000196 return PyInt_FromLong((long)PyCallable_Check(v));
Guido van Rossum2d951851994-08-29 12:52:16 +0000197}
198
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000199static char callable_doc[] =
200"callable(object) -> Boolean\n\
201\n\
202Return whether the object is callable (i.e., some kind of function).\n\
203Note that classes are callable, as are instances with a __call__() method.";
204
205
Guido van Rossum79f25d91997-04-29 20:08:16 +0000206static PyObject *
Guido van Rossume77a7571993-11-03 15:01:26 +0000207builtin_filter(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000208 PyObject *self;
209 PyObject *args;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000210{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000211 PyObject *func, *seq, *result;
212 PySequenceMethods *sqf;
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000213 int len;
214 register int i, j;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000215
Guido van Rossum79f25d91997-04-29 20:08:16 +0000216 if (!PyArg_ParseTuple(args, "OO:filter", &func, &seq))
Guido van Rossum12d12c51993-10-26 17:58:25 +0000217 return NULL;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000218
Guido van Rossum79f25d91997-04-29 20:08:16 +0000219 if (PyString_Check(seq)) {
220 PyObject *r = filterstring(func, seq);
Guido van Rossum12d12c51993-10-26 17:58:25 +0000221 return r;
222 }
223
Guido van Rossum79f25d91997-04-29 20:08:16 +0000224 if (PyTuple_Check(seq)) {
225 PyObject *r = filtertuple(func, seq);
Guido van Rossum12d12c51993-10-26 17:58:25 +0000226 return r;
227 }
228
Guido van Rossum09df08a1998-05-22 00:51:39 +0000229 sqf = seq->ob_type->tp_as_sequence;
230 if (sqf == NULL || sqf->sq_length == NULL || sqf->sq_item == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000231 PyErr_SetString(PyExc_TypeError,
Guido van Rossume77a7571993-11-03 15:01:26 +0000232 "argument 2 to filter() must be a sequence type");
Guido van Rossum12d12c51993-10-26 17:58:25 +0000233 goto Fail_2;
234 }
235
236 if ((len = (*sqf->sq_length)(seq)) < 0)
237 goto Fail_2;
238
Guido van Rossum79f25d91997-04-29 20:08:16 +0000239 if (PyList_Check(seq) && seq->ob_refcnt == 1) {
240 Py_INCREF(seq);
Guido van Rossum12d12c51993-10-26 17:58:25 +0000241 result = seq;
242 }
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000243 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000244 if ((result = PyList_New(len)) == NULL)
Guido van Rossum12d12c51993-10-26 17:58:25 +0000245 goto Fail_2;
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000246 }
Guido van Rossum12d12c51993-10-26 17:58:25 +0000247
Guido van Rossum2d951851994-08-29 12:52:16 +0000248 for (i = j = 0; ; ++i) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000249 PyObject *item, *good;
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000250 int ok;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000251
Guido van Rossum2d951851994-08-29 12:52:16 +0000252 if ((item = (*sqf->sq_item)(seq, i)) == NULL) {
Barry Warsawcde8b1b1997-08-22 21:14:38 +0000253 if (PyErr_ExceptionMatches(PyExc_IndexError)) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000254 PyErr_Clear();
Guido van Rossum2d951851994-08-29 12:52:16 +0000255 break;
256 }
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000257 goto Fail_1;
Guido van Rossum2d951851994-08-29 12:52:16 +0000258 }
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000259
Guido van Rossum79f25d91997-04-29 20:08:16 +0000260 if (func == Py_None) {
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000261 good = item;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000262 Py_INCREF(good);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000263 }
264 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000265 PyObject *arg = Py_BuildValue("(O)", item);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000266 if (arg == NULL)
267 goto Fail_1;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000268 good = PyEval_CallObject(func, arg);
269 Py_DECREF(arg);
Guido van Rossum58b68731995-01-10 17:40:55 +0000270 if (good == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000271 Py_DECREF(item);
Guido van Rossum12d12c51993-10-26 17:58:25 +0000272 goto Fail_1;
Guido van Rossum58b68731995-01-10 17:40:55 +0000273 }
Guido van Rossum12d12c51993-10-26 17:58:25 +0000274 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000275 ok = PyObject_IsTrue(good);
276 Py_DECREF(good);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000277 if (ok) {
Guido van Rossum2d951851994-08-29 12:52:16 +0000278 if (j < len) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000279 if (PyList_SetItem(result, j++, item) < 0)
Guido van Rossum2d951851994-08-29 12:52:16 +0000280 goto Fail_1;
281 }
282 else {
Barry Warsawfa77e091999-01-28 18:49:12 +0000283 int status = PyList_Append(result, item);
Guido van Rossum2d951851994-08-29 12:52:16 +0000284 j++;
Barry Warsawfa77e091999-01-28 18:49:12 +0000285 Py_DECREF(item);
286 if (status < 0)
Guido van Rossum2d951851994-08-29 12:52:16 +0000287 goto Fail_1;
288 }
Guido van Rossum58b68731995-01-10 17:40:55 +0000289 } else {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000290 Py_DECREF(item);
Guido van Rossum12d12c51993-10-26 17:58:25 +0000291 }
Guido van Rossum12d12c51993-10-26 17:58:25 +0000292 }
293
Guido van Rossum12d12c51993-10-26 17:58:25 +0000294
Guido van Rossum79f25d91997-04-29 20:08:16 +0000295 if (j < len && PyList_SetSlice(result, j, len, NULL) < 0)
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000296 goto Fail_1;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000297
Guido van Rossum12d12c51993-10-26 17:58:25 +0000298 return result;
299
Guido van Rossum12d12c51993-10-26 17:58:25 +0000300Fail_1:
Guido van Rossum79f25d91997-04-29 20:08:16 +0000301 Py_DECREF(result);
Guido van Rossum12d12c51993-10-26 17:58:25 +0000302Fail_2:
Guido van Rossum12d12c51993-10-26 17:58:25 +0000303 return NULL;
304}
305
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000306static char filter_doc[] =
307"filter(function, sequence) -> list\n\
308\n\
309Return a list containing those items of sequence for which function(item)\n\
310is true. If function is None, return a list of items that are true.";
311
312
Guido van Rossum79f25d91997-04-29 20:08:16 +0000313static PyObject *
Guido van Rossum94390a41992-08-14 15:14:30 +0000314builtin_chr(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000315 PyObject *self;
316 PyObject *args;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000317{
318 long x;
319 char s[1];
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000320
Guido van Rossum79f25d91997-04-29 20:08:16 +0000321 if (!PyArg_ParseTuple(args, "l:chr", &x))
Guido van Rossum3f5da241990-12-20 15:06:42 +0000322 return NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000323 if (x < 0 || x >= 256) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000324 PyErr_SetString(PyExc_ValueError,
325 "chr() arg not in range(256)");
Guido van Rossum3f5da241990-12-20 15:06:42 +0000326 return NULL;
327 }
Guido van Rossum6bf62da1997-04-11 20:37:35 +0000328 s[0] = (char)x;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000329 return PyString_FromStringAndSize(s, 1);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000330}
331
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000332static char chr_doc[] =
333"chr(i) -> character\n\
334\n\
335Return a string of one character with ordinal i; 0 <= i < 256.";
336
337
Guido van Rossum79f25d91997-04-29 20:08:16 +0000338static PyObject *
Guido van Rossum09095f32000-03-10 23:00:52 +0000339builtin_unichr(self, args)
340 PyObject *self;
341 PyObject *args;
342{
343 long x;
344 Py_UNICODE s[1];
345
346 if (!PyArg_ParseTuple(args, "l:unichr", &x))
347 return NULL;
348 if (x < 0 || x >= 65536) {
349 PyErr_SetString(PyExc_ValueError,
350 "unichr() arg not in range(65536)");
351 return NULL;
352 }
353 s[0] = (Py_UNICODE)x;
354 return PyUnicode_FromUnicode(s, 1);
355}
356
357static char unichr_doc[] =
358"unichr(i) -> unicode character\n\
359\n\
360Return a unicode string of one character with ordinal i; 0 <= i < 65536.";
361
362
363static PyObject *
Guido van Rossuma9e7dc11992-10-18 18:53:57 +0000364builtin_cmp(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000365 PyObject *self;
366 PyObject *args;
Guido van Rossuma9e7dc11992-10-18 18:53:57 +0000367{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000368 PyObject *a, *b;
Guido van Rossum09df08a1998-05-22 00:51:39 +0000369 int c;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000370
Guido van Rossum79f25d91997-04-29 20:08:16 +0000371 if (!PyArg_ParseTuple(args, "OO:cmp", &a, &b))
Guido van Rossuma9e7dc11992-10-18 18:53:57 +0000372 return NULL;
Guido van Rossum09df08a1998-05-22 00:51:39 +0000373 if (PyObject_Cmp(a, b, &c) < 0)
Guido van Rossumc8b6df91997-05-23 00:06:51 +0000374 return NULL;
Guido van Rossum09df08a1998-05-22 00:51:39 +0000375 return PyInt_FromLong((long)c);
Guido van Rossuma9e7dc11992-10-18 18:53:57 +0000376}
377
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000378static char cmp_doc[] =
379"cmp(x, y) -> integer\n\
380\n\
381Return negative if x<y, zero if x==y, positive if x>y.";
382
383
Guido van Rossum79f25d91997-04-29 20:08:16 +0000384static PyObject *
Guido van Rossum5524a591995-01-10 15:26:20 +0000385builtin_coerce(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000386 PyObject *self;
387 PyObject *args;
Guido van Rossum6a00cd81995-01-07 12:39:01 +0000388{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000389 PyObject *v, *w;
390 PyObject *res;
Guido van Rossum5524a591995-01-10 15:26:20 +0000391
Guido van Rossum79f25d91997-04-29 20:08:16 +0000392 if (!PyArg_ParseTuple(args, "OO:coerce", &v, &w))
Guido van Rossum5524a591995-01-10 15:26:20 +0000393 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000394 if (PyNumber_Coerce(&v, &w) < 0)
Guido van Rossum04691fc1992-08-12 15:35:34 +0000395 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000396 res = Py_BuildValue("(OO)", v, w);
397 Py_DECREF(v);
398 Py_DECREF(w);
Guido van Rossum04691fc1992-08-12 15:35:34 +0000399 return res;
400}
401
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000402static char coerce_doc[] =
403"coerce(x, y) -> None or (x1, y1)\n\
404\n\
405When x and y can be coerced to values of the same type, return a tuple\n\
406containing the coerced values. When they can't be coerced, return None.";
407
408
Guido van Rossum79f25d91997-04-29 20:08:16 +0000409static PyObject *
Guido van Rossum5b722181993-03-30 17:46:03 +0000410builtin_compile(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000411 PyObject *self;
412 PyObject *args;
Guido van Rossum5b722181993-03-30 17:46:03 +0000413{
414 char *str;
415 char *filename;
416 char *startstr;
417 int start;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000418
Guido van Rossum79f25d91997-04-29 20:08:16 +0000419 if (!PyArg_ParseTuple(args, "sss:compile", &str, &filename, &startstr))
Guido van Rossum5b722181993-03-30 17:46:03 +0000420 return NULL;
421 if (strcmp(startstr, "exec") == 0)
Guido van Rossumb05a5c71997-05-07 17:46:13 +0000422 start = Py_file_input;
Guido van Rossum5b722181993-03-30 17:46:03 +0000423 else if (strcmp(startstr, "eval") == 0)
Guido van Rossumb05a5c71997-05-07 17:46:13 +0000424 start = Py_eval_input;
Guido van Rossum872537c1995-07-07 22:43:42 +0000425 else if (strcmp(startstr, "single") == 0)
Guido van Rossumb05a5c71997-05-07 17:46:13 +0000426 start = Py_single_input;
Guido van Rossum5b722181993-03-30 17:46:03 +0000427 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000428 PyErr_SetString(PyExc_ValueError,
Guido van Rossum872537c1995-07-07 22:43:42 +0000429 "compile() mode must be 'exec' or 'eval' or 'single'");
Guido van Rossum5b722181993-03-30 17:46:03 +0000430 return NULL;
431 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000432 return Py_CompileString(str, filename, start);
Guido van Rossum5b722181993-03-30 17:46:03 +0000433}
434
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000435static char compile_doc[] =
436"compile(source, filename, mode) -> code object\n\
437\n\
438Compile the source string (a Python module, statement or expression)\n\
439into a code object that can be executed by the exec statement or eval().\n\
440The filename will be used for run-time error messages.\n\
441The mode must be 'exec' to compile a module, 'single' to compile a\n\
442single (interactive) statement, or 'eval' to compile an expression.";
443
444
Guido van Rossum8a5c5d21996-01-12 01:09:56 +0000445#ifndef WITHOUT_COMPLEX
446
Guido van Rossum79f25d91997-04-29 20:08:16 +0000447static PyObject *
Guido van Rossum11950231999-03-25 21:16:07 +0000448complex_from_string(v)
449 PyObject *v;
450{
451 extern double strtod Py_PROTO((const char *, char **));
Guido van Rossum99fb7c71999-04-07 16:05:47 +0000452 char *s, *start, *end;
Guido van Rossum11950231999-03-25 21:16:07 +0000453 double x=0.0, y=0.0, z;
454 int got_re=0, got_im=0, done=0;
455 int digit_or_dot;
456 int sw_error=0;
457 int sign;
458 char buffer[256]; /* For errors */
459
460 start = s = PyString_AS_STRING(v);
461
462 /* position on first nonblank */
463 while (*s && isspace(Py_CHARMASK(*s)))
464 s++;
465 if (s[0] == '\0') {
466 PyErr_SetString(PyExc_ValueError,
467 "empty string for complex()");
468 return NULL;
469 }
470
471 z = -1.0;
472 sign = 1;
473 do {
474
475 switch (*s) {
476
477 case '\0':
478 if (s-start != PyString_GET_SIZE(v)) {
479 PyErr_SetString(
480 PyExc_ValueError,
481 "null byte in argument for complex()");
482 return NULL;
483 }
484 if(!done) sw_error=1;
485 break;
486
487 case '-':
488 sign = -1;
489 /* Fallthrough */
490 case '+':
491 if (done) sw_error=1;
492 s++;
493 if ( *s=='\0'||*s=='+'||*s=='-' ||
494 isspace(Py_CHARMASK(*s)) ) sw_error=1;
495 break;
496
497 case 'J':
498 case 'j':
499 if (got_im || done) {
500 sw_error = 1;
501 break;
502 }
503 if (z<0.0) {
504 y=sign;
505 }
506 else{
507 y=sign*z;
508 }
509 got_im=1;
510 s++;
511 if (*s!='+' && *s!='-' )
512 done=1;
513 break;
514
515 default:
516 if (isspace(Py_CHARMASK(*s))) {
517 while (*s && isspace(Py_CHARMASK(*s)))
518 s++;
519 if (s[0] != '\0')
520 sw_error=1;
521 else
522 done = 1;
523 break;
524 }
525 digit_or_dot =
526 (*s=='.' || isdigit(Py_CHARMASK(*s)));
527 if (done||!digit_or_dot) {
528 sw_error=1;
529 break;
530 }
531 errno = 0;
532 PyFPE_START_PROTECT("strtod", return 0)
533 z = strtod(s, &end) ;
534 PyFPE_END_PROTECT(z)
535 if (errno != 0) {
536 sprintf(buffer,
537 "float() out of range: %.150s", s);
538 PyErr_SetString(
539 PyExc_ValueError,
540 buffer);
541 return NULL;
542 }
543 s=end;
544 if (*s=='J' || *s=='j') {
545
546 break;
547 }
548 if (got_re) {
549 sw_error=1;
550 break;
551 }
552
553 /* accept a real part */
554 x=sign*z;
555 got_re=1;
556 if (got_im) done=1;
557 z = -1.0;
558 sign = 1;
559 break;
560
561 } /* end of switch */
562
563 } while (*s!='\0' && !sw_error);
564
565 if (sw_error) {
566 PyErr_SetString(PyExc_ValueError,
567 "malformed string for complex()");
568 return NULL;
569 }
570
571 return PyComplex_FromDoubles(x,y);
572}
573
574static PyObject *
Guido van Rossum8a5c5d21996-01-12 01:09:56 +0000575builtin_complex(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000576 PyObject *self;
577 PyObject *args;
Guido van Rossum8a5c5d21996-01-12 01:09:56 +0000578{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000579 PyObject *r, *i, *tmp;
580 PyNumberMethods *nbr, *nbi = NULL;
Guido van Rossum530956d1996-07-21 02:27:43 +0000581 Py_complex cr, ci;
Guido van Rossumc6472e91997-03-31 17:15:43 +0000582 int own_r = 0;
Guido van Rossum8a5c5d21996-01-12 01:09:56 +0000583
584 i = NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000585 if (!PyArg_ParseTuple(args, "O|O:complex", &r, &i))
Guido van Rossum8a5c5d21996-01-12 01:09:56 +0000586 return NULL;
Guido van Rossum11950231999-03-25 21:16:07 +0000587 if (PyString_Check(r))
588 return complex_from_string(r);
Guido van Rossum8a5c5d21996-01-12 01:09:56 +0000589 if ((nbr = r->ob_type->tp_as_number) == NULL ||
Guido van Rossumc6472e91997-03-31 17:15:43 +0000590 nbr->nb_float == NULL ||
591 (i != NULL &&
592 ((nbi = i->ob_type->tp_as_number) == NULL ||
593 nbi->nb_float == NULL))) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000594 PyErr_SetString(PyExc_TypeError,
Guido van Rossum8a5c5d21996-01-12 01:09:56 +0000595 "complex() argument can't be converted to complex");
596 return NULL;
597 }
Guido van Rossumed0af8f1996-12-05 23:18:18 +0000598 /* XXX Hack to support classes with __complex__ method */
Guido van Rossum79f25d91997-04-29 20:08:16 +0000599 if (PyInstance_Check(r)) {
600 static PyObject *complexstr;
601 PyObject *f;
Guido van Rossumed0af8f1996-12-05 23:18:18 +0000602 if (complexstr == NULL) {
Guido van Rossum8d751611997-01-18 08:04:16 +0000603 complexstr = PyString_InternFromString("__complex__");
Guido van Rossumed0af8f1996-12-05 23:18:18 +0000604 if (complexstr == NULL)
605 return NULL;
606 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000607 f = PyObject_GetAttr(r, complexstr);
Guido van Rossumed0af8f1996-12-05 23:18:18 +0000608 if (f == NULL)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000609 PyErr_Clear();
Guido van Rossumed0af8f1996-12-05 23:18:18 +0000610 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000611 PyObject *args = Py_BuildValue("()");
Guido van Rossumed0af8f1996-12-05 23:18:18 +0000612 if (args == NULL)
613 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000614 r = PyEval_CallObject(f, args);
615 Py_DECREF(args);
Barry Warsawf988e681999-01-27 23:13:59 +0000616 Py_DECREF(f);
Guido van Rossumed0af8f1996-12-05 23:18:18 +0000617 if (r == NULL)
618 return NULL;
Guido van Rossumc6472e91997-03-31 17:15:43 +0000619 own_r = 1;
Guido van Rossumed0af8f1996-12-05 23:18:18 +0000620 }
621 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000622 if (PyComplex_Check(r)) {
623 cr = ((PyComplexObject*)r)->cval;
Guido van Rossum730806d1998-04-10 22:27:42 +0000624 if (own_r) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000625 Py_DECREF(r);
Guido van Rossum730806d1998-04-10 22:27:42 +0000626 }
Guido van Rossumc6472e91997-03-31 17:15:43 +0000627 }
Guido van Rossum8a5c5d21996-01-12 01:09:56 +0000628 else {
Guido van Rossumfe4b6ee1996-08-08 18:49:41 +0000629 tmp = (*nbr->nb_float)(r);
Guido van Rossum730806d1998-04-10 22:27:42 +0000630 if (own_r) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000631 Py_DECREF(r);
Guido van Rossum730806d1998-04-10 22:27:42 +0000632 }
Guido van Rossumfe4b6ee1996-08-08 18:49:41 +0000633 if (tmp == NULL)
634 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000635 cr.real = PyFloat_AsDouble(tmp);
636 Py_DECREF(tmp);
Guido van Rossum11950231999-03-25 21:16:07 +0000637 cr.imag = 0.0;
Guido van Rossum8a5c5d21996-01-12 01:09:56 +0000638 }
639 if (i == NULL) {
Guido van Rossum11950231999-03-25 21:16:07 +0000640 ci.real = 0.0;
641 ci.imag = 0.0;
Guido van Rossum8a5c5d21996-01-12 01:09:56 +0000642 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000643 else if (PyComplex_Check(i))
644 ci = ((PyComplexObject*)i)->cval;
Guido van Rossum8a5c5d21996-01-12 01:09:56 +0000645 else {
Guido van Rossumc6472e91997-03-31 17:15:43 +0000646 tmp = (*nbi->nb_float)(i);
Guido van Rossumfe4b6ee1996-08-08 18:49:41 +0000647 if (tmp == NULL)
648 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000649 ci.real = PyFloat_AsDouble(tmp);
650 Py_DECREF(tmp);
Guido van Rossum8a5c5d21996-01-12 01:09:56 +0000651 ci.imag = 0.;
652 }
653 cr.real -= ci.imag;
654 cr.imag += ci.real;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000655 return PyComplex_FromCComplex(cr);
Guido van Rossum8a5c5d21996-01-12 01:09:56 +0000656}
657
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000658static char complex_doc[] =
659"complex(real[, imag]) -> complex number\n\
660\n\
661Create a complex number from a real part and an optional imaginary part.\n\
662This is equivalent to (real + imag*1j) where imag defaults to 0.";
663
664
Guido van Rossum8a5c5d21996-01-12 01:09:56 +0000665#endif
666
Guido van Rossum79f25d91997-04-29 20:08:16 +0000667static PyObject *
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000668builtin_dir(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000669 PyObject *self;
670 PyObject *args;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000671{
Guido van Rossum666b17a1997-05-06 16:36:57 +0000672 static char *attrlist[] = {"__members__", "__methods__", NULL};
673 PyObject *v = NULL, *l = NULL, *m = NULL;
674 PyObject *d, *x;
675 int i;
676 char **s;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000677
Guido van Rossum79f25d91997-04-29 20:08:16 +0000678 if (!PyArg_ParseTuple(args, "|O:dir", &v))
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000679 return NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000680 if (v == NULL) {
Guido van Rossum666b17a1997-05-06 16:36:57 +0000681 x = PyEval_GetLocals();
682 if (x == NULL)
683 goto error;
684 l = PyMapping_Keys(x);
685 if (l == NULL)
686 goto error;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000687 }
688 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000689 d = PyObject_GetAttrString(v, "__dict__");
Guido van Rossum666b17a1997-05-06 16:36:57 +0000690 if (d == NULL)
Guido van Rossum795ba581996-05-23 22:49:07 +0000691 PyErr_Clear();
Guido van Rossum666b17a1997-05-06 16:36:57 +0000692 else {
693 l = PyMapping_Keys(d);
694 if (l == NULL)
695 PyErr_Clear();
696 Py_DECREF(d);
697 }
698 if (l == NULL) {
699 l = PyList_New(0);
700 if (l == NULL)
701 goto error;
702 }
703 for (s = attrlist; *s != NULL; s++) {
704 m = PyObject_GetAttrString(v, *s);
705 if (m == NULL) {
706 PyErr_Clear();
707 continue;
708 }
709 for (i = 0; ; i++) {
710 x = PySequence_GetItem(m, i);
711 if (x == NULL) {
712 PyErr_Clear();
713 break;
714 }
715 if (PyList_Append(l, x) != 0) {
716 Py_DECREF(x);
717 Py_DECREF(m);
718 goto error;
719 }
720 Py_DECREF(x);
721 }
722 Py_DECREF(m);
Guido van Rossum795ba581996-05-23 22:49:07 +0000723 }
Guido van Rossum006bcd41991-10-24 14:54:44 +0000724 }
Guido van Rossum666b17a1997-05-06 16:36:57 +0000725 if (PyList_Sort(l) != 0)
726 goto error;
727 return l;
728 error:
729 Py_XDECREF(l);
730 return NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000731}
732
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000733static char dir_doc[] =
734"dir([object]) -> list of strings\n\
735\n\
736Return an alphabetized list of names comprising (some of) the attributes\n\
737of the given object. Without an argument, the names in the current scope\n\
738are listed. With an instance argument, only the instance attributes are\n\
739returned. With a class argument, attributes of the base class are not\n\
740returned. For other types or arguments, this may list members or methods.";
741
742
Guido van Rossum79f25d91997-04-29 20:08:16 +0000743static PyObject *
Guido van Rossum6a00cd81995-01-07 12:39:01 +0000744builtin_divmod(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000745 PyObject *self;
746 PyObject *args;
Guido van Rossum6a00cd81995-01-07 12:39:01 +0000747{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000748 PyObject *v, *w;
Guido van Rossum6a00cd81995-01-07 12:39:01 +0000749
Guido van Rossum79f25d91997-04-29 20:08:16 +0000750 if (!PyArg_ParseTuple(args, "OO:divmod", &v, &w))
Guido van Rossum6a00cd81995-01-07 12:39:01 +0000751 return NULL;
Guido van Rossum09df08a1998-05-22 00:51:39 +0000752 return PyNumber_Divmod(v, w);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000753}
754
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000755static char divmod_doc[] =
756"divmod(x, y) -> (div, mod)\n\
757\n\
758Return the tuple ((x-x%y)/y, x%y). Invariant: div*y + mod == x.";
759
760
Guido van Rossum79f25d91997-04-29 20:08:16 +0000761static PyObject *
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000762builtin_eval(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000763 PyObject *self;
764 PyObject *args;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000765{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000766 PyObject *cmd;
767 PyObject *globals = Py_None, *locals = Py_None;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000768 char *str;
Guido van Rossum590baa41993-11-30 13:40:46 +0000769
Guido van Rossum79f25d91997-04-29 20:08:16 +0000770 if (!PyArg_ParseTuple(args, "O|O!O!:eval",
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000771 &cmd,
Guido van Rossum79f25d91997-04-29 20:08:16 +0000772 &PyDict_Type, &globals,
773 &PyDict_Type, &locals))
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000774 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000775 if (globals == Py_None) {
776 globals = PyEval_GetGlobals();
777 if (locals == Py_None)
778 locals = PyEval_GetLocals();
Guido van Rossum6135a871995-01-09 17:53:26 +0000779 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000780 else if (locals == Py_None)
Guido van Rossum6135a871995-01-09 17:53:26 +0000781 locals = globals;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000782 if (PyDict_GetItemString(globals, "__builtins__") == NULL) {
783 if (PyDict_SetItemString(globals, "__builtins__",
784 PyEval_GetBuiltins()) != 0)
Guido van Rossum6135a871995-01-09 17:53:26 +0000785 return NULL;
786 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000787 if (PyCode_Check(cmd))
788 return PyEval_EvalCode((PyCodeObject *) cmd, globals, locals);
789 if (!PyString_Check(cmd)) {
790 PyErr_SetString(PyExc_TypeError,
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000791 "eval() argument 1 must be string or code object");
Guido van Rossum94390a41992-08-14 15:14:30 +0000792 return NULL;
793 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000794 str = PyString_AsString(cmd);
795 if ((int)strlen(str) != PyString_Size(cmd)) {
796 PyErr_SetString(PyExc_ValueError,
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000797 "embedded '\\0' in string arg");
798 return NULL;
Guido van Rossumf08ab0a1992-03-04 16:41:41 +0000799 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000800 while (*str == ' ' || *str == '\t')
801 str++;
Guido van Rossumb05a5c71997-05-07 17:46:13 +0000802 return PyRun_String(str, Py_eval_input, globals, locals);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000803}
804
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000805static char eval_doc[] =
806"eval(source[, globals[, locals]]) -> value\n\
807\n\
808Evaluate the source in the context of globals and locals.\n\
809The source may be a string representing a Python expression\n\
810or a code object as returned by compile().\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 Rossum1ae940a1995-01-02 19:04:15 +0000816builtin_execfile(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000817 PyObject *self;
818 PyObject *args;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000819{
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000820 char *filename;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000821 PyObject *globals = Py_None, *locals = Py_None;
822 PyObject *res;
Guido van Rossum0f61f8a1992-02-25 18:55:05 +0000823 FILE* fp;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000824
Guido van Rossum79f25d91997-04-29 20:08:16 +0000825 if (!PyArg_ParseTuple(args, "s|O!O!:execfile",
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000826 &filename,
Guido van Rossum79f25d91997-04-29 20:08:16 +0000827 &PyDict_Type, &globals,
828 &PyDict_Type, &locals))
Guido van Rossum0f61f8a1992-02-25 18:55:05 +0000829 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000830 if (globals == Py_None) {
831 globals = PyEval_GetGlobals();
832 if (locals == Py_None)
833 locals = PyEval_GetLocals();
Guido van Rossum6135a871995-01-09 17:53:26 +0000834 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000835 else if (locals == Py_None)
Guido van Rossum6135a871995-01-09 17:53:26 +0000836 locals = globals;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000837 if (PyDict_GetItemString(globals, "__builtins__") == NULL) {
838 if (PyDict_SetItemString(globals, "__builtins__",
839 PyEval_GetBuiltins()) != 0)
Guido van Rossum6135a871995-01-09 17:53:26 +0000840 return NULL;
841 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000842 Py_BEGIN_ALLOW_THREADS
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000843 fp = fopen(filename, "r");
Guido van Rossum79f25d91997-04-29 20:08:16 +0000844 Py_END_ALLOW_THREADS
Guido van Rossum0f61f8a1992-02-25 18:55:05 +0000845 if (fp == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000846 PyErr_SetFromErrno(PyExc_IOError);
Guido van Rossum0f61f8a1992-02-25 18:55:05 +0000847 return NULL;
848 }
Guido van Rossumb05a5c71997-05-07 17:46:13 +0000849 res = PyRun_File(fp, filename, Py_file_input, globals, locals);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000850 Py_BEGIN_ALLOW_THREADS
Guido van Rossum0f61f8a1992-02-25 18:55:05 +0000851 fclose(fp);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000852 Py_END_ALLOW_THREADS
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000853 return res;
Guido van Rossum0f61f8a1992-02-25 18:55:05 +0000854}
855
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000856static char execfile_doc[] =
857"execfile(filename[, globals[, locals]])\n\
858\n\
859Read and execute a Python script from a file.\n\
860The globals and locals are dictionaries, defaulting to the current\n\
861globals and locals. If only globals is given, locals defaults to it.";
862
863
Guido van Rossum79f25d91997-04-29 20:08:16 +0000864static PyObject *
Guido van Rossum94390a41992-08-14 15:14:30 +0000865builtin_getattr(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000866 PyObject *self;
867 PyObject *args;
Guido van Rossum33894be1992-01-27 16:53:09 +0000868{
Guido van Rossum950ff291998-06-29 13:38:57 +0000869 PyObject *v, *result, *dflt = NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000870 PyObject *name;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000871
Guido van Rossum950ff291998-06-29 13:38:57 +0000872 if (!PyArg_ParseTuple(args, "OS|O:getattr", &v, &name, &dflt))
Guido van Rossum33894be1992-01-27 16:53:09 +0000873 return NULL;
Guido van Rossum950ff291998-06-29 13:38:57 +0000874 result = PyObject_GetAttr(v, name);
875 if (result == NULL && dflt != NULL) {
876 PyErr_Clear();
877 Py_INCREF(dflt);
878 result = dflt;
879 }
880 return result;
Guido van Rossum9bfef441993-03-29 10:43:31 +0000881}
882
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000883static char getattr_doc[] =
Guido van Rossum950ff291998-06-29 13:38:57 +0000884"getattr(object, name[, default]) -> value\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000885\n\
Guido van Rossum950ff291998-06-29 13:38:57 +0000886Get a named attribute from an object; getattr(x, 'y') is equivalent to x.y.\n\
887When a default argument is given, it is returned when the attribute doesn't\n\
888exist; without it, an exception is raised in that case.";
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000889
890
Guido van Rossum79f25d91997-04-29 20:08:16 +0000891static PyObject *
Guido van Rossum872537c1995-07-07 22:43:42 +0000892builtin_globals(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000893 PyObject *self;
894 PyObject *args;
Guido van Rossum872537c1995-07-07 22:43:42 +0000895{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000896 PyObject *d;
Guido van Rossum872537c1995-07-07 22:43:42 +0000897
Guido van Rossum43713e52000-02-29 13:59:29 +0000898 if (!PyArg_ParseTuple(args, ":globals"))
Guido van Rossum872537c1995-07-07 22:43:42 +0000899 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000900 d = PyEval_GetGlobals();
901 Py_INCREF(d);
Guido van Rossum872537c1995-07-07 22:43:42 +0000902 return d;
903}
904
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000905static char globals_doc[] =
906"globals() -> dictionary\n\
907\n\
908Return the dictionary containing the current scope's global variables.";
909
910
Guido van Rossum79f25d91997-04-29 20:08:16 +0000911static PyObject *
Guido van Rossum9bfef441993-03-29 10:43:31 +0000912builtin_hasattr(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000913 PyObject *self;
914 PyObject *args;
Guido van Rossum9bfef441993-03-29 10:43:31 +0000915{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000916 PyObject *v;
917 PyObject *name;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000918
Guido van Rossum79f25d91997-04-29 20:08:16 +0000919 if (!PyArg_ParseTuple(args, "OS:hasattr", &v, &name))
Guido van Rossum9bfef441993-03-29 10:43:31 +0000920 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000921 v = PyObject_GetAttr(v, name);
Guido van Rossum9bfef441993-03-29 10:43:31 +0000922 if (v == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000923 PyErr_Clear();
Guido van Rossum09df08a1998-05-22 00:51:39 +0000924 Py_INCREF(Py_False);
925 return Py_False;
Guido van Rossum9bfef441993-03-29 10:43:31 +0000926 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000927 Py_DECREF(v);
Guido van Rossum09df08a1998-05-22 00:51:39 +0000928 Py_INCREF(Py_True);
929 return Py_True;
Guido van Rossum33894be1992-01-27 16:53:09 +0000930}
931
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000932static char hasattr_doc[] =
933"hasattr(object, name) -> Boolean\n\
934\n\
935Return whether the object has an attribute with the given name.\n\
936(This is done by calling getattr(object, name) and catching exceptions.)";
937
938
Guido van Rossum79f25d91997-04-29 20:08:16 +0000939static PyObject *
Guido van Rossum5b722181993-03-30 17:46:03 +0000940builtin_id(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000941 PyObject *self;
942 PyObject *args;
Guido van Rossum5b722181993-03-30 17:46:03 +0000943{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000944 PyObject *v;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000945
Guido van Rossum79f25d91997-04-29 20:08:16 +0000946 if (!PyArg_ParseTuple(args, "O:id", &v))
Guido van Rossum5b722181993-03-30 17:46:03 +0000947 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000948 return PyInt_FromLong((long)v);
Guido van Rossum5b722181993-03-30 17:46:03 +0000949}
950
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000951static char id_doc[] =
952"id(object) -> integer\n\
953\n\
954Return the identity of an object. This is guaranteed to be unique among\n\
955simultaneously existing objects. (Hint: it's the object's memory address.)";
956
957
Guido van Rossum79f25d91997-04-29 20:08:16 +0000958static PyObject *
Guido van Rossum12d12c51993-10-26 17:58:25 +0000959builtin_map(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000960 PyObject *self;
961 PyObject *args;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000962{
963 typedef struct {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000964 PyObject *seq;
965 PySequenceMethods *sqf;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000966 int len;
967 } sequence;
968
Guido van Rossum79f25d91997-04-29 20:08:16 +0000969 PyObject *func, *result;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000970 sequence *seqs = NULL, *sqp;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000971 int n, len;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000972 register int i, j;
973
Guido van Rossum79f25d91997-04-29 20:08:16 +0000974 n = PyTuple_Size(args);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000975 if (n < 2) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000976 PyErr_SetString(PyExc_TypeError,
977 "map() requires at least two args");
Guido van Rossum12d12c51993-10-26 17:58:25 +0000978 return NULL;
979 }
980
Guido van Rossum79f25d91997-04-29 20:08:16 +0000981 func = PyTuple_GetItem(args, 0);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000982 n--;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000983
Guido van Rossumfa4ac711998-07-10 17:37:30 +0000984 if (func == Py_None && n == 1) {
985 /* map(None, S) is the same as list(S). */
986 return PySequence_List(PyTuple_GetItem(args, 1));
987 }
988
Guido van Rossum79f25d91997-04-29 20:08:16 +0000989 if ((seqs = PyMem_NEW(sequence, n)) == NULL) {
990 PyErr_NoMemory();
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000991 goto Fail_2;
992 }
Guido van Rossum12d12c51993-10-26 17:58:25 +0000993
Guido van Rossum2d951851994-08-29 12:52:16 +0000994 for (len = 0, i = 0, sqp = seqs; i < n; ++i, ++sqp) {
Guido van Rossum12d12c51993-10-26 17:58:25 +0000995 int curlen;
Guido van Rossum09df08a1998-05-22 00:51:39 +0000996 PySequenceMethods *sqf;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000997
Guido van Rossum79f25d91997-04-29 20:08:16 +0000998 if ((sqp->seq = PyTuple_GetItem(args, i + 1)) == NULL)
Guido van Rossum12d12c51993-10-26 17:58:25 +0000999 goto Fail_2;
1000
Guido van Rossum09df08a1998-05-22 00:51:39 +00001001 sqp->sqf = sqf = sqp->seq->ob_type->tp_as_sequence;
1002 if (sqf == NULL ||
1003 sqf->sq_length == NULL ||
1004 sqf->sq_item == NULL)
1005 {
Guido van Rossum12d12c51993-10-26 17:58:25 +00001006 static char errmsg[] =
1007 "argument %d to map() must be a sequence object";
Guido van Rossum15e33a41997-04-30 19:00:27 +00001008 char errbuf[sizeof(errmsg) + 25];
Guido van Rossum12d12c51993-10-26 17:58:25 +00001009
1010 sprintf(errbuf, errmsg, i+2);
Guido van Rossum79f25d91997-04-29 20:08:16 +00001011 PyErr_SetString(PyExc_TypeError, errbuf);
Guido van Rossum12d12c51993-10-26 17:58:25 +00001012 goto Fail_2;
1013 }
1014
1015 if ((curlen = sqp->len = (*sqp->sqf->sq_length)(sqp->seq)) < 0)
1016 goto Fail_2;
1017
1018 if (curlen > len)
1019 len = curlen;
1020 }
1021
Guido van Rossum79f25d91997-04-29 20:08:16 +00001022 if ((result = (PyObject *) PyList_New(len)) == NULL)
Guido van Rossum12d12c51993-10-26 17:58:25 +00001023 goto Fail_2;
1024
Guido van Rossum2d951851994-08-29 12:52:16 +00001025 for (i = 0; ; ++i) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001026 PyObject *alist, *item=NULL, *value;
Guido van Rossum2d951851994-08-29 12:52:16 +00001027 int any = 0;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001028
Guido van Rossum79f25d91997-04-29 20:08:16 +00001029 if (func == Py_None && n == 1)
Guido van Rossum32120311995-07-10 13:52:21 +00001030 alist = NULL;
Guido van Rossum2d951851994-08-29 12:52:16 +00001031 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001032 if ((alist = PyTuple_New(n)) == NULL)
Guido van Rossum2d951851994-08-29 12:52:16 +00001033 goto Fail_1;
1034 }
Guido van Rossum12d12c51993-10-26 17:58:25 +00001035
1036 for (j = 0, sqp = seqs; j < n; ++j, ++sqp) {
Guido van Rossum2d951851994-08-29 12:52:16 +00001037 if (sqp->len < 0) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001038 Py_INCREF(Py_None);
1039 item = Py_None;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001040 }
Guido van Rossum12d12c51993-10-26 17:58:25 +00001041 else {
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00001042 item = (*sqp->sqf->sq_item)(sqp->seq, i);
Guido van Rossum2d951851994-08-29 12:52:16 +00001043 if (item == NULL) {
Barry Warsawcde8b1b1997-08-22 21:14:38 +00001044 if (PyErr_ExceptionMatches(
1045 PyExc_IndexError))
1046 {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001047 PyErr_Clear();
1048 Py_INCREF(Py_None);
1049 item = Py_None;
Guido van Rossum2d951851994-08-29 12:52:16 +00001050 sqp->len = -1;
1051 }
1052 else {
1053 goto Fail_0;
1054 }
1055 }
1056 else
1057 any = 1;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001058
Guido van Rossum12d12c51993-10-26 17:58:25 +00001059 }
Guido van Rossum32120311995-07-10 13:52:21 +00001060 if (!alist)
Guido van Rossum2d951851994-08-29 12:52:16 +00001061 break;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001062 if (PyTuple_SetItem(alist, j, item) < 0) {
1063 Py_DECREF(item);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00001064 goto Fail_0;
Guido van Rossum2d951851994-08-29 12:52:16 +00001065 }
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00001066 continue;
1067
1068 Fail_0:
Guido van Rossum79f25d91997-04-29 20:08:16 +00001069 Py_XDECREF(alist);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00001070 goto Fail_1;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001071 }
1072
Guido van Rossum32120311995-07-10 13:52:21 +00001073 if (!alist)
1074 alist = item;
Guido van Rossum2d951851994-08-29 12:52:16 +00001075
1076 if (!any) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001077 Py_DECREF(alist);
Guido van Rossum2d951851994-08-29 12:52:16 +00001078 break;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001079 }
Guido van Rossum2d951851994-08-29 12:52:16 +00001080
Guido van Rossum79f25d91997-04-29 20:08:16 +00001081 if (func == Py_None)
Guido van Rossum32120311995-07-10 13:52:21 +00001082 value = alist;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001083 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001084 value = PyEval_CallObject(func, alist);
1085 Py_DECREF(alist);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00001086 if (value == NULL)
1087 goto Fail_1;
Guido van Rossum2d951851994-08-29 12:52:16 +00001088 }
1089 if (i >= len) {
Barry Warsawfa77e091999-01-28 18:49:12 +00001090 int status = PyList_Append(result, value);
Barry Warsaw21332871999-01-28 04:21:35 +00001091 Py_DECREF(value);
Barry Warsawfa77e091999-01-28 18:49:12 +00001092 if (status < 0)
1093 goto Fail_1;
Guido van Rossum2d951851994-08-29 12:52:16 +00001094 }
1095 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001096 if (PyList_SetItem(result, i, value) < 0)
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00001097 goto Fail_1;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001098 }
1099 }
1100
Guido van Rossumfa4ac711998-07-10 17:37:30 +00001101 if (i < len && PyList_SetSlice(result, i, len, NULL) < 0)
1102 goto Fail_1;
1103
Guido van Rossum79f25d91997-04-29 20:08:16 +00001104 PyMem_DEL(seqs);
Guido van Rossum12d12c51993-10-26 17:58:25 +00001105 return result;
1106
Guido van Rossum12d12c51993-10-26 17:58:25 +00001107Fail_1:
Guido van Rossum79f25d91997-04-29 20:08:16 +00001108 Py_DECREF(result);
Guido van Rossum12d12c51993-10-26 17:58:25 +00001109Fail_2:
Guido van Rossum79f25d91997-04-29 20:08:16 +00001110 if (seqs) PyMem_DEL(seqs);
Guido van Rossum12d12c51993-10-26 17:58:25 +00001111 return NULL;
1112}
1113
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001114static char map_doc[] =
1115"map(function, sequence[, sequence, ...]) -> list\n\
1116\n\
1117Return a list of the results of applying the function to the items of\n\
1118the argument sequence(s). If more than one sequence is given, the\n\
1119function is called with an argument list consisting of the corresponding\n\
1120item of each sequence, substituting None for missing values when not all\n\
1121sequences have the same length. If the function is None, return a list of\n\
1122the items of the sequence (or a list of tuples if more than one sequence).";
1123
1124
Guido van Rossum79f25d91997-04-29 20:08:16 +00001125static PyObject *
Guido van Rossum94390a41992-08-14 15:14:30 +00001126builtin_setattr(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001127 PyObject *self;
1128 PyObject *args;
Guido van Rossum33894be1992-01-27 16:53:09 +00001129{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001130 PyObject *v;
1131 PyObject *name;
1132 PyObject *value;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001133
Guido van Rossum79f25d91997-04-29 20:08:16 +00001134 if (!PyArg_ParseTuple(args, "OSO:setattr", &v, &name, &value))
Guido van Rossum33894be1992-01-27 16:53:09 +00001135 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001136 if (PyObject_SetAttr(v, name, value) != 0)
Guido van Rossum33894be1992-01-27 16:53:09 +00001137 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001138 Py_INCREF(Py_None);
1139 return Py_None;
Guido van Rossum33894be1992-01-27 16:53:09 +00001140}
1141
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001142static char setattr_doc[] =
1143"setattr(object, name, value)\n\
1144\n\
1145Set a named attribute on an object; setattr(x, 'y', v) is equivalent to\n\
1146``x.y = v''.";
1147
1148
Guido van Rossum79f25d91997-04-29 20:08:16 +00001149static PyObject *
Guido van Rossum14144fc1994-08-29 12:53:40 +00001150builtin_delattr(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001151 PyObject *self;
1152 PyObject *args;
Guido van Rossum14144fc1994-08-29 12:53:40 +00001153{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001154 PyObject *v;
1155 PyObject *name;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001156
Guido van Rossum79f25d91997-04-29 20:08:16 +00001157 if (!PyArg_ParseTuple(args, "OS:delattr", &v, &name))
Guido van Rossum14144fc1994-08-29 12:53:40 +00001158 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001159 if (PyObject_SetAttr(v, name, (PyObject *)NULL) != 0)
Guido van Rossum14144fc1994-08-29 12:53:40 +00001160 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001161 Py_INCREF(Py_None);
1162 return Py_None;
Guido van Rossum14144fc1994-08-29 12:53:40 +00001163}
1164
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001165static char delattr_doc[] =
Guido van Rossumdf12a591998-11-23 22:13:04 +00001166"delattr(object, name)\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001167\n\
1168Delete a named attribute on an object; delattr(x, 'y') is equivalent to\n\
1169``del x.y''.";
1170
1171
Guido van Rossum79f25d91997-04-29 20:08:16 +00001172static PyObject *
Guido van Rossum9bfef441993-03-29 10:43:31 +00001173builtin_hash(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001174 PyObject *self;
1175 PyObject *args;
Guido van Rossum9bfef441993-03-29 10:43:31 +00001176{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001177 PyObject *v;
Guido van Rossum9bfef441993-03-29 10:43:31 +00001178 long x;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001179
Guido van Rossum79f25d91997-04-29 20:08:16 +00001180 if (!PyArg_ParseTuple(args, "O:hash", &v))
Guido van Rossum9bfef441993-03-29 10:43:31 +00001181 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001182 x = PyObject_Hash(v);
Guido van Rossum9bfef441993-03-29 10:43:31 +00001183 if (x == -1)
1184 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001185 return PyInt_FromLong(x);
Guido van Rossum9bfef441993-03-29 10:43:31 +00001186}
1187
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001188static char hash_doc[] =
1189"hash(object) -> integer\n\
1190\n\
1191Return a hash value for the object. Two objects with the same value have\n\
1192the same hash value. The reverse is not necessarily true, but likely.";
1193
1194
Guido van Rossum79f25d91997-04-29 20:08:16 +00001195static PyObject *
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001196builtin_hex(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001197 PyObject *self;
1198 PyObject *args;
Guido van Rossum006bcd41991-10-24 14:54:44 +00001199{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001200 PyObject *v;
1201 PyNumberMethods *nb;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001202
Guido van Rossum79f25d91997-04-29 20:08:16 +00001203 if (!PyArg_ParseTuple(args, "O:hex", &v))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001204 return NULL;
Guido van Rossum1899c2e1992-09-12 11:09:23 +00001205
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001206 if ((nb = v->ob_type->tp_as_number) == NULL ||
Guido van Rossum1899c2e1992-09-12 11:09:23 +00001207 nb->nb_hex == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001208 PyErr_SetString(PyExc_TypeError,
Guido van Rossum1899c2e1992-09-12 11:09:23 +00001209 "hex() argument can't be converted to hex");
1210 return NULL;
Guido van Rossum006bcd41991-10-24 14:54:44 +00001211 }
Guido van Rossum1899c2e1992-09-12 11:09:23 +00001212 return (*nb->nb_hex)(v);
Guido van Rossum006bcd41991-10-24 14:54:44 +00001213}
1214
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001215static char hex_doc[] =
1216"hex(number) -> string\n\
1217\n\
1218Return the hexadecimal representation of an integer or long integer.";
1219
1220
Guido van Rossum79f25d91997-04-29 20:08:16 +00001221static PyObject *builtin_raw_input Py_PROTO((PyObject *, PyObject *));
Guido van Rossum3165fe61992-09-25 21:59:05 +00001222
Guido van Rossum79f25d91997-04-29 20:08:16 +00001223static PyObject *
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001224builtin_input(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001225 PyObject *self;
1226 PyObject *args;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001227{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001228 PyObject *line;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001229 char *str;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001230 PyObject *res;
1231 PyObject *globals, *locals;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001232
1233 line = builtin_raw_input(self, args);
Guido van Rossum3165fe61992-09-25 21:59:05 +00001234 if (line == NULL)
1235 return line;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001236 if (!PyArg_Parse(line, "s;embedded '\\0' in input line", &str))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001237 return NULL;
1238 while (*str == ' ' || *str == '\t')
1239 str++;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001240 globals = PyEval_GetGlobals();
1241 locals = PyEval_GetLocals();
1242 if (PyDict_GetItemString(globals, "__builtins__") == NULL) {
1243 if (PyDict_SetItemString(globals, "__builtins__",
1244 PyEval_GetBuiltins()) != 0)
Guido van Rossum6135a871995-01-09 17:53:26 +00001245 return NULL;
1246 }
Guido van Rossumb05a5c71997-05-07 17:46:13 +00001247 res = PyRun_String(str, Py_eval_input, globals, locals);
Guido van Rossum79f25d91997-04-29 20:08:16 +00001248 Py_DECREF(line);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001249 return res;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001250}
1251
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001252static char input_doc[] =
1253"input([prompt]) -> value\n\
1254\n\
1255Equivalent to eval(raw_input(prompt)).";
1256
1257
Guido van Rossume8811f81997-02-14 15:48:05 +00001258static PyObject *
1259builtin_intern(self, args)
1260 PyObject *self;
1261 PyObject *args;
1262{
1263 PyObject *s;
Guido van Rossum43713e52000-02-29 13:59:29 +00001264 if (!PyArg_ParseTuple(args, "S:intern", &s))
Guido van Rossume8811f81997-02-14 15:48:05 +00001265 return NULL;
1266 Py_INCREF(s);
1267 PyString_InternInPlace(&s);
1268 return s;
1269}
1270
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001271static char intern_doc[] =
1272"intern(string) -> string\n\
1273\n\
1274``Intern'' the given string. This enters the string in the (global)\n\
1275table of interned strings whose purpose is to speed up dictionary lookups.\n\
1276Return the string itself or the previously interned string object with the\n\
1277same value.";
1278
1279
Guido van Rossum79f25d91997-04-29 20:08:16 +00001280static PyObject *
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001281builtin_int(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001282 PyObject *self;
1283 PyObject *args;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001284{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001285 PyObject *v;
Barry Warsaw226ae6c1999-10-12 19:54:53 +00001286 int base = -909; /* unlikely! */
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001287
Barry Warsaw226ae6c1999-10-12 19:54:53 +00001288 if (!PyArg_ParseTuple(args, "O|i:int", &v, &base))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001289 return NULL;
Barry Warsaw226ae6c1999-10-12 19:54:53 +00001290 if (base == -909)
1291 return PyNumber_Int(v);
1292 else if (!PyString_Check(v)) {
1293 PyErr_SetString(PyExc_TypeError,
1294 "can't convert non-string with explicit base");
1295 return NULL;
1296 }
1297 return PyInt_FromString(PyString_AS_STRING(v), NULL, base);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001298}
1299
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001300static char int_doc[] =
Barry Warsaw226ae6c1999-10-12 19:54:53 +00001301"int(x[, base]) -> integer\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001302\n\
Barry Warsaw226ae6c1999-10-12 19:54:53 +00001303Convert a string or number to an integer, if possible. A floating point\n\
1304argument will be truncated towards zero (this does not include a string\n\
1305representation of a floating point number!) When converting a string, use\n\
1306the optional base. It is an error to supply a base when converting a\n\
1307non-string.";
1308
1309
1310static PyObject *
1311builtin_long(self, args)
1312 PyObject *self;
1313 PyObject *args;
1314{
1315 PyObject *v;
1316 int base = -909; /* unlikely! */
1317
1318 if (!PyArg_ParseTuple(args, "O|i:long", &v, &base))
1319 return NULL;
1320 if (base == -909)
1321 return PyNumber_Long(v);
1322 else if (!PyString_Check(v)) {
1323 PyErr_SetString(PyExc_TypeError,
1324 "can't convert non-string with explicit base");
1325 return NULL;
1326 }
1327 return PyLong_FromString(PyString_AS_STRING(v), NULL, base);
1328}
1329
1330static char long_doc[] =
1331"long(x) -> long integer\n\
1332long(x, base) -> long integer\n\
1333\n\
1334Convert a string or number to a long integer, if possible. A floating\n\
1335point argument will be truncated towards zero (this does not include a\n\
1336string representation of a floating point number!) When converting a\n\
1337string, use the given base. It is an error to supply a base when\n\
1338converting a non-string.";
1339
1340
1341static PyObject *
1342builtin_float(self, args)
1343 PyObject *self;
1344 PyObject *args;
1345{
1346 PyObject *v;
1347
1348 if (!PyArg_ParseTuple(args, "O:float", &v))
1349 return NULL;
1350 if (PyString_Check(v))
1351 return PyFloat_FromString(v, NULL);
1352 return PyNumber_Float(v);
1353}
1354
1355static char float_doc[] =
1356"float(x) -> floating point number\n\
1357\n\
1358Convert a string or number to a floating point number, if possible.";
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001359
1360
Guido van Rossum79f25d91997-04-29 20:08:16 +00001361static PyObject *
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001362builtin_len(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001363 PyObject *self;
1364 PyObject *args;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001365{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001366 PyObject *v;
Guido van Rossum8ea9f4d1998-06-29 22:26:50 +00001367 long res;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001368
Guido van Rossum79f25d91997-04-29 20:08:16 +00001369 if (!PyArg_ParseTuple(args, "O:len", &v))
Guido van Rossum3f5da241990-12-20 15:06:42 +00001370 return NULL;
Guido van Rossum8ea9f4d1998-06-29 22:26:50 +00001371 res = PyObject_Length(v);
1372 if (res < 0 && PyErr_Occurred())
1373 return NULL;
1374 return PyInt_FromLong(res);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001375}
1376
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001377static char len_doc[] =
1378"len(object) -> integer\n\
1379\n\
1380Return the number of items of a sequence or mapping.";
1381
1382
Guido van Rossum79f25d91997-04-29 20:08:16 +00001383static PyObject *
Guido van Rossumd1705771996-04-09 02:41:06 +00001384builtin_list(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001385 PyObject *self;
1386 PyObject *args;
Guido van Rossumd1705771996-04-09 02:41:06 +00001387{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001388 PyObject *v;
Guido van Rossumd1705771996-04-09 02:41:06 +00001389
Guido van Rossum79f25d91997-04-29 20:08:16 +00001390 if (!PyArg_ParseTuple(args, "O:list", &v))
Guido van Rossumd1705771996-04-09 02:41:06 +00001391 return NULL;
Guido van Rossum09df08a1998-05-22 00:51:39 +00001392 return PySequence_List(v);
Guido van Rossumd1705771996-04-09 02:41:06 +00001393}
1394
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001395static char list_doc[] =
1396"list(sequence) -> list\n\
1397\n\
1398Return a new list whose items are the same as those of the argument sequence.";
1399
Guido van Rossum8861b741996-07-30 16:49:37 +00001400
1401static PyObject *
1402builtin_slice(self, args)
1403 PyObject *self;
1404 PyObject *args;
1405{
Guido van Rossum09df08a1998-05-22 00:51:39 +00001406 PyObject *start, *stop, *step;
Guido van Rossum8861b741996-07-30 16:49:37 +00001407
Guido van Rossum09df08a1998-05-22 00:51:39 +00001408 start = stop = step = NULL;
Guido van Rossum8861b741996-07-30 16:49:37 +00001409
Guido van Rossum09df08a1998-05-22 00:51:39 +00001410 if (!PyArg_ParseTuple(args, "O|OO:slice", &start, &stop, &step))
1411 return NULL;
Guido van Rossum8861b741996-07-30 16:49:37 +00001412
Guido van Rossum09df08a1998-05-22 00:51:39 +00001413 /* This swapping of stop and start is to maintain similarity with
1414 range(). */
1415 if (stop == NULL) {
1416 stop = start;
1417 start = NULL;
1418 }
1419 return PySlice_New(start, stop, step);
Guido van Rossum8861b741996-07-30 16:49:37 +00001420}
1421
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001422static char slice_doc[] =
Fred Drake3d587441999-07-19 15:21:16 +00001423"slice([start,] stop[, step]) -> slice object\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001424\n\
1425Create a slice object. This is used for slicing by the Numeric extensions.";
1426
1427
Guido van Rossum79f25d91997-04-29 20:08:16 +00001428static PyObject *
Guido van Rossum872537c1995-07-07 22:43:42 +00001429builtin_locals(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001430 PyObject *self;
1431 PyObject *args;
Guido van Rossum872537c1995-07-07 22:43:42 +00001432{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001433 PyObject *d;
Guido van Rossum872537c1995-07-07 22:43:42 +00001434
Guido van Rossum43713e52000-02-29 13:59:29 +00001435 if (!PyArg_ParseTuple(args, ":locals"))
Guido van Rossum872537c1995-07-07 22:43:42 +00001436 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001437 d = PyEval_GetLocals();
1438 Py_INCREF(d);
Guido van Rossum872537c1995-07-07 22:43:42 +00001439 return d;
1440}
1441
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001442static char locals_doc[] =
1443"locals() -> dictionary\n\
1444\n\
1445Return the dictionary containing the current scope's local variables.";
1446
1447
Guido van Rossum79f25d91997-04-29 20:08:16 +00001448static PyObject *
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001449min_max(args, sign)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001450 PyObject *args;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001451 int sign;
1452{
Guido van Rossum2d951851994-08-29 12:52:16 +00001453 int i;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001454 PyObject *v, *w, *x;
1455 PySequenceMethods *sq;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001456
Guido van Rossum79f25d91997-04-29 20:08:16 +00001457 if (PyTuple_Size(args) > 1)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001458 v = args;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001459 else if (!PyArg_ParseTuple(args, "O:min/max", &v))
Guido van Rossum3f5da241990-12-20 15:06:42 +00001460 return NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001461 sq = v->ob_type->tp_as_sequence;
Guido van Rossum09df08a1998-05-22 00:51:39 +00001462 if (sq == NULL || sq->sq_item == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001463 PyErr_SetString(PyExc_TypeError,
1464 "min() or max() of non-sequence");
Guido van Rossum3f5da241990-12-20 15:06:42 +00001465 return NULL;
1466 }
Guido van Rossum2d951851994-08-29 12:52:16 +00001467 w = NULL;
1468 for (i = 0; ; i++) {
Guido van Rossum3f5da241990-12-20 15:06:42 +00001469 x = (*sq->sq_item)(v, i); /* Implies INCREF */
Guido van Rossum2d951851994-08-29 12:52:16 +00001470 if (x == NULL) {
Barry Warsawcde8b1b1997-08-22 21:14:38 +00001471 if (PyErr_ExceptionMatches(PyExc_IndexError)) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001472 PyErr_Clear();
Guido van Rossum2d951851994-08-29 12:52:16 +00001473 break;
1474 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00001475 Py_XDECREF(w);
Guido van Rossum2d951851994-08-29 12:52:16 +00001476 return NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001477 }
Guido van Rossum2d951851994-08-29 12:52:16 +00001478 if (w == NULL)
1479 w = x;
1480 else {
Guido van Rossumc8b6df91997-05-23 00:06:51 +00001481 int c = PyObject_Compare(x, w);
1482 if (c && PyErr_Occurred()) {
1483 Py_DECREF(x);
1484 Py_XDECREF(w);
1485 return NULL;
1486 }
1487 if (c * sign > 0) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001488 Py_DECREF(w);
Guido van Rossum2d951851994-08-29 12:52:16 +00001489 w = x;
1490 }
1491 else
Guido van Rossum79f25d91997-04-29 20:08:16 +00001492 Py_DECREF(x);
Guido van Rossum2d951851994-08-29 12:52:16 +00001493 }
Guido van Rossum3f5da241990-12-20 15:06:42 +00001494 }
Guido van Rossum2d951851994-08-29 12:52:16 +00001495 if (w == NULL)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001496 PyErr_SetString(PyExc_ValueError,
1497 "min() or max() of empty sequence");
Guido van Rossum3f5da241990-12-20 15:06:42 +00001498 return w;
1499}
1500
Guido van Rossum79f25d91997-04-29 20:08:16 +00001501static PyObject *
Guido van Rossum3f5da241990-12-20 15:06:42 +00001502builtin_min(self, v)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001503 PyObject *self;
1504 PyObject *v;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001505{
1506 return min_max(v, -1);
1507}
1508
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001509static char min_doc[] =
1510"min(sequence) -> value\n\
1511min(a, b, c, ...) -> value\n\
1512\n\
1513With a single sequence argument, return its smallest item.\n\
1514With two or more arguments, return the smallest argument.";
1515
1516
Guido van Rossum79f25d91997-04-29 20:08:16 +00001517static PyObject *
Guido van Rossum3f5da241990-12-20 15:06:42 +00001518builtin_max(self, v)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001519 PyObject *self;
1520 PyObject *v;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001521{
1522 return min_max(v, 1);
1523}
1524
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001525static char max_doc[] =
1526"max(sequence) -> value\n\
1527max(a, b, c, ...) -> value\n\
1528\n\
1529With a single sequence argument, return its largest item.\n\
1530With two or more arguments, return the largest argument.";
1531
1532
Guido van Rossum79f25d91997-04-29 20:08:16 +00001533static PyObject *
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001534builtin_oct(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001535 PyObject *self;
1536 PyObject *args;
Guido van Rossum006bcd41991-10-24 14:54:44 +00001537{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001538 PyObject *v;
1539 PyNumberMethods *nb;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001540
Guido van Rossum79f25d91997-04-29 20:08:16 +00001541 if (!PyArg_ParseTuple(args, "O:oct", &v))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001542 return NULL;
Guido van Rossum1899c2e1992-09-12 11:09:23 +00001543 if (v == NULL || (nb = v->ob_type->tp_as_number) == NULL ||
1544 nb->nb_oct == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001545 PyErr_SetString(PyExc_TypeError,
Guido van Rossum1899c2e1992-09-12 11:09:23 +00001546 "oct() argument can't be converted to oct");
1547 return NULL;
Guido van Rossum006bcd41991-10-24 14:54:44 +00001548 }
Guido van Rossum1899c2e1992-09-12 11:09:23 +00001549 return (*nb->nb_oct)(v);
Guido van Rossum006bcd41991-10-24 14:54:44 +00001550}
1551
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001552static char oct_doc[] =
1553"oct(number) -> string\n\
1554\n\
1555Return the octal representation of an integer or long integer.";
1556
1557
Guido van Rossum79f25d91997-04-29 20:08:16 +00001558static PyObject *
Guido van Rossum94390a41992-08-14 15:14:30 +00001559builtin_open(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001560 PyObject *self;
1561 PyObject *args;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001562{
Guido van Rossum2d951851994-08-29 12:52:16 +00001563 char *name;
1564 char *mode = "r";
1565 int bufsize = -1;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001566 PyObject *f;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001567
Guido van Rossum79f25d91997-04-29 20:08:16 +00001568 if (!PyArg_ParseTuple(args, "s|si:open", &name, &mode, &bufsize))
Guido van Rossum3f5da241990-12-20 15:06:42 +00001569 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001570 f = PyFile_FromString(name, mode);
Guido van Rossum2d951851994-08-29 12:52:16 +00001571 if (f != NULL)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001572 PyFile_SetBufSize(f, bufsize);
Guido van Rossum2d951851994-08-29 12:52:16 +00001573 return f;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001574}
1575
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001576static char open_doc[] =
1577"open(filename[, mode[, buffering]]) -> file object\n\
1578\n\
1579Open a file. The mode can be 'r', 'w' or 'a' for reading (default),\n\
1580writing or appending. The file will be created if it doesn't exist\n\
1581when opened for writing or appending; it will be truncated when\n\
1582opened for writing. Add a 'b' to the mode for binary files.\n\
1583Add a '+' to the mode to allow simultaneous reading and writing.\n\
1584If the buffering argument is given, 0 means unbuffered, 1 means line\n\
1585buffered, and larger numbers specify the buffer size.";
1586
1587
Guido van Rossum79f25d91997-04-29 20:08:16 +00001588static PyObject *
Guido van Rossum94390a41992-08-14 15:14:30 +00001589builtin_ord(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001590 PyObject *self;
1591 PyObject *args;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001592{
Guido van Rossum09095f32000-03-10 23:00:52 +00001593 PyObject *obj;
1594 long ord;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001595
Guido van Rossum09095f32000-03-10 23:00:52 +00001596 if (!PyArg_ParseTuple(args, "O:ord", &obj))
Guido van Rossum3f5da241990-12-20 15:06:42 +00001597 return NULL;
Guido van Rossum09095f32000-03-10 23:00:52 +00001598
1599 if (PyString_Check(obj) && PyString_GET_SIZE(obj) == 1)
1600 ord = (long)((unsigned char)*PyString_AS_STRING(obj));
1601 else if (PyUnicode_Check(obj) && PyUnicode_GET_SIZE(obj) == 1)
1602 ord = (long)*PyUnicode_AS_UNICODE(obj);
1603 else {
1604 PyErr_SetString(PyExc_TypeError,
1605 "expected a string or unicode character");
1606 return NULL;
1607 }
1608
1609 return PyInt_FromLong(ord);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001610}
1611
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001612static char ord_doc[] =
1613"ord(c) -> integer\n\
1614\n\
Guido van Rossum09095f32000-03-10 23:00:52 +00001615Return the integer ordinal of a one character [unicode] string.";
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001616
1617
Guido van Rossum79f25d91997-04-29 20:08:16 +00001618static PyObject *
Guido van Rossum6a00cd81995-01-07 12:39:01 +00001619builtin_pow(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001620 PyObject *self;
1621 PyObject *args;
Guido van Rossum6a00cd81995-01-07 12:39:01 +00001622{
Guido van Rossum09df08a1998-05-22 00:51:39 +00001623 PyObject *v, *w, *z = Py_None;
Guido van Rossum6a00cd81995-01-07 12:39:01 +00001624
Guido van Rossum79f25d91997-04-29 20:08:16 +00001625 if (!PyArg_ParseTuple(args, "OO|O:pow", &v, &w, &z))
Guido van Rossum6a00cd81995-01-07 12:39:01 +00001626 return NULL;
Guido van Rossum09df08a1998-05-22 00:51:39 +00001627 return PyNumber_Power(v, w, z);
Guido van Rossumd4905451991-05-05 20:00:36 +00001628}
1629
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001630static char pow_doc[] =
1631"pow(x, y[, z]) -> number\n\
1632\n\
1633With two arguments, equivalent to x**y. With three arguments,\n\
1634equivalent to (x**y) % z, but may be more efficient (e.g. for longs).";
1635
1636
Guido van Rossum124eff01999-02-23 16:11:01 +00001637/* Return number of items in range/xrange (lo, hi, step). step > 0
1638 * required. Return a value < 0 if & only if the true value is too
1639 * large to fit in a signed long.
1640 */
1641static long
1642get_len_of_range(lo, hi, step)
1643 long lo;
1644 long hi;
1645 long step; /* must be > 0 */
1646{
1647 /* -------------------------------------------------------------
1648 If lo >= hi, the range is empty.
1649 Else if n values are in the range, the last one is
1650 lo + (n-1)*step, which must be <= hi-1. Rearranging,
1651 n <= (hi - lo - 1)/step + 1, so taking the floor of the RHS gives
1652 the proper value. Since lo < hi in this case, hi-lo-1 >= 0, so
1653 the RHS is non-negative and so truncation is the same as the
1654 floor. Letting M be the largest positive long, the worst case
1655 for the RHS numerator is hi=M, lo=-M-1, and then
1656 hi-lo-1 = M-(-M-1)-1 = 2*M. Therefore unsigned long has enough
1657 precision to compute the RHS exactly.
1658 ---------------------------------------------------------------*/
1659 long n = 0;
1660 if (lo < hi) {
1661 unsigned long uhi = (unsigned long)hi;
1662 unsigned long ulo = (unsigned long)lo;
1663 unsigned long diff = uhi - ulo - 1;
1664 n = (long)(diff / (unsigned long)step + 1);
1665 }
1666 return n;
1667}
1668
Guido van Rossum79f25d91997-04-29 20:08:16 +00001669static PyObject *
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001670builtin_range(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001671 PyObject *self;
1672 PyObject *args;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001673{
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001674 long ilow = 0, ihigh = 0, istep = 1;
Guido van Rossum124eff01999-02-23 16:11:01 +00001675 long bign;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001676 int i, n;
Guido van Rossum124eff01999-02-23 16:11:01 +00001677
Guido van Rossum79f25d91997-04-29 20:08:16 +00001678 PyObject *v;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001679
Guido van Rossum79f25d91997-04-29 20:08:16 +00001680 if (PyTuple_Size(args) <= 1) {
1681 if (!PyArg_ParseTuple(args,
Guido van Rossum0865dd91995-01-17 16:30:22 +00001682 "l;range() requires 1-3 int arguments",
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001683 &ihigh))
1684 return NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001685 }
1686 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001687 if (!PyArg_ParseTuple(args,
Guido van Rossum0865dd91995-01-17 16:30:22 +00001688 "ll|l;range() requires 1-3 int arguments",
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001689 &ilow, &ihigh, &istep))
Guido van Rossum3f5da241990-12-20 15:06:42 +00001690 return NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001691 }
1692 if (istep == 0) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001693 PyErr_SetString(PyExc_ValueError, "zero step for range()");
Guido van Rossum3f5da241990-12-20 15:06:42 +00001694 return NULL;
1695 }
Guido van Rossum124eff01999-02-23 16:11:01 +00001696 if (istep > 0)
1697 bign = get_len_of_range(ilow, ihigh, istep);
1698 else
1699 bign = get_len_of_range(ihigh, ilow, -istep);
1700 n = (int)bign;
1701 if (bign < 0 || (long)n != bign) {
Guido van Rossume23cde21999-01-12 05:07:47 +00001702 PyErr_SetString(PyExc_OverflowError,
Guido van Rossum124eff01999-02-23 16:11:01 +00001703 "range() has too many items");
Guido van Rossume23cde21999-01-12 05:07:47 +00001704 return NULL;
1705 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00001706 v = PyList_New(n);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001707 if (v == NULL)
1708 return NULL;
1709 for (i = 0; i < n; i++) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001710 PyObject *w = PyInt_FromLong(ilow);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001711 if (w == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001712 Py_DECREF(v);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001713 return NULL;
1714 }
Guido van Rossuma937d141998-04-24 18:22:02 +00001715 PyList_SET_ITEM(v, i, w);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001716 ilow += istep;
1717 }
1718 return v;
1719}
1720
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001721static char range_doc[] =
1722"range([start,] stop[, step]) -> list of integers\n\
1723\n\
1724Return a list containing an arithmetic progression of integers.\n\
1725range(i, j) returns [i, i+1, i+2, ..., j-1]; start (!) defaults to 0.\n\
1726When step is given, it specifies the increment (or decrement).\n\
1727For example, range(4) returns [0, 1, 2, 3]. The end point is omitted!\n\
1728These are exactly the valid indices for a list of 4 elements.";
1729
1730
Guido van Rossum79f25d91997-04-29 20:08:16 +00001731static PyObject *
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001732builtin_xrange(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001733 PyObject *self;
1734 PyObject *args;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001735{
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001736 long ilow = 0, ihigh = 0, istep = 1;
Guido van Rossum0865dd91995-01-17 16:30:22 +00001737 long n;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001738
Guido van Rossum79f25d91997-04-29 20:08:16 +00001739 if (PyTuple_Size(args) <= 1) {
1740 if (!PyArg_ParseTuple(args,
Guido van Rossum0865dd91995-01-17 16:30:22 +00001741 "l;xrange() requires 1-3 int arguments",
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001742 &ihigh))
1743 return NULL;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001744 }
1745 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001746 if (!PyArg_ParseTuple(args,
Guido van Rossum0865dd91995-01-17 16:30:22 +00001747 "ll|l;xrange() requires 1-3 int arguments",
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001748 &ilow, &ihigh, &istep))
Guido van Rossum12d12c51993-10-26 17:58:25 +00001749 return NULL;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001750 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001751 if (istep == 0) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001752 PyErr_SetString(PyExc_ValueError, "zero step for xrange()");
Guido van Rossum12d12c51993-10-26 17:58:25 +00001753 return NULL;
1754 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001755 if (istep > 0)
Guido van Rossum124eff01999-02-23 16:11:01 +00001756 n = get_len_of_range(ilow, ihigh, istep);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001757 else
Guido van Rossum124eff01999-02-23 16:11:01 +00001758 n = get_len_of_range(ihigh, ilow, -istep);
1759 if (n < 0) {
1760 PyErr_SetString(PyExc_OverflowError,
1761 "xrange() has more than sys.maxint items");
1762 return NULL;
1763 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00001764 return PyRange_New(ilow, n, istep, 1);
Guido van Rossum12d12c51993-10-26 17:58:25 +00001765}
1766
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001767static char xrange_doc[] =
1768"xrange([start,] stop[, step]) -> xrange object\n\
1769\n\
1770Like range(), but instead of returning a list, returns an object that\n\
1771generates the numbers in the range on demand. This is slightly slower\n\
1772than range() but more memory efficient.";
1773
1774
Guido van Rossum79f25d91997-04-29 20:08:16 +00001775static PyObject *
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001776builtin_raw_input(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001777 PyObject *self;
1778 PyObject *args;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001779{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001780 PyObject *v = NULL;
1781 PyObject *f;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001782
Guido van Rossum79f25d91997-04-29 20:08:16 +00001783 if (!PyArg_ParseTuple(args, "|O:[raw_]input", &v))
Guido van Rossum3165fe61992-09-25 21:59:05 +00001784 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001785 if (PyFile_AsFile(PySys_GetObject("stdin")) == stdin &&
1786 PyFile_AsFile(PySys_GetObject("stdout")) == stdout &&
Guido van Rossum53bb7ff1995-07-26 16:26:31 +00001787 isatty(fileno(stdin)) && isatty(fileno(stdout))) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001788 PyObject *po;
Guido van Rossum872537c1995-07-07 22:43:42 +00001789 char *prompt;
1790 char *s;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001791 PyObject *result;
Guido van Rossum872537c1995-07-07 22:43:42 +00001792 if (v != NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001793 po = PyObject_Str(v);
Guido van Rossum872537c1995-07-07 22:43:42 +00001794 if (po == NULL)
1795 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001796 prompt = PyString_AsString(po);
Guido van Rossumd9b52081998-06-26 18:25:38 +00001797 if (prompt == NULL)
1798 return NULL;
Guido van Rossum872537c1995-07-07 22:43:42 +00001799 }
1800 else {
1801 po = NULL;
1802 prompt = "";
1803 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00001804 s = PyOS_Readline(prompt);
1805 Py_XDECREF(po);
Guido van Rossum872537c1995-07-07 22:43:42 +00001806 if (s == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001807 PyErr_SetNone(PyExc_KeyboardInterrupt);
Guido van Rossum872537c1995-07-07 22:43:42 +00001808 return NULL;
1809 }
1810 if (*s == '\0') {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001811 PyErr_SetNone(PyExc_EOFError);
Guido van Rossum872537c1995-07-07 22:43:42 +00001812 result = NULL;
1813 }
1814 else { /* strip trailing '\n' */
Guido van Rossum79f25d91997-04-29 20:08:16 +00001815 result = PyString_FromStringAndSize(s, strlen(s)-1);
Guido van Rossum872537c1995-07-07 22:43:42 +00001816 }
1817 free(s);
1818 return result;
1819 }
Guido van Rossum90933611991-06-07 16:10:43 +00001820 if (v != NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001821 f = PySys_GetObject("stdout");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001822 if (f == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001823 PyErr_SetString(PyExc_RuntimeError, "lost sys.stdout");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001824 return NULL;
1825 }
Guido van Rossumc8b6df91997-05-23 00:06:51 +00001826 if (Py_FlushLine() != 0 ||
1827 PyFile_WriteObject(v, f, Py_PRINT_RAW) != 0)
Guido van Rossum90933611991-06-07 16:10:43 +00001828 return NULL;
1829 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00001830 f = PySys_GetObject("stdin");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001831 if (f == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001832 PyErr_SetString(PyExc_RuntimeError, "lost sys.stdin");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001833 return NULL;
1834 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00001835 return PyFile_GetLine(f, -1);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001836}
1837
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001838static char raw_input_doc[] =
1839"raw_input([prompt]) -> string\n\
1840\n\
1841Read a string from standard input. The trailing newline is stripped.\n\
1842If the user hits EOF (Unix: Ctl-D, Windows: Ctl-Z+Return), raise EOFError.\n\
1843On Unix, GNU readline is used if enabled. The prompt string, if given,\n\
1844is printed without a trailing newline before reading.";
1845
1846
Guido van Rossum79f25d91997-04-29 20:08:16 +00001847static PyObject *
Guido van Rossum12d12c51993-10-26 17:58:25 +00001848builtin_reduce(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001849 PyObject *self;
1850 PyObject *args;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001851{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001852 PyObject *seq, *func, *result = NULL;
1853 PySequenceMethods *sqf;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001854 register int i;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001855
Guido van Rossum79f25d91997-04-29 20:08:16 +00001856 if (!PyArg_ParseTuple(args, "OO|O:reduce", &func, &seq, &result))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001857 return NULL;
1858 if (result != NULL)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001859 Py_INCREF(result);
Guido van Rossum12d12c51993-10-26 17:58:25 +00001860
Guido van Rossum09df08a1998-05-22 00:51:39 +00001861 sqf = seq->ob_type->tp_as_sequence;
1862 if (sqf == NULL || sqf->sq_item == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001863 PyErr_SetString(PyExc_TypeError,
Guido van Rossum12d12c51993-10-26 17:58:25 +00001864 "2nd argument to reduce() must be a sequence object");
1865 return NULL;
1866 }
1867
Guido van Rossum79f25d91997-04-29 20:08:16 +00001868 if ((args = PyTuple_New(2)) == NULL)
Guido van Rossum2d951851994-08-29 12:52:16 +00001869 goto Fail;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001870
Guido van Rossum2d951851994-08-29 12:52:16 +00001871 for (i = 0; ; ++i) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001872 PyObject *op2;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001873
1874 if (args->ob_refcnt > 1) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001875 Py_DECREF(args);
1876 if ((args = PyTuple_New(2)) == NULL)
Guido van Rossum2d951851994-08-29 12:52:16 +00001877 goto Fail;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001878 }
1879
Guido van Rossum2d951851994-08-29 12:52:16 +00001880 if ((op2 = (*sqf->sq_item)(seq, i)) == NULL) {
Barry Warsawcde8b1b1997-08-22 21:14:38 +00001881 if (PyErr_ExceptionMatches(PyExc_IndexError)) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001882 PyErr_Clear();
Guido van Rossum2d951851994-08-29 12:52:16 +00001883 break;
1884 }
1885 goto Fail;
1886 }
Guido van Rossum12d12c51993-10-26 17:58:25 +00001887
Guido van Rossum2d951851994-08-29 12:52:16 +00001888 if (result == NULL)
1889 result = op2;
1890 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001891 PyTuple_SetItem(args, 0, result);
1892 PyTuple_SetItem(args, 1, op2);
1893 if ((result = PyEval_CallObject(func, args)) == NULL)
Guido van Rossum2d951851994-08-29 12:52:16 +00001894 goto Fail;
1895 }
Guido van Rossum12d12c51993-10-26 17:58:25 +00001896 }
1897
Guido van Rossum79f25d91997-04-29 20:08:16 +00001898 Py_DECREF(args);
Guido van Rossum12d12c51993-10-26 17:58:25 +00001899
Guido van Rossum2d951851994-08-29 12:52:16 +00001900 if (result == NULL)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001901 PyErr_SetString(PyExc_TypeError,
Guido van Rossum2d951851994-08-29 12:52:16 +00001902 "reduce of empty sequence with no initial value");
1903
Guido van Rossum12d12c51993-10-26 17:58:25 +00001904 return result;
1905
Guido van Rossum2d951851994-08-29 12:52:16 +00001906Fail:
Guido van Rossum79f25d91997-04-29 20:08:16 +00001907 Py_XDECREF(args);
1908 Py_XDECREF(result);
Guido van Rossum12d12c51993-10-26 17:58:25 +00001909 return NULL;
1910}
1911
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001912static char reduce_doc[] =
1913"reduce(function, sequence[, initial]) -> value\n\
1914\n\
1915Apply a function of two arguments cumulatively to the items of a sequence,\n\
1916from left to right, so as to reduce the sequence to a single value.\n\
1917For example, reduce(lambda x, y: x+y, [1, 2, 3, 4, 5]) calculates\n\
1918((((1+2)+3)+4)+5). If initial is present, it is placed before the items\n\
1919of the sequence in the calculation, and serves as a default when the\n\
1920sequence is empty.";
1921
1922
Guido van Rossum79f25d91997-04-29 20:08:16 +00001923static PyObject *
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001924builtin_reload(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001925 PyObject *self;
1926 PyObject *args;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001927{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001928 PyObject *v;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001929
Guido van Rossum79f25d91997-04-29 20:08:16 +00001930 if (!PyArg_ParseTuple(args, "O:reload", &v))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001931 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001932 return PyImport_ReloadModule(v);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001933}
1934
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001935static char reload_doc[] =
1936"reload(module) -> module\n\
1937\n\
1938Reload the module. The module must have been successfully imported before.";
1939
1940
Guido van Rossum79f25d91997-04-29 20:08:16 +00001941static PyObject *
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001942builtin_repr(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001943 PyObject *self;
1944 PyObject *args;
Guido van Rossumc89705d1992-11-26 08:54:07 +00001945{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001946 PyObject *v;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001947
Guido van Rossum79f25d91997-04-29 20:08:16 +00001948 if (!PyArg_ParseTuple(args, "O:repr", &v))
Guido van Rossumc89705d1992-11-26 08:54:07 +00001949 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001950 return PyObject_Repr(v);
Guido van Rossumc89705d1992-11-26 08:54:07 +00001951}
1952
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001953static char repr_doc[] =
1954"repr(object) -> string\n\
1955\n\
1956Return the canonical string representation of the object.\n\
1957For most object types, eval(repr(object)) == object.";
1958
1959
Guido van Rossum79f25d91997-04-29 20:08:16 +00001960static PyObject *
Guido van Rossum9e51f9b1993-02-12 16:29:05 +00001961builtin_round(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001962 PyObject *self;
1963 PyObject *args;
Guido van Rossum9e51f9b1993-02-12 16:29:05 +00001964{
Guido van Rossum9e51f9b1993-02-12 16:29:05 +00001965 double x;
1966 double f;
1967 int ndigits = 0;
Guido van Rossum9e51f9b1993-02-12 16:29:05 +00001968 int i;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001969
Guido van Rossum79f25d91997-04-29 20:08:16 +00001970 if (!PyArg_ParseTuple(args, "d|i:round", &x, &ndigits))
Guido van Rossum9e51f9b1993-02-12 16:29:05 +00001971 return NULL;
Guido van Rossum9e51f9b1993-02-12 16:29:05 +00001972 f = 1.0;
Guido van Rossum1e162d31998-05-09 14:42:25 +00001973 i = abs(ndigits);
1974 while (--i >= 0)
Guido van Rossum9e51f9b1993-02-12 16:29:05 +00001975 f = f*10.0;
Guido van Rossum1e162d31998-05-09 14:42:25 +00001976 if (ndigits < 0)
1977 x /= f;
Guido van Rossum9e51f9b1993-02-12 16:29:05 +00001978 else
Guido van Rossum1e162d31998-05-09 14:42:25 +00001979 x *= f;
1980 if (x >= 0.0)
1981 x = floor(x + 0.5);
1982 else
1983 x = ceil(x - 0.5);
1984 if (ndigits < 0)
1985 x *= f;
1986 else
1987 x /= f;
1988 return PyFloat_FromDouble(x);
Guido van Rossum9e51f9b1993-02-12 16:29:05 +00001989}
1990
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001991static char round_doc[] =
1992"round(number[, ndigits]) -> floating point number\n\
1993\n\
1994Round a number to a given precision in decimal digits (default 0 digits).\n\
1995This always returns a floating point number. Precision may be negative.";
1996
1997
Guido van Rossum79f25d91997-04-29 20:08:16 +00001998static PyObject *
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001999builtin_str(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00002000 PyObject *self;
2001 PyObject *args;
Guido van Rossumc89705d1992-11-26 08:54:07 +00002002{
Guido van Rossum79f25d91997-04-29 20:08:16 +00002003 PyObject *v;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002004
Guido van Rossum79f25d91997-04-29 20:08:16 +00002005 if (!PyArg_ParseTuple(args, "O:str", &v))
Guido van Rossumc89705d1992-11-26 08:54:07 +00002006 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002007 return PyObject_Str(v);
Guido van Rossumc89705d1992-11-26 08:54:07 +00002008}
2009
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002010static char str_doc[] =
2011"str(object) -> string\n\
2012\n\
2013Return a nice string representation of the object.\n\
2014If the argument is a string, the return value is the same object.";
2015
2016
Guido van Rossum79f25d91997-04-29 20:08:16 +00002017static PyObject *
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002018builtin_tuple(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00002019 PyObject *self;
2020 PyObject *args;
Guido van Rossumcae027b1994-08-29 12:53:11 +00002021{
Guido van Rossum79f25d91997-04-29 20:08:16 +00002022 PyObject *v;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002023
Guido van Rossum79f25d91997-04-29 20:08:16 +00002024 if (!PyArg_ParseTuple(args, "O:tuple", &v))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002025 return NULL;
Guido van Rossum09df08a1998-05-22 00:51:39 +00002026 return PySequence_Tuple(v);
Guido van Rossumcae027b1994-08-29 12:53:11 +00002027}
2028
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002029static char tuple_doc[] =
2030"tuple(sequence) -> list\n\
2031\n\
2032Return a tuple whose items are the same as those of the argument sequence.\n\
2033If the argument is a tuple, the return value is the same object.";
2034
2035
Guido van Rossum79f25d91997-04-29 20:08:16 +00002036static PyObject *
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002037builtin_type(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00002038 PyObject *self;
2039 PyObject *args;
Guido van Rossum3f5da241990-12-20 15:06:42 +00002040{
Guido van Rossum79f25d91997-04-29 20:08:16 +00002041 PyObject *v;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002042
Guido van Rossum79f25d91997-04-29 20:08:16 +00002043 if (!PyArg_ParseTuple(args, "O:type", &v))
Guido van Rossum3f5da241990-12-20 15:06:42 +00002044 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002045 v = (PyObject *)v->ob_type;
2046 Py_INCREF(v);
Guido van Rossum3f5da241990-12-20 15:06:42 +00002047 return v;
2048}
2049
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002050static char type_doc[] =
2051"type(object) -> type object\n\
2052\n\
2053Return the type of the object.";
2054
2055
Guido van Rossum79f25d91997-04-29 20:08:16 +00002056static PyObject *
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002057builtin_vars(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00002058 PyObject *self;
2059 PyObject *args;
Guido van Rossum2d951851994-08-29 12:52:16 +00002060{
Guido van Rossum79f25d91997-04-29 20:08:16 +00002061 PyObject *v = NULL;
2062 PyObject *d;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002063
Guido van Rossum79f25d91997-04-29 20:08:16 +00002064 if (!PyArg_ParseTuple(args, "|O:vars", &v))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002065 return NULL;
Guido van Rossum2d951851994-08-29 12:52:16 +00002066 if (v == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002067 d = PyEval_GetLocals();
Guido van Rossum53bb7ff1995-07-26 16:26:31 +00002068 if (d == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002069 if (!PyErr_Occurred())
2070 PyErr_SetString(PyExc_SystemError,
2071 "no locals!?");
Guido van Rossum53bb7ff1995-07-26 16:26:31 +00002072 }
2073 else
Guido van Rossum79f25d91997-04-29 20:08:16 +00002074 Py_INCREF(d);
Guido van Rossum2d951851994-08-29 12:52:16 +00002075 }
2076 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002077 d = PyObject_GetAttrString(v, "__dict__");
Guido van Rossum2d951851994-08-29 12:52:16 +00002078 if (d == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002079 PyErr_SetString(PyExc_TypeError,
Guido van Rossum2d951851994-08-29 12:52:16 +00002080 "vars() argument must have __dict__ attribute");
2081 return NULL;
2082 }
2083 }
2084 return d;
2085}
2086
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002087static char vars_doc[] =
2088"vars([object]) -> dictionary\n\
2089\n\
2090Without arguments, equivalent to locals().\n\
2091With an argument, equivalent to object.__dict__.";
2092
Guido van Rossum668213d1999-06-16 17:28:37 +00002093static int
2094abstract_issubclass(derived, cls, err, first)
2095 PyObject *derived;
2096 PyObject *cls;
2097 char *err;
2098 int first;
2099{
2100 static PyObject *__bases__ = NULL;
2101 PyObject *bases;
Guido van Rossum7f851861999-06-17 19:12:39 +00002102 int i, n;
Guido van Rossum668213d1999-06-16 17:28:37 +00002103 int r = 0;
2104
2105 if (__bases__ == NULL) {
2106 __bases__ = PyString_FromString("__bases__");
2107 if (__bases__ == NULL)
2108 return -1;
2109 }
2110
2111 if (first) {
2112 bases = PyObject_GetAttr(cls, __bases__);
2113 if (bases == NULL || !PyTuple_Check(bases)) {
2114 Py_XDECREF(bases);
2115 PyErr_SetString(PyExc_TypeError, err);
2116 return -1;
2117 }
2118 Py_DECREF(bases);
2119 }
2120
2121 if (derived == cls)
2122 return 1;
2123
2124 bases = PyObject_GetAttr(derived, __bases__);
2125 if (bases == NULL || !PyTuple_Check(bases)) {
2126 Py_XDECREF(bases);
2127 PyErr_SetString(PyExc_TypeError, err);
2128 return -1;
2129 }
2130
2131 n = PyTuple_GET_SIZE(bases);
2132 for (i = 0; i < n; i++) {
2133 r = abstract_issubclass(PyTuple_GET_ITEM(bases, i),
2134 cls, err, 0);
2135 if (r != 0)
2136 break;
2137 }
2138
2139 Py_DECREF(bases);
2140
2141 return r;
2142}
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002143
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002144static PyObject *
2145builtin_isinstance(self, args)
2146 PyObject *self;
2147 PyObject *args;
2148{
2149 PyObject *inst;
2150 PyObject *cls;
Guido van Rossum668213d1999-06-16 17:28:37 +00002151 PyObject *icls;
2152 static PyObject *__class__ = NULL;
2153 int retval = 0;
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002154
Guido van Rossum43713e52000-02-29 13:59:29 +00002155 if (!PyArg_ParseTuple(args, "OO:isinstance", &inst, &cls))
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002156 return NULL;
Guido van Rossumf5dd9141997-12-02 19:11:45 +00002157
Guido van Rossum668213d1999-06-16 17:28:37 +00002158 if (PyClass_Check(cls)) {
2159 if (PyInstance_Check(inst)) {
Guido van Rossumf5dd9141997-12-02 19:11:45 +00002160 PyObject *inclass =
2161 (PyObject*)((PyInstanceObject*)inst)->in_class;
2162 retval = PyClass_IsSubclass(inclass, cls);
2163 }
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002164 }
Guido van Rossum668213d1999-06-16 17:28:37 +00002165 else if (PyType_Check(cls)) {
2166 retval = ((PyObject *)(inst->ob_type) == cls);
2167 }
2168 else if (!PyInstance_Check(inst)) {
2169 if (__class__ == NULL) {
2170 __class__ = PyString_FromString("__class__");
2171 if (__class__ == NULL)
2172 return NULL;
2173 }
2174 icls = PyObject_GetAttr(inst, __class__);
2175 if (icls != NULL) {
2176 retval = abstract_issubclass(
2177 icls, cls,
2178 "second argument must be a class",
2179 1);
2180 Py_DECREF(icls);
2181 if (retval < 0)
2182 return NULL;
2183 }
2184 else {
2185 PyErr_SetString(PyExc_TypeError,
2186 "second argument must be a class");
2187 return NULL;
2188 }
2189 }
2190 else {
2191 PyErr_SetString(PyExc_TypeError,
2192 "second argument must be a class");
2193 return NULL;
2194 }
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002195 return PyInt_FromLong(retval);
2196}
2197
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002198static char isinstance_doc[] =
2199"isinstance(object, class-or-type) -> Boolean\n\
2200\n\
2201Return whether an object is an instance of a class or of a subclass thereof.\n\
2202With a type as second argument, return whether that is the object's type.";
2203
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002204
2205static PyObject *
2206builtin_issubclass(self, args)
2207 PyObject *self;
2208 PyObject *args;
2209{
2210 PyObject *derived;
2211 PyObject *cls;
2212 int retval;
2213
Guido van Rossum43713e52000-02-29 13:59:29 +00002214 if (!PyArg_ParseTuple(args, "OO:issubclass", &derived, &cls))
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002215 return NULL;
Guido van Rossum668213d1999-06-16 17:28:37 +00002216
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002217 if (!PyClass_Check(derived) || !PyClass_Check(cls)) {
Guido van Rossum668213d1999-06-16 17:28:37 +00002218 retval = abstract_issubclass(
2219 derived, cls, "arguments must be classes", 1);
2220 if (retval < 0)
2221 return NULL;
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002222 }
Guido van Rossum668213d1999-06-16 17:28:37 +00002223 else {
2224 /* shortcut */
2225 if (!(retval = (derived == cls)))
2226 retval = PyClass_IsSubclass(derived, cls);
2227 }
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002228
2229 return PyInt_FromLong(retval);
2230}
2231
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002232static char issubclass_doc[] =
2233"issubclass(C, B) -> Boolean\n\
2234\n\
2235Return whether class C is a subclass (i.e., a derived class) of class B.";
2236
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002237
Guido van Rossum79f25d91997-04-29 20:08:16 +00002238static PyMethodDef builtin_methods[] = {
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002239 {"__import__", builtin___import__, 1, import_doc},
2240 {"abs", builtin_abs, 1, abs_doc},
2241 {"apply", builtin_apply, 1, apply_doc},
Guido van Rossum0daf0221999-03-19 19:07:19 +00002242 {"buffer", builtin_buffer, 1, buffer_doc},
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002243 {"callable", builtin_callable, 1, callable_doc},
2244 {"chr", builtin_chr, 1, chr_doc},
2245 {"cmp", builtin_cmp, 1, cmp_doc},
2246 {"coerce", builtin_coerce, 1, coerce_doc},
2247 {"compile", builtin_compile, 1, compile_doc},
Guido van Rossum8a5c5d21996-01-12 01:09:56 +00002248#ifndef WITHOUT_COMPLEX
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002249 {"complex", builtin_complex, 1, complex_doc},
Guido van Rossum8a5c5d21996-01-12 01:09:56 +00002250#endif
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002251 {"delattr", builtin_delattr, 1, delattr_doc},
2252 {"dir", builtin_dir, 1, dir_doc},
2253 {"divmod", builtin_divmod, 1, divmod_doc},
2254 {"eval", builtin_eval, 1, eval_doc},
2255 {"execfile", builtin_execfile, 1, execfile_doc},
2256 {"filter", builtin_filter, 1, filter_doc},
2257 {"float", builtin_float, 1, float_doc},
2258 {"getattr", builtin_getattr, 1, getattr_doc},
2259 {"globals", builtin_globals, 1, globals_doc},
2260 {"hasattr", builtin_hasattr, 1, hasattr_doc},
2261 {"hash", builtin_hash, 1, hash_doc},
2262 {"hex", builtin_hex, 1, hex_doc},
2263 {"id", builtin_id, 1, id_doc},
2264 {"input", builtin_input, 1, input_doc},
2265 {"intern", builtin_intern, 1, intern_doc},
2266 {"int", builtin_int, 1, int_doc},
2267 {"isinstance", builtin_isinstance, 1, isinstance_doc},
2268 {"issubclass", builtin_issubclass, 1, issubclass_doc},
2269 {"len", builtin_len, 1, len_doc},
2270 {"list", builtin_list, 1, list_doc},
2271 {"locals", builtin_locals, 1, locals_doc},
2272 {"long", builtin_long, 1, long_doc},
2273 {"map", builtin_map, 1, map_doc},
2274 {"max", builtin_max, 1, max_doc},
2275 {"min", builtin_min, 1, min_doc},
2276 {"oct", builtin_oct, 1, oct_doc},
2277 {"open", builtin_open, 1, open_doc},
2278 {"ord", builtin_ord, 1, ord_doc},
2279 {"pow", builtin_pow, 1, pow_doc},
2280 {"range", builtin_range, 1, range_doc},
2281 {"raw_input", builtin_raw_input, 1, raw_input_doc},
2282 {"reduce", builtin_reduce, 1, reduce_doc},
2283 {"reload", builtin_reload, 1, reload_doc},
2284 {"repr", builtin_repr, 1, repr_doc},
2285 {"round", builtin_round, 1, round_doc},
2286 {"setattr", builtin_setattr, 1, setattr_doc},
2287 {"slice", builtin_slice, 1, slice_doc},
2288 {"str", builtin_str, 1, str_doc},
2289 {"tuple", builtin_tuple, 1, tuple_doc},
2290 {"type", builtin_type, 1, type_doc},
Guido van Rossum09095f32000-03-10 23:00:52 +00002291 {"unicode", builtin_unicode, 1, unicode_doc},
2292 {"unichr", builtin_unichr, 1, unichr_doc},
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002293 {"vars", builtin_vars, 1, vars_doc},
2294 {"xrange", builtin_xrange, 1, xrange_doc},
Guido van Rossumc02e15c1991-12-16 13:03:00 +00002295 {NULL, NULL},
Guido van Rossum3f5da241990-12-20 15:06:42 +00002296};
2297
Guido van Rossum3f5da241990-12-20 15:06:42 +00002298/* Predefined exceptions */
2299
Guido van Rossum04748321997-09-16 18:43:15 +00002300PyObject *PyExc_Exception;
Barry Warsaw757af0e1997-08-29 22:13:51 +00002301PyObject *PyExc_StandardError;
Barry Warsaw412cdc21997-09-16 21:51:14 +00002302PyObject *PyExc_ArithmeticError;
Barry Warsaw757af0e1997-08-29 22:13:51 +00002303PyObject *PyExc_LookupError;
2304
Guido van Rossum79f25d91997-04-29 20:08:16 +00002305PyObject *PyExc_AssertionError;
2306PyObject *PyExc_AttributeError;
2307PyObject *PyExc_EOFError;
Guido van Rossumb6a7f771997-05-09 03:03:23 +00002308PyObject *PyExc_FloatingPointError;
Barry Warsawd086a1a1998-07-23 15:59:57 +00002309PyObject *PyExc_EnvironmentError;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002310PyObject *PyExc_IOError;
Barry Warsawd086a1a1998-07-23 15:59:57 +00002311PyObject *PyExc_OSError;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002312PyObject *PyExc_ImportError;
2313PyObject *PyExc_IndexError;
2314PyObject *PyExc_KeyError;
2315PyObject *PyExc_KeyboardInterrupt;
2316PyObject *PyExc_MemoryError;
2317PyObject *PyExc_NameError;
2318PyObject *PyExc_OverflowError;
2319PyObject *PyExc_RuntimeError;
Barry Warsaw344864f1998-12-01 18:52:06 +00002320PyObject *PyExc_NotImplementedError;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002321PyObject *PyExc_SyntaxError;
2322PyObject *PyExc_SystemError;
2323PyObject *PyExc_SystemExit;
Guido van Rossum87460821999-06-22 14:47:32 +00002324PyObject *PyExc_UnboundLocalError;
Guido van Rossum09095f32000-03-10 23:00:52 +00002325PyObject *PyExc_UnicodeError;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002326PyObject *PyExc_TypeError;
2327PyObject *PyExc_ValueError;
2328PyObject *PyExc_ZeroDivisionError;
Guido van Rossum65a75b02000-02-17 15:18:10 +00002329#ifdef MS_WINDOWS
2330PyObject *PyExc_WindowsError;
2331#endif
Guido van Rossum50afb7a1991-12-10 13:52:31 +00002332
Barry Warsaw757af0e1997-08-29 22:13:51 +00002333PyObject *PyExc_MemoryErrorInst;
2334
Guido van Rossum11950231999-03-25 21:16:07 +00002335static struct
Barry Warsaw757af0e1997-08-29 22:13:51 +00002336{
2337 char* name;
2338 PyObject** exc;
2339 int leaf_exc;
2340}
2341bltin_exc[] = {
Guido van Rossum04748321997-09-16 18:43:15 +00002342 {"Exception", &PyExc_Exception, 0},
Barry Warsaw757af0e1997-08-29 22:13:51 +00002343 {"StandardError", &PyExc_StandardError, 0},
Barry Warsaw412cdc21997-09-16 21:51:14 +00002344 {"ArithmeticError", &PyExc_ArithmeticError, 0},
Barry Warsaw757af0e1997-08-29 22:13:51 +00002345 {"LookupError", &PyExc_LookupError, 0},
2346 {"AssertionError", &PyExc_AssertionError, 1},
2347 {"AttributeError", &PyExc_AttributeError, 1},
2348 {"EOFError", &PyExc_EOFError, 1},
2349 {"FloatingPointError", &PyExc_FloatingPointError, 1},
Barry Warsaw78902031999-01-29 20:29:49 +00002350 {"EnvironmentError", &PyExc_EnvironmentError, 0},
Barry Warsaw757af0e1997-08-29 22:13:51 +00002351 {"IOError", &PyExc_IOError, 1},
Barry Warsawd086a1a1998-07-23 15:59:57 +00002352 {"OSError", &PyExc_OSError, 1},
Barry Warsaw757af0e1997-08-29 22:13:51 +00002353 {"ImportError", &PyExc_ImportError, 1},
2354 {"IndexError", &PyExc_IndexError, 1},
2355 {"KeyError", &PyExc_KeyError, 1},
2356 {"KeyboardInterrupt", &PyExc_KeyboardInterrupt, 1},
2357 {"MemoryError", &PyExc_MemoryError, 1},
Guido van Rossum87460821999-06-22 14:47:32 +00002358 /* Note: NameError is not a leaf in exceptions.py, but unlike
2359 the other non-leafs NameError is meant to be raised directly
2360 at times -- the leaf_exc member really seems to mean something
2361 like "this is an abstract base class" when false.
2362 */
Barry Warsaw757af0e1997-08-29 22:13:51 +00002363 {"NameError", &PyExc_NameError, 1},
2364 {"OverflowError", &PyExc_OverflowError, 1},
2365 {"RuntimeError", &PyExc_RuntimeError, 1},
Barry Warsaw344864f1998-12-01 18:52:06 +00002366 {"NotImplementedError",&PyExc_NotImplementedError,1},
Barry Warsaw757af0e1997-08-29 22:13:51 +00002367 {"SyntaxError", &PyExc_SyntaxError, 1},
2368 {"SystemError", &PyExc_SystemError, 1},
2369 {"SystemExit", &PyExc_SystemExit, 1},
Guido van Rossum87460821999-06-22 14:47:32 +00002370 {"UnboundLocalError", &PyExc_UnboundLocalError, 1},
Guido van Rossum09095f32000-03-10 23:00:52 +00002371 {"UnicodeError", &PyExc_UnicodeError, 1},
Barry Warsaw757af0e1997-08-29 22:13:51 +00002372 {"TypeError", &PyExc_TypeError, 1},
2373 {"ValueError", &PyExc_ValueError, 1},
Guido van Rossum65a75b02000-02-17 15:18:10 +00002374#ifdef MS_WINDOWS
2375 {"WindowsError", &PyExc_WindowsError, 1},
2376#endif
Barry Warsaw757af0e1997-08-29 22:13:51 +00002377 {"ZeroDivisionError", &PyExc_ZeroDivisionError, 1},
2378 {NULL, NULL}
2379};
2380
2381
Barry Warsaw98b62461998-09-14 18:51:11 +00002382/* import exceptions module to extract class exceptions. on success,
2383 * return 1. on failure return 0 which signals _PyBuiltin_Init_2 to fall
2384 * back to using old-style string based exceptions.
2385 */
2386static int
Barry Warsaw757af0e1997-08-29 22:13:51 +00002387init_class_exc(dict)
2388 PyObject *dict;
2389{
2390 int i;
2391 PyObject *m = PyImport_ImportModule("exceptions");
Barry Warsaw98b62461998-09-14 18:51:11 +00002392 PyObject *args = NULL;
2393 PyObject *d = NULL;
Barry Warsaw757af0e1997-08-29 22:13:51 +00002394
Barry Warsaw98b62461998-09-14 18:51:11 +00002395 /* make sure we got the module and its dictionary */
Barry Warsaw757af0e1997-08-29 22:13:51 +00002396 if (m == NULL ||
2397 (d = PyModule_GetDict(m)) == NULL)
2398 {
Barry Warsaw98b62461998-09-14 18:51:11 +00002399 PySys_WriteStderr("'import exceptions' failed; ");
Barry Warsaw757af0e1997-08-29 22:13:51 +00002400 if (Py_VerboseFlag) {
Barry Warsaw98b62461998-09-14 18:51:11 +00002401 PySys_WriteStderr("traceback:\n");
Barry Warsaw757af0e1997-08-29 22:13:51 +00002402 PyErr_Print();
2403 }
2404 else {
Barry Warsaw98b62461998-09-14 18:51:11 +00002405 PySys_WriteStderr("use -v for traceback\n");
Barry Warsaw757af0e1997-08-29 22:13:51 +00002406 }
Barry Warsaw98b62461998-09-14 18:51:11 +00002407 goto finally;
Barry Warsaw757af0e1997-08-29 22:13:51 +00002408 }
2409 for (i = 0; bltin_exc[i].name; i++) {
2410 /* dig the exception out of the module */
2411 PyObject *exc = PyDict_GetItemString(d, bltin_exc[i].name);
Barry Warsaw98b62461998-09-14 18:51:11 +00002412 if (!exc) {
2413 PySys_WriteStderr(
2414 "Built-in exception class not found: %s. Library mismatch?\n",
2415 bltin_exc[i].name);
2416 goto finally;
2417 }
2418 /* free the old-style exception string object */
Barry Warsaw757af0e1997-08-29 22:13:51 +00002419 Py_XDECREF(*bltin_exc[i].exc);
2420
2421 /* squirrel away a pointer to the exception */
2422 Py_INCREF(exc);
2423 *bltin_exc[i].exc = exc;
2424
2425 /* and insert the name in the __builtin__ module */
Barry Warsaw98b62461998-09-14 18:51:11 +00002426 if (PyDict_SetItemString(dict, bltin_exc[i].name, exc)) {
2427 PySys_WriteStderr(
2428 "Cannot insert exception into __builtin__: %s\n",
2429 bltin_exc[i].name);
2430 goto finally;
2431 }
Barry Warsaw757af0e1997-08-29 22:13:51 +00002432 }
2433
2434 /* we need one pre-allocated instance */
2435 args = Py_BuildValue("()");
Barry Warsaw98b62461998-09-14 18:51:11 +00002436 if (!args ||
2437 !(PyExc_MemoryErrorInst =
2438 PyEval_CallObject(PyExc_MemoryError, args)))
2439 {
2440 PySys_WriteStderr("Cannot pre-allocate MemoryError instance\n");
2441 goto finally;
Barry Warsaw757af0e1997-08-29 22:13:51 +00002442 }
Barry Warsaw98b62461998-09-14 18:51:11 +00002443 Py_DECREF(args);
Barry Warsaw757af0e1997-08-29 22:13:51 +00002444
2445 /* we're done with the exceptions module */
2446 Py_DECREF(m);
2447
Barry Warsaw98b62461998-09-14 18:51:11 +00002448 if (PyErr_Occurred()) {
2449 PySys_WriteStderr("Cannot initialize standard class exceptions; ");
2450 if (Py_VerboseFlag) {
2451 PySys_WriteStderr("traceback:\n");
2452 PyErr_Print();
2453 }
2454 else
2455 PySys_WriteStderr("use -v for traceback\n");
2456 goto finally;
2457 }
2458 return 1;
2459 finally:
2460 Py_XDECREF(m);
2461 Py_XDECREF(args);
2462 PyErr_Clear();
2463 return 0;
Barry Warsaw757af0e1997-08-29 22:13:51 +00002464}
2465
2466
2467static void
2468fini_instances()
2469{
2470 Py_XDECREF(PyExc_MemoryErrorInst);
2471 PyExc_MemoryErrorInst = NULL;
2472}
2473
2474
Guido van Rossum79f25d91997-04-29 20:08:16 +00002475static PyObject *
Guido van Rossum25ce5661997-08-02 03:10:38 +00002476newstdexception(dict, name)
2477 PyObject *dict;
Guido van Rossumfb905c31991-12-16 15:42:38 +00002478 char *name;
Guido van Rossum3f5da241990-12-20 15:06:42 +00002479{
Guido van Rossum79f25d91997-04-29 20:08:16 +00002480 PyObject *v = PyString_FromString(name);
Guido van Rossum25ce5661997-08-02 03:10:38 +00002481 if (v == NULL || PyDict_SetItemString(dict, name, v) != 0)
Barry Warsaw98b62461998-09-14 18:51:11 +00002482 Py_FatalError("Cannot create string-based exceptions");
Guido van Rossum3f5da241990-12-20 15:06:42 +00002483 return v;
2484}
2485
2486static void
Guido van Rossum25ce5661997-08-02 03:10:38 +00002487initerrors(dict)
2488 PyObject *dict;
Guido van Rossum3f5da241990-12-20 15:06:42 +00002489{
Barry Warsaw72b715d1999-02-24 00:35:43 +00002490 int i, j;
Barry Warsaw757af0e1997-08-29 22:13:51 +00002491 int exccnt = 0;
2492 for (i = 0; bltin_exc[i].name; i++, exccnt++) {
Barry Warsaw98b62461998-09-14 18:51:11 +00002493 Py_XDECREF(*bltin_exc[i].exc);
Barry Warsaw757af0e1997-08-29 22:13:51 +00002494 if (bltin_exc[i].leaf_exc)
2495 *bltin_exc[i].exc =
2496 newstdexception(dict, bltin_exc[i].name);
2497 }
2498
Barry Warsawd086a1a1998-07-23 15:59:57 +00002499 /* This is kind of bogus because we special case the some of the
2500 * new exceptions to be nearly forward compatible. But this means
2501 * we hard code knowledge about exceptions.py into C here. I don't
2502 * have a better solution, though.
2503 */
Barry Warsaw757af0e1997-08-29 22:13:51 +00002504 PyExc_LookupError = PyTuple_New(2);
2505 Py_INCREF(PyExc_IndexError);
2506 PyTuple_SET_ITEM(PyExc_LookupError, 0, PyExc_IndexError);
2507 Py_INCREF(PyExc_KeyError);
2508 PyTuple_SET_ITEM(PyExc_LookupError, 1, PyExc_KeyError);
2509 PyDict_SetItemString(dict, "LookupError", PyExc_LookupError);
2510
Barry Warsaw412cdc21997-09-16 21:51:14 +00002511 PyExc_ArithmeticError = PyTuple_New(3);
Barry Warsaw757af0e1997-08-29 22:13:51 +00002512 Py_INCREF(PyExc_OverflowError);
Barry Warsaw412cdc21997-09-16 21:51:14 +00002513 PyTuple_SET_ITEM(PyExc_ArithmeticError, 0, PyExc_OverflowError);
Barry Warsaw757af0e1997-08-29 22:13:51 +00002514 Py_INCREF(PyExc_ZeroDivisionError);
Barry Warsaw412cdc21997-09-16 21:51:14 +00002515 PyTuple_SET_ITEM(PyExc_ArithmeticError, 1, PyExc_ZeroDivisionError);
Barry Warsaw757af0e1997-08-29 22:13:51 +00002516 Py_INCREF(PyExc_FloatingPointError);
Barry Warsaw412cdc21997-09-16 21:51:14 +00002517 PyTuple_SET_ITEM(PyExc_ArithmeticError, 2, PyExc_FloatingPointError);
2518 PyDict_SetItemString(dict, "ArithmeticError", PyExc_ArithmeticError);
Barry Warsaw757af0e1997-08-29 22:13:51 +00002519
Barry Warsawd086a1a1998-07-23 15:59:57 +00002520 PyExc_EnvironmentError = PyTuple_New(2);
2521 Py_INCREF(PyExc_IOError);
2522 PyTuple_SET_ITEM(PyExc_EnvironmentError, 0, PyExc_IOError);
2523 Py_INCREF(PyExc_OSError);
2524 PyTuple_SET_ITEM(PyExc_EnvironmentError, 1, PyExc_OSError);
2525 PyDict_SetItemString(dict, "EnvironmentError", PyExc_EnvironmentError);
2526
Guido van Rossum87460821999-06-22 14:47:32 +00002527 /* Make UnboundLocalError an alias for NameError */
2528 Py_INCREF(PyExc_NameError);
2529 Py_DECREF(PyExc_UnboundLocalError);
2530 PyExc_UnboundLocalError = PyExc_NameError;
2531 if (PyDict_SetItemString(dict, "UnboundLocalError",
2532 PyExc_NameError) != 0)
2533 Py_FatalError("Cannot create string-based exceptions");
2534
Guido van Rossum09095f32000-03-10 23:00:52 +00002535 /* Make UnicodeError an alias for ValueError */
2536 Py_INCREF(PyExc_ValueError);
2537 Py_DECREF(PyExc_UnicodeError);
2538 PyExc_UnicodeError = PyExc_ValueError;
2539 if (PyDict_SetItemString(dict, "UnicodeError",
2540 PyExc_ValueError) != 0)
2541 Py_FatalError("Cannot create string-based exceptions");
2542
Barry Warsaw72b715d1999-02-24 00:35:43 +00002543 /* missing from the StandardError tuple: Exception, StandardError,
2544 * and SystemExit
2545 */
2546 PyExc_StandardError = PyTuple_New(exccnt-3);
2547 for (i = 2, j = 0; bltin_exc[i].name; i++) {
Barry Warsaw757af0e1997-08-29 22:13:51 +00002548 PyObject *exc = *bltin_exc[i].exc;
Barry Warsaw72b715d1999-02-24 00:35:43 +00002549 /* SystemExit is not an error, but it is an exception */
2550 if (exc != PyExc_SystemExit) {
2551 Py_INCREF(exc);
2552 PyTuple_SET_ITEM(PyExc_StandardError, j++, exc);
2553 }
Barry Warsaw757af0e1997-08-29 22:13:51 +00002554 }
2555 PyDict_SetItemString(dict, "StandardError", PyExc_StandardError);
Guido van Rossum04748321997-09-16 18:43:15 +00002556
Barry Warsaw72b715d1999-02-24 00:35:43 +00002557 /* Exception is a 2-tuple */
2558 PyExc_Exception = PyTuple_New(2);
2559 Py_INCREF(PyExc_SystemExit);
2560 PyTuple_SET_ITEM(PyExc_Exception, 0, PyExc_SystemExit);
2561 Py_INCREF(PyExc_StandardError);
2562 PyTuple_SET_ITEM(PyExc_Exception, 1, PyExc_StandardError);
Guido van Rossum04748321997-09-16 18:43:15 +00002563 PyDict_SetItemString(dict, "Exception", PyExc_Exception);
Barry Warsaw757af0e1997-08-29 22:13:51 +00002564
2565 if (PyErr_Occurred())
2566 Py_FatalError("Could not initialize built-in string exceptions");
Guido van Rossum25ce5661997-08-02 03:10:38 +00002567}
2568
Barry Warsaw72b715d1999-02-24 00:35:43 +00002569
Guido van Rossum25ce5661997-08-02 03:10:38 +00002570static void
2571finierrors()
2572{
Barry Warsaw757af0e1997-08-29 22:13:51 +00002573 int i;
2574 for (i = 0; bltin_exc[i].name; i++) {
2575 PyObject *exc = *bltin_exc[i].exc;
2576 Py_XDECREF(exc);
2577 *bltin_exc[i].exc = NULL;
2578 }
Guido van Rossum25ce5661997-08-02 03:10:38 +00002579}
2580
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002581static char builtin_doc[] =
2582"Built-in functions, exceptions, and other objects.\n\
2583\n\
2584Noteworthy: None is the `nil' object; Ellipsis represents `...' in slices.";
2585
Guido van Rossum25ce5661997-08-02 03:10:38 +00002586PyObject *
Barry Warsaw757af0e1997-08-29 22:13:51 +00002587_PyBuiltin_Init_1()
Guido van Rossum25ce5661997-08-02 03:10:38 +00002588{
2589 PyObject *mod, *dict;
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002590 mod = Py_InitModule4("__builtin__", builtin_methods,
2591 builtin_doc, (PyObject *)NULL,
2592 PYTHON_API_VERSION);
Guido van Rossum25ce5661997-08-02 03:10:38 +00002593 if (mod == NULL)
2594 return NULL;
2595 dict = PyModule_GetDict(mod);
2596 initerrors(dict);
2597 if (PyDict_SetItemString(dict, "None", Py_None) < 0)
2598 return NULL;
2599 if (PyDict_SetItemString(dict, "Ellipsis", Py_Ellipsis) < 0)
2600 return NULL;
2601 if (PyDict_SetItemString(dict, "__debug__",
2602 PyInt_FromLong(Py_OptimizeFlag == 0)) < 0)
2603 return NULL;
Barry Warsaw757af0e1997-08-29 22:13:51 +00002604
Guido van Rossum25ce5661997-08-02 03:10:38 +00002605 return mod;
Guido van Rossum3f5da241990-12-20 15:06:42 +00002606}
2607
2608void
Barry Warsaw757af0e1997-08-29 22:13:51 +00002609_PyBuiltin_Init_2(dict)
2610 PyObject *dict;
2611{
2612 /* if Python was started with -X, initialize the class exceptions */
Barry Warsaw98b62461998-09-14 18:51:11 +00002613 if (Py_UseClassExceptionsFlag) {
2614 if (!init_class_exc(dict)) {
2615 /* class based exceptions could not be
2616 * initialized. Fall back to using string based
2617 * exceptions.
2618 */
2619 PySys_WriteStderr(
2620 "Warning! Falling back to string-based exceptions\n");
2621 initerrors(dict);
2622 }
2623 }
Barry Warsaw757af0e1997-08-29 22:13:51 +00002624}
2625
2626
2627void
2628_PyBuiltin_Fini_1()
2629{
2630 fini_instances();
2631}
2632
2633
2634void
2635_PyBuiltin_Fini_2()
Guido van Rossum3f5da241990-12-20 15:06:42 +00002636{
Guido van Rossum25ce5661997-08-02 03:10:38 +00002637 finierrors();
Guido van Rossum3f5da241990-12-20 15:06:42 +00002638}
Guido van Rossumc6bb8f71991-07-01 18:42:41 +00002639
Guido van Rossum12d12c51993-10-26 17:58:25 +00002640
Guido van Rossume77a7571993-11-03 15:01:26 +00002641/* Helper for filter(): filter a tuple through a function */
Guido van Rossum12d12c51993-10-26 17:58:25 +00002642
Guido van Rossum79f25d91997-04-29 20:08:16 +00002643static PyObject *
Guido van Rossum12d12c51993-10-26 17:58:25 +00002644filtertuple(func, tuple)
Guido van Rossum79f25d91997-04-29 20:08:16 +00002645 PyObject *func;
2646 PyObject *tuple;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002647{
Guido van Rossum79f25d91997-04-29 20:08:16 +00002648 PyObject *result;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002649 register int i, j;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002650 int len = PyTuple_Size(tuple);
Guido van Rossum12d12c51993-10-26 17:58:25 +00002651
Guido van Rossumb7b45621995-08-04 04:07:45 +00002652 if (len == 0) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002653 Py_INCREF(tuple);
Guido van Rossumb7b45621995-08-04 04:07:45 +00002654 return tuple;
2655 }
2656
Guido van Rossum79f25d91997-04-29 20:08:16 +00002657 if ((result = PyTuple_New(len)) == NULL)
Guido van Rossum2586bf01993-11-01 16:21:44 +00002658 return NULL;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002659
Guido van Rossum12d12c51993-10-26 17:58:25 +00002660 for (i = j = 0; i < len; ++i) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002661 PyObject *item, *good;
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002662 int ok;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002663
Guido van Rossum79f25d91997-04-29 20:08:16 +00002664 if ((item = PyTuple_GetItem(tuple, i)) == NULL)
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002665 goto Fail_1;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002666 if (func == Py_None) {
2667 Py_INCREF(item);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002668 good = item;
2669 }
2670 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002671 PyObject *arg = Py_BuildValue("(O)", item);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002672 if (arg == NULL)
2673 goto Fail_1;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002674 good = PyEval_CallObject(func, arg);
2675 Py_DECREF(arg);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002676 if (good == NULL)
Guido van Rossum12d12c51993-10-26 17:58:25 +00002677 goto Fail_1;
2678 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002679 ok = PyObject_IsTrue(good);
2680 Py_DECREF(good);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002681 if (ok) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002682 Py_INCREF(item);
2683 if (PyTuple_SetItem(result, j++, item) < 0)
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002684 goto Fail_1;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002685 }
Guido van Rossum12d12c51993-10-26 17:58:25 +00002686 }
2687
Guido van Rossum79f25d91997-04-29 20:08:16 +00002688 if (_PyTuple_Resize(&result, j, 0) < 0)
Guido van Rossum12d12c51993-10-26 17:58:25 +00002689 return NULL;
2690
Guido van Rossum12d12c51993-10-26 17:58:25 +00002691 return result;
2692
Guido van Rossum12d12c51993-10-26 17:58:25 +00002693Fail_1:
Guido van Rossum79f25d91997-04-29 20:08:16 +00002694 Py_DECREF(result);
Guido van Rossum12d12c51993-10-26 17:58:25 +00002695 return NULL;
2696}
2697
2698
Guido van Rossume77a7571993-11-03 15:01:26 +00002699/* Helper for filter(): filter a string through a function */
Guido van Rossum12d12c51993-10-26 17:58:25 +00002700
Guido van Rossum79f25d91997-04-29 20:08:16 +00002701static PyObject *
Guido van Rossum12d12c51993-10-26 17:58:25 +00002702filterstring(func, strobj)
Guido van Rossum79f25d91997-04-29 20:08:16 +00002703 PyObject *func;
2704 PyObject *strobj;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002705{
Guido van Rossum79f25d91997-04-29 20:08:16 +00002706 PyObject *result;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002707 register int i, j;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002708 int len = PyString_Size(strobj);
Guido van Rossum12d12c51993-10-26 17:58:25 +00002709
Guido van Rossum79f25d91997-04-29 20:08:16 +00002710 if (func == Py_None) {
Guido van Rossum2586bf01993-11-01 16:21:44 +00002711 /* No character is ever false -- share input string */
Guido van Rossum79f25d91997-04-29 20:08:16 +00002712 Py_INCREF(strobj);
Guido van Rossum2d951851994-08-29 12:52:16 +00002713 return strobj;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002714 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002715 if ((result = PyString_FromStringAndSize(NULL, len)) == NULL)
Guido van Rossum2586bf01993-11-01 16:21:44 +00002716 return NULL;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002717
Guido van Rossum12d12c51993-10-26 17:58:25 +00002718 for (i = j = 0; i < len; ++i) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002719 PyObject *item, *arg, *good;
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002720 int ok;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002721
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002722 item = (*strobj->ob_type->tp_as_sequence->sq_item)(strobj, i);
2723 if (item == NULL)
2724 goto Fail_1;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002725 arg = Py_BuildValue("(O)", item);
2726 Py_DECREF(item);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002727 if (arg == NULL)
2728 goto Fail_1;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002729 good = PyEval_CallObject(func, arg);
2730 Py_DECREF(arg);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002731 if (good == NULL)
2732 goto Fail_1;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002733 ok = PyObject_IsTrue(good);
2734 Py_DECREF(good);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002735 if (ok)
Guido van Rossum79f25d91997-04-29 20:08:16 +00002736 PyString_AS_STRING((PyStringObject *)result)[j++] =
2737 PyString_AS_STRING((PyStringObject *)item)[0];
Guido van Rossum12d12c51993-10-26 17:58:25 +00002738 }
2739
Guido van Rossum79f25d91997-04-29 20:08:16 +00002740 if (j < len && _PyString_Resize(&result, j) < 0)
Guido van Rossum12d12c51993-10-26 17:58:25 +00002741 return NULL;
2742
Guido van Rossum12d12c51993-10-26 17:58:25 +00002743 return result;
2744
Guido van Rossum12d12c51993-10-26 17:58:25 +00002745Fail_1:
Guido van Rossum79f25d91997-04-29 20:08:16 +00002746 Py_DECREF(result);
Guido van Rossum12d12c51993-10-26 17:58:25 +00002747 return NULL;
2748}