blob: d5dc32247fdcdbb09f805dde905549252796492c [file] [log] [blame]
Andrew M. Kuchling9bcc68c2000-12-20 15:07:34 +00001
Guido van Rossum3f5da241990-12-20 15:06:42 +00002/* Built-in functions */
3
Guido van Rossum79f25d91997-04-29 20:08:16 +00004#include "Python.h"
Guido van Rossum3f5da241990-12-20 15:06:42 +00005
6#include "node.h"
Guido van Rossum5b722181993-03-30 17:46:03 +00007#include "compile.h"
8#include "eval.h"
Guido van Rossum3f5da241990-12-20 15:06:42 +00009
Guido van Rossum6bf62da1997-04-11 20:37:35 +000010#include <ctype.h>
11
Guido van Rossum1a2c5cb1996-12-10 15:37:36 +000012#ifdef HAVE_UNISTD_H
13#include <unistd.h>
14#endif
15
Mark Hammond26cffde42001-05-14 12:17:34 +000016/* The default encoding used by the platform file system APIs
17 Can remain NULL for all platforms that don't have such a concept
18*/
Fredrik Lundh5b979352001-06-26 17:46:10 +000019#if defined(MS_WIN32) && defined(HAVE_USABLE_WCHAR_T)
Mark Hammond26cffde42001-05-14 12:17:34 +000020const char *Py_FileSystemDefaultEncoding = "mbcs";
21#else
22const char *Py_FileSystemDefaultEncoding = NULL; /* use default */
23#endif
Mark Hammondef8b6542001-05-13 08:04:26 +000024
Guido van Rossum12d12c51993-10-26 17:58:25 +000025/* Forward */
Tim Petersdbd9ba62000-07-09 03:09:57 +000026static PyObject *filterstring(PyObject *, PyObject *);
27static PyObject *filtertuple (PyObject *, PyObject *);
Guido van Rossum12d12c51993-10-26 17:58:25 +000028
Guido van Rossum79f25d91997-04-29 20:08:16 +000029static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +000030builtin___import__(PyObject *self, PyObject *args)
Guido van Rossum3f5da241990-12-20 15:06:42 +000031{
Guido van Rossum1ae940a1995-01-02 19:04:15 +000032 char *name;
Guido van Rossum79f25d91997-04-29 20:08:16 +000033 PyObject *globals = NULL;
34 PyObject *locals = NULL;
35 PyObject *fromlist = NULL;
Guido van Rossum1ae940a1995-01-02 19:04:15 +000036
Guido van Rossum79f25d91997-04-29 20:08:16 +000037 if (!PyArg_ParseTuple(args, "s|OOO:__import__",
Guido van Rossum24c13741995-02-14 09:42:43 +000038 &name, &globals, &locals, &fromlist))
Guido van Rossum1ae940a1995-01-02 19:04:15 +000039 return NULL;
Guido van Rossumaee0bad1997-09-05 07:33:22 +000040 return PyImport_ImportModuleEx(name, globals, locals, fromlist);
Guido van Rossum1ae940a1995-01-02 19:04:15 +000041}
42
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +000043static char import_doc[] =
44"__import__(name, globals, locals, fromlist) -> module\n\
45\n\
46Import a module. The globals are only used to determine the context;\n\
47they are not modified. The locals are currently unused. The fromlist\n\
48should be a list of names to emulate ``from name import ...'', or an\n\
49empty list to emulate ``import name''.\n\
50When importing a module from a package, note that __import__('A.B', ...)\n\
51returns package A when fromlist is empty, but its submodule B when\n\
52fromlist is not empty.";
53
Guido van Rossum1ae940a1995-01-02 19:04:15 +000054
Guido van Rossum79f25d91997-04-29 20:08:16 +000055static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +000056builtin_abs(PyObject *self, PyObject *v)
Guido van Rossum1ae940a1995-01-02 19:04:15 +000057{
Guido van Rossum09df08a1998-05-22 00:51:39 +000058 return PyNumber_Absolute(v);
Guido van Rossum3f5da241990-12-20 15:06:42 +000059}
60
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +000061static char abs_doc[] =
62"abs(number) -> number\n\
63\n\
64Return the absolute value of the argument.";
65
66
Guido van Rossum79f25d91997-04-29 20:08:16 +000067static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +000068builtin_apply(PyObject *self, PyObject *args)
Guido van Rossumc02e15c1991-12-16 13:03:00 +000069{
Guido van Rossum79f25d91997-04-29 20:08:16 +000070 PyObject *func, *alist = NULL, *kwdict = NULL;
Barry Warsaw968f8cb1998-10-01 15:33:12 +000071 PyObject *t = NULL, *retval = NULL;
Guido van Rossum1ae940a1995-01-02 19:04:15 +000072
Guido van Rossum79f25d91997-04-29 20:08:16 +000073 if (!PyArg_ParseTuple(args, "O|OO:apply", &func, &alist, &kwdict))
Guido van Rossumc02e15c1991-12-16 13:03:00 +000074 return NULL;
Barry Warsaw968f8cb1998-10-01 15:33:12 +000075 if (alist != NULL) {
76 if (!PyTuple_Check(alist)) {
77 if (!PySequence_Check(alist)) {
Jeremy Hyltonc862cf42001-01-19 03:25:05 +000078 PyErr_Format(PyExc_TypeError,
79 "apply() arg 2 expect sequence, found %s",
80 alist->ob_type->tp_name);
Barry Warsaw968f8cb1998-10-01 15:33:12 +000081 return NULL;
82 }
83 t = PySequence_Tuple(alist);
84 if (t == NULL)
85 return NULL;
86 alist = t;
87 }
Guido van Rossum2d951851994-08-29 12:52:16 +000088 }
Guido van Rossum79f25d91997-04-29 20:08:16 +000089 if (kwdict != NULL && !PyDict_Check(kwdict)) {
Jeremy Hyltonc862cf42001-01-19 03:25:05 +000090 PyErr_Format(PyExc_TypeError,
91 "apply() arg 3 expected dictionary, found %s",
92 kwdict->ob_type->tp_name);
Barry Warsaw968f8cb1998-10-01 15:33:12 +000093 goto finally;
Guido van Rossum681d79a1995-07-18 14:51:37 +000094 }
Barry Warsaw968f8cb1998-10-01 15:33:12 +000095 retval = PyEval_CallObjectWithKeywords(func, alist, kwdict);
96 finally:
97 Py_XDECREF(t);
98 return retval;
Guido van Rossumc02e15c1991-12-16 13:03:00 +000099}
100
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000101static char apply_doc[] =
Fred Drakef1fbc622001-01-12 17:05:05 +0000102"apply(object[, args[, kwargs]]) -> value\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000103\n\
Fred Drake7b912121999-12-23 14:16:55 +0000104Call a callable object with positional arguments taken from the tuple args,\n\
105and keyword arguments taken from the optional dictionary kwargs.\n\
106Note that classes are callable, as are instances with a __call__() method.";
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000107
108
Guido van Rossum79f25d91997-04-29 20:08:16 +0000109static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000110builtin_buffer(PyObject *self, PyObject *args)
Guido van Rossum0daf0221999-03-19 19:07:19 +0000111{
112 PyObject *ob;
113 int offset = 0;
114 int size = Py_END_OF_BUFFER;
115
116 if ( !PyArg_ParseTuple(args, "O|ii:buffer", &ob, &offset, &size) )
117 return NULL;
118 return PyBuffer_FromObject(ob, offset, size);
119}
120
121static char buffer_doc[] =
Guido van Rossum09095f32000-03-10 23:00:52 +0000122"buffer(object [, offset[, size]]) -> object\n\
Guido van Rossum0daf0221999-03-19 19:07:19 +0000123\n\
Guido van Rossumad991772001-01-12 16:03:05 +0000124Create a new buffer object which references the given object.\n\
Guido van Rossum0daf0221999-03-19 19:07:19 +0000125The buffer will reference a slice of the target object from the\n\
126start of the object (or at the specified offset). The slice will\n\
127extend to the end of the target object (or with the specified size).";
128
129
130static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000131builtin_callable(PyObject *self, PyObject *v)
Guido van Rossum2d951851994-08-29 12:52:16 +0000132{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000133 return PyInt_FromLong((long)PyCallable_Check(v));
Guido van Rossum2d951851994-08-29 12:52:16 +0000134}
135
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000136static char callable_doc[] =
137"callable(object) -> Boolean\n\
138\n\
139Return whether the object is callable (i.e., some kind of function).\n\
140Note that classes are callable, as are instances with a __call__() method.";
141
142
Guido van Rossum79f25d91997-04-29 20:08:16 +0000143static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000144builtin_filter(PyObject *self, PyObject *args)
Guido van Rossum12d12c51993-10-26 17:58:25 +0000145{
Tim Peters0e57abf2001-05-02 07:39:38 +0000146 PyObject *func, *seq, *result, *it;
147 int len; /* guess for result list size */
Tim Petersf4848da2001-05-05 00:14:56 +0000148 register int j;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000149
Guido van Rossum79f25d91997-04-29 20:08:16 +0000150 if (!PyArg_ParseTuple(args, "OO:filter", &func, &seq))
Guido van Rossum12d12c51993-10-26 17:58:25 +0000151 return NULL;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000152
Tim Peters0e57abf2001-05-02 07:39:38 +0000153 /* Strings and tuples return a result of the same type. */
154 if (PyString_Check(seq))
155 return filterstring(func, seq);
156 if (PyTuple_Check(seq))
157 return filtertuple(func, seq);
158
159 /* Get iterator. */
160 it = PyObject_GetIter(seq);
161 if (it == NULL)
162 return NULL;
163
164 /* Guess a result list size. */
165 len = -1; /* unknown */
166 if (PySequence_Check(seq) &&
167 seq->ob_type->tp_as_sequence->sq_length) {
168 len = PySequence_Size(seq);
169 if (len < 0)
170 PyErr_Clear();
Guido van Rossum12d12c51993-10-26 17:58:25 +0000171 }
Tim Peters0e57abf2001-05-02 07:39:38 +0000172 if (len < 0)
173 len = 8; /* arbitrary */
Guido van Rossum12d12c51993-10-26 17:58:25 +0000174
Tim Peters0e57abf2001-05-02 07:39:38 +0000175 /* Get a result list. */
Guido van Rossum79f25d91997-04-29 20:08:16 +0000176 if (PyList_Check(seq) && seq->ob_refcnt == 1) {
Tim Peters0e57abf2001-05-02 07:39:38 +0000177 /* Eww - can modify the list in-place. */
Guido van Rossum79f25d91997-04-29 20:08:16 +0000178 Py_INCREF(seq);
Guido van Rossum12d12c51993-10-26 17:58:25 +0000179 result = seq;
180 }
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000181 else {
Tim Peters0e57abf2001-05-02 07:39:38 +0000182 result = PyList_New(len);
183 if (result == NULL)
184 goto Fail_it;
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000185 }
Guido van Rossum12d12c51993-10-26 17:58:25 +0000186
Tim Peters0e57abf2001-05-02 07:39:38 +0000187 /* Build the result list. */
Tim Petersf4848da2001-05-05 00:14:56 +0000188 j = 0;
189 for (;;) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000190 PyObject *item, *good;
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000191 int ok;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000192
Tim Peters0e57abf2001-05-02 07:39:38 +0000193 item = PyIter_Next(it);
194 if (item == NULL) {
Tim Petersf4848da2001-05-05 00:14:56 +0000195 if (PyErr_Occurred())
196 goto Fail_result_it;
Tim Peters0e57abf2001-05-02 07:39:38 +0000197 break;
Guido van Rossum2d951851994-08-29 12:52:16 +0000198 }
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000199
Guido van Rossum79f25d91997-04-29 20:08:16 +0000200 if (func == Py_None) {
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000201 good = item;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000202 Py_INCREF(good);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000203 }
204 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000205 PyObject *arg = Py_BuildValue("(O)", item);
Tim Peters0e57abf2001-05-02 07:39:38 +0000206 if (arg == NULL) {
207 Py_DECREF(item);
208 goto Fail_result_it;
209 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000210 good = PyEval_CallObject(func, arg);
211 Py_DECREF(arg);
Guido van Rossum58b68731995-01-10 17:40:55 +0000212 if (good == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000213 Py_DECREF(item);
Tim Peters0e57abf2001-05-02 07:39:38 +0000214 goto Fail_result_it;
Guido van Rossum58b68731995-01-10 17:40:55 +0000215 }
Guido van Rossum12d12c51993-10-26 17:58:25 +0000216 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000217 ok = PyObject_IsTrue(good);
218 Py_DECREF(good);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000219 if (ok) {
Tim Peters0e57abf2001-05-02 07:39:38 +0000220 if (j < len)
221 PyList_SET_ITEM(result, j, item);
Guido van Rossum2d951851994-08-29 12:52:16 +0000222 else {
Barry Warsawfa77e091999-01-28 18:49:12 +0000223 int status = PyList_Append(result, item);
Barry Warsawfa77e091999-01-28 18:49:12 +0000224 Py_DECREF(item);
225 if (status < 0)
Tim Peters0e57abf2001-05-02 07:39:38 +0000226 goto Fail_result_it;
Guido van Rossum2d951851994-08-29 12:52:16 +0000227 }
Tim Peters0e57abf2001-05-02 07:39:38 +0000228 ++j;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000229 }
Tim Peters0e57abf2001-05-02 07:39:38 +0000230 else
231 Py_DECREF(item);
Guido van Rossum12d12c51993-10-26 17:58:25 +0000232 }
233
Guido van Rossum12d12c51993-10-26 17:58:25 +0000234
Tim Peters0e57abf2001-05-02 07:39:38 +0000235 /* Cut back result list if len is too big. */
Guido van Rossum79f25d91997-04-29 20:08:16 +0000236 if (j < len && PyList_SetSlice(result, j, len, NULL) < 0)
Tim Peters0e57abf2001-05-02 07:39:38 +0000237 goto Fail_result_it;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000238
Tim Peters3c6b1482001-05-21 08:07:05 +0000239 Py_DECREF(it);
Guido van Rossum12d12c51993-10-26 17:58:25 +0000240 return result;
241
Tim Peters0e57abf2001-05-02 07:39:38 +0000242Fail_result_it:
Guido van Rossum79f25d91997-04-29 20:08:16 +0000243 Py_DECREF(result);
Tim Peters0e57abf2001-05-02 07:39:38 +0000244Fail_it:
245 Py_DECREF(it);
Guido van Rossum12d12c51993-10-26 17:58:25 +0000246 return NULL;
247}
248
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000249static char filter_doc[] =
250"filter(function, sequence) -> list\n\
251\n\
252Return a list containing those items of sequence for which function(item)\n\
253is true. If function is None, return a list of items that are true.";
254
255
Guido van Rossum79f25d91997-04-29 20:08:16 +0000256static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000257builtin_chr(PyObject *self, PyObject *args)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000258{
259 long x;
260 char s[1];
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000261
Guido van Rossum79f25d91997-04-29 20:08:16 +0000262 if (!PyArg_ParseTuple(args, "l:chr", &x))
Guido van Rossum3f5da241990-12-20 15:06:42 +0000263 return NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000264 if (x < 0 || x >= 256) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000265 PyErr_SetString(PyExc_ValueError,
266 "chr() arg not in range(256)");
Guido van Rossum3f5da241990-12-20 15:06:42 +0000267 return NULL;
268 }
Guido van Rossum6bf62da1997-04-11 20:37:35 +0000269 s[0] = (char)x;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000270 return PyString_FromStringAndSize(s, 1);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000271}
272
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000273static char chr_doc[] =
274"chr(i) -> character\n\
275\n\
276Return a string of one character with ordinal i; 0 <= i < 256.";
277
278
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000279#ifdef Py_USING_UNICODE
Guido van Rossum79f25d91997-04-29 20:08:16 +0000280static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000281builtin_unichr(PyObject *self, PyObject *args)
Guido van Rossum09095f32000-03-10 23:00:52 +0000282{
283 long x;
Fredrik Lundh0dcf67e2001-06-26 20:01:56 +0000284 Py_UNICODE s[2];
Guido van Rossum09095f32000-03-10 23:00:52 +0000285
286 if (!PyArg_ParseTuple(args, "l:unichr", &x))
287 return NULL;
Fredrik Lundh0dcf67e2001-06-26 20:01:56 +0000288
Marc-André Lemburgae21df52001-07-26 16:29:25 +0000289#ifdef Py_UNICODE_WIDE
Fredrik Lundh0dcf67e2001-06-26 20:01:56 +0000290 if (x < 0 || x > 0x10ffff) {
Guido van Rossum09095f32000-03-10 23:00:52 +0000291 PyErr_SetString(PyExc_ValueError,
Marc-André Lemburgae21df52001-07-26 16:29:25 +0000292 "unichr() arg not in range(0x110000) "
293 "(wide Python build)");
Guido van Rossum09095f32000-03-10 23:00:52 +0000294 return NULL;
295 }
Marc-André Lemburgae21df52001-07-26 16:29:25 +0000296#else
297 if (x < 0 || x > 0xffff) {
298 PyErr_SetString(PyExc_ValueError,
299 "unichr() arg not in range(0x10000) "
300 "(narrow Python build)");
301 return NULL;
302 }
303#endif
Fredrik Lundh0dcf67e2001-06-26 20:01:56 +0000304
305 if (x <= 0xffff) {
306 /* UCS-2 character */
307 s[0] = (Py_UNICODE) x;
308 return PyUnicode_FromUnicode(s, 1);
Guido van Rossum236d8b72001-06-26 23:12:25 +0000309 }
310 else {
Fredrik Lundh8f455852001-06-27 18:59:43 +0000311#ifndef Py_UNICODE_WIDE
Fredrik Lundh0dcf67e2001-06-26 20:01:56 +0000312 /* UCS-4 character. store as two surrogate characters */
313 x -= 0x10000L;
314 s[0] = 0xD800 + (Py_UNICODE) (x >> 10);
315 s[1] = 0xDC00 + (Py_UNICODE) (x & 0x03FF);
316 return PyUnicode_FromUnicode(s, 2);
Guido van Rossum236d8b72001-06-26 23:12:25 +0000317#else
318 s[0] = (Py_UNICODE)x;
319 return PyUnicode_FromUnicode(s, 1);
Martin v. Löwis0ba70cc2001-06-26 22:22:37 +0000320#endif
Fredrik Lundh0dcf67e2001-06-26 20:01:56 +0000321 }
Guido van Rossum09095f32000-03-10 23:00:52 +0000322}
323
324static char unichr_doc[] =
Fred Drake078b24f2000-04-13 02:42:50 +0000325"unichr(i) -> Unicode character\n\
Guido van Rossum09095f32000-03-10 23:00:52 +0000326\n\
Fredrik Lundh0dcf67e2001-06-26 20:01:56 +0000327Return a Unicode string of one character with ordinal i; 0 <= i <= 0x10ffff.";
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000328#endif
Guido van Rossum09095f32000-03-10 23:00:52 +0000329
330
331static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000332builtin_cmp(PyObject *self, PyObject *args)
Guido van Rossuma9e7dc11992-10-18 18:53:57 +0000333{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000334 PyObject *a, *b;
Guido van Rossum09df08a1998-05-22 00:51:39 +0000335 int c;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000336
Guido van Rossum79f25d91997-04-29 20:08:16 +0000337 if (!PyArg_ParseTuple(args, "OO:cmp", &a, &b))
Guido van Rossuma9e7dc11992-10-18 18:53:57 +0000338 return NULL;
Guido van Rossum09df08a1998-05-22 00:51:39 +0000339 if (PyObject_Cmp(a, b, &c) < 0)
Guido van Rossumc8b6df91997-05-23 00:06:51 +0000340 return NULL;
Guido van Rossum09df08a1998-05-22 00:51:39 +0000341 return PyInt_FromLong((long)c);
Guido van Rossuma9e7dc11992-10-18 18:53:57 +0000342}
343
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000344static char cmp_doc[] =
345"cmp(x, y) -> integer\n\
346\n\
347Return negative if x<y, zero if x==y, positive if x>y.";
348
349
Guido van Rossum79f25d91997-04-29 20:08:16 +0000350static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000351builtin_coerce(PyObject *self, PyObject *args)
Guido van Rossum6a00cd81995-01-07 12:39:01 +0000352{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000353 PyObject *v, *w;
354 PyObject *res;
Guido van Rossum5524a591995-01-10 15:26:20 +0000355
Guido van Rossum79f25d91997-04-29 20:08:16 +0000356 if (!PyArg_ParseTuple(args, "OO:coerce", &v, &w))
Guido van Rossum5524a591995-01-10 15:26:20 +0000357 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000358 if (PyNumber_Coerce(&v, &w) < 0)
Guido van Rossum04691fc1992-08-12 15:35:34 +0000359 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000360 res = Py_BuildValue("(OO)", v, w);
361 Py_DECREF(v);
362 Py_DECREF(w);
Guido van Rossum04691fc1992-08-12 15:35:34 +0000363 return res;
364}
365
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000366static char coerce_doc[] =
367"coerce(x, y) -> None or (x1, y1)\n\
368\n\
369When x and y can be coerced to values of the same type, return a tuple\n\
370containing the coerced values. When they can't be coerced, return None.";
371
372
Guido van Rossum79f25d91997-04-29 20:08:16 +0000373static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000374builtin_compile(PyObject *self, PyObject *args)
Guido van Rossum5b722181993-03-30 17:46:03 +0000375{
376 char *str;
377 char *filename;
378 char *startstr;
379 int start;
Tim Peters6cd6a822001-08-17 22:11:27 +0000380 int dont_inherit = 0;
381 int supplied_flags = 0;
Tim Peters5ba58662001-07-16 02:29:45 +0000382 PyCompilerFlags cf;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000383
Tim Peters6cd6a822001-08-17 22:11:27 +0000384 if (!PyArg_ParseTuple(args, "sss|ii:compile", &str, &filename,
385 &startstr, &supplied_flags, &dont_inherit))
Guido van Rossum5b722181993-03-30 17:46:03 +0000386 return NULL;
Tim Peters6cd6a822001-08-17 22:11:27 +0000387
Guido van Rossum5b722181993-03-30 17:46:03 +0000388 if (strcmp(startstr, "exec") == 0)
Guido van Rossumb05a5c71997-05-07 17:46:13 +0000389 start = Py_file_input;
Guido van Rossum5b722181993-03-30 17:46:03 +0000390 else if (strcmp(startstr, "eval") == 0)
Guido van Rossumb05a5c71997-05-07 17:46:13 +0000391 start = Py_eval_input;
Guido van Rossum872537c1995-07-07 22:43:42 +0000392 else if (strcmp(startstr, "single") == 0)
Guido van Rossumb05a5c71997-05-07 17:46:13 +0000393 start = Py_single_input;
Guido van Rossum5b722181993-03-30 17:46:03 +0000394 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000395 PyErr_SetString(PyExc_ValueError,
Fred Drake661ea262000-10-24 19:57:45 +0000396 "compile() arg 3 must be 'exec' or 'eval' or 'single'");
Guido van Rossum5b722181993-03-30 17:46:03 +0000397 return NULL;
398 }
Tim Peters6cd6a822001-08-17 22:11:27 +0000399
400 if (supplied_flags & ~(PyCF_MASK | PyCF_MASK_OBSOLETE)) {
401 PyErr_SetString(PyExc_ValueError,
402 "compile(): unrecognised flags");
403 return NULL;
404 }
405 /* XXX Warn if (supplied_flags & PyCF_MASK_OBSOLETE) != 0? */
406
407 cf.cf_flags = supplied_flags;
408 if (!dont_inherit) {
409 PyEval_MergeCompilerFlags(&cf);
410 }
411 return Py_CompileStringFlags(str, filename, start, &cf);
Guido van Rossum5b722181993-03-30 17:46:03 +0000412}
413
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000414static char compile_doc[] =
Tim Peters6cd6a822001-08-17 22:11:27 +0000415"compile(source, filename, mode[, flags[, dont_inherit]]) -> code object\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000416\n\
417Compile the source string (a Python module, statement or expression)\n\
418into a code object that can be executed by the exec statement or eval().\n\
419The filename will be used for run-time error messages.\n\
420The mode must be 'exec' to compile a module, 'single' to compile a\n\
Tim Peters6cd6a822001-08-17 22:11:27 +0000421single (interactive) statement, or 'eval' to compile an expression.\n\
422The flags argument, if present, controls which future statements influence\n\
423the compilation of the code.\n\
424The dont_inherit argument, if non-zero, stops the compilation inheriting\n\
425the effects of any future statements in effect in the code calling\n\
426compile; if absent or zero these statements do influence the compilation,\n\
427in addition to any features explicitly specified.";
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000428
Tim Peters5d2b77c2001-09-03 05:47:38 +0000429/* Merge the __dict__ of aclass into dict, and recursively also all
430 the __dict__s of aclass's base classes. The order of merging isn't
431 defined, as it's expected that only the final set of dict keys is
432 interesting.
433 Return 0 on success, -1 on error.
434*/
435
436static int
437merge_class_dict(PyObject* dict, PyObject* aclass)
438{
439 PyObject *classdict;
440 PyObject *bases;
441
442 assert(PyDict_Check(dict));
443 /* XXX Class objects fail the PyType_Check check. Don't
444 XXX know of others. */
445 /* assert(PyType_Check(aclass)); */
446 assert(aclass);
447
448 /* Merge in the type's dict (if any). */
449 classdict = PyObject_GetAttrString(aclass, "__dict__");
450 if (classdict == NULL)
451 PyErr_Clear();
452 else {
453 int status = PyDict_Update(dict, classdict);
454 Py_DECREF(classdict);
455 if (status < 0)
456 return -1;
457 }
458
459 /* Recursively merge in the base types' (if any) dicts. */
460 bases = PyObject_GetAttrString(aclass, "__bases__");
461 if (bases != NULL) {
462 int i, n;
463 assert(PyTuple_Check(bases));
464 n = PyTuple_GET_SIZE(bases);
465 for (i = 0; i < n; i++) {
466 PyObject *base = PyTuple_GET_ITEM(bases, i);
467 if (merge_class_dict(dict, base) < 0) {
468 Py_DECREF(bases);
469 return -1;
470 }
471 }
472 Py_DECREF(bases);
473 }
474 return 0;
475}
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000476
Guido van Rossum79f25d91997-04-29 20:08:16 +0000477static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000478builtin_dir(PyObject *self, PyObject *args)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000479{
Tim Peters5d2b77c2001-09-03 05:47:38 +0000480 PyObject *arg = NULL;
481 /* Set exactly one of these non-NULL before the end. */
482 PyObject *result = NULL; /* result list */
483 PyObject *masterdict = NULL; /* result is masterdict.keys() */
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000484
Tim Peters5d2b77c2001-09-03 05:47:38 +0000485 if (!PyArg_ParseTuple(args, "|O:dir", &arg))
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000486 return NULL;
Tim Peters5d2b77c2001-09-03 05:47:38 +0000487
488 /* If no arg, return the locals. */
489 if (arg == NULL) {
490 PyObject *locals = PyEval_GetLocals();
491 if (locals == NULL)
Guido van Rossum666b17a1997-05-06 16:36:57 +0000492 goto error;
Tim Peters5d2b77c2001-09-03 05:47:38 +0000493 result = PyMapping_Keys(locals);
494 if (result == NULL)
Guido van Rossum666b17a1997-05-06 16:36:57 +0000495 goto error;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000496 }
Tim Peters5d2b77c2001-09-03 05:47:38 +0000497
498 /* Elif this is some form of module, we only want its dict. */
499 else if (PyObject_TypeCheck(arg, &PyModule_Type)) {
500 masterdict = PyObject_GetAttrString(arg, "__dict__");
501 if (masterdict == NULL)
502 goto error;
503 }
504
505 /* Elif some form of type, recurse. */
506 else if (PyType_Check(arg)) {
507 masterdict = PyDict_New();
508 if (masterdict == NULL)
509 goto error;
510 if (merge_class_dict(masterdict, arg) < 0)
511 goto error;
512 }
513
514 /* Else look at its dict, and the attrs reachable from its class. */
Guido van Rossum3f5da241990-12-20 15:06:42 +0000515 else {
Tim Peters5d2b77c2001-09-03 05:47:38 +0000516 PyObject *itsclass;
517 /* Create a dict to start with. */
518 masterdict = PyObject_GetAttrString(arg, "__dict__");
519 if (masterdict == NULL) {
Guido van Rossum795ba581996-05-23 22:49:07 +0000520 PyErr_Clear();
Tim Peters5d2b77c2001-09-03 05:47:38 +0000521 masterdict = PyDict_New();
522 if (masterdict == NULL)
Guido van Rossum666b17a1997-05-06 16:36:57 +0000523 goto error;
524 }
Tim Peters5d2b77c2001-09-03 05:47:38 +0000525 else {
526 /* The object may have returned a reference to its
527 dict, so copy it to avoid mutating it. */
528 PyObject *temp = PyDict_Copy(masterdict);
529 if (temp == NULL)
530 goto error;
531 Py_DECREF(masterdict);
532 masterdict = temp;
533 }
534 /* Merge in attrs reachable from its class. */
535 itsclass = PyObject_GetAttrString(arg, "__class__");
536 /* XXX Sometimes this is null! Like after "class C: pass",
537 C.__class__ raises AttributeError. Don't know of other
538 cases. */
539 if (itsclass == NULL)
540 PyErr_Clear();
541 else {
542 int status = merge_class_dict(masterdict, itsclass);
543 Py_DECREF(itsclass);
544 if (status < 0)
545 goto error;
Guido van Rossum795ba581996-05-23 22:49:07 +0000546 }
Guido van Rossum006bcd41991-10-24 14:54:44 +0000547 }
Tim Peters5d2b77c2001-09-03 05:47:38 +0000548
549 assert((result == NULL) ^ (masterdict == NULL));
550 if (masterdict != NULL) {
551 /* The result comes from its keys. */
552 assert(result == NULL);
553 result = PyMapping_Keys(masterdict);
554 if (result == NULL)
555 goto error;
556 }
557
558 assert(result);
559 if (PyList_Sort(result) != 0)
Guido van Rossum666b17a1997-05-06 16:36:57 +0000560 goto error;
Tim Peters5d2b77c2001-09-03 05:47:38 +0000561 else
562 goto normal_return;
563
Guido van Rossum666b17a1997-05-06 16:36:57 +0000564 error:
Tim Peters5d2b77c2001-09-03 05:47:38 +0000565 Py_XDECREF(result);
566 result = NULL;
567 /* fall through */
568 normal_return:
569 Py_XDECREF(masterdict);
570 return result;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000571}
572
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000573static char dir_doc[] =
Tim Peters5d2b77c2001-09-03 05:47:38 +0000574"dir([object]) -> list of strings\n"
575"\n"
576"Return an alphabetized list of names comprising (some of) the attributes\n"
577"of the given object, and of attributes reachable from it:\n"
578"\n"
579"No argument: the names in the current scope.\n"
580"Module object: the module attributes.\n"
581"Type object: its attributes, and recursively the attributes of its bases.\n"
582"Otherwise: its attributes, its class's attributes, and recursively the\n"
583" attributes of its class's base classes.";
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000584
Guido van Rossum79f25d91997-04-29 20:08:16 +0000585static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000586builtin_divmod(PyObject *self, PyObject *args)
Guido van Rossum6a00cd81995-01-07 12:39:01 +0000587{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000588 PyObject *v, *w;
Guido van Rossum6a00cd81995-01-07 12:39:01 +0000589
Guido van Rossum79f25d91997-04-29 20:08:16 +0000590 if (!PyArg_ParseTuple(args, "OO:divmod", &v, &w))
Guido van Rossum6a00cd81995-01-07 12:39:01 +0000591 return NULL;
Guido van Rossum09df08a1998-05-22 00:51:39 +0000592 return PyNumber_Divmod(v, w);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000593}
594
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000595static char divmod_doc[] =
596"divmod(x, y) -> (div, mod)\n\
597\n\
598Return the tuple ((x-x%y)/y, x%y). Invariant: div*y + mod == x.";
599
600
Guido van Rossum79f25d91997-04-29 20:08:16 +0000601static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000602builtin_eval(PyObject *self, PyObject *args)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000603{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000604 PyObject *cmd;
605 PyObject *globals = Py_None, *locals = Py_None;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000606 char *str;
Tim Peters9fa96be2001-08-17 23:04:59 +0000607 PyCompilerFlags cf;
Guido van Rossum590baa41993-11-30 13:40:46 +0000608
Guido van Rossum79f25d91997-04-29 20:08:16 +0000609 if (!PyArg_ParseTuple(args, "O|O!O!:eval",
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000610 &cmd,
Guido van Rossum79f25d91997-04-29 20:08:16 +0000611 &PyDict_Type, &globals,
612 &PyDict_Type, &locals))
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000613 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000614 if (globals == Py_None) {
615 globals = PyEval_GetGlobals();
616 if (locals == Py_None)
617 locals = PyEval_GetLocals();
Guido van Rossum6135a871995-01-09 17:53:26 +0000618 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000619 else if (locals == Py_None)
Guido van Rossum6135a871995-01-09 17:53:26 +0000620 locals = globals;
Tim Peters9fa96be2001-08-17 23:04:59 +0000621
Guido van Rossum79f25d91997-04-29 20:08:16 +0000622 if (PyDict_GetItemString(globals, "__builtins__") == NULL) {
623 if (PyDict_SetItemString(globals, "__builtins__",
624 PyEval_GetBuiltins()) != 0)
Guido van Rossum6135a871995-01-09 17:53:26 +0000625 return NULL;
626 }
Tim Peters9fa96be2001-08-17 23:04:59 +0000627
Jeremy Hylton15c1c4f2001-07-30 21:50:55 +0000628 if (PyCode_Check(cmd)) {
629 if (PyTuple_GET_SIZE(((PyCodeObject *)cmd)->co_freevars) > 0) {
630 PyErr_SetString(PyExc_TypeError,
631 "code object passed to eval() may not contain free variables");
632 return NULL;
633 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000634 return PyEval_EvalCode((PyCodeObject *) cmd, globals, locals);
Jeremy Hylton15c1c4f2001-07-30 21:50:55 +0000635 }
Tim Peters9fa96be2001-08-17 23:04:59 +0000636
Marc-André Lemburgd1ba4432000-09-19 21:04:18 +0000637 if (!PyString_Check(cmd) &&
638 !PyUnicode_Check(cmd)) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000639 PyErr_SetString(PyExc_TypeError,
Fred Drake661ea262000-10-24 19:57:45 +0000640 "eval() arg 1 must be a string or code object");
Guido van Rossum94390a41992-08-14 15:14:30 +0000641 return NULL;
642 }
Marc-André Lemburgd1ba4432000-09-19 21:04:18 +0000643 if (PyString_AsStringAndSize(cmd, &str, NULL))
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000644 return NULL;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000645 while (*str == ' ' || *str == '\t')
646 str++;
Tim Peters9fa96be2001-08-17 23:04:59 +0000647
648 cf.cf_flags = 0;
649 (void)PyEval_MergeCompilerFlags(&cf);
650 return PyRun_StringFlags(str, Py_eval_input, globals, locals, &cf);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000651}
652
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000653static char eval_doc[] =
654"eval(source[, globals[, locals]]) -> value\n\
655\n\
656Evaluate the source in the context of globals and locals.\n\
657The source may be a string representing a Python expression\n\
658or a code object as returned by compile().\n\
659The globals and locals are dictionaries, defaulting to the current\n\
660globals and locals. If only globals is given, locals defaults to it.";
661
662
Guido van Rossum79f25d91997-04-29 20:08:16 +0000663static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000664builtin_execfile(PyObject *self, PyObject *args)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000665{
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000666 char *filename;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000667 PyObject *globals = Py_None, *locals = Py_None;
668 PyObject *res;
Guido van Rossum0f61f8a1992-02-25 18:55:05 +0000669 FILE* fp;
Tim Peters5ba58662001-07-16 02:29:45 +0000670 PyCompilerFlags cf;
Martin v. Löwis6b3a2c42001-08-08 05:30:36 +0000671 int exists;
672 struct stat s;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000673
Guido van Rossum79f25d91997-04-29 20:08:16 +0000674 if (!PyArg_ParseTuple(args, "s|O!O!:execfile",
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000675 &filename,
Guido van Rossum79f25d91997-04-29 20:08:16 +0000676 &PyDict_Type, &globals,
677 &PyDict_Type, &locals))
Guido van Rossum0f61f8a1992-02-25 18:55:05 +0000678 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000679 if (globals == Py_None) {
680 globals = PyEval_GetGlobals();
681 if (locals == Py_None)
682 locals = PyEval_GetLocals();
Guido van Rossum6135a871995-01-09 17:53:26 +0000683 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000684 else if (locals == Py_None)
Guido van Rossum6135a871995-01-09 17:53:26 +0000685 locals = globals;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000686 if (PyDict_GetItemString(globals, "__builtins__") == NULL) {
687 if (PyDict_SetItemString(globals, "__builtins__",
688 PyEval_GetBuiltins()) != 0)
Guido van Rossum6135a871995-01-09 17:53:26 +0000689 return NULL;
690 }
Martin v. Löwis6b3a2c42001-08-08 05:30:36 +0000691
692 exists = 0;
693 /* Test for existence or directory. */
694 if (!stat(filename, &s)) {
Martin v. Löwisf9836ba2001-08-08 10:28:06 +0000695 if (S_ISDIR(s.st_mode))
Martin v. Löwis6b3a2c42001-08-08 05:30:36 +0000696 errno = EISDIR;
697 else
698 exists = 1;
699 }
700
701 if (exists) {
702 Py_BEGIN_ALLOW_THREADS
703 fp = fopen(filename, "r");
704 Py_END_ALLOW_THREADS
705
706 if (fp == NULL) {
707 exists = 0;
708 }
709 }
710
711 if (!exists) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000712 PyErr_SetFromErrno(PyExc_IOError);
Guido van Rossum0f61f8a1992-02-25 18:55:05 +0000713 return NULL;
714 }
Tim Peters5ba58662001-07-16 02:29:45 +0000715 cf.cf_flags = 0;
716 if (PyEval_MergeCompilerFlags(&cf))
Tim Peters748b8bb2001-04-28 08:20:22 +0000717 res = PyRun_FileExFlags(fp, filename, Py_file_input, globals,
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000718 locals, 1, &cf);
Tim Peters5ba58662001-07-16 02:29:45 +0000719 else
Tim Peters748b8bb2001-04-28 08:20:22 +0000720 res = PyRun_FileEx(fp, filename, Py_file_input, globals,
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000721 locals, 1);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000722 return res;
Guido van Rossum0f61f8a1992-02-25 18:55:05 +0000723}
724
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000725static char execfile_doc[] =
726"execfile(filename[, globals[, locals]])\n\
727\n\
728Read and execute a Python script from a file.\n\
729The globals and locals are dictionaries, defaulting to the current\n\
730globals and locals. If only globals is given, locals defaults to it.";
731
732
Guido van Rossum79f25d91997-04-29 20:08:16 +0000733static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000734builtin_getattr(PyObject *self, PyObject *args)
Guido van Rossum33894be1992-01-27 16:53:09 +0000735{
Guido van Rossum950ff291998-06-29 13:38:57 +0000736 PyObject *v, *result, *dflt = NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000737 PyObject *name;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000738
Marc-André Lemburg691270f2000-09-18 16:22:27 +0000739 if (!PyArg_ParseTuple(args, "OO|O:getattr", &v, &name, &dflt))
Guido van Rossum33894be1992-01-27 16:53:09 +0000740 return NULL;
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000741#ifdef Py_USING_UNICODE
Jeremy Hylton0eb11152001-07-30 22:39:31 +0000742 if (PyUnicode_Check(name)) {
743 name = _PyUnicode_AsDefaultEncodedString(name, NULL);
744 if (name == NULL)
745 return NULL;
746 }
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000747#endif
Jeremy Hylton0eb11152001-07-30 22:39:31 +0000748
749 if (!PyString_Check(name)) {
750 PyErr_SetString(PyExc_TypeError,
751 "attribute name must be string");
752 return NULL;
753 }
Guido van Rossum950ff291998-06-29 13:38:57 +0000754 result = PyObject_GetAttr(v, name);
755 if (result == NULL && dflt != NULL) {
756 PyErr_Clear();
757 Py_INCREF(dflt);
758 result = dflt;
759 }
760 return result;
Guido van Rossum9bfef441993-03-29 10:43:31 +0000761}
762
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000763static char getattr_doc[] =
Guido van Rossum950ff291998-06-29 13:38:57 +0000764"getattr(object, name[, default]) -> value\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000765\n\
Guido van Rossum950ff291998-06-29 13:38:57 +0000766Get a named attribute from an object; getattr(x, 'y') is equivalent to x.y.\n\
767When a default argument is given, it is returned when the attribute doesn't\n\
768exist; without it, an exception is raised in that case.";
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000769
770
Guido van Rossum79f25d91997-04-29 20:08:16 +0000771static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000772builtin_globals(PyObject *self)
Guido van Rossum872537c1995-07-07 22:43:42 +0000773{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000774 PyObject *d;
Guido van Rossum872537c1995-07-07 22:43:42 +0000775
Guido van Rossum79f25d91997-04-29 20:08:16 +0000776 d = PyEval_GetGlobals();
777 Py_INCREF(d);
Guido van Rossum872537c1995-07-07 22:43:42 +0000778 return d;
779}
780
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000781static char globals_doc[] =
782"globals() -> dictionary\n\
783\n\
784Return the dictionary containing the current scope's global variables.";
785
786
Guido van Rossum79f25d91997-04-29 20:08:16 +0000787static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000788builtin_hasattr(PyObject *self, PyObject *args)
Guido van Rossum9bfef441993-03-29 10:43:31 +0000789{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000790 PyObject *v;
791 PyObject *name;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000792
Marc-André Lemburg691270f2000-09-18 16:22:27 +0000793 if (!PyArg_ParseTuple(args, "OO:hasattr", &v, &name))
Guido van Rossum9bfef441993-03-29 10:43:31 +0000794 return NULL;
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000795#ifdef Py_USING_UNICODE
Jeremy Hylton302b54a2001-07-30 22:45:19 +0000796 if (PyUnicode_Check(name)) {
797 name = _PyUnicode_AsDefaultEncodedString(name, NULL);
798 if (name == NULL)
799 return NULL;
800 }
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000801#endif
Jeremy Hylton302b54a2001-07-30 22:45:19 +0000802
803 if (!PyString_Check(name)) {
804 PyErr_SetString(PyExc_TypeError,
805 "attribute name must be string");
806 return NULL;
807 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000808 v = PyObject_GetAttr(v, name);
Guido van Rossum9bfef441993-03-29 10:43:31 +0000809 if (v == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000810 PyErr_Clear();
Guido van Rossum09df08a1998-05-22 00:51:39 +0000811 Py_INCREF(Py_False);
812 return Py_False;
Guido van Rossum9bfef441993-03-29 10:43:31 +0000813 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000814 Py_DECREF(v);
Guido van Rossum09df08a1998-05-22 00:51:39 +0000815 Py_INCREF(Py_True);
816 return Py_True;
Guido van Rossum33894be1992-01-27 16:53:09 +0000817}
818
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000819static char hasattr_doc[] =
820"hasattr(object, name) -> Boolean\n\
821\n\
822Return whether the object has an attribute with the given name.\n\
823(This is done by calling getattr(object, name) and catching exceptions.)";
824
825
Guido van Rossum79f25d91997-04-29 20:08:16 +0000826static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000827builtin_id(PyObject *self, PyObject *v)
Guido van Rossum5b722181993-03-30 17:46:03 +0000828{
Guido van Rossum106f2da2000-06-28 21:12:25 +0000829 return PyLong_FromVoidPtr(v);
Guido van Rossum5b722181993-03-30 17:46:03 +0000830}
831
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000832static char id_doc[] =
833"id(object) -> integer\n\
834\n\
835Return the identity of an object. This is guaranteed to be unique among\n\
836simultaneously existing objects. (Hint: it's the object's memory address.)";
837
838
Guido van Rossum79f25d91997-04-29 20:08:16 +0000839static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000840builtin_map(PyObject *self, PyObject *args)
Guido van Rossum12d12c51993-10-26 17:58:25 +0000841{
842 typedef struct {
Tim Peters4e9afdc2001-05-03 23:54:49 +0000843 PyObject *it; /* the iterator object */
844 int saw_StopIteration; /* bool: did the iterator end? */
Guido van Rossum12d12c51993-10-26 17:58:25 +0000845 } sequence;
846
Guido van Rossum79f25d91997-04-29 20:08:16 +0000847 PyObject *func, *result;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000848 sequence *seqs = NULL, *sqp;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000849 int n, len;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000850 register int i, j;
851
Guido van Rossum79f25d91997-04-29 20:08:16 +0000852 n = PyTuple_Size(args);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000853 if (n < 2) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000854 PyErr_SetString(PyExc_TypeError,
855 "map() requires at least two args");
Guido van Rossum12d12c51993-10-26 17:58:25 +0000856 return NULL;
857 }
858
Guido van Rossum79f25d91997-04-29 20:08:16 +0000859 func = PyTuple_GetItem(args, 0);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000860 n--;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000861
Guido van Rossumfa4ac711998-07-10 17:37:30 +0000862 if (func == Py_None && n == 1) {
863 /* map(None, S) is the same as list(S). */
864 return PySequence_List(PyTuple_GetItem(args, 1));
865 }
866
Tim Peters4e9afdc2001-05-03 23:54:49 +0000867 /* Get space for sequence descriptors. Must NULL out the iterator
868 * pointers so that jumping to Fail_2 later doesn't see trash.
869 */
Guido van Rossum79f25d91997-04-29 20:08:16 +0000870 if ((seqs = PyMem_NEW(sequence, n)) == NULL) {
871 PyErr_NoMemory();
Tim Peters4e9afdc2001-05-03 23:54:49 +0000872 return NULL;
873 }
874 for (i = 0; i < n; ++i) {
875 seqs[i].it = (PyObject*)NULL;
876 seqs[i].saw_StopIteration = 0;
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000877 }
Guido van Rossum12d12c51993-10-26 17:58:25 +0000878
Tim Peters4e9afdc2001-05-03 23:54:49 +0000879 /* Do a first pass to obtain iterators for the arguments, and set len
880 * to the largest of their lengths.
Tim Peters748b8bb2001-04-28 08:20:22 +0000881 */
Tim Peters4e9afdc2001-05-03 23:54:49 +0000882 len = 0;
883 for (i = 0, sqp = seqs; i < n; ++i, ++sqp) {
884 PyObject *curseq;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000885 int curlen;
Guido van Rossumad991772001-01-12 16:03:05 +0000886
Tim Peters4e9afdc2001-05-03 23:54:49 +0000887 /* Get iterator. */
888 curseq = PyTuple_GetItem(args, i+1);
889 sqp->it = PyObject_GetIter(curseq);
890 if (sqp->it == NULL) {
Guido van Rossum12d12c51993-10-26 17:58:25 +0000891 static char errmsg[] =
Tim Peters4e9afdc2001-05-03 23:54:49 +0000892 "argument %d to map() must support iteration";
Guido van Rossum15e33a41997-04-30 19:00:27 +0000893 char errbuf[sizeof(errmsg) + 25];
Guido van Rossum12d12c51993-10-26 17:58:25 +0000894 sprintf(errbuf, errmsg, i+2);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000895 PyErr_SetString(PyExc_TypeError, errbuf);
Guido van Rossum12d12c51993-10-26 17:58:25 +0000896 goto Fail_2;
897 }
898
Tim Peters4e9afdc2001-05-03 23:54:49 +0000899 /* Update len. */
900 curlen = -1; /* unknown */
901 if (PySequence_Check(curseq) &&
902 curseq->ob_type->tp_as_sequence->sq_length) {
903 curlen = PySequence_Size(curseq);
904 if (curlen < 0)
905 PyErr_Clear();
906 }
Tim Peters748b8bb2001-04-28 08:20:22 +0000907 if (curlen < 0)
Tim Peters4e9afdc2001-05-03 23:54:49 +0000908 curlen = 8; /* arbitrary */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000909 if (curlen > len)
910 len = curlen;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000911 }
912
Tim Peters4e9afdc2001-05-03 23:54:49 +0000913 /* Get space for the result list. */
Guido van Rossum79f25d91997-04-29 20:08:16 +0000914 if ((result = (PyObject *) PyList_New(len)) == NULL)
Guido van Rossum12d12c51993-10-26 17:58:25 +0000915 goto Fail_2;
916
Tim Peters4e9afdc2001-05-03 23:54:49 +0000917 /* Iterate over the sequences until all have stopped. */
Guido van Rossum2d951851994-08-29 12:52:16 +0000918 for (i = 0; ; ++i) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000919 PyObject *alist, *item=NULL, *value;
Tim Peters4e9afdc2001-05-03 23:54:49 +0000920 int numactive = 0;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000921
Guido van Rossum79f25d91997-04-29 20:08:16 +0000922 if (func == Py_None && n == 1)
Guido van Rossum32120311995-07-10 13:52:21 +0000923 alist = NULL;
Tim Peters4e9afdc2001-05-03 23:54:49 +0000924 else if ((alist = PyTuple_New(n)) == NULL)
925 goto Fail_1;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000926
927 for (j = 0, sqp = seqs; j < n; ++j, ++sqp) {
Tim Peters4e9afdc2001-05-03 23:54:49 +0000928 if (sqp->saw_StopIteration) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000929 Py_INCREF(Py_None);
930 item = Py_None;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000931 }
Guido van Rossum12d12c51993-10-26 17:58:25 +0000932 else {
Tim Peters4e9afdc2001-05-03 23:54:49 +0000933 item = PyIter_Next(sqp->it);
934 if (item)
935 ++numactive;
936 else {
Tim Peters4e9afdc2001-05-03 23:54:49 +0000937 if (PyErr_Occurred()) {
Tim Petersf4848da2001-05-05 00:14:56 +0000938 Py_XDECREF(alist);
939 goto Fail_1;
Guido van Rossum2d951851994-08-29 12:52:16 +0000940 }
Tim Peters4e9afdc2001-05-03 23:54:49 +0000941 Py_INCREF(Py_None);
942 item = Py_None;
943 sqp->saw_StopIteration = 1;
Guido van Rossum2d951851994-08-29 12:52:16 +0000944 }
Guido van Rossum12d12c51993-10-26 17:58:25 +0000945 }
Tim Peters4e9afdc2001-05-03 23:54:49 +0000946 if (alist)
947 PyTuple_SET_ITEM(alist, j, item);
948 else
Guido van Rossum2d951851994-08-29 12:52:16 +0000949 break;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000950 }
951
Guido van Rossum32120311995-07-10 13:52:21 +0000952 if (!alist)
953 alist = item;
Guido van Rossum2d951851994-08-29 12:52:16 +0000954
Tim Peters4e9afdc2001-05-03 23:54:49 +0000955 if (numactive == 0) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000956 Py_DECREF(alist);
Guido van Rossum2d951851994-08-29 12:52:16 +0000957 break;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000958 }
Guido van Rossum2d951851994-08-29 12:52:16 +0000959
Guido van Rossum79f25d91997-04-29 20:08:16 +0000960 if (func == Py_None)
Guido van Rossum32120311995-07-10 13:52:21 +0000961 value = alist;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000962 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000963 value = PyEval_CallObject(func, alist);
964 Py_DECREF(alist);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000965 if (value == NULL)
966 goto Fail_1;
Guido van Rossum2d951851994-08-29 12:52:16 +0000967 }
968 if (i >= len) {
Barry Warsawfa77e091999-01-28 18:49:12 +0000969 int status = PyList_Append(result, value);
Barry Warsaw21332871999-01-28 04:21:35 +0000970 Py_DECREF(value);
Barry Warsawfa77e091999-01-28 18:49:12 +0000971 if (status < 0)
972 goto Fail_1;
Guido van Rossum2d951851994-08-29 12:52:16 +0000973 }
Tim Peters4e9afdc2001-05-03 23:54:49 +0000974 else if (PyList_SetItem(result, i, value) < 0)
975 goto Fail_1;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000976 }
977
Guido van Rossumfa4ac711998-07-10 17:37:30 +0000978 if (i < len && PyList_SetSlice(result, i, len, NULL) < 0)
979 goto Fail_1;
980
Tim Peters4e9afdc2001-05-03 23:54:49 +0000981 goto Succeed;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000982
Guido van Rossum12d12c51993-10-26 17:58:25 +0000983Fail_1:
Guido van Rossum79f25d91997-04-29 20:08:16 +0000984 Py_DECREF(result);
Guido van Rossum12d12c51993-10-26 17:58:25 +0000985Fail_2:
Tim Peters4e9afdc2001-05-03 23:54:49 +0000986 result = NULL;
987Succeed:
988 assert(seqs);
989 for (i = 0; i < n; ++i)
990 Py_XDECREF(seqs[i].it);
991 PyMem_DEL(seqs);
992 return result;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000993}
994
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000995static char map_doc[] =
996"map(function, sequence[, sequence, ...]) -> list\n\
997\n\
998Return a list of the results of applying the function to the items of\n\
999the argument sequence(s). If more than one sequence is given, the\n\
1000function is called with an argument list consisting of the corresponding\n\
1001item of each sequence, substituting None for missing values when not all\n\
1002sequences have the same length. If the function is None, return a list of\n\
1003the items of the sequence (or a list of tuples if more than one sequence).";
1004
1005
Guido van Rossum79f25d91997-04-29 20:08:16 +00001006static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001007builtin_setattr(PyObject *self, PyObject *args)
Guido van Rossum33894be1992-01-27 16:53:09 +00001008{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001009 PyObject *v;
1010 PyObject *name;
1011 PyObject *value;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001012
Marc-André Lemburg691270f2000-09-18 16:22:27 +00001013 if (!PyArg_ParseTuple(args, "OOO:setattr", &v, &name, &value))
Guido van Rossum33894be1992-01-27 16:53:09 +00001014 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001015 if (PyObject_SetAttr(v, name, value) != 0)
Guido van Rossum33894be1992-01-27 16:53:09 +00001016 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001017 Py_INCREF(Py_None);
1018 return Py_None;
Guido van Rossum33894be1992-01-27 16:53:09 +00001019}
1020
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001021static char setattr_doc[] =
1022"setattr(object, name, value)\n\
1023\n\
1024Set a named attribute on an object; setattr(x, 'y', v) is equivalent to\n\
1025``x.y = v''.";
1026
1027
Guido van Rossum79f25d91997-04-29 20:08:16 +00001028static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001029builtin_delattr(PyObject *self, PyObject *args)
Guido van Rossum14144fc1994-08-29 12:53:40 +00001030{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001031 PyObject *v;
1032 PyObject *name;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001033
Marc-André Lemburg691270f2000-09-18 16:22:27 +00001034 if (!PyArg_ParseTuple(args, "OO:delattr", &v, &name))
Guido van Rossum14144fc1994-08-29 12:53:40 +00001035 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001036 if (PyObject_SetAttr(v, name, (PyObject *)NULL) != 0)
Guido van Rossum14144fc1994-08-29 12:53:40 +00001037 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001038 Py_INCREF(Py_None);
1039 return Py_None;
Guido van Rossum14144fc1994-08-29 12:53:40 +00001040}
1041
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001042static char delattr_doc[] =
Guido van Rossumdf12a591998-11-23 22:13:04 +00001043"delattr(object, name)\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001044\n\
1045Delete a named attribute on an object; delattr(x, 'y') is equivalent to\n\
1046``del x.y''.";
1047
1048
Guido van Rossum79f25d91997-04-29 20:08:16 +00001049static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001050builtin_hash(PyObject *self, PyObject *v)
Guido van Rossum9bfef441993-03-29 10:43:31 +00001051{
Guido van Rossum9bfef441993-03-29 10:43:31 +00001052 long x;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001053
Guido van Rossum79f25d91997-04-29 20:08:16 +00001054 x = PyObject_Hash(v);
Guido van Rossum9bfef441993-03-29 10:43:31 +00001055 if (x == -1)
1056 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001057 return PyInt_FromLong(x);
Guido van Rossum9bfef441993-03-29 10:43:31 +00001058}
1059
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001060static char hash_doc[] =
1061"hash(object) -> integer\n\
1062\n\
1063Return a hash value for the object. Two objects with the same value have\n\
1064the same hash value. The reverse is not necessarily true, but likely.";
1065
1066
Guido van Rossum79f25d91997-04-29 20:08:16 +00001067static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001068builtin_hex(PyObject *self, PyObject *v)
Guido van Rossum006bcd41991-10-24 14:54:44 +00001069{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001070 PyNumberMethods *nb;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001071
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001072 if ((nb = v->ob_type->tp_as_number) == NULL ||
Guido van Rossum1899c2e1992-09-12 11:09:23 +00001073 nb->nb_hex == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001074 PyErr_SetString(PyExc_TypeError,
Guido van Rossum1899c2e1992-09-12 11:09:23 +00001075 "hex() argument can't be converted to hex");
1076 return NULL;
Guido van Rossum006bcd41991-10-24 14:54:44 +00001077 }
Guido van Rossum1899c2e1992-09-12 11:09:23 +00001078 return (*nb->nb_hex)(v);
Guido van Rossum006bcd41991-10-24 14:54:44 +00001079}
1080
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001081static char hex_doc[] =
1082"hex(number) -> string\n\
1083\n\
1084Return the hexadecimal representation of an integer or long integer.";
1085
1086
Tim Petersdbd9ba62000-07-09 03:09:57 +00001087static PyObject *builtin_raw_input(PyObject *, PyObject *);
Guido van Rossum3165fe61992-09-25 21:59:05 +00001088
Guido van Rossum79f25d91997-04-29 20:08:16 +00001089static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001090builtin_input(PyObject *self, PyObject *args)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001091{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001092 PyObject *line;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001093 char *str;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001094 PyObject *res;
1095 PyObject *globals, *locals;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001096
1097 line = builtin_raw_input(self, args);
Guido van Rossum3165fe61992-09-25 21:59:05 +00001098 if (line == NULL)
1099 return line;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001100 if (!PyArg_Parse(line, "s;embedded '\\0' in input line", &str))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001101 return NULL;
1102 while (*str == ' ' || *str == '\t')
1103 str++;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001104 globals = PyEval_GetGlobals();
1105 locals = PyEval_GetLocals();
1106 if (PyDict_GetItemString(globals, "__builtins__") == NULL) {
1107 if (PyDict_SetItemString(globals, "__builtins__",
1108 PyEval_GetBuiltins()) != 0)
Guido van Rossum6135a871995-01-09 17:53:26 +00001109 return NULL;
1110 }
Guido van Rossumb05a5c71997-05-07 17:46:13 +00001111 res = PyRun_String(str, Py_eval_input, globals, locals);
Guido van Rossum79f25d91997-04-29 20:08:16 +00001112 Py_DECREF(line);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001113 return res;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001114}
1115
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001116static char input_doc[] =
1117"input([prompt]) -> value\n\
1118\n\
1119Equivalent to eval(raw_input(prompt)).";
1120
1121
Guido van Rossume8811f81997-02-14 15:48:05 +00001122static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001123builtin_intern(PyObject *self, PyObject *args)
Guido van Rossume8811f81997-02-14 15:48:05 +00001124{
1125 PyObject *s;
Guido van Rossum43713e52000-02-29 13:59:29 +00001126 if (!PyArg_ParseTuple(args, "S:intern", &s))
Guido van Rossume8811f81997-02-14 15:48:05 +00001127 return NULL;
1128 Py_INCREF(s);
1129 PyString_InternInPlace(&s);
1130 return s;
1131}
1132
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001133static char intern_doc[] =
1134"intern(string) -> string\n\
1135\n\
1136``Intern'' the given string. This enters the string in the (global)\n\
1137table of interned strings whose purpose is to speed up dictionary lookups.\n\
1138Return the string itself or the previously interned string object with the\n\
1139same value.";
1140
1141
Guido van Rossum79f25d91997-04-29 20:08:16 +00001142static PyObject *
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001143builtin_iter(PyObject *self, PyObject *args)
1144{
1145 PyObject *v, *w = NULL;
1146
1147 if (!PyArg_ParseTuple(args, "O|O:iter", &v, &w))
1148 return NULL;
1149 if (w == NULL)
1150 return PyObject_GetIter(v);
1151 if (!PyCallable_Check(v)) {
1152 PyErr_SetString(PyExc_TypeError,
1153 "iter(v, w): v must be callable");
1154 return NULL;
1155 }
1156 return PyCallIter_New(v, w);
1157}
1158
1159static char iter_doc[] =
1160"iter(collection) -> iterator\n\
1161iter(callable, sentinel) -> iterator\n\
1162\n\
1163Get an iterator from an object. In the first form, the argument must\n\
1164supply its own iterator, or be a sequence.\n\
1165In the second form, the callable is called until it returns the sentinel.";
1166
1167
1168static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001169builtin_len(PyObject *self, PyObject *v)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001170{
Guido van Rossum8ea9f4d1998-06-29 22:26:50 +00001171 long res;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001172
Jeremy Hylton03657cf2000-07-12 13:05:33 +00001173 res = PyObject_Size(v);
Guido van Rossum8ea9f4d1998-06-29 22:26:50 +00001174 if (res < 0 && PyErr_Occurred())
1175 return NULL;
1176 return PyInt_FromLong(res);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001177}
1178
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001179static char len_doc[] =
1180"len(object) -> integer\n\
1181\n\
1182Return the number of items of a sequence or mapping.";
1183
1184
Guido van Rossum79f25d91997-04-29 20:08:16 +00001185static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001186builtin_slice(PyObject *self, PyObject *args)
Guido van Rossum8861b741996-07-30 16:49:37 +00001187{
Guido van Rossum09df08a1998-05-22 00:51:39 +00001188 PyObject *start, *stop, *step;
Guido van Rossum8861b741996-07-30 16:49:37 +00001189
Guido van Rossum09df08a1998-05-22 00:51:39 +00001190 start = stop = step = NULL;
Guido van Rossum8861b741996-07-30 16:49:37 +00001191
Guido van Rossum09df08a1998-05-22 00:51:39 +00001192 if (!PyArg_ParseTuple(args, "O|OO:slice", &start, &stop, &step))
1193 return NULL;
Guido van Rossum8861b741996-07-30 16:49:37 +00001194
Guido van Rossum09df08a1998-05-22 00:51:39 +00001195 /* This swapping of stop and start is to maintain similarity with
1196 range(). */
1197 if (stop == NULL) {
1198 stop = start;
1199 start = NULL;
1200 }
1201 return PySlice_New(start, stop, step);
Guido van Rossum8861b741996-07-30 16:49:37 +00001202}
1203
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001204static char slice_doc[] =
Fred Drake3d587441999-07-19 15:21:16 +00001205"slice([start,] stop[, step]) -> slice object\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001206\n\
1207Create a slice object. This is used for slicing by the Numeric extensions.";
1208
1209
Guido van Rossum79f25d91997-04-29 20:08:16 +00001210static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001211builtin_locals(PyObject *self)
Guido van Rossum872537c1995-07-07 22:43:42 +00001212{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001213 PyObject *d;
Guido van Rossum872537c1995-07-07 22:43:42 +00001214
Guido van Rossum79f25d91997-04-29 20:08:16 +00001215 d = PyEval_GetLocals();
1216 Py_INCREF(d);
Guido van Rossum872537c1995-07-07 22:43:42 +00001217 return d;
1218}
1219
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001220static char locals_doc[] =
1221"locals() -> dictionary\n\
1222\n\
1223Return the dictionary containing the current scope's local variables.";
1224
1225
Guido van Rossum79f25d91997-04-29 20:08:16 +00001226static PyObject *
Guido van Rossum53451b32001-01-17 15:47:24 +00001227min_max(PyObject *args, int op)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001228{
Tim Petersc3074532001-05-03 07:00:32 +00001229 PyObject *v, *w, *x, *it;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001230
Guido van Rossum79f25d91997-04-29 20:08:16 +00001231 if (PyTuple_Size(args) > 1)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001232 v = args;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001233 else if (!PyArg_ParseTuple(args, "O:min/max", &v))
Guido van Rossum3f5da241990-12-20 15:06:42 +00001234 return NULL;
Tim Petersc3074532001-05-03 07:00:32 +00001235
1236 it = PyObject_GetIter(v);
1237 if (it == NULL)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001238 return NULL;
Tim Petersc3074532001-05-03 07:00:32 +00001239
1240 w = NULL; /* the result */
Tim Petersf4848da2001-05-05 00:14:56 +00001241 for (;;) {
Tim Petersc3074532001-05-03 07:00:32 +00001242 x = PyIter_Next(it);
Guido van Rossum2d951851994-08-29 12:52:16 +00001243 if (x == NULL) {
Tim Petersc3074532001-05-03 07:00:32 +00001244 if (PyErr_Occurred()) {
Tim Petersf4848da2001-05-05 00:14:56 +00001245 Py_XDECREF(w);
1246 Py_DECREF(it);
1247 return NULL;
Guido van Rossum2d951851994-08-29 12:52:16 +00001248 }
Tim Petersc3074532001-05-03 07:00:32 +00001249 break;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001250 }
Tim Petersc3074532001-05-03 07:00:32 +00001251
Guido van Rossum2d951851994-08-29 12:52:16 +00001252 if (w == NULL)
1253 w = x;
1254 else {
Guido van Rossum53451b32001-01-17 15:47:24 +00001255 int cmp = PyObject_RichCompareBool(x, w, op);
1256 if (cmp > 0) {
1257 Py_DECREF(w);
1258 w = x;
1259 }
1260 else if (cmp < 0) {
Guido van Rossumc8b6df91997-05-23 00:06:51 +00001261 Py_DECREF(x);
Tim Petersc3074532001-05-03 07:00:32 +00001262 Py_DECREF(w);
1263 Py_DECREF(it);
Guido van Rossumc8b6df91997-05-23 00:06:51 +00001264 return NULL;
1265 }
Guido van Rossum2d951851994-08-29 12:52:16 +00001266 else
Guido van Rossum79f25d91997-04-29 20:08:16 +00001267 Py_DECREF(x);
Guido van Rossum2d951851994-08-29 12:52:16 +00001268 }
Guido van Rossum3f5da241990-12-20 15:06:42 +00001269 }
Guido van Rossum2d951851994-08-29 12:52:16 +00001270 if (w == NULL)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001271 PyErr_SetString(PyExc_ValueError,
Fred Drake661ea262000-10-24 19:57:45 +00001272 "min() or max() arg is an empty sequence");
Tim Petersc3074532001-05-03 07:00:32 +00001273 Py_DECREF(it);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001274 return w;
1275}
1276
Guido van Rossum79f25d91997-04-29 20:08:16 +00001277static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001278builtin_min(PyObject *self, PyObject *v)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001279{
Guido van Rossum53451b32001-01-17 15:47:24 +00001280 return min_max(v, Py_LT);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001281}
1282
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001283static char min_doc[] =
1284"min(sequence) -> value\n\
1285min(a, b, c, ...) -> value\n\
1286\n\
1287With a single sequence argument, return its smallest item.\n\
1288With two or more arguments, return the smallest argument.";
1289
1290
Guido van Rossum79f25d91997-04-29 20:08:16 +00001291static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001292builtin_max(PyObject *self, PyObject *v)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001293{
Guido van Rossum53451b32001-01-17 15:47:24 +00001294 return min_max(v, Py_GT);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001295}
1296
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001297static char max_doc[] =
1298"max(sequence) -> value\n\
1299max(a, b, c, ...) -> value\n\
1300\n\
1301With a single sequence argument, return its largest item.\n\
1302With two or more arguments, return the largest argument.";
1303
1304
Guido van Rossum79f25d91997-04-29 20:08:16 +00001305static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001306builtin_oct(PyObject *self, PyObject *v)
Guido van Rossum006bcd41991-10-24 14:54:44 +00001307{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001308 PyNumberMethods *nb;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001309
Guido van Rossum1899c2e1992-09-12 11:09:23 +00001310 if (v == NULL || (nb = v->ob_type->tp_as_number) == NULL ||
1311 nb->nb_oct == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001312 PyErr_SetString(PyExc_TypeError,
Guido van Rossum1899c2e1992-09-12 11:09:23 +00001313 "oct() argument can't be converted to oct");
1314 return NULL;
Guido van Rossum006bcd41991-10-24 14:54:44 +00001315 }
Guido van Rossum1899c2e1992-09-12 11:09:23 +00001316 return (*nb->nb_oct)(v);
Guido van Rossum006bcd41991-10-24 14:54:44 +00001317}
1318
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001319static char oct_doc[] =
1320"oct(number) -> string\n\
1321\n\
1322Return the octal representation of an integer or long integer.";
1323
1324
Guido van Rossum79f25d91997-04-29 20:08:16 +00001325static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001326builtin_open(PyObject *self, PyObject *args)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001327{
Mark Hammondef8b6542001-05-13 08:04:26 +00001328 char *name = NULL;
Guido van Rossum2d951851994-08-29 12:52:16 +00001329 char *mode = "r";
1330 int bufsize = -1;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001331 PyObject *f;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001332
Mark Hammondef8b6542001-05-13 08:04:26 +00001333 if (!PyArg_ParseTuple(args, "et|si:open", Py_FileSystemDefaultEncoding,
1334 &name, &mode, &bufsize))
Guido van Rossum3f5da241990-12-20 15:06:42 +00001335 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001336 f = PyFile_FromString(name, mode);
Mark Hammondef8b6542001-05-13 08:04:26 +00001337 PyMem_Free(name); /* free the encoded string */
Guido van Rossum2d951851994-08-29 12:52:16 +00001338 if (f != NULL)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001339 PyFile_SetBufSize(f, bufsize);
Guido van Rossum2d951851994-08-29 12:52:16 +00001340 return f;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001341}
1342
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001343static char open_doc[] =
1344"open(filename[, mode[, buffering]]) -> file object\n\
1345\n\
1346Open a file. The mode can be 'r', 'w' or 'a' for reading (default),\n\
1347writing or appending. The file will be created if it doesn't exist\n\
1348when opened for writing or appending; it will be truncated when\n\
1349opened for writing. Add a 'b' to the mode for binary files.\n\
1350Add a '+' to the mode to allow simultaneous reading and writing.\n\
1351If the buffering argument is given, 0 means unbuffered, 1 means line\n\
1352buffered, and larger numbers specify the buffer size.";
1353
1354
Guido van Rossum79f25d91997-04-29 20:08:16 +00001355static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001356builtin_ord(PyObject *self, PyObject* obj)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001357{
Guido van Rossum09095f32000-03-10 23:00:52 +00001358 long ord;
Jeremy Hylton394b54d2000-04-12 21:19:47 +00001359 int size;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001360
Jeremy Hylton394b54d2000-04-12 21:19:47 +00001361 if (PyString_Check(obj)) {
1362 size = PyString_GET_SIZE(obj);
Andrew M. Kuchling34c20cf2000-12-20 14:36:56 +00001363 if (size == 1) {
Jeremy Hylton394b54d2000-04-12 21:19:47 +00001364 ord = (long)((unsigned char)*PyString_AS_STRING(obj));
Andrew M. Kuchling34c20cf2000-12-20 14:36:56 +00001365 return PyInt_FromLong(ord);
1366 }
Martin v. Löwis339d0f72001-08-17 18:39:25 +00001367#ifdef Py_USING_UNICODE
Jeremy Hylton394b54d2000-04-12 21:19:47 +00001368 } else if (PyUnicode_Check(obj)) {
1369 size = PyUnicode_GET_SIZE(obj);
Andrew M. Kuchling34c20cf2000-12-20 14:36:56 +00001370 if (size == 1) {
Jeremy Hylton394b54d2000-04-12 21:19:47 +00001371 ord = (long)*PyUnicode_AS_UNICODE(obj);
Andrew M. Kuchling34c20cf2000-12-20 14:36:56 +00001372 return PyInt_FromLong(ord);
1373 }
Martin v. Löwis339d0f72001-08-17 18:39:25 +00001374#endif
Jeremy Hylton394b54d2000-04-12 21:19:47 +00001375 } else {
1376 PyErr_Format(PyExc_TypeError,
Andrew M. Kuchlingf07aad12000-12-23 14:11:28 +00001377 "ord() expected string of length 1, but " \
Jeremy Hylton394b54d2000-04-12 21:19:47 +00001378 "%.200s found", obj->ob_type->tp_name);
Guido van Rossum09095f32000-03-10 23:00:52 +00001379 return NULL;
1380 }
1381
Guido van Rossumad991772001-01-12 16:03:05 +00001382 PyErr_Format(PyExc_TypeError,
Andrew M. Kuchlingf07aad12000-12-23 14:11:28 +00001383 "ord() expected a character, "
1384 "but string of length %d found",
Jeremy Hylton394b54d2000-04-12 21:19:47 +00001385 size);
1386 return NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001387}
1388
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001389static char ord_doc[] =
1390"ord(c) -> integer\n\
1391\n\
Guido van Rossumad991772001-01-12 16:03:05 +00001392Return the integer ordinal of a one-character string.";
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001393
1394
Guido van Rossum79f25d91997-04-29 20:08:16 +00001395static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001396builtin_pow(PyObject *self, PyObject *args)
Guido van Rossum6a00cd81995-01-07 12:39:01 +00001397{
Guido van Rossum09df08a1998-05-22 00:51:39 +00001398 PyObject *v, *w, *z = Py_None;
Guido van Rossum6a00cd81995-01-07 12:39:01 +00001399
Guido van Rossum79f25d91997-04-29 20:08:16 +00001400 if (!PyArg_ParseTuple(args, "OO|O:pow", &v, &w, &z))
Guido van Rossum6a00cd81995-01-07 12:39:01 +00001401 return NULL;
Guido van Rossum09df08a1998-05-22 00:51:39 +00001402 return PyNumber_Power(v, w, z);
Guido van Rossumd4905451991-05-05 20:00:36 +00001403}
1404
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001405static char pow_doc[] =
1406"pow(x, y[, z]) -> number\n\
1407\n\
1408With two arguments, equivalent to x**y. With three arguments,\n\
1409equivalent to (x**y) % z, but may be more efficient (e.g. for longs).";
1410
1411
Guido van Rossum124eff01999-02-23 16:11:01 +00001412/* Return number of items in range/xrange (lo, hi, step). step > 0
1413 * required. Return a value < 0 if & only if the true value is too
1414 * large to fit in a signed long.
1415 */
1416static long
Thomas Wouterse28c2962000-07-23 22:21:32 +00001417get_len_of_range(long lo, long hi, long step)
Guido van Rossum124eff01999-02-23 16:11:01 +00001418{
1419 /* -------------------------------------------------------------
1420 If lo >= hi, the range is empty.
1421 Else if n values are in the range, the last one is
1422 lo + (n-1)*step, which must be <= hi-1. Rearranging,
1423 n <= (hi - lo - 1)/step + 1, so taking the floor of the RHS gives
1424 the proper value. Since lo < hi in this case, hi-lo-1 >= 0, so
1425 the RHS is non-negative and so truncation is the same as the
1426 floor. Letting M be the largest positive long, the worst case
1427 for the RHS numerator is hi=M, lo=-M-1, and then
1428 hi-lo-1 = M-(-M-1)-1 = 2*M. Therefore unsigned long has enough
1429 precision to compute the RHS exactly.
1430 ---------------------------------------------------------------*/
1431 long n = 0;
1432 if (lo < hi) {
1433 unsigned long uhi = (unsigned long)hi;
1434 unsigned long ulo = (unsigned long)lo;
1435 unsigned long diff = uhi - ulo - 1;
1436 n = (long)(diff / (unsigned long)step + 1);
1437 }
1438 return n;
1439}
1440
Guido van Rossum79f25d91997-04-29 20:08:16 +00001441static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001442builtin_range(PyObject *self, PyObject *args)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001443{
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001444 long ilow = 0, ihigh = 0, istep = 1;
Guido van Rossum124eff01999-02-23 16:11:01 +00001445 long bign;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001446 int i, n;
Guido van Rossum124eff01999-02-23 16:11:01 +00001447
Guido van Rossum79f25d91997-04-29 20:08:16 +00001448 PyObject *v;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001449
Guido van Rossum79f25d91997-04-29 20:08:16 +00001450 if (PyTuple_Size(args) <= 1) {
1451 if (!PyArg_ParseTuple(args,
Guido van Rossum0865dd91995-01-17 16:30:22 +00001452 "l;range() requires 1-3 int arguments",
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001453 &ihigh))
1454 return NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001455 }
1456 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001457 if (!PyArg_ParseTuple(args,
Guido van Rossum0865dd91995-01-17 16:30:22 +00001458 "ll|l;range() requires 1-3 int arguments",
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001459 &ilow, &ihigh, &istep))
Guido van Rossum3f5da241990-12-20 15:06:42 +00001460 return NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001461 }
1462 if (istep == 0) {
Fred Drake661ea262000-10-24 19:57:45 +00001463 PyErr_SetString(PyExc_ValueError, "range() arg 3 must not be zero");
Guido van Rossum3f5da241990-12-20 15:06:42 +00001464 return NULL;
1465 }
Guido van Rossum124eff01999-02-23 16:11:01 +00001466 if (istep > 0)
1467 bign = get_len_of_range(ilow, ihigh, istep);
1468 else
1469 bign = get_len_of_range(ihigh, ilow, -istep);
1470 n = (int)bign;
1471 if (bign < 0 || (long)n != bign) {
Guido van Rossume23cde21999-01-12 05:07:47 +00001472 PyErr_SetString(PyExc_OverflowError,
Fred Drake661ea262000-10-24 19:57:45 +00001473 "range() result has too many items");
Guido van Rossume23cde21999-01-12 05:07:47 +00001474 return NULL;
1475 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00001476 v = PyList_New(n);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001477 if (v == NULL)
1478 return NULL;
1479 for (i = 0; i < n; i++) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001480 PyObject *w = PyInt_FromLong(ilow);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001481 if (w == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001482 Py_DECREF(v);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001483 return NULL;
1484 }
Guido van Rossuma937d141998-04-24 18:22:02 +00001485 PyList_SET_ITEM(v, i, w);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001486 ilow += istep;
1487 }
1488 return v;
1489}
1490
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001491static char range_doc[] =
1492"range([start,] stop[, step]) -> list of integers\n\
1493\n\
1494Return a list containing an arithmetic progression of integers.\n\
1495range(i, j) returns [i, i+1, i+2, ..., j-1]; start (!) defaults to 0.\n\
1496When step is given, it specifies the increment (or decrement).\n\
1497For example, range(4) returns [0, 1, 2, 3]. The end point is omitted!\n\
1498These are exactly the valid indices for a list of 4 elements.";
1499
1500
Guido van Rossum79f25d91997-04-29 20:08:16 +00001501static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001502builtin_xrange(PyObject *self, PyObject *args)
Guido van Rossum12d12c51993-10-26 17:58:25 +00001503{
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001504 long ilow = 0, ihigh = 0, istep = 1;
Guido van Rossum0865dd91995-01-17 16:30:22 +00001505 long n;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001506
Guido van Rossum79f25d91997-04-29 20:08:16 +00001507 if (PyTuple_Size(args) <= 1) {
1508 if (!PyArg_ParseTuple(args,
Guido van Rossum0865dd91995-01-17 16:30:22 +00001509 "l;xrange() requires 1-3 int arguments",
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001510 &ihigh))
1511 return NULL;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001512 }
1513 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001514 if (!PyArg_ParseTuple(args,
Guido van Rossum0865dd91995-01-17 16:30:22 +00001515 "ll|l;xrange() requires 1-3 int arguments",
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001516 &ilow, &ihigh, &istep))
Guido van Rossum12d12c51993-10-26 17:58:25 +00001517 return NULL;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001518 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001519 if (istep == 0) {
Fred Drake661ea262000-10-24 19:57:45 +00001520 PyErr_SetString(PyExc_ValueError, "xrange() arg 3 must not be zero");
Guido van Rossum12d12c51993-10-26 17:58:25 +00001521 return NULL;
1522 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001523 if (istep > 0)
Guido van Rossum124eff01999-02-23 16:11:01 +00001524 n = get_len_of_range(ilow, ihigh, istep);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001525 else
Guido van Rossum124eff01999-02-23 16:11:01 +00001526 n = get_len_of_range(ihigh, ilow, -istep);
1527 if (n < 0) {
1528 PyErr_SetString(PyExc_OverflowError,
Fred Drake661ea262000-10-24 19:57:45 +00001529 "xrange() result has too many items");
Guido van Rossum124eff01999-02-23 16:11:01 +00001530 return NULL;
1531 }
Thomas Woutersefafcea2001-07-09 12:30:54 +00001532 return PyRange_New(ilow, n, istep, 1);
Guido van Rossum12d12c51993-10-26 17:58:25 +00001533}
1534
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001535static char xrange_doc[] =
1536"xrange([start,] stop[, step]) -> xrange object\n\
1537\n\
1538Like range(), but instead of returning a list, returns an object that\n\
1539generates the numbers in the range on demand. This is slightly slower\n\
1540than range() but more memory efficient.";
1541
1542
Guido van Rossum79f25d91997-04-29 20:08:16 +00001543static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001544builtin_raw_input(PyObject *self, PyObject *args)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001545{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001546 PyObject *v = NULL;
1547 PyObject *f;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001548
Guido van Rossum79f25d91997-04-29 20:08:16 +00001549 if (!PyArg_ParseTuple(args, "|O:[raw_]input", &v))
Guido van Rossum3165fe61992-09-25 21:59:05 +00001550 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001551 if (PyFile_AsFile(PySys_GetObject("stdin")) == stdin &&
1552 PyFile_AsFile(PySys_GetObject("stdout")) == stdout &&
Guido van Rossum53bb7ff1995-07-26 16:26:31 +00001553 isatty(fileno(stdin)) && isatty(fileno(stdout))) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001554 PyObject *po;
Guido van Rossum872537c1995-07-07 22:43:42 +00001555 char *prompt;
1556 char *s;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001557 PyObject *result;
Guido van Rossum872537c1995-07-07 22:43:42 +00001558 if (v != NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001559 po = PyObject_Str(v);
Guido van Rossum872537c1995-07-07 22:43:42 +00001560 if (po == NULL)
1561 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001562 prompt = PyString_AsString(po);
Guido van Rossumd9b52081998-06-26 18:25:38 +00001563 if (prompt == NULL)
1564 return NULL;
Guido van Rossum872537c1995-07-07 22:43:42 +00001565 }
1566 else {
1567 po = NULL;
1568 prompt = "";
1569 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00001570 s = PyOS_Readline(prompt);
1571 Py_XDECREF(po);
Guido van Rossum872537c1995-07-07 22:43:42 +00001572 if (s == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001573 PyErr_SetNone(PyExc_KeyboardInterrupt);
Guido van Rossum872537c1995-07-07 22:43:42 +00001574 return NULL;
1575 }
1576 if (*s == '\0') {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001577 PyErr_SetNone(PyExc_EOFError);
Guido van Rossum872537c1995-07-07 22:43:42 +00001578 result = NULL;
1579 }
1580 else { /* strip trailing '\n' */
Guido van Rossum106f2da2000-06-28 21:12:25 +00001581 size_t len = strlen(s);
1582 if (len > INT_MAX) {
1583 PyErr_SetString(PyExc_OverflowError, "input too long");
1584 result = NULL;
1585 }
1586 else {
1587 result = PyString_FromStringAndSize(s, (int)(len-1));
1588 }
Guido van Rossum872537c1995-07-07 22:43:42 +00001589 }
Guido van Rossumb18618d2000-05-03 23:44:39 +00001590 PyMem_FREE(s);
Guido van Rossum872537c1995-07-07 22:43:42 +00001591 return result;
1592 }
Guido van Rossum90933611991-06-07 16:10:43 +00001593 if (v != NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001594 f = PySys_GetObject("stdout");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001595 if (f == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001596 PyErr_SetString(PyExc_RuntimeError, "lost sys.stdout");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001597 return NULL;
1598 }
Guido van Rossumc8b6df91997-05-23 00:06:51 +00001599 if (Py_FlushLine() != 0 ||
1600 PyFile_WriteObject(v, f, Py_PRINT_RAW) != 0)
Guido van Rossum90933611991-06-07 16:10:43 +00001601 return NULL;
1602 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00001603 f = PySys_GetObject("stdin");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001604 if (f == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001605 PyErr_SetString(PyExc_RuntimeError, "lost sys.stdin");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001606 return NULL;
1607 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00001608 return PyFile_GetLine(f, -1);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001609}
1610
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001611static char raw_input_doc[] =
1612"raw_input([prompt]) -> string\n\
1613\n\
1614Read a string from standard input. The trailing newline is stripped.\n\
1615If the user hits EOF (Unix: Ctl-D, Windows: Ctl-Z+Return), raise EOFError.\n\
1616On Unix, GNU readline is used if enabled. The prompt string, if given,\n\
1617is printed without a trailing newline before reading.";
1618
1619
Guido van Rossum79f25d91997-04-29 20:08:16 +00001620static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001621builtin_reduce(PyObject *self, PyObject *args)
Guido van Rossum12d12c51993-10-26 17:58:25 +00001622{
Tim Peters15d81ef2001-05-04 04:39:21 +00001623 PyObject *seq, *func, *result = NULL, *it;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001624
Guido van Rossum79f25d91997-04-29 20:08:16 +00001625 if (!PyArg_ParseTuple(args, "OO|O:reduce", &func, &seq, &result))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001626 return NULL;
1627 if (result != NULL)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001628 Py_INCREF(result);
Guido van Rossum12d12c51993-10-26 17:58:25 +00001629
Tim Peters15d81ef2001-05-04 04:39:21 +00001630 it = PyObject_GetIter(seq);
1631 if (it == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001632 PyErr_SetString(PyExc_TypeError,
Tim Peters15d81ef2001-05-04 04:39:21 +00001633 "reduce() arg 2 must support iteration");
1634 Py_XDECREF(result);
Guido van Rossum12d12c51993-10-26 17:58:25 +00001635 return NULL;
1636 }
1637
Guido van Rossum79f25d91997-04-29 20:08:16 +00001638 if ((args = PyTuple_New(2)) == NULL)
Guido van Rossum2d951851994-08-29 12:52:16 +00001639 goto Fail;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001640
Tim Peters15d81ef2001-05-04 04:39:21 +00001641 for (;;) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001642 PyObject *op2;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001643
1644 if (args->ob_refcnt > 1) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001645 Py_DECREF(args);
1646 if ((args = PyTuple_New(2)) == NULL)
Guido van Rossum2d951851994-08-29 12:52:16 +00001647 goto Fail;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001648 }
1649
Tim Peters15d81ef2001-05-04 04:39:21 +00001650 op2 = PyIter_Next(it);
1651 if (op2 == NULL) {
Tim Petersf4848da2001-05-05 00:14:56 +00001652 if (PyErr_Occurred())
1653 goto Fail;
1654 break;
Guido van Rossum2d951851994-08-29 12:52:16 +00001655 }
Guido van Rossum12d12c51993-10-26 17:58:25 +00001656
Guido van Rossum2d951851994-08-29 12:52:16 +00001657 if (result == NULL)
1658 result = op2;
1659 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001660 PyTuple_SetItem(args, 0, result);
1661 PyTuple_SetItem(args, 1, op2);
1662 if ((result = PyEval_CallObject(func, args)) == NULL)
Guido van Rossum2d951851994-08-29 12:52:16 +00001663 goto Fail;
1664 }
Guido van Rossum12d12c51993-10-26 17:58:25 +00001665 }
1666
Guido van Rossum79f25d91997-04-29 20:08:16 +00001667 Py_DECREF(args);
Guido van Rossum12d12c51993-10-26 17:58:25 +00001668
Guido van Rossum2d951851994-08-29 12:52:16 +00001669 if (result == NULL)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001670 PyErr_SetString(PyExc_TypeError,
Fred Drake661ea262000-10-24 19:57:45 +00001671 "reduce() of empty sequence with no initial value");
Guido van Rossum2d951851994-08-29 12:52:16 +00001672
Tim Peters15d81ef2001-05-04 04:39:21 +00001673 Py_DECREF(it);
Guido van Rossum12d12c51993-10-26 17:58:25 +00001674 return result;
1675
Guido van Rossum2d951851994-08-29 12:52:16 +00001676Fail:
Guido van Rossum79f25d91997-04-29 20:08:16 +00001677 Py_XDECREF(args);
1678 Py_XDECREF(result);
Tim Peters15d81ef2001-05-04 04:39:21 +00001679 Py_DECREF(it);
Guido van Rossum12d12c51993-10-26 17:58:25 +00001680 return NULL;
1681}
1682
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001683static char reduce_doc[] =
1684"reduce(function, sequence[, initial]) -> value\n\
1685\n\
1686Apply a function of two arguments cumulatively to the items of a sequence,\n\
1687from left to right, so as to reduce the sequence to a single value.\n\
1688For example, reduce(lambda x, y: x+y, [1, 2, 3, 4, 5]) calculates\n\
1689((((1+2)+3)+4)+5). If initial is present, it is placed before the items\n\
1690of the sequence in the calculation, and serves as a default when the\n\
1691sequence is empty.";
1692
1693
Guido van Rossum79f25d91997-04-29 20:08:16 +00001694static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001695builtin_reload(PyObject *self, PyObject *v)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001696{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001697 return PyImport_ReloadModule(v);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001698}
1699
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001700static char reload_doc[] =
1701"reload(module) -> module\n\
1702\n\
1703Reload the module. The module must have been successfully imported before.";
1704
1705
Guido van Rossum79f25d91997-04-29 20:08:16 +00001706static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001707builtin_repr(PyObject *self, PyObject *v)
Guido van Rossumc89705d1992-11-26 08:54:07 +00001708{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001709 return PyObject_Repr(v);
Guido van Rossumc89705d1992-11-26 08:54:07 +00001710}
1711
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001712static char repr_doc[] =
1713"repr(object) -> string\n\
1714\n\
1715Return the canonical string representation of the object.\n\
1716For most object types, eval(repr(object)) == object.";
1717
1718
Guido van Rossum79f25d91997-04-29 20:08:16 +00001719static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001720builtin_round(PyObject *self, PyObject *args)
Guido van Rossum9e51f9b1993-02-12 16:29:05 +00001721{
Guido van Rossum9e51f9b1993-02-12 16:29:05 +00001722 double x;
1723 double f;
1724 int ndigits = 0;
Guido van Rossum9e51f9b1993-02-12 16:29:05 +00001725 int i;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001726
Guido van Rossum79f25d91997-04-29 20:08:16 +00001727 if (!PyArg_ParseTuple(args, "d|i:round", &x, &ndigits))
Guido van Rossum9e51f9b1993-02-12 16:29:05 +00001728 return NULL;
Guido van Rossum9e51f9b1993-02-12 16:29:05 +00001729 f = 1.0;
Guido van Rossum1e162d31998-05-09 14:42:25 +00001730 i = abs(ndigits);
1731 while (--i >= 0)
Guido van Rossum9e51f9b1993-02-12 16:29:05 +00001732 f = f*10.0;
Guido van Rossum1e162d31998-05-09 14:42:25 +00001733 if (ndigits < 0)
1734 x /= f;
Guido van Rossum9e51f9b1993-02-12 16:29:05 +00001735 else
Guido van Rossum1e162d31998-05-09 14:42:25 +00001736 x *= f;
1737 if (x >= 0.0)
1738 x = floor(x + 0.5);
1739 else
1740 x = ceil(x - 0.5);
1741 if (ndigits < 0)
1742 x *= f;
1743 else
1744 x /= f;
1745 return PyFloat_FromDouble(x);
Guido van Rossum9e51f9b1993-02-12 16:29:05 +00001746}
1747
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001748static char round_doc[] =
1749"round(number[, ndigits]) -> floating point number\n\
1750\n\
1751Round a number to a given precision in decimal digits (default 0 digits).\n\
1752This always returns a floating point number. Precision may be negative.";
1753
1754
Guido van Rossum79f25d91997-04-29 20:08:16 +00001755static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001756builtin_vars(PyObject *self, PyObject *args)
Guido van Rossum2d951851994-08-29 12:52:16 +00001757{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001758 PyObject *v = NULL;
1759 PyObject *d;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001760
Guido van Rossum79f25d91997-04-29 20:08:16 +00001761 if (!PyArg_ParseTuple(args, "|O:vars", &v))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001762 return NULL;
Guido van Rossum2d951851994-08-29 12:52:16 +00001763 if (v == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001764 d = PyEval_GetLocals();
Guido van Rossum53bb7ff1995-07-26 16:26:31 +00001765 if (d == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001766 if (!PyErr_Occurred())
1767 PyErr_SetString(PyExc_SystemError,
1768 "no locals!?");
Guido van Rossum53bb7ff1995-07-26 16:26:31 +00001769 }
1770 else
Guido van Rossum79f25d91997-04-29 20:08:16 +00001771 Py_INCREF(d);
Guido van Rossum2d951851994-08-29 12:52:16 +00001772 }
1773 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001774 d = PyObject_GetAttrString(v, "__dict__");
Guido van Rossum2d951851994-08-29 12:52:16 +00001775 if (d == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001776 PyErr_SetString(PyExc_TypeError,
Guido van Rossum2d951851994-08-29 12:52:16 +00001777 "vars() argument must have __dict__ attribute");
1778 return NULL;
1779 }
1780 }
1781 return d;
1782}
1783
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001784static char vars_doc[] =
1785"vars([object]) -> dictionary\n\
1786\n\
1787Without arguments, equivalent to locals().\n\
1788With an argument, equivalent to object.__dict__.";
1789
Barry Warsawcde8b1b1997-08-22 21:14:38 +00001790static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001791builtin_isinstance(PyObject *self, PyObject *args)
Barry Warsawcde8b1b1997-08-22 21:14:38 +00001792{
1793 PyObject *inst;
1794 PyObject *cls;
Guido van Rossum823649d2001-03-21 18:40:58 +00001795 int retval;
Barry Warsawcde8b1b1997-08-22 21:14:38 +00001796
Guido van Rossum43713e52000-02-29 13:59:29 +00001797 if (!PyArg_ParseTuple(args, "OO:isinstance", &inst, &cls))
Barry Warsawcde8b1b1997-08-22 21:14:38 +00001798 return NULL;
Guido van Rossumf5dd9141997-12-02 19:11:45 +00001799
Guido van Rossum823649d2001-03-21 18:40:58 +00001800 retval = PyObject_IsInstance(inst, cls);
1801 if (retval < 0)
Guido van Rossumad991772001-01-12 16:03:05 +00001802 return NULL;
Barry Warsawcde8b1b1997-08-22 21:14:38 +00001803 return PyInt_FromLong(retval);
1804}
1805
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001806static char isinstance_doc[] =
1807"isinstance(object, class-or-type) -> Boolean\n\
1808\n\
1809Return whether an object is an instance of a class or of a subclass thereof.\n\
1810With a type as second argument, return whether that is the object's type.";
1811
Barry Warsawcde8b1b1997-08-22 21:14:38 +00001812
1813static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001814builtin_issubclass(PyObject *self, PyObject *args)
Barry Warsawcde8b1b1997-08-22 21:14:38 +00001815{
1816 PyObject *derived;
1817 PyObject *cls;
1818 int retval;
1819
Guido van Rossum43713e52000-02-29 13:59:29 +00001820 if (!PyArg_ParseTuple(args, "OO:issubclass", &derived, &cls))
Barry Warsawcde8b1b1997-08-22 21:14:38 +00001821 return NULL;
Guido van Rossum668213d1999-06-16 17:28:37 +00001822
Guido van Rossum823649d2001-03-21 18:40:58 +00001823 retval = PyObject_IsSubclass(derived, cls);
1824 if (retval < 0)
1825 return NULL;
Barry Warsawcde8b1b1997-08-22 21:14:38 +00001826 return PyInt_FromLong(retval);
1827}
1828
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001829static char issubclass_doc[] =
1830"issubclass(C, B) -> Boolean\n\
1831\n\
1832Return whether class C is a subclass (i.e., a derived class) of class B.";
1833
Barry Warsawcde8b1b1997-08-22 21:14:38 +00001834
Barry Warsawbd599b52000-08-03 15:45:29 +00001835static PyObject*
1836builtin_zip(PyObject *self, PyObject *args)
1837{
1838 PyObject *ret;
1839 int itemsize = PySequence_Length(args);
Tim Peters8572b4f2001-05-06 01:05:02 +00001840 int i;
1841 PyObject *itlist; /* tuple of iterators */
Barry Warsawbd599b52000-08-03 15:45:29 +00001842
1843 if (itemsize < 1) {
1844 PyErr_SetString(PyExc_TypeError,
Fred Drake661ea262000-10-24 19:57:45 +00001845 "zip() requires at least one sequence");
Barry Warsawbd599b52000-08-03 15:45:29 +00001846 return NULL;
1847 }
1848 /* args must be a tuple */
1849 assert(PyTuple_Check(args));
1850
Tim Peters8572b4f2001-05-06 01:05:02 +00001851 /* allocate result list */
Barry Warsawbd599b52000-08-03 15:45:29 +00001852 if ((ret = PyList_New(0)) == NULL)
1853 return NULL;
1854
Tim Peters8572b4f2001-05-06 01:05:02 +00001855 /* obtain iterators */
1856 itlist = PyTuple_New(itemsize);
1857 if (itlist == NULL)
1858 goto Fail_ret;
1859 for (i = 0; i < itemsize; ++i) {
1860 PyObject *item = PyTuple_GET_ITEM(args, i);
1861 PyObject *it = PyObject_GetIter(item);
1862 if (it == NULL) {
1863 if (PyErr_ExceptionMatches(PyExc_TypeError))
1864 PyErr_Format(PyExc_TypeError,
1865 "zip argument #%d must support iteration",
1866 i+1);
1867 goto Fail_ret_itlist;
Barry Warsawbd599b52000-08-03 15:45:29 +00001868 }
Tim Peters8572b4f2001-05-06 01:05:02 +00001869 PyTuple_SET_ITEM(itlist, i, it);
1870 }
Barry Warsawbd599b52000-08-03 15:45:29 +00001871
Tim Peters8572b4f2001-05-06 01:05:02 +00001872 /* build result into ret list */
1873 for (;;) {
1874 int status;
1875 PyObject *next = PyTuple_New(itemsize);
1876 if (!next)
1877 goto Fail_ret_itlist;
1878
1879 for (i = 0; i < itemsize; i++) {
1880 PyObject *it = PyTuple_GET_ITEM(itlist, i);
1881 PyObject *item = PyIter_Next(it);
Barry Warsawbd599b52000-08-03 15:45:29 +00001882 if (!item) {
Tim Peters8572b4f2001-05-06 01:05:02 +00001883 if (PyErr_Occurred()) {
1884 Py_DECREF(ret);
1885 ret = NULL;
Barry Warsawbd599b52000-08-03 15:45:29 +00001886 }
1887 Py_DECREF(next);
Tim Peters8572b4f2001-05-06 01:05:02 +00001888 Py_DECREF(itlist);
1889 return ret;
Barry Warsawbd599b52000-08-03 15:45:29 +00001890 }
Tim Peters8572b4f2001-05-06 01:05:02 +00001891 PyTuple_SET_ITEM(next, i, item);
Barry Warsawbd599b52000-08-03 15:45:29 +00001892 }
Tim Peters8572b4f2001-05-06 01:05:02 +00001893
1894 status = PyList_Append(ret, next);
Barry Warsawbd599b52000-08-03 15:45:29 +00001895 Py_DECREF(next);
Tim Peters8572b4f2001-05-06 01:05:02 +00001896 if (status < 0)
1897 goto Fail_ret_itlist;
Barry Warsawbd599b52000-08-03 15:45:29 +00001898 }
Tim Peters8572b4f2001-05-06 01:05:02 +00001899
1900Fail_ret_itlist:
1901 Py_DECREF(itlist);
1902Fail_ret:
1903 Py_DECREF(ret);
1904 return NULL;
Barry Warsawbd599b52000-08-03 15:45:29 +00001905}
1906
1907
1908static char zip_doc[] =
1909"zip(seq1 [, seq2 [...]]) -> [(seq1[0], seq2[0] ...), (...)]\n\
1910\n\
1911Return a list of tuples, where each tuple contains the i-th element\n\
1912from each of the argument sequences. The returned list is truncated\n\
1913in length to the length of the shortest argument sequence.";
1914
1915
Guido van Rossum79f25d91997-04-29 20:08:16 +00001916static PyMethodDef builtin_methods[] = {
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001917 {"__import__", builtin___import__, METH_VARARGS, import_doc},
1918 {"abs", builtin_abs, METH_O, abs_doc},
1919 {"apply", builtin_apply, METH_VARARGS, apply_doc},
1920 {"buffer", builtin_buffer, METH_VARARGS, buffer_doc},
1921 {"callable", builtin_callable, METH_O, callable_doc},
1922 {"chr", builtin_chr, METH_VARARGS, chr_doc},
1923 {"cmp", builtin_cmp, METH_VARARGS, cmp_doc},
1924 {"coerce", builtin_coerce, METH_VARARGS, coerce_doc},
1925 {"compile", builtin_compile, METH_VARARGS, compile_doc},
1926 {"delattr", builtin_delattr, METH_VARARGS, delattr_doc},
1927 {"dir", builtin_dir, METH_VARARGS, dir_doc},
1928 {"divmod", builtin_divmod, METH_VARARGS, divmod_doc},
1929 {"eval", builtin_eval, METH_VARARGS, eval_doc},
1930 {"execfile", builtin_execfile, METH_VARARGS, execfile_doc},
1931 {"filter", builtin_filter, METH_VARARGS, filter_doc},
1932 {"getattr", builtin_getattr, METH_VARARGS, getattr_doc},
1933 {"globals", (PyCFunction)builtin_globals, METH_NOARGS, globals_doc},
1934 {"hasattr", builtin_hasattr, METH_VARARGS, hasattr_doc},
1935 {"hash", builtin_hash, METH_O, hash_doc},
1936 {"hex", builtin_hex, METH_O, hex_doc},
1937 {"id", builtin_id, METH_O, id_doc},
1938 {"input", builtin_input, METH_VARARGS, input_doc},
1939 {"intern", builtin_intern, METH_VARARGS, intern_doc},
1940 {"isinstance", builtin_isinstance, METH_VARARGS, isinstance_doc},
1941 {"issubclass", builtin_issubclass, METH_VARARGS, issubclass_doc},
1942 {"iter", builtin_iter, METH_VARARGS, iter_doc},
1943 {"len", builtin_len, METH_O, len_doc},
1944 {"locals", (PyCFunction)builtin_locals, METH_NOARGS, locals_doc},
1945 {"map", builtin_map, METH_VARARGS, map_doc},
1946 {"max", builtin_max, METH_VARARGS, max_doc},
1947 {"min", builtin_min, METH_VARARGS, min_doc},
1948 {"oct", builtin_oct, METH_O, oct_doc},
1949 {"open", builtin_open, METH_VARARGS, open_doc},
1950 {"ord", builtin_ord, METH_O, ord_doc},
1951 {"pow", builtin_pow, METH_VARARGS, pow_doc},
1952 {"range", builtin_range, METH_VARARGS, range_doc},
1953 {"raw_input", builtin_raw_input, METH_VARARGS, raw_input_doc},
1954 {"reduce", builtin_reduce, METH_VARARGS, reduce_doc},
1955 {"reload", builtin_reload, METH_O, reload_doc},
1956 {"repr", builtin_repr, METH_O, repr_doc},
1957 {"round", builtin_round, METH_VARARGS, round_doc},
1958 {"setattr", builtin_setattr, METH_VARARGS, setattr_doc},
1959 {"slice", builtin_slice, METH_VARARGS, slice_doc},
Martin v. Löwis339d0f72001-08-17 18:39:25 +00001960#ifdef Py_USING_UNICODE
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001961 {"unichr", builtin_unichr, METH_VARARGS, unichr_doc},
Martin v. Löwis339d0f72001-08-17 18:39:25 +00001962#endif
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001963 {"vars", builtin_vars, METH_VARARGS, vars_doc},
1964 {"xrange", builtin_xrange, METH_VARARGS, xrange_doc},
1965 {"zip", builtin_zip, METH_VARARGS, zip_doc},
Guido van Rossumc02e15c1991-12-16 13:03:00 +00001966 {NULL, NULL},
Guido van Rossum3f5da241990-12-20 15:06:42 +00001967};
1968
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001969static char builtin_doc[] =
1970"Built-in functions, exceptions, and other objects.\n\
1971\n\
1972Noteworthy: None is the `nil' object; Ellipsis represents `...' in slices.";
1973
Guido van Rossum25ce5661997-08-02 03:10:38 +00001974PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001975_PyBuiltin_Init(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +00001976{
Fred Drake5550de32000-06-20 04:54:19 +00001977 PyObject *mod, *dict, *debug;
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001978 mod = Py_InitModule4("__builtin__", builtin_methods,
1979 builtin_doc, (PyObject *)NULL,
1980 PYTHON_API_VERSION);
Guido van Rossum25ce5661997-08-02 03:10:38 +00001981 if (mod == NULL)
1982 return NULL;
1983 dict = PyModule_GetDict(mod);
Guido van Rossum25ce5661997-08-02 03:10:38 +00001984 if (PyDict_SetItemString(dict, "None", Py_None) < 0)
1985 return NULL;
1986 if (PyDict_SetItemString(dict, "Ellipsis", Py_Ellipsis) < 0)
1987 return NULL;
Guido van Rossumad991772001-01-12 16:03:05 +00001988 if (PyDict_SetItemString(dict, "NotImplemented",
Neil Schemenauer23ab1992001-01-04 01:48:42 +00001989 Py_NotImplemented) < 0)
1990 return NULL;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001991 if (PyDict_SetItemString(dict, "classmethod",
1992 (PyObject *) &PyClassMethod_Type) < 0)
1993 return NULL;
1994#ifndef WITHOUT_COMPLEX
1995 if (PyDict_SetItemString(dict, "complex",
1996 (PyObject *) &PyComplex_Type) < 0)
1997 return NULL;
1998#endif
1999 if (PyDict_SetItemString(dict, "dictionary",
2000 (PyObject *) &PyDict_Type) < 0)
2001 return NULL;
2002 if (PyDict_SetItemString(dict, "float",
2003 (PyObject *) &PyFloat_Type) < 0)
2004 return NULL;
Guido van Rossum29a62dd2001-08-23 21:40:38 +00002005 if (PyDict_SetItemString(dict, "getset",
2006 (PyObject *) &PyGetSet_Type) < 0)
2007 return NULL;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002008 if (PyDict_SetItemString(dict, "int", (PyObject *) &PyInt_Type) < 0)
2009 return NULL;
2010 if (PyDict_SetItemString(dict, "list", (PyObject *) &PyList_Type) < 0)
2011 return NULL;
2012 if (PyDict_SetItemString(dict, "long", (PyObject *) &PyLong_Type) < 0)
2013 return NULL;
2014 if (PyDict_SetItemString(dict, "object",
2015 (PyObject *) &PyBaseObject_Type) < 0)
2016 return NULL;
2017 if (PyDict_SetItemString(dict, "staticmethod",
2018 (PyObject *) &PyStaticMethod_Type) < 0)
2019 return NULL;
2020 if (PyDict_SetItemString(dict, "str", (PyObject *) &PyString_Type) < 0)
2021 return NULL;
Guido van Rossumf5cb3572001-08-24 16:52:18 +00002022 if (PyDict_SetItemString(dict, "super",
2023 (PyObject *) &PySuper_Type) < 0)
2024 return NULL;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002025 if (PyDict_SetItemString(dict, "tuple",
2026 (PyObject *) &PyTuple_Type) < 0)
2027 return NULL;
2028 if (PyDict_SetItemString(dict, "type", (PyObject *) &PyType_Type) < 0)
2029 return NULL;
Martin v. Löwis339d0f72001-08-17 18:39:25 +00002030#ifdef Py_USING_UNICODE
Tim Peters6d6c1a32001-08-02 04:15:00 +00002031 if (PyDict_SetItemString(dict, "unicode",
2032 (PyObject *) &PyUnicode_Type) < 0)
2033 return NULL;
Martin v. Löwis339d0f72001-08-17 18:39:25 +00002034#endif
Fred Drake5550de32000-06-20 04:54:19 +00002035 debug = PyInt_FromLong(Py_OptimizeFlag == 0);
2036 if (PyDict_SetItemString(dict, "__debug__", debug) < 0) {
2037 Py_XDECREF(debug);
Guido van Rossum25ce5661997-08-02 03:10:38 +00002038 return NULL;
Fred Drake5550de32000-06-20 04:54:19 +00002039 }
2040 Py_XDECREF(debug);
Barry Warsaw757af0e1997-08-29 22:13:51 +00002041
Guido van Rossum25ce5661997-08-02 03:10:38 +00002042 return mod;
Guido van Rossum3f5da241990-12-20 15:06:42 +00002043}
2044
Guido van Rossume77a7571993-11-03 15:01:26 +00002045/* Helper for filter(): filter a tuple through a function */
Guido van Rossum12d12c51993-10-26 17:58:25 +00002046
Guido van Rossum79f25d91997-04-29 20:08:16 +00002047static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002048filtertuple(PyObject *func, PyObject *tuple)
Guido van Rossum12d12c51993-10-26 17:58:25 +00002049{
Guido van Rossum79f25d91997-04-29 20:08:16 +00002050 PyObject *result;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002051 register int i, j;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002052 int len = PyTuple_Size(tuple);
Guido van Rossum12d12c51993-10-26 17:58:25 +00002053
Guido van Rossumb7b45621995-08-04 04:07:45 +00002054 if (len == 0) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002055 Py_INCREF(tuple);
Guido van Rossumb7b45621995-08-04 04:07:45 +00002056 return tuple;
2057 }
2058
Guido van Rossum79f25d91997-04-29 20:08:16 +00002059 if ((result = PyTuple_New(len)) == NULL)
Guido van Rossum2586bf01993-11-01 16:21:44 +00002060 return NULL;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002061
Guido van Rossum12d12c51993-10-26 17:58:25 +00002062 for (i = j = 0; i < len; ++i) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002063 PyObject *item, *good;
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002064 int ok;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002065
Guido van Rossum79f25d91997-04-29 20:08:16 +00002066 if ((item = PyTuple_GetItem(tuple, i)) == NULL)
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002067 goto Fail_1;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002068 if (func == Py_None) {
2069 Py_INCREF(item);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002070 good = item;
2071 }
2072 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002073 PyObject *arg = Py_BuildValue("(O)", item);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002074 if (arg == NULL)
2075 goto Fail_1;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002076 good = PyEval_CallObject(func, arg);
2077 Py_DECREF(arg);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002078 if (good == NULL)
Guido van Rossum12d12c51993-10-26 17:58:25 +00002079 goto Fail_1;
2080 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002081 ok = PyObject_IsTrue(good);
2082 Py_DECREF(good);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002083 if (ok) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002084 Py_INCREF(item);
2085 if (PyTuple_SetItem(result, j++, item) < 0)
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002086 goto Fail_1;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002087 }
Guido van Rossum12d12c51993-10-26 17:58:25 +00002088 }
2089
Tim Peters4324aa32001-05-28 22:30:08 +00002090 if (_PyTuple_Resize(&result, j) < 0)
Guido van Rossum12d12c51993-10-26 17:58:25 +00002091 return NULL;
2092
Guido van Rossum12d12c51993-10-26 17:58:25 +00002093 return result;
2094
Guido van Rossum12d12c51993-10-26 17:58:25 +00002095Fail_1:
Guido van Rossum79f25d91997-04-29 20:08:16 +00002096 Py_DECREF(result);
Guido van Rossum12d12c51993-10-26 17:58:25 +00002097 return NULL;
2098}
2099
2100
Guido van Rossume77a7571993-11-03 15:01:26 +00002101/* Helper for filter(): filter a string through a function */
Guido van Rossum12d12c51993-10-26 17:58:25 +00002102
Guido van Rossum79f25d91997-04-29 20:08:16 +00002103static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002104filterstring(PyObject *func, PyObject *strobj)
Guido van Rossum12d12c51993-10-26 17:58:25 +00002105{
Guido van Rossum79f25d91997-04-29 20:08:16 +00002106 PyObject *result;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002107 register int i, j;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002108 int len = PyString_Size(strobj);
Guido van Rossum12d12c51993-10-26 17:58:25 +00002109
Guido van Rossum79f25d91997-04-29 20:08:16 +00002110 if (func == Py_None) {
Guido van Rossum2586bf01993-11-01 16:21:44 +00002111 /* No character is ever false -- share input string */
Guido van Rossum79f25d91997-04-29 20:08:16 +00002112 Py_INCREF(strobj);
Guido van Rossum2d951851994-08-29 12:52:16 +00002113 return strobj;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002114 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002115 if ((result = PyString_FromStringAndSize(NULL, len)) == NULL)
Guido van Rossum2586bf01993-11-01 16:21:44 +00002116 return NULL;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002117
Guido van Rossum12d12c51993-10-26 17:58:25 +00002118 for (i = j = 0; i < len; ++i) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002119 PyObject *item, *arg, *good;
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002120 int ok;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002121
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002122 item = (*strobj->ob_type->tp_as_sequence->sq_item)(strobj, i);
2123 if (item == NULL)
2124 goto Fail_1;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002125 arg = Py_BuildValue("(O)", item);
Tim Peters388ed082001-04-07 20:34:48 +00002126 if (arg == NULL) {
2127 Py_DECREF(item);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002128 goto Fail_1;
Tim Peters388ed082001-04-07 20:34:48 +00002129 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002130 good = PyEval_CallObject(func, arg);
2131 Py_DECREF(arg);
Tim Peters388ed082001-04-07 20:34:48 +00002132 if (good == NULL) {
2133 Py_DECREF(item);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002134 goto Fail_1;
Tim Peters388ed082001-04-07 20:34:48 +00002135 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002136 ok = PyObject_IsTrue(good);
2137 Py_DECREF(good);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002138 if (ok)
Guido van Rossum79f25d91997-04-29 20:08:16 +00002139 PyString_AS_STRING((PyStringObject *)result)[j++] =
2140 PyString_AS_STRING((PyStringObject *)item)[0];
Tim Peters388ed082001-04-07 20:34:48 +00002141 Py_DECREF(item);
Guido van Rossum12d12c51993-10-26 17:58:25 +00002142 }
2143
Guido van Rossum79f25d91997-04-29 20:08:16 +00002144 if (j < len && _PyString_Resize(&result, j) < 0)
Guido van Rossum12d12c51993-10-26 17:58:25 +00002145 return NULL;
2146
Guido van Rossum12d12c51993-10-26 17:58:25 +00002147 return result;
2148
Guido van Rossum12d12c51993-10-26 17:58:25 +00002149Fail_1:
Guido van Rossum79f25d91997-04-29 20:08:16 +00002150 Py_DECREF(result);
Guido van Rossum12d12c51993-10-26 17:58:25 +00002151 return NULL;
2152}