blob: 7e7ad2ef60c3c3d12ac1bbbfe6ba2bae6d5fb757 [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 Rossume2ae77b2001-10-24 20:42:55 +000012#ifdef RISCOS
13#include "unixstuff.h"
14#endif
15
Mark Hammond26cffde2001-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*/
Martin v. Löwis6238d2b2002-06-30 15:26:10 +000019#if defined(MS_WINDOWS) && defined(HAVE_USABLE_WCHAR_T)
Mark Hammond26cffde2001-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
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000043PyDoc_STRVAR(import_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +000044"__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\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000052fromlist is not empty.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +000053
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
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000061PyDoc_STRVAR(abs_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +000062"abs(number) -> number\n\
63\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000064Return the absolute value of the argument.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +000065
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
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000101PyDoc_STRVAR(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\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000106Note 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 *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000110builtin_callable(PyObject *self, PyObject *v)
Guido van Rossum2d951851994-08-29 12:52:16 +0000111{
Guido van Rossum77f6a652002-04-03 22:41:51 +0000112 return PyBool_FromLong((long)PyCallable_Check(v));
Guido van Rossum2d951851994-08-29 12:52:16 +0000113}
114
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000115PyDoc_STRVAR(callable_doc,
Guido van Rossum77f6a652002-04-03 22:41:51 +0000116"callable(object) -> bool\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000117\n\
118Return whether the object is callable (i.e., some kind of function).\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000119Note that classes are callable, as are instances with a __call__() method.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000120
121
Guido van Rossum79f25d91997-04-29 20:08:16 +0000122static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000123builtin_filter(PyObject *self, PyObject *args)
Guido van Rossum12d12c51993-10-26 17:58:25 +0000124{
Guido van Rossumc7903a12002-08-16 07:04:56 +0000125 PyObject *func, *seq, *result, *it, *arg;
Tim Peters0e57abf2001-05-02 07:39:38 +0000126 int len; /* guess for result list size */
Tim Petersf4848da2001-05-05 00:14:56 +0000127 register int j;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000128
Guido van Rossum79f25d91997-04-29 20:08:16 +0000129 if (!PyArg_ParseTuple(args, "OO:filter", &func, &seq))
Guido van Rossum12d12c51993-10-26 17:58:25 +0000130 return NULL;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000131
Tim Peters0e57abf2001-05-02 07:39:38 +0000132 /* Strings and tuples return a result of the same type. */
133 if (PyString_Check(seq))
134 return filterstring(func, seq);
135 if (PyTuple_Check(seq))
136 return filtertuple(func, seq);
137
138 /* Get iterator. */
139 it = PyObject_GetIter(seq);
140 if (it == NULL)
141 return NULL;
142
143 /* Guess a result list size. */
144 len = -1; /* unknown */
145 if (PySequence_Check(seq) &&
146 seq->ob_type->tp_as_sequence->sq_length) {
147 len = PySequence_Size(seq);
148 if (len < 0)
149 PyErr_Clear();
Guido van Rossum12d12c51993-10-26 17:58:25 +0000150 }
Tim Peters0e57abf2001-05-02 07:39:38 +0000151 if (len < 0)
152 len = 8; /* arbitrary */
Guido van Rossum12d12c51993-10-26 17:58:25 +0000153
Guido van Rossumc7903a12002-08-16 07:04:56 +0000154 /* Pre-allocate argument list tuple. */
155 arg = PyTuple_New(1);
156 if (arg == NULL)
157 goto Fail_arg;
158
Tim Peters0e57abf2001-05-02 07:39:38 +0000159 /* Get a result list. */
Guido van Rossum79f25d91997-04-29 20:08:16 +0000160 if (PyList_Check(seq) && seq->ob_refcnt == 1) {
Tim Peters0e57abf2001-05-02 07:39:38 +0000161 /* Eww - can modify the list in-place. */
Guido van Rossum79f25d91997-04-29 20:08:16 +0000162 Py_INCREF(seq);
Guido van Rossum12d12c51993-10-26 17:58:25 +0000163 result = seq;
164 }
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000165 else {
Tim Peters0e57abf2001-05-02 07:39:38 +0000166 result = PyList_New(len);
167 if (result == NULL)
168 goto Fail_it;
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000169 }
Guido van Rossum12d12c51993-10-26 17:58:25 +0000170
Tim Peters0e57abf2001-05-02 07:39:38 +0000171 /* Build the result list. */
Tim Petersf4848da2001-05-05 00:14:56 +0000172 j = 0;
173 for (;;) {
Guido van Rossumc7903a12002-08-16 07:04:56 +0000174 PyObject *item;
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000175 int ok;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000176
Tim Peters0e57abf2001-05-02 07:39:38 +0000177 item = PyIter_Next(it);
178 if (item == NULL) {
Tim Petersf4848da2001-05-05 00:14:56 +0000179 if (PyErr_Occurred())
180 goto Fail_result_it;
Tim Peters0e57abf2001-05-02 07:39:38 +0000181 break;
Guido van Rossum2d951851994-08-29 12:52:16 +0000182 }
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000183
Guido van Rossum79f25d91997-04-29 20:08:16 +0000184 if (func == Py_None) {
Guido van Rossumc7903a12002-08-16 07:04:56 +0000185 ok = PyObject_IsTrue(item);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000186 }
187 else {
Guido van Rossumc7903a12002-08-16 07:04:56 +0000188 PyObject *good;
189 PyTuple_SET_ITEM(arg, 0, item);
190 good = PyObject_Call(func, arg, NULL);
191 PyTuple_SET_ITEM(arg, 0, NULL);
Guido van Rossum58b68731995-01-10 17:40:55 +0000192 if (good == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000193 Py_DECREF(item);
Tim Peters0e57abf2001-05-02 07:39:38 +0000194 goto Fail_result_it;
Guido van Rossum58b68731995-01-10 17:40:55 +0000195 }
Guido van Rossumc7903a12002-08-16 07:04:56 +0000196 ok = PyObject_IsTrue(good);
197 Py_DECREF(good);
Guido van Rossum12d12c51993-10-26 17:58:25 +0000198 }
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000199 if (ok) {
Tim Peters0e57abf2001-05-02 07:39:38 +0000200 if (j < len)
201 PyList_SET_ITEM(result, j, item);
Guido van Rossum2d951851994-08-29 12:52:16 +0000202 else {
Barry Warsawfa77e091999-01-28 18:49:12 +0000203 int status = PyList_Append(result, item);
Barry Warsawfa77e091999-01-28 18:49:12 +0000204 Py_DECREF(item);
205 if (status < 0)
Tim Peters0e57abf2001-05-02 07:39:38 +0000206 goto Fail_result_it;
Guido van Rossum2d951851994-08-29 12:52:16 +0000207 }
Tim Peters0e57abf2001-05-02 07:39:38 +0000208 ++j;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000209 }
Tim Peters0e57abf2001-05-02 07:39:38 +0000210 else
211 Py_DECREF(item);
Guido van Rossum12d12c51993-10-26 17:58:25 +0000212 }
213
Guido van Rossum12d12c51993-10-26 17:58:25 +0000214
Tim Peters0e57abf2001-05-02 07:39:38 +0000215 /* Cut back result list if len is too big. */
Guido van Rossum79f25d91997-04-29 20:08:16 +0000216 if (j < len && PyList_SetSlice(result, j, len, NULL) < 0)
Tim Peters0e57abf2001-05-02 07:39:38 +0000217 goto Fail_result_it;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000218
Tim Peters3c6b1482001-05-21 08:07:05 +0000219 Py_DECREF(it);
Guido van Rossumc7903a12002-08-16 07:04:56 +0000220 Py_DECREF(arg);
Guido van Rossum12d12c51993-10-26 17:58:25 +0000221 return result;
222
Tim Peters0e57abf2001-05-02 07:39:38 +0000223Fail_result_it:
Guido van Rossum79f25d91997-04-29 20:08:16 +0000224 Py_DECREF(result);
Tim Peters0e57abf2001-05-02 07:39:38 +0000225Fail_it:
226 Py_DECREF(it);
Guido van Rossumc7903a12002-08-16 07:04:56 +0000227Fail_arg:
228 Py_DECREF(arg);
Guido van Rossum12d12c51993-10-26 17:58:25 +0000229 return NULL;
230}
231
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000232PyDoc_STRVAR(filter_doc,
Tim Petersd50e5442002-03-09 00:06:26 +0000233"filter(function or None, sequence) -> list, tuple, or string\n"
234"\n"
235"Return those items of sequence for which function(item) is true. If\n"
236"function is None, return the items that are true. If sequence is a tuple\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000237"or string, return the same type, else return a list.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000238
Guido van Rossum79f25d91997-04-29 20:08:16 +0000239static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000240builtin_chr(PyObject *self, PyObject *args)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000241{
242 long x;
243 char s[1];
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000244
Guido van Rossum79f25d91997-04-29 20:08:16 +0000245 if (!PyArg_ParseTuple(args, "l:chr", &x))
Guido van Rossum3f5da241990-12-20 15:06:42 +0000246 return NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000247 if (x < 0 || x >= 256) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000248 PyErr_SetString(PyExc_ValueError,
249 "chr() arg not in range(256)");
Guido van Rossum3f5da241990-12-20 15:06:42 +0000250 return NULL;
251 }
Guido van Rossum6bf62da1997-04-11 20:37:35 +0000252 s[0] = (char)x;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000253 return PyString_FromStringAndSize(s, 1);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000254}
255
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000256PyDoc_STRVAR(chr_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000257"chr(i) -> character\n\
258\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000259Return a string of one character with ordinal i; 0 <= i < 256.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000260
261
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000262#ifdef Py_USING_UNICODE
Guido van Rossum79f25d91997-04-29 20:08:16 +0000263static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000264builtin_unichr(PyObject *self, PyObject *args)
Guido van Rossum09095f32000-03-10 23:00:52 +0000265{
266 long x;
Guido van Rossum09095f32000-03-10 23:00:52 +0000267
268 if (!PyArg_ParseTuple(args, "l:unichr", &x))
269 return NULL;
Fredrik Lundh0dcf67e2001-06-26 20:01:56 +0000270
Marc-André Lemburgcc8764c2002-08-11 12:23:04 +0000271 return PyUnicode_FromOrdinal(x);
Guido van Rossum09095f32000-03-10 23:00:52 +0000272}
273
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000274PyDoc_STRVAR(unichr_doc,
Fred Drake078b24f2000-04-13 02:42:50 +0000275"unichr(i) -> Unicode character\n\
Guido van Rossum09095f32000-03-10 23:00:52 +0000276\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000277Return a Unicode string of one character with ordinal i; 0 <= i <= 0x10ffff.");
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000278#endif
Guido van Rossum09095f32000-03-10 23:00:52 +0000279
280
281static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000282builtin_cmp(PyObject *self, PyObject *args)
Guido van Rossuma9e7dc11992-10-18 18:53:57 +0000283{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000284 PyObject *a, *b;
Guido van Rossum09df08a1998-05-22 00:51:39 +0000285 int c;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000286
Guido van Rossum79f25d91997-04-29 20:08:16 +0000287 if (!PyArg_ParseTuple(args, "OO:cmp", &a, &b))
Guido van Rossuma9e7dc11992-10-18 18:53:57 +0000288 return NULL;
Guido van Rossum09df08a1998-05-22 00:51:39 +0000289 if (PyObject_Cmp(a, b, &c) < 0)
Guido van Rossumc8b6df91997-05-23 00:06:51 +0000290 return NULL;
Guido van Rossum09df08a1998-05-22 00:51:39 +0000291 return PyInt_FromLong((long)c);
Guido van Rossuma9e7dc11992-10-18 18:53:57 +0000292}
293
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000294PyDoc_STRVAR(cmp_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000295"cmp(x, y) -> integer\n\
296\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000297Return negative if x<y, zero if x==y, positive if x>y.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000298
299
Guido van Rossum79f25d91997-04-29 20:08:16 +0000300static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000301builtin_coerce(PyObject *self, PyObject *args)
Guido van Rossum6a00cd81995-01-07 12:39:01 +0000302{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000303 PyObject *v, *w;
304 PyObject *res;
Guido van Rossum5524a591995-01-10 15:26:20 +0000305
Guido van Rossum79f25d91997-04-29 20:08:16 +0000306 if (!PyArg_ParseTuple(args, "OO:coerce", &v, &w))
Guido van Rossum5524a591995-01-10 15:26:20 +0000307 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000308 if (PyNumber_Coerce(&v, &w) < 0)
Guido van Rossum04691fc1992-08-12 15:35:34 +0000309 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000310 res = Py_BuildValue("(OO)", v, w);
311 Py_DECREF(v);
312 Py_DECREF(w);
Guido van Rossum04691fc1992-08-12 15:35:34 +0000313 return res;
314}
315
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000316PyDoc_STRVAR(coerce_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000317"coerce(x, y) -> None or (x1, y1)\n\
318\n\
319When x and y can be coerced to values of the same type, return a tuple\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000320containing the coerced values. When they can't be coerced, return None.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000321
322
Guido van Rossum79f25d91997-04-29 20:08:16 +0000323static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000324builtin_compile(PyObject *self, PyObject *args)
Guido van Rossum5b722181993-03-30 17:46:03 +0000325{
326 char *str;
327 char *filename;
328 char *startstr;
329 int start;
Tim Peters6cd6a822001-08-17 22:11:27 +0000330 int dont_inherit = 0;
331 int supplied_flags = 0;
Tim Peters5ba58662001-07-16 02:29:45 +0000332 PyCompilerFlags cf;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000333
Tim Peters67d687a2002-04-29 21:27:32 +0000334 if (!PyArg_ParseTuple(args, "sss|ii:compile", &str, &filename,
Tim Peters6cd6a822001-08-17 22:11:27 +0000335 &startstr, &supplied_flags, &dont_inherit))
Guido van Rossum5b722181993-03-30 17:46:03 +0000336 return NULL;
Tim Peters6cd6a822001-08-17 22:11:27 +0000337
Guido van Rossum5b722181993-03-30 17:46:03 +0000338 if (strcmp(startstr, "exec") == 0)
Guido van Rossumb05a5c71997-05-07 17:46:13 +0000339 start = Py_file_input;
Guido van Rossum5b722181993-03-30 17:46:03 +0000340 else if (strcmp(startstr, "eval") == 0)
Guido van Rossumb05a5c71997-05-07 17:46:13 +0000341 start = Py_eval_input;
Guido van Rossum872537c1995-07-07 22:43:42 +0000342 else if (strcmp(startstr, "single") == 0)
Guido van Rossumb05a5c71997-05-07 17:46:13 +0000343 start = Py_single_input;
Guido van Rossum5b722181993-03-30 17:46:03 +0000344 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000345 PyErr_SetString(PyExc_ValueError,
Fred Drake661ea262000-10-24 19:57:45 +0000346 "compile() arg 3 must be 'exec' or 'eval' or 'single'");
Guido van Rossum5b722181993-03-30 17:46:03 +0000347 return NULL;
348 }
Tim Peters6cd6a822001-08-17 22:11:27 +0000349
350 if (supplied_flags & ~(PyCF_MASK | PyCF_MASK_OBSOLETE)) {
351 PyErr_SetString(PyExc_ValueError,
352 "compile(): unrecognised flags");
353 return NULL;
354 }
355 /* XXX Warn if (supplied_flags & PyCF_MASK_OBSOLETE) != 0? */
356
357 cf.cf_flags = supplied_flags;
358 if (!dont_inherit) {
359 PyEval_MergeCompilerFlags(&cf);
360 }
361 return Py_CompileStringFlags(str, filename, start, &cf);
Guido van Rossum5b722181993-03-30 17:46:03 +0000362}
363
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000364PyDoc_STRVAR(compile_doc,
Tim Peters6cd6a822001-08-17 22:11:27 +0000365"compile(source, filename, mode[, flags[, dont_inherit]]) -> code object\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000366\n\
367Compile the source string (a Python module, statement or expression)\n\
368into a code object that can be executed by the exec statement or eval().\n\
369The filename will be used for run-time error messages.\n\
370The mode must be 'exec' to compile a module, 'single' to compile a\n\
Tim Peters6cd6a822001-08-17 22:11:27 +0000371single (interactive) statement, or 'eval' to compile an expression.\n\
372The flags argument, if present, controls which future statements influence\n\
373the compilation of the code.\n\
374The dont_inherit argument, if non-zero, stops the compilation inheriting\n\
375the effects of any future statements in effect in the code calling\n\
376compile; if absent or zero these statements do influence the compilation,\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000377in addition to any features explicitly specified.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000378
Guido van Rossum79f25d91997-04-29 20:08:16 +0000379static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000380builtin_dir(PyObject *self, PyObject *args)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000381{
Tim Peters5d2b77c2001-09-03 05:47:38 +0000382 PyObject *arg = NULL;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000383
Tim Peters5d2b77c2001-09-03 05:47:38 +0000384 if (!PyArg_ParseTuple(args, "|O:dir", &arg))
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000385 return NULL;
Tim Peters7eea37e2001-09-04 22:08:56 +0000386 return PyObject_Dir(arg);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000387}
388
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000389PyDoc_STRVAR(dir_doc,
Tim Peters5d2b77c2001-09-03 05:47:38 +0000390"dir([object]) -> list of strings\n"
391"\n"
392"Return an alphabetized list of names comprising (some of) the attributes\n"
393"of the given object, and of attributes reachable from it:\n"
394"\n"
395"No argument: the names in the current scope.\n"
396"Module object: the module attributes.\n"
Tim Peters37a309d2001-09-04 01:20:04 +0000397"Type or class object: its attributes, and recursively the attributes of\n"
398" its bases.\n"
Tim Peters5d2b77c2001-09-03 05:47:38 +0000399"Otherwise: its attributes, its class's attributes, and recursively the\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000400" attributes of its class's base classes.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000401
Guido van Rossum79f25d91997-04-29 20:08:16 +0000402static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000403builtin_divmod(PyObject *self, PyObject *args)
Guido van Rossum6a00cd81995-01-07 12:39:01 +0000404{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000405 PyObject *v, *w;
Guido van Rossum6a00cd81995-01-07 12:39:01 +0000406
Guido van Rossum79f25d91997-04-29 20:08:16 +0000407 if (!PyArg_ParseTuple(args, "OO:divmod", &v, &w))
Guido van Rossum6a00cd81995-01-07 12:39:01 +0000408 return NULL;
Guido van Rossum09df08a1998-05-22 00:51:39 +0000409 return PyNumber_Divmod(v, w);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000410}
411
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000412PyDoc_STRVAR(divmod_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000413"divmod(x, y) -> (div, mod)\n\
414\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000415Return the tuple ((x-x%y)/y, x%y). Invariant: div*y + mod == x.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000416
417
Guido van Rossum79f25d91997-04-29 20:08:16 +0000418static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000419builtin_eval(PyObject *self, PyObject *args)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000420{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000421 PyObject *cmd;
422 PyObject *globals = Py_None, *locals = Py_None;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000423 char *str;
Tim Peters9fa96be2001-08-17 23:04:59 +0000424 PyCompilerFlags cf;
Guido van Rossum590baa41993-11-30 13:40:46 +0000425
Guido van Rossum79f25d91997-04-29 20:08:16 +0000426 if (!PyArg_ParseTuple(args, "O|O!O!:eval",
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000427 &cmd,
Guido van Rossum79f25d91997-04-29 20:08:16 +0000428 &PyDict_Type, &globals,
429 &PyDict_Type, &locals))
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000430 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000431 if (globals == Py_None) {
432 globals = PyEval_GetGlobals();
433 if (locals == Py_None)
434 locals = PyEval_GetLocals();
Guido van Rossum6135a871995-01-09 17:53:26 +0000435 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000436 else if (locals == Py_None)
Guido van Rossum6135a871995-01-09 17:53:26 +0000437 locals = globals;
Tim Peters9fa96be2001-08-17 23:04:59 +0000438
Guido van Rossum79f25d91997-04-29 20:08:16 +0000439 if (PyDict_GetItemString(globals, "__builtins__") == NULL) {
440 if (PyDict_SetItemString(globals, "__builtins__",
441 PyEval_GetBuiltins()) != 0)
Guido van Rossum6135a871995-01-09 17:53:26 +0000442 return NULL;
443 }
Tim Peters9fa96be2001-08-17 23:04:59 +0000444
Jeremy Hylton15c1c4f2001-07-30 21:50:55 +0000445 if (PyCode_Check(cmd)) {
Jeremy Hylton733c8932001-12-13 19:51:56 +0000446 if (PyCode_GetNumFree((PyCodeObject *)cmd) > 0) {
Jeremy Hylton15c1c4f2001-07-30 21:50:55 +0000447 PyErr_SetString(PyExc_TypeError,
448 "code object passed to eval() may not contain free variables");
449 return NULL;
450 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000451 return PyEval_EvalCode((PyCodeObject *) cmd, globals, locals);
Jeremy Hylton15c1c4f2001-07-30 21:50:55 +0000452 }
Tim Peters9fa96be2001-08-17 23:04:59 +0000453
Marc-André Lemburgd1ba4432000-09-19 21:04:18 +0000454 if (!PyString_Check(cmd) &&
455 !PyUnicode_Check(cmd)) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000456 PyErr_SetString(PyExc_TypeError,
Fred Drake661ea262000-10-24 19:57:45 +0000457 "eval() arg 1 must be a string or code object");
Guido van Rossum94390a41992-08-14 15:14:30 +0000458 return NULL;
459 }
Marc-André Lemburgd1ba4432000-09-19 21:04:18 +0000460 if (PyString_AsStringAndSize(cmd, &str, NULL))
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000461 return NULL;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000462 while (*str == ' ' || *str == '\t')
463 str++;
Tim Peters9fa96be2001-08-17 23:04:59 +0000464
465 cf.cf_flags = 0;
466 (void)PyEval_MergeCompilerFlags(&cf);
467 return PyRun_StringFlags(str, Py_eval_input, globals, locals, &cf);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000468}
469
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000470PyDoc_STRVAR(eval_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000471"eval(source[, globals[, locals]]) -> value\n\
472\n\
473Evaluate the source in the context of globals and locals.\n\
474The source may be a string representing a Python expression\n\
475or a code object as returned by compile().\n\
476The globals and locals are dictionaries, defaulting to the current\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000477globals and locals. If only globals is given, locals defaults to it.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000478
479
Guido van Rossum79f25d91997-04-29 20:08:16 +0000480static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000481builtin_execfile(PyObject *self, PyObject *args)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000482{
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000483 char *filename;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000484 PyObject *globals = Py_None, *locals = Py_None;
485 PyObject *res;
Guido van Rossumb3a639e2001-09-05 13:37:47 +0000486 FILE* fp = NULL;
Tim Peters5ba58662001-07-16 02:29:45 +0000487 PyCompilerFlags cf;
Martin v. Löwis6b3a2c42001-08-08 05:30:36 +0000488 int exists;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000489
Guido van Rossum79f25d91997-04-29 20:08:16 +0000490 if (!PyArg_ParseTuple(args, "s|O!O!:execfile",
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000491 &filename,
Guido van Rossum79f25d91997-04-29 20:08:16 +0000492 &PyDict_Type, &globals,
493 &PyDict_Type, &locals))
Guido van Rossum0f61f8a1992-02-25 18:55:05 +0000494 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000495 if (globals == Py_None) {
496 globals = PyEval_GetGlobals();
497 if (locals == Py_None)
498 locals = PyEval_GetLocals();
Guido van Rossum6135a871995-01-09 17:53:26 +0000499 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000500 else if (locals == Py_None)
Guido van Rossum6135a871995-01-09 17:53:26 +0000501 locals = globals;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000502 if (PyDict_GetItemString(globals, "__builtins__") == NULL) {
503 if (PyDict_SetItemString(globals, "__builtins__",
504 PyEval_GetBuiltins()) != 0)
Guido van Rossum6135a871995-01-09 17:53:26 +0000505 return NULL;
506 }
Martin v. Löwis6b3a2c42001-08-08 05:30:36 +0000507
508 exists = 0;
509 /* Test for existence or directory. */
Martin v. Löwis3484a182002-03-09 12:07:51 +0000510#if defined(PLAN9)
511 {
512 Dir *d;
513
514 if ((d = dirstat(filename))!=nil) {
515 if(d->mode & DMDIR)
516 werrstr("is a directory");
517 else
518 exists = 1;
519 free(d);
520 }
Martin v. Löwis6b3a2c42001-08-08 05:30:36 +0000521 }
Martin v. Löwis3484a182002-03-09 12:07:51 +0000522#elif defined(RISCOS)
Guido van Rossume2ae77b2001-10-24 20:42:55 +0000523 if (object_exists(filename)) {
524 if (isdir(filename))
525 errno = EISDIR;
526 else
527 exists = 1;
528 }
Martin v. Löwis3484a182002-03-09 12:07:51 +0000529#else /* standard Posix */
530 {
531 struct stat s;
532 if (stat(filename, &s) == 0) {
533 if (S_ISDIR(s.st_mode))
534# if defined(PY_OS2) && defined(PYCC_VACPP)
535 errno = EOS2ERR;
536# else
537 errno = EISDIR;
538# endif
539 else
540 exists = 1;
541 }
542 }
543#endif
Martin v. Löwis6b3a2c42001-08-08 05:30:36 +0000544
545 if (exists) {
546 Py_BEGIN_ALLOW_THREADS
Jack Jansen7b8c7542002-04-14 20:12:41 +0000547 fp = fopen(filename, "r" PY_STDIOTEXTMODE);
Martin v. Löwis6b3a2c42001-08-08 05:30:36 +0000548 Py_END_ALLOW_THREADS
549
550 if (fp == NULL) {
551 exists = 0;
552 }
553 }
554
555 if (!exists) {
Peter Schneider-Kamp4c013422002-08-27 16:58:00 +0000556 PyErr_SetFromErrnoWithFilename(PyExc_IOError, filename);
Guido van Rossum0f61f8a1992-02-25 18:55:05 +0000557 return NULL;
558 }
Tim Peters5ba58662001-07-16 02:29:45 +0000559 cf.cf_flags = 0;
560 if (PyEval_MergeCompilerFlags(&cf))
Tim Peters748b8bb2001-04-28 08:20:22 +0000561 res = PyRun_FileExFlags(fp, filename, Py_file_input, globals,
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000562 locals, 1, &cf);
Tim Peters5ba58662001-07-16 02:29:45 +0000563 else
Tim Peters748b8bb2001-04-28 08:20:22 +0000564 res = PyRun_FileEx(fp, filename, Py_file_input, globals,
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000565 locals, 1);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000566 return res;
Guido van Rossum0f61f8a1992-02-25 18:55:05 +0000567}
568
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000569PyDoc_STRVAR(execfile_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000570"execfile(filename[, globals[, locals]])\n\
571\n\
572Read and execute a Python script from a file.\n\
573The globals and locals are dictionaries, defaulting to the current\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000574globals and locals. If only globals is given, locals defaults to it.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000575
576
Guido van Rossum79f25d91997-04-29 20:08:16 +0000577static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000578builtin_getattr(PyObject *self, PyObject *args)
Guido van Rossum33894be1992-01-27 16:53:09 +0000579{
Guido van Rossum950ff291998-06-29 13:38:57 +0000580 PyObject *v, *result, *dflt = NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000581 PyObject *name;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000582
Marc-André Lemburg691270f2000-09-18 16:22:27 +0000583 if (!PyArg_ParseTuple(args, "OO|O:getattr", &v, &name, &dflt))
Guido van Rossum33894be1992-01-27 16:53:09 +0000584 return NULL;
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000585#ifdef Py_USING_UNICODE
Jeremy Hylton0eb11152001-07-30 22:39:31 +0000586 if (PyUnicode_Check(name)) {
587 name = _PyUnicode_AsDefaultEncodedString(name, NULL);
588 if (name == NULL)
589 return NULL;
590 }
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000591#endif
Jeremy Hylton0eb11152001-07-30 22:39:31 +0000592
593 if (!PyString_Check(name)) {
594 PyErr_SetString(PyExc_TypeError,
595 "attribute name must be string");
596 return NULL;
597 }
Guido van Rossum950ff291998-06-29 13:38:57 +0000598 result = PyObject_GetAttr(v, name);
Guido van Rossumd8923572001-10-16 21:31:32 +0000599 if (result == NULL && dflt != NULL &&
600 PyErr_ExceptionMatches(PyExc_AttributeError))
601 {
Guido van Rossum950ff291998-06-29 13:38:57 +0000602 PyErr_Clear();
603 Py_INCREF(dflt);
604 result = dflt;
605 }
606 return result;
Guido van Rossum9bfef441993-03-29 10:43:31 +0000607}
608
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000609PyDoc_STRVAR(getattr_doc,
Guido van Rossum950ff291998-06-29 13:38:57 +0000610"getattr(object, name[, default]) -> value\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000611\n\
Guido van Rossum950ff291998-06-29 13:38:57 +0000612Get a named attribute from an object; getattr(x, 'y') is equivalent to x.y.\n\
613When a default argument is given, it is returned when the attribute doesn't\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000614exist; without it, an exception is raised in that case.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000615
616
Guido van Rossum79f25d91997-04-29 20:08:16 +0000617static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000618builtin_globals(PyObject *self)
Guido van Rossum872537c1995-07-07 22:43:42 +0000619{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000620 PyObject *d;
Guido van Rossum872537c1995-07-07 22:43:42 +0000621
Guido van Rossum79f25d91997-04-29 20:08:16 +0000622 d = PyEval_GetGlobals();
623 Py_INCREF(d);
Guido van Rossum872537c1995-07-07 22:43:42 +0000624 return d;
625}
626
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000627PyDoc_STRVAR(globals_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000628"globals() -> dictionary\n\
629\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000630Return the dictionary containing the current scope's global variables.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000631
632
Guido van Rossum79f25d91997-04-29 20:08:16 +0000633static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000634builtin_hasattr(PyObject *self, PyObject *args)
Guido van Rossum9bfef441993-03-29 10:43:31 +0000635{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000636 PyObject *v;
637 PyObject *name;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000638
Marc-André Lemburg691270f2000-09-18 16:22:27 +0000639 if (!PyArg_ParseTuple(args, "OO:hasattr", &v, &name))
Guido van Rossum9bfef441993-03-29 10:43:31 +0000640 return NULL;
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000641#ifdef Py_USING_UNICODE
Jeremy Hylton302b54a2001-07-30 22:45:19 +0000642 if (PyUnicode_Check(name)) {
643 name = _PyUnicode_AsDefaultEncodedString(name, NULL);
644 if (name == NULL)
645 return NULL;
646 }
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000647#endif
Jeremy Hylton302b54a2001-07-30 22:45:19 +0000648
649 if (!PyString_Check(name)) {
650 PyErr_SetString(PyExc_TypeError,
651 "attribute name must be string");
652 return NULL;
653 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000654 v = PyObject_GetAttr(v, name);
Guido van Rossum9bfef441993-03-29 10:43:31 +0000655 if (v == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000656 PyErr_Clear();
Guido van Rossum09df08a1998-05-22 00:51:39 +0000657 Py_INCREF(Py_False);
658 return Py_False;
Guido van Rossum9bfef441993-03-29 10:43:31 +0000659 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000660 Py_DECREF(v);
Guido van Rossum09df08a1998-05-22 00:51:39 +0000661 Py_INCREF(Py_True);
662 return Py_True;
Guido van Rossum33894be1992-01-27 16:53:09 +0000663}
664
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000665PyDoc_STRVAR(hasattr_doc,
Guido van Rossum77f6a652002-04-03 22:41:51 +0000666"hasattr(object, name) -> bool\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000667\n\
668Return whether the object has an attribute with the given name.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000669(This is done by calling getattr(object, name) and catching exceptions.)");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000670
671
Guido van Rossum79f25d91997-04-29 20:08:16 +0000672static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000673builtin_id(PyObject *self, PyObject *v)
Guido van Rossum5b722181993-03-30 17:46:03 +0000674{
Guido van Rossum106f2da2000-06-28 21:12:25 +0000675 return PyLong_FromVoidPtr(v);
Guido van Rossum5b722181993-03-30 17:46:03 +0000676}
677
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000678PyDoc_STRVAR(id_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000679"id(object) -> integer\n\
680\n\
681Return the identity of an object. This is guaranteed to be unique among\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000682simultaneously existing objects. (Hint: it's the object's memory address.)");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000683
684
Guido van Rossum79f25d91997-04-29 20:08:16 +0000685static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000686builtin_map(PyObject *self, PyObject *args)
Guido van Rossum12d12c51993-10-26 17:58:25 +0000687{
688 typedef struct {
Tim Peters4e9afdc2001-05-03 23:54:49 +0000689 PyObject *it; /* the iterator object */
690 int saw_StopIteration; /* bool: did the iterator end? */
Guido van Rossum12d12c51993-10-26 17:58:25 +0000691 } sequence;
692
Guido van Rossum79f25d91997-04-29 20:08:16 +0000693 PyObject *func, *result;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000694 sequence *seqs = NULL, *sqp;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000695 int n, len;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000696 register int i, j;
697
Guido van Rossum79f25d91997-04-29 20:08:16 +0000698 n = PyTuple_Size(args);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000699 if (n < 2) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000700 PyErr_SetString(PyExc_TypeError,
701 "map() requires at least two args");
Guido van Rossum12d12c51993-10-26 17:58:25 +0000702 return NULL;
703 }
704
Guido van Rossum79f25d91997-04-29 20:08:16 +0000705 func = PyTuple_GetItem(args, 0);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000706 n--;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000707
Guido van Rossumfa4ac711998-07-10 17:37:30 +0000708 if (func == Py_None && n == 1) {
709 /* map(None, S) is the same as list(S). */
710 return PySequence_List(PyTuple_GetItem(args, 1));
711 }
712
Tim Peters4e9afdc2001-05-03 23:54:49 +0000713 /* Get space for sequence descriptors. Must NULL out the iterator
714 * pointers so that jumping to Fail_2 later doesn't see trash.
715 */
Guido van Rossum79f25d91997-04-29 20:08:16 +0000716 if ((seqs = PyMem_NEW(sequence, n)) == NULL) {
717 PyErr_NoMemory();
Tim Peters4e9afdc2001-05-03 23:54:49 +0000718 return NULL;
719 }
720 for (i = 0; i < n; ++i) {
721 seqs[i].it = (PyObject*)NULL;
722 seqs[i].saw_StopIteration = 0;
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000723 }
Guido van Rossum12d12c51993-10-26 17:58:25 +0000724
Tim Peters4e9afdc2001-05-03 23:54:49 +0000725 /* Do a first pass to obtain iterators for the arguments, and set len
726 * to the largest of their lengths.
Tim Peters748b8bb2001-04-28 08:20:22 +0000727 */
Tim Peters4e9afdc2001-05-03 23:54:49 +0000728 len = 0;
729 for (i = 0, sqp = seqs; i < n; ++i, ++sqp) {
730 PyObject *curseq;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000731 int curlen;
Guido van Rossumad991772001-01-12 16:03:05 +0000732
Tim Peters4e9afdc2001-05-03 23:54:49 +0000733 /* Get iterator. */
734 curseq = PyTuple_GetItem(args, i+1);
735 sqp->it = PyObject_GetIter(curseq);
736 if (sqp->it == NULL) {
Guido van Rossum12d12c51993-10-26 17:58:25 +0000737 static char errmsg[] =
Tim Peters4e9afdc2001-05-03 23:54:49 +0000738 "argument %d to map() must support iteration";
Guido van Rossum15e33a41997-04-30 19:00:27 +0000739 char errbuf[sizeof(errmsg) + 25];
Jeremy Hylton518ab1c2001-11-28 20:42:20 +0000740 PyOS_snprintf(errbuf, sizeof(errbuf), errmsg, i+2);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000741 PyErr_SetString(PyExc_TypeError, errbuf);
Guido van Rossum12d12c51993-10-26 17:58:25 +0000742 goto Fail_2;
743 }
744
Tim Peters4e9afdc2001-05-03 23:54:49 +0000745 /* Update len. */
746 curlen = -1; /* unknown */
747 if (PySequence_Check(curseq) &&
748 curseq->ob_type->tp_as_sequence->sq_length) {
749 curlen = PySequence_Size(curseq);
750 if (curlen < 0)
751 PyErr_Clear();
752 }
Tim Peters748b8bb2001-04-28 08:20:22 +0000753 if (curlen < 0)
Tim Peters4e9afdc2001-05-03 23:54:49 +0000754 curlen = 8; /* arbitrary */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000755 if (curlen > len)
756 len = curlen;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000757 }
758
Tim Peters4e9afdc2001-05-03 23:54:49 +0000759 /* Get space for the result list. */
Guido van Rossum79f25d91997-04-29 20:08:16 +0000760 if ((result = (PyObject *) PyList_New(len)) == NULL)
Guido van Rossum12d12c51993-10-26 17:58:25 +0000761 goto Fail_2;
762
Tim Peters4e9afdc2001-05-03 23:54:49 +0000763 /* Iterate over the sequences until all have stopped. */
Guido van Rossum2d951851994-08-29 12:52:16 +0000764 for (i = 0; ; ++i) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000765 PyObject *alist, *item=NULL, *value;
Tim Peters4e9afdc2001-05-03 23:54:49 +0000766 int numactive = 0;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000767
Guido van Rossum79f25d91997-04-29 20:08:16 +0000768 if (func == Py_None && n == 1)
Guido van Rossum32120311995-07-10 13:52:21 +0000769 alist = NULL;
Tim Peters4e9afdc2001-05-03 23:54:49 +0000770 else if ((alist = PyTuple_New(n)) == NULL)
771 goto Fail_1;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000772
773 for (j = 0, sqp = seqs; j < n; ++j, ++sqp) {
Tim Peters4e9afdc2001-05-03 23:54:49 +0000774 if (sqp->saw_StopIteration) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000775 Py_INCREF(Py_None);
776 item = Py_None;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000777 }
Guido van Rossum12d12c51993-10-26 17:58:25 +0000778 else {
Tim Peters4e9afdc2001-05-03 23:54:49 +0000779 item = PyIter_Next(sqp->it);
780 if (item)
781 ++numactive;
782 else {
Tim Peters4e9afdc2001-05-03 23:54:49 +0000783 if (PyErr_Occurred()) {
Tim Petersf4848da2001-05-05 00:14:56 +0000784 Py_XDECREF(alist);
785 goto Fail_1;
Guido van Rossum2d951851994-08-29 12:52:16 +0000786 }
Tim Peters4e9afdc2001-05-03 23:54:49 +0000787 Py_INCREF(Py_None);
788 item = Py_None;
789 sqp->saw_StopIteration = 1;
Guido van Rossum2d951851994-08-29 12:52:16 +0000790 }
Guido van Rossum12d12c51993-10-26 17:58:25 +0000791 }
Tim Peters4e9afdc2001-05-03 23:54:49 +0000792 if (alist)
793 PyTuple_SET_ITEM(alist, j, item);
794 else
Guido van Rossum2d951851994-08-29 12:52:16 +0000795 break;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000796 }
797
Guido van Rossum32120311995-07-10 13:52:21 +0000798 if (!alist)
799 alist = item;
Guido van Rossum2d951851994-08-29 12:52:16 +0000800
Tim Peters4e9afdc2001-05-03 23:54:49 +0000801 if (numactive == 0) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000802 Py_DECREF(alist);
Guido van Rossum2d951851994-08-29 12:52:16 +0000803 break;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000804 }
Guido van Rossum2d951851994-08-29 12:52:16 +0000805
Guido van Rossum79f25d91997-04-29 20:08:16 +0000806 if (func == Py_None)
Guido van Rossum32120311995-07-10 13:52:21 +0000807 value = alist;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000808 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000809 value = PyEval_CallObject(func, alist);
810 Py_DECREF(alist);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000811 if (value == NULL)
812 goto Fail_1;
Guido van Rossum2d951851994-08-29 12:52:16 +0000813 }
814 if (i >= len) {
Barry Warsawfa77e091999-01-28 18:49:12 +0000815 int status = PyList_Append(result, value);
Barry Warsaw21332871999-01-28 04:21:35 +0000816 Py_DECREF(value);
Barry Warsawfa77e091999-01-28 18:49:12 +0000817 if (status < 0)
818 goto Fail_1;
Guido van Rossum2d951851994-08-29 12:52:16 +0000819 }
Tim Peters4e9afdc2001-05-03 23:54:49 +0000820 else if (PyList_SetItem(result, i, value) < 0)
821 goto Fail_1;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000822 }
823
Guido van Rossumfa4ac711998-07-10 17:37:30 +0000824 if (i < len && PyList_SetSlice(result, i, len, NULL) < 0)
825 goto Fail_1;
826
Tim Peters4e9afdc2001-05-03 23:54:49 +0000827 goto Succeed;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000828
Guido van Rossum12d12c51993-10-26 17:58:25 +0000829Fail_1:
Guido van Rossum79f25d91997-04-29 20:08:16 +0000830 Py_DECREF(result);
Guido van Rossum12d12c51993-10-26 17:58:25 +0000831Fail_2:
Tim Peters4e9afdc2001-05-03 23:54:49 +0000832 result = NULL;
833Succeed:
834 assert(seqs);
835 for (i = 0; i < n; ++i)
836 Py_XDECREF(seqs[i].it);
837 PyMem_DEL(seqs);
838 return result;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000839}
840
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000841PyDoc_STRVAR(map_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000842"map(function, sequence[, sequence, ...]) -> list\n\
843\n\
844Return a list of the results of applying the function to the items of\n\
845the argument sequence(s). If more than one sequence is given, the\n\
846function is called with an argument list consisting of the corresponding\n\
847item of each sequence, substituting None for missing values when not all\n\
848sequences have the same length. If the function is None, return a list of\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000849the items of the sequence (or a list of tuples if more than one sequence).");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000850
851
Guido van Rossum79f25d91997-04-29 20:08:16 +0000852static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000853builtin_setattr(PyObject *self, PyObject *args)
Guido van Rossum33894be1992-01-27 16:53:09 +0000854{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000855 PyObject *v;
856 PyObject *name;
857 PyObject *value;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000858
Marc-André Lemburg691270f2000-09-18 16:22:27 +0000859 if (!PyArg_ParseTuple(args, "OOO:setattr", &v, &name, &value))
Guido van Rossum33894be1992-01-27 16:53:09 +0000860 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000861 if (PyObject_SetAttr(v, name, value) != 0)
Guido van Rossum33894be1992-01-27 16:53:09 +0000862 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000863 Py_INCREF(Py_None);
864 return Py_None;
Guido van Rossum33894be1992-01-27 16:53:09 +0000865}
866
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000867PyDoc_STRVAR(setattr_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000868"setattr(object, name, value)\n\
869\n\
870Set a named attribute on an object; setattr(x, 'y', v) is equivalent to\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000871``x.y = v''.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000872
873
Guido van Rossum79f25d91997-04-29 20:08:16 +0000874static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000875builtin_delattr(PyObject *self, PyObject *args)
Guido van Rossum14144fc1994-08-29 12:53:40 +0000876{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000877 PyObject *v;
878 PyObject *name;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000879
Marc-André Lemburg691270f2000-09-18 16:22:27 +0000880 if (!PyArg_ParseTuple(args, "OO:delattr", &v, &name))
Guido van Rossum14144fc1994-08-29 12:53:40 +0000881 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000882 if (PyObject_SetAttr(v, name, (PyObject *)NULL) != 0)
Guido van Rossum14144fc1994-08-29 12:53:40 +0000883 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000884 Py_INCREF(Py_None);
885 return Py_None;
Guido van Rossum14144fc1994-08-29 12:53:40 +0000886}
887
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000888PyDoc_STRVAR(delattr_doc,
Guido van Rossumdf12a591998-11-23 22:13:04 +0000889"delattr(object, name)\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000890\n\
891Delete a named attribute on an object; delattr(x, 'y') is equivalent to\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000892``del x.y''.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000893
894
Guido van Rossum79f25d91997-04-29 20:08:16 +0000895static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000896builtin_hash(PyObject *self, PyObject *v)
Guido van Rossum9bfef441993-03-29 10:43:31 +0000897{
Guido van Rossum9bfef441993-03-29 10:43:31 +0000898 long x;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000899
Guido van Rossum79f25d91997-04-29 20:08:16 +0000900 x = PyObject_Hash(v);
Guido van Rossum9bfef441993-03-29 10:43:31 +0000901 if (x == -1)
902 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000903 return PyInt_FromLong(x);
Guido van Rossum9bfef441993-03-29 10:43:31 +0000904}
905
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000906PyDoc_STRVAR(hash_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000907"hash(object) -> integer\n\
908\n\
909Return a hash value for the object. Two objects with the same value have\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000910the same hash value. The reverse is not necessarily true, but likely.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000911
912
Guido van Rossum79f25d91997-04-29 20:08:16 +0000913static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000914builtin_hex(PyObject *self, PyObject *v)
Guido van Rossum006bcd41991-10-24 14:54:44 +0000915{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000916 PyNumberMethods *nb;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000917
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000918 if ((nb = v->ob_type->tp_as_number) == NULL ||
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000919 nb->nb_hex == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000920 PyErr_SetString(PyExc_TypeError,
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000921 "hex() argument can't be converted to hex");
922 return NULL;
Guido van Rossum006bcd41991-10-24 14:54:44 +0000923 }
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000924 return (*nb->nb_hex)(v);
Guido van Rossum006bcd41991-10-24 14:54:44 +0000925}
926
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000927PyDoc_STRVAR(hex_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000928"hex(number) -> string\n\
929\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000930Return the hexadecimal representation of an integer or long integer.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000931
932
Tim Petersdbd9ba62000-07-09 03:09:57 +0000933static PyObject *builtin_raw_input(PyObject *, PyObject *);
Guido van Rossum3165fe61992-09-25 21:59:05 +0000934
Guido van Rossum79f25d91997-04-29 20:08:16 +0000935static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000936builtin_input(PyObject *self, PyObject *args)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000937{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000938 PyObject *line;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000939 char *str;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000940 PyObject *res;
941 PyObject *globals, *locals;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000942
943 line = builtin_raw_input(self, args);
Guido van Rossum3165fe61992-09-25 21:59:05 +0000944 if (line == NULL)
945 return line;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000946 if (!PyArg_Parse(line, "s;embedded '\\0' in input line", &str))
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000947 return NULL;
948 while (*str == ' ' || *str == '\t')
949 str++;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000950 globals = PyEval_GetGlobals();
951 locals = PyEval_GetLocals();
952 if (PyDict_GetItemString(globals, "__builtins__") == NULL) {
953 if (PyDict_SetItemString(globals, "__builtins__",
954 PyEval_GetBuiltins()) != 0)
Guido van Rossum6135a871995-01-09 17:53:26 +0000955 return NULL;
956 }
Guido van Rossumb05a5c71997-05-07 17:46:13 +0000957 res = PyRun_String(str, Py_eval_input, globals, locals);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000958 Py_DECREF(line);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000959 return res;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000960}
961
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000962PyDoc_STRVAR(input_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000963"input([prompt]) -> value\n\
964\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000965Equivalent to eval(raw_input(prompt)).");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000966
967
Guido van Rossume8811f81997-02-14 15:48:05 +0000968static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000969builtin_intern(PyObject *self, PyObject *args)
Guido van Rossume8811f81997-02-14 15:48:05 +0000970{
971 PyObject *s;
Guido van Rossum43713e52000-02-29 13:59:29 +0000972 if (!PyArg_ParseTuple(args, "S:intern", &s))
Guido van Rossume8811f81997-02-14 15:48:05 +0000973 return NULL;
974 Py_INCREF(s);
975 PyString_InternInPlace(&s);
976 return s;
977}
978
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000979PyDoc_STRVAR(intern_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000980"intern(string) -> string\n\
981\n\
982``Intern'' the given string. This enters the string in the (global)\n\
983table of interned strings whose purpose is to speed up dictionary lookups.\n\
984Return the string itself or the previously interned string object with the\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000985same value.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000986
987
Guido van Rossum79f25d91997-04-29 20:08:16 +0000988static PyObject *
Guido van Rossum59d1d2b2001-04-20 19:13:02 +0000989builtin_iter(PyObject *self, PyObject *args)
990{
991 PyObject *v, *w = NULL;
992
993 if (!PyArg_ParseTuple(args, "O|O:iter", &v, &w))
994 return NULL;
995 if (w == NULL)
996 return PyObject_GetIter(v);
997 if (!PyCallable_Check(v)) {
998 PyErr_SetString(PyExc_TypeError,
999 "iter(v, w): v must be callable");
1000 return NULL;
1001 }
1002 return PyCallIter_New(v, w);
1003}
1004
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001005PyDoc_STRVAR(iter_doc,
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001006"iter(collection) -> iterator\n\
1007iter(callable, sentinel) -> iterator\n\
1008\n\
1009Get an iterator from an object. In the first form, the argument must\n\
1010supply its own iterator, or be a sequence.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001011In the second form, the callable is called until it returns the sentinel.");
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001012
1013
1014static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001015builtin_len(PyObject *self, PyObject *v)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001016{
Guido van Rossum8ea9f4d1998-06-29 22:26:50 +00001017 long res;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001018
Jeremy Hylton03657cf2000-07-12 13:05:33 +00001019 res = PyObject_Size(v);
Guido van Rossum8ea9f4d1998-06-29 22:26:50 +00001020 if (res < 0 && PyErr_Occurred())
1021 return NULL;
1022 return PyInt_FromLong(res);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001023}
1024
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001025PyDoc_STRVAR(len_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001026"len(object) -> integer\n\
1027\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001028Return the number of items of a sequence or mapping.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001029
1030
Guido van Rossum79f25d91997-04-29 20:08:16 +00001031static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001032builtin_locals(PyObject *self)
Guido van Rossum872537c1995-07-07 22:43:42 +00001033{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001034 PyObject *d;
Guido van Rossum872537c1995-07-07 22:43:42 +00001035
Guido van Rossum79f25d91997-04-29 20:08:16 +00001036 d = PyEval_GetLocals();
1037 Py_INCREF(d);
Guido van Rossum872537c1995-07-07 22:43:42 +00001038 return d;
1039}
1040
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001041PyDoc_STRVAR(locals_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001042"locals() -> dictionary\n\
1043\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001044Return the dictionary containing the current scope's local variables.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001045
1046
Guido van Rossum79f25d91997-04-29 20:08:16 +00001047static PyObject *
Guido van Rossum53451b32001-01-17 15:47:24 +00001048min_max(PyObject *args, int op)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001049{
Tim Petersc3074532001-05-03 07:00:32 +00001050 PyObject *v, *w, *x, *it;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001051
Guido van Rossum79f25d91997-04-29 20:08:16 +00001052 if (PyTuple_Size(args) > 1)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001053 v = args;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001054 else if (!PyArg_ParseTuple(args, "O:min/max", &v))
Guido van Rossum3f5da241990-12-20 15:06:42 +00001055 return NULL;
Tim Peters67d687a2002-04-29 21:27:32 +00001056
Tim Petersc3074532001-05-03 07:00:32 +00001057 it = PyObject_GetIter(v);
1058 if (it == NULL)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001059 return NULL;
Tim Petersc3074532001-05-03 07:00:32 +00001060
1061 w = NULL; /* the result */
Tim Petersf4848da2001-05-05 00:14:56 +00001062 for (;;) {
Tim Petersc3074532001-05-03 07:00:32 +00001063 x = PyIter_Next(it);
Guido van Rossum2d951851994-08-29 12:52:16 +00001064 if (x == NULL) {
Tim Petersc3074532001-05-03 07:00:32 +00001065 if (PyErr_Occurred()) {
Tim Petersf4848da2001-05-05 00:14:56 +00001066 Py_XDECREF(w);
1067 Py_DECREF(it);
1068 return NULL;
Guido van Rossum2d951851994-08-29 12:52:16 +00001069 }
Tim Petersc3074532001-05-03 07:00:32 +00001070 break;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001071 }
Tim Petersc3074532001-05-03 07:00:32 +00001072
Guido van Rossum2d951851994-08-29 12:52:16 +00001073 if (w == NULL)
1074 w = x;
1075 else {
Guido van Rossum53451b32001-01-17 15:47:24 +00001076 int cmp = PyObject_RichCompareBool(x, w, op);
1077 if (cmp > 0) {
1078 Py_DECREF(w);
1079 w = x;
1080 }
1081 else if (cmp < 0) {
Guido van Rossumc8b6df91997-05-23 00:06:51 +00001082 Py_DECREF(x);
Tim Petersc3074532001-05-03 07:00:32 +00001083 Py_DECREF(w);
1084 Py_DECREF(it);
Guido van Rossumc8b6df91997-05-23 00:06:51 +00001085 return NULL;
1086 }
Guido van Rossum2d951851994-08-29 12:52:16 +00001087 else
Guido van Rossum79f25d91997-04-29 20:08:16 +00001088 Py_DECREF(x);
Guido van Rossum2d951851994-08-29 12:52:16 +00001089 }
Guido van Rossum3f5da241990-12-20 15:06:42 +00001090 }
Guido van Rossum2d951851994-08-29 12:52:16 +00001091 if (w == NULL)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001092 PyErr_SetString(PyExc_ValueError,
Fred Drake661ea262000-10-24 19:57:45 +00001093 "min() or max() arg is an empty sequence");
Tim Petersc3074532001-05-03 07:00:32 +00001094 Py_DECREF(it);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001095 return w;
1096}
1097
Guido van Rossum79f25d91997-04-29 20:08:16 +00001098static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001099builtin_min(PyObject *self, PyObject *v)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001100{
Guido van Rossum53451b32001-01-17 15:47:24 +00001101 return min_max(v, Py_LT);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001102}
1103
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001104PyDoc_STRVAR(min_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001105"min(sequence) -> value\n\
1106min(a, b, c, ...) -> value\n\
1107\n\
1108With a single sequence argument, return its smallest item.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001109With two or more arguments, return the smallest argument.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001110
1111
Guido van Rossum79f25d91997-04-29 20:08:16 +00001112static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001113builtin_max(PyObject *self, PyObject *v)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001114{
Guido van Rossum53451b32001-01-17 15:47:24 +00001115 return min_max(v, Py_GT);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001116}
1117
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001118PyDoc_STRVAR(max_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001119"max(sequence) -> value\n\
1120max(a, b, c, ...) -> value\n\
1121\n\
1122With a single sequence argument, return its largest item.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001123With two or more arguments, return the largest argument.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001124
1125
Guido van Rossum79f25d91997-04-29 20:08:16 +00001126static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001127builtin_oct(PyObject *self, PyObject *v)
Guido van Rossum006bcd41991-10-24 14:54:44 +00001128{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001129 PyNumberMethods *nb;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001130
Guido van Rossum1899c2e1992-09-12 11:09:23 +00001131 if (v == NULL || (nb = v->ob_type->tp_as_number) == NULL ||
1132 nb->nb_oct == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001133 PyErr_SetString(PyExc_TypeError,
Guido van Rossum1899c2e1992-09-12 11:09:23 +00001134 "oct() argument can't be converted to oct");
1135 return NULL;
Guido van Rossum006bcd41991-10-24 14:54:44 +00001136 }
Guido van Rossum1899c2e1992-09-12 11:09:23 +00001137 return (*nb->nb_oct)(v);
Guido van Rossum006bcd41991-10-24 14:54:44 +00001138}
1139
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001140PyDoc_STRVAR(oct_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001141"oct(number) -> string\n\
1142\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001143Return the octal representation of an integer or long integer.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001144
1145
Guido van Rossum79f25d91997-04-29 20:08:16 +00001146static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001147builtin_ord(PyObject *self, PyObject* obj)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001148{
Guido van Rossum09095f32000-03-10 23:00:52 +00001149 long ord;
Jeremy Hylton394b54d2000-04-12 21:19:47 +00001150 int size;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001151
Jeremy Hylton394b54d2000-04-12 21:19:47 +00001152 if (PyString_Check(obj)) {
1153 size = PyString_GET_SIZE(obj);
Andrew M. Kuchling34c20cf2000-12-20 14:36:56 +00001154 if (size == 1) {
Jeremy Hylton394b54d2000-04-12 21:19:47 +00001155 ord = (long)((unsigned char)*PyString_AS_STRING(obj));
Andrew M. Kuchling34c20cf2000-12-20 14:36:56 +00001156 return PyInt_FromLong(ord);
1157 }
Martin v. Löwis339d0f72001-08-17 18:39:25 +00001158#ifdef Py_USING_UNICODE
Jeremy Hylton394b54d2000-04-12 21:19:47 +00001159 } else if (PyUnicode_Check(obj)) {
1160 size = PyUnicode_GET_SIZE(obj);
Andrew M. Kuchling34c20cf2000-12-20 14:36:56 +00001161 if (size == 1) {
Jeremy Hylton394b54d2000-04-12 21:19:47 +00001162 ord = (long)*PyUnicode_AS_UNICODE(obj);
Andrew M. Kuchling34c20cf2000-12-20 14:36:56 +00001163 return PyInt_FromLong(ord);
1164 }
Martin v. Löwis339d0f72001-08-17 18:39:25 +00001165#endif
Jeremy Hylton394b54d2000-04-12 21:19:47 +00001166 } else {
1167 PyErr_Format(PyExc_TypeError,
Andrew M. Kuchlingf07aad12000-12-23 14:11:28 +00001168 "ord() expected string of length 1, but " \
Jeremy Hylton394b54d2000-04-12 21:19:47 +00001169 "%.200s found", obj->ob_type->tp_name);
Guido van Rossum09095f32000-03-10 23:00:52 +00001170 return NULL;
1171 }
1172
Guido van Rossumad991772001-01-12 16:03:05 +00001173 PyErr_Format(PyExc_TypeError,
Andrew M. Kuchlingf07aad12000-12-23 14:11:28 +00001174 "ord() expected a character, "
1175 "but string of length %d found",
Jeremy Hylton394b54d2000-04-12 21:19:47 +00001176 size);
1177 return NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001178}
1179
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001180PyDoc_STRVAR(ord_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001181"ord(c) -> integer\n\
1182\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001183Return the integer ordinal of a one-character string.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001184
1185
Guido van Rossum79f25d91997-04-29 20:08:16 +00001186static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001187builtin_pow(PyObject *self, PyObject *args)
Guido van Rossum6a00cd81995-01-07 12:39:01 +00001188{
Guido van Rossum09df08a1998-05-22 00:51:39 +00001189 PyObject *v, *w, *z = Py_None;
Guido van Rossum6a00cd81995-01-07 12:39:01 +00001190
Guido van Rossum79f25d91997-04-29 20:08:16 +00001191 if (!PyArg_ParseTuple(args, "OO|O:pow", &v, &w, &z))
Guido van Rossum6a00cd81995-01-07 12:39:01 +00001192 return NULL;
Guido van Rossum09df08a1998-05-22 00:51:39 +00001193 return PyNumber_Power(v, w, z);
Guido van Rossumd4905451991-05-05 20:00:36 +00001194}
1195
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001196PyDoc_STRVAR(pow_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001197"pow(x, y[, z]) -> number\n\
1198\n\
1199With two arguments, equivalent to x**y. With three arguments,\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001200equivalent to (x**y) % z, but may be more efficient (e.g. for longs).");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001201
1202
Guido van Rossum124eff01999-02-23 16:11:01 +00001203/* Return number of items in range/xrange (lo, hi, step). step > 0
1204 * required. Return a value < 0 if & only if the true value is too
1205 * large to fit in a signed long.
1206 */
1207static long
Thomas Wouterse28c2962000-07-23 22:21:32 +00001208get_len_of_range(long lo, long hi, long step)
Guido van Rossum124eff01999-02-23 16:11:01 +00001209{
1210 /* -------------------------------------------------------------
1211 If lo >= hi, the range is empty.
1212 Else if n values are in the range, the last one is
1213 lo + (n-1)*step, which must be <= hi-1. Rearranging,
1214 n <= (hi - lo - 1)/step + 1, so taking the floor of the RHS gives
1215 the proper value. Since lo < hi in this case, hi-lo-1 >= 0, so
1216 the RHS is non-negative and so truncation is the same as the
1217 floor. Letting M be the largest positive long, the worst case
1218 for the RHS numerator is hi=M, lo=-M-1, and then
1219 hi-lo-1 = M-(-M-1)-1 = 2*M. Therefore unsigned long has enough
1220 precision to compute the RHS exactly.
1221 ---------------------------------------------------------------*/
1222 long n = 0;
1223 if (lo < hi) {
1224 unsigned long uhi = (unsigned long)hi;
1225 unsigned long ulo = (unsigned long)lo;
1226 unsigned long diff = uhi - ulo - 1;
1227 n = (long)(diff / (unsigned long)step + 1);
1228 }
1229 return n;
1230}
1231
Guido van Rossum79f25d91997-04-29 20:08:16 +00001232static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001233builtin_range(PyObject *self, PyObject *args)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001234{
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001235 long ilow = 0, ihigh = 0, istep = 1;
Guido van Rossum124eff01999-02-23 16:11:01 +00001236 long bign;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001237 int i, n;
Guido van Rossum124eff01999-02-23 16:11:01 +00001238
Guido van Rossum79f25d91997-04-29 20:08:16 +00001239 PyObject *v;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001240
Guido van Rossum79f25d91997-04-29 20:08:16 +00001241 if (PyTuple_Size(args) <= 1) {
1242 if (!PyArg_ParseTuple(args,
Guido van Rossum0865dd91995-01-17 16:30:22 +00001243 "l;range() requires 1-3 int arguments",
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001244 &ihigh))
1245 return NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001246 }
1247 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001248 if (!PyArg_ParseTuple(args,
Guido van Rossum0865dd91995-01-17 16:30:22 +00001249 "ll|l;range() requires 1-3 int arguments",
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001250 &ilow, &ihigh, &istep))
Guido van Rossum3f5da241990-12-20 15:06:42 +00001251 return NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001252 }
1253 if (istep == 0) {
Fred Drake661ea262000-10-24 19:57:45 +00001254 PyErr_SetString(PyExc_ValueError, "range() arg 3 must not be zero");
Guido van Rossum3f5da241990-12-20 15:06:42 +00001255 return NULL;
1256 }
Guido van Rossum124eff01999-02-23 16:11:01 +00001257 if (istep > 0)
1258 bign = get_len_of_range(ilow, ihigh, istep);
1259 else
1260 bign = get_len_of_range(ihigh, ilow, -istep);
1261 n = (int)bign;
1262 if (bign < 0 || (long)n != bign) {
Guido van Rossume23cde21999-01-12 05:07:47 +00001263 PyErr_SetString(PyExc_OverflowError,
Fred Drake661ea262000-10-24 19:57:45 +00001264 "range() result has too many items");
Guido van Rossume23cde21999-01-12 05:07:47 +00001265 return NULL;
1266 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00001267 v = PyList_New(n);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001268 if (v == NULL)
1269 return NULL;
1270 for (i = 0; i < n; i++) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001271 PyObject *w = PyInt_FromLong(ilow);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001272 if (w == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001273 Py_DECREF(v);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001274 return NULL;
1275 }
Guido van Rossuma937d141998-04-24 18:22:02 +00001276 PyList_SET_ITEM(v, i, w);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001277 ilow += istep;
1278 }
1279 return v;
1280}
1281
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001282PyDoc_STRVAR(range_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001283"range([start,] stop[, step]) -> list of integers\n\
1284\n\
1285Return a list containing an arithmetic progression of integers.\n\
1286range(i, j) returns [i, i+1, i+2, ..., j-1]; start (!) defaults to 0.\n\
1287When step is given, it specifies the increment (or decrement).\n\
1288For example, range(4) returns [0, 1, 2, 3]. The end point is omitted!\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001289These are exactly the valid indices for a list of 4 elements.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001290
1291
Guido van Rossum79f25d91997-04-29 20:08:16 +00001292static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001293builtin_raw_input(PyObject *self, PyObject *args)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001294{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001295 PyObject *v = NULL;
Martin v. Löwis31d2df52002-08-14 15:46:02 +00001296 PyObject *fin = PySys_GetObject("stdin");
1297 PyObject *fout = PySys_GetObject("stdout");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001298
Guido van Rossum79f25d91997-04-29 20:08:16 +00001299 if (!PyArg_ParseTuple(args, "|O:[raw_]input", &v))
Guido van Rossum3165fe61992-09-25 21:59:05 +00001300 return NULL;
Martin v. Löwis31d2df52002-08-14 15:46:02 +00001301
1302 if (fin == NULL) {
1303 PyErr_SetString(PyExc_RuntimeError, "lost sys.stdin");
1304 return NULL;
1305 }
1306 if (fout == NULL) {
1307 PyErr_SetString(PyExc_RuntimeError, "lost sys.stdout");
1308 return NULL;
1309 }
1310 if (PyFile_SoftSpace(fout, 0)) {
1311 if (PyFile_WriteString(" ", fout) != 0)
1312 return NULL;
1313 }
Martin v. Löwis566f6af2002-10-26 14:39:10 +00001314 if (PyFile_Check (fin) && PyFile_Check (fout)
1315 && isatty(fileno(PyFile_AsFile(fin)))
1316 && isatty(fileno(PyFile_AsFile(fout)))) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001317 PyObject *po;
Guido van Rossum872537c1995-07-07 22:43:42 +00001318 char *prompt;
1319 char *s;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001320 PyObject *result;
Guido van Rossum872537c1995-07-07 22:43:42 +00001321 if (v != NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001322 po = PyObject_Str(v);
Guido van Rossum872537c1995-07-07 22:43:42 +00001323 if (po == NULL)
1324 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001325 prompt = PyString_AsString(po);
Guido van Rossumd9b52081998-06-26 18:25:38 +00001326 if (prompt == NULL)
1327 return NULL;
Guido van Rossum872537c1995-07-07 22:43:42 +00001328 }
1329 else {
1330 po = NULL;
1331 prompt = "";
1332 }
Martin v. Löwis566f6af2002-10-26 14:39:10 +00001333 s = PyOS_Readline(PyFile_AsFile (fin), PyFile_AsFile (fout),
1334 prompt);
Guido van Rossum79f25d91997-04-29 20:08:16 +00001335 Py_XDECREF(po);
Guido van Rossum872537c1995-07-07 22:43:42 +00001336 if (s == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001337 PyErr_SetNone(PyExc_KeyboardInterrupt);
Guido van Rossum872537c1995-07-07 22:43:42 +00001338 return NULL;
1339 }
1340 if (*s == '\0') {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001341 PyErr_SetNone(PyExc_EOFError);
Guido van Rossum872537c1995-07-07 22:43:42 +00001342 result = NULL;
1343 }
1344 else { /* strip trailing '\n' */
Guido van Rossum106f2da2000-06-28 21:12:25 +00001345 size_t len = strlen(s);
1346 if (len > INT_MAX) {
Martin v. Löwis31d2df52002-08-14 15:46:02 +00001347 PyErr_SetString(PyExc_OverflowError,
1348 "input too long");
Guido van Rossum106f2da2000-06-28 21:12:25 +00001349 result = NULL;
1350 }
1351 else {
Martin v. Löwis31d2df52002-08-14 15:46:02 +00001352 result = PyString_FromStringAndSize(s,
1353 (int)(len-1));
Guido van Rossum106f2da2000-06-28 21:12:25 +00001354 }
Guido van Rossum872537c1995-07-07 22:43:42 +00001355 }
Guido van Rossumb18618d2000-05-03 23:44:39 +00001356 PyMem_FREE(s);
Guido van Rossum872537c1995-07-07 22:43:42 +00001357 return result;
1358 }
Guido van Rossum90933611991-06-07 16:10:43 +00001359 if (v != NULL) {
Martin v. Löwis31d2df52002-08-14 15:46:02 +00001360 if (PyFile_WriteObject(v, fout, Py_PRINT_RAW) != 0)
Guido van Rossum90933611991-06-07 16:10:43 +00001361 return NULL;
1362 }
Martin v. Löwis31d2df52002-08-14 15:46:02 +00001363 return PyFile_GetLine(fin, -1);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001364}
1365
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001366PyDoc_STRVAR(raw_input_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001367"raw_input([prompt]) -> string\n\
1368\n\
1369Read a string from standard input. The trailing newline is stripped.\n\
1370If the user hits EOF (Unix: Ctl-D, Windows: Ctl-Z+Return), raise EOFError.\n\
1371On Unix, GNU readline is used if enabled. The prompt string, if given,\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001372is printed without a trailing newline before reading.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001373
1374
Guido van Rossum79f25d91997-04-29 20:08:16 +00001375static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001376builtin_reduce(PyObject *self, PyObject *args)
Guido van Rossum12d12c51993-10-26 17:58:25 +00001377{
Tim Peters15d81ef2001-05-04 04:39:21 +00001378 PyObject *seq, *func, *result = NULL, *it;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001379
Guido van Rossum79f25d91997-04-29 20:08:16 +00001380 if (!PyArg_ParseTuple(args, "OO|O:reduce", &func, &seq, &result))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001381 return NULL;
1382 if (result != NULL)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001383 Py_INCREF(result);
Guido van Rossum12d12c51993-10-26 17:58:25 +00001384
Tim Peters15d81ef2001-05-04 04:39:21 +00001385 it = PyObject_GetIter(seq);
1386 if (it == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001387 PyErr_SetString(PyExc_TypeError,
Tim Peters15d81ef2001-05-04 04:39:21 +00001388 "reduce() arg 2 must support iteration");
1389 Py_XDECREF(result);
Guido van Rossum12d12c51993-10-26 17:58:25 +00001390 return NULL;
1391 }
1392
Guido van Rossum79f25d91997-04-29 20:08:16 +00001393 if ((args = PyTuple_New(2)) == NULL)
Guido van Rossum2d951851994-08-29 12:52:16 +00001394 goto Fail;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001395
Tim Peters15d81ef2001-05-04 04:39:21 +00001396 for (;;) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001397 PyObject *op2;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001398
1399 if (args->ob_refcnt > 1) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001400 Py_DECREF(args);
1401 if ((args = PyTuple_New(2)) == NULL)
Guido van Rossum2d951851994-08-29 12:52:16 +00001402 goto Fail;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001403 }
1404
Tim Peters15d81ef2001-05-04 04:39:21 +00001405 op2 = PyIter_Next(it);
1406 if (op2 == NULL) {
Tim Petersf4848da2001-05-05 00:14:56 +00001407 if (PyErr_Occurred())
1408 goto Fail;
1409 break;
Guido van Rossum2d951851994-08-29 12:52:16 +00001410 }
Guido van Rossum12d12c51993-10-26 17:58:25 +00001411
Guido van Rossum2d951851994-08-29 12:52:16 +00001412 if (result == NULL)
1413 result = op2;
1414 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001415 PyTuple_SetItem(args, 0, result);
1416 PyTuple_SetItem(args, 1, op2);
1417 if ((result = PyEval_CallObject(func, args)) == NULL)
Guido van Rossum2d951851994-08-29 12:52:16 +00001418 goto Fail;
1419 }
Guido van Rossum12d12c51993-10-26 17:58:25 +00001420 }
1421
Guido van Rossum79f25d91997-04-29 20:08:16 +00001422 Py_DECREF(args);
Guido van Rossum12d12c51993-10-26 17:58:25 +00001423
Guido van Rossum2d951851994-08-29 12:52:16 +00001424 if (result == NULL)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001425 PyErr_SetString(PyExc_TypeError,
Fred Drake661ea262000-10-24 19:57:45 +00001426 "reduce() of empty sequence with no initial value");
Guido van Rossum2d951851994-08-29 12:52:16 +00001427
Tim Peters15d81ef2001-05-04 04:39:21 +00001428 Py_DECREF(it);
Guido van Rossum12d12c51993-10-26 17:58:25 +00001429 return result;
1430
Guido van Rossum2d951851994-08-29 12:52:16 +00001431Fail:
Guido van Rossum79f25d91997-04-29 20:08:16 +00001432 Py_XDECREF(args);
1433 Py_XDECREF(result);
Tim Peters15d81ef2001-05-04 04:39:21 +00001434 Py_DECREF(it);
Guido van Rossum12d12c51993-10-26 17:58:25 +00001435 return NULL;
1436}
1437
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001438PyDoc_STRVAR(reduce_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001439"reduce(function, sequence[, initial]) -> value\n\
1440\n\
1441Apply a function of two arguments cumulatively to the items of a sequence,\n\
1442from left to right, so as to reduce the sequence to a single value.\n\
1443For example, reduce(lambda x, y: x+y, [1, 2, 3, 4, 5]) calculates\n\
1444((((1+2)+3)+4)+5). If initial is present, it is placed before the items\n\
1445of the sequence in the calculation, and serves as a default when the\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001446sequence is empty.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001447
1448
Guido van Rossum79f25d91997-04-29 20:08:16 +00001449static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001450builtin_reload(PyObject *self, PyObject *v)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001451{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001452 return PyImport_ReloadModule(v);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001453}
1454
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001455PyDoc_STRVAR(reload_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001456"reload(module) -> module\n\
1457\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001458Reload the module. The module must have been successfully imported before.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001459
1460
Guido van Rossum79f25d91997-04-29 20:08:16 +00001461static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001462builtin_repr(PyObject *self, PyObject *v)
Guido van Rossumc89705d1992-11-26 08:54:07 +00001463{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001464 return PyObject_Repr(v);
Guido van Rossumc89705d1992-11-26 08:54:07 +00001465}
1466
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001467PyDoc_STRVAR(repr_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001468"repr(object) -> string\n\
1469\n\
1470Return the canonical string representation of the object.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001471For most object types, eval(repr(object)) == object.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001472
1473
Guido van Rossum79f25d91997-04-29 20:08:16 +00001474static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001475builtin_round(PyObject *self, PyObject *args)
Guido van Rossum9e51f9b1993-02-12 16:29:05 +00001476{
Guido van Rossum9e51f9b1993-02-12 16:29:05 +00001477 double x;
1478 double f;
1479 int ndigits = 0;
Guido van Rossum9e51f9b1993-02-12 16:29:05 +00001480 int i;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001481
Guido van Rossum79f25d91997-04-29 20:08:16 +00001482 if (!PyArg_ParseTuple(args, "d|i:round", &x, &ndigits))
Guido van Rossum9e51f9b1993-02-12 16:29:05 +00001483 return NULL;
Guido van Rossum9e51f9b1993-02-12 16:29:05 +00001484 f = 1.0;
Guido van Rossum1e162d31998-05-09 14:42:25 +00001485 i = abs(ndigits);
1486 while (--i >= 0)
Guido van Rossum9e51f9b1993-02-12 16:29:05 +00001487 f = f*10.0;
Guido van Rossum1e162d31998-05-09 14:42:25 +00001488 if (ndigits < 0)
1489 x /= f;
Guido van Rossum9e51f9b1993-02-12 16:29:05 +00001490 else
Guido van Rossum1e162d31998-05-09 14:42:25 +00001491 x *= f;
1492 if (x >= 0.0)
1493 x = floor(x + 0.5);
1494 else
1495 x = ceil(x - 0.5);
1496 if (ndigits < 0)
1497 x *= f;
1498 else
1499 x /= f;
1500 return PyFloat_FromDouble(x);
Guido van Rossum9e51f9b1993-02-12 16:29:05 +00001501}
1502
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001503PyDoc_STRVAR(round_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001504"round(number[, ndigits]) -> floating point number\n\
1505\n\
1506Round a number to a given precision in decimal digits (default 0 digits).\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001507This always returns a floating point number. Precision may be negative.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001508
1509
Guido van Rossum79f25d91997-04-29 20:08:16 +00001510static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001511builtin_vars(PyObject *self, PyObject *args)
Guido van Rossum2d951851994-08-29 12:52:16 +00001512{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001513 PyObject *v = NULL;
1514 PyObject *d;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001515
Guido van Rossum79f25d91997-04-29 20:08:16 +00001516 if (!PyArg_ParseTuple(args, "|O:vars", &v))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001517 return NULL;
Guido van Rossum2d951851994-08-29 12:52:16 +00001518 if (v == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001519 d = PyEval_GetLocals();
Guido van Rossum53bb7ff1995-07-26 16:26:31 +00001520 if (d == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001521 if (!PyErr_Occurred())
1522 PyErr_SetString(PyExc_SystemError,
1523 "no locals!?");
Guido van Rossum53bb7ff1995-07-26 16:26:31 +00001524 }
1525 else
Guido van Rossum79f25d91997-04-29 20:08:16 +00001526 Py_INCREF(d);
Guido van Rossum2d951851994-08-29 12:52:16 +00001527 }
1528 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001529 d = PyObject_GetAttrString(v, "__dict__");
Guido van Rossum2d951851994-08-29 12:52:16 +00001530 if (d == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001531 PyErr_SetString(PyExc_TypeError,
Guido van Rossum2d951851994-08-29 12:52:16 +00001532 "vars() argument must have __dict__ attribute");
1533 return NULL;
1534 }
1535 }
1536 return d;
1537}
1538
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001539PyDoc_STRVAR(vars_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001540"vars([object]) -> dictionary\n\
1541\n\
1542Without arguments, equivalent to locals().\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001543With an argument, equivalent to object.__dict__.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001544
Barry Warsawcde8b1b1997-08-22 21:14:38 +00001545static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001546builtin_isinstance(PyObject *self, PyObject *args)
Barry Warsawcde8b1b1997-08-22 21:14:38 +00001547{
1548 PyObject *inst;
1549 PyObject *cls;
Guido van Rossum823649d2001-03-21 18:40:58 +00001550 int retval;
Barry Warsawcde8b1b1997-08-22 21:14:38 +00001551
Guido van Rossum43713e52000-02-29 13:59:29 +00001552 if (!PyArg_ParseTuple(args, "OO:isinstance", &inst, &cls))
Barry Warsawcde8b1b1997-08-22 21:14:38 +00001553 return NULL;
Guido van Rossumf5dd9141997-12-02 19:11:45 +00001554
Guido van Rossum823649d2001-03-21 18:40:58 +00001555 retval = PyObject_IsInstance(inst, cls);
1556 if (retval < 0)
Guido van Rossumad991772001-01-12 16:03:05 +00001557 return NULL;
Guido van Rossum77f6a652002-04-03 22:41:51 +00001558 return PyBool_FromLong(retval);
Barry Warsawcde8b1b1997-08-22 21:14:38 +00001559}
1560
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001561PyDoc_STRVAR(isinstance_doc,
Guido van Rossum77f6a652002-04-03 22:41:51 +00001562"isinstance(object, class-or-type-or-tuple) -> bool\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001563\n\
1564Return whether an object is an instance of a class or of a subclass thereof.\n\
Guido van Rossum03290ec2001-10-07 20:54:12 +00001565With a type as second argument, return whether that is the object's type.\n\
1566The form using a tuple, isinstance(x, (A, B, ...)), is a shortcut for\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001567isinstance(x, A) or isinstance(x, B) or ... (etc.).");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001568
Barry Warsawcde8b1b1997-08-22 21:14:38 +00001569
1570static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001571builtin_issubclass(PyObject *self, PyObject *args)
Barry Warsawcde8b1b1997-08-22 21:14:38 +00001572{
1573 PyObject *derived;
1574 PyObject *cls;
1575 int retval;
1576
Guido van Rossum43713e52000-02-29 13:59:29 +00001577 if (!PyArg_ParseTuple(args, "OO:issubclass", &derived, &cls))
Barry Warsawcde8b1b1997-08-22 21:14:38 +00001578 return NULL;
Guido van Rossum668213d1999-06-16 17:28:37 +00001579
Guido van Rossum823649d2001-03-21 18:40:58 +00001580 retval = PyObject_IsSubclass(derived, cls);
1581 if (retval < 0)
1582 return NULL;
Guido van Rossum77f6a652002-04-03 22:41:51 +00001583 return PyBool_FromLong(retval);
Barry Warsawcde8b1b1997-08-22 21:14:38 +00001584}
1585
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001586PyDoc_STRVAR(issubclass_doc,
Guido van Rossum77f6a652002-04-03 22:41:51 +00001587"issubclass(C, B) -> bool\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001588\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001589Return whether class C is a subclass (i.e., a derived class) of class B.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001590
Barry Warsawcde8b1b1997-08-22 21:14:38 +00001591
Barry Warsawbd599b52000-08-03 15:45:29 +00001592static PyObject*
1593builtin_zip(PyObject *self, PyObject *args)
1594{
1595 PyObject *ret;
Tim Peters67d687a2002-04-29 21:27:32 +00001596 const int itemsize = PySequence_Length(args);
Tim Peters8572b4f2001-05-06 01:05:02 +00001597 int i;
1598 PyObject *itlist; /* tuple of iterators */
Tim Peters67d687a2002-04-29 21:27:32 +00001599 int len; /* guess at result length */
Barry Warsawbd599b52000-08-03 15:45:29 +00001600
1601 if (itemsize < 1) {
1602 PyErr_SetString(PyExc_TypeError,
Fred Drake661ea262000-10-24 19:57:45 +00001603 "zip() requires at least one sequence");
Barry Warsawbd599b52000-08-03 15:45:29 +00001604 return NULL;
1605 }
1606 /* args must be a tuple */
1607 assert(PyTuple_Check(args));
1608
Tim Peters39a86c22002-05-12 07:19:38 +00001609 /* Guess at result length: the shortest of the input lengths.
1610 If some argument refuses to say, we refuse to guess too, lest
1611 an argument like xrange(sys.maxint) lead us astray.*/
Tim Peters67d687a2002-04-29 21:27:32 +00001612 len = -1; /* unknown */
1613 for (i = 0; i < itemsize; ++i) {
1614 PyObject *item = PyTuple_GET_ITEM(args, i);
1615 int thislen = PySequence_Length(item);
Tim Peters39a86c22002-05-12 07:19:38 +00001616 if (thislen < 0) {
Tim Peters67d687a2002-04-29 21:27:32 +00001617 PyErr_Clear();
Tim Peters39a86c22002-05-12 07:19:38 +00001618 len = -1;
1619 break;
1620 }
Tim Peters67d687a2002-04-29 21:27:32 +00001621 else if (len < 0 || thislen < len)
1622 len = thislen;
1623 }
1624
Tim Peters8572b4f2001-05-06 01:05:02 +00001625 /* allocate result list */
Tim Peters67d687a2002-04-29 21:27:32 +00001626 if (len < 0)
1627 len = 10; /* arbitrary */
1628 if ((ret = PyList_New(len)) == NULL)
Barry Warsawbd599b52000-08-03 15:45:29 +00001629 return NULL;
1630
Tim Peters8572b4f2001-05-06 01:05:02 +00001631 /* obtain iterators */
1632 itlist = PyTuple_New(itemsize);
1633 if (itlist == NULL)
1634 goto Fail_ret;
1635 for (i = 0; i < itemsize; ++i) {
1636 PyObject *item = PyTuple_GET_ITEM(args, i);
1637 PyObject *it = PyObject_GetIter(item);
1638 if (it == NULL) {
1639 if (PyErr_ExceptionMatches(PyExc_TypeError))
1640 PyErr_Format(PyExc_TypeError,
1641 "zip argument #%d must support iteration",
1642 i+1);
1643 goto Fail_ret_itlist;
Barry Warsawbd599b52000-08-03 15:45:29 +00001644 }
Tim Peters8572b4f2001-05-06 01:05:02 +00001645 PyTuple_SET_ITEM(itlist, i, it);
1646 }
Barry Warsawbd599b52000-08-03 15:45:29 +00001647
Tim Peters8572b4f2001-05-06 01:05:02 +00001648 /* build result into ret list */
Tim Peters67d687a2002-04-29 21:27:32 +00001649 for (i = 0; ; ++i) {
1650 int j;
Tim Peters8572b4f2001-05-06 01:05:02 +00001651 PyObject *next = PyTuple_New(itemsize);
1652 if (!next)
1653 goto Fail_ret_itlist;
1654
Tim Peters67d687a2002-04-29 21:27:32 +00001655 for (j = 0; j < itemsize; j++) {
1656 PyObject *it = PyTuple_GET_ITEM(itlist, j);
Tim Peters8572b4f2001-05-06 01:05:02 +00001657 PyObject *item = PyIter_Next(it);
Barry Warsawbd599b52000-08-03 15:45:29 +00001658 if (!item) {
Tim Peters8572b4f2001-05-06 01:05:02 +00001659 if (PyErr_Occurred()) {
1660 Py_DECREF(ret);
1661 ret = NULL;
Barry Warsawbd599b52000-08-03 15:45:29 +00001662 }
1663 Py_DECREF(next);
Tim Peters8572b4f2001-05-06 01:05:02 +00001664 Py_DECREF(itlist);
Tim Peters67d687a2002-04-29 21:27:32 +00001665 goto Done;
Barry Warsawbd599b52000-08-03 15:45:29 +00001666 }
Tim Peters67d687a2002-04-29 21:27:32 +00001667 PyTuple_SET_ITEM(next, j, item);
Barry Warsawbd599b52000-08-03 15:45:29 +00001668 }
Tim Peters8572b4f2001-05-06 01:05:02 +00001669
Tim Peters67d687a2002-04-29 21:27:32 +00001670 if (i < len)
1671 PyList_SET_ITEM(ret, i, next);
1672 else {
1673 int status = PyList_Append(ret, next);
1674 Py_DECREF(next);
1675 ++len;
1676 if (status < 0)
1677 goto Fail_ret_itlist;
1678 }
Barry Warsawbd599b52000-08-03 15:45:29 +00001679 }
Tim Peters8572b4f2001-05-06 01:05:02 +00001680
Tim Peters67d687a2002-04-29 21:27:32 +00001681Done:
1682 if (ret != NULL && i < len) {
1683 /* The list is too big. */
1684 if (PyList_SetSlice(ret, i, len, NULL) < 0)
1685 return NULL;
1686 }
1687 return ret;
1688
Tim Peters8572b4f2001-05-06 01:05:02 +00001689Fail_ret_itlist:
1690 Py_DECREF(itlist);
1691Fail_ret:
1692 Py_DECREF(ret);
1693 return NULL;
Barry Warsawbd599b52000-08-03 15:45:29 +00001694}
1695
1696
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001697PyDoc_STRVAR(zip_doc,
Barry Warsawbd599b52000-08-03 15:45:29 +00001698"zip(seq1 [, seq2 [...]]) -> [(seq1[0], seq2[0] ...), (...)]\n\
1699\n\
1700Return a list of tuples, where each tuple contains the i-th element\n\
1701from each of the argument sequences. The returned list is truncated\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001702in length to the length of the shortest argument sequence.");
Barry Warsawbd599b52000-08-03 15:45:29 +00001703
1704
Guido van Rossum79f25d91997-04-29 20:08:16 +00001705static PyMethodDef builtin_methods[] = {
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001706 {"__import__", builtin___import__, METH_VARARGS, import_doc},
1707 {"abs", builtin_abs, METH_O, abs_doc},
1708 {"apply", builtin_apply, METH_VARARGS, apply_doc},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001709 {"callable", builtin_callable, METH_O, callable_doc},
1710 {"chr", builtin_chr, METH_VARARGS, chr_doc},
1711 {"cmp", builtin_cmp, METH_VARARGS, cmp_doc},
1712 {"coerce", builtin_coerce, METH_VARARGS, coerce_doc},
1713 {"compile", builtin_compile, METH_VARARGS, compile_doc},
1714 {"delattr", builtin_delattr, METH_VARARGS, delattr_doc},
1715 {"dir", builtin_dir, METH_VARARGS, dir_doc},
1716 {"divmod", builtin_divmod, METH_VARARGS, divmod_doc},
1717 {"eval", builtin_eval, METH_VARARGS, eval_doc},
1718 {"execfile", builtin_execfile, METH_VARARGS, execfile_doc},
1719 {"filter", builtin_filter, METH_VARARGS, filter_doc},
1720 {"getattr", builtin_getattr, METH_VARARGS, getattr_doc},
1721 {"globals", (PyCFunction)builtin_globals, METH_NOARGS, globals_doc},
1722 {"hasattr", builtin_hasattr, METH_VARARGS, hasattr_doc},
1723 {"hash", builtin_hash, METH_O, hash_doc},
1724 {"hex", builtin_hex, METH_O, hex_doc},
1725 {"id", builtin_id, METH_O, id_doc},
1726 {"input", builtin_input, METH_VARARGS, input_doc},
1727 {"intern", builtin_intern, METH_VARARGS, intern_doc},
1728 {"isinstance", builtin_isinstance, METH_VARARGS, isinstance_doc},
1729 {"issubclass", builtin_issubclass, METH_VARARGS, issubclass_doc},
1730 {"iter", builtin_iter, METH_VARARGS, iter_doc},
1731 {"len", builtin_len, METH_O, len_doc},
1732 {"locals", (PyCFunction)builtin_locals, METH_NOARGS, locals_doc},
1733 {"map", builtin_map, METH_VARARGS, map_doc},
1734 {"max", builtin_max, METH_VARARGS, max_doc},
1735 {"min", builtin_min, METH_VARARGS, min_doc},
1736 {"oct", builtin_oct, METH_O, oct_doc},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001737 {"ord", builtin_ord, METH_O, ord_doc},
1738 {"pow", builtin_pow, METH_VARARGS, pow_doc},
1739 {"range", builtin_range, METH_VARARGS, range_doc},
1740 {"raw_input", builtin_raw_input, METH_VARARGS, raw_input_doc},
1741 {"reduce", builtin_reduce, METH_VARARGS, reduce_doc},
1742 {"reload", builtin_reload, METH_O, reload_doc},
1743 {"repr", builtin_repr, METH_O, repr_doc},
1744 {"round", builtin_round, METH_VARARGS, round_doc},
1745 {"setattr", builtin_setattr, METH_VARARGS, setattr_doc},
Martin v. Löwis339d0f72001-08-17 18:39:25 +00001746#ifdef Py_USING_UNICODE
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001747 {"unichr", builtin_unichr, METH_VARARGS, unichr_doc},
Martin v. Löwis339d0f72001-08-17 18:39:25 +00001748#endif
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001749 {"vars", builtin_vars, METH_VARARGS, vars_doc},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001750 {"zip", builtin_zip, METH_VARARGS, zip_doc},
Guido van Rossumc02e15c1991-12-16 13:03:00 +00001751 {NULL, NULL},
Guido van Rossum3f5da241990-12-20 15:06:42 +00001752};
1753
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001754PyDoc_STRVAR(builtin_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001755"Built-in functions, exceptions, and other objects.\n\
1756\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001757Noteworthy: None is the `nil' object; Ellipsis represents `...' in slices.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001758
Guido van Rossum25ce5661997-08-02 03:10:38 +00001759PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001760_PyBuiltin_Init(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +00001761{
Fred Drake5550de32000-06-20 04:54:19 +00001762 PyObject *mod, *dict, *debug;
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001763 mod = Py_InitModule4("__builtin__", builtin_methods,
1764 builtin_doc, (PyObject *)NULL,
1765 PYTHON_API_VERSION);
Guido van Rossum25ce5661997-08-02 03:10:38 +00001766 if (mod == NULL)
1767 return NULL;
1768 dict = PyModule_GetDict(mod);
Tim Peters4b7625e2001-09-13 21:37:17 +00001769
1770#define SETBUILTIN(NAME, OBJECT) \
1771 if (PyDict_SetItemString(dict, NAME, (PyObject *)OBJECT) < 0) \
1772 return NULL
1773
1774 SETBUILTIN("None", Py_None);
1775 SETBUILTIN("Ellipsis", Py_Ellipsis);
1776 SETBUILTIN("NotImplemented", Py_NotImplemented);
Guido van Rossum77f6a652002-04-03 22:41:51 +00001777 SETBUILTIN("False", Py_False);
1778 SETBUILTIN("True", Py_True);
Neal Norwitz32a7e7f2002-05-31 19:58:02 +00001779 SETBUILTIN("basestring", &PyBaseString_Type);
Guido van Rossum77f6a652002-04-03 22:41:51 +00001780 SETBUILTIN("bool", &PyBool_Type);
Guido van Rossumbea18cc2002-06-14 20:41:17 +00001781 SETBUILTIN("buffer", &PyBuffer_Type);
Tim Peters4b7625e2001-09-13 21:37:17 +00001782 SETBUILTIN("classmethod", &PyClassMethod_Type);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001783#ifndef WITHOUT_COMPLEX
Tim Peters4b7625e2001-09-13 21:37:17 +00001784 SETBUILTIN("complex", &PyComplex_Type);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001785#endif
Tim Petersa427a2b2001-10-29 22:25:45 +00001786 SETBUILTIN("dict", &PyDict_Type);
Tim Peters67d687a2002-04-29 21:27:32 +00001787 SETBUILTIN("enumerate", &PyEnum_Type);
Tim Peters4b7625e2001-09-13 21:37:17 +00001788 SETBUILTIN("float", &PyFloat_Type);
1789 SETBUILTIN("property", &PyProperty_Type);
1790 SETBUILTIN("int", &PyInt_Type);
1791 SETBUILTIN("list", &PyList_Type);
1792 SETBUILTIN("long", &PyLong_Type);
1793 SETBUILTIN("object", &PyBaseObject_Type);
Guido van Rossumbea18cc2002-06-14 20:41:17 +00001794 SETBUILTIN("slice", &PySlice_Type);
Tim Peters4b7625e2001-09-13 21:37:17 +00001795 SETBUILTIN("staticmethod", &PyStaticMethod_Type);
1796 SETBUILTIN("str", &PyString_Type);
1797 SETBUILTIN("super", &PySuper_Type);
1798 SETBUILTIN("tuple", &PyTuple_Type);
1799 SETBUILTIN("type", &PyType_Type);
Raymond Hettingerc4c453f2002-06-05 23:12:45 +00001800 SETBUILTIN("xrange", &PyRange_Type);
Tim Peters742dfd62001-09-13 21:49:44 +00001801
1802 /* Note that open() is just an alias of file(). */
1803 SETBUILTIN("open", &PyFile_Type);
Tim Peters4b7625e2001-09-13 21:37:17 +00001804 SETBUILTIN("file", &PyFile_Type);
Martin v. Löwis339d0f72001-08-17 18:39:25 +00001805#ifdef Py_USING_UNICODE
Tim Peters4b7625e2001-09-13 21:37:17 +00001806 SETBUILTIN("unicode", &PyUnicode_Type);
Martin v. Löwis339d0f72001-08-17 18:39:25 +00001807#endif
Guido van Rossum77f6a652002-04-03 22:41:51 +00001808 debug = PyBool_FromLong(Py_OptimizeFlag == 0);
Fred Drake5550de32000-06-20 04:54:19 +00001809 if (PyDict_SetItemString(dict, "__debug__", debug) < 0) {
1810 Py_XDECREF(debug);
Guido van Rossum25ce5661997-08-02 03:10:38 +00001811 return NULL;
Fred Drake5550de32000-06-20 04:54:19 +00001812 }
1813 Py_XDECREF(debug);
Barry Warsaw757af0e1997-08-29 22:13:51 +00001814
Guido van Rossum25ce5661997-08-02 03:10:38 +00001815 return mod;
Tim Peters4b7625e2001-09-13 21:37:17 +00001816#undef SETBUILTIN
Guido van Rossum3f5da241990-12-20 15:06:42 +00001817}
1818
Guido van Rossume77a7571993-11-03 15:01:26 +00001819/* Helper for filter(): filter a tuple through a function */
Guido van Rossum12d12c51993-10-26 17:58:25 +00001820
Guido van Rossum79f25d91997-04-29 20:08:16 +00001821static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001822filtertuple(PyObject *func, PyObject *tuple)
Guido van Rossum12d12c51993-10-26 17:58:25 +00001823{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001824 PyObject *result;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001825 register int i, j;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001826 int len = PyTuple_Size(tuple);
Guido van Rossum12d12c51993-10-26 17:58:25 +00001827
Guido van Rossumb7b45621995-08-04 04:07:45 +00001828 if (len == 0) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001829 Py_INCREF(tuple);
Guido van Rossumb7b45621995-08-04 04:07:45 +00001830 return tuple;
1831 }
1832
Guido van Rossum79f25d91997-04-29 20:08:16 +00001833 if ((result = PyTuple_New(len)) == NULL)
Guido van Rossum2586bf01993-11-01 16:21:44 +00001834 return NULL;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001835
Guido van Rossum12d12c51993-10-26 17:58:25 +00001836 for (i = j = 0; i < len; ++i) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001837 PyObject *item, *good;
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00001838 int ok;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001839
Guido van Rossum79f25d91997-04-29 20:08:16 +00001840 if ((item = PyTuple_GetItem(tuple, i)) == NULL)
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00001841 goto Fail_1;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001842 if (func == Py_None) {
1843 Py_INCREF(item);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00001844 good = item;
1845 }
1846 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001847 PyObject *arg = Py_BuildValue("(O)", item);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00001848 if (arg == NULL)
1849 goto Fail_1;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001850 good = PyEval_CallObject(func, arg);
1851 Py_DECREF(arg);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00001852 if (good == NULL)
Guido van Rossum12d12c51993-10-26 17:58:25 +00001853 goto Fail_1;
1854 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00001855 ok = PyObject_IsTrue(good);
1856 Py_DECREF(good);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00001857 if (ok) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001858 Py_INCREF(item);
1859 if (PyTuple_SetItem(result, j++, item) < 0)
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00001860 goto Fail_1;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001861 }
Guido van Rossum12d12c51993-10-26 17:58:25 +00001862 }
1863
Tim Peters4324aa32001-05-28 22:30:08 +00001864 if (_PyTuple_Resize(&result, j) < 0)
Guido van Rossum12d12c51993-10-26 17:58:25 +00001865 return NULL;
1866
Guido van Rossum12d12c51993-10-26 17:58:25 +00001867 return result;
1868
Guido van Rossum12d12c51993-10-26 17:58:25 +00001869Fail_1:
Guido van Rossum79f25d91997-04-29 20:08:16 +00001870 Py_DECREF(result);
Guido van Rossum12d12c51993-10-26 17:58:25 +00001871 return NULL;
1872}
1873
1874
Guido van Rossume77a7571993-11-03 15:01:26 +00001875/* Helper for filter(): filter a string through a function */
Guido van Rossum12d12c51993-10-26 17:58:25 +00001876
Guido van Rossum79f25d91997-04-29 20:08:16 +00001877static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001878filterstring(PyObject *func, PyObject *strobj)
Guido van Rossum12d12c51993-10-26 17:58:25 +00001879{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001880 PyObject *result;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001881 register int i, j;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001882 int len = PyString_Size(strobj);
Guido van Rossum12d12c51993-10-26 17:58:25 +00001883
Guido van Rossum79f25d91997-04-29 20:08:16 +00001884 if (func == Py_None) {
Guido van Rossum2586bf01993-11-01 16:21:44 +00001885 /* No character is ever false -- share input string */
Guido van Rossum79f25d91997-04-29 20:08:16 +00001886 Py_INCREF(strobj);
Guido van Rossum2d951851994-08-29 12:52:16 +00001887 return strobj;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001888 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00001889 if ((result = PyString_FromStringAndSize(NULL, len)) == NULL)
Guido van Rossum2586bf01993-11-01 16:21:44 +00001890 return NULL;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001891
Guido van Rossum12d12c51993-10-26 17:58:25 +00001892 for (i = j = 0; i < len; ++i) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001893 PyObject *item, *arg, *good;
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00001894 int ok;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001895
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00001896 item = (*strobj->ob_type->tp_as_sequence->sq_item)(strobj, i);
1897 if (item == NULL)
1898 goto Fail_1;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001899 arg = Py_BuildValue("(O)", item);
Tim Peters388ed082001-04-07 20:34:48 +00001900 if (arg == NULL) {
1901 Py_DECREF(item);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00001902 goto Fail_1;
Tim Peters388ed082001-04-07 20:34:48 +00001903 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00001904 good = PyEval_CallObject(func, arg);
1905 Py_DECREF(arg);
Tim Peters388ed082001-04-07 20:34:48 +00001906 if (good == NULL) {
1907 Py_DECREF(item);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00001908 goto Fail_1;
Tim Peters388ed082001-04-07 20:34:48 +00001909 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00001910 ok = PyObject_IsTrue(good);
1911 Py_DECREF(good);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00001912 if (ok)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001913 PyString_AS_STRING((PyStringObject *)result)[j++] =
1914 PyString_AS_STRING((PyStringObject *)item)[0];
Tim Peters388ed082001-04-07 20:34:48 +00001915 Py_DECREF(item);
Guido van Rossum12d12c51993-10-26 17:58:25 +00001916 }
1917
Tim Peters5de98422002-04-27 18:44:32 +00001918 if (j < len)
1919 _PyString_Resize(&result, j);
Guido van Rossum12d12c51993-10-26 17:58:25 +00001920
Guido van Rossum12d12c51993-10-26 17:58:25 +00001921 return result;
1922
Guido van Rossum12d12c51993-10-26 17:58:25 +00001923Fail_1:
Guido van Rossum79f25d91997-04-29 20:08:16 +00001924 Py_DECREF(result);
Guido van Rossum12d12c51993-10-26 17:58:25 +00001925 return NULL;
1926}