blob: 680152d510b5215c514c141bf31e208354bb8fd9 [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 Hammond26cffde42001-05-14 12:17:34 +000016/* The default encoding used by the platform file system APIs
17 Can remain NULL for all platforms that don't have such a concept
18*/
Fredrik Lundh5b979352001-06-26 17:46:10 +000019#if defined(MS_WIN32) && defined(HAVE_USABLE_WCHAR_T)
Mark Hammond26cffde42001-05-14 12:17:34 +000020const char *Py_FileSystemDefaultEncoding = "mbcs";
21#else
22const char *Py_FileSystemDefaultEncoding = NULL; /* use default */
23#endif
Mark Hammondef8b6542001-05-13 08:04:26 +000024
Guido van Rossum12d12c51993-10-26 17:58:25 +000025/* Forward */
Tim Petersdbd9ba62000-07-09 03:09:57 +000026static PyObject *filterstring(PyObject *, PyObject *);
27static PyObject *filtertuple (PyObject *, PyObject *);
Guido van Rossum12d12c51993-10-26 17:58:25 +000028
Guido van Rossum79f25d91997-04-29 20:08:16 +000029static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +000030builtin___import__(PyObject *self, PyObject *args)
Guido van Rossum3f5da241990-12-20 15:06:42 +000031{
Guido van Rossum1ae940a1995-01-02 19:04:15 +000032 char *name;
Guido van Rossum79f25d91997-04-29 20:08:16 +000033 PyObject *globals = NULL;
34 PyObject *locals = NULL;
35 PyObject *fromlist = NULL;
Guido van Rossum1ae940a1995-01-02 19:04:15 +000036
Guido van Rossum79f25d91997-04-29 20:08:16 +000037 if (!PyArg_ParseTuple(args, "s|OOO:__import__",
Guido van Rossum24c13741995-02-14 09:42:43 +000038 &name, &globals, &locals, &fromlist))
Guido van Rossum1ae940a1995-01-02 19:04:15 +000039 return NULL;
Guido van Rossumaee0bad1997-09-05 07:33:22 +000040 return PyImport_ImportModuleEx(name, globals, locals, fromlist);
Guido van Rossum1ae940a1995-01-02 19:04:15 +000041}
42
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +000043static char import_doc[] =
44"__import__(name, globals, locals, fromlist) -> module\n\
45\n\
46Import a module. The globals are only used to determine the context;\n\
47they are not modified. The locals are currently unused. The fromlist\n\
48should be a list of names to emulate ``from name import ...'', or an\n\
49empty list to emulate ``import name''.\n\
50When importing a module from a package, note that __import__('A.B', ...)\n\
51returns package A when fromlist is empty, but its submodule B when\n\
52fromlist is not empty.";
53
Guido van Rossum1ae940a1995-01-02 19:04:15 +000054
Guido van Rossum79f25d91997-04-29 20:08:16 +000055static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +000056builtin_abs(PyObject *self, PyObject *v)
Guido van Rossum1ae940a1995-01-02 19:04:15 +000057{
Guido van Rossum09df08a1998-05-22 00:51:39 +000058 return PyNumber_Absolute(v);
Guido van Rossum3f5da241990-12-20 15:06:42 +000059}
60
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +000061static char abs_doc[] =
62"abs(number) -> number\n\
63\n\
64Return the absolute value of the argument.";
65
66
Guido van Rossum79f25d91997-04-29 20:08:16 +000067static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +000068builtin_apply(PyObject *self, PyObject *args)
Guido van Rossumc02e15c1991-12-16 13:03:00 +000069{
Guido van Rossum79f25d91997-04-29 20:08:16 +000070 PyObject *func, *alist = NULL, *kwdict = NULL;
Barry Warsaw968f8cb1998-10-01 15:33:12 +000071 PyObject *t = NULL, *retval = NULL;
Guido van Rossum1ae940a1995-01-02 19:04:15 +000072
Guido van Rossum79f25d91997-04-29 20:08:16 +000073 if (!PyArg_ParseTuple(args, "O|OO:apply", &func, &alist, &kwdict))
Guido van Rossumc02e15c1991-12-16 13:03:00 +000074 return NULL;
Barry Warsaw968f8cb1998-10-01 15:33:12 +000075 if (alist != NULL) {
76 if (!PyTuple_Check(alist)) {
77 if (!PySequence_Check(alist)) {
Jeremy Hyltonc862cf42001-01-19 03:25:05 +000078 PyErr_Format(PyExc_TypeError,
79 "apply() arg 2 expect sequence, found %s",
80 alist->ob_type->tp_name);
Barry Warsaw968f8cb1998-10-01 15:33:12 +000081 return NULL;
82 }
83 t = PySequence_Tuple(alist);
84 if (t == NULL)
85 return NULL;
86 alist = t;
87 }
Guido van Rossum2d951851994-08-29 12:52:16 +000088 }
Guido van Rossum79f25d91997-04-29 20:08:16 +000089 if (kwdict != NULL && !PyDict_Check(kwdict)) {
Jeremy Hyltonc862cf42001-01-19 03:25:05 +000090 PyErr_Format(PyExc_TypeError,
91 "apply() arg 3 expected dictionary, found %s",
92 kwdict->ob_type->tp_name);
Barry Warsaw968f8cb1998-10-01 15:33:12 +000093 goto finally;
Guido van Rossum681d79a1995-07-18 14:51:37 +000094 }
Barry Warsaw968f8cb1998-10-01 15:33:12 +000095 retval = PyEval_CallObjectWithKeywords(func, alist, kwdict);
96 finally:
97 Py_XDECREF(t);
98 return retval;
Guido van Rossumc02e15c1991-12-16 13:03:00 +000099}
100
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000101static char apply_doc[] =
Fred Drakef1fbc622001-01-12 17:05:05 +0000102"apply(object[, args[, kwargs]]) -> value\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000103\n\
Fred Drake7b912121999-12-23 14:16:55 +0000104Call a callable object with positional arguments taken from the tuple args,\n\
105and keyword arguments taken from the optional dictionary kwargs.\n\
106Note that classes are callable, as are instances with a __call__() method.";
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000107
108
Guido van Rossum79f25d91997-04-29 20:08:16 +0000109static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000110builtin_buffer(PyObject *self, PyObject *args)
Guido van Rossum0daf0221999-03-19 19:07:19 +0000111{
112 PyObject *ob;
113 int offset = 0;
114 int size = Py_END_OF_BUFFER;
115
116 if ( !PyArg_ParseTuple(args, "O|ii:buffer", &ob, &offset, &size) )
117 return NULL;
118 return PyBuffer_FromObject(ob, offset, size);
119}
120
121static char buffer_doc[] =
Guido van Rossum09095f32000-03-10 23:00:52 +0000122"buffer(object [, offset[, size]]) -> object\n\
Guido van Rossum0daf0221999-03-19 19:07:19 +0000123\n\
Guido van Rossumad991772001-01-12 16:03:05 +0000124Create a new buffer object which references the given object.\n\
Guido van Rossum0daf0221999-03-19 19:07:19 +0000125The buffer will reference a slice of the target object from the\n\
126start of the object (or at the specified offset). The slice will\n\
127extend to the end of the target object (or with the specified size).";
128
129
130static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000131builtin_callable(PyObject *self, PyObject *v)
Guido van Rossum2d951851994-08-29 12:52:16 +0000132{
Guido van Rossum77f6a652002-04-03 22:41:51 +0000133 return PyBool_FromLong((long)PyCallable_Check(v));
Guido van Rossum2d951851994-08-29 12:52:16 +0000134}
135
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000136static char callable_doc[] =
Guido van Rossum77f6a652002-04-03 22:41:51 +0000137"callable(object) -> bool\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000138\n\
139Return whether the object is callable (i.e., some kind of function).\n\
140Note that classes are callable, as are instances with a __call__() method.";
141
142
Guido van Rossum79f25d91997-04-29 20:08:16 +0000143static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000144builtin_filter(PyObject *self, PyObject *args)
Guido van Rossum12d12c51993-10-26 17:58:25 +0000145{
Tim Peters0e57abf2001-05-02 07:39:38 +0000146 PyObject *func, *seq, *result, *it;
147 int len; /* guess for result list size */
Tim Petersf4848da2001-05-05 00:14:56 +0000148 register int j;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000149
Guido van Rossum79f25d91997-04-29 20:08:16 +0000150 if (!PyArg_ParseTuple(args, "OO:filter", &func, &seq))
Guido van Rossum12d12c51993-10-26 17:58:25 +0000151 return NULL;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000152
Tim Peters0e57abf2001-05-02 07:39:38 +0000153 /* Strings and tuples return a result of the same type. */
154 if (PyString_Check(seq))
155 return filterstring(func, seq);
156 if (PyTuple_Check(seq))
157 return filtertuple(func, seq);
158
159 /* Get iterator. */
160 it = PyObject_GetIter(seq);
161 if (it == NULL)
162 return NULL;
163
164 /* Guess a result list size. */
165 len = -1; /* unknown */
166 if (PySequence_Check(seq) &&
167 seq->ob_type->tp_as_sequence->sq_length) {
168 len = PySequence_Size(seq);
169 if (len < 0)
170 PyErr_Clear();
Guido van Rossum12d12c51993-10-26 17:58:25 +0000171 }
Tim Peters0e57abf2001-05-02 07:39:38 +0000172 if (len < 0)
173 len = 8; /* arbitrary */
Guido van Rossum12d12c51993-10-26 17:58:25 +0000174
Tim Peters0e57abf2001-05-02 07:39:38 +0000175 /* Get a result list. */
Guido van Rossum79f25d91997-04-29 20:08:16 +0000176 if (PyList_Check(seq) && seq->ob_refcnt == 1) {
Tim Peters0e57abf2001-05-02 07:39:38 +0000177 /* Eww - can modify the list in-place. */
Guido van Rossum79f25d91997-04-29 20:08:16 +0000178 Py_INCREF(seq);
Guido van Rossum12d12c51993-10-26 17:58:25 +0000179 result = seq;
180 }
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000181 else {
Tim Peters0e57abf2001-05-02 07:39:38 +0000182 result = PyList_New(len);
183 if (result == NULL)
184 goto Fail_it;
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000185 }
Guido van Rossum12d12c51993-10-26 17:58:25 +0000186
Tim Peters0e57abf2001-05-02 07:39:38 +0000187 /* Build the result list. */
Tim Petersf4848da2001-05-05 00:14:56 +0000188 j = 0;
189 for (;;) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000190 PyObject *item, *good;
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000191 int ok;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000192
Tim Peters0e57abf2001-05-02 07:39:38 +0000193 item = PyIter_Next(it);
194 if (item == NULL) {
Tim Petersf4848da2001-05-05 00:14:56 +0000195 if (PyErr_Occurred())
196 goto Fail_result_it;
Tim Peters0e57abf2001-05-02 07:39:38 +0000197 break;
Guido van Rossum2d951851994-08-29 12:52:16 +0000198 }
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000199
Guido van Rossum79f25d91997-04-29 20:08:16 +0000200 if (func == Py_None) {
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000201 good = item;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000202 Py_INCREF(good);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000203 }
204 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000205 PyObject *arg = Py_BuildValue("(O)", item);
Tim Peters0e57abf2001-05-02 07:39:38 +0000206 if (arg == NULL) {
207 Py_DECREF(item);
208 goto Fail_result_it;
209 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000210 good = PyEval_CallObject(func, arg);
211 Py_DECREF(arg);
Guido van Rossum58b68731995-01-10 17:40:55 +0000212 if (good == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000213 Py_DECREF(item);
Tim Peters0e57abf2001-05-02 07:39:38 +0000214 goto Fail_result_it;
Guido van Rossum58b68731995-01-10 17:40:55 +0000215 }
Guido van Rossum12d12c51993-10-26 17:58:25 +0000216 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000217 ok = PyObject_IsTrue(good);
218 Py_DECREF(good);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000219 if (ok) {
Tim Peters0e57abf2001-05-02 07:39:38 +0000220 if (j < len)
221 PyList_SET_ITEM(result, j, item);
Guido van Rossum2d951851994-08-29 12:52:16 +0000222 else {
Barry Warsawfa77e091999-01-28 18:49:12 +0000223 int status = PyList_Append(result, item);
Barry Warsawfa77e091999-01-28 18:49:12 +0000224 Py_DECREF(item);
225 if (status < 0)
Tim Peters0e57abf2001-05-02 07:39:38 +0000226 goto Fail_result_it;
Guido van Rossum2d951851994-08-29 12:52:16 +0000227 }
Tim Peters0e57abf2001-05-02 07:39:38 +0000228 ++j;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000229 }
Tim Peters0e57abf2001-05-02 07:39:38 +0000230 else
231 Py_DECREF(item);
Guido van Rossum12d12c51993-10-26 17:58:25 +0000232 }
233
Guido van Rossum12d12c51993-10-26 17:58:25 +0000234
Tim Peters0e57abf2001-05-02 07:39:38 +0000235 /* Cut back result list if len is too big. */
Guido van Rossum79f25d91997-04-29 20:08:16 +0000236 if (j < len && PyList_SetSlice(result, j, len, NULL) < 0)
Tim Peters0e57abf2001-05-02 07:39:38 +0000237 goto Fail_result_it;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000238
Tim Peters3c6b1482001-05-21 08:07:05 +0000239 Py_DECREF(it);
Guido van Rossum12d12c51993-10-26 17:58:25 +0000240 return result;
241
Tim Peters0e57abf2001-05-02 07:39:38 +0000242Fail_result_it:
Guido van Rossum79f25d91997-04-29 20:08:16 +0000243 Py_DECREF(result);
Tim Peters0e57abf2001-05-02 07:39:38 +0000244Fail_it:
245 Py_DECREF(it);
Guido van Rossum12d12c51993-10-26 17:58:25 +0000246 return NULL;
247}
248
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000249static char filter_doc[] =
Tim Petersd50e5442002-03-09 00:06:26 +0000250"filter(function or None, sequence) -> list, tuple, or string\n"
251"\n"
252"Return those items of sequence for which function(item) is true. If\n"
253"function is None, return the items that are true. If sequence is a tuple\n"
254"or string, return the same type, else return a list.";
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000255
Guido van Rossum79f25d91997-04-29 20:08:16 +0000256static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000257builtin_chr(PyObject *self, PyObject *args)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000258{
259 long x;
260 char s[1];
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000261
Guido van Rossum79f25d91997-04-29 20:08:16 +0000262 if (!PyArg_ParseTuple(args, "l:chr", &x))
Guido van Rossum3f5da241990-12-20 15:06:42 +0000263 return NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000264 if (x < 0 || x >= 256) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000265 PyErr_SetString(PyExc_ValueError,
266 "chr() arg not in range(256)");
Guido van Rossum3f5da241990-12-20 15:06:42 +0000267 return NULL;
268 }
Guido van Rossum6bf62da1997-04-11 20:37:35 +0000269 s[0] = (char)x;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000270 return PyString_FromStringAndSize(s, 1);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000271}
272
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000273static char chr_doc[] =
274"chr(i) -> character\n\
275\n\
276Return a string of one character with ordinal i; 0 <= i < 256.";
277
278
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000279#ifdef Py_USING_UNICODE
Guido van Rossum79f25d91997-04-29 20:08:16 +0000280static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000281builtin_unichr(PyObject *self, PyObject *args)
Guido van Rossum09095f32000-03-10 23:00:52 +0000282{
283 long x;
Fredrik Lundh0dcf67e2001-06-26 20:01:56 +0000284 Py_UNICODE s[2];
Guido van Rossum09095f32000-03-10 23:00:52 +0000285
286 if (!PyArg_ParseTuple(args, "l:unichr", &x))
287 return NULL;
Fredrik Lundh0dcf67e2001-06-26 20:01:56 +0000288
Marc-André Lemburgae21df52001-07-26 16:29:25 +0000289#ifdef Py_UNICODE_WIDE
Fredrik Lundh0dcf67e2001-06-26 20:01:56 +0000290 if (x < 0 || x > 0x10ffff) {
Guido van Rossum09095f32000-03-10 23:00:52 +0000291 PyErr_SetString(PyExc_ValueError,
Marc-André Lemburgae21df52001-07-26 16:29:25 +0000292 "unichr() arg not in range(0x110000) "
293 "(wide Python build)");
Guido van Rossum09095f32000-03-10 23:00:52 +0000294 return NULL;
295 }
Marc-André Lemburgae21df52001-07-26 16:29:25 +0000296#else
297 if (x < 0 || x > 0xffff) {
298 PyErr_SetString(PyExc_ValueError,
299 "unichr() arg not in range(0x10000) "
300 "(narrow Python build)");
301 return NULL;
302 }
303#endif
Fredrik Lundh0dcf67e2001-06-26 20:01:56 +0000304
305 if (x <= 0xffff) {
306 /* UCS-2 character */
307 s[0] = (Py_UNICODE) x;
308 return PyUnicode_FromUnicode(s, 1);
Guido van Rossum236d8b72001-06-26 23:12:25 +0000309 }
310 else {
Fredrik Lundh8f455852001-06-27 18:59:43 +0000311#ifndef Py_UNICODE_WIDE
Fredrik Lundh0dcf67e2001-06-26 20:01:56 +0000312 /* UCS-4 character. store as two surrogate characters */
313 x -= 0x10000L;
314 s[0] = 0xD800 + (Py_UNICODE) (x >> 10);
315 s[1] = 0xDC00 + (Py_UNICODE) (x & 0x03FF);
316 return PyUnicode_FromUnicode(s, 2);
Guido van Rossum236d8b72001-06-26 23:12:25 +0000317#else
318 s[0] = (Py_UNICODE)x;
319 return PyUnicode_FromUnicode(s, 1);
Martin v. Löwis0ba70cc2001-06-26 22:22:37 +0000320#endif
Fredrik Lundh0dcf67e2001-06-26 20:01:56 +0000321 }
Guido van Rossum09095f32000-03-10 23:00:52 +0000322}
323
324static char unichr_doc[] =
Fred Drake078b24f2000-04-13 02:42:50 +0000325"unichr(i) -> Unicode character\n\
Guido van Rossum09095f32000-03-10 23:00:52 +0000326\n\
Fredrik Lundh0dcf67e2001-06-26 20:01:56 +0000327Return a Unicode string of one character with ordinal i; 0 <= i <= 0x10ffff.";
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000328#endif
Guido van Rossum09095f32000-03-10 23:00:52 +0000329
330
331static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000332builtin_cmp(PyObject *self, PyObject *args)
Guido van Rossuma9e7dc11992-10-18 18:53:57 +0000333{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000334 PyObject *a, *b;
Guido van Rossum09df08a1998-05-22 00:51:39 +0000335 int c;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000336
Guido van Rossum79f25d91997-04-29 20:08:16 +0000337 if (!PyArg_ParseTuple(args, "OO:cmp", &a, &b))
Guido van Rossuma9e7dc11992-10-18 18:53:57 +0000338 return NULL;
Guido van Rossum09df08a1998-05-22 00:51:39 +0000339 if (PyObject_Cmp(a, b, &c) < 0)
Guido van Rossumc8b6df91997-05-23 00:06:51 +0000340 return NULL;
Guido van Rossum09df08a1998-05-22 00:51:39 +0000341 return PyInt_FromLong((long)c);
Guido van Rossuma9e7dc11992-10-18 18:53:57 +0000342}
343
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000344static char cmp_doc[] =
345"cmp(x, y) -> integer\n\
346\n\
347Return negative if x<y, zero if x==y, positive if x>y.";
348
349
Guido van Rossum79f25d91997-04-29 20:08:16 +0000350static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000351builtin_coerce(PyObject *self, PyObject *args)
Guido van Rossum6a00cd81995-01-07 12:39:01 +0000352{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000353 PyObject *v, *w;
354 PyObject *res;
Guido van Rossum5524a591995-01-10 15:26:20 +0000355
Guido van Rossum79f25d91997-04-29 20:08:16 +0000356 if (!PyArg_ParseTuple(args, "OO:coerce", &v, &w))
Guido van Rossum5524a591995-01-10 15:26:20 +0000357 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000358 if (PyNumber_Coerce(&v, &w) < 0)
Guido van Rossum04691fc1992-08-12 15:35:34 +0000359 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000360 res = Py_BuildValue("(OO)", v, w);
361 Py_DECREF(v);
362 Py_DECREF(w);
Guido van Rossum04691fc1992-08-12 15:35:34 +0000363 return res;
364}
365
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000366static char coerce_doc[] =
367"coerce(x, y) -> None or (x1, y1)\n\
368\n\
369When x and y can be coerced to values of the same type, return a tuple\n\
370containing the coerced values. When they can't be coerced, return None.";
371
372
Guido van Rossum79f25d91997-04-29 20:08:16 +0000373static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000374builtin_compile(PyObject *self, PyObject *args)
Guido van Rossum5b722181993-03-30 17:46:03 +0000375{
376 char *str;
377 char *filename;
378 char *startstr;
379 int start;
Tim Peters6cd6a822001-08-17 22:11:27 +0000380 int dont_inherit = 0;
381 int supplied_flags = 0;
Tim Peters5ba58662001-07-16 02:29:45 +0000382 PyCompilerFlags cf;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000383
Tim Peters6cd6a822001-08-17 22:11:27 +0000384 if (!PyArg_ParseTuple(args, "sss|ii:compile", &str, &filename,
385 &startstr, &supplied_flags, &dont_inherit))
Guido van Rossum5b722181993-03-30 17:46:03 +0000386 return NULL;
Tim Peters6cd6a822001-08-17 22:11:27 +0000387
Guido van Rossum5b722181993-03-30 17:46:03 +0000388 if (strcmp(startstr, "exec") == 0)
Guido van Rossumb05a5c71997-05-07 17:46:13 +0000389 start = Py_file_input;
Guido van Rossum5b722181993-03-30 17:46:03 +0000390 else if (strcmp(startstr, "eval") == 0)
Guido van Rossumb05a5c71997-05-07 17:46:13 +0000391 start = Py_eval_input;
Guido van Rossum872537c1995-07-07 22:43:42 +0000392 else if (strcmp(startstr, "single") == 0)
Guido van Rossumb05a5c71997-05-07 17:46:13 +0000393 start = Py_single_input;
Guido van Rossum5b722181993-03-30 17:46:03 +0000394 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000395 PyErr_SetString(PyExc_ValueError,
Fred Drake661ea262000-10-24 19:57:45 +0000396 "compile() arg 3 must be 'exec' or 'eval' or 'single'");
Guido van Rossum5b722181993-03-30 17:46:03 +0000397 return NULL;
398 }
Tim Peters6cd6a822001-08-17 22:11:27 +0000399
400 if (supplied_flags & ~(PyCF_MASK | PyCF_MASK_OBSOLETE)) {
401 PyErr_SetString(PyExc_ValueError,
402 "compile(): unrecognised flags");
403 return NULL;
404 }
405 /* XXX Warn if (supplied_flags & PyCF_MASK_OBSOLETE) != 0? */
406
407 cf.cf_flags = supplied_flags;
408 if (!dont_inherit) {
409 PyEval_MergeCompilerFlags(&cf);
410 }
411 return Py_CompileStringFlags(str, filename, start, &cf);
Guido van Rossum5b722181993-03-30 17:46:03 +0000412}
413
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000414static char compile_doc[] =
Tim Peters6cd6a822001-08-17 22:11:27 +0000415"compile(source, filename, mode[, flags[, dont_inherit]]) -> code object\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000416\n\
417Compile the source string (a Python module, statement or expression)\n\
418into a code object that can be executed by the exec statement or eval().\n\
419The filename will be used for run-time error messages.\n\
420The mode must be 'exec' to compile a module, 'single' to compile a\n\
Tim Peters6cd6a822001-08-17 22:11:27 +0000421single (interactive) statement, or 'eval' to compile an expression.\n\
422The flags argument, if present, controls which future statements influence\n\
423the compilation of the code.\n\
424The dont_inherit argument, if non-zero, stops the compilation inheriting\n\
425the effects of any future statements in effect in the code calling\n\
426compile; if absent or zero these statements do influence the compilation,\n\
427in addition to any features explicitly specified.";
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000428
Guido van Rossum79f25d91997-04-29 20:08:16 +0000429static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000430builtin_dir(PyObject *self, PyObject *args)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000431{
Tim Peters5d2b77c2001-09-03 05:47:38 +0000432 PyObject *arg = NULL;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000433
Tim Peters5d2b77c2001-09-03 05:47:38 +0000434 if (!PyArg_ParseTuple(args, "|O:dir", &arg))
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000435 return NULL;
Tim Peters7eea37e2001-09-04 22:08:56 +0000436 return PyObject_Dir(arg);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000437}
438
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000439static char dir_doc[] =
Tim Peters5d2b77c2001-09-03 05:47:38 +0000440"dir([object]) -> list of strings\n"
441"\n"
442"Return an alphabetized list of names comprising (some of) the attributes\n"
443"of the given object, and of attributes reachable from it:\n"
444"\n"
445"No argument: the names in the current scope.\n"
446"Module object: the module attributes.\n"
Tim Peters37a309d2001-09-04 01:20:04 +0000447"Type or class object: its attributes, and recursively the attributes of\n"
448" its bases.\n"
Tim Peters5d2b77c2001-09-03 05:47:38 +0000449"Otherwise: its attributes, its class's attributes, and recursively the\n"
450" attributes of its class's base classes.";
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000451
Guido van Rossum79f25d91997-04-29 20:08:16 +0000452static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000453builtin_divmod(PyObject *self, PyObject *args)
Guido van Rossum6a00cd81995-01-07 12:39:01 +0000454{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000455 PyObject *v, *w;
Guido van Rossum6a00cd81995-01-07 12:39:01 +0000456
Guido van Rossum79f25d91997-04-29 20:08:16 +0000457 if (!PyArg_ParseTuple(args, "OO:divmod", &v, &w))
Guido van Rossum6a00cd81995-01-07 12:39:01 +0000458 return NULL;
Guido van Rossum09df08a1998-05-22 00:51:39 +0000459 return PyNumber_Divmod(v, w);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000460}
461
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000462static char divmod_doc[] =
463"divmod(x, y) -> (div, mod)\n\
464\n\
465Return the tuple ((x-x%y)/y, x%y). Invariant: div*y + mod == x.";
466
467
Guido van Rossum79f25d91997-04-29 20:08:16 +0000468static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000469builtin_eval(PyObject *self, PyObject *args)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000470{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000471 PyObject *cmd;
472 PyObject *globals = Py_None, *locals = Py_None;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000473 char *str;
Tim Peters9fa96be2001-08-17 23:04:59 +0000474 PyCompilerFlags cf;
Guido van Rossum590baa41993-11-30 13:40:46 +0000475
Guido van Rossum79f25d91997-04-29 20:08:16 +0000476 if (!PyArg_ParseTuple(args, "O|O!O!:eval",
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000477 &cmd,
Guido van Rossum79f25d91997-04-29 20:08:16 +0000478 &PyDict_Type, &globals,
479 &PyDict_Type, &locals))
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000480 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000481 if (globals == Py_None) {
482 globals = PyEval_GetGlobals();
483 if (locals == Py_None)
484 locals = PyEval_GetLocals();
Guido van Rossum6135a871995-01-09 17:53:26 +0000485 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000486 else if (locals == Py_None)
Guido van Rossum6135a871995-01-09 17:53:26 +0000487 locals = globals;
Tim Peters9fa96be2001-08-17 23:04:59 +0000488
Guido van Rossum79f25d91997-04-29 20:08:16 +0000489 if (PyDict_GetItemString(globals, "__builtins__") == NULL) {
490 if (PyDict_SetItemString(globals, "__builtins__",
491 PyEval_GetBuiltins()) != 0)
Guido van Rossum6135a871995-01-09 17:53:26 +0000492 return NULL;
493 }
Tim Peters9fa96be2001-08-17 23:04:59 +0000494
Jeremy Hylton15c1c4f2001-07-30 21:50:55 +0000495 if (PyCode_Check(cmd)) {
Jeremy Hylton733c8932001-12-13 19:51:56 +0000496 if (PyCode_GetNumFree((PyCodeObject *)cmd) > 0) {
Jeremy Hylton15c1c4f2001-07-30 21:50:55 +0000497 PyErr_SetString(PyExc_TypeError,
498 "code object passed to eval() may not contain free variables");
499 return NULL;
500 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000501 return PyEval_EvalCode((PyCodeObject *) cmd, globals, locals);
Jeremy Hylton15c1c4f2001-07-30 21:50:55 +0000502 }
Tim Peters9fa96be2001-08-17 23:04:59 +0000503
Marc-André Lemburgd1ba4432000-09-19 21:04:18 +0000504 if (!PyString_Check(cmd) &&
505 !PyUnicode_Check(cmd)) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000506 PyErr_SetString(PyExc_TypeError,
Fred Drake661ea262000-10-24 19:57:45 +0000507 "eval() arg 1 must be a string or code object");
Guido van Rossum94390a41992-08-14 15:14:30 +0000508 return NULL;
509 }
Marc-André Lemburgd1ba4432000-09-19 21:04:18 +0000510 if (PyString_AsStringAndSize(cmd, &str, NULL))
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000511 return NULL;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000512 while (*str == ' ' || *str == '\t')
513 str++;
Tim Peters9fa96be2001-08-17 23:04:59 +0000514
515 cf.cf_flags = 0;
516 (void)PyEval_MergeCompilerFlags(&cf);
517 return PyRun_StringFlags(str, Py_eval_input, globals, locals, &cf);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000518}
519
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000520static char eval_doc[] =
521"eval(source[, globals[, locals]]) -> value\n\
522\n\
523Evaluate the source in the context of globals and locals.\n\
524The source may be a string representing a Python expression\n\
525or a code object as returned by compile().\n\
526The globals and locals are dictionaries, defaulting to the current\n\
527globals and locals. If only globals is given, locals defaults to it.";
528
529
Guido van Rossum79f25d91997-04-29 20:08:16 +0000530static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000531builtin_execfile(PyObject *self, PyObject *args)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000532{
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000533 char *filename;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000534 PyObject *globals = Py_None, *locals = Py_None;
535 PyObject *res;
Guido van Rossumb3a639e2001-09-05 13:37:47 +0000536 FILE* fp = NULL;
Tim Peters5ba58662001-07-16 02:29:45 +0000537 PyCompilerFlags cf;
Martin v. Löwis6b3a2c42001-08-08 05:30:36 +0000538 int exists;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000539
Guido van Rossum79f25d91997-04-29 20:08:16 +0000540 if (!PyArg_ParseTuple(args, "s|O!O!:execfile",
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000541 &filename,
Guido van Rossum79f25d91997-04-29 20:08:16 +0000542 &PyDict_Type, &globals,
543 &PyDict_Type, &locals))
Guido van Rossum0f61f8a1992-02-25 18:55:05 +0000544 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000545 if (globals == Py_None) {
546 globals = PyEval_GetGlobals();
547 if (locals == Py_None)
548 locals = PyEval_GetLocals();
Guido van Rossum6135a871995-01-09 17:53:26 +0000549 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000550 else if (locals == Py_None)
Guido van Rossum6135a871995-01-09 17:53:26 +0000551 locals = globals;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000552 if (PyDict_GetItemString(globals, "__builtins__") == NULL) {
553 if (PyDict_SetItemString(globals, "__builtins__",
554 PyEval_GetBuiltins()) != 0)
Guido van Rossum6135a871995-01-09 17:53:26 +0000555 return NULL;
556 }
Martin v. Löwis6b3a2c42001-08-08 05:30:36 +0000557
558 exists = 0;
559 /* Test for existence or directory. */
Martin v. Löwis3484a182002-03-09 12:07:51 +0000560#if defined(PLAN9)
561 {
562 Dir *d;
563
564 if ((d = dirstat(filename))!=nil) {
565 if(d->mode & DMDIR)
566 werrstr("is a directory");
567 else
568 exists = 1;
569 free(d);
570 }
Martin v. Löwis6b3a2c42001-08-08 05:30:36 +0000571 }
Martin v. Löwis3484a182002-03-09 12:07:51 +0000572#elif defined(RISCOS)
Guido van Rossume2ae77b2001-10-24 20:42:55 +0000573 if (object_exists(filename)) {
574 if (isdir(filename))
575 errno = EISDIR;
576 else
577 exists = 1;
578 }
Martin v. Löwis3484a182002-03-09 12:07:51 +0000579#else /* standard Posix */
580 {
581 struct stat s;
582 if (stat(filename, &s) == 0) {
583 if (S_ISDIR(s.st_mode))
584# if defined(PY_OS2) && defined(PYCC_VACPP)
585 errno = EOS2ERR;
586# else
587 errno = EISDIR;
588# endif
589 else
590 exists = 1;
591 }
592 }
593#endif
Martin v. Löwis6b3a2c42001-08-08 05:30:36 +0000594
595 if (exists) {
596 Py_BEGIN_ALLOW_THREADS
Jack Jansen7b8c7542002-04-14 20:12:41 +0000597 fp = fopen(filename, "r" PY_STDIOTEXTMODE);
Martin v. Löwis6b3a2c42001-08-08 05:30:36 +0000598 Py_END_ALLOW_THREADS
599
600 if (fp == NULL) {
601 exists = 0;
602 }
603 }
604
605 if (!exists) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000606 PyErr_SetFromErrno(PyExc_IOError);
Guido van Rossum0f61f8a1992-02-25 18:55:05 +0000607 return NULL;
608 }
Tim Peters5ba58662001-07-16 02:29:45 +0000609 cf.cf_flags = 0;
610 if (PyEval_MergeCompilerFlags(&cf))
Tim Peters748b8bb2001-04-28 08:20:22 +0000611 res = PyRun_FileExFlags(fp, filename, Py_file_input, globals,
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000612 locals, 1, &cf);
Tim Peters5ba58662001-07-16 02:29:45 +0000613 else
Tim Peters748b8bb2001-04-28 08:20:22 +0000614 res = PyRun_FileEx(fp, filename, Py_file_input, globals,
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000615 locals, 1);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000616 return res;
Guido van Rossum0f61f8a1992-02-25 18:55:05 +0000617}
618
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000619static char execfile_doc[] =
620"execfile(filename[, globals[, locals]])\n\
621\n\
622Read and execute a Python script from a file.\n\
623The globals and locals are dictionaries, defaulting to the current\n\
624globals and locals. If only globals is given, locals defaults to it.";
625
626
Guido van Rossum79f25d91997-04-29 20:08:16 +0000627static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000628builtin_getattr(PyObject *self, PyObject *args)
Guido van Rossum33894be1992-01-27 16:53:09 +0000629{
Guido van Rossum950ff291998-06-29 13:38:57 +0000630 PyObject *v, *result, *dflt = NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000631 PyObject *name;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000632
Marc-André Lemburg691270f2000-09-18 16:22:27 +0000633 if (!PyArg_ParseTuple(args, "OO|O:getattr", &v, &name, &dflt))
Guido van Rossum33894be1992-01-27 16:53:09 +0000634 return NULL;
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000635#ifdef Py_USING_UNICODE
Jeremy Hylton0eb11152001-07-30 22:39:31 +0000636 if (PyUnicode_Check(name)) {
637 name = _PyUnicode_AsDefaultEncodedString(name, NULL);
638 if (name == NULL)
639 return NULL;
640 }
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000641#endif
Jeremy Hylton0eb11152001-07-30 22:39:31 +0000642
643 if (!PyString_Check(name)) {
644 PyErr_SetString(PyExc_TypeError,
645 "attribute name must be string");
646 return NULL;
647 }
Guido van Rossum950ff291998-06-29 13:38:57 +0000648 result = PyObject_GetAttr(v, name);
Guido van Rossumd8923572001-10-16 21:31:32 +0000649 if (result == NULL && dflt != NULL &&
650 PyErr_ExceptionMatches(PyExc_AttributeError))
651 {
Guido van Rossum950ff291998-06-29 13:38:57 +0000652 PyErr_Clear();
653 Py_INCREF(dflt);
654 result = dflt;
655 }
656 return result;
Guido van Rossum9bfef441993-03-29 10:43:31 +0000657}
658
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000659static char getattr_doc[] =
Guido van Rossum950ff291998-06-29 13:38:57 +0000660"getattr(object, name[, default]) -> value\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000661\n\
Guido van Rossum950ff291998-06-29 13:38:57 +0000662Get a named attribute from an object; getattr(x, 'y') is equivalent to x.y.\n\
663When a default argument is given, it is returned when the attribute doesn't\n\
664exist; without it, an exception is raised in that case.";
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000665
666
Guido van Rossum79f25d91997-04-29 20:08:16 +0000667static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000668builtin_globals(PyObject *self)
Guido van Rossum872537c1995-07-07 22:43:42 +0000669{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000670 PyObject *d;
Guido van Rossum872537c1995-07-07 22:43:42 +0000671
Guido van Rossum79f25d91997-04-29 20:08:16 +0000672 d = PyEval_GetGlobals();
673 Py_INCREF(d);
Guido van Rossum872537c1995-07-07 22:43:42 +0000674 return d;
675}
676
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000677static char globals_doc[] =
678"globals() -> dictionary\n\
679\n\
680Return the dictionary containing the current scope's global variables.";
681
682
Guido van Rossum79f25d91997-04-29 20:08:16 +0000683static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000684builtin_hasattr(PyObject *self, PyObject *args)
Guido van Rossum9bfef441993-03-29 10:43:31 +0000685{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000686 PyObject *v;
687 PyObject *name;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000688
Marc-André Lemburg691270f2000-09-18 16:22:27 +0000689 if (!PyArg_ParseTuple(args, "OO:hasattr", &v, &name))
Guido van Rossum9bfef441993-03-29 10:43:31 +0000690 return NULL;
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000691#ifdef Py_USING_UNICODE
Jeremy Hylton302b54a2001-07-30 22:45:19 +0000692 if (PyUnicode_Check(name)) {
693 name = _PyUnicode_AsDefaultEncodedString(name, NULL);
694 if (name == NULL)
695 return NULL;
696 }
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000697#endif
Jeremy Hylton302b54a2001-07-30 22:45:19 +0000698
699 if (!PyString_Check(name)) {
700 PyErr_SetString(PyExc_TypeError,
701 "attribute name must be string");
702 return NULL;
703 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000704 v = PyObject_GetAttr(v, name);
Guido van Rossum9bfef441993-03-29 10:43:31 +0000705 if (v == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000706 PyErr_Clear();
Guido van Rossum09df08a1998-05-22 00:51:39 +0000707 Py_INCREF(Py_False);
708 return Py_False;
Guido van Rossum9bfef441993-03-29 10:43:31 +0000709 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000710 Py_DECREF(v);
Guido van Rossum09df08a1998-05-22 00:51:39 +0000711 Py_INCREF(Py_True);
712 return Py_True;
Guido van Rossum33894be1992-01-27 16:53:09 +0000713}
714
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000715static char hasattr_doc[] =
Guido van Rossum77f6a652002-04-03 22:41:51 +0000716"hasattr(object, name) -> bool\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000717\n\
718Return whether the object has an attribute with the given name.\n\
719(This is done by calling getattr(object, name) and catching exceptions.)";
720
721
Guido van Rossum79f25d91997-04-29 20:08:16 +0000722static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000723builtin_id(PyObject *self, PyObject *v)
Guido van Rossum5b722181993-03-30 17:46:03 +0000724{
Guido van Rossum106f2da2000-06-28 21:12:25 +0000725 return PyLong_FromVoidPtr(v);
Guido van Rossum5b722181993-03-30 17:46:03 +0000726}
727
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000728static char id_doc[] =
729"id(object) -> integer\n\
730\n\
731Return the identity of an object. This is guaranteed to be unique among\n\
732simultaneously existing objects. (Hint: it's the object's memory address.)";
733
734
Guido van Rossum79f25d91997-04-29 20:08:16 +0000735static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000736builtin_map(PyObject *self, PyObject *args)
Guido van Rossum12d12c51993-10-26 17:58:25 +0000737{
738 typedef struct {
Tim Peters4e9afdc2001-05-03 23:54:49 +0000739 PyObject *it; /* the iterator object */
740 int saw_StopIteration; /* bool: did the iterator end? */
Guido van Rossum12d12c51993-10-26 17:58:25 +0000741 } sequence;
742
Guido van Rossum79f25d91997-04-29 20:08:16 +0000743 PyObject *func, *result;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000744 sequence *seqs = NULL, *sqp;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000745 int n, len;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000746 register int i, j;
747
Guido van Rossum79f25d91997-04-29 20:08:16 +0000748 n = PyTuple_Size(args);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000749 if (n < 2) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000750 PyErr_SetString(PyExc_TypeError,
751 "map() requires at least two args");
Guido van Rossum12d12c51993-10-26 17:58:25 +0000752 return NULL;
753 }
754
Guido van Rossum79f25d91997-04-29 20:08:16 +0000755 func = PyTuple_GetItem(args, 0);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000756 n--;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000757
Guido van Rossumfa4ac711998-07-10 17:37:30 +0000758 if (func == Py_None && n == 1) {
759 /* map(None, S) is the same as list(S). */
760 return PySequence_List(PyTuple_GetItem(args, 1));
761 }
762
Tim Peters4e9afdc2001-05-03 23:54:49 +0000763 /* Get space for sequence descriptors. Must NULL out the iterator
764 * pointers so that jumping to Fail_2 later doesn't see trash.
765 */
Guido van Rossum79f25d91997-04-29 20:08:16 +0000766 if ((seqs = PyMem_NEW(sequence, n)) == NULL) {
767 PyErr_NoMemory();
Tim Peters4e9afdc2001-05-03 23:54:49 +0000768 return NULL;
769 }
770 for (i = 0; i < n; ++i) {
771 seqs[i].it = (PyObject*)NULL;
772 seqs[i].saw_StopIteration = 0;
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000773 }
Guido van Rossum12d12c51993-10-26 17:58:25 +0000774
Tim Peters4e9afdc2001-05-03 23:54:49 +0000775 /* Do a first pass to obtain iterators for the arguments, and set len
776 * to the largest of their lengths.
Tim Peters748b8bb2001-04-28 08:20:22 +0000777 */
Tim Peters4e9afdc2001-05-03 23:54:49 +0000778 len = 0;
779 for (i = 0, sqp = seqs; i < n; ++i, ++sqp) {
780 PyObject *curseq;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000781 int curlen;
Guido van Rossumad991772001-01-12 16:03:05 +0000782
Tim Peters4e9afdc2001-05-03 23:54:49 +0000783 /* Get iterator. */
784 curseq = PyTuple_GetItem(args, i+1);
785 sqp->it = PyObject_GetIter(curseq);
786 if (sqp->it == NULL) {
Guido van Rossum12d12c51993-10-26 17:58:25 +0000787 static char errmsg[] =
Tim Peters4e9afdc2001-05-03 23:54:49 +0000788 "argument %d to map() must support iteration";
Guido van Rossum15e33a41997-04-30 19:00:27 +0000789 char errbuf[sizeof(errmsg) + 25];
Jeremy Hylton518ab1c2001-11-28 20:42:20 +0000790 PyOS_snprintf(errbuf, sizeof(errbuf), errmsg, i+2);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000791 PyErr_SetString(PyExc_TypeError, errbuf);
Guido van Rossum12d12c51993-10-26 17:58:25 +0000792 goto Fail_2;
793 }
794
Tim Peters4e9afdc2001-05-03 23:54:49 +0000795 /* Update len. */
796 curlen = -1; /* unknown */
797 if (PySequence_Check(curseq) &&
798 curseq->ob_type->tp_as_sequence->sq_length) {
799 curlen = PySequence_Size(curseq);
800 if (curlen < 0)
801 PyErr_Clear();
802 }
Tim Peters748b8bb2001-04-28 08:20:22 +0000803 if (curlen < 0)
Tim Peters4e9afdc2001-05-03 23:54:49 +0000804 curlen = 8; /* arbitrary */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000805 if (curlen > len)
806 len = curlen;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000807 }
808
Tim Peters4e9afdc2001-05-03 23:54:49 +0000809 /* Get space for the result list. */
Guido van Rossum79f25d91997-04-29 20:08:16 +0000810 if ((result = (PyObject *) PyList_New(len)) == NULL)
Guido van Rossum12d12c51993-10-26 17:58:25 +0000811 goto Fail_2;
812
Tim Peters4e9afdc2001-05-03 23:54:49 +0000813 /* Iterate over the sequences until all have stopped. */
Guido van Rossum2d951851994-08-29 12:52:16 +0000814 for (i = 0; ; ++i) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000815 PyObject *alist, *item=NULL, *value;
Tim Peters4e9afdc2001-05-03 23:54:49 +0000816 int numactive = 0;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000817
Guido van Rossum79f25d91997-04-29 20:08:16 +0000818 if (func == Py_None && n == 1)
Guido van Rossum32120311995-07-10 13:52:21 +0000819 alist = NULL;
Tim Peters4e9afdc2001-05-03 23:54:49 +0000820 else if ((alist = PyTuple_New(n)) == NULL)
821 goto Fail_1;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000822
823 for (j = 0, sqp = seqs; j < n; ++j, ++sqp) {
Tim Peters4e9afdc2001-05-03 23:54:49 +0000824 if (sqp->saw_StopIteration) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000825 Py_INCREF(Py_None);
826 item = Py_None;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000827 }
Guido van Rossum12d12c51993-10-26 17:58:25 +0000828 else {
Tim Peters4e9afdc2001-05-03 23:54:49 +0000829 item = PyIter_Next(sqp->it);
830 if (item)
831 ++numactive;
832 else {
Tim Peters4e9afdc2001-05-03 23:54:49 +0000833 if (PyErr_Occurred()) {
Tim Petersf4848da2001-05-05 00:14:56 +0000834 Py_XDECREF(alist);
835 goto Fail_1;
Guido van Rossum2d951851994-08-29 12:52:16 +0000836 }
Tim Peters4e9afdc2001-05-03 23:54:49 +0000837 Py_INCREF(Py_None);
838 item = Py_None;
839 sqp->saw_StopIteration = 1;
Guido van Rossum2d951851994-08-29 12:52:16 +0000840 }
Guido van Rossum12d12c51993-10-26 17:58:25 +0000841 }
Tim Peters4e9afdc2001-05-03 23:54:49 +0000842 if (alist)
843 PyTuple_SET_ITEM(alist, j, item);
844 else
Guido van Rossum2d951851994-08-29 12:52:16 +0000845 break;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000846 }
847
Guido van Rossum32120311995-07-10 13:52:21 +0000848 if (!alist)
849 alist = item;
Guido van Rossum2d951851994-08-29 12:52:16 +0000850
Tim Peters4e9afdc2001-05-03 23:54:49 +0000851 if (numactive == 0) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000852 Py_DECREF(alist);
Guido van Rossum2d951851994-08-29 12:52:16 +0000853 break;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000854 }
Guido van Rossum2d951851994-08-29 12:52:16 +0000855
Guido van Rossum79f25d91997-04-29 20:08:16 +0000856 if (func == Py_None)
Guido van Rossum32120311995-07-10 13:52:21 +0000857 value = alist;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000858 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000859 value = PyEval_CallObject(func, alist);
860 Py_DECREF(alist);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000861 if (value == NULL)
862 goto Fail_1;
Guido van Rossum2d951851994-08-29 12:52:16 +0000863 }
864 if (i >= len) {
Barry Warsawfa77e091999-01-28 18:49:12 +0000865 int status = PyList_Append(result, value);
Barry Warsaw21332871999-01-28 04:21:35 +0000866 Py_DECREF(value);
Barry Warsawfa77e091999-01-28 18:49:12 +0000867 if (status < 0)
868 goto Fail_1;
Guido van Rossum2d951851994-08-29 12:52:16 +0000869 }
Tim Peters4e9afdc2001-05-03 23:54:49 +0000870 else if (PyList_SetItem(result, i, value) < 0)
871 goto Fail_1;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000872 }
873
Guido van Rossumfa4ac711998-07-10 17:37:30 +0000874 if (i < len && PyList_SetSlice(result, i, len, NULL) < 0)
875 goto Fail_1;
876
Tim Peters4e9afdc2001-05-03 23:54:49 +0000877 goto Succeed;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000878
Guido van Rossum12d12c51993-10-26 17:58:25 +0000879Fail_1:
Guido van Rossum79f25d91997-04-29 20:08:16 +0000880 Py_DECREF(result);
Guido van Rossum12d12c51993-10-26 17:58:25 +0000881Fail_2:
Tim Peters4e9afdc2001-05-03 23:54:49 +0000882 result = NULL;
883Succeed:
884 assert(seqs);
885 for (i = 0; i < n; ++i)
886 Py_XDECREF(seqs[i].it);
887 PyMem_DEL(seqs);
888 return result;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000889}
890
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000891static char map_doc[] =
892"map(function, sequence[, sequence, ...]) -> list\n\
893\n\
894Return a list of the results of applying the function to the items of\n\
895the argument sequence(s). If more than one sequence is given, the\n\
896function is called with an argument list consisting of the corresponding\n\
897item of each sequence, substituting None for missing values when not all\n\
898sequences have the same length. If the function is None, return a list of\n\
899the items of the sequence (or a list of tuples if more than one sequence).";
900
901
Guido van Rossum79f25d91997-04-29 20:08:16 +0000902static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000903builtin_setattr(PyObject *self, PyObject *args)
Guido van Rossum33894be1992-01-27 16:53:09 +0000904{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000905 PyObject *v;
906 PyObject *name;
907 PyObject *value;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000908
Marc-André Lemburg691270f2000-09-18 16:22:27 +0000909 if (!PyArg_ParseTuple(args, "OOO:setattr", &v, &name, &value))
Guido van Rossum33894be1992-01-27 16:53:09 +0000910 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000911 if (PyObject_SetAttr(v, name, value) != 0)
Guido van Rossum33894be1992-01-27 16:53:09 +0000912 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000913 Py_INCREF(Py_None);
914 return Py_None;
Guido van Rossum33894be1992-01-27 16:53:09 +0000915}
916
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000917static char setattr_doc[] =
918"setattr(object, name, value)\n\
919\n\
920Set a named attribute on an object; setattr(x, 'y', v) is equivalent to\n\
921``x.y = v''.";
922
923
Guido van Rossum79f25d91997-04-29 20:08:16 +0000924static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000925builtin_delattr(PyObject *self, PyObject *args)
Guido van Rossum14144fc1994-08-29 12:53:40 +0000926{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000927 PyObject *v;
928 PyObject *name;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000929
Marc-André Lemburg691270f2000-09-18 16:22:27 +0000930 if (!PyArg_ParseTuple(args, "OO:delattr", &v, &name))
Guido van Rossum14144fc1994-08-29 12:53:40 +0000931 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000932 if (PyObject_SetAttr(v, name, (PyObject *)NULL) != 0)
Guido van Rossum14144fc1994-08-29 12:53:40 +0000933 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000934 Py_INCREF(Py_None);
935 return Py_None;
Guido van Rossum14144fc1994-08-29 12:53:40 +0000936}
937
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000938static char delattr_doc[] =
Guido van Rossumdf12a591998-11-23 22:13:04 +0000939"delattr(object, name)\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000940\n\
941Delete a named attribute on an object; delattr(x, 'y') is equivalent to\n\
942``del x.y''.";
943
944
Guido van Rossum79f25d91997-04-29 20:08:16 +0000945static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000946builtin_hash(PyObject *self, PyObject *v)
Guido van Rossum9bfef441993-03-29 10:43:31 +0000947{
Guido van Rossum9bfef441993-03-29 10:43:31 +0000948 long x;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000949
Guido van Rossum79f25d91997-04-29 20:08:16 +0000950 x = PyObject_Hash(v);
Guido van Rossum9bfef441993-03-29 10:43:31 +0000951 if (x == -1)
952 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000953 return PyInt_FromLong(x);
Guido van Rossum9bfef441993-03-29 10:43:31 +0000954}
955
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000956static char hash_doc[] =
957"hash(object) -> integer\n\
958\n\
959Return a hash value for the object. Two objects with the same value have\n\
960the same hash value. The reverse is not necessarily true, but likely.";
961
962
Guido van Rossum79f25d91997-04-29 20:08:16 +0000963static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000964builtin_hex(PyObject *self, PyObject *v)
Guido van Rossum006bcd41991-10-24 14:54:44 +0000965{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000966 PyNumberMethods *nb;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000967
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000968 if ((nb = v->ob_type->tp_as_number) == NULL ||
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000969 nb->nb_hex == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000970 PyErr_SetString(PyExc_TypeError,
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000971 "hex() argument can't be converted to hex");
972 return NULL;
Guido van Rossum006bcd41991-10-24 14:54:44 +0000973 }
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000974 return (*nb->nb_hex)(v);
Guido van Rossum006bcd41991-10-24 14:54:44 +0000975}
976
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000977static char hex_doc[] =
978"hex(number) -> string\n\
979\n\
980Return the hexadecimal representation of an integer or long integer.";
981
982
Tim Petersdbd9ba62000-07-09 03:09:57 +0000983static PyObject *builtin_raw_input(PyObject *, PyObject *);
Guido van Rossum3165fe61992-09-25 21:59:05 +0000984
Guido van Rossum79f25d91997-04-29 20:08:16 +0000985static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000986builtin_input(PyObject *self, PyObject *args)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000987{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000988 PyObject *line;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000989 char *str;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000990 PyObject *res;
991 PyObject *globals, *locals;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000992
993 line = builtin_raw_input(self, args);
Guido van Rossum3165fe61992-09-25 21:59:05 +0000994 if (line == NULL)
995 return line;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000996 if (!PyArg_Parse(line, "s;embedded '\\0' in input line", &str))
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000997 return NULL;
998 while (*str == ' ' || *str == '\t')
999 str++;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001000 globals = PyEval_GetGlobals();
1001 locals = PyEval_GetLocals();
1002 if (PyDict_GetItemString(globals, "__builtins__") == NULL) {
1003 if (PyDict_SetItemString(globals, "__builtins__",
1004 PyEval_GetBuiltins()) != 0)
Guido van Rossum6135a871995-01-09 17:53:26 +00001005 return NULL;
1006 }
Guido van Rossumb05a5c71997-05-07 17:46:13 +00001007 res = PyRun_String(str, Py_eval_input, globals, locals);
Guido van Rossum79f25d91997-04-29 20:08:16 +00001008 Py_DECREF(line);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001009 return res;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001010}
1011
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001012static char input_doc[] =
1013"input([prompt]) -> value\n\
1014\n\
1015Equivalent to eval(raw_input(prompt)).";
1016
1017
Guido van Rossume8811f81997-02-14 15:48:05 +00001018static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001019builtin_intern(PyObject *self, PyObject *args)
Guido van Rossume8811f81997-02-14 15:48:05 +00001020{
1021 PyObject *s;
Guido van Rossum43713e52000-02-29 13:59:29 +00001022 if (!PyArg_ParseTuple(args, "S:intern", &s))
Guido van Rossume8811f81997-02-14 15:48:05 +00001023 return NULL;
1024 Py_INCREF(s);
1025 PyString_InternInPlace(&s);
1026 return s;
1027}
1028
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001029static char intern_doc[] =
1030"intern(string) -> string\n\
1031\n\
1032``Intern'' the given string. This enters the string in the (global)\n\
1033table of interned strings whose purpose is to speed up dictionary lookups.\n\
1034Return the string itself or the previously interned string object with the\n\
1035same value.";
1036
1037
Guido van Rossum79f25d91997-04-29 20:08:16 +00001038static PyObject *
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001039builtin_iter(PyObject *self, PyObject *args)
1040{
1041 PyObject *v, *w = NULL;
1042
1043 if (!PyArg_ParseTuple(args, "O|O:iter", &v, &w))
1044 return NULL;
1045 if (w == NULL)
1046 return PyObject_GetIter(v);
1047 if (!PyCallable_Check(v)) {
1048 PyErr_SetString(PyExc_TypeError,
1049 "iter(v, w): v must be callable");
1050 return NULL;
1051 }
1052 return PyCallIter_New(v, w);
1053}
1054
1055static char iter_doc[] =
1056"iter(collection) -> iterator\n\
1057iter(callable, sentinel) -> iterator\n\
1058\n\
1059Get an iterator from an object. In the first form, the argument must\n\
1060supply its own iterator, or be a sequence.\n\
1061In the second form, the callable is called until it returns the sentinel.";
1062
1063
1064static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001065builtin_len(PyObject *self, PyObject *v)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001066{
Guido van Rossum8ea9f4d1998-06-29 22:26:50 +00001067 long res;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001068
Jeremy Hylton03657cf2000-07-12 13:05:33 +00001069 res = PyObject_Size(v);
Guido van Rossum8ea9f4d1998-06-29 22:26:50 +00001070 if (res < 0 && PyErr_Occurred())
1071 return NULL;
1072 return PyInt_FromLong(res);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001073}
1074
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001075static char len_doc[] =
1076"len(object) -> integer\n\
1077\n\
1078Return the number of items of a sequence or mapping.";
1079
1080
Guido van Rossum79f25d91997-04-29 20:08:16 +00001081static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001082builtin_slice(PyObject *self, PyObject *args)
Guido van Rossum8861b741996-07-30 16:49:37 +00001083{
Guido van Rossum09df08a1998-05-22 00:51:39 +00001084 PyObject *start, *stop, *step;
Guido van Rossum8861b741996-07-30 16:49:37 +00001085
Guido van Rossum09df08a1998-05-22 00:51:39 +00001086 start = stop = step = NULL;
Guido van Rossum8861b741996-07-30 16:49:37 +00001087
Guido van Rossum09df08a1998-05-22 00:51:39 +00001088 if (!PyArg_ParseTuple(args, "O|OO:slice", &start, &stop, &step))
1089 return NULL;
Guido van Rossum8861b741996-07-30 16:49:37 +00001090
Guido van Rossum09df08a1998-05-22 00:51:39 +00001091 /* This swapping of stop and start is to maintain similarity with
1092 range(). */
1093 if (stop == NULL) {
1094 stop = start;
1095 start = NULL;
1096 }
1097 return PySlice_New(start, stop, step);
Guido van Rossum8861b741996-07-30 16:49:37 +00001098}
1099
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001100static char slice_doc[] =
Fred Drake3d587441999-07-19 15:21:16 +00001101"slice([start,] stop[, step]) -> slice object\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001102\n\
1103Create a slice object. This is used for slicing by the Numeric extensions.";
1104
1105
Guido van Rossum79f25d91997-04-29 20:08:16 +00001106static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001107builtin_locals(PyObject *self)
Guido van Rossum872537c1995-07-07 22:43:42 +00001108{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001109 PyObject *d;
Guido van Rossum872537c1995-07-07 22:43:42 +00001110
Guido van Rossum79f25d91997-04-29 20:08:16 +00001111 d = PyEval_GetLocals();
1112 Py_INCREF(d);
Guido van Rossum872537c1995-07-07 22:43:42 +00001113 return d;
1114}
1115
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001116static char locals_doc[] =
1117"locals() -> dictionary\n\
1118\n\
1119Return the dictionary containing the current scope's local variables.";
1120
1121
Guido van Rossum79f25d91997-04-29 20:08:16 +00001122static PyObject *
Guido van Rossum53451b32001-01-17 15:47:24 +00001123min_max(PyObject *args, int op)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001124{
Tim Petersc3074532001-05-03 07:00:32 +00001125 PyObject *v, *w, *x, *it;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001126
Guido van Rossum79f25d91997-04-29 20:08:16 +00001127 if (PyTuple_Size(args) > 1)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001128 v = args;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001129 else if (!PyArg_ParseTuple(args, "O:min/max", &v))
Guido van Rossum3f5da241990-12-20 15:06:42 +00001130 return NULL;
Tim Petersc3074532001-05-03 07:00:32 +00001131
1132 it = PyObject_GetIter(v);
1133 if (it == NULL)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001134 return NULL;
Tim Petersc3074532001-05-03 07:00:32 +00001135
1136 w = NULL; /* the result */
Tim Petersf4848da2001-05-05 00:14:56 +00001137 for (;;) {
Tim Petersc3074532001-05-03 07:00:32 +00001138 x = PyIter_Next(it);
Guido van Rossum2d951851994-08-29 12:52:16 +00001139 if (x == NULL) {
Tim Petersc3074532001-05-03 07:00:32 +00001140 if (PyErr_Occurred()) {
Tim Petersf4848da2001-05-05 00:14:56 +00001141 Py_XDECREF(w);
1142 Py_DECREF(it);
1143 return NULL;
Guido van Rossum2d951851994-08-29 12:52:16 +00001144 }
Tim Petersc3074532001-05-03 07:00:32 +00001145 break;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001146 }
Tim Petersc3074532001-05-03 07:00:32 +00001147
Guido van Rossum2d951851994-08-29 12:52:16 +00001148 if (w == NULL)
1149 w = x;
1150 else {
Guido van Rossum53451b32001-01-17 15:47:24 +00001151 int cmp = PyObject_RichCompareBool(x, w, op);
1152 if (cmp > 0) {
1153 Py_DECREF(w);
1154 w = x;
1155 }
1156 else if (cmp < 0) {
Guido van Rossumc8b6df91997-05-23 00:06:51 +00001157 Py_DECREF(x);
Tim Petersc3074532001-05-03 07:00:32 +00001158 Py_DECREF(w);
1159 Py_DECREF(it);
Guido van Rossumc8b6df91997-05-23 00:06:51 +00001160 return NULL;
1161 }
Guido van Rossum2d951851994-08-29 12:52:16 +00001162 else
Guido van Rossum79f25d91997-04-29 20:08:16 +00001163 Py_DECREF(x);
Guido van Rossum2d951851994-08-29 12:52:16 +00001164 }
Guido van Rossum3f5da241990-12-20 15:06:42 +00001165 }
Guido van Rossum2d951851994-08-29 12:52:16 +00001166 if (w == NULL)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001167 PyErr_SetString(PyExc_ValueError,
Fred Drake661ea262000-10-24 19:57:45 +00001168 "min() or max() arg is an empty sequence");
Tim Petersc3074532001-05-03 07:00:32 +00001169 Py_DECREF(it);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001170 return w;
1171}
1172
Guido van Rossum79f25d91997-04-29 20:08:16 +00001173static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001174builtin_min(PyObject *self, PyObject *v)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001175{
Guido van Rossum53451b32001-01-17 15:47:24 +00001176 return min_max(v, Py_LT);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001177}
1178
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001179static char min_doc[] =
1180"min(sequence) -> value\n\
1181min(a, b, c, ...) -> value\n\
1182\n\
1183With a single sequence argument, return its smallest item.\n\
1184With two or more arguments, return the smallest argument.";
1185
1186
Guido van Rossum79f25d91997-04-29 20:08:16 +00001187static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001188builtin_max(PyObject *self, PyObject *v)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001189{
Guido van Rossum53451b32001-01-17 15:47:24 +00001190 return min_max(v, Py_GT);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001191}
1192
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001193static char max_doc[] =
1194"max(sequence) -> value\n\
1195max(a, b, c, ...) -> value\n\
1196\n\
1197With a single sequence argument, return its largest item.\n\
1198With two or more arguments, return the largest argument.";
1199
1200
Guido van Rossum79f25d91997-04-29 20:08:16 +00001201static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001202builtin_oct(PyObject *self, PyObject *v)
Guido van Rossum006bcd41991-10-24 14:54:44 +00001203{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001204 PyNumberMethods *nb;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001205
Guido van Rossum1899c2e1992-09-12 11:09:23 +00001206 if (v == NULL || (nb = v->ob_type->tp_as_number) == NULL ||
1207 nb->nb_oct == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001208 PyErr_SetString(PyExc_TypeError,
Guido van Rossum1899c2e1992-09-12 11:09:23 +00001209 "oct() argument can't be converted to oct");
1210 return NULL;
Guido van Rossum006bcd41991-10-24 14:54:44 +00001211 }
Guido van Rossum1899c2e1992-09-12 11:09:23 +00001212 return (*nb->nb_oct)(v);
Guido van Rossum006bcd41991-10-24 14:54:44 +00001213}
1214
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001215static char oct_doc[] =
1216"oct(number) -> string\n\
1217\n\
1218Return the octal representation of an integer or long integer.";
1219
1220
Guido van Rossum79f25d91997-04-29 20:08:16 +00001221static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001222builtin_ord(PyObject *self, PyObject* obj)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001223{
Guido van Rossum09095f32000-03-10 23:00:52 +00001224 long ord;
Jeremy Hylton394b54d2000-04-12 21:19:47 +00001225 int size;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001226
Jeremy Hylton394b54d2000-04-12 21:19:47 +00001227 if (PyString_Check(obj)) {
1228 size = PyString_GET_SIZE(obj);
Andrew M. Kuchling34c20cf2000-12-20 14:36:56 +00001229 if (size == 1) {
Jeremy Hylton394b54d2000-04-12 21:19:47 +00001230 ord = (long)((unsigned char)*PyString_AS_STRING(obj));
Andrew M. Kuchling34c20cf2000-12-20 14:36:56 +00001231 return PyInt_FromLong(ord);
1232 }
Martin v. Löwis339d0f72001-08-17 18:39:25 +00001233#ifdef Py_USING_UNICODE
Jeremy Hylton394b54d2000-04-12 21:19:47 +00001234 } else if (PyUnicode_Check(obj)) {
1235 size = PyUnicode_GET_SIZE(obj);
Andrew M. Kuchling34c20cf2000-12-20 14:36:56 +00001236 if (size == 1) {
Jeremy Hylton394b54d2000-04-12 21:19:47 +00001237 ord = (long)*PyUnicode_AS_UNICODE(obj);
Andrew M. Kuchling34c20cf2000-12-20 14:36:56 +00001238 return PyInt_FromLong(ord);
1239 }
Martin v. Löwis339d0f72001-08-17 18:39:25 +00001240#endif
Jeremy Hylton394b54d2000-04-12 21:19:47 +00001241 } else {
1242 PyErr_Format(PyExc_TypeError,
Andrew M. Kuchlingf07aad12000-12-23 14:11:28 +00001243 "ord() expected string of length 1, but " \
Jeremy Hylton394b54d2000-04-12 21:19:47 +00001244 "%.200s found", obj->ob_type->tp_name);
Guido van Rossum09095f32000-03-10 23:00:52 +00001245 return NULL;
1246 }
1247
Guido van Rossumad991772001-01-12 16:03:05 +00001248 PyErr_Format(PyExc_TypeError,
Andrew M. Kuchlingf07aad12000-12-23 14:11:28 +00001249 "ord() expected a character, "
1250 "but string of length %d found",
Jeremy Hylton394b54d2000-04-12 21:19:47 +00001251 size);
1252 return NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001253}
1254
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001255static char ord_doc[] =
1256"ord(c) -> integer\n\
1257\n\
Guido van Rossumad991772001-01-12 16:03:05 +00001258Return the integer ordinal of a one-character string.";
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001259
1260
Guido van Rossum79f25d91997-04-29 20:08:16 +00001261static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001262builtin_pow(PyObject *self, PyObject *args)
Guido van Rossum6a00cd81995-01-07 12:39:01 +00001263{
Guido van Rossum09df08a1998-05-22 00:51:39 +00001264 PyObject *v, *w, *z = Py_None;
Guido van Rossum6a00cd81995-01-07 12:39:01 +00001265
Guido van Rossum79f25d91997-04-29 20:08:16 +00001266 if (!PyArg_ParseTuple(args, "OO|O:pow", &v, &w, &z))
Guido van Rossum6a00cd81995-01-07 12:39:01 +00001267 return NULL;
Guido van Rossum09df08a1998-05-22 00:51:39 +00001268 return PyNumber_Power(v, w, z);
Guido van Rossumd4905451991-05-05 20:00:36 +00001269}
1270
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001271static char pow_doc[] =
1272"pow(x, y[, z]) -> number\n\
1273\n\
1274With two arguments, equivalent to x**y. With three arguments,\n\
1275equivalent to (x**y) % z, but may be more efficient (e.g. for longs).";
1276
1277
Guido van Rossum124eff01999-02-23 16:11:01 +00001278/* Return number of items in range/xrange (lo, hi, step). step > 0
1279 * required. Return a value < 0 if & only if the true value is too
1280 * large to fit in a signed long.
1281 */
1282static long
Thomas Wouterse28c2962000-07-23 22:21:32 +00001283get_len_of_range(long lo, long hi, long step)
Guido van Rossum124eff01999-02-23 16:11:01 +00001284{
1285 /* -------------------------------------------------------------
1286 If lo >= hi, the range is empty.
1287 Else if n values are in the range, the last one is
1288 lo + (n-1)*step, which must be <= hi-1. Rearranging,
1289 n <= (hi - lo - 1)/step + 1, so taking the floor of the RHS gives
1290 the proper value. Since lo < hi in this case, hi-lo-1 >= 0, so
1291 the RHS is non-negative and so truncation is the same as the
1292 floor. Letting M be the largest positive long, the worst case
1293 for the RHS numerator is hi=M, lo=-M-1, and then
1294 hi-lo-1 = M-(-M-1)-1 = 2*M. Therefore unsigned long has enough
1295 precision to compute the RHS exactly.
1296 ---------------------------------------------------------------*/
1297 long n = 0;
1298 if (lo < hi) {
1299 unsigned long uhi = (unsigned long)hi;
1300 unsigned long ulo = (unsigned long)lo;
1301 unsigned long diff = uhi - ulo - 1;
1302 n = (long)(diff / (unsigned long)step + 1);
1303 }
1304 return n;
1305}
1306
Guido van Rossum79f25d91997-04-29 20:08:16 +00001307static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001308builtin_range(PyObject *self, PyObject *args)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001309{
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001310 long ilow = 0, ihigh = 0, istep = 1;
Guido van Rossum124eff01999-02-23 16:11:01 +00001311 long bign;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001312 int i, n;
Guido van Rossum124eff01999-02-23 16:11:01 +00001313
Guido van Rossum79f25d91997-04-29 20:08:16 +00001314 PyObject *v;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001315
Guido van Rossum79f25d91997-04-29 20:08:16 +00001316 if (PyTuple_Size(args) <= 1) {
1317 if (!PyArg_ParseTuple(args,
Guido van Rossum0865dd91995-01-17 16:30:22 +00001318 "l;range() requires 1-3 int arguments",
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001319 &ihigh))
1320 return NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001321 }
1322 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001323 if (!PyArg_ParseTuple(args,
Guido van Rossum0865dd91995-01-17 16:30:22 +00001324 "ll|l;range() requires 1-3 int arguments",
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001325 &ilow, &ihigh, &istep))
Guido van Rossum3f5da241990-12-20 15:06:42 +00001326 return NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001327 }
1328 if (istep == 0) {
Fred Drake661ea262000-10-24 19:57:45 +00001329 PyErr_SetString(PyExc_ValueError, "range() arg 3 must not be zero");
Guido van Rossum3f5da241990-12-20 15:06:42 +00001330 return NULL;
1331 }
Guido van Rossum124eff01999-02-23 16:11:01 +00001332 if (istep > 0)
1333 bign = get_len_of_range(ilow, ihigh, istep);
1334 else
1335 bign = get_len_of_range(ihigh, ilow, -istep);
1336 n = (int)bign;
1337 if (bign < 0 || (long)n != bign) {
Guido van Rossume23cde21999-01-12 05:07:47 +00001338 PyErr_SetString(PyExc_OverflowError,
Fred Drake661ea262000-10-24 19:57:45 +00001339 "range() result has too many items");
Guido van Rossume23cde21999-01-12 05:07:47 +00001340 return NULL;
1341 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00001342 v = PyList_New(n);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001343 if (v == NULL)
1344 return NULL;
1345 for (i = 0; i < n; i++) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001346 PyObject *w = PyInt_FromLong(ilow);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001347 if (w == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001348 Py_DECREF(v);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001349 return NULL;
1350 }
Guido van Rossuma937d141998-04-24 18:22:02 +00001351 PyList_SET_ITEM(v, i, w);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001352 ilow += istep;
1353 }
1354 return v;
1355}
1356
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001357static char range_doc[] =
1358"range([start,] stop[, step]) -> list of integers\n\
1359\n\
1360Return a list containing an arithmetic progression of integers.\n\
1361range(i, j) returns [i, i+1, i+2, ..., j-1]; start (!) defaults to 0.\n\
1362When step is given, it specifies the increment (or decrement).\n\
1363For example, range(4) returns [0, 1, 2, 3]. The end point is omitted!\n\
1364These are exactly the valid indices for a list of 4 elements.";
1365
1366
Guido van Rossum79f25d91997-04-29 20:08:16 +00001367static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001368builtin_xrange(PyObject *self, PyObject *args)
Guido van Rossum12d12c51993-10-26 17:58:25 +00001369{
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001370 long ilow = 0, ihigh = 0, istep = 1;
Guido van Rossum0865dd91995-01-17 16:30:22 +00001371 long n;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001372
Guido van Rossum79f25d91997-04-29 20:08:16 +00001373 if (PyTuple_Size(args) <= 1) {
1374 if (!PyArg_ParseTuple(args,
Guido van Rossum0865dd91995-01-17 16:30:22 +00001375 "l;xrange() requires 1-3 int arguments",
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001376 &ihigh))
1377 return NULL;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001378 }
1379 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001380 if (!PyArg_ParseTuple(args,
Guido van Rossum0865dd91995-01-17 16:30:22 +00001381 "ll|l;xrange() requires 1-3 int arguments",
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001382 &ilow, &ihigh, &istep))
Guido van Rossum12d12c51993-10-26 17:58:25 +00001383 return NULL;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001384 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001385 if (istep == 0) {
Fred Drake661ea262000-10-24 19:57:45 +00001386 PyErr_SetString(PyExc_ValueError, "xrange() arg 3 must not be zero");
Guido van Rossum12d12c51993-10-26 17:58:25 +00001387 return NULL;
1388 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001389 if (istep > 0)
Guido van Rossum124eff01999-02-23 16:11:01 +00001390 n = get_len_of_range(ilow, ihigh, istep);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001391 else
Guido van Rossum124eff01999-02-23 16:11:01 +00001392 n = get_len_of_range(ihigh, ilow, -istep);
1393 if (n < 0) {
1394 PyErr_SetString(PyExc_OverflowError,
Fred Drake661ea262000-10-24 19:57:45 +00001395 "xrange() result has too many items");
Guido van Rossum124eff01999-02-23 16:11:01 +00001396 return NULL;
1397 }
Thomas Woutersefafcea2001-07-09 12:30:54 +00001398 return PyRange_New(ilow, n, istep, 1);
Guido van Rossum12d12c51993-10-26 17:58:25 +00001399}
1400
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001401static char xrange_doc[] =
1402"xrange([start,] stop[, step]) -> xrange object\n\
1403\n\
1404Like range(), but instead of returning a list, returns an object that\n\
1405generates the numbers in the range on demand. This is slightly slower\n\
1406than range() but more memory efficient.";
1407
1408
Guido van Rossum79f25d91997-04-29 20:08:16 +00001409static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001410builtin_raw_input(PyObject *self, PyObject *args)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001411{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001412 PyObject *v = NULL;
1413 PyObject *f;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001414
Guido van Rossum79f25d91997-04-29 20:08:16 +00001415 if (!PyArg_ParseTuple(args, "|O:[raw_]input", &v))
Guido van Rossum3165fe61992-09-25 21:59:05 +00001416 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001417 if (PyFile_AsFile(PySys_GetObject("stdin")) == stdin &&
1418 PyFile_AsFile(PySys_GetObject("stdout")) == stdout &&
Guido van Rossum53bb7ff1995-07-26 16:26:31 +00001419 isatty(fileno(stdin)) && isatty(fileno(stdout))) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001420 PyObject *po;
Guido van Rossum872537c1995-07-07 22:43:42 +00001421 char *prompt;
1422 char *s;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001423 PyObject *result;
Guido van Rossum872537c1995-07-07 22:43:42 +00001424 if (v != NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001425 po = PyObject_Str(v);
Guido van Rossum872537c1995-07-07 22:43:42 +00001426 if (po == NULL)
1427 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001428 prompt = PyString_AsString(po);
Guido van Rossumd9b52081998-06-26 18:25:38 +00001429 if (prompt == NULL)
1430 return NULL;
Guido van Rossum872537c1995-07-07 22:43:42 +00001431 }
1432 else {
1433 po = NULL;
1434 prompt = "";
1435 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00001436 s = PyOS_Readline(prompt);
1437 Py_XDECREF(po);
Guido van Rossum872537c1995-07-07 22:43:42 +00001438 if (s == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001439 PyErr_SetNone(PyExc_KeyboardInterrupt);
Guido van Rossum872537c1995-07-07 22:43:42 +00001440 return NULL;
1441 }
1442 if (*s == '\0') {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001443 PyErr_SetNone(PyExc_EOFError);
Guido van Rossum872537c1995-07-07 22:43:42 +00001444 result = NULL;
1445 }
1446 else { /* strip trailing '\n' */
Guido van Rossum106f2da2000-06-28 21:12:25 +00001447 size_t len = strlen(s);
1448 if (len > INT_MAX) {
1449 PyErr_SetString(PyExc_OverflowError, "input too long");
1450 result = NULL;
1451 }
1452 else {
1453 result = PyString_FromStringAndSize(s, (int)(len-1));
1454 }
Guido van Rossum872537c1995-07-07 22:43:42 +00001455 }
Guido van Rossumb18618d2000-05-03 23:44:39 +00001456 PyMem_FREE(s);
Guido van Rossum872537c1995-07-07 22:43:42 +00001457 return result;
1458 }
Guido van Rossum90933611991-06-07 16:10:43 +00001459 if (v != NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001460 f = PySys_GetObject("stdout");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001461 if (f == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001462 PyErr_SetString(PyExc_RuntimeError, "lost sys.stdout");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001463 return NULL;
1464 }
Guido van Rossumc8b6df91997-05-23 00:06:51 +00001465 if (Py_FlushLine() != 0 ||
1466 PyFile_WriteObject(v, f, Py_PRINT_RAW) != 0)
Guido van Rossum90933611991-06-07 16:10:43 +00001467 return NULL;
1468 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00001469 f = PySys_GetObject("stdin");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001470 if (f == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001471 PyErr_SetString(PyExc_RuntimeError, "lost sys.stdin");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001472 return NULL;
1473 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00001474 return PyFile_GetLine(f, -1);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001475}
1476
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001477static char raw_input_doc[] =
1478"raw_input([prompt]) -> string\n\
1479\n\
1480Read a string from standard input. The trailing newline is stripped.\n\
1481If the user hits EOF (Unix: Ctl-D, Windows: Ctl-Z+Return), raise EOFError.\n\
1482On Unix, GNU readline is used if enabled. The prompt string, if given,\n\
1483is printed without a trailing newline before reading.";
1484
1485
Guido van Rossum79f25d91997-04-29 20:08:16 +00001486static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001487builtin_reduce(PyObject *self, PyObject *args)
Guido van Rossum12d12c51993-10-26 17:58:25 +00001488{
Tim Peters15d81ef2001-05-04 04:39:21 +00001489 PyObject *seq, *func, *result = NULL, *it;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001490
Guido van Rossum79f25d91997-04-29 20:08:16 +00001491 if (!PyArg_ParseTuple(args, "OO|O:reduce", &func, &seq, &result))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001492 return NULL;
1493 if (result != NULL)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001494 Py_INCREF(result);
Guido van Rossum12d12c51993-10-26 17:58:25 +00001495
Tim Peters15d81ef2001-05-04 04:39:21 +00001496 it = PyObject_GetIter(seq);
1497 if (it == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001498 PyErr_SetString(PyExc_TypeError,
Tim Peters15d81ef2001-05-04 04:39:21 +00001499 "reduce() arg 2 must support iteration");
1500 Py_XDECREF(result);
Guido van Rossum12d12c51993-10-26 17:58:25 +00001501 return NULL;
1502 }
1503
Guido van Rossum79f25d91997-04-29 20:08:16 +00001504 if ((args = PyTuple_New(2)) == NULL)
Guido van Rossum2d951851994-08-29 12:52:16 +00001505 goto Fail;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001506
Tim Peters15d81ef2001-05-04 04:39:21 +00001507 for (;;) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001508 PyObject *op2;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001509
1510 if (args->ob_refcnt > 1) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001511 Py_DECREF(args);
1512 if ((args = PyTuple_New(2)) == NULL)
Guido van Rossum2d951851994-08-29 12:52:16 +00001513 goto Fail;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001514 }
1515
Tim Peters15d81ef2001-05-04 04:39:21 +00001516 op2 = PyIter_Next(it);
1517 if (op2 == NULL) {
Tim Petersf4848da2001-05-05 00:14:56 +00001518 if (PyErr_Occurred())
1519 goto Fail;
1520 break;
Guido van Rossum2d951851994-08-29 12:52:16 +00001521 }
Guido van Rossum12d12c51993-10-26 17:58:25 +00001522
Guido van Rossum2d951851994-08-29 12:52:16 +00001523 if (result == NULL)
1524 result = op2;
1525 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001526 PyTuple_SetItem(args, 0, result);
1527 PyTuple_SetItem(args, 1, op2);
1528 if ((result = PyEval_CallObject(func, args)) == NULL)
Guido van Rossum2d951851994-08-29 12:52:16 +00001529 goto Fail;
1530 }
Guido van Rossum12d12c51993-10-26 17:58:25 +00001531 }
1532
Guido van Rossum79f25d91997-04-29 20:08:16 +00001533 Py_DECREF(args);
Guido van Rossum12d12c51993-10-26 17:58:25 +00001534
Guido van Rossum2d951851994-08-29 12:52:16 +00001535 if (result == NULL)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001536 PyErr_SetString(PyExc_TypeError,
Fred Drake661ea262000-10-24 19:57:45 +00001537 "reduce() of empty sequence with no initial value");
Guido van Rossum2d951851994-08-29 12:52:16 +00001538
Tim Peters15d81ef2001-05-04 04:39:21 +00001539 Py_DECREF(it);
Guido van Rossum12d12c51993-10-26 17:58:25 +00001540 return result;
1541
Guido van Rossum2d951851994-08-29 12:52:16 +00001542Fail:
Guido van Rossum79f25d91997-04-29 20:08:16 +00001543 Py_XDECREF(args);
1544 Py_XDECREF(result);
Tim Peters15d81ef2001-05-04 04:39:21 +00001545 Py_DECREF(it);
Guido van Rossum12d12c51993-10-26 17:58:25 +00001546 return NULL;
1547}
1548
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001549static char reduce_doc[] =
1550"reduce(function, sequence[, initial]) -> value\n\
1551\n\
1552Apply a function of two arguments cumulatively to the items of a sequence,\n\
1553from left to right, so as to reduce the sequence to a single value.\n\
1554For example, reduce(lambda x, y: x+y, [1, 2, 3, 4, 5]) calculates\n\
1555((((1+2)+3)+4)+5). If initial is present, it is placed before the items\n\
1556of the sequence in the calculation, and serves as a default when the\n\
1557sequence is empty.";
1558
1559
Guido van Rossum79f25d91997-04-29 20:08:16 +00001560static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001561builtin_reload(PyObject *self, PyObject *v)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001562{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001563 return PyImport_ReloadModule(v);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001564}
1565
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001566static char reload_doc[] =
1567"reload(module) -> module\n\
1568\n\
1569Reload the module. The module must have been successfully imported before.";
1570
1571
Guido van Rossum79f25d91997-04-29 20:08:16 +00001572static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001573builtin_repr(PyObject *self, PyObject *v)
Guido van Rossumc89705d1992-11-26 08:54:07 +00001574{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001575 return PyObject_Repr(v);
Guido van Rossumc89705d1992-11-26 08:54:07 +00001576}
1577
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001578static char repr_doc[] =
1579"repr(object) -> string\n\
1580\n\
1581Return the canonical string representation of the object.\n\
1582For most object types, eval(repr(object)) == object.";
1583
1584
Guido van Rossum79f25d91997-04-29 20:08:16 +00001585static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001586builtin_round(PyObject *self, PyObject *args)
Guido van Rossum9e51f9b1993-02-12 16:29:05 +00001587{
Guido van Rossum9e51f9b1993-02-12 16:29:05 +00001588 double x;
1589 double f;
1590 int ndigits = 0;
Guido van Rossum9e51f9b1993-02-12 16:29:05 +00001591 int i;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001592
Guido van Rossum79f25d91997-04-29 20:08:16 +00001593 if (!PyArg_ParseTuple(args, "d|i:round", &x, &ndigits))
Guido van Rossum9e51f9b1993-02-12 16:29:05 +00001594 return NULL;
Guido van Rossum9e51f9b1993-02-12 16:29:05 +00001595 f = 1.0;
Guido van Rossum1e162d31998-05-09 14:42:25 +00001596 i = abs(ndigits);
1597 while (--i >= 0)
Guido van Rossum9e51f9b1993-02-12 16:29:05 +00001598 f = f*10.0;
Guido van Rossum1e162d31998-05-09 14:42:25 +00001599 if (ndigits < 0)
1600 x /= f;
Guido van Rossum9e51f9b1993-02-12 16:29:05 +00001601 else
Guido van Rossum1e162d31998-05-09 14:42:25 +00001602 x *= f;
1603 if (x >= 0.0)
1604 x = floor(x + 0.5);
1605 else
1606 x = ceil(x - 0.5);
1607 if (ndigits < 0)
1608 x *= f;
1609 else
1610 x /= f;
1611 return PyFloat_FromDouble(x);
Guido van Rossum9e51f9b1993-02-12 16:29:05 +00001612}
1613
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001614static char round_doc[] =
1615"round(number[, ndigits]) -> floating point number\n\
1616\n\
1617Round a number to a given precision in decimal digits (default 0 digits).\n\
1618This always returns a floating point number. Precision may be negative.";
1619
1620
Guido van Rossum79f25d91997-04-29 20:08:16 +00001621static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001622builtin_vars(PyObject *self, PyObject *args)
Guido van Rossum2d951851994-08-29 12:52:16 +00001623{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001624 PyObject *v = NULL;
1625 PyObject *d;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001626
Guido van Rossum79f25d91997-04-29 20:08:16 +00001627 if (!PyArg_ParseTuple(args, "|O:vars", &v))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001628 return NULL;
Guido van Rossum2d951851994-08-29 12:52:16 +00001629 if (v == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001630 d = PyEval_GetLocals();
Guido van Rossum53bb7ff1995-07-26 16:26:31 +00001631 if (d == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001632 if (!PyErr_Occurred())
1633 PyErr_SetString(PyExc_SystemError,
1634 "no locals!?");
Guido van Rossum53bb7ff1995-07-26 16:26:31 +00001635 }
1636 else
Guido van Rossum79f25d91997-04-29 20:08:16 +00001637 Py_INCREF(d);
Guido van Rossum2d951851994-08-29 12:52:16 +00001638 }
1639 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001640 d = PyObject_GetAttrString(v, "__dict__");
Guido van Rossum2d951851994-08-29 12:52:16 +00001641 if (d == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001642 PyErr_SetString(PyExc_TypeError,
Guido van Rossum2d951851994-08-29 12:52:16 +00001643 "vars() argument must have __dict__ attribute");
1644 return NULL;
1645 }
1646 }
1647 return d;
1648}
1649
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001650static char vars_doc[] =
1651"vars([object]) -> dictionary\n\
1652\n\
1653Without arguments, equivalent to locals().\n\
1654With an argument, equivalent to object.__dict__.";
1655
Barry Warsawcde8b1b1997-08-22 21:14:38 +00001656static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001657builtin_isinstance(PyObject *self, PyObject *args)
Barry Warsawcde8b1b1997-08-22 21:14:38 +00001658{
1659 PyObject *inst;
1660 PyObject *cls;
Guido van Rossum823649d2001-03-21 18:40:58 +00001661 int retval;
Barry Warsawcde8b1b1997-08-22 21:14:38 +00001662
Guido van Rossum43713e52000-02-29 13:59:29 +00001663 if (!PyArg_ParseTuple(args, "OO:isinstance", &inst, &cls))
Barry Warsawcde8b1b1997-08-22 21:14:38 +00001664 return NULL;
Guido van Rossumf5dd9141997-12-02 19:11:45 +00001665
Guido van Rossum823649d2001-03-21 18:40:58 +00001666 retval = PyObject_IsInstance(inst, cls);
1667 if (retval < 0)
Guido van Rossumad991772001-01-12 16:03:05 +00001668 return NULL;
Guido van Rossum77f6a652002-04-03 22:41:51 +00001669 return PyBool_FromLong(retval);
Barry Warsawcde8b1b1997-08-22 21:14:38 +00001670}
1671
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001672static char isinstance_doc[] =
Guido van Rossum77f6a652002-04-03 22:41:51 +00001673"isinstance(object, class-or-type-or-tuple) -> bool\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001674\n\
1675Return whether an object is an instance of a class or of a subclass thereof.\n\
Guido van Rossum03290ec2001-10-07 20:54:12 +00001676With a type as second argument, return whether that is the object's type.\n\
1677The form using a tuple, isinstance(x, (A, B, ...)), is a shortcut for\n\
1678isinstance(x, A) or isinstance(x, B) or ... (etc.).";
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001679
Barry Warsawcde8b1b1997-08-22 21:14:38 +00001680
1681static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001682builtin_issubclass(PyObject *self, PyObject *args)
Barry Warsawcde8b1b1997-08-22 21:14:38 +00001683{
1684 PyObject *derived;
1685 PyObject *cls;
1686 int retval;
1687
Guido van Rossum43713e52000-02-29 13:59:29 +00001688 if (!PyArg_ParseTuple(args, "OO:issubclass", &derived, &cls))
Barry Warsawcde8b1b1997-08-22 21:14:38 +00001689 return NULL;
Guido van Rossum668213d1999-06-16 17:28:37 +00001690
Guido van Rossum823649d2001-03-21 18:40:58 +00001691 retval = PyObject_IsSubclass(derived, cls);
1692 if (retval < 0)
1693 return NULL;
Guido van Rossum77f6a652002-04-03 22:41:51 +00001694 return PyBool_FromLong(retval);
Barry Warsawcde8b1b1997-08-22 21:14:38 +00001695}
1696
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001697static char issubclass_doc[] =
Guido van Rossum77f6a652002-04-03 22:41:51 +00001698"issubclass(C, B) -> bool\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001699\n\
1700Return whether class C is a subclass (i.e., a derived class) of class B.";
1701
Barry Warsawcde8b1b1997-08-22 21:14:38 +00001702
Barry Warsawbd599b52000-08-03 15:45:29 +00001703static PyObject*
1704builtin_zip(PyObject *self, PyObject *args)
1705{
1706 PyObject *ret;
1707 int itemsize = PySequence_Length(args);
Tim Peters8572b4f2001-05-06 01:05:02 +00001708 int i;
1709 PyObject *itlist; /* tuple of iterators */
Barry Warsawbd599b52000-08-03 15:45:29 +00001710
1711 if (itemsize < 1) {
1712 PyErr_SetString(PyExc_TypeError,
Fred Drake661ea262000-10-24 19:57:45 +00001713 "zip() requires at least one sequence");
Barry Warsawbd599b52000-08-03 15:45:29 +00001714 return NULL;
1715 }
1716 /* args must be a tuple */
1717 assert(PyTuple_Check(args));
1718
Tim Peters8572b4f2001-05-06 01:05:02 +00001719 /* allocate result list */
Barry Warsawbd599b52000-08-03 15:45:29 +00001720 if ((ret = PyList_New(0)) == NULL)
1721 return NULL;
1722
Tim Peters8572b4f2001-05-06 01:05:02 +00001723 /* obtain iterators */
1724 itlist = PyTuple_New(itemsize);
1725 if (itlist == NULL)
1726 goto Fail_ret;
1727 for (i = 0; i < itemsize; ++i) {
1728 PyObject *item = PyTuple_GET_ITEM(args, i);
1729 PyObject *it = PyObject_GetIter(item);
1730 if (it == NULL) {
1731 if (PyErr_ExceptionMatches(PyExc_TypeError))
1732 PyErr_Format(PyExc_TypeError,
1733 "zip argument #%d must support iteration",
1734 i+1);
1735 goto Fail_ret_itlist;
Barry Warsawbd599b52000-08-03 15:45:29 +00001736 }
Tim Peters8572b4f2001-05-06 01:05:02 +00001737 PyTuple_SET_ITEM(itlist, i, it);
1738 }
Barry Warsawbd599b52000-08-03 15:45:29 +00001739
Tim Peters8572b4f2001-05-06 01:05:02 +00001740 /* build result into ret list */
1741 for (;;) {
1742 int status;
1743 PyObject *next = PyTuple_New(itemsize);
1744 if (!next)
1745 goto Fail_ret_itlist;
1746
1747 for (i = 0; i < itemsize; i++) {
1748 PyObject *it = PyTuple_GET_ITEM(itlist, i);
1749 PyObject *item = PyIter_Next(it);
Barry Warsawbd599b52000-08-03 15:45:29 +00001750 if (!item) {
Tim Peters8572b4f2001-05-06 01:05:02 +00001751 if (PyErr_Occurred()) {
1752 Py_DECREF(ret);
1753 ret = NULL;
Barry Warsawbd599b52000-08-03 15:45:29 +00001754 }
1755 Py_DECREF(next);
Tim Peters8572b4f2001-05-06 01:05:02 +00001756 Py_DECREF(itlist);
1757 return ret;
Barry Warsawbd599b52000-08-03 15:45:29 +00001758 }
Tim Peters8572b4f2001-05-06 01:05:02 +00001759 PyTuple_SET_ITEM(next, i, item);
Barry Warsawbd599b52000-08-03 15:45:29 +00001760 }
Tim Peters8572b4f2001-05-06 01:05:02 +00001761
1762 status = PyList_Append(ret, next);
Barry Warsawbd599b52000-08-03 15:45:29 +00001763 Py_DECREF(next);
Tim Peters8572b4f2001-05-06 01:05:02 +00001764 if (status < 0)
1765 goto Fail_ret_itlist;
Barry Warsawbd599b52000-08-03 15:45:29 +00001766 }
Tim Peters8572b4f2001-05-06 01:05:02 +00001767
1768Fail_ret_itlist:
1769 Py_DECREF(itlist);
1770Fail_ret:
1771 Py_DECREF(ret);
1772 return NULL;
Barry Warsawbd599b52000-08-03 15:45:29 +00001773}
1774
1775
1776static char zip_doc[] =
1777"zip(seq1 [, seq2 [...]]) -> [(seq1[0], seq2[0] ...), (...)]\n\
1778\n\
1779Return a list of tuples, where each tuple contains the i-th element\n\
1780from each of the argument sequences. The returned list is truncated\n\
1781in length to the length of the shortest argument sequence.";
1782
1783
Guido van Rossum79f25d91997-04-29 20:08:16 +00001784static PyMethodDef builtin_methods[] = {
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001785 {"__import__", builtin___import__, METH_VARARGS, import_doc},
1786 {"abs", builtin_abs, METH_O, abs_doc},
1787 {"apply", builtin_apply, METH_VARARGS, apply_doc},
1788 {"buffer", builtin_buffer, METH_VARARGS, buffer_doc},
1789 {"callable", builtin_callable, METH_O, callable_doc},
1790 {"chr", builtin_chr, METH_VARARGS, chr_doc},
1791 {"cmp", builtin_cmp, METH_VARARGS, cmp_doc},
1792 {"coerce", builtin_coerce, METH_VARARGS, coerce_doc},
1793 {"compile", builtin_compile, METH_VARARGS, compile_doc},
1794 {"delattr", builtin_delattr, METH_VARARGS, delattr_doc},
1795 {"dir", builtin_dir, METH_VARARGS, dir_doc},
1796 {"divmod", builtin_divmod, METH_VARARGS, divmod_doc},
1797 {"eval", builtin_eval, METH_VARARGS, eval_doc},
1798 {"execfile", builtin_execfile, METH_VARARGS, execfile_doc},
1799 {"filter", builtin_filter, METH_VARARGS, filter_doc},
1800 {"getattr", builtin_getattr, METH_VARARGS, getattr_doc},
1801 {"globals", (PyCFunction)builtin_globals, METH_NOARGS, globals_doc},
1802 {"hasattr", builtin_hasattr, METH_VARARGS, hasattr_doc},
1803 {"hash", builtin_hash, METH_O, hash_doc},
1804 {"hex", builtin_hex, METH_O, hex_doc},
1805 {"id", builtin_id, METH_O, id_doc},
1806 {"input", builtin_input, METH_VARARGS, input_doc},
1807 {"intern", builtin_intern, METH_VARARGS, intern_doc},
1808 {"isinstance", builtin_isinstance, METH_VARARGS, isinstance_doc},
1809 {"issubclass", builtin_issubclass, METH_VARARGS, issubclass_doc},
1810 {"iter", builtin_iter, METH_VARARGS, iter_doc},
1811 {"len", builtin_len, METH_O, len_doc},
1812 {"locals", (PyCFunction)builtin_locals, METH_NOARGS, locals_doc},
1813 {"map", builtin_map, METH_VARARGS, map_doc},
1814 {"max", builtin_max, METH_VARARGS, max_doc},
1815 {"min", builtin_min, METH_VARARGS, min_doc},
1816 {"oct", builtin_oct, METH_O, oct_doc},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001817 {"ord", builtin_ord, METH_O, ord_doc},
1818 {"pow", builtin_pow, METH_VARARGS, pow_doc},
1819 {"range", builtin_range, METH_VARARGS, range_doc},
1820 {"raw_input", builtin_raw_input, METH_VARARGS, raw_input_doc},
1821 {"reduce", builtin_reduce, METH_VARARGS, reduce_doc},
1822 {"reload", builtin_reload, METH_O, reload_doc},
1823 {"repr", builtin_repr, METH_O, repr_doc},
1824 {"round", builtin_round, METH_VARARGS, round_doc},
1825 {"setattr", builtin_setattr, METH_VARARGS, setattr_doc},
1826 {"slice", builtin_slice, METH_VARARGS, slice_doc},
Martin v. Löwis339d0f72001-08-17 18:39:25 +00001827#ifdef Py_USING_UNICODE
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001828 {"unichr", builtin_unichr, METH_VARARGS, unichr_doc},
Martin v. Löwis339d0f72001-08-17 18:39:25 +00001829#endif
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001830 {"vars", builtin_vars, METH_VARARGS, vars_doc},
1831 {"xrange", builtin_xrange, METH_VARARGS, xrange_doc},
1832 {"zip", builtin_zip, METH_VARARGS, zip_doc},
Guido van Rossumc02e15c1991-12-16 13:03:00 +00001833 {NULL, NULL},
Guido van Rossum3f5da241990-12-20 15:06:42 +00001834};
1835
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001836static char builtin_doc[] =
1837"Built-in functions, exceptions, and other objects.\n\
1838\n\
1839Noteworthy: None is the `nil' object; Ellipsis represents `...' in slices.";
1840
Guido van Rossum25ce5661997-08-02 03:10:38 +00001841PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001842_PyBuiltin_Init(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +00001843{
Fred Drake5550de32000-06-20 04:54:19 +00001844 PyObject *mod, *dict, *debug;
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001845 mod = Py_InitModule4("__builtin__", builtin_methods,
1846 builtin_doc, (PyObject *)NULL,
1847 PYTHON_API_VERSION);
Guido van Rossum25ce5661997-08-02 03:10:38 +00001848 if (mod == NULL)
1849 return NULL;
1850 dict = PyModule_GetDict(mod);
Tim Peters4b7625e2001-09-13 21:37:17 +00001851
1852#define SETBUILTIN(NAME, OBJECT) \
1853 if (PyDict_SetItemString(dict, NAME, (PyObject *)OBJECT) < 0) \
1854 return NULL
1855
1856 SETBUILTIN("None", Py_None);
1857 SETBUILTIN("Ellipsis", Py_Ellipsis);
1858 SETBUILTIN("NotImplemented", Py_NotImplemented);
Guido van Rossum77f6a652002-04-03 22:41:51 +00001859 SETBUILTIN("False", Py_False);
1860 SETBUILTIN("True", Py_True);
1861 SETBUILTIN("bool", &PyBool_Type);
Tim Peters4b7625e2001-09-13 21:37:17 +00001862 SETBUILTIN("classmethod", &PyClassMethod_Type);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001863#ifndef WITHOUT_COMPLEX
Tim Peters4b7625e2001-09-13 21:37:17 +00001864 SETBUILTIN("complex", &PyComplex_Type);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001865#endif
Tim Petersa427a2b2001-10-29 22:25:45 +00001866 SETBUILTIN("dict", &PyDict_Type);
Tim Peters4b7625e2001-09-13 21:37:17 +00001867 SETBUILTIN("float", &PyFloat_Type);
1868 SETBUILTIN("property", &PyProperty_Type);
1869 SETBUILTIN("int", &PyInt_Type);
1870 SETBUILTIN("list", &PyList_Type);
1871 SETBUILTIN("long", &PyLong_Type);
1872 SETBUILTIN("object", &PyBaseObject_Type);
1873 SETBUILTIN("staticmethod", &PyStaticMethod_Type);
1874 SETBUILTIN("str", &PyString_Type);
1875 SETBUILTIN("super", &PySuper_Type);
1876 SETBUILTIN("tuple", &PyTuple_Type);
1877 SETBUILTIN("type", &PyType_Type);
Tim Peters742dfd62001-09-13 21:49:44 +00001878
1879 /* Note that open() is just an alias of file(). */
1880 SETBUILTIN("open", &PyFile_Type);
Tim Peters4b7625e2001-09-13 21:37:17 +00001881 SETBUILTIN("file", &PyFile_Type);
Martin v. Löwis339d0f72001-08-17 18:39:25 +00001882#ifdef Py_USING_UNICODE
Tim Peters4b7625e2001-09-13 21:37:17 +00001883 SETBUILTIN("unicode", &PyUnicode_Type);
Martin v. Löwis339d0f72001-08-17 18:39:25 +00001884#endif
Guido van Rossum77f6a652002-04-03 22:41:51 +00001885 debug = PyBool_FromLong(Py_OptimizeFlag == 0);
Fred Drake5550de32000-06-20 04:54:19 +00001886 if (PyDict_SetItemString(dict, "__debug__", debug) < 0) {
1887 Py_XDECREF(debug);
Guido van Rossum25ce5661997-08-02 03:10:38 +00001888 return NULL;
Fred Drake5550de32000-06-20 04:54:19 +00001889 }
1890 Py_XDECREF(debug);
Barry Warsaw757af0e1997-08-29 22:13:51 +00001891
Guido van Rossum25ce5661997-08-02 03:10:38 +00001892 return mod;
Tim Peters4b7625e2001-09-13 21:37:17 +00001893#undef SETBUILTIN
Guido van Rossum3f5da241990-12-20 15:06:42 +00001894}
1895
Guido van Rossume77a7571993-11-03 15:01:26 +00001896/* Helper for filter(): filter a tuple through a function */
Guido van Rossum12d12c51993-10-26 17:58:25 +00001897
Guido van Rossum79f25d91997-04-29 20:08:16 +00001898static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001899filtertuple(PyObject *func, PyObject *tuple)
Guido van Rossum12d12c51993-10-26 17:58:25 +00001900{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001901 PyObject *result;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001902 register int i, j;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001903 int len = PyTuple_Size(tuple);
Guido van Rossum12d12c51993-10-26 17:58:25 +00001904
Guido van Rossumb7b45621995-08-04 04:07:45 +00001905 if (len == 0) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001906 Py_INCREF(tuple);
Guido van Rossumb7b45621995-08-04 04:07:45 +00001907 return tuple;
1908 }
1909
Guido van Rossum79f25d91997-04-29 20:08:16 +00001910 if ((result = PyTuple_New(len)) == NULL)
Guido van Rossum2586bf01993-11-01 16:21:44 +00001911 return NULL;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001912
Guido van Rossum12d12c51993-10-26 17:58:25 +00001913 for (i = j = 0; i < len; ++i) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001914 PyObject *item, *good;
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00001915 int ok;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001916
Guido van Rossum79f25d91997-04-29 20:08:16 +00001917 if ((item = PyTuple_GetItem(tuple, i)) == NULL)
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00001918 goto Fail_1;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001919 if (func == Py_None) {
1920 Py_INCREF(item);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00001921 good = item;
1922 }
1923 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001924 PyObject *arg = Py_BuildValue("(O)", item);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00001925 if (arg == NULL)
1926 goto Fail_1;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001927 good = PyEval_CallObject(func, arg);
1928 Py_DECREF(arg);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00001929 if (good == NULL)
Guido van Rossum12d12c51993-10-26 17:58:25 +00001930 goto Fail_1;
1931 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00001932 ok = PyObject_IsTrue(good);
1933 Py_DECREF(good);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00001934 if (ok) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001935 Py_INCREF(item);
1936 if (PyTuple_SetItem(result, j++, item) < 0)
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00001937 goto Fail_1;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001938 }
Guido van Rossum12d12c51993-10-26 17:58:25 +00001939 }
1940
Tim Peters4324aa32001-05-28 22:30:08 +00001941 if (_PyTuple_Resize(&result, j) < 0)
Guido van Rossum12d12c51993-10-26 17:58:25 +00001942 return NULL;
1943
Guido van Rossum12d12c51993-10-26 17:58:25 +00001944 return result;
1945
Guido van Rossum12d12c51993-10-26 17:58:25 +00001946Fail_1:
Guido van Rossum79f25d91997-04-29 20:08:16 +00001947 Py_DECREF(result);
Guido van Rossum12d12c51993-10-26 17:58:25 +00001948 return NULL;
1949}
1950
1951
Guido van Rossume77a7571993-11-03 15:01:26 +00001952/* Helper for filter(): filter a string through a function */
Guido van Rossum12d12c51993-10-26 17:58:25 +00001953
Guido van Rossum79f25d91997-04-29 20:08:16 +00001954static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001955filterstring(PyObject *func, PyObject *strobj)
Guido van Rossum12d12c51993-10-26 17:58:25 +00001956{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001957 PyObject *result;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001958 register int i, j;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001959 int len = PyString_Size(strobj);
Guido van Rossum12d12c51993-10-26 17:58:25 +00001960
Guido van Rossum79f25d91997-04-29 20:08:16 +00001961 if (func == Py_None) {
Guido van Rossum2586bf01993-11-01 16:21:44 +00001962 /* No character is ever false -- share input string */
Guido van Rossum79f25d91997-04-29 20:08:16 +00001963 Py_INCREF(strobj);
Guido van Rossum2d951851994-08-29 12:52:16 +00001964 return strobj;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001965 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00001966 if ((result = PyString_FromStringAndSize(NULL, len)) == NULL)
Guido van Rossum2586bf01993-11-01 16:21:44 +00001967 return NULL;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001968
Guido van Rossum12d12c51993-10-26 17:58:25 +00001969 for (i = j = 0; i < len; ++i) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001970 PyObject *item, *arg, *good;
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00001971 int ok;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001972
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00001973 item = (*strobj->ob_type->tp_as_sequence->sq_item)(strobj, i);
1974 if (item == NULL)
1975 goto Fail_1;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001976 arg = Py_BuildValue("(O)", item);
Tim Peters388ed082001-04-07 20:34:48 +00001977 if (arg == NULL) {
1978 Py_DECREF(item);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00001979 goto Fail_1;
Tim Peters388ed082001-04-07 20:34:48 +00001980 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00001981 good = PyEval_CallObject(func, arg);
1982 Py_DECREF(arg);
Tim Peters388ed082001-04-07 20:34:48 +00001983 if (good == NULL) {
1984 Py_DECREF(item);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00001985 goto Fail_1;
Tim Peters388ed082001-04-07 20:34:48 +00001986 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00001987 ok = PyObject_IsTrue(good);
1988 Py_DECREF(good);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00001989 if (ok)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001990 PyString_AS_STRING((PyStringObject *)result)[j++] =
1991 PyString_AS_STRING((PyStringObject *)item)[0];
Tim Peters388ed082001-04-07 20:34:48 +00001992 Py_DECREF(item);
Guido van Rossum12d12c51993-10-26 17:58:25 +00001993 }
1994
Guido van Rossum79f25d91997-04-29 20:08:16 +00001995 if (j < len && _PyString_Resize(&result, j) < 0)
Guido van Rossum12d12c51993-10-26 17:58:25 +00001996 return NULL;
1997
Guido van Rossum12d12c51993-10-26 17:58:25 +00001998 return result;
1999
Guido van Rossum12d12c51993-10-26 17:58:25 +00002000Fail_1:
Guido van Rossum79f25d91997-04-29 20:08:16 +00002001 Py_DECREF(result);
Guido van Rossum12d12c51993-10-26 17:58:25 +00002002 return NULL;
2003}