blob: 3016d567584a329c9d7778bc35207054ff64d067 [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 Rossum9e896b32000-04-05 20:11:21 +0000452 const char *s, *start;
453 char *end;
Guido van Rossum11950231999-03-25 21:16:07 +0000454 double x=0.0, y=0.0, z;
455 int got_re=0, got_im=0, done=0;
456 int digit_or_dot;
457 int sw_error=0;
458 int sign;
459 char buffer[256]; /* For errors */
Guido van Rossum9e896b32000-04-05 20:11:21 +0000460 int len;
Guido van Rossum11950231999-03-25 21:16:07 +0000461
Guido van Rossum9e896b32000-04-05 20:11:21 +0000462 if (PyString_Check(v)) {
463 s = PyString_AS_STRING(v);
464 len = PyString_GET_SIZE(v);
465 }
466 else if (PyUnicode_Check(v)) {
467 char s_buffer[256];
468
469 if (PyUnicode_GET_SIZE(v) >= sizeof(s_buffer)) {
470 PyErr_SetString(PyExc_ValueError,
471 "complex() literal too large to convert");
472 return NULL;
473 }
474 if (PyUnicode_EncodeDecimal(PyUnicode_AS_UNICODE(v),
475 PyUnicode_GET_SIZE(v),
476 s_buffer,
477 NULL))
478 return NULL;
479 s = s_buffer;
480 len = strlen(s);
481 }
482 else if (PyObject_AsCharBuffer(v, &s, &len)) {
483 PyErr_SetString(PyExc_TypeError,
484 "complex() needs a string first argument");
485 return NULL;
486 }
Guido van Rossum11950231999-03-25 21:16:07 +0000487
488 /* position on first nonblank */
Guido van Rossum9e896b32000-04-05 20:11:21 +0000489 start = s;
Guido van Rossum11950231999-03-25 21:16:07 +0000490 while (*s && isspace(Py_CHARMASK(*s)))
491 s++;
492 if (s[0] == '\0') {
493 PyErr_SetString(PyExc_ValueError,
494 "empty string for complex()");
495 return NULL;
496 }
497
498 z = -1.0;
499 sign = 1;
500 do {
501
502 switch (*s) {
503
504 case '\0':
Guido van Rossum9e896b32000-04-05 20:11:21 +0000505 if (s-start != len) {
Guido van Rossum11950231999-03-25 21:16:07 +0000506 PyErr_SetString(
507 PyExc_ValueError,
508 "null byte in argument for complex()");
509 return NULL;
510 }
511 if(!done) sw_error=1;
512 break;
513
514 case '-':
515 sign = -1;
516 /* Fallthrough */
517 case '+':
518 if (done) sw_error=1;
519 s++;
520 if ( *s=='\0'||*s=='+'||*s=='-' ||
521 isspace(Py_CHARMASK(*s)) ) sw_error=1;
522 break;
523
524 case 'J':
525 case 'j':
526 if (got_im || done) {
527 sw_error = 1;
528 break;
529 }
530 if (z<0.0) {
531 y=sign;
532 }
533 else{
534 y=sign*z;
535 }
536 got_im=1;
537 s++;
538 if (*s!='+' && *s!='-' )
539 done=1;
540 break;
541
542 default:
543 if (isspace(Py_CHARMASK(*s))) {
544 while (*s && isspace(Py_CHARMASK(*s)))
545 s++;
546 if (s[0] != '\0')
547 sw_error=1;
548 else
549 done = 1;
550 break;
551 }
552 digit_or_dot =
553 (*s=='.' || isdigit(Py_CHARMASK(*s)));
554 if (done||!digit_or_dot) {
555 sw_error=1;
556 break;
557 }
558 errno = 0;
559 PyFPE_START_PROTECT("strtod", return 0)
560 z = strtod(s, &end) ;
561 PyFPE_END_PROTECT(z)
562 if (errno != 0) {
563 sprintf(buffer,
564 "float() out of range: %.150s", s);
565 PyErr_SetString(
566 PyExc_ValueError,
567 buffer);
568 return NULL;
569 }
570 s=end;
571 if (*s=='J' || *s=='j') {
572
573 break;
574 }
575 if (got_re) {
576 sw_error=1;
577 break;
578 }
579
580 /* accept a real part */
581 x=sign*z;
582 got_re=1;
583 if (got_im) done=1;
584 z = -1.0;
585 sign = 1;
586 break;
587
588 } /* end of switch */
589
590 } while (*s!='\0' && !sw_error);
591
592 if (sw_error) {
593 PyErr_SetString(PyExc_ValueError,
594 "malformed string for complex()");
595 return NULL;
596 }
597
598 return PyComplex_FromDoubles(x,y);
599}
600
601static PyObject *
Guido van Rossum8a5c5d21996-01-12 01:09:56 +0000602builtin_complex(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000603 PyObject *self;
604 PyObject *args;
Guido van Rossum8a5c5d21996-01-12 01:09:56 +0000605{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000606 PyObject *r, *i, *tmp;
607 PyNumberMethods *nbr, *nbi = NULL;
Guido van Rossum530956d1996-07-21 02:27:43 +0000608 Py_complex cr, ci;
Guido van Rossumc6472e91997-03-31 17:15:43 +0000609 int own_r = 0;
Guido van Rossum8a5c5d21996-01-12 01:09:56 +0000610
611 i = NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000612 if (!PyArg_ParseTuple(args, "O|O:complex", &r, &i))
Guido van Rossum8a5c5d21996-01-12 01:09:56 +0000613 return NULL;
Guido van Rossum9e896b32000-04-05 20:11:21 +0000614 if (PyString_Check(r) || PyUnicode_Check(r))
Guido van Rossum11950231999-03-25 21:16:07 +0000615 return complex_from_string(r);
Guido van Rossum8a5c5d21996-01-12 01:09:56 +0000616 if ((nbr = r->ob_type->tp_as_number) == NULL ||
Guido van Rossumc6472e91997-03-31 17:15:43 +0000617 nbr->nb_float == NULL ||
618 (i != NULL &&
619 ((nbi = i->ob_type->tp_as_number) == NULL ||
620 nbi->nb_float == NULL))) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000621 PyErr_SetString(PyExc_TypeError,
Guido van Rossum8a5c5d21996-01-12 01:09:56 +0000622 "complex() argument can't be converted to complex");
623 return NULL;
624 }
Guido van Rossumed0af8f1996-12-05 23:18:18 +0000625 /* XXX Hack to support classes with __complex__ method */
Guido van Rossum79f25d91997-04-29 20:08:16 +0000626 if (PyInstance_Check(r)) {
627 static PyObject *complexstr;
628 PyObject *f;
Guido van Rossumed0af8f1996-12-05 23:18:18 +0000629 if (complexstr == NULL) {
Guido van Rossum8d751611997-01-18 08:04:16 +0000630 complexstr = PyString_InternFromString("__complex__");
Guido van Rossumed0af8f1996-12-05 23:18:18 +0000631 if (complexstr == NULL)
632 return NULL;
633 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000634 f = PyObject_GetAttr(r, complexstr);
Guido van Rossumed0af8f1996-12-05 23:18:18 +0000635 if (f == NULL)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000636 PyErr_Clear();
Guido van Rossumed0af8f1996-12-05 23:18:18 +0000637 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000638 PyObject *args = Py_BuildValue("()");
Guido van Rossumed0af8f1996-12-05 23:18:18 +0000639 if (args == NULL)
640 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000641 r = PyEval_CallObject(f, args);
642 Py_DECREF(args);
Barry Warsawf988e681999-01-27 23:13:59 +0000643 Py_DECREF(f);
Guido van Rossumed0af8f1996-12-05 23:18:18 +0000644 if (r == NULL)
645 return NULL;
Guido van Rossumc6472e91997-03-31 17:15:43 +0000646 own_r = 1;
Guido van Rossumed0af8f1996-12-05 23:18:18 +0000647 }
648 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000649 if (PyComplex_Check(r)) {
650 cr = ((PyComplexObject*)r)->cval;
Guido van Rossum730806d1998-04-10 22:27:42 +0000651 if (own_r) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000652 Py_DECREF(r);
Guido van Rossum730806d1998-04-10 22:27:42 +0000653 }
Guido van Rossumc6472e91997-03-31 17:15:43 +0000654 }
Guido van Rossum8a5c5d21996-01-12 01:09:56 +0000655 else {
Guido van Rossumfe4b6ee1996-08-08 18:49:41 +0000656 tmp = (*nbr->nb_float)(r);
Guido van Rossum730806d1998-04-10 22:27:42 +0000657 if (own_r) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000658 Py_DECREF(r);
Guido van Rossum730806d1998-04-10 22:27:42 +0000659 }
Guido van Rossumfe4b6ee1996-08-08 18:49:41 +0000660 if (tmp == NULL)
661 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000662 cr.real = PyFloat_AsDouble(tmp);
663 Py_DECREF(tmp);
Guido van Rossum11950231999-03-25 21:16:07 +0000664 cr.imag = 0.0;
Guido van Rossum8a5c5d21996-01-12 01:09:56 +0000665 }
666 if (i == NULL) {
Guido van Rossum11950231999-03-25 21:16:07 +0000667 ci.real = 0.0;
668 ci.imag = 0.0;
Guido van Rossum8a5c5d21996-01-12 01:09:56 +0000669 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000670 else if (PyComplex_Check(i))
671 ci = ((PyComplexObject*)i)->cval;
Guido van Rossum8a5c5d21996-01-12 01:09:56 +0000672 else {
Guido van Rossumc6472e91997-03-31 17:15:43 +0000673 tmp = (*nbi->nb_float)(i);
Guido van Rossumfe4b6ee1996-08-08 18:49:41 +0000674 if (tmp == NULL)
675 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000676 ci.real = PyFloat_AsDouble(tmp);
677 Py_DECREF(tmp);
Guido van Rossum8a5c5d21996-01-12 01:09:56 +0000678 ci.imag = 0.;
679 }
680 cr.real -= ci.imag;
681 cr.imag += ci.real;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000682 return PyComplex_FromCComplex(cr);
Guido van Rossum8a5c5d21996-01-12 01:09:56 +0000683}
684
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000685static char complex_doc[] =
686"complex(real[, imag]) -> complex number\n\
687\n\
688Create a complex number from a real part and an optional imaginary part.\n\
689This is equivalent to (real + imag*1j) where imag defaults to 0.";
690
691
Guido van Rossum8a5c5d21996-01-12 01:09:56 +0000692#endif
693
Guido van Rossum79f25d91997-04-29 20:08:16 +0000694static PyObject *
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000695builtin_dir(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000696 PyObject *self;
697 PyObject *args;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000698{
Guido van Rossum666b17a1997-05-06 16:36:57 +0000699 static char *attrlist[] = {"__members__", "__methods__", NULL};
700 PyObject *v = NULL, *l = NULL, *m = NULL;
701 PyObject *d, *x;
702 int i;
703 char **s;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000704
Guido van Rossum79f25d91997-04-29 20:08:16 +0000705 if (!PyArg_ParseTuple(args, "|O:dir", &v))
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000706 return NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000707 if (v == NULL) {
Guido van Rossum666b17a1997-05-06 16:36:57 +0000708 x = PyEval_GetLocals();
709 if (x == NULL)
710 goto error;
711 l = PyMapping_Keys(x);
712 if (l == NULL)
713 goto error;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000714 }
715 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000716 d = PyObject_GetAttrString(v, "__dict__");
Guido van Rossum666b17a1997-05-06 16:36:57 +0000717 if (d == NULL)
Guido van Rossum795ba581996-05-23 22:49:07 +0000718 PyErr_Clear();
Guido van Rossum666b17a1997-05-06 16:36:57 +0000719 else {
720 l = PyMapping_Keys(d);
721 if (l == NULL)
722 PyErr_Clear();
723 Py_DECREF(d);
724 }
725 if (l == NULL) {
726 l = PyList_New(0);
727 if (l == NULL)
728 goto error;
729 }
730 for (s = attrlist; *s != NULL; s++) {
731 m = PyObject_GetAttrString(v, *s);
732 if (m == NULL) {
733 PyErr_Clear();
734 continue;
735 }
736 for (i = 0; ; i++) {
737 x = PySequence_GetItem(m, i);
738 if (x == NULL) {
739 PyErr_Clear();
740 break;
741 }
742 if (PyList_Append(l, x) != 0) {
743 Py_DECREF(x);
744 Py_DECREF(m);
745 goto error;
746 }
747 Py_DECREF(x);
748 }
749 Py_DECREF(m);
Guido van Rossum795ba581996-05-23 22:49:07 +0000750 }
Guido van Rossum006bcd41991-10-24 14:54:44 +0000751 }
Guido van Rossum666b17a1997-05-06 16:36:57 +0000752 if (PyList_Sort(l) != 0)
753 goto error;
754 return l;
755 error:
756 Py_XDECREF(l);
757 return NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000758}
759
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000760static char dir_doc[] =
761"dir([object]) -> list of strings\n\
762\n\
763Return an alphabetized list of names comprising (some of) the attributes\n\
764of the given object. Without an argument, the names in the current scope\n\
765are listed. With an instance argument, only the instance attributes are\n\
766returned. With a class argument, attributes of the base class are not\n\
767returned. For other types or arguments, this may list members or methods.";
768
769
Guido van Rossum79f25d91997-04-29 20:08:16 +0000770static PyObject *
Guido van Rossum6a00cd81995-01-07 12:39:01 +0000771builtin_divmod(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000772 PyObject *self;
773 PyObject *args;
Guido van Rossum6a00cd81995-01-07 12:39:01 +0000774{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000775 PyObject *v, *w;
Guido van Rossum6a00cd81995-01-07 12:39:01 +0000776
Guido van Rossum79f25d91997-04-29 20:08:16 +0000777 if (!PyArg_ParseTuple(args, "OO:divmod", &v, &w))
Guido van Rossum6a00cd81995-01-07 12:39:01 +0000778 return NULL;
Guido van Rossum09df08a1998-05-22 00:51:39 +0000779 return PyNumber_Divmod(v, w);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000780}
781
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000782static char divmod_doc[] =
783"divmod(x, y) -> (div, mod)\n\
784\n\
785Return the tuple ((x-x%y)/y, x%y). Invariant: div*y + mod == x.";
786
787
Guido van Rossum79f25d91997-04-29 20:08:16 +0000788static PyObject *
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000789builtin_eval(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000790 PyObject *self;
791 PyObject *args;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000792{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000793 PyObject *cmd;
794 PyObject *globals = Py_None, *locals = Py_None;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000795 char *str;
Guido van Rossum590baa41993-11-30 13:40:46 +0000796
Guido van Rossum79f25d91997-04-29 20:08:16 +0000797 if (!PyArg_ParseTuple(args, "O|O!O!:eval",
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000798 &cmd,
Guido van Rossum79f25d91997-04-29 20:08:16 +0000799 &PyDict_Type, &globals,
800 &PyDict_Type, &locals))
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000801 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000802 if (globals == Py_None) {
803 globals = PyEval_GetGlobals();
804 if (locals == Py_None)
805 locals = PyEval_GetLocals();
Guido van Rossum6135a871995-01-09 17:53:26 +0000806 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000807 else if (locals == Py_None)
Guido van Rossum6135a871995-01-09 17:53:26 +0000808 locals = globals;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000809 if (PyDict_GetItemString(globals, "__builtins__") == NULL) {
810 if (PyDict_SetItemString(globals, "__builtins__",
811 PyEval_GetBuiltins()) != 0)
Guido van Rossum6135a871995-01-09 17:53:26 +0000812 return NULL;
813 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000814 if (PyCode_Check(cmd))
815 return PyEval_EvalCode((PyCodeObject *) cmd, globals, locals);
816 if (!PyString_Check(cmd)) {
817 PyErr_SetString(PyExc_TypeError,
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000818 "eval() argument 1 must be string or code object");
Guido van Rossum94390a41992-08-14 15:14:30 +0000819 return NULL;
820 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000821 str = PyString_AsString(cmd);
822 if ((int)strlen(str) != PyString_Size(cmd)) {
823 PyErr_SetString(PyExc_ValueError,
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000824 "embedded '\\0' in string arg");
825 return NULL;
Guido van Rossumf08ab0a1992-03-04 16:41:41 +0000826 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000827 while (*str == ' ' || *str == '\t')
828 str++;
Guido van Rossumb05a5c71997-05-07 17:46:13 +0000829 return PyRun_String(str, Py_eval_input, globals, locals);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000830}
831
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000832static char eval_doc[] =
833"eval(source[, globals[, locals]]) -> value\n\
834\n\
835Evaluate the source in the context of globals and locals.\n\
836The source may be a string representing a Python expression\n\
837or a code object as returned by compile().\n\
838The globals and locals are dictionaries, defaulting to the current\n\
839globals and locals. If only globals is given, locals defaults to it.";
840
841
Guido van Rossum79f25d91997-04-29 20:08:16 +0000842static PyObject *
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000843builtin_execfile(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000844 PyObject *self;
845 PyObject *args;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000846{
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000847 char *filename;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000848 PyObject *globals = Py_None, *locals = Py_None;
849 PyObject *res;
Guido van Rossum0f61f8a1992-02-25 18:55:05 +0000850 FILE* fp;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000851
Guido van Rossum79f25d91997-04-29 20:08:16 +0000852 if (!PyArg_ParseTuple(args, "s|O!O!:execfile",
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000853 &filename,
Guido van Rossum79f25d91997-04-29 20:08:16 +0000854 &PyDict_Type, &globals,
855 &PyDict_Type, &locals))
Guido van Rossum0f61f8a1992-02-25 18:55:05 +0000856 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000857 if (globals == Py_None) {
858 globals = PyEval_GetGlobals();
859 if (locals == Py_None)
860 locals = PyEval_GetLocals();
Guido van Rossum6135a871995-01-09 17:53:26 +0000861 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000862 else if (locals == Py_None)
Guido van Rossum6135a871995-01-09 17:53:26 +0000863 locals = globals;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000864 if (PyDict_GetItemString(globals, "__builtins__") == NULL) {
865 if (PyDict_SetItemString(globals, "__builtins__",
866 PyEval_GetBuiltins()) != 0)
Guido van Rossum6135a871995-01-09 17:53:26 +0000867 return NULL;
868 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000869 Py_BEGIN_ALLOW_THREADS
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000870 fp = fopen(filename, "r");
Guido van Rossum79f25d91997-04-29 20:08:16 +0000871 Py_END_ALLOW_THREADS
Guido van Rossum0f61f8a1992-02-25 18:55:05 +0000872 if (fp == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000873 PyErr_SetFromErrno(PyExc_IOError);
Guido van Rossum0f61f8a1992-02-25 18:55:05 +0000874 return NULL;
875 }
Guido van Rossumb05a5c71997-05-07 17:46:13 +0000876 res = PyRun_File(fp, filename, Py_file_input, globals, locals);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000877 Py_BEGIN_ALLOW_THREADS
Guido van Rossum0f61f8a1992-02-25 18:55:05 +0000878 fclose(fp);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000879 Py_END_ALLOW_THREADS
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000880 return res;
Guido van Rossum0f61f8a1992-02-25 18:55:05 +0000881}
882
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000883static char execfile_doc[] =
884"execfile(filename[, globals[, locals]])\n\
885\n\
886Read and execute a Python script from a file.\n\
887The globals and locals are dictionaries, defaulting to the current\n\
888globals and locals. If only globals is given, locals defaults to it.";
889
890
Guido van Rossum79f25d91997-04-29 20:08:16 +0000891static PyObject *
Guido van Rossum94390a41992-08-14 15:14:30 +0000892builtin_getattr(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000893 PyObject *self;
894 PyObject *args;
Guido van Rossum33894be1992-01-27 16:53:09 +0000895{
Guido van Rossum950ff291998-06-29 13:38:57 +0000896 PyObject *v, *result, *dflt = NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000897 PyObject *name;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000898
Guido van Rossum950ff291998-06-29 13:38:57 +0000899 if (!PyArg_ParseTuple(args, "OS|O:getattr", &v, &name, &dflt))
Guido van Rossum33894be1992-01-27 16:53:09 +0000900 return NULL;
Guido van Rossum950ff291998-06-29 13:38:57 +0000901 result = PyObject_GetAttr(v, name);
902 if (result == NULL && dflt != NULL) {
903 PyErr_Clear();
904 Py_INCREF(dflt);
905 result = dflt;
906 }
907 return result;
Guido van Rossum9bfef441993-03-29 10:43:31 +0000908}
909
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000910static char getattr_doc[] =
Guido van Rossum950ff291998-06-29 13:38:57 +0000911"getattr(object, name[, default]) -> value\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000912\n\
Guido van Rossum950ff291998-06-29 13:38:57 +0000913Get a named attribute from an object; getattr(x, 'y') is equivalent to x.y.\n\
914When a default argument is given, it is returned when the attribute doesn't\n\
915exist; without it, an exception is raised in that case.";
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000916
917
Guido van Rossum79f25d91997-04-29 20:08:16 +0000918static PyObject *
Guido van Rossum872537c1995-07-07 22:43:42 +0000919builtin_globals(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000920 PyObject *self;
921 PyObject *args;
Guido van Rossum872537c1995-07-07 22:43:42 +0000922{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000923 PyObject *d;
Guido van Rossum872537c1995-07-07 22:43:42 +0000924
Guido van Rossum43713e52000-02-29 13:59:29 +0000925 if (!PyArg_ParseTuple(args, ":globals"))
Guido van Rossum872537c1995-07-07 22:43:42 +0000926 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000927 d = PyEval_GetGlobals();
928 Py_INCREF(d);
Guido van Rossum872537c1995-07-07 22:43:42 +0000929 return d;
930}
931
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000932static char globals_doc[] =
933"globals() -> dictionary\n\
934\n\
935Return the dictionary containing the current scope's global variables.";
936
937
Guido van Rossum79f25d91997-04-29 20:08:16 +0000938static PyObject *
Guido van Rossum9bfef441993-03-29 10:43:31 +0000939builtin_hasattr(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000940 PyObject *self;
941 PyObject *args;
Guido van Rossum9bfef441993-03-29 10:43:31 +0000942{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000943 PyObject *v;
944 PyObject *name;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000945
Guido van Rossum79f25d91997-04-29 20:08:16 +0000946 if (!PyArg_ParseTuple(args, "OS:hasattr", &v, &name))
Guido van Rossum9bfef441993-03-29 10:43:31 +0000947 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000948 v = PyObject_GetAttr(v, name);
Guido van Rossum9bfef441993-03-29 10:43:31 +0000949 if (v == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000950 PyErr_Clear();
Guido van Rossum09df08a1998-05-22 00:51:39 +0000951 Py_INCREF(Py_False);
952 return Py_False;
Guido van Rossum9bfef441993-03-29 10:43:31 +0000953 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000954 Py_DECREF(v);
Guido van Rossum09df08a1998-05-22 00:51:39 +0000955 Py_INCREF(Py_True);
956 return Py_True;
Guido van Rossum33894be1992-01-27 16:53:09 +0000957}
958
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000959static char hasattr_doc[] =
960"hasattr(object, name) -> Boolean\n\
961\n\
962Return whether the object has an attribute with the given name.\n\
963(This is done by calling getattr(object, name) and catching exceptions.)";
964
965
Guido van Rossum79f25d91997-04-29 20:08:16 +0000966static PyObject *
Guido van Rossum5b722181993-03-30 17:46:03 +0000967builtin_id(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000968 PyObject *self;
969 PyObject *args;
Guido van Rossum5b722181993-03-30 17:46:03 +0000970{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000971 PyObject *v;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000972
Guido van Rossum79f25d91997-04-29 20:08:16 +0000973 if (!PyArg_ParseTuple(args, "O:id", &v))
Guido van Rossum5b722181993-03-30 17:46:03 +0000974 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000975 return PyInt_FromLong((long)v);
Guido van Rossum5b722181993-03-30 17:46:03 +0000976}
977
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000978static char id_doc[] =
979"id(object) -> integer\n\
980\n\
981Return the identity of an object. This is guaranteed to be unique among\n\
982simultaneously existing objects. (Hint: it's the object's memory address.)";
983
984
Guido van Rossum79f25d91997-04-29 20:08:16 +0000985static PyObject *
Guido van Rossum12d12c51993-10-26 17:58:25 +0000986builtin_map(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000987 PyObject *self;
988 PyObject *args;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000989{
990 typedef struct {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000991 PyObject *seq;
992 PySequenceMethods *sqf;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000993 int len;
994 } sequence;
995
Guido van Rossum79f25d91997-04-29 20:08:16 +0000996 PyObject *func, *result;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000997 sequence *seqs = NULL, *sqp;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000998 int n, len;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000999 register int i, j;
1000
Guido van Rossum79f25d91997-04-29 20:08:16 +00001001 n = PyTuple_Size(args);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001002 if (n < 2) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001003 PyErr_SetString(PyExc_TypeError,
1004 "map() requires at least two args");
Guido van Rossum12d12c51993-10-26 17:58:25 +00001005 return NULL;
1006 }
1007
Guido van Rossum79f25d91997-04-29 20:08:16 +00001008 func = PyTuple_GetItem(args, 0);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001009 n--;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001010
Guido van Rossumfa4ac711998-07-10 17:37:30 +00001011 if (func == Py_None && n == 1) {
1012 /* map(None, S) is the same as list(S). */
1013 return PySequence_List(PyTuple_GetItem(args, 1));
1014 }
1015
Guido van Rossum79f25d91997-04-29 20:08:16 +00001016 if ((seqs = PyMem_NEW(sequence, n)) == NULL) {
1017 PyErr_NoMemory();
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00001018 goto Fail_2;
1019 }
Guido van Rossum12d12c51993-10-26 17:58:25 +00001020
Guido van Rossum2d951851994-08-29 12:52:16 +00001021 for (len = 0, i = 0, sqp = seqs; i < n; ++i, ++sqp) {
Guido van Rossum12d12c51993-10-26 17:58:25 +00001022 int curlen;
Guido van Rossum09df08a1998-05-22 00:51:39 +00001023 PySequenceMethods *sqf;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001024
Guido van Rossum79f25d91997-04-29 20:08:16 +00001025 if ((sqp->seq = PyTuple_GetItem(args, i + 1)) == NULL)
Guido van Rossum12d12c51993-10-26 17:58:25 +00001026 goto Fail_2;
1027
Guido van Rossum09df08a1998-05-22 00:51:39 +00001028 sqp->sqf = sqf = sqp->seq->ob_type->tp_as_sequence;
1029 if (sqf == NULL ||
1030 sqf->sq_length == NULL ||
1031 sqf->sq_item == NULL)
1032 {
Guido van Rossum12d12c51993-10-26 17:58:25 +00001033 static char errmsg[] =
1034 "argument %d to map() must be a sequence object";
Guido van Rossum15e33a41997-04-30 19:00:27 +00001035 char errbuf[sizeof(errmsg) + 25];
Guido van Rossum12d12c51993-10-26 17:58:25 +00001036
1037 sprintf(errbuf, errmsg, i+2);
Guido van Rossum79f25d91997-04-29 20:08:16 +00001038 PyErr_SetString(PyExc_TypeError, errbuf);
Guido van Rossum12d12c51993-10-26 17:58:25 +00001039 goto Fail_2;
1040 }
1041
1042 if ((curlen = sqp->len = (*sqp->sqf->sq_length)(sqp->seq)) < 0)
1043 goto Fail_2;
1044
1045 if (curlen > len)
1046 len = curlen;
1047 }
1048
Guido van Rossum79f25d91997-04-29 20:08:16 +00001049 if ((result = (PyObject *) PyList_New(len)) == NULL)
Guido van Rossum12d12c51993-10-26 17:58:25 +00001050 goto Fail_2;
1051
Guido van Rossum2d951851994-08-29 12:52:16 +00001052 for (i = 0; ; ++i) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001053 PyObject *alist, *item=NULL, *value;
Guido van Rossum2d951851994-08-29 12:52:16 +00001054 int any = 0;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001055
Guido van Rossum79f25d91997-04-29 20:08:16 +00001056 if (func == Py_None && n == 1)
Guido van Rossum32120311995-07-10 13:52:21 +00001057 alist = NULL;
Guido van Rossum2d951851994-08-29 12:52:16 +00001058 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001059 if ((alist = PyTuple_New(n)) == NULL)
Guido van Rossum2d951851994-08-29 12:52:16 +00001060 goto Fail_1;
1061 }
Guido van Rossum12d12c51993-10-26 17:58:25 +00001062
1063 for (j = 0, sqp = seqs; j < n; ++j, ++sqp) {
Guido van Rossum2d951851994-08-29 12:52:16 +00001064 if (sqp->len < 0) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001065 Py_INCREF(Py_None);
1066 item = Py_None;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001067 }
Guido van Rossum12d12c51993-10-26 17:58:25 +00001068 else {
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00001069 item = (*sqp->sqf->sq_item)(sqp->seq, i);
Guido van Rossum2d951851994-08-29 12:52:16 +00001070 if (item == NULL) {
Barry Warsawcde8b1b1997-08-22 21:14:38 +00001071 if (PyErr_ExceptionMatches(
1072 PyExc_IndexError))
1073 {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001074 PyErr_Clear();
1075 Py_INCREF(Py_None);
1076 item = Py_None;
Guido van Rossum2d951851994-08-29 12:52:16 +00001077 sqp->len = -1;
1078 }
1079 else {
1080 goto Fail_0;
1081 }
1082 }
1083 else
1084 any = 1;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001085
Guido van Rossum12d12c51993-10-26 17:58:25 +00001086 }
Guido van Rossum32120311995-07-10 13:52:21 +00001087 if (!alist)
Guido van Rossum2d951851994-08-29 12:52:16 +00001088 break;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001089 if (PyTuple_SetItem(alist, j, item) < 0) {
1090 Py_DECREF(item);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00001091 goto Fail_0;
Guido van Rossum2d951851994-08-29 12:52:16 +00001092 }
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00001093 continue;
1094
1095 Fail_0:
Guido van Rossum79f25d91997-04-29 20:08:16 +00001096 Py_XDECREF(alist);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00001097 goto Fail_1;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001098 }
1099
Guido van Rossum32120311995-07-10 13:52:21 +00001100 if (!alist)
1101 alist = item;
Guido van Rossum2d951851994-08-29 12:52:16 +00001102
1103 if (!any) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001104 Py_DECREF(alist);
Guido van Rossum2d951851994-08-29 12:52:16 +00001105 break;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001106 }
Guido van Rossum2d951851994-08-29 12:52:16 +00001107
Guido van Rossum79f25d91997-04-29 20:08:16 +00001108 if (func == Py_None)
Guido van Rossum32120311995-07-10 13:52:21 +00001109 value = alist;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001110 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001111 value = PyEval_CallObject(func, alist);
1112 Py_DECREF(alist);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00001113 if (value == NULL)
1114 goto Fail_1;
Guido van Rossum2d951851994-08-29 12:52:16 +00001115 }
1116 if (i >= len) {
Barry Warsawfa77e091999-01-28 18:49:12 +00001117 int status = PyList_Append(result, value);
Barry Warsaw21332871999-01-28 04:21:35 +00001118 Py_DECREF(value);
Barry Warsawfa77e091999-01-28 18:49:12 +00001119 if (status < 0)
1120 goto Fail_1;
Guido van Rossum2d951851994-08-29 12:52:16 +00001121 }
1122 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001123 if (PyList_SetItem(result, i, value) < 0)
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00001124 goto Fail_1;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001125 }
1126 }
1127
Guido van Rossumfa4ac711998-07-10 17:37:30 +00001128 if (i < len && PyList_SetSlice(result, i, len, NULL) < 0)
1129 goto Fail_1;
1130
Guido van Rossum79f25d91997-04-29 20:08:16 +00001131 PyMem_DEL(seqs);
Guido van Rossum12d12c51993-10-26 17:58:25 +00001132 return result;
1133
Guido van Rossum12d12c51993-10-26 17:58:25 +00001134Fail_1:
Guido van Rossum79f25d91997-04-29 20:08:16 +00001135 Py_DECREF(result);
Guido van Rossum12d12c51993-10-26 17:58:25 +00001136Fail_2:
Guido van Rossum79f25d91997-04-29 20:08:16 +00001137 if (seqs) PyMem_DEL(seqs);
Guido van Rossum12d12c51993-10-26 17:58:25 +00001138 return NULL;
1139}
1140
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001141static char map_doc[] =
1142"map(function, sequence[, sequence, ...]) -> list\n\
1143\n\
1144Return a list of the results of applying the function to the items of\n\
1145the argument sequence(s). If more than one sequence is given, the\n\
1146function is called with an argument list consisting of the corresponding\n\
1147item of each sequence, substituting None for missing values when not all\n\
1148sequences have the same length. If the function is None, return a list of\n\
1149the items of the sequence (or a list of tuples if more than one sequence).";
1150
1151
Guido van Rossum79f25d91997-04-29 20:08:16 +00001152static PyObject *
Guido van Rossum94390a41992-08-14 15:14:30 +00001153builtin_setattr(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001154 PyObject *self;
1155 PyObject *args;
Guido van Rossum33894be1992-01-27 16:53:09 +00001156{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001157 PyObject *v;
1158 PyObject *name;
1159 PyObject *value;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001160
Guido van Rossum79f25d91997-04-29 20:08:16 +00001161 if (!PyArg_ParseTuple(args, "OSO:setattr", &v, &name, &value))
Guido van Rossum33894be1992-01-27 16:53:09 +00001162 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001163 if (PyObject_SetAttr(v, name, value) != 0)
Guido van Rossum33894be1992-01-27 16:53:09 +00001164 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001165 Py_INCREF(Py_None);
1166 return Py_None;
Guido van Rossum33894be1992-01-27 16:53:09 +00001167}
1168
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001169static char setattr_doc[] =
1170"setattr(object, name, value)\n\
1171\n\
1172Set a named attribute on an object; setattr(x, 'y', v) is equivalent to\n\
1173``x.y = v''.";
1174
1175
Guido van Rossum79f25d91997-04-29 20:08:16 +00001176static PyObject *
Guido van Rossum14144fc1994-08-29 12:53:40 +00001177builtin_delattr(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001178 PyObject *self;
1179 PyObject *args;
Guido van Rossum14144fc1994-08-29 12:53:40 +00001180{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001181 PyObject *v;
1182 PyObject *name;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001183
Guido van Rossum79f25d91997-04-29 20:08:16 +00001184 if (!PyArg_ParseTuple(args, "OS:delattr", &v, &name))
Guido van Rossum14144fc1994-08-29 12:53:40 +00001185 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001186 if (PyObject_SetAttr(v, name, (PyObject *)NULL) != 0)
Guido van Rossum14144fc1994-08-29 12:53:40 +00001187 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001188 Py_INCREF(Py_None);
1189 return Py_None;
Guido van Rossum14144fc1994-08-29 12:53:40 +00001190}
1191
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001192static char delattr_doc[] =
Guido van Rossumdf12a591998-11-23 22:13:04 +00001193"delattr(object, name)\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001194\n\
1195Delete a named attribute on an object; delattr(x, 'y') is equivalent to\n\
1196``del x.y''.";
1197
1198
Guido van Rossum79f25d91997-04-29 20:08:16 +00001199static PyObject *
Guido van Rossum9bfef441993-03-29 10:43:31 +00001200builtin_hash(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001201 PyObject *self;
1202 PyObject *args;
Guido van Rossum9bfef441993-03-29 10:43:31 +00001203{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001204 PyObject *v;
Guido van Rossum9bfef441993-03-29 10:43:31 +00001205 long x;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001206
Guido van Rossum79f25d91997-04-29 20:08:16 +00001207 if (!PyArg_ParseTuple(args, "O:hash", &v))
Guido van Rossum9bfef441993-03-29 10:43:31 +00001208 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001209 x = PyObject_Hash(v);
Guido van Rossum9bfef441993-03-29 10:43:31 +00001210 if (x == -1)
1211 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001212 return PyInt_FromLong(x);
Guido van Rossum9bfef441993-03-29 10:43:31 +00001213}
1214
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001215static char hash_doc[] =
1216"hash(object) -> integer\n\
1217\n\
1218Return a hash value for the object. Two objects with the same value have\n\
1219the same hash value. The reverse is not necessarily true, but likely.";
1220
1221
Guido van Rossum79f25d91997-04-29 20:08:16 +00001222static PyObject *
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001223builtin_hex(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001224 PyObject *self;
1225 PyObject *args;
Guido van Rossum006bcd41991-10-24 14:54:44 +00001226{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001227 PyObject *v;
1228 PyNumberMethods *nb;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001229
Guido van Rossum79f25d91997-04-29 20:08:16 +00001230 if (!PyArg_ParseTuple(args, "O:hex", &v))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001231 return NULL;
Guido van Rossum1899c2e1992-09-12 11:09:23 +00001232
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001233 if ((nb = v->ob_type->tp_as_number) == NULL ||
Guido van Rossum1899c2e1992-09-12 11:09:23 +00001234 nb->nb_hex == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001235 PyErr_SetString(PyExc_TypeError,
Guido van Rossum1899c2e1992-09-12 11:09:23 +00001236 "hex() argument can't be converted to hex");
1237 return NULL;
Guido van Rossum006bcd41991-10-24 14:54:44 +00001238 }
Guido van Rossum1899c2e1992-09-12 11:09:23 +00001239 return (*nb->nb_hex)(v);
Guido van Rossum006bcd41991-10-24 14:54:44 +00001240}
1241
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001242static char hex_doc[] =
1243"hex(number) -> string\n\
1244\n\
1245Return the hexadecimal representation of an integer or long integer.";
1246
1247
Guido van Rossum79f25d91997-04-29 20:08:16 +00001248static PyObject *builtin_raw_input Py_PROTO((PyObject *, PyObject *));
Guido van Rossum3165fe61992-09-25 21:59:05 +00001249
Guido van Rossum79f25d91997-04-29 20:08:16 +00001250static PyObject *
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001251builtin_input(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001252 PyObject *self;
1253 PyObject *args;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001254{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001255 PyObject *line;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001256 char *str;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001257 PyObject *res;
1258 PyObject *globals, *locals;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001259
1260 line = builtin_raw_input(self, args);
Guido van Rossum3165fe61992-09-25 21:59:05 +00001261 if (line == NULL)
1262 return line;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001263 if (!PyArg_Parse(line, "s;embedded '\\0' in input line", &str))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001264 return NULL;
1265 while (*str == ' ' || *str == '\t')
1266 str++;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001267 globals = PyEval_GetGlobals();
1268 locals = PyEval_GetLocals();
1269 if (PyDict_GetItemString(globals, "__builtins__") == NULL) {
1270 if (PyDict_SetItemString(globals, "__builtins__",
1271 PyEval_GetBuiltins()) != 0)
Guido van Rossum6135a871995-01-09 17:53:26 +00001272 return NULL;
1273 }
Guido van Rossumb05a5c71997-05-07 17:46:13 +00001274 res = PyRun_String(str, Py_eval_input, globals, locals);
Guido van Rossum79f25d91997-04-29 20:08:16 +00001275 Py_DECREF(line);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001276 return res;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001277}
1278
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001279static char input_doc[] =
1280"input([prompt]) -> value\n\
1281\n\
1282Equivalent to eval(raw_input(prompt)).";
1283
1284
Guido van Rossume8811f81997-02-14 15:48:05 +00001285static PyObject *
1286builtin_intern(self, args)
1287 PyObject *self;
1288 PyObject *args;
1289{
1290 PyObject *s;
Guido van Rossum43713e52000-02-29 13:59:29 +00001291 if (!PyArg_ParseTuple(args, "S:intern", &s))
Guido van Rossume8811f81997-02-14 15:48:05 +00001292 return NULL;
1293 Py_INCREF(s);
1294 PyString_InternInPlace(&s);
1295 return s;
1296}
1297
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001298static char intern_doc[] =
1299"intern(string) -> string\n\
1300\n\
1301``Intern'' the given string. This enters the string in the (global)\n\
1302table of interned strings whose purpose is to speed up dictionary lookups.\n\
1303Return the string itself or the previously interned string object with the\n\
1304same value.";
1305
1306
Guido van Rossum79f25d91997-04-29 20:08:16 +00001307static PyObject *
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001308builtin_int(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001309 PyObject *self;
1310 PyObject *args;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001311{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001312 PyObject *v;
Barry Warsaw226ae6c1999-10-12 19:54:53 +00001313 int base = -909; /* unlikely! */
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001314
Barry Warsaw226ae6c1999-10-12 19:54:53 +00001315 if (!PyArg_ParseTuple(args, "O|i:int", &v, &base))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001316 return NULL;
Barry Warsaw226ae6c1999-10-12 19:54:53 +00001317 if (base == -909)
1318 return PyNumber_Int(v);
Guido van Rossum9e896b32000-04-05 20:11:21 +00001319 else if (PyString_Check(v))
1320 return PyInt_FromString(PyString_AS_STRING(v), NULL, base);
1321 else if (PyUnicode_Check(v))
1322 return PyInt_FromUnicode(PyUnicode_AS_UNICODE(v),
1323 PyUnicode_GET_SIZE(v),
1324 base);
1325 else {
Barry Warsaw226ae6c1999-10-12 19:54:53 +00001326 PyErr_SetString(PyExc_TypeError,
1327 "can't convert non-string with explicit base");
1328 return NULL;
1329 }
Guido van Rossum3f5da241990-12-20 15:06:42 +00001330}
1331
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001332static char int_doc[] =
Barry Warsaw226ae6c1999-10-12 19:54:53 +00001333"int(x[, base]) -> integer\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001334\n\
Barry Warsaw226ae6c1999-10-12 19:54:53 +00001335Convert a string or number to an integer, if possible. A floating point\n\
1336argument will be truncated towards zero (this does not include a string\n\
1337representation of a floating point number!) When converting a string, use\n\
1338the optional base. It is an error to supply a base when converting a\n\
1339non-string.";
1340
1341
1342static PyObject *
1343builtin_long(self, args)
1344 PyObject *self;
1345 PyObject *args;
1346{
1347 PyObject *v;
1348 int base = -909; /* unlikely! */
1349
1350 if (!PyArg_ParseTuple(args, "O|i:long", &v, &base))
1351 return NULL;
1352 if (base == -909)
1353 return PyNumber_Long(v);
Guido van Rossum9e896b32000-04-05 20:11:21 +00001354 else if (PyString_Check(v))
1355 return PyLong_FromString(PyString_AS_STRING(v), NULL, base);
1356 else if (PyUnicode_Check(v))
1357 return PyLong_FromUnicode(PyUnicode_AS_UNICODE(v),
1358 PyUnicode_GET_SIZE(v),
1359 base);
1360 else {
Barry Warsaw226ae6c1999-10-12 19:54:53 +00001361 PyErr_SetString(PyExc_TypeError,
1362 "can't convert non-string with explicit base");
1363 return NULL;
1364 }
Barry Warsaw226ae6c1999-10-12 19:54:53 +00001365}
1366
1367static char long_doc[] =
1368"long(x) -> long integer\n\
1369long(x, base) -> long integer\n\
1370\n\
1371Convert a string or number to a long integer, if possible. A floating\n\
1372point argument will be truncated towards zero (this does not include a\n\
1373string representation of a floating point number!) When converting a\n\
1374string, use the given base. It is an error to supply a base when\n\
1375converting a non-string.";
1376
1377
1378static PyObject *
1379builtin_float(self, args)
1380 PyObject *self;
1381 PyObject *args;
1382{
1383 PyObject *v;
1384
1385 if (!PyArg_ParseTuple(args, "O:float", &v))
1386 return NULL;
1387 if (PyString_Check(v))
1388 return PyFloat_FromString(v, NULL);
1389 return PyNumber_Float(v);
1390}
1391
1392static char float_doc[] =
1393"float(x) -> floating point number\n\
1394\n\
1395Convert a string or number to a floating point number, if possible.";
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001396
1397
Guido van Rossum79f25d91997-04-29 20:08:16 +00001398static PyObject *
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001399builtin_len(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001400 PyObject *self;
1401 PyObject *args;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001402{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001403 PyObject *v;
Guido van Rossum8ea9f4d1998-06-29 22:26:50 +00001404 long res;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001405
Guido van Rossum79f25d91997-04-29 20:08:16 +00001406 if (!PyArg_ParseTuple(args, "O:len", &v))
Guido van Rossum3f5da241990-12-20 15:06:42 +00001407 return NULL;
Guido van Rossum8ea9f4d1998-06-29 22:26:50 +00001408 res = PyObject_Length(v);
1409 if (res < 0 && PyErr_Occurred())
1410 return NULL;
1411 return PyInt_FromLong(res);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001412}
1413
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001414static char len_doc[] =
1415"len(object) -> integer\n\
1416\n\
1417Return the number of items of a sequence or mapping.";
1418
1419
Guido van Rossum79f25d91997-04-29 20:08:16 +00001420static PyObject *
Guido van Rossumd1705771996-04-09 02:41:06 +00001421builtin_list(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001422 PyObject *self;
1423 PyObject *args;
Guido van Rossumd1705771996-04-09 02:41:06 +00001424{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001425 PyObject *v;
Guido van Rossumd1705771996-04-09 02:41:06 +00001426
Guido van Rossum79f25d91997-04-29 20:08:16 +00001427 if (!PyArg_ParseTuple(args, "O:list", &v))
Guido van Rossumd1705771996-04-09 02:41:06 +00001428 return NULL;
Guido van Rossum09df08a1998-05-22 00:51:39 +00001429 return PySequence_List(v);
Guido van Rossumd1705771996-04-09 02:41:06 +00001430}
1431
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001432static char list_doc[] =
1433"list(sequence) -> list\n\
1434\n\
1435Return a new list whose items are the same as those of the argument sequence.";
1436
Guido van Rossum8861b741996-07-30 16:49:37 +00001437
1438static PyObject *
1439builtin_slice(self, args)
1440 PyObject *self;
1441 PyObject *args;
1442{
Guido van Rossum09df08a1998-05-22 00:51:39 +00001443 PyObject *start, *stop, *step;
Guido van Rossum8861b741996-07-30 16:49:37 +00001444
Guido van Rossum09df08a1998-05-22 00:51:39 +00001445 start = stop = step = NULL;
Guido van Rossum8861b741996-07-30 16:49:37 +00001446
Guido van Rossum09df08a1998-05-22 00:51:39 +00001447 if (!PyArg_ParseTuple(args, "O|OO:slice", &start, &stop, &step))
1448 return NULL;
Guido van Rossum8861b741996-07-30 16:49:37 +00001449
Guido van Rossum09df08a1998-05-22 00:51:39 +00001450 /* This swapping of stop and start is to maintain similarity with
1451 range(). */
1452 if (stop == NULL) {
1453 stop = start;
1454 start = NULL;
1455 }
1456 return PySlice_New(start, stop, step);
Guido van Rossum8861b741996-07-30 16:49:37 +00001457}
1458
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001459static char slice_doc[] =
Fred Drake3d587441999-07-19 15:21:16 +00001460"slice([start,] stop[, step]) -> slice object\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001461\n\
1462Create a slice object. This is used for slicing by the Numeric extensions.";
1463
1464
Guido van Rossum79f25d91997-04-29 20:08:16 +00001465static PyObject *
Guido van Rossum872537c1995-07-07 22:43:42 +00001466builtin_locals(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001467 PyObject *self;
1468 PyObject *args;
Guido van Rossum872537c1995-07-07 22:43:42 +00001469{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001470 PyObject *d;
Guido van Rossum872537c1995-07-07 22:43:42 +00001471
Guido van Rossum43713e52000-02-29 13:59:29 +00001472 if (!PyArg_ParseTuple(args, ":locals"))
Guido van Rossum872537c1995-07-07 22:43:42 +00001473 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001474 d = PyEval_GetLocals();
1475 Py_INCREF(d);
Guido van Rossum872537c1995-07-07 22:43:42 +00001476 return d;
1477}
1478
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001479static char locals_doc[] =
1480"locals() -> dictionary\n\
1481\n\
1482Return the dictionary containing the current scope's local variables.";
1483
1484
Guido van Rossum79f25d91997-04-29 20:08:16 +00001485static PyObject *
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001486min_max(args, sign)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001487 PyObject *args;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001488 int sign;
1489{
Guido van Rossum2d951851994-08-29 12:52:16 +00001490 int i;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001491 PyObject *v, *w, *x;
1492 PySequenceMethods *sq;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001493
Guido van Rossum79f25d91997-04-29 20:08:16 +00001494 if (PyTuple_Size(args) > 1)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001495 v = args;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001496 else if (!PyArg_ParseTuple(args, "O:min/max", &v))
Guido van Rossum3f5da241990-12-20 15:06:42 +00001497 return NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001498 sq = v->ob_type->tp_as_sequence;
Guido van Rossum09df08a1998-05-22 00:51:39 +00001499 if (sq == NULL || sq->sq_item == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001500 PyErr_SetString(PyExc_TypeError,
1501 "min() or max() of non-sequence");
Guido van Rossum3f5da241990-12-20 15:06:42 +00001502 return NULL;
1503 }
Guido van Rossum2d951851994-08-29 12:52:16 +00001504 w = NULL;
1505 for (i = 0; ; i++) {
Guido van Rossum3f5da241990-12-20 15:06:42 +00001506 x = (*sq->sq_item)(v, i); /* Implies INCREF */
Guido van Rossum2d951851994-08-29 12:52:16 +00001507 if (x == NULL) {
Barry Warsawcde8b1b1997-08-22 21:14:38 +00001508 if (PyErr_ExceptionMatches(PyExc_IndexError)) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001509 PyErr_Clear();
Guido van Rossum2d951851994-08-29 12:52:16 +00001510 break;
1511 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00001512 Py_XDECREF(w);
Guido van Rossum2d951851994-08-29 12:52:16 +00001513 return NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001514 }
Guido van Rossum2d951851994-08-29 12:52:16 +00001515 if (w == NULL)
1516 w = x;
1517 else {
Guido van Rossumc8b6df91997-05-23 00:06:51 +00001518 int c = PyObject_Compare(x, w);
1519 if (c && PyErr_Occurred()) {
1520 Py_DECREF(x);
1521 Py_XDECREF(w);
1522 return NULL;
1523 }
1524 if (c * sign > 0) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001525 Py_DECREF(w);
Guido van Rossum2d951851994-08-29 12:52:16 +00001526 w = x;
1527 }
1528 else
Guido van Rossum79f25d91997-04-29 20:08:16 +00001529 Py_DECREF(x);
Guido van Rossum2d951851994-08-29 12:52:16 +00001530 }
Guido van Rossum3f5da241990-12-20 15:06:42 +00001531 }
Guido van Rossum2d951851994-08-29 12:52:16 +00001532 if (w == NULL)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001533 PyErr_SetString(PyExc_ValueError,
1534 "min() or max() of empty sequence");
Guido van Rossum3f5da241990-12-20 15:06:42 +00001535 return w;
1536}
1537
Guido van Rossum79f25d91997-04-29 20:08:16 +00001538static PyObject *
Guido van Rossum3f5da241990-12-20 15:06:42 +00001539builtin_min(self, v)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001540 PyObject *self;
1541 PyObject *v;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001542{
1543 return min_max(v, -1);
1544}
1545
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001546static char min_doc[] =
1547"min(sequence) -> value\n\
1548min(a, b, c, ...) -> value\n\
1549\n\
1550With a single sequence argument, return its smallest item.\n\
1551With two or more arguments, return the smallest argument.";
1552
1553
Guido van Rossum79f25d91997-04-29 20:08:16 +00001554static PyObject *
Guido van Rossum3f5da241990-12-20 15:06:42 +00001555builtin_max(self, v)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001556 PyObject *self;
1557 PyObject *v;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001558{
1559 return min_max(v, 1);
1560}
1561
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001562static char max_doc[] =
1563"max(sequence) -> value\n\
1564max(a, b, c, ...) -> value\n\
1565\n\
1566With a single sequence argument, return its largest item.\n\
1567With two or more arguments, return the largest argument.";
1568
1569
Guido van Rossum79f25d91997-04-29 20:08:16 +00001570static PyObject *
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001571builtin_oct(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001572 PyObject *self;
1573 PyObject *args;
Guido van Rossum006bcd41991-10-24 14:54:44 +00001574{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001575 PyObject *v;
1576 PyNumberMethods *nb;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001577
Guido van Rossum79f25d91997-04-29 20:08:16 +00001578 if (!PyArg_ParseTuple(args, "O:oct", &v))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001579 return NULL;
Guido van Rossum1899c2e1992-09-12 11:09:23 +00001580 if (v == NULL || (nb = v->ob_type->tp_as_number) == NULL ||
1581 nb->nb_oct == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001582 PyErr_SetString(PyExc_TypeError,
Guido van Rossum1899c2e1992-09-12 11:09:23 +00001583 "oct() argument can't be converted to oct");
1584 return NULL;
Guido van Rossum006bcd41991-10-24 14:54:44 +00001585 }
Guido van Rossum1899c2e1992-09-12 11:09:23 +00001586 return (*nb->nb_oct)(v);
Guido van Rossum006bcd41991-10-24 14:54:44 +00001587}
1588
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001589static char oct_doc[] =
1590"oct(number) -> string\n\
1591\n\
1592Return the octal representation of an integer or long integer.";
1593
1594
Guido van Rossum79f25d91997-04-29 20:08:16 +00001595static PyObject *
Guido van Rossum94390a41992-08-14 15:14:30 +00001596builtin_open(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001597 PyObject *self;
1598 PyObject *args;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001599{
Guido van Rossum2d951851994-08-29 12:52:16 +00001600 char *name;
1601 char *mode = "r";
1602 int bufsize = -1;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001603 PyObject *f;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001604
Guido van Rossum79f25d91997-04-29 20:08:16 +00001605 if (!PyArg_ParseTuple(args, "s|si:open", &name, &mode, &bufsize))
Guido van Rossum3f5da241990-12-20 15:06:42 +00001606 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001607 f = PyFile_FromString(name, mode);
Guido van Rossum2d951851994-08-29 12:52:16 +00001608 if (f != NULL)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001609 PyFile_SetBufSize(f, bufsize);
Guido van Rossum2d951851994-08-29 12:52:16 +00001610 return f;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001611}
1612
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001613static char open_doc[] =
1614"open(filename[, mode[, buffering]]) -> file object\n\
1615\n\
1616Open a file. The mode can be 'r', 'w' or 'a' for reading (default),\n\
1617writing or appending. The file will be created if it doesn't exist\n\
1618when opened for writing or appending; it will be truncated when\n\
1619opened for writing. Add a 'b' to the mode for binary files.\n\
1620Add a '+' to the mode to allow simultaneous reading and writing.\n\
1621If the buffering argument is given, 0 means unbuffered, 1 means line\n\
1622buffered, and larger numbers specify the buffer size.";
1623
1624
Guido van Rossum79f25d91997-04-29 20:08:16 +00001625static PyObject *
Guido van Rossum94390a41992-08-14 15:14:30 +00001626builtin_ord(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001627 PyObject *self;
1628 PyObject *args;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001629{
Guido van Rossum09095f32000-03-10 23:00:52 +00001630 PyObject *obj;
1631 long ord;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001632
Guido van Rossum09095f32000-03-10 23:00:52 +00001633 if (!PyArg_ParseTuple(args, "O:ord", &obj))
Guido van Rossum3f5da241990-12-20 15:06:42 +00001634 return NULL;
Guido van Rossum09095f32000-03-10 23:00:52 +00001635
1636 if (PyString_Check(obj) && PyString_GET_SIZE(obj) == 1)
1637 ord = (long)((unsigned char)*PyString_AS_STRING(obj));
1638 else if (PyUnicode_Check(obj) && PyUnicode_GET_SIZE(obj) == 1)
1639 ord = (long)*PyUnicode_AS_UNICODE(obj);
1640 else {
1641 PyErr_SetString(PyExc_TypeError,
1642 "expected a string or unicode character");
1643 return NULL;
1644 }
1645
1646 return PyInt_FromLong(ord);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001647}
1648
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001649static char ord_doc[] =
1650"ord(c) -> integer\n\
1651\n\
Guido van Rossum09095f32000-03-10 23:00:52 +00001652Return the integer ordinal of a one character [unicode] string.";
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001653
1654
Guido van Rossum79f25d91997-04-29 20:08:16 +00001655static PyObject *
Guido van Rossum6a00cd81995-01-07 12:39:01 +00001656builtin_pow(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001657 PyObject *self;
1658 PyObject *args;
Guido van Rossum6a00cd81995-01-07 12:39:01 +00001659{
Guido van Rossum09df08a1998-05-22 00:51:39 +00001660 PyObject *v, *w, *z = Py_None;
Guido van Rossum6a00cd81995-01-07 12:39:01 +00001661
Guido van Rossum79f25d91997-04-29 20:08:16 +00001662 if (!PyArg_ParseTuple(args, "OO|O:pow", &v, &w, &z))
Guido van Rossum6a00cd81995-01-07 12:39:01 +00001663 return NULL;
Guido van Rossum09df08a1998-05-22 00:51:39 +00001664 return PyNumber_Power(v, w, z);
Guido van Rossumd4905451991-05-05 20:00:36 +00001665}
1666
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001667static char pow_doc[] =
1668"pow(x, y[, z]) -> number\n\
1669\n\
1670With two arguments, equivalent to x**y. With three arguments,\n\
1671equivalent to (x**y) % z, but may be more efficient (e.g. for longs).";
1672
1673
Guido van Rossum124eff01999-02-23 16:11:01 +00001674/* Return number of items in range/xrange (lo, hi, step). step > 0
1675 * required. Return a value < 0 if & only if the true value is too
1676 * large to fit in a signed long.
1677 */
1678static long
1679get_len_of_range(lo, hi, step)
1680 long lo;
1681 long hi;
1682 long step; /* must be > 0 */
1683{
1684 /* -------------------------------------------------------------
1685 If lo >= hi, the range is empty.
1686 Else if n values are in the range, the last one is
1687 lo + (n-1)*step, which must be <= hi-1. Rearranging,
1688 n <= (hi - lo - 1)/step + 1, so taking the floor of the RHS gives
1689 the proper value. Since lo < hi in this case, hi-lo-1 >= 0, so
1690 the RHS is non-negative and so truncation is the same as the
1691 floor. Letting M be the largest positive long, the worst case
1692 for the RHS numerator is hi=M, lo=-M-1, and then
1693 hi-lo-1 = M-(-M-1)-1 = 2*M. Therefore unsigned long has enough
1694 precision to compute the RHS exactly.
1695 ---------------------------------------------------------------*/
1696 long n = 0;
1697 if (lo < hi) {
1698 unsigned long uhi = (unsigned long)hi;
1699 unsigned long ulo = (unsigned long)lo;
1700 unsigned long diff = uhi - ulo - 1;
1701 n = (long)(diff / (unsigned long)step + 1);
1702 }
1703 return n;
1704}
1705
Guido van Rossum79f25d91997-04-29 20:08:16 +00001706static PyObject *
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001707builtin_range(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001708 PyObject *self;
1709 PyObject *args;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001710{
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001711 long ilow = 0, ihigh = 0, istep = 1;
Guido van Rossum124eff01999-02-23 16:11:01 +00001712 long bign;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001713 int i, n;
Guido van Rossum124eff01999-02-23 16:11:01 +00001714
Guido van Rossum79f25d91997-04-29 20:08:16 +00001715 PyObject *v;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001716
Guido van Rossum79f25d91997-04-29 20:08:16 +00001717 if (PyTuple_Size(args) <= 1) {
1718 if (!PyArg_ParseTuple(args,
Guido van Rossum0865dd91995-01-17 16:30:22 +00001719 "l;range() requires 1-3 int arguments",
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001720 &ihigh))
1721 return NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001722 }
1723 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001724 if (!PyArg_ParseTuple(args,
Guido van Rossum0865dd91995-01-17 16:30:22 +00001725 "ll|l;range() requires 1-3 int arguments",
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001726 &ilow, &ihigh, &istep))
Guido van Rossum3f5da241990-12-20 15:06:42 +00001727 return NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001728 }
1729 if (istep == 0) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001730 PyErr_SetString(PyExc_ValueError, "zero step for range()");
Guido van Rossum3f5da241990-12-20 15:06:42 +00001731 return NULL;
1732 }
Guido van Rossum124eff01999-02-23 16:11:01 +00001733 if (istep > 0)
1734 bign = get_len_of_range(ilow, ihigh, istep);
1735 else
1736 bign = get_len_of_range(ihigh, ilow, -istep);
1737 n = (int)bign;
1738 if (bign < 0 || (long)n != bign) {
Guido van Rossume23cde21999-01-12 05:07:47 +00001739 PyErr_SetString(PyExc_OverflowError,
Guido van Rossum124eff01999-02-23 16:11:01 +00001740 "range() has too many items");
Guido van Rossume23cde21999-01-12 05:07:47 +00001741 return NULL;
1742 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00001743 v = PyList_New(n);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001744 if (v == NULL)
1745 return NULL;
1746 for (i = 0; i < n; i++) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001747 PyObject *w = PyInt_FromLong(ilow);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001748 if (w == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001749 Py_DECREF(v);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001750 return NULL;
1751 }
Guido van Rossuma937d141998-04-24 18:22:02 +00001752 PyList_SET_ITEM(v, i, w);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001753 ilow += istep;
1754 }
1755 return v;
1756}
1757
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001758static char range_doc[] =
1759"range([start,] stop[, step]) -> list of integers\n\
1760\n\
1761Return a list containing an arithmetic progression of integers.\n\
1762range(i, j) returns [i, i+1, i+2, ..., j-1]; start (!) defaults to 0.\n\
1763When step is given, it specifies the increment (or decrement).\n\
1764For example, range(4) returns [0, 1, 2, 3]. The end point is omitted!\n\
1765These are exactly the valid indices for a list of 4 elements.";
1766
1767
Guido van Rossum79f25d91997-04-29 20:08:16 +00001768static PyObject *
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001769builtin_xrange(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001770 PyObject *self;
1771 PyObject *args;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001772{
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001773 long ilow = 0, ihigh = 0, istep = 1;
Guido van Rossum0865dd91995-01-17 16:30:22 +00001774 long n;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001775
Guido van Rossum79f25d91997-04-29 20:08:16 +00001776 if (PyTuple_Size(args) <= 1) {
1777 if (!PyArg_ParseTuple(args,
Guido van Rossum0865dd91995-01-17 16:30:22 +00001778 "l;xrange() requires 1-3 int arguments",
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001779 &ihigh))
1780 return NULL;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001781 }
1782 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001783 if (!PyArg_ParseTuple(args,
Guido van Rossum0865dd91995-01-17 16:30:22 +00001784 "ll|l;xrange() requires 1-3 int arguments",
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001785 &ilow, &ihigh, &istep))
Guido van Rossum12d12c51993-10-26 17:58:25 +00001786 return NULL;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001787 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001788 if (istep == 0) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001789 PyErr_SetString(PyExc_ValueError, "zero step for xrange()");
Guido van Rossum12d12c51993-10-26 17:58:25 +00001790 return NULL;
1791 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001792 if (istep > 0)
Guido van Rossum124eff01999-02-23 16:11:01 +00001793 n = get_len_of_range(ilow, ihigh, istep);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001794 else
Guido van Rossum124eff01999-02-23 16:11:01 +00001795 n = get_len_of_range(ihigh, ilow, -istep);
1796 if (n < 0) {
1797 PyErr_SetString(PyExc_OverflowError,
1798 "xrange() has more than sys.maxint items");
1799 return NULL;
1800 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00001801 return PyRange_New(ilow, n, istep, 1);
Guido van Rossum12d12c51993-10-26 17:58:25 +00001802}
1803
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001804static char xrange_doc[] =
1805"xrange([start,] stop[, step]) -> xrange object\n\
1806\n\
1807Like range(), but instead of returning a list, returns an object that\n\
1808generates the numbers in the range on demand. This is slightly slower\n\
1809than range() but more memory efficient.";
1810
1811
Guido van Rossum79f25d91997-04-29 20:08:16 +00001812static PyObject *
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001813builtin_raw_input(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001814 PyObject *self;
1815 PyObject *args;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001816{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001817 PyObject *v = NULL;
1818 PyObject *f;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001819
Guido van Rossum79f25d91997-04-29 20:08:16 +00001820 if (!PyArg_ParseTuple(args, "|O:[raw_]input", &v))
Guido van Rossum3165fe61992-09-25 21:59:05 +00001821 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001822 if (PyFile_AsFile(PySys_GetObject("stdin")) == stdin &&
1823 PyFile_AsFile(PySys_GetObject("stdout")) == stdout &&
Guido van Rossum53bb7ff1995-07-26 16:26:31 +00001824 isatty(fileno(stdin)) && isatty(fileno(stdout))) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001825 PyObject *po;
Guido van Rossum872537c1995-07-07 22:43:42 +00001826 char *prompt;
1827 char *s;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001828 PyObject *result;
Guido van Rossum872537c1995-07-07 22:43:42 +00001829 if (v != NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001830 po = PyObject_Str(v);
Guido van Rossum872537c1995-07-07 22:43:42 +00001831 if (po == NULL)
1832 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001833 prompt = PyString_AsString(po);
Guido van Rossumd9b52081998-06-26 18:25:38 +00001834 if (prompt == NULL)
1835 return NULL;
Guido van Rossum872537c1995-07-07 22:43:42 +00001836 }
1837 else {
1838 po = NULL;
1839 prompt = "";
1840 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00001841 s = PyOS_Readline(prompt);
1842 Py_XDECREF(po);
Guido van Rossum872537c1995-07-07 22:43:42 +00001843 if (s == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001844 PyErr_SetNone(PyExc_KeyboardInterrupt);
Guido van Rossum872537c1995-07-07 22:43:42 +00001845 return NULL;
1846 }
1847 if (*s == '\0') {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001848 PyErr_SetNone(PyExc_EOFError);
Guido van Rossum872537c1995-07-07 22:43:42 +00001849 result = NULL;
1850 }
1851 else { /* strip trailing '\n' */
Guido van Rossum79f25d91997-04-29 20:08:16 +00001852 result = PyString_FromStringAndSize(s, strlen(s)-1);
Guido van Rossum872537c1995-07-07 22:43:42 +00001853 }
1854 free(s);
1855 return result;
1856 }
Guido van Rossum90933611991-06-07 16:10:43 +00001857 if (v != NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001858 f = PySys_GetObject("stdout");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001859 if (f == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001860 PyErr_SetString(PyExc_RuntimeError, "lost sys.stdout");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001861 return NULL;
1862 }
Guido van Rossumc8b6df91997-05-23 00:06:51 +00001863 if (Py_FlushLine() != 0 ||
1864 PyFile_WriteObject(v, f, Py_PRINT_RAW) != 0)
Guido van Rossum90933611991-06-07 16:10:43 +00001865 return NULL;
1866 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00001867 f = PySys_GetObject("stdin");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001868 if (f == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001869 PyErr_SetString(PyExc_RuntimeError, "lost sys.stdin");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001870 return NULL;
1871 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00001872 return PyFile_GetLine(f, -1);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001873}
1874
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001875static char raw_input_doc[] =
1876"raw_input([prompt]) -> string\n\
1877\n\
1878Read a string from standard input. The trailing newline is stripped.\n\
1879If the user hits EOF (Unix: Ctl-D, Windows: Ctl-Z+Return), raise EOFError.\n\
1880On Unix, GNU readline is used if enabled. The prompt string, if given,\n\
1881is printed without a trailing newline before reading.";
1882
1883
Guido van Rossum79f25d91997-04-29 20:08:16 +00001884static PyObject *
Guido van Rossum12d12c51993-10-26 17:58:25 +00001885builtin_reduce(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001886 PyObject *self;
1887 PyObject *args;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001888{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001889 PyObject *seq, *func, *result = NULL;
1890 PySequenceMethods *sqf;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001891 register int i;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001892
Guido van Rossum79f25d91997-04-29 20:08:16 +00001893 if (!PyArg_ParseTuple(args, "OO|O:reduce", &func, &seq, &result))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001894 return NULL;
1895 if (result != NULL)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001896 Py_INCREF(result);
Guido van Rossum12d12c51993-10-26 17:58:25 +00001897
Guido van Rossum09df08a1998-05-22 00:51:39 +00001898 sqf = seq->ob_type->tp_as_sequence;
1899 if (sqf == NULL || sqf->sq_item == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001900 PyErr_SetString(PyExc_TypeError,
Guido van Rossum12d12c51993-10-26 17:58:25 +00001901 "2nd argument to reduce() must be a sequence object");
1902 return NULL;
1903 }
1904
Guido van Rossum79f25d91997-04-29 20:08:16 +00001905 if ((args = PyTuple_New(2)) == NULL)
Guido van Rossum2d951851994-08-29 12:52:16 +00001906 goto Fail;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001907
Guido van Rossum2d951851994-08-29 12:52:16 +00001908 for (i = 0; ; ++i) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001909 PyObject *op2;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001910
1911 if (args->ob_refcnt > 1) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001912 Py_DECREF(args);
1913 if ((args = PyTuple_New(2)) == NULL)
Guido van Rossum2d951851994-08-29 12:52:16 +00001914 goto Fail;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001915 }
1916
Guido van Rossum2d951851994-08-29 12:52:16 +00001917 if ((op2 = (*sqf->sq_item)(seq, i)) == NULL) {
Barry Warsawcde8b1b1997-08-22 21:14:38 +00001918 if (PyErr_ExceptionMatches(PyExc_IndexError)) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001919 PyErr_Clear();
Guido van Rossum2d951851994-08-29 12:52:16 +00001920 break;
1921 }
1922 goto Fail;
1923 }
Guido van Rossum12d12c51993-10-26 17:58:25 +00001924
Guido van Rossum2d951851994-08-29 12:52:16 +00001925 if (result == NULL)
1926 result = op2;
1927 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001928 PyTuple_SetItem(args, 0, result);
1929 PyTuple_SetItem(args, 1, op2);
1930 if ((result = PyEval_CallObject(func, args)) == NULL)
Guido van Rossum2d951851994-08-29 12:52:16 +00001931 goto Fail;
1932 }
Guido van Rossum12d12c51993-10-26 17:58:25 +00001933 }
1934
Guido van Rossum79f25d91997-04-29 20:08:16 +00001935 Py_DECREF(args);
Guido van Rossum12d12c51993-10-26 17:58:25 +00001936
Guido van Rossum2d951851994-08-29 12:52:16 +00001937 if (result == NULL)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001938 PyErr_SetString(PyExc_TypeError,
Guido van Rossum2d951851994-08-29 12:52:16 +00001939 "reduce of empty sequence with no initial value");
1940
Guido van Rossum12d12c51993-10-26 17:58:25 +00001941 return result;
1942
Guido van Rossum2d951851994-08-29 12:52:16 +00001943Fail:
Guido van Rossum79f25d91997-04-29 20:08:16 +00001944 Py_XDECREF(args);
1945 Py_XDECREF(result);
Guido van Rossum12d12c51993-10-26 17:58:25 +00001946 return NULL;
1947}
1948
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001949static char reduce_doc[] =
1950"reduce(function, sequence[, initial]) -> value\n\
1951\n\
1952Apply a function of two arguments cumulatively to the items of a sequence,\n\
1953from left to right, so as to reduce the sequence to a single value.\n\
1954For example, reduce(lambda x, y: x+y, [1, 2, 3, 4, 5]) calculates\n\
1955((((1+2)+3)+4)+5). If initial is present, it is placed before the items\n\
1956of the sequence in the calculation, and serves as a default when the\n\
1957sequence is empty.";
1958
1959
Guido van Rossum79f25d91997-04-29 20:08:16 +00001960static PyObject *
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001961builtin_reload(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001962 PyObject *self;
1963 PyObject *args;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001964{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001965 PyObject *v;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001966
Guido van Rossum79f25d91997-04-29 20:08:16 +00001967 if (!PyArg_ParseTuple(args, "O:reload", &v))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001968 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001969 return PyImport_ReloadModule(v);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001970}
1971
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001972static char reload_doc[] =
1973"reload(module) -> module\n\
1974\n\
1975Reload the module. The module must have been successfully imported before.";
1976
1977
Guido van Rossum79f25d91997-04-29 20:08:16 +00001978static PyObject *
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001979builtin_repr(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001980 PyObject *self;
1981 PyObject *args;
Guido van Rossumc89705d1992-11-26 08:54:07 +00001982{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001983 PyObject *v;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001984
Guido van Rossum79f25d91997-04-29 20:08:16 +00001985 if (!PyArg_ParseTuple(args, "O:repr", &v))
Guido van Rossumc89705d1992-11-26 08:54:07 +00001986 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001987 return PyObject_Repr(v);
Guido van Rossumc89705d1992-11-26 08:54:07 +00001988}
1989
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001990static char repr_doc[] =
1991"repr(object) -> string\n\
1992\n\
1993Return the canonical string representation of the object.\n\
1994For most object types, eval(repr(object)) == object.";
1995
1996
Guido van Rossum79f25d91997-04-29 20:08:16 +00001997static PyObject *
Guido van Rossum9e51f9b1993-02-12 16:29:05 +00001998builtin_round(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001999 PyObject *self;
2000 PyObject *args;
Guido van Rossum9e51f9b1993-02-12 16:29:05 +00002001{
Guido van Rossum9e51f9b1993-02-12 16:29:05 +00002002 double x;
2003 double f;
2004 int ndigits = 0;
Guido van Rossum9e51f9b1993-02-12 16:29:05 +00002005 int i;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002006
Guido van Rossum79f25d91997-04-29 20:08:16 +00002007 if (!PyArg_ParseTuple(args, "d|i:round", &x, &ndigits))
Guido van Rossum9e51f9b1993-02-12 16:29:05 +00002008 return NULL;
Guido van Rossum9e51f9b1993-02-12 16:29:05 +00002009 f = 1.0;
Guido van Rossum1e162d31998-05-09 14:42:25 +00002010 i = abs(ndigits);
2011 while (--i >= 0)
Guido van Rossum9e51f9b1993-02-12 16:29:05 +00002012 f = f*10.0;
Guido van Rossum1e162d31998-05-09 14:42:25 +00002013 if (ndigits < 0)
2014 x /= f;
Guido van Rossum9e51f9b1993-02-12 16:29:05 +00002015 else
Guido van Rossum1e162d31998-05-09 14:42:25 +00002016 x *= f;
2017 if (x >= 0.0)
2018 x = floor(x + 0.5);
2019 else
2020 x = ceil(x - 0.5);
2021 if (ndigits < 0)
2022 x *= f;
2023 else
2024 x /= f;
2025 return PyFloat_FromDouble(x);
Guido van Rossum9e51f9b1993-02-12 16:29:05 +00002026}
2027
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002028static char round_doc[] =
2029"round(number[, ndigits]) -> floating point number\n\
2030\n\
2031Round a number to a given precision in decimal digits (default 0 digits).\n\
2032This always returns a floating point number. Precision may be negative.";
2033
2034
Guido van Rossum79f25d91997-04-29 20:08:16 +00002035static PyObject *
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002036builtin_str(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00002037 PyObject *self;
2038 PyObject *args;
Guido van Rossumc89705d1992-11-26 08:54:07 +00002039{
Guido van Rossum79f25d91997-04-29 20:08:16 +00002040 PyObject *v;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002041
Guido van Rossum79f25d91997-04-29 20:08:16 +00002042 if (!PyArg_ParseTuple(args, "O:str", &v))
Guido van Rossumc89705d1992-11-26 08:54:07 +00002043 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002044 return PyObject_Str(v);
Guido van Rossumc89705d1992-11-26 08:54:07 +00002045}
2046
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002047static char str_doc[] =
2048"str(object) -> string\n\
2049\n\
2050Return a nice string representation of the object.\n\
2051If the argument is a string, the return value is the same object.";
2052
2053
Guido van Rossum79f25d91997-04-29 20:08:16 +00002054static PyObject *
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002055builtin_tuple(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00002056 PyObject *self;
2057 PyObject *args;
Guido van Rossumcae027b1994-08-29 12:53:11 +00002058{
Guido van Rossum79f25d91997-04-29 20:08:16 +00002059 PyObject *v;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002060
Guido van Rossum79f25d91997-04-29 20:08:16 +00002061 if (!PyArg_ParseTuple(args, "O:tuple", &v))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002062 return NULL;
Guido van Rossum09df08a1998-05-22 00:51:39 +00002063 return PySequence_Tuple(v);
Guido van Rossumcae027b1994-08-29 12:53:11 +00002064}
2065
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002066static char tuple_doc[] =
2067"tuple(sequence) -> list\n\
2068\n\
2069Return a tuple whose items are the same as those of the argument sequence.\n\
2070If the argument is a tuple, the return value is the same object.";
2071
2072
Guido van Rossum79f25d91997-04-29 20:08:16 +00002073static PyObject *
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002074builtin_type(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00002075 PyObject *self;
2076 PyObject *args;
Guido van Rossum3f5da241990-12-20 15:06:42 +00002077{
Guido van Rossum79f25d91997-04-29 20:08:16 +00002078 PyObject *v;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002079
Guido van Rossum79f25d91997-04-29 20:08:16 +00002080 if (!PyArg_ParseTuple(args, "O:type", &v))
Guido van Rossum3f5da241990-12-20 15:06:42 +00002081 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002082 v = (PyObject *)v->ob_type;
2083 Py_INCREF(v);
Guido van Rossum3f5da241990-12-20 15:06:42 +00002084 return v;
2085}
2086
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002087static char type_doc[] =
2088"type(object) -> type object\n\
2089\n\
2090Return the type of the object.";
2091
2092
Guido van Rossum79f25d91997-04-29 20:08:16 +00002093static PyObject *
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002094builtin_vars(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00002095 PyObject *self;
2096 PyObject *args;
Guido van Rossum2d951851994-08-29 12:52:16 +00002097{
Guido van Rossum79f25d91997-04-29 20:08:16 +00002098 PyObject *v = NULL;
2099 PyObject *d;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002100
Guido van Rossum79f25d91997-04-29 20:08:16 +00002101 if (!PyArg_ParseTuple(args, "|O:vars", &v))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002102 return NULL;
Guido van Rossum2d951851994-08-29 12:52:16 +00002103 if (v == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002104 d = PyEval_GetLocals();
Guido van Rossum53bb7ff1995-07-26 16:26:31 +00002105 if (d == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002106 if (!PyErr_Occurred())
2107 PyErr_SetString(PyExc_SystemError,
2108 "no locals!?");
Guido van Rossum53bb7ff1995-07-26 16:26:31 +00002109 }
2110 else
Guido van Rossum79f25d91997-04-29 20:08:16 +00002111 Py_INCREF(d);
Guido van Rossum2d951851994-08-29 12:52:16 +00002112 }
2113 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002114 d = PyObject_GetAttrString(v, "__dict__");
Guido van Rossum2d951851994-08-29 12:52:16 +00002115 if (d == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002116 PyErr_SetString(PyExc_TypeError,
Guido van Rossum2d951851994-08-29 12:52:16 +00002117 "vars() argument must have __dict__ attribute");
2118 return NULL;
2119 }
2120 }
2121 return d;
2122}
2123
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002124static char vars_doc[] =
2125"vars([object]) -> dictionary\n\
2126\n\
2127Without arguments, equivalent to locals().\n\
2128With an argument, equivalent to object.__dict__.";
2129
Guido van Rossum668213d1999-06-16 17:28:37 +00002130static int
2131abstract_issubclass(derived, cls, err, first)
2132 PyObject *derived;
2133 PyObject *cls;
2134 char *err;
2135 int first;
2136{
2137 static PyObject *__bases__ = NULL;
2138 PyObject *bases;
Guido van Rossum7f851861999-06-17 19:12:39 +00002139 int i, n;
Guido van Rossum668213d1999-06-16 17:28:37 +00002140 int r = 0;
2141
2142 if (__bases__ == NULL) {
2143 __bases__ = PyString_FromString("__bases__");
2144 if (__bases__ == NULL)
2145 return -1;
2146 }
2147
2148 if (first) {
2149 bases = PyObject_GetAttr(cls, __bases__);
2150 if (bases == NULL || !PyTuple_Check(bases)) {
2151 Py_XDECREF(bases);
2152 PyErr_SetString(PyExc_TypeError, err);
2153 return -1;
2154 }
2155 Py_DECREF(bases);
2156 }
2157
2158 if (derived == cls)
2159 return 1;
2160
2161 bases = PyObject_GetAttr(derived, __bases__);
2162 if (bases == NULL || !PyTuple_Check(bases)) {
2163 Py_XDECREF(bases);
2164 PyErr_SetString(PyExc_TypeError, err);
2165 return -1;
2166 }
2167
2168 n = PyTuple_GET_SIZE(bases);
2169 for (i = 0; i < n; i++) {
2170 r = abstract_issubclass(PyTuple_GET_ITEM(bases, i),
2171 cls, err, 0);
2172 if (r != 0)
2173 break;
2174 }
2175
2176 Py_DECREF(bases);
2177
2178 return r;
2179}
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002180
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002181static PyObject *
2182builtin_isinstance(self, args)
2183 PyObject *self;
2184 PyObject *args;
2185{
2186 PyObject *inst;
2187 PyObject *cls;
Guido van Rossum668213d1999-06-16 17:28:37 +00002188 PyObject *icls;
2189 static PyObject *__class__ = NULL;
2190 int retval = 0;
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002191
Guido van Rossum43713e52000-02-29 13:59:29 +00002192 if (!PyArg_ParseTuple(args, "OO:isinstance", &inst, &cls))
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002193 return NULL;
Guido van Rossumf5dd9141997-12-02 19:11:45 +00002194
Guido van Rossum668213d1999-06-16 17:28:37 +00002195 if (PyClass_Check(cls)) {
2196 if (PyInstance_Check(inst)) {
Guido van Rossumf5dd9141997-12-02 19:11:45 +00002197 PyObject *inclass =
2198 (PyObject*)((PyInstanceObject*)inst)->in_class;
2199 retval = PyClass_IsSubclass(inclass, cls);
2200 }
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002201 }
Guido van Rossum668213d1999-06-16 17:28:37 +00002202 else if (PyType_Check(cls)) {
2203 retval = ((PyObject *)(inst->ob_type) == cls);
2204 }
2205 else if (!PyInstance_Check(inst)) {
2206 if (__class__ == NULL) {
2207 __class__ = PyString_FromString("__class__");
2208 if (__class__ == NULL)
2209 return NULL;
2210 }
2211 icls = PyObject_GetAttr(inst, __class__);
2212 if (icls != NULL) {
2213 retval = abstract_issubclass(
2214 icls, cls,
2215 "second argument must be a class",
2216 1);
2217 Py_DECREF(icls);
2218 if (retval < 0)
2219 return NULL;
2220 }
2221 else {
2222 PyErr_SetString(PyExc_TypeError,
2223 "second argument must be a class");
2224 return NULL;
2225 }
2226 }
2227 else {
2228 PyErr_SetString(PyExc_TypeError,
2229 "second argument must be a class");
2230 return NULL;
2231 }
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002232 return PyInt_FromLong(retval);
2233}
2234
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002235static char isinstance_doc[] =
2236"isinstance(object, class-or-type) -> Boolean\n\
2237\n\
2238Return whether an object is an instance of a class or of a subclass thereof.\n\
2239With a type as second argument, return whether that is the object's type.";
2240
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002241
2242static PyObject *
2243builtin_issubclass(self, args)
2244 PyObject *self;
2245 PyObject *args;
2246{
2247 PyObject *derived;
2248 PyObject *cls;
2249 int retval;
2250
Guido van Rossum43713e52000-02-29 13:59:29 +00002251 if (!PyArg_ParseTuple(args, "OO:issubclass", &derived, &cls))
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002252 return NULL;
Guido van Rossum668213d1999-06-16 17:28:37 +00002253
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002254 if (!PyClass_Check(derived) || !PyClass_Check(cls)) {
Guido van Rossum668213d1999-06-16 17:28:37 +00002255 retval = abstract_issubclass(
2256 derived, cls, "arguments must be classes", 1);
2257 if (retval < 0)
2258 return NULL;
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002259 }
Guido van Rossum668213d1999-06-16 17:28:37 +00002260 else {
2261 /* shortcut */
2262 if (!(retval = (derived == cls)))
2263 retval = PyClass_IsSubclass(derived, cls);
2264 }
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002265
2266 return PyInt_FromLong(retval);
2267}
2268
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002269static char issubclass_doc[] =
2270"issubclass(C, B) -> Boolean\n\
2271\n\
2272Return whether class C is a subclass (i.e., a derived class) of class B.";
2273
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002274
Guido van Rossum79f25d91997-04-29 20:08:16 +00002275static PyMethodDef builtin_methods[] = {
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002276 {"__import__", builtin___import__, 1, import_doc},
2277 {"abs", builtin_abs, 1, abs_doc},
2278 {"apply", builtin_apply, 1, apply_doc},
Guido van Rossum0daf0221999-03-19 19:07:19 +00002279 {"buffer", builtin_buffer, 1, buffer_doc},
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002280 {"callable", builtin_callable, 1, callable_doc},
2281 {"chr", builtin_chr, 1, chr_doc},
2282 {"cmp", builtin_cmp, 1, cmp_doc},
2283 {"coerce", builtin_coerce, 1, coerce_doc},
2284 {"compile", builtin_compile, 1, compile_doc},
Guido van Rossum8a5c5d21996-01-12 01:09:56 +00002285#ifndef WITHOUT_COMPLEX
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002286 {"complex", builtin_complex, 1, complex_doc},
Guido van Rossum8a5c5d21996-01-12 01:09:56 +00002287#endif
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002288 {"delattr", builtin_delattr, 1, delattr_doc},
2289 {"dir", builtin_dir, 1, dir_doc},
2290 {"divmod", builtin_divmod, 1, divmod_doc},
2291 {"eval", builtin_eval, 1, eval_doc},
2292 {"execfile", builtin_execfile, 1, execfile_doc},
2293 {"filter", builtin_filter, 1, filter_doc},
2294 {"float", builtin_float, 1, float_doc},
2295 {"getattr", builtin_getattr, 1, getattr_doc},
2296 {"globals", builtin_globals, 1, globals_doc},
2297 {"hasattr", builtin_hasattr, 1, hasattr_doc},
2298 {"hash", builtin_hash, 1, hash_doc},
2299 {"hex", builtin_hex, 1, hex_doc},
2300 {"id", builtin_id, 1, id_doc},
2301 {"input", builtin_input, 1, input_doc},
2302 {"intern", builtin_intern, 1, intern_doc},
2303 {"int", builtin_int, 1, int_doc},
2304 {"isinstance", builtin_isinstance, 1, isinstance_doc},
2305 {"issubclass", builtin_issubclass, 1, issubclass_doc},
2306 {"len", builtin_len, 1, len_doc},
2307 {"list", builtin_list, 1, list_doc},
2308 {"locals", builtin_locals, 1, locals_doc},
2309 {"long", builtin_long, 1, long_doc},
2310 {"map", builtin_map, 1, map_doc},
2311 {"max", builtin_max, 1, max_doc},
2312 {"min", builtin_min, 1, min_doc},
2313 {"oct", builtin_oct, 1, oct_doc},
2314 {"open", builtin_open, 1, open_doc},
2315 {"ord", builtin_ord, 1, ord_doc},
2316 {"pow", builtin_pow, 1, pow_doc},
2317 {"range", builtin_range, 1, range_doc},
2318 {"raw_input", builtin_raw_input, 1, raw_input_doc},
2319 {"reduce", builtin_reduce, 1, reduce_doc},
2320 {"reload", builtin_reload, 1, reload_doc},
2321 {"repr", builtin_repr, 1, repr_doc},
2322 {"round", builtin_round, 1, round_doc},
2323 {"setattr", builtin_setattr, 1, setattr_doc},
2324 {"slice", builtin_slice, 1, slice_doc},
2325 {"str", builtin_str, 1, str_doc},
2326 {"tuple", builtin_tuple, 1, tuple_doc},
2327 {"type", builtin_type, 1, type_doc},
Guido van Rossum09095f32000-03-10 23:00:52 +00002328 {"unicode", builtin_unicode, 1, unicode_doc},
2329 {"unichr", builtin_unichr, 1, unichr_doc},
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002330 {"vars", builtin_vars, 1, vars_doc},
2331 {"xrange", builtin_xrange, 1, xrange_doc},
Guido van Rossumc02e15c1991-12-16 13:03:00 +00002332 {NULL, NULL},
Guido van Rossum3f5da241990-12-20 15:06:42 +00002333};
2334
Guido van Rossum3f5da241990-12-20 15:06:42 +00002335/* Predefined exceptions */
2336
Guido van Rossum04748321997-09-16 18:43:15 +00002337PyObject *PyExc_Exception;
Barry Warsaw757af0e1997-08-29 22:13:51 +00002338PyObject *PyExc_StandardError;
Barry Warsaw412cdc21997-09-16 21:51:14 +00002339PyObject *PyExc_ArithmeticError;
Barry Warsaw757af0e1997-08-29 22:13:51 +00002340PyObject *PyExc_LookupError;
2341
Guido van Rossum79f25d91997-04-29 20:08:16 +00002342PyObject *PyExc_AssertionError;
2343PyObject *PyExc_AttributeError;
2344PyObject *PyExc_EOFError;
Guido van Rossumb6a7f771997-05-09 03:03:23 +00002345PyObject *PyExc_FloatingPointError;
Barry Warsawd086a1a1998-07-23 15:59:57 +00002346PyObject *PyExc_EnvironmentError;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002347PyObject *PyExc_IOError;
Barry Warsawd086a1a1998-07-23 15:59:57 +00002348PyObject *PyExc_OSError;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002349PyObject *PyExc_ImportError;
2350PyObject *PyExc_IndexError;
2351PyObject *PyExc_KeyError;
2352PyObject *PyExc_KeyboardInterrupt;
2353PyObject *PyExc_MemoryError;
2354PyObject *PyExc_NameError;
2355PyObject *PyExc_OverflowError;
2356PyObject *PyExc_RuntimeError;
Barry Warsaw344864f1998-12-01 18:52:06 +00002357PyObject *PyExc_NotImplementedError;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002358PyObject *PyExc_SyntaxError;
2359PyObject *PyExc_SystemError;
2360PyObject *PyExc_SystemExit;
Guido van Rossum87460821999-06-22 14:47:32 +00002361PyObject *PyExc_UnboundLocalError;
Guido van Rossum09095f32000-03-10 23:00:52 +00002362PyObject *PyExc_UnicodeError;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002363PyObject *PyExc_TypeError;
2364PyObject *PyExc_ValueError;
2365PyObject *PyExc_ZeroDivisionError;
Guido van Rossum65a75b02000-02-17 15:18:10 +00002366#ifdef MS_WINDOWS
2367PyObject *PyExc_WindowsError;
2368#endif
Guido van Rossum50afb7a1991-12-10 13:52:31 +00002369
Barry Warsaw757af0e1997-08-29 22:13:51 +00002370PyObject *PyExc_MemoryErrorInst;
2371
Guido van Rossum11950231999-03-25 21:16:07 +00002372static struct
Barry Warsaw757af0e1997-08-29 22:13:51 +00002373{
2374 char* name;
2375 PyObject** exc;
2376 int leaf_exc;
2377}
2378bltin_exc[] = {
Guido van Rossum04748321997-09-16 18:43:15 +00002379 {"Exception", &PyExc_Exception, 0},
Barry Warsaw757af0e1997-08-29 22:13:51 +00002380 {"StandardError", &PyExc_StandardError, 0},
Barry Warsaw412cdc21997-09-16 21:51:14 +00002381 {"ArithmeticError", &PyExc_ArithmeticError, 0},
Barry Warsaw757af0e1997-08-29 22:13:51 +00002382 {"LookupError", &PyExc_LookupError, 0},
2383 {"AssertionError", &PyExc_AssertionError, 1},
2384 {"AttributeError", &PyExc_AttributeError, 1},
2385 {"EOFError", &PyExc_EOFError, 1},
2386 {"FloatingPointError", &PyExc_FloatingPointError, 1},
Barry Warsaw78902031999-01-29 20:29:49 +00002387 {"EnvironmentError", &PyExc_EnvironmentError, 0},
Barry Warsaw757af0e1997-08-29 22:13:51 +00002388 {"IOError", &PyExc_IOError, 1},
Barry Warsawd086a1a1998-07-23 15:59:57 +00002389 {"OSError", &PyExc_OSError, 1},
Barry Warsaw757af0e1997-08-29 22:13:51 +00002390 {"ImportError", &PyExc_ImportError, 1},
2391 {"IndexError", &PyExc_IndexError, 1},
2392 {"KeyError", &PyExc_KeyError, 1},
2393 {"KeyboardInterrupt", &PyExc_KeyboardInterrupt, 1},
2394 {"MemoryError", &PyExc_MemoryError, 1},
Guido van Rossum87460821999-06-22 14:47:32 +00002395 /* Note: NameError is not a leaf in exceptions.py, but unlike
2396 the other non-leafs NameError is meant to be raised directly
2397 at times -- the leaf_exc member really seems to mean something
2398 like "this is an abstract base class" when false.
2399 */
Barry Warsaw757af0e1997-08-29 22:13:51 +00002400 {"NameError", &PyExc_NameError, 1},
2401 {"OverflowError", &PyExc_OverflowError, 1},
2402 {"RuntimeError", &PyExc_RuntimeError, 1},
Barry Warsaw344864f1998-12-01 18:52:06 +00002403 {"NotImplementedError",&PyExc_NotImplementedError,1},
Barry Warsaw757af0e1997-08-29 22:13:51 +00002404 {"SyntaxError", &PyExc_SyntaxError, 1},
2405 {"SystemError", &PyExc_SystemError, 1},
2406 {"SystemExit", &PyExc_SystemExit, 1},
Guido van Rossum87460821999-06-22 14:47:32 +00002407 {"UnboundLocalError", &PyExc_UnboundLocalError, 1},
Guido van Rossum09095f32000-03-10 23:00:52 +00002408 {"UnicodeError", &PyExc_UnicodeError, 1},
Barry Warsaw757af0e1997-08-29 22:13:51 +00002409 {"TypeError", &PyExc_TypeError, 1},
2410 {"ValueError", &PyExc_ValueError, 1},
Guido van Rossum65a75b02000-02-17 15:18:10 +00002411#ifdef MS_WINDOWS
2412 {"WindowsError", &PyExc_WindowsError, 1},
2413#endif
Barry Warsaw757af0e1997-08-29 22:13:51 +00002414 {"ZeroDivisionError", &PyExc_ZeroDivisionError, 1},
2415 {NULL, NULL}
2416};
2417
2418
Barry Warsaw98b62461998-09-14 18:51:11 +00002419/* import exceptions module to extract class exceptions. on success,
2420 * return 1. on failure return 0 which signals _PyBuiltin_Init_2 to fall
2421 * back to using old-style string based exceptions.
2422 */
2423static int
Barry Warsaw757af0e1997-08-29 22:13:51 +00002424init_class_exc(dict)
2425 PyObject *dict;
2426{
2427 int i;
2428 PyObject *m = PyImport_ImportModule("exceptions");
Barry Warsaw98b62461998-09-14 18:51:11 +00002429 PyObject *args = NULL;
2430 PyObject *d = NULL;
Barry Warsaw757af0e1997-08-29 22:13:51 +00002431
Barry Warsaw98b62461998-09-14 18:51:11 +00002432 /* make sure we got the module and its dictionary */
Barry Warsaw757af0e1997-08-29 22:13:51 +00002433 if (m == NULL ||
2434 (d = PyModule_GetDict(m)) == NULL)
2435 {
Barry Warsaw98b62461998-09-14 18:51:11 +00002436 PySys_WriteStderr("'import exceptions' failed; ");
Barry Warsaw757af0e1997-08-29 22:13:51 +00002437 if (Py_VerboseFlag) {
Barry Warsaw98b62461998-09-14 18:51:11 +00002438 PySys_WriteStderr("traceback:\n");
Barry Warsaw757af0e1997-08-29 22:13:51 +00002439 PyErr_Print();
2440 }
2441 else {
Barry Warsaw98b62461998-09-14 18:51:11 +00002442 PySys_WriteStderr("use -v for traceback\n");
Barry Warsaw757af0e1997-08-29 22:13:51 +00002443 }
Barry Warsaw98b62461998-09-14 18:51:11 +00002444 goto finally;
Barry Warsaw757af0e1997-08-29 22:13:51 +00002445 }
2446 for (i = 0; bltin_exc[i].name; i++) {
2447 /* dig the exception out of the module */
2448 PyObject *exc = PyDict_GetItemString(d, bltin_exc[i].name);
Barry Warsaw98b62461998-09-14 18:51:11 +00002449 if (!exc) {
2450 PySys_WriteStderr(
2451 "Built-in exception class not found: %s. Library mismatch?\n",
2452 bltin_exc[i].name);
2453 goto finally;
2454 }
2455 /* free the old-style exception string object */
Barry Warsaw757af0e1997-08-29 22:13:51 +00002456 Py_XDECREF(*bltin_exc[i].exc);
2457
2458 /* squirrel away a pointer to the exception */
2459 Py_INCREF(exc);
2460 *bltin_exc[i].exc = exc;
2461
2462 /* and insert the name in the __builtin__ module */
Barry Warsaw98b62461998-09-14 18:51:11 +00002463 if (PyDict_SetItemString(dict, bltin_exc[i].name, exc)) {
2464 PySys_WriteStderr(
2465 "Cannot insert exception into __builtin__: %s\n",
2466 bltin_exc[i].name);
2467 goto finally;
2468 }
Barry Warsaw757af0e1997-08-29 22:13:51 +00002469 }
2470
2471 /* we need one pre-allocated instance */
2472 args = Py_BuildValue("()");
Barry Warsaw98b62461998-09-14 18:51:11 +00002473 if (!args ||
2474 !(PyExc_MemoryErrorInst =
2475 PyEval_CallObject(PyExc_MemoryError, args)))
2476 {
2477 PySys_WriteStderr("Cannot pre-allocate MemoryError instance\n");
2478 goto finally;
Barry Warsaw757af0e1997-08-29 22:13:51 +00002479 }
Barry Warsaw98b62461998-09-14 18:51:11 +00002480 Py_DECREF(args);
Barry Warsaw757af0e1997-08-29 22:13:51 +00002481
2482 /* we're done with the exceptions module */
2483 Py_DECREF(m);
2484
Barry Warsaw98b62461998-09-14 18:51:11 +00002485 if (PyErr_Occurred()) {
2486 PySys_WriteStderr("Cannot initialize standard class exceptions; ");
2487 if (Py_VerboseFlag) {
2488 PySys_WriteStderr("traceback:\n");
2489 PyErr_Print();
2490 }
2491 else
2492 PySys_WriteStderr("use -v for traceback\n");
2493 goto finally;
2494 }
2495 return 1;
2496 finally:
2497 Py_XDECREF(m);
2498 Py_XDECREF(args);
2499 PyErr_Clear();
2500 return 0;
Barry Warsaw757af0e1997-08-29 22:13:51 +00002501}
2502
2503
2504static void
2505fini_instances()
2506{
2507 Py_XDECREF(PyExc_MemoryErrorInst);
2508 PyExc_MemoryErrorInst = NULL;
2509}
2510
2511
Guido van Rossum79f25d91997-04-29 20:08:16 +00002512static PyObject *
Guido van Rossum25ce5661997-08-02 03:10:38 +00002513newstdexception(dict, name)
2514 PyObject *dict;
Guido van Rossumfb905c31991-12-16 15:42:38 +00002515 char *name;
Guido van Rossum3f5da241990-12-20 15:06:42 +00002516{
Guido van Rossum79f25d91997-04-29 20:08:16 +00002517 PyObject *v = PyString_FromString(name);
Guido van Rossum25ce5661997-08-02 03:10:38 +00002518 if (v == NULL || PyDict_SetItemString(dict, name, v) != 0)
Barry Warsaw98b62461998-09-14 18:51:11 +00002519 Py_FatalError("Cannot create string-based exceptions");
Guido van Rossum3f5da241990-12-20 15:06:42 +00002520 return v;
2521}
2522
2523static void
Guido van Rossum25ce5661997-08-02 03:10:38 +00002524initerrors(dict)
2525 PyObject *dict;
Guido van Rossum3f5da241990-12-20 15:06:42 +00002526{
Barry Warsaw72b715d1999-02-24 00:35:43 +00002527 int i, j;
Barry Warsaw757af0e1997-08-29 22:13:51 +00002528 int exccnt = 0;
2529 for (i = 0; bltin_exc[i].name; i++, exccnt++) {
Barry Warsaw98b62461998-09-14 18:51:11 +00002530 Py_XDECREF(*bltin_exc[i].exc);
Barry Warsaw757af0e1997-08-29 22:13:51 +00002531 if (bltin_exc[i].leaf_exc)
2532 *bltin_exc[i].exc =
2533 newstdexception(dict, bltin_exc[i].name);
2534 }
2535
Barry Warsawd086a1a1998-07-23 15:59:57 +00002536 /* This is kind of bogus because we special case the some of the
2537 * new exceptions to be nearly forward compatible. But this means
2538 * we hard code knowledge about exceptions.py into C here. I don't
2539 * have a better solution, though.
2540 */
Barry Warsaw757af0e1997-08-29 22:13:51 +00002541 PyExc_LookupError = PyTuple_New(2);
2542 Py_INCREF(PyExc_IndexError);
2543 PyTuple_SET_ITEM(PyExc_LookupError, 0, PyExc_IndexError);
2544 Py_INCREF(PyExc_KeyError);
2545 PyTuple_SET_ITEM(PyExc_LookupError, 1, PyExc_KeyError);
2546 PyDict_SetItemString(dict, "LookupError", PyExc_LookupError);
2547
Barry Warsaw412cdc21997-09-16 21:51:14 +00002548 PyExc_ArithmeticError = PyTuple_New(3);
Barry Warsaw757af0e1997-08-29 22:13:51 +00002549 Py_INCREF(PyExc_OverflowError);
Barry Warsaw412cdc21997-09-16 21:51:14 +00002550 PyTuple_SET_ITEM(PyExc_ArithmeticError, 0, PyExc_OverflowError);
Barry Warsaw757af0e1997-08-29 22:13:51 +00002551 Py_INCREF(PyExc_ZeroDivisionError);
Barry Warsaw412cdc21997-09-16 21:51:14 +00002552 PyTuple_SET_ITEM(PyExc_ArithmeticError, 1, PyExc_ZeroDivisionError);
Barry Warsaw757af0e1997-08-29 22:13:51 +00002553 Py_INCREF(PyExc_FloatingPointError);
Barry Warsaw412cdc21997-09-16 21:51:14 +00002554 PyTuple_SET_ITEM(PyExc_ArithmeticError, 2, PyExc_FloatingPointError);
2555 PyDict_SetItemString(dict, "ArithmeticError", PyExc_ArithmeticError);
Barry Warsaw757af0e1997-08-29 22:13:51 +00002556
Barry Warsawd086a1a1998-07-23 15:59:57 +00002557 PyExc_EnvironmentError = PyTuple_New(2);
2558 Py_INCREF(PyExc_IOError);
2559 PyTuple_SET_ITEM(PyExc_EnvironmentError, 0, PyExc_IOError);
2560 Py_INCREF(PyExc_OSError);
2561 PyTuple_SET_ITEM(PyExc_EnvironmentError, 1, PyExc_OSError);
2562 PyDict_SetItemString(dict, "EnvironmentError", PyExc_EnvironmentError);
2563
Guido van Rossum87460821999-06-22 14:47:32 +00002564 /* Make UnboundLocalError an alias for NameError */
2565 Py_INCREF(PyExc_NameError);
2566 Py_DECREF(PyExc_UnboundLocalError);
2567 PyExc_UnboundLocalError = PyExc_NameError;
2568 if (PyDict_SetItemString(dict, "UnboundLocalError",
2569 PyExc_NameError) != 0)
2570 Py_FatalError("Cannot create string-based exceptions");
2571
Guido van Rossum09095f32000-03-10 23:00:52 +00002572 /* Make UnicodeError an alias for ValueError */
2573 Py_INCREF(PyExc_ValueError);
2574 Py_DECREF(PyExc_UnicodeError);
2575 PyExc_UnicodeError = PyExc_ValueError;
2576 if (PyDict_SetItemString(dict, "UnicodeError",
2577 PyExc_ValueError) != 0)
2578 Py_FatalError("Cannot create string-based exceptions");
2579
Barry Warsaw72b715d1999-02-24 00:35:43 +00002580 /* missing from the StandardError tuple: Exception, StandardError,
2581 * and SystemExit
2582 */
2583 PyExc_StandardError = PyTuple_New(exccnt-3);
2584 for (i = 2, j = 0; bltin_exc[i].name; i++) {
Barry Warsaw757af0e1997-08-29 22:13:51 +00002585 PyObject *exc = *bltin_exc[i].exc;
Barry Warsaw72b715d1999-02-24 00:35:43 +00002586 /* SystemExit is not an error, but it is an exception */
2587 if (exc != PyExc_SystemExit) {
2588 Py_INCREF(exc);
2589 PyTuple_SET_ITEM(PyExc_StandardError, j++, exc);
2590 }
Barry Warsaw757af0e1997-08-29 22:13:51 +00002591 }
2592 PyDict_SetItemString(dict, "StandardError", PyExc_StandardError);
Guido van Rossum04748321997-09-16 18:43:15 +00002593
Barry Warsaw72b715d1999-02-24 00:35:43 +00002594 /* Exception is a 2-tuple */
2595 PyExc_Exception = PyTuple_New(2);
2596 Py_INCREF(PyExc_SystemExit);
2597 PyTuple_SET_ITEM(PyExc_Exception, 0, PyExc_SystemExit);
2598 Py_INCREF(PyExc_StandardError);
2599 PyTuple_SET_ITEM(PyExc_Exception, 1, PyExc_StandardError);
Guido van Rossum04748321997-09-16 18:43:15 +00002600 PyDict_SetItemString(dict, "Exception", PyExc_Exception);
Barry Warsaw757af0e1997-08-29 22:13:51 +00002601
2602 if (PyErr_Occurred())
2603 Py_FatalError("Could not initialize built-in string exceptions");
Guido van Rossum25ce5661997-08-02 03:10:38 +00002604}
2605
Barry Warsaw72b715d1999-02-24 00:35:43 +00002606
Guido van Rossum25ce5661997-08-02 03:10:38 +00002607static void
2608finierrors()
2609{
Barry Warsaw757af0e1997-08-29 22:13:51 +00002610 int i;
2611 for (i = 0; bltin_exc[i].name; i++) {
2612 PyObject *exc = *bltin_exc[i].exc;
2613 Py_XDECREF(exc);
2614 *bltin_exc[i].exc = NULL;
2615 }
Guido van Rossum25ce5661997-08-02 03:10:38 +00002616}
2617
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002618static char builtin_doc[] =
2619"Built-in functions, exceptions, and other objects.\n\
2620\n\
2621Noteworthy: None is the `nil' object; Ellipsis represents `...' in slices.";
2622
Guido van Rossum25ce5661997-08-02 03:10:38 +00002623PyObject *
Barry Warsaw757af0e1997-08-29 22:13:51 +00002624_PyBuiltin_Init_1()
Guido van Rossum25ce5661997-08-02 03:10:38 +00002625{
2626 PyObject *mod, *dict;
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002627 mod = Py_InitModule4("__builtin__", builtin_methods,
2628 builtin_doc, (PyObject *)NULL,
2629 PYTHON_API_VERSION);
Guido van Rossum25ce5661997-08-02 03:10:38 +00002630 if (mod == NULL)
2631 return NULL;
2632 dict = PyModule_GetDict(mod);
2633 initerrors(dict);
2634 if (PyDict_SetItemString(dict, "None", Py_None) < 0)
2635 return NULL;
2636 if (PyDict_SetItemString(dict, "Ellipsis", Py_Ellipsis) < 0)
2637 return NULL;
2638 if (PyDict_SetItemString(dict, "__debug__",
2639 PyInt_FromLong(Py_OptimizeFlag == 0)) < 0)
2640 return NULL;
Barry Warsaw757af0e1997-08-29 22:13:51 +00002641
Guido van Rossum25ce5661997-08-02 03:10:38 +00002642 return mod;
Guido van Rossum3f5da241990-12-20 15:06:42 +00002643}
2644
2645void
Barry Warsaw757af0e1997-08-29 22:13:51 +00002646_PyBuiltin_Init_2(dict)
2647 PyObject *dict;
2648{
2649 /* if Python was started with -X, initialize the class exceptions */
Barry Warsaw98b62461998-09-14 18:51:11 +00002650 if (Py_UseClassExceptionsFlag) {
2651 if (!init_class_exc(dict)) {
2652 /* class based exceptions could not be
2653 * initialized. Fall back to using string based
2654 * exceptions.
2655 */
2656 PySys_WriteStderr(
2657 "Warning! Falling back to string-based exceptions\n");
2658 initerrors(dict);
2659 }
2660 }
Barry Warsaw757af0e1997-08-29 22:13:51 +00002661}
2662
2663
2664void
2665_PyBuiltin_Fini_1()
2666{
2667 fini_instances();
2668}
2669
2670
2671void
2672_PyBuiltin_Fini_2()
Guido van Rossum3f5da241990-12-20 15:06:42 +00002673{
Guido van Rossum25ce5661997-08-02 03:10:38 +00002674 finierrors();
Guido van Rossum3f5da241990-12-20 15:06:42 +00002675}
Guido van Rossumc6bb8f71991-07-01 18:42:41 +00002676
Guido van Rossum12d12c51993-10-26 17:58:25 +00002677
Guido van Rossume77a7571993-11-03 15:01:26 +00002678/* Helper for filter(): filter a tuple through a function */
Guido van Rossum12d12c51993-10-26 17:58:25 +00002679
Guido van Rossum79f25d91997-04-29 20:08:16 +00002680static PyObject *
Guido van Rossum12d12c51993-10-26 17:58:25 +00002681filtertuple(func, tuple)
Guido van Rossum79f25d91997-04-29 20:08:16 +00002682 PyObject *func;
2683 PyObject *tuple;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002684{
Guido van Rossum79f25d91997-04-29 20:08:16 +00002685 PyObject *result;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002686 register int i, j;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002687 int len = PyTuple_Size(tuple);
Guido van Rossum12d12c51993-10-26 17:58:25 +00002688
Guido van Rossumb7b45621995-08-04 04:07:45 +00002689 if (len == 0) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002690 Py_INCREF(tuple);
Guido van Rossumb7b45621995-08-04 04:07:45 +00002691 return tuple;
2692 }
2693
Guido van Rossum79f25d91997-04-29 20:08:16 +00002694 if ((result = PyTuple_New(len)) == NULL)
Guido van Rossum2586bf01993-11-01 16:21:44 +00002695 return NULL;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002696
Guido van Rossum12d12c51993-10-26 17:58:25 +00002697 for (i = j = 0; i < len; ++i) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002698 PyObject *item, *good;
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002699 int ok;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002700
Guido van Rossum79f25d91997-04-29 20:08:16 +00002701 if ((item = PyTuple_GetItem(tuple, i)) == NULL)
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002702 goto Fail_1;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002703 if (func == Py_None) {
2704 Py_INCREF(item);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002705 good = item;
2706 }
2707 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002708 PyObject *arg = Py_BuildValue("(O)", item);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002709 if (arg == NULL)
2710 goto Fail_1;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002711 good = PyEval_CallObject(func, arg);
2712 Py_DECREF(arg);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002713 if (good == NULL)
Guido van Rossum12d12c51993-10-26 17:58:25 +00002714 goto Fail_1;
2715 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002716 ok = PyObject_IsTrue(good);
2717 Py_DECREF(good);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002718 if (ok) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002719 Py_INCREF(item);
2720 if (PyTuple_SetItem(result, j++, item) < 0)
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002721 goto Fail_1;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002722 }
Guido van Rossum12d12c51993-10-26 17:58:25 +00002723 }
2724
Guido van Rossum79f25d91997-04-29 20:08:16 +00002725 if (_PyTuple_Resize(&result, j, 0) < 0)
Guido van Rossum12d12c51993-10-26 17:58:25 +00002726 return NULL;
2727
Guido van Rossum12d12c51993-10-26 17:58:25 +00002728 return result;
2729
Guido van Rossum12d12c51993-10-26 17:58:25 +00002730Fail_1:
Guido van Rossum79f25d91997-04-29 20:08:16 +00002731 Py_DECREF(result);
Guido van Rossum12d12c51993-10-26 17:58:25 +00002732 return NULL;
2733}
2734
2735
Guido van Rossume77a7571993-11-03 15:01:26 +00002736/* Helper for filter(): filter a string through a function */
Guido van Rossum12d12c51993-10-26 17:58:25 +00002737
Guido van Rossum79f25d91997-04-29 20:08:16 +00002738static PyObject *
Guido van Rossum12d12c51993-10-26 17:58:25 +00002739filterstring(func, strobj)
Guido van Rossum79f25d91997-04-29 20:08:16 +00002740 PyObject *func;
2741 PyObject *strobj;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002742{
Guido van Rossum79f25d91997-04-29 20:08:16 +00002743 PyObject *result;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002744 register int i, j;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002745 int len = PyString_Size(strobj);
Guido van Rossum12d12c51993-10-26 17:58:25 +00002746
Guido van Rossum79f25d91997-04-29 20:08:16 +00002747 if (func == Py_None) {
Guido van Rossum2586bf01993-11-01 16:21:44 +00002748 /* No character is ever false -- share input string */
Guido van Rossum79f25d91997-04-29 20:08:16 +00002749 Py_INCREF(strobj);
Guido van Rossum2d951851994-08-29 12:52:16 +00002750 return strobj;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002751 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002752 if ((result = PyString_FromStringAndSize(NULL, len)) == NULL)
Guido van Rossum2586bf01993-11-01 16:21:44 +00002753 return NULL;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002754
Guido van Rossum12d12c51993-10-26 17:58:25 +00002755 for (i = j = 0; i < len; ++i) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002756 PyObject *item, *arg, *good;
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002757 int ok;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002758
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002759 item = (*strobj->ob_type->tp_as_sequence->sq_item)(strobj, i);
2760 if (item == NULL)
2761 goto Fail_1;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002762 arg = Py_BuildValue("(O)", item);
2763 Py_DECREF(item);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002764 if (arg == NULL)
2765 goto Fail_1;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002766 good = PyEval_CallObject(func, arg);
2767 Py_DECREF(arg);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002768 if (good == NULL)
2769 goto Fail_1;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002770 ok = PyObject_IsTrue(good);
2771 Py_DECREF(good);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002772 if (ok)
Guido van Rossum79f25d91997-04-29 20:08:16 +00002773 PyString_AS_STRING((PyStringObject *)result)[j++] =
2774 PyString_AS_STRING((PyStringObject *)item)[0];
Guido van Rossum12d12c51993-10-26 17:58:25 +00002775 }
2776
Guido van Rossum79f25d91997-04-29 20:08:16 +00002777 if (j < len && _PyString_Resize(&result, j) < 0)
Guido van Rossum12d12c51993-10-26 17:58:25 +00002778 return NULL;
2779
Guido van Rossum12d12c51993-10-26 17:58:25 +00002780 return result;
2781
Guido van Rossum12d12c51993-10-26 17:58:25 +00002782Fail_1:
Guido van Rossum79f25d91997-04-29 20:08:16 +00002783 Py_DECREF(result);
Guido van Rossum12d12c51993-10-26 17:58:25 +00002784 return NULL;
2785}