blob: 1df95d7a9527addb71df2888b8cc01bb74d543dc [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{
Guido van Rossum3afba762000-04-11 15:38:23 +0000168 PyObject *v;
169 const void *buffer;
Guido van Rossum09095f32000-03-10 23:00:52 +0000170 int len;
171 char *encoding = NULL;
172 char *errors = NULL;
173
Guido van Rossum3afba762000-04-11 15:38:23 +0000174 if ( !PyArg_ParseTuple(args, "O|ss:unicode", &v, &encoding, &errors) )
Guido van Rossum09095f32000-03-10 23:00:52 +0000175 return NULL;
Guido van Rossum3afba762000-04-11 15:38:23 +0000176 /* Special case: Unicode will stay Unicode */
177 if (PyUnicode_Check(v)) {
178 if (encoding) {
179 PyErr_SetString(PyExc_TypeError,
180 "unicode() does not support decoding of Unicode objects");
181 return NULL;
182 }
183 Py_INCREF(v);
184 return v;
185 }
186 /* Read raw data and decode it */
187 if (PyObject_AsReadBuffer(v, &buffer, &len))
188 return NULL;
189 return PyUnicode_Decode((const char *)buffer, len, encoding, errors);
Guido van Rossum09095f32000-03-10 23:00:52 +0000190}
191
192static char unicode_doc[] =
193"unicode(string [, encoding[, errors]]) -> object\n\
194\n\
Fred Drake078b24f2000-04-13 02:42:50 +0000195Creates a new Unicode object from the given encoded string.\n\
Guido van Rossum09095f32000-03-10 23:00:52 +0000196encoding defaults to 'utf-8' and errors, defining the error handling,\n\
197to 'strict'.";
198
199
200static PyObject *
Guido van Rossum2d951851994-08-29 12:52:16 +0000201builtin_callable(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000202 PyObject *self;
203 PyObject *args;
Guido van Rossum2d951851994-08-29 12:52:16 +0000204{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000205 PyObject *v;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000206
Guido van Rossum79f25d91997-04-29 20:08:16 +0000207 if (!PyArg_ParseTuple(args, "O:callable", &v))
Guido van Rossum2d951851994-08-29 12:52:16 +0000208 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000209 return PyInt_FromLong((long)PyCallable_Check(v));
Guido van Rossum2d951851994-08-29 12:52:16 +0000210}
211
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000212static char callable_doc[] =
213"callable(object) -> Boolean\n\
214\n\
215Return whether the object is callable (i.e., some kind of function).\n\
216Note that classes are callable, as are instances with a __call__() method.";
217
218
Guido van Rossum79f25d91997-04-29 20:08:16 +0000219static PyObject *
Guido van Rossume77a7571993-11-03 15:01:26 +0000220builtin_filter(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000221 PyObject *self;
222 PyObject *args;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000223{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000224 PyObject *func, *seq, *result;
225 PySequenceMethods *sqf;
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000226 int len;
227 register int i, j;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000228
Guido van Rossum79f25d91997-04-29 20:08:16 +0000229 if (!PyArg_ParseTuple(args, "OO:filter", &func, &seq))
Guido van Rossum12d12c51993-10-26 17:58:25 +0000230 return NULL;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000231
Guido van Rossum79f25d91997-04-29 20:08:16 +0000232 if (PyString_Check(seq)) {
233 PyObject *r = filterstring(func, seq);
Guido van Rossum12d12c51993-10-26 17:58:25 +0000234 return r;
235 }
236
Guido van Rossum79f25d91997-04-29 20:08:16 +0000237 if (PyTuple_Check(seq)) {
238 PyObject *r = filtertuple(func, seq);
Guido van Rossum12d12c51993-10-26 17:58:25 +0000239 return r;
240 }
241
Guido van Rossum09df08a1998-05-22 00:51:39 +0000242 sqf = seq->ob_type->tp_as_sequence;
243 if (sqf == NULL || sqf->sq_length == NULL || sqf->sq_item == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000244 PyErr_SetString(PyExc_TypeError,
Guido van Rossume77a7571993-11-03 15:01:26 +0000245 "argument 2 to filter() must be a sequence type");
Guido van Rossum12d12c51993-10-26 17:58:25 +0000246 goto Fail_2;
247 }
248
249 if ((len = (*sqf->sq_length)(seq)) < 0)
250 goto Fail_2;
251
Guido van Rossum79f25d91997-04-29 20:08:16 +0000252 if (PyList_Check(seq) && seq->ob_refcnt == 1) {
253 Py_INCREF(seq);
Guido van Rossum12d12c51993-10-26 17:58:25 +0000254 result = seq;
255 }
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000256 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000257 if ((result = PyList_New(len)) == NULL)
Guido van Rossum12d12c51993-10-26 17:58:25 +0000258 goto Fail_2;
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000259 }
Guido van Rossum12d12c51993-10-26 17:58:25 +0000260
Guido van Rossum2d951851994-08-29 12:52:16 +0000261 for (i = j = 0; ; ++i) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000262 PyObject *item, *good;
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000263 int ok;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000264
Guido van Rossum2d951851994-08-29 12:52:16 +0000265 if ((item = (*sqf->sq_item)(seq, i)) == NULL) {
Barry Warsawcde8b1b1997-08-22 21:14:38 +0000266 if (PyErr_ExceptionMatches(PyExc_IndexError)) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000267 PyErr_Clear();
Guido van Rossum2d951851994-08-29 12:52:16 +0000268 break;
269 }
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000270 goto Fail_1;
Guido van Rossum2d951851994-08-29 12:52:16 +0000271 }
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000272
Guido van Rossum79f25d91997-04-29 20:08:16 +0000273 if (func == Py_None) {
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000274 good = item;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000275 Py_INCREF(good);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000276 }
277 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000278 PyObject *arg = Py_BuildValue("(O)", item);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000279 if (arg == NULL)
280 goto Fail_1;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000281 good = PyEval_CallObject(func, arg);
282 Py_DECREF(arg);
Guido van Rossum58b68731995-01-10 17:40:55 +0000283 if (good == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000284 Py_DECREF(item);
Guido van Rossum12d12c51993-10-26 17:58:25 +0000285 goto Fail_1;
Guido van Rossum58b68731995-01-10 17:40:55 +0000286 }
Guido van Rossum12d12c51993-10-26 17:58:25 +0000287 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000288 ok = PyObject_IsTrue(good);
289 Py_DECREF(good);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000290 if (ok) {
Guido van Rossum2d951851994-08-29 12:52:16 +0000291 if (j < len) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000292 if (PyList_SetItem(result, j++, item) < 0)
Guido van Rossum2d951851994-08-29 12:52:16 +0000293 goto Fail_1;
294 }
295 else {
Barry Warsawfa77e091999-01-28 18:49:12 +0000296 int status = PyList_Append(result, item);
Guido van Rossum2d951851994-08-29 12:52:16 +0000297 j++;
Barry Warsawfa77e091999-01-28 18:49:12 +0000298 Py_DECREF(item);
299 if (status < 0)
Guido van Rossum2d951851994-08-29 12:52:16 +0000300 goto Fail_1;
301 }
Guido van Rossum58b68731995-01-10 17:40:55 +0000302 } else {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000303 Py_DECREF(item);
Guido van Rossum12d12c51993-10-26 17:58:25 +0000304 }
Guido van Rossum12d12c51993-10-26 17:58:25 +0000305 }
306
Guido van Rossum12d12c51993-10-26 17:58:25 +0000307
Guido van Rossum79f25d91997-04-29 20:08:16 +0000308 if (j < len && PyList_SetSlice(result, j, len, NULL) < 0)
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000309 goto Fail_1;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000310
Guido van Rossum12d12c51993-10-26 17:58:25 +0000311 return result;
312
Guido van Rossum12d12c51993-10-26 17:58:25 +0000313Fail_1:
Guido van Rossum79f25d91997-04-29 20:08:16 +0000314 Py_DECREF(result);
Guido van Rossum12d12c51993-10-26 17:58:25 +0000315Fail_2:
Guido van Rossum12d12c51993-10-26 17:58:25 +0000316 return NULL;
317}
318
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000319static char filter_doc[] =
320"filter(function, sequence) -> list\n\
321\n\
322Return a list containing those items of sequence for which function(item)\n\
323is true. If function is None, return a list of items that are true.";
324
325
Guido van Rossum79f25d91997-04-29 20:08:16 +0000326static PyObject *
Guido van Rossum94390a41992-08-14 15:14:30 +0000327builtin_chr(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000328 PyObject *self;
329 PyObject *args;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000330{
331 long x;
332 char s[1];
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000333
Guido van Rossum79f25d91997-04-29 20:08:16 +0000334 if (!PyArg_ParseTuple(args, "l:chr", &x))
Guido van Rossum3f5da241990-12-20 15:06:42 +0000335 return NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000336 if (x < 0 || x >= 256) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000337 PyErr_SetString(PyExc_ValueError,
338 "chr() arg not in range(256)");
Guido van Rossum3f5da241990-12-20 15:06:42 +0000339 return NULL;
340 }
Guido van Rossum6bf62da1997-04-11 20:37:35 +0000341 s[0] = (char)x;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000342 return PyString_FromStringAndSize(s, 1);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000343}
344
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000345static char chr_doc[] =
346"chr(i) -> character\n\
347\n\
348Return a string of one character with ordinal i; 0 <= i < 256.";
349
350
Guido van Rossum79f25d91997-04-29 20:08:16 +0000351static PyObject *
Guido van Rossum09095f32000-03-10 23:00:52 +0000352builtin_unichr(self, args)
353 PyObject *self;
354 PyObject *args;
355{
356 long x;
357 Py_UNICODE s[1];
358
359 if (!PyArg_ParseTuple(args, "l:unichr", &x))
360 return NULL;
361 if (x < 0 || x >= 65536) {
362 PyErr_SetString(PyExc_ValueError,
363 "unichr() arg not in range(65536)");
364 return NULL;
365 }
366 s[0] = (Py_UNICODE)x;
367 return PyUnicode_FromUnicode(s, 1);
368}
369
370static char unichr_doc[] =
Fred Drake078b24f2000-04-13 02:42:50 +0000371"unichr(i) -> Unicode character\n\
Guido van Rossum09095f32000-03-10 23:00:52 +0000372\n\
Fred Drake078b24f2000-04-13 02:42:50 +0000373Return a Unicode string of one character with ordinal i; 0 <= i < 65536.";
Guido van Rossum09095f32000-03-10 23:00:52 +0000374
375
376static PyObject *
Guido van Rossuma9e7dc11992-10-18 18:53:57 +0000377builtin_cmp(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000378 PyObject *self;
379 PyObject *args;
Guido van Rossuma9e7dc11992-10-18 18:53:57 +0000380{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000381 PyObject *a, *b;
Guido van Rossum09df08a1998-05-22 00:51:39 +0000382 int c;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000383
Guido van Rossum79f25d91997-04-29 20:08:16 +0000384 if (!PyArg_ParseTuple(args, "OO:cmp", &a, &b))
Guido van Rossuma9e7dc11992-10-18 18:53:57 +0000385 return NULL;
Guido van Rossum09df08a1998-05-22 00:51:39 +0000386 if (PyObject_Cmp(a, b, &c) < 0)
Guido van Rossumc8b6df91997-05-23 00:06:51 +0000387 return NULL;
Guido van Rossum09df08a1998-05-22 00:51:39 +0000388 return PyInt_FromLong((long)c);
Guido van Rossuma9e7dc11992-10-18 18:53:57 +0000389}
390
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000391static char cmp_doc[] =
392"cmp(x, y) -> integer\n\
393\n\
394Return negative if x<y, zero if x==y, positive if x>y.";
395
396
Guido van Rossum79f25d91997-04-29 20:08:16 +0000397static PyObject *
Guido van Rossum5524a591995-01-10 15:26:20 +0000398builtin_coerce(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000399 PyObject *self;
400 PyObject *args;
Guido van Rossum6a00cd81995-01-07 12:39:01 +0000401{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000402 PyObject *v, *w;
403 PyObject *res;
Guido van Rossum5524a591995-01-10 15:26:20 +0000404
Guido van Rossum79f25d91997-04-29 20:08:16 +0000405 if (!PyArg_ParseTuple(args, "OO:coerce", &v, &w))
Guido van Rossum5524a591995-01-10 15:26:20 +0000406 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000407 if (PyNumber_Coerce(&v, &w) < 0)
Guido van Rossum04691fc1992-08-12 15:35:34 +0000408 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000409 res = Py_BuildValue("(OO)", v, w);
410 Py_DECREF(v);
411 Py_DECREF(w);
Guido van Rossum04691fc1992-08-12 15:35:34 +0000412 return res;
413}
414
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000415static char coerce_doc[] =
416"coerce(x, y) -> None or (x1, y1)\n\
417\n\
418When x and y can be coerced to values of the same type, return a tuple\n\
419containing the coerced values. When they can't be coerced, return None.";
420
421
Guido van Rossum79f25d91997-04-29 20:08:16 +0000422static PyObject *
Guido van Rossum5b722181993-03-30 17:46:03 +0000423builtin_compile(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000424 PyObject *self;
425 PyObject *args;
Guido van Rossum5b722181993-03-30 17:46:03 +0000426{
427 char *str;
428 char *filename;
429 char *startstr;
430 int start;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000431
Guido van Rossum79f25d91997-04-29 20:08:16 +0000432 if (!PyArg_ParseTuple(args, "sss:compile", &str, &filename, &startstr))
Guido van Rossum5b722181993-03-30 17:46:03 +0000433 return NULL;
434 if (strcmp(startstr, "exec") == 0)
Guido van Rossumb05a5c71997-05-07 17:46:13 +0000435 start = Py_file_input;
Guido van Rossum5b722181993-03-30 17:46:03 +0000436 else if (strcmp(startstr, "eval") == 0)
Guido van Rossumb05a5c71997-05-07 17:46:13 +0000437 start = Py_eval_input;
Guido van Rossum872537c1995-07-07 22:43:42 +0000438 else if (strcmp(startstr, "single") == 0)
Guido van Rossumb05a5c71997-05-07 17:46:13 +0000439 start = Py_single_input;
Guido van Rossum5b722181993-03-30 17:46:03 +0000440 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000441 PyErr_SetString(PyExc_ValueError,
Guido van Rossum872537c1995-07-07 22:43:42 +0000442 "compile() mode must be 'exec' or 'eval' or 'single'");
Guido van Rossum5b722181993-03-30 17:46:03 +0000443 return NULL;
444 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000445 return Py_CompileString(str, filename, start);
Guido van Rossum5b722181993-03-30 17:46:03 +0000446}
447
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000448static char compile_doc[] =
449"compile(source, filename, mode) -> code object\n\
450\n\
451Compile the source string (a Python module, statement or expression)\n\
452into a code object that can be executed by the exec statement or eval().\n\
453The filename will be used for run-time error messages.\n\
454The mode must be 'exec' to compile a module, 'single' to compile a\n\
455single (interactive) statement, or 'eval' to compile an expression.";
456
457
Guido van Rossum8a5c5d21996-01-12 01:09:56 +0000458#ifndef WITHOUT_COMPLEX
459
Guido van Rossum79f25d91997-04-29 20:08:16 +0000460static PyObject *
Guido van Rossum11950231999-03-25 21:16:07 +0000461complex_from_string(v)
462 PyObject *v;
463{
464 extern double strtod Py_PROTO((const char *, char **));
Guido van Rossum9e896b32000-04-05 20:11:21 +0000465 const char *s, *start;
466 char *end;
Guido van Rossum11950231999-03-25 21:16:07 +0000467 double x=0.0, y=0.0, z;
468 int got_re=0, got_im=0, done=0;
469 int digit_or_dot;
470 int sw_error=0;
471 int sign;
472 char buffer[256]; /* For errors */
Guido van Rossum9e896b32000-04-05 20:11:21 +0000473 int len;
Guido van Rossum11950231999-03-25 21:16:07 +0000474
Guido van Rossum9e896b32000-04-05 20:11:21 +0000475 if (PyString_Check(v)) {
476 s = PyString_AS_STRING(v);
477 len = PyString_GET_SIZE(v);
478 }
479 else if (PyUnicode_Check(v)) {
480 char s_buffer[256];
481
482 if (PyUnicode_GET_SIZE(v) >= sizeof(s_buffer)) {
483 PyErr_SetString(PyExc_ValueError,
484 "complex() literal too large to convert");
485 return NULL;
486 }
487 if (PyUnicode_EncodeDecimal(PyUnicode_AS_UNICODE(v),
488 PyUnicode_GET_SIZE(v),
489 s_buffer,
490 NULL))
491 return NULL;
492 s = s_buffer;
493 len = strlen(s);
494 }
495 else if (PyObject_AsCharBuffer(v, &s, &len)) {
496 PyErr_SetString(PyExc_TypeError,
497 "complex() needs a string first argument");
498 return NULL;
499 }
Guido van Rossum11950231999-03-25 21:16:07 +0000500
501 /* position on first nonblank */
Guido van Rossum9e896b32000-04-05 20:11:21 +0000502 start = s;
Guido van Rossum11950231999-03-25 21:16:07 +0000503 while (*s && isspace(Py_CHARMASK(*s)))
504 s++;
505 if (s[0] == '\0') {
506 PyErr_SetString(PyExc_ValueError,
507 "empty string for complex()");
508 return NULL;
509 }
510
511 z = -1.0;
512 sign = 1;
513 do {
514
515 switch (*s) {
516
517 case '\0':
Guido van Rossum9e896b32000-04-05 20:11:21 +0000518 if (s-start != len) {
Guido van Rossum11950231999-03-25 21:16:07 +0000519 PyErr_SetString(
520 PyExc_ValueError,
521 "null byte in argument for complex()");
522 return NULL;
523 }
524 if(!done) sw_error=1;
525 break;
526
527 case '-':
528 sign = -1;
529 /* Fallthrough */
530 case '+':
531 if (done) sw_error=1;
532 s++;
533 if ( *s=='\0'||*s=='+'||*s=='-' ||
534 isspace(Py_CHARMASK(*s)) ) sw_error=1;
535 break;
536
537 case 'J':
538 case 'j':
539 if (got_im || done) {
540 sw_error = 1;
541 break;
542 }
543 if (z<0.0) {
544 y=sign;
545 }
546 else{
547 y=sign*z;
548 }
549 got_im=1;
550 s++;
551 if (*s!='+' && *s!='-' )
552 done=1;
553 break;
554
555 default:
556 if (isspace(Py_CHARMASK(*s))) {
557 while (*s && isspace(Py_CHARMASK(*s)))
558 s++;
559 if (s[0] != '\0')
560 sw_error=1;
561 else
562 done = 1;
563 break;
564 }
565 digit_or_dot =
566 (*s=='.' || isdigit(Py_CHARMASK(*s)));
567 if (done||!digit_or_dot) {
568 sw_error=1;
569 break;
570 }
571 errno = 0;
572 PyFPE_START_PROTECT("strtod", return 0)
573 z = strtod(s, &end) ;
574 PyFPE_END_PROTECT(z)
575 if (errno != 0) {
576 sprintf(buffer,
577 "float() out of range: %.150s", s);
578 PyErr_SetString(
579 PyExc_ValueError,
580 buffer);
581 return NULL;
582 }
583 s=end;
584 if (*s=='J' || *s=='j') {
585
586 break;
587 }
588 if (got_re) {
589 sw_error=1;
590 break;
591 }
592
593 /* accept a real part */
594 x=sign*z;
595 got_re=1;
596 if (got_im) done=1;
597 z = -1.0;
598 sign = 1;
599 break;
600
601 } /* end of switch */
602
603 } while (*s!='\0' && !sw_error);
604
605 if (sw_error) {
606 PyErr_SetString(PyExc_ValueError,
607 "malformed string for complex()");
608 return NULL;
609 }
610
611 return PyComplex_FromDoubles(x,y);
612}
613
614static PyObject *
Guido van Rossum8a5c5d21996-01-12 01:09:56 +0000615builtin_complex(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000616 PyObject *self;
617 PyObject *args;
Guido van Rossum8a5c5d21996-01-12 01:09:56 +0000618{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000619 PyObject *r, *i, *tmp;
620 PyNumberMethods *nbr, *nbi = NULL;
Guido van Rossum530956d1996-07-21 02:27:43 +0000621 Py_complex cr, ci;
Guido van Rossumc6472e91997-03-31 17:15:43 +0000622 int own_r = 0;
Guido van Rossum8a5c5d21996-01-12 01:09:56 +0000623
624 i = NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000625 if (!PyArg_ParseTuple(args, "O|O:complex", &r, &i))
Guido van Rossum8a5c5d21996-01-12 01:09:56 +0000626 return NULL;
Guido van Rossum9e896b32000-04-05 20:11:21 +0000627 if (PyString_Check(r) || PyUnicode_Check(r))
Guido van Rossum11950231999-03-25 21:16:07 +0000628 return complex_from_string(r);
Guido van Rossum8a5c5d21996-01-12 01:09:56 +0000629 if ((nbr = r->ob_type->tp_as_number) == NULL ||
Guido van Rossumc6472e91997-03-31 17:15:43 +0000630 nbr->nb_float == NULL ||
631 (i != NULL &&
632 ((nbi = i->ob_type->tp_as_number) == NULL ||
633 nbi->nb_float == NULL))) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000634 PyErr_SetString(PyExc_TypeError,
Guido van Rossum8a5c5d21996-01-12 01:09:56 +0000635 "complex() argument can't be converted to complex");
636 return NULL;
637 }
Guido van Rossumed0af8f1996-12-05 23:18:18 +0000638 /* XXX Hack to support classes with __complex__ method */
Guido van Rossum79f25d91997-04-29 20:08:16 +0000639 if (PyInstance_Check(r)) {
640 static PyObject *complexstr;
641 PyObject *f;
Guido van Rossumed0af8f1996-12-05 23:18:18 +0000642 if (complexstr == NULL) {
Guido van Rossum8d751611997-01-18 08:04:16 +0000643 complexstr = PyString_InternFromString("__complex__");
Guido van Rossumed0af8f1996-12-05 23:18:18 +0000644 if (complexstr == NULL)
645 return NULL;
646 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000647 f = PyObject_GetAttr(r, complexstr);
Guido van Rossumed0af8f1996-12-05 23:18:18 +0000648 if (f == NULL)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000649 PyErr_Clear();
Guido van Rossumed0af8f1996-12-05 23:18:18 +0000650 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000651 PyObject *args = Py_BuildValue("()");
Guido van Rossumed0af8f1996-12-05 23:18:18 +0000652 if (args == NULL)
653 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000654 r = PyEval_CallObject(f, args);
655 Py_DECREF(args);
Barry Warsawf988e681999-01-27 23:13:59 +0000656 Py_DECREF(f);
Guido van Rossumed0af8f1996-12-05 23:18:18 +0000657 if (r == NULL)
658 return NULL;
Guido van Rossumc6472e91997-03-31 17:15:43 +0000659 own_r = 1;
Guido van Rossumed0af8f1996-12-05 23:18:18 +0000660 }
661 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000662 if (PyComplex_Check(r)) {
663 cr = ((PyComplexObject*)r)->cval;
Guido van Rossum730806d1998-04-10 22:27:42 +0000664 if (own_r) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000665 Py_DECREF(r);
Guido van Rossum730806d1998-04-10 22:27:42 +0000666 }
Guido van Rossumc6472e91997-03-31 17:15:43 +0000667 }
Guido van Rossum8a5c5d21996-01-12 01:09:56 +0000668 else {
Guido van Rossumfe4b6ee1996-08-08 18:49:41 +0000669 tmp = (*nbr->nb_float)(r);
Guido van Rossum730806d1998-04-10 22:27:42 +0000670 if (own_r) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000671 Py_DECREF(r);
Guido van Rossum730806d1998-04-10 22:27:42 +0000672 }
Guido van Rossumfe4b6ee1996-08-08 18:49:41 +0000673 if (tmp == NULL)
674 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000675 cr.real = PyFloat_AsDouble(tmp);
676 Py_DECREF(tmp);
Guido van Rossum11950231999-03-25 21:16:07 +0000677 cr.imag = 0.0;
Guido van Rossum8a5c5d21996-01-12 01:09:56 +0000678 }
679 if (i == NULL) {
Guido van Rossum11950231999-03-25 21:16:07 +0000680 ci.real = 0.0;
681 ci.imag = 0.0;
Guido van Rossum8a5c5d21996-01-12 01:09:56 +0000682 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000683 else if (PyComplex_Check(i))
684 ci = ((PyComplexObject*)i)->cval;
Guido van Rossum8a5c5d21996-01-12 01:09:56 +0000685 else {
Guido van Rossumc6472e91997-03-31 17:15:43 +0000686 tmp = (*nbi->nb_float)(i);
Guido van Rossumfe4b6ee1996-08-08 18:49:41 +0000687 if (tmp == NULL)
688 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000689 ci.real = PyFloat_AsDouble(tmp);
690 Py_DECREF(tmp);
Guido van Rossum8a5c5d21996-01-12 01:09:56 +0000691 ci.imag = 0.;
692 }
693 cr.real -= ci.imag;
694 cr.imag += ci.real;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000695 return PyComplex_FromCComplex(cr);
Guido van Rossum8a5c5d21996-01-12 01:09:56 +0000696}
697
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000698static char complex_doc[] =
699"complex(real[, imag]) -> complex number\n\
700\n\
701Create a complex number from a real part and an optional imaginary part.\n\
702This is equivalent to (real + imag*1j) where imag defaults to 0.";
703
704
Guido van Rossum8a5c5d21996-01-12 01:09:56 +0000705#endif
706
Guido van Rossum79f25d91997-04-29 20:08:16 +0000707static PyObject *
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000708builtin_dir(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000709 PyObject *self;
710 PyObject *args;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000711{
Guido van Rossum666b17a1997-05-06 16:36:57 +0000712 static char *attrlist[] = {"__members__", "__methods__", NULL};
713 PyObject *v = NULL, *l = NULL, *m = NULL;
714 PyObject *d, *x;
715 int i;
716 char **s;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000717
Guido van Rossum79f25d91997-04-29 20:08:16 +0000718 if (!PyArg_ParseTuple(args, "|O:dir", &v))
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000719 return NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000720 if (v == NULL) {
Guido van Rossum666b17a1997-05-06 16:36:57 +0000721 x = PyEval_GetLocals();
722 if (x == NULL)
723 goto error;
724 l = PyMapping_Keys(x);
725 if (l == NULL)
726 goto error;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000727 }
728 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000729 d = PyObject_GetAttrString(v, "__dict__");
Guido van Rossum666b17a1997-05-06 16:36:57 +0000730 if (d == NULL)
Guido van Rossum795ba581996-05-23 22:49:07 +0000731 PyErr_Clear();
Guido van Rossum666b17a1997-05-06 16:36:57 +0000732 else {
733 l = PyMapping_Keys(d);
734 if (l == NULL)
735 PyErr_Clear();
736 Py_DECREF(d);
737 }
738 if (l == NULL) {
739 l = PyList_New(0);
740 if (l == NULL)
741 goto error;
742 }
743 for (s = attrlist; *s != NULL; s++) {
744 m = PyObject_GetAttrString(v, *s);
745 if (m == NULL) {
746 PyErr_Clear();
747 continue;
748 }
749 for (i = 0; ; i++) {
750 x = PySequence_GetItem(m, i);
751 if (x == NULL) {
752 PyErr_Clear();
753 break;
754 }
755 if (PyList_Append(l, x) != 0) {
756 Py_DECREF(x);
757 Py_DECREF(m);
758 goto error;
759 }
760 Py_DECREF(x);
761 }
762 Py_DECREF(m);
Guido van Rossum795ba581996-05-23 22:49:07 +0000763 }
Guido van Rossum006bcd41991-10-24 14:54:44 +0000764 }
Guido van Rossum666b17a1997-05-06 16:36:57 +0000765 if (PyList_Sort(l) != 0)
766 goto error;
767 return l;
768 error:
769 Py_XDECREF(l);
770 return NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000771}
772
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000773static char dir_doc[] =
774"dir([object]) -> list of strings\n\
775\n\
776Return an alphabetized list of names comprising (some of) the attributes\n\
777of the given object. Without an argument, the names in the current scope\n\
778are listed. With an instance argument, only the instance attributes are\n\
779returned. With a class argument, attributes of the base class are not\n\
780returned. For other types or arguments, this may list members or methods.";
781
782
Guido van Rossum79f25d91997-04-29 20:08:16 +0000783static PyObject *
Guido van Rossum6a00cd81995-01-07 12:39:01 +0000784builtin_divmod(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000785 PyObject *self;
786 PyObject *args;
Guido van Rossum6a00cd81995-01-07 12:39:01 +0000787{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000788 PyObject *v, *w;
Guido van Rossum6a00cd81995-01-07 12:39:01 +0000789
Guido van Rossum79f25d91997-04-29 20:08:16 +0000790 if (!PyArg_ParseTuple(args, "OO:divmod", &v, &w))
Guido van Rossum6a00cd81995-01-07 12:39:01 +0000791 return NULL;
Guido van Rossum09df08a1998-05-22 00:51:39 +0000792 return PyNumber_Divmod(v, w);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000793}
794
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000795static char divmod_doc[] =
796"divmod(x, y) -> (div, mod)\n\
797\n\
798Return the tuple ((x-x%y)/y, x%y). Invariant: div*y + mod == x.";
799
800
Guido van Rossum79f25d91997-04-29 20:08:16 +0000801static PyObject *
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000802builtin_eval(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000803 PyObject *self;
804 PyObject *args;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000805{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000806 PyObject *cmd;
807 PyObject *globals = Py_None, *locals = Py_None;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000808 char *str;
Guido van Rossum590baa41993-11-30 13:40:46 +0000809
Guido van Rossum79f25d91997-04-29 20:08:16 +0000810 if (!PyArg_ParseTuple(args, "O|O!O!:eval",
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000811 &cmd,
Guido van Rossum79f25d91997-04-29 20:08:16 +0000812 &PyDict_Type, &globals,
813 &PyDict_Type, &locals))
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000814 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000815 if (globals == Py_None) {
816 globals = PyEval_GetGlobals();
817 if (locals == Py_None)
818 locals = PyEval_GetLocals();
Guido van Rossum6135a871995-01-09 17:53:26 +0000819 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000820 else if (locals == Py_None)
Guido van Rossum6135a871995-01-09 17:53:26 +0000821 locals = globals;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000822 if (PyDict_GetItemString(globals, "__builtins__") == NULL) {
823 if (PyDict_SetItemString(globals, "__builtins__",
824 PyEval_GetBuiltins()) != 0)
Guido van Rossum6135a871995-01-09 17:53:26 +0000825 return NULL;
826 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000827 if (PyCode_Check(cmd))
828 return PyEval_EvalCode((PyCodeObject *) cmd, globals, locals);
829 if (!PyString_Check(cmd)) {
830 PyErr_SetString(PyExc_TypeError,
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000831 "eval() argument 1 must be string or code object");
Guido van Rossum94390a41992-08-14 15:14:30 +0000832 return NULL;
833 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000834 str = PyString_AsString(cmd);
835 if ((int)strlen(str) != PyString_Size(cmd)) {
836 PyErr_SetString(PyExc_ValueError,
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000837 "embedded '\\0' in string arg");
838 return NULL;
Guido van Rossumf08ab0a1992-03-04 16:41:41 +0000839 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000840 while (*str == ' ' || *str == '\t')
841 str++;
Guido van Rossumb05a5c71997-05-07 17:46:13 +0000842 return PyRun_String(str, Py_eval_input, globals, locals);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000843}
844
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000845static char eval_doc[] =
846"eval(source[, globals[, locals]]) -> value\n\
847\n\
848Evaluate the source in the context of globals and locals.\n\
849The source may be a string representing a Python expression\n\
850or a code object as returned by compile().\n\
851The globals and locals are dictionaries, defaulting to the current\n\
852globals and locals. If only globals is given, locals defaults to it.";
853
854
Guido van Rossum79f25d91997-04-29 20:08:16 +0000855static PyObject *
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000856builtin_execfile(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000857 PyObject *self;
858 PyObject *args;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000859{
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000860 char *filename;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000861 PyObject *globals = Py_None, *locals = Py_None;
862 PyObject *res;
Guido van Rossum0f61f8a1992-02-25 18:55:05 +0000863 FILE* fp;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000864
Guido van Rossum79f25d91997-04-29 20:08:16 +0000865 if (!PyArg_ParseTuple(args, "s|O!O!:execfile",
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000866 &filename,
Guido van Rossum79f25d91997-04-29 20:08:16 +0000867 &PyDict_Type, &globals,
868 &PyDict_Type, &locals))
Guido van Rossum0f61f8a1992-02-25 18:55:05 +0000869 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000870 if (globals == Py_None) {
871 globals = PyEval_GetGlobals();
872 if (locals == Py_None)
873 locals = PyEval_GetLocals();
Guido van Rossum6135a871995-01-09 17:53:26 +0000874 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000875 else if (locals == Py_None)
Guido van Rossum6135a871995-01-09 17:53:26 +0000876 locals = globals;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000877 if (PyDict_GetItemString(globals, "__builtins__") == NULL) {
878 if (PyDict_SetItemString(globals, "__builtins__",
879 PyEval_GetBuiltins()) != 0)
Guido van Rossum6135a871995-01-09 17:53:26 +0000880 return NULL;
881 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000882 Py_BEGIN_ALLOW_THREADS
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000883 fp = fopen(filename, "r");
Guido van Rossum79f25d91997-04-29 20:08:16 +0000884 Py_END_ALLOW_THREADS
Guido van Rossum0f61f8a1992-02-25 18:55:05 +0000885 if (fp == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000886 PyErr_SetFromErrno(PyExc_IOError);
Guido van Rossum0f61f8a1992-02-25 18:55:05 +0000887 return NULL;
888 }
Guido van Rossumb05a5c71997-05-07 17:46:13 +0000889 res = PyRun_File(fp, filename, Py_file_input, globals, locals);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000890 Py_BEGIN_ALLOW_THREADS
Guido van Rossum0f61f8a1992-02-25 18:55:05 +0000891 fclose(fp);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000892 Py_END_ALLOW_THREADS
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000893 return res;
Guido van Rossum0f61f8a1992-02-25 18:55:05 +0000894}
895
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000896static char execfile_doc[] =
897"execfile(filename[, globals[, locals]])\n\
898\n\
899Read and execute a Python script from a file.\n\
900The globals and locals are dictionaries, defaulting to the current\n\
901globals and locals. If only globals is given, locals defaults to it.";
902
903
Guido van Rossum79f25d91997-04-29 20:08:16 +0000904static PyObject *
Guido van Rossum94390a41992-08-14 15:14:30 +0000905builtin_getattr(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000906 PyObject *self;
907 PyObject *args;
Guido van Rossum33894be1992-01-27 16:53:09 +0000908{
Guido van Rossum950ff291998-06-29 13:38:57 +0000909 PyObject *v, *result, *dflt = NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000910 PyObject *name;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000911
Guido van Rossum950ff291998-06-29 13:38:57 +0000912 if (!PyArg_ParseTuple(args, "OS|O:getattr", &v, &name, &dflt))
Guido van Rossum33894be1992-01-27 16:53:09 +0000913 return NULL;
Guido van Rossum950ff291998-06-29 13:38:57 +0000914 result = PyObject_GetAttr(v, name);
915 if (result == NULL && dflt != NULL) {
916 PyErr_Clear();
917 Py_INCREF(dflt);
918 result = dflt;
919 }
920 return result;
Guido van Rossum9bfef441993-03-29 10:43:31 +0000921}
922
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000923static char getattr_doc[] =
Guido van Rossum950ff291998-06-29 13:38:57 +0000924"getattr(object, name[, default]) -> value\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000925\n\
Guido van Rossum950ff291998-06-29 13:38:57 +0000926Get a named attribute from an object; getattr(x, 'y') is equivalent to x.y.\n\
927When a default argument is given, it is returned when the attribute doesn't\n\
928exist; without it, an exception is raised in that case.";
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000929
930
Guido van Rossum79f25d91997-04-29 20:08:16 +0000931static PyObject *
Guido van Rossum872537c1995-07-07 22:43:42 +0000932builtin_globals(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000933 PyObject *self;
934 PyObject *args;
Guido van Rossum872537c1995-07-07 22:43:42 +0000935{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000936 PyObject *d;
Guido van Rossum872537c1995-07-07 22:43:42 +0000937
Guido van Rossum43713e52000-02-29 13:59:29 +0000938 if (!PyArg_ParseTuple(args, ":globals"))
Guido van Rossum872537c1995-07-07 22:43:42 +0000939 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000940 d = PyEval_GetGlobals();
941 Py_INCREF(d);
Guido van Rossum872537c1995-07-07 22:43:42 +0000942 return d;
943}
944
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000945static char globals_doc[] =
946"globals() -> dictionary\n\
947\n\
948Return the dictionary containing the current scope's global variables.";
949
950
Guido van Rossum79f25d91997-04-29 20:08:16 +0000951static PyObject *
Guido van Rossum9bfef441993-03-29 10:43:31 +0000952builtin_hasattr(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000953 PyObject *self;
954 PyObject *args;
Guido van Rossum9bfef441993-03-29 10:43:31 +0000955{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000956 PyObject *v;
957 PyObject *name;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000958
Guido van Rossum79f25d91997-04-29 20:08:16 +0000959 if (!PyArg_ParseTuple(args, "OS:hasattr", &v, &name))
Guido van Rossum9bfef441993-03-29 10:43:31 +0000960 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000961 v = PyObject_GetAttr(v, name);
Guido van Rossum9bfef441993-03-29 10:43:31 +0000962 if (v == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000963 PyErr_Clear();
Guido van Rossum09df08a1998-05-22 00:51:39 +0000964 Py_INCREF(Py_False);
965 return Py_False;
Guido van Rossum9bfef441993-03-29 10:43:31 +0000966 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000967 Py_DECREF(v);
Guido van Rossum09df08a1998-05-22 00:51:39 +0000968 Py_INCREF(Py_True);
969 return Py_True;
Guido van Rossum33894be1992-01-27 16:53:09 +0000970}
971
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000972static char hasattr_doc[] =
973"hasattr(object, name) -> Boolean\n\
974\n\
975Return whether the object has an attribute with the given name.\n\
976(This is done by calling getattr(object, name) and catching exceptions.)";
977
978
Guido van Rossum79f25d91997-04-29 20:08:16 +0000979static PyObject *
Guido van Rossum5b722181993-03-30 17:46:03 +0000980builtin_id(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000981 PyObject *self;
982 PyObject *args;
Guido van Rossum5b722181993-03-30 17:46:03 +0000983{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000984 PyObject *v;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000985
Guido van Rossum79f25d91997-04-29 20:08:16 +0000986 if (!PyArg_ParseTuple(args, "O:id", &v))
Guido van Rossum5b722181993-03-30 17:46:03 +0000987 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000988 return PyInt_FromLong((long)v);
Guido van Rossum5b722181993-03-30 17:46:03 +0000989}
990
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000991static char id_doc[] =
992"id(object) -> integer\n\
993\n\
994Return the identity of an object. This is guaranteed to be unique among\n\
995simultaneously existing objects. (Hint: it's the object's memory address.)";
996
997
Guido van Rossum79f25d91997-04-29 20:08:16 +0000998static PyObject *
Guido van Rossum12d12c51993-10-26 17:58:25 +0000999builtin_map(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001000 PyObject *self;
1001 PyObject *args;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001002{
1003 typedef struct {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001004 PyObject *seq;
1005 PySequenceMethods *sqf;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001006 int len;
1007 } sequence;
1008
Guido van Rossum79f25d91997-04-29 20:08:16 +00001009 PyObject *func, *result;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001010 sequence *seqs = NULL, *sqp;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001011 int n, len;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001012 register int i, j;
1013
Guido van Rossum79f25d91997-04-29 20:08:16 +00001014 n = PyTuple_Size(args);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001015 if (n < 2) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001016 PyErr_SetString(PyExc_TypeError,
1017 "map() requires at least two args");
Guido van Rossum12d12c51993-10-26 17:58:25 +00001018 return NULL;
1019 }
1020
Guido van Rossum79f25d91997-04-29 20:08:16 +00001021 func = PyTuple_GetItem(args, 0);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001022 n--;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001023
Guido van Rossumfa4ac711998-07-10 17:37:30 +00001024 if (func == Py_None && n == 1) {
1025 /* map(None, S) is the same as list(S). */
1026 return PySequence_List(PyTuple_GetItem(args, 1));
1027 }
1028
Guido van Rossum79f25d91997-04-29 20:08:16 +00001029 if ((seqs = PyMem_NEW(sequence, n)) == NULL) {
1030 PyErr_NoMemory();
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00001031 goto Fail_2;
1032 }
Guido van Rossum12d12c51993-10-26 17:58:25 +00001033
Guido van Rossum2d951851994-08-29 12:52:16 +00001034 for (len = 0, i = 0, sqp = seqs; i < n; ++i, ++sqp) {
Guido van Rossum12d12c51993-10-26 17:58:25 +00001035 int curlen;
Guido van Rossum09df08a1998-05-22 00:51:39 +00001036 PySequenceMethods *sqf;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001037
Guido van Rossum79f25d91997-04-29 20:08:16 +00001038 if ((sqp->seq = PyTuple_GetItem(args, i + 1)) == NULL)
Guido van Rossum12d12c51993-10-26 17:58:25 +00001039 goto Fail_2;
1040
Guido van Rossum09df08a1998-05-22 00:51:39 +00001041 sqp->sqf = sqf = sqp->seq->ob_type->tp_as_sequence;
1042 if (sqf == NULL ||
1043 sqf->sq_length == NULL ||
1044 sqf->sq_item == NULL)
1045 {
Guido van Rossum12d12c51993-10-26 17:58:25 +00001046 static char errmsg[] =
1047 "argument %d to map() must be a sequence object";
Guido van Rossum15e33a41997-04-30 19:00:27 +00001048 char errbuf[sizeof(errmsg) + 25];
Guido van Rossum12d12c51993-10-26 17:58:25 +00001049
1050 sprintf(errbuf, errmsg, i+2);
Guido van Rossum79f25d91997-04-29 20:08:16 +00001051 PyErr_SetString(PyExc_TypeError, errbuf);
Guido van Rossum12d12c51993-10-26 17:58:25 +00001052 goto Fail_2;
1053 }
1054
1055 if ((curlen = sqp->len = (*sqp->sqf->sq_length)(sqp->seq)) < 0)
1056 goto Fail_2;
1057
1058 if (curlen > len)
1059 len = curlen;
1060 }
1061
Guido van Rossum79f25d91997-04-29 20:08:16 +00001062 if ((result = (PyObject *) PyList_New(len)) == NULL)
Guido van Rossum12d12c51993-10-26 17:58:25 +00001063 goto Fail_2;
1064
Guido van Rossum2d951851994-08-29 12:52:16 +00001065 for (i = 0; ; ++i) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001066 PyObject *alist, *item=NULL, *value;
Guido van Rossum2d951851994-08-29 12:52:16 +00001067 int any = 0;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001068
Guido van Rossum79f25d91997-04-29 20:08:16 +00001069 if (func == Py_None && n == 1)
Guido van Rossum32120311995-07-10 13:52:21 +00001070 alist = NULL;
Guido van Rossum2d951851994-08-29 12:52:16 +00001071 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001072 if ((alist = PyTuple_New(n)) == NULL)
Guido van Rossum2d951851994-08-29 12:52:16 +00001073 goto Fail_1;
1074 }
Guido van Rossum12d12c51993-10-26 17:58:25 +00001075
1076 for (j = 0, sqp = seqs; j < n; ++j, ++sqp) {
Guido van Rossum2d951851994-08-29 12:52:16 +00001077 if (sqp->len < 0) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001078 Py_INCREF(Py_None);
1079 item = Py_None;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001080 }
Guido van Rossum12d12c51993-10-26 17:58:25 +00001081 else {
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00001082 item = (*sqp->sqf->sq_item)(sqp->seq, i);
Guido van Rossum2d951851994-08-29 12:52:16 +00001083 if (item == NULL) {
Barry Warsawcde8b1b1997-08-22 21:14:38 +00001084 if (PyErr_ExceptionMatches(
1085 PyExc_IndexError))
1086 {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001087 PyErr_Clear();
1088 Py_INCREF(Py_None);
1089 item = Py_None;
Guido van Rossum2d951851994-08-29 12:52:16 +00001090 sqp->len = -1;
1091 }
1092 else {
1093 goto Fail_0;
1094 }
1095 }
1096 else
1097 any = 1;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001098
Guido van Rossum12d12c51993-10-26 17:58:25 +00001099 }
Guido van Rossum32120311995-07-10 13:52:21 +00001100 if (!alist)
Guido van Rossum2d951851994-08-29 12:52:16 +00001101 break;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001102 if (PyTuple_SetItem(alist, j, item) < 0) {
1103 Py_DECREF(item);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00001104 goto Fail_0;
Guido van Rossum2d951851994-08-29 12:52:16 +00001105 }
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00001106 continue;
1107
1108 Fail_0:
Guido van Rossum79f25d91997-04-29 20:08:16 +00001109 Py_XDECREF(alist);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00001110 goto Fail_1;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001111 }
1112
Guido van Rossum32120311995-07-10 13:52:21 +00001113 if (!alist)
1114 alist = item;
Guido van Rossum2d951851994-08-29 12:52:16 +00001115
1116 if (!any) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001117 Py_DECREF(alist);
Guido van Rossum2d951851994-08-29 12:52:16 +00001118 break;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001119 }
Guido van Rossum2d951851994-08-29 12:52:16 +00001120
Guido van Rossum79f25d91997-04-29 20:08:16 +00001121 if (func == Py_None)
Guido van Rossum32120311995-07-10 13:52:21 +00001122 value = alist;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001123 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001124 value = PyEval_CallObject(func, alist);
1125 Py_DECREF(alist);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00001126 if (value == NULL)
1127 goto Fail_1;
Guido van Rossum2d951851994-08-29 12:52:16 +00001128 }
1129 if (i >= len) {
Barry Warsawfa77e091999-01-28 18:49:12 +00001130 int status = PyList_Append(result, value);
Barry Warsaw21332871999-01-28 04:21:35 +00001131 Py_DECREF(value);
Barry Warsawfa77e091999-01-28 18:49:12 +00001132 if (status < 0)
1133 goto Fail_1;
Guido van Rossum2d951851994-08-29 12:52:16 +00001134 }
1135 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001136 if (PyList_SetItem(result, i, value) < 0)
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00001137 goto Fail_1;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001138 }
1139 }
1140
Guido van Rossumfa4ac711998-07-10 17:37:30 +00001141 if (i < len && PyList_SetSlice(result, i, len, NULL) < 0)
1142 goto Fail_1;
1143
Guido van Rossum79f25d91997-04-29 20:08:16 +00001144 PyMem_DEL(seqs);
Guido van Rossum12d12c51993-10-26 17:58:25 +00001145 return result;
1146
Guido van Rossum12d12c51993-10-26 17:58:25 +00001147Fail_1:
Guido van Rossum79f25d91997-04-29 20:08:16 +00001148 Py_DECREF(result);
Guido van Rossum12d12c51993-10-26 17:58:25 +00001149Fail_2:
Guido van Rossum79f25d91997-04-29 20:08:16 +00001150 if (seqs) PyMem_DEL(seqs);
Guido van Rossum12d12c51993-10-26 17:58:25 +00001151 return NULL;
1152}
1153
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001154static char map_doc[] =
1155"map(function, sequence[, sequence, ...]) -> list\n\
1156\n\
1157Return a list of the results of applying the function to the items of\n\
1158the argument sequence(s). If more than one sequence is given, the\n\
1159function is called with an argument list consisting of the corresponding\n\
1160item of each sequence, substituting None for missing values when not all\n\
1161sequences have the same length. If the function is None, return a list of\n\
1162the items of the sequence (or a list of tuples if more than one sequence).";
1163
1164
Guido van Rossum79f25d91997-04-29 20:08:16 +00001165static PyObject *
Guido van Rossum94390a41992-08-14 15:14:30 +00001166builtin_setattr(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001167 PyObject *self;
1168 PyObject *args;
Guido van Rossum33894be1992-01-27 16:53:09 +00001169{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001170 PyObject *v;
1171 PyObject *name;
1172 PyObject *value;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001173
Guido van Rossum79f25d91997-04-29 20:08:16 +00001174 if (!PyArg_ParseTuple(args, "OSO:setattr", &v, &name, &value))
Guido van Rossum33894be1992-01-27 16:53:09 +00001175 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001176 if (PyObject_SetAttr(v, name, value) != 0)
Guido van Rossum33894be1992-01-27 16:53:09 +00001177 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001178 Py_INCREF(Py_None);
1179 return Py_None;
Guido van Rossum33894be1992-01-27 16:53:09 +00001180}
1181
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001182static char setattr_doc[] =
1183"setattr(object, name, value)\n\
1184\n\
1185Set a named attribute on an object; setattr(x, 'y', v) is equivalent to\n\
1186``x.y = v''.";
1187
1188
Guido van Rossum79f25d91997-04-29 20:08:16 +00001189static PyObject *
Guido van Rossum14144fc1994-08-29 12:53:40 +00001190builtin_delattr(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001191 PyObject *self;
1192 PyObject *args;
Guido van Rossum14144fc1994-08-29 12:53:40 +00001193{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001194 PyObject *v;
1195 PyObject *name;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001196
Guido van Rossum79f25d91997-04-29 20:08:16 +00001197 if (!PyArg_ParseTuple(args, "OS:delattr", &v, &name))
Guido van Rossum14144fc1994-08-29 12:53:40 +00001198 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001199 if (PyObject_SetAttr(v, name, (PyObject *)NULL) != 0)
Guido van Rossum14144fc1994-08-29 12:53:40 +00001200 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001201 Py_INCREF(Py_None);
1202 return Py_None;
Guido van Rossum14144fc1994-08-29 12:53:40 +00001203}
1204
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001205static char delattr_doc[] =
Guido van Rossumdf12a591998-11-23 22:13:04 +00001206"delattr(object, name)\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001207\n\
1208Delete a named attribute on an object; delattr(x, 'y') is equivalent to\n\
1209``del x.y''.";
1210
1211
Guido van Rossum79f25d91997-04-29 20:08:16 +00001212static PyObject *
Guido van Rossum9bfef441993-03-29 10:43:31 +00001213builtin_hash(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001214 PyObject *self;
1215 PyObject *args;
Guido van Rossum9bfef441993-03-29 10:43:31 +00001216{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001217 PyObject *v;
Guido van Rossum9bfef441993-03-29 10:43:31 +00001218 long x;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001219
Guido van Rossum79f25d91997-04-29 20:08:16 +00001220 if (!PyArg_ParseTuple(args, "O:hash", &v))
Guido van Rossum9bfef441993-03-29 10:43:31 +00001221 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001222 x = PyObject_Hash(v);
Guido van Rossum9bfef441993-03-29 10:43:31 +00001223 if (x == -1)
1224 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001225 return PyInt_FromLong(x);
Guido van Rossum9bfef441993-03-29 10:43:31 +00001226}
1227
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001228static char hash_doc[] =
1229"hash(object) -> integer\n\
1230\n\
1231Return a hash value for the object. Two objects with the same value have\n\
1232the same hash value. The reverse is not necessarily true, but likely.";
1233
1234
Guido van Rossum79f25d91997-04-29 20:08:16 +00001235static PyObject *
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001236builtin_hex(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001237 PyObject *self;
1238 PyObject *args;
Guido van Rossum006bcd41991-10-24 14:54:44 +00001239{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001240 PyObject *v;
1241 PyNumberMethods *nb;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001242
Guido van Rossum79f25d91997-04-29 20:08:16 +00001243 if (!PyArg_ParseTuple(args, "O:hex", &v))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001244 return NULL;
Guido van Rossum1899c2e1992-09-12 11:09:23 +00001245
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001246 if ((nb = v->ob_type->tp_as_number) == NULL ||
Guido van Rossum1899c2e1992-09-12 11:09:23 +00001247 nb->nb_hex == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001248 PyErr_SetString(PyExc_TypeError,
Guido van Rossum1899c2e1992-09-12 11:09:23 +00001249 "hex() argument can't be converted to hex");
1250 return NULL;
Guido van Rossum006bcd41991-10-24 14:54:44 +00001251 }
Guido van Rossum1899c2e1992-09-12 11:09:23 +00001252 return (*nb->nb_hex)(v);
Guido van Rossum006bcd41991-10-24 14:54:44 +00001253}
1254
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001255static char hex_doc[] =
1256"hex(number) -> string\n\
1257\n\
1258Return the hexadecimal representation of an integer or long integer.";
1259
1260
Guido van Rossum79f25d91997-04-29 20:08:16 +00001261static PyObject *builtin_raw_input Py_PROTO((PyObject *, PyObject *));
Guido van Rossum3165fe61992-09-25 21:59:05 +00001262
Guido van Rossum79f25d91997-04-29 20:08:16 +00001263static PyObject *
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001264builtin_input(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001265 PyObject *self;
1266 PyObject *args;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001267{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001268 PyObject *line;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001269 char *str;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001270 PyObject *res;
1271 PyObject *globals, *locals;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001272
1273 line = builtin_raw_input(self, args);
Guido van Rossum3165fe61992-09-25 21:59:05 +00001274 if (line == NULL)
1275 return line;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001276 if (!PyArg_Parse(line, "s;embedded '\\0' in input line", &str))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001277 return NULL;
1278 while (*str == ' ' || *str == '\t')
1279 str++;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001280 globals = PyEval_GetGlobals();
1281 locals = PyEval_GetLocals();
1282 if (PyDict_GetItemString(globals, "__builtins__") == NULL) {
1283 if (PyDict_SetItemString(globals, "__builtins__",
1284 PyEval_GetBuiltins()) != 0)
Guido van Rossum6135a871995-01-09 17:53:26 +00001285 return NULL;
1286 }
Guido van Rossumb05a5c71997-05-07 17:46:13 +00001287 res = PyRun_String(str, Py_eval_input, globals, locals);
Guido van Rossum79f25d91997-04-29 20:08:16 +00001288 Py_DECREF(line);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001289 return res;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001290}
1291
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001292static char input_doc[] =
1293"input([prompt]) -> value\n\
1294\n\
1295Equivalent to eval(raw_input(prompt)).";
1296
1297
Guido van Rossume8811f81997-02-14 15:48:05 +00001298static PyObject *
1299builtin_intern(self, args)
1300 PyObject *self;
1301 PyObject *args;
1302{
1303 PyObject *s;
Guido van Rossum43713e52000-02-29 13:59:29 +00001304 if (!PyArg_ParseTuple(args, "S:intern", &s))
Guido van Rossume8811f81997-02-14 15:48:05 +00001305 return NULL;
1306 Py_INCREF(s);
1307 PyString_InternInPlace(&s);
1308 return s;
1309}
1310
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001311static char intern_doc[] =
1312"intern(string) -> string\n\
1313\n\
1314``Intern'' the given string. This enters the string in the (global)\n\
1315table of interned strings whose purpose is to speed up dictionary lookups.\n\
1316Return the string itself or the previously interned string object with the\n\
1317same value.";
1318
1319
Guido van Rossum79f25d91997-04-29 20:08:16 +00001320static PyObject *
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001321builtin_int(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001322 PyObject *self;
1323 PyObject *args;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001324{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001325 PyObject *v;
Barry Warsaw226ae6c1999-10-12 19:54:53 +00001326 int base = -909; /* unlikely! */
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001327
Barry Warsaw226ae6c1999-10-12 19:54:53 +00001328 if (!PyArg_ParseTuple(args, "O|i:int", &v, &base))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001329 return NULL;
Barry Warsaw226ae6c1999-10-12 19:54:53 +00001330 if (base == -909)
1331 return PyNumber_Int(v);
Guido van Rossum9e896b32000-04-05 20:11:21 +00001332 else if (PyString_Check(v))
1333 return PyInt_FromString(PyString_AS_STRING(v), NULL, base);
1334 else if (PyUnicode_Check(v))
1335 return PyInt_FromUnicode(PyUnicode_AS_UNICODE(v),
1336 PyUnicode_GET_SIZE(v),
1337 base);
1338 else {
Barry Warsaw226ae6c1999-10-12 19:54:53 +00001339 PyErr_SetString(PyExc_TypeError,
1340 "can't convert non-string with explicit base");
1341 return NULL;
1342 }
Guido van Rossum3f5da241990-12-20 15:06:42 +00001343}
1344
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001345static char int_doc[] =
Barry Warsaw226ae6c1999-10-12 19:54:53 +00001346"int(x[, base]) -> integer\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001347\n\
Barry Warsaw226ae6c1999-10-12 19:54:53 +00001348Convert a string or number to an integer, if possible. A floating point\n\
1349argument will be truncated towards zero (this does not include a string\n\
1350representation of a floating point number!) When converting a string, use\n\
1351the optional base. It is an error to supply a base when converting a\n\
1352non-string.";
1353
1354
1355static PyObject *
1356builtin_long(self, args)
1357 PyObject *self;
1358 PyObject *args;
1359{
1360 PyObject *v;
1361 int base = -909; /* unlikely! */
1362
1363 if (!PyArg_ParseTuple(args, "O|i:long", &v, &base))
1364 return NULL;
1365 if (base == -909)
1366 return PyNumber_Long(v);
Guido van Rossum9e896b32000-04-05 20:11:21 +00001367 else if (PyString_Check(v))
1368 return PyLong_FromString(PyString_AS_STRING(v), NULL, base);
1369 else if (PyUnicode_Check(v))
1370 return PyLong_FromUnicode(PyUnicode_AS_UNICODE(v),
1371 PyUnicode_GET_SIZE(v),
1372 base);
1373 else {
Barry Warsaw226ae6c1999-10-12 19:54:53 +00001374 PyErr_SetString(PyExc_TypeError,
1375 "can't convert non-string with explicit base");
1376 return NULL;
1377 }
Barry Warsaw226ae6c1999-10-12 19:54:53 +00001378}
1379
1380static char long_doc[] =
1381"long(x) -> long integer\n\
1382long(x, base) -> long integer\n\
1383\n\
1384Convert a string or number to a long integer, if possible. A floating\n\
1385point argument will be truncated towards zero (this does not include a\n\
1386string representation of a floating point number!) When converting a\n\
1387string, use the given base. It is an error to supply a base when\n\
1388converting a non-string.";
1389
1390
1391static PyObject *
1392builtin_float(self, args)
1393 PyObject *self;
1394 PyObject *args;
1395{
1396 PyObject *v;
1397
1398 if (!PyArg_ParseTuple(args, "O:float", &v))
1399 return NULL;
1400 if (PyString_Check(v))
1401 return PyFloat_FromString(v, NULL);
1402 return PyNumber_Float(v);
1403}
1404
1405static char float_doc[] =
1406"float(x) -> floating point number\n\
1407\n\
1408Convert a string or number to a floating point number, if possible.";
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001409
1410
Guido van Rossum79f25d91997-04-29 20:08:16 +00001411static PyObject *
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001412builtin_len(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001413 PyObject *self;
1414 PyObject *args;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001415{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001416 PyObject *v;
Guido van Rossum8ea9f4d1998-06-29 22:26:50 +00001417 long res;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001418
Guido van Rossum79f25d91997-04-29 20:08:16 +00001419 if (!PyArg_ParseTuple(args, "O:len", &v))
Guido van Rossum3f5da241990-12-20 15:06:42 +00001420 return NULL;
Guido van Rossum8ea9f4d1998-06-29 22:26:50 +00001421 res = PyObject_Length(v);
1422 if (res < 0 && PyErr_Occurred())
1423 return NULL;
1424 return PyInt_FromLong(res);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001425}
1426
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001427static char len_doc[] =
1428"len(object) -> integer\n\
1429\n\
1430Return the number of items of a sequence or mapping.";
1431
1432
Guido van Rossum79f25d91997-04-29 20:08:16 +00001433static PyObject *
Guido van Rossumd1705771996-04-09 02:41:06 +00001434builtin_list(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001435 PyObject *self;
1436 PyObject *args;
Guido van Rossumd1705771996-04-09 02:41:06 +00001437{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001438 PyObject *v;
Guido van Rossumd1705771996-04-09 02:41:06 +00001439
Guido van Rossum79f25d91997-04-29 20:08:16 +00001440 if (!PyArg_ParseTuple(args, "O:list", &v))
Guido van Rossumd1705771996-04-09 02:41:06 +00001441 return NULL;
Guido van Rossum09df08a1998-05-22 00:51:39 +00001442 return PySequence_List(v);
Guido van Rossumd1705771996-04-09 02:41:06 +00001443}
1444
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001445static char list_doc[] =
1446"list(sequence) -> list\n\
1447\n\
1448Return a new list whose items are the same as those of the argument sequence.";
1449
Guido van Rossum8861b741996-07-30 16:49:37 +00001450
1451static PyObject *
1452builtin_slice(self, args)
1453 PyObject *self;
1454 PyObject *args;
1455{
Guido van Rossum09df08a1998-05-22 00:51:39 +00001456 PyObject *start, *stop, *step;
Guido van Rossum8861b741996-07-30 16:49:37 +00001457
Guido van Rossum09df08a1998-05-22 00:51:39 +00001458 start = stop = step = NULL;
Guido van Rossum8861b741996-07-30 16:49:37 +00001459
Guido van Rossum09df08a1998-05-22 00:51:39 +00001460 if (!PyArg_ParseTuple(args, "O|OO:slice", &start, &stop, &step))
1461 return NULL;
Guido van Rossum8861b741996-07-30 16:49:37 +00001462
Guido van Rossum09df08a1998-05-22 00:51:39 +00001463 /* This swapping of stop and start is to maintain similarity with
1464 range(). */
1465 if (stop == NULL) {
1466 stop = start;
1467 start = NULL;
1468 }
1469 return PySlice_New(start, stop, step);
Guido van Rossum8861b741996-07-30 16:49:37 +00001470}
1471
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001472static char slice_doc[] =
Fred Drake3d587441999-07-19 15:21:16 +00001473"slice([start,] stop[, step]) -> slice object\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001474\n\
1475Create a slice object. This is used for slicing by the Numeric extensions.";
1476
1477
Guido van Rossum79f25d91997-04-29 20:08:16 +00001478static PyObject *
Guido van Rossum872537c1995-07-07 22:43:42 +00001479builtin_locals(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001480 PyObject *self;
1481 PyObject *args;
Guido van Rossum872537c1995-07-07 22:43:42 +00001482{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001483 PyObject *d;
Guido van Rossum872537c1995-07-07 22:43:42 +00001484
Guido van Rossum43713e52000-02-29 13:59:29 +00001485 if (!PyArg_ParseTuple(args, ":locals"))
Guido van Rossum872537c1995-07-07 22:43:42 +00001486 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001487 d = PyEval_GetLocals();
1488 Py_INCREF(d);
Guido van Rossum872537c1995-07-07 22:43:42 +00001489 return d;
1490}
1491
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001492static char locals_doc[] =
1493"locals() -> dictionary\n\
1494\n\
1495Return the dictionary containing the current scope's local variables.";
1496
1497
Guido van Rossum79f25d91997-04-29 20:08:16 +00001498static PyObject *
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001499min_max(args, sign)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001500 PyObject *args;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001501 int sign;
1502{
Guido van Rossum2d951851994-08-29 12:52:16 +00001503 int i;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001504 PyObject *v, *w, *x;
1505 PySequenceMethods *sq;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001506
Guido van Rossum79f25d91997-04-29 20:08:16 +00001507 if (PyTuple_Size(args) > 1)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001508 v = args;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001509 else if (!PyArg_ParseTuple(args, "O:min/max", &v))
Guido van Rossum3f5da241990-12-20 15:06:42 +00001510 return NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001511 sq = v->ob_type->tp_as_sequence;
Guido van Rossum09df08a1998-05-22 00:51:39 +00001512 if (sq == NULL || sq->sq_item == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001513 PyErr_SetString(PyExc_TypeError,
1514 "min() or max() of non-sequence");
Guido van Rossum3f5da241990-12-20 15:06:42 +00001515 return NULL;
1516 }
Guido van Rossum2d951851994-08-29 12:52:16 +00001517 w = NULL;
1518 for (i = 0; ; i++) {
Guido van Rossum3f5da241990-12-20 15:06:42 +00001519 x = (*sq->sq_item)(v, i); /* Implies INCREF */
Guido van Rossum2d951851994-08-29 12:52:16 +00001520 if (x == NULL) {
Barry Warsawcde8b1b1997-08-22 21:14:38 +00001521 if (PyErr_ExceptionMatches(PyExc_IndexError)) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001522 PyErr_Clear();
Guido van Rossum2d951851994-08-29 12:52:16 +00001523 break;
1524 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00001525 Py_XDECREF(w);
Guido van Rossum2d951851994-08-29 12:52:16 +00001526 return NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001527 }
Guido van Rossum2d951851994-08-29 12:52:16 +00001528 if (w == NULL)
1529 w = x;
1530 else {
Guido van Rossumc8b6df91997-05-23 00:06:51 +00001531 int c = PyObject_Compare(x, w);
1532 if (c && PyErr_Occurred()) {
1533 Py_DECREF(x);
1534 Py_XDECREF(w);
1535 return NULL;
1536 }
1537 if (c * sign > 0) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001538 Py_DECREF(w);
Guido van Rossum2d951851994-08-29 12:52:16 +00001539 w = x;
1540 }
1541 else
Guido van Rossum79f25d91997-04-29 20:08:16 +00001542 Py_DECREF(x);
Guido van Rossum2d951851994-08-29 12:52:16 +00001543 }
Guido van Rossum3f5da241990-12-20 15:06:42 +00001544 }
Guido van Rossum2d951851994-08-29 12:52:16 +00001545 if (w == NULL)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001546 PyErr_SetString(PyExc_ValueError,
1547 "min() or max() of empty sequence");
Guido van Rossum3f5da241990-12-20 15:06:42 +00001548 return w;
1549}
1550
Guido van Rossum79f25d91997-04-29 20:08:16 +00001551static PyObject *
Guido van Rossum3f5da241990-12-20 15:06:42 +00001552builtin_min(self, v)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001553 PyObject *self;
1554 PyObject *v;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001555{
1556 return min_max(v, -1);
1557}
1558
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001559static char min_doc[] =
1560"min(sequence) -> value\n\
1561min(a, b, c, ...) -> value\n\
1562\n\
1563With a single sequence argument, return its smallest item.\n\
1564With two or more arguments, return the smallest argument.";
1565
1566
Guido van Rossum79f25d91997-04-29 20:08:16 +00001567static PyObject *
Guido van Rossum3f5da241990-12-20 15:06:42 +00001568builtin_max(self, v)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001569 PyObject *self;
1570 PyObject *v;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001571{
1572 return min_max(v, 1);
1573}
1574
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001575static char max_doc[] =
1576"max(sequence) -> value\n\
1577max(a, b, c, ...) -> value\n\
1578\n\
1579With a single sequence argument, return its largest item.\n\
1580With two or more arguments, return the largest argument.";
1581
1582
Guido van Rossum79f25d91997-04-29 20:08:16 +00001583static PyObject *
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001584builtin_oct(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001585 PyObject *self;
1586 PyObject *args;
Guido van Rossum006bcd41991-10-24 14:54:44 +00001587{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001588 PyObject *v;
1589 PyNumberMethods *nb;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001590
Guido van Rossum79f25d91997-04-29 20:08:16 +00001591 if (!PyArg_ParseTuple(args, "O:oct", &v))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001592 return NULL;
Guido van Rossum1899c2e1992-09-12 11:09:23 +00001593 if (v == NULL || (nb = v->ob_type->tp_as_number) == NULL ||
1594 nb->nb_oct == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001595 PyErr_SetString(PyExc_TypeError,
Guido van Rossum1899c2e1992-09-12 11:09:23 +00001596 "oct() argument can't be converted to oct");
1597 return NULL;
Guido van Rossum006bcd41991-10-24 14:54:44 +00001598 }
Guido van Rossum1899c2e1992-09-12 11:09:23 +00001599 return (*nb->nb_oct)(v);
Guido van Rossum006bcd41991-10-24 14:54:44 +00001600}
1601
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001602static char oct_doc[] =
1603"oct(number) -> string\n\
1604\n\
1605Return the octal representation of an integer or long integer.";
1606
1607
Guido van Rossum79f25d91997-04-29 20:08:16 +00001608static PyObject *
Guido van Rossum94390a41992-08-14 15:14:30 +00001609builtin_open(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001610 PyObject *self;
1611 PyObject *args;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001612{
Guido van Rossum2d951851994-08-29 12:52:16 +00001613 char *name;
1614 char *mode = "r";
1615 int bufsize = -1;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001616 PyObject *f;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001617
Guido van Rossum79f25d91997-04-29 20:08:16 +00001618 if (!PyArg_ParseTuple(args, "s|si:open", &name, &mode, &bufsize))
Guido van Rossum3f5da241990-12-20 15:06:42 +00001619 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001620 f = PyFile_FromString(name, mode);
Guido van Rossum2d951851994-08-29 12:52:16 +00001621 if (f != NULL)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001622 PyFile_SetBufSize(f, bufsize);
Guido van Rossum2d951851994-08-29 12:52:16 +00001623 return f;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001624}
1625
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001626static char open_doc[] =
1627"open(filename[, mode[, buffering]]) -> file object\n\
1628\n\
1629Open a file. The mode can be 'r', 'w' or 'a' for reading (default),\n\
1630writing or appending. The file will be created if it doesn't exist\n\
1631when opened for writing or appending; it will be truncated when\n\
1632opened for writing. Add a 'b' to the mode for binary files.\n\
1633Add a '+' to the mode to allow simultaneous reading and writing.\n\
1634If the buffering argument is given, 0 means unbuffered, 1 means line\n\
1635buffered, and larger numbers specify the buffer size.";
1636
1637
Guido van Rossum79f25d91997-04-29 20:08:16 +00001638static PyObject *
Guido van Rossum94390a41992-08-14 15:14:30 +00001639builtin_ord(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001640 PyObject *self;
1641 PyObject *args;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001642{
Guido van Rossum09095f32000-03-10 23:00:52 +00001643 PyObject *obj;
1644 long ord;
Jeremy Hylton394b54d2000-04-12 21:19:47 +00001645 int size;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001646
Guido van Rossum09095f32000-03-10 23:00:52 +00001647 if (!PyArg_ParseTuple(args, "O:ord", &obj))
Guido van Rossum3f5da241990-12-20 15:06:42 +00001648 return NULL;
Guido van Rossum09095f32000-03-10 23:00:52 +00001649
Jeremy Hylton394b54d2000-04-12 21:19:47 +00001650 if (PyString_Check(obj)) {
1651 size = PyString_GET_SIZE(obj);
1652 if (size == 1)
1653 ord = (long)((unsigned char)*PyString_AS_STRING(obj));
1654 } else if (PyUnicode_Check(obj)) {
1655 size = PyUnicode_GET_SIZE(obj);
1656 if (size == 1)
1657 ord = (long)*PyUnicode_AS_UNICODE(obj);
1658 } else {
1659 PyErr_Format(PyExc_TypeError,
Fred Drake078b24f2000-04-13 02:42:50 +00001660 "expected string or Unicode character, " \
Jeremy Hylton394b54d2000-04-12 21:19:47 +00001661 "%.200s found", obj->ob_type->tp_name);
Guido van Rossum09095f32000-03-10 23:00:52 +00001662 return NULL;
1663 }
Jeremy Hylton394b54d2000-04-12 21:19:47 +00001664 if (size == 1)
1665 return PyInt_FromLong(ord);
Guido van Rossum09095f32000-03-10 23:00:52 +00001666
Jeremy Hylton394b54d2000-04-12 21:19:47 +00001667 PyErr_Format(PyExc_TypeError,
1668 "expected a character, length-%d string found",
1669 size);
1670 return NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001671}
1672
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001673static char ord_doc[] =
1674"ord(c) -> integer\n\
1675\n\
Fred Drake078b24f2000-04-13 02:42:50 +00001676Return the integer ordinal of a one character string.";
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001677
1678
Guido van Rossum79f25d91997-04-29 20:08:16 +00001679static PyObject *
Guido van Rossum6a00cd81995-01-07 12:39:01 +00001680builtin_pow(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001681 PyObject *self;
1682 PyObject *args;
Guido van Rossum6a00cd81995-01-07 12:39:01 +00001683{
Guido van Rossum09df08a1998-05-22 00:51:39 +00001684 PyObject *v, *w, *z = Py_None;
Guido van Rossum6a00cd81995-01-07 12:39:01 +00001685
Guido van Rossum79f25d91997-04-29 20:08:16 +00001686 if (!PyArg_ParseTuple(args, "OO|O:pow", &v, &w, &z))
Guido van Rossum6a00cd81995-01-07 12:39:01 +00001687 return NULL;
Guido van Rossum09df08a1998-05-22 00:51:39 +00001688 return PyNumber_Power(v, w, z);
Guido van Rossumd4905451991-05-05 20:00:36 +00001689}
1690
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001691static char pow_doc[] =
1692"pow(x, y[, z]) -> number\n\
1693\n\
1694With two arguments, equivalent to x**y. With three arguments,\n\
1695equivalent to (x**y) % z, but may be more efficient (e.g. for longs).";
1696
1697
Guido van Rossum124eff01999-02-23 16:11:01 +00001698/* Return number of items in range/xrange (lo, hi, step). step > 0
1699 * required. Return a value < 0 if & only if the true value is too
1700 * large to fit in a signed long.
1701 */
1702static long
1703get_len_of_range(lo, hi, step)
1704 long lo;
1705 long hi;
1706 long step; /* must be > 0 */
1707{
1708 /* -------------------------------------------------------------
1709 If lo >= hi, the range is empty.
1710 Else if n values are in the range, the last one is
1711 lo + (n-1)*step, which must be <= hi-1. Rearranging,
1712 n <= (hi - lo - 1)/step + 1, so taking the floor of the RHS gives
1713 the proper value. Since lo < hi in this case, hi-lo-1 >= 0, so
1714 the RHS is non-negative and so truncation is the same as the
1715 floor. Letting M be the largest positive long, the worst case
1716 for the RHS numerator is hi=M, lo=-M-1, and then
1717 hi-lo-1 = M-(-M-1)-1 = 2*M. Therefore unsigned long has enough
1718 precision to compute the RHS exactly.
1719 ---------------------------------------------------------------*/
1720 long n = 0;
1721 if (lo < hi) {
1722 unsigned long uhi = (unsigned long)hi;
1723 unsigned long ulo = (unsigned long)lo;
1724 unsigned long diff = uhi - ulo - 1;
1725 n = (long)(diff / (unsigned long)step + 1);
1726 }
1727 return n;
1728}
1729
Guido van Rossum79f25d91997-04-29 20:08:16 +00001730static PyObject *
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001731builtin_range(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001732 PyObject *self;
1733 PyObject *args;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001734{
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001735 long ilow = 0, ihigh = 0, istep = 1;
Guido van Rossum124eff01999-02-23 16:11:01 +00001736 long bign;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001737 int i, n;
Guido van Rossum124eff01999-02-23 16:11:01 +00001738
Guido van Rossum79f25d91997-04-29 20:08:16 +00001739 PyObject *v;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001740
Guido van Rossum79f25d91997-04-29 20:08:16 +00001741 if (PyTuple_Size(args) <= 1) {
1742 if (!PyArg_ParseTuple(args,
Guido van Rossum0865dd91995-01-17 16:30:22 +00001743 "l;range() requires 1-3 int arguments",
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001744 &ihigh))
1745 return NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001746 }
1747 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001748 if (!PyArg_ParseTuple(args,
Guido van Rossum0865dd91995-01-17 16:30:22 +00001749 "ll|l;range() requires 1-3 int arguments",
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001750 &ilow, &ihigh, &istep))
Guido van Rossum3f5da241990-12-20 15:06:42 +00001751 return NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001752 }
1753 if (istep == 0) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001754 PyErr_SetString(PyExc_ValueError, "zero step for range()");
Guido van Rossum3f5da241990-12-20 15:06:42 +00001755 return NULL;
1756 }
Guido van Rossum124eff01999-02-23 16:11:01 +00001757 if (istep > 0)
1758 bign = get_len_of_range(ilow, ihigh, istep);
1759 else
1760 bign = get_len_of_range(ihigh, ilow, -istep);
1761 n = (int)bign;
1762 if (bign < 0 || (long)n != bign) {
Guido van Rossume23cde21999-01-12 05:07:47 +00001763 PyErr_SetString(PyExc_OverflowError,
Guido van Rossum124eff01999-02-23 16:11:01 +00001764 "range() has too many items");
Guido van Rossume23cde21999-01-12 05:07:47 +00001765 return NULL;
1766 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00001767 v = PyList_New(n);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001768 if (v == NULL)
1769 return NULL;
1770 for (i = 0; i < n; i++) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001771 PyObject *w = PyInt_FromLong(ilow);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001772 if (w == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001773 Py_DECREF(v);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001774 return NULL;
1775 }
Guido van Rossuma937d141998-04-24 18:22:02 +00001776 PyList_SET_ITEM(v, i, w);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001777 ilow += istep;
1778 }
1779 return v;
1780}
1781
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001782static char range_doc[] =
1783"range([start,] stop[, step]) -> list of integers\n\
1784\n\
1785Return a list containing an arithmetic progression of integers.\n\
1786range(i, j) returns [i, i+1, i+2, ..., j-1]; start (!) defaults to 0.\n\
1787When step is given, it specifies the increment (or decrement).\n\
1788For example, range(4) returns [0, 1, 2, 3]. The end point is omitted!\n\
1789These are exactly the valid indices for a list of 4 elements.";
1790
1791
Guido van Rossum79f25d91997-04-29 20:08:16 +00001792static PyObject *
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001793builtin_xrange(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001794 PyObject *self;
1795 PyObject *args;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001796{
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001797 long ilow = 0, ihigh = 0, istep = 1;
Guido van Rossum0865dd91995-01-17 16:30:22 +00001798 long n;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001799
Guido van Rossum79f25d91997-04-29 20:08:16 +00001800 if (PyTuple_Size(args) <= 1) {
1801 if (!PyArg_ParseTuple(args,
Guido van Rossum0865dd91995-01-17 16:30:22 +00001802 "l;xrange() requires 1-3 int arguments",
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001803 &ihigh))
1804 return NULL;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001805 }
1806 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001807 if (!PyArg_ParseTuple(args,
Guido van Rossum0865dd91995-01-17 16:30:22 +00001808 "ll|l;xrange() requires 1-3 int arguments",
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001809 &ilow, &ihigh, &istep))
Guido van Rossum12d12c51993-10-26 17:58:25 +00001810 return NULL;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001811 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001812 if (istep == 0) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001813 PyErr_SetString(PyExc_ValueError, "zero step for xrange()");
Guido van Rossum12d12c51993-10-26 17:58:25 +00001814 return NULL;
1815 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001816 if (istep > 0)
Guido van Rossum124eff01999-02-23 16:11:01 +00001817 n = get_len_of_range(ilow, ihigh, istep);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001818 else
Guido van Rossum124eff01999-02-23 16:11:01 +00001819 n = get_len_of_range(ihigh, ilow, -istep);
1820 if (n < 0) {
1821 PyErr_SetString(PyExc_OverflowError,
1822 "xrange() has more than sys.maxint items");
1823 return NULL;
1824 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00001825 return PyRange_New(ilow, n, istep, 1);
Guido van Rossum12d12c51993-10-26 17:58:25 +00001826}
1827
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001828static char xrange_doc[] =
1829"xrange([start,] stop[, step]) -> xrange object\n\
1830\n\
1831Like range(), but instead of returning a list, returns an object that\n\
1832generates the numbers in the range on demand. This is slightly slower\n\
1833than range() but more memory efficient.";
1834
1835
Guido van Rossum79f25d91997-04-29 20:08:16 +00001836static PyObject *
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001837builtin_raw_input(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001838 PyObject *self;
1839 PyObject *args;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001840{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001841 PyObject *v = NULL;
1842 PyObject *f;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001843
Guido van Rossum79f25d91997-04-29 20:08:16 +00001844 if (!PyArg_ParseTuple(args, "|O:[raw_]input", &v))
Guido van Rossum3165fe61992-09-25 21:59:05 +00001845 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001846 if (PyFile_AsFile(PySys_GetObject("stdin")) == stdin &&
1847 PyFile_AsFile(PySys_GetObject("stdout")) == stdout &&
Guido van Rossum53bb7ff1995-07-26 16:26:31 +00001848 isatty(fileno(stdin)) && isatty(fileno(stdout))) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001849 PyObject *po;
Guido van Rossum872537c1995-07-07 22:43:42 +00001850 char *prompt;
1851 char *s;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001852 PyObject *result;
Guido van Rossum872537c1995-07-07 22:43:42 +00001853 if (v != NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001854 po = PyObject_Str(v);
Guido van Rossum872537c1995-07-07 22:43:42 +00001855 if (po == NULL)
1856 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001857 prompt = PyString_AsString(po);
Guido van Rossumd9b52081998-06-26 18:25:38 +00001858 if (prompt == NULL)
1859 return NULL;
Guido van Rossum872537c1995-07-07 22:43:42 +00001860 }
1861 else {
1862 po = NULL;
1863 prompt = "";
1864 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00001865 s = PyOS_Readline(prompt);
1866 Py_XDECREF(po);
Guido van Rossum872537c1995-07-07 22:43:42 +00001867 if (s == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001868 PyErr_SetNone(PyExc_KeyboardInterrupt);
Guido van Rossum872537c1995-07-07 22:43:42 +00001869 return NULL;
1870 }
1871 if (*s == '\0') {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001872 PyErr_SetNone(PyExc_EOFError);
Guido van Rossum872537c1995-07-07 22:43:42 +00001873 result = NULL;
1874 }
1875 else { /* strip trailing '\n' */
Guido van Rossum79f25d91997-04-29 20:08:16 +00001876 result = PyString_FromStringAndSize(s, strlen(s)-1);
Guido van Rossum872537c1995-07-07 22:43:42 +00001877 }
Guido van Rossumb18618d2000-05-03 23:44:39 +00001878 PyMem_FREE(s);
Guido van Rossum872537c1995-07-07 22:43:42 +00001879 return result;
1880 }
Guido van Rossum90933611991-06-07 16:10:43 +00001881 if (v != NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001882 f = PySys_GetObject("stdout");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001883 if (f == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001884 PyErr_SetString(PyExc_RuntimeError, "lost sys.stdout");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001885 return NULL;
1886 }
Guido van Rossumc8b6df91997-05-23 00:06:51 +00001887 if (Py_FlushLine() != 0 ||
1888 PyFile_WriteObject(v, f, Py_PRINT_RAW) != 0)
Guido van Rossum90933611991-06-07 16:10:43 +00001889 return NULL;
1890 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00001891 f = PySys_GetObject("stdin");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001892 if (f == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001893 PyErr_SetString(PyExc_RuntimeError, "lost sys.stdin");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001894 return NULL;
1895 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00001896 return PyFile_GetLine(f, -1);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001897}
1898
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001899static char raw_input_doc[] =
1900"raw_input([prompt]) -> string\n\
1901\n\
1902Read a string from standard input. The trailing newline is stripped.\n\
1903If the user hits EOF (Unix: Ctl-D, Windows: Ctl-Z+Return), raise EOFError.\n\
1904On Unix, GNU readline is used if enabled. The prompt string, if given,\n\
1905is printed without a trailing newline before reading.";
1906
1907
Guido van Rossum79f25d91997-04-29 20:08:16 +00001908static PyObject *
Guido van Rossum12d12c51993-10-26 17:58:25 +00001909builtin_reduce(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001910 PyObject *self;
1911 PyObject *args;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001912{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001913 PyObject *seq, *func, *result = NULL;
1914 PySequenceMethods *sqf;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001915 register int i;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001916
Guido van Rossum79f25d91997-04-29 20:08:16 +00001917 if (!PyArg_ParseTuple(args, "OO|O:reduce", &func, &seq, &result))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001918 return NULL;
1919 if (result != NULL)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001920 Py_INCREF(result);
Guido van Rossum12d12c51993-10-26 17:58:25 +00001921
Guido van Rossum09df08a1998-05-22 00:51:39 +00001922 sqf = seq->ob_type->tp_as_sequence;
1923 if (sqf == NULL || sqf->sq_item == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001924 PyErr_SetString(PyExc_TypeError,
Guido van Rossum12d12c51993-10-26 17:58:25 +00001925 "2nd argument to reduce() must be a sequence object");
1926 return NULL;
1927 }
1928
Guido van Rossum79f25d91997-04-29 20:08:16 +00001929 if ((args = PyTuple_New(2)) == NULL)
Guido van Rossum2d951851994-08-29 12:52:16 +00001930 goto Fail;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001931
Guido van Rossum2d951851994-08-29 12:52:16 +00001932 for (i = 0; ; ++i) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001933 PyObject *op2;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001934
1935 if (args->ob_refcnt > 1) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001936 Py_DECREF(args);
1937 if ((args = PyTuple_New(2)) == NULL)
Guido van Rossum2d951851994-08-29 12:52:16 +00001938 goto Fail;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001939 }
1940
Guido van Rossum2d951851994-08-29 12:52:16 +00001941 if ((op2 = (*sqf->sq_item)(seq, i)) == NULL) {
Barry Warsawcde8b1b1997-08-22 21:14:38 +00001942 if (PyErr_ExceptionMatches(PyExc_IndexError)) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001943 PyErr_Clear();
Guido van Rossum2d951851994-08-29 12:52:16 +00001944 break;
1945 }
1946 goto Fail;
1947 }
Guido van Rossum12d12c51993-10-26 17:58:25 +00001948
Guido van Rossum2d951851994-08-29 12:52:16 +00001949 if (result == NULL)
1950 result = op2;
1951 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001952 PyTuple_SetItem(args, 0, result);
1953 PyTuple_SetItem(args, 1, op2);
1954 if ((result = PyEval_CallObject(func, args)) == NULL)
Guido van Rossum2d951851994-08-29 12:52:16 +00001955 goto Fail;
1956 }
Guido van Rossum12d12c51993-10-26 17:58:25 +00001957 }
1958
Guido van Rossum79f25d91997-04-29 20:08:16 +00001959 Py_DECREF(args);
Guido van Rossum12d12c51993-10-26 17:58:25 +00001960
Guido van Rossum2d951851994-08-29 12:52:16 +00001961 if (result == NULL)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001962 PyErr_SetString(PyExc_TypeError,
Guido van Rossum2d951851994-08-29 12:52:16 +00001963 "reduce of empty sequence with no initial value");
1964
Guido van Rossum12d12c51993-10-26 17:58:25 +00001965 return result;
1966
Guido van Rossum2d951851994-08-29 12:52:16 +00001967Fail:
Guido van Rossum79f25d91997-04-29 20:08:16 +00001968 Py_XDECREF(args);
1969 Py_XDECREF(result);
Guido van Rossum12d12c51993-10-26 17:58:25 +00001970 return NULL;
1971}
1972
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001973static char reduce_doc[] =
1974"reduce(function, sequence[, initial]) -> value\n\
1975\n\
1976Apply a function of two arguments cumulatively to the items of a sequence,\n\
1977from left to right, so as to reduce the sequence to a single value.\n\
1978For example, reduce(lambda x, y: x+y, [1, 2, 3, 4, 5]) calculates\n\
1979((((1+2)+3)+4)+5). If initial is present, it is placed before the items\n\
1980of the sequence in the calculation, and serves as a default when the\n\
1981sequence is empty.";
1982
1983
Guido van Rossum79f25d91997-04-29 20:08:16 +00001984static PyObject *
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001985builtin_reload(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001986 PyObject *self;
1987 PyObject *args;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001988{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001989 PyObject *v;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001990
Guido van Rossum79f25d91997-04-29 20:08:16 +00001991 if (!PyArg_ParseTuple(args, "O:reload", &v))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001992 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001993 return PyImport_ReloadModule(v);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001994}
1995
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001996static char reload_doc[] =
1997"reload(module) -> module\n\
1998\n\
1999Reload the module. The module must have been successfully imported before.";
2000
2001
Guido van Rossum79f25d91997-04-29 20:08:16 +00002002static PyObject *
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002003builtin_repr(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00002004 PyObject *self;
2005 PyObject *args;
Guido van Rossumc89705d1992-11-26 08:54:07 +00002006{
Guido van Rossum79f25d91997-04-29 20:08:16 +00002007 PyObject *v;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002008
Guido van Rossum79f25d91997-04-29 20:08:16 +00002009 if (!PyArg_ParseTuple(args, "O:repr", &v))
Guido van Rossumc89705d1992-11-26 08:54:07 +00002010 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002011 return PyObject_Repr(v);
Guido van Rossumc89705d1992-11-26 08:54:07 +00002012}
2013
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002014static char repr_doc[] =
2015"repr(object) -> string\n\
2016\n\
2017Return the canonical string representation of the object.\n\
2018For most object types, eval(repr(object)) == object.";
2019
2020
Guido van Rossum79f25d91997-04-29 20:08:16 +00002021static PyObject *
Guido van Rossum9e51f9b1993-02-12 16:29:05 +00002022builtin_round(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00002023 PyObject *self;
2024 PyObject *args;
Guido van Rossum9e51f9b1993-02-12 16:29:05 +00002025{
Guido van Rossum9e51f9b1993-02-12 16:29:05 +00002026 double x;
2027 double f;
2028 int ndigits = 0;
Guido van Rossum9e51f9b1993-02-12 16:29:05 +00002029 int i;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002030
Guido van Rossum79f25d91997-04-29 20:08:16 +00002031 if (!PyArg_ParseTuple(args, "d|i:round", &x, &ndigits))
Guido van Rossum9e51f9b1993-02-12 16:29:05 +00002032 return NULL;
Guido van Rossum9e51f9b1993-02-12 16:29:05 +00002033 f = 1.0;
Guido van Rossum1e162d31998-05-09 14:42:25 +00002034 i = abs(ndigits);
2035 while (--i >= 0)
Guido van Rossum9e51f9b1993-02-12 16:29:05 +00002036 f = f*10.0;
Guido van Rossum1e162d31998-05-09 14:42:25 +00002037 if (ndigits < 0)
2038 x /= f;
Guido van Rossum9e51f9b1993-02-12 16:29:05 +00002039 else
Guido van Rossum1e162d31998-05-09 14:42:25 +00002040 x *= f;
2041 if (x >= 0.0)
2042 x = floor(x + 0.5);
2043 else
2044 x = ceil(x - 0.5);
2045 if (ndigits < 0)
2046 x *= f;
2047 else
2048 x /= f;
2049 return PyFloat_FromDouble(x);
Guido van Rossum9e51f9b1993-02-12 16:29:05 +00002050}
2051
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002052static char round_doc[] =
2053"round(number[, ndigits]) -> floating point number\n\
2054\n\
2055Round a number to a given precision in decimal digits (default 0 digits).\n\
2056This always returns a floating point number. Precision may be negative.";
2057
2058
Guido van Rossum79f25d91997-04-29 20:08:16 +00002059static PyObject *
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002060builtin_str(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00002061 PyObject *self;
2062 PyObject *args;
Guido van Rossumc89705d1992-11-26 08:54:07 +00002063{
Guido van Rossum79f25d91997-04-29 20:08:16 +00002064 PyObject *v;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002065
Guido van Rossum79f25d91997-04-29 20:08:16 +00002066 if (!PyArg_ParseTuple(args, "O:str", &v))
Guido van Rossumc89705d1992-11-26 08:54:07 +00002067 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002068 return PyObject_Str(v);
Guido van Rossumc89705d1992-11-26 08:54:07 +00002069}
2070
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002071static char str_doc[] =
2072"str(object) -> string\n\
2073\n\
2074Return a nice string representation of the object.\n\
2075If the argument is a string, the return value is the same object.";
2076
2077
Guido van Rossum79f25d91997-04-29 20:08:16 +00002078static PyObject *
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002079builtin_tuple(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00002080 PyObject *self;
2081 PyObject *args;
Guido van Rossumcae027b1994-08-29 12:53:11 +00002082{
Guido van Rossum79f25d91997-04-29 20:08:16 +00002083 PyObject *v;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002084
Guido van Rossum79f25d91997-04-29 20:08:16 +00002085 if (!PyArg_ParseTuple(args, "O:tuple", &v))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002086 return NULL;
Guido van Rossum09df08a1998-05-22 00:51:39 +00002087 return PySequence_Tuple(v);
Guido van Rossumcae027b1994-08-29 12:53:11 +00002088}
2089
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002090static char tuple_doc[] =
2091"tuple(sequence) -> list\n\
2092\n\
2093Return a tuple whose items are the same as those of the argument sequence.\n\
2094If the argument is a tuple, the return value is the same object.";
2095
2096
Guido van Rossum79f25d91997-04-29 20:08:16 +00002097static PyObject *
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002098builtin_type(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00002099 PyObject *self;
2100 PyObject *args;
Guido van Rossum3f5da241990-12-20 15:06:42 +00002101{
Guido van Rossum79f25d91997-04-29 20:08:16 +00002102 PyObject *v;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002103
Guido van Rossum79f25d91997-04-29 20:08:16 +00002104 if (!PyArg_ParseTuple(args, "O:type", &v))
Guido van Rossum3f5da241990-12-20 15:06:42 +00002105 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002106 v = (PyObject *)v->ob_type;
2107 Py_INCREF(v);
Guido van Rossum3f5da241990-12-20 15:06:42 +00002108 return v;
2109}
2110
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002111static char type_doc[] =
2112"type(object) -> type object\n\
2113\n\
2114Return the type of the object.";
2115
2116
Guido van Rossum79f25d91997-04-29 20:08:16 +00002117static PyObject *
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002118builtin_vars(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00002119 PyObject *self;
2120 PyObject *args;
Guido van Rossum2d951851994-08-29 12:52:16 +00002121{
Guido van Rossum79f25d91997-04-29 20:08:16 +00002122 PyObject *v = NULL;
2123 PyObject *d;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002124
Guido van Rossum79f25d91997-04-29 20:08:16 +00002125 if (!PyArg_ParseTuple(args, "|O:vars", &v))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002126 return NULL;
Guido van Rossum2d951851994-08-29 12:52:16 +00002127 if (v == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002128 d = PyEval_GetLocals();
Guido van Rossum53bb7ff1995-07-26 16:26:31 +00002129 if (d == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002130 if (!PyErr_Occurred())
2131 PyErr_SetString(PyExc_SystemError,
2132 "no locals!?");
Guido van Rossum53bb7ff1995-07-26 16:26:31 +00002133 }
2134 else
Guido van Rossum79f25d91997-04-29 20:08:16 +00002135 Py_INCREF(d);
Guido van Rossum2d951851994-08-29 12:52:16 +00002136 }
2137 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002138 d = PyObject_GetAttrString(v, "__dict__");
Guido van Rossum2d951851994-08-29 12:52:16 +00002139 if (d == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002140 PyErr_SetString(PyExc_TypeError,
Guido van Rossum2d951851994-08-29 12:52:16 +00002141 "vars() argument must have __dict__ attribute");
2142 return NULL;
2143 }
2144 }
2145 return d;
2146}
2147
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002148static char vars_doc[] =
2149"vars([object]) -> dictionary\n\
2150\n\
2151Without arguments, equivalent to locals().\n\
2152With an argument, equivalent to object.__dict__.";
2153
Guido van Rossum668213d1999-06-16 17:28:37 +00002154static int
2155abstract_issubclass(derived, cls, err, first)
2156 PyObject *derived;
2157 PyObject *cls;
2158 char *err;
2159 int first;
2160{
2161 static PyObject *__bases__ = NULL;
2162 PyObject *bases;
Guido van Rossum7f851861999-06-17 19:12:39 +00002163 int i, n;
Guido van Rossum668213d1999-06-16 17:28:37 +00002164 int r = 0;
2165
2166 if (__bases__ == NULL) {
2167 __bases__ = PyString_FromString("__bases__");
2168 if (__bases__ == NULL)
2169 return -1;
2170 }
2171
2172 if (first) {
2173 bases = PyObject_GetAttr(cls, __bases__);
2174 if (bases == NULL || !PyTuple_Check(bases)) {
2175 Py_XDECREF(bases);
2176 PyErr_SetString(PyExc_TypeError, err);
2177 return -1;
2178 }
2179 Py_DECREF(bases);
2180 }
2181
2182 if (derived == cls)
2183 return 1;
2184
2185 bases = PyObject_GetAttr(derived, __bases__);
2186 if (bases == NULL || !PyTuple_Check(bases)) {
2187 Py_XDECREF(bases);
2188 PyErr_SetString(PyExc_TypeError, err);
2189 return -1;
2190 }
2191
2192 n = PyTuple_GET_SIZE(bases);
2193 for (i = 0; i < n; i++) {
2194 r = abstract_issubclass(PyTuple_GET_ITEM(bases, i),
2195 cls, err, 0);
2196 if (r != 0)
2197 break;
2198 }
2199
2200 Py_DECREF(bases);
2201
2202 return r;
2203}
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002204
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002205static PyObject *
2206builtin_isinstance(self, args)
2207 PyObject *self;
2208 PyObject *args;
2209{
2210 PyObject *inst;
2211 PyObject *cls;
Guido van Rossum668213d1999-06-16 17:28:37 +00002212 PyObject *icls;
2213 static PyObject *__class__ = NULL;
2214 int retval = 0;
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002215
Guido van Rossum43713e52000-02-29 13:59:29 +00002216 if (!PyArg_ParseTuple(args, "OO:isinstance", &inst, &cls))
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002217 return NULL;
Guido van Rossumf5dd9141997-12-02 19:11:45 +00002218
Guido van Rossum668213d1999-06-16 17:28:37 +00002219 if (PyClass_Check(cls)) {
2220 if (PyInstance_Check(inst)) {
Guido van Rossumf5dd9141997-12-02 19:11:45 +00002221 PyObject *inclass =
2222 (PyObject*)((PyInstanceObject*)inst)->in_class;
2223 retval = PyClass_IsSubclass(inclass, cls);
2224 }
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002225 }
Guido van Rossum668213d1999-06-16 17:28:37 +00002226 else if (PyType_Check(cls)) {
2227 retval = ((PyObject *)(inst->ob_type) == cls);
2228 }
2229 else if (!PyInstance_Check(inst)) {
2230 if (__class__ == NULL) {
2231 __class__ = PyString_FromString("__class__");
2232 if (__class__ == NULL)
2233 return NULL;
2234 }
2235 icls = PyObject_GetAttr(inst, __class__);
2236 if (icls != NULL) {
2237 retval = abstract_issubclass(
2238 icls, cls,
2239 "second argument must be a class",
2240 1);
2241 Py_DECREF(icls);
2242 if (retval < 0)
2243 return NULL;
2244 }
2245 else {
2246 PyErr_SetString(PyExc_TypeError,
2247 "second argument must be a class");
2248 return NULL;
2249 }
2250 }
2251 else {
2252 PyErr_SetString(PyExc_TypeError,
2253 "second argument must be a class");
2254 return NULL;
2255 }
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002256 return PyInt_FromLong(retval);
2257}
2258
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002259static char isinstance_doc[] =
2260"isinstance(object, class-or-type) -> Boolean\n\
2261\n\
2262Return whether an object is an instance of a class or of a subclass thereof.\n\
2263With a type as second argument, return whether that is the object's type.";
2264
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002265
2266static PyObject *
2267builtin_issubclass(self, args)
2268 PyObject *self;
2269 PyObject *args;
2270{
2271 PyObject *derived;
2272 PyObject *cls;
2273 int retval;
2274
Guido van Rossum43713e52000-02-29 13:59:29 +00002275 if (!PyArg_ParseTuple(args, "OO:issubclass", &derived, &cls))
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002276 return NULL;
Guido van Rossum668213d1999-06-16 17:28:37 +00002277
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002278 if (!PyClass_Check(derived) || !PyClass_Check(cls)) {
Guido van Rossum668213d1999-06-16 17:28:37 +00002279 retval = abstract_issubclass(
2280 derived, cls, "arguments must be classes", 1);
2281 if (retval < 0)
2282 return NULL;
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002283 }
Guido van Rossum668213d1999-06-16 17:28:37 +00002284 else {
2285 /* shortcut */
2286 if (!(retval = (derived == cls)))
2287 retval = PyClass_IsSubclass(derived, cls);
2288 }
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002289
2290 return PyInt_FromLong(retval);
2291}
2292
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002293static char issubclass_doc[] =
2294"issubclass(C, B) -> Boolean\n\
2295\n\
2296Return whether class C is a subclass (i.e., a derived class) of class B.";
2297
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002298
Guido van Rossum79f25d91997-04-29 20:08:16 +00002299static PyMethodDef builtin_methods[] = {
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002300 {"__import__", builtin___import__, 1, import_doc},
2301 {"abs", builtin_abs, 1, abs_doc},
2302 {"apply", builtin_apply, 1, apply_doc},
Guido van Rossum0daf0221999-03-19 19:07:19 +00002303 {"buffer", builtin_buffer, 1, buffer_doc},
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002304 {"callable", builtin_callable, 1, callable_doc},
2305 {"chr", builtin_chr, 1, chr_doc},
2306 {"cmp", builtin_cmp, 1, cmp_doc},
2307 {"coerce", builtin_coerce, 1, coerce_doc},
2308 {"compile", builtin_compile, 1, compile_doc},
Guido van Rossum8a5c5d21996-01-12 01:09:56 +00002309#ifndef WITHOUT_COMPLEX
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002310 {"complex", builtin_complex, 1, complex_doc},
Guido van Rossum8a5c5d21996-01-12 01:09:56 +00002311#endif
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002312 {"delattr", builtin_delattr, 1, delattr_doc},
2313 {"dir", builtin_dir, 1, dir_doc},
2314 {"divmod", builtin_divmod, 1, divmod_doc},
2315 {"eval", builtin_eval, 1, eval_doc},
2316 {"execfile", builtin_execfile, 1, execfile_doc},
2317 {"filter", builtin_filter, 1, filter_doc},
2318 {"float", builtin_float, 1, float_doc},
2319 {"getattr", builtin_getattr, 1, getattr_doc},
2320 {"globals", builtin_globals, 1, globals_doc},
2321 {"hasattr", builtin_hasattr, 1, hasattr_doc},
2322 {"hash", builtin_hash, 1, hash_doc},
2323 {"hex", builtin_hex, 1, hex_doc},
2324 {"id", builtin_id, 1, id_doc},
2325 {"input", builtin_input, 1, input_doc},
2326 {"intern", builtin_intern, 1, intern_doc},
2327 {"int", builtin_int, 1, int_doc},
2328 {"isinstance", builtin_isinstance, 1, isinstance_doc},
2329 {"issubclass", builtin_issubclass, 1, issubclass_doc},
2330 {"len", builtin_len, 1, len_doc},
2331 {"list", builtin_list, 1, list_doc},
2332 {"locals", builtin_locals, 1, locals_doc},
2333 {"long", builtin_long, 1, long_doc},
2334 {"map", builtin_map, 1, map_doc},
2335 {"max", builtin_max, 1, max_doc},
2336 {"min", builtin_min, 1, min_doc},
2337 {"oct", builtin_oct, 1, oct_doc},
2338 {"open", builtin_open, 1, open_doc},
2339 {"ord", builtin_ord, 1, ord_doc},
2340 {"pow", builtin_pow, 1, pow_doc},
2341 {"range", builtin_range, 1, range_doc},
2342 {"raw_input", builtin_raw_input, 1, raw_input_doc},
2343 {"reduce", builtin_reduce, 1, reduce_doc},
2344 {"reload", builtin_reload, 1, reload_doc},
2345 {"repr", builtin_repr, 1, repr_doc},
2346 {"round", builtin_round, 1, round_doc},
2347 {"setattr", builtin_setattr, 1, setattr_doc},
2348 {"slice", builtin_slice, 1, slice_doc},
2349 {"str", builtin_str, 1, str_doc},
2350 {"tuple", builtin_tuple, 1, tuple_doc},
2351 {"type", builtin_type, 1, type_doc},
Guido van Rossum09095f32000-03-10 23:00:52 +00002352 {"unicode", builtin_unicode, 1, unicode_doc},
2353 {"unichr", builtin_unichr, 1, unichr_doc},
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002354 {"vars", builtin_vars, 1, vars_doc},
2355 {"xrange", builtin_xrange, 1, xrange_doc},
Guido van Rossumc02e15c1991-12-16 13:03:00 +00002356 {NULL, NULL},
Guido van Rossum3f5da241990-12-20 15:06:42 +00002357};
2358
Guido van Rossum3f5da241990-12-20 15:06:42 +00002359/* Predefined exceptions */
2360
Guido van Rossum04748321997-09-16 18:43:15 +00002361PyObject *PyExc_Exception;
Barry Warsaw757af0e1997-08-29 22:13:51 +00002362PyObject *PyExc_StandardError;
Barry Warsaw412cdc21997-09-16 21:51:14 +00002363PyObject *PyExc_ArithmeticError;
Barry Warsaw757af0e1997-08-29 22:13:51 +00002364PyObject *PyExc_LookupError;
2365
Guido van Rossum79f25d91997-04-29 20:08:16 +00002366PyObject *PyExc_AssertionError;
2367PyObject *PyExc_AttributeError;
2368PyObject *PyExc_EOFError;
Guido van Rossumb6a7f771997-05-09 03:03:23 +00002369PyObject *PyExc_FloatingPointError;
Barry Warsawd086a1a1998-07-23 15:59:57 +00002370PyObject *PyExc_EnvironmentError;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002371PyObject *PyExc_IOError;
Barry Warsawd086a1a1998-07-23 15:59:57 +00002372PyObject *PyExc_OSError;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002373PyObject *PyExc_ImportError;
2374PyObject *PyExc_IndexError;
2375PyObject *PyExc_KeyError;
2376PyObject *PyExc_KeyboardInterrupt;
2377PyObject *PyExc_MemoryError;
2378PyObject *PyExc_NameError;
2379PyObject *PyExc_OverflowError;
2380PyObject *PyExc_RuntimeError;
Barry Warsaw344864f1998-12-01 18:52:06 +00002381PyObject *PyExc_NotImplementedError;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002382PyObject *PyExc_SyntaxError;
2383PyObject *PyExc_SystemError;
2384PyObject *PyExc_SystemExit;
Guido van Rossum87460821999-06-22 14:47:32 +00002385PyObject *PyExc_UnboundLocalError;
Guido van Rossum09095f32000-03-10 23:00:52 +00002386PyObject *PyExc_UnicodeError;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002387PyObject *PyExc_TypeError;
2388PyObject *PyExc_ValueError;
2389PyObject *PyExc_ZeroDivisionError;
Guido van Rossum65a75b02000-02-17 15:18:10 +00002390#ifdef MS_WINDOWS
2391PyObject *PyExc_WindowsError;
2392#endif
Guido van Rossum50afb7a1991-12-10 13:52:31 +00002393
Barry Warsaw757af0e1997-08-29 22:13:51 +00002394PyObject *PyExc_MemoryErrorInst;
2395
Guido van Rossum11950231999-03-25 21:16:07 +00002396static struct
Barry Warsaw757af0e1997-08-29 22:13:51 +00002397{
2398 char* name;
2399 PyObject** exc;
2400 int leaf_exc;
2401}
2402bltin_exc[] = {
Guido van Rossum04748321997-09-16 18:43:15 +00002403 {"Exception", &PyExc_Exception, 0},
Barry Warsaw757af0e1997-08-29 22:13:51 +00002404 {"StandardError", &PyExc_StandardError, 0},
Barry Warsaw412cdc21997-09-16 21:51:14 +00002405 {"ArithmeticError", &PyExc_ArithmeticError, 0},
Barry Warsaw757af0e1997-08-29 22:13:51 +00002406 {"LookupError", &PyExc_LookupError, 0},
2407 {"AssertionError", &PyExc_AssertionError, 1},
2408 {"AttributeError", &PyExc_AttributeError, 1},
2409 {"EOFError", &PyExc_EOFError, 1},
2410 {"FloatingPointError", &PyExc_FloatingPointError, 1},
Barry Warsaw78902031999-01-29 20:29:49 +00002411 {"EnvironmentError", &PyExc_EnvironmentError, 0},
Barry Warsaw757af0e1997-08-29 22:13:51 +00002412 {"IOError", &PyExc_IOError, 1},
Barry Warsawd086a1a1998-07-23 15:59:57 +00002413 {"OSError", &PyExc_OSError, 1},
Barry Warsaw757af0e1997-08-29 22:13:51 +00002414 {"ImportError", &PyExc_ImportError, 1},
2415 {"IndexError", &PyExc_IndexError, 1},
2416 {"KeyError", &PyExc_KeyError, 1},
2417 {"KeyboardInterrupt", &PyExc_KeyboardInterrupt, 1},
2418 {"MemoryError", &PyExc_MemoryError, 1},
Guido van Rossum87460821999-06-22 14:47:32 +00002419 /* Note: NameError is not a leaf in exceptions.py, but unlike
2420 the other non-leafs NameError is meant to be raised directly
2421 at times -- the leaf_exc member really seems to mean something
2422 like "this is an abstract base class" when false.
2423 */
Barry Warsaw757af0e1997-08-29 22:13:51 +00002424 {"NameError", &PyExc_NameError, 1},
2425 {"OverflowError", &PyExc_OverflowError, 1},
2426 {"RuntimeError", &PyExc_RuntimeError, 1},
Barry Warsaw344864f1998-12-01 18:52:06 +00002427 {"NotImplementedError",&PyExc_NotImplementedError,1},
Barry Warsaw757af0e1997-08-29 22:13:51 +00002428 {"SyntaxError", &PyExc_SyntaxError, 1},
2429 {"SystemError", &PyExc_SystemError, 1},
2430 {"SystemExit", &PyExc_SystemExit, 1},
Guido van Rossum87460821999-06-22 14:47:32 +00002431 {"UnboundLocalError", &PyExc_UnboundLocalError, 1},
Guido van Rossum09095f32000-03-10 23:00:52 +00002432 {"UnicodeError", &PyExc_UnicodeError, 1},
Barry Warsaw757af0e1997-08-29 22:13:51 +00002433 {"TypeError", &PyExc_TypeError, 1},
2434 {"ValueError", &PyExc_ValueError, 1},
Guido van Rossum65a75b02000-02-17 15:18:10 +00002435#ifdef MS_WINDOWS
2436 {"WindowsError", &PyExc_WindowsError, 1},
2437#endif
Barry Warsaw757af0e1997-08-29 22:13:51 +00002438 {"ZeroDivisionError", &PyExc_ZeroDivisionError, 1},
2439 {NULL, NULL}
2440};
2441
2442
Guido van Rossuma7cfca22000-05-03 22:03:46 +00002443/* Import exceptions module to extract class exceptions. On success,
2444 * return 1. On failure return 0 which signals _PyBuiltin_Init_2 to
2445 * issue a fatal error.
Barry Warsaw98b62461998-09-14 18:51:11 +00002446 */
2447static int
Barry Warsaw757af0e1997-08-29 22:13:51 +00002448init_class_exc(dict)
2449 PyObject *dict;
2450{
2451 int i;
2452 PyObject *m = PyImport_ImportModule("exceptions");
Barry Warsaw98b62461998-09-14 18:51:11 +00002453 PyObject *args = NULL;
2454 PyObject *d = NULL;
Barry Warsaw757af0e1997-08-29 22:13:51 +00002455
Barry Warsaw98b62461998-09-14 18:51:11 +00002456 /* make sure we got the module and its dictionary */
Barry Warsaw757af0e1997-08-29 22:13:51 +00002457 if (m == NULL ||
2458 (d = PyModule_GetDict(m)) == NULL)
2459 {
Guido van Rossuma7cfca22000-05-03 22:03:46 +00002460 PySys_WriteStderr("'import exceptions' failed\n");
Barry Warsaw98b62461998-09-14 18:51:11 +00002461 goto finally;
Barry Warsaw757af0e1997-08-29 22:13:51 +00002462 }
2463 for (i = 0; bltin_exc[i].name; i++) {
2464 /* dig the exception out of the module */
2465 PyObject *exc = PyDict_GetItemString(d, bltin_exc[i].name);
Barry Warsaw98b62461998-09-14 18:51:11 +00002466 if (!exc) {
2467 PySys_WriteStderr(
2468 "Built-in exception class not found: %s. Library mismatch?\n",
2469 bltin_exc[i].name);
2470 goto finally;
2471 }
2472 /* free the old-style exception string object */
Barry Warsaw757af0e1997-08-29 22:13:51 +00002473 Py_XDECREF(*bltin_exc[i].exc);
2474
2475 /* squirrel away a pointer to the exception */
2476 Py_INCREF(exc);
2477 *bltin_exc[i].exc = exc;
2478
2479 /* and insert the name in the __builtin__ module */
Barry Warsaw98b62461998-09-14 18:51:11 +00002480 if (PyDict_SetItemString(dict, bltin_exc[i].name, exc)) {
2481 PySys_WriteStderr(
2482 "Cannot insert exception into __builtin__: %s\n",
2483 bltin_exc[i].name);
2484 goto finally;
2485 }
Barry Warsaw757af0e1997-08-29 22:13:51 +00002486 }
2487
2488 /* we need one pre-allocated instance */
2489 args = Py_BuildValue("()");
Barry Warsaw98b62461998-09-14 18:51:11 +00002490 if (!args ||
2491 !(PyExc_MemoryErrorInst =
2492 PyEval_CallObject(PyExc_MemoryError, args)))
2493 {
2494 PySys_WriteStderr("Cannot pre-allocate MemoryError instance\n");
2495 goto finally;
Barry Warsaw757af0e1997-08-29 22:13:51 +00002496 }
Barry Warsaw98b62461998-09-14 18:51:11 +00002497 Py_DECREF(args);
Barry Warsaw757af0e1997-08-29 22:13:51 +00002498
2499 /* we're done with the exceptions module */
2500 Py_DECREF(m);
Barry Warsaw98b62461998-09-14 18:51:11 +00002501 return 1;
Guido van Rossuma7cfca22000-05-03 22:03:46 +00002502
Barry Warsaw98b62461998-09-14 18:51:11 +00002503 finally:
2504 Py_XDECREF(m);
2505 Py_XDECREF(args);
2506 PyErr_Clear();
2507 return 0;
Barry Warsaw757af0e1997-08-29 22:13:51 +00002508}
2509
2510
2511static void
2512fini_instances()
2513{
2514 Py_XDECREF(PyExc_MemoryErrorInst);
2515 PyExc_MemoryErrorInst = NULL;
2516}
2517
2518
Guido van Rossum25ce5661997-08-02 03:10:38 +00002519static void
2520finierrors()
2521{
Barry Warsaw757af0e1997-08-29 22:13:51 +00002522 int i;
2523 for (i = 0; bltin_exc[i].name; i++) {
2524 PyObject *exc = *bltin_exc[i].exc;
2525 Py_XDECREF(exc);
2526 *bltin_exc[i].exc = NULL;
2527 }
Guido van Rossum25ce5661997-08-02 03:10:38 +00002528}
2529
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002530static char builtin_doc[] =
2531"Built-in functions, exceptions, and other objects.\n\
2532\n\
2533Noteworthy: None is the `nil' object; Ellipsis represents `...' in slices.";
2534
Guido van Rossum25ce5661997-08-02 03:10:38 +00002535PyObject *
Barry Warsaw757af0e1997-08-29 22:13:51 +00002536_PyBuiltin_Init_1()
Guido van Rossum25ce5661997-08-02 03:10:38 +00002537{
2538 PyObject *mod, *dict;
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002539 mod = Py_InitModule4("__builtin__", builtin_methods,
2540 builtin_doc, (PyObject *)NULL,
2541 PYTHON_API_VERSION);
Guido van Rossum25ce5661997-08-02 03:10:38 +00002542 if (mod == NULL)
2543 return NULL;
2544 dict = PyModule_GetDict(mod);
Guido van Rossum25ce5661997-08-02 03:10:38 +00002545 if (PyDict_SetItemString(dict, "None", Py_None) < 0)
2546 return NULL;
2547 if (PyDict_SetItemString(dict, "Ellipsis", Py_Ellipsis) < 0)
2548 return NULL;
2549 if (PyDict_SetItemString(dict, "__debug__",
2550 PyInt_FromLong(Py_OptimizeFlag == 0)) < 0)
2551 return NULL;
Barry Warsaw757af0e1997-08-29 22:13:51 +00002552
Guido van Rossum25ce5661997-08-02 03:10:38 +00002553 return mod;
Guido van Rossum3f5da241990-12-20 15:06:42 +00002554}
2555
2556void
Barry Warsaw757af0e1997-08-29 22:13:51 +00002557_PyBuiltin_Init_2(dict)
2558 PyObject *dict;
2559{
Barry Warsaw47eeb9b2000-05-02 19:24:06 +00002560 if (!init_class_exc(dict))
2561 /* class based exceptions could not be initialized. */
2562 Py_FatalError("Standard exceptions could not be initialized.");
Barry Warsaw757af0e1997-08-29 22:13:51 +00002563}
2564
2565
2566void
2567_PyBuiltin_Fini_1()
2568{
2569 fini_instances();
2570}
2571
2572
2573void
2574_PyBuiltin_Fini_2()
Guido van Rossum3f5da241990-12-20 15:06:42 +00002575{
Guido van Rossum25ce5661997-08-02 03:10:38 +00002576 finierrors();
Guido van Rossum3f5da241990-12-20 15:06:42 +00002577}
Guido van Rossumc6bb8f71991-07-01 18:42:41 +00002578
Guido van Rossum12d12c51993-10-26 17:58:25 +00002579
Guido van Rossume77a7571993-11-03 15:01:26 +00002580/* Helper for filter(): filter a tuple through a function */
Guido van Rossum12d12c51993-10-26 17:58:25 +00002581
Guido van Rossum79f25d91997-04-29 20:08:16 +00002582static PyObject *
Guido van Rossum12d12c51993-10-26 17:58:25 +00002583filtertuple(func, tuple)
Guido van Rossum79f25d91997-04-29 20:08:16 +00002584 PyObject *func;
2585 PyObject *tuple;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002586{
Guido van Rossum79f25d91997-04-29 20:08:16 +00002587 PyObject *result;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002588 register int i, j;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002589 int len = PyTuple_Size(tuple);
Guido van Rossum12d12c51993-10-26 17:58:25 +00002590
Guido van Rossumb7b45621995-08-04 04:07:45 +00002591 if (len == 0) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002592 Py_INCREF(tuple);
Guido van Rossumb7b45621995-08-04 04:07:45 +00002593 return tuple;
2594 }
2595
Guido van Rossum79f25d91997-04-29 20:08:16 +00002596 if ((result = PyTuple_New(len)) == NULL)
Guido van Rossum2586bf01993-11-01 16:21:44 +00002597 return NULL;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002598
Guido van Rossum12d12c51993-10-26 17:58:25 +00002599 for (i = j = 0; i < len; ++i) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002600 PyObject *item, *good;
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002601 int ok;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002602
Guido van Rossum79f25d91997-04-29 20:08:16 +00002603 if ((item = PyTuple_GetItem(tuple, i)) == NULL)
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002604 goto Fail_1;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002605 if (func == Py_None) {
2606 Py_INCREF(item);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002607 good = item;
2608 }
2609 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002610 PyObject *arg = Py_BuildValue("(O)", item);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002611 if (arg == NULL)
2612 goto Fail_1;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002613 good = PyEval_CallObject(func, arg);
2614 Py_DECREF(arg);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002615 if (good == NULL)
Guido van Rossum12d12c51993-10-26 17:58:25 +00002616 goto Fail_1;
2617 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002618 ok = PyObject_IsTrue(good);
2619 Py_DECREF(good);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002620 if (ok) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002621 Py_INCREF(item);
2622 if (PyTuple_SetItem(result, j++, item) < 0)
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002623 goto Fail_1;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002624 }
Guido van Rossum12d12c51993-10-26 17:58:25 +00002625 }
2626
Guido van Rossum79f25d91997-04-29 20:08:16 +00002627 if (_PyTuple_Resize(&result, j, 0) < 0)
Guido van Rossum12d12c51993-10-26 17:58:25 +00002628 return NULL;
2629
Guido van Rossum12d12c51993-10-26 17:58:25 +00002630 return result;
2631
Guido van Rossum12d12c51993-10-26 17:58:25 +00002632Fail_1:
Guido van Rossum79f25d91997-04-29 20:08:16 +00002633 Py_DECREF(result);
Guido van Rossum12d12c51993-10-26 17:58:25 +00002634 return NULL;
2635}
2636
2637
Guido van Rossume77a7571993-11-03 15:01:26 +00002638/* Helper for filter(): filter a string through a function */
Guido van Rossum12d12c51993-10-26 17:58:25 +00002639
Guido van Rossum79f25d91997-04-29 20:08:16 +00002640static PyObject *
Guido van Rossum12d12c51993-10-26 17:58:25 +00002641filterstring(func, strobj)
Guido van Rossum79f25d91997-04-29 20:08:16 +00002642 PyObject *func;
2643 PyObject *strobj;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002644{
Guido van Rossum79f25d91997-04-29 20:08:16 +00002645 PyObject *result;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002646 register int i, j;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002647 int len = PyString_Size(strobj);
Guido van Rossum12d12c51993-10-26 17:58:25 +00002648
Guido van Rossum79f25d91997-04-29 20:08:16 +00002649 if (func == Py_None) {
Guido van Rossum2586bf01993-11-01 16:21:44 +00002650 /* No character is ever false -- share input string */
Guido van Rossum79f25d91997-04-29 20:08:16 +00002651 Py_INCREF(strobj);
Guido van Rossum2d951851994-08-29 12:52:16 +00002652 return strobj;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002653 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002654 if ((result = PyString_FromStringAndSize(NULL, len)) == NULL)
Guido van Rossum2586bf01993-11-01 16:21:44 +00002655 return NULL;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002656
Guido van Rossum12d12c51993-10-26 17:58:25 +00002657 for (i = j = 0; i < len; ++i) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002658 PyObject *item, *arg, *good;
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002659 int ok;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002660
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002661 item = (*strobj->ob_type->tp_as_sequence->sq_item)(strobj, i);
2662 if (item == NULL)
2663 goto Fail_1;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002664 arg = Py_BuildValue("(O)", item);
2665 Py_DECREF(item);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002666 if (arg == NULL)
2667 goto Fail_1;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002668 good = PyEval_CallObject(func, arg);
2669 Py_DECREF(arg);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002670 if (good == NULL)
2671 goto Fail_1;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002672 ok = PyObject_IsTrue(good);
2673 Py_DECREF(good);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002674 if (ok)
Guido van Rossum79f25d91997-04-29 20:08:16 +00002675 PyString_AS_STRING((PyStringObject *)result)[j++] =
2676 PyString_AS_STRING((PyStringObject *)item)[0];
Guido van Rossum12d12c51993-10-26 17:58:25 +00002677 }
2678
Guido van Rossum79f25d91997-04-29 20:08:16 +00002679 if (j < len && _PyString_Resize(&result, j) < 0)
Guido van Rossum12d12c51993-10-26 17:58:25 +00002680 return NULL;
2681
Guido van Rossum12d12c51993-10-26 17:58:25 +00002682 return result;
2683
Guido van Rossum12d12c51993-10-26 17:58:25 +00002684Fail_1:
Guido van Rossum79f25d91997-04-29 20:08:16 +00002685 Py_DECREF(result);
Guido van Rossum12d12c51993-10-26 17:58:25 +00002686 return NULL;
2687}