blob: d3d32c9ff566fb8cb5a1ffd4a8eaab97fd77e8db [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
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000279#ifdef Py_USING_UNICODE
Guido van Rossum79f25d91997-04-29 20:08:16 +0000280static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000281builtin_unichr(PyObject *self, PyObject *args)
Guido van Rossum09095f32000-03-10 23:00:52 +0000282{
283 long x;
Fredrik Lundh0dcf67e2001-06-26 20:01:56 +0000284 Py_UNICODE s[2];
Guido van Rossum09095f32000-03-10 23:00:52 +0000285
286 if (!PyArg_ParseTuple(args, "l:unichr", &x))
287 return NULL;
Fredrik Lundh0dcf67e2001-06-26 20:01:56 +0000288
Marc-André Lemburgae21df52001-07-26 16:29:25 +0000289#ifdef Py_UNICODE_WIDE
Fredrik Lundh0dcf67e2001-06-26 20:01:56 +0000290 if (x < 0 || x > 0x10ffff) {
Guido van Rossum09095f32000-03-10 23:00:52 +0000291 PyErr_SetString(PyExc_ValueError,
Marc-André Lemburgae21df52001-07-26 16:29:25 +0000292 "unichr() arg not in range(0x110000) "
293 "(wide Python build)");
Guido van Rossum09095f32000-03-10 23:00:52 +0000294 return NULL;
295 }
Marc-André Lemburgae21df52001-07-26 16:29:25 +0000296#else
297 if (x < 0 || x > 0xffff) {
298 PyErr_SetString(PyExc_ValueError,
299 "unichr() arg not in range(0x10000) "
300 "(narrow Python build)");
301 return NULL;
302 }
303#endif
Fredrik Lundh0dcf67e2001-06-26 20:01:56 +0000304
305 if (x <= 0xffff) {
306 /* UCS-2 character */
307 s[0] = (Py_UNICODE) x;
308 return PyUnicode_FromUnicode(s, 1);
Guido van Rossum236d8b72001-06-26 23:12:25 +0000309 }
310 else {
Fredrik Lundh8f455852001-06-27 18:59:43 +0000311#ifndef Py_UNICODE_WIDE
Fredrik Lundh0dcf67e2001-06-26 20:01:56 +0000312 /* UCS-4 character. store as two surrogate characters */
313 x -= 0x10000L;
314 s[0] = 0xD800 + (Py_UNICODE) (x >> 10);
315 s[1] = 0xDC00 + (Py_UNICODE) (x & 0x03FF);
316 return PyUnicode_FromUnicode(s, 2);
Guido van Rossum236d8b72001-06-26 23:12:25 +0000317#else
318 s[0] = (Py_UNICODE)x;
319 return PyUnicode_FromUnicode(s, 1);
Martin v. Löwis0ba70cc2001-06-26 22:22:37 +0000320#endif
Fredrik Lundh0dcf67e2001-06-26 20:01:56 +0000321 }
Guido van Rossum09095f32000-03-10 23:00:52 +0000322}
323
324static char unichr_doc[] =
Fred Drake078b24f2000-04-13 02:42:50 +0000325"unichr(i) -> Unicode character\n\
Guido van Rossum09095f32000-03-10 23:00:52 +0000326\n\
Fredrik Lundh0dcf67e2001-06-26 20:01:56 +0000327Return a Unicode string of one character with ordinal i; 0 <= i <= 0x10ffff.";
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000328#endif
Guido van Rossum09095f32000-03-10 23:00:52 +0000329
330
331static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000332builtin_cmp(PyObject *self, PyObject *args)
Guido van Rossuma9e7dc11992-10-18 18:53:57 +0000333{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000334 PyObject *a, *b;
Guido van Rossum09df08a1998-05-22 00:51:39 +0000335 int c;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000336
Guido van Rossum79f25d91997-04-29 20:08:16 +0000337 if (!PyArg_ParseTuple(args, "OO:cmp", &a, &b))
Guido van Rossuma9e7dc11992-10-18 18:53:57 +0000338 return NULL;
Guido van Rossum09df08a1998-05-22 00:51:39 +0000339 if (PyObject_Cmp(a, b, &c) < 0)
Guido van Rossumc8b6df91997-05-23 00:06:51 +0000340 return NULL;
Guido van Rossum09df08a1998-05-22 00:51:39 +0000341 return PyInt_FromLong((long)c);
Guido van Rossuma9e7dc11992-10-18 18:53:57 +0000342}
343
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000344static char cmp_doc[] =
345"cmp(x, y) -> integer\n\
346\n\
347Return negative if x<y, zero if x==y, positive if x>y.";
348
349
Guido van Rossum79f25d91997-04-29 20:08:16 +0000350static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000351builtin_coerce(PyObject *self, PyObject *args)
Guido van Rossum6a00cd81995-01-07 12:39:01 +0000352{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000353 PyObject *v, *w;
354 PyObject *res;
Guido van Rossum5524a591995-01-10 15:26:20 +0000355
Guido van Rossum79f25d91997-04-29 20:08:16 +0000356 if (!PyArg_ParseTuple(args, "OO:coerce", &v, &w))
Guido van Rossum5524a591995-01-10 15:26:20 +0000357 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000358 if (PyNumber_Coerce(&v, &w) < 0)
Guido van Rossum04691fc1992-08-12 15:35:34 +0000359 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000360 res = Py_BuildValue("(OO)", v, w);
361 Py_DECREF(v);
362 Py_DECREF(w);
Guido van Rossum04691fc1992-08-12 15:35:34 +0000363 return res;
364}
365
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000366static char coerce_doc[] =
367"coerce(x, y) -> None or (x1, y1)\n\
368\n\
369When x and y can be coerced to values of the same type, return a tuple\n\
370containing the coerced values. When they can't be coerced, return None.";
371
372
Guido van Rossum79f25d91997-04-29 20:08:16 +0000373static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000374builtin_compile(PyObject *self, PyObject *args)
Guido van Rossum5b722181993-03-30 17:46:03 +0000375{
376 char *str;
377 char *filename;
378 char *startstr;
379 int start;
Tim Peters6cd6a822001-08-17 22:11:27 +0000380 int dont_inherit = 0;
381 int supplied_flags = 0;
Tim Peters5ba58662001-07-16 02:29:45 +0000382 PyCompilerFlags cf;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000383
Tim Peters6cd6a822001-08-17 22:11:27 +0000384 if (!PyArg_ParseTuple(args, "sss|ii:compile", &str, &filename,
385 &startstr, &supplied_flags, &dont_inherit))
Guido van Rossum5b722181993-03-30 17:46:03 +0000386 return NULL;
Tim Peters6cd6a822001-08-17 22:11:27 +0000387
Guido van Rossum5b722181993-03-30 17:46:03 +0000388 if (strcmp(startstr, "exec") == 0)
Guido van Rossumb05a5c71997-05-07 17:46:13 +0000389 start = Py_file_input;
Guido van Rossum5b722181993-03-30 17:46:03 +0000390 else if (strcmp(startstr, "eval") == 0)
Guido van Rossumb05a5c71997-05-07 17:46:13 +0000391 start = Py_eval_input;
Guido van Rossum872537c1995-07-07 22:43:42 +0000392 else if (strcmp(startstr, "single") == 0)
Guido van Rossumb05a5c71997-05-07 17:46:13 +0000393 start = Py_single_input;
Guido van Rossum5b722181993-03-30 17:46:03 +0000394 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000395 PyErr_SetString(PyExc_ValueError,
Fred Drake661ea262000-10-24 19:57:45 +0000396 "compile() arg 3 must be 'exec' or 'eval' or 'single'");
Guido van Rossum5b722181993-03-30 17:46:03 +0000397 return NULL;
398 }
Tim Peters6cd6a822001-08-17 22:11:27 +0000399
400 if (supplied_flags & ~(PyCF_MASK | PyCF_MASK_OBSOLETE)) {
401 PyErr_SetString(PyExc_ValueError,
402 "compile(): unrecognised flags");
403 return NULL;
404 }
405 /* XXX Warn if (supplied_flags & PyCF_MASK_OBSOLETE) != 0? */
406
407 cf.cf_flags = supplied_flags;
408 if (!dont_inherit) {
409 PyEval_MergeCompilerFlags(&cf);
410 }
411 return Py_CompileStringFlags(str, filename, start, &cf);
Guido van Rossum5b722181993-03-30 17:46:03 +0000412}
413
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000414static char compile_doc[] =
Tim Peters6cd6a822001-08-17 22:11:27 +0000415"compile(source, filename, mode[, flags[, dont_inherit]]) -> code object\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000416\n\
417Compile the source string (a Python module, statement or expression)\n\
418into a code object that can be executed by the exec statement or eval().\n\
419The filename will be used for run-time error messages.\n\
420The mode must be 'exec' to compile a module, 'single' to compile a\n\
Tim Peters6cd6a822001-08-17 22:11:27 +0000421single (interactive) statement, or 'eval' to compile an expression.\n\
422The flags argument, if present, controls which future statements influence\n\
423the compilation of the code.\n\
424The dont_inherit argument, if non-zero, stops the compilation inheriting\n\
425the effects of any future statements in effect in the code calling\n\
426compile; if absent or zero these statements do influence the compilation,\n\
427in addition to any features explicitly specified.";
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000428
Tim Peters5d2b77c2001-09-03 05:47:38 +0000429/* Merge the __dict__ of aclass into dict, and recursively also all
430 the __dict__s of aclass's base classes. The order of merging isn't
431 defined, as it's expected that only the final set of dict keys is
432 interesting.
433 Return 0 on success, -1 on error.
434*/
435
436static int
437merge_class_dict(PyObject* dict, PyObject* aclass)
438{
439 PyObject *classdict;
440 PyObject *bases;
441
442 assert(PyDict_Check(dict));
Tim Peters5d2b77c2001-09-03 05:47:38 +0000443 assert(aclass);
444
445 /* Merge in the type's dict (if any). */
446 classdict = PyObject_GetAttrString(aclass, "__dict__");
447 if (classdict == NULL)
448 PyErr_Clear();
449 else {
450 int status = PyDict_Update(dict, classdict);
451 Py_DECREF(classdict);
452 if (status < 0)
453 return -1;
454 }
455
456 /* Recursively merge in the base types' (if any) dicts. */
457 bases = PyObject_GetAttrString(aclass, "__bases__");
458 if (bases != NULL) {
459 int i, n;
460 assert(PyTuple_Check(bases));
461 n = PyTuple_GET_SIZE(bases);
462 for (i = 0; i < n; i++) {
463 PyObject *base = PyTuple_GET_ITEM(bases, i);
464 if (merge_class_dict(dict, base) < 0) {
465 Py_DECREF(bases);
466 return -1;
467 }
468 }
469 Py_DECREF(bases);
470 }
471 return 0;
472}
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000473
Guido van Rossum79f25d91997-04-29 20:08:16 +0000474static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000475builtin_dir(PyObject *self, PyObject *args)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000476{
Tim Peters5d2b77c2001-09-03 05:47:38 +0000477 PyObject *arg = NULL;
478 /* Set exactly one of these non-NULL before the end. */
479 PyObject *result = NULL; /* result list */
480 PyObject *masterdict = NULL; /* result is masterdict.keys() */
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000481
Tim Peters5d2b77c2001-09-03 05:47:38 +0000482 if (!PyArg_ParseTuple(args, "|O:dir", &arg))
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000483 return NULL;
Tim Peters5d2b77c2001-09-03 05:47:38 +0000484
485 /* If no arg, return the locals. */
486 if (arg == NULL) {
487 PyObject *locals = PyEval_GetLocals();
488 if (locals == NULL)
Guido van Rossum666b17a1997-05-06 16:36:57 +0000489 goto error;
Tim Peters37a309d2001-09-04 01:20:04 +0000490 result = PyDict_Keys(locals);
Tim Peters5d2b77c2001-09-03 05:47:38 +0000491 if (result == NULL)
Guido van Rossum666b17a1997-05-06 16:36:57 +0000492 goto error;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000493 }
Tim Peters5d2b77c2001-09-03 05:47:38 +0000494
495 /* Elif this is some form of module, we only want its dict. */
496 else if (PyObject_TypeCheck(arg, &PyModule_Type)) {
497 masterdict = PyObject_GetAttrString(arg, "__dict__");
498 if (masterdict == NULL)
499 goto error;
Tim Peters37a309d2001-09-04 01:20:04 +0000500 assert(PyDict_Check(masterdict));
Tim Peters5d2b77c2001-09-03 05:47:38 +0000501 }
502
Tim Peters37a309d2001-09-04 01:20:04 +0000503 /* Elif some form of type or class, grab its dict and its bases.
504 We deliberately don't suck up its __class__, as methods belonging
505 to the metaclass would probably be more confusing than helpful. */
506 else if (PyType_Check(arg) || PyClass_Check(arg)) {
Tim Peters5d2b77c2001-09-03 05:47:38 +0000507 masterdict = PyDict_New();
508 if (masterdict == NULL)
509 goto error;
510 if (merge_class_dict(masterdict, arg) < 0)
511 goto error;
512 }
513
514 /* Else look at its dict, and the attrs reachable from its class. */
Guido van Rossum3f5da241990-12-20 15:06:42 +0000515 else {
Tim Peters5d2b77c2001-09-03 05:47:38 +0000516 PyObject *itsclass;
Tim Peters37a309d2001-09-04 01:20:04 +0000517 /* Create a dict to start with. CAUTION: Not everything
518 responding to __dict__ returns a dict! */
Tim Peters5d2b77c2001-09-03 05:47:38 +0000519 masterdict = PyObject_GetAttrString(arg, "__dict__");
520 if (masterdict == NULL) {
Guido van Rossum795ba581996-05-23 22:49:07 +0000521 PyErr_Clear();
Tim Peters5d2b77c2001-09-03 05:47:38 +0000522 masterdict = PyDict_New();
Tim Peters37a309d2001-09-04 01:20:04 +0000523 }
524 else if (!PyDict_Check(masterdict)) {
525 Py_DECREF(masterdict);
526 masterdict = PyDict_New();
Guido van Rossum666b17a1997-05-06 16:36:57 +0000527 }
Tim Peters5d2b77c2001-09-03 05:47:38 +0000528 else {
529 /* The object may have returned a reference to its
530 dict, so copy it to avoid mutating it. */
531 PyObject *temp = PyDict_Copy(masterdict);
Tim Peters5d2b77c2001-09-03 05:47:38 +0000532 Py_DECREF(masterdict);
533 masterdict = temp;
534 }
Tim Peters37a309d2001-09-04 01:20:04 +0000535 if (masterdict == NULL)
536 goto error;
537
538 /* Merge in attrs reachable from its class.
539 CAUTION: Not all objects have a __class__ attr. */
Tim Peters5d2b77c2001-09-03 05:47:38 +0000540 itsclass = PyObject_GetAttrString(arg, "__class__");
Tim Peters5d2b77c2001-09-03 05:47:38 +0000541 if (itsclass == NULL)
542 PyErr_Clear();
543 else {
544 int status = merge_class_dict(masterdict, itsclass);
545 Py_DECREF(itsclass);
546 if (status < 0)
547 goto error;
Guido van Rossum795ba581996-05-23 22:49:07 +0000548 }
Guido van Rossum006bcd41991-10-24 14:54:44 +0000549 }
Tim Peters5d2b77c2001-09-03 05:47:38 +0000550
551 assert((result == NULL) ^ (masterdict == NULL));
552 if (masterdict != NULL) {
553 /* The result comes from its keys. */
554 assert(result == NULL);
Tim Peters37a309d2001-09-04 01:20:04 +0000555 result = PyDict_Keys(masterdict);
Tim Peters5d2b77c2001-09-03 05:47:38 +0000556 if (result == NULL)
557 goto error;
558 }
559
560 assert(result);
561 if (PyList_Sort(result) != 0)
Guido van Rossum666b17a1997-05-06 16:36:57 +0000562 goto error;
Tim Peters5d2b77c2001-09-03 05:47:38 +0000563 else
564 goto normal_return;
565
Guido van Rossum666b17a1997-05-06 16:36:57 +0000566 error:
Tim Peters5d2b77c2001-09-03 05:47:38 +0000567 Py_XDECREF(result);
568 result = NULL;
569 /* fall through */
570 normal_return:
571 Py_XDECREF(masterdict);
572 return result;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000573}
574
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000575static char dir_doc[] =
Tim Peters5d2b77c2001-09-03 05:47:38 +0000576"dir([object]) -> list of strings\n"
577"\n"
578"Return an alphabetized list of names comprising (some of) the attributes\n"
579"of the given object, and of attributes reachable from it:\n"
580"\n"
581"No argument: the names in the current scope.\n"
582"Module object: the module attributes.\n"
Tim Peters37a309d2001-09-04 01:20:04 +0000583"Type or class object: its attributes, and recursively the attributes of\n"
584" its bases.\n"
Tim Peters5d2b77c2001-09-03 05:47:38 +0000585"Otherwise: its attributes, its class's attributes, and recursively the\n"
586" attributes of its class's base classes.";
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000587
Guido van Rossum79f25d91997-04-29 20:08:16 +0000588static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000589builtin_divmod(PyObject *self, PyObject *args)
Guido van Rossum6a00cd81995-01-07 12:39:01 +0000590{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000591 PyObject *v, *w;
Guido van Rossum6a00cd81995-01-07 12:39:01 +0000592
Guido van Rossum79f25d91997-04-29 20:08:16 +0000593 if (!PyArg_ParseTuple(args, "OO:divmod", &v, &w))
Guido van Rossum6a00cd81995-01-07 12:39:01 +0000594 return NULL;
Guido van Rossum09df08a1998-05-22 00:51:39 +0000595 return PyNumber_Divmod(v, w);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000596}
597
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000598static char divmod_doc[] =
599"divmod(x, y) -> (div, mod)\n\
600\n\
601Return the tuple ((x-x%y)/y, x%y). Invariant: div*y + mod == x.";
602
603
Guido van Rossum79f25d91997-04-29 20:08:16 +0000604static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000605builtin_eval(PyObject *self, PyObject *args)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000606{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000607 PyObject *cmd;
608 PyObject *globals = Py_None, *locals = Py_None;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000609 char *str;
Tim Peters9fa96be2001-08-17 23:04:59 +0000610 PyCompilerFlags cf;
Guido van Rossum590baa41993-11-30 13:40:46 +0000611
Guido van Rossum79f25d91997-04-29 20:08:16 +0000612 if (!PyArg_ParseTuple(args, "O|O!O!:eval",
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000613 &cmd,
Guido van Rossum79f25d91997-04-29 20:08:16 +0000614 &PyDict_Type, &globals,
615 &PyDict_Type, &locals))
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000616 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000617 if (globals == Py_None) {
618 globals = PyEval_GetGlobals();
619 if (locals == Py_None)
620 locals = PyEval_GetLocals();
Guido van Rossum6135a871995-01-09 17:53:26 +0000621 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000622 else if (locals == Py_None)
Guido van Rossum6135a871995-01-09 17:53:26 +0000623 locals = globals;
Tim Peters9fa96be2001-08-17 23:04:59 +0000624
Guido van Rossum79f25d91997-04-29 20:08:16 +0000625 if (PyDict_GetItemString(globals, "__builtins__") == NULL) {
626 if (PyDict_SetItemString(globals, "__builtins__",
627 PyEval_GetBuiltins()) != 0)
Guido van Rossum6135a871995-01-09 17:53:26 +0000628 return NULL;
629 }
Tim Peters9fa96be2001-08-17 23:04:59 +0000630
Jeremy Hylton15c1c4f2001-07-30 21:50:55 +0000631 if (PyCode_Check(cmd)) {
632 if (PyTuple_GET_SIZE(((PyCodeObject *)cmd)->co_freevars) > 0) {
633 PyErr_SetString(PyExc_TypeError,
634 "code object passed to eval() may not contain free variables");
635 return NULL;
636 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000637 return PyEval_EvalCode((PyCodeObject *) cmd, globals, locals);
Jeremy Hylton15c1c4f2001-07-30 21:50:55 +0000638 }
Tim Peters9fa96be2001-08-17 23:04:59 +0000639
Marc-André Lemburgd1ba4432000-09-19 21:04:18 +0000640 if (!PyString_Check(cmd) &&
641 !PyUnicode_Check(cmd)) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000642 PyErr_SetString(PyExc_TypeError,
Fred Drake661ea262000-10-24 19:57:45 +0000643 "eval() arg 1 must be a string or code object");
Guido van Rossum94390a41992-08-14 15:14:30 +0000644 return NULL;
645 }
Marc-André Lemburgd1ba4432000-09-19 21:04:18 +0000646 if (PyString_AsStringAndSize(cmd, &str, NULL))
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000647 return NULL;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000648 while (*str == ' ' || *str == '\t')
649 str++;
Tim Peters9fa96be2001-08-17 23:04:59 +0000650
651 cf.cf_flags = 0;
652 (void)PyEval_MergeCompilerFlags(&cf);
653 return PyRun_StringFlags(str, Py_eval_input, globals, locals, &cf);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000654}
655
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000656static char eval_doc[] =
657"eval(source[, globals[, locals]]) -> value\n\
658\n\
659Evaluate the source in the context of globals and locals.\n\
660The source may be a string representing a Python expression\n\
661or a code object as returned by compile().\n\
662The globals and locals are dictionaries, defaulting to the current\n\
663globals and locals. If only globals is given, locals defaults to it.";
664
665
Guido van Rossum79f25d91997-04-29 20:08:16 +0000666static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000667builtin_execfile(PyObject *self, PyObject *args)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000668{
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000669 char *filename;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000670 PyObject *globals = Py_None, *locals = Py_None;
671 PyObject *res;
Guido van Rossum0f61f8a1992-02-25 18:55:05 +0000672 FILE* fp;
Tim Peters5ba58662001-07-16 02:29:45 +0000673 PyCompilerFlags cf;
Martin v. Löwis6b3a2c42001-08-08 05:30:36 +0000674 int exists;
675 struct stat s;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000676
Guido van Rossum79f25d91997-04-29 20:08:16 +0000677 if (!PyArg_ParseTuple(args, "s|O!O!:execfile",
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000678 &filename,
Guido van Rossum79f25d91997-04-29 20:08:16 +0000679 &PyDict_Type, &globals,
680 &PyDict_Type, &locals))
Guido van Rossum0f61f8a1992-02-25 18:55:05 +0000681 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000682 if (globals == Py_None) {
683 globals = PyEval_GetGlobals();
684 if (locals == Py_None)
685 locals = PyEval_GetLocals();
Guido van Rossum6135a871995-01-09 17:53:26 +0000686 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000687 else if (locals == Py_None)
Guido van Rossum6135a871995-01-09 17:53:26 +0000688 locals = globals;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000689 if (PyDict_GetItemString(globals, "__builtins__") == NULL) {
690 if (PyDict_SetItemString(globals, "__builtins__",
691 PyEval_GetBuiltins()) != 0)
Guido van Rossum6135a871995-01-09 17:53:26 +0000692 return NULL;
693 }
Martin v. Löwis6b3a2c42001-08-08 05:30:36 +0000694
695 exists = 0;
696 /* Test for existence or directory. */
697 if (!stat(filename, &s)) {
Martin v. Löwisf9836ba2001-08-08 10:28:06 +0000698 if (S_ISDIR(s.st_mode))
Martin v. Löwis6b3a2c42001-08-08 05:30:36 +0000699 errno = EISDIR;
700 else
701 exists = 1;
702 }
703
704 if (exists) {
705 Py_BEGIN_ALLOW_THREADS
706 fp = fopen(filename, "r");
707 Py_END_ALLOW_THREADS
708
709 if (fp == NULL) {
710 exists = 0;
711 }
712 }
713
714 if (!exists) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000715 PyErr_SetFromErrno(PyExc_IOError);
Guido van Rossum0f61f8a1992-02-25 18:55:05 +0000716 return NULL;
717 }
Tim Peters5ba58662001-07-16 02:29:45 +0000718 cf.cf_flags = 0;
719 if (PyEval_MergeCompilerFlags(&cf))
Tim Peters748b8bb2001-04-28 08:20:22 +0000720 res = PyRun_FileExFlags(fp, filename, Py_file_input, globals,
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000721 locals, 1, &cf);
Tim Peters5ba58662001-07-16 02:29:45 +0000722 else
Tim Peters748b8bb2001-04-28 08:20:22 +0000723 res = PyRun_FileEx(fp, filename, Py_file_input, globals,
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000724 locals, 1);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000725 return res;
Guido van Rossum0f61f8a1992-02-25 18:55:05 +0000726}
727
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000728static char execfile_doc[] =
729"execfile(filename[, globals[, locals]])\n\
730\n\
731Read and execute a Python script from a file.\n\
732The globals and locals are dictionaries, defaulting to the current\n\
733globals and locals. If only globals is given, locals defaults to it.";
734
735
Guido van Rossum79f25d91997-04-29 20:08:16 +0000736static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000737builtin_getattr(PyObject *self, PyObject *args)
Guido van Rossum33894be1992-01-27 16:53:09 +0000738{
Guido van Rossum950ff291998-06-29 13:38:57 +0000739 PyObject *v, *result, *dflt = NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000740 PyObject *name;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000741
Marc-André Lemburg691270f2000-09-18 16:22:27 +0000742 if (!PyArg_ParseTuple(args, "OO|O:getattr", &v, &name, &dflt))
Guido van Rossum33894be1992-01-27 16:53:09 +0000743 return NULL;
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000744#ifdef Py_USING_UNICODE
Jeremy Hylton0eb11152001-07-30 22:39:31 +0000745 if (PyUnicode_Check(name)) {
746 name = _PyUnicode_AsDefaultEncodedString(name, NULL);
747 if (name == NULL)
748 return NULL;
749 }
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000750#endif
Jeremy Hylton0eb11152001-07-30 22:39:31 +0000751
752 if (!PyString_Check(name)) {
753 PyErr_SetString(PyExc_TypeError,
754 "attribute name must be string");
755 return NULL;
756 }
Guido van Rossum950ff291998-06-29 13:38:57 +0000757 result = PyObject_GetAttr(v, name);
758 if (result == NULL && dflt != NULL) {
759 PyErr_Clear();
760 Py_INCREF(dflt);
761 result = dflt;
762 }
763 return result;
Guido van Rossum9bfef441993-03-29 10:43:31 +0000764}
765
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000766static char getattr_doc[] =
Guido van Rossum950ff291998-06-29 13:38:57 +0000767"getattr(object, name[, default]) -> value\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000768\n\
Guido van Rossum950ff291998-06-29 13:38:57 +0000769Get a named attribute from an object; getattr(x, 'y') is equivalent to x.y.\n\
770When a default argument is given, it is returned when the attribute doesn't\n\
771exist; without it, an exception is raised in that case.";
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000772
773
Guido van Rossum79f25d91997-04-29 20:08:16 +0000774static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000775builtin_globals(PyObject *self)
Guido van Rossum872537c1995-07-07 22:43:42 +0000776{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000777 PyObject *d;
Guido van Rossum872537c1995-07-07 22:43:42 +0000778
Guido van Rossum79f25d91997-04-29 20:08:16 +0000779 d = PyEval_GetGlobals();
780 Py_INCREF(d);
Guido van Rossum872537c1995-07-07 22:43:42 +0000781 return d;
782}
783
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000784static char globals_doc[] =
785"globals() -> dictionary\n\
786\n\
787Return the dictionary containing the current scope's global variables.";
788
789
Guido van Rossum79f25d91997-04-29 20:08:16 +0000790static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000791builtin_hasattr(PyObject *self, PyObject *args)
Guido van Rossum9bfef441993-03-29 10:43:31 +0000792{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000793 PyObject *v;
794 PyObject *name;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000795
Marc-André Lemburg691270f2000-09-18 16:22:27 +0000796 if (!PyArg_ParseTuple(args, "OO:hasattr", &v, &name))
Guido van Rossum9bfef441993-03-29 10:43:31 +0000797 return NULL;
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000798#ifdef Py_USING_UNICODE
Jeremy Hylton302b54a2001-07-30 22:45:19 +0000799 if (PyUnicode_Check(name)) {
800 name = _PyUnicode_AsDefaultEncodedString(name, NULL);
801 if (name == NULL)
802 return NULL;
803 }
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000804#endif
Jeremy Hylton302b54a2001-07-30 22:45:19 +0000805
806 if (!PyString_Check(name)) {
807 PyErr_SetString(PyExc_TypeError,
808 "attribute name must be string");
809 return NULL;
810 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000811 v = PyObject_GetAttr(v, name);
Guido van Rossum9bfef441993-03-29 10:43:31 +0000812 if (v == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000813 PyErr_Clear();
Guido van Rossum09df08a1998-05-22 00:51:39 +0000814 Py_INCREF(Py_False);
815 return Py_False;
Guido van Rossum9bfef441993-03-29 10:43:31 +0000816 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000817 Py_DECREF(v);
Guido van Rossum09df08a1998-05-22 00:51:39 +0000818 Py_INCREF(Py_True);
819 return Py_True;
Guido van Rossum33894be1992-01-27 16:53:09 +0000820}
821
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000822static char hasattr_doc[] =
823"hasattr(object, name) -> Boolean\n\
824\n\
825Return whether the object has an attribute with the given name.\n\
826(This is done by calling getattr(object, name) and catching exceptions.)";
827
828
Guido van Rossum79f25d91997-04-29 20:08:16 +0000829static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000830builtin_id(PyObject *self, PyObject *v)
Guido van Rossum5b722181993-03-30 17:46:03 +0000831{
Guido van Rossum106f2da2000-06-28 21:12:25 +0000832 return PyLong_FromVoidPtr(v);
Guido van Rossum5b722181993-03-30 17:46:03 +0000833}
834
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000835static char id_doc[] =
836"id(object) -> integer\n\
837\n\
838Return the identity of an object. This is guaranteed to be unique among\n\
839simultaneously existing objects. (Hint: it's the object's memory address.)";
840
841
Guido van Rossum79f25d91997-04-29 20:08:16 +0000842static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000843builtin_map(PyObject *self, PyObject *args)
Guido van Rossum12d12c51993-10-26 17:58:25 +0000844{
845 typedef struct {
Tim Peters4e9afdc2001-05-03 23:54:49 +0000846 PyObject *it; /* the iterator object */
847 int saw_StopIteration; /* bool: did the iterator end? */
Guido van Rossum12d12c51993-10-26 17:58:25 +0000848 } sequence;
849
Guido van Rossum79f25d91997-04-29 20:08:16 +0000850 PyObject *func, *result;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000851 sequence *seqs = NULL, *sqp;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000852 int n, len;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000853 register int i, j;
854
Guido van Rossum79f25d91997-04-29 20:08:16 +0000855 n = PyTuple_Size(args);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000856 if (n < 2) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000857 PyErr_SetString(PyExc_TypeError,
858 "map() requires at least two args");
Guido van Rossum12d12c51993-10-26 17:58:25 +0000859 return NULL;
860 }
861
Guido van Rossum79f25d91997-04-29 20:08:16 +0000862 func = PyTuple_GetItem(args, 0);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000863 n--;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000864
Guido van Rossumfa4ac711998-07-10 17:37:30 +0000865 if (func == Py_None && n == 1) {
866 /* map(None, S) is the same as list(S). */
867 return PySequence_List(PyTuple_GetItem(args, 1));
868 }
869
Tim Peters4e9afdc2001-05-03 23:54:49 +0000870 /* Get space for sequence descriptors. Must NULL out the iterator
871 * pointers so that jumping to Fail_2 later doesn't see trash.
872 */
Guido van Rossum79f25d91997-04-29 20:08:16 +0000873 if ((seqs = PyMem_NEW(sequence, n)) == NULL) {
874 PyErr_NoMemory();
Tim Peters4e9afdc2001-05-03 23:54:49 +0000875 return NULL;
876 }
877 for (i = 0; i < n; ++i) {
878 seqs[i].it = (PyObject*)NULL;
879 seqs[i].saw_StopIteration = 0;
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000880 }
Guido van Rossum12d12c51993-10-26 17:58:25 +0000881
Tim Peters4e9afdc2001-05-03 23:54:49 +0000882 /* Do a first pass to obtain iterators for the arguments, and set len
883 * to the largest of their lengths.
Tim Peters748b8bb2001-04-28 08:20:22 +0000884 */
Tim Peters4e9afdc2001-05-03 23:54:49 +0000885 len = 0;
886 for (i = 0, sqp = seqs; i < n; ++i, ++sqp) {
887 PyObject *curseq;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000888 int curlen;
Guido van Rossumad991772001-01-12 16:03:05 +0000889
Tim Peters4e9afdc2001-05-03 23:54:49 +0000890 /* Get iterator. */
891 curseq = PyTuple_GetItem(args, i+1);
892 sqp->it = PyObject_GetIter(curseq);
893 if (sqp->it == NULL) {
Guido van Rossum12d12c51993-10-26 17:58:25 +0000894 static char errmsg[] =
Tim Peters4e9afdc2001-05-03 23:54:49 +0000895 "argument %d to map() must support iteration";
Guido van Rossum15e33a41997-04-30 19:00:27 +0000896 char errbuf[sizeof(errmsg) + 25];
Guido van Rossum12d12c51993-10-26 17:58:25 +0000897 sprintf(errbuf, errmsg, i+2);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000898 PyErr_SetString(PyExc_TypeError, errbuf);
Guido van Rossum12d12c51993-10-26 17:58:25 +0000899 goto Fail_2;
900 }
901
Tim Peters4e9afdc2001-05-03 23:54:49 +0000902 /* Update len. */
903 curlen = -1; /* unknown */
904 if (PySequence_Check(curseq) &&
905 curseq->ob_type->tp_as_sequence->sq_length) {
906 curlen = PySequence_Size(curseq);
907 if (curlen < 0)
908 PyErr_Clear();
909 }
Tim Peters748b8bb2001-04-28 08:20:22 +0000910 if (curlen < 0)
Tim Peters4e9afdc2001-05-03 23:54:49 +0000911 curlen = 8; /* arbitrary */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000912 if (curlen > len)
913 len = curlen;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000914 }
915
Tim Peters4e9afdc2001-05-03 23:54:49 +0000916 /* Get space for the result list. */
Guido van Rossum79f25d91997-04-29 20:08:16 +0000917 if ((result = (PyObject *) PyList_New(len)) == NULL)
Guido van Rossum12d12c51993-10-26 17:58:25 +0000918 goto Fail_2;
919
Tim Peters4e9afdc2001-05-03 23:54:49 +0000920 /* Iterate over the sequences until all have stopped. */
Guido van Rossum2d951851994-08-29 12:52:16 +0000921 for (i = 0; ; ++i) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000922 PyObject *alist, *item=NULL, *value;
Tim Peters4e9afdc2001-05-03 23:54:49 +0000923 int numactive = 0;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000924
Guido van Rossum79f25d91997-04-29 20:08:16 +0000925 if (func == Py_None && n == 1)
Guido van Rossum32120311995-07-10 13:52:21 +0000926 alist = NULL;
Tim Peters4e9afdc2001-05-03 23:54:49 +0000927 else if ((alist = PyTuple_New(n)) == NULL)
928 goto Fail_1;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000929
930 for (j = 0, sqp = seqs; j < n; ++j, ++sqp) {
Tim Peters4e9afdc2001-05-03 23:54:49 +0000931 if (sqp->saw_StopIteration) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000932 Py_INCREF(Py_None);
933 item = Py_None;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000934 }
Guido van Rossum12d12c51993-10-26 17:58:25 +0000935 else {
Tim Peters4e9afdc2001-05-03 23:54:49 +0000936 item = PyIter_Next(sqp->it);
937 if (item)
938 ++numactive;
939 else {
Tim Peters4e9afdc2001-05-03 23:54:49 +0000940 if (PyErr_Occurred()) {
Tim Petersf4848da2001-05-05 00:14:56 +0000941 Py_XDECREF(alist);
942 goto Fail_1;
Guido van Rossum2d951851994-08-29 12:52:16 +0000943 }
Tim Peters4e9afdc2001-05-03 23:54:49 +0000944 Py_INCREF(Py_None);
945 item = Py_None;
946 sqp->saw_StopIteration = 1;
Guido van Rossum2d951851994-08-29 12:52:16 +0000947 }
Guido van Rossum12d12c51993-10-26 17:58:25 +0000948 }
Tim Peters4e9afdc2001-05-03 23:54:49 +0000949 if (alist)
950 PyTuple_SET_ITEM(alist, j, item);
951 else
Guido van Rossum2d951851994-08-29 12:52:16 +0000952 break;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000953 }
954
Guido van Rossum32120311995-07-10 13:52:21 +0000955 if (!alist)
956 alist = item;
Guido van Rossum2d951851994-08-29 12:52:16 +0000957
Tim Peters4e9afdc2001-05-03 23:54:49 +0000958 if (numactive == 0) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000959 Py_DECREF(alist);
Guido van Rossum2d951851994-08-29 12:52:16 +0000960 break;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000961 }
Guido van Rossum2d951851994-08-29 12:52:16 +0000962
Guido van Rossum79f25d91997-04-29 20:08:16 +0000963 if (func == Py_None)
Guido van Rossum32120311995-07-10 13:52:21 +0000964 value = alist;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000965 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000966 value = PyEval_CallObject(func, alist);
967 Py_DECREF(alist);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000968 if (value == NULL)
969 goto Fail_1;
Guido van Rossum2d951851994-08-29 12:52:16 +0000970 }
971 if (i >= len) {
Barry Warsawfa77e091999-01-28 18:49:12 +0000972 int status = PyList_Append(result, value);
Barry Warsaw21332871999-01-28 04:21:35 +0000973 Py_DECREF(value);
Barry Warsawfa77e091999-01-28 18:49:12 +0000974 if (status < 0)
975 goto Fail_1;
Guido van Rossum2d951851994-08-29 12:52:16 +0000976 }
Tim Peters4e9afdc2001-05-03 23:54:49 +0000977 else if (PyList_SetItem(result, i, value) < 0)
978 goto Fail_1;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000979 }
980
Guido van Rossumfa4ac711998-07-10 17:37:30 +0000981 if (i < len && PyList_SetSlice(result, i, len, NULL) < 0)
982 goto Fail_1;
983
Tim Peters4e9afdc2001-05-03 23:54:49 +0000984 goto Succeed;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000985
Guido van Rossum12d12c51993-10-26 17:58:25 +0000986Fail_1:
Guido van Rossum79f25d91997-04-29 20:08:16 +0000987 Py_DECREF(result);
Guido van Rossum12d12c51993-10-26 17:58:25 +0000988Fail_2:
Tim Peters4e9afdc2001-05-03 23:54:49 +0000989 result = NULL;
990Succeed:
991 assert(seqs);
992 for (i = 0; i < n; ++i)
993 Py_XDECREF(seqs[i].it);
994 PyMem_DEL(seqs);
995 return result;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000996}
997
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000998static char map_doc[] =
999"map(function, sequence[, sequence, ...]) -> list\n\
1000\n\
1001Return a list of the results of applying the function to the items of\n\
1002the argument sequence(s). If more than one sequence is given, the\n\
1003function is called with an argument list consisting of the corresponding\n\
1004item of each sequence, substituting None for missing values when not all\n\
1005sequences have the same length. If the function is None, return a list of\n\
1006the items of the sequence (or a list of tuples if more than one sequence).";
1007
1008
Guido van Rossum79f25d91997-04-29 20:08:16 +00001009static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001010builtin_setattr(PyObject *self, PyObject *args)
Guido van Rossum33894be1992-01-27 16:53:09 +00001011{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001012 PyObject *v;
1013 PyObject *name;
1014 PyObject *value;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001015
Marc-André Lemburg691270f2000-09-18 16:22:27 +00001016 if (!PyArg_ParseTuple(args, "OOO:setattr", &v, &name, &value))
Guido van Rossum33894be1992-01-27 16:53:09 +00001017 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001018 if (PyObject_SetAttr(v, name, value) != 0)
Guido van Rossum33894be1992-01-27 16:53:09 +00001019 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001020 Py_INCREF(Py_None);
1021 return Py_None;
Guido van Rossum33894be1992-01-27 16:53:09 +00001022}
1023
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001024static char setattr_doc[] =
1025"setattr(object, name, value)\n\
1026\n\
1027Set a named attribute on an object; setattr(x, 'y', v) is equivalent to\n\
1028``x.y = v''.";
1029
1030
Guido van Rossum79f25d91997-04-29 20:08:16 +00001031static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001032builtin_delattr(PyObject *self, PyObject *args)
Guido van Rossum14144fc1994-08-29 12:53:40 +00001033{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001034 PyObject *v;
1035 PyObject *name;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001036
Marc-André Lemburg691270f2000-09-18 16:22:27 +00001037 if (!PyArg_ParseTuple(args, "OO:delattr", &v, &name))
Guido van Rossum14144fc1994-08-29 12:53:40 +00001038 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001039 if (PyObject_SetAttr(v, name, (PyObject *)NULL) != 0)
Guido van Rossum14144fc1994-08-29 12:53:40 +00001040 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001041 Py_INCREF(Py_None);
1042 return Py_None;
Guido van Rossum14144fc1994-08-29 12:53:40 +00001043}
1044
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001045static char delattr_doc[] =
Guido van Rossumdf12a591998-11-23 22:13:04 +00001046"delattr(object, name)\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001047\n\
1048Delete a named attribute on an object; delattr(x, 'y') is equivalent to\n\
1049``del x.y''.";
1050
1051
Guido van Rossum79f25d91997-04-29 20:08:16 +00001052static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001053builtin_hash(PyObject *self, PyObject *v)
Guido van Rossum9bfef441993-03-29 10:43:31 +00001054{
Guido van Rossum9bfef441993-03-29 10:43:31 +00001055 long x;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001056
Guido van Rossum79f25d91997-04-29 20:08:16 +00001057 x = PyObject_Hash(v);
Guido van Rossum9bfef441993-03-29 10:43:31 +00001058 if (x == -1)
1059 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001060 return PyInt_FromLong(x);
Guido van Rossum9bfef441993-03-29 10:43:31 +00001061}
1062
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001063static char hash_doc[] =
1064"hash(object) -> integer\n\
1065\n\
1066Return a hash value for the object. Two objects with the same value have\n\
1067the same hash value. The reverse is not necessarily true, but likely.";
1068
1069
Guido van Rossum79f25d91997-04-29 20:08:16 +00001070static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001071builtin_hex(PyObject *self, PyObject *v)
Guido van Rossum006bcd41991-10-24 14:54:44 +00001072{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001073 PyNumberMethods *nb;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001074
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001075 if ((nb = v->ob_type->tp_as_number) == NULL ||
Guido van Rossum1899c2e1992-09-12 11:09:23 +00001076 nb->nb_hex == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001077 PyErr_SetString(PyExc_TypeError,
Guido van Rossum1899c2e1992-09-12 11:09:23 +00001078 "hex() argument can't be converted to hex");
1079 return NULL;
Guido van Rossum006bcd41991-10-24 14:54:44 +00001080 }
Guido van Rossum1899c2e1992-09-12 11:09:23 +00001081 return (*nb->nb_hex)(v);
Guido van Rossum006bcd41991-10-24 14:54:44 +00001082}
1083
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001084static char hex_doc[] =
1085"hex(number) -> string\n\
1086\n\
1087Return the hexadecimal representation of an integer or long integer.";
1088
1089
Tim Petersdbd9ba62000-07-09 03:09:57 +00001090static PyObject *builtin_raw_input(PyObject *, PyObject *);
Guido van Rossum3165fe61992-09-25 21:59:05 +00001091
Guido van Rossum79f25d91997-04-29 20:08:16 +00001092static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001093builtin_input(PyObject *self, PyObject *args)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001094{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001095 PyObject *line;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001096 char *str;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001097 PyObject *res;
1098 PyObject *globals, *locals;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001099
1100 line = builtin_raw_input(self, args);
Guido van Rossum3165fe61992-09-25 21:59:05 +00001101 if (line == NULL)
1102 return line;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001103 if (!PyArg_Parse(line, "s;embedded '\\0' in input line", &str))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001104 return NULL;
1105 while (*str == ' ' || *str == '\t')
1106 str++;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001107 globals = PyEval_GetGlobals();
1108 locals = PyEval_GetLocals();
1109 if (PyDict_GetItemString(globals, "__builtins__") == NULL) {
1110 if (PyDict_SetItemString(globals, "__builtins__",
1111 PyEval_GetBuiltins()) != 0)
Guido van Rossum6135a871995-01-09 17:53:26 +00001112 return NULL;
1113 }
Guido van Rossumb05a5c71997-05-07 17:46:13 +00001114 res = PyRun_String(str, Py_eval_input, globals, locals);
Guido van Rossum79f25d91997-04-29 20:08:16 +00001115 Py_DECREF(line);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001116 return res;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001117}
1118
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001119static char input_doc[] =
1120"input([prompt]) -> value\n\
1121\n\
1122Equivalent to eval(raw_input(prompt)).";
1123
1124
Guido van Rossume8811f81997-02-14 15:48:05 +00001125static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001126builtin_intern(PyObject *self, PyObject *args)
Guido van Rossume8811f81997-02-14 15:48:05 +00001127{
1128 PyObject *s;
Guido van Rossum43713e52000-02-29 13:59:29 +00001129 if (!PyArg_ParseTuple(args, "S:intern", &s))
Guido van Rossume8811f81997-02-14 15:48:05 +00001130 return NULL;
1131 Py_INCREF(s);
1132 PyString_InternInPlace(&s);
1133 return s;
1134}
1135
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001136static char intern_doc[] =
1137"intern(string) -> string\n\
1138\n\
1139``Intern'' the given string. This enters the string in the (global)\n\
1140table of interned strings whose purpose is to speed up dictionary lookups.\n\
1141Return the string itself or the previously interned string object with the\n\
1142same value.";
1143
1144
Guido van Rossum79f25d91997-04-29 20:08:16 +00001145static PyObject *
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001146builtin_iter(PyObject *self, PyObject *args)
1147{
1148 PyObject *v, *w = NULL;
1149
1150 if (!PyArg_ParseTuple(args, "O|O:iter", &v, &w))
1151 return NULL;
1152 if (w == NULL)
1153 return PyObject_GetIter(v);
1154 if (!PyCallable_Check(v)) {
1155 PyErr_SetString(PyExc_TypeError,
1156 "iter(v, w): v must be callable");
1157 return NULL;
1158 }
1159 return PyCallIter_New(v, w);
1160}
1161
1162static char iter_doc[] =
1163"iter(collection) -> iterator\n\
1164iter(callable, sentinel) -> iterator\n\
1165\n\
1166Get an iterator from an object. In the first form, the argument must\n\
1167supply its own iterator, or be a sequence.\n\
1168In the second form, the callable is called until it returns the sentinel.";
1169
1170
1171static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001172builtin_len(PyObject *self, PyObject *v)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001173{
Guido van Rossum8ea9f4d1998-06-29 22:26:50 +00001174 long res;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001175
Jeremy Hylton03657cf2000-07-12 13:05:33 +00001176 res = PyObject_Size(v);
Guido van Rossum8ea9f4d1998-06-29 22:26:50 +00001177 if (res < 0 && PyErr_Occurred())
1178 return NULL;
1179 return PyInt_FromLong(res);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001180}
1181
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001182static char len_doc[] =
1183"len(object) -> integer\n\
1184\n\
1185Return the number of items of a sequence or mapping.";
1186
1187
Guido van Rossum79f25d91997-04-29 20:08:16 +00001188static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001189builtin_slice(PyObject *self, PyObject *args)
Guido van Rossum8861b741996-07-30 16:49:37 +00001190{
Guido van Rossum09df08a1998-05-22 00:51:39 +00001191 PyObject *start, *stop, *step;
Guido van Rossum8861b741996-07-30 16:49:37 +00001192
Guido van Rossum09df08a1998-05-22 00:51:39 +00001193 start = stop = step = NULL;
Guido van Rossum8861b741996-07-30 16:49:37 +00001194
Guido van Rossum09df08a1998-05-22 00:51:39 +00001195 if (!PyArg_ParseTuple(args, "O|OO:slice", &start, &stop, &step))
1196 return NULL;
Guido van Rossum8861b741996-07-30 16:49:37 +00001197
Guido van Rossum09df08a1998-05-22 00:51:39 +00001198 /* This swapping of stop and start is to maintain similarity with
1199 range(). */
1200 if (stop == NULL) {
1201 stop = start;
1202 start = NULL;
1203 }
1204 return PySlice_New(start, stop, step);
Guido van Rossum8861b741996-07-30 16:49:37 +00001205}
1206
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001207static char slice_doc[] =
Fred Drake3d587441999-07-19 15:21:16 +00001208"slice([start,] stop[, step]) -> slice object\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001209\n\
1210Create a slice object. This is used for slicing by the Numeric extensions.";
1211
1212
Guido van Rossum79f25d91997-04-29 20:08:16 +00001213static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001214builtin_locals(PyObject *self)
Guido van Rossum872537c1995-07-07 22:43:42 +00001215{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001216 PyObject *d;
Guido van Rossum872537c1995-07-07 22:43:42 +00001217
Guido van Rossum79f25d91997-04-29 20:08:16 +00001218 d = PyEval_GetLocals();
1219 Py_INCREF(d);
Guido van Rossum872537c1995-07-07 22:43:42 +00001220 return d;
1221}
1222
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001223static char locals_doc[] =
1224"locals() -> dictionary\n\
1225\n\
1226Return the dictionary containing the current scope's local variables.";
1227
1228
Guido van Rossum79f25d91997-04-29 20:08:16 +00001229static PyObject *
Guido van Rossum53451b32001-01-17 15:47:24 +00001230min_max(PyObject *args, int op)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001231{
Tim Petersc3074532001-05-03 07:00:32 +00001232 PyObject *v, *w, *x, *it;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001233
Guido van Rossum79f25d91997-04-29 20:08:16 +00001234 if (PyTuple_Size(args) > 1)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001235 v = args;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001236 else if (!PyArg_ParseTuple(args, "O:min/max", &v))
Guido van Rossum3f5da241990-12-20 15:06:42 +00001237 return NULL;
Tim Petersc3074532001-05-03 07:00:32 +00001238
1239 it = PyObject_GetIter(v);
1240 if (it == NULL)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001241 return NULL;
Tim Petersc3074532001-05-03 07:00:32 +00001242
1243 w = NULL; /* the result */
Tim Petersf4848da2001-05-05 00:14:56 +00001244 for (;;) {
Tim Petersc3074532001-05-03 07:00:32 +00001245 x = PyIter_Next(it);
Guido van Rossum2d951851994-08-29 12:52:16 +00001246 if (x == NULL) {
Tim Petersc3074532001-05-03 07:00:32 +00001247 if (PyErr_Occurred()) {
Tim Petersf4848da2001-05-05 00:14:56 +00001248 Py_XDECREF(w);
1249 Py_DECREF(it);
1250 return NULL;
Guido van Rossum2d951851994-08-29 12:52:16 +00001251 }
Tim Petersc3074532001-05-03 07:00:32 +00001252 break;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001253 }
Tim Petersc3074532001-05-03 07:00:32 +00001254
Guido van Rossum2d951851994-08-29 12:52:16 +00001255 if (w == NULL)
1256 w = x;
1257 else {
Guido van Rossum53451b32001-01-17 15:47:24 +00001258 int cmp = PyObject_RichCompareBool(x, w, op);
1259 if (cmp > 0) {
1260 Py_DECREF(w);
1261 w = x;
1262 }
1263 else if (cmp < 0) {
Guido van Rossumc8b6df91997-05-23 00:06:51 +00001264 Py_DECREF(x);
Tim Petersc3074532001-05-03 07:00:32 +00001265 Py_DECREF(w);
1266 Py_DECREF(it);
Guido van Rossumc8b6df91997-05-23 00:06:51 +00001267 return NULL;
1268 }
Guido van Rossum2d951851994-08-29 12:52:16 +00001269 else
Guido van Rossum79f25d91997-04-29 20:08:16 +00001270 Py_DECREF(x);
Guido van Rossum2d951851994-08-29 12:52:16 +00001271 }
Guido van Rossum3f5da241990-12-20 15:06:42 +00001272 }
Guido van Rossum2d951851994-08-29 12:52:16 +00001273 if (w == NULL)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001274 PyErr_SetString(PyExc_ValueError,
Fred Drake661ea262000-10-24 19:57:45 +00001275 "min() or max() arg is an empty sequence");
Tim Petersc3074532001-05-03 07:00:32 +00001276 Py_DECREF(it);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001277 return w;
1278}
1279
Guido van Rossum79f25d91997-04-29 20:08:16 +00001280static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001281builtin_min(PyObject *self, PyObject *v)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001282{
Guido van Rossum53451b32001-01-17 15:47:24 +00001283 return min_max(v, Py_LT);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001284}
1285
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001286static char min_doc[] =
1287"min(sequence) -> value\n\
1288min(a, b, c, ...) -> value\n\
1289\n\
1290With a single sequence argument, return its smallest item.\n\
1291With two or more arguments, return the smallest argument.";
1292
1293
Guido van Rossum79f25d91997-04-29 20:08:16 +00001294static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001295builtin_max(PyObject *self, PyObject *v)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001296{
Guido van Rossum53451b32001-01-17 15:47:24 +00001297 return min_max(v, Py_GT);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001298}
1299
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001300static char max_doc[] =
1301"max(sequence) -> value\n\
1302max(a, b, c, ...) -> value\n\
1303\n\
1304With a single sequence argument, return its largest item.\n\
1305With two or more arguments, return the largest argument.";
1306
1307
Guido van Rossum79f25d91997-04-29 20:08:16 +00001308static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001309builtin_oct(PyObject *self, PyObject *v)
Guido van Rossum006bcd41991-10-24 14:54:44 +00001310{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001311 PyNumberMethods *nb;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001312
Guido van Rossum1899c2e1992-09-12 11:09:23 +00001313 if (v == NULL || (nb = v->ob_type->tp_as_number) == NULL ||
1314 nb->nb_oct == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001315 PyErr_SetString(PyExc_TypeError,
Guido van Rossum1899c2e1992-09-12 11:09:23 +00001316 "oct() argument can't be converted to oct");
1317 return NULL;
Guido van Rossum006bcd41991-10-24 14:54:44 +00001318 }
Guido van Rossum1899c2e1992-09-12 11:09:23 +00001319 return (*nb->nb_oct)(v);
Guido van Rossum006bcd41991-10-24 14:54:44 +00001320}
1321
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001322static char oct_doc[] =
1323"oct(number) -> string\n\
1324\n\
1325Return the octal representation of an integer or long integer.";
1326
1327
Guido van Rossum79f25d91997-04-29 20:08:16 +00001328static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001329builtin_open(PyObject *self, PyObject *args)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001330{
Mark Hammondef8b6542001-05-13 08:04:26 +00001331 char *name = NULL;
Guido van Rossum2d951851994-08-29 12:52:16 +00001332 char *mode = "r";
1333 int bufsize = -1;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001334 PyObject *f;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001335
Mark Hammondef8b6542001-05-13 08:04:26 +00001336 if (!PyArg_ParseTuple(args, "et|si:open", Py_FileSystemDefaultEncoding,
1337 &name, &mode, &bufsize))
Guido van Rossum3f5da241990-12-20 15:06:42 +00001338 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001339 f = PyFile_FromString(name, mode);
Mark Hammondef8b6542001-05-13 08:04:26 +00001340 PyMem_Free(name); /* free the encoded string */
Guido van Rossum2d951851994-08-29 12:52:16 +00001341 if (f != NULL)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001342 PyFile_SetBufSize(f, bufsize);
Guido van Rossum2d951851994-08-29 12:52:16 +00001343 return f;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001344}
1345
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001346static char open_doc[] =
1347"open(filename[, mode[, buffering]]) -> file object\n\
1348\n\
1349Open a file. The mode can be 'r', 'w' or 'a' for reading (default),\n\
1350writing or appending. The file will be created if it doesn't exist\n\
1351when opened for writing or appending; it will be truncated when\n\
1352opened for writing. Add a 'b' to the mode for binary files.\n\
1353Add a '+' to the mode to allow simultaneous reading and writing.\n\
1354If the buffering argument is given, 0 means unbuffered, 1 means line\n\
1355buffered, and larger numbers specify the buffer size.";
1356
1357
Guido van Rossum79f25d91997-04-29 20:08:16 +00001358static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001359builtin_ord(PyObject *self, PyObject* obj)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001360{
Guido van Rossum09095f32000-03-10 23:00:52 +00001361 long ord;
Jeremy Hylton394b54d2000-04-12 21:19:47 +00001362 int size;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001363
Jeremy Hylton394b54d2000-04-12 21:19:47 +00001364 if (PyString_Check(obj)) {
1365 size = PyString_GET_SIZE(obj);
Andrew M. Kuchling34c20cf2000-12-20 14:36:56 +00001366 if (size == 1) {
Jeremy Hylton394b54d2000-04-12 21:19:47 +00001367 ord = (long)((unsigned char)*PyString_AS_STRING(obj));
Andrew M. Kuchling34c20cf2000-12-20 14:36:56 +00001368 return PyInt_FromLong(ord);
1369 }
Martin v. Löwis339d0f72001-08-17 18:39:25 +00001370#ifdef Py_USING_UNICODE
Jeremy Hylton394b54d2000-04-12 21:19:47 +00001371 } else if (PyUnicode_Check(obj)) {
1372 size = PyUnicode_GET_SIZE(obj);
Andrew M. Kuchling34c20cf2000-12-20 14:36:56 +00001373 if (size == 1) {
Jeremy Hylton394b54d2000-04-12 21:19:47 +00001374 ord = (long)*PyUnicode_AS_UNICODE(obj);
Andrew M. Kuchling34c20cf2000-12-20 14:36:56 +00001375 return PyInt_FromLong(ord);
1376 }
Martin v. Löwis339d0f72001-08-17 18:39:25 +00001377#endif
Jeremy Hylton394b54d2000-04-12 21:19:47 +00001378 } else {
1379 PyErr_Format(PyExc_TypeError,
Andrew M. Kuchlingf07aad12000-12-23 14:11:28 +00001380 "ord() expected string of length 1, but " \
Jeremy Hylton394b54d2000-04-12 21:19:47 +00001381 "%.200s found", obj->ob_type->tp_name);
Guido van Rossum09095f32000-03-10 23:00:52 +00001382 return NULL;
1383 }
1384
Guido van Rossumad991772001-01-12 16:03:05 +00001385 PyErr_Format(PyExc_TypeError,
Andrew M. Kuchlingf07aad12000-12-23 14:11:28 +00001386 "ord() expected a character, "
1387 "but string of length %d found",
Jeremy Hylton394b54d2000-04-12 21:19:47 +00001388 size);
1389 return NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001390}
1391
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001392static char ord_doc[] =
1393"ord(c) -> integer\n\
1394\n\
Guido van Rossumad991772001-01-12 16:03:05 +00001395Return the integer ordinal of a one-character string.";
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001396
1397
Guido van Rossum79f25d91997-04-29 20:08:16 +00001398static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001399builtin_pow(PyObject *self, PyObject *args)
Guido van Rossum6a00cd81995-01-07 12:39:01 +00001400{
Guido van Rossum09df08a1998-05-22 00:51:39 +00001401 PyObject *v, *w, *z = Py_None;
Guido van Rossum6a00cd81995-01-07 12:39:01 +00001402
Guido van Rossum79f25d91997-04-29 20:08:16 +00001403 if (!PyArg_ParseTuple(args, "OO|O:pow", &v, &w, &z))
Guido van Rossum6a00cd81995-01-07 12:39:01 +00001404 return NULL;
Guido van Rossum09df08a1998-05-22 00:51:39 +00001405 return PyNumber_Power(v, w, z);
Guido van Rossumd4905451991-05-05 20:00:36 +00001406}
1407
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001408static char pow_doc[] =
1409"pow(x, y[, z]) -> number\n\
1410\n\
1411With two arguments, equivalent to x**y. With three arguments,\n\
1412equivalent to (x**y) % z, but may be more efficient (e.g. for longs).";
1413
1414
Guido van Rossum124eff01999-02-23 16:11:01 +00001415/* Return number of items in range/xrange (lo, hi, step). step > 0
1416 * required. Return a value < 0 if & only if the true value is too
1417 * large to fit in a signed long.
1418 */
1419static long
Thomas Wouterse28c2962000-07-23 22:21:32 +00001420get_len_of_range(long lo, long hi, long step)
Guido van Rossum124eff01999-02-23 16:11:01 +00001421{
1422 /* -------------------------------------------------------------
1423 If lo >= hi, the range is empty.
1424 Else if n values are in the range, the last one is
1425 lo + (n-1)*step, which must be <= hi-1. Rearranging,
1426 n <= (hi - lo - 1)/step + 1, so taking the floor of the RHS gives
1427 the proper value. Since lo < hi in this case, hi-lo-1 >= 0, so
1428 the RHS is non-negative and so truncation is the same as the
1429 floor. Letting M be the largest positive long, the worst case
1430 for the RHS numerator is hi=M, lo=-M-1, and then
1431 hi-lo-1 = M-(-M-1)-1 = 2*M. Therefore unsigned long has enough
1432 precision to compute the RHS exactly.
1433 ---------------------------------------------------------------*/
1434 long n = 0;
1435 if (lo < hi) {
1436 unsigned long uhi = (unsigned long)hi;
1437 unsigned long ulo = (unsigned long)lo;
1438 unsigned long diff = uhi - ulo - 1;
1439 n = (long)(diff / (unsigned long)step + 1);
1440 }
1441 return n;
1442}
1443
Guido van Rossum79f25d91997-04-29 20:08:16 +00001444static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001445builtin_range(PyObject *self, PyObject *args)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001446{
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001447 long ilow = 0, ihigh = 0, istep = 1;
Guido van Rossum124eff01999-02-23 16:11:01 +00001448 long bign;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001449 int i, n;
Guido van Rossum124eff01999-02-23 16:11:01 +00001450
Guido van Rossum79f25d91997-04-29 20:08:16 +00001451 PyObject *v;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001452
Guido van Rossum79f25d91997-04-29 20:08:16 +00001453 if (PyTuple_Size(args) <= 1) {
1454 if (!PyArg_ParseTuple(args,
Guido van Rossum0865dd91995-01-17 16:30:22 +00001455 "l;range() requires 1-3 int arguments",
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001456 &ihigh))
1457 return NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001458 }
1459 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001460 if (!PyArg_ParseTuple(args,
Guido van Rossum0865dd91995-01-17 16:30:22 +00001461 "ll|l;range() requires 1-3 int arguments",
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001462 &ilow, &ihigh, &istep))
Guido van Rossum3f5da241990-12-20 15:06:42 +00001463 return NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001464 }
1465 if (istep == 0) {
Fred Drake661ea262000-10-24 19:57:45 +00001466 PyErr_SetString(PyExc_ValueError, "range() arg 3 must not be zero");
Guido van Rossum3f5da241990-12-20 15:06:42 +00001467 return NULL;
1468 }
Guido van Rossum124eff01999-02-23 16:11:01 +00001469 if (istep > 0)
1470 bign = get_len_of_range(ilow, ihigh, istep);
1471 else
1472 bign = get_len_of_range(ihigh, ilow, -istep);
1473 n = (int)bign;
1474 if (bign < 0 || (long)n != bign) {
Guido van Rossume23cde21999-01-12 05:07:47 +00001475 PyErr_SetString(PyExc_OverflowError,
Fred Drake661ea262000-10-24 19:57:45 +00001476 "range() result has too many items");
Guido van Rossume23cde21999-01-12 05:07:47 +00001477 return NULL;
1478 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00001479 v = PyList_New(n);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001480 if (v == NULL)
1481 return NULL;
1482 for (i = 0; i < n; i++) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001483 PyObject *w = PyInt_FromLong(ilow);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001484 if (w == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001485 Py_DECREF(v);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001486 return NULL;
1487 }
Guido van Rossuma937d141998-04-24 18:22:02 +00001488 PyList_SET_ITEM(v, i, w);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001489 ilow += istep;
1490 }
1491 return v;
1492}
1493
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001494static char range_doc[] =
1495"range([start,] stop[, step]) -> list of integers\n\
1496\n\
1497Return a list containing an arithmetic progression of integers.\n\
1498range(i, j) returns [i, i+1, i+2, ..., j-1]; start (!) defaults to 0.\n\
1499When step is given, it specifies the increment (or decrement).\n\
1500For example, range(4) returns [0, 1, 2, 3]. The end point is omitted!\n\
1501These are exactly the valid indices for a list of 4 elements.";
1502
1503
Guido van Rossum79f25d91997-04-29 20:08:16 +00001504static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001505builtin_xrange(PyObject *self, PyObject *args)
Guido van Rossum12d12c51993-10-26 17:58:25 +00001506{
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001507 long ilow = 0, ihigh = 0, istep = 1;
Guido van Rossum0865dd91995-01-17 16:30:22 +00001508 long n;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001509
Guido van Rossum79f25d91997-04-29 20:08:16 +00001510 if (PyTuple_Size(args) <= 1) {
1511 if (!PyArg_ParseTuple(args,
Guido van Rossum0865dd91995-01-17 16:30:22 +00001512 "l;xrange() requires 1-3 int arguments",
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001513 &ihigh))
1514 return NULL;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001515 }
1516 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001517 if (!PyArg_ParseTuple(args,
Guido van Rossum0865dd91995-01-17 16:30:22 +00001518 "ll|l;xrange() requires 1-3 int arguments",
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001519 &ilow, &ihigh, &istep))
Guido van Rossum12d12c51993-10-26 17:58:25 +00001520 return NULL;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001521 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001522 if (istep == 0) {
Fred Drake661ea262000-10-24 19:57:45 +00001523 PyErr_SetString(PyExc_ValueError, "xrange() arg 3 must not be zero");
Guido van Rossum12d12c51993-10-26 17:58:25 +00001524 return NULL;
1525 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001526 if (istep > 0)
Guido van Rossum124eff01999-02-23 16:11:01 +00001527 n = get_len_of_range(ilow, ihigh, istep);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001528 else
Guido van Rossum124eff01999-02-23 16:11:01 +00001529 n = get_len_of_range(ihigh, ilow, -istep);
1530 if (n < 0) {
1531 PyErr_SetString(PyExc_OverflowError,
Fred Drake661ea262000-10-24 19:57:45 +00001532 "xrange() result has too many items");
Guido van Rossum124eff01999-02-23 16:11:01 +00001533 return NULL;
1534 }
Thomas Woutersefafcea2001-07-09 12:30:54 +00001535 return PyRange_New(ilow, n, istep, 1);
Guido van Rossum12d12c51993-10-26 17:58:25 +00001536}
1537
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001538static char xrange_doc[] =
1539"xrange([start,] stop[, step]) -> xrange object\n\
1540\n\
1541Like range(), but instead of returning a list, returns an object that\n\
1542generates the numbers in the range on demand. This is slightly slower\n\
1543than range() but more memory efficient.";
1544
1545
Guido van Rossum79f25d91997-04-29 20:08:16 +00001546static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001547builtin_raw_input(PyObject *self, PyObject *args)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001548{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001549 PyObject *v = NULL;
1550 PyObject *f;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001551
Guido van Rossum79f25d91997-04-29 20:08:16 +00001552 if (!PyArg_ParseTuple(args, "|O:[raw_]input", &v))
Guido van Rossum3165fe61992-09-25 21:59:05 +00001553 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001554 if (PyFile_AsFile(PySys_GetObject("stdin")) == stdin &&
1555 PyFile_AsFile(PySys_GetObject("stdout")) == stdout &&
Guido van Rossum53bb7ff1995-07-26 16:26:31 +00001556 isatty(fileno(stdin)) && isatty(fileno(stdout))) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001557 PyObject *po;
Guido van Rossum872537c1995-07-07 22:43:42 +00001558 char *prompt;
1559 char *s;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001560 PyObject *result;
Guido van Rossum872537c1995-07-07 22:43:42 +00001561 if (v != NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001562 po = PyObject_Str(v);
Guido van Rossum872537c1995-07-07 22:43:42 +00001563 if (po == NULL)
1564 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001565 prompt = PyString_AsString(po);
Guido van Rossumd9b52081998-06-26 18:25:38 +00001566 if (prompt == NULL)
1567 return NULL;
Guido van Rossum872537c1995-07-07 22:43:42 +00001568 }
1569 else {
1570 po = NULL;
1571 prompt = "";
1572 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00001573 s = PyOS_Readline(prompt);
1574 Py_XDECREF(po);
Guido van Rossum872537c1995-07-07 22:43:42 +00001575 if (s == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001576 PyErr_SetNone(PyExc_KeyboardInterrupt);
Guido van Rossum872537c1995-07-07 22:43:42 +00001577 return NULL;
1578 }
1579 if (*s == '\0') {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001580 PyErr_SetNone(PyExc_EOFError);
Guido van Rossum872537c1995-07-07 22:43:42 +00001581 result = NULL;
1582 }
1583 else { /* strip trailing '\n' */
Guido van Rossum106f2da2000-06-28 21:12:25 +00001584 size_t len = strlen(s);
1585 if (len > INT_MAX) {
1586 PyErr_SetString(PyExc_OverflowError, "input too long");
1587 result = NULL;
1588 }
1589 else {
1590 result = PyString_FromStringAndSize(s, (int)(len-1));
1591 }
Guido van Rossum872537c1995-07-07 22:43:42 +00001592 }
Guido van Rossumb18618d2000-05-03 23:44:39 +00001593 PyMem_FREE(s);
Guido van Rossum872537c1995-07-07 22:43:42 +00001594 return result;
1595 }
Guido van Rossum90933611991-06-07 16:10:43 +00001596 if (v != NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001597 f = PySys_GetObject("stdout");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001598 if (f == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001599 PyErr_SetString(PyExc_RuntimeError, "lost sys.stdout");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001600 return NULL;
1601 }
Guido van Rossumc8b6df91997-05-23 00:06:51 +00001602 if (Py_FlushLine() != 0 ||
1603 PyFile_WriteObject(v, f, Py_PRINT_RAW) != 0)
Guido van Rossum90933611991-06-07 16:10:43 +00001604 return NULL;
1605 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00001606 f = PySys_GetObject("stdin");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001607 if (f == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001608 PyErr_SetString(PyExc_RuntimeError, "lost sys.stdin");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001609 return NULL;
1610 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00001611 return PyFile_GetLine(f, -1);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001612}
1613
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001614static char raw_input_doc[] =
1615"raw_input([prompt]) -> string\n\
1616\n\
1617Read a string from standard input. The trailing newline is stripped.\n\
1618If the user hits EOF (Unix: Ctl-D, Windows: Ctl-Z+Return), raise EOFError.\n\
1619On Unix, GNU readline is used if enabled. The prompt string, if given,\n\
1620is printed without a trailing newline before reading.";
1621
1622
Guido van Rossum79f25d91997-04-29 20:08:16 +00001623static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001624builtin_reduce(PyObject *self, PyObject *args)
Guido van Rossum12d12c51993-10-26 17:58:25 +00001625{
Tim Peters15d81ef2001-05-04 04:39:21 +00001626 PyObject *seq, *func, *result = NULL, *it;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001627
Guido van Rossum79f25d91997-04-29 20:08:16 +00001628 if (!PyArg_ParseTuple(args, "OO|O:reduce", &func, &seq, &result))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001629 return NULL;
1630 if (result != NULL)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001631 Py_INCREF(result);
Guido van Rossum12d12c51993-10-26 17:58:25 +00001632
Tim Peters15d81ef2001-05-04 04:39:21 +00001633 it = PyObject_GetIter(seq);
1634 if (it == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001635 PyErr_SetString(PyExc_TypeError,
Tim Peters15d81ef2001-05-04 04:39:21 +00001636 "reduce() arg 2 must support iteration");
1637 Py_XDECREF(result);
Guido van Rossum12d12c51993-10-26 17:58:25 +00001638 return NULL;
1639 }
1640
Guido van Rossum79f25d91997-04-29 20:08:16 +00001641 if ((args = PyTuple_New(2)) == NULL)
Guido van Rossum2d951851994-08-29 12:52:16 +00001642 goto Fail;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001643
Tim Peters15d81ef2001-05-04 04:39:21 +00001644 for (;;) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001645 PyObject *op2;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001646
1647 if (args->ob_refcnt > 1) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001648 Py_DECREF(args);
1649 if ((args = PyTuple_New(2)) == NULL)
Guido van Rossum2d951851994-08-29 12:52:16 +00001650 goto Fail;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001651 }
1652
Tim Peters15d81ef2001-05-04 04:39:21 +00001653 op2 = PyIter_Next(it);
1654 if (op2 == NULL) {
Tim Petersf4848da2001-05-05 00:14:56 +00001655 if (PyErr_Occurred())
1656 goto Fail;
1657 break;
Guido van Rossum2d951851994-08-29 12:52:16 +00001658 }
Guido van Rossum12d12c51993-10-26 17:58:25 +00001659
Guido van Rossum2d951851994-08-29 12:52:16 +00001660 if (result == NULL)
1661 result = op2;
1662 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001663 PyTuple_SetItem(args, 0, result);
1664 PyTuple_SetItem(args, 1, op2);
1665 if ((result = PyEval_CallObject(func, args)) == NULL)
Guido van Rossum2d951851994-08-29 12:52:16 +00001666 goto Fail;
1667 }
Guido van Rossum12d12c51993-10-26 17:58:25 +00001668 }
1669
Guido van Rossum79f25d91997-04-29 20:08:16 +00001670 Py_DECREF(args);
Guido van Rossum12d12c51993-10-26 17:58:25 +00001671
Guido van Rossum2d951851994-08-29 12:52:16 +00001672 if (result == NULL)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001673 PyErr_SetString(PyExc_TypeError,
Fred Drake661ea262000-10-24 19:57:45 +00001674 "reduce() of empty sequence with no initial value");
Guido van Rossum2d951851994-08-29 12:52:16 +00001675
Tim Peters15d81ef2001-05-04 04:39:21 +00001676 Py_DECREF(it);
Guido van Rossum12d12c51993-10-26 17:58:25 +00001677 return result;
1678
Guido van Rossum2d951851994-08-29 12:52:16 +00001679Fail:
Guido van Rossum79f25d91997-04-29 20:08:16 +00001680 Py_XDECREF(args);
1681 Py_XDECREF(result);
Tim Peters15d81ef2001-05-04 04:39:21 +00001682 Py_DECREF(it);
Guido van Rossum12d12c51993-10-26 17:58:25 +00001683 return NULL;
1684}
1685
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001686static char reduce_doc[] =
1687"reduce(function, sequence[, initial]) -> value\n\
1688\n\
1689Apply a function of two arguments cumulatively to the items of a sequence,\n\
1690from left to right, so as to reduce the sequence to a single value.\n\
1691For example, reduce(lambda x, y: x+y, [1, 2, 3, 4, 5]) calculates\n\
1692((((1+2)+3)+4)+5). If initial is present, it is placed before the items\n\
1693of the sequence in the calculation, and serves as a default when the\n\
1694sequence is empty.";
1695
1696
Guido van Rossum79f25d91997-04-29 20:08:16 +00001697static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001698builtin_reload(PyObject *self, PyObject *v)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001699{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001700 return PyImport_ReloadModule(v);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001701}
1702
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001703static char reload_doc[] =
1704"reload(module) -> module\n\
1705\n\
1706Reload the module. The module must have been successfully imported before.";
1707
1708
Guido van Rossum79f25d91997-04-29 20:08:16 +00001709static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001710builtin_repr(PyObject *self, PyObject *v)
Guido van Rossumc89705d1992-11-26 08:54:07 +00001711{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001712 return PyObject_Repr(v);
Guido van Rossumc89705d1992-11-26 08:54:07 +00001713}
1714
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001715static char repr_doc[] =
1716"repr(object) -> string\n\
1717\n\
1718Return the canonical string representation of the object.\n\
1719For most object types, eval(repr(object)) == object.";
1720
1721
Guido van Rossum79f25d91997-04-29 20:08:16 +00001722static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001723builtin_round(PyObject *self, PyObject *args)
Guido van Rossum9e51f9b1993-02-12 16:29:05 +00001724{
Guido van Rossum9e51f9b1993-02-12 16:29:05 +00001725 double x;
1726 double f;
1727 int ndigits = 0;
Guido van Rossum9e51f9b1993-02-12 16:29:05 +00001728 int i;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001729
Guido van Rossum79f25d91997-04-29 20:08:16 +00001730 if (!PyArg_ParseTuple(args, "d|i:round", &x, &ndigits))
Guido van Rossum9e51f9b1993-02-12 16:29:05 +00001731 return NULL;
Guido van Rossum9e51f9b1993-02-12 16:29:05 +00001732 f = 1.0;
Guido van Rossum1e162d31998-05-09 14:42:25 +00001733 i = abs(ndigits);
1734 while (--i >= 0)
Guido van Rossum9e51f9b1993-02-12 16:29:05 +00001735 f = f*10.0;
Guido van Rossum1e162d31998-05-09 14:42:25 +00001736 if (ndigits < 0)
1737 x /= f;
Guido van Rossum9e51f9b1993-02-12 16:29:05 +00001738 else
Guido van Rossum1e162d31998-05-09 14:42:25 +00001739 x *= f;
1740 if (x >= 0.0)
1741 x = floor(x + 0.5);
1742 else
1743 x = ceil(x - 0.5);
1744 if (ndigits < 0)
1745 x *= f;
1746 else
1747 x /= f;
1748 return PyFloat_FromDouble(x);
Guido van Rossum9e51f9b1993-02-12 16:29:05 +00001749}
1750
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001751static char round_doc[] =
1752"round(number[, ndigits]) -> floating point number\n\
1753\n\
1754Round a number to a given precision in decimal digits (default 0 digits).\n\
1755This always returns a floating point number. Precision may be negative.";
1756
1757
Guido van Rossum79f25d91997-04-29 20:08:16 +00001758static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001759builtin_vars(PyObject *self, PyObject *args)
Guido van Rossum2d951851994-08-29 12:52:16 +00001760{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001761 PyObject *v = NULL;
1762 PyObject *d;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001763
Guido van Rossum79f25d91997-04-29 20:08:16 +00001764 if (!PyArg_ParseTuple(args, "|O:vars", &v))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001765 return NULL;
Guido van Rossum2d951851994-08-29 12:52:16 +00001766 if (v == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001767 d = PyEval_GetLocals();
Guido van Rossum53bb7ff1995-07-26 16:26:31 +00001768 if (d == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001769 if (!PyErr_Occurred())
1770 PyErr_SetString(PyExc_SystemError,
1771 "no locals!?");
Guido van Rossum53bb7ff1995-07-26 16:26:31 +00001772 }
1773 else
Guido van Rossum79f25d91997-04-29 20:08:16 +00001774 Py_INCREF(d);
Guido van Rossum2d951851994-08-29 12:52:16 +00001775 }
1776 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001777 d = PyObject_GetAttrString(v, "__dict__");
Guido van Rossum2d951851994-08-29 12:52:16 +00001778 if (d == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001779 PyErr_SetString(PyExc_TypeError,
Guido van Rossum2d951851994-08-29 12:52:16 +00001780 "vars() argument must have __dict__ attribute");
1781 return NULL;
1782 }
1783 }
1784 return d;
1785}
1786
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001787static char vars_doc[] =
1788"vars([object]) -> dictionary\n\
1789\n\
1790Without arguments, equivalent to locals().\n\
1791With an argument, equivalent to object.__dict__.";
1792
Barry Warsawcde8b1b1997-08-22 21:14:38 +00001793static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001794builtin_isinstance(PyObject *self, PyObject *args)
Barry Warsawcde8b1b1997-08-22 21:14:38 +00001795{
1796 PyObject *inst;
1797 PyObject *cls;
Guido van Rossum823649d2001-03-21 18:40:58 +00001798 int retval;
Barry Warsawcde8b1b1997-08-22 21:14:38 +00001799
Guido van Rossum43713e52000-02-29 13:59:29 +00001800 if (!PyArg_ParseTuple(args, "OO:isinstance", &inst, &cls))
Barry Warsawcde8b1b1997-08-22 21:14:38 +00001801 return NULL;
Guido van Rossumf5dd9141997-12-02 19:11:45 +00001802
Guido van Rossum823649d2001-03-21 18:40:58 +00001803 retval = PyObject_IsInstance(inst, cls);
1804 if (retval < 0)
Guido van Rossumad991772001-01-12 16:03:05 +00001805 return NULL;
Barry Warsawcde8b1b1997-08-22 21:14:38 +00001806 return PyInt_FromLong(retval);
1807}
1808
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001809static char isinstance_doc[] =
1810"isinstance(object, class-or-type) -> Boolean\n\
1811\n\
1812Return whether an object is an instance of a class or of a subclass thereof.\n\
1813With a type as second argument, return whether that is the object's type.";
1814
Barry Warsawcde8b1b1997-08-22 21:14:38 +00001815
1816static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001817builtin_issubclass(PyObject *self, PyObject *args)
Barry Warsawcde8b1b1997-08-22 21:14:38 +00001818{
1819 PyObject *derived;
1820 PyObject *cls;
1821 int retval;
1822
Guido van Rossum43713e52000-02-29 13:59:29 +00001823 if (!PyArg_ParseTuple(args, "OO:issubclass", &derived, &cls))
Barry Warsawcde8b1b1997-08-22 21:14:38 +00001824 return NULL;
Guido van Rossum668213d1999-06-16 17:28:37 +00001825
Guido van Rossum823649d2001-03-21 18:40:58 +00001826 retval = PyObject_IsSubclass(derived, cls);
1827 if (retval < 0)
1828 return NULL;
Barry Warsawcde8b1b1997-08-22 21:14:38 +00001829 return PyInt_FromLong(retval);
1830}
1831
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001832static char issubclass_doc[] =
1833"issubclass(C, B) -> Boolean\n\
1834\n\
1835Return whether class C is a subclass (i.e., a derived class) of class B.";
1836
Barry Warsawcde8b1b1997-08-22 21:14:38 +00001837
Barry Warsawbd599b52000-08-03 15:45:29 +00001838static PyObject*
1839builtin_zip(PyObject *self, PyObject *args)
1840{
1841 PyObject *ret;
1842 int itemsize = PySequence_Length(args);
Tim Peters8572b4f2001-05-06 01:05:02 +00001843 int i;
1844 PyObject *itlist; /* tuple of iterators */
Barry Warsawbd599b52000-08-03 15:45:29 +00001845
1846 if (itemsize < 1) {
1847 PyErr_SetString(PyExc_TypeError,
Fred Drake661ea262000-10-24 19:57:45 +00001848 "zip() requires at least one sequence");
Barry Warsawbd599b52000-08-03 15:45:29 +00001849 return NULL;
1850 }
1851 /* args must be a tuple */
1852 assert(PyTuple_Check(args));
1853
Tim Peters8572b4f2001-05-06 01:05:02 +00001854 /* allocate result list */
Barry Warsawbd599b52000-08-03 15:45:29 +00001855 if ((ret = PyList_New(0)) == NULL)
1856 return NULL;
1857
Tim Peters8572b4f2001-05-06 01:05:02 +00001858 /* obtain iterators */
1859 itlist = PyTuple_New(itemsize);
1860 if (itlist == NULL)
1861 goto Fail_ret;
1862 for (i = 0; i < itemsize; ++i) {
1863 PyObject *item = PyTuple_GET_ITEM(args, i);
1864 PyObject *it = PyObject_GetIter(item);
1865 if (it == NULL) {
1866 if (PyErr_ExceptionMatches(PyExc_TypeError))
1867 PyErr_Format(PyExc_TypeError,
1868 "zip argument #%d must support iteration",
1869 i+1);
1870 goto Fail_ret_itlist;
Barry Warsawbd599b52000-08-03 15:45:29 +00001871 }
Tim Peters8572b4f2001-05-06 01:05:02 +00001872 PyTuple_SET_ITEM(itlist, i, it);
1873 }
Barry Warsawbd599b52000-08-03 15:45:29 +00001874
Tim Peters8572b4f2001-05-06 01:05:02 +00001875 /* build result into ret list */
1876 for (;;) {
1877 int status;
1878 PyObject *next = PyTuple_New(itemsize);
1879 if (!next)
1880 goto Fail_ret_itlist;
1881
1882 for (i = 0; i < itemsize; i++) {
1883 PyObject *it = PyTuple_GET_ITEM(itlist, i);
1884 PyObject *item = PyIter_Next(it);
Barry Warsawbd599b52000-08-03 15:45:29 +00001885 if (!item) {
Tim Peters8572b4f2001-05-06 01:05:02 +00001886 if (PyErr_Occurred()) {
1887 Py_DECREF(ret);
1888 ret = NULL;
Barry Warsawbd599b52000-08-03 15:45:29 +00001889 }
1890 Py_DECREF(next);
Tim Peters8572b4f2001-05-06 01:05:02 +00001891 Py_DECREF(itlist);
1892 return ret;
Barry Warsawbd599b52000-08-03 15:45:29 +00001893 }
Tim Peters8572b4f2001-05-06 01:05:02 +00001894 PyTuple_SET_ITEM(next, i, item);
Barry Warsawbd599b52000-08-03 15:45:29 +00001895 }
Tim Peters8572b4f2001-05-06 01:05:02 +00001896
1897 status = PyList_Append(ret, next);
Barry Warsawbd599b52000-08-03 15:45:29 +00001898 Py_DECREF(next);
Tim Peters8572b4f2001-05-06 01:05:02 +00001899 if (status < 0)
1900 goto Fail_ret_itlist;
Barry Warsawbd599b52000-08-03 15:45:29 +00001901 }
Tim Peters8572b4f2001-05-06 01:05:02 +00001902
1903Fail_ret_itlist:
1904 Py_DECREF(itlist);
1905Fail_ret:
1906 Py_DECREF(ret);
1907 return NULL;
Barry Warsawbd599b52000-08-03 15:45:29 +00001908}
1909
1910
1911static char zip_doc[] =
1912"zip(seq1 [, seq2 [...]]) -> [(seq1[0], seq2[0] ...), (...)]\n\
1913\n\
1914Return a list of tuples, where each tuple contains the i-th element\n\
1915from each of the argument sequences. The returned list is truncated\n\
1916in length to the length of the shortest argument sequence.";
1917
1918
Guido van Rossum79f25d91997-04-29 20:08:16 +00001919static PyMethodDef builtin_methods[] = {
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001920 {"__import__", builtin___import__, METH_VARARGS, import_doc},
1921 {"abs", builtin_abs, METH_O, abs_doc},
1922 {"apply", builtin_apply, METH_VARARGS, apply_doc},
1923 {"buffer", builtin_buffer, METH_VARARGS, buffer_doc},
1924 {"callable", builtin_callable, METH_O, callable_doc},
1925 {"chr", builtin_chr, METH_VARARGS, chr_doc},
1926 {"cmp", builtin_cmp, METH_VARARGS, cmp_doc},
1927 {"coerce", builtin_coerce, METH_VARARGS, coerce_doc},
1928 {"compile", builtin_compile, METH_VARARGS, compile_doc},
1929 {"delattr", builtin_delattr, METH_VARARGS, delattr_doc},
1930 {"dir", builtin_dir, METH_VARARGS, dir_doc},
1931 {"divmod", builtin_divmod, METH_VARARGS, divmod_doc},
1932 {"eval", builtin_eval, METH_VARARGS, eval_doc},
1933 {"execfile", builtin_execfile, METH_VARARGS, execfile_doc},
1934 {"filter", builtin_filter, METH_VARARGS, filter_doc},
1935 {"getattr", builtin_getattr, METH_VARARGS, getattr_doc},
1936 {"globals", (PyCFunction)builtin_globals, METH_NOARGS, globals_doc},
1937 {"hasattr", builtin_hasattr, METH_VARARGS, hasattr_doc},
1938 {"hash", builtin_hash, METH_O, hash_doc},
1939 {"hex", builtin_hex, METH_O, hex_doc},
1940 {"id", builtin_id, METH_O, id_doc},
1941 {"input", builtin_input, METH_VARARGS, input_doc},
1942 {"intern", builtin_intern, METH_VARARGS, intern_doc},
1943 {"isinstance", builtin_isinstance, METH_VARARGS, isinstance_doc},
1944 {"issubclass", builtin_issubclass, METH_VARARGS, issubclass_doc},
1945 {"iter", builtin_iter, METH_VARARGS, iter_doc},
1946 {"len", builtin_len, METH_O, len_doc},
1947 {"locals", (PyCFunction)builtin_locals, METH_NOARGS, locals_doc},
1948 {"map", builtin_map, METH_VARARGS, map_doc},
1949 {"max", builtin_max, METH_VARARGS, max_doc},
1950 {"min", builtin_min, METH_VARARGS, min_doc},
1951 {"oct", builtin_oct, METH_O, oct_doc},
1952 {"open", builtin_open, METH_VARARGS, open_doc},
1953 {"ord", builtin_ord, METH_O, ord_doc},
1954 {"pow", builtin_pow, METH_VARARGS, pow_doc},
1955 {"range", builtin_range, METH_VARARGS, range_doc},
1956 {"raw_input", builtin_raw_input, METH_VARARGS, raw_input_doc},
1957 {"reduce", builtin_reduce, METH_VARARGS, reduce_doc},
1958 {"reload", builtin_reload, METH_O, reload_doc},
1959 {"repr", builtin_repr, METH_O, repr_doc},
1960 {"round", builtin_round, METH_VARARGS, round_doc},
1961 {"setattr", builtin_setattr, METH_VARARGS, setattr_doc},
1962 {"slice", builtin_slice, METH_VARARGS, slice_doc},
Martin v. Löwis339d0f72001-08-17 18:39:25 +00001963#ifdef Py_USING_UNICODE
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001964 {"unichr", builtin_unichr, METH_VARARGS, unichr_doc},
Martin v. Löwis339d0f72001-08-17 18:39:25 +00001965#endif
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001966 {"vars", builtin_vars, METH_VARARGS, vars_doc},
1967 {"xrange", builtin_xrange, METH_VARARGS, xrange_doc},
1968 {"zip", builtin_zip, METH_VARARGS, zip_doc},
Guido van Rossumc02e15c1991-12-16 13:03:00 +00001969 {NULL, NULL},
Guido van Rossum3f5da241990-12-20 15:06:42 +00001970};
1971
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001972static char builtin_doc[] =
1973"Built-in functions, exceptions, and other objects.\n\
1974\n\
1975Noteworthy: None is the `nil' object; Ellipsis represents `...' in slices.";
1976
Guido van Rossum25ce5661997-08-02 03:10:38 +00001977PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001978_PyBuiltin_Init(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +00001979{
Fred Drake5550de32000-06-20 04:54:19 +00001980 PyObject *mod, *dict, *debug;
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001981 mod = Py_InitModule4("__builtin__", builtin_methods,
1982 builtin_doc, (PyObject *)NULL,
1983 PYTHON_API_VERSION);
Guido van Rossum25ce5661997-08-02 03:10:38 +00001984 if (mod == NULL)
1985 return NULL;
1986 dict = PyModule_GetDict(mod);
Guido van Rossum25ce5661997-08-02 03:10:38 +00001987 if (PyDict_SetItemString(dict, "None", Py_None) < 0)
1988 return NULL;
1989 if (PyDict_SetItemString(dict, "Ellipsis", Py_Ellipsis) < 0)
1990 return NULL;
Guido van Rossumad991772001-01-12 16:03:05 +00001991 if (PyDict_SetItemString(dict, "NotImplemented",
Neil Schemenauer23ab1992001-01-04 01:48:42 +00001992 Py_NotImplemented) < 0)
1993 return NULL;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001994 if (PyDict_SetItemString(dict, "classmethod",
1995 (PyObject *) &PyClassMethod_Type) < 0)
1996 return NULL;
1997#ifndef WITHOUT_COMPLEX
1998 if (PyDict_SetItemString(dict, "complex",
1999 (PyObject *) &PyComplex_Type) < 0)
2000 return NULL;
2001#endif
2002 if (PyDict_SetItemString(dict, "dictionary",
2003 (PyObject *) &PyDict_Type) < 0)
2004 return NULL;
2005 if (PyDict_SetItemString(dict, "float",
2006 (PyObject *) &PyFloat_Type) < 0)
2007 return NULL;
Guido van Rossum29a62dd2001-08-23 21:40:38 +00002008 if (PyDict_SetItemString(dict, "getset",
2009 (PyObject *) &PyGetSet_Type) < 0)
2010 return NULL;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002011 if (PyDict_SetItemString(dict, "int", (PyObject *) &PyInt_Type) < 0)
2012 return NULL;
2013 if (PyDict_SetItemString(dict, "list", (PyObject *) &PyList_Type) < 0)
2014 return NULL;
2015 if (PyDict_SetItemString(dict, "long", (PyObject *) &PyLong_Type) < 0)
2016 return NULL;
2017 if (PyDict_SetItemString(dict, "object",
2018 (PyObject *) &PyBaseObject_Type) < 0)
2019 return NULL;
2020 if (PyDict_SetItemString(dict, "staticmethod",
2021 (PyObject *) &PyStaticMethod_Type) < 0)
2022 return NULL;
2023 if (PyDict_SetItemString(dict, "str", (PyObject *) &PyString_Type) < 0)
2024 return NULL;
Guido van Rossumf5cb3572001-08-24 16:52:18 +00002025 if (PyDict_SetItemString(dict, "super",
2026 (PyObject *) &PySuper_Type) < 0)
2027 return NULL;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002028 if (PyDict_SetItemString(dict, "tuple",
2029 (PyObject *) &PyTuple_Type) < 0)
2030 return NULL;
2031 if (PyDict_SetItemString(dict, "type", (PyObject *) &PyType_Type) < 0)
2032 return NULL;
Martin v. Löwis339d0f72001-08-17 18:39:25 +00002033#ifdef Py_USING_UNICODE
Tim Peters6d6c1a32001-08-02 04:15:00 +00002034 if (PyDict_SetItemString(dict, "unicode",
2035 (PyObject *) &PyUnicode_Type) < 0)
2036 return NULL;
Martin v. Löwis339d0f72001-08-17 18:39:25 +00002037#endif
Fred Drake5550de32000-06-20 04:54:19 +00002038 debug = PyInt_FromLong(Py_OptimizeFlag == 0);
2039 if (PyDict_SetItemString(dict, "__debug__", debug) < 0) {
2040 Py_XDECREF(debug);
Guido van Rossum25ce5661997-08-02 03:10:38 +00002041 return NULL;
Fred Drake5550de32000-06-20 04:54:19 +00002042 }
2043 Py_XDECREF(debug);
Barry Warsaw757af0e1997-08-29 22:13:51 +00002044
Guido van Rossum25ce5661997-08-02 03:10:38 +00002045 return mod;
Guido van Rossum3f5da241990-12-20 15:06:42 +00002046}
2047
Guido van Rossume77a7571993-11-03 15:01:26 +00002048/* Helper for filter(): filter a tuple through a function */
Guido van Rossum12d12c51993-10-26 17:58:25 +00002049
Guido van Rossum79f25d91997-04-29 20:08:16 +00002050static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002051filtertuple(PyObject *func, PyObject *tuple)
Guido van Rossum12d12c51993-10-26 17:58:25 +00002052{
Guido van Rossum79f25d91997-04-29 20:08:16 +00002053 PyObject *result;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002054 register int i, j;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002055 int len = PyTuple_Size(tuple);
Guido van Rossum12d12c51993-10-26 17:58:25 +00002056
Guido van Rossumb7b45621995-08-04 04:07:45 +00002057 if (len == 0) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002058 Py_INCREF(tuple);
Guido van Rossumb7b45621995-08-04 04:07:45 +00002059 return tuple;
2060 }
2061
Guido van Rossum79f25d91997-04-29 20:08:16 +00002062 if ((result = PyTuple_New(len)) == NULL)
Guido van Rossum2586bf01993-11-01 16:21:44 +00002063 return NULL;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002064
Guido van Rossum12d12c51993-10-26 17:58:25 +00002065 for (i = j = 0; i < len; ++i) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002066 PyObject *item, *good;
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002067 int ok;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002068
Guido van Rossum79f25d91997-04-29 20:08:16 +00002069 if ((item = PyTuple_GetItem(tuple, i)) == NULL)
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002070 goto Fail_1;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002071 if (func == Py_None) {
2072 Py_INCREF(item);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002073 good = item;
2074 }
2075 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002076 PyObject *arg = Py_BuildValue("(O)", item);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002077 if (arg == NULL)
2078 goto Fail_1;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002079 good = PyEval_CallObject(func, arg);
2080 Py_DECREF(arg);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002081 if (good == NULL)
Guido van Rossum12d12c51993-10-26 17:58:25 +00002082 goto Fail_1;
2083 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002084 ok = PyObject_IsTrue(good);
2085 Py_DECREF(good);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002086 if (ok) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002087 Py_INCREF(item);
2088 if (PyTuple_SetItem(result, j++, item) < 0)
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002089 goto Fail_1;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002090 }
Guido van Rossum12d12c51993-10-26 17:58:25 +00002091 }
2092
Tim Peters4324aa32001-05-28 22:30:08 +00002093 if (_PyTuple_Resize(&result, j) < 0)
Guido van Rossum12d12c51993-10-26 17:58:25 +00002094 return NULL;
2095
Guido van Rossum12d12c51993-10-26 17:58:25 +00002096 return result;
2097
Guido van Rossum12d12c51993-10-26 17:58:25 +00002098Fail_1:
Guido van Rossum79f25d91997-04-29 20:08:16 +00002099 Py_DECREF(result);
Guido van Rossum12d12c51993-10-26 17:58:25 +00002100 return NULL;
2101}
2102
2103
Guido van Rossume77a7571993-11-03 15:01:26 +00002104/* Helper for filter(): filter a string through a function */
Guido van Rossum12d12c51993-10-26 17:58:25 +00002105
Guido van Rossum79f25d91997-04-29 20:08:16 +00002106static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002107filterstring(PyObject *func, PyObject *strobj)
Guido van Rossum12d12c51993-10-26 17:58:25 +00002108{
Guido van Rossum79f25d91997-04-29 20:08:16 +00002109 PyObject *result;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002110 register int i, j;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002111 int len = PyString_Size(strobj);
Guido van Rossum12d12c51993-10-26 17:58:25 +00002112
Guido van Rossum79f25d91997-04-29 20:08:16 +00002113 if (func == Py_None) {
Guido van Rossum2586bf01993-11-01 16:21:44 +00002114 /* No character is ever false -- share input string */
Guido van Rossum79f25d91997-04-29 20:08:16 +00002115 Py_INCREF(strobj);
Guido van Rossum2d951851994-08-29 12:52:16 +00002116 return strobj;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002117 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002118 if ((result = PyString_FromStringAndSize(NULL, len)) == NULL)
Guido van Rossum2586bf01993-11-01 16:21:44 +00002119 return NULL;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002120
Guido van Rossum12d12c51993-10-26 17:58:25 +00002121 for (i = j = 0; i < len; ++i) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002122 PyObject *item, *arg, *good;
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002123 int ok;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002124
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002125 item = (*strobj->ob_type->tp_as_sequence->sq_item)(strobj, i);
2126 if (item == NULL)
2127 goto Fail_1;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002128 arg = Py_BuildValue("(O)", item);
Tim Peters388ed082001-04-07 20:34:48 +00002129 if (arg == NULL) {
2130 Py_DECREF(item);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002131 goto Fail_1;
Tim Peters388ed082001-04-07 20:34:48 +00002132 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002133 good = PyEval_CallObject(func, arg);
2134 Py_DECREF(arg);
Tim Peters388ed082001-04-07 20:34:48 +00002135 if (good == NULL) {
2136 Py_DECREF(item);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002137 goto Fail_1;
Tim Peters388ed082001-04-07 20:34:48 +00002138 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002139 ok = PyObject_IsTrue(good);
2140 Py_DECREF(good);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002141 if (ok)
Guido van Rossum79f25d91997-04-29 20:08:16 +00002142 PyString_AS_STRING((PyStringObject *)result)[j++] =
2143 PyString_AS_STRING((PyStringObject *)item)[0];
Tim Peters388ed082001-04-07 20:34:48 +00002144 Py_DECREF(item);
Guido van Rossum12d12c51993-10-26 17:58:25 +00002145 }
2146
Guido van Rossum79f25d91997-04-29 20:08:16 +00002147 if (j < len && _PyString_Resize(&result, j) < 0)
Guido van Rossum12d12c51993-10-26 17:58:25 +00002148 return NULL;
2149
Guido van Rossum12d12c51993-10-26 17:58:25 +00002150 return result;
2151
Guido van Rossum12d12c51993-10-26 17:58:25 +00002152Fail_1:
Guido van Rossum79f25d91997-04-29 20:08:16 +00002153 Py_DECREF(result);
Guido van Rossum12d12c51993-10-26 17:58:25 +00002154 return NULL;
2155}