blob: cd3db231d2e65b5c299bd4770e7648220ce5cd5b [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\
195Creates a new unicode object from the given encoded string.\n\
196encoding 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[] =
371"unichr(i) -> unicode character\n\
372\n\
373Return a unicode string of one character with ordinal i; 0 <= i < 65536.";
374
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;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001645
Guido van Rossum09095f32000-03-10 23:00:52 +00001646 if (!PyArg_ParseTuple(args, "O:ord", &obj))
Guido van Rossum3f5da241990-12-20 15:06:42 +00001647 return NULL;
Guido van Rossum09095f32000-03-10 23:00:52 +00001648
1649 if (PyString_Check(obj) && PyString_GET_SIZE(obj) == 1)
1650 ord = (long)((unsigned char)*PyString_AS_STRING(obj));
1651 else if (PyUnicode_Check(obj) && PyUnicode_GET_SIZE(obj) == 1)
1652 ord = (long)*PyUnicode_AS_UNICODE(obj);
1653 else {
1654 PyErr_SetString(PyExc_TypeError,
1655 "expected a string or unicode character");
1656 return NULL;
1657 }
1658
1659 return PyInt_FromLong(ord);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001660}
1661
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001662static char ord_doc[] =
1663"ord(c) -> integer\n\
1664\n\
Guido van Rossum09095f32000-03-10 23:00:52 +00001665Return the integer ordinal of a one character [unicode] string.";
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001666
1667
Guido van Rossum79f25d91997-04-29 20:08:16 +00001668static PyObject *
Guido van Rossum6a00cd81995-01-07 12:39:01 +00001669builtin_pow(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001670 PyObject *self;
1671 PyObject *args;
Guido van Rossum6a00cd81995-01-07 12:39:01 +00001672{
Guido van Rossum09df08a1998-05-22 00:51:39 +00001673 PyObject *v, *w, *z = Py_None;
Guido van Rossum6a00cd81995-01-07 12:39:01 +00001674
Guido van Rossum79f25d91997-04-29 20:08:16 +00001675 if (!PyArg_ParseTuple(args, "OO|O:pow", &v, &w, &z))
Guido van Rossum6a00cd81995-01-07 12:39:01 +00001676 return NULL;
Guido van Rossum09df08a1998-05-22 00:51:39 +00001677 return PyNumber_Power(v, w, z);
Guido van Rossumd4905451991-05-05 20:00:36 +00001678}
1679
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001680static char pow_doc[] =
1681"pow(x, y[, z]) -> number\n\
1682\n\
1683With two arguments, equivalent to x**y. With three arguments,\n\
1684equivalent to (x**y) % z, but may be more efficient (e.g. for longs).";
1685
1686
Guido van Rossum124eff01999-02-23 16:11:01 +00001687/* Return number of items in range/xrange (lo, hi, step). step > 0
1688 * required. Return a value < 0 if & only if the true value is too
1689 * large to fit in a signed long.
1690 */
1691static long
1692get_len_of_range(lo, hi, step)
1693 long lo;
1694 long hi;
1695 long step; /* must be > 0 */
1696{
1697 /* -------------------------------------------------------------
1698 If lo >= hi, the range is empty.
1699 Else if n values are in the range, the last one is
1700 lo + (n-1)*step, which must be <= hi-1. Rearranging,
1701 n <= (hi - lo - 1)/step + 1, so taking the floor of the RHS gives
1702 the proper value. Since lo < hi in this case, hi-lo-1 >= 0, so
1703 the RHS is non-negative and so truncation is the same as the
1704 floor. Letting M be the largest positive long, the worst case
1705 for the RHS numerator is hi=M, lo=-M-1, and then
1706 hi-lo-1 = M-(-M-1)-1 = 2*M. Therefore unsigned long has enough
1707 precision to compute the RHS exactly.
1708 ---------------------------------------------------------------*/
1709 long n = 0;
1710 if (lo < hi) {
1711 unsigned long uhi = (unsigned long)hi;
1712 unsigned long ulo = (unsigned long)lo;
1713 unsigned long diff = uhi - ulo - 1;
1714 n = (long)(diff / (unsigned long)step + 1);
1715 }
1716 return n;
1717}
1718
Guido van Rossum79f25d91997-04-29 20:08:16 +00001719static PyObject *
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001720builtin_range(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001721 PyObject *self;
1722 PyObject *args;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001723{
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001724 long ilow = 0, ihigh = 0, istep = 1;
Guido van Rossum124eff01999-02-23 16:11:01 +00001725 long bign;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001726 int i, n;
Guido van Rossum124eff01999-02-23 16:11:01 +00001727
Guido van Rossum79f25d91997-04-29 20:08:16 +00001728 PyObject *v;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001729
Guido van Rossum79f25d91997-04-29 20:08:16 +00001730 if (PyTuple_Size(args) <= 1) {
1731 if (!PyArg_ParseTuple(args,
Guido van Rossum0865dd91995-01-17 16:30:22 +00001732 "l;range() requires 1-3 int arguments",
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001733 &ihigh))
1734 return NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001735 }
1736 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001737 if (!PyArg_ParseTuple(args,
Guido van Rossum0865dd91995-01-17 16:30:22 +00001738 "ll|l;range() requires 1-3 int arguments",
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001739 &ilow, &ihigh, &istep))
Guido van Rossum3f5da241990-12-20 15:06:42 +00001740 return NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001741 }
1742 if (istep == 0) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001743 PyErr_SetString(PyExc_ValueError, "zero step for range()");
Guido van Rossum3f5da241990-12-20 15:06:42 +00001744 return NULL;
1745 }
Guido van Rossum124eff01999-02-23 16:11:01 +00001746 if (istep > 0)
1747 bign = get_len_of_range(ilow, ihigh, istep);
1748 else
1749 bign = get_len_of_range(ihigh, ilow, -istep);
1750 n = (int)bign;
1751 if (bign < 0 || (long)n != bign) {
Guido van Rossume23cde21999-01-12 05:07:47 +00001752 PyErr_SetString(PyExc_OverflowError,
Guido van Rossum124eff01999-02-23 16:11:01 +00001753 "range() has too many items");
Guido van Rossume23cde21999-01-12 05:07:47 +00001754 return NULL;
1755 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00001756 v = PyList_New(n);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001757 if (v == NULL)
1758 return NULL;
1759 for (i = 0; i < n; i++) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001760 PyObject *w = PyInt_FromLong(ilow);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001761 if (w == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001762 Py_DECREF(v);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001763 return NULL;
1764 }
Guido van Rossuma937d141998-04-24 18:22:02 +00001765 PyList_SET_ITEM(v, i, w);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001766 ilow += istep;
1767 }
1768 return v;
1769}
1770
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001771static char range_doc[] =
1772"range([start,] stop[, step]) -> list of integers\n\
1773\n\
1774Return a list containing an arithmetic progression of integers.\n\
1775range(i, j) returns [i, i+1, i+2, ..., j-1]; start (!) defaults to 0.\n\
1776When step is given, it specifies the increment (or decrement).\n\
1777For example, range(4) returns [0, 1, 2, 3]. The end point is omitted!\n\
1778These are exactly the valid indices for a list of 4 elements.";
1779
1780
Guido van Rossum79f25d91997-04-29 20:08:16 +00001781static PyObject *
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001782builtin_xrange(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001783 PyObject *self;
1784 PyObject *args;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001785{
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001786 long ilow = 0, ihigh = 0, istep = 1;
Guido van Rossum0865dd91995-01-17 16:30:22 +00001787 long n;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001788
Guido van Rossum79f25d91997-04-29 20:08:16 +00001789 if (PyTuple_Size(args) <= 1) {
1790 if (!PyArg_ParseTuple(args,
Guido van Rossum0865dd91995-01-17 16:30:22 +00001791 "l;xrange() requires 1-3 int arguments",
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001792 &ihigh))
1793 return NULL;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001794 }
1795 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001796 if (!PyArg_ParseTuple(args,
Guido van Rossum0865dd91995-01-17 16:30:22 +00001797 "ll|l;xrange() requires 1-3 int arguments",
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001798 &ilow, &ihigh, &istep))
Guido van Rossum12d12c51993-10-26 17:58:25 +00001799 return NULL;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001800 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001801 if (istep == 0) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001802 PyErr_SetString(PyExc_ValueError, "zero step for xrange()");
Guido van Rossum12d12c51993-10-26 17:58:25 +00001803 return NULL;
1804 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001805 if (istep > 0)
Guido van Rossum124eff01999-02-23 16:11:01 +00001806 n = get_len_of_range(ilow, ihigh, istep);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001807 else
Guido van Rossum124eff01999-02-23 16:11:01 +00001808 n = get_len_of_range(ihigh, ilow, -istep);
1809 if (n < 0) {
1810 PyErr_SetString(PyExc_OverflowError,
1811 "xrange() has more than sys.maxint items");
1812 return NULL;
1813 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00001814 return PyRange_New(ilow, n, istep, 1);
Guido van Rossum12d12c51993-10-26 17:58:25 +00001815}
1816
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001817static char xrange_doc[] =
1818"xrange([start,] stop[, step]) -> xrange object\n\
1819\n\
1820Like range(), but instead of returning a list, returns an object that\n\
1821generates the numbers in the range on demand. This is slightly slower\n\
1822than range() but more memory efficient.";
1823
1824
Guido van Rossum79f25d91997-04-29 20:08:16 +00001825static PyObject *
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001826builtin_raw_input(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001827 PyObject *self;
1828 PyObject *args;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001829{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001830 PyObject *v = NULL;
1831 PyObject *f;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001832
Guido van Rossum79f25d91997-04-29 20:08:16 +00001833 if (!PyArg_ParseTuple(args, "|O:[raw_]input", &v))
Guido van Rossum3165fe61992-09-25 21:59:05 +00001834 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001835 if (PyFile_AsFile(PySys_GetObject("stdin")) == stdin &&
1836 PyFile_AsFile(PySys_GetObject("stdout")) == stdout &&
Guido van Rossum53bb7ff1995-07-26 16:26:31 +00001837 isatty(fileno(stdin)) && isatty(fileno(stdout))) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001838 PyObject *po;
Guido van Rossum872537c1995-07-07 22:43:42 +00001839 char *prompt;
1840 char *s;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001841 PyObject *result;
Guido van Rossum872537c1995-07-07 22:43:42 +00001842 if (v != NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001843 po = PyObject_Str(v);
Guido van Rossum872537c1995-07-07 22:43:42 +00001844 if (po == NULL)
1845 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001846 prompt = PyString_AsString(po);
Guido van Rossumd9b52081998-06-26 18:25:38 +00001847 if (prompt == NULL)
1848 return NULL;
Guido van Rossum872537c1995-07-07 22:43:42 +00001849 }
1850 else {
1851 po = NULL;
1852 prompt = "";
1853 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00001854 s = PyOS_Readline(prompt);
1855 Py_XDECREF(po);
Guido van Rossum872537c1995-07-07 22:43:42 +00001856 if (s == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001857 PyErr_SetNone(PyExc_KeyboardInterrupt);
Guido van Rossum872537c1995-07-07 22:43:42 +00001858 return NULL;
1859 }
1860 if (*s == '\0') {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001861 PyErr_SetNone(PyExc_EOFError);
Guido van Rossum872537c1995-07-07 22:43:42 +00001862 result = NULL;
1863 }
1864 else { /* strip trailing '\n' */
Guido van Rossum79f25d91997-04-29 20:08:16 +00001865 result = PyString_FromStringAndSize(s, strlen(s)-1);
Guido van Rossum872537c1995-07-07 22:43:42 +00001866 }
1867 free(s);
1868 return result;
1869 }
Guido van Rossum90933611991-06-07 16:10:43 +00001870 if (v != NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001871 f = PySys_GetObject("stdout");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001872 if (f == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001873 PyErr_SetString(PyExc_RuntimeError, "lost sys.stdout");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001874 return NULL;
1875 }
Guido van Rossumc8b6df91997-05-23 00:06:51 +00001876 if (Py_FlushLine() != 0 ||
1877 PyFile_WriteObject(v, f, Py_PRINT_RAW) != 0)
Guido van Rossum90933611991-06-07 16:10:43 +00001878 return NULL;
1879 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00001880 f = PySys_GetObject("stdin");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001881 if (f == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001882 PyErr_SetString(PyExc_RuntimeError, "lost sys.stdin");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001883 return NULL;
1884 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00001885 return PyFile_GetLine(f, -1);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001886}
1887
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001888static char raw_input_doc[] =
1889"raw_input([prompt]) -> string\n\
1890\n\
1891Read a string from standard input. The trailing newline is stripped.\n\
1892If the user hits EOF (Unix: Ctl-D, Windows: Ctl-Z+Return), raise EOFError.\n\
1893On Unix, GNU readline is used if enabled. The prompt string, if given,\n\
1894is printed without a trailing newline before reading.";
1895
1896
Guido van Rossum79f25d91997-04-29 20:08:16 +00001897static PyObject *
Guido van Rossum12d12c51993-10-26 17:58:25 +00001898builtin_reduce(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001899 PyObject *self;
1900 PyObject *args;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001901{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001902 PyObject *seq, *func, *result = NULL;
1903 PySequenceMethods *sqf;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001904 register int i;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001905
Guido van Rossum79f25d91997-04-29 20:08:16 +00001906 if (!PyArg_ParseTuple(args, "OO|O:reduce", &func, &seq, &result))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001907 return NULL;
1908 if (result != NULL)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001909 Py_INCREF(result);
Guido van Rossum12d12c51993-10-26 17:58:25 +00001910
Guido van Rossum09df08a1998-05-22 00:51:39 +00001911 sqf = seq->ob_type->tp_as_sequence;
1912 if (sqf == NULL || sqf->sq_item == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001913 PyErr_SetString(PyExc_TypeError,
Guido van Rossum12d12c51993-10-26 17:58:25 +00001914 "2nd argument to reduce() must be a sequence object");
1915 return NULL;
1916 }
1917
Guido van Rossum79f25d91997-04-29 20:08:16 +00001918 if ((args = PyTuple_New(2)) == NULL)
Guido van Rossum2d951851994-08-29 12:52:16 +00001919 goto Fail;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001920
Guido van Rossum2d951851994-08-29 12:52:16 +00001921 for (i = 0; ; ++i) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001922 PyObject *op2;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001923
1924 if (args->ob_refcnt > 1) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001925 Py_DECREF(args);
1926 if ((args = PyTuple_New(2)) == NULL)
Guido van Rossum2d951851994-08-29 12:52:16 +00001927 goto Fail;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001928 }
1929
Guido van Rossum2d951851994-08-29 12:52:16 +00001930 if ((op2 = (*sqf->sq_item)(seq, i)) == NULL) {
Barry Warsawcde8b1b1997-08-22 21:14:38 +00001931 if (PyErr_ExceptionMatches(PyExc_IndexError)) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001932 PyErr_Clear();
Guido van Rossum2d951851994-08-29 12:52:16 +00001933 break;
1934 }
1935 goto Fail;
1936 }
Guido van Rossum12d12c51993-10-26 17:58:25 +00001937
Guido van Rossum2d951851994-08-29 12:52:16 +00001938 if (result == NULL)
1939 result = op2;
1940 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001941 PyTuple_SetItem(args, 0, result);
1942 PyTuple_SetItem(args, 1, op2);
1943 if ((result = PyEval_CallObject(func, args)) == NULL)
Guido van Rossum2d951851994-08-29 12:52:16 +00001944 goto Fail;
1945 }
Guido van Rossum12d12c51993-10-26 17:58:25 +00001946 }
1947
Guido van Rossum79f25d91997-04-29 20:08:16 +00001948 Py_DECREF(args);
Guido van Rossum12d12c51993-10-26 17:58:25 +00001949
Guido van Rossum2d951851994-08-29 12:52:16 +00001950 if (result == NULL)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001951 PyErr_SetString(PyExc_TypeError,
Guido van Rossum2d951851994-08-29 12:52:16 +00001952 "reduce of empty sequence with no initial value");
1953
Guido van Rossum12d12c51993-10-26 17:58:25 +00001954 return result;
1955
Guido van Rossum2d951851994-08-29 12:52:16 +00001956Fail:
Guido van Rossum79f25d91997-04-29 20:08:16 +00001957 Py_XDECREF(args);
1958 Py_XDECREF(result);
Guido van Rossum12d12c51993-10-26 17:58:25 +00001959 return NULL;
1960}
1961
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001962static char reduce_doc[] =
1963"reduce(function, sequence[, initial]) -> value\n\
1964\n\
1965Apply a function of two arguments cumulatively to the items of a sequence,\n\
1966from left to right, so as to reduce the sequence to a single value.\n\
1967For example, reduce(lambda x, y: x+y, [1, 2, 3, 4, 5]) calculates\n\
1968((((1+2)+3)+4)+5). If initial is present, it is placed before the items\n\
1969of the sequence in the calculation, and serves as a default when the\n\
1970sequence is empty.";
1971
1972
Guido van Rossum79f25d91997-04-29 20:08:16 +00001973static PyObject *
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001974builtin_reload(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001975 PyObject *self;
1976 PyObject *args;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001977{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001978 PyObject *v;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001979
Guido van Rossum79f25d91997-04-29 20:08:16 +00001980 if (!PyArg_ParseTuple(args, "O:reload", &v))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001981 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001982 return PyImport_ReloadModule(v);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001983}
1984
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001985static char reload_doc[] =
1986"reload(module) -> module\n\
1987\n\
1988Reload the module. The module must have been successfully imported before.";
1989
1990
Guido van Rossum79f25d91997-04-29 20:08:16 +00001991static PyObject *
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001992builtin_repr(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001993 PyObject *self;
1994 PyObject *args;
Guido van Rossumc89705d1992-11-26 08:54:07 +00001995{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001996 PyObject *v;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001997
Guido van Rossum79f25d91997-04-29 20:08:16 +00001998 if (!PyArg_ParseTuple(args, "O:repr", &v))
Guido van Rossumc89705d1992-11-26 08:54:07 +00001999 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002000 return PyObject_Repr(v);
Guido van Rossumc89705d1992-11-26 08:54:07 +00002001}
2002
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002003static char repr_doc[] =
2004"repr(object) -> string\n\
2005\n\
2006Return the canonical string representation of the object.\n\
2007For most object types, eval(repr(object)) == object.";
2008
2009
Guido van Rossum79f25d91997-04-29 20:08:16 +00002010static PyObject *
Guido van Rossum9e51f9b1993-02-12 16:29:05 +00002011builtin_round(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00002012 PyObject *self;
2013 PyObject *args;
Guido van Rossum9e51f9b1993-02-12 16:29:05 +00002014{
Guido van Rossum9e51f9b1993-02-12 16:29:05 +00002015 double x;
2016 double f;
2017 int ndigits = 0;
Guido van Rossum9e51f9b1993-02-12 16:29:05 +00002018 int i;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002019
Guido van Rossum79f25d91997-04-29 20:08:16 +00002020 if (!PyArg_ParseTuple(args, "d|i:round", &x, &ndigits))
Guido van Rossum9e51f9b1993-02-12 16:29:05 +00002021 return NULL;
Guido van Rossum9e51f9b1993-02-12 16:29:05 +00002022 f = 1.0;
Guido van Rossum1e162d31998-05-09 14:42:25 +00002023 i = abs(ndigits);
2024 while (--i >= 0)
Guido van Rossum9e51f9b1993-02-12 16:29:05 +00002025 f = f*10.0;
Guido van Rossum1e162d31998-05-09 14:42:25 +00002026 if (ndigits < 0)
2027 x /= f;
Guido van Rossum9e51f9b1993-02-12 16:29:05 +00002028 else
Guido van Rossum1e162d31998-05-09 14:42:25 +00002029 x *= f;
2030 if (x >= 0.0)
2031 x = floor(x + 0.5);
2032 else
2033 x = ceil(x - 0.5);
2034 if (ndigits < 0)
2035 x *= f;
2036 else
2037 x /= f;
2038 return PyFloat_FromDouble(x);
Guido van Rossum9e51f9b1993-02-12 16:29:05 +00002039}
2040
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002041static char round_doc[] =
2042"round(number[, ndigits]) -> floating point number\n\
2043\n\
2044Round a number to a given precision in decimal digits (default 0 digits).\n\
2045This always returns a floating point number. Precision may be negative.";
2046
2047
Guido van Rossum79f25d91997-04-29 20:08:16 +00002048static PyObject *
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002049builtin_str(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00002050 PyObject *self;
2051 PyObject *args;
Guido van Rossumc89705d1992-11-26 08:54:07 +00002052{
Guido van Rossum79f25d91997-04-29 20:08:16 +00002053 PyObject *v;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002054
Guido van Rossum79f25d91997-04-29 20:08:16 +00002055 if (!PyArg_ParseTuple(args, "O:str", &v))
Guido van Rossumc89705d1992-11-26 08:54:07 +00002056 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002057 return PyObject_Str(v);
Guido van Rossumc89705d1992-11-26 08:54:07 +00002058}
2059
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002060static char str_doc[] =
2061"str(object) -> string\n\
2062\n\
2063Return a nice string representation of the object.\n\
2064If the argument is a string, the return value is the same object.";
2065
2066
Guido van Rossum79f25d91997-04-29 20:08:16 +00002067static PyObject *
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002068builtin_tuple(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00002069 PyObject *self;
2070 PyObject *args;
Guido van Rossumcae027b1994-08-29 12:53:11 +00002071{
Guido van Rossum79f25d91997-04-29 20:08:16 +00002072 PyObject *v;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002073
Guido van Rossum79f25d91997-04-29 20:08:16 +00002074 if (!PyArg_ParseTuple(args, "O:tuple", &v))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002075 return NULL;
Guido van Rossum09df08a1998-05-22 00:51:39 +00002076 return PySequence_Tuple(v);
Guido van Rossumcae027b1994-08-29 12:53:11 +00002077}
2078
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002079static char tuple_doc[] =
2080"tuple(sequence) -> list\n\
2081\n\
2082Return a tuple whose items are the same as those of the argument sequence.\n\
2083If the argument is a tuple, the return value is the same object.";
2084
2085
Guido van Rossum79f25d91997-04-29 20:08:16 +00002086static PyObject *
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002087builtin_type(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00002088 PyObject *self;
2089 PyObject *args;
Guido van Rossum3f5da241990-12-20 15:06:42 +00002090{
Guido van Rossum79f25d91997-04-29 20:08:16 +00002091 PyObject *v;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002092
Guido van Rossum79f25d91997-04-29 20:08:16 +00002093 if (!PyArg_ParseTuple(args, "O:type", &v))
Guido van Rossum3f5da241990-12-20 15:06:42 +00002094 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002095 v = (PyObject *)v->ob_type;
2096 Py_INCREF(v);
Guido van Rossum3f5da241990-12-20 15:06:42 +00002097 return v;
2098}
2099
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002100static char type_doc[] =
2101"type(object) -> type object\n\
2102\n\
2103Return the type of the object.";
2104
2105
Guido van Rossum79f25d91997-04-29 20:08:16 +00002106static PyObject *
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002107builtin_vars(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00002108 PyObject *self;
2109 PyObject *args;
Guido van Rossum2d951851994-08-29 12:52:16 +00002110{
Guido van Rossum79f25d91997-04-29 20:08:16 +00002111 PyObject *v = NULL;
2112 PyObject *d;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002113
Guido van Rossum79f25d91997-04-29 20:08:16 +00002114 if (!PyArg_ParseTuple(args, "|O:vars", &v))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002115 return NULL;
Guido van Rossum2d951851994-08-29 12:52:16 +00002116 if (v == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002117 d = PyEval_GetLocals();
Guido van Rossum53bb7ff1995-07-26 16:26:31 +00002118 if (d == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002119 if (!PyErr_Occurred())
2120 PyErr_SetString(PyExc_SystemError,
2121 "no locals!?");
Guido van Rossum53bb7ff1995-07-26 16:26:31 +00002122 }
2123 else
Guido van Rossum79f25d91997-04-29 20:08:16 +00002124 Py_INCREF(d);
Guido van Rossum2d951851994-08-29 12:52:16 +00002125 }
2126 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002127 d = PyObject_GetAttrString(v, "__dict__");
Guido van Rossum2d951851994-08-29 12:52:16 +00002128 if (d == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002129 PyErr_SetString(PyExc_TypeError,
Guido van Rossum2d951851994-08-29 12:52:16 +00002130 "vars() argument must have __dict__ attribute");
2131 return NULL;
2132 }
2133 }
2134 return d;
2135}
2136
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002137static char vars_doc[] =
2138"vars([object]) -> dictionary\n\
2139\n\
2140Without arguments, equivalent to locals().\n\
2141With an argument, equivalent to object.__dict__.";
2142
Guido van Rossum668213d1999-06-16 17:28:37 +00002143static int
2144abstract_issubclass(derived, cls, err, first)
2145 PyObject *derived;
2146 PyObject *cls;
2147 char *err;
2148 int first;
2149{
2150 static PyObject *__bases__ = NULL;
2151 PyObject *bases;
Guido van Rossum7f851861999-06-17 19:12:39 +00002152 int i, n;
Guido van Rossum668213d1999-06-16 17:28:37 +00002153 int r = 0;
2154
2155 if (__bases__ == NULL) {
2156 __bases__ = PyString_FromString("__bases__");
2157 if (__bases__ == NULL)
2158 return -1;
2159 }
2160
2161 if (first) {
2162 bases = PyObject_GetAttr(cls, __bases__);
2163 if (bases == NULL || !PyTuple_Check(bases)) {
2164 Py_XDECREF(bases);
2165 PyErr_SetString(PyExc_TypeError, err);
2166 return -1;
2167 }
2168 Py_DECREF(bases);
2169 }
2170
2171 if (derived == cls)
2172 return 1;
2173
2174 bases = PyObject_GetAttr(derived, __bases__);
2175 if (bases == NULL || !PyTuple_Check(bases)) {
2176 Py_XDECREF(bases);
2177 PyErr_SetString(PyExc_TypeError, err);
2178 return -1;
2179 }
2180
2181 n = PyTuple_GET_SIZE(bases);
2182 for (i = 0; i < n; i++) {
2183 r = abstract_issubclass(PyTuple_GET_ITEM(bases, i),
2184 cls, err, 0);
2185 if (r != 0)
2186 break;
2187 }
2188
2189 Py_DECREF(bases);
2190
2191 return r;
2192}
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002193
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002194static PyObject *
2195builtin_isinstance(self, args)
2196 PyObject *self;
2197 PyObject *args;
2198{
2199 PyObject *inst;
2200 PyObject *cls;
Guido van Rossum668213d1999-06-16 17:28:37 +00002201 PyObject *icls;
2202 static PyObject *__class__ = NULL;
2203 int retval = 0;
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002204
Guido van Rossum43713e52000-02-29 13:59:29 +00002205 if (!PyArg_ParseTuple(args, "OO:isinstance", &inst, &cls))
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002206 return NULL;
Guido van Rossumf5dd9141997-12-02 19:11:45 +00002207
Guido van Rossum668213d1999-06-16 17:28:37 +00002208 if (PyClass_Check(cls)) {
2209 if (PyInstance_Check(inst)) {
Guido van Rossumf5dd9141997-12-02 19:11:45 +00002210 PyObject *inclass =
2211 (PyObject*)((PyInstanceObject*)inst)->in_class;
2212 retval = PyClass_IsSubclass(inclass, cls);
2213 }
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002214 }
Guido van Rossum668213d1999-06-16 17:28:37 +00002215 else if (PyType_Check(cls)) {
2216 retval = ((PyObject *)(inst->ob_type) == cls);
2217 }
2218 else if (!PyInstance_Check(inst)) {
2219 if (__class__ == NULL) {
2220 __class__ = PyString_FromString("__class__");
2221 if (__class__ == NULL)
2222 return NULL;
2223 }
2224 icls = PyObject_GetAttr(inst, __class__);
2225 if (icls != NULL) {
2226 retval = abstract_issubclass(
2227 icls, cls,
2228 "second argument must be a class",
2229 1);
2230 Py_DECREF(icls);
2231 if (retval < 0)
2232 return NULL;
2233 }
2234 else {
2235 PyErr_SetString(PyExc_TypeError,
2236 "second argument must be a class");
2237 return NULL;
2238 }
2239 }
2240 else {
2241 PyErr_SetString(PyExc_TypeError,
2242 "second argument must be a class");
2243 return NULL;
2244 }
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002245 return PyInt_FromLong(retval);
2246}
2247
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002248static char isinstance_doc[] =
2249"isinstance(object, class-or-type) -> Boolean\n\
2250\n\
2251Return whether an object is an instance of a class or of a subclass thereof.\n\
2252With a type as second argument, return whether that is the object's type.";
2253
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002254
2255static PyObject *
2256builtin_issubclass(self, args)
2257 PyObject *self;
2258 PyObject *args;
2259{
2260 PyObject *derived;
2261 PyObject *cls;
2262 int retval;
2263
Guido van Rossum43713e52000-02-29 13:59:29 +00002264 if (!PyArg_ParseTuple(args, "OO:issubclass", &derived, &cls))
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002265 return NULL;
Guido van Rossum668213d1999-06-16 17:28:37 +00002266
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002267 if (!PyClass_Check(derived) || !PyClass_Check(cls)) {
Guido van Rossum668213d1999-06-16 17:28:37 +00002268 retval = abstract_issubclass(
2269 derived, cls, "arguments must be classes", 1);
2270 if (retval < 0)
2271 return NULL;
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002272 }
Guido van Rossum668213d1999-06-16 17:28:37 +00002273 else {
2274 /* shortcut */
2275 if (!(retval = (derived == cls)))
2276 retval = PyClass_IsSubclass(derived, cls);
2277 }
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002278
2279 return PyInt_FromLong(retval);
2280}
2281
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002282static char issubclass_doc[] =
2283"issubclass(C, B) -> Boolean\n\
2284\n\
2285Return whether class C is a subclass (i.e., a derived class) of class B.";
2286
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002287
Guido van Rossum79f25d91997-04-29 20:08:16 +00002288static PyMethodDef builtin_methods[] = {
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002289 {"__import__", builtin___import__, 1, import_doc},
2290 {"abs", builtin_abs, 1, abs_doc},
2291 {"apply", builtin_apply, 1, apply_doc},
Guido van Rossum0daf0221999-03-19 19:07:19 +00002292 {"buffer", builtin_buffer, 1, buffer_doc},
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002293 {"callable", builtin_callable, 1, callable_doc},
2294 {"chr", builtin_chr, 1, chr_doc},
2295 {"cmp", builtin_cmp, 1, cmp_doc},
2296 {"coerce", builtin_coerce, 1, coerce_doc},
2297 {"compile", builtin_compile, 1, compile_doc},
Guido van Rossum8a5c5d21996-01-12 01:09:56 +00002298#ifndef WITHOUT_COMPLEX
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002299 {"complex", builtin_complex, 1, complex_doc},
Guido van Rossum8a5c5d21996-01-12 01:09:56 +00002300#endif
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002301 {"delattr", builtin_delattr, 1, delattr_doc},
2302 {"dir", builtin_dir, 1, dir_doc},
2303 {"divmod", builtin_divmod, 1, divmod_doc},
2304 {"eval", builtin_eval, 1, eval_doc},
2305 {"execfile", builtin_execfile, 1, execfile_doc},
2306 {"filter", builtin_filter, 1, filter_doc},
2307 {"float", builtin_float, 1, float_doc},
2308 {"getattr", builtin_getattr, 1, getattr_doc},
2309 {"globals", builtin_globals, 1, globals_doc},
2310 {"hasattr", builtin_hasattr, 1, hasattr_doc},
2311 {"hash", builtin_hash, 1, hash_doc},
2312 {"hex", builtin_hex, 1, hex_doc},
2313 {"id", builtin_id, 1, id_doc},
2314 {"input", builtin_input, 1, input_doc},
2315 {"intern", builtin_intern, 1, intern_doc},
2316 {"int", builtin_int, 1, int_doc},
2317 {"isinstance", builtin_isinstance, 1, isinstance_doc},
2318 {"issubclass", builtin_issubclass, 1, issubclass_doc},
2319 {"len", builtin_len, 1, len_doc},
2320 {"list", builtin_list, 1, list_doc},
2321 {"locals", builtin_locals, 1, locals_doc},
2322 {"long", builtin_long, 1, long_doc},
2323 {"map", builtin_map, 1, map_doc},
2324 {"max", builtin_max, 1, max_doc},
2325 {"min", builtin_min, 1, min_doc},
2326 {"oct", builtin_oct, 1, oct_doc},
2327 {"open", builtin_open, 1, open_doc},
2328 {"ord", builtin_ord, 1, ord_doc},
2329 {"pow", builtin_pow, 1, pow_doc},
2330 {"range", builtin_range, 1, range_doc},
2331 {"raw_input", builtin_raw_input, 1, raw_input_doc},
2332 {"reduce", builtin_reduce, 1, reduce_doc},
2333 {"reload", builtin_reload, 1, reload_doc},
2334 {"repr", builtin_repr, 1, repr_doc},
2335 {"round", builtin_round, 1, round_doc},
2336 {"setattr", builtin_setattr, 1, setattr_doc},
2337 {"slice", builtin_slice, 1, slice_doc},
2338 {"str", builtin_str, 1, str_doc},
2339 {"tuple", builtin_tuple, 1, tuple_doc},
2340 {"type", builtin_type, 1, type_doc},
Guido van Rossum09095f32000-03-10 23:00:52 +00002341 {"unicode", builtin_unicode, 1, unicode_doc},
2342 {"unichr", builtin_unichr, 1, unichr_doc},
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002343 {"vars", builtin_vars, 1, vars_doc},
2344 {"xrange", builtin_xrange, 1, xrange_doc},
Guido van Rossumc02e15c1991-12-16 13:03:00 +00002345 {NULL, NULL},
Guido van Rossum3f5da241990-12-20 15:06:42 +00002346};
2347
Guido van Rossum3f5da241990-12-20 15:06:42 +00002348/* Predefined exceptions */
2349
Guido van Rossum04748321997-09-16 18:43:15 +00002350PyObject *PyExc_Exception;
Barry Warsaw757af0e1997-08-29 22:13:51 +00002351PyObject *PyExc_StandardError;
Barry Warsaw412cdc21997-09-16 21:51:14 +00002352PyObject *PyExc_ArithmeticError;
Barry Warsaw757af0e1997-08-29 22:13:51 +00002353PyObject *PyExc_LookupError;
2354
Guido van Rossum79f25d91997-04-29 20:08:16 +00002355PyObject *PyExc_AssertionError;
2356PyObject *PyExc_AttributeError;
2357PyObject *PyExc_EOFError;
Guido van Rossumb6a7f771997-05-09 03:03:23 +00002358PyObject *PyExc_FloatingPointError;
Barry Warsawd086a1a1998-07-23 15:59:57 +00002359PyObject *PyExc_EnvironmentError;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002360PyObject *PyExc_IOError;
Barry Warsawd086a1a1998-07-23 15:59:57 +00002361PyObject *PyExc_OSError;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002362PyObject *PyExc_ImportError;
2363PyObject *PyExc_IndexError;
2364PyObject *PyExc_KeyError;
2365PyObject *PyExc_KeyboardInterrupt;
2366PyObject *PyExc_MemoryError;
2367PyObject *PyExc_NameError;
2368PyObject *PyExc_OverflowError;
2369PyObject *PyExc_RuntimeError;
Barry Warsaw344864f1998-12-01 18:52:06 +00002370PyObject *PyExc_NotImplementedError;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002371PyObject *PyExc_SyntaxError;
2372PyObject *PyExc_SystemError;
2373PyObject *PyExc_SystemExit;
Guido van Rossum87460821999-06-22 14:47:32 +00002374PyObject *PyExc_UnboundLocalError;
Guido van Rossum09095f32000-03-10 23:00:52 +00002375PyObject *PyExc_UnicodeError;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002376PyObject *PyExc_TypeError;
2377PyObject *PyExc_ValueError;
2378PyObject *PyExc_ZeroDivisionError;
Guido van Rossum65a75b02000-02-17 15:18:10 +00002379#ifdef MS_WINDOWS
2380PyObject *PyExc_WindowsError;
2381#endif
Guido van Rossum50afb7a1991-12-10 13:52:31 +00002382
Barry Warsaw757af0e1997-08-29 22:13:51 +00002383PyObject *PyExc_MemoryErrorInst;
2384
Guido van Rossum11950231999-03-25 21:16:07 +00002385static struct
Barry Warsaw757af0e1997-08-29 22:13:51 +00002386{
2387 char* name;
2388 PyObject** exc;
2389 int leaf_exc;
2390}
2391bltin_exc[] = {
Guido van Rossum04748321997-09-16 18:43:15 +00002392 {"Exception", &PyExc_Exception, 0},
Barry Warsaw757af0e1997-08-29 22:13:51 +00002393 {"StandardError", &PyExc_StandardError, 0},
Barry Warsaw412cdc21997-09-16 21:51:14 +00002394 {"ArithmeticError", &PyExc_ArithmeticError, 0},
Barry Warsaw757af0e1997-08-29 22:13:51 +00002395 {"LookupError", &PyExc_LookupError, 0},
2396 {"AssertionError", &PyExc_AssertionError, 1},
2397 {"AttributeError", &PyExc_AttributeError, 1},
2398 {"EOFError", &PyExc_EOFError, 1},
2399 {"FloatingPointError", &PyExc_FloatingPointError, 1},
Barry Warsaw78902031999-01-29 20:29:49 +00002400 {"EnvironmentError", &PyExc_EnvironmentError, 0},
Barry Warsaw757af0e1997-08-29 22:13:51 +00002401 {"IOError", &PyExc_IOError, 1},
Barry Warsawd086a1a1998-07-23 15:59:57 +00002402 {"OSError", &PyExc_OSError, 1},
Barry Warsaw757af0e1997-08-29 22:13:51 +00002403 {"ImportError", &PyExc_ImportError, 1},
2404 {"IndexError", &PyExc_IndexError, 1},
2405 {"KeyError", &PyExc_KeyError, 1},
2406 {"KeyboardInterrupt", &PyExc_KeyboardInterrupt, 1},
2407 {"MemoryError", &PyExc_MemoryError, 1},
Guido van Rossum87460821999-06-22 14:47:32 +00002408 /* Note: NameError is not a leaf in exceptions.py, but unlike
2409 the other non-leafs NameError is meant to be raised directly
2410 at times -- the leaf_exc member really seems to mean something
2411 like "this is an abstract base class" when false.
2412 */
Barry Warsaw757af0e1997-08-29 22:13:51 +00002413 {"NameError", &PyExc_NameError, 1},
2414 {"OverflowError", &PyExc_OverflowError, 1},
2415 {"RuntimeError", &PyExc_RuntimeError, 1},
Barry Warsaw344864f1998-12-01 18:52:06 +00002416 {"NotImplementedError",&PyExc_NotImplementedError,1},
Barry Warsaw757af0e1997-08-29 22:13:51 +00002417 {"SyntaxError", &PyExc_SyntaxError, 1},
2418 {"SystemError", &PyExc_SystemError, 1},
2419 {"SystemExit", &PyExc_SystemExit, 1},
Guido van Rossum87460821999-06-22 14:47:32 +00002420 {"UnboundLocalError", &PyExc_UnboundLocalError, 1},
Guido van Rossum09095f32000-03-10 23:00:52 +00002421 {"UnicodeError", &PyExc_UnicodeError, 1},
Barry Warsaw757af0e1997-08-29 22:13:51 +00002422 {"TypeError", &PyExc_TypeError, 1},
2423 {"ValueError", &PyExc_ValueError, 1},
Guido van Rossum65a75b02000-02-17 15:18:10 +00002424#ifdef MS_WINDOWS
2425 {"WindowsError", &PyExc_WindowsError, 1},
2426#endif
Barry Warsaw757af0e1997-08-29 22:13:51 +00002427 {"ZeroDivisionError", &PyExc_ZeroDivisionError, 1},
2428 {NULL, NULL}
2429};
2430
2431
Barry Warsaw98b62461998-09-14 18:51:11 +00002432/* import exceptions module to extract class exceptions. on success,
2433 * return 1. on failure return 0 which signals _PyBuiltin_Init_2 to fall
2434 * back to using old-style string based exceptions.
2435 */
2436static int
Barry Warsaw757af0e1997-08-29 22:13:51 +00002437init_class_exc(dict)
2438 PyObject *dict;
2439{
2440 int i;
2441 PyObject *m = PyImport_ImportModule("exceptions");
Barry Warsaw98b62461998-09-14 18:51:11 +00002442 PyObject *args = NULL;
2443 PyObject *d = NULL;
Barry Warsaw757af0e1997-08-29 22:13:51 +00002444
Barry Warsaw98b62461998-09-14 18:51:11 +00002445 /* make sure we got the module and its dictionary */
Barry Warsaw757af0e1997-08-29 22:13:51 +00002446 if (m == NULL ||
2447 (d = PyModule_GetDict(m)) == NULL)
2448 {
Barry Warsaw98b62461998-09-14 18:51:11 +00002449 PySys_WriteStderr("'import exceptions' failed; ");
Barry Warsaw757af0e1997-08-29 22:13:51 +00002450 if (Py_VerboseFlag) {
Barry Warsaw98b62461998-09-14 18:51:11 +00002451 PySys_WriteStderr("traceback:\n");
Barry Warsaw757af0e1997-08-29 22:13:51 +00002452 PyErr_Print();
2453 }
2454 else {
Barry Warsaw98b62461998-09-14 18:51:11 +00002455 PySys_WriteStderr("use -v for traceback\n");
Barry Warsaw757af0e1997-08-29 22:13:51 +00002456 }
Barry Warsaw98b62461998-09-14 18:51:11 +00002457 goto finally;
Barry Warsaw757af0e1997-08-29 22:13:51 +00002458 }
2459 for (i = 0; bltin_exc[i].name; i++) {
2460 /* dig the exception out of the module */
2461 PyObject *exc = PyDict_GetItemString(d, bltin_exc[i].name);
Barry Warsaw98b62461998-09-14 18:51:11 +00002462 if (!exc) {
2463 PySys_WriteStderr(
2464 "Built-in exception class not found: %s. Library mismatch?\n",
2465 bltin_exc[i].name);
2466 goto finally;
2467 }
2468 /* free the old-style exception string object */
Barry Warsaw757af0e1997-08-29 22:13:51 +00002469 Py_XDECREF(*bltin_exc[i].exc);
2470
2471 /* squirrel away a pointer to the exception */
2472 Py_INCREF(exc);
2473 *bltin_exc[i].exc = exc;
2474
2475 /* and insert the name in the __builtin__ module */
Barry Warsaw98b62461998-09-14 18:51:11 +00002476 if (PyDict_SetItemString(dict, bltin_exc[i].name, exc)) {
2477 PySys_WriteStderr(
2478 "Cannot insert exception into __builtin__: %s\n",
2479 bltin_exc[i].name);
2480 goto finally;
2481 }
Barry Warsaw757af0e1997-08-29 22:13:51 +00002482 }
2483
2484 /* we need one pre-allocated instance */
2485 args = Py_BuildValue("()");
Barry Warsaw98b62461998-09-14 18:51:11 +00002486 if (!args ||
2487 !(PyExc_MemoryErrorInst =
2488 PyEval_CallObject(PyExc_MemoryError, args)))
2489 {
2490 PySys_WriteStderr("Cannot pre-allocate MemoryError instance\n");
2491 goto finally;
Barry Warsaw757af0e1997-08-29 22:13:51 +00002492 }
Barry Warsaw98b62461998-09-14 18:51:11 +00002493 Py_DECREF(args);
Barry Warsaw757af0e1997-08-29 22:13:51 +00002494
2495 /* we're done with the exceptions module */
2496 Py_DECREF(m);
2497
Barry Warsaw98b62461998-09-14 18:51:11 +00002498 if (PyErr_Occurred()) {
2499 PySys_WriteStderr("Cannot initialize standard class exceptions; ");
2500 if (Py_VerboseFlag) {
2501 PySys_WriteStderr("traceback:\n");
2502 PyErr_Print();
2503 }
2504 else
2505 PySys_WriteStderr("use -v for traceback\n");
2506 goto finally;
2507 }
2508 return 1;
2509 finally:
2510 Py_XDECREF(m);
2511 Py_XDECREF(args);
2512 PyErr_Clear();
2513 return 0;
Barry Warsaw757af0e1997-08-29 22:13:51 +00002514}
2515
2516
2517static void
2518fini_instances()
2519{
2520 Py_XDECREF(PyExc_MemoryErrorInst);
2521 PyExc_MemoryErrorInst = NULL;
2522}
2523
2524
Guido van Rossum79f25d91997-04-29 20:08:16 +00002525static PyObject *
Guido van Rossum25ce5661997-08-02 03:10:38 +00002526newstdexception(dict, name)
2527 PyObject *dict;
Guido van Rossumfb905c31991-12-16 15:42:38 +00002528 char *name;
Guido van Rossum3f5da241990-12-20 15:06:42 +00002529{
Guido van Rossum79f25d91997-04-29 20:08:16 +00002530 PyObject *v = PyString_FromString(name);
Guido van Rossum25ce5661997-08-02 03:10:38 +00002531 if (v == NULL || PyDict_SetItemString(dict, name, v) != 0)
Barry Warsaw98b62461998-09-14 18:51:11 +00002532 Py_FatalError("Cannot create string-based exceptions");
Guido van Rossum3f5da241990-12-20 15:06:42 +00002533 return v;
2534}
2535
2536static void
Guido van Rossum25ce5661997-08-02 03:10:38 +00002537initerrors(dict)
2538 PyObject *dict;
Guido van Rossum3f5da241990-12-20 15:06:42 +00002539{
Barry Warsaw72b715d1999-02-24 00:35:43 +00002540 int i, j;
Barry Warsaw757af0e1997-08-29 22:13:51 +00002541 int exccnt = 0;
2542 for (i = 0; bltin_exc[i].name; i++, exccnt++) {
Barry Warsaw98b62461998-09-14 18:51:11 +00002543 Py_XDECREF(*bltin_exc[i].exc);
Barry Warsaw757af0e1997-08-29 22:13:51 +00002544 if (bltin_exc[i].leaf_exc)
2545 *bltin_exc[i].exc =
2546 newstdexception(dict, bltin_exc[i].name);
2547 }
2548
Barry Warsawd086a1a1998-07-23 15:59:57 +00002549 /* This is kind of bogus because we special case the some of the
2550 * new exceptions to be nearly forward compatible. But this means
2551 * we hard code knowledge about exceptions.py into C here. I don't
2552 * have a better solution, though.
2553 */
Barry Warsaw757af0e1997-08-29 22:13:51 +00002554 PyExc_LookupError = PyTuple_New(2);
2555 Py_INCREF(PyExc_IndexError);
2556 PyTuple_SET_ITEM(PyExc_LookupError, 0, PyExc_IndexError);
2557 Py_INCREF(PyExc_KeyError);
2558 PyTuple_SET_ITEM(PyExc_LookupError, 1, PyExc_KeyError);
2559 PyDict_SetItemString(dict, "LookupError", PyExc_LookupError);
2560
Barry Warsaw412cdc21997-09-16 21:51:14 +00002561 PyExc_ArithmeticError = PyTuple_New(3);
Barry Warsaw757af0e1997-08-29 22:13:51 +00002562 Py_INCREF(PyExc_OverflowError);
Barry Warsaw412cdc21997-09-16 21:51:14 +00002563 PyTuple_SET_ITEM(PyExc_ArithmeticError, 0, PyExc_OverflowError);
Barry Warsaw757af0e1997-08-29 22:13:51 +00002564 Py_INCREF(PyExc_ZeroDivisionError);
Barry Warsaw412cdc21997-09-16 21:51:14 +00002565 PyTuple_SET_ITEM(PyExc_ArithmeticError, 1, PyExc_ZeroDivisionError);
Barry Warsaw757af0e1997-08-29 22:13:51 +00002566 Py_INCREF(PyExc_FloatingPointError);
Barry Warsaw412cdc21997-09-16 21:51:14 +00002567 PyTuple_SET_ITEM(PyExc_ArithmeticError, 2, PyExc_FloatingPointError);
2568 PyDict_SetItemString(dict, "ArithmeticError", PyExc_ArithmeticError);
Barry Warsaw757af0e1997-08-29 22:13:51 +00002569
Barry Warsawd086a1a1998-07-23 15:59:57 +00002570 PyExc_EnvironmentError = PyTuple_New(2);
2571 Py_INCREF(PyExc_IOError);
2572 PyTuple_SET_ITEM(PyExc_EnvironmentError, 0, PyExc_IOError);
2573 Py_INCREF(PyExc_OSError);
2574 PyTuple_SET_ITEM(PyExc_EnvironmentError, 1, PyExc_OSError);
2575 PyDict_SetItemString(dict, "EnvironmentError", PyExc_EnvironmentError);
2576
Guido van Rossum87460821999-06-22 14:47:32 +00002577 /* Make UnboundLocalError an alias for NameError */
2578 Py_INCREF(PyExc_NameError);
2579 Py_DECREF(PyExc_UnboundLocalError);
2580 PyExc_UnboundLocalError = PyExc_NameError;
2581 if (PyDict_SetItemString(dict, "UnboundLocalError",
2582 PyExc_NameError) != 0)
2583 Py_FatalError("Cannot create string-based exceptions");
2584
Guido van Rossum09095f32000-03-10 23:00:52 +00002585 /* Make UnicodeError an alias for ValueError */
2586 Py_INCREF(PyExc_ValueError);
2587 Py_DECREF(PyExc_UnicodeError);
2588 PyExc_UnicodeError = PyExc_ValueError;
2589 if (PyDict_SetItemString(dict, "UnicodeError",
2590 PyExc_ValueError) != 0)
2591 Py_FatalError("Cannot create string-based exceptions");
2592
Barry Warsaw72b715d1999-02-24 00:35:43 +00002593 /* missing from the StandardError tuple: Exception, StandardError,
2594 * and SystemExit
2595 */
2596 PyExc_StandardError = PyTuple_New(exccnt-3);
2597 for (i = 2, j = 0; bltin_exc[i].name; i++) {
Barry Warsaw757af0e1997-08-29 22:13:51 +00002598 PyObject *exc = *bltin_exc[i].exc;
Barry Warsaw72b715d1999-02-24 00:35:43 +00002599 /* SystemExit is not an error, but it is an exception */
2600 if (exc != PyExc_SystemExit) {
2601 Py_INCREF(exc);
2602 PyTuple_SET_ITEM(PyExc_StandardError, j++, exc);
2603 }
Barry Warsaw757af0e1997-08-29 22:13:51 +00002604 }
2605 PyDict_SetItemString(dict, "StandardError", PyExc_StandardError);
Guido van Rossum04748321997-09-16 18:43:15 +00002606
Barry Warsaw72b715d1999-02-24 00:35:43 +00002607 /* Exception is a 2-tuple */
2608 PyExc_Exception = PyTuple_New(2);
2609 Py_INCREF(PyExc_SystemExit);
2610 PyTuple_SET_ITEM(PyExc_Exception, 0, PyExc_SystemExit);
2611 Py_INCREF(PyExc_StandardError);
2612 PyTuple_SET_ITEM(PyExc_Exception, 1, PyExc_StandardError);
Guido van Rossum04748321997-09-16 18:43:15 +00002613 PyDict_SetItemString(dict, "Exception", PyExc_Exception);
Barry Warsaw757af0e1997-08-29 22:13:51 +00002614
2615 if (PyErr_Occurred())
2616 Py_FatalError("Could not initialize built-in string exceptions");
Guido van Rossum25ce5661997-08-02 03:10:38 +00002617}
2618
Barry Warsaw72b715d1999-02-24 00:35:43 +00002619
Guido van Rossum25ce5661997-08-02 03:10:38 +00002620static void
2621finierrors()
2622{
Barry Warsaw757af0e1997-08-29 22:13:51 +00002623 int i;
2624 for (i = 0; bltin_exc[i].name; i++) {
2625 PyObject *exc = *bltin_exc[i].exc;
2626 Py_XDECREF(exc);
2627 *bltin_exc[i].exc = NULL;
2628 }
Guido van Rossum25ce5661997-08-02 03:10:38 +00002629}
2630
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002631static char builtin_doc[] =
2632"Built-in functions, exceptions, and other objects.\n\
2633\n\
2634Noteworthy: None is the `nil' object; Ellipsis represents `...' in slices.";
2635
Guido van Rossum25ce5661997-08-02 03:10:38 +00002636PyObject *
Barry Warsaw757af0e1997-08-29 22:13:51 +00002637_PyBuiltin_Init_1()
Guido van Rossum25ce5661997-08-02 03:10:38 +00002638{
2639 PyObject *mod, *dict;
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002640 mod = Py_InitModule4("__builtin__", builtin_methods,
2641 builtin_doc, (PyObject *)NULL,
2642 PYTHON_API_VERSION);
Guido van Rossum25ce5661997-08-02 03:10:38 +00002643 if (mod == NULL)
2644 return NULL;
2645 dict = PyModule_GetDict(mod);
2646 initerrors(dict);
2647 if (PyDict_SetItemString(dict, "None", Py_None) < 0)
2648 return NULL;
2649 if (PyDict_SetItemString(dict, "Ellipsis", Py_Ellipsis) < 0)
2650 return NULL;
2651 if (PyDict_SetItemString(dict, "__debug__",
2652 PyInt_FromLong(Py_OptimizeFlag == 0)) < 0)
2653 return NULL;
Barry Warsaw757af0e1997-08-29 22:13:51 +00002654
Guido van Rossum25ce5661997-08-02 03:10:38 +00002655 return mod;
Guido van Rossum3f5da241990-12-20 15:06:42 +00002656}
2657
2658void
Barry Warsaw757af0e1997-08-29 22:13:51 +00002659_PyBuiltin_Init_2(dict)
2660 PyObject *dict;
2661{
2662 /* if Python was started with -X, initialize the class exceptions */
Barry Warsaw98b62461998-09-14 18:51:11 +00002663 if (Py_UseClassExceptionsFlag) {
2664 if (!init_class_exc(dict)) {
2665 /* class based exceptions could not be
2666 * initialized. Fall back to using string based
2667 * exceptions.
2668 */
2669 PySys_WriteStderr(
2670 "Warning! Falling back to string-based exceptions\n");
2671 initerrors(dict);
2672 }
2673 }
Barry Warsaw757af0e1997-08-29 22:13:51 +00002674}
2675
2676
2677void
2678_PyBuiltin_Fini_1()
2679{
2680 fini_instances();
2681}
2682
2683
2684void
2685_PyBuiltin_Fini_2()
Guido van Rossum3f5da241990-12-20 15:06:42 +00002686{
Guido van Rossum25ce5661997-08-02 03:10:38 +00002687 finierrors();
Guido van Rossum3f5da241990-12-20 15:06:42 +00002688}
Guido van Rossumc6bb8f71991-07-01 18:42:41 +00002689
Guido van Rossum12d12c51993-10-26 17:58:25 +00002690
Guido van Rossume77a7571993-11-03 15:01:26 +00002691/* Helper for filter(): filter a tuple through a function */
Guido van Rossum12d12c51993-10-26 17:58:25 +00002692
Guido van Rossum79f25d91997-04-29 20:08:16 +00002693static PyObject *
Guido van Rossum12d12c51993-10-26 17:58:25 +00002694filtertuple(func, tuple)
Guido van Rossum79f25d91997-04-29 20:08:16 +00002695 PyObject *func;
2696 PyObject *tuple;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002697{
Guido van Rossum79f25d91997-04-29 20:08:16 +00002698 PyObject *result;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002699 register int i, j;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002700 int len = PyTuple_Size(tuple);
Guido van Rossum12d12c51993-10-26 17:58:25 +00002701
Guido van Rossumb7b45621995-08-04 04:07:45 +00002702 if (len == 0) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002703 Py_INCREF(tuple);
Guido van Rossumb7b45621995-08-04 04:07:45 +00002704 return tuple;
2705 }
2706
Guido van Rossum79f25d91997-04-29 20:08:16 +00002707 if ((result = PyTuple_New(len)) == NULL)
Guido van Rossum2586bf01993-11-01 16:21:44 +00002708 return NULL;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002709
Guido van Rossum12d12c51993-10-26 17:58:25 +00002710 for (i = j = 0; i < len; ++i) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002711 PyObject *item, *good;
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002712 int ok;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002713
Guido van Rossum79f25d91997-04-29 20:08:16 +00002714 if ((item = PyTuple_GetItem(tuple, i)) == NULL)
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002715 goto Fail_1;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002716 if (func == Py_None) {
2717 Py_INCREF(item);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002718 good = item;
2719 }
2720 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002721 PyObject *arg = Py_BuildValue("(O)", item);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002722 if (arg == NULL)
2723 goto Fail_1;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002724 good = PyEval_CallObject(func, arg);
2725 Py_DECREF(arg);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002726 if (good == NULL)
Guido van Rossum12d12c51993-10-26 17:58:25 +00002727 goto Fail_1;
2728 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002729 ok = PyObject_IsTrue(good);
2730 Py_DECREF(good);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002731 if (ok) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002732 Py_INCREF(item);
2733 if (PyTuple_SetItem(result, j++, item) < 0)
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002734 goto Fail_1;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002735 }
Guido van Rossum12d12c51993-10-26 17:58:25 +00002736 }
2737
Guido van Rossum79f25d91997-04-29 20:08:16 +00002738 if (_PyTuple_Resize(&result, j, 0) < 0)
Guido van Rossum12d12c51993-10-26 17:58:25 +00002739 return NULL;
2740
Guido van Rossum12d12c51993-10-26 17:58:25 +00002741 return result;
2742
Guido van Rossum12d12c51993-10-26 17:58:25 +00002743Fail_1:
Guido van Rossum79f25d91997-04-29 20:08:16 +00002744 Py_DECREF(result);
Guido van Rossum12d12c51993-10-26 17:58:25 +00002745 return NULL;
2746}
2747
2748
Guido van Rossume77a7571993-11-03 15:01:26 +00002749/* Helper for filter(): filter a string through a function */
Guido van Rossum12d12c51993-10-26 17:58:25 +00002750
Guido van Rossum79f25d91997-04-29 20:08:16 +00002751static PyObject *
Guido van Rossum12d12c51993-10-26 17:58:25 +00002752filterstring(func, strobj)
Guido van Rossum79f25d91997-04-29 20:08:16 +00002753 PyObject *func;
2754 PyObject *strobj;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002755{
Guido van Rossum79f25d91997-04-29 20:08:16 +00002756 PyObject *result;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002757 register int i, j;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002758 int len = PyString_Size(strobj);
Guido van Rossum12d12c51993-10-26 17:58:25 +00002759
Guido van Rossum79f25d91997-04-29 20:08:16 +00002760 if (func == Py_None) {
Guido van Rossum2586bf01993-11-01 16:21:44 +00002761 /* No character is ever false -- share input string */
Guido van Rossum79f25d91997-04-29 20:08:16 +00002762 Py_INCREF(strobj);
Guido van Rossum2d951851994-08-29 12:52:16 +00002763 return strobj;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002764 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002765 if ((result = PyString_FromStringAndSize(NULL, len)) == NULL)
Guido van Rossum2586bf01993-11-01 16:21:44 +00002766 return NULL;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002767
Guido van Rossum12d12c51993-10-26 17:58:25 +00002768 for (i = j = 0; i < len; ++i) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002769 PyObject *item, *arg, *good;
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002770 int ok;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002771
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002772 item = (*strobj->ob_type->tp_as_sequence->sq_item)(strobj, i);
2773 if (item == NULL)
2774 goto Fail_1;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002775 arg = Py_BuildValue("(O)", item);
2776 Py_DECREF(item);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002777 if (arg == NULL)
2778 goto Fail_1;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002779 good = PyEval_CallObject(func, arg);
2780 Py_DECREF(arg);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002781 if (good == NULL)
2782 goto Fail_1;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002783 ok = PyObject_IsTrue(good);
2784 Py_DECREF(good);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002785 if (ok)
Guido van Rossum79f25d91997-04-29 20:08:16 +00002786 PyString_AS_STRING((PyStringObject *)result)[j++] =
2787 PyString_AS_STRING((PyStringObject *)item)[0];
Guido van Rossum12d12c51993-10-26 17:58:25 +00002788 }
2789
Guido van Rossum79f25d91997-04-29 20:08:16 +00002790 if (j < len && _PyString_Resize(&result, j) < 0)
Guido van Rossum12d12c51993-10-26 17:58:25 +00002791 return NULL;
2792
Guido van Rossum12d12c51993-10-26 17:58:25 +00002793 return result;
2794
Guido van Rossum12d12c51993-10-26 17:58:25 +00002795Fail_1:
Guido van Rossum79f25d91997-04-29 20:08:16 +00002796 Py_DECREF(result);
Guido van Rossum12d12c51993-10-26 17:58:25 +00002797 return NULL;
2798}