blob: 12af47f25c7d71e531a0277afba2520903daefc0 [file] [log] [blame]
Andrew M. Kuchling9bcc68c2000-12-20 15:07:34 +00001
Guido van Rossum3f5da241990-12-20 15:06:42 +00002/* Built-in functions */
3
Guido van Rossum79f25d91997-04-29 20:08:16 +00004#include "Python.h"
Guido van Rossum3f5da241990-12-20 15:06:42 +00005
6#include "node.h"
Guido van Rossum5b722181993-03-30 17:46:03 +00007#include "compile.h"
8#include "eval.h"
Guido van Rossum3f5da241990-12-20 15:06:42 +00009
Guido van Rossum6bf62da1997-04-11 20:37:35 +000010#include <ctype.h>
11
Guido van Rossum1a2c5cb1996-12-10 15:37:36 +000012#ifdef HAVE_UNISTD_H
13#include <unistd.h>
14#endif
15
Mark Hammond26cffde2001-05-14 12:17:34 +000016/* The default encoding used by the platform file system APIs
17 Can remain NULL for all platforms that don't have such a concept
18*/
Fredrik Lundh5b979352001-06-26 17:46:10 +000019#if defined(MS_WIN32) && defined(HAVE_USABLE_WCHAR_T)
Mark Hammond26cffde2001-05-14 12:17:34 +000020const char *Py_FileSystemDefaultEncoding = "mbcs";
21#else
22const char *Py_FileSystemDefaultEncoding = NULL; /* use default */
23#endif
Mark Hammondef8b6542001-05-13 08:04:26 +000024
Guido van Rossum12d12c51993-10-26 17:58:25 +000025/* Forward */
Tim Petersdbd9ba62000-07-09 03:09:57 +000026static PyObject *filterstring(PyObject *, PyObject *);
27static PyObject *filtertuple (PyObject *, PyObject *);
Guido van Rossum12d12c51993-10-26 17:58:25 +000028
Guido van Rossum79f25d91997-04-29 20:08:16 +000029static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +000030builtin___import__(PyObject *self, PyObject *args)
Guido van Rossum3f5da241990-12-20 15:06:42 +000031{
Guido van Rossum1ae940a1995-01-02 19:04:15 +000032 char *name;
Guido van Rossum79f25d91997-04-29 20:08:16 +000033 PyObject *globals = NULL;
34 PyObject *locals = NULL;
35 PyObject *fromlist = NULL;
Guido van Rossum1ae940a1995-01-02 19:04:15 +000036
Guido van Rossum79f25d91997-04-29 20:08:16 +000037 if (!PyArg_ParseTuple(args, "s|OOO:__import__",
Guido van Rossum24c13741995-02-14 09:42:43 +000038 &name, &globals, &locals, &fromlist))
Guido van Rossum1ae940a1995-01-02 19:04:15 +000039 return NULL;
Guido van Rossumaee0bad1997-09-05 07:33:22 +000040 return PyImport_ImportModuleEx(name, globals, locals, fromlist);
Guido van Rossum1ae940a1995-01-02 19:04:15 +000041}
42
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +000043static char import_doc[] =
44"__import__(name, globals, locals, fromlist) -> module\n\
45\n\
46Import a module. The globals are only used to determine the context;\n\
47they are not modified. The locals are currently unused. The fromlist\n\
48should be a list of names to emulate ``from name import ...'', or an\n\
49empty list to emulate ``import name''.\n\
50When importing a module from a package, note that __import__('A.B', ...)\n\
51returns package A when fromlist is empty, but its submodule B when\n\
52fromlist is not empty.";
53
Guido van Rossum1ae940a1995-01-02 19:04:15 +000054
Guido van Rossum79f25d91997-04-29 20:08:16 +000055static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +000056builtin_abs(PyObject *self, PyObject *v)
Guido van Rossum1ae940a1995-01-02 19:04:15 +000057{
Guido van Rossum09df08a1998-05-22 00:51:39 +000058 return PyNumber_Absolute(v);
Guido van Rossum3f5da241990-12-20 15:06:42 +000059}
60
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +000061static char abs_doc[] =
62"abs(number) -> number\n\
63\n\
64Return the absolute value of the argument.";
65
66
Guido van Rossum79f25d91997-04-29 20:08:16 +000067static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +000068builtin_apply(PyObject *self, PyObject *args)
Guido van Rossumc02e15c1991-12-16 13:03:00 +000069{
Guido van Rossum79f25d91997-04-29 20:08:16 +000070 PyObject *func, *alist = NULL, *kwdict = NULL;
Barry Warsaw968f8cb1998-10-01 15:33:12 +000071 PyObject *t = NULL, *retval = NULL;
Guido van Rossum1ae940a1995-01-02 19:04:15 +000072
Guido van Rossum79f25d91997-04-29 20:08:16 +000073 if (!PyArg_ParseTuple(args, "O|OO:apply", &func, &alist, &kwdict))
Guido van Rossumc02e15c1991-12-16 13:03:00 +000074 return NULL;
Barry Warsaw968f8cb1998-10-01 15:33:12 +000075 if (alist != NULL) {
76 if (!PyTuple_Check(alist)) {
77 if (!PySequence_Check(alist)) {
Jeremy Hyltonc862cf42001-01-19 03:25:05 +000078 PyErr_Format(PyExc_TypeError,
79 "apply() arg 2 expect sequence, found %s",
80 alist->ob_type->tp_name);
Barry Warsaw968f8cb1998-10-01 15:33:12 +000081 return NULL;
82 }
83 t = PySequence_Tuple(alist);
84 if (t == NULL)
85 return NULL;
86 alist = t;
87 }
Guido van Rossum2d951851994-08-29 12:52:16 +000088 }
Guido van Rossum79f25d91997-04-29 20:08:16 +000089 if (kwdict != NULL && !PyDict_Check(kwdict)) {
Jeremy Hyltonc862cf42001-01-19 03:25:05 +000090 PyErr_Format(PyExc_TypeError,
91 "apply() arg 3 expected dictionary, found %s",
92 kwdict->ob_type->tp_name);
Barry Warsaw968f8cb1998-10-01 15:33:12 +000093 goto finally;
Guido van Rossum681d79a1995-07-18 14:51:37 +000094 }
Barry Warsaw968f8cb1998-10-01 15:33:12 +000095 retval = PyEval_CallObjectWithKeywords(func, alist, kwdict);
96 finally:
97 Py_XDECREF(t);
98 return retval;
Guido van Rossumc02e15c1991-12-16 13:03:00 +000099}
100
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000101static char apply_doc[] =
Fred Drakef1fbc622001-01-12 17:05:05 +0000102"apply(object[, args[, kwargs]]) -> value\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000103\n\
Fred Drake7b912121999-12-23 14:16:55 +0000104Call a callable object with positional arguments taken from the tuple args,\n\
105and keyword arguments taken from the optional dictionary kwargs.\n\
106Note that classes are callable, as are instances with a __call__() method.";
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000107
108
Guido van Rossum79f25d91997-04-29 20:08:16 +0000109static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000110builtin_buffer(PyObject *self, PyObject *args)
Guido van Rossum0daf0221999-03-19 19:07:19 +0000111{
112 PyObject *ob;
113 int offset = 0;
114 int size = Py_END_OF_BUFFER;
115
116 if ( !PyArg_ParseTuple(args, "O|ii:buffer", &ob, &offset, &size) )
117 return NULL;
118 return PyBuffer_FromObject(ob, offset, size);
119}
120
121static char buffer_doc[] =
Guido van Rossum09095f32000-03-10 23:00:52 +0000122"buffer(object [, offset[, size]]) -> object\n\
Guido van Rossum0daf0221999-03-19 19:07:19 +0000123\n\
Guido van Rossumad991772001-01-12 16:03:05 +0000124Create a new buffer object which references the given object.\n\
Guido van Rossum0daf0221999-03-19 19:07:19 +0000125The buffer will reference a slice of the target object from the\n\
126start of the object (or at the specified offset). The slice will\n\
127extend to the end of the target object (or with the specified size).";
128
129
130static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000131builtin_callable(PyObject *self, PyObject *v)
Guido van Rossum2d951851994-08-29 12:52:16 +0000132{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000133 return PyInt_FromLong((long)PyCallable_Check(v));
Guido van Rossum2d951851994-08-29 12:52:16 +0000134}
135
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000136static char callable_doc[] =
137"callable(object) -> Boolean\n\
138\n\
139Return whether the object is callable (i.e., some kind of function).\n\
140Note that classes are callable, as are instances with a __call__() method.";
141
142
Guido van Rossum79f25d91997-04-29 20:08:16 +0000143static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000144builtin_filter(PyObject *self, PyObject *args)
Guido van Rossum12d12c51993-10-26 17:58:25 +0000145{
Tim Peters0e57abf2001-05-02 07:39:38 +0000146 PyObject *func, *seq, *result, *it;
147 int len; /* guess for result list size */
Tim Petersf4848da2001-05-05 00:14:56 +0000148 register int j;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000149
Guido van Rossum79f25d91997-04-29 20:08:16 +0000150 if (!PyArg_ParseTuple(args, "OO:filter", &func, &seq))
Guido van Rossum12d12c51993-10-26 17:58:25 +0000151 return NULL;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000152
Tim Peters0e57abf2001-05-02 07:39:38 +0000153 /* Strings and tuples return a result of the same type. */
154 if (PyString_Check(seq))
155 return filterstring(func, seq);
156 if (PyTuple_Check(seq))
157 return filtertuple(func, seq);
158
159 /* Get iterator. */
160 it = PyObject_GetIter(seq);
161 if (it == NULL)
162 return NULL;
163
164 /* Guess a result list size. */
165 len = -1; /* unknown */
166 if (PySequence_Check(seq) &&
167 seq->ob_type->tp_as_sequence->sq_length) {
168 len = PySequence_Size(seq);
169 if (len < 0)
170 PyErr_Clear();
Guido van Rossum12d12c51993-10-26 17:58:25 +0000171 }
Tim Peters0e57abf2001-05-02 07:39:38 +0000172 if (len < 0)
173 len = 8; /* arbitrary */
Guido van Rossum12d12c51993-10-26 17:58:25 +0000174
Tim Peters0e57abf2001-05-02 07:39:38 +0000175 /* Get a result list. */
Guido van Rossum79f25d91997-04-29 20:08:16 +0000176 if (PyList_Check(seq) && seq->ob_refcnt == 1) {
Tim Peters0e57abf2001-05-02 07:39:38 +0000177 /* Eww - can modify the list in-place. */
Guido van Rossum79f25d91997-04-29 20:08:16 +0000178 Py_INCREF(seq);
Guido van Rossum12d12c51993-10-26 17:58:25 +0000179 result = seq;
180 }
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000181 else {
Tim Peters0e57abf2001-05-02 07:39:38 +0000182 result = PyList_New(len);
183 if (result == NULL)
184 goto Fail_it;
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000185 }
Guido van Rossum12d12c51993-10-26 17:58:25 +0000186
Tim Peters0e57abf2001-05-02 07:39:38 +0000187 /* Build the result list. */
Tim Petersf4848da2001-05-05 00:14:56 +0000188 j = 0;
189 for (;;) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000190 PyObject *item, *good;
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000191 int ok;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000192
Tim Peters0e57abf2001-05-02 07:39:38 +0000193 item = PyIter_Next(it);
194 if (item == NULL) {
Tim Petersf4848da2001-05-05 00:14:56 +0000195 if (PyErr_Occurred())
196 goto Fail_result_it;
Tim Peters0e57abf2001-05-02 07:39:38 +0000197 break;
Guido van Rossum2d951851994-08-29 12:52:16 +0000198 }
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000199
Guido van Rossum79f25d91997-04-29 20:08:16 +0000200 if (func == Py_None) {
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000201 good = item;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000202 Py_INCREF(good);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000203 }
204 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000205 PyObject *arg = Py_BuildValue("(O)", item);
Tim Peters0e57abf2001-05-02 07:39:38 +0000206 if (arg == NULL) {
207 Py_DECREF(item);
208 goto Fail_result_it;
209 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000210 good = PyEval_CallObject(func, arg);
211 Py_DECREF(arg);
Guido van Rossum58b68731995-01-10 17:40:55 +0000212 if (good == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000213 Py_DECREF(item);
Tim Peters0e57abf2001-05-02 07:39:38 +0000214 goto Fail_result_it;
Guido van Rossum58b68731995-01-10 17:40:55 +0000215 }
Guido van Rossum12d12c51993-10-26 17:58:25 +0000216 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000217 ok = PyObject_IsTrue(good);
218 Py_DECREF(good);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000219 if (ok) {
Tim Peters0e57abf2001-05-02 07:39:38 +0000220 if (j < len)
221 PyList_SET_ITEM(result, j, item);
Guido van Rossum2d951851994-08-29 12:52:16 +0000222 else {
Barry Warsawfa77e091999-01-28 18:49:12 +0000223 int status = PyList_Append(result, item);
Barry Warsawfa77e091999-01-28 18:49:12 +0000224 Py_DECREF(item);
225 if (status < 0)
Tim Peters0e57abf2001-05-02 07:39:38 +0000226 goto Fail_result_it;
Guido van Rossum2d951851994-08-29 12:52:16 +0000227 }
Tim Peters0e57abf2001-05-02 07:39:38 +0000228 ++j;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000229 }
Tim Peters0e57abf2001-05-02 07:39:38 +0000230 else
231 Py_DECREF(item);
Guido van Rossum12d12c51993-10-26 17:58:25 +0000232 }
233
Guido van Rossum12d12c51993-10-26 17:58:25 +0000234
Tim Peters0e57abf2001-05-02 07:39:38 +0000235 /* Cut back result list if len is too big. */
Guido van Rossum79f25d91997-04-29 20:08:16 +0000236 if (j < len && PyList_SetSlice(result, j, len, NULL) < 0)
Tim Peters0e57abf2001-05-02 07:39:38 +0000237 goto Fail_result_it;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000238
Tim Peters3c6b1482001-05-21 08:07:05 +0000239 Py_DECREF(it);
Guido van Rossum12d12c51993-10-26 17:58:25 +0000240 return result;
241
Tim Peters0e57abf2001-05-02 07:39:38 +0000242Fail_result_it:
Guido van Rossum79f25d91997-04-29 20:08:16 +0000243 Py_DECREF(result);
Tim Peters0e57abf2001-05-02 07:39:38 +0000244Fail_it:
245 Py_DECREF(it);
Guido van Rossum12d12c51993-10-26 17:58:25 +0000246 return NULL;
247}
248
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000249static char filter_doc[] =
250"filter(function, sequence) -> list\n\
251\n\
252Return a list containing those items of sequence for which function(item)\n\
253is true. If function is None, return a list of items that are true.";
254
255
Guido van Rossum79f25d91997-04-29 20:08:16 +0000256static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000257builtin_chr(PyObject *self, PyObject *args)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000258{
259 long x;
260 char s[1];
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000261
Guido van Rossum79f25d91997-04-29 20:08:16 +0000262 if (!PyArg_ParseTuple(args, "l:chr", &x))
Guido van Rossum3f5da241990-12-20 15:06:42 +0000263 return NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000264 if (x < 0 || x >= 256) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000265 PyErr_SetString(PyExc_ValueError,
266 "chr() arg not in range(256)");
Guido van Rossum3f5da241990-12-20 15:06:42 +0000267 return NULL;
268 }
Guido van Rossum6bf62da1997-04-11 20:37:35 +0000269 s[0] = (char)x;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000270 return PyString_FromStringAndSize(s, 1);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000271}
272
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000273static char chr_doc[] =
274"chr(i) -> character\n\
275\n\
276Return a string of one character with ordinal i; 0 <= i < 256.";
277
278
Guido van Rossum79f25d91997-04-29 20:08:16 +0000279static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000280builtin_unichr(PyObject *self, PyObject *args)
Guido van Rossum09095f32000-03-10 23:00:52 +0000281{
282 long x;
Fredrik Lundh0dcf67e2001-06-26 20:01:56 +0000283 Py_UNICODE s[2];
Guido van Rossum09095f32000-03-10 23:00:52 +0000284
285 if (!PyArg_ParseTuple(args, "l:unichr", &x))
286 return NULL;
Fredrik Lundh0dcf67e2001-06-26 20:01:56 +0000287
Marc-André Lemburgae21df52001-07-26 16:29:25 +0000288#ifdef Py_UNICODE_WIDE
Fredrik Lundh0dcf67e2001-06-26 20:01:56 +0000289 if (x < 0 || x > 0x10ffff) {
Guido van Rossum09095f32000-03-10 23:00:52 +0000290 PyErr_SetString(PyExc_ValueError,
Marc-André Lemburgae21df52001-07-26 16:29:25 +0000291 "unichr() arg not in range(0x110000) "
292 "(wide Python build)");
Guido van Rossum09095f32000-03-10 23:00:52 +0000293 return NULL;
294 }
Marc-André Lemburgae21df52001-07-26 16:29:25 +0000295#else
296 if (x < 0 || x > 0xffff) {
297 PyErr_SetString(PyExc_ValueError,
298 "unichr() arg not in range(0x10000) "
299 "(narrow Python build)");
300 return NULL;
301 }
302#endif
Fredrik Lundh0dcf67e2001-06-26 20:01:56 +0000303
304 if (x <= 0xffff) {
305 /* UCS-2 character */
306 s[0] = (Py_UNICODE) x;
307 return PyUnicode_FromUnicode(s, 1);
Guido van Rossum236d8b72001-06-26 23:12:25 +0000308 }
309 else {
Fredrik Lundh8f455852001-06-27 18:59:43 +0000310#ifndef Py_UNICODE_WIDE
Fredrik Lundh0dcf67e2001-06-26 20:01:56 +0000311 /* UCS-4 character. store as two surrogate characters */
312 x -= 0x10000L;
313 s[0] = 0xD800 + (Py_UNICODE) (x >> 10);
314 s[1] = 0xDC00 + (Py_UNICODE) (x & 0x03FF);
315 return PyUnicode_FromUnicode(s, 2);
Guido van Rossum236d8b72001-06-26 23:12:25 +0000316#else
317 s[0] = (Py_UNICODE)x;
318 return PyUnicode_FromUnicode(s, 1);
Martin v. Löwis0ba70cc2001-06-26 22:22:37 +0000319#endif
Fredrik Lundh0dcf67e2001-06-26 20:01:56 +0000320 }
Guido van Rossum09095f32000-03-10 23:00:52 +0000321}
322
323static char unichr_doc[] =
Fred Drake078b24f2000-04-13 02:42:50 +0000324"unichr(i) -> Unicode character\n\
Guido van Rossum09095f32000-03-10 23:00:52 +0000325\n\
Fredrik Lundh0dcf67e2001-06-26 20:01:56 +0000326Return a Unicode string of one character with ordinal i; 0 <= i <= 0x10ffff.";
Guido van Rossum09095f32000-03-10 23:00:52 +0000327
328
329static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000330builtin_cmp(PyObject *self, PyObject *args)
Guido van Rossuma9e7dc11992-10-18 18:53:57 +0000331{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000332 PyObject *a, *b;
Guido van Rossum09df08a1998-05-22 00:51:39 +0000333 int c;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000334
Guido van Rossum79f25d91997-04-29 20:08:16 +0000335 if (!PyArg_ParseTuple(args, "OO:cmp", &a, &b))
Guido van Rossuma9e7dc11992-10-18 18:53:57 +0000336 return NULL;
Guido van Rossum09df08a1998-05-22 00:51:39 +0000337 if (PyObject_Cmp(a, b, &c) < 0)
Guido van Rossumc8b6df91997-05-23 00:06:51 +0000338 return NULL;
Guido van Rossum09df08a1998-05-22 00:51:39 +0000339 return PyInt_FromLong((long)c);
Guido van Rossuma9e7dc11992-10-18 18:53:57 +0000340}
341
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000342static char cmp_doc[] =
343"cmp(x, y) -> integer\n\
344\n\
345Return negative if x<y, zero if x==y, positive if x>y.";
346
347
Guido van Rossum79f25d91997-04-29 20:08:16 +0000348static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000349builtin_coerce(PyObject *self, PyObject *args)
Guido van Rossum6a00cd81995-01-07 12:39:01 +0000350{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000351 PyObject *v, *w;
352 PyObject *res;
Guido van Rossum5524a591995-01-10 15:26:20 +0000353
Guido van Rossum79f25d91997-04-29 20:08:16 +0000354 if (!PyArg_ParseTuple(args, "OO:coerce", &v, &w))
Guido van Rossum5524a591995-01-10 15:26:20 +0000355 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000356 if (PyNumber_Coerce(&v, &w) < 0)
Guido van Rossum04691fc1992-08-12 15:35:34 +0000357 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000358 res = Py_BuildValue("(OO)", v, w);
359 Py_DECREF(v);
360 Py_DECREF(w);
Guido van Rossum04691fc1992-08-12 15:35:34 +0000361 return res;
362}
363
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000364static char coerce_doc[] =
365"coerce(x, y) -> None or (x1, y1)\n\
366\n\
367When x and y can be coerced to values of the same type, return a tuple\n\
368containing the coerced values. When they can't be coerced, return None.";
369
370
Guido van Rossum79f25d91997-04-29 20:08:16 +0000371static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000372builtin_compile(PyObject *self, PyObject *args)
Guido van Rossum5b722181993-03-30 17:46:03 +0000373{
374 char *str;
375 char *filename;
376 char *startstr;
377 int start;
Tim Peters5ba58662001-07-16 02:29:45 +0000378 PyCompilerFlags cf;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000379
Guido van Rossum79f25d91997-04-29 20:08:16 +0000380 if (!PyArg_ParseTuple(args, "sss:compile", &str, &filename, &startstr))
Guido van Rossum5b722181993-03-30 17:46:03 +0000381 return NULL;
382 if (strcmp(startstr, "exec") == 0)
Guido van Rossumb05a5c71997-05-07 17:46:13 +0000383 start = Py_file_input;
Guido van Rossum5b722181993-03-30 17:46:03 +0000384 else if (strcmp(startstr, "eval") == 0)
Guido van Rossumb05a5c71997-05-07 17:46:13 +0000385 start = Py_eval_input;
Guido van Rossum872537c1995-07-07 22:43:42 +0000386 else if (strcmp(startstr, "single") == 0)
Guido van Rossumb05a5c71997-05-07 17:46:13 +0000387 start = Py_single_input;
Guido van Rossum5b722181993-03-30 17:46:03 +0000388 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000389 PyErr_SetString(PyExc_ValueError,
Fred Drake661ea262000-10-24 19:57:45 +0000390 "compile() arg 3 must be 'exec' or 'eval' or 'single'");
Guido van Rossum5b722181993-03-30 17:46:03 +0000391 return NULL;
392 }
Tim Peters5ba58662001-07-16 02:29:45 +0000393 cf.cf_flags = 0;
394 if (PyEval_MergeCompilerFlags(&cf))
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000395 return Py_CompileStringFlags(str, filename, start, &cf);
Tim Peters5ba58662001-07-16 02:29:45 +0000396 else
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000397 return Py_CompileString(str, filename, start);
Guido van Rossum5b722181993-03-30 17:46:03 +0000398}
399
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000400static char compile_doc[] =
401"compile(source, filename, mode) -> code object\n\
402\n\
403Compile the source string (a Python module, statement or expression)\n\
404into a code object that can be executed by the exec statement or eval().\n\
405The filename will be used for run-time error messages.\n\
406The mode must be 'exec' to compile a module, 'single' to compile a\n\
407single (interactive) statement, or 'eval' to compile an expression.";
408
409
Guido van Rossum79f25d91997-04-29 20:08:16 +0000410static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000411builtin_dir(PyObject *self, PyObject *args)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000412{
Guido van Rossum666b17a1997-05-06 16:36:57 +0000413 static char *attrlist[] = {"__members__", "__methods__", NULL};
414 PyObject *v = NULL, *l = NULL, *m = NULL;
415 PyObject *d, *x;
416 int i;
417 char **s;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000418
Guido van Rossum79f25d91997-04-29 20:08:16 +0000419 if (!PyArg_ParseTuple(args, "|O:dir", &v))
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000420 return NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000421 if (v == NULL) {
Guido van Rossum666b17a1997-05-06 16:36:57 +0000422 x = PyEval_GetLocals();
423 if (x == NULL)
424 goto error;
425 l = PyMapping_Keys(x);
426 if (l == NULL)
427 goto error;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000428 }
429 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000430 d = PyObject_GetAttrString(v, "__dict__");
Guido van Rossum666b17a1997-05-06 16:36:57 +0000431 if (d == NULL)
Guido van Rossum795ba581996-05-23 22:49:07 +0000432 PyErr_Clear();
Guido van Rossum666b17a1997-05-06 16:36:57 +0000433 else {
434 l = PyMapping_Keys(d);
435 if (l == NULL)
436 PyErr_Clear();
437 Py_DECREF(d);
438 }
439 if (l == NULL) {
440 l = PyList_New(0);
441 if (l == NULL)
442 goto error;
443 }
444 for (s = attrlist; *s != NULL; s++) {
445 m = PyObject_GetAttrString(v, *s);
446 if (m == NULL) {
447 PyErr_Clear();
448 continue;
449 }
450 for (i = 0; ; i++) {
451 x = PySequence_GetItem(m, i);
452 if (x == NULL) {
453 PyErr_Clear();
454 break;
455 }
456 if (PyList_Append(l, x) != 0) {
457 Py_DECREF(x);
458 Py_DECREF(m);
459 goto error;
460 }
461 Py_DECREF(x);
462 }
463 Py_DECREF(m);
Guido van Rossum795ba581996-05-23 22:49:07 +0000464 }
Guido van Rossum006bcd41991-10-24 14:54:44 +0000465 }
Guido van Rossum666b17a1997-05-06 16:36:57 +0000466 if (PyList_Sort(l) != 0)
467 goto error;
468 return l;
469 error:
470 Py_XDECREF(l);
471 return NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000472}
473
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000474static char dir_doc[] =
475"dir([object]) -> list of strings\n\
476\n\
477Return an alphabetized list of names comprising (some of) the attributes\n\
478of the given object. Without an argument, the names in the current scope\n\
479are listed. With an instance argument, only the instance attributes are\n\
480returned. With a class argument, attributes of the base class are not\n\
481returned. For other types or arguments, this may list members or methods.";
482
483
Guido van Rossum79f25d91997-04-29 20:08:16 +0000484static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000485builtin_divmod(PyObject *self, PyObject *args)
Guido van Rossum6a00cd81995-01-07 12:39:01 +0000486{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000487 PyObject *v, *w;
Guido van Rossum6a00cd81995-01-07 12:39:01 +0000488
Guido van Rossum79f25d91997-04-29 20:08:16 +0000489 if (!PyArg_ParseTuple(args, "OO:divmod", &v, &w))
Guido van Rossum6a00cd81995-01-07 12:39:01 +0000490 return NULL;
Guido van Rossum09df08a1998-05-22 00:51:39 +0000491 return PyNumber_Divmod(v, w);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000492}
493
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000494static char divmod_doc[] =
495"divmod(x, y) -> (div, mod)\n\
496\n\
497Return the tuple ((x-x%y)/y, x%y). Invariant: div*y + mod == x.";
498
499
Guido van Rossum79f25d91997-04-29 20:08:16 +0000500static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000501builtin_eval(PyObject *self, PyObject *args)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000502{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000503 PyObject *cmd;
504 PyObject *globals = Py_None, *locals = Py_None;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000505 char *str;
Guido van Rossum590baa41993-11-30 13:40:46 +0000506
Guido van Rossum79f25d91997-04-29 20:08:16 +0000507 if (!PyArg_ParseTuple(args, "O|O!O!:eval",
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000508 &cmd,
Guido van Rossum79f25d91997-04-29 20:08:16 +0000509 &PyDict_Type, &globals,
510 &PyDict_Type, &locals))
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000511 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000512 if (globals == Py_None) {
513 globals = PyEval_GetGlobals();
514 if (locals == Py_None)
515 locals = PyEval_GetLocals();
Guido van Rossum6135a871995-01-09 17:53:26 +0000516 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000517 else if (locals == Py_None)
Guido van Rossum6135a871995-01-09 17:53:26 +0000518 locals = globals;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000519 if (PyDict_GetItemString(globals, "__builtins__") == NULL) {
520 if (PyDict_SetItemString(globals, "__builtins__",
521 PyEval_GetBuiltins()) != 0)
Guido van Rossum6135a871995-01-09 17:53:26 +0000522 return NULL;
523 }
Jeremy Hylton15c1c4f2001-07-30 21:50:55 +0000524 if (PyCode_Check(cmd)) {
525 if (PyTuple_GET_SIZE(((PyCodeObject *)cmd)->co_freevars) > 0) {
526 PyErr_SetString(PyExc_TypeError,
527 "code object passed to eval() may not contain free variables");
528 return NULL;
529 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000530 return PyEval_EvalCode((PyCodeObject *) cmd, globals, locals);
Jeremy Hylton15c1c4f2001-07-30 21:50:55 +0000531 }
Marc-André Lemburgd1ba4432000-09-19 21:04:18 +0000532 if (!PyString_Check(cmd) &&
533 !PyUnicode_Check(cmd)) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000534 PyErr_SetString(PyExc_TypeError,
Fred Drake661ea262000-10-24 19:57:45 +0000535 "eval() arg 1 must be a string or code object");
Guido van Rossum94390a41992-08-14 15:14:30 +0000536 return NULL;
537 }
Marc-André Lemburgd1ba4432000-09-19 21:04:18 +0000538 if (PyString_AsStringAndSize(cmd, &str, NULL))
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000539 return NULL;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000540 while (*str == ' ' || *str == '\t')
541 str++;
Guido van Rossumb05a5c71997-05-07 17:46:13 +0000542 return PyRun_String(str, Py_eval_input, globals, locals);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000543}
544
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000545static char eval_doc[] =
546"eval(source[, globals[, locals]]) -> value\n\
547\n\
548Evaluate the source in the context of globals and locals.\n\
549The source may be a string representing a Python expression\n\
550or a code object as returned by compile().\n\
551The globals and locals are dictionaries, defaulting to the current\n\
552globals and locals. If only globals is given, locals defaults to it.";
553
554
Guido van Rossum79f25d91997-04-29 20:08:16 +0000555static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000556builtin_execfile(PyObject *self, PyObject *args)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000557{
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000558 char *filename;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000559 PyObject *globals = Py_None, *locals = Py_None;
560 PyObject *res;
Guido van Rossum0f61f8a1992-02-25 18:55:05 +0000561 FILE* fp;
Tim Peters5ba58662001-07-16 02:29:45 +0000562 PyCompilerFlags cf;
Martin v. Löwis6b3a2c42001-08-08 05:30:36 +0000563 int exists;
564 struct stat s;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000565
Guido van Rossum79f25d91997-04-29 20:08:16 +0000566 if (!PyArg_ParseTuple(args, "s|O!O!:execfile",
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000567 &filename,
Guido van Rossum79f25d91997-04-29 20:08:16 +0000568 &PyDict_Type, &globals,
569 &PyDict_Type, &locals))
Guido van Rossum0f61f8a1992-02-25 18:55:05 +0000570 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000571 if (globals == Py_None) {
572 globals = PyEval_GetGlobals();
573 if (locals == Py_None)
574 locals = PyEval_GetLocals();
Guido van Rossum6135a871995-01-09 17:53:26 +0000575 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000576 else if (locals == Py_None)
Guido van Rossum6135a871995-01-09 17:53:26 +0000577 locals = globals;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000578 if (PyDict_GetItemString(globals, "__builtins__") == NULL) {
579 if (PyDict_SetItemString(globals, "__builtins__",
580 PyEval_GetBuiltins()) != 0)
Guido van Rossum6135a871995-01-09 17:53:26 +0000581 return NULL;
582 }
Martin v. Löwis6b3a2c42001-08-08 05:30:36 +0000583
584 exists = 0;
585 /* Test for existence or directory. */
586 if (!stat(filename, &s)) {
Martin v. Löwisf9836ba2001-08-08 10:28:06 +0000587 if (S_ISDIR(s.st_mode))
Martin v. Löwis6b3a2c42001-08-08 05:30:36 +0000588 errno = EISDIR;
589 else
590 exists = 1;
591 }
592
593 if (exists) {
594 Py_BEGIN_ALLOW_THREADS
595 fp = fopen(filename, "r");
596 Py_END_ALLOW_THREADS
597
598 if (fp == NULL) {
599 exists = 0;
600 }
601 }
602
603 if (!exists) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000604 PyErr_SetFromErrno(PyExc_IOError);
Guido van Rossum0f61f8a1992-02-25 18:55:05 +0000605 return NULL;
606 }
Tim Peters5ba58662001-07-16 02:29:45 +0000607 cf.cf_flags = 0;
608 if (PyEval_MergeCompilerFlags(&cf))
Tim Peters748b8bb2001-04-28 08:20:22 +0000609 res = PyRun_FileExFlags(fp, filename, Py_file_input, globals,
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000610 locals, 1, &cf);
Tim Peters5ba58662001-07-16 02:29:45 +0000611 else
Tim Peters748b8bb2001-04-28 08:20:22 +0000612 res = PyRun_FileEx(fp, filename, Py_file_input, globals,
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000613 locals, 1);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000614 return res;
Guido van Rossum0f61f8a1992-02-25 18:55:05 +0000615}
616
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000617static char execfile_doc[] =
618"execfile(filename[, globals[, locals]])\n\
619\n\
620Read and execute a Python script from a file.\n\
621The globals and locals are dictionaries, defaulting to the current\n\
622globals and locals. If only globals is given, locals defaults to it.";
623
624
Guido van Rossum79f25d91997-04-29 20:08:16 +0000625static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000626builtin_getattr(PyObject *self, PyObject *args)
Guido van Rossum33894be1992-01-27 16:53:09 +0000627{
Guido van Rossum950ff291998-06-29 13:38:57 +0000628 PyObject *v, *result, *dflt = NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000629 PyObject *name;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000630
Marc-André Lemburg691270f2000-09-18 16:22:27 +0000631 if (!PyArg_ParseTuple(args, "OO|O:getattr", &v, &name, &dflt))
Guido van Rossum33894be1992-01-27 16:53:09 +0000632 return NULL;
Jeremy Hylton0eb11152001-07-30 22:39:31 +0000633 if (PyUnicode_Check(name)) {
634 name = _PyUnicode_AsDefaultEncodedString(name, NULL);
635 if (name == NULL)
636 return NULL;
637 }
638
639 if (!PyString_Check(name)) {
640 PyErr_SetString(PyExc_TypeError,
641 "attribute name must be string");
642 return NULL;
643 }
Guido van Rossum950ff291998-06-29 13:38:57 +0000644 result = PyObject_GetAttr(v, name);
645 if (result == NULL && dflt != NULL) {
646 PyErr_Clear();
647 Py_INCREF(dflt);
648 result = dflt;
649 }
650 return result;
Guido van Rossum9bfef441993-03-29 10:43:31 +0000651}
652
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000653static char getattr_doc[] =
Guido van Rossum950ff291998-06-29 13:38:57 +0000654"getattr(object, name[, default]) -> value\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000655\n\
Guido van Rossum950ff291998-06-29 13:38:57 +0000656Get a named attribute from an object; getattr(x, 'y') is equivalent to x.y.\n\
657When a default argument is given, it is returned when the attribute doesn't\n\
658exist; without it, an exception is raised in that case.";
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000659
660
Guido van Rossum79f25d91997-04-29 20:08:16 +0000661static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000662builtin_globals(PyObject *self)
Guido van Rossum872537c1995-07-07 22:43:42 +0000663{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000664 PyObject *d;
Guido van Rossum872537c1995-07-07 22:43:42 +0000665
Guido van Rossum79f25d91997-04-29 20:08:16 +0000666 d = PyEval_GetGlobals();
667 Py_INCREF(d);
Guido van Rossum872537c1995-07-07 22:43:42 +0000668 return d;
669}
670
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000671static char globals_doc[] =
672"globals() -> dictionary\n\
673\n\
674Return the dictionary containing the current scope's global variables.";
675
676
Guido van Rossum79f25d91997-04-29 20:08:16 +0000677static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000678builtin_hasattr(PyObject *self, PyObject *args)
Guido van Rossum9bfef441993-03-29 10:43:31 +0000679{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000680 PyObject *v;
681 PyObject *name;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000682
Marc-André Lemburg691270f2000-09-18 16:22:27 +0000683 if (!PyArg_ParseTuple(args, "OO:hasattr", &v, &name))
Guido van Rossum9bfef441993-03-29 10:43:31 +0000684 return NULL;
Jeremy Hylton302b54a2001-07-30 22:45:19 +0000685 if (PyUnicode_Check(name)) {
686 name = _PyUnicode_AsDefaultEncodedString(name, NULL);
687 if (name == NULL)
688 return NULL;
689 }
690
691 if (!PyString_Check(name)) {
692 PyErr_SetString(PyExc_TypeError,
693 "attribute name must be string");
694 return NULL;
695 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000696 v = PyObject_GetAttr(v, name);
Guido van Rossum9bfef441993-03-29 10:43:31 +0000697 if (v == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000698 PyErr_Clear();
Guido van Rossum09df08a1998-05-22 00:51:39 +0000699 Py_INCREF(Py_False);
700 return Py_False;
Guido van Rossum9bfef441993-03-29 10:43:31 +0000701 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000702 Py_DECREF(v);
Guido van Rossum09df08a1998-05-22 00:51:39 +0000703 Py_INCREF(Py_True);
704 return Py_True;
Guido van Rossum33894be1992-01-27 16:53:09 +0000705}
706
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000707static char hasattr_doc[] =
708"hasattr(object, name) -> Boolean\n\
709\n\
710Return whether the object has an attribute with the given name.\n\
711(This is done by calling getattr(object, name) and catching exceptions.)";
712
713
Guido van Rossum79f25d91997-04-29 20:08:16 +0000714static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000715builtin_id(PyObject *self, PyObject *v)
Guido van Rossum5b722181993-03-30 17:46:03 +0000716{
Guido van Rossum106f2da2000-06-28 21:12:25 +0000717 return PyLong_FromVoidPtr(v);
Guido van Rossum5b722181993-03-30 17:46:03 +0000718}
719
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000720static char id_doc[] =
721"id(object) -> integer\n\
722\n\
723Return the identity of an object. This is guaranteed to be unique among\n\
724simultaneously existing objects. (Hint: it's the object's memory address.)";
725
726
Guido van Rossum79f25d91997-04-29 20:08:16 +0000727static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000728builtin_map(PyObject *self, PyObject *args)
Guido van Rossum12d12c51993-10-26 17:58:25 +0000729{
730 typedef struct {
Tim Peters4e9afdc2001-05-03 23:54:49 +0000731 PyObject *it; /* the iterator object */
732 int saw_StopIteration; /* bool: did the iterator end? */
Guido van Rossum12d12c51993-10-26 17:58:25 +0000733 } sequence;
734
Guido van Rossum79f25d91997-04-29 20:08:16 +0000735 PyObject *func, *result;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000736 sequence *seqs = NULL, *sqp;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000737 int n, len;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000738 register int i, j;
739
Guido van Rossum79f25d91997-04-29 20:08:16 +0000740 n = PyTuple_Size(args);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000741 if (n < 2) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000742 PyErr_SetString(PyExc_TypeError,
743 "map() requires at least two args");
Guido van Rossum12d12c51993-10-26 17:58:25 +0000744 return NULL;
745 }
746
Guido van Rossum79f25d91997-04-29 20:08:16 +0000747 func = PyTuple_GetItem(args, 0);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000748 n--;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000749
Guido van Rossumfa4ac711998-07-10 17:37:30 +0000750 if (func == Py_None && n == 1) {
751 /* map(None, S) is the same as list(S). */
752 return PySequence_List(PyTuple_GetItem(args, 1));
753 }
754
Tim Peters4e9afdc2001-05-03 23:54:49 +0000755 /* Get space for sequence descriptors. Must NULL out the iterator
756 * pointers so that jumping to Fail_2 later doesn't see trash.
757 */
Guido van Rossum79f25d91997-04-29 20:08:16 +0000758 if ((seqs = PyMem_NEW(sequence, n)) == NULL) {
759 PyErr_NoMemory();
Tim Peters4e9afdc2001-05-03 23:54:49 +0000760 return NULL;
761 }
762 for (i = 0; i < n; ++i) {
763 seqs[i].it = (PyObject*)NULL;
764 seqs[i].saw_StopIteration = 0;
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000765 }
Guido van Rossum12d12c51993-10-26 17:58:25 +0000766
Tim Peters4e9afdc2001-05-03 23:54:49 +0000767 /* Do a first pass to obtain iterators for the arguments, and set len
768 * to the largest of their lengths.
Tim Peters748b8bb2001-04-28 08:20:22 +0000769 */
Tim Peters4e9afdc2001-05-03 23:54:49 +0000770 len = 0;
771 for (i = 0, sqp = seqs; i < n; ++i, ++sqp) {
772 PyObject *curseq;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000773 int curlen;
Guido van Rossumad991772001-01-12 16:03:05 +0000774
Tim Peters4e9afdc2001-05-03 23:54:49 +0000775 /* Get iterator. */
776 curseq = PyTuple_GetItem(args, i+1);
777 sqp->it = PyObject_GetIter(curseq);
778 if (sqp->it == NULL) {
Guido van Rossum12d12c51993-10-26 17:58:25 +0000779 static char errmsg[] =
Tim Peters4e9afdc2001-05-03 23:54:49 +0000780 "argument %d to map() must support iteration";
Guido van Rossum15e33a41997-04-30 19:00:27 +0000781 char errbuf[sizeof(errmsg) + 25];
Guido van Rossum12d12c51993-10-26 17:58:25 +0000782 sprintf(errbuf, errmsg, i+2);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000783 PyErr_SetString(PyExc_TypeError, errbuf);
Guido van Rossum12d12c51993-10-26 17:58:25 +0000784 goto Fail_2;
785 }
786
Tim Peters4e9afdc2001-05-03 23:54:49 +0000787 /* Update len. */
788 curlen = -1; /* unknown */
789 if (PySequence_Check(curseq) &&
790 curseq->ob_type->tp_as_sequence->sq_length) {
791 curlen = PySequence_Size(curseq);
792 if (curlen < 0)
793 PyErr_Clear();
794 }
Tim Peters748b8bb2001-04-28 08:20:22 +0000795 if (curlen < 0)
Tim Peters4e9afdc2001-05-03 23:54:49 +0000796 curlen = 8; /* arbitrary */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000797 if (curlen > len)
798 len = curlen;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000799 }
800
Tim Peters4e9afdc2001-05-03 23:54:49 +0000801 /* Get space for the result list. */
Guido van Rossum79f25d91997-04-29 20:08:16 +0000802 if ((result = (PyObject *) PyList_New(len)) == NULL)
Guido van Rossum12d12c51993-10-26 17:58:25 +0000803 goto Fail_2;
804
Tim Peters4e9afdc2001-05-03 23:54:49 +0000805 /* Iterate over the sequences until all have stopped. */
Guido van Rossum2d951851994-08-29 12:52:16 +0000806 for (i = 0; ; ++i) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000807 PyObject *alist, *item=NULL, *value;
Tim Peters4e9afdc2001-05-03 23:54:49 +0000808 int numactive = 0;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000809
Guido van Rossum79f25d91997-04-29 20:08:16 +0000810 if (func == Py_None && n == 1)
Guido van Rossum32120311995-07-10 13:52:21 +0000811 alist = NULL;
Tim Peters4e9afdc2001-05-03 23:54:49 +0000812 else if ((alist = PyTuple_New(n)) == NULL)
813 goto Fail_1;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000814
815 for (j = 0, sqp = seqs; j < n; ++j, ++sqp) {
Tim Peters4e9afdc2001-05-03 23:54:49 +0000816 if (sqp->saw_StopIteration) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000817 Py_INCREF(Py_None);
818 item = Py_None;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000819 }
Guido van Rossum12d12c51993-10-26 17:58:25 +0000820 else {
Tim Peters4e9afdc2001-05-03 23:54:49 +0000821 item = PyIter_Next(sqp->it);
822 if (item)
823 ++numactive;
824 else {
Tim Peters4e9afdc2001-05-03 23:54:49 +0000825 if (PyErr_Occurred()) {
Tim Petersf4848da2001-05-05 00:14:56 +0000826 Py_XDECREF(alist);
827 goto Fail_1;
Guido van Rossum2d951851994-08-29 12:52:16 +0000828 }
Tim Peters4e9afdc2001-05-03 23:54:49 +0000829 Py_INCREF(Py_None);
830 item = Py_None;
831 sqp->saw_StopIteration = 1;
Guido van Rossum2d951851994-08-29 12:52:16 +0000832 }
Guido van Rossum12d12c51993-10-26 17:58:25 +0000833 }
Tim Peters4e9afdc2001-05-03 23:54:49 +0000834 if (alist)
835 PyTuple_SET_ITEM(alist, j, item);
836 else
Guido van Rossum2d951851994-08-29 12:52:16 +0000837 break;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000838 }
839
Guido van Rossum32120311995-07-10 13:52:21 +0000840 if (!alist)
841 alist = item;
Guido van Rossum2d951851994-08-29 12:52:16 +0000842
Tim Peters4e9afdc2001-05-03 23:54:49 +0000843 if (numactive == 0) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000844 Py_DECREF(alist);
Guido van Rossum2d951851994-08-29 12:52:16 +0000845 break;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000846 }
Guido van Rossum2d951851994-08-29 12:52:16 +0000847
Guido van Rossum79f25d91997-04-29 20:08:16 +0000848 if (func == Py_None)
Guido van Rossum32120311995-07-10 13:52:21 +0000849 value = alist;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000850 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000851 value = PyEval_CallObject(func, alist);
852 Py_DECREF(alist);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000853 if (value == NULL)
854 goto Fail_1;
Guido van Rossum2d951851994-08-29 12:52:16 +0000855 }
856 if (i >= len) {
Barry Warsawfa77e091999-01-28 18:49:12 +0000857 int status = PyList_Append(result, value);
Barry Warsaw21332871999-01-28 04:21:35 +0000858 Py_DECREF(value);
Barry Warsawfa77e091999-01-28 18:49:12 +0000859 if (status < 0)
860 goto Fail_1;
Guido van Rossum2d951851994-08-29 12:52:16 +0000861 }
Tim Peters4e9afdc2001-05-03 23:54:49 +0000862 else if (PyList_SetItem(result, i, value) < 0)
863 goto Fail_1;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000864 }
865
Guido van Rossumfa4ac711998-07-10 17:37:30 +0000866 if (i < len && PyList_SetSlice(result, i, len, NULL) < 0)
867 goto Fail_1;
868
Tim Peters4e9afdc2001-05-03 23:54:49 +0000869 goto Succeed;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000870
Guido van Rossum12d12c51993-10-26 17:58:25 +0000871Fail_1:
Guido van Rossum79f25d91997-04-29 20:08:16 +0000872 Py_DECREF(result);
Guido van Rossum12d12c51993-10-26 17:58:25 +0000873Fail_2:
Tim Peters4e9afdc2001-05-03 23:54:49 +0000874 result = NULL;
875Succeed:
876 assert(seqs);
877 for (i = 0; i < n; ++i)
878 Py_XDECREF(seqs[i].it);
879 PyMem_DEL(seqs);
880 return result;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000881}
882
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000883static char map_doc[] =
884"map(function, sequence[, sequence, ...]) -> list\n\
885\n\
886Return a list of the results of applying the function to the items of\n\
887the argument sequence(s). If more than one sequence is given, the\n\
888function is called with an argument list consisting of the corresponding\n\
889item of each sequence, substituting None for missing values when not all\n\
890sequences have the same length. If the function is None, return a list of\n\
891the items of the sequence (or a list of tuples if more than one sequence).";
892
893
Guido van Rossum79f25d91997-04-29 20:08:16 +0000894static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000895builtin_setattr(PyObject *self, PyObject *args)
Guido van Rossum33894be1992-01-27 16:53:09 +0000896{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000897 PyObject *v;
898 PyObject *name;
899 PyObject *value;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000900
Marc-André Lemburg691270f2000-09-18 16:22:27 +0000901 if (!PyArg_ParseTuple(args, "OOO:setattr", &v, &name, &value))
Guido van Rossum33894be1992-01-27 16:53:09 +0000902 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000903 if (PyObject_SetAttr(v, name, value) != 0)
Guido van Rossum33894be1992-01-27 16:53:09 +0000904 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000905 Py_INCREF(Py_None);
906 return Py_None;
Guido van Rossum33894be1992-01-27 16:53:09 +0000907}
908
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000909static char setattr_doc[] =
910"setattr(object, name, value)\n\
911\n\
912Set a named attribute on an object; setattr(x, 'y', v) is equivalent to\n\
913``x.y = v''.";
914
915
Guido van Rossum79f25d91997-04-29 20:08:16 +0000916static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000917builtin_delattr(PyObject *self, PyObject *args)
Guido van Rossum14144fc1994-08-29 12:53:40 +0000918{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000919 PyObject *v;
920 PyObject *name;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000921
Marc-André Lemburg691270f2000-09-18 16:22:27 +0000922 if (!PyArg_ParseTuple(args, "OO:delattr", &v, &name))
Guido van Rossum14144fc1994-08-29 12:53:40 +0000923 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000924 if (PyObject_SetAttr(v, name, (PyObject *)NULL) != 0)
Guido van Rossum14144fc1994-08-29 12:53:40 +0000925 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000926 Py_INCREF(Py_None);
927 return Py_None;
Guido van Rossum14144fc1994-08-29 12:53:40 +0000928}
929
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000930static char delattr_doc[] =
Guido van Rossumdf12a591998-11-23 22:13:04 +0000931"delattr(object, name)\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000932\n\
933Delete a named attribute on an object; delattr(x, 'y') is equivalent to\n\
934``del x.y''.";
935
936
Guido van Rossum79f25d91997-04-29 20:08:16 +0000937static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000938builtin_hash(PyObject *self, PyObject *v)
Guido van Rossum9bfef441993-03-29 10:43:31 +0000939{
Guido van Rossum9bfef441993-03-29 10:43:31 +0000940 long x;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000941
Guido van Rossum79f25d91997-04-29 20:08:16 +0000942 x = PyObject_Hash(v);
Guido van Rossum9bfef441993-03-29 10:43:31 +0000943 if (x == -1)
944 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000945 return PyInt_FromLong(x);
Guido van Rossum9bfef441993-03-29 10:43:31 +0000946}
947
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000948static char hash_doc[] =
949"hash(object) -> integer\n\
950\n\
951Return a hash value for the object. Two objects with the same value have\n\
952the same hash value. The reverse is not necessarily true, but likely.";
953
954
Guido van Rossum79f25d91997-04-29 20:08:16 +0000955static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000956builtin_hex(PyObject *self, PyObject *v)
Guido van Rossum006bcd41991-10-24 14:54:44 +0000957{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000958 PyNumberMethods *nb;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000959
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000960 if ((nb = v->ob_type->tp_as_number) == NULL ||
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000961 nb->nb_hex == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000962 PyErr_SetString(PyExc_TypeError,
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000963 "hex() argument can't be converted to hex");
964 return NULL;
Guido van Rossum006bcd41991-10-24 14:54:44 +0000965 }
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000966 return (*nb->nb_hex)(v);
Guido van Rossum006bcd41991-10-24 14:54:44 +0000967}
968
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000969static char hex_doc[] =
970"hex(number) -> string\n\
971\n\
972Return the hexadecimal representation of an integer or long integer.";
973
974
Tim Petersdbd9ba62000-07-09 03:09:57 +0000975static PyObject *builtin_raw_input(PyObject *, PyObject *);
Guido van Rossum3165fe61992-09-25 21:59:05 +0000976
Guido van Rossum79f25d91997-04-29 20:08:16 +0000977static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000978builtin_input(PyObject *self, PyObject *args)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000979{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000980 PyObject *line;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000981 char *str;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000982 PyObject *res;
983 PyObject *globals, *locals;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000984
985 line = builtin_raw_input(self, args);
Guido van Rossum3165fe61992-09-25 21:59:05 +0000986 if (line == NULL)
987 return line;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000988 if (!PyArg_Parse(line, "s;embedded '\\0' in input line", &str))
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000989 return NULL;
990 while (*str == ' ' || *str == '\t')
991 str++;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000992 globals = PyEval_GetGlobals();
993 locals = PyEval_GetLocals();
994 if (PyDict_GetItemString(globals, "__builtins__") == NULL) {
995 if (PyDict_SetItemString(globals, "__builtins__",
996 PyEval_GetBuiltins()) != 0)
Guido van Rossum6135a871995-01-09 17:53:26 +0000997 return NULL;
998 }
Guido van Rossumb05a5c71997-05-07 17:46:13 +0000999 res = PyRun_String(str, Py_eval_input, globals, locals);
Guido van Rossum79f25d91997-04-29 20:08:16 +00001000 Py_DECREF(line);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001001 return res;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001002}
1003
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001004static char input_doc[] =
1005"input([prompt]) -> value\n\
1006\n\
1007Equivalent to eval(raw_input(prompt)).";
1008
1009
Guido van Rossume8811f81997-02-14 15:48:05 +00001010static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001011builtin_intern(PyObject *self, PyObject *args)
Guido van Rossume8811f81997-02-14 15:48:05 +00001012{
1013 PyObject *s;
Guido van Rossum43713e52000-02-29 13:59:29 +00001014 if (!PyArg_ParseTuple(args, "S:intern", &s))
Guido van Rossume8811f81997-02-14 15:48:05 +00001015 return NULL;
1016 Py_INCREF(s);
1017 PyString_InternInPlace(&s);
1018 return s;
1019}
1020
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001021static char intern_doc[] =
1022"intern(string) -> string\n\
1023\n\
1024``Intern'' the given string. This enters the string in the (global)\n\
1025table of interned strings whose purpose is to speed up dictionary lookups.\n\
1026Return the string itself or the previously interned string object with the\n\
1027same value.";
1028
1029
Guido van Rossum79f25d91997-04-29 20:08:16 +00001030static PyObject *
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001031builtin_iter(PyObject *self, PyObject *args)
1032{
1033 PyObject *v, *w = NULL;
1034
1035 if (!PyArg_ParseTuple(args, "O|O:iter", &v, &w))
1036 return NULL;
1037 if (w == NULL)
1038 return PyObject_GetIter(v);
1039 if (!PyCallable_Check(v)) {
1040 PyErr_SetString(PyExc_TypeError,
1041 "iter(v, w): v must be callable");
1042 return NULL;
1043 }
1044 return PyCallIter_New(v, w);
1045}
1046
1047static char iter_doc[] =
1048"iter(collection) -> iterator\n\
1049iter(callable, sentinel) -> iterator\n\
1050\n\
1051Get an iterator from an object. In the first form, the argument must\n\
1052supply its own iterator, or be a sequence.\n\
1053In the second form, the callable is called until it returns the sentinel.";
1054
1055
1056static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001057builtin_len(PyObject *self, PyObject *v)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001058{
Guido van Rossum8ea9f4d1998-06-29 22:26:50 +00001059 long res;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001060
Jeremy Hylton03657cf2000-07-12 13:05:33 +00001061 res = PyObject_Size(v);
Guido van Rossum8ea9f4d1998-06-29 22:26:50 +00001062 if (res < 0 && PyErr_Occurred())
1063 return NULL;
1064 return PyInt_FromLong(res);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001065}
1066
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001067static char len_doc[] =
1068"len(object) -> integer\n\
1069\n\
1070Return the number of items of a sequence or mapping.";
1071
1072
Guido van Rossum79f25d91997-04-29 20:08:16 +00001073static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001074builtin_slice(PyObject *self, PyObject *args)
Guido van Rossum8861b741996-07-30 16:49:37 +00001075{
Guido van Rossum09df08a1998-05-22 00:51:39 +00001076 PyObject *start, *stop, *step;
Guido van Rossum8861b741996-07-30 16:49:37 +00001077
Guido van Rossum09df08a1998-05-22 00:51:39 +00001078 start = stop = step = NULL;
Guido van Rossum8861b741996-07-30 16:49:37 +00001079
Guido van Rossum09df08a1998-05-22 00:51:39 +00001080 if (!PyArg_ParseTuple(args, "O|OO:slice", &start, &stop, &step))
1081 return NULL;
Guido van Rossum8861b741996-07-30 16:49:37 +00001082
Guido van Rossum09df08a1998-05-22 00:51:39 +00001083 /* This swapping of stop and start is to maintain similarity with
1084 range(). */
1085 if (stop == NULL) {
1086 stop = start;
1087 start = NULL;
1088 }
1089 return PySlice_New(start, stop, step);
Guido van Rossum8861b741996-07-30 16:49:37 +00001090}
1091
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001092static char slice_doc[] =
Fred Drake3d587441999-07-19 15:21:16 +00001093"slice([start,] stop[, step]) -> slice object\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001094\n\
1095Create a slice object. This is used for slicing by the Numeric extensions.";
1096
1097
Guido van Rossum79f25d91997-04-29 20:08:16 +00001098static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001099builtin_locals(PyObject *self)
Guido van Rossum872537c1995-07-07 22:43:42 +00001100{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001101 PyObject *d;
Guido van Rossum872537c1995-07-07 22:43:42 +00001102
Guido van Rossum79f25d91997-04-29 20:08:16 +00001103 d = PyEval_GetLocals();
1104 Py_INCREF(d);
Guido van Rossum872537c1995-07-07 22:43:42 +00001105 return d;
1106}
1107
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001108static char locals_doc[] =
1109"locals() -> dictionary\n\
1110\n\
1111Return the dictionary containing the current scope's local variables.";
1112
1113
Guido van Rossum79f25d91997-04-29 20:08:16 +00001114static PyObject *
Guido van Rossum53451b32001-01-17 15:47:24 +00001115min_max(PyObject *args, int op)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001116{
Tim Petersc3074532001-05-03 07:00:32 +00001117 PyObject *v, *w, *x, *it;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001118
Guido van Rossum79f25d91997-04-29 20:08:16 +00001119 if (PyTuple_Size(args) > 1)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001120 v = args;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001121 else if (!PyArg_ParseTuple(args, "O:min/max", &v))
Guido van Rossum3f5da241990-12-20 15:06:42 +00001122 return NULL;
Tim Petersc3074532001-05-03 07:00:32 +00001123
1124 it = PyObject_GetIter(v);
1125 if (it == NULL)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001126 return NULL;
Tim Petersc3074532001-05-03 07:00:32 +00001127
1128 w = NULL; /* the result */
Tim Petersf4848da2001-05-05 00:14:56 +00001129 for (;;) {
Tim Petersc3074532001-05-03 07:00:32 +00001130 x = PyIter_Next(it);
Guido van Rossum2d951851994-08-29 12:52:16 +00001131 if (x == NULL) {
Tim Petersc3074532001-05-03 07:00:32 +00001132 if (PyErr_Occurred()) {
Tim Petersf4848da2001-05-05 00:14:56 +00001133 Py_XDECREF(w);
1134 Py_DECREF(it);
1135 return NULL;
Guido van Rossum2d951851994-08-29 12:52:16 +00001136 }
Tim Petersc3074532001-05-03 07:00:32 +00001137 break;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001138 }
Tim Petersc3074532001-05-03 07:00:32 +00001139
Guido van Rossum2d951851994-08-29 12:52:16 +00001140 if (w == NULL)
1141 w = x;
1142 else {
Guido van Rossum53451b32001-01-17 15:47:24 +00001143 int cmp = PyObject_RichCompareBool(x, w, op);
1144 if (cmp > 0) {
1145 Py_DECREF(w);
1146 w = x;
1147 }
1148 else if (cmp < 0) {
Guido van Rossumc8b6df91997-05-23 00:06:51 +00001149 Py_DECREF(x);
Tim Petersc3074532001-05-03 07:00:32 +00001150 Py_DECREF(w);
1151 Py_DECREF(it);
Guido van Rossumc8b6df91997-05-23 00:06:51 +00001152 return NULL;
1153 }
Guido van Rossum2d951851994-08-29 12:52:16 +00001154 else
Guido van Rossum79f25d91997-04-29 20:08:16 +00001155 Py_DECREF(x);
Guido van Rossum2d951851994-08-29 12:52:16 +00001156 }
Guido van Rossum3f5da241990-12-20 15:06:42 +00001157 }
Guido van Rossum2d951851994-08-29 12:52:16 +00001158 if (w == NULL)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001159 PyErr_SetString(PyExc_ValueError,
Fred Drake661ea262000-10-24 19:57:45 +00001160 "min() or max() arg is an empty sequence");
Tim Petersc3074532001-05-03 07:00:32 +00001161 Py_DECREF(it);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001162 return w;
1163}
1164
Guido van Rossum79f25d91997-04-29 20:08:16 +00001165static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001166builtin_min(PyObject *self, PyObject *v)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001167{
Guido van Rossum53451b32001-01-17 15:47:24 +00001168 return min_max(v, Py_LT);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001169}
1170
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001171static char min_doc[] =
1172"min(sequence) -> value\n\
1173min(a, b, c, ...) -> value\n\
1174\n\
1175With a single sequence argument, return its smallest item.\n\
1176With two or more arguments, return the smallest argument.";
1177
1178
Guido van Rossum79f25d91997-04-29 20:08:16 +00001179static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001180builtin_max(PyObject *self, PyObject *v)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001181{
Guido van Rossum53451b32001-01-17 15:47:24 +00001182 return min_max(v, Py_GT);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001183}
1184
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001185static char max_doc[] =
1186"max(sequence) -> value\n\
1187max(a, b, c, ...) -> value\n\
1188\n\
1189With a single sequence argument, return its largest item.\n\
1190With two or more arguments, return the largest argument.";
1191
1192
Guido van Rossum79f25d91997-04-29 20:08:16 +00001193static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001194builtin_oct(PyObject *self, PyObject *v)
Guido van Rossum006bcd41991-10-24 14:54:44 +00001195{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001196 PyNumberMethods *nb;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001197
Guido van Rossum1899c2e1992-09-12 11:09:23 +00001198 if (v == NULL || (nb = v->ob_type->tp_as_number) == NULL ||
1199 nb->nb_oct == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001200 PyErr_SetString(PyExc_TypeError,
Guido van Rossum1899c2e1992-09-12 11:09:23 +00001201 "oct() argument can't be converted to oct");
1202 return NULL;
Guido van Rossum006bcd41991-10-24 14:54:44 +00001203 }
Guido van Rossum1899c2e1992-09-12 11:09:23 +00001204 return (*nb->nb_oct)(v);
Guido van Rossum006bcd41991-10-24 14:54:44 +00001205}
1206
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001207static char oct_doc[] =
1208"oct(number) -> string\n\
1209\n\
1210Return the octal representation of an integer or long integer.";
1211
1212
Guido van Rossum79f25d91997-04-29 20:08:16 +00001213static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001214builtin_open(PyObject *self, PyObject *args)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001215{
Mark Hammondef8b6542001-05-13 08:04:26 +00001216 char *name = NULL;
Guido van Rossum2d951851994-08-29 12:52:16 +00001217 char *mode = "r";
1218 int bufsize = -1;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001219 PyObject *f;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001220
Mark Hammondef8b6542001-05-13 08:04:26 +00001221 if (!PyArg_ParseTuple(args, "et|si:open", Py_FileSystemDefaultEncoding,
1222 &name, &mode, &bufsize))
Guido van Rossum3f5da241990-12-20 15:06:42 +00001223 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001224 f = PyFile_FromString(name, mode);
Mark Hammondef8b6542001-05-13 08:04:26 +00001225 PyMem_Free(name); /* free the encoded string */
Guido van Rossum2d951851994-08-29 12:52:16 +00001226 if (f != NULL)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001227 PyFile_SetBufSize(f, bufsize);
Guido van Rossum2d951851994-08-29 12:52:16 +00001228 return f;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001229}
1230
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001231static char open_doc[] =
1232"open(filename[, mode[, buffering]]) -> file object\n\
1233\n\
1234Open a file. The mode can be 'r', 'w' or 'a' for reading (default),\n\
1235writing or appending. The file will be created if it doesn't exist\n\
1236when opened for writing or appending; it will be truncated when\n\
1237opened for writing. Add a 'b' to the mode for binary files.\n\
1238Add a '+' to the mode to allow simultaneous reading and writing.\n\
1239If the buffering argument is given, 0 means unbuffered, 1 means line\n\
1240buffered, and larger numbers specify the buffer size.";
1241
1242
Guido van Rossum79f25d91997-04-29 20:08:16 +00001243static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001244builtin_ord(PyObject *self, PyObject* obj)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001245{
Guido van Rossum09095f32000-03-10 23:00:52 +00001246 long ord;
Jeremy Hylton394b54d2000-04-12 21:19:47 +00001247 int size;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001248
Jeremy Hylton394b54d2000-04-12 21:19:47 +00001249 if (PyString_Check(obj)) {
1250 size = PyString_GET_SIZE(obj);
Andrew M. Kuchling34c20cf2000-12-20 14:36:56 +00001251 if (size == 1) {
Jeremy Hylton394b54d2000-04-12 21:19:47 +00001252 ord = (long)((unsigned char)*PyString_AS_STRING(obj));
Andrew M. Kuchling34c20cf2000-12-20 14:36:56 +00001253 return PyInt_FromLong(ord);
1254 }
Jeremy Hylton394b54d2000-04-12 21:19:47 +00001255 } else if (PyUnicode_Check(obj)) {
1256 size = PyUnicode_GET_SIZE(obj);
Andrew M. Kuchling34c20cf2000-12-20 14:36:56 +00001257 if (size == 1) {
Jeremy Hylton394b54d2000-04-12 21:19:47 +00001258 ord = (long)*PyUnicode_AS_UNICODE(obj);
Andrew M. Kuchling34c20cf2000-12-20 14:36:56 +00001259 return PyInt_FromLong(ord);
1260 }
Jeremy Hylton394b54d2000-04-12 21:19:47 +00001261 } else {
1262 PyErr_Format(PyExc_TypeError,
Andrew M. Kuchlingf07aad12000-12-23 14:11:28 +00001263 "ord() expected string of length 1, but " \
Jeremy Hylton394b54d2000-04-12 21:19:47 +00001264 "%.200s found", obj->ob_type->tp_name);
Guido van Rossum09095f32000-03-10 23:00:52 +00001265 return NULL;
1266 }
1267
Guido van Rossumad991772001-01-12 16:03:05 +00001268 PyErr_Format(PyExc_TypeError,
Andrew M. Kuchlingf07aad12000-12-23 14:11:28 +00001269 "ord() expected a character, "
1270 "but string of length %d found",
Jeremy Hylton394b54d2000-04-12 21:19:47 +00001271 size);
1272 return NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001273}
1274
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001275static char ord_doc[] =
1276"ord(c) -> integer\n\
1277\n\
Guido van Rossumad991772001-01-12 16:03:05 +00001278Return the integer ordinal of a one-character string.";
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001279
1280
Guido van Rossum79f25d91997-04-29 20:08:16 +00001281static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001282builtin_pow(PyObject *self, PyObject *args)
Guido van Rossum6a00cd81995-01-07 12:39:01 +00001283{
Guido van Rossum09df08a1998-05-22 00:51:39 +00001284 PyObject *v, *w, *z = Py_None;
Guido van Rossum6a00cd81995-01-07 12:39:01 +00001285
Guido van Rossum79f25d91997-04-29 20:08:16 +00001286 if (!PyArg_ParseTuple(args, "OO|O:pow", &v, &w, &z))
Guido van Rossum6a00cd81995-01-07 12:39:01 +00001287 return NULL;
Guido van Rossum09df08a1998-05-22 00:51:39 +00001288 return PyNumber_Power(v, w, z);
Guido van Rossumd4905451991-05-05 20:00:36 +00001289}
1290
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001291static char pow_doc[] =
1292"pow(x, y[, z]) -> number\n\
1293\n\
1294With two arguments, equivalent to x**y. With three arguments,\n\
1295equivalent to (x**y) % z, but may be more efficient (e.g. for longs).";
1296
1297
Guido van Rossum124eff01999-02-23 16:11:01 +00001298/* Return number of items in range/xrange (lo, hi, step). step > 0
1299 * required. Return a value < 0 if & only if the true value is too
1300 * large to fit in a signed long.
1301 */
1302static long
Thomas Wouterse28c2962000-07-23 22:21:32 +00001303get_len_of_range(long lo, long hi, long step)
Guido van Rossum124eff01999-02-23 16:11:01 +00001304{
1305 /* -------------------------------------------------------------
1306 If lo >= hi, the range is empty.
1307 Else if n values are in the range, the last one is
1308 lo + (n-1)*step, which must be <= hi-1. Rearranging,
1309 n <= (hi - lo - 1)/step + 1, so taking the floor of the RHS gives
1310 the proper value. Since lo < hi in this case, hi-lo-1 >= 0, so
1311 the RHS is non-negative and so truncation is the same as the
1312 floor. Letting M be the largest positive long, the worst case
1313 for the RHS numerator is hi=M, lo=-M-1, and then
1314 hi-lo-1 = M-(-M-1)-1 = 2*M. Therefore unsigned long has enough
1315 precision to compute the RHS exactly.
1316 ---------------------------------------------------------------*/
1317 long n = 0;
1318 if (lo < hi) {
1319 unsigned long uhi = (unsigned long)hi;
1320 unsigned long ulo = (unsigned long)lo;
1321 unsigned long diff = uhi - ulo - 1;
1322 n = (long)(diff / (unsigned long)step + 1);
1323 }
1324 return n;
1325}
1326
Guido van Rossum79f25d91997-04-29 20:08:16 +00001327static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001328builtin_range(PyObject *self, PyObject *args)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001329{
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001330 long ilow = 0, ihigh = 0, istep = 1;
Guido van Rossum124eff01999-02-23 16:11:01 +00001331 long bign;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001332 int i, n;
Guido van Rossum124eff01999-02-23 16:11:01 +00001333
Guido van Rossum79f25d91997-04-29 20:08:16 +00001334 PyObject *v;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001335
Guido van Rossum79f25d91997-04-29 20:08:16 +00001336 if (PyTuple_Size(args) <= 1) {
1337 if (!PyArg_ParseTuple(args,
Guido van Rossum0865dd91995-01-17 16:30:22 +00001338 "l;range() requires 1-3 int arguments",
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001339 &ihigh))
1340 return NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001341 }
1342 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001343 if (!PyArg_ParseTuple(args,
Guido van Rossum0865dd91995-01-17 16:30:22 +00001344 "ll|l;range() requires 1-3 int arguments",
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001345 &ilow, &ihigh, &istep))
Guido van Rossum3f5da241990-12-20 15:06:42 +00001346 return NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001347 }
1348 if (istep == 0) {
Fred Drake661ea262000-10-24 19:57:45 +00001349 PyErr_SetString(PyExc_ValueError, "range() arg 3 must not be zero");
Guido van Rossum3f5da241990-12-20 15:06:42 +00001350 return NULL;
1351 }
Guido van Rossum124eff01999-02-23 16:11:01 +00001352 if (istep > 0)
1353 bign = get_len_of_range(ilow, ihigh, istep);
1354 else
1355 bign = get_len_of_range(ihigh, ilow, -istep);
1356 n = (int)bign;
1357 if (bign < 0 || (long)n != bign) {
Guido van Rossume23cde21999-01-12 05:07:47 +00001358 PyErr_SetString(PyExc_OverflowError,
Fred Drake661ea262000-10-24 19:57:45 +00001359 "range() result has too many items");
Guido van Rossume23cde21999-01-12 05:07:47 +00001360 return NULL;
1361 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00001362 v = PyList_New(n);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001363 if (v == NULL)
1364 return NULL;
1365 for (i = 0; i < n; i++) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001366 PyObject *w = PyInt_FromLong(ilow);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001367 if (w == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001368 Py_DECREF(v);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001369 return NULL;
1370 }
Guido van Rossuma937d141998-04-24 18:22:02 +00001371 PyList_SET_ITEM(v, i, w);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001372 ilow += istep;
1373 }
1374 return v;
1375}
1376
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001377static char range_doc[] =
1378"range([start,] stop[, step]) -> list of integers\n\
1379\n\
1380Return a list containing an arithmetic progression of integers.\n\
1381range(i, j) returns [i, i+1, i+2, ..., j-1]; start (!) defaults to 0.\n\
1382When step is given, it specifies the increment (or decrement).\n\
1383For example, range(4) returns [0, 1, 2, 3]. The end point is omitted!\n\
1384These are exactly the valid indices for a list of 4 elements.";
1385
1386
Guido van Rossum79f25d91997-04-29 20:08:16 +00001387static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001388builtin_xrange(PyObject *self, PyObject *args)
Guido van Rossum12d12c51993-10-26 17:58:25 +00001389{
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001390 long ilow = 0, ihigh = 0, istep = 1;
Guido van Rossum0865dd91995-01-17 16:30:22 +00001391 long n;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001392
Guido van Rossum79f25d91997-04-29 20:08:16 +00001393 if (PyTuple_Size(args) <= 1) {
1394 if (!PyArg_ParseTuple(args,
Guido van Rossum0865dd91995-01-17 16:30:22 +00001395 "l;xrange() requires 1-3 int arguments",
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001396 &ihigh))
1397 return NULL;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001398 }
1399 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001400 if (!PyArg_ParseTuple(args,
Guido van Rossum0865dd91995-01-17 16:30:22 +00001401 "ll|l;xrange() requires 1-3 int arguments",
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001402 &ilow, &ihigh, &istep))
Guido van Rossum12d12c51993-10-26 17:58:25 +00001403 return NULL;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001404 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001405 if (istep == 0) {
Fred Drake661ea262000-10-24 19:57:45 +00001406 PyErr_SetString(PyExc_ValueError, "xrange() arg 3 must not be zero");
Guido van Rossum12d12c51993-10-26 17:58:25 +00001407 return NULL;
1408 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001409 if (istep > 0)
Guido van Rossum124eff01999-02-23 16:11:01 +00001410 n = get_len_of_range(ilow, ihigh, istep);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001411 else
Guido van Rossum124eff01999-02-23 16:11:01 +00001412 n = get_len_of_range(ihigh, ilow, -istep);
1413 if (n < 0) {
1414 PyErr_SetString(PyExc_OverflowError,
Fred Drake661ea262000-10-24 19:57:45 +00001415 "xrange() result has too many items");
Guido van Rossum124eff01999-02-23 16:11:01 +00001416 return NULL;
1417 }
Thomas Woutersefafcea2001-07-09 12:30:54 +00001418 return PyRange_New(ilow, n, istep, 1);
Guido van Rossum12d12c51993-10-26 17:58:25 +00001419}
1420
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001421static char xrange_doc[] =
1422"xrange([start,] stop[, step]) -> xrange object\n\
1423\n\
1424Like range(), but instead of returning a list, returns an object that\n\
1425generates the numbers in the range on demand. This is slightly slower\n\
1426than range() but more memory efficient.";
1427
1428
Guido van Rossum79f25d91997-04-29 20:08:16 +00001429static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001430builtin_raw_input(PyObject *self, PyObject *args)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001431{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001432 PyObject *v = NULL;
1433 PyObject *f;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001434
Guido van Rossum79f25d91997-04-29 20:08:16 +00001435 if (!PyArg_ParseTuple(args, "|O:[raw_]input", &v))
Guido van Rossum3165fe61992-09-25 21:59:05 +00001436 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001437 if (PyFile_AsFile(PySys_GetObject("stdin")) == stdin &&
1438 PyFile_AsFile(PySys_GetObject("stdout")) == stdout &&
Guido van Rossum53bb7ff1995-07-26 16:26:31 +00001439 isatty(fileno(stdin)) && isatty(fileno(stdout))) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001440 PyObject *po;
Guido van Rossum872537c1995-07-07 22:43:42 +00001441 char *prompt;
1442 char *s;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001443 PyObject *result;
Guido van Rossum872537c1995-07-07 22:43:42 +00001444 if (v != NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001445 po = PyObject_Str(v);
Guido van Rossum872537c1995-07-07 22:43:42 +00001446 if (po == NULL)
1447 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001448 prompt = PyString_AsString(po);
Guido van Rossumd9b52081998-06-26 18:25:38 +00001449 if (prompt == NULL)
1450 return NULL;
Guido van Rossum872537c1995-07-07 22:43:42 +00001451 }
1452 else {
1453 po = NULL;
1454 prompt = "";
1455 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00001456 s = PyOS_Readline(prompt);
1457 Py_XDECREF(po);
Guido van Rossum872537c1995-07-07 22:43:42 +00001458 if (s == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001459 PyErr_SetNone(PyExc_KeyboardInterrupt);
Guido van Rossum872537c1995-07-07 22:43:42 +00001460 return NULL;
1461 }
1462 if (*s == '\0') {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001463 PyErr_SetNone(PyExc_EOFError);
Guido van Rossum872537c1995-07-07 22:43:42 +00001464 result = NULL;
1465 }
1466 else { /* strip trailing '\n' */
Guido van Rossum106f2da2000-06-28 21:12:25 +00001467 size_t len = strlen(s);
1468 if (len > INT_MAX) {
1469 PyErr_SetString(PyExc_OverflowError, "input too long");
1470 result = NULL;
1471 }
1472 else {
1473 result = PyString_FromStringAndSize(s, (int)(len-1));
1474 }
Guido van Rossum872537c1995-07-07 22:43:42 +00001475 }
Guido van Rossumb18618d2000-05-03 23:44:39 +00001476 PyMem_FREE(s);
Guido van Rossum872537c1995-07-07 22:43:42 +00001477 return result;
1478 }
Guido van Rossum90933611991-06-07 16:10:43 +00001479 if (v != NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001480 f = PySys_GetObject("stdout");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001481 if (f == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001482 PyErr_SetString(PyExc_RuntimeError, "lost sys.stdout");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001483 return NULL;
1484 }
Guido van Rossumc8b6df91997-05-23 00:06:51 +00001485 if (Py_FlushLine() != 0 ||
1486 PyFile_WriteObject(v, f, Py_PRINT_RAW) != 0)
Guido van Rossum90933611991-06-07 16:10:43 +00001487 return NULL;
1488 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00001489 f = PySys_GetObject("stdin");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001490 if (f == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001491 PyErr_SetString(PyExc_RuntimeError, "lost sys.stdin");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001492 return NULL;
1493 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00001494 return PyFile_GetLine(f, -1);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001495}
1496
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001497static char raw_input_doc[] =
1498"raw_input([prompt]) -> string\n\
1499\n\
1500Read a string from standard input. The trailing newline is stripped.\n\
1501If the user hits EOF (Unix: Ctl-D, Windows: Ctl-Z+Return), raise EOFError.\n\
1502On Unix, GNU readline is used if enabled. The prompt string, if given,\n\
1503is printed without a trailing newline before reading.";
1504
1505
Guido van Rossum79f25d91997-04-29 20:08:16 +00001506static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001507builtin_reduce(PyObject *self, PyObject *args)
Guido van Rossum12d12c51993-10-26 17:58:25 +00001508{
Tim Peters15d81ef2001-05-04 04:39:21 +00001509 PyObject *seq, *func, *result = NULL, *it;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001510
Guido van Rossum79f25d91997-04-29 20:08:16 +00001511 if (!PyArg_ParseTuple(args, "OO|O:reduce", &func, &seq, &result))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001512 return NULL;
1513 if (result != NULL)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001514 Py_INCREF(result);
Guido van Rossum12d12c51993-10-26 17:58:25 +00001515
Tim Peters15d81ef2001-05-04 04:39:21 +00001516 it = PyObject_GetIter(seq);
1517 if (it == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001518 PyErr_SetString(PyExc_TypeError,
Tim Peters15d81ef2001-05-04 04:39:21 +00001519 "reduce() arg 2 must support iteration");
1520 Py_XDECREF(result);
Guido van Rossum12d12c51993-10-26 17:58:25 +00001521 return NULL;
1522 }
1523
Guido van Rossum79f25d91997-04-29 20:08:16 +00001524 if ((args = PyTuple_New(2)) == NULL)
Guido van Rossum2d951851994-08-29 12:52:16 +00001525 goto Fail;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001526
Tim Peters15d81ef2001-05-04 04:39:21 +00001527 for (;;) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001528 PyObject *op2;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001529
1530 if (args->ob_refcnt > 1) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001531 Py_DECREF(args);
1532 if ((args = PyTuple_New(2)) == NULL)
Guido van Rossum2d951851994-08-29 12:52:16 +00001533 goto Fail;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001534 }
1535
Tim Peters15d81ef2001-05-04 04:39:21 +00001536 op2 = PyIter_Next(it);
1537 if (op2 == NULL) {
Tim Petersf4848da2001-05-05 00:14:56 +00001538 if (PyErr_Occurred())
1539 goto Fail;
1540 break;
Guido van Rossum2d951851994-08-29 12:52:16 +00001541 }
Guido van Rossum12d12c51993-10-26 17:58:25 +00001542
Guido van Rossum2d951851994-08-29 12:52:16 +00001543 if (result == NULL)
1544 result = op2;
1545 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001546 PyTuple_SetItem(args, 0, result);
1547 PyTuple_SetItem(args, 1, op2);
1548 if ((result = PyEval_CallObject(func, args)) == NULL)
Guido van Rossum2d951851994-08-29 12:52:16 +00001549 goto Fail;
1550 }
Guido van Rossum12d12c51993-10-26 17:58:25 +00001551 }
1552
Guido van Rossum79f25d91997-04-29 20:08:16 +00001553 Py_DECREF(args);
Guido van Rossum12d12c51993-10-26 17:58:25 +00001554
Guido van Rossum2d951851994-08-29 12:52:16 +00001555 if (result == NULL)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001556 PyErr_SetString(PyExc_TypeError,
Fred Drake661ea262000-10-24 19:57:45 +00001557 "reduce() of empty sequence with no initial value");
Guido van Rossum2d951851994-08-29 12:52:16 +00001558
Tim Peters15d81ef2001-05-04 04:39:21 +00001559 Py_DECREF(it);
Guido van Rossum12d12c51993-10-26 17:58:25 +00001560 return result;
1561
Guido van Rossum2d951851994-08-29 12:52:16 +00001562Fail:
Guido van Rossum79f25d91997-04-29 20:08:16 +00001563 Py_XDECREF(args);
1564 Py_XDECREF(result);
Tim Peters15d81ef2001-05-04 04:39:21 +00001565 Py_DECREF(it);
Guido van Rossum12d12c51993-10-26 17:58:25 +00001566 return NULL;
1567}
1568
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001569static char reduce_doc[] =
1570"reduce(function, sequence[, initial]) -> value\n\
1571\n\
1572Apply a function of two arguments cumulatively to the items of a sequence,\n\
1573from left to right, so as to reduce the sequence to a single value.\n\
1574For example, reduce(lambda x, y: x+y, [1, 2, 3, 4, 5]) calculates\n\
1575((((1+2)+3)+4)+5). If initial is present, it is placed before the items\n\
1576of the sequence in the calculation, and serves as a default when the\n\
1577sequence is empty.";
1578
1579
Guido van Rossum79f25d91997-04-29 20:08:16 +00001580static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001581builtin_reload(PyObject *self, PyObject *v)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001582{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001583 return PyImport_ReloadModule(v);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001584}
1585
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001586static char reload_doc[] =
1587"reload(module) -> module\n\
1588\n\
1589Reload the module. The module must have been successfully imported before.";
1590
1591
Guido van Rossum79f25d91997-04-29 20:08:16 +00001592static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001593builtin_repr(PyObject *self, PyObject *v)
Guido van Rossumc89705d1992-11-26 08:54:07 +00001594{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001595 return PyObject_Repr(v);
Guido van Rossumc89705d1992-11-26 08:54:07 +00001596}
1597
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001598static char repr_doc[] =
1599"repr(object) -> string\n\
1600\n\
1601Return the canonical string representation of the object.\n\
1602For most object types, eval(repr(object)) == object.";
1603
1604
Guido van Rossum79f25d91997-04-29 20:08:16 +00001605static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001606builtin_round(PyObject *self, PyObject *args)
Guido van Rossum9e51f9b1993-02-12 16:29:05 +00001607{
Guido van Rossum9e51f9b1993-02-12 16:29:05 +00001608 double x;
1609 double f;
1610 int ndigits = 0;
Guido van Rossum9e51f9b1993-02-12 16:29:05 +00001611 int i;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001612
Guido van Rossum79f25d91997-04-29 20:08:16 +00001613 if (!PyArg_ParseTuple(args, "d|i:round", &x, &ndigits))
Guido van Rossum9e51f9b1993-02-12 16:29:05 +00001614 return NULL;
Guido van Rossum9e51f9b1993-02-12 16:29:05 +00001615 f = 1.0;
Guido van Rossum1e162d31998-05-09 14:42:25 +00001616 i = abs(ndigits);
1617 while (--i >= 0)
Guido van Rossum9e51f9b1993-02-12 16:29:05 +00001618 f = f*10.0;
Guido van Rossum1e162d31998-05-09 14:42:25 +00001619 if (ndigits < 0)
1620 x /= f;
Guido van Rossum9e51f9b1993-02-12 16:29:05 +00001621 else
Guido van Rossum1e162d31998-05-09 14:42:25 +00001622 x *= f;
1623 if (x >= 0.0)
1624 x = floor(x + 0.5);
1625 else
1626 x = ceil(x - 0.5);
1627 if (ndigits < 0)
1628 x *= f;
1629 else
1630 x /= f;
1631 return PyFloat_FromDouble(x);
Guido van Rossum9e51f9b1993-02-12 16:29:05 +00001632}
1633
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001634static char round_doc[] =
1635"round(number[, ndigits]) -> floating point number\n\
1636\n\
1637Round a number to a given precision in decimal digits (default 0 digits).\n\
1638This always returns a floating point number. Precision may be negative.";
1639
1640
Guido van Rossum79f25d91997-04-29 20:08:16 +00001641static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001642builtin_vars(PyObject *self, PyObject *args)
Guido van Rossum2d951851994-08-29 12:52:16 +00001643{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001644 PyObject *v = NULL;
1645 PyObject *d;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001646
Guido van Rossum79f25d91997-04-29 20:08:16 +00001647 if (!PyArg_ParseTuple(args, "|O:vars", &v))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001648 return NULL;
Guido van Rossum2d951851994-08-29 12:52:16 +00001649 if (v == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001650 d = PyEval_GetLocals();
Guido van Rossum53bb7ff1995-07-26 16:26:31 +00001651 if (d == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001652 if (!PyErr_Occurred())
1653 PyErr_SetString(PyExc_SystemError,
1654 "no locals!?");
Guido van Rossum53bb7ff1995-07-26 16:26:31 +00001655 }
1656 else
Guido van Rossum79f25d91997-04-29 20:08:16 +00001657 Py_INCREF(d);
Guido van Rossum2d951851994-08-29 12:52:16 +00001658 }
1659 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001660 d = PyObject_GetAttrString(v, "__dict__");
Guido van Rossum2d951851994-08-29 12:52:16 +00001661 if (d == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001662 PyErr_SetString(PyExc_TypeError,
Guido van Rossum2d951851994-08-29 12:52:16 +00001663 "vars() argument must have __dict__ attribute");
1664 return NULL;
1665 }
1666 }
1667 return d;
1668}
1669
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001670static char vars_doc[] =
1671"vars([object]) -> dictionary\n\
1672\n\
1673Without arguments, equivalent to locals().\n\
1674With an argument, equivalent to object.__dict__.";
1675
Barry Warsawcde8b1b1997-08-22 21:14:38 +00001676static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001677builtin_isinstance(PyObject *self, PyObject *args)
Barry Warsawcde8b1b1997-08-22 21:14:38 +00001678{
1679 PyObject *inst;
1680 PyObject *cls;
Guido van Rossum823649d2001-03-21 18:40:58 +00001681 int retval;
Barry Warsawcde8b1b1997-08-22 21:14:38 +00001682
Guido van Rossum43713e52000-02-29 13:59:29 +00001683 if (!PyArg_ParseTuple(args, "OO:isinstance", &inst, &cls))
Barry Warsawcde8b1b1997-08-22 21:14:38 +00001684 return NULL;
Guido van Rossumf5dd9141997-12-02 19:11:45 +00001685
Guido van Rossum823649d2001-03-21 18:40:58 +00001686 retval = PyObject_IsInstance(inst, cls);
1687 if (retval < 0)
Guido van Rossumad991772001-01-12 16:03:05 +00001688 return NULL;
Barry Warsawcde8b1b1997-08-22 21:14:38 +00001689 return PyInt_FromLong(retval);
1690}
1691
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001692static char isinstance_doc[] =
1693"isinstance(object, class-or-type) -> Boolean\n\
1694\n\
1695Return whether an object is an instance of a class or of a subclass thereof.\n\
1696With a type as second argument, return whether that is the object's type.";
1697
Barry Warsawcde8b1b1997-08-22 21:14:38 +00001698
1699static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001700builtin_issubclass(PyObject *self, PyObject *args)
Barry Warsawcde8b1b1997-08-22 21:14:38 +00001701{
1702 PyObject *derived;
1703 PyObject *cls;
1704 int retval;
1705
Guido van Rossum43713e52000-02-29 13:59:29 +00001706 if (!PyArg_ParseTuple(args, "OO:issubclass", &derived, &cls))
Barry Warsawcde8b1b1997-08-22 21:14:38 +00001707 return NULL;
Guido van Rossum668213d1999-06-16 17:28:37 +00001708
Guido van Rossum823649d2001-03-21 18:40:58 +00001709 retval = PyObject_IsSubclass(derived, cls);
1710 if (retval < 0)
1711 return NULL;
Barry Warsawcde8b1b1997-08-22 21:14:38 +00001712 return PyInt_FromLong(retval);
1713}
1714
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001715static char issubclass_doc[] =
1716"issubclass(C, B) -> Boolean\n\
1717\n\
1718Return whether class C is a subclass (i.e., a derived class) of class B.";
1719
Barry Warsawcde8b1b1997-08-22 21:14:38 +00001720
Barry Warsawbd599b52000-08-03 15:45:29 +00001721static PyObject*
1722builtin_zip(PyObject *self, PyObject *args)
1723{
1724 PyObject *ret;
1725 int itemsize = PySequence_Length(args);
Tim Peters8572b4f2001-05-06 01:05:02 +00001726 int i;
1727 PyObject *itlist; /* tuple of iterators */
Barry Warsawbd599b52000-08-03 15:45:29 +00001728
1729 if (itemsize < 1) {
1730 PyErr_SetString(PyExc_TypeError,
Fred Drake661ea262000-10-24 19:57:45 +00001731 "zip() requires at least one sequence");
Barry Warsawbd599b52000-08-03 15:45:29 +00001732 return NULL;
1733 }
1734 /* args must be a tuple */
1735 assert(PyTuple_Check(args));
1736
Tim Peters8572b4f2001-05-06 01:05:02 +00001737 /* allocate result list */
Barry Warsawbd599b52000-08-03 15:45:29 +00001738 if ((ret = PyList_New(0)) == NULL)
1739 return NULL;
1740
Tim Peters8572b4f2001-05-06 01:05:02 +00001741 /* obtain iterators */
1742 itlist = PyTuple_New(itemsize);
1743 if (itlist == NULL)
1744 goto Fail_ret;
1745 for (i = 0; i < itemsize; ++i) {
1746 PyObject *item = PyTuple_GET_ITEM(args, i);
1747 PyObject *it = PyObject_GetIter(item);
1748 if (it == NULL) {
1749 if (PyErr_ExceptionMatches(PyExc_TypeError))
1750 PyErr_Format(PyExc_TypeError,
1751 "zip argument #%d must support iteration",
1752 i+1);
1753 goto Fail_ret_itlist;
Barry Warsawbd599b52000-08-03 15:45:29 +00001754 }
Tim Peters8572b4f2001-05-06 01:05:02 +00001755 PyTuple_SET_ITEM(itlist, i, it);
1756 }
Barry Warsawbd599b52000-08-03 15:45:29 +00001757
Tim Peters8572b4f2001-05-06 01:05:02 +00001758 /* build result into ret list */
1759 for (;;) {
1760 int status;
1761 PyObject *next = PyTuple_New(itemsize);
1762 if (!next)
1763 goto Fail_ret_itlist;
1764
1765 for (i = 0; i < itemsize; i++) {
1766 PyObject *it = PyTuple_GET_ITEM(itlist, i);
1767 PyObject *item = PyIter_Next(it);
Barry Warsawbd599b52000-08-03 15:45:29 +00001768 if (!item) {
Tim Peters8572b4f2001-05-06 01:05:02 +00001769 if (PyErr_Occurred()) {
1770 Py_DECREF(ret);
1771 ret = NULL;
Barry Warsawbd599b52000-08-03 15:45:29 +00001772 }
1773 Py_DECREF(next);
Tim Peters8572b4f2001-05-06 01:05:02 +00001774 Py_DECREF(itlist);
1775 return ret;
Barry Warsawbd599b52000-08-03 15:45:29 +00001776 }
Tim Peters8572b4f2001-05-06 01:05:02 +00001777 PyTuple_SET_ITEM(next, i, item);
Barry Warsawbd599b52000-08-03 15:45:29 +00001778 }
Tim Peters8572b4f2001-05-06 01:05:02 +00001779
1780 status = PyList_Append(ret, next);
Barry Warsawbd599b52000-08-03 15:45:29 +00001781 Py_DECREF(next);
Tim Peters8572b4f2001-05-06 01:05:02 +00001782 if (status < 0)
1783 goto Fail_ret_itlist;
Barry Warsawbd599b52000-08-03 15:45:29 +00001784 }
Tim Peters8572b4f2001-05-06 01:05:02 +00001785
1786Fail_ret_itlist:
1787 Py_DECREF(itlist);
1788Fail_ret:
1789 Py_DECREF(ret);
1790 return NULL;
Barry Warsawbd599b52000-08-03 15:45:29 +00001791}
1792
1793
1794static char zip_doc[] =
1795"zip(seq1 [, seq2 [...]]) -> [(seq1[0], seq2[0] ...), (...)]\n\
1796\n\
1797Return a list of tuples, where each tuple contains the i-th element\n\
1798from each of the argument sequences. The returned list is truncated\n\
1799in length to the length of the shortest argument sequence.";
1800
1801
Guido van Rossum79f25d91997-04-29 20:08:16 +00001802static PyMethodDef builtin_methods[] = {
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001803 {"__import__", builtin___import__, METH_VARARGS, import_doc},
1804 {"abs", builtin_abs, METH_O, abs_doc},
1805 {"apply", builtin_apply, METH_VARARGS, apply_doc},
1806 {"buffer", builtin_buffer, METH_VARARGS, buffer_doc},
1807 {"callable", builtin_callable, METH_O, callable_doc},
1808 {"chr", builtin_chr, METH_VARARGS, chr_doc},
1809 {"cmp", builtin_cmp, METH_VARARGS, cmp_doc},
1810 {"coerce", builtin_coerce, METH_VARARGS, coerce_doc},
1811 {"compile", builtin_compile, METH_VARARGS, compile_doc},
1812 {"delattr", builtin_delattr, METH_VARARGS, delattr_doc},
1813 {"dir", builtin_dir, METH_VARARGS, dir_doc},
1814 {"divmod", builtin_divmod, METH_VARARGS, divmod_doc},
1815 {"eval", builtin_eval, METH_VARARGS, eval_doc},
1816 {"execfile", builtin_execfile, METH_VARARGS, execfile_doc},
1817 {"filter", builtin_filter, METH_VARARGS, filter_doc},
1818 {"getattr", builtin_getattr, METH_VARARGS, getattr_doc},
1819 {"globals", (PyCFunction)builtin_globals, METH_NOARGS, globals_doc},
1820 {"hasattr", builtin_hasattr, METH_VARARGS, hasattr_doc},
1821 {"hash", builtin_hash, METH_O, hash_doc},
1822 {"hex", builtin_hex, METH_O, hex_doc},
1823 {"id", builtin_id, METH_O, id_doc},
1824 {"input", builtin_input, METH_VARARGS, input_doc},
1825 {"intern", builtin_intern, METH_VARARGS, intern_doc},
1826 {"isinstance", builtin_isinstance, METH_VARARGS, isinstance_doc},
1827 {"issubclass", builtin_issubclass, METH_VARARGS, issubclass_doc},
1828 {"iter", builtin_iter, METH_VARARGS, iter_doc},
1829 {"len", builtin_len, METH_O, len_doc},
1830 {"locals", (PyCFunction)builtin_locals, METH_NOARGS, locals_doc},
1831 {"map", builtin_map, METH_VARARGS, map_doc},
1832 {"max", builtin_max, METH_VARARGS, max_doc},
1833 {"min", builtin_min, METH_VARARGS, min_doc},
1834 {"oct", builtin_oct, METH_O, oct_doc},
1835 {"open", builtin_open, METH_VARARGS, open_doc},
1836 {"ord", builtin_ord, METH_O, ord_doc},
1837 {"pow", builtin_pow, METH_VARARGS, pow_doc},
1838 {"range", builtin_range, METH_VARARGS, range_doc},
1839 {"raw_input", builtin_raw_input, METH_VARARGS, raw_input_doc},
1840 {"reduce", builtin_reduce, METH_VARARGS, reduce_doc},
1841 {"reload", builtin_reload, METH_O, reload_doc},
1842 {"repr", builtin_repr, METH_O, repr_doc},
1843 {"round", builtin_round, METH_VARARGS, round_doc},
1844 {"setattr", builtin_setattr, METH_VARARGS, setattr_doc},
1845 {"slice", builtin_slice, METH_VARARGS, slice_doc},
1846 {"unichr", builtin_unichr, METH_VARARGS, unichr_doc},
1847 {"vars", builtin_vars, METH_VARARGS, vars_doc},
1848 {"xrange", builtin_xrange, METH_VARARGS, xrange_doc},
1849 {"zip", builtin_zip, METH_VARARGS, zip_doc},
Guido van Rossumc02e15c1991-12-16 13:03:00 +00001850 {NULL, NULL},
Guido van Rossum3f5da241990-12-20 15:06:42 +00001851};
1852
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001853static char builtin_doc[] =
1854"Built-in functions, exceptions, and other objects.\n\
1855\n\
1856Noteworthy: None is the `nil' object; Ellipsis represents `...' in slices.";
1857
Guido van Rossum25ce5661997-08-02 03:10:38 +00001858PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001859_PyBuiltin_Init(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +00001860{
Fred Drake5550de32000-06-20 04:54:19 +00001861 PyObject *mod, *dict, *debug;
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001862 mod = Py_InitModule4("__builtin__", builtin_methods,
1863 builtin_doc, (PyObject *)NULL,
1864 PYTHON_API_VERSION);
Guido van Rossum25ce5661997-08-02 03:10:38 +00001865 if (mod == NULL)
1866 return NULL;
1867 dict = PyModule_GetDict(mod);
Guido van Rossum25ce5661997-08-02 03:10:38 +00001868 if (PyDict_SetItemString(dict, "None", Py_None) < 0)
1869 return NULL;
1870 if (PyDict_SetItemString(dict, "Ellipsis", Py_Ellipsis) < 0)
1871 return NULL;
Guido van Rossumad991772001-01-12 16:03:05 +00001872 if (PyDict_SetItemString(dict, "NotImplemented",
Neil Schemenauer23ab1992001-01-04 01:48:42 +00001873 Py_NotImplemented) < 0)
1874 return NULL;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001875 if (PyDict_SetItemString(dict, "classmethod",
1876 (PyObject *) &PyClassMethod_Type) < 0)
1877 return NULL;
1878#ifndef WITHOUT_COMPLEX
1879 if (PyDict_SetItemString(dict, "complex",
1880 (PyObject *) &PyComplex_Type) < 0)
1881 return NULL;
1882#endif
1883 if (PyDict_SetItemString(dict, "dictionary",
1884 (PyObject *) &PyDict_Type) < 0)
1885 return NULL;
1886 if (PyDict_SetItemString(dict, "float",
1887 (PyObject *) &PyFloat_Type) < 0)
1888 return NULL;
1889 if (PyDict_SetItemString(dict, "int", (PyObject *) &PyInt_Type) < 0)
1890 return NULL;
1891 if (PyDict_SetItemString(dict, "list", (PyObject *) &PyList_Type) < 0)
1892 return NULL;
1893 if (PyDict_SetItemString(dict, "long", (PyObject *) &PyLong_Type) < 0)
1894 return NULL;
1895 if (PyDict_SetItemString(dict, "object",
1896 (PyObject *) &PyBaseObject_Type) < 0)
1897 return NULL;
1898 if (PyDict_SetItemString(dict, "staticmethod",
1899 (PyObject *) &PyStaticMethod_Type) < 0)
1900 return NULL;
1901 if (PyDict_SetItemString(dict, "str", (PyObject *) &PyString_Type) < 0)
1902 return NULL;
1903 if (PyDict_SetItemString(dict, "tuple",
1904 (PyObject *) &PyTuple_Type) < 0)
1905 return NULL;
1906 if (PyDict_SetItemString(dict, "type", (PyObject *) &PyType_Type) < 0)
1907 return NULL;
1908 if (PyDict_SetItemString(dict, "unicode",
1909 (PyObject *) &PyUnicode_Type) < 0)
1910 return NULL;
Fred Drake5550de32000-06-20 04:54:19 +00001911 debug = PyInt_FromLong(Py_OptimizeFlag == 0);
1912 if (PyDict_SetItemString(dict, "__debug__", debug) < 0) {
1913 Py_XDECREF(debug);
Guido van Rossum25ce5661997-08-02 03:10:38 +00001914 return NULL;
Fred Drake5550de32000-06-20 04:54:19 +00001915 }
1916 Py_XDECREF(debug);
Barry Warsaw757af0e1997-08-29 22:13:51 +00001917
Guido van Rossum25ce5661997-08-02 03:10:38 +00001918 return mod;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001919}
1920
Guido van Rossume77a7571993-11-03 15:01:26 +00001921/* Helper for filter(): filter a tuple through a function */
Guido van Rossum12d12c51993-10-26 17:58:25 +00001922
Guido van Rossum79f25d91997-04-29 20:08:16 +00001923static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001924filtertuple(PyObject *func, PyObject *tuple)
Guido van Rossum12d12c51993-10-26 17:58:25 +00001925{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001926 PyObject *result;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001927 register int i, j;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001928 int len = PyTuple_Size(tuple);
Guido van Rossum12d12c51993-10-26 17:58:25 +00001929
Guido van Rossumb7b45621995-08-04 04:07:45 +00001930 if (len == 0) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001931 Py_INCREF(tuple);
Guido van Rossumb7b45621995-08-04 04:07:45 +00001932 return tuple;
1933 }
1934
Guido van Rossum79f25d91997-04-29 20:08:16 +00001935 if ((result = PyTuple_New(len)) == NULL)
Guido van Rossum2586bf01993-11-01 16:21:44 +00001936 return NULL;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001937
Guido van Rossum12d12c51993-10-26 17:58:25 +00001938 for (i = j = 0; i < len; ++i) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001939 PyObject *item, *good;
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00001940 int ok;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001941
Guido van Rossum79f25d91997-04-29 20:08:16 +00001942 if ((item = PyTuple_GetItem(tuple, i)) == NULL)
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00001943 goto Fail_1;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001944 if (func == Py_None) {
1945 Py_INCREF(item);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00001946 good = item;
1947 }
1948 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001949 PyObject *arg = Py_BuildValue("(O)", item);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00001950 if (arg == NULL)
1951 goto Fail_1;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001952 good = PyEval_CallObject(func, arg);
1953 Py_DECREF(arg);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00001954 if (good == NULL)
Guido van Rossum12d12c51993-10-26 17:58:25 +00001955 goto Fail_1;
1956 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00001957 ok = PyObject_IsTrue(good);
1958 Py_DECREF(good);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00001959 if (ok) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001960 Py_INCREF(item);
1961 if (PyTuple_SetItem(result, j++, item) < 0)
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00001962 goto Fail_1;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001963 }
Guido van Rossum12d12c51993-10-26 17:58:25 +00001964 }
1965
Tim Peters4324aa32001-05-28 22:30:08 +00001966 if (_PyTuple_Resize(&result, j) < 0)
Guido van Rossum12d12c51993-10-26 17:58:25 +00001967 return NULL;
1968
Guido van Rossum12d12c51993-10-26 17:58:25 +00001969 return result;
1970
Guido van Rossum12d12c51993-10-26 17:58:25 +00001971Fail_1:
Guido van Rossum79f25d91997-04-29 20:08:16 +00001972 Py_DECREF(result);
Guido van Rossum12d12c51993-10-26 17:58:25 +00001973 return NULL;
1974}
1975
1976
Guido van Rossume77a7571993-11-03 15:01:26 +00001977/* Helper for filter(): filter a string through a function */
Guido van Rossum12d12c51993-10-26 17:58:25 +00001978
Guido van Rossum79f25d91997-04-29 20:08:16 +00001979static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001980filterstring(PyObject *func, PyObject *strobj)
Guido van Rossum12d12c51993-10-26 17:58:25 +00001981{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001982 PyObject *result;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001983 register int i, j;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001984 int len = PyString_Size(strobj);
Guido van Rossum12d12c51993-10-26 17:58:25 +00001985
Guido van Rossum79f25d91997-04-29 20:08:16 +00001986 if (func == Py_None) {
Guido van Rossum2586bf01993-11-01 16:21:44 +00001987 /* No character is ever false -- share input string */
Guido van Rossum79f25d91997-04-29 20:08:16 +00001988 Py_INCREF(strobj);
Guido van Rossum2d951851994-08-29 12:52:16 +00001989 return strobj;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001990 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00001991 if ((result = PyString_FromStringAndSize(NULL, len)) == NULL)
Guido van Rossum2586bf01993-11-01 16:21:44 +00001992 return NULL;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001993
Guido van Rossum12d12c51993-10-26 17:58:25 +00001994 for (i = j = 0; i < len; ++i) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001995 PyObject *item, *arg, *good;
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00001996 int ok;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001997
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00001998 item = (*strobj->ob_type->tp_as_sequence->sq_item)(strobj, i);
1999 if (item == NULL)
2000 goto Fail_1;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002001 arg = Py_BuildValue("(O)", item);
Tim Peters388ed082001-04-07 20:34:48 +00002002 if (arg == NULL) {
2003 Py_DECREF(item);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002004 goto Fail_1;
Tim Peters388ed082001-04-07 20:34:48 +00002005 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002006 good = PyEval_CallObject(func, arg);
2007 Py_DECREF(arg);
Tim Peters388ed082001-04-07 20:34:48 +00002008 if (good == NULL) {
2009 Py_DECREF(item);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002010 goto Fail_1;
Tim Peters388ed082001-04-07 20:34:48 +00002011 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002012 ok = PyObject_IsTrue(good);
2013 Py_DECREF(good);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002014 if (ok)
Guido van Rossum79f25d91997-04-29 20:08:16 +00002015 PyString_AS_STRING((PyStringObject *)result)[j++] =
2016 PyString_AS_STRING((PyStringObject *)item)[0];
Tim Peters388ed082001-04-07 20:34:48 +00002017 Py_DECREF(item);
Guido van Rossum12d12c51993-10-26 17:58:25 +00002018 }
2019
Guido van Rossum79f25d91997-04-29 20:08:16 +00002020 if (j < len && _PyString_Resize(&result, j) < 0)
Guido van Rossum12d12c51993-10-26 17:58:25 +00002021 return NULL;
2022
Guido van Rossum12d12c51993-10-26 17:58:25 +00002023 return result;
2024
Guido van Rossum12d12c51993-10-26 17:58:25 +00002025Fail_1:
Guido van Rossum79f25d91997-04-29 20:08:16 +00002026 Py_DECREF(result);
Guido van Rossum12d12c51993-10-26 17:58:25 +00002027 return NULL;
2028}