blob: 9e8a2279c0db7aa4729108d6d5d791a42aa7886c [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 {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000939 PyObject *seq;
940 PySequenceMethods *sqf;
Tim Peters748b8bb2001-04-28 08:20:22 +0000941 int saw_IndexError;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000942 } sequence;
943
Guido van Rossum79f25d91997-04-29 20:08:16 +0000944 PyObject *func, *result;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000945 sequence *seqs = NULL, *sqp;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000946 int n, len;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000947 register int i, j;
948
Guido van Rossum79f25d91997-04-29 20:08:16 +0000949 n = PyTuple_Size(args);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000950 if (n < 2) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000951 PyErr_SetString(PyExc_TypeError,
952 "map() requires at least two args");
Guido van Rossum12d12c51993-10-26 17:58:25 +0000953 return NULL;
954 }
955
Guido van Rossum79f25d91997-04-29 20:08:16 +0000956 func = PyTuple_GetItem(args, 0);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000957 n--;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000958
Guido van Rossumfa4ac711998-07-10 17:37:30 +0000959 if (func == Py_None && n == 1) {
960 /* map(None, S) is the same as list(S). */
961 return PySequence_List(PyTuple_GetItem(args, 1));
962 }
963
Guido van Rossum79f25d91997-04-29 20:08:16 +0000964 if ((seqs = PyMem_NEW(sequence, n)) == NULL) {
965 PyErr_NoMemory();
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000966 goto Fail_2;
967 }
Guido van Rossum12d12c51993-10-26 17:58:25 +0000968
Tim Peters748b8bb2001-04-28 08:20:22 +0000969 /* Do a first pass to (a) verify the args are sequences; (b) set
970 * len to the largest of their lengths; (c) initialize the seqs
971 * descriptor vector.
972 */
Guido van Rossum2d951851994-08-29 12:52:16 +0000973 for (len = 0, i = 0, sqp = seqs; i < n; ++i, ++sqp) {
Guido van Rossum12d12c51993-10-26 17:58:25 +0000974 int curlen;
Guido van Rossum09df08a1998-05-22 00:51:39 +0000975 PySequenceMethods *sqf;
Guido van Rossumad991772001-01-12 16:03:05 +0000976
Guido van Rossum79f25d91997-04-29 20:08:16 +0000977 if ((sqp->seq = PyTuple_GetItem(args, i + 1)) == NULL)
Guido van Rossum12d12c51993-10-26 17:58:25 +0000978 goto Fail_2;
979
Tim Peters748b8bb2001-04-28 08:20:22 +0000980 sqp->saw_IndexError = 0;
981
Guido van Rossum09df08a1998-05-22 00:51:39 +0000982 sqp->sqf = sqf = sqp->seq->ob_type->tp_as_sequence;
983 if (sqf == NULL ||
Guido van Rossum09df08a1998-05-22 00:51:39 +0000984 sqf->sq_item == NULL)
985 {
Guido van Rossum12d12c51993-10-26 17:58:25 +0000986 static char errmsg[] =
987 "argument %d to map() must be a sequence object";
Guido van Rossum15e33a41997-04-30 19:00:27 +0000988 char errbuf[sizeof(errmsg) + 25];
Guido van Rossum12d12c51993-10-26 17:58:25 +0000989
990 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 Peters748b8bb2001-04-28 08:20:22 +0000995 if (sqf->sq_length == NULL)
996 /* doesn't matter -- make something up */
997 curlen = 8;
998 else
999 curlen = (*sqf->sq_length)(sqp->seq);
1000 if (curlen < 0)
Guido van Rossum12d12c51993-10-26 17:58:25 +00001001 goto Fail_2;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001002 if (curlen > len)
1003 len = curlen;
1004 }
1005
Guido van Rossum79f25d91997-04-29 20:08:16 +00001006 if ((result = (PyObject *) PyList_New(len)) == NULL)
Guido van Rossum12d12c51993-10-26 17:58:25 +00001007 goto Fail_2;
1008
Tim Peters748b8bb2001-04-28 08:20:22 +00001009 /* Iterate over the sequences until all have raised IndexError. */
Guido van Rossum2d951851994-08-29 12:52:16 +00001010 for (i = 0; ; ++i) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001011 PyObject *alist, *item=NULL, *value;
Guido van Rossum2d951851994-08-29 12:52:16 +00001012 int any = 0;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001013
Guido van Rossum79f25d91997-04-29 20:08:16 +00001014 if (func == Py_None && n == 1)
Guido van Rossum32120311995-07-10 13:52:21 +00001015 alist = NULL;
Guido van Rossum2d951851994-08-29 12:52:16 +00001016 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001017 if ((alist = PyTuple_New(n)) == NULL)
Guido van Rossum2d951851994-08-29 12:52:16 +00001018 goto Fail_1;
1019 }
Guido van Rossum12d12c51993-10-26 17:58:25 +00001020
1021 for (j = 0, sqp = seqs; j < n; ++j, ++sqp) {
Tim Peters748b8bb2001-04-28 08:20:22 +00001022 if (sqp->saw_IndexError) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001023 Py_INCREF(Py_None);
1024 item = Py_None;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001025 }
Guido van Rossum12d12c51993-10-26 17:58:25 +00001026 else {
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00001027 item = (*sqp->sqf->sq_item)(sqp->seq, i);
Guido van Rossum2d951851994-08-29 12:52:16 +00001028 if (item == NULL) {
Barry Warsawcde8b1b1997-08-22 21:14:38 +00001029 if (PyErr_ExceptionMatches(
1030 PyExc_IndexError))
1031 {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001032 PyErr_Clear();
1033 Py_INCREF(Py_None);
1034 item = Py_None;
Tim Peters748b8bb2001-04-28 08:20:22 +00001035 sqp->saw_IndexError = 1;
Guido van Rossum2d951851994-08-29 12:52:16 +00001036 }
1037 else {
1038 goto Fail_0;
1039 }
1040 }
1041 else
1042 any = 1;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001043
Guido van Rossum12d12c51993-10-26 17:58:25 +00001044 }
Guido van Rossum32120311995-07-10 13:52:21 +00001045 if (!alist)
Guido van Rossum2d951851994-08-29 12:52:16 +00001046 break;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001047 if (PyTuple_SetItem(alist, j, item) < 0) {
1048 Py_DECREF(item);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00001049 goto Fail_0;
Guido van Rossum2d951851994-08-29 12:52:16 +00001050 }
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00001051 continue;
1052
1053 Fail_0:
Guido van Rossum79f25d91997-04-29 20:08:16 +00001054 Py_XDECREF(alist);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00001055 goto Fail_1;
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
1061 if (!any) {
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 }
1080 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001081 if (PyList_SetItem(result, i, value) < 0)
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00001082 goto Fail_1;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001083 }
1084 }
1085
Guido van Rossumfa4ac711998-07-10 17:37:30 +00001086 if (i < len && PyList_SetSlice(result, i, len, NULL) < 0)
1087 goto Fail_1;
1088
Guido van Rossum79f25d91997-04-29 20:08:16 +00001089 PyMem_DEL(seqs);
Guido van Rossum12d12c51993-10-26 17:58:25 +00001090 return result;
1091
Guido van Rossum12d12c51993-10-26 17:58:25 +00001092Fail_1:
Guido van Rossum79f25d91997-04-29 20:08:16 +00001093 Py_DECREF(result);
Guido van Rossum12d12c51993-10-26 17:58:25 +00001094Fail_2:
Guido van Rossum79f25d91997-04-29 20:08:16 +00001095 if (seqs) PyMem_DEL(seqs);
Guido van Rossum12d12c51993-10-26 17:58:25 +00001096 return NULL;
1097}
1098
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001099static char map_doc[] =
1100"map(function, sequence[, sequence, ...]) -> list\n\
1101\n\
1102Return a list of the results of applying the function to the items of\n\
1103the argument sequence(s). If more than one sequence is given, the\n\
1104function is called with an argument list consisting of the corresponding\n\
1105item of each sequence, substituting None for missing values when not all\n\
1106sequences have the same length. If the function is None, return a list of\n\
1107the items of the sequence (or a list of tuples if more than one sequence).";
1108
1109
Guido van Rossum79f25d91997-04-29 20:08:16 +00001110static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001111builtin_setattr(PyObject *self, PyObject *args)
Guido van Rossum33894be1992-01-27 16:53:09 +00001112{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001113 PyObject *v;
1114 PyObject *name;
1115 PyObject *value;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001116
Marc-André Lemburg691270f2000-09-18 16:22:27 +00001117 if (!PyArg_ParseTuple(args, "OOO:setattr", &v, &name, &value))
Guido van Rossum33894be1992-01-27 16:53:09 +00001118 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001119 if (PyObject_SetAttr(v, name, value) != 0)
Guido van Rossum33894be1992-01-27 16:53:09 +00001120 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001121 Py_INCREF(Py_None);
1122 return Py_None;
Guido van Rossum33894be1992-01-27 16:53:09 +00001123}
1124
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001125static char setattr_doc[] =
1126"setattr(object, name, value)\n\
1127\n\
1128Set a named attribute on an object; setattr(x, 'y', v) is equivalent to\n\
1129``x.y = v''.";
1130
1131
Guido van Rossum79f25d91997-04-29 20:08:16 +00001132static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001133builtin_delattr(PyObject *self, PyObject *args)
Guido van Rossum14144fc1994-08-29 12:53:40 +00001134{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001135 PyObject *v;
1136 PyObject *name;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001137
Marc-André Lemburg691270f2000-09-18 16:22:27 +00001138 if (!PyArg_ParseTuple(args, "OO:delattr", &v, &name))
Guido van Rossum14144fc1994-08-29 12:53:40 +00001139 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001140 if (PyObject_SetAttr(v, name, (PyObject *)NULL) != 0)
Guido van Rossum14144fc1994-08-29 12:53:40 +00001141 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001142 Py_INCREF(Py_None);
1143 return Py_None;
Guido van Rossum14144fc1994-08-29 12:53:40 +00001144}
1145
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001146static char delattr_doc[] =
Guido van Rossumdf12a591998-11-23 22:13:04 +00001147"delattr(object, name)\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001148\n\
1149Delete a named attribute on an object; delattr(x, 'y') is equivalent to\n\
1150``del x.y''.";
1151
1152
Guido van Rossum79f25d91997-04-29 20:08:16 +00001153static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001154builtin_hash(PyObject *self, PyObject *args)
Guido van Rossum9bfef441993-03-29 10:43:31 +00001155{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001156 PyObject *v;
Guido van Rossum9bfef441993-03-29 10:43:31 +00001157 long x;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001158
Guido van Rossum79f25d91997-04-29 20:08:16 +00001159 if (!PyArg_ParseTuple(args, "O:hash", &v))
Guido van Rossum9bfef441993-03-29 10:43:31 +00001160 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001161 x = PyObject_Hash(v);
Guido van Rossum9bfef441993-03-29 10:43:31 +00001162 if (x == -1)
1163 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001164 return PyInt_FromLong(x);
Guido van Rossum9bfef441993-03-29 10:43:31 +00001165}
1166
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001167static char hash_doc[] =
1168"hash(object) -> integer\n\
1169\n\
1170Return a hash value for the object. Two objects with the same value have\n\
1171the same hash value. The reverse is not necessarily true, but likely.";
1172
1173
Guido van Rossum79f25d91997-04-29 20:08:16 +00001174static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001175builtin_hex(PyObject *self, PyObject *args)
Guido van Rossum006bcd41991-10-24 14:54:44 +00001176{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001177 PyObject *v;
1178 PyNumberMethods *nb;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001179
Guido van Rossum79f25d91997-04-29 20:08:16 +00001180 if (!PyArg_ParseTuple(args, "O:hex", &v))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001181 return NULL;
Guido van Rossumad991772001-01-12 16:03:05 +00001182
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001183 if ((nb = v->ob_type->tp_as_number) == NULL ||
Guido van Rossum1899c2e1992-09-12 11:09:23 +00001184 nb->nb_hex == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001185 PyErr_SetString(PyExc_TypeError,
Guido van Rossum1899c2e1992-09-12 11:09:23 +00001186 "hex() argument can't be converted to hex");
1187 return NULL;
Guido van Rossum006bcd41991-10-24 14:54:44 +00001188 }
Guido van Rossum1899c2e1992-09-12 11:09:23 +00001189 return (*nb->nb_hex)(v);
Guido van Rossum006bcd41991-10-24 14:54:44 +00001190}
1191
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001192static char hex_doc[] =
1193"hex(number) -> string\n\
1194\n\
1195Return the hexadecimal representation of an integer or long integer.";
1196
1197
Tim Petersdbd9ba62000-07-09 03:09:57 +00001198static PyObject *builtin_raw_input(PyObject *, PyObject *);
Guido van Rossum3165fe61992-09-25 21:59:05 +00001199
Guido van Rossum79f25d91997-04-29 20:08:16 +00001200static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001201builtin_input(PyObject *self, PyObject *args)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001202{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001203 PyObject *line;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001204 char *str;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001205 PyObject *res;
1206 PyObject *globals, *locals;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001207
1208 line = builtin_raw_input(self, args);
Guido van Rossum3165fe61992-09-25 21:59:05 +00001209 if (line == NULL)
1210 return line;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001211 if (!PyArg_Parse(line, "s;embedded '\\0' in input line", &str))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001212 return NULL;
1213 while (*str == ' ' || *str == '\t')
1214 str++;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001215 globals = PyEval_GetGlobals();
1216 locals = PyEval_GetLocals();
1217 if (PyDict_GetItemString(globals, "__builtins__") == NULL) {
1218 if (PyDict_SetItemString(globals, "__builtins__",
1219 PyEval_GetBuiltins()) != 0)
Guido van Rossum6135a871995-01-09 17:53:26 +00001220 return NULL;
1221 }
Guido van Rossumb05a5c71997-05-07 17:46:13 +00001222 res = PyRun_String(str, Py_eval_input, globals, locals);
Guido van Rossum79f25d91997-04-29 20:08:16 +00001223 Py_DECREF(line);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001224 return res;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001225}
1226
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001227static char input_doc[] =
1228"input([prompt]) -> value\n\
1229\n\
1230Equivalent to eval(raw_input(prompt)).";
1231
1232
Guido van Rossume8811f81997-02-14 15:48:05 +00001233static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001234builtin_intern(PyObject *self, PyObject *args)
Guido van Rossume8811f81997-02-14 15:48:05 +00001235{
1236 PyObject *s;
Guido van Rossum43713e52000-02-29 13:59:29 +00001237 if (!PyArg_ParseTuple(args, "S:intern", &s))
Guido van Rossume8811f81997-02-14 15:48:05 +00001238 return NULL;
1239 Py_INCREF(s);
1240 PyString_InternInPlace(&s);
1241 return s;
1242}
1243
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001244static char intern_doc[] =
1245"intern(string) -> string\n\
1246\n\
1247``Intern'' the given string. This enters the string in the (global)\n\
1248table of interned strings whose purpose is to speed up dictionary lookups.\n\
1249Return the string itself or the previously interned string object with the\n\
1250same value.";
1251
1252
Guido van Rossum79f25d91997-04-29 20:08:16 +00001253static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001254builtin_int(PyObject *self, PyObject *args)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001255{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001256 PyObject *v;
Barry Warsaw226ae6c1999-10-12 19:54:53 +00001257 int base = -909; /* unlikely! */
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001258
Barry Warsaw226ae6c1999-10-12 19:54:53 +00001259 if (!PyArg_ParseTuple(args, "O|i:int", &v, &base))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001260 return NULL;
Barry Warsaw226ae6c1999-10-12 19:54:53 +00001261 if (base == -909)
1262 return PyNumber_Int(v);
Guido van Rossum9e896b32000-04-05 20:11:21 +00001263 else if (PyString_Check(v))
1264 return PyInt_FromString(PyString_AS_STRING(v), NULL, base);
1265 else if (PyUnicode_Check(v))
1266 return PyInt_FromUnicode(PyUnicode_AS_UNICODE(v),
1267 PyUnicode_GET_SIZE(v),
1268 base);
1269 else {
Barry Warsaw226ae6c1999-10-12 19:54:53 +00001270 PyErr_SetString(PyExc_TypeError,
Fred Drake661ea262000-10-24 19:57:45 +00001271 "int() can't convert non-string with explicit base");
Barry Warsaw226ae6c1999-10-12 19:54:53 +00001272 return NULL;
1273 }
Guido van Rossum3f5da241990-12-20 15:06:42 +00001274}
1275
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001276static char int_doc[] =
Barry Warsaw226ae6c1999-10-12 19:54:53 +00001277"int(x[, base]) -> integer\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001278\n\
Barry Warsaw226ae6c1999-10-12 19:54:53 +00001279Convert a string or number to an integer, if possible. A floating point\n\
1280argument will be truncated towards zero (this does not include a string\n\
1281representation of a floating point number!) When converting a string, use\n\
1282the optional base. It is an error to supply a base when converting a\n\
1283non-string.";
1284
1285
1286static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001287builtin_long(PyObject *self, PyObject *args)
Barry Warsaw226ae6c1999-10-12 19:54:53 +00001288{
1289 PyObject *v;
1290 int base = -909; /* unlikely! */
Guido van Rossumad991772001-01-12 16:03:05 +00001291
Barry Warsaw226ae6c1999-10-12 19:54:53 +00001292 if (!PyArg_ParseTuple(args, "O|i:long", &v, &base))
1293 return NULL;
1294 if (base == -909)
1295 return PyNumber_Long(v);
Guido van Rossum9e896b32000-04-05 20:11:21 +00001296 else if (PyString_Check(v))
1297 return PyLong_FromString(PyString_AS_STRING(v), NULL, base);
1298 else if (PyUnicode_Check(v))
1299 return PyLong_FromUnicode(PyUnicode_AS_UNICODE(v),
1300 PyUnicode_GET_SIZE(v),
1301 base);
1302 else {
Barry Warsaw226ae6c1999-10-12 19:54:53 +00001303 PyErr_SetString(PyExc_TypeError,
Fred Drake661ea262000-10-24 19:57:45 +00001304 "long() can't convert non-string with explicit base");
Barry Warsaw226ae6c1999-10-12 19:54:53 +00001305 return NULL;
1306 }
Barry Warsaw226ae6c1999-10-12 19:54:53 +00001307}
1308
1309static char long_doc[] =
1310"long(x) -> long integer\n\
1311long(x, base) -> long integer\n\
1312\n\
1313Convert a string or number to a long integer, if possible. A floating\n\
1314point argument will be truncated towards zero (this does not include a\n\
1315string representation of a floating point number!) When converting a\n\
1316string, use the given base. It is an error to supply a base when\n\
1317converting a non-string.";
1318
1319
1320static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001321builtin_float(PyObject *self, PyObject *args)
Barry Warsaw226ae6c1999-10-12 19:54:53 +00001322{
1323 PyObject *v;
1324
1325 if (!PyArg_ParseTuple(args, "O:float", &v))
1326 return NULL;
1327 if (PyString_Check(v))
1328 return PyFloat_FromString(v, NULL);
1329 return PyNumber_Float(v);
1330}
1331
1332static char float_doc[] =
1333"float(x) -> floating point number\n\
1334\n\
1335Convert a string or number to a floating point number, if possible.";
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001336
1337
Guido van Rossum79f25d91997-04-29 20:08:16 +00001338static PyObject *
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001339builtin_iter(PyObject *self, PyObject *args)
1340{
1341 PyObject *v, *w = NULL;
1342
1343 if (!PyArg_ParseTuple(args, "O|O:iter", &v, &w))
1344 return NULL;
1345 if (w == NULL)
1346 return PyObject_GetIter(v);
1347 if (!PyCallable_Check(v)) {
1348 PyErr_SetString(PyExc_TypeError,
1349 "iter(v, w): v must be callable");
1350 return NULL;
1351 }
1352 return PyCallIter_New(v, w);
1353}
1354
1355static char iter_doc[] =
1356"iter(collection) -> iterator\n\
1357iter(callable, sentinel) -> iterator\n\
1358\n\
1359Get an iterator from an object. In the first form, the argument must\n\
1360supply its own iterator, or be a sequence.\n\
1361In the second form, the callable is called until it returns the sentinel.";
1362
1363
1364static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001365builtin_len(PyObject *self, PyObject *args)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001366{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001367 PyObject *v;
Guido van Rossum8ea9f4d1998-06-29 22:26:50 +00001368 long res;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001369
Guido van Rossum79f25d91997-04-29 20:08:16 +00001370 if (!PyArg_ParseTuple(args, "O:len", &v))
Guido van Rossum3f5da241990-12-20 15:06:42 +00001371 return NULL;
Jeremy Hylton03657cf2000-07-12 13:05:33 +00001372 res = PyObject_Size(v);
Guido van Rossum8ea9f4d1998-06-29 22:26:50 +00001373 if (res < 0 && PyErr_Occurred())
1374 return NULL;
1375 return PyInt_FromLong(res);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001376}
1377
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001378static char len_doc[] =
1379"len(object) -> integer\n\
1380\n\
1381Return the number of items of a sequence or mapping.";
1382
1383
Guido van Rossum79f25d91997-04-29 20:08:16 +00001384static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001385builtin_list(PyObject *self, PyObject *args)
Guido van Rossumd1705771996-04-09 02:41:06 +00001386{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001387 PyObject *v;
Guido van Rossumd1705771996-04-09 02:41:06 +00001388
Guido van Rossum79f25d91997-04-29 20:08:16 +00001389 if (!PyArg_ParseTuple(args, "O:list", &v))
Guido van Rossumd1705771996-04-09 02:41:06 +00001390 return NULL;
Guido van Rossum09df08a1998-05-22 00:51:39 +00001391 return PySequence_List(v);
Guido van Rossumd1705771996-04-09 02:41:06 +00001392}
1393
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001394static char list_doc[] =
1395"list(sequence) -> list\n\
1396\n\
1397Return a new list whose items are the same as those of the argument sequence.";
1398
Guido van Rossum8861b741996-07-30 16:49:37 +00001399
1400static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001401builtin_slice(PyObject *self, PyObject *args)
Guido van Rossum8861b741996-07-30 16:49:37 +00001402{
Guido van Rossum09df08a1998-05-22 00:51:39 +00001403 PyObject *start, *stop, *step;
Guido van Rossum8861b741996-07-30 16:49:37 +00001404
Guido van Rossum09df08a1998-05-22 00:51:39 +00001405 start = stop = step = NULL;
Guido van Rossum8861b741996-07-30 16:49:37 +00001406
Guido van Rossum09df08a1998-05-22 00:51:39 +00001407 if (!PyArg_ParseTuple(args, "O|OO:slice", &start, &stop, &step))
1408 return NULL;
Guido van Rossum8861b741996-07-30 16:49:37 +00001409
Guido van Rossum09df08a1998-05-22 00:51:39 +00001410 /* This swapping of stop and start is to maintain similarity with
1411 range(). */
1412 if (stop == NULL) {
1413 stop = start;
1414 start = NULL;
1415 }
1416 return PySlice_New(start, stop, step);
Guido van Rossum8861b741996-07-30 16:49:37 +00001417}
1418
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001419static char slice_doc[] =
Fred Drake3d587441999-07-19 15:21:16 +00001420"slice([start,] stop[, step]) -> slice object\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001421\n\
1422Create a slice object. This is used for slicing by the Numeric extensions.";
1423
1424
Guido van Rossum79f25d91997-04-29 20:08:16 +00001425static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001426builtin_locals(PyObject *self, PyObject *args)
Guido van Rossum872537c1995-07-07 22:43:42 +00001427{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001428 PyObject *d;
Guido van Rossum872537c1995-07-07 22:43:42 +00001429
Guido van Rossum43713e52000-02-29 13:59:29 +00001430 if (!PyArg_ParseTuple(args, ":locals"))
Guido van Rossum872537c1995-07-07 22:43:42 +00001431 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001432 d = PyEval_GetLocals();
1433 Py_INCREF(d);
Guido van Rossum872537c1995-07-07 22:43:42 +00001434 return d;
1435}
1436
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001437static char locals_doc[] =
1438"locals() -> dictionary\n\
1439\n\
1440Return the dictionary containing the current scope's local variables.";
1441
1442
Guido van Rossum79f25d91997-04-29 20:08:16 +00001443static PyObject *
Guido van Rossum53451b32001-01-17 15:47:24 +00001444min_max(PyObject *args, int op)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001445{
Guido van Rossum2d951851994-08-29 12:52:16 +00001446 int i;
Tim Petersc3074532001-05-03 07:00:32 +00001447 PyObject *v, *w, *x, *it;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001448
Guido van Rossum79f25d91997-04-29 20:08:16 +00001449 if (PyTuple_Size(args) > 1)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001450 v = args;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001451 else if (!PyArg_ParseTuple(args, "O:min/max", &v))
Guido van Rossum3f5da241990-12-20 15:06:42 +00001452 return NULL;
Tim Petersc3074532001-05-03 07:00:32 +00001453
1454 it = PyObject_GetIter(v);
1455 if (it == NULL)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001456 return NULL;
Tim Petersc3074532001-05-03 07:00:32 +00001457
1458 w = NULL; /* the result */
Guido van Rossum2d951851994-08-29 12:52:16 +00001459 for (i = 0; ; i++) {
Tim Petersc3074532001-05-03 07:00:32 +00001460 x = PyIter_Next(it);
Guido van Rossum2d951851994-08-29 12:52:16 +00001461 if (x == NULL) {
Tim Petersc3074532001-05-03 07:00:32 +00001462 /* We're out of here in any case, but if this is a
1463 * StopIteration exception it's expected, but if
1464 * any other kind of exception it's an error.
1465 */
1466 if (PyErr_Occurred()) {
1467 if (PyErr_ExceptionMatches(PyExc_StopIteration))
1468 PyErr_Clear();
1469 else {
1470 Py_XDECREF(w);
1471 Py_DECREF(it);
1472 return NULL;
1473 }
Guido van Rossum2d951851994-08-29 12:52:16 +00001474 }
Tim Petersc3074532001-05-03 07:00:32 +00001475 break;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001476 }
Tim Petersc3074532001-05-03 07:00:32 +00001477
Guido van Rossum2d951851994-08-29 12:52:16 +00001478 if (w == NULL)
1479 w = x;
1480 else {
Guido van Rossum53451b32001-01-17 15:47:24 +00001481 int cmp = PyObject_RichCompareBool(x, w, op);
1482 if (cmp > 0) {
1483 Py_DECREF(w);
1484 w = x;
1485 }
1486 else if (cmp < 0) {
Guido van Rossumc8b6df91997-05-23 00:06:51 +00001487 Py_DECREF(x);
Tim Petersc3074532001-05-03 07:00:32 +00001488 Py_DECREF(w);
1489 Py_DECREF(it);
Guido van Rossumc8b6df91997-05-23 00:06:51 +00001490 return NULL;
1491 }
Guido van Rossum2d951851994-08-29 12:52:16 +00001492 else
Guido van Rossum79f25d91997-04-29 20:08:16 +00001493 Py_DECREF(x);
Guido van Rossum2d951851994-08-29 12:52:16 +00001494 }
Guido van Rossum3f5da241990-12-20 15:06:42 +00001495 }
Guido van Rossum2d951851994-08-29 12:52:16 +00001496 if (w == NULL)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001497 PyErr_SetString(PyExc_ValueError,
Fred Drake661ea262000-10-24 19:57:45 +00001498 "min() or max() arg is an empty sequence");
Tim Petersc3074532001-05-03 07:00:32 +00001499 Py_DECREF(it);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001500 return w;
1501}
1502
Guido van Rossum79f25d91997-04-29 20:08:16 +00001503static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001504builtin_min(PyObject *self, PyObject *v)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001505{
Guido van Rossum53451b32001-01-17 15:47:24 +00001506 return min_max(v, Py_LT);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001507}
1508
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001509static char min_doc[] =
1510"min(sequence) -> value\n\
1511min(a, b, c, ...) -> value\n\
1512\n\
1513With a single sequence argument, return its smallest item.\n\
1514With two or more arguments, return the smallest argument.";
1515
1516
Guido van Rossum79f25d91997-04-29 20:08:16 +00001517static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001518builtin_max(PyObject *self, PyObject *v)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001519{
Guido van Rossum53451b32001-01-17 15:47:24 +00001520 return min_max(v, Py_GT);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001521}
1522
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001523static char max_doc[] =
1524"max(sequence) -> value\n\
1525max(a, b, c, ...) -> value\n\
1526\n\
1527With a single sequence argument, return its largest item.\n\
1528With two or more arguments, return the largest argument.";
1529
1530
Guido van Rossum79f25d91997-04-29 20:08:16 +00001531static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001532builtin_oct(PyObject *self, PyObject *args)
Guido van Rossum006bcd41991-10-24 14:54:44 +00001533{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001534 PyObject *v;
1535 PyNumberMethods *nb;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001536
Guido van Rossum79f25d91997-04-29 20:08:16 +00001537 if (!PyArg_ParseTuple(args, "O:oct", &v))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001538 return NULL;
Guido van Rossum1899c2e1992-09-12 11:09:23 +00001539 if (v == NULL || (nb = v->ob_type->tp_as_number) == NULL ||
1540 nb->nb_oct == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001541 PyErr_SetString(PyExc_TypeError,
Guido van Rossum1899c2e1992-09-12 11:09:23 +00001542 "oct() argument can't be converted to oct");
1543 return NULL;
Guido van Rossum006bcd41991-10-24 14:54:44 +00001544 }
Guido van Rossum1899c2e1992-09-12 11:09:23 +00001545 return (*nb->nb_oct)(v);
Guido van Rossum006bcd41991-10-24 14:54:44 +00001546}
1547
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001548static char oct_doc[] =
1549"oct(number) -> string\n\
1550\n\
1551Return the octal representation of an integer or long integer.";
1552
1553
Guido van Rossum79f25d91997-04-29 20:08:16 +00001554static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001555builtin_open(PyObject *self, PyObject *args)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001556{
Guido van Rossum2d951851994-08-29 12:52:16 +00001557 char *name;
1558 char *mode = "r";
1559 int bufsize = -1;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001560 PyObject *f;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001561
Guido van Rossum79f25d91997-04-29 20:08:16 +00001562 if (!PyArg_ParseTuple(args, "s|si:open", &name, &mode, &bufsize))
Guido van Rossum3f5da241990-12-20 15:06:42 +00001563 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001564 f = PyFile_FromString(name, mode);
Guido van Rossum2d951851994-08-29 12:52:16 +00001565 if (f != NULL)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001566 PyFile_SetBufSize(f, bufsize);
Guido van Rossum2d951851994-08-29 12:52:16 +00001567 return f;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001568}
1569
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001570static char open_doc[] =
1571"open(filename[, mode[, buffering]]) -> file object\n\
1572\n\
1573Open a file. The mode can be 'r', 'w' or 'a' for reading (default),\n\
1574writing or appending. The file will be created if it doesn't exist\n\
1575when opened for writing or appending; it will be truncated when\n\
1576opened for writing. Add a 'b' to the mode for binary files.\n\
1577Add a '+' to the mode to allow simultaneous reading and writing.\n\
1578If the buffering argument is given, 0 means unbuffered, 1 means line\n\
1579buffered, and larger numbers specify the buffer size.";
1580
1581
Guido van Rossum79f25d91997-04-29 20:08:16 +00001582static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001583builtin_ord(PyObject *self, PyObject *args)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001584{
Guido van Rossum09095f32000-03-10 23:00:52 +00001585 PyObject *obj;
1586 long ord;
Jeremy Hylton394b54d2000-04-12 21:19:47 +00001587 int size;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001588
Guido van Rossum09095f32000-03-10 23:00:52 +00001589 if (!PyArg_ParseTuple(args, "O:ord", &obj))
Guido van Rossum3f5da241990-12-20 15:06:42 +00001590 return NULL;
Guido van Rossum09095f32000-03-10 23:00:52 +00001591
Jeremy Hylton394b54d2000-04-12 21:19:47 +00001592 if (PyString_Check(obj)) {
1593 size = PyString_GET_SIZE(obj);
Andrew M. Kuchling34c20cf2000-12-20 14:36:56 +00001594 if (size == 1) {
Jeremy Hylton394b54d2000-04-12 21:19:47 +00001595 ord = (long)((unsigned char)*PyString_AS_STRING(obj));
Andrew M. Kuchling34c20cf2000-12-20 14:36:56 +00001596 return PyInt_FromLong(ord);
1597 }
Jeremy Hylton394b54d2000-04-12 21:19:47 +00001598 } else if (PyUnicode_Check(obj)) {
1599 size = PyUnicode_GET_SIZE(obj);
Andrew M. Kuchling34c20cf2000-12-20 14:36:56 +00001600 if (size == 1) {
Jeremy Hylton394b54d2000-04-12 21:19:47 +00001601 ord = (long)*PyUnicode_AS_UNICODE(obj);
Andrew M. Kuchling34c20cf2000-12-20 14:36:56 +00001602 return PyInt_FromLong(ord);
1603 }
Jeremy Hylton394b54d2000-04-12 21:19:47 +00001604 } else {
1605 PyErr_Format(PyExc_TypeError,
Andrew M. Kuchlingf07aad12000-12-23 14:11:28 +00001606 "ord() expected string of length 1, but " \
Jeremy Hylton394b54d2000-04-12 21:19:47 +00001607 "%.200s found", obj->ob_type->tp_name);
Guido van Rossum09095f32000-03-10 23:00:52 +00001608 return NULL;
1609 }
1610
Guido van Rossumad991772001-01-12 16:03:05 +00001611 PyErr_Format(PyExc_TypeError,
Andrew M. Kuchlingf07aad12000-12-23 14:11:28 +00001612 "ord() expected a character, "
1613 "but string of length %d found",
Jeremy Hylton394b54d2000-04-12 21:19:47 +00001614 size);
1615 return NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001616}
1617
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001618static char ord_doc[] =
1619"ord(c) -> integer\n\
1620\n\
Guido van Rossumad991772001-01-12 16:03:05 +00001621Return the integer ordinal of a one-character string.";
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001622
1623
Guido van Rossum79f25d91997-04-29 20:08:16 +00001624static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001625builtin_pow(PyObject *self, PyObject *args)
Guido van Rossum6a00cd81995-01-07 12:39:01 +00001626{
Guido van Rossum09df08a1998-05-22 00:51:39 +00001627 PyObject *v, *w, *z = Py_None;
Guido van Rossum6a00cd81995-01-07 12:39:01 +00001628
Guido van Rossum79f25d91997-04-29 20:08:16 +00001629 if (!PyArg_ParseTuple(args, "OO|O:pow", &v, &w, &z))
Guido van Rossum6a00cd81995-01-07 12:39:01 +00001630 return NULL;
Guido van Rossum09df08a1998-05-22 00:51:39 +00001631 return PyNumber_Power(v, w, z);
Guido van Rossumd4905451991-05-05 20:00:36 +00001632}
1633
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001634static char pow_doc[] =
1635"pow(x, y[, z]) -> number\n\
1636\n\
1637With two arguments, equivalent to x**y. With three arguments,\n\
1638equivalent to (x**y) % z, but may be more efficient (e.g. for longs).";
1639
1640
Guido van Rossum124eff01999-02-23 16:11:01 +00001641/* Return number of items in range/xrange (lo, hi, step). step > 0
1642 * required. Return a value < 0 if & only if the true value is too
1643 * large to fit in a signed long.
1644 */
1645static long
Thomas Wouterse28c2962000-07-23 22:21:32 +00001646get_len_of_range(long lo, long hi, long step)
Guido van Rossum124eff01999-02-23 16:11:01 +00001647{
1648 /* -------------------------------------------------------------
1649 If lo >= hi, the range is empty.
1650 Else if n values are in the range, the last one is
1651 lo + (n-1)*step, which must be <= hi-1. Rearranging,
1652 n <= (hi - lo - 1)/step + 1, so taking the floor of the RHS gives
1653 the proper value. Since lo < hi in this case, hi-lo-1 >= 0, so
1654 the RHS is non-negative and so truncation is the same as the
1655 floor. Letting M be the largest positive long, the worst case
1656 for the RHS numerator is hi=M, lo=-M-1, and then
1657 hi-lo-1 = M-(-M-1)-1 = 2*M. Therefore unsigned long has enough
1658 precision to compute the RHS exactly.
1659 ---------------------------------------------------------------*/
1660 long n = 0;
1661 if (lo < hi) {
1662 unsigned long uhi = (unsigned long)hi;
1663 unsigned long ulo = (unsigned long)lo;
1664 unsigned long diff = uhi - ulo - 1;
1665 n = (long)(diff / (unsigned long)step + 1);
1666 }
1667 return n;
1668}
1669
Guido van Rossum79f25d91997-04-29 20:08:16 +00001670static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001671builtin_range(PyObject *self, PyObject *args)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001672{
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001673 long ilow = 0, ihigh = 0, istep = 1;
Guido van Rossum124eff01999-02-23 16:11:01 +00001674 long bign;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001675 int i, n;
Guido van Rossum124eff01999-02-23 16:11:01 +00001676
Guido van Rossum79f25d91997-04-29 20:08:16 +00001677 PyObject *v;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001678
Guido van Rossum79f25d91997-04-29 20:08:16 +00001679 if (PyTuple_Size(args) <= 1) {
1680 if (!PyArg_ParseTuple(args,
Guido van Rossum0865dd91995-01-17 16:30:22 +00001681 "l;range() requires 1-3 int arguments",
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001682 &ihigh))
1683 return NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001684 }
1685 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001686 if (!PyArg_ParseTuple(args,
Guido van Rossum0865dd91995-01-17 16:30:22 +00001687 "ll|l;range() requires 1-3 int arguments",
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001688 &ilow, &ihigh, &istep))
Guido van Rossum3f5da241990-12-20 15:06:42 +00001689 return NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001690 }
1691 if (istep == 0) {
Fred Drake661ea262000-10-24 19:57:45 +00001692 PyErr_SetString(PyExc_ValueError, "range() arg 3 must not be zero");
Guido van Rossum3f5da241990-12-20 15:06:42 +00001693 return NULL;
1694 }
Guido van Rossum124eff01999-02-23 16:11:01 +00001695 if (istep > 0)
1696 bign = get_len_of_range(ilow, ihigh, istep);
1697 else
1698 bign = get_len_of_range(ihigh, ilow, -istep);
1699 n = (int)bign;
1700 if (bign < 0 || (long)n != bign) {
Guido van Rossume23cde21999-01-12 05:07:47 +00001701 PyErr_SetString(PyExc_OverflowError,
Fred Drake661ea262000-10-24 19:57:45 +00001702 "range() result has too many items");
Guido van Rossume23cde21999-01-12 05:07:47 +00001703 return NULL;
1704 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00001705 v = PyList_New(n);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001706 if (v == NULL)
1707 return NULL;
1708 for (i = 0; i < n; i++) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001709 PyObject *w = PyInt_FromLong(ilow);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001710 if (w == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001711 Py_DECREF(v);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001712 return NULL;
1713 }
Guido van Rossuma937d141998-04-24 18:22:02 +00001714 PyList_SET_ITEM(v, i, w);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001715 ilow += istep;
1716 }
1717 return v;
1718}
1719
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001720static char range_doc[] =
1721"range([start,] stop[, step]) -> list of integers\n\
1722\n\
1723Return a list containing an arithmetic progression of integers.\n\
1724range(i, j) returns [i, i+1, i+2, ..., j-1]; start (!) defaults to 0.\n\
1725When step is given, it specifies the increment (or decrement).\n\
1726For example, range(4) returns [0, 1, 2, 3]. The end point is omitted!\n\
1727These are exactly the valid indices for a list of 4 elements.";
1728
1729
Guido van Rossum79f25d91997-04-29 20:08:16 +00001730static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001731builtin_xrange(PyObject *self, PyObject *args)
Guido van Rossum12d12c51993-10-26 17:58:25 +00001732{
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001733 long ilow = 0, ihigh = 0, istep = 1;
Guido van Rossum0865dd91995-01-17 16:30:22 +00001734 long n;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001735
Guido van Rossum79f25d91997-04-29 20:08:16 +00001736 if (PyTuple_Size(args) <= 1) {
1737 if (!PyArg_ParseTuple(args,
Guido van Rossum0865dd91995-01-17 16:30:22 +00001738 "l;xrange() requires 1-3 int arguments",
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001739 &ihigh))
1740 return NULL;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001741 }
1742 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001743 if (!PyArg_ParseTuple(args,
Guido van Rossum0865dd91995-01-17 16:30:22 +00001744 "ll|l;xrange() requires 1-3 int arguments",
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001745 &ilow, &ihigh, &istep))
Guido van Rossum12d12c51993-10-26 17:58:25 +00001746 return NULL;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001747 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001748 if (istep == 0) {
Fred Drake661ea262000-10-24 19:57:45 +00001749 PyErr_SetString(PyExc_ValueError, "xrange() arg 3 must not be zero");
Guido van Rossum12d12c51993-10-26 17:58:25 +00001750 return NULL;
1751 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001752 if (istep > 0)
Guido van Rossum124eff01999-02-23 16:11:01 +00001753 n = get_len_of_range(ilow, ihigh, istep);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001754 else
Guido van Rossum124eff01999-02-23 16:11:01 +00001755 n = get_len_of_range(ihigh, ilow, -istep);
1756 if (n < 0) {
1757 PyErr_SetString(PyExc_OverflowError,
Fred Drake661ea262000-10-24 19:57:45 +00001758 "xrange() result has too many items");
Guido van Rossum124eff01999-02-23 16:11:01 +00001759 return NULL;
1760 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00001761 return PyRange_New(ilow, n, istep, 1);
Guido van Rossum12d12c51993-10-26 17:58:25 +00001762}
1763
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001764static char xrange_doc[] =
1765"xrange([start,] stop[, step]) -> xrange object\n\
1766\n\
1767Like range(), but instead of returning a list, returns an object that\n\
1768generates the numbers in the range on demand. This is slightly slower\n\
1769than range() but more memory efficient.";
1770
1771
Guido van Rossum79f25d91997-04-29 20:08:16 +00001772static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001773builtin_raw_input(PyObject *self, PyObject *args)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001774{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001775 PyObject *v = NULL;
1776 PyObject *f;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001777
Guido van Rossum79f25d91997-04-29 20:08:16 +00001778 if (!PyArg_ParseTuple(args, "|O:[raw_]input", &v))
Guido van Rossum3165fe61992-09-25 21:59:05 +00001779 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001780 if (PyFile_AsFile(PySys_GetObject("stdin")) == stdin &&
1781 PyFile_AsFile(PySys_GetObject("stdout")) == stdout &&
Guido van Rossum53bb7ff1995-07-26 16:26:31 +00001782 isatty(fileno(stdin)) && isatty(fileno(stdout))) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001783 PyObject *po;
Guido van Rossum872537c1995-07-07 22:43:42 +00001784 char *prompt;
1785 char *s;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001786 PyObject *result;
Guido van Rossum872537c1995-07-07 22:43:42 +00001787 if (v != NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001788 po = PyObject_Str(v);
Guido van Rossum872537c1995-07-07 22:43:42 +00001789 if (po == NULL)
1790 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001791 prompt = PyString_AsString(po);
Guido van Rossumd9b52081998-06-26 18:25:38 +00001792 if (prompt == NULL)
1793 return NULL;
Guido van Rossum872537c1995-07-07 22:43:42 +00001794 }
1795 else {
1796 po = NULL;
1797 prompt = "";
1798 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00001799 s = PyOS_Readline(prompt);
1800 Py_XDECREF(po);
Guido van Rossum872537c1995-07-07 22:43:42 +00001801 if (s == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001802 PyErr_SetNone(PyExc_KeyboardInterrupt);
Guido van Rossum872537c1995-07-07 22:43:42 +00001803 return NULL;
1804 }
1805 if (*s == '\0') {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001806 PyErr_SetNone(PyExc_EOFError);
Guido van Rossum872537c1995-07-07 22:43:42 +00001807 result = NULL;
1808 }
1809 else { /* strip trailing '\n' */
Guido van Rossum106f2da2000-06-28 21:12:25 +00001810 size_t len = strlen(s);
1811 if (len > INT_MAX) {
1812 PyErr_SetString(PyExc_OverflowError, "input too long");
1813 result = NULL;
1814 }
1815 else {
1816 result = PyString_FromStringAndSize(s, (int)(len-1));
1817 }
Guido van Rossum872537c1995-07-07 22:43:42 +00001818 }
Guido van Rossumb18618d2000-05-03 23:44:39 +00001819 PyMem_FREE(s);
Guido van Rossum872537c1995-07-07 22:43:42 +00001820 return result;
1821 }
Guido van Rossum90933611991-06-07 16:10:43 +00001822 if (v != NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001823 f = PySys_GetObject("stdout");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001824 if (f == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001825 PyErr_SetString(PyExc_RuntimeError, "lost sys.stdout");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001826 return NULL;
1827 }
Guido van Rossumc8b6df91997-05-23 00:06:51 +00001828 if (Py_FlushLine() != 0 ||
1829 PyFile_WriteObject(v, f, Py_PRINT_RAW) != 0)
Guido van Rossum90933611991-06-07 16:10:43 +00001830 return NULL;
1831 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00001832 f = PySys_GetObject("stdin");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001833 if (f == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001834 PyErr_SetString(PyExc_RuntimeError, "lost sys.stdin");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001835 return NULL;
1836 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00001837 return PyFile_GetLine(f, -1);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001838}
1839
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001840static char raw_input_doc[] =
1841"raw_input([prompt]) -> string\n\
1842\n\
1843Read a string from standard input. The trailing newline is stripped.\n\
1844If the user hits EOF (Unix: Ctl-D, Windows: Ctl-Z+Return), raise EOFError.\n\
1845On Unix, GNU readline is used if enabled. The prompt string, if given,\n\
1846is printed without a trailing newline before reading.";
1847
1848
Guido van Rossum79f25d91997-04-29 20:08:16 +00001849static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001850builtin_reduce(PyObject *self, PyObject *args)
Guido van Rossum12d12c51993-10-26 17:58:25 +00001851{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001852 PyObject *seq, *func, *result = NULL;
1853 PySequenceMethods *sqf;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001854 register int i;
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
Guido van Rossum09df08a1998-05-22 00:51:39 +00001861 sqf = seq->ob_type->tp_as_sequence;
1862 if (sqf == NULL || sqf->sq_item == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001863 PyErr_SetString(PyExc_TypeError,
Fred Drake661ea262000-10-24 19:57:45 +00001864 "reduce() arg 2 must be a sequence");
Guido van Rossum12d12c51993-10-26 17:58:25 +00001865 return NULL;
1866 }
1867
Guido van Rossum79f25d91997-04-29 20:08:16 +00001868 if ((args = PyTuple_New(2)) == NULL)
Guido van Rossum2d951851994-08-29 12:52:16 +00001869 goto Fail;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001870
Guido van Rossum2d951851994-08-29 12:52:16 +00001871 for (i = 0; ; ++i) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001872 PyObject *op2;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001873
1874 if (args->ob_refcnt > 1) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001875 Py_DECREF(args);
1876 if ((args = PyTuple_New(2)) == NULL)
Guido van Rossum2d951851994-08-29 12:52:16 +00001877 goto Fail;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001878 }
1879
Guido van Rossum2d951851994-08-29 12:52:16 +00001880 if ((op2 = (*sqf->sq_item)(seq, i)) == NULL) {
Barry Warsawcde8b1b1997-08-22 21:14:38 +00001881 if (PyErr_ExceptionMatches(PyExc_IndexError)) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001882 PyErr_Clear();
Guido van Rossum2d951851994-08-29 12:52:16 +00001883 break;
1884 }
1885 goto Fail;
1886 }
Guido van Rossum12d12c51993-10-26 17:58:25 +00001887
Guido van Rossum2d951851994-08-29 12:52:16 +00001888 if (result == NULL)
1889 result = op2;
1890 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001891 PyTuple_SetItem(args, 0, result);
1892 PyTuple_SetItem(args, 1, op2);
1893 if ((result = PyEval_CallObject(func, args)) == NULL)
Guido van Rossum2d951851994-08-29 12:52:16 +00001894 goto Fail;
1895 }
Guido van Rossum12d12c51993-10-26 17:58:25 +00001896 }
1897
Guido van Rossum79f25d91997-04-29 20:08:16 +00001898 Py_DECREF(args);
Guido van Rossum12d12c51993-10-26 17:58:25 +00001899
Guido van Rossum2d951851994-08-29 12:52:16 +00001900 if (result == NULL)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001901 PyErr_SetString(PyExc_TypeError,
Fred Drake661ea262000-10-24 19:57:45 +00001902 "reduce() of empty sequence with no initial value");
Guido van Rossum2d951851994-08-29 12:52:16 +00001903
Guido van Rossum12d12c51993-10-26 17:58:25 +00001904 return result;
1905
Guido van Rossum2d951851994-08-29 12:52:16 +00001906Fail:
Guido van Rossum79f25d91997-04-29 20:08:16 +00001907 Py_XDECREF(args);
1908 Py_XDECREF(result);
Guido van Rossum12d12c51993-10-26 17:58:25 +00001909 return NULL;
1910}
1911
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001912static char reduce_doc[] =
1913"reduce(function, sequence[, initial]) -> value\n\
1914\n\
1915Apply a function of two arguments cumulatively to the items of a sequence,\n\
1916from left to right, so as to reduce the sequence to a single value.\n\
1917For example, reduce(lambda x, y: x+y, [1, 2, 3, 4, 5]) calculates\n\
1918((((1+2)+3)+4)+5). If initial is present, it is placed before the items\n\
1919of the sequence in the calculation, and serves as a default when the\n\
1920sequence is empty.";
1921
1922
Guido van Rossum79f25d91997-04-29 20:08:16 +00001923static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001924builtin_reload(PyObject *self, PyObject *args)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001925{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001926 PyObject *v;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001927
Guido van Rossum79f25d91997-04-29 20:08:16 +00001928 if (!PyArg_ParseTuple(args, "O:reload", &v))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001929 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001930 return PyImport_ReloadModule(v);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001931}
1932
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001933static char reload_doc[] =
1934"reload(module) -> module\n\
1935\n\
1936Reload the module. The module must have been successfully imported before.";
1937
1938
Guido van Rossum79f25d91997-04-29 20:08:16 +00001939static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001940builtin_repr(PyObject *self, PyObject *args)
Guido van Rossumc89705d1992-11-26 08:54:07 +00001941{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001942 PyObject *v;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001943
Guido van Rossum79f25d91997-04-29 20:08:16 +00001944 if (!PyArg_ParseTuple(args, "O:repr", &v))
Guido van Rossumc89705d1992-11-26 08:54:07 +00001945 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001946 return PyObject_Repr(v);
Guido van Rossumc89705d1992-11-26 08:54:07 +00001947}
1948
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001949static char repr_doc[] =
1950"repr(object) -> string\n\
1951\n\
1952Return the canonical string representation of the object.\n\
1953For most object types, eval(repr(object)) == object.";
1954
1955
Guido van Rossum79f25d91997-04-29 20:08:16 +00001956static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001957builtin_round(PyObject *self, PyObject *args)
Guido van Rossum9e51f9b1993-02-12 16:29:05 +00001958{
Guido van Rossum9e51f9b1993-02-12 16:29:05 +00001959 double x;
1960 double f;
1961 int ndigits = 0;
Guido van Rossum9e51f9b1993-02-12 16:29:05 +00001962 int i;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001963
Guido van Rossum79f25d91997-04-29 20:08:16 +00001964 if (!PyArg_ParseTuple(args, "d|i:round", &x, &ndigits))
Guido van Rossum9e51f9b1993-02-12 16:29:05 +00001965 return NULL;
Guido van Rossum9e51f9b1993-02-12 16:29:05 +00001966 f = 1.0;
Guido van Rossum1e162d31998-05-09 14:42:25 +00001967 i = abs(ndigits);
1968 while (--i >= 0)
Guido van Rossum9e51f9b1993-02-12 16:29:05 +00001969 f = f*10.0;
Guido van Rossum1e162d31998-05-09 14:42:25 +00001970 if (ndigits < 0)
1971 x /= f;
Guido van Rossum9e51f9b1993-02-12 16:29:05 +00001972 else
Guido van Rossum1e162d31998-05-09 14:42:25 +00001973 x *= f;
1974 if (x >= 0.0)
1975 x = floor(x + 0.5);
1976 else
1977 x = ceil(x - 0.5);
1978 if (ndigits < 0)
1979 x *= f;
1980 else
1981 x /= f;
1982 return PyFloat_FromDouble(x);
Guido van Rossum9e51f9b1993-02-12 16:29:05 +00001983}
1984
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001985static char round_doc[] =
1986"round(number[, ndigits]) -> floating point number\n\
1987\n\
1988Round a number to a given precision in decimal digits (default 0 digits).\n\
1989This always returns a floating point number. Precision may be negative.";
1990
1991
Guido van Rossum79f25d91997-04-29 20:08:16 +00001992static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001993builtin_str(PyObject *self, PyObject *args)
Guido van Rossumc89705d1992-11-26 08:54:07 +00001994{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001995 PyObject *v;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001996
Guido van Rossum79f25d91997-04-29 20:08:16 +00001997 if (!PyArg_ParseTuple(args, "O:str", &v))
Guido van Rossumc89705d1992-11-26 08:54:07 +00001998 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001999 return PyObject_Str(v);
Guido van Rossumc89705d1992-11-26 08:54:07 +00002000}
2001
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002002static char str_doc[] =
2003"str(object) -> string\n\
2004\n\
2005Return a nice string representation of the object.\n\
2006If the argument is a string, the return value is the same object.";
2007
2008
Guido van Rossum79f25d91997-04-29 20:08:16 +00002009static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002010builtin_tuple(PyObject *self, PyObject *args)
Guido van Rossumcae027b1994-08-29 12:53:11 +00002011{
Guido van Rossum79f25d91997-04-29 20:08:16 +00002012 PyObject *v;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002013
Guido van Rossum79f25d91997-04-29 20:08:16 +00002014 if (!PyArg_ParseTuple(args, "O:tuple", &v))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002015 return NULL;
Guido van Rossum09df08a1998-05-22 00:51:39 +00002016 return PySequence_Tuple(v);
Guido van Rossumcae027b1994-08-29 12:53:11 +00002017}
2018
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002019static char tuple_doc[] =
2020"tuple(sequence) -> list\n\
2021\n\
2022Return a tuple whose items are the same as those of the argument sequence.\n\
2023If the argument is a tuple, the return value is the same object.";
2024
2025
Guido van Rossum79f25d91997-04-29 20:08:16 +00002026static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002027builtin_type(PyObject *self, PyObject *args)
Guido van Rossum3f5da241990-12-20 15:06:42 +00002028{
Guido van Rossum79f25d91997-04-29 20:08:16 +00002029 PyObject *v;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002030
Guido van Rossum79f25d91997-04-29 20:08:16 +00002031 if (!PyArg_ParseTuple(args, "O:type", &v))
Guido van Rossum3f5da241990-12-20 15:06:42 +00002032 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002033 v = (PyObject *)v->ob_type;
2034 Py_INCREF(v);
Guido van Rossum3f5da241990-12-20 15:06:42 +00002035 return v;
2036}
2037
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002038static char type_doc[] =
2039"type(object) -> type object\n\
2040\n\
2041Return the type of the object.";
2042
2043
Guido van Rossum79f25d91997-04-29 20:08:16 +00002044static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002045builtin_vars(PyObject *self, PyObject *args)
Guido van Rossum2d951851994-08-29 12:52:16 +00002046{
Guido van Rossum79f25d91997-04-29 20:08:16 +00002047 PyObject *v = NULL;
2048 PyObject *d;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002049
Guido van Rossum79f25d91997-04-29 20:08:16 +00002050 if (!PyArg_ParseTuple(args, "|O:vars", &v))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002051 return NULL;
Guido van Rossum2d951851994-08-29 12:52:16 +00002052 if (v == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002053 d = PyEval_GetLocals();
Guido van Rossum53bb7ff1995-07-26 16:26:31 +00002054 if (d == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002055 if (!PyErr_Occurred())
2056 PyErr_SetString(PyExc_SystemError,
2057 "no locals!?");
Guido van Rossum53bb7ff1995-07-26 16:26:31 +00002058 }
2059 else
Guido van Rossum79f25d91997-04-29 20:08:16 +00002060 Py_INCREF(d);
Guido van Rossum2d951851994-08-29 12:52:16 +00002061 }
2062 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002063 d = PyObject_GetAttrString(v, "__dict__");
Guido van Rossum2d951851994-08-29 12:52:16 +00002064 if (d == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002065 PyErr_SetString(PyExc_TypeError,
Guido van Rossum2d951851994-08-29 12:52:16 +00002066 "vars() argument must have __dict__ attribute");
2067 return NULL;
2068 }
2069 }
2070 return d;
2071}
2072
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002073static char vars_doc[] =
2074"vars([object]) -> dictionary\n\
2075\n\
2076Without arguments, equivalent to locals().\n\
2077With an argument, equivalent to object.__dict__.";
2078
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002079static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002080builtin_isinstance(PyObject *self, PyObject *args)
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002081{
2082 PyObject *inst;
2083 PyObject *cls;
Guido van Rossum823649d2001-03-21 18:40:58 +00002084 int retval;
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002085
Guido van Rossum43713e52000-02-29 13:59:29 +00002086 if (!PyArg_ParseTuple(args, "OO:isinstance", &inst, &cls))
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002087 return NULL;
Guido van Rossumf5dd9141997-12-02 19:11:45 +00002088
Guido van Rossum823649d2001-03-21 18:40:58 +00002089 retval = PyObject_IsInstance(inst, cls);
2090 if (retval < 0)
Guido van Rossumad991772001-01-12 16:03:05 +00002091 return NULL;
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002092 return PyInt_FromLong(retval);
2093}
2094
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002095static char isinstance_doc[] =
2096"isinstance(object, class-or-type) -> Boolean\n\
2097\n\
2098Return whether an object is an instance of a class or of a subclass thereof.\n\
2099With a type as second argument, return whether that is the object's type.";
2100
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002101
2102static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002103builtin_issubclass(PyObject *self, PyObject *args)
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002104{
2105 PyObject *derived;
2106 PyObject *cls;
2107 int retval;
2108
Guido van Rossum43713e52000-02-29 13:59:29 +00002109 if (!PyArg_ParseTuple(args, "OO:issubclass", &derived, &cls))
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002110 return NULL;
Guido van Rossum668213d1999-06-16 17:28:37 +00002111
Guido van Rossum823649d2001-03-21 18:40:58 +00002112 retval = PyObject_IsSubclass(derived, cls);
2113 if (retval < 0)
2114 return NULL;
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002115 return PyInt_FromLong(retval);
2116}
2117
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002118static char issubclass_doc[] =
2119"issubclass(C, B) -> Boolean\n\
2120\n\
2121Return whether class C is a subclass (i.e., a derived class) of class B.";
2122
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002123
Barry Warsawbd599b52000-08-03 15:45:29 +00002124static PyObject*
2125builtin_zip(PyObject *self, PyObject *args)
2126{
2127 PyObject *ret;
2128 int itemsize = PySequence_Length(args);
2129 int i, j;
2130
2131 if (itemsize < 1) {
2132 PyErr_SetString(PyExc_TypeError,
Fred Drake661ea262000-10-24 19:57:45 +00002133 "zip() requires at least one sequence");
Barry Warsawbd599b52000-08-03 15:45:29 +00002134 return NULL;
2135 }
2136 /* args must be a tuple */
2137 assert(PyTuple_Check(args));
2138
2139 if ((ret = PyList_New(0)) == NULL)
2140 return NULL;
2141
2142 for (i = 0;; i++) {
2143 PyObject *next = PyTuple_New(itemsize);
2144 if (!next) {
2145 Py_DECREF(ret);
2146 return NULL;
2147 }
2148 for (j = 0; j < itemsize; j++) {
2149 PyObject *seq = PyTuple_GET_ITEM(args, j);
2150 PyObject *item = PySequence_GetItem(seq, i);
2151
2152 if (!item) {
2153 if (PyErr_ExceptionMatches(PyExc_IndexError)) {
2154 PyErr_Clear();
2155 Py_DECREF(next);
2156 return ret;
2157 }
2158 Py_DECREF(next);
2159 Py_DECREF(ret);
2160 return NULL;
2161 }
2162 PyTuple_SET_ITEM(next, j, item);
2163 }
2164 PyList_Append(ret, next);
2165 Py_DECREF(next);
2166 }
2167 /* no return */
2168}
2169
2170
2171static char zip_doc[] =
2172"zip(seq1 [, seq2 [...]]) -> [(seq1[0], seq2[0] ...), (...)]\n\
2173\n\
2174Return a list of tuples, where each tuple contains the i-th element\n\
2175from each of the argument sequences. The returned list is truncated\n\
2176in length to the length of the shortest argument sequence.";
2177
2178
Guido van Rossum79f25d91997-04-29 20:08:16 +00002179static PyMethodDef builtin_methods[] = {
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002180 {"__import__", builtin___import__, 1, import_doc},
2181 {"abs", builtin_abs, 1, abs_doc},
2182 {"apply", builtin_apply, 1, apply_doc},
Guido van Rossum0daf0221999-03-19 19:07:19 +00002183 {"buffer", builtin_buffer, 1, buffer_doc},
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002184 {"callable", builtin_callable, 1, callable_doc},
2185 {"chr", builtin_chr, 1, chr_doc},
2186 {"cmp", builtin_cmp, 1, cmp_doc},
2187 {"coerce", builtin_coerce, 1, coerce_doc},
2188 {"compile", builtin_compile, 1, compile_doc},
Guido van Rossum8a5c5d21996-01-12 01:09:56 +00002189#ifndef WITHOUT_COMPLEX
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002190 {"complex", builtin_complex, 1, complex_doc},
Guido van Rossum8a5c5d21996-01-12 01:09:56 +00002191#endif
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002192 {"delattr", builtin_delattr, 1, delattr_doc},
2193 {"dir", builtin_dir, 1, dir_doc},
2194 {"divmod", builtin_divmod, 1, divmod_doc},
2195 {"eval", builtin_eval, 1, eval_doc},
2196 {"execfile", builtin_execfile, 1, execfile_doc},
2197 {"filter", builtin_filter, 1, filter_doc},
2198 {"float", builtin_float, 1, float_doc},
2199 {"getattr", builtin_getattr, 1, getattr_doc},
2200 {"globals", builtin_globals, 1, globals_doc},
2201 {"hasattr", builtin_hasattr, 1, hasattr_doc},
2202 {"hash", builtin_hash, 1, hash_doc},
2203 {"hex", builtin_hex, 1, hex_doc},
2204 {"id", builtin_id, 1, id_doc},
2205 {"input", builtin_input, 1, input_doc},
2206 {"intern", builtin_intern, 1, intern_doc},
2207 {"int", builtin_int, 1, int_doc},
2208 {"isinstance", builtin_isinstance, 1, isinstance_doc},
2209 {"issubclass", builtin_issubclass, 1, issubclass_doc},
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002210 {"iter", builtin_iter, 1, iter_doc},
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002211 {"len", builtin_len, 1, len_doc},
2212 {"list", builtin_list, 1, list_doc},
2213 {"locals", builtin_locals, 1, locals_doc},
2214 {"long", builtin_long, 1, long_doc},
2215 {"map", builtin_map, 1, map_doc},
2216 {"max", builtin_max, 1, max_doc},
2217 {"min", builtin_min, 1, min_doc},
2218 {"oct", builtin_oct, 1, oct_doc},
2219 {"open", builtin_open, 1, open_doc},
2220 {"ord", builtin_ord, 1, ord_doc},
2221 {"pow", builtin_pow, 1, pow_doc},
2222 {"range", builtin_range, 1, range_doc},
2223 {"raw_input", builtin_raw_input, 1, raw_input_doc},
2224 {"reduce", builtin_reduce, 1, reduce_doc},
2225 {"reload", builtin_reload, 1, reload_doc},
2226 {"repr", builtin_repr, 1, repr_doc},
2227 {"round", builtin_round, 1, round_doc},
2228 {"setattr", builtin_setattr, 1, setattr_doc},
2229 {"slice", builtin_slice, 1, slice_doc},
2230 {"str", builtin_str, 1, str_doc},
2231 {"tuple", builtin_tuple, 1, tuple_doc},
2232 {"type", builtin_type, 1, type_doc},
Guido van Rossum09095f32000-03-10 23:00:52 +00002233 {"unicode", builtin_unicode, 1, unicode_doc},
2234 {"unichr", builtin_unichr, 1, unichr_doc},
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002235 {"vars", builtin_vars, 1, vars_doc},
2236 {"xrange", builtin_xrange, 1, xrange_doc},
Barry Warsawbd599b52000-08-03 15:45:29 +00002237 {"zip", builtin_zip, 1, zip_doc},
Guido van Rossumc02e15c1991-12-16 13:03:00 +00002238 {NULL, NULL},
Guido van Rossum3f5da241990-12-20 15:06:42 +00002239};
2240
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002241static char builtin_doc[] =
2242"Built-in functions, exceptions, and other objects.\n\
2243\n\
2244Noteworthy: None is the `nil' object; Ellipsis represents `...' in slices.";
2245
Guido van Rossum25ce5661997-08-02 03:10:38 +00002246PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002247_PyBuiltin_Init(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +00002248{
Fred Drake5550de32000-06-20 04:54:19 +00002249 PyObject *mod, *dict, *debug;
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002250 mod = Py_InitModule4("__builtin__", builtin_methods,
2251 builtin_doc, (PyObject *)NULL,
2252 PYTHON_API_VERSION);
Guido van Rossum25ce5661997-08-02 03:10:38 +00002253 if (mod == NULL)
2254 return NULL;
2255 dict = PyModule_GetDict(mod);
Guido van Rossum25ce5661997-08-02 03:10:38 +00002256 if (PyDict_SetItemString(dict, "None", Py_None) < 0)
2257 return NULL;
2258 if (PyDict_SetItemString(dict, "Ellipsis", Py_Ellipsis) < 0)
2259 return NULL;
Guido van Rossumad991772001-01-12 16:03:05 +00002260 if (PyDict_SetItemString(dict, "NotImplemented",
Neil Schemenauer23ab1992001-01-04 01:48:42 +00002261 Py_NotImplemented) < 0)
2262 return NULL;
Fred Drake5550de32000-06-20 04:54:19 +00002263 debug = PyInt_FromLong(Py_OptimizeFlag == 0);
2264 if (PyDict_SetItemString(dict, "__debug__", debug) < 0) {
2265 Py_XDECREF(debug);
Guido van Rossum25ce5661997-08-02 03:10:38 +00002266 return NULL;
Fred Drake5550de32000-06-20 04:54:19 +00002267 }
2268 Py_XDECREF(debug);
Barry Warsaw757af0e1997-08-29 22:13:51 +00002269
Guido van Rossum25ce5661997-08-02 03:10:38 +00002270 return mod;
Guido van Rossum3f5da241990-12-20 15:06:42 +00002271}
2272
Guido van Rossume77a7571993-11-03 15:01:26 +00002273/* Helper for filter(): filter a tuple through a function */
Guido van Rossum12d12c51993-10-26 17:58:25 +00002274
Guido van Rossum79f25d91997-04-29 20:08:16 +00002275static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002276filtertuple(PyObject *func, PyObject *tuple)
Guido van Rossum12d12c51993-10-26 17:58:25 +00002277{
Guido van Rossum79f25d91997-04-29 20:08:16 +00002278 PyObject *result;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002279 register int i, j;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002280 int len = PyTuple_Size(tuple);
Guido van Rossum12d12c51993-10-26 17:58:25 +00002281
Guido van Rossumb7b45621995-08-04 04:07:45 +00002282 if (len == 0) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002283 Py_INCREF(tuple);
Guido van Rossumb7b45621995-08-04 04:07:45 +00002284 return tuple;
2285 }
2286
Guido van Rossum79f25d91997-04-29 20:08:16 +00002287 if ((result = PyTuple_New(len)) == NULL)
Guido van Rossum2586bf01993-11-01 16:21:44 +00002288 return NULL;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002289
Guido van Rossum12d12c51993-10-26 17:58:25 +00002290 for (i = j = 0; i < len; ++i) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002291 PyObject *item, *good;
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002292 int ok;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002293
Guido van Rossum79f25d91997-04-29 20:08:16 +00002294 if ((item = PyTuple_GetItem(tuple, i)) == NULL)
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002295 goto Fail_1;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002296 if (func == Py_None) {
2297 Py_INCREF(item);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002298 good = item;
2299 }
2300 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002301 PyObject *arg = Py_BuildValue("(O)", item);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002302 if (arg == NULL)
2303 goto Fail_1;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002304 good = PyEval_CallObject(func, arg);
2305 Py_DECREF(arg);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002306 if (good == NULL)
Guido van Rossum12d12c51993-10-26 17:58:25 +00002307 goto Fail_1;
2308 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002309 ok = PyObject_IsTrue(good);
2310 Py_DECREF(good);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002311 if (ok) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002312 Py_INCREF(item);
2313 if (PyTuple_SetItem(result, j++, item) < 0)
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002314 goto Fail_1;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002315 }
Guido van Rossum12d12c51993-10-26 17:58:25 +00002316 }
2317
Guido van Rossum79f25d91997-04-29 20:08:16 +00002318 if (_PyTuple_Resize(&result, j, 0) < 0)
Guido van Rossum12d12c51993-10-26 17:58:25 +00002319 return NULL;
2320
Guido van Rossum12d12c51993-10-26 17:58:25 +00002321 return result;
2322
Guido van Rossum12d12c51993-10-26 17:58:25 +00002323Fail_1:
Guido van Rossum79f25d91997-04-29 20:08:16 +00002324 Py_DECREF(result);
Guido van Rossum12d12c51993-10-26 17:58:25 +00002325 return NULL;
2326}
2327
2328
Guido van Rossume77a7571993-11-03 15:01:26 +00002329/* Helper for filter(): filter a string through a function */
Guido van Rossum12d12c51993-10-26 17:58:25 +00002330
Guido van Rossum79f25d91997-04-29 20:08:16 +00002331static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002332filterstring(PyObject *func, PyObject *strobj)
Guido van Rossum12d12c51993-10-26 17:58:25 +00002333{
Guido van Rossum79f25d91997-04-29 20:08:16 +00002334 PyObject *result;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002335 register int i, j;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002336 int len = PyString_Size(strobj);
Guido van Rossum12d12c51993-10-26 17:58:25 +00002337
Guido van Rossum79f25d91997-04-29 20:08:16 +00002338 if (func == Py_None) {
Guido van Rossum2586bf01993-11-01 16:21:44 +00002339 /* No character is ever false -- share input string */
Guido van Rossum79f25d91997-04-29 20:08:16 +00002340 Py_INCREF(strobj);
Guido van Rossum2d951851994-08-29 12:52:16 +00002341 return strobj;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002342 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002343 if ((result = PyString_FromStringAndSize(NULL, len)) == NULL)
Guido van Rossum2586bf01993-11-01 16:21:44 +00002344 return NULL;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002345
Guido van Rossum12d12c51993-10-26 17:58:25 +00002346 for (i = j = 0; i < len; ++i) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002347 PyObject *item, *arg, *good;
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002348 int ok;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002349
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002350 item = (*strobj->ob_type->tp_as_sequence->sq_item)(strobj, i);
2351 if (item == NULL)
2352 goto Fail_1;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002353 arg = Py_BuildValue("(O)", item);
Tim Peters388ed082001-04-07 20:34:48 +00002354 if (arg == NULL) {
2355 Py_DECREF(item);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002356 goto Fail_1;
Tim Peters388ed082001-04-07 20:34:48 +00002357 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002358 good = PyEval_CallObject(func, arg);
2359 Py_DECREF(arg);
Tim Peters388ed082001-04-07 20:34:48 +00002360 if (good == NULL) {
2361 Py_DECREF(item);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002362 goto Fail_1;
Tim Peters388ed082001-04-07 20:34:48 +00002363 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002364 ok = PyObject_IsTrue(good);
2365 Py_DECREF(good);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002366 if (ok)
Guido van Rossum79f25d91997-04-29 20:08:16 +00002367 PyString_AS_STRING((PyStringObject *)result)[j++] =
2368 PyString_AS_STRING((PyStringObject *)item)[0];
Tim Peters388ed082001-04-07 20:34:48 +00002369 Py_DECREF(item);
Guido van Rossum12d12c51993-10-26 17:58:25 +00002370 }
2371
Guido van Rossum79f25d91997-04-29 20:08:16 +00002372 if (j < len && _PyString_Resize(&result, j) < 0)
Guido van Rossum12d12c51993-10-26 17:58:25 +00002373 return NULL;
2374
Guido van Rossum12d12c51993-10-26 17:58:25 +00002375 return result;
2376
Guido van Rossum12d12c51993-10-26 17:58:25 +00002377Fail_1:
Guido van Rossum79f25d91997-04-29 20:08:16 +00002378 Py_DECREF(result);
Guido van Rossum12d12c51993-10-26 17:58:25 +00002379 return NULL;
2380}