blob: 8572cc8183ed226fb06dd79e396a931b372cacbd [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{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000165 PyObject *func, *seq, *result;
166 PySequenceMethods *sqf;
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000167 int len;
168 register int i, j;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000169
Guido van Rossum79f25d91997-04-29 20:08:16 +0000170 if (!PyArg_ParseTuple(args, "OO:filter", &func, &seq))
Guido van Rossum12d12c51993-10-26 17:58:25 +0000171 return NULL;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000172
Guido van Rossum79f25d91997-04-29 20:08:16 +0000173 if (PyString_Check(seq)) {
174 PyObject *r = filterstring(func, seq);
Guido van Rossum12d12c51993-10-26 17:58:25 +0000175 return r;
176 }
177
Guido van Rossum79f25d91997-04-29 20:08:16 +0000178 if (PyTuple_Check(seq)) {
179 PyObject *r = filtertuple(func, seq);
Guido van Rossum12d12c51993-10-26 17:58:25 +0000180 return r;
181 }
182
Guido van Rossum09df08a1998-05-22 00:51:39 +0000183 sqf = seq->ob_type->tp_as_sequence;
184 if (sqf == NULL || sqf->sq_length == NULL || sqf->sq_item == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000185 PyErr_SetString(PyExc_TypeError,
Fred Drake661ea262000-10-24 19:57:45 +0000186 "filter() arg 2 must be a sequence");
Guido van Rossum12d12c51993-10-26 17:58:25 +0000187 goto Fail_2;
188 }
189
190 if ((len = (*sqf->sq_length)(seq)) < 0)
191 goto Fail_2;
192
Guido van Rossum79f25d91997-04-29 20:08:16 +0000193 if (PyList_Check(seq) && seq->ob_refcnt == 1) {
194 Py_INCREF(seq);
Guido van Rossum12d12c51993-10-26 17:58:25 +0000195 result = seq;
196 }
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000197 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000198 if ((result = PyList_New(len)) == NULL)
Guido van Rossum12d12c51993-10-26 17:58:25 +0000199 goto Fail_2;
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000200 }
Guido van Rossum12d12c51993-10-26 17:58:25 +0000201
Guido van Rossum2d951851994-08-29 12:52:16 +0000202 for (i = j = 0; ; ++i) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000203 PyObject *item, *good;
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000204 int ok;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000205
Guido van Rossum2d951851994-08-29 12:52:16 +0000206 if ((item = (*sqf->sq_item)(seq, i)) == NULL) {
Barry Warsawcde8b1b1997-08-22 21:14:38 +0000207 if (PyErr_ExceptionMatches(PyExc_IndexError)) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000208 PyErr_Clear();
Guido van Rossum2d951851994-08-29 12:52:16 +0000209 break;
210 }
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000211 goto Fail_1;
Guido van Rossum2d951851994-08-29 12:52:16 +0000212 }
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000213
Guido van Rossum79f25d91997-04-29 20:08:16 +0000214 if (func == Py_None) {
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000215 good = item;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000216 Py_INCREF(good);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000217 }
218 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000219 PyObject *arg = Py_BuildValue("(O)", item);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000220 if (arg == NULL)
221 goto Fail_1;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000222 good = PyEval_CallObject(func, arg);
223 Py_DECREF(arg);
Guido van Rossum58b68731995-01-10 17:40:55 +0000224 if (good == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000225 Py_DECREF(item);
Guido van Rossum12d12c51993-10-26 17:58:25 +0000226 goto Fail_1;
Guido van Rossum58b68731995-01-10 17:40:55 +0000227 }
Guido van Rossum12d12c51993-10-26 17:58:25 +0000228 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000229 ok = PyObject_IsTrue(good);
230 Py_DECREF(good);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000231 if (ok) {
Guido van Rossum2d951851994-08-29 12:52:16 +0000232 if (j < len) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000233 if (PyList_SetItem(result, j++, item) < 0)
Guido van Rossum2d951851994-08-29 12:52:16 +0000234 goto Fail_1;
235 }
236 else {
Barry Warsawfa77e091999-01-28 18:49:12 +0000237 int status = PyList_Append(result, item);
Guido van Rossum2d951851994-08-29 12:52:16 +0000238 j++;
Barry Warsawfa77e091999-01-28 18:49:12 +0000239 Py_DECREF(item);
240 if (status < 0)
Guido van Rossum2d951851994-08-29 12:52:16 +0000241 goto Fail_1;
242 }
Guido van Rossum58b68731995-01-10 17:40:55 +0000243 } else {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000244 Py_DECREF(item);
Guido van Rossum12d12c51993-10-26 17:58:25 +0000245 }
Guido van Rossum12d12c51993-10-26 17:58:25 +0000246 }
247
Guido van Rossum12d12c51993-10-26 17:58:25 +0000248
Guido van Rossum79f25d91997-04-29 20:08:16 +0000249 if (j < len && PyList_SetSlice(result, j, len, NULL) < 0)
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000250 goto Fail_1;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000251
Guido van Rossum12d12c51993-10-26 17:58:25 +0000252 return result;
253
Guido van Rossum12d12c51993-10-26 17:58:25 +0000254Fail_1:
Guido van Rossum79f25d91997-04-29 20:08:16 +0000255 Py_DECREF(result);
Guido van Rossum12d12c51993-10-26 17:58:25 +0000256Fail_2:
Guido van Rossum12d12c51993-10-26 17:58:25 +0000257 return NULL;
258}
259
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000260static char filter_doc[] =
261"filter(function, sequence) -> list\n\
262\n\
263Return a list containing those items of sequence for which function(item)\n\
264is true. If function is None, return a list of items that are true.";
265
266
Guido van Rossum79f25d91997-04-29 20:08:16 +0000267static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000268builtin_chr(PyObject *self, PyObject *args)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000269{
270 long x;
271 char s[1];
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000272
Guido van Rossum79f25d91997-04-29 20:08:16 +0000273 if (!PyArg_ParseTuple(args, "l:chr", &x))
Guido van Rossum3f5da241990-12-20 15:06:42 +0000274 return NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000275 if (x < 0 || x >= 256) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000276 PyErr_SetString(PyExc_ValueError,
277 "chr() arg not in range(256)");
Guido van Rossum3f5da241990-12-20 15:06:42 +0000278 return NULL;
279 }
Guido van Rossum6bf62da1997-04-11 20:37:35 +0000280 s[0] = (char)x;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000281 return PyString_FromStringAndSize(s, 1);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000282}
283
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000284static char chr_doc[] =
285"chr(i) -> character\n\
286\n\
287Return a string of one character with ordinal i; 0 <= i < 256.";
288
289
Guido van Rossum79f25d91997-04-29 20:08:16 +0000290static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000291builtin_unichr(PyObject *self, PyObject *args)
Guido van Rossum09095f32000-03-10 23:00:52 +0000292{
293 long x;
294 Py_UNICODE s[1];
295
296 if (!PyArg_ParseTuple(args, "l:unichr", &x))
297 return NULL;
298 if (x < 0 || x >= 65536) {
299 PyErr_SetString(PyExc_ValueError,
300 "unichr() arg not in range(65536)");
301 return NULL;
302 }
303 s[0] = (Py_UNICODE)x;
304 return PyUnicode_FromUnicode(s, 1);
305}
306
307static char unichr_doc[] =
Fred Drake078b24f2000-04-13 02:42:50 +0000308"unichr(i) -> Unicode character\n\
Guido van Rossum09095f32000-03-10 23:00:52 +0000309\n\
Fred Drake078b24f2000-04-13 02:42:50 +0000310Return a Unicode string of one character with ordinal i; 0 <= i < 65536.";
Guido van Rossum09095f32000-03-10 23:00:52 +0000311
312
313static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000314builtin_cmp(PyObject *self, PyObject *args)
Guido van Rossuma9e7dc11992-10-18 18:53:57 +0000315{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000316 PyObject *a, *b;
Guido van Rossum09df08a1998-05-22 00:51:39 +0000317 int c;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000318
Guido van Rossum79f25d91997-04-29 20:08:16 +0000319 if (!PyArg_ParseTuple(args, "OO:cmp", &a, &b))
Guido van Rossuma9e7dc11992-10-18 18:53:57 +0000320 return NULL;
Guido van Rossum09df08a1998-05-22 00:51:39 +0000321 if (PyObject_Cmp(a, b, &c) < 0)
Guido van Rossumc8b6df91997-05-23 00:06:51 +0000322 return NULL;
Guido van Rossum09df08a1998-05-22 00:51:39 +0000323 return PyInt_FromLong((long)c);
Guido van Rossuma9e7dc11992-10-18 18:53:57 +0000324}
325
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000326static char cmp_doc[] =
327"cmp(x, y) -> integer\n\
328\n\
329Return negative if x<y, zero if x==y, positive if x>y.";
330
331
Guido van Rossum79f25d91997-04-29 20:08:16 +0000332static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000333builtin_coerce(PyObject *self, PyObject *args)
Guido van Rossum6a00cd81995-01-07 12:39:01 +0000334{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000335 PyObject *v, *w;
336 PyObject *res;
Guido van Rossum5524a591995-01-10 15:26:20 +0000337
Guido van Rossum79f25d91997-04-29 20:08:16 +0000338 if (!PyArg_ParseTuple(args, "OO:coerce", &v, &w))
Guido van Rossum5524a591995-01-10 15:26:20 +0000339 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000340 if (PyNumber_Coerce(&v, &w) < 0)
Guido van Rossum04691fc1992-08-12 15:35:34 +0000341 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000342 res = Py_BuildValue("(OO)", v, w);
343 Py_DECREF(v);
344 Py_DECREF(w);
Guido van Rossum04691fc1992-08-12 15:35:34 +0000345 return res;
346}
347
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000348static char coerce_doc[] =
349"coerce(x, y) -> None or (x1, y1)\n\
350\n\
351When x and y can be coerced to values of the same type, return a tuple\n\
352containing the coerced values. When they can't be coerced, return None.";
353
354
Guido van Rossum79f25d91997-04-29 20:08:16 +0000355static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000356builtin_compile(PyObject *self, PyObject *args)
Guido van Rossum5b722181993-03-30 17:46:03 +0000357{
358 char *str;
359 char *filename;
360 char *startstr;
361 int start;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000362
Guido van Rossum79f25d91997-04-29 20:08:16 +0000363 if (!PyArg_ParseTuple(args, "sss:compile", &str, &filename, &startstr))
Guido van Rossum5b722181993-03-30 17:46:03 +0000364 return NULL;
365 if (strcmp(startstr, "exec") == 0)
Guido van Rossumb05a5c71997-05-07 17:46:13 +0000366 start = Py_file_input;
Guido van Rossum5b722181993-03-30 17:46:03 +0000367 else if (strcmp(startstr, "eval") == 0)
Guido van Rossumb05a5c71997-05-07 17:46:13 +0000368 start = Py_eval_input;
Guido van Rossum872537c1995-07-07 22:43:42 +0000369 else if (strcmp(startstr, "single") == 0)
Guido van Rossumb05a5c71997-05-07 17:46:13 +0000370 start = Py_single_input;
Guido van Rossum5b722181993-03-30 17:46:03 +0000371 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000372 PyErr_SetString(PyExc_ValueError,
Fred Drake661ea262000-10-24 19:57:45 +0000373 "compile() arg 3 must be 'exec' or 'eval' or 'single'");
Guido van Rossum5b722181993-03-30 17:46:03 +0000374 return NULL;
375 }
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000376 if (PyEval_GetNestedScopes()) {
377 PyCompilerFlags cf;
378 cf.cf_nested_scopes = 1;
379 return Py_CompileStringFlags(str, filename, start, &cf);
380 } else
381 return Py_CompileString(str, filename, start);
Guido van Rossum5b722181993-03-30 17:46:03 +0000382}
383
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000384static char compile_doc[] =
385"compile(source, filename, mode) -> code object\n\
386\n\
387Compile the source string (a Python module, statement or expression)\n\
388into a code object that can be executed by the exec statement or eval().\n\
389The filename will be used for run-time error messages.\n\
390The mode must be 'exec' to compile a module, 'single' to compile a\n\
391single (interactive) statement, or 'eval' to compile an expression.";
392
393
Guido van Rossum8a5c5d21996-01-12 01:09:56 +0000394#ifndef WITHOUT_COMPLEX
395
Guido van Rossum79f25d91997-04-29 20:08:16 +0000396static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000397complex_from_string(PyObject *v)
Guido van Rossum11950231999-03-25 21:16:07 +0000398{
Tim Petersdbd9ba62000-07-09 03:09:57 +0000399 extern double strtod(const char *, char **);
Guido van Rossum9e896b32000-04-05 20:11:21 +0000400 const char *s, *start;
401 char *end;
Guido van Rossum11950231999-03-25 21:16:07 +0000402 double x=0.0, y=0.0, z;
403 int got_re=0, got_im=0, done=0;
404 int digit_or_dot;
405 int sw_error=0;
406 int sign;
407 char buffer[256]; /* For errors */
Barry Warsaw5ca1ef92000-08-18 05:02:16 +0000408 char s_buffer[256];
Guido van Rossum9e896b32000-04-05 20:11:21 +0000409 int len;
Guido van Rossum11950231999-03-25 21:16:07 +0000410
Guido van Rossum9e896b32000-04-05 20:11:21 +0000411 if (PyString_Check(v)) {
412 s = PyString_AS_STRING(v);
413 len = PyString_GET_SIZE(v);
414 }
415 else if (PyUnicode_Check(v)) {
Guido van Rossum9e896b32000-04-05 20:11:21 +0000416 if (PyUnicode_GET_SIZE(v) >= sizeof(s_buffer)) {
417 PyErr_SetString(PyExc_ValueError,
418 "complex() literal too large to convert");
419 return NULL;
420 }
Guido van Rossumad991772001-01-12 16:03:05 +0000421 if (PyUnicode_EncodeDecimal(PyUnicode_AS_UNICODE(v),
Guido van Rossum9e896b32000-04-05 20:11:21 +0000422 PyUnicode_GET_SIZE(v),
Guido van Rossumad991772001-01-12 16:03:05 +0000423 s_buffer,
Guido van Rossum9e896b32000-04-05 20:11:21 +0000424 NULL))
425 return NULL;
426 s = s_buffer;
Trent Mick29b83812000-08-12 21:35:36 +0000427 len = (int)strlen(s);
Guido van Rossum9e896b32000-04-05 20:11:21 +0000428 }
429 else if (PyObject_AsCharBuffer(v, &s, &len)) {
430 PyErr_SetString(PyExc_TypeError,
Fred Drake661ea262000-10-24 19:57:45 +0000431 "complex() arg is not a string");
Guido van Rossum9e896b32000-04-05 20:11:21 +0000432 return NULL;
433 }
Guido van Rossum11950231999-03-25 21:16:07 +0000434
435 /* position on first nonblank */
Guido van Rossum9e896b32000-04-05 20:11:21 +0000436 start = s;
Guido van Rossum11950231999-03-25 21:16:07 +0000437 while (*s && isspace(Py_CHARMASK(*s)))
438 s++;
439 if (s[0] == '\0') {
440 PyErr_SetString(PyExc_ValueError,
Fred Drake661ea262000-10-24 19:57:45 +0000441 "complex() arg is an empty string");
Guido van Rossum11950231999-03-25 21:16:07 +0000442 return NULL;
443 }
444
445 z = -1.0;
446 sign = 1;
447 do {
Guido van Rossumad991772001-01-12 16:03:05 +0000448
Guido van Rossum11950231999-03-25 21:16:07 +0000449 switch (*s) {
450
451 case '\0':
Guido van Rossum9e896b32000-04-05 20:11:21 +0000452 if (s-start != len) {
Guido van Rossum11950231999-03-25 21:16:07 +0000453 PyErr_SetString(
454 PyExc_ValueError,
Fred Drake661ea262000-10-24 19:57:45 +0000455 "complex() arg contains a null byte");
Guido van Rossum11950231999-03-25 21:16:07 +0000456 return NULL;
457 }
458 if(!done) sw_error=1;
459 break;
Guido van Rossumad991772001-01-12 16:03:05 +0000460
Guido van Rossum11950231999-03-25 21:16:07 +0000461 case '-':
462 sign = -1;
463 /* Fallthrough */
464 case '+':
465 if (done) sw_error=1;
466 s++;
467 if ( *s=='\0'||*s=='+'||*s=='-' ||
468 isspace(Py_CHARMASK(*s)) ) sw_error=1;
469 break;
470
471 case 'J':
472 case 'j':
473 if (got_im || done) {
474 sw_error = 1;
475 break;
476 }
477 if (z<0.0) {
478 y=sign;
479 }
480 else{
481 y=sign*z;
482 }
483 got_im=1;
484 s++;
485 if (*s!='+' && *s!='-' )
486 done=1;
487 break;
488
489 default:
490 if (isspace(Py_CHARMASK(*s))) {
491 while (*s && isspace(Py_CHARMASK(*s)))
492 s++;
493 if (s[0] != '\0')
494 sw_error=1;
495 else
496 done = 1;
497 break;
498 }
499 digit_or_dot =
500 (*s=='.' || isdigit(Py_CHARMASK(*s)));
501 if (done||!digit_or_dot) {
502 sw_error=1;
503 break;
504 }
505 errno = 0;
506 PyFPE_START_PROTECT("strtod", return 0)
507 z = strtod(s, &end) ;
508 PyFPE_END_PROTECT(z)
509 if (errno != 0) {
510 sprintf(buffer,
511 "float() out of range: %.150s", s);
512 PyErr_SetString(
513 PyExc_ValueError,
514 buffer);
515 return NULL;
516 }
517 s=end;
518 if (*s=='J' || *s=='j') {
Guido van Rossumad991772001-01-12 16:03:05 +0000519
Guido van Rossum11950231999-03-25 21:16:07 +0000520 break;
521 }
522 if (got_re) {
523 sw_error=1;
524 break;
525 }
526
527 /* accept a real part */
528 x=sign*z;
529 got_re=1;
530 if (got_im) done=1;
531 z = -1.0;
532 sign = 1;
533 break;
Guido van Rossumad991772001-01-12 16:03:05 +0000534
Guido van Rossum11950231999-03-25 21:16:07 +0000535 } /* end of switch */
536
537 } while (*s!='\0' && !sw_error);
538
539 if (sw_error) {
540 PyErr_SetString(PyExc_ValueError,
Fred Drake661ea262000-10-24 19:57:45 +0000541 "complex() arg is a malformed string");
Guido van Rossum11950231999-03-25 21:16:07 +0000542 return NULL;
543 }
544
545 return PyComplex_FromDoubles(x,y);
546}
547
548static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000549builtin_complex(PyObject *self, PyObject *args)
Guido van Rossum8a5c5d21996-01-12 01:09:56 +0000550{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000551 PyObject *r, *i, *tmp;
552 PyNumberMethods *nbr, *nbi = NULL;
Guido van Rossum530956d1996-07-21 02:27:43 +0000553 Py_complex cr, ci;
Guido van Rossumc6472e91997-03-31 17:15:43 +0000554 int own_r = 0;
Guido van Rossum8a5c5d21996-01-12 01:09:56 +0000555
556 i = NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000557 if (!PyArg_ParseTuple(args, "O|O:complex", &r, &i))
Guido van Rossum8a5c5d21996-01-12 01:09:56 +0000558 return NULL;
Guido van Rossum9e896b32000-04-05 20:11:21 +0000559 if (PyString_Check(r) || PyUnicode_Check(r))
Guido van Rossum11950231999-03-25 21:16:07 +0000560 return complex_from_string(r);
Guido van Rossum8a5c5d21996-01-12 01:09:56 +0000561 if ((nbr = r->ob_type->tp_as_number) == NULL ||
Guido van Rossumc6472e91997-03-31 17:15:43 +0000562 nbr->nb_float == NULL ||
563 (i != NULL &&
564 ((nbi = i->ob_type->tp_as_number) == NULL ||
565 nbi->nb_float == NULL))) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000566 PyErr_SetString(PyExc_TypeError,
Fred Drake661ea262000-10-24 19:57:45 +0000567 "complex() arg can't be converted to complex");
Guido van Rossum8a5c5d21996-01-12 01:09:56 +0000568 return NULL;
569 }
Guido van Rossumed0af8f1996-12-05 23:18:18 +0000570 /* XXX Hack to support classes with __complex__ method */
Guido van Rossum79f25d91997-04-29 20:08:16 +0000571 if (PyInstance_Check(r)) {
572 static PyObject *complexstr;
573 PyObject *f;
Guido van Rossumed0af8f1996-12-05 23:18:18 +0000574 if (complexstr == NULL) {
Guido van Rossum8d751611997-01-18 08:04:16 +0000575 complexstr = PyString_InternFromString("__complex__");
Guido van Rossumed0af8f1996-12-05 23:18:18 +0000576 if (complexstr == NULL)
577 return NULL;
578 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000579 f = PyObject_GetAttr(r, complexstr);
Guido van Rossumed0af8f1996-12-05 23:18:18 +0000580 if (f == NULL)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000581 PyErr_Clear();
Guido van Rossumed0af8f1996-12-05 23:18:18 +0000582 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000583 PyObject *args = Py_BuildValue("()");
Guido van Rossumed0af8f1996-12-05 23:18:18 +0000584 if (args == NULL)
585 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000586 r = PyEval_CallObject(f, args);
587 Py_DECREF(args);
Barry Warsawf988e681999-01-27 23:13:59 +0000588 Py_DECREF(f);
Guido van Rossumed0af8f1996-12-05 23:18:18 +0000589 if (r == NULL)
590 return NULL;
Guido van Rossumc6472e91997-03-31 17:15:43 +0000591 own_r = 1;
Guido van Rossumed0af8f1996-12-05 23:18:18 +0000592 }
593 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000594 if (PyComplex_Check(r)) {
595 cr = ((PyComplexObject*)r)->cval;
Guido van Rossum730806d1998-04-10 22:27:42 +0000596 if (own_r) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000597 Py_DECREF(r);
Guido van Rossum730806d1998-04-10 22:27:42 +0000598 }
Guido van Rossumc6472e91997-03-31 17:15:43 +0000599 }
Guido van Rossum8a5c5d21996-01-12 01:09:56 +0000600 else {
Guido van Rossum8dabbf12001-01-19 02:11:59 +0000601 tmp = PyNumber_Float(r);
Guido van Rossum730806d1998-04-10 22:27:42 +0000602 if (own_r) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000603 Py_DECREF(r);
Guido van Rossum730806d1998-04-10 22:27:42 +0000604 }
Guido van Rossumfe4b6ee1996-08-08 18:49:41 +0000605 if (tmp == NULL)
606 return NULL;
Guido van Rossum8dabbf12001-01-19 02:11:59 +0000607 if (!PyFloat_Check(tmp)) {
608 PyErr_SetString(PyExc_TypeError,
609 "float(r) didn't return a float");
610 Py_DECREF(tmp);
611 return NULL;
612 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000613 cr.real = PyFloat_AsDouble(tmp);
614 Py_DECREF(tmp);
Guido van Rossum11950231999-03-25 21:16:07 +0000615 cr.imag = 0.0;
Guido van Rossum8a5c5d21996-01-12 01:09:56 +0000616 }
617 if (i == NULL) {
Guido van Rossum11950231999-03-25 21:16:07 +0000618 ci.real = 0.0;
619 ci.imag = 0.0;
Guido van Rossum8a5c5d21996-01-12 01:09:56 +0000620 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000621 else if (PyComplex_Check(i))
622 ci = ((PyComplexObject*)i)->cval;
Guido van Rossum8a5c5d21996-01-12 01:09:56 +0000623 else {
Guido van Rossumc6472e91997-03-31 17:15:43 +0000624 tmp = (*nbi->nb_float)(i);
Guido van Rossumfe4b6ee1996-08-08 18:49:41 +0000625 if (tmp == NULL)
626 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000627 ci.real = PyFloat_AsDouble(tmp);
628 Py_DECREF(tmp);
Guido van Rossum8a5c5d21996-01-12 01:09:56 +0000629 ci.imag = 0.;
630 }
631 cr.real -= ci.imag;
632 cr.imag += ci.real;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000633 return PyComplex_FromCComplex(cr);
Guido van Rossum8a5c5d21996-01-12 01:09:56 +0000634}
635
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000636static char complex_doc[] =
637"complex(real[, imag]) -> complex number\n\
638\n\
639Create a complex number from a real part and an optional imaginary part.\n\
640This is equivalent to (real + imag*1j) where imag defaults to 0.";
641
642
Guido van Rossum8a5c5d21996-01-12 01:09:56 +0000643#endif
644
Guido van Rossum79f25d91997-04-29 20:08:16 +0000645static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000646builtin_dir(PyObject *self, PyObject *args)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000647{
Guido van Rossum666b17a1997-05-06 16:36:57 +0000648 static char *attrlist[] = {"__members__", "__methods__", NULL};
649 PyObject *v = NULL, *l = NULL, *m = NULL;
650 PyObject *d, *x;
651 int i;
652 char **s;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000653
Guido van Rossum79f25d91997-04-29 20:08:16 +0000654 if (!PyArg_ParseTuple(args, "|O:dir", &v))
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000655 return NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000656 if (v == NULL) {
Guido van Rossum666b17a1997-05-06 16:36:57 +0000657 x = PyEval_GetLocals();
658 if (x == NULL)
659 goto error;
660 l = PyMapping_Keys(x);
661 if (l == NULL)
662 goto error;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000663 }
664 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000665 d = PyObject_GetAttrString(v, "__dict__");
Guido van Rossum666b17a1997-05-06 16:36:57 +0000666 if (d == NULL)
Guido van Rossum795ba581996-05-23 22:49:07 +0000667 PyErr_Clear();
Guido van Rossum666b17a1997-05-06 16:36:57 +0000668 else {
669 l = PyMapping_Keys(d);
670 if (l == NULL)
671 PyErr_Clear();
672 Py_DECREF(d);
673 }
674 if (l == NULL) {
675 l = PyList_New(0);
676 if (l == NULL)
677 goto error;
678 }
679 for (s = attrlist; *s != NULL; s++) {
680 m = PyObject_GetAttrString(v, *s);
681 if (m == NULL) {
682 PyErr_Clear();
683 continue;
684 }
685 for (i = 0; ; i++) {
686 x = PySequence_GetItem(m, i);
687 if (x == NULL) {
688 PyErr_Clear();
689 break;
690 }
691 if (PyList_Append(l, x) != 0) {
692 Py_DECREF(x);
693 Py_DECREF(m);
694 goto error;
695 }
696 Py_DECREF(x);
697 }
698 Py_DECREF(m);
Guido van Rossum795ba581996-05-23 22:49:07 +0000699 }
Guido van Rossum006bcd41991-10-24 14:54:44 +0000700 }
Guido van Rossum666b17a1997-05-06 16:36:57 +0000701 if (PyList_Sort(l) != 0)
702 goto error;
703 return l;
704 error:
705 Py_XDECREF(l);
706 return NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000707}
708
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000709static char dir_doc[] =
710"dir([object]) -> list of strings\n\
711\n\
712Return an alphabetized list of names comprising (some of) the attributes\n\
713of the given object. Without an argument, the names in the current scope\n\
714are listed. With an instance argument, only the instance attributes are\n\
715returned. With a class argument, attributes of the base class are not\n\
716returned. For other types or arguments, this may list members or methods.";
717
718
Guido van Rossum79f25d91997-04-29 20:08:16 +0000719static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000720builtin_divmod(PyObject *self, PyObject *args)
Guido van Rossum6a00cd81995-01-07 12:39:01 +0000721{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000722 PyObject *v, *w;
Guido van Rossum6a00cd81995-01-07 12:39:01 +0000723
Guido van Rossum79f25d91997-04-29 20:08:16 +0000724 if (!PyArg_ParseTuple(args, "OO:divmod", &v, &w))
Guido van Rossum6a00cd81995-01-07 12:39:01 +0000725 return NULL;
Guido van Rossum09df08a1998-05-22 00:51:39 +0000726 return PyNumber_Divmod(v, w);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000727}
728
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000729static char divmod_doc[] =
730"divmod(x, y) -> (div, mod)\n\
731\n\
732Return the tuple ((x-x%y)/y, x%y). Invariant: div*y + mod == x.";
733
734
Guido van Rossum79f25d91997-04-29 20:08:16 +0000735static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000736builtin_eval(PyObject *self, PyObject *args)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000737{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000738 PyObject *cmd;
739 PyObject *globals = Py_None, *locals = Py_None;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000740 char *str;
Guido van Rossum590baa41993-11-30 13:40:46 +0000741
Guido van Rossum79f25d91997-04-29 20:08:16 +0000742 if (!PyArg_ParseTuple(args, "O|O!O!:eval",
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000743 &cmd,
Guido van Rossum79f25d91997-04-29 20:08:16 +0000744 &PyDict_Type, &globals,
745 &PyDict_Type, &locals))
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000746 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000747 if (globals == Py_None) {
748 globals = PyEval_GetGlobals();
749 if (locals == Py_None)
750 locals = PyEval_GetLocals();
Guido van Rossum6135a871995-01-09 17:53:26 +0000751 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000752 else if (locals == Py_None)
Guido van Rossum6135a871995-01-09 17:53:26 +0000753 locals = globals;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000754 if (PyDict_GetItemString(globals, "__builtins__") == NULL) {
755 if (PyDict_SetItemString(globals, "__builtins__",
756 PyEval_GetBuiltins()) != 0)
Guido van Rossum6135a871995-01-09 17:53:26 +0000757 return NULL;
758 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000759 if (PyCode_Check(cmd))
760 return PyEval_EvalCode((PyCodeObject *) cmd, globals, locals);
Marc-André Lemburgd1ba4432000-09-19 21:04:18 +0000761 if (!PyString_Check(cmd) &&
762 !PyUnicode_Check(cmd)) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000763 PyErr_SetString(PyExc_TypeError,
Fred Drake661ea262000-10-24 19:57:45 +0000764 "eval() arg 1 must be a string or code object");
Guido van Rossum94390a41992-08-14 15:14:30 +0000765 return NULL;
766 }
Marc-André Lemburgd1ba4432000-09-19 21:04:18 +0000767 if (PyString_AsStringAndSize(cmd, &str, NULL))
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000768 return NULL;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000769 while (*str == ' ' || *str == '\t')
770 str++;
Guido van Rossumb05a5c71997-05-07 17:46:13 +0000771 return PyRun_String(str, Py_eval_input, globals, locals);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000772}
773
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000774static char eval_doc[] =
775"eval(source[, globals[, locals]]) -> value\n\
776\n\
777Evaluate the source in the context of globals and locals.\n\
778The source may be a string representing a Python expression\n\
779or a code object as returned by compile().\n\
780The globals and locals are dictionaries, defaulting to the current\n\
781globals and locals. If only globals is given, locals defaults to it.";
782
783
Guido van Rossum79f25d91997-04-29 20:08:16 +0000784static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000785builtin_execfile(PyObject *self, PyObject *args)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000786{
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000787 char *filename;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000788 PyObject *globals = Py_None, *locals = Py_None;
789 PyObject *res;
Guido van Rossum0f61f8a1992-02-25 18:55:05 +0000790 FILE* fp;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000791
Guido van Rossum79f25d91997-04-29 20:08:16 +0000792 if (!PyArg_ParseTuple(args, "s|O!O!:execfile",
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000793 &filename,
Guido van Rossum79f25d91997-04-29 20:08:16 +0000794 &PyDict_Type, &globals,
795 &PyDict_Type, &locals))
Guido van Rossum0f61f8a1992-02-25 18:55:05 +0000796 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000797 if (globals == Py_None) {
798 globals = PyEval_GetGlobals();
799 if (locals == Py_None)
800 locals = PyEval_GetLocals();
Guido van Rossum6135a871995-01-09 17:53:26 +0000801 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000802 else if (locals == Py_None)
Guido van Rossum6135a871995-01-09 17:53:26 +0000803 locals = globals;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000804 if (PyDict_GetItemString(globals, "__builtins__") == NULL) {
805 if (PyDict_SetItemString(globals, "__builtins__",
806 PyEval_GetBuiltins()) != 0)
Guido van Rossum6135a871995-01-09 17:53:26 +0000807 return NULL;
808 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000809 Py_BEGIN_ALLOW_THREADS
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000810 fp = fopen(filename, "r");
Guido van Rossum79f25d91997-04-29 20:08:16 +0000811 Py_END_ALLOW_THREADS
Guido van Rossum0f61f8a1992-02-25 18:55:05 +0000812 if (fp == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000813 PyErr_SetFromErrno(PyExc_IOError);
Guido van Rossum0f61f8a1992-02-25 18:55:05 +0000814 return NULL;
815 }
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000816 if (PyEval_GetNestedScopes()) {
817 PyCompilerFlags cf;
818 cf.cf_nested_scopes = 1;
Tim Peters748b8bb2001-04-28 08:20:22 +0000819 res = PyRun_FileExFlags(fp, filename, Py_file_input, globals,
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000820 locals, 1, &cf);
Tim Peters748b8bb2001-04-28 08:20:22 +0000821 } else
822 res = PyRun_FileEx(fp, filename, Py_file_input, globals,
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000823 locals, 1);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000824 return res;
Guido van Rossum0f61f8a1992-02-25 18:55:05 +0000825}
826
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000827static char execfile_doc[] =
828"execfile(filename[, globals[, locals]])\n\
829\n\
830Read and execute a Python script from a file.\n\
831The globals and locals are dictionaries, defaulting to the current\n\
832globals and locals. If only globals is given, locals defaults to it.";
833
834
Guido van Rossum79f25d91997-04-29 20:08:16 +0000835static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000836builtin_getattr(PyObject *self, PyObject *args)
Guido van Rossum33894be1992-01-27 16:53:09 +0000837{
Guido van Rossum950ff291998-06-29 13:38:57 +0000838 PyObject *v, *result, *dflt = NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000839 PyObject *name;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000840
Marc-André Lemburg691270f2000-09-18 16:22:27 +0000841 if (!PyArg_ParseTuple(args, "OO|O:getattr", &v, &name, &dflt))
Guido van Rossum33894be1992-01-27 16:53:09 +0000842 return NULL;
Guido van Rossum950ff291998-06-29 13:38:57 +0000843 result = PyObject_GetAttr(v, name);
844 if (result == NULL && dflt != NULL) {
845 PyErr_Clear();
846 Py_INCREF(dflt);
847 result = dflt;
848 }
849 return result;
Guido van Rossum9bfef441993-03-29 10:43:31 +0000850}
851
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000852static char getattr_doc[] =
Guido van Rossum950ff291998-06-29 13:38:57 +0000853"getattr(object, name[, default]) -> value\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000854\n\
Guido van Rossum950ff291998-06-29 13:38:57 +0000855Get a named attribute from an object; getattr(x, 'y') is equivalent to x.y.\n\
856When a default argument is given, it is returned when the attribute doesn't\n\
857exist; without it, an exception is raised in that case.";
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000858
859
Guido van Rossum79f25d91997-04-29 20:08:16 +0000860static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000861builtin_globals(PyObject *self, PyObject *args)
Guido van Rossum872537c1995-07-07 22:43:42 +0000862{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000863 PyObject *d;
Guido van Rossum872537c1995-07-07 22:43:42 +0000864
Guido van Rossum43713e52000-02-29 13:59:29 +0000865 if (!PyArg_ParseTuple(args, ":globals"))
Guido van Rossum872537c1995-07-07 22:43:42 +0000866 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000867 d = PyEval_GetGlobals();
868 Py_INCREF(d);
Guido van Rossum872537c1995-07-07 22:43:42 +0000869 return d;
870}
871
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000872static char globals_doc[] =
873"globals() -> dictionary\n\
874\n\
875Return the dictionary containing the current scope's global variables.";
876
877
Guido van Rossum79f25d91997-04-29 20:08:16 +0000878static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000879builtin_hasattr(PyObject *self, PyObject *args)
Guido van Rossum9bfef441993-03-29 10:43:31 +0000880{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000881 PyObject *v;
882 PyObject *name;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000883
Marc-André Lemburg691270f2000-09-18 16:22:27 +0000884 if (!PyArg_ParseTuple(args, "OO:hasattr", &v, &name))
Guido van Rossum9bfef441993-03-29 10:43:31 +0000885 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000886 v = PyObject_GetAttr(v, name);
Guido van Rossum9bfef441993-03-29 10:43:31 +0000887 if (v == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000888 PyErr_Clear();
Guido van Rossum09df08a1998-05-22 00:51:39 +0000889 Py_INCREF(Py_False);
890 return Py_False;
Guido van Rossum9bfef441993-03-29 10:43:31 +0000891 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000892 Py_DECREF(v);
Guido van Rossum09df08a1998-05-22 00:51:39 +0000893 Py_INCREF(Py_True);
894 return Py_True;
Guido van Rossum33894be1992-01-27 16:53:09 +0000895}
896
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000897static char hasattr_doc[] =
898"hasattr(object, name) -> Boolean\n\
899\n\
900Return whether the object has an attribute with the given name.\n\
901(This is done by calling getattr(object, name) and catching exceptions.)";
902
903
Guido van Rossum79f25d91997-04-29 20:08:16 +0000904static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000905builtin_id(PyObject *self, PyObject *args)
Guido van Rossum5b722181993-03-30 17:46:03 +0000906{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000907 PyObject *v;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000908
Guido van Rossum79f25d91997-04-29 20:08:16 +0000909 if (!PyArg_ParseTuple(args, "O:id", &v))
Guido van Rossum5b722181993-03-30 17:46:03 +0000910 return NULL;
Guido van Rossum106f2da2000-06-28 21:12:25 +0000911 return PyLong_FromVoidPtr(v);
Guido van Rossum5b722181993-03-30 17:46:03 +0000912}
913
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000914static char id_doc[] =
915"id(object) -> integer\n\
916\n\
917Return the identity of an object. This is guaranteed to be unique among\n\
918simultaneously existing objects. (Hint: it's the object's memory address.)";
919
920
Guido van Rossum79f25d91997-04-29 20:08:16 +0000921static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000922builtin_map(PyObject *self, PyObject *args)
Guido van Rossum12d12c51993-10-26 17:58:25 +0000923{
924 typedef struct {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000925 PyObject *seq;
926 PySequenceMethods *sqf;
Tim Peters748b8bb2001-04-28 08:20:22 +0000927 int saw_IndexError;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000928 } sequence;
929
Guido van Rossum79f25d91997-04-29 20:08:16 +0000930 PyObject *func, *result;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000931 sequence *seqs = NULL, *sqp;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000932 int n, len;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000933 register int i, j;
934
Guido van Rossum79f25d91997-04-29 20:08:16 +0000935 n = PyTuple_Size(args);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000936 if (n < 2) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000937 PyErr_SetString(PyExc_TypeError,
938 "map() requires at least two args");
Guido van Rossum12d12c51993-10-26 17:58:25 +0000939 return NULL;
940 }
941
Guido van Rossum79f25d91997-04-29 20:08:16 +0000942 func = PyTuple_GetItem(args, 0);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000943 n--;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000944
Guido van Rossumfa4ac711998-07-10 17:37:30 +0000945 if (func == Py_None && n == 1) {
946 /* map(None, S) is the same as list(S). */
947 return PySequence_List(PyTuple_GetItem(args, 1));
948 }
949
Guido van Rossum79f25d91997-04-29 20:08:16 +0000950 if ((seqs = PyMem_NEW(sequence, n)) == NULL) {
951 PyErr_NoMemory();
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000952 goto Fail_2;
953 }
Guido van Rossum12d12c51993-10-26 17:58:25 +0000954
Tim Peters748b8bb2001-04-28 08:20:22 +0000955 /* Do a first pass to (a) verify the args are sequences; (b) set
956 * len to the largest of their lengths; (c) initialize the seqs
957 * descriptor vector.
958 */
Guido van Rossum2d951851994-08-29 12:52:16 +0000959 for (len = 0, i = 0, sqp = seqs; i < n; ++i, ++sqp) {
Guido van Rossum12d12c51993-10-26 17:58:25 +0000960 int curlen;
Guido van Rossum09df08a1998-05-22 00:51:39 +0000961 PySequenceMethods *sqf;
Guido van Rossumad991772001-01-12 16:03:05 +0000962
Guido van Rossum79f25d91997-04-29 20:08:16 +0000963 if ((sqp->seq = PyTuple_GetItem(args, i + 1)) == NULL)
Guido van Rossum12d12c51993-10-26 17:58:25 +0000964 goto Fail_2;
965
Tim Peters748b8bb2001-04-28 08:20:22 +0000966 sqp->saw_IndexError = 0;
967
Guido van Rossum09df08a1998-05-22 00:51:39 +0000968 sqp->sqf = sqf = sqp->seq->ob_type->tp_as_sequence;
969 if (sqf == NULL ||
Guido van Rossum09df08a1998-05-22 00:51:39 +0000970 sqf->sq_item == NULL)
971 {
Guido van Rossum12d12c51993-10-26 17:58:25 +0000972 static char errmsg[] =
973 "argument %d to map() must be a sequence object";
Guido van Rossum15e33a41997-04-30 19:00:27 +0000974 char errbuf[sizeof(errmsg) + 25];
Guido van Rossum12d12c51993-10-26 17:58:25 +0000975
976 sprintf(errbuf, errmsg, i+2);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000977 PyErr_SetString(PyExc_TypeError, errbuf);
Guido van Rossum12d12c51993-10-26 17:58:25 +0000978 goto Fail_2;
979 }
980
Tim Peters748b8bb2001-04-28 08:20:22 +0000981 if (sqf->sq_length == NULL)
982 /* doesn't matter -- make something up */
983 curlen = 8;
984 else
985 curlen = (*sqf->sq_length)(sqp->seq);
986 if (curlen < 0)
Guido van Rossum12d12c51993-10-26 17:58:25 +0000987 goto Fail_2;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000988 if (curlen > len)
989 len = curlen;
990 }
991
Guido van Rossum79f25d91997-04-29 20:08:16 +0000992 if ((result = (PyObject *) PyList_New(len)) == NULL)
Guido van Rossum12d12c51993-10-26 17:58:25 +0000993 goto Fail_2;
994
Tim Peters748b8bb2001-04-28 08:20:22 +0000995 /* Iterate over the sequences until all have raised IndexError. */
Guido van Rossum2d951851994-08-29 12:52:16 +0000996 for (i = 0; ; ++i) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000997 PyObject *alist, *item=NULL, *value;
Guido van Rossum2d951851994-08-29 12:52:16 +0000998 int any = 0;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000999
Guido van Rossum79f25d91997-04-29 20:08:16 +00001000 if (func == Py_None && n == 1)
Guido van Rossum32120311995-07-10 13:52:21 +00001001 alist = NULL;
Guido van Rossum2d951851994-08-29 12:52:16 +00001002 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001003 if ((alist = PyTuple_New(n)) == NULL)
Guido van Rossum2d951851994-08-29 12:52:16 +00001004 goto Fail_1;
1005 }
Guido van Rossum12d12c51993-10-26 17:58:25 +00001006
1007 for (j = 0, sqp = seqs; j < n; ++j, ++sqp) {
Tim Peters748b8bb2001-04-28 08:20:22 +00001008 if (sqp->saw_IndexError) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001009 Py_INCREF(Py_None);
1010 item = Py_None;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001011 }
Guido van Rossum12d12c51993-10-26 17:58:25 +00001012 else {
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00001013 item = (*sqp->sqf->sq_item)(sqp->seq, i);
Guido van Rossum2d951851994-08-29 12:52:16 +00001014 if (item == NULL) {
Barry Warsawcde8b1b1997-08-22 21:14:38 +00001015 if (PyErr_ExceptionMatches(
1016 PyExc_IndexError))
1017 {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001018 PyErr_Clear();
1019 Py_INCREF(Py_None);
1020 item = Py_None;
Tim Peters748b8bb2001-04-28 08:20:22 +00001021 sqp->saw_IndexError = 1;
Guido van Rossum2d951851994-08-29 12:52:16 +00001022 }
1023 else {
1024 goto Fail_0;
1025 }
1026 }
1027 else
1028 any = 1;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001029
Guido van Rossum12d12c51993-10-26 17:58:25 +00001030 }
Guido van Rossum32120311995-07-10 13:52:21 +00001031 if (!alist)
Guido van Rossum2d951851994-08-29 12:52:16 +00001032 break;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001033 if (PyTuple_SetItem(alist, j, item) < 0) {
1034 Py_DECREF(item);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00001035 goto Fail_0;
Guido van Rossum2d951851994-08-29 12:52:16 +00001036 }
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00001037 continue;
1038
1039 Fail_0:
Guido van Rossum79f25d91997-04-29 20:08:16 +00001040 Py_XDECREF(alist);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00001041 goto Fail_1;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001042 }
1043
Guido van Rossum32120311995-07-10 13:52:21 +00001044 if (!alist)
1045 alist = item;
Guido van Rossum2d951851994-08-29 12:52:16 +00001046
1047 if (!any) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001048 Py_DECREF(alist);
Guido van Rossum2d951851994-08-29 12:52:16 +00001049 break;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001050 }
Guido van Rossum2d951851994-08-29 12:52:16 +00001051
Guido van Rossum79f25d91997-04-29 20:08:16 +00001052 if (func == Py_None)
Guido van Rossum32120311995-07-10 13:52:21 +00001053 value = alist;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001054 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001055 value = PyEval_CallObject(func, alist);
1056 Py_DECREF(alist);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00001057 if (value == NULL)
1058 goto Fail_1;
Guido van Rossum2d951851994-08-29 12:52:16 +00001059 }
1060 if (i >= len) {
Barry Warsawfa77e091999-01-28 18:49:12 +00001061 int status = PyList_Append(result, value);
Barry Warsaw21332871999-01-28 04:21:35 +00001062 Py_DECREF(value);
Barry Warsawfa77e091999-01-28 18:49:12 +00001063 if (status < 0)
1064 goto Fail_1;
Guido van Rossum2d951851994-08-29 12:52:16 +00001065 }
1066 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001067 if (PyList_SetItem(result, i, value) < 0)
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00001068 goto Fail_1;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001069 }
1070 }
1071
Guido van Rossumfa4ac711998-07-10 17:37:30 +00001072 if (i < len && PyList_SetSlice(result, i, len, NULL) < 0)
1073 goto Fail_1;
1074
Guido van Rossum79f25d91997-04-29 20:08:16 +00001075 PyMem_DEL(seqs);
Guido van Rossum12d12c51993-10-26 17:58:25 +00001076 return result;
1077
Guido van Rossum12d12c51993-10-26 17:58:25 +00001078Fail_1:
Guido van Rossum79f25d91997-04-29 20:08:16 +00001079 Py_DECREF(result);
Guido van Rossum12d12c51993-10-26 17:58:25 +00001080Fail_2:
Guido van Rossum79f25d91997-04-29 20:08:16 +00001081 if (seqs) PyMem_DEL(seqs);
Guido van Rossum12d12c51993-10-26 17:58:25 +00001082 return NULL;
1083}
1084
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001085static char map_doc[] =
1086"map(function, sequence[, sequence, ...]) -> list\n\
1087\n\
1088Return a list of the results of applying the function to the items of\n\
1089the argument sequence(s). If more than one sequence is given, the\n\
1090function is called with an argument list consisting of the corresponding\n\
1091item of each sequence, substituting None for missing values when not all\n\
1092sequences have the same length. If the function is None, return a list of\n\
1093the items of the sequence (or a list of tuples if more than one sequence).";
1094
1095
Guido van Rossum79f25d91997-04-29 20:08:16 +00001096static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001097builtin_setattr(PyObject *self, PyObject *args)
Guido van Rossum33894be1992-01-27 16:53:09 +00001098{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001099 PyObject *v;
1100 PyObject *name;
1101 PyObject *value;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001102
Marc-André Lemburg691270f2000-09-18 16:22:27 +00001103 if (!PyArg_ParseTuple(args, "OOO:setattr", &v, &name, &value))
Guido van Rossum33894be1992-01-27 16:53:09 +00001104 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001105 if (PyObject_SetAttr(v, name, value) != 0)
Guido van Rossum33894be1992-01-27 16:53:09 +00001106 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001107 Py_INCREF(Py_None);
1108 return Py_None;
Guido van Rossum33894be1992-01-27 16:53:09 +00001109}
1110
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001111static char setattr_doc[] =
1112"setattr(object, name, value)\n\
1113\n\
1114Set a named attribute on an object; setattr(x, 'y', v) is equivalent to\n\
1115``x.y = v''.";
1116
1117
Guido van Rossum79f25d91997-04-29 20:08:16 +00001118static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001119builtin_delattr(PyObject *self, PyObject *args)
Guido van Rossum14144fc1994-08-29 12:53:40 +00001120{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001121 PyObject *v;
1122 PyObject *name;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001123
Marc-André Lemburg691270f2000-09-18 16:22:27 +00001124 if (!PyArg_ParseTuple(args, "OO:delattr", &v, &name))
Guido van Rossum14144fc1994-08-29 12:53:40 +00001125 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001126 if (PyObject_SetAttr(v, name, (PyObject *)NULL) != 0)
Guido van Rossum14144fc1994-08-29 12:53:40 +00001127 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001128 Py_INCREF(Py_None);
1129 return Py_None;
Guido van Rossum14144fc1994-08-29 12:53:40 +00001130}
1131
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001132static char delattr_doc[] =
Guido van Rossumdf12a591998-11-23 22:13:04 +00001133"delattr(object, name)\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001134\n\
1135Delete a named attribute on an object; delattr(x, 'y') is equivalent to\n\
1136``del x.y''.";
1137
1138
Guido van Rossum79f25d91997-04-29 20:08:16 +00001139static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001140builtin_hash(PyObject *self, PyObject *args)
Guido van Rossum9bfef441993-03-29 10:43:31 +00001141{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001142 PyObject *v;
Guido van Rossum9bfef441993-03-29 10:43:31 +00001143 long x;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001144
Guido van Rossum79f25d91997-04-29 20:08:16 +00001145 if (!PyArg_ParseTuple(args, "O:hash", &v))
Guido van Rossum9bfef441993-03-29 10:43:31 +00001146 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001147 x = PyObject_Hash(v);
Guido van Rossum9bfef441993-03-29 10:43:31 +00001148 if (x == -1)
1149 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001150 return PyInt_FromLong(x);
Guido van Rossum9bfef441993-03-29 10:43:31 +00001151}
1152
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001153static char hash_doc[] =
1154"hash(object) -> integer\n\
1155\n\
1156Return a hash value for the object. Two objects with the same value have\n\
1157the same hash value. The reverse is not necessarily true, but likely.";
1158
1159
Guido van Rossum79f25d91997-04-29 20:08:16 +00001160static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001161builtin_hex(PyObject *self, PyObject *args)
Guido van Rossum006bcd41991-10-24 14:54:44 +00001162{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001163 PyObject *v;
1164 PyNumberMethods *nb;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001165
Guido van Rossum79f25d91997-04-29 20:08:16 +00001166 if (!PyArg_ParseTuple(args, "O:hex", &v))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001167 return NULL;
Guido van Rossumad991772001-01-12 16:03:05 +00001168
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001169 if ((nb = v->ob_type->tp_as_number) == NULL ||
Guido van Rossum1899c2e1992-09-12 11:09:23 +00001170 nb->nb_hex == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001171 PyErr_SetString(PyExc_TypeError,
Guido van Rossum1899c2e1992-09-12 11:09:23 +00001172 "hex() argument can't be converted to hex");
1173 return NULL;
Guido van Rossum006bcd41991-10-24 14:54:44 +00001174 }
Guido van Rossum1899c2e1992-09-12 11:09:23 +00001175 return (*nb->nb_hex)(v);
Guido van Rossum006bcd41991-10-24 14:54:44 +00001176}
1177
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001178static char hex_doc[] =
1179"hex(number) -> string\n\
1180\n\
1181Return the hexadecimal representation of an integer or long integer.";
1182
1183
Tim Petersdbd9ba62000-07-09 03:09:57 +00001184static PyObject *builtin_raw_input(PyObject *, PyObject *);
Guido van Rossum3165fe61992-09-25 21:59:05 +00001185
Guido van Rossum79f25d91997-04-29 20:08:16 +00001186static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001187builtin_input(PyObject *self, PyObject *args)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001188{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001189 PyObject *line;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001190 char *str;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001191 PyObject *res;
1192 PyObject *globals, *locals;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001193
1194 line = builtin_raw_input(self, args);
Guido van Rossum3165fe61992-09-25 21:59:05 +00001195 if (line == NULL)
1196 return line;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001197 if (!PyArg_Parse(line, "s;embedded '\\0' in input line", &str))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001198 return NULL;
1199 while (*str == ' ' || *str == '\t')
1200 str++;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001201 globals = PyEval_GetGlobals();
1202 locals = PyEval_GetLocals();
1203 if (PyDict_GetItemString(globals, "__builtins__") == NULL) {
1204 if (PyDict_SetItemString(globals, "__builtins__",
1205 PyEval_GetBuiltins()) != 0)
Guido van Rossum6135a871995-01-09 17:53:26 +00001206 return NULL;
1207 }
Guido van Rossumb05a5c71997-05-07 17:46:13 +00001208 res = PyRun_String(str, Py_eval_input, globals, locals);
Guido van Rossum79f25d91997-04-29 20:08:16 +00001209 Py_DECREF(line);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001210 return res;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001211}
1212
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001213static char input_doc[] =
1214"input([prompt]) -> value\n\
1215\n\
1216Equivalent to eval(raw_input(prompt)).";
1217
1218
Guido van Rossume8811f81997-02-14 15:48:05 +00001219static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001220builtin_intern(PyObject *self, PyObject *args)
Guido van Rossume8811f81997-02-14 15:48:05 +00001221{
1222 PyObject *s;
Guido van Rossum43713e52000-02-29 13:59:29 +00001223 if (!PyArg_ParseTuple(args, "S:intern", &s))
Guido van Rossume8811f81997-02-14 15:48:05 +00001224 return NULL;
1225 Py_INCREF(s);
1226 PyString_InternInPlace(&s);
1227 return s;
1228}
1229
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001230static char intern_doc[] =
1231"intern(string) -> string\n\
1232\n\
1233``Intern'' the given string. This enters the string in the (global)\n\
1234table of interned strings whose purpose is to speed up dictionary lookups.\n\
1235Return the string itself or the previously interned string object with the\n\
1236same value.";
1237
1238
Guido van Rossum79f25d91997-04-29 20:08:16 +00001239static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001240builtin_int(PyObject *self, PyObject *args)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001241{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001242 PyObject *v;
Barry Warsaw226ae6c1999-10-12 19:54:53 +00001243 int base = -909; /* unlikely! */
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001244
Barry Warsaw226ae6c1999-10-12 19:54:53 +00001245 if (!PyArg_ParseTuple(args, "O|i:int", &v, &base))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001246 return NULL;
Barry Warsaw226ae6c1999-10-12 19:54:53 +00001247 if (base == -909)
1248 return PyNumber_Int(v);
Guido van Rossum9e896b32000-04-05 20:11:21 +00001249 else if (PyString_Check(v))
1250 return PyInt_FromString(PyString_AS_STRING(v), NULL, base);
1251 else if (PyUnicode_Check(v))
1252 return PyInt_FromUnicode(PyUnicode_AS_UNICODE(v),
1253 PyUnicode_GET_SIZE(v),
1254 base);
1255 else {
Barry Warsaw226ae6c1999-10-12 19:54:53 +00001256 PyErr_SetString(PyExc_TypeError,
Fred Drake661ea262000-10-24 19:57:45 +00001257 "int() can't convert non-string with explicit base");
Barry Warsaw226ae6c1999-10-12 19:54:53 +00001258 return NULL;
1259 }
Guido van Rossum3f5da241990-12-20 15:06:42 +00001260}
1261
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001262static char int_doc[] =
Barry Warsaw226ae6c1999-10-12 19:54:53 +00001263"int(x[, base]) -> integer\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001264\n\
Barry Warsaw226ae6c1999-10-12 19:54:53 +00001265Convert a string or number to an integer, if possible. A floating point\n\
1266argument will be truncated towards zero (this does not include a string\n\
1267representation of a floating point number!) When converting a string, use\n\
1268the optional base. It is an error to supply a base when converting a\n\
1269non-string.";
1270
1271
1272static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001273builtin_long(PyObject *self, PyObject *args)
Barry Warsaw226ae6c1999-10-12 19:54:53 +00001274{
1275 PyObject *v;
1276 int base = -909; /* unlikely! */
Guido van Rossumad991772001-01-12 16:03:05 +00001277
Barry Warsaw226ae6c1999-10-12 19:54:53 +00001278 if (!PyArg_ParseTuple(args, "O|i:long", &v, &base))
1279 return NULL;
1280 if (base == -909)
1281 return PyNumber_Long(v);
Guido van Rossum9e896b32000-04-05 20:11:21 +00001282 else if (PyString_Check(v))
1283 return PyLong_FromString(PyString_AS_STRING(v), NULL, base);
1284 else if (PyUnicode_Check(v))
1285 return PyLong_FromUnicode(PyUnicode_AS_UNICODE(v),
1286 PyUnicode_GET_SIZE(v),
1287 base);
1288 else {
Barry Warsaw226ae6c1999-10-12 19:54:53 +00001289 PyErr_SetString(PyExc_TypeError,
Fred Drake661ea262000-10-24 19:57:45 +00001290 "long() can't convert non-string with explicit base");
Barry Warsaw226ae6c1999-10-12 19:54:53 +00001291 return NULL;
1292 }
Barry Warsaw226ae6c1999-10-12 19:54:53 +00001293}
1294
1295static char long_doc[] =
1296"long(x) -> long integer\n\
1297long(x, base) -> long integer\n\
1298\n\
1299Convert a string or number to a long integer, if possible. A floating\n\
1300point argument will be truncated towards zero (this does not include a\n\
1301string representation of a floating point number!) When converting a\n\
1302string, use the given base. It is an error to supply a base when\n\
1303converting a non-string.";
1304
1305
1306static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001307builtin_float(PyObject *self, PyObject *args)
Barry Warsaw226ae6c1999-10-12 19:54:53 +00001308{
1309 PyObject *v;
1310
1311 if (!PyArg_ParseTuple(args, "O:float", &v))
1312 return NULL;
1313 if (PyString_Check(v))
1314 return PyFloat_FromString(v, NULL);
1315 return PyNumber_Float(v);
1316}
1317
1318static char float_doc[] =
1319"float(x) -> floating point number\n\
1320\n\
1321Convert a string or number to a floating point number, if possible.";
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001322
1323
Guido van Rossum79f25d91997-04-29 20:08:16 +00001324static PyObject *
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001325builtin_iter(PyObject *self, PyObject *args)
1326{
1327 PyObject *v, *w = NULL;
1328
1329 if (!PyArg_ParseTuple(args, "O|O:iter", &v, &w))
1330 return NULL;
1331 if (w == NULL)
1332 return PyObject_GetIter(v);
1333 if (!PyCallable_Check(v)) {
1334 PyErr_SetString(PyExc_TypeError,
1335 "iter(v, w): v must be callable");
1336 return NULL;
1337 }
1338 return PyCallIter_New(v, w);
1339}
1340
1341static char iter_doc[] =
1342"iter(collection) -> iterator\n\
1343iter(callable, sentinel) -> iterator\n\
1344\n\
1345Get an iterator from an object. In the first form, the argument must\n\
1346supply its own iterator, or be a sequence.\n\
1347In the second form, the callable is called until it returns the sentinel.";
1348
1349
1350static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001351builtin_len(PyObject *self, PyObject *args)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001352{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001353 PyObject *v;
Guido van Rossum8ea9f4d1998-06-29 22:26:50 +00001354 long res;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001355
Guido van Rossum79f25d91997-04-29 20:08:16 +00001356 if (!PyArg_ParseTuple(args, "O:len", &v))
Guido van Rossum3f5da241990-12-20 15:06:42 +00001357 return NULL;
Jeremy Hylton03657cf2000-07-12 13:05:33 +00001358 res = PyObject_Size(v);
Guido van Rossum8ea9f4d1998-06-29 22:26:50 +00001359 if (res < 0 && PyErr_Occurred())
1360 return NULL;
1361 return PyInt_FromLong(res);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001362}
1363
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001364static char len_doc[] =
1365"len(object) -> integer\n\
1366\n\
1367Return the number of items of a sequence or mapping.";
1368
1369
Guido van Rossum79f25d91997-04-29 20:08:16 +00001370static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001371builtin_list(PyObject *self, PyObject *args)
Guido van Rossumd1705771996-04-09 02:41:06 +00001372{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001373 PyObject *v;
Guido van Rossumd1705771996-04-09 02:41:06 +00001374
Guido van Rossum79f25d91997-04-29 20:08:16 +00001375 if (!PyArg_ParseTuple(args, "O:list", &v))
Guido van Rossumd1705771996-04-09 02:41:06 +00001376 return NULL;
Guido van Rossum09df08a1998-05-22 00:51:39 +00001377 return PySequence_List(v);
Guido van Rossumd1705771996-04-09 02:41:06 +00001378}
1379
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001380static char list_doc[] =
1381"list(sequence) -> list\n\
1382\n\
1383Return a new list whose items are the same as those of the argument sequence.";
1384
Guido van Rossum8861b741996-07-30 16:49:37 +00001385
1386static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001387builtin_slice(PyObject *self, PyObject *args)
Guido van Rossum8861b741996-07-30 16:49:37 +00001388{
Guido van Rossum09df08a1998-05-22 00:51:39 +00001389 PyObject *start, *stop, *step;
Guido van Rossum8861b741996-07-30 16:49:37 +00001390
Guido van Rossum09df08a1998-05-22 00:51:39 +00001391 start = stop = step = NULL;
Guido van Rossum8861b741996-07-30 16:49:37 +00001392
Guido van Rossum09df08a1998-05-22 00:51:39 +00001393 if (!PyArg_ParseTuple(args, "O|OO:slice", &start, &stop, &step))
1394 return NULL;
Guido van Rossum8861b741996-07-30 16:49:37 +00001395
Guido van Rossum09df08a1998-05-22 00:51:39 +00001396 /* This swapping of stop and start is to maintain similarity with
1397 range(). */
1398 if (stop == NULL) {
1399 stop = start;
1400 start = NULL;
1401 }
1402 return PySlice_New(start, stop, step);
Guido van Rossum8861b741996-07-30 16:49:37 +00001403}
1404
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001405static char slice_doc[] =
Fred Drake3d587441999-07-19 15:21:16 +00001406"slice([start,] stop[, step]) -> slice object\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001407\n\
1408Create a slice object. This is used for slicing by the Numeric extensions.";
1409
1410
Guido van Rossum79f25d91997-04-29 20:08:16 +00001411static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001412builtin_locals(PyObject *self, PyObject *args)
Guido van Rossum872537c1995-07-07 22:43:42 +00001413{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001414 PyObject *d;
Guido van Rossum872537c1995-07-07 22:43:42 +00001415
Guido van Rossum43713e52000-02-29 13:59:29 +00001416 if (!PyArg_ParseTuple(args, ":locals"))
Guido van Rossum872537c1995-07-07 22:43:42 +00001417 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001418 d = PyEval_GetLocals();
1419 Py_INCREF(d);
Guido van Rossum872537c1995-07-07 22:43:42 +00001420 return d;
1421}
1422
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001423static char locals_doc[] =
1424"locals() -> dictionary\n\
1425\n\
1426Return the dictionary containing the current scope's local variables.";
1427
1428
Guido van Rossum79f25d91997-04-29 20:08:16 +00001429static PyObject *
Guido van Rossum53451b32001-01-17 15:47:24 +00001430min_max(PyObject *args, int op)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001431{
Guido van Rossum2d951851994-08-29 12:52:16 +00001432 int i;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001433 PyObject *v, *w, *x;
1434 PySequenceMethods *sq;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001435
Guido van Rossum79f25d91997-04-29 20:08:16 +00001436 if (PyTuple_Size(args) > 1)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001437 v = args;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001438 else if (!PyArg_ParseTuple(args, "O:min/max", &v))
Guido van Rossum3f5da241990-12-20 15:06:42 +00001439 return NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001440 sq = v->ob_type->tp_as_sequence;
Guido van Rossum09df08a1998-05-22 00:51:39 +00001441 if (sq == NULL || sq->sq_item == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001442 PyErr_SetString(PyExc_TypeError,
Fred Drake661ea262000-10-24 19:57:45 +00001443 "min() or max() arg must be a sequence");
Guido van Rossum3f5da241990-12-20 15:06:42 +00001444 return NULL;
1445 }
Guido van Rossum2d951851994-08-29 12:52:16 +00001446 w = NULL;
1447 for (i = 0; ; i++) {
Guido van Rossum3f5da241990-12-20 15:06:42 +00001448 x = (*sq->sq_item)(v, i); /* Implies INCREF */
Guido van Rossum2d951851994-08-29 12:52:16 +00001449 if (x == NULL) {
Barry Warsawcde8b1b1997-08-22 21:14:38 +00001450 if (PyErr_ExceptionMatches(PyExc_IndexError)) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001451 PyErr_Clear();
Guido van Rossum2d951851994-08-29 12:52:16 +00001452 break;
1453 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00001454 Py_XDECREF(w);
Guido van Rossum2d951851994-08-29 12:52:16 +00001455 return NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001456 }
Guido van Rossum2d951851994-08-29 12:52:16 +00001457 if (w == NULL)
1458 w = x;
1459 else {
Guido van Rossum53451b32001-01-17 15:47:24 +00001460 int cmp = PyObject_RichCompareBool(x, w, op);
1461 if (cmp > 0) {
1462 Py_DECREF(w);
1463 w = x;
1464 }
1465 else if (cmp < 0) {
Guido van Rossumc8b6df91997-05-23 00:06:51 +00001466 Py_DECREF(x);
1467 Py_XDECREF(w);
1468 return NULL;
1469 }
Guido van Rossum2d951851994-08-29 12:52:16 +00001470 else
Guido van Rossum79f25d91997-04-29 20:08:16 +00001471 Py_DECREF(x);
Guido van Rossum2d951851994-08-29 12:52:16 +00001472 }
Guido van Rossum3f5da241990-12-20 15:06:42 +00001473 }
Guido van Rossum2d951851994-08-29 12:52:16 +00001474 if (w == NULL)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001475 PyErr_SetString(PyExc_ValueError,
Fred Drake661ea262000-10-24 19:57:45 +00001476 "min() or max() arg is an empty sequence");
Guido van Rossum3f5da241990-12-20 15:06:42 +00001477 return w;
1478}
1479
Guido van Rossum79f25d91997-04-29 20:08:16 +00001480static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001481builtin_min(PyObject *self, PyObject *v)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001482{
Guido van Rossum53451b32001-01-17 15:47:24 +00001483 return min_max(v, Py_LT);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001484}
1485
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001486static char min_doc[] =
1487"min(sequence) -> value\n\
1488min(a, b, c, ...) -> value\n\
1489\n\
1490With a single sequence argument, return its smallest item.\n\
1491With two or more arguments, return the smallest argument.";
1492
1493
Guido van Rossum79f25d91997-04-29 20:08:16 +00001494static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001495builtin_max(PyObject *self, PyObject *v)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001496{
Guido van Rossum53451b32001-01-17 15:47:24 +00001497 return min_max(v, Py_GT);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001498}
1499
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001500static char max_doc[] =
1501"max(sequence) -> value\n\
1502max(a, b, c, ...) -> value\n\
1503\n\
1504With a single sequence argument, return its largest item.\n\
1505With two or more arguments, return the largest argument.";
1506
1507
Guido van Rossum79f25d91997-04-29 20:08:16 +00001508static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001509builtin_oct(PyObject *self, PyObject *args)
Guido van Rossum006bcd41991-10-24 14:54:44 +00001510{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001511 PyObject *v;
1512 PyNumberMethods *nb;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001513
Guido van Rossum79f25d91997-04-29 20:08:16 +00001514 if (!PyArg_ParseTuple(args, "O:oct", &v))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001515 return NULL;
Guido van Rossum1899c2e1992-09-12 11:09:23 +00001516 if (v == NULL || (nb = v->ob_type->tp_as_number) == NULL ||
1517 nb->nb_oct == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001518 PyErr_SetString(PyExc_TypeError,
Guido van Rossum1899c2e1992-09-12 11:09:23 +00001519 "oct() argument can't be converted to oct");
1520 return NULL;
Guido van Rossum006bcd41991-10-24 14:54:44 +00001521 }
Guido van Rossum1899c2e1992-09-12 11:09:23 +00001522 return (*nb->nb_oct)(v);
Guido van Rossum006bcd41991-10-24 14:54:44 +00001523}
1524
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001525static char oct_doc[] =
1526"oct(number) -> string\n\
1527\n\
1528Return the octal representation of an integer or long integer.";
1529
1530
Guido van Rossum79f25d91997-04-29 20:08:16 +00001531static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001532builtin_open(PyObject *self, PyObject *args)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001533{
Guido van Rossum2d951851994-08-29 12:52:16 +00001534 char *name;
1535 char *mode = "r";
1536 int bufsize = -1;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001537 PyObject *f;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001538
Guido van Rossum79f25d91997-04-29 20:08:16 +00001539 if (!PyArg_ParseTuple(args, "s|si:open", &name, &mode, &bufsize))
Guido van Rossum3f5da241990-12-20 15:06:42 +00001540 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001541 f = PyFile_FromString(name, mode);
Guido van Rossum2d951851994-08-29 12:52:16 +00001542 if (f != NULL)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001543 PyFile_SetBufSize(f, bufsize);
Guido van Rossum2d951851994-08-29 12:52:16 +00001544 return f;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001545}
1546
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001547static char open_doc[] =
1548"open(filename[, mode[, buffering]]) -> file object\n\
1549\n\
1550Open a file. The mode can be 'r', 'w' or 'a' for reading (default),\n\
1551writing or appending. The file will be created if it doesn't exist\n\
1552when opened for writing or appending; it will be truncated when\n\
1553opened for writing. Add a 'b' to the mode for binary files.\n\
1554Add a '+' to the mode to allow simultaneous reading and writing.\n\
1555If the buffering argument is given, 0 means unbuffered, 1 means line\n\
1556buffered, and larger numbers specify the buffer size.";
1557
1558
Guido van Rossum79f25d91997-04-29 20:08:16 +00001559static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001560builtin_ord(PyObject *self, PyObject *args)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001561{
Guido van Rossum09095f32000-03-10 23:00:52 +00001562 PyObject *obj;
1563 long ord;
Jeremy Hylton394b54d2000-04-12 21:19:47 +00001564 int size;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001565
Guido van Rossum09095f32000-03-10 23:00:52 +00001566 if (!PyArg_ParseTuple(args, "O:ord", &obj))
Guido van Rossum3f5da241990-12-20 15:06:42 +00001567 return NULL;
Guido van Rossum09095f32000-03-10 23:00:52 +00001568
Jeremy Hylton394b54d2000-04-12 21:19:47 +00001569 if (PyString_Check(obj)) {
1570 size = PyString_GET_SIZE(obj);
Andrew M. Kuchling34c20cf2000-12-20 14:36:56 +00001571 if (size == 1) {
Jeremy Hylton394b54d2000-04-12 21:19:47 +00001572 ord = (long)((unsigned char)*PyString_AS_STRING(obj));
Andrew M. Kuchling34c20cf2000-12-20 14:36:56 +00001573 return PyInt_FromLong(ord);
1574 }
Jeremy Hylton394b54d2000-04-12 21:19:47 +00001575 } else if (PyUnicode_Check(obj)) {
1576 size = PyUnicode_GET_SIZE(obj);
Andrew M. Kuchling34c20cf2000-12-20 14:36:56 +00001577 if (size == 1) {
Jeremy Hylton394b54d2000-04-12 21:19:47 +00001578 ord = (long)*PyUnicode_AS_UNICODE(obj);
Andrew M. Kuchling34c20cf2000-12-20 14:36:56 +00001579 return PyInt_FromLong(ord);
1580 }
Jeremy Hylton394b54d2000-04-12 21:19:47 +00001581 } else {
1582 PyErr_Format(PyExc_TypeError,
Andrew M. Kuchlingf07aad12000-12-23 14:11:28 +00001583 "ord() expected string of length 1, but " \
Jeremy Hylton394b54d2000-04-12 21:19:47 +00001584 "%.200s found", obj->ob_type->tp_name);
Guido van Rossum09095f32000-03-10 23:00:52 +00001585 return NULL;
1586 }
1587
Guido van Rossumad991772001-01-12 16:03:05 +00001588 PyErr_Format(PyExc_TypeError,
Andrew M. Kuchlingf07aad12000-12-23 14:11:28 +00001589 "ord() expected a character, "
1590 "but string of length %d found",
Jeremy Hylton394b54d2000-04-12 21:19:47 +00001591 size);
1592 return NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001593}
1594
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001595static char ord_doc[] =
1596"ord(c) -> integer\n\
1597\n\
Guido van Rossumad991772001-01-12 16:03:05 +00001598Return the integer ordinal of a one-character string.";
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001599
1600
Guido van Rossum79f25d91997-04-29 20:08:16 +00001601static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001602builtin_pow(PyObject *self, PyObject *args)
Guido van Rossum6a00cd81995-01-07 12:39:01 +00001603{
Guido van Rossum09df08a1998-05-22 00:51:39 +00001604 PyObject *v, *w, *z = Py_None;
Guido van Rossum6a00cd81995-01-07 12:39:01 +00001605
Guido van Rossum79f25d91997-04-29 20:08:16 +00001606 if (!PyArg_ParseTuple(args, "OO|O:pow", &v, &w, &z))
Guido van Rossum6a00cd81995-01-07 12:39:01 +00001607 return NULL;
Guido van Rossum09df08a1998-05-22 00:51:39 +00001608 return PyNumber_Power(v, w, z);
Guido van Rossumd4905451991-05-05 20:00:36 +00001609}
1610
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001611static char pow_doc[] =
1612"pow(x, y[, z]) -> number\n\
1613\n\
1614With two arguments, equivalent to x**y. With three arguments,\n\
1615equivalent to (x**y) % z, but may be more efficient (e.g. for longs).";
1616
1617
Guido van Rossum124eff01999-02-23 16:11:01 +00001618/* Return number of items in range/xrange (lo, hi, step). step > 0
1619 * required. Return a value < 0 if & only if the true value is too
1620 * large to fit in a signed long.
1621 */
1622static long
Thomas Wouterse28c2962000-07-23 22:21:32 +00001623get_len_of_range(long lo, long hi, long step)
Guido van Rossum124eff01999-02-23 16:11:01 +00001624{
1625 /* -------------------------------------------------------------
1626 If lo >= hi, the range is empty.
1627 Else if n values are in the range, the last one is
1628 lo + (n-1)*step, which must be <= hi-1. Rearranging,
1629 n <= (hi - lo - 1)/step + 1, so taking the floor of the RHS gives
1630 the proper value. Since lo < hi in this case, hi-lo-1 >= 0, so
1631 the RHS is non-negative and so truncation is the same as the
1632 floor. Letting M be the largest positive long, the worst case
1633 for the RHS numerator is hi=M, lo=-M-1, and then
1634 hi-lo-1 = M-(-M-1)-1 = 2*M. Therefore unsigned long has enough
1635 precision to compute the RHS exactly.
1636 ---------------------------------------------------------------*/
1637 long n = 0;
1638 if (lo < hi) {
1639 unsigned long uhi = (unsigned long)hi;
1640 unsigned long ulo = (unsigned long)lo;
1641 unsigned long diff = uhi - ulo - 1;
1642 n = (long)(diff / (unsigned long)step + 1);
1643 }
1644 return n;
1645}
1646
Guido van Rossum79f25d91997-04-29 20:08:16 +00001647static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001648builtin_range(PyObject *self, PyObject *args)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001649{
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001650 long ilow = 0, ihigh = 0, istep = 1;
Guido van Rossum124eff01999-02-23 16:11:01 +00001651 long bign;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001652 int i, n;
Guido van Rossum124eff01999-02-23 16:11:01 +00001653
Guido van Rossum79f25d91997-04-29 20:08:16 +00001654 PyObject *v;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001655
Guido van Rossum79f25d91997-04-29 20:08:16 +00001656 if (PyTuple_Size(args) <= 1) {
1657 if (!PyArg_ParseTuple(args,
Guido van Rossum0865dd91995-01-17 16:30:22 +00001658 "l;range() requires 1-3 int arguments",
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001659 &ihigh))
1660 return NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001661 }
1662 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001663 if (!PyArg_ParseTuple(args,
Guido van Rossum0865dd91995-01-17 16:30:22 +00001664 "ll|l;range() requires 1-3 int arguments",
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001665 &ilow, &ihigh, &istep))
Guido van Rossum3f5da241990-12-20 15:06:42 +00001666 return NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001667 }
1668 if (istep == 0) {
Fred Drake661ea262000-10-24 19:57:45 +00001669 PyErr_SetString(PyExc_ValueError, "range() arg 3 must not be zero");
Guido van Rossum3f5da241990-12-20 15:06:42 +00001670 return NULL;
1671 }
Guido van Rossum124eff01999-02-23 16:11:01 +00001672 if (istep > 0)
1673 bign = get_len_of_range(ilow, ihigh, istep);
1674 else
1675 bign = get_len_of_range(ihigh, ilow, -istep);
1676 n = (int)bign;
1677 if (bign < 0 || (long)n != bign) {
Guido van Rossume23cde21999-01-12 05:07:47 +00001678 PyErr_SetString(PyExc_OverflowError,
Fred Drake661ea262000-10-24 19:57:45 +00001679 "range() result has too many items");
Guido van Rossume23cde21999-01-12 05:07:47 +00001680 return NULL;
1681 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00001682 v = PyList_New(n);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001683 if (v == NULL)
1684 return NULL;
1685 for (i = 0; i < n; i++) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001686 PyObject *w = PyInt_FromLong(ilow);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001687 if (w == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001688 Py_DECREF(v);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001689 return NULL;
1690 }
Guido van Rossuma937d141998-04-24 18:22:02 +00001691 PyList_SET_ITEM(v, i, w);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001692 ilow += istep;
1693 }
1694 return v;
1695}
1696
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001697static char range_doc[] =
1698"range([start,] stop[, step]) -> list of integers\n\
1699\n\
1700Return a list containing an arithmetic progression of integers.\n\
1701range(i, j) returns [i, i+1, i+2, ..., j-1]; start (!) defaults to 0.\n\
1702When step is given, it specifies the increment (or decrement).\n\
1703For example, range(4) returns [0, 1, 2, 3]. The end point is omitted!\n\
1704These are exactly the valid indices for a list of 4 elements.";
1705
1706
Guido van Rossum79f25d91997-04-29 20:08:16 +00001707static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001708builtin_xrange(PyObject *self, PyObject *args)
Guido van Rossum12d12c51993-10-26 17:58:25 +00001709{
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001710 long ilow = 0, ihigh = 0, istep = 1;
Guido van Rossum0865dd91995-01-17 16:30:22 +00001711 long n;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001712
Guido van Rossum79f25d91997-04-29 20:08:16 +00001713 if (PyTuple_Size(args) <= 1) {
1714 if (!PyArg_ParseTuple(args,
Guido van Rossum0865dd91995-01-17 16:30:22 +00001715 "l;xrange() requires 1-3 int arguments",
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001716 &ihigh))
1717 return NULL;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001718 }
1719 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001720 if (!PyArg_ParseTuple(args,
Guido van Rossum0865dd91995-01-17 16:30:22 +00001721 "ll|l;xrange() requires 1-3 int arguments",
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001722 &ilow, &ihigh, &istep))
Guido van Rossum12d12c51993-10-26 17:58:25 +00001723 return NULL;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001724 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001725 if (istep == 0) {
Fred Drake661ea262000-10-24 19:57:45 +00001726 PyErr_SetString(PyExc_ValueError, "xrange() arg 3 must not be zero");
Guido van Rossum12d12c51993-10-26 17:58:25 +00001727 return NULL;
1728 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001729 if (istep > 0)
Guido van Rossum124eff01999-02-23 16:11:01 +00001730 n = get_len_of_range(ilow, ihigh, istep);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001731 else
Guido van Rossum124eff01999-02-23 16:11:01 +00001732 n = get_len_of_range(ihigh, ilow, -istep);
1733 if (n < 0) {
1734 PyErr_SetString(PyExc_OverflowError,
Fred Drake661ea262000-10-24 19:57:45 +00001735 "xrange() result has too many items");
Guido van Rossum124eff01999-02-23 16:11:01 +00001736 return NULL;
1737 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00001738 return PyRange_New(ilow, n, istep, 1);
Guido van Rossum12d12c51993-10-26 17:58:25 +00001739}
1740
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001741static char xrange_doc[] =
1742"xrange([start,] stop[, step]) -> xrange object\n\
1743\n\
1744Like range(), but instead of returning a list, returns an object that\n\
1745generates the numbers in the range on demand. This is slightly slower\n\
1746than range() but more memory efficient.";
1747
1748
Guido van Rossum79f25d91997-04-29 20:08:16 +00001749static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001750builtin_raw_input(PyObject *self, PyObject *args)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001751{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001752 PyObject *v = NULL;
1753 PyObject *f;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001754
Guido van Rossum79f25d91997-04-29 20:08:16 +00001755 if (!PyArg_ParseTuple(args, "|O:[raw_]input", &v))
Guido van Rossum3165fe61992-09-25 21:59:05 +00001756 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001757 if (PyFile_AsFile(PySys_GetObject("stdin")) == stdin &&
1758 PyFile_AsFile(PySys_GetObject("stdout")) == stdout &&
Guido van Rossum53bb7ff1995-07-26 16:26:31 +00001759 isatty(fileno(stdin)) && isatty(fileno(stdout))) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001760 PyObject *po;
Guido van Rossum872537c1995-07-07 22:43:42 +00001761 char *prompt;
1762 char *s;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001763 PyObject *result;
Guido van Rossum872537c1995-07-07 22:43:42 +00001764 if (v != NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001765 po = PyObject_Str(v);
Guido van Rossum872537c1995-07-07 22:43:42 +00001766 if (po == NULL)
1767 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001768 prompt = PyString_AsString(po);
Guido van Rossumd9b52081998-06-26 18:25:38 +00001769 if (prompt == NULL)
1770 return NULL;
Guido van Rossum872537c1995-07-07 22:43:42 +00001771 }
1772 else {
1773 po = NULL;
1774 prompt = "";
1775 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00001776 s = PyOS_Readline(prompt);
1777 Py_XDECREF(po);
Guido van Rossum872537c1995-07-07 22:43:42 +00001778 if (s == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001779 PyErr_SetNone(PyExc_KeyboardInterrupt);
Guido van Rossum872537c1995-07-07 22:43:42 +00001780 return NULL;
1781 }
1782 if (*s == '\0') {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001783 PyErr_SetNone(PyExc_EOFError);
Guido van Rossum872537c1995-07-07 22:43:42 +00001784 result = NULL;
1785 }
1786 else { /* strip trailing '\n' */
Guido van Rossum106f2da2000-06-28 21:12:25 +00001787 size_t len = strlen(s);
1788 if (len > INT_MAX) {
1789 PyErr_SetString(PyExc_OverflowError, "input too long");
1790 result = NULL;
1791 }
1792 else {
1793 result = PyString_FromStringAndSize(s, (int)(len-1));
1794 }
Guido van Rossum872537c1995-07-07 22:43:42 +00001795 }
Guido van Rossumb18618d2000-05-03 23:44:39 +00001796 PyMem_FREE(s);
Guido van Rossum872537c1995-07-07 22:43:42 +00001797 return result;
1798 }
Guido van Rossum90933611991-06-07 16:10:43 +00001799 if (v != NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001800 f = PySys_GetObject("stdout");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001801 if (f == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001802 PyErr_SetString(PyExc_RuntimeError, "lost sys.stdout");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001803 return NULL;
1804 }
Guido van Rossumc8b6df91997-05-23 00:06:51 +00001805 if (Py_FlushLine() != 0 ||
1806 PyFile_WriteObject(v, f, Py_PRINT_RAW) != 0)
Guido van Rossum90933611991-06-07 16:10:43 +00001807 return NULL;
1808 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00001809 f = PySys_GetObject("stdin");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001810 if (f == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001811 PyErr_SetString(PyExc_RuntimeError, "lost sys.stdin");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001812 return NULL;
1813 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00001814 return PyFile_GetLine(f, -1);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001815}
1816
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001817static char raw_input_doc[] =
1818"raw_input([prompt]) -> string\n\
1819\n\
1820Read a string from standard input. The trailing newline is stripped.\n\
1821If the user hits EOF (Unix: Ctl-D, Windows: Ctl-Z+Return), raise EOFError.\n\
1822On Unix, GNU readline is used if enabled. The prompt string, if given,\n\
1823is printed without a trailing newline before reading.";
1824
1825
Guido van Rossum79f25d91997-04-29 20:08:16 +00001826static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001827builtin_reduce(PyObject *self, PyObject *args)
Guido van Rossum12d12c51993-10-26 17:58:25 +00001828{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001829 PyObject *seq, *func, *result = NULL;
1830 PySequenceMethods *sqf;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001831 register int i;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001832
Guido van Rossum79f25d91997-04-29 20:08:16 +00001833 if (!PyArg_ParseTuple(args, "OO|O:reduce", &func, &seq, &result))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001834 return NULL;
1835 if (result != NULL)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001836 Py_INCREF(result);
Guido van Rossum12d12c51993-10-26 17:58:25 +00001837
Guido van Rossum09df08a1998-05-22 00:51:39 +00001838 sqf = seq->ob_type->tp_as_sequence;
1839 if (sqf == NULL || sqf->sq_item == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001840 PyErr_SetString(PyExc_TypeError,
Fred Drake661ea262000-10-24 19:57:45 +00001841 "reduce() arg 2 must be a sequence");
Guido van Rossum12d12c51993-10-26 17:58:25 +00001842 return NULL;
1843 }
1844
Guido van Rossum79f25d91997-04-29 20:08:16 +00001845 if ((args = PyTuple_New(2)) == NULL)
Guido van Rossum2d951851994-08-29 12:52:16 +00001846 goto Fail;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001847
Guido van Rossum2d951851994-08-29 12:52:16 +00001848 for (i = 0; ; ++i) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001849 PyObject *op2;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001850
1851 if (args->ob_refcnt > 1) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001852 Py_DECREF(args);
1853 if ((args = PyTuple_New(2)) == NULL)
Guido van Rossum2d951851994-08-29 12:52:16 +00001854 goto Fail;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001855 }
1856
Guido van Rossum2d951851994-08-29 12:52:16 +00001857 if ((op2 = (*sqf->sq_item)(seq, i)) == NULL) {
Barry Warsawcde8b1b1997-08-22 21:14:38 +00001858 if (PyErr_ExceptionMatches(PyExc_IndexError)) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001859 PyErr_Clear();
Guido van Rossum2d951851994-08-29 12:52:16 +00001860 break;
1861 }
1862 goto Fail;
1863 }
Guido van Rossum12d12c51993-10-26 17:58:25 +00001864
Guido van Rossum2d951851994-08-29 12:52:16 +00001865 if (result == NULL)
1866 result = op2;
1867 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001868 PyTuple_SetItem(args, 0, result);
1869 PyTuple_SetItem(args, 1, op2);
1870 if ((result = PyEval_CallObject(func, args)) == NULL)
Guido van Rossum2d951851994-08-29 12:52:16 +00001871 goto Fail;
1872 }
Guido van Rossum12d12c51993-10-26 17:58:25 +00001873 }
1874
Guido van Rossum79f25d91997-04-29 20:08:16 +00001875 Py_DECREF(args);
Guido van Rossum12d12c51993-10-26 17:58:25 +00001876
Guido van Rossum2d951851994-08-29 12:52:16 +00001877 if (result == NULL)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001878 PyErr_SetString(PyExc_TypeError,
Fred Drake661ea262000-10-24 19:57:45 +00001879 "reduce() of empty sequence with no initial value");
Guido van Rossum2d951851994-08-29 12:52:16 +00001880
Guido van Rossum12d12c51993-10-26 17:58:25 +00001881 return result;
1882
Guido van Rossum2d951851994-08-29 12:52:16 +00001883Fail:
Guido van Rossum79f25d91997-04-29 20:08:16 +00001884 Py_XDECREF(args);
1885 Py_XDECREF(result);
Guido van Rossum12d12c51993-10-26 17:58:25 +00001886 return NULL;
1887}
1888
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001889static char reduce_doc[] =
1890"reduce(function, sequence[, initial]) -> value\n\
1891\n\
1892Apply a function of two arguments cumulatively to the items of a sequence,\n\
1893from left to right, so as to reduce the sequence to a single value.\n\
1894For example, reduce(lambda x, y: x+y, [1, 2, 3, 4, 5]) calculates\n\
1895((((1+2)+3)+4)+5). If initial is present, it is placed before the items\n\
1896of the sequence in the calculation, and serves as a default when the\n\
1897sequence is empty.";
1898
1899
Guido van Rossum79f25d91997-04-29 20:08:16 +00001900static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001901builtin_reload(PyObject *self, PyObject *args)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001902{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001903 PyObject *v;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001904
Guido van Rossum79f25d91997-04-29 20:08:16 +00001905 if (!PyArg_ParseTuple(args, "O:reload", &v))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001906 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001907 return PyImport_ReloadModule(v);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001908}
1909
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001910static char reload_doc[] =
1911"reload(module) -> module\n\
1912\n\
1913Reload the module. The module must have been successfully imported before.";
1914
1915
Guido van Rossum79f25d91997-04-29 20:08:16 +00001916static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001917builtin_repr(PyObject *self, PyObject *args)
Guido van Rossumc89705d1992-11-26 08:54:07 +00001918{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001919 PyObject *v;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001920
Guido van Rossum79f25d91997-04-29 20:08:16 +00001921 if (!PyArg_ParseTuple(args, "O:repr", &v))
Guido van Rossumc89705d1992-11-26 08:54:07 +00001922 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001923 return PyObject_Repr(v);
Guido van Rossumc89705d1992-11-26 08:54:07 +00001924}
1925
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001926static char repr_doc[] =
1927"repr(object) -> string\n\
1928\n\
1929Return the canonical string representation of the object.\n\
1930For most object types, eval(repr(object)) == object.";
1931
1932
Guido van Rossum79f25d91997-04-29 20:08:16 +00001933static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001934builtin_round(PyObject *self, PyObject *args)
Guido van Rossum9e51f9b1993-02-12 16:29:05 +00001935{
Guido van Rossum9e51f9b1993-02-12 16:29:05 +00001936 double x;
1937 double f;
1938 int ndigits = 0;
Guido van Rossum9e51f9b1993-02-12 16:29:05 +00001939 int i;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001940
Guido van Rossum79f25d91997-04-29 20:08:16 +00001941 if (!PyArg_ParseTuple(args, "d|i:round", &x, &ndigits))
Guido van Rossum9e51f9b1993-02-12 16:29:05 +00001942 return NULL;
Guido van Rossum9e51f9b1993-02-12 16:29:05 +00001943 f = 1.0;
Guido van Rossum1e162d31998-05-09 14:42:25 +00001944 i = abs(ndigits);
1945 while (--i >= 0)
Guido van Rossum9e51f9b1993-02-12 16:29:05 +00001946 f = f*10.0;
Guido van Rossum1e162d31998-05-09 14:42:25 +00001947 if (ndigits < 0)
1948 x /= f;
Guido van Rossum9e51f9b1993-02-12 16:29:05 +00001949 else
Guido van Rossum1e162d31998-05-09 14:42:25 +00001950 x *= f;
1951 if (x >= 0.0)
1952 x = floor(x + 0.5);
1953 else
1954 x = ceil(x - 0.5);
1955 if (ndigits < 0)
1956 x *= f;
1957 else
1958 x /= f;
1959 return PyFloat_FromDouble(x);
Guido van Rossum9e51f9b1993-02-12 16:29:05 +00001960}
1961
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001962static char round_doc[] =
1963"round(number[, ndigits]) -> floating point number\n\
1964\n\
1965Round a number to a given precision in decimal digits (default 0 digits).\n\
1966This always returns a floating point number. Precision may be negative.";
1967
1968
Guido van Rossum79f25d91997-04-29 20:08:16 +00001969static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001970builtin_str(PyObject *self, PyObject *args)
Guido van Rossumc89705d1992-11-26 08:54:07 +00001971{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001972 PyObject *v;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001973
Guido van Rossum79f25d91997-04-29 20:08:16 +00001974 if (!PyArg_ParseTuple(args, "O:str", &v))
Guido van Rossumc89705d1992-11-26 08:54:07 +00001975 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001976 return PyObject_Str(v);
Guido van Rossumc89705d1992-11-26 08:54:07 +00001977}
1978
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001979static char str_doc[] =
1980"str(object) -> string\n\
1981\n\
1982Return a nice string representation of the object.\n\
1983If the argument is a string, the return value is the same object.";
1984
1985
Guido van Rossum79f25d91997-04-29 20:08:16 +00001986static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001987builtin_tuple(PyObject *self, PyObject *args)
Guido van Rossumcae027b1994-08-29 12:53:11 +00001988{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001989 PyObject *v;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001990
Guido van Rossum79f25d91997-04-29 20:08:16 +00001991 if (!PyArg_ParseTuple(args, "O:tuple", &v))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001992 return NULL;
Guido van Rossum09df08a1998-05-22 00:51:39 +00001993 return PySequence_Tuple(v);
Guido van Rossumcae027b1994-08-29 12:53:11 +00001994}
1995
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001996static char tuple_doc[] =
1997"tuple(sequence) -> list\n\
1998\n\
1999Return a tuple whose items are the same as those of the argument sequence.\n\
2000If the argument is a tuple, the return value is the same object.";
2001
2002
Guido van Rossum79f25d91997-04-29 20:08:16 +00002003static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002004builtin_type(PyObject *self, PyObject *args)
Guido van Rossum3f5da241990-12-20 15:06:42 +00002005{
Guido van Rossum79f25d91997-04-29 20:08:16 +00002006 PyObject *v;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002007
Guido van Rossum79f25d91997-04-29 20:08:16 +00002008 if (!PyArg_ParseTuple(args, "O:type", &v))
Guido van Rossum3f5da241990-12-20 15:06:42 +00002009 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002010 v = (PyObject *)v->ob_type;
2011 Py_INCREF(v);
Guido van Rossum3f5da241990-12-20 15:06:42 +00002012 return v;
2013}
2014
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002015static char type_doc[] =
2016"type(object) -> type object\n\
2017\n\
2018Return the type of the object.";
2019
2020
Guido van Rossum79f25d91997-04-29 20:08:16 +00002021static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002022builtin_vars(PyObject *self, PyObject *args)
Guido van Rossum2d951851994-08-29 12:52:16 +00002023{
Guido van Rossum79f25d91997-04-29 20:08:16 +00002024 PyObject *v = NULL;
2025 PyObject *d;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002026
Guido van Rossum79f25d91997-04-29 20:08:16 +00002027 if (!PyArg_ParseTuple(args, "|O:vars", &v))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002028 return NULL;
Guido van Rossum2d951851994-08-29 12:52:16 +00002029 if (v == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002030 d = PyEval_GetLocals();
Guido van Rossum53bb7ff1995-07-26 16:26:31 +00002031 if (d == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002032 if (!PyErr_Occurred())
2033 PyErr_SetString(PyExc_SystemError,
2034 "no locals!?");
Guido van Rossum53bb7ff1995-07-26 16:26:31 +00002035 }
2036 else
Guido van Rossum79f25d91997-04-29 20:08:16 +00002037 Py_INCREF(d);
Guido van Rossum2d951851994-08-29 12:52:16 +00002038 }
2039 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002040 d = PyObject_GetAttrString(v, "__dict__");
Guido van Rossum2d951851994-08-29 12:52:16 +00002041 if (d == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002042 PyErr_SetString(PyExc_TypeError,
Guido van Rossum2d951851994-08-29 12:52:16 +00002043 "vars() argument must have __dict__ attribute");
2044 return NULL;
2045 }
2046 }
2047 return d;
2048}
2049
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002050static char vars_doc[] =
2051"vars([object]) -> dictionary\n\
2052\n\
2053Without arguments, equivalent to locals().\n\
2054With an argument, equivalent to object.__dict__.";
2055
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002056static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002057builtin_isinstance(PyObject *self, PyObject *args)
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002058{
2059 PyObject *inst;
2060 PyObject *cls;
Guido van Rossum823649d2001-03-21 18:40:58 +00002061 int retval;
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002062
Guido van Rossum43713e52000-02-29 13:59:29 +00002063 if (!PyArg_ParseTuple(args, "OO:isinstance", &inst, &cls))
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002064 return NULL;
Guido van Rossumf5dd9141997-12-02 19:11:45 +00002065
Guido van Rossum823649d2001-03-21 18:40:58 +00002066 retval = PyObject_IsInstance(inst, cls);
2067 if (retval < 0)
Guido van Rossumad991772001-01-12 16:03:05 +00002068 return NULL;
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002069 return PyInt_FromLong(retval);
2070}
2071
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002072static char isinstance_doc[] =
2073"isinstance(object, class-or-type) -> Boolean\n\
2074\n\
2075Return whether an object is an instance of a class or of a subclass thereof.\n\
2076With a type as second argument, return whether that is the object's type.";
2077
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002078
2079static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002080builtin_issubclass(PyObject *self, PyObject *args)
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002081{
2082 PyObject *derived;
2083 PyObject *cls;
2084 int retval;
2085
Guido van Rossum43713e52000-02-29 13:59:29 +00002086 if (!PyArg_ParseTuple(args, "OO:issubclass", &derived, &cls))
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002087 return NULL;
Guido van Rossum668213d1999-06-16 17:28:37 +00002088
Guido van Rossum823649d2001-03-21 18:40:58 +00002089 retval = PyObject_IsSubclass(derived, cls);
2090 if (retval < 0)
2091 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 issubclass_doc[] =
2096"issubclass(C, B) -> Boolean\n\
2097\n\
2098Return whether class C is a subclass (i.e., a derived class) of class B.";
2099
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002100
Barry Warsawbd599b52000-08-03 15:45:29 +00002101static PyObject*
2102builtin_zip(PyObject *self, PyObject *args)
2103{
2104 PyObject *ret;
2105 int itemsize = PySequence_Length(args);
2106 int i, j;
2107
2108 if (itemsize < 1) {
2109 PyErr_SetString(PyExc_TypeError,
Fred Drake661ea262000-10-24 19:57:45 +00002110 "zip() requires at least one sequence");
Barry Warsawbd599b52000-08-03 15:45:29 +00002111 return NULL;
2112 }
2113 /* args must be a tuple */
2114 assert(PyTuple_Check(args));
2115
2116 if ((ret = PyList_New(0)) == NULL)
2117 return NULL;
2118
2119 for (i = 0;; i++) {
2120 PyObject *next = PyTuple_New(itemsize);
2121 if (!next) {
2122 Py_DECREF(ret);
2123 return NULL;
2124 }
2125 for (j = 0; j < itemsize; j++) {
2126 PyObject *seq = PyTuple_GET_ITEM(args, j);
2127 PyObject *item = PySequence_GetItem(seq, i);
2128
2129 if (!item) {
2130 if (PyErr_ExceptionMatches(PyExc_IndexError)) {
2131 PyErr_Clear();
2132 Py_DECREF(next);
2133 return ret;
2134 }
2135 Py_DECREF(next);
2136 Py_DECREF(ret);
2137 return NULL;
2138 }
2139 PyTuple_SET_ITEM(next, j, item);
2140 }
2141 PyList_Append(ret, next);
2142 Py_DECREF(next);
2143 }
2144 /* no return */
2145}
2146
2147
2148static char zip_doc[] =
2149"zip(seq1 [, seq2 [...]]) -> [(seq1[0], seq2[0] ...), (...)]\n\
2150\n\
2151Return a list of tuples, where each tuple contains the i-th element\n\
2152from each of the argument sequences. The returned list is truncated\n\
2153in length to the length of the shortest argument sequence.";
2154
2155
Guido van Rossum79f25d91997-04-29 20:08:16 +00002156static PyMethodDef builtin_methods[] = {
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002157 {"__import__", builtin___import__, 1, import_doc},
2158 {"abs", builtin_abs, 1, abs_doc},
2159 {"apply", builtin_apply, 1, apply_doc},
Guido van Rossum0daf0221999-03-19 19:07:19 +00002160 {"buffer", builtin_buffer, 1, buffer_doc},
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002161 {"callable", builtin_callable, 1, callable_doc},
2162 {"chr", builtin_chr, 1, chr_doc},
2163 {"cmp", builtin_cmp, 1, cmp_doc},
2164 {"coerce", builtin_coerce, 1, coerce_doc},
2165 {"compile", builtin_compile, 1, compile_doc},
Guido van Rossum8a5c5d21996-01-12 01:09:56 +00002166#ifndef WITHOUT_COMPLEX
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002167 {"complex", builtin_complex, 1, complex_doc},
Guido van Rossum8a5c5d21996-01-12 01:09:56 +00002168#endif
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002169 {"delattr", builtin_delattr, 1, delattr_doc},
2170 {"dir", builtin_dir, 1, dir_doc},
2171 {"divmod", builtin_divmod, 1, divmod_doc},
2172 {"eval", builtin_eval, 1, eval_doc},
2173 {"execfile", builtin_execfile, 1, execfile_doc},
2174 {"filter", builtin_filter, 1, filter_doc},
2175 {"float", builtin_float, 1, float_doc},
2176 {"getattr", builtin_getattr, 1, getattr_doc},
2177 {"globals", builtin_globals, 1, globals_doc},
2178 {"hasattr", builtin_hasattr, 1, hasattr_doc},
2179 {"hash", builtin_hash, 1, hash_doc},
2180 {"hex", builtin_hex, 1, hex_doc},
2181 {"id", builtin_id, 1, id_doc},
2182 {"input", builtin_input, 1, input_doc},
2183 {"intern", builtin_intern, 1, intern_doc},
2184 {"int", builtin_int, 1, int_doc},
2185 {"isinstance", builtin_isinstance, 1, isinstance_doc},
2186 {"issubclass", builtin_issubclass, 1, issubclass_doc},
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002187 {"iter", builtin_iter, 1, iter_doc},
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002188 {"len", builtin_len, 1, len_doc},
2189 {"list", builtin_list, 1, list_doc},
2190 {"locals", builtin_locals, 1, locals_doc},
2191 {"long", builtin_long, 1, long_doc},
2192 {"map", builtin_map, 1, map_doc},
2193 {"max", builtin_max, 1, max_doc},
2194 {"min", builtin_min, 1, min_doc},
2195 {"oct", builtin_oct, 1, oct_doc},
2196 {"open", builtin_open, 1, open_doc},
2197 {"ord", builtin_ord, 1, ord_doc},
2198 {"pow", builtin_pow, 1, pow_doc},
2199 {"range", builtin_range, 1, range_doc},
2200 {"raw_input", builtin_raw_input, 1, raw_input_doc},
2201 {"reduce", builtin_reduce, 1, reduce_doc},
2202 {"reload", builtin_reload, 1, reload_doc},
2203 {"repr", builtin_repr, 1, repr_doc},
2204 {"round", builtin_round, 1, round_doc},
2205 {"setattr", builtin_setattr, 1, setattr_doc},
2206 {"slice", builtin_slice, 1, slice_doc},
2207 {"str", builtin_str, 1, str_doc},
2208 {"tuple", builtin_tuple, 1, tuple_doc},
2209 {"type", builtin_type, 1, type_doc},
Guido van Rossum09095f32000-03-10 23:00:52 +00002210 {"unicode", builtin_unicode, 1, unicode_doc},
2211 {"unichr", builtin_unichr, 1, unichr_doc},
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002212 {"vars", builtin_vars, 1, vars_doc},
2213 {"xrange", builtin_xrange, 1, xrange_doc},
Barry Warsawbd599b52000-08-03 15:45:29 +00002214 {"zip", builtin_zip, 1, zip_doc},
Guido van Rossumc02e15c1991-12-16 13:03:00 +00002215 {NULL, NULL},
Guido van Rossum3f5da241990-12-20 15:06:42 +00002216};
2217
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002218static char builtin_doc[] =
2219"Built-in functions, exceptions, and other objects.\n\
2220\n\
2221Noteworthy: None is the `nil' object; Ellipsis represents `...' in slices.";
2222
Guido van Rossum25ce5661997-08-02 03:10:38 +00002223PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002224_PyBuiltin_Init(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +00002225{
Fred Drake5550de32000-06-20 04:54:19 +00002226 PyObject *mod, *dict, *debug;
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002227 mod = Py_InitModule4("__builtin__", builtin_methods,
2228 builtin_doc, (PyObject *)NULL,
2229 PYTHON_API_VERSION);
Guido van Rossum25ce5661997-08-02 03:10:38 +00002230 if (mod == NULL)
2231 return NULL;
2232 dict = PyModule_GetDict(mod);
Guido van Rossum25ce5661997-08-02 03:10:38 +00002233 if (PyDict_SetItemString(dict, "None", Py_None) < 0)
2234 return NULL;
2235 if (PyDict_SetItemString(dict, "Ellipsis", Py_Ellipsis) < 0)
2236 return NULL;
Guido van Rossumad991772001-01-12 16:03:05 +00002237 if (PyDict_SetItemString(dict, "NotImplemented",
Neil Schemenauer23ab1992001-01-04 01:48:42 +00002238 Py_NotImplemented) < 0)
2239 return NULL;
Fred Drake5550de32000-06-20 04:54:19 +00002240 debug = PyInt_FromLong(Py_OptimizeFlag == 0);
2241 if (PyDict_SetItemString(dict, "__debug__", debug) < 0) {
2242 Py_XDECREF(debug);
Guido van Rossum25ce5661997-08-02 03:10:38 +00002243 return NULL;
Fred Drake5550de32000-06-20 04:54:19 +00002244 }
2245 Py_XDECREF(debug);
Barry Warsaw757af0e1997-08-29 22:13:51 +00002246
Guido van Rossum25ce5661997-08-02 03:10:38 +00002247 return mod;
Guido van Rossum3f5da241990-12-20 15:06:42 +00002248}
2249
Guido van Rossume77a7571993-11-03 15:01:26 +00002250/* Helper for filter(): filter a tuple through a function */
Guido van Rossum12d12c51993-10-26 17:58:25 +00002251
Guido van Rossum79f25d91997-04-29 20:08:16 +00002252static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002253filtertuple(PyObject *func, PyObject *tuple)
Guido van Rossum12d12c51993-10-26 17:58:25 +00002254{
Guido van Rossum79f25d91997-04-29 20:08:16 +00002255 PyObject *result;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002256 register int i, j;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002257 int len = PyTuple_Size(tuple);
Guido van Rossum12d12c51993-10-26 17:58:25 +00002258
Guido van Rossumb7b45621995-08-04 04:07:45 +00002259 if (len == 0) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002260 Py_INCREF(tuple);
Guido van Rossumb7b45621995-08-04 04:07:45 +00002261 return tuple;
2262 }
2263
Guido van Rossum79f25d91997-04-29 20:08:16 +00002264 if ((result = PyTuple_New(len)) == NULL)
Guido van Rossum2586bf01993-11-01 16:21:44 +00002265 return NULL;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002266
Guido van Rossum12d12c51993-10-26 17:58:25 +00002267 for (i = j = 0; i < len; ++i) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002268 PyObject *item, *good;
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002269 int ok;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002270
Guido van Rossum79f25d91997-04-29 20:08:16 +00002271 if ((item = PyTuple_GetItem(tuple, i)) == NULL)
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002272 goto Fail_1;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002273 if (func == Py_None) {
2274 Py_INCREF(item);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002275 good = item;
2276 }
2277 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002278 PyObject *arg = Py_BuildValue("(O)", item);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002279 if (arg == NULL)
2280 goto Fail_1;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002281 good = PyEval_CallObject(func, arg);
2282 Py_DECREF(arg);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002283 if (good == NULL)
Guido van Rossum12d12c51993-10-26 17:58:25 +00002284 goto Fail_1;
2285 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002286 ok = PyObject_IsTrue(good);
2287 Py_DECREF(good);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002288 if (ok) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002289 Py_INCREF(item);
2290 if (PyTuple_SetItem(result, j++, item) < 0)
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002291 goto Fail_1;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002292 }
Guido van Rossum12d12c51993-10-26 17:58:25 +00002293 }
2294
Guido van Rossum79f25d91997-04-29 20:08:16 +00002295 if (_PyTuple_Resize(&result, j, 0) < 0)
Guido van Rossum12d12c51993-10-26 17:58:25 +00002296 return NULL;
2297
Guido van Rossum12d12c51993-10-26 17:58:25 +00002298 return result;
2299
Guido van Rossum12d12c51993-10-26 17:58:25 +00002300Fail_1:
Guido van Rossum79f25d91997-04-29 20:08:16 +00002301 Py_DECREF(result);
Guido van Rossum12d12c51993-10-26 17:58:25 +00002302 return NULL;
2303}
2304
2305
Guido van Rossume77a7571993-11-03 15:01:26 +00002306/* Helper for filter(): filter a string through a function */
Guido van Rossum12d12c51993-10-26 17:58:25 +00002307
Guido van Rossum79f25d91997-04-29 20:08:16 +00002308static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002309filterstring(PyObject *func, PyObject *strobj)
Guido van Rossum12d12c51993-10-26 17:58:25 +00002310{
Guido van Rossum79f25d91997-04-29 20:08:16 +00002311 PyObject *result;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002312 register int i, j;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002313 int len = PyString_Size(strobj);
Guido van Rossum12d12c51993-10-26 17:58:25 +00002314
Guido van Rossum79f25d91997-04-29 20:08:16 +00002315 if (func == Py_None) {
Guido van Rossum2586bf01993-11-01 16:21:44 +00002316 /* No character is ever false -- share input string */
Guido van Rossum79f25d91997-04-29 20:08:16 +00002317 Py_INCREF(strobj);
Guido van Rossum2d951851994-08-29 12:52:16 +00002318 return strobj;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002319 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002320 if ((result = PyString_FromStringAndSize(NULL, len)) == NULL)
Guido van Rossum2586bf01993-11-01 16:21:44 +00002321 return NULL;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002322
Guido van Rossum12d12c51993-10-26 17:58:25 +00002323 for (i = j = 0; i < len; ++i) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002324 PyObject *item, *arg, *good;
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002325 int ok;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002326
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002327 item = (*strobj->ob_type->tp_as_sequence->sq_item)(strobj, i);
2328 if (item == NULL)
2329 goto Fail_1;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002330 arg = Py_BuildValue("(O)", item);
Tim Peters388ed082001-04-07 20:34:48 +00002331 if (arg == NULL) {
2332 Py_DECREF(item);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002333 goto Fail_1;
Tim Peters388ed082001-04-07 20:34:48 +00002334 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002335 good = PyEval_CallObject(func, arg);
2336 Py_DECREF(arg);
Tim Peters388ed082001-04-07 20:34:48 +00002337 if (good == NULL) {
2338 Py_DECREF(item);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002339 goto Fail_1;
Tim Peters388ed082001-04-07 20:34:48 +00002340 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002341 ok = PyObject_IsTrue(good);
2342 Py_DECREF(good);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002343 if (ok)
Guido van Rossum79f25d91997-04-29 20:08:16 +00002344 PyString_AS_STRING((PyStringObject *)result)[j++] =
2345 PyString_AS_STRING((PyStringObject *)item)[0];
Tim Peters388ed082001-04-07 20:34:48 +00002346 Py_DECREF(item);
Guido van Rossum12d12c51993-10-26 17:58:25 +00002347 }
2348
Guido van Rossum79f25d91997-04-29 20:08:16 +00002349 if (j < len && _PyString_Resize(&result, j) < 0)
Guido van Rossum12d12c51993-10-26 17:58:25 +00002350 return NULL;
2351
Guido van Rossum12d12c51993-10-26 17:58:25 +00002352 return result;
2353
Guido van Rossum12d12c51993-10-26 17:58:25 +00002354Fail_1:
Guido van Rossum79f25d91997-04-29 20:08:16 +00002355 Py_DECREF(result);
Guido van Rossum12d12c51993-10-26 17:58:25 +00002356 return NULL;
2357}