blob: 5209607df154d1db00cf90c356ddf5b0442affd7 [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
Guido van Rossum12d12c51993-10-26 17:58:25 +000016/* Forward */
Tim Petersdbd9ba62000-07-09 03:09:57 +000017static PyObject *filterstring(PyObject *, PyObject *);
18static PyObject *filtertuple (PyObject *, PyObject *);
Guido van Rossum12d12c51993-10-26 17:58:25 +000019
Guido van Rossum79f25d91997-04-29 20:08:16 +000020static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +000021builtin___import__(PyObject *self, PyObject *args)
Guido van Rossum3f5da241990-12-20 15:06:42 +000022{
Guido van Rossum1ae940a1995-01-02 19:04:15 +000023 char *name;
Guido van Rossum79f25d91997-04-29 20:08:16 +000024 PyObject *globals = NULL;
25 PyObject *locals = NULL;
26 PyObject *fromlist = NULL;
Guido van Rossum1ae940a1995-01-02 19:04:15 +000027
Guido van Rossum79f25d91997-04-29 20:08:16 +000028 if (!PyArg_ParseTuple(args, "s|OOO:__import__",
Guido van Rossum24c13741995-02-14 09:42:43 +000029 &name, &globals, &locals, &fromlist))
Guido van Rossum1ae940a1995-01-02 19:04:15 +000030 return NULL;
Guido van Rossumaee0bad1997-09-05 07:33:22 +000031 return PyImport_ImportModuleEx(name, globals, locals, fromlist);
Guido van Rossum1ae940a1995-01-02 19:04:15 +000032}
33
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +000034static char import_doc[] =
35"__import__(name, globals, locals, fromlist) -> module\n\
36\n\
37Import a module. The globals are only used to determine the context;\n\
38they are not modified. The locals are currently unused. The fromlist\n\
39should be a list of names to emulate ``from name import ...'', or an\n\
40empty list to emulate ``import name''.\n\
41When importing a module from a package, note that __import__('A.B', ...)\n\
42returns package A when fromlist is empty, but its submodule B when\n\
43fromlist is not empty.";
44
Guido van Rossum1ae940a1995-01-02 19:04:15 +000045
Guido van Rossum79f25d91997-04-29 20:08:16 +000046static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +000047builtin_abs(PyObject *self, PyObject *args)
Guido van Rossum1ae940a1995-01-02 19:04:15 +000048{
Guido van Rossum79f25d91997-04-29 20:08:16 +000049 PyObject *v;
Guido van Rossum1ae940a1995-01-02 19:04:15 +000050
Guido van Rossum79f25d91997-04-29 20:08:16 +000051 if (!PyArg_ParseTuple(args, "O:abs", &v))
Guido van Rossum1ae940a1995-01-02 19:04:15 +000052 return NULL;
Guido van Rossum09df08a1998-05-22 00:51:39 +000053 return PyNumber_Absolute(v);
Guido van Rossum3f5da241990-12-20 15:06:42 +000054}
55
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +000056static char abs_doc[] =
57"abs(number) -> number\n\
58\n\
59Return the absolute value of the argument.";
60
61
Guido van Rossum79f25d91997-04-29 20:08:16 +000062static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +000063builtin_apply(PyObject *self, PyObject *args)
Guido van Rossumc02e15c1991-12-16 13:03:00 +000064{
Guido van Rossum79f25d91997-04-29 20:08:16 +000065 PyObject *func, *alist = NULL, *kwdict = NULL;
Barry Warsaw968f8cb1998-10-01 15:33:12 +000066 PyObject *t = NULL, *retval = NULL;
Guido van Rossum1ae940a1995-01-02 19:04:15 +000067
Guido van Rossum79f25d91997-04-29 20:08:16 +000068 if (!PyArg_ParseTuple(args, "O|OO:apply", &func, &alist, &kwdict))
Guido van Rossumc02e15c1991-12-16 13:03:00 +000069 return NULL;
Barry Warsaw968f8cb1998-10-01 15:33:12 +000070 if (alist != NULL) {
71 if (!PyTuple_Check(alist)) {
72 if (!PySequence_Check(alist)) {
Jeremy Hyltonc862cf42001-01-19 03:25:05 +000073 PyErr_Format(PyExc_TypeError,
74 "apply() arg 2 expect sequence, found %s",
75 alist->ob_type->tp_name);
Barry Warsaw968f8cb1998-10-01 15:33:12 +000076 return NULL;
77 }
78 t = PySequence_Tuple(alist);
79 if (t == NULL)
80 return NULL;
81 alist = t;
82 }
Guido van Rossum2d951851994-08-29 12:52:16 +000083 }
Guido van Rossum79f25d91997-04-29 20:08:16 +000084 if (kwdict != NULL && !PyDict_Check(kwdict)) {
Jeremy Hyltonc862cf42001-01-19 03:25:05 +000085 PyErr_Format(PyExc_TypeError,
86 "apply() arg 3 expected dictionary, found %s",
87 kwdict->ob_type->tp_name);
Barry Warsaw968f8cb1998-10-01 15:33:12 +000088 goto finally;
Guido van Rossum681d79a1995-07-18 14:51:37 +000089 }
Barry Warsaw968f8cb1998-10-01 15:33:12 +000090 retval = PyEval_CallObjectWithKeywords(func, alist, kwdict);
91 finally:
92 Py_XDECREF(t);
93 return retval;
Guido van Rossumc02e15c1991-12-16 13:03:00 +000094}
95
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +000096static char apply_doc[] =
Fred Drakef1fbc622001-01-12 17:05:05 +000097"apply(object[, args[, kwargs]]) -> value\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +000098\n\
Fred Drake7b912121999-12-23 14:16:55 +000099Call a callable object with positional arguments taken from the tuple args,\n\
100and keyword arguments taken from the optional dictionary kwargs.\n\
101Note that classes are callable, as are instances with a __call__() method.";
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000102
103
Guido van Rossum79f25d91997-04-29 20:08:16 +0000104static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000105builtin_buffer(PyObject *self, PyObject *args)
Guido van Rossum0daf0221999-03-19 19:07:19 +0000106{
107 PyObject *ob;
108 int offset = 0;
109 int size = Py_END_OF_BUFFER;
110
111 if ( !PyArg_ParseTuple(args, "O|ii:buffer", &ob, &offset, &size) )
112 return NULL;
113 return PyBuffer_FromObject(ob, offset, size);
114}
115
116static char buffer_doc[] =
Guido van Rossum09095f32000-03-10 23:00:52 +0000117"buffer(object [, offset[, size]]) -> object\n\
Guido van Rossum0daf0221999-03-19 19:07:19 +0000118\n\
Guido van Rossumad991772001-01-12 16:03:05 +0000119Create a new buffer object which references the given object.\n\
Guido van Rossum0daf0221999-03-19 19:07:19 +0000120The buffer will reference a slice of the target object from the\n\
121start of the object (or at the specified offset). The slice will\n\
122extend to the end of the target object (or with the specified size).";
123
124
125static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000126builtin_unicode(PyObject *self, PyObject *args)
Guido van Rossum09095f32000-03-10 23:00:52 +0000127{
Guido van Rossum3afba762000-04-11 15:38:23 +0000128 PyObject *v;
Guido van Rossum09095f32000-03-10 23:00:52 +0000129 char *encoding = NULL;
130 char *errors = NULL;
131
Guido van Rossum3afba762000-04-11 15:38:23 +0000132 if ( !PyArg_ParseTuple(args, "O|ss:unicode", &v, &encoding, &errors) )
Guido van Rossum09095f32000-03-10 23:00:52 +0000133 return NULL;
Marc-André Lemburg1b1bcc92000-07-07 13:48:25 +0000134 return PyUnicode_FromEncodedObject(v, encoding, errors);
Guido van Rossum09095f32000-03-10 23:00:52 +0000135}
136
137static char unicode_doc[] =
138"unicode(string [, encoding[, errors]]) -> object\n\
139\n\
Guido van Rossumad991772001-01-12 16:03:05 +0000140Create a new Unicode object from the given encoded string.\n\
Fred Drakec640b182000-05-09 19:55:16 +0000141encoding defaults to the current default string encoding and \n\
142errors, defining the error handling, to 'strict'.";
Guido van Rossum09095f32000-03-10 23:00:52 +0000143
144
145static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000146builtin_callable(PyObject *self, PyObject *args)
Guido van Rossum2d951851994-08-29 12:52:16 +0000147{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000148 PyObject *v;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000149
Guido van Rossum79f25d91997-04-29 20:08:16 +0000150 if (!PyArg_ParseTuple(args, "O:callable", &v))
Guido van Rossum2d951851994-08-29 12:52:16 +0000151 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000152 return PyInt_FromLong((long)PyCallable_Check(v));
Guido van Rossum2d951851994-08-29 12:52:16 +0000153}
154
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000155static char callable_doc[] =
156"callable(object) -> Boolean\n\
157\n\
158Return whether the object is callable (i.e., some kind of function).\n\
159Note that classes are callable, as are instances with a __call__() method.";
160
161
Guido van Rossum79f25d91997-04-29 20:08:16 +0000162static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000163builtin_filter(PyObject *self, PyObject *args)
Guido van Rossum12d12c51993-10-26 17:58:25 +0000164{
Tim Peters0e57abf2001-05-02 07:39:38 +0000165 PyObject *func, *seq, *result, *it;
166 int len; /* guess for result list size */
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000167 register int i, j;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000168
Guido van Rossum79f25d91997-04-29 20:08:16 +0000169 if (!PyArg_ParseTuple(args, "OO:filter", &func, &seq))
Guido van Rossum12d12c51993-10-26 17:58:25 +0000170 return NULL;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000171
Tim Peters0e57abf2001-05-02 07:39:38 +0000172 /* Strings and tuples return a result of the same type. */
173 if (PyString_Check(seq))
174 return filterstring(func, seq);
175 if (PyTuple_Check(seq))
176 return filtertuple(func, seq);
177
178 /* Get iterator. */
179 it = PyObject_GetIter(seq);
180 if (it == NULL)
181 return NULL;
182
183 /* Guess a result list size. */
184 len = -1; /* unknown */
185 if (PySequence_Check(seq) &&
186 seq->ob_type->tp_as_sequence->sq_length) {
187 len = PySequence_Size(seq);
188 if (len < 0)
189 PyErr_Clear();
Guido van Rossum12d12c51993-10-26 17:58:25 +0000190 }
Tim Peters0e57abf2001-05-02 07:39:38 +0000191 if (len < 0)
192 len = 8; /* arbitrary */
Guido van Rossum12d12c51993-10-26 17:58:25 +0000193
Tim Peters0e57abf2001-05-02 07:39:38 +0000194 /* Get a result list. */
Guido van Rossum79f25d91997-04-29 20:08:16 +0000195 if (PyList_Check(seq) && seq->ob_refcnt == 1) {
Tim Peters0e57abf2001-05-02 07:39:38 +0000196 /* Eww - can modify the list in-place. */
Guido van Rossum79f25d91997-04-29 20:08:16 +0000197 Py_INCREF(seq);
Guido van Rossum12d12c51993-10-26 17:58:25 +0000198 result = seq;
199 }
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000200 else {
Tim Peters0e57abf2001-05-02 07:39:38 +0000201 result = PyList_New(len);
202 if (result == NULL)
203 goto Fail_it;
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000204 }
Guido van Rossum12d12c51993-10-26 17:58:25 +0000205
Tim Peters0e57abf2001-05-02 07:39:38 +0000206 /* Build the result list. */
Guido van Rossum2d951851994-08-29 12:52:16 +0000207 for (i = j = 0; ; ++i) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000208 PyObject *item, *good;
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000209 int ok;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000210
Tim Peters0e57abf2001-05-02 07:39:38 +0000211 item = PyIter_Next(it);
212 if (item == NULL) {
213 /* We're out of here in any case, but if this is a
214 * StopIteration exception it's expected, but if
215 * any other kind of exception it's an error.
216 */
217 if (PyErr_Occurred()) {
218 if (PyErr_ExceptionMatches(PyExc_StopIteration))
219 PyErr_Clear();
220 else
221 goto Fail_result_it;
Guido van Rossum2d951851994-08-29 12:52:16 +0000222 }
Tim Peters0e57abf2001-05-02 07:39:38 +0000223 break;
Guido van Rossum2d951851994-08-29 12:52:16 +0000224 }
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000225
Guido van Rossum79f25d91997-04-29 20:08:16 +0000226 if (func == Py_None) {
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000227 good = item;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000228 Py_INCREF(good);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000229 }
230 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000231 PyObject *arg = Py_BuildValue("(O)", item);
Tim Peters0e57abf2001-05-02 07:39:38 +0000232 if (arg == NULL) {
233 Py_DECREF(item);
234 goto Fail_result_it;
235 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000236 good = PyEval_CallObject(func, arg);
237 Py_DECREF(arg);
Guido van Rossum58b68731995-01-10 17:40:55 +0000238 if (good == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000239 Py_DECREF(item);
Tim Peters0e57abf2001-05-02 07:39:38 +0000240 goto Fail_result_it;
Guido van Rossum58b68731995-01-10 17:40:55 +0000241 }
Guido van Rossum12d12c51993-10-26 17:58:25 +0000242 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000243 ok = PyObject_IsTrue(good);
244 Py_DECREF(good);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000245 if (ok) {
Tim Peters0e57abf2001-05-02 07:39:38 +0000246 if (j < len)
247 PyList_SET_ITEM(result, j, item);
Guido van Rossum2d951851994-08-29 12:52:16 +0000248 else {
Barry Warsawfa77e091999-01-28 18:49:12 +0000249 int status = PyList_Append(result, item);
Barry Warsawfa77e091999-01-28 18:49:12 +0000250 Py_DECREF(item);
251 if (status < 0)
Tim Peters0e57abf2001-05-02 07:39:38 +0000252 goto Fail_result_it;
Guido van Rossum2d951851994-08-29 12:52:16 +0000253 }
Tim Peters0e57abf2001-05-02 07:39:38 +0000254 ++j;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000255 }
Tim Peters0e57abf2001-05-02 07:39:38 +0000256 else
257 Py_DECREF(item);
Guido van Rossum12d12c51993-10-26 17:58:25 +0000258 }
259
Guido van Rossum12d12c51993-10-26 17:58:25 +0000260
Tim Peters0e57abf2001-05-02 07:39:38 +0000261 /* Cut back result list if len is too big. */
Guido van Rossum79f25d91997-04-29 20:08:16 +0000262 if (j < len && PyList_SetSlice(result, j, len, NULL) < 0)
Tim Peters0e57abf2001-05-02 07:39:38 +0000263 goto Fail_result_it;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000264
Guido van Rossum12d12c51993-10-26 17:58:25 +0000265 return result;
266
Tim Peters0e57abf2001-05-02 07:39:38 +0000267Fail_result_it:
Guido van Rossum79f25d91997-04-29 20:08:16 +0000268 Py_DECREF(result);
Tim Peters0e57abf2001-05-02 07:39:38 +0000269Fail_it:
270 Py_DECREF(it);
Guido van Rossum12d12c51993-10-26 17:58:25 +0000271 return NULL;
272}
273
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000274static char filter_doc[] =
275"filter(function, sequence) -> list\n\
276\n\
277Return a list containing those items of sequence for which function(item)\n\
278is true. If function is None, return a list of items that are true.";
279
280
Guido van Rossum79f25d91997-04-29 20:08:16 +0000281static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000282builtin_chr(PyObject *self, PyObject *args)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000283{
284 long x;
285 char s[1];
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000286
Guido van Rossum79f25d91997-04-29 20:08:16 +0000287 if (!PyArg_ParseTuple(args, "l:chr", &x))
Guido van Rossum3f5da241990-12-20 15:06:42 +0000288 return NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000289 if (x < 0 || x >= 256) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000290 PyErr_SetString(PyExc_ValueError,
291 "chr() arg not in range(256)");
Guido van Rossum3f5da241990-12-20 15:06:42 +0000292 return NULL;
293 }
Guido van Rossum6bf62da1997-04-11 20:37:35 +0000294 s[0] = (char)x;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000295 return PyString_FromStringAndSize(s, 1);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000296}
297
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000298static char chr_doc[] =
299"chr(i) -> character\n\
300\n\
301Return a string of one character with ordinal i; 0 <= i < 256.";
302
303
Guido van Rossum79f25d91997-04-29 20:08:16 +0000304static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000305builtin_unichr(PyObject *self, PyObject *args)
Guido van Rossum09095f32000-03-10 23:00:52 +0000306{
307 long x;
308 Py_UNICODE s[1];
309
310 if (!PyArg_ParseTuple(args, "l:unichr", &x))
311 return NULL;
312 if (x < 0 || x >= 65536) {
313 PyErr_SetString(PyExc_ValueError,
314 "unichr() arg not in range(65536)");
315 return NULL;
316 }
317 s[0] = (Py_UNICODE)x;
318 return PyUnicode_FromUnicode(s, 1);
319}
320
321static char unichr_doc[] =
Fred Drake078b24f2000-04-13 02:42:50 +0000322"unichr(i) -> Unicode character\n\
Guido van Rossum09095f32000-03-10 23:00:52 +0000323\n\
Fred Drake078b24f2000-04-13 02:42:50 +0000324Return a Unicode string of one character with ordinal i; 0 <= i < 65536.";
Guido van Rossum09095f32000-03-10 23:00:52 +0000325
326
327static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000328builtin_cmp(PyObject *self, PyObject *args)
Guido van Rossuma9e7dc11992-10-18 18:53:57 +0000329{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000330 PyObject *a, *b;
Guido van Rossum09df08a1998-05-22 00:51:39 +0000331 int c;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000332
Guido van Rossum79f25d91997-04-29 20:08:16 +0000333 if (!PyArg_ParseTuple(args, "OO:cmp", &a, &b))
Guido van Rossuma9e7dc11992-10-18 18:53:57 +0000334 return NULL;
Guido van Rossum09df08a1998-05-22 00:51:39 +0000335 if (PyObject_Cmp(a, b, &c) < 0)
Guido van Rossumc8b6df91997-05-23 00:06:51 +0000336 return NULL;
Guido van Rossum09df08a1998-05-22 00:51:39 +0000337 return PyInt_FromLong((long)c);
Guido van Rossuma9e7dc11992-10-18 18:53:57 +0000338}
339
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000340static char cmp_doc[] =
341"cmp(x, y) -> integer\n\
342\n\
343Return negative if x<y, zero if x==y, positive if x>y.";
344
345
Guido van Rossum79f25d91997-04-29 20:08:16 +0000346static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000347builtin_coerce(PyObject *self, PyObject *args)
Guido van Rossum6a00cd81995-01-07 12:39:01 +0000348{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000349 PyObject *v, *w;
350 PyObject *res;
Guido van Rossum5524a591995-01-10 15:26:20 +0000351
Guido van Rossum79f25d91997-04-29 20:08:16 +0000352 if (!PyArg_ParseTuple(args, "OO:coerce", &v, &w))
Guido van Rossum5524a591995-01-10 15:26:20 +0000353 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000354 if (PyNumber_Coerce(&v, &w) < 0)
Guido van Rossum04691fc1992-08-12 15:35:34 +0000355 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000356 res = Py_BuildValue("(OO)", v, w);
357 Py_DECREF(v);
358 Py_DECREF(w);
Guido van Rossum04691fc1992-08-12 15:35:34 +0000359 return res;
360}
361
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000362static char coerce_doc[] =
363"coerce(x, y) -> None or (x1, y1)\n\
364\n\
365When x and y can be coerced to values of the same type, return a tuple\n\
366containing the coerced values. When they can't be coerced, return None.";
367
368
Guido van Rossum79f25d91997-04-29 20:08:16 +0000369static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000370builtin_compile(PyObject *self, PyObject *args)
Guido van Rossum5b722181993-03-30 17:46:03 +0000371{
372 char *str;
373 char *filename;
374 char *startstr;
375 int start;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000376
Guido van Rossum79f25d91997-04-29 20:08:16 +0000377 if (!PyArg_ParseTuple(args, "sss:compile", &str, &filename, &startstr))
Guido van Rossum5b722181993-03-30 17:46:03 +0000378 return NULL;
379 if (strcmp(startstr, "exec") == 0)
Guido van Rossumb05a5c71997-05-07 17:46:13 +0000380 start = Py_file_input;
Guido van Rossum5b722181993-03-30 17:46:03 +0000381 else if (strcmp(startstr, "eval") == 0)
Guido van Rossumb05a5c71997-05-07 17:46:13 +0000382 start = Py_eval_input;
Guido van Rossum872537c1995-07-07 22:43:42 +0000383 else if (strcmp(startstr, "single") == 0)
Guido van Rossumb05a5c71997-05-07 17:46:13 +0000384 start = Py_single_input;
Guido van Rossum5b722181993-03-30 17:46:03 +0000385 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000386 PyErr_SetString(PyExc_ValueError,
Fred Drake661ea262000-10-24 19:57:45 +0000387 "compile() arg 3 must be 'exec' or 'eval' or 'single'");
Guido van Rossum5b722181993-03-30 17:46:03 +0000388 return NULL;
389 }
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000390 if (PyEval_GetNestedScopes()) {
391 PyCompilerFlags cf;
392 cf.cf_nested_scopes = 1;
393 return Py_CompileStringFlags(str, filename, start, &cf);
394 } else
395 return Py_CompileString(str, filename, start);
Guido van Rossum5b722181993-03-30 17:46:03 +0000396}
397
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000398static char compile_doc[] =
399"compile(source, filename, mode) -> code object\n\
400\n\
401Compile the source string (a Python module, statement or expression)\n\
402into a code object that can be executed by the exec statement or eval().\n\
403The filename will be used for run-time error messages.\n\
404The mode must be 'exec' to compile a module, 'single' to compile a\n\
405single (interactive) statement, or 'eval' to compile an expression.";
406
407
Guido van Rossum8a5c5d21996-01-12 01:09:56 +0000408#ifndef WITHOUT_COMPLEX
409
Guido van Rossum79f25d91997-04-29 20:08:16 +0000410static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000411complex_from_string(PyObject *v)
Guido van Rossum11950231999-03-25 21:16:07 +0000412{
Tim Petersdbd9ba62000-07-09 03:09:57 +0000413 extern double strtod(const char *, char **);
Guido van Rossum9e896b32000-04-05 20:11:21 +0000414 const char *s, *start;
415 char *end;
Guido van Rossum11950231999-03-25 21:16:07 +0000416 double x=0.0, y=0.0, z;
417 int got_re=0, got_im=0, done=0;
418 int digit_or_dot;
419 int sw_error=0;
420 int sign;
421 char buffer[256]; /* For errors */
Barry Warsaw5ca1ef92000-08-18 05:02:16 +0000422 char s_buffer[256];
Guido van Rossum9e896b32000-04-05 20:11:21 +0000423 int len;
Guido van Rossum11950231999-03-25 21:16:07 +0000424
Guido van Rossum9e896b32000-04-05 20:11:21 +0000425 if (PyString_Check(v)) {
426 s = PyString_AS_STRING(v);
427 len = PyString_GET_SIZE(v);
428 }
429 else if (PyUnicode_Check(v)) {
Guido van Rossum9e896b32000-04-05 20:11:21 +0000430 if (PyUnicode_GET_SIZE(v) >= sizeof(s_buffer)) {
431 PyErr_SetString(PyExc_ValueError,
432 "complex() literal too large to convert");
433 return NULL;
434 }
Guido van Rossumad991772001-01-12 16:03:05 +0000435 if (PyUnicode_EncodeDecimal(PyUnicode_AS_UNICODE(v),
Guido van Rossum9e896b32000-04-05 20:11:21 +0000436 PyUnicode_GET_SIZE(v),
Guido van Rossumad991772001-01-12 16:03:05 +0000437 s_buffer,
Guido van Rossum9e896b32000-04-05 20:11:21 +0000438 NULL))
439 return NULL;
440 s = s_buffer;
Trent Mick29b83812000-08-12 21:35:36 +0000441 len = (int)strlen(s);
Guido van Rossum9e896b32000-04-05 20:11:21 +0000442 }
443 else if (PyObject_AsCharBuffer(v, &s, &len)) {
444 PyErr_SetString(PyExc_TypeError,
Fred Drake661ea262000-10-24 19:57:45 +0000445 "complex() arg is not a string");
Guido van Rossum9e896b32000-04-05 20:11:21 +0000446 return NULL;
447 }
Guido van Rossum11950231999-03-25 21:16:07 +0000448
449 /* position on first nonblank */
Guido van Rossum9e896b32000-04-05 20:11:21 +0000450 start = s;
Guido van Rossum11950231999-03-25 21:16:07 +0000451 while (*s && isspace(Py_CHARMASK(*s)))
452 s++;
453 if (s[0] == '\0') {
454 PyErr_SetString(PyExc_ValueError,
Fred Drake661ea262000-10-24 19:57:45 +0000455 "complex() arg is an empty string");
Guido van Rossum11950231999-03-25 21:16:07 +0000456 return NULL;
457 }
458
459 z = -1.0;
460 sign = 1;
461 do {
Guido van Rossumad991772001-01-12 16:03:05 +0000462
Guido van Rossum11950231999-03-25 21:16:07 +0000463 switch (*s) {
464
465 case '\0':
Guido van Rossum9e896b32000-04-05 20:11:21 +0000466 if (s-start != len) {
Guido van Rossum11950231999-03-25 21:16:07 +0000467 PyErr_SetString(
468 PyExc_ValueError,
Fred Drake661ea262000-10-24 19:57:45 +0000469 "complex() arg contains a null byte");
Guido van Rossum11950231999-03-25 21:16:07 +0000470 return NULL;
471 }
472 if(!done) sw_error=1;
473 break;
Guido van Rossumad991772001-01-12 16:03:05 +0000474
Guido van Rossum11950231999-03-25 21:16:07 +0000475 case '-':
476 sign = -1;
477 /* Fallthrough */
478 case '+':
479 if (done) sw_error=1;
480 s++;
481 if ( *s=='\0'||*s=='+'||*s=='-' ||
482 isspace(Py_CHARMASK(*s)) ) sw_error=1;
483 break;
484
485 case 'J':
486 case 'j':
487 if (got_im || done) {
488 sw_error = 1;
489 break;
490 }
491 if (z<0.0) {
492 y=sign;
493 }
494 else{
495 y=sign*z;
496 }
497 got_im=1;
498 s++;
499 if (*s!='+' && *s!='-' )
500 done=1;
501 break;
502
503 default:
504 if (isspace(Py_CHARMASK(*s))) {
505 while (*s && isspace(Py_CHARMASK(*s)))
506 s++;
507 if (s[0] != '\0')
508 sw_error=1;
509 else
510 done = 1;
511 break;
512 }
513 digit_or_dot =
514 (*s=='.' || isdigit(Py_CHARMASK(*s)));
515 if (done||!digit_or_dot) {
516 sw_error=1;
517 break;
518 }
519 errno = 0;
520 PyFPE_START_PROTECT("strtod", return 0)
521 z = strtod(s, &end) ;
522 PyFPE_END_PROTECT(z)
523 if (errno != 0) {
524 sprintf(buffer,
525 "float() out of range: %.150s", s);
526 PyErr_SetString(
527 PyExc_ValueError,
528 buffer);
529 return NULL;
530 }
531 s=end;
532 if (*s=='J' || *s=='j') {
Guido van Rossumad991772001-01-12 16:03:05 +0000533
Guido van Rossum11950231999-03-25 21:16:07 +0000534 break;
535 }
536 if (got_re) {
537 sw_error=1;
538 break;
539 }
540
541 /* accept a real part */
542 x=sign*z;
543 got_re=1;
544 if (got_im) done=1;
545 z = -1.0;
546 sign = 1;
547 break;
Guido van Rossumad991772001-01-12 16:03:05 +0000548
Guido van Rossum11950231999-03-25 21:16:07 +0000549 } /* end of switch */
550
551 } while (*s!='\0' && !sw_error);
552
553 if (sw_error) {
554 PyErr_SetString(PyExc_ValueError,
Fred Drake661ea262000-10-24 19:57:45 +0000555 "complex() arg is a malformed string");
Guido van Rossum11950231999-03-25 21:16:07 +0000556 return NULL;
557 }
558
559 return PyComplex_FromDoubles(x,y);
560}
561
562static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000563builtin_complex(PyObject *self, PyObject *args)
Guido van Rossum8a5c5d21996-01-12 01:09:56 +0000564{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000565 PyObject *r, *i, *tmp;
566 PyNumberMethods *nbr, *nbi = NULL;
Guido van Rossum530956d1996-07-21 02:27:43 +0000567 Py_complex cr, ci;
Guido van Rossumc6472e91997-03-31 17:15:43 +0000568 int own_r = 0;
Guido van Rossum8a5c5d21996-01-12 01:09:56 +0000569
570 i = NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000571 if (!PyArg_ParseTuple(args, "O|O:complex", &r, &i))
Guido van Rossum8a5c5d21996-01-12 01:09:56 +0000572 return NULL;
Guido van Rossum9e896b32000-04-05 20:11:21 +0000573 if (PyString_Check(r) || PyUnicode_Check(r))
Guido van Rossum11950231999-03-25 21:16:07 +0000574 return complex_from_string(r);
Guido van Rossum8a5c5d21996-01-12 01:09:56 +0000575 if ((nbr = r->ob_type->tp_as_number) == NULL ||
Guido van Rossumc6472e91997-03-31 17:15:43 +0000576 nbr->nb_float == NULL ||
577 (i != NULL &&
578 ((nbi = i->ob_type->tp_as_number) == NULL ||
579 nbi->nb_float == NULL))) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000580 PyErr_SetString(PyExc_TypeError,
Fred Drake661ea262000-10-24 19:57:45 +0000581 "complex() arg can't be converted to complex");
Guido van Rossum8a5c5d21996-01-12 01:09:56 +0000582 return NULL;
583 }
Guido van Rossumed0af8f1996-12-05 23:18:18 +0000584 /* XXX Hack to support classes with __complex__ method */
Guido van Rossum79f25d91997-04-29 20:08:16 +0000585 if (PyInstance_Check(r)) {
586 static PyObject *complexstr;
587 PyObject *f;
Guido van Rossumed0af8f1996-12-05 23:18:18 +0000588 if (complexstr == NULL) {
Guido van Rossum8d751611997-01-18 08:04:16 +0000589 complexstr = PyString_InternFromString("__complex__");
Guido van Rossumed0af8f1996-12-05 23:18:18 +0000590 if (complexstr == NULL)
591 return NULL;
592 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000593 f = PyObject_GetAttr(r, complexstr);
Guido van Rossumed0af8f1996-12-05 23:18:18 +0000594 if (f == NULL)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000595 PyErr_Clear();
Guido van Rossumed0af8f1996-12-05 23:18:18 +0000596 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000597 PyObject *args = Py_BuildValue("()");
Guido van Rossumed0af8f1996-12-05 23:18:18 +0000598 if (args == NULL)
599 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000600 r = PyEval_CallObject(f, args);
601 Py_DECREF(args);
Barry Warsawf988e681999-01-27 23:13:59 +0000602 Py_DECREF(f);
Guido van Rossumed0af8f1996-12-05 23:18:18 +0000603 if (r == NULL)
604 return NULL;
Guido van Rossumc6472e91997-03-31 17:15:43 +0000605 own_r = 1;
Guido van Rossumed0af8f1996-12-05 23:18:18 +0000606 }
607 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000608 if (PyComplex_Check(r)) {
609 cr = ((PyComplexObject*)r)->cval;
Guido van Rossum730806d1998-04-10 22:27:42 +0000610 if (own_r) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000611 Py_DECREF(r);
Guido van Rossum730806d1998-04-10 22:27:42 +0000612 }
Guido van Rossumc6472e91997-03-31 17:15:43 +0000613 }
Guido van Rossum8a5c5d21996-01-12 01:09:56 +0000614 else {
Guido van Rossum8dabbf12001-01-19 02:11:59 +0000615 tmp = PyNumber_Float(r);
Guido van Rossum730806d1998-04-10 22:27:42 +0000616 if (own_r) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000617 Py_DECREF(r);
Guido van Rossum730806d1998-04-10 22:27:42 +0000618 }
Guido van Rossumfe4b6ee1996-08-08 18:49:41 +0000619 if (tmp == NULL)
620 return NULL;
Guido van Rossum8dabbf12001-01-19 02:11:59 +0000621 if (!PyFloat_Check(tmp)) {
622 PyErr_SetString(PyExc_TypeError,
623 "float(r) didn't return a float");
624 Py_DECREF(tmp);
625 return NULL;
626 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000627 cr.real = PyFloat_AsDouble(tmp);
628 Py_DECREF(tmp);
Guido van Rossum11950231999-03-25 21:16:07 +0000629 cr.imag = 0.0;
Guido van Rossum8a5c5d21996-01-12 01:09:56 +0000630 }
631 if (i == NULL) {
Guido van Rossum11950231999-03-25 21:16:07 +0000632 ci.real = 0.0;
633 ci.imag = 0.0;
Guido van Rossum8a5c5d21996-01-12 01:09:56 +0000634 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000635 else if (PyComplex_Check(i))
636 ci = ((PyComplexObject*)i)->cval;
Guido van Rossum8a5c5d21996-01-12 01:09:56 +0000637 else {
Guido van Rossumc6472e91997-03-31 17:15:43 +0000638 tmp = (*nbi->nb_float)(i);
Guido van Rossumfe4b6ee1996-08-08 18:49:41 +0000639 if (tmp == NULL)
640 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000641 ci.real = PyFloat_AsDouble(tmp);
642 Py_DECREF(tmp);
Guido van Rossum8a5c5d21996-01-12 01:09:56 +0000643 ci.imag = 0.;
644 }
645 cr.real -= ci.imag;
646 cr.imag += ci.real;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000647 return PyComplex_FromCComplex(cr);
Guido van Rossum8a5c5d21996-01-12 01:09:56 +0000648}
649
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000650static char complex_doc[] =
651"complex(real[, imag]) -> complex number\n\
652\n\
653Create a complex number from a real part and an optional imaginary part.\n\
654This is equivalent to (real + imag*1j) where imag defaults to 0.";
655
656
Guido van Rossum8a5c5d21996-01-12 01:09:56 +0000657#endif
658
Guido van Rossum79f25d91997-04-29 20:08:16 +0000659static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000660builtin_dir(PyObject *self, PyObject *args)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000661{
Guido van Rossum666b17a1997-05-06 16:36:57 +0000662 static char *attrlist[] = {"__members__", "__methods__", NULL};
663 PyObject *v = NULL, *l = NULL, *m = NULL;
664 PyObject *d, *x;
665 int i;
666 char **s;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000667
Guido van Rossum79f25d91997-04-29 20:08:16 +0000668 if (!PyArg_ParseTuple(args, "|O:dir", &v))
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000669 return NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000670 if (v == NULL) {
Guido van Rossum666b17a1997-05-06 16:36:57 +0000671 x = PyEval_GetLocals();
672 if (x == NULL)
673 goto error;
674 l = PyMapping_Keys(x);
675 if (l == NULL)
676 goto error;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000677 }
678 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000679 d = PyObject_GetAttrString(v, "__dict__");
Guido van Rossum666b17a1997-05-06 16:36:57 +0000680 if (d == NULL)
Guido van Rossum795ba581996-05-23 22:49:07 +0000681 PyErr_Clear();
Guido van Rossum666b17a1997-05-06 16:36:57 +0000682 else {
683 l = PyMapping_Keys(d);
684 if (l == NULL)
685 PyErr_Clear();
686 Py_DECREF(d);
687 }
688 if (l == NULL) {
689 l = PyList_New(0);
690 if (l == NULL)
691 goto error;
692 }
693 for (s = attrlist; *s != NULL; s++) {
694 m = PyObject_GetAttrString(v, *s);
695 if (m == NULL) {
696 PyErr_Clear();
697 continue;
698 }
699 for (i = 0; ; i++) {
700 x = PySequence_GetItem(m, i);
701 if (x == NULL) {
702 PyErr_Clear();
703 break;
704 }
705 if (PyList_Append(l, x) != 0) {
706 Py_DECREF(x);
707 Py_DECREF(m);
708 goto error;
709 }
710 Py_DECREF(x);
711 }
712 Py_DECREF(m);
Guido van Rossum795ba581996-05-23 22:49:07 +0000713 }
Guido van Rossum006bcd41991-10-24 14:54:44 +0000714 }
Guido van Rossum666b17a1997-05-06 16:36:57 +0000715 if (PyList_Sort(l) != 0)
716 goto error;
717 return l;
718 error:
719 Py_XDECREF(l);
720 return NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000721}
722
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000723static char dir_doc[] =
724"dir([object]) -> list of strings\n\
725\n\
726Return an alphabetized list of names comprising (some of) the attributes\n\
727of the given object. Without an argument, the names in the current scope\n\
728are listed. With an instance argument, only the instance attributes are\n\
729returned. With a class argument, attributes of the base class are not\n\
730returned. For other types or arguments, this may list members or methods.";
731
732
Guido van Rossum79f25d91997-04-29 20:08:16 +0000733static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000734builtin_divmod(PyObject *self, PyObject *args)
Guido van Rossum6a00cd81995-01-07 12:39:01 +0000735{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000736 PyObject *v, *w;
Guido van Rossum6a00cd81995-01-07 12:39:01 +0000737
Guido van Rossum79f25d91997-04-29 20:08:16 +0000738 if (!PyArg_ParseTuple(args, "OO:divmod", &v, &w))
Guido van Rossum6a00cd81995-01-07 12:39:01 +0000739 return NULL;
Guido van Rossum09df08a1998-05-22 00:51:39 +0000740 return PyNumber_Divmod(v, w);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000741}
742
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000743static char divmod_doc[] =
744"divmod(x, y) -> (div, mod)\n\
745\n\
746Return the tuple ((x-x%y)/y, x%y). Invariant: div*y + mod == x.";
747
748
Guido van Rossum79f25d91997-04-29 20:08:16 +0000749static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000750builtin_eval(PyObject *self, PyObject *args)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000751{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000752 PyObject *cmd;
753 PyObject *globals = Py_None, *locals = Py_None;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000754 char *str;
Guido van Rossum590baa41993-11-30 13:40:46 +0000755
Guido van Rossum79f25d91997-04-29 20:08:16 +0000756 if (!PyArg_ParseTuple(args, "O|O!O!:eval",
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000757 &cmd,
Guido van Rossum79f25d91997-04-29 20:08:16 +0000758 &PyDict_Type, &globals,
759 &PyDict_Type, &locals))
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000760 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000761 if (globals == Py_None) {
762 globals = PyEval_GetGlobals();
763 if (locals == Py_None)
764 locals = PyEval_GetLocals();
Guido van Rossum6135a871995-01-09 17:53:26 +0000765 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000766 else if (locals == Py_None)
Guido van Rossum6135a871995-01-09 17:53:26 +0000767 locals = globals;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000768 if (PyDict_GetItemString(globals, "__builtins__") == NULL) {
769 if (PyDict_SetItemString(globals, "__builtins__",
770 PyEval_GetBuiltins()) != 0)
Guido van Rossum6135a871995-01-09 17:53:26 +0000771 return NULL;
772 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000773 if (PyCode_Check(cmd))
774 return PyEval_EvalCode((PyCodeObject *) cmd, globals, locals);
Marc-André Lemburgd1ba4432000-09-19 21:04:18 +0000775 if (!PyString_Check(cmd) &&
776 !PyUnicode_Check(cmd)) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000777 PyErr_SetString(PyExc_TypeError,
Fred Drake661ea262000-10-24 19:57:45 +0000778 "eval() arg 1 must be a string or code object");
Guido van Rossum94390a41992-08-14 15:14:30 +0000779 return NULL;
780 }
Marc-André Lemburgd1ba4432000-09-19 21:04:18 +0000781 if (PyString_AsStringAndSize(cmd, &str, NULL))
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000782 return NULL;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000783 while (*str == ' ' || *str == '\t')
784 str++;
Guido van Rossumb05a5c71997-05-07 17:46:13 +0000785 return PyRun_String(str, Py_eval_input, globals, locals);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000786}
787
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000788static char eval_doc[] =
789"eval(source[, globals[, locals]]) -> value\n\
790\n\
791Evaluate the source in the context of globals and locals.\n\
792The source may be a string representing a Python expression\n\
793or a code object as returned by compile().\n\
794The globals and locals are dictionaries, defaulting to the current\n\
795globals and locals. If only globals is given, locals defaults to it.";
796
797
Guido van Rossum79f25d91997-04-29 20:08:16 +0000798static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000799builtin_execfile(PyObject *self, PyObject *args)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000800{
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000801 char *filename;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000802 PyObject *globals = Py_None, *locals = Py_None;
803 PyObject *res;
Guido van Rossum0f61f8a1992-02-25 18:55:05 +0000804 FILE* fp;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000805
Guido van Rossum79f25d91997-04-29 20:08:16 +0000806 if (!PyArg_ParseTuple(args, "s|O!O!:execfile",
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000807 &filename,
Guido van Rossum79f25d91997-04-29 20:08:16 +0000808 &PyDict_Type, &globals,
809 &PyDict_Type, &locals))
Guido van Rossum0f61f8a1992-02-25 18:55:05 +0000810 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000811 if (globals == Py_None) {
812 globals = PyEval_GetGlobals();
813 if (locals == Py_None)
814 locals = PyEval_GetLocals();
Guido van Rossum6135a871995-01-09 17:53:26 +0000815 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000816 else if (locals == Py_None)
Guido van Rossum6135a871995-01-09 17:53:26 +0000817 locals = globals;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000818 if (PyDict_GetItemString(globals, "__builtins__") == NULL) {
819 if (PyDict_SetItemString(globals, "__builtins__",
820 PyEval_GetBuiltins()) != 0)
Guido van Rossum6135a871995-01-09 17:53:26 +0000821 return NULL;
822 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000823 Py_BEGIN_ALLOW_THREADS
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000824 fp = fopen(filename, "r");
Guido van Rossum79f25d91997-04-29 20:08:16 +0000825 Py_END_ALLOW_THREADS
Guido van Rossum0f61f8a1992-02-25 18:55:05 +0000826 if (fp == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000827 PyErr_SetFromErrno(PyExc_IOError);
Guido van Rossum0f61f8a1992-02-25 18:55:05 +0000828 return NULL;
829 }
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000830 if (PyEval_GetNestedScopes()) {
831 PyCompilerFlags cf;
832 cf.cf_nested_scopes = 1;
Tim Peters748b8bb2001-04-28 08:20:22 +0000833 res = PyRun_FileExFlags(fp, filename, Py_file_input, globals,
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000834 locals, 1, &cf);
Tim Peters748b8bb2001-04-28 08:20:22 +0000835 } else
836 res = PyRun_FileEx(fp, filename, Py_file_input, globals,
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000837 locals, 1);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000838 return res;
Guido van Rossum0f61f8a1992-02-25 18:55:05 +0000839}
840
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000841static char execfile_doc[] =
842"execfile(filename[, globals[, locals]])\n\
843\n\
844Read and execute a Python script from a file.\n\
845The globals and locals are dictionaries, defaulting to the current\n\
846globals and locals. If only globals is given, locals defaults to it.";
847
848
Guido van Rossum79f25d91997-04-29 20:08:16 +0000849static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000850builtin_getattr(PyObject *self, PyObject *args)
Guido van Rossum33894be1992-01-27 16:53:09 +0000851{
Guido van Rossum950ff291998-06-29 13:38:57 +0000852 PyObject *v, *result, *dflt = NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000853 PyObject *name;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000854
Marc-André Lemburg691270f2000-09-18 16:22:27 +0000855 if (!PyArg_ParseTuple(args, "OO|O:getattr", &v, &name, &dflt))
Guido van Rossum33894be1992-01-27 16:53:09 +0000856 return NULL;
Guido van Rossum950ff291998-06-29 13:38:57 +0000857 result = PyObject_GetAttr(v, name);
858 if (result == NULL && dflt != NULL) {
859 PyErr_Clear();
860 Py_INCREF(dflt);
861 result = dflt;
862 }
863 return result;
Guido van Rossum9bfef441993-03-29 10:43:31 +0000864}
865
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000866static char getattr_doc[] =
Guido van Rossum950ff291998-06-29 13:38:57 +0000867"getattr(object, name[, default]) -> value\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000868\n\
Guido van Rossum950ff291998-06-29 13:38:57 +0000869Get a named attribute from an object; getattr(x, 'y') is equivalent to x.y.\n\
870When a default argument is given, it is returned when the attribute doesn't\n\
871exist; without it, an exception is raised in that case.";
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000872
873
Guido van Rossum79f25d91997-04-29 20:08:16 +0000874static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000875builtin_globals(PyObject *self, PyObject *args)
Guido van Rossum872537c1995-07-07 22:43:42 +0000876{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000877 PyObject *d;
Guido van Rossum872537c1995-07-07 22:43:42 +0000878
Guido van Rossum43713e52000-02-29 13:59:29 +0000879 if (!PyArg_ParseTuple(args, ":globals"))
Guido van Rossum872537c1995-07-07 22:43:42 +0000880 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000881 d = PyEval_GetGlobals();
882 Py_INCREF(d);
Guido van Rossum872537c1995-07-07 22:43:42 +0000883 return d;
884}
885
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000886static char globals_doc[] =
887"globals() -> dictionary\n\
888\n\
889Return the dictionary containing the current scope's global variables.";
890
891
Guido van Rossum79f25d91997-04-29 20:08:16 +0000892static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000893builtin_hasattr(PyObject *self, PyObject *args)
Guido van Rossum9bfef441993-03-29 10:43:31 +0000894{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000895 PyObject *v;
896 PyObject *name;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000897
Marc-André Lemburg691270f2000-09-18 16:22:27 +0000898 if (!PyArg_ParseTuple(args, "OO:hasattr", &v, &name))
Guido van Rossum9bfef441993-03-29 10:43:31 +0000899 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000900 v = PyObject_GetAttr(v, name);
Guido van Rossum9bfef441993-03-29 10:43:31 +0000901 if (v == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000902 PyErr_Clear();
Guido van Rossum09df08a1998-05-22 00:51:39 +0000903 Py_INCREF(Py_False);
904 return Py_False;
Guido van Rossum9bfef441993-03-29 10:43:31 +0000905 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000906 Py_DECREF(v);
Guido van Rossum09df08a1998-05-22 00:51:39 +0000907 Py_INCREF(Py_True);
908 return Py_True;
Guido van Rossum33894be1992-01-27 16:53:09 +0000909}
910
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000911static char hasattr_doc[] =
912"hasattr(object, name) -> Boolean\n\
913\n\
914Return whether the object has an attribute with the given name.\n\
915(This is done by calling getattr(object, name) and catching exceptions.)";
916
917
Guido van Rossum79f25d91997-04-29 20:08:16 +0000918static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000919builtin_id(PyObject *self, PyObject *args)
Guido van Rossum5b722181993-03-30 17:46:03 +0000920{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000921 PyObject *v;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000922
Guido van Rossum79f25d91997-04-29 20:08:16 +0000923 if (!PyArg_ParseTuple(args, "O:id", &v))
Guido van Rossum5b722181993-03-30 17:46:03 +0000924 return NULL;
Guido van Rossum106f2da2000-06-28 21:12:25 +0000925 return PyLong_FromVoidPtr(v);
Guido van Rossum5b722181993-03-30 17:46:03 +0000926}
927
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000928static char id_doc[] =
929"id(object) -> integer\n\
930\n\
931Return the identity of an object. This is guaranteed to be unique among\n\
932simultaneously existing objects. (Hint: it's the object's memory address.)";
933
934
Guido van Rossum79f25d91997-04-29 20:08:16 +0000935static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000936builtin_map(PyObject *self, PyObject *args)
Guido van Rossum12d12c51993-10-26 17:58:25 +0000937{
938 typedef struct {
Tim Peters4e9afdc2001-05-03 23:54:49 +0000939 PyObject *it; /* the iterator object */
940 int saw_StopIteration; /* bool: did the iterator end? */
Guido van Rossum12d12c51993-10-26 17:58:25 +0000941 } sequence;
942
Guido van Rossum79f25d91997-04-29 20:08:16 +0000943 PyObject *func, *result;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000944 sequence *seqs = NULL, *sqp;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000945 int n, len;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000946 register int i, j;
947
Guido van Rossum79f25d91997-04-29 20:08:16 +0000948 n = PyTuple_Size(args);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000949 if (n < 2) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000950 PyErr_SetString(PyExc_TypeError,
951 "map() requires at least two args");
Guido van Rossum12d12c51993-10-26 17:58:25 +0000952 return NULL;
953 }
954
Guido van Rossum79f25d91997-04-29 20:08:16 +0000955 func = PyTuple_GetItem(args, 0);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000956 n--;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000957
Guido van Rossumfa4ac711998-07-10 17:37:30 +0000958 if (func == Py_None && n == 1) {
959 /* map(None, S) is the same as list(S). */
960 return PySequence_List(PyTuple_GetItem(args, 1));
961 }
962
Tim Peters4e9afdc2001-05-03 23:54:49 +0000963 /* Get space for sequence descriptors. Must NULL out the iterator
964 * pointers so that jumping to Fail_2 later doesn't see trash.
965 */
Guido van Rossum79f25d91997-04-29 20:08:16 +0000966 if ((seqs = PyMem_NEW(sequence, n)) == NULL) {
967 PyErr_NoMemory();
Tim Peters4e9afdc2001-05-03 23:54:49 +0000968 return NULL;
969 }
970 for (i = 0; i < n; ++i) {
971 seqs[i].it = (PyObject*)NULL;
972 seqs[i].saw_StopIteration = 0;
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000973 }
Guido van Rossum12d12c51993-10-26 17:58:25 +0000974
Tim Peters4e9afdc2001-05-03 23:54:49 +0000975 /* Do a first pass to obtain iterators for the arguments, and set len
976 * to the largest of their lengths.
Tim Peters748b8bb2001-04-28 08:20:22 +0000977 */
Tim Peters4e9afdc2001-05-03 23:54:49 +0000978 len = 0;
979 for (i = 0, sqp = seqs; i < n; ++i, ++sqp) {
980 PyObject *curseq;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000981 int curlen;
Guido van Rossumad991772001-01-12 16:03:05 +0000982
Tim Peters4e9afdc2001-05-03 23:54:49 +0000983 /* Get iterator. */
984 curseq = PyTuple_GetItem(args, i+1);
985 sqp->it = PyObject_GetIter(curseq);
986 if (sqp->it == NULL) {
Guido van Rossum12d12c51993-10-26 17:58:25 +0000987 static char errmsg[] =
Tim Peters4e9afdc2001-05-03 23:54:49 +0000988 "argument %d to map() must support iteration";
Guido van Rossum15e33a41997-04-30 19:00:27 +0000989 char errbuf[sizeof(errmsg) + 25];
Guido van Rossum12d12c51993-10-26 17:58:25 +0000990 sprintf(errbuf, errmsg, i+2);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000991 PyErr_SetString(PyExc_TypeError, errbuf);
Guido van Rossum12d12c51993-10-26 17:58:25 +0000992 goto Fail_2;
993 }
994
Tim Peters4e9afdc2001-05-03 23:54:49 +0000995 /* Update len. */
996 curlen = -1; /* unknown */
997 if (PySequence_Check(curseq) &&
998 curseq->ob_type->tp_as_sequence->sq_length) {
999 curlen = PySequence_Size(curseq);
1000 if (curlen < 0)
1001 PyErr_Clear();
1002 }
Tim Peters748b8bb2001-04-28 08:20:22 +00001003 if (curlen < 0)
Tim Peters4e9afdc2001-05-03 23:54:49 +00001004 curlen = 8; /* arbitrary */
Guido van Rossum12d12c51993-10-26 17:58:25 +00001005 if (curlen > len)
1006 len = curlen;
1007 }
1008
Tim Peters4e9afdc2001-05-03 23:54:49 +00001009 /* Get space for the result list. */
Guido van Rossum79f25d91997-04-29 20:08:16 +00001010 if ((result = (PyObject *) PyList_New(len)) == NULL)
Guido van Rossum12d12c51993-10-26 17:58:25 +00001011 goto Fail_2;
1012
Tim Peters4e9afdc2001-05-03 23:54:49 +00001013 /* Iterate over the sequences until all have stopped. */
Guido van Rossum2d951851994-08-29 12:52:16 +00001014 for (i = 0; ; ++i) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001015 PyObject *alist, *item=NULL, *value;
Tim Peters4e9afdc2001-05-03 23:54:49 +00001016 int numactive = 0;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001017
Guido van Rossum79f25d91997-04-29 20:08:16 +00001018 if (func == Py_None && n == 1)
Guido van Rossum32120311995-07-10 13:52:21 +00001019 alist = NULL;
Tim Peters4e9afdc2001-05-03 23:54:49 +00001020 else if ((alist = PyTuple_New(n)) == NULL)
1021 goto Fail_1;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001022
1023 for (j = 0, sqp = seqs; j < n; ++j, ++sqp) {
Tim Peters4e9afdc2001-05-03 23:54:49 +00001024 if (sqp->saw_StopIteration) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001025 Py_INCREF(Py_None);
1026 item = Py_None;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001027 }
Guido van Rossum12d12c51993-10-26 17:58:25 +00001028 else {
Tim Peters4e9afdc2001-05-03 23:54:49 +00001029 item = PyIter_Next(sqp->it);
1030 if (item)
1031 ++numactive;
1032 else {
1033 /* StopIteration is *implied* by a
1034 * NULL return from PyIter_Next() if
1035 * PyErr_Occurred() is false.
1036 */
1037 if (PyErr_Occurred()) {
1038 if (PyErr_ExceptionMatches(
1039 PyExc_StopIteration))
1040 PyErr_Clear();
1041 else {
1042 Py_XDECREF(alist);
1043 goto Fail_1;
1044 }
Guido van Rossum2d951851994-08-29 12:52:16 +00001045 }
Tim Peters4e9afdc2001-05-03 23:54:49 +00001046 Py_INCREF(Py_None);
1047 item = Py_None;
1048 sqp->saw_StopIteration = 1;
Guido van Rossum2d951851994-08-29 12:52:16 +00001049 }
Guido van Rossum12d12c51993-10-26 17:58:25 +00001050
Guido van Rossum12d12c51993-10-26 17:58:25 +00001051 }
Tim Peters4e9afdc2001-05-03 23:54:49 +00001052 if (alist)
1053 PyTuple_SET_ITEM(alist, j, item);
1054 else
Guido van Rossum2d951851994-08-29 12:52:16 +00001055 break;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001056 }
1057
Guido van Rossum32120311995-07-10 13:52:21 +00001058 if (!alist)
1059 alist = item;
Guido van Rossum2d951851994-08-29 12:52:16 +00001060
Tim Peters4e9afdc2001-05-03 23:54:49 +00001061 if (numactive == 0) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001062 Py_DECREF(alist);
Guido van Rossum2d951851994-08-29 12:52:16 +00001063 break;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001064 }
Guido van Rossum2d951851994-08-29 12:52:16 +00001065
Guido van Rossum79f25d91997-04-29 20:08:16 +00001066 if (func == Py_None)
Guido van Rossum32120311995-07-10 13:52:21 +00001067 value = alist;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001068 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001069 value = PyEval_CallObject(func, alist);
1070 Py_DECREF(alist);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00001071 if (value == NULL)
1072 goto Fail_1;
Guido van Rossum2d951851994-08-29 12:52:16 +00001073 }
1074 if (i >= len) {
Barry Warsawfa77e091999-01-28 18:49:12 +00001075 int status = PyList_Append(result, value);
Barry Warsaw21332871999-01-28 04:21:35 +00001076 Py_DECREF(value);
Barry Warsawfa77e091999-01-28 18:49:12 +00001077 if (status < 0)
1078 goto Fail_1;
Guido van Rossum2d951851994-08-29 12:52:16 +00001079 }
Tim Peters4e9afdc2001-05-03 23:54:49 +00001080 else if (PyList_SetItem(result, i, value) < 0)
1081 goto Fail_1;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001082 }
1083
Guido van Rossumfa4ac711998-07-10 17:37:30 +00001084 if (i < len && PyList_SetSlice(result, i, len, NULL) < 0)
1085 goto Fail_1;
1086
Tim Peters4e9afdc2001-05-03 23:54:49 +00001087 goto Succeed;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001088
Guido van Rossum12d12c51993-10-26 17:58:25 +00001089Fail_1:
Guido van Rossum79f25d91997-04-29 20:08:16 +00001090 Py_DECREF(result);
Guido van Rossum12d12c51993-10-26 17:58:25 +00001091Fail_2:
Tim Peters4e9afdc2001-05-03 23:54:49 +00001092 result = NULL;
1093Succeed:
1094 assert(seqs);
1095 for (i = 0; i < n; ++i)
1096 Py_XDECREF(seqs[i].it);
1097 PyMem_DEL(seqs);
1098 return result;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001099}
1100
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001101static char map_doc[] =
1102"map(function, sequence[, sequence, ...]) -> list\n\
1103\n\
1104Return a list of the results of applying the function to the items of\n\
1105the argument sequence(s). If more than one sequence is given, the\n\
1106function is called with an argument list consisting of the corresponding\n\
1107item of each sequence, substituting None for missing values when not all\n\
1108sequences have the same length. If the function is None, return a list of\n\
1109the items of the sequence (or a list of tuples if more than one sequence).";
1110
1111
Guido van Rossum79f25d91997-04-29 20:08:16 +00001112static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001113builtin_setattr(PyObject *self, PyObject *args)
Guido van Rossum33894be1992-01-27 16:53:09 +00001114{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001115 PyObject *v;
1116 PyObject *name;
1117 PyObject *value;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001118
Marc-André Lemburg691270f2000-09-18 16:22:27 +00001119 if (!PyArg_ParseTuple(args, "OOO:setattr", &v, &name, &value))
Guido van Rossum33894be1992-01-27 16:53:09 +00001120 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001121 if (PyObject_SetAttr(v, name, value) != 0)
Guido van Rossum33894be1992-01-27 16:53:09 +00001122 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001123 Py_INCREF(Py_None);
1124 return Py_None;
Guido van Rossum33894be1992-01-27 16:53:09 +00001125}
1126
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001127static char setattr_doc[] =
1128"setattr(object, name, value)\n\
1129\n\
1130Set a named attribute on an object; setattr(x, 'y', v) is equivalent to\n\
1131``x.y = v''.";
1132
1133
Guido van Rossum79f25d91997-04-29 20:08:16 +00001134static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001135builtin_delattr(PyObject *self, PyObject *args)
Guido van Rossum14144fc1994-08-29 12:53:40 +00001136{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001137 PyObject *v;
1138 PyObject *name;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001139
Marc-André Lemburg691270f2000-09-18 16:22:27 +00001140 if (!PyArg_ParseTuple(args, "OO:delattr", &v, &name))
Guido van Rossum14144fc1994-08-29 12:53:40 +00001141 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001142 if (PyObject_SetAttr(v, name, (PyObject *)NULL) != 0)
Guido van Rossum14144fc1994-08-29 12:53:40 +00001143 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001144 Py_INCREF(Py_None);
1145 return Py_None;
Guido van Rossum14144fc1994-08-29 12:53:40 +00001146}
1147
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001148static char delattr_doc[] =
Guido van Rossumdf12a591998-11-23 22:13:04 +00001149"delattr(object, name)\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001150\n\
1151Delete a named attribute on an object; delattr(x, 'y') is equivalent to\n\
1152``del x.y''.";
1153
1154
Guido van Rossum79f25d91997-04-29 20:08:16 +00001155static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001156builtin_hash(PyObject *self, PyObject *args)
Guido van Rossum9bfef441993-03-29 10:43:31 +00001157{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001158 PyObject *v;
Guido van Rossum9bfef441993-03-29 10:43:31 +00001159 long x;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001160
Guido van Rossum79f25d91997-04-29 20:08:16 +00001161 if (!PyArg_ParseTuple(args, "O:hash", &v))
Guido van Rossum9bfef441993-03-29 10:43:31 +00001162 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001163 x = PyObject_Hash(v);
Guido van Rossum9bfef441993-03-29 10:43:31 +00001164 if (x == -1)
1165 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001166 return PyInt_FromLong(x);
Guido van Rossum9bfef441993-03-29 10:43:31 +00001167}
1168
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001169static char hash_doc[] =
1170"hash(object) -> integer\n\
1171\n\
1172Return a hash value for the object. Two objects with the same value have\n\
1173the same hash value. The reverse is not necessarily true, but likely.";
1174
1175
Guido van Rossum79f25d91997-04-29 20:08:16 +00001176static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001177builtin_hex(PyObject *self, PyObject *args)
Guido van Rossum006bcd41991-10-24 14:54:44 +00001178{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001179 PyObject *v;
1180 PyNumberMethods *nb;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001181
Guido van Rossum79f25d91997-04-29 20:08:16 +00001182 if (!PyArg_ParseTuple(args, "O:hex", &v))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001183 return NULL;
Guido van Rossumad991772001-01-12 16:03:05 +00001184
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001185 if ((nb = v->ob_type->tp_as_number) == NULL ||
Guido van Rossum1899c2e1992-09-12 11:09:23 +00001186 nb->nb_hex == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001187 PyErr_SetString(PyExc_TypeError,
Guido van Rossum1899c2e1992-09-12 11:09:23 +00001188 "hex() argument can't be converted to hex");
1189 return NULL;
Guido van Rossum006bcd41991-10-24 14:54:44 +00001190 }
Guido van Rossum1899c2e1992-09-12 11:09:23 +00001191 return (*nb->nb_hex)(v);
Guido van Rossum006bcd41991-10-24 14:54:44 +00001192}
1193
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001194static char hex_doc[] =
1195"hex(number) -> string\n\
1196\n\
1197Return the hexadecimal representation of an integer or long integer.";
1198
1199
Tim Petersdbd9ba62000-07-09 03:09:57 +00001200static PyObject *builtin_raw_input(PyObject *, PyObject *);
Guido van Rossum3165fe61992-09-25 21:59:05 +00001201
Guido van Rossum79f25d91997-04-29 20:08:16 +00001202static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001203builtin_input(PyObject *self, PyObject *args)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001204{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001205 PyObject *line;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001206 char *str;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001207 PyObject *res;
1208 PyObject *globals, *locals;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001209
1210 line = builtin_raw_input(self, args);
Guido van Rossum3165fe61992-09-25 21:59:05 +00001211 if (line == NULL)
1212 return line;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001213 if (!PyArg_Parse(line, "s;embedded '\\0' in input line", &str))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001214 return NULL;
1215 while (*str == ' ' || *str == '\t')
1216 str++;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001217 globals = PyEval_GetGlobals();
1218 locals = PyEval_GetLocals();
1219 if (PyDict_GetItemString(globals, "__builtins__") == NULL) {
1220 if (PyDict_SetItemString(globals, "__builtins__",
1221 PyEval_GetBuiltins()) != 0)
Guido van Rossum6135a871995-01-09 17:53:26 +00001222 return NULL;
1223 }
Guido van Rossumb05a5c71997-05-07 17:46:13 +00001224 res = PyRun_String(str, Py_eval_input, globals, locals);
Guido van Rossum79f25d91997-04-29 20:08:16 +00001225 Py_DECREF(line);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001226 return res;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001227}
1228
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001229static char input_doc[] =
1230"input([prompt]) -> value\n\
1231\n\
1232Equivalent to eval(raw_input(prompt)).";
1233
1234
Guido van Rossume8811f81997-02-14 15:48:05 +00001235static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001236builtin_intern(PyObject *self, PyObject *args)
Guido van Rossume8811f81997-02-14 15:48:05 +00001237{
1238 PyObject *s;
Guido van Rossum43713e52000-02-29 13:59:29 +00001239 if (!PyArg_ParseTuple(args, "S:intern", &s))
Guido van Rossume8811f81997-02-14 15:48:05 +00001240 return NULL;
1241 Py_INCREF(s);
1242 PyString_InternInPlace(&s);
1243 return s;
1244}
1245
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001246static char intern_doc[] =
1247"intern(string) -> string\n\
1248\n\
1249``Intern'' the given string. This enters the string in the (global)\n\
1250table of interned strings whose purpose is to speed up dictionary lookups.\n\
1251Return the string itself or the previously interned string object with the\n\
1252same value.";
1253
1254
Guido van Rossum79f25d91997-04-29 20:08:16 +00001255static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001256builtin_int(PyObject *self, PyObject *args)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001257{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001258 PyObject *v;
Barry Warsaw226ae6c1999-10-12 19:54:53 +00001259 int base = -909; /* unlikely! */
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001260
Barry Warsaw226ae6c1999-10-12 19:54:53 +00001261 if (!PyArg_ParseTuple(args, "O|i:int", &v, &base))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001262 return NULL;
Barry Warsaw226ae6c1999-10-12 19:54:53 +00001263 if (base == -909)
1264 return PyNumber_Int(v);
Guido van Rossum9e896b32000-04-05 20:11:21 +00001265 else if (PyString_Check(v))
1266 return PyInt_FromString(PyString_AS_STRING(v), NULL, base);
1267 else if (PyUnicode_Check(v))
1268 return PyInt_FromUnicode(PyUnicode_AS_UNICODE(v),
1269 PyUnicode_GET_SIZE(v),
1270 base);
1271 else {
Barry Warsaw226ae6c1999-10-12 19:54:53 +00001272 PyErr_SetString(PyExc_TypeError,
Fred Drake661ea262000-10-24 19:57:45 +00001273 "int() can't convert non-string with explicit base");
Barry Warsaw226ae6c1999-10-12 19:54:53 +00001274 return NULL;
1275 }
Guido van Rossum3f5da241990-12-20 15:06:42 +00001276}
1277
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001278static char int_doc[] =
Barry Warsaw226ae6c1999-10-12 19:54:53 +00001279"int(x[, base]) -> integer\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001280\n\
Barry Warsaw226ae6c1999-10-12 19:54:53 +00001281Convert a string or number to an integer, if possible. A floating point\n\
1282argument will be truncated towards zero (this does not include a string\n\
1283representation of a floating point number!) When converting a string, use\n\
1284the optional base. It is an error to supply a base when converting a\n\
1285non-string.";
1286
1287
1288static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001289builtin_long(PyObject *self, PyObject *args)
Barry Warsaw226ae6c1999-10-12 19:54:53 +00001290{
1291 PyObject *v;
1292 int base = -909; /* unlikely! */
Guido van Rossumad991772001-01-12 16:03:05 +00001293
Barry Warsaw226ae6c1999-10-12 19:54:53 +00001294 if (!PyArg_ParseTuple(args, "O|i:long", &v, &base))
1295 return NULL;
1296 if (base == -909)
1297 return PyNumber_Long(v);
Guido van Rossum9e896b32000-04-05 20:11:21 +00001298 else if (PyString_Check(v))
1299 return PyLong_FromString(PyString_AS_STRING(v), NULL, base);
1300 else if (PyUnicode_Check(v))
1301 return PyLong_FromUnicode(PyUnicode_AS_UNICODE(v),
1302 PyUnicode_GET_SIZE(v),
1303 base);
1304 else {
Barry Warsaw226ae6c1999-10-12 19:54:53 +00001305 PyErr_SetString(PyExc_TypeError,
Fred Drake661ea262000-10-24 19:57:45 +00001306 "long() can't convert non-string with explicit base");
Barry Warsaw226ae6c1999-10-12 19:54:53 +00001307 return NULL;
1308 }
Barry Warsaw226ae6c1999-10-12 19:54:53 +00001309}
1310
1311static char long_doc[] =
1312"long(x) -> long integer\n\
1313long(x, base) -> long integer\n\
1314\n\
1315Convert a string or number to a long integer, if possible. A floating\n\
1316point argument will be truncated towards zero (this does not include a\n\
1317string representation of a floating point number!) When converting a\n\
1318string, use the given base. It is an error to supply a base when\n\
1319converting a non-string.";
1320
1321
1322static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001323builtin_float(PyObject *self, PyObject *args)
Barry Warsaw226ae6c1999-10-12 19:54:53 +00001324{
1325 PyObject *v;
1326
1327 if (!PyArg_ParseTuple(args, "O:float", &v))
1328 return NULL;
1329 if (PyString_Check(v))
1330 return PyFloat_FromString(v, NULL);
1331 return PyNumber_Float(v);
1332}
1333
1334static char float_doc[] =
1335"float(x) -> floating point number\n\
1336\n\
1337Convert a string or number to a floating point number, if possible.";
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001338
1339
Guido van Rossum79f25d91997-04-29 20:08:16 +00001340static PyObject *
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001341builtin_iter(PyObject *self, PyObject *args)
1342{
1343 PyObject *v, *w = NULL;
1344
1345 if (!PyArg_ParseTuple(args, "O|O:iter", &v, &w))
1346 return NULL;
1347 if (w == NULL)
1348 return PyObject_GetIter(v);
1349 if (!PyCallable_Check(v)) {
1350 PyErr_SetString(PyExc_TypeError,
1351 "iter(v, w): v must be callable");
1352 return NULL;
1353 }
1354 return PyCallIter_New(v, w);
1355}
1356
1357static char iter_doc[] =
1358"iter(collection) -> iterator\n\
1359iter(callable, sentinel) -> iterator\n\
1360\n\
1361Get an iterator from an object. In the first form, the argument must\n\
1362supply its own iterator, or be a sequence.\n\
1363In the second form, the callable is called until it returns the sentinel.";
1364
1365
1366static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001367builtin_len(PyObject *self, PyObject *args)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001368{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001369 PyObject *v;
Guido van Rossum8ea9f4d1998-06-29 22:26:50 +00001370 long res;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001371
Guido van Rossum79f25d91997-04-29 20:08:16 +00001372 if (!PyArg_ParseTuple(args, "O:len", &v))
Guido van Rossum3f5da241990-12-20 15:06:42 +00001373 return NULL;
Jeremy Hylton03657cf2000-07-12 13:05:33 +00001374 res = PyObject_Size(v);
Guido van Rossum8ea9f4d1998-06-29 22:26:50 +00001375 if (res < 0 && PyErr_Occurred())
1376 return NULL;
1377 return PyInt_FromLong(res);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001378}
1379
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001380static char len_doc[] =
1381"len(object) -> integer\n\
1382\n\
1383Return the number of items of a sequence or mapping.";
1384
1385
Guido van Rossum79f25d91997-04-29 20:08:16 +00001386static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001387builtin_list(PyObject *self, PyObject *args)
Guido van Rossumd1705771996-04-09 02:41:06 +00001388{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001389 PyObject *v;
Guido van Rossumd1705771996-04-09 02:41:06 +00001390
Guido van Rossum79f25d91997-04-29 20:08:16 +00001391 if (!PyArg_ParseTuple(args, "O:list", &v))
Guido van Rossumd1705771996-04-09 02:41:06 +00001392 return NULL;
Guido van Rossum09df08a1998-05-22 00:51:39 +00001393 return PySequence_List(v);
Guido van Rossumd1705771996-04-09 02:41:06 +00001394}
1395
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001396static char list_doc[] =
1397"list(sequence) -> list\n\
1398\n\
1399Return a new list whose items are the same as those of the argument sequence.";
1400
Guido van Rossum8861b741996-07-30 16:49:37 +00001401
1402static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001403builtin_slice(PyObject *self, PyObject *args)
Guido van Rossum8861b741996-07-30 16:49:37 +00001404{
Guido van Rossum09df08a1998-05-22 00:51:39 +00001405 PyObject *start, *stop, *step;
Guido van Rossum8861b741996-07-30 16:49:37 +00001406
Guido van Rossum09df08a1998-05-22 00:51:39 +00001407 start = stop = step = NULL;
Guido van Rossum8861b741996-07-30 16:49:37 +00001408
Guido van Rossum09df08a1998-05-22 00:51:39 +00001409 if (!PyArg_ParseTuple(args, "O|OO:slice", &start, &stop, &step))
1410 return NULL;
Guido van Rossum8861b741996-07-30 16:49:37 +00001411
Guido van Rossum09df08a1998-05-22 00:51:39 +00001412 /* This swapping of stop and start is to maintain similarity with
1413 range(). */
1414 if (stop == NULL) {
1415 stop = start;
1416 start = NULL;
1417 }
1418 return PySlice_New(start, stop, step);
Guido van Rossum8861b741996-07-30 16:49:37 +00001419}
1420
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001421static char slice_doc[] =
Fred Drake3d587441999-07-19 15:21:16 +00001422"slice([start,] stop[, step]) -> slice object\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001423\n\
1424Create a slice object. This is used for slicing by the Numeric extensions.";
1425
1426
Guido van Rossum79f25d91997-04-29 20:08:16 +00001427static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001428builtin_locals(PyObject *self, PyObject *args)
Guido van Rossum872537c1995-07-07 22:43:42 +00001429{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001430 PyObject *d;
Guido van Rossum872537c1995-07-07 22:43:42 +00001431
Guido van Rossum43713e52000-02-29 13:59:29 +00001432 if (!PyArg_ParseTuple(args, ":locals"))
Guido van Rossum872537c1995-07-07 22:43:42 +00001433 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001434 d = PyEval_GetLocals();
1435 Py_INCREF(d);
Guido van Rossum872537c1995-07-07 22:43:42 +00001436 return d;
1437}
1438
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001439static char locals_doc[] =
1440"locals() -> dictionary\n\
1441\n\
1442Return the dictionary containing the current scope's local variables.";
1443
1444
Guido van Rossum79f25d91997-04-29 20:08:16 +00001445static PyObject *
Guido van Rossum53451b32001-01-17 15:47:24 +00001446min_max(PyObject *args, int op)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001447{
Guido van Rossum2d951851994-08-29 12:52:16 +00001448 int i;
Tim Petersc3074532001-05-03 07:00:32 +00001449 PyObject *v, *w, *x, *it;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001450
Guido van Rossum79f25d91997-04-29 20:08:16 +00001451 if (PyTuple_Size(args) > 1)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001452 v = args;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001453 else if (!PyArg_ParseTuple(args, "O:min/max", &v))
Guido van Rossum3f5da241990-12-20 15:06:42 +00001454 return NULL;
Tim Petersc3074532001-05-03 07:00:32 +00001455
1456 it = PyObject_GetIter(v);
1457 if (it == NULL)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001458 return NULL;
Tim Petersc3074532001-05-03 07:00:32 +00001459
1460 w = NULL; /* the result */
Guido van Rossum2d951851994-08-29 12:52:16 +00001461 for (i = 0; ; i++) {
Tim Petersc3074532001-05-03 07:00:32 +00001462 x = PyIter_Next(it);
Guido van Rossum2d951851994-08-29 12:52:16 +00001463 if (x == NULL) {
Tim Petersc3074532001-05-03 07:00:32 +00001464 /* We're out of here in any case, but if this is a
1465 * StopIteration exception it's expected, but if
1466 * any other kind of exception it's an error.
1467 */
1468 if (PyErr_Occurred()) {
1469 if (PyErr_ExceptionMatches(PyExc_StopIteration))
1470 PyErr_Clear();
1471 else {
1472 Py_XDECREF(w);
1473 Py_DECREF(it);
1474 return NULL;
1475 }
Guido van Rossum2d951851994-08-29 12:52:16 +00001476 }
Tim Petersc3074532001-05-03 07:00:32 +00001477 break;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001478 }
Tim Petersc3074532001-05-03 07:00:32 +00001479
Guido van Rossum2d951851994-08-29 12:52:16 +00001480 if (w == NULL)
1481 w = x;
1482 else {
Guido van Rossum53451b32001-01-17 15:47:24 +00001483 int cmp = PyObject_RichCompareBool(x, w, op);
1484 if (cmp > 0) {
1485 Py_DECREF(w);
1486 w = x;
1487 }
1488 else if (cmp < 0) {
Guido van Rossumc8b6df91997-05-23 00:06:51 +00001489 Py_DECREF(x);
Tim Petersc3074532001-05-03 07:00:32 +00001490 Py_DECREF(w);
1491 Py_DECREF(it);
Guido van Rossumc8b6df91997-05-23 00:06:51 +00001492 return NULL;
1493 }
Guido van Rossum2d951851994-08-29 12:52:16 +00001494 else
Guido van Rossum79f25d91997-04-29 20:08:16 +00001495 Py_DECREF(x);
Guido van Rossum2d951851994-08-29 12:52:16 +00001496 }
Guido van Rossum3f5da241990-12-20 15:06:42 +00001497 }
Guido van Rossum2d951851994-08-29 12:52:16 +00001498 if (w == NULL)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001499 PyErr_SetString(PyExc_ValueError,
Fred Drake661ea262000-10-24 19:57:45 +00001500 "min() or max() arg is an empty sequence");
Tim Petersc3074532001-05-03 07:00:32 +00001501 Py_DECREF(it);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001502 return w;
1503}
1504
Guido van Rossum79f25d91997-04-29 20:08:16 +00001505static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001506builtin_min(PyObject *self, PyObject *v)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001507{
Guido van Rossum53451b32001-01-17 15:47:24 +00001508 return min_max(v, Py_LT);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001509}
1510
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001511static char min_doc[] =
1512"min(sequence) -> value\n\
1513min(a, b, c, ...) -> value\n\
1514\n\
1515With a single sequence argument, return its smallest item.\n\
1516With two or more arguments, return the smallest argument.";
1517
1518
Guido van Rossum79f25d91997-04-29 20:08:16 +00001519static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001520builtin_max(PyObject *self, PyObject *v)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001521{
Guido van Rossum53451b32001-01-17 15:47:24 +00001522 return min_max(v, Py_GT);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001523}
1524
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001525static char max_doc[] =
1526"max(sequence) -> value\n\
1527max(a, b, c, ...) -> value\n\
1528\n\
1529With a single sequence argument, return its largest item.\n\
1530With two or more arguments, return the largest argument.";
1531
1532
Guido van Rossum79f25d91997-04-29 20:08:16 +00001533static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001534builtin_oct(PyObject *self, PyObject *args)
Guido van Rossum006bcd41991-10-24 14:54:44 +00001535{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001536 PyObject *v;
1537 PyNumberMethods *nb;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001538
Guido van Rossum79f25d91997-04-29 20:08:16 +00001539 if (!PyArg_ParseTuple(args, "O:oct", &v))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001540 return NULL;
Guido van Rossum1899c2e1992-09-12 11:09:23 +00001541 if (v == NULL || (nb = v->ob_type->tp_as_number) == NULL ||
1542 nb->nb_oct == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001543 PyErr_SetString(PyExc_TypeError,
Guido van Rossum1899c2e1992-09-12 11:09:23 +00001544 "oct() argument can't be converted to oct");
1545 return NULL;
Guido van Rossum006bcd41991-10-24 14:54:44 +00001546 }
Guido van Rossum1899c2e1992-09-12 11:09:23 +00001547 return (*nb->nb_oct)(v);
Guido van Rossum006bcd41991-10-24 14:54:44 +00001548}
1549
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001550static char oct_doc[] =
1551"oct(number) -> string\n\
1552\n\
1553Return the octal representation of an integer or long integer.";
1554
1555
Guido van Rossum79f25d91997-04-29 20:08:16 +00001556static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001557builtin_open(PyObject *self, PyObject *args)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001558{
Guido van Rossum2d951851994-08-29 12:52:16 +00001559 char *name;
1560 char *mode = "r";
1561 int bufsize = -1;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001562 PyObject *f;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001563
Guido van Rossum79f25d91997-04-29 20:08:16 +00001564 if (!PyArg_ParseTuple(args, "s|si:open", &name, &mode, &bufsize))
Guido van Rossum3f5da241990-12-20 15:06:42 +00001565 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001566 f = PyFile_FromString(name, mode);
Guido van Rossum2d951851994-08-29 12:52:16 +00001567 if (f != NULL)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001568 PyFile_SetBufSize(f, bufsize);
Guido van Rossum2d951851994-08-29 12:52:16 +00001569 return f;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001570}
1571
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001572static char open_doc[] =
1573"open(filename[, mode[, buffering]]) -> file object\n\
1574\n\
1575Open a file. The mode can be 'r', 'w' or 'a' for reading (default),\n\
1576writing or appending. The file will be created if it doesn't exist\n\
1577when opened for writing or appending; it will be truncated when\n\
1578opened for writing. Add a 'b' to the mode for binary files.\n\
1579Add a '+' to the mode to allow simultaneous reading and writing.\n\
1580If the buffering argument is given, 0 means unbuffered, 1 means line\n\
1581buffered, and larger numbers specify the buffer size.";
1582
1583
Guido van Rossum79f25d91997-04-29 20:08:16 +00001584static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001585builtin_ord(PyObject *self, PyObject *args)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001586{
Guido van Rossum09095f32000-03-10 23:00:52 +00001587 PyObject *obj;
1588 long ord;
Jeremy Hylton394b54d2000-04-12 21:19:47 +00001589 int size;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001590
Guido van Rossum09095f32000-03-10 23:00:52 +00001591 if (!PyArg_ParseTuple(args, "O:ord", &obj))
Guido van Rossum3f5da241990-12-20 15:06:42 +00001592 return NULL;
Guido van Rossum09095f32000-03-10 23:00:52 +00001593
Jeremy Hylton394b54d2000-04-12 21:19:47 +00001594 if (PyString_Check(obj)) {
1595 size = PyString_GET_SIZE(obj);
Andrew M. Kuchling34c20cf2000-12-20 14:36:56 +00001596 if (size == 1) {
Jeremy Hylton394b54d2000-04-12 21:19:47 +00001597 ord = (long)((unsigned char)*PyString_AS_STRING(obj));
Andrew M. Kuchling34c20cf2000-12-20 14:36:56 +00001598 return PyInt_FromLong(ord);
1599 }
Jeremy Hylton394b54d2000-04-12 21:19:47 +00001600 } else if (PyUnicode_Check(obj)) {
1601 size = PyUnicode_GET_SIZE(obj);
Andrew M. Kuchling34c20cf2000-12-20 14:36:56 +00001602 if (size == 1) {
Jeremy Hylton394b54d2000-04-12 21:19:47 +00001603 ord = (long)*PyUnicode_AS_UNICODE(obj);
Andrew M. Kuchling34c20cf2000-12-20 14:36:56 +00001604 return PyInt_FromLong(ord);
1605 }
Jeremy Hylton394b54d2000-04-12 21:19:47 +00001606 } else {
1607 PyErr_Format(PyExc_TypeError,
Andrew M. Kuchlingf07aad12000-12-23 14:11:28 +00001608 "ord() expected string of length 1, but " \
Jeremy Hylton394b54d2000-04-12 21:19:47 +00001609 "%.200s found", obj->ob_type->tp_name);
Guido van Rossum09095f32000-03-10 23:00:52 +00001610 return NULL;
1611 }
1612
Guido van Rossumad991772001-01-12 16:03:05 +00001613 PyErr_Format(PyExc_TypeError,
Andrew M. Kuchlingf07aad12000-12-23 14:11:28 +00001614 "ord() expected a character, "
1615 "but string of length %d found",
Jeremy Hylton394b54d2000-04-12 21:19:47 +00001616 size);
1617 return NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001618}
1619
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001620static char ord_doc[] =
1621"ord(c) -> integer\n\
1622\n\
Guido van Rossumad991772001-01-12 16:03:05 +00001623Return the integer ordinal of a one-character string.";
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001624
1625
Guido van Rossum79f25d91997-04-29 20:08:16 +00001626static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001627builtin_pow(PyObject *self, PyObject *args)
Guido van Rossum6a00cd81995-01-07 12:39:01 +00001628{
Guido van Rossum09df08a1998-05-22 00:51:39 +00001629 PyObject *v, *w, *z = Py_None;
Guido van Rossum6a00cd81995-01-07 12:39:01 +00001630
Guido van Rossum79f25d91997-04-29 20:08:16 +00001631 if (!PyArg_ParseTuple(args, "OO|O:pow", &v, &w, &z))
Guido van Rossum6a00cd81995-01-07 12:39:01 +00001632 return NULL;
Guido van Rossum09df08a1998-05-22 00:51:39 +00001633 return PyNumber_Power(v, w, z);
Guido van Rossumd4905451991-05-05 20:00:36 +00001634}
1635
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001636static char pow_doc[] =
1637"pow(x, y[, z]) -> number\n\
1638\n\
1639With two arguments, equivalent to x**y. With three arguments,\n\
1640equivalent to (x**y) % z, but may be more efficient (e.g. for longs).";
1641
1642
Guido van Rossum124eff01999-02-23 16:11:01 +00001643/* Return number of items in range/xrange (lo, hi, step). step > 0
1644 * required. Return a value < 0 if & only if the true value is too
1645 * large to fit in a signed long.
1646 */
1647static long
Thomas Wouterse28c2962000-07-23 22:21:32 +00001648get_len_of_range(long lo, long hi, long step)
Guido van Rossum124eff01999-02-23 16:11:01 +00001649{
1650 /* -------------------------------------------------------------
1651 If lo >= hi, the range is empty.
1652 Else if n values are in the range, the last one is
1653 lo + (n-1)*step, which must be <= hi-1. Rearranging,
1654 n <= (hi - lo - 1)/step + 1, so taking the floor of the RHS gives
1655 the proper value. Since lo < hi in this case, hi-lo-1 >= 0, so
1656 the RHS is non-negative and so truncation is the same as the
1657 floor. Letting M be the largest positive long, the worst case
1658 for the RHS numerator is hi=M, lo=-M-1, and then
1659 hi-lo-1 = M-(-M-1)-1 = 2*M. Therefore unsigned long has enough
1660 precision to compute the RHS exactly.
1661 ---------------------------------------------------------------*/
1662 long n = 0;
1663 if (lo < hi) {
1664 unsigned long uhi = (unsigned long)hi;
1665 unsigned long ulo = (unsigned long)lo;
1666 unsigned long diff = uhi - ulo - 1;
1667 n = (long)(diff / (unsigned long)step + 1);
1668 }
1669 return n;
1670}
1671
Guido van Rossum79f25d91997-04-29 20:08:16 +00001672static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001673builtin_range(PyObject *self, PyObject *args)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001674{
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001675 long ilow = 0, ihigh = 0, istep = 1;
Guido van Rossum124eff01999-02-23 16:11:01 +00001676 long bign;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001677 int i, n;
Guido van Rossum124eff01999-02-23 16:11:01 +00001678
Guido van Rossum79f25d91997-04-29 20:08:16 +00001679 PyObject *v;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001680
Guido van Rossum79f25d91997-04-29 20:08:16 +00001681 if (PyTuple_Size(args) <= 1) {
1682 if (!PyArg_ParseTuple(args,
Guido van Rossum0865dd91995-01-17 16:30:22 +00001683 "l;range() requires 1-3 int arguments",
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001684 &ihigh))
1685 return NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001686 }
1687 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001688 if (!PyArg_ParseTuple(args,
Guido van Rossum0865dd91995-01-17 16:30:22 +00001689 "ll|l;range() requires 1-3 int arguments",
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001690 &ilow, &ihigh, &istep))
Guido van Rossum3f5da241990-12-20 15:06:42 +00001691 return NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001692 }
1693 if (istep == 0) {
Fred Drake661ea262000-10-24 19:57:45 +00001694 PyErr_SetString(PyExc_ValueError, "range() arg 3 must not be zero");
Guido van Rossum3f5da241990-12-20 15:06:42 +00001695 return NULL;
1696 }
Guido van Rossum124eff01999-02-23 16:11:01 +00001697 if (istep > 0)
1698 bign = get_len_of_range(ilow, ihigh, istep);
1699 else
1700 bign = get_len_of_range(ihigh, ilow, -istep);
1701 n = (int)bign;
1702 if (bign < 0 || (long)n != bign) {
Guido van Rossume23cde21999-01-12 05:07:47 +00001703 PyErr_SetString(PyExc_OverflowError,
Fred Drake661ea262000-10-24 19:57:45 +00001704 "range() result has too many items");
Guido van Rossume23cde21999-01-12 05:07:47 +00001705 return NULL;
1706 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00001707 v = PyList_New(n);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001708 if (v == NULL)
1709 return NULL;
1710 for (i = 0; i < n; i++) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001711 PyObject *w = PyInt_FromLong(ilow);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001712 if (w == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001713 Py_DECREF(v);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001714 return NULL;
1715 }
Guido van Rossuma937d141998-04-24 18:22:02 +00001716 PyList_SET_ITEM(v, i, w);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001717 ilow += istep;
1718 }
1719 return v;
1720}
1721
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001722static char range_doc[] =
1723"range([start,] stop[, step]) -> list of integers\n\
1724\n\
1725Return a list containing an arithmetic progression of integers.\n\
1726range(i, j) returns [i, i+1, i+2, ..., j-1]; start (!) defaults to 0.\n\
1727When step is given, it specifies the increment (or decrement).\n\
1728For example, range(4) returns [0, 1, 2, 3]. The end point is omitted!\n\
1729These are exactly the valid indices for a list of 4 elements.";
1730
1731
Guido van Rossum79f25d91997-04-29 20:08:16 +00001732static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001733builtin_xrange(PyObject *self, PyObject *args)
Guido van Rossum12d12c51993-10-26 17:58:25 +00001734{
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001735 long ilow = 0, ihigh = 0, istep = 1;
Guido van Rossum0865dd91995-01-17 16:30:22 +00001736 long n;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001737
Guido van Rossum79f25d91997-04-29 20:08:16 +00001738 if (PyTuple_Size(args) <= 1) {
1739 if (!PyArg_ParseTuple(args,
Guido van Rossum0865dd91995-01-17 16:30:22 +00001740 "l;xrange() requires 1-3 int arguments",
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001741 &ihigh))
1742 return NULL;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001743 }
1744 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001745 if (!PyArg_ParseTuple(args,
Guido van Rossum0865dd91995-01-17 16:30:22 +00001746 "ll|l;xrange() requires 1-3 int arguments",
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001747 &ilow, &ihigh, &istep))
Guido van Rossum12d12c51993-10-26 17:58:25 +00001748 return NULL;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001749 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001750 if (istep == 0) {
Fred Drake661ea262000-10-24 19:57:45 +00001751 PyErr_SetString(PyExc_ValueError, "xrange() arg 3 must not be zero");
Guido van Rossum12d12c51993-10-26 17:58:25 +00001752 return NULL;
1753 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001754 if (istep > 0)
Guido van Rossum124eff01999-02-23 16:11:01 +00001755 n = get_len_of_range(ilow, ihigh, istep);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001756 else
Guido van Rossum124eff01999-02-23 16:11:01 +00001757 n = get_len_of_range(ihigh, ilow, -istep);
1758 if (n < 0) {
1759 PyErr_SetString(PyExc_OverflowError,
Fred Drake661ea262000-10-24 19:57:45 +00001760 "xrange() result has too many items");
Guido van Rossum124eff01999-02-23 16:11:01 +00001761 return NULL;
1762 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00001763 return PyRange_New(ilow, n, istep, 1);
Guido van Rossum12d12c51993-10-26 17:58:25 +00001764}
1765
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001766static char xrange_doc[] =
1767"xrange([start,] stop[, step]) -> xrange object\n\
1768\n\
1769Like range(), but instead of returning a list, returns an object that\n\
1770generates the numbers in the range on demand. This is slightly slower\n\
1771than range() but more memory efficient.";
1772
1773
Guido van Rossum79f25d91997-04-29 20:08:16 +00001774static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001775builtin_raw_input(PyObject *self, PyObject *args)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001776{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001777 PyObject *v = NULL;
1778 PyObject *f;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001779
Guido van Rossum79f25d91997-04-29 20:08:16 +00001780 if (!PyArg_ParseTuple(args, "|O:[raw_]input", &v))
Guido van Rossum3165fe61992-09-25 21:59:05 +00001781 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001782 if (PyFile_AsFile(PySys_GetObject("stdin")) == stdin &&
1783 PyFile_AsFile(PySys_GetObject("stdout")) == stdout &&
Guido van Rossum53bb7ff1995-07-26 16:26:31 +00001784 isatty(fileno(stdin)) && isatty(fileno(stdout))) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001785 PyObject *po;
Guido van Rossum872537c1995-07-07 22:43:42 +00001786 char *prompt;
1787 char *s;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001788 PyObject *result;
Guido van Rossum872537c1995-07-07 22:43:42 +00001789 if (v != NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001790 po = PyObject_Str(v);
Guido van Rossum872537c1995-07-07 22:43:42 +00001791 if (po == NULL)
1792 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001793 prompt = PyString_AsString(po);
Guido van Rossumd9b52081998-06-26 18:25:38 +00001794 if (prompt == NULL)
1795 return NULL;
Guido van Rossum872537c1995-07-07 22:43:42 +00001796 }
1797 else {
1798 po = NULL;
1799 prompt = "";
1800 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00001801 s = PyOS_Readline(prompt);
1802 Py_XDECREF(po);
Guido van Rossum872537c1995-07-07 22:43:42 +00001803 if (s == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001804 PyErr_SetNone(PyExc_KeyboardInterrupt);
Guido van Rossum872537c1995-07-07 22:43:42 +00001805 return NULL;
1806 }
1807 if (*s == '\0') {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001808 PyErr_SetNone(PyExc_EOFError);
Guido van Rossum872537c1995-07-07 22:43:42 +00001809 result = NULL;
1810 }
1811 else { /* strip trailing '\n' */
Guido van Rossum106f2da2000-06-28 21:12:25 +00001812 size_t len = strlen(s);
1813 if (len > INT_MAX) {
1814 PyErr_SetString(PyExc_OverflowError, "input too long");
1815 result = NULL;
1816 }
1817 else {
1818 result = PyString_FromStringAndSize(s, (int)(len-1));
1819 }
Guido van Rossum872537c1995-07-07 22:43:42 +00001820 }
Guido van Rossumb18618d2000-05-03 23:44:39 +00001821 PyMem_FREE(s);
Guido van Rossum872537c1995-07-07 22:43:42 +00001822 return result;
1823 }
Guido van Rossum90933611991-06-07 16:10:43 +00001824 if (v != NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001825 f = PySys_GetObject("stdout");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001826 if (f == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001827 PyErr_SetString(PyExc_RuntimeError, "lost sys.stdout");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001828 return NULL;
1829 }
Guido van Rossumc8b6df91997-05-23 00:06:51 +00001830 if (Py_FlushLine() != 0 ||
1831 PyFile_WriteObject(v, f, Py_PRINT_RAW) != 0)
Guido van Rossum90933611991-06-07 16:10:43 +00001832 return NULL;
1833 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00001834 f = PySys_GetObject("stdin");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001835 if (f == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001836 PyErr_SetString(PyExc_RuntimeError, "lost sys.stdin");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001837 return NULL;
1838 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00001839 return PyFile_GetLine(f, -1);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001840}
1841
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001842static char raw_input_doc[] =
1843"raw_input([prompt]) -> string\n\
1844\n\
1845Read a string from standard input. The trailing newline is stripped.\n\
1846If the user hits EOF (Unix: Ctl-D, Windows: Ctl-Z+Return), raise EOFError.\n\
1847On Unix, GNU readline is used if enabled. The prompt string, if given,\n\
1848is printed without a trailing newline before reading.";
1849
1850
Guido van Rossum79f25d91997-04-29 20:08:16 +00001851static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001852builtin_reduce(PyObject *self, PyObject *args)
Guido van Rossum12d12c51993-10-26 17:58:25 +00001853{
Tim Peters15d81ef2001-05-04 04:39:21 +00001854 PyObject *seq, *func, *result = NULL, *it;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001855
Guido van Rossum79f25d91997-04-29 20:08:16 +00001856 if (!PyArg_ParseTuple(args, "OO|O:reduce", &func, &seq, &result))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001857 return NULL;
1858 if (result != NULL)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001859 Py_INCREF(result);
Guido van Rossum12d12c51993-10-26 17:58:25 +00001860
Tim Peters15d81ef2001-05-04 04:39:21 +00001861 it = PyObject_GetIter(seq);
1862 if (it == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001863 PyErr_SetString(PyExc_TypeError,
Tim Peters15d81ef2001-05-04 04:39:21 +00001864 "reduce() arg 2 must support iteration");
1865 Py_XDECREF(result);
Guido van Rossum12d12c51993-10-26 17:58:25 +00001866 return NULL;
1867 }
1868
Guido van Rossum79f25d91997-04-29 20:08:16 +00001869 if ((args = PyTuple_New(2)) == NULL)
Guido van Rossum2d951851994-08-29 12:52:16 +00001870 goto Fail;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001871
Tim Peters15d81ef2001-05-04 04:39:21 +00001872 for (;;) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001873 PyObject *op2;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001874
1875 if (args->ob_refcnt > 1) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001876 Py_DECREF(args);
1877 if ((args = PyTuple_New(2)) == NULL)
Guido van Rossum2d951851994-08-29 12:52:16 +00001878 goto Fail;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001879 }
1880
Tim Peters15d81ef2001-05-04 04:39:21 +00001881 op2 = PyIter_Next(it);
1882 if (op2 == NULL) {
1883 /* StopIteration is *implied* by a NULL return from
1884 * PyIter_Next() if PyErr_Occurred() is false.
1885 */
1886 if (PyErr_Occurred()) {
1887 if (PyErr_ExceptionMatches(PyExc_StopIteration))
1888 PyErr_Clear();
1889 else
1890 goto Fail;
Guido van Rossum2d951851994-08-29 12:52:16 +00001891 }
Tim Peters15d81ef2001-05-04 04:39:21 +00001892 break;
Guido van Rossum2d951851994-08-29 12:52:16 +00001893 }
Guido van Rossum12d12c51993-10-26 17:58:25 +00001894
Guido van Rossum2d951851994-08-29 12:52:16 +00001895 if (result == NULL)
1896 result = op2;
1897 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001898 PyTuple_SetItem(args, 0, result);
1899 PyTuple_SetItem(args, 1, op2);
1900 if ((result = PyEval_CallObject(func, args)) == NULL)
Guido van Rossum2d951851994-08-29 12:52:16 +00001901 goto Fail;
1902 }
Guido van Rossum12d12c51993-10-26 17:58:25 +00001903 }
1904
Guido van Rossum79f25d91997-04-29 20:08:16 +00001905 Py_DECREF(args);
Guido van Rossum12d12c51993-10-26 17:58:25 +00001906
Guido van Rossum2d951851994-08-29 12:52:16 +00001907 if (result == NULL)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001908 PyErr_SetString(PyExc_TypeError,
Fred Drake661ea262000-10-24 19:57:45 +00001909 "reduce() of empty sequence with no initial value");
Guido van Rossum2d951851994-08-29 12:52:16 +00001910
Tim Peters15d81ef2001-05-04 04:39:21 +00001911 Py_DECREF(it);
Guido van Rossum12d12c51993-10-26 17:58:25 +00001912 return result;
1913
Guido van Rossum2d951851994-08-29 12:52:16 +00001914Fail:
Guido van Rossum79f25d91997-04-29 20:08:16 +00001915 Py_XDECREF(args);
1916 Py_XDECREF(result);
Tim Peters15d81ef2001-05-04 04:39:21 +00001917 Py_DECREF(it);
Guido van Rossum12d12c51993-10-26 17:58:25 +00001918 return NULL;
1919}
1920
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001921static char reduce_doc[] =
1922"reduce(function, sequence[, initial]) -> value\n\
1923\n\
1924Apply a function of two arguments cumulatively to the items of a sequence,\n\
1925from left to right, so as to reduce the sequence to a single value.\n\
1926For example, reduce(lambda x, y: x+y, [1, 2, 3, 4, 5]) calculates\n\
1927((((1+2)+3)+4)+5). If initial is present, it is placed before the items\n\
1928of the sequence in the calculation, and serves as a default when the\n\
1929sequence is empty.";
1930
1931
Guido van Rossum79f25d91997-04-29 20:08:16 +00001932static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001933builtin_reload(PyObject *self, PyObject *args)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001934{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001935 PyObject *v;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001936
Guido van Rossum79f25d91997-04-29 20:08:16 +00001937 if (!PyArg_ParseTuple(args, "O:reload", &v))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001938 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001939 return PyImport_ReloadModule(v);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001940}
1941
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001942static char reload_doc[] =
1943"reload(module) -> module\n\
1944\n\
1945Reload the module. The module must have been successfully imported before.";
1946
1947
Guido van Rossum79f25d91997-04-29 20:08:16 +00001948static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001949builtin_repr(PyObject *self, PyObject *args)
Guido van Rossumc89705d1992-11-26 08:54:07 +00001950{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001951 PyObject *v;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001952
Guido van Rossum79f25d91997-04-29 20:08:16 +00001953 if (!PyArg_ParseTuple(args, "O:repr", &v))
Guido van Rossumc89705d1992-11-26 08:54:07 +00001954 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001955 return PyObject_Repr(v);
Guido van Rossumc89705d1992-11-26 08:54:07 +00001956}
1957
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001958static char repr_doc[] =
1959"repr(object) -> string\n\
1960\n\
1961Return the canonical string representation of the object.\n\
1962For most object types, eval(repr(object)) == object.";
1963
1964
Guido van Rossum79f25d91997-04-29 20:08:16 +00001965static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001966builtin_round(PyObject *self, PyObject *args)
Guido van Rossum9e51f9b1993-02-12 16:29:05 +00001967{
Guido van Rossum9e51f9b1993-02-12 16:29:05 +00001968 double x;
1969 double f;
1970 int ndigits = 0;
Guido van Rossum9e51f9b1993-02-12 16:29:05 +00001971 int i;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001972
Guido van Rossum79f25d91997-04-29 20:08:16 +00001973 if (!PyArg_ParseTuple(args, "d|i:round", &x, &ndigits))
Guido van Rossum9e51f9b1993-02-12 16:29:05 +00001974 return NULL;
Guido van Rossum9e51f9b1993-02-12 16:29:05 +00001975 f = 1.0;
Guido van Rossum1e162d31998-05-09 14:42:25 +00001976 i = abs(ndigits);
1977 while (--i >= 0)
Guido van Rossum9e51f9b1993-02-12 16:29:05 +00001978 f = f*10.0;
Guido van Rossum1e162d31998-05-09 14:42:25 +00001979 if (ndigits < 0)
1980 x /= f;
Guido van Rossum9e51f9b1993-02-12 16:29:05 +00001981 else
Guido van Rossum1e162d31998-05-09 14:42:25 +00001982 x *= f;
1983 if (x >= 0.0)
1984 x = floor(x + 0.5);
1985 else
1986 x = ceil(x - 0.5);
1987 if (ndigits < 0)
1988 x *= f;
1989 else
1990 x /= f;
1991 return PyFloat_FromDouble(x);
Guido van Rossum9e51f9b1993-02-12 16:29:05 +00001992}
1993
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001994static char round_doc[] =
1995"round(number[, ndigits]) -> floating point number\n\
1996\n\
1997Round a number to a given precision in decimal digits (default 0 digits).\n\
1998This always returns a floating point number. Precision may be negative.";
1999
2000
Guido van Rossum79f25d91997-04-29 20:08:16 +00002001static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002002builtin_str(PyObject *self, PyObject *args)
Guido van Rossumc89705d1992-11-26 08:54:07 +00002003{
Guido van Rossum79f25d91997-04-29 20:08:16 +00002004 PyObject *v;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002005
Guido van Rossum79f25d91997-04-29 20:08:16 +00002006 if (!PyArg_ParseTuple(args, "O:str", &v))
Guido van Rossumc89705d1992-11-26 08:54:07 +00002007 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002008 return PyObject_Str(v);
Guido van Rossumc89705d1992-11-26 08:54:07 +00002009}
2010
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002011static char str_doc[] =
2012"str(object) -> string\n\
2013\n\
2014Return a nice string representation of the object.\n\
2015If the argument is a string, the return value is the same object.";
2016
2017
Guido van Rossum79f25d91997-04-29 20:08:16 +00002018static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002019builtin_tuple(PyObject *self, PyObject *args)
Guido van Rossumcae027b1994-08-29 12:53:11 +00002020{
Guido van Rossum79f25d91997-04-29 20:08:16 +00002021 PyObject *v;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002022
Guido van Rossum79f25d91997-04-29 20:08:16 +00002023 if (!PyArg_ParseTuple(args, "O:tuple", &v))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002024 return NULL;
Guido van Rossum09df08a1998-05-22 00:51:39 +00002025 return PySequence_Tuple(v);
Guido van Rossumcae027b1994-08-29 12:53:11 +00002026}
2027
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002028static char tuple_doc[] =
2029"tuple(sequence) -> list\n\
2030\n\
2031Return a tuple whose items are the same as those of the argument sequence.\n\
2032If the argument is a tuple, the return value is the same object.";
2033
2034
Guido van Rossum79f25d91997-04-29 20:08:16 +00002035static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002036builtin_type(PyObject *self, PyObject *args)
Guido van Rossum3f5da241990-12-20 15:06:42 +00002037{
Guido van Rossum79f25d91997-04-29 20:08:16 +00002038 PyObject *v;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002039
Guido van Rossum79f25d91997-04-29 20:08:16 +00002040 if (!PyArg_ParseTuple(args, "O:type", &v))
Guido van Rossum3f5da241990-12-20 15:06:42 +00002041 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002042 v = (PyObject *)v->ob_type;
2043 Py_INCREF(v);
Guido van Rossum3f5da241990-12-20 15:06:42 +00002044 return v;
2045}
2046
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002047static char type_doc[] =
2048"type(object) -> type object\n\
2049\n\
2050Return the type of the object.";
2051
2052
Guido van Rossum79f25d91997-04-29 20:08:16 +00002053static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002054builtin_vars(PyObject *self, PyObject *args)
Guido van Rossum2d951851994-08-29 12:52:16 +00002055{
Guido van Rossum79f25d91997-04-29 20:08:16 +00002056 PyObject *v = NULL;
2057 PyObject *d;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002058
Guido van Rossum79f25d91997-04-29 20:08:16 +00002059 if (!PyArg_ParseTuple(args, "|O:vars", &v))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002060 return NULL;
Guido van Rossum2d951851994-08-29 12:52:16 +00002061 if (v == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002062 d = PyEval_GetLocals();
Guido van Rossum53bb7ff1995-07-26 16:26:31 +00002063 if (d == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002064 if (!PyErr_Occurred())
2065 PyErr_SetString(PyExc_SystemError,
2066 "no locals!?");
Guido van Rossum53bb7ff1995-07-26 16:26:31 +00002067 }
2068 else
Guido van Rossum79f25d91997-04-29 20:08:16 +00002069 Py_INCREF(d);
Guido van Rossum2d951851994-08-29 12:52:16 +00002070 }
2071 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002072 d = PyObject_GetAttrString(v, "__dict__");
Guido van Rossum2d951851994-08-29 12:52:16 +00002073 if (d == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002074 PyErr_SetString(PyExc_TypeError,
Guido van Rossum2d951851994-08-29 12:52:16 +00002075 "vars() argument must have __dict__ attribute");
2076 return NULL;
2077 }
2078 }
2079 return d;
2080}
2081
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002082static char vars_doc[] =
2083"vars([object]) -> dictionary\n\
2084\n\
2085Without arguments, equivalent to locals().\n\
2086With an argument, equivalent to object.__dict__.";
2087
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002088static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002089builtin_isinstance(PyObject *self, PyObject *args)
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002090{
2091 PyObject *inst;
2092 PyObject *cls;
Guido van Rossum823649d2001-03-21 18:40:58 +00002093 int retval;
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002094
Guido van Rossum43713e52000-02-29 13:59:29 +00002095 if (!PyArg_ParseTuple(args, "OO:isinstance", &inst, &cls))
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002096 return NULL;
Guido van Rossumf5dd9141997-12-02 19:11:45 +00002097
Guido van Rossum823649d2001-03-21 18:40:58 +00002098 retval = PyObject_IsInstance(inst, cls);
2099 if (retval < 0)
Guido van Rossumad991772001-01-12 16:03:05 +00002100 return NULL;
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002101 return PyInt_FromLong(retval);
2102}
2103
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002104static char isinstance_doc[] =
2105"isinstance(object, class-or-type) -> Boolean\n\
2106\n\
2107Return whether an object is an instance of a class or of a subclass thereof.\n\
2108With a type as second argument, return whether that is the object's type.";
2109
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002110
2111static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002112builtin_issubclass(PyObject *self, PyObject *args)
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002113{
2114 PyObject *derived;
2115 PyObject *cls;
2116 int retval;
2117
Guido van Rossum43713e52000-02-29 13:59:29 +00002118 if (!PyArg_ParseTuple(args, "OO:issubclass", &derived, &cls))
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002119 return NULL;
Guido van Rossum668213d1999-06-16 17:28:37 +00002120
Guido van Rossum823649d2001-03-21 18:40:58 +00002121 retval = PyObject_IsSubclass(derived, cls);
2122 if (retval < 0)
2123 return NULL;
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002124 return PyInt_FromLong(retval);
2125}
2126
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002127static char issubclass_doc[] =
2128"issubclass(C, B) -> Boolean\n\
2129\n\
2130Return whether class C is a subclass (i.e., a derived class) of class B.";
2131
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002132
Barry Warsawbd599b52000-08-03 15:45:29 +00002133static PyObject*
2134builtin_zip(PyObject *self, PyObject *args)
2135{
2136 PyObject *ret;
2137 int itemsize = PySequence_Length(args);
2138 int i, j;
2139
2140 if (itemsize < 1) {
2141 PyErr_SetString(PyExc_TypeError,
Fred Drake661ea262000-10-24 19:57:45 +00002142 "zip() requires at least one sequence");
Barry Warsawbd599b52000-08-03 15:45:29 +00002143 return NULL;
2144 }
2145 /* args must be a tuple */
2146 assert(PyTuple_Check(args));
2147
2148 if ((ret = PyList_New(0)) == NULL)
2149 return NULL;
2150
2151 for (i = 0;; i++) {
2152 PyObject *next = PyTuple_New(itemsize);
2153 if (!next) {
2154 Py_DECREF(ret);
2155 return NULL;
2156 }
2157 for (j = 0; j < itemsize; j++) {
2158 PyObject *seq = PyTuple_GET_ITEM(args, j);
2159 PyObject *item = PySequence_GetItem(seq, i);
2160
2161 if (!item) {
2162 if (PyErr_ExceptionMatches(PyExc_IndexError)) {
2163 PyErr_Clear();
2164 Py_DECREF(next);
2165 return ret;
2166 }
2167 Py_DECREF(next);
2168 Py_DECREF(ret);
2169 return NULL;
2170 }
2171 PyTuple_SET_ITEM(next, j, item);
2172 }
2173 PyList_Append(ret, next);
2174 Py_DECREF(next);
2175 }
2176 /* no return */
2177}
2178
2179
2180static char zip_doc[] =
2181"zip(seq1 [, seq2 [...]]) -> [(seq1[0], seq2[0] ...), (...)]\n\
2182\n\
2183Return a list of tuples, where each tuple contains the i-th element\n\
2184from each of the argument sequences. The returned list is truncated\n\
2185in length to the length of the shortest argument sequence.";
2186
2187
Guido van Rossum79f25d91997-04-29 20:08:16 +00002188static PyMethodDef builtin_methods[] = {
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002189 {"__import__", builtin___import__, 1, import_doc},
2190 {"abs", builtin_abs, 1, abs_doc},
2191 {"apply", builtin_apply, 1, apply_doc},
Guido van Rossum0daf0221999-03-19 19:07:19 +00002192 {"buffer", builtin_buffer, 1, buffer_doc},
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002193 {"callable", builtin_callable, 1, callable_doc},
2194 {"chr", builtin_chr, 1, chr_doc},
2195 {"cmp", builtin_cmp, 1, cmp_doc},
2196 {"coerce", builtin_coerce, 1, coerce_doc},
2197 {"compile", builtin_compile, 1, compile_doc},
Guido van Rossum8a5c5d21996-01-12 01:09:56 +00002198#ifndef WITHOUT_COMPLEX
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002199 {"complex", builtin_complex, 1, complex_doc},
Guido van Rossum8a5c5d21996-01-12 01:09:56 +00002200#endif
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002201 {"delattr", builtin_delattr, 1, delattr_doc},
2202 {"dir", builtin_dir, 1, dir_doc},
2203 {"divmod", builtin_divmod, 1, divmod_doc},
2204 {"eval", builtin_eval, 1, eval_doc},
2205 {"execfile", builtin_execfile, 1, execfile_doc},
2206 {"filter", builtin_filter, 1, filter_doc},
2207 {"float", builtin_float, 1, float_doc},
2208 {"getattr", builtin_getattr, 1, getattr_doc},
2209 {"globals", builtin_globals, 1, globals_doc},
2210 {"hasattr", builtin_hasattr, 1, hasattr_doc},
2211 {"hash", builtin_hash, 1, hash_doc},
2212 {"hex", builtin_hex, 1, hex_doc},
2213 {"id", builtin_id, 1, id_doc},
2214 {"input", builtin_input, 1, input_doc},
2215 {"intern", builtin_intern, 1, intern_doc},
2216 {"int", builtin_int, 1, int_doc},
2217 {"isinstance", builtin_isinstance, 1, isinstance_doc},
2218 {"issubclass", builtin_issubclass, 1, issubclass_doc},
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002219 {"iter", builtin_iter, 1, iter_doc},
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002220 {"len", builtin_len, 1, len_doc},
2221 {"list", builtin_list, 1, list_doc},
2222 {"locals", builtin_locals, 1, locals_doc},
2223 {"long", builtin_long, 1, long_doc},
2224 {"map", builtin_map, 1, map_doc},
2225 {"max", builtin_max, 1, max_doc},
2226 {"min", builtin_min, 1, min_doc},
2227 {"oct", builtin_oct, 1, oct_doc},
2228 {"open", builtin_open, 1, open_doc},
2229 {"ord", builtin_ord, 1, ord_doc},
2230 {"pow", builtin_pow, 1, pow_doc},
2231 {"range", builtin_range, 1, range_doc},
2232 {"raw_input", builtin_raw_input, 1, raw_input_doc},
2233 {"reduce", builtin_reduce, 1, reduce_doc},
2234 {"reload", builtin_reload, 1, reload_doc},
2235 {"repr", builtin_repr, 1, repr_doc},
2236 {"round", builtin_round, 1, round_doc},
2237 {"setattr", builtin_setattr, 1, setattr_doc},
2238 {"slice", builtin_slice, 1, slice_doc},
2239 {"str", builtin_str, 1, str_doc},
2240 {"tuple", builtin_tuple, 1, tuple_doc},
2241 {"type", builtin_type, 1, type_doc},
Guido van Rossum09095f32000-03-10 23:00:52 +00002242 {"unicode", builtin_unicode, 1, unicode_doc},
2243 {"unichr", builtin_unichr, 1, unichr_doc},
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002244 {"vars", builtin_vars, 1, vars_doc},
2245 {"xrange", builtin_xrange, 1, xrange_doc},
Barry Warsawbd599b52000-08-03 15:45:29 +00002246 {"zip", builtin_zip, 1, zip_doc},
Guido van Rossumc02e15c1991-12-16 13:03:00 +00002247 {NULL, NULL},
Guido van Rossum3f5da241990-12-20 15:06:42 +00002248};
2249
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002250static char builtin_doc[] =
2251"Built-in functions, exceptions, and other objects.\n\
2252\n\
2253Noteworthy: None is the `nil' object; Ellipsis represents `...' in slices.";
2254
Guido van Rossum25ce5661997-08-02 03:10:38 +00002255PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002256_PyBuiltin_Init(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +00002257{
Fred Drake5550de32000-06-20 04:54:19 +00002258 PyObject *mod, *dict, *debug;
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002259 mod = Py_InitModule4("__builtin__", builtin_methods,
2260 builtin_doc, (PyObject *)NULL,
2261 PYTHON_API_VERSION);
Guido van Rossum25ce5661997-08-02 03:10:38 +00002262 if (mod == NULL)
2263 return NULL;
2264 dict = PyModule_GetDict(mod);
Guido van Rossum25ce5661997-08-02 03:10:38 +00002265 if (PyDict_SetItemString(dict, "None", Py_None) < 0)
2266 return NULL;
2267 if (PyDict_SetItemString(dict, "Ellipsis", Py_Ellipsis) < 0)
2268 return NULL;
Guido van Rossumad991772001-01-12 16:03:05 +00002269 if (PyDict_SetItemString(dict, "NotImplemented",
Neil Schemenauer23ab1992001-01-04 01:48:42 +00002270 Py_NotImplemented) < 0)
2271 return NULL;
Fred Drake5550de32000-06-20 04:54:19 +00002272 debug = PyInt_FromLong(Py_OptimizeFlag == 0);
2273 if (PyDict_SetItemString(dict, "__debug__", debug) < 0) {
2274 Py_XDECREF(debug);
Guido van Rossum25ce5661997-08-02 03:10:38 +00002275 return NULL;
Fred Drake5550de32000-06-20 04:54:19 +00002276 }
2277 Py_XDECREF(debug);
Barry Warsaw757af0e1997-08-29 22:13:51 +00002278
Guido van Rossum25ce5661997-08-02 03:10:38 +00002279 return mod;
Guido van Rossum3f5da241990-12-20 15:06:42 +00002280}
2281
Guido van Rossume77a7571993-11-03 15:01:26 +00002282/* Helper for filter(): filter a tuple through a function */
Guido van Rossum12d12c51993-10-26 17:58:25 +00002283
Guido van Rossum79f25d91997-04-29 20:08:16 +00002284static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002285filtertuple(PyObject *func, PyObject *tuple)
Guido van Rossum12d12c51993-10-26 17:58:25 +00002286{
Guido van Rossum79f25d91997-04-29 20:08:16 +00002287 PyObject *result;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002288 register int i, j;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002289 int len = PyTuple_Size(tuple);
Guido van Rossum12d12c51993-10-26 17:58:25 +00002290
Guido van Rossumb7b45621995-08-04 04:07:45 +00002291 if (len == 0) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002292 Py_INCREF(tuple);
Guido van Rossumb7b45621995-08-04 04:07:45 +00002293 return tuple;
2294 }
2295
Guido van Rossum79f25d91997-04-29 20:08:16 +00002296 if ((result = PyTuple_New(len)) == NULL)
Guido van Rossum2586bf01993-11-01 16:21:44 +00002297 return NULL;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002298
Guido van Rossum12d12c51993-10-26 17:58:25 +00002299 for (i = j = 0; i < len; ++i) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002300 PyObject *item, *good;
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002301 int ok;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002302
Guido van Rossum79f25d91997-04-29 20:08:16 +00002303 if ((item = PyTuple_GetItem(tuple, i)) == NULL)
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002304 goto Fail_1;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002305 if (func == Py_None) {
2306 Py_INCREF(item);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002307 good = item;
2308 }
2309 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002310 PyObject *arg = Py_BuildValue("(O)", item);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002311 if (arg == NULL)
2312 goto Fail_1;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002313 good = PyEval_CallObject(func, arg);
2314 Py_DECREF(arg);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002315 if (good == NULL)
Guido van Rossum12d12c51993-10-26 17:58:25 +00002316 goto Fail_1;
2317 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002318 ok = PyObject_IsTrue(good);
2319 Py_DECREF(good);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002320 if (ok) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002321 Py_INCREF(item);
2322 if (PyTuple_SetItem(result, j++, item) < 0)
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002323 goto Fail_1;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002324 }
Guido van Rossum12d12c51993-10-26 17:58:25 +00002325 }
2326
Guido van Rossum79f25d91997-04-29 20:08:16 +00002327 if (_PyTuple_Resize(&result, j, 0) < 0)
Guido van Rossum12d12c51993-10-26 17:58:25 +00002328 return NULL;
2329
Guido van Rossum12d12c51993-10-26 17:58:25 +00002330 return result;
2331
Guido van Rossum12d12c51993-10-26 17:58:25 +00002332Fail_1:
Guido van Rossum79f25d91997-04-29 20:08:16 +00002333 Py_DECREF(result);
Guido van Rossum12d12c51993-10-26 17:58:25 +00002334 return NULL;
2335}
2336
2337
Guido van Rossume77a7571993-11-03 15:01:26 +00002338/* Helper for filter(): filter a string through a function */
Guido van Rossum12d12c51993-10-26 17:58:25 +00002339
Guido van Rossum79f25d91997-04-29 20:08:16 +00002340static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002341filterstring(PyObject *func, PyObject *strobj)
Guido van Rossum12d12c51993-10-26 17:58:25 +00002342{
Guido van Rossum79f25d91997-04-29 20:08:16 +00002343 PyObject *result;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002344 register int i, j;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002345 int len = PyString_Size(strobj);
Guido van Rossum12d12c51993-10-26 17:58:25 +00002346
Guido van Rossum79f25d91997-04-29 20:08:16 +00002347 if (func == Py_None) {
Guido van Rossum2586bf01993-11-01 16:21:44 +00002348 /* No character is ever false -- share input string */
Guido van Rossum79f25d91997-04-29 20:08:16 +00002349 Py_INCREF(strobj);
Guido van Rossum2d951851994-08-29 12:52:16 +00002350 return strobj;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002351 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002352 if ((result = PyString_FromStringAndSize(NULL, len)) == NULL)
Guido van Rossum2586bf01993-11-01 16:21:44 +00002353 return NULL;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002354
Guido van Rossum12d12c51993-10-26 17:58:25 +00002355 for (i = j = 0; i < len; ++i) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002356 PyObject *item, *arg, *good;
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002357 int ok;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002358
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002359 item = (*strobj->ob_type->tp_as_sequence->sq_item)(strobj, i);
2360 if (item == NULL)
2361 goto Fail_1;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002362 arg = Py_BuildValue("(O)", item);
Tim Peters388ed082001-04-07 20:34:48 +00002363 if (arg == NULL) {
2364 Py_DECREF(item);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002365 goto Fail_1;
Tim Peters388ed082001-04-07 20:34:48 +00002366 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002367 good = PyEval_CallObject(func, arg);
2368 Py_DECREF(arg);
Tim Peters388ed082001-04-07 20:34:48 +00002369 if (good == NULL) {
2370 Py_DECREF(item);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002371 goto Fail_1;
Tim Peters388ed082001-04-07 20:34:48 +00002372 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002373 ok = PyObject_IsTrue(good);
2374 Py_DECREF(good);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002375 if (ok)
Guido van Rossum79f25d91997-04-29 20:08:16 +00002376 PyString_AS_STRING((PyStringObject *)result)[j++] =
2377 PyString_AS_STRING((PyStringObject *)item)[0];
Tim Peters388ed082001-04-07 20:34:48 +00002378 Py_DECREF(item);
Guido van Rossum12d12c51993-10-26 17:58:25 +00002379 }
2380
Guido van Rossum79f25d91997-04-29 20:08:16 +00002381 if (j < len && _PyString_Resize(&result, j) < 0)
Guido van Rossum12d12c51993-10-26 17:58:25 +00002382 return NULL;
2383
Guido van Rossum12d12c51993-10-26 17:58:25 +00002384 return result;
2385
Guido van Rossum12d12c51993-10-26 17:58:25 +00002386Fail_1:
Guido van Rossum79f25d91997-04-29 20:08:16 +00002387 Py_DECREF(result);
Guido van Rossum12d12c51993-10-26 17:58:25 +00002388 return NULL;
2389}