blob: 7e8f55584765caed9528dcf230036bea515c99fc [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;
819 res = PyRun_FileExFlags(fp, filename, Py_file_input, globals,
820 locals, 1, &cf);
821 } else
822 res = PyRun_FileEx(fp, filename, Py_file_input, globals,
823 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;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000927 int len;
928 } 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
Guido van Rossum2d951851994-08-29 12:52:16 +0000955 for (len = 0, i = 0, sqp = seqs; i < n; ++i, ++sqp) {
Guido van Rossum12d12c51993-10-26 17:58:25 +0000956 int curlen;
Guido van Rossum09df08a1998-05-22 00:51:39 +0000957 PySequenceMethods *sqf;
Guido van Rossumad991772001-01-12 16:03:05 +0000958
Guido van Rossum79f25d91997-04-29 20:08:16 +0000959 if ((sqp->seq = PyTuple_GetItem(args, i + 1)) == NULL)
Guido van Rossum12d12c51993-10-26 17:58:25 +0000960 goto Fail_2;
961
Guido van Rossum09df08a1998-05-22 00:51:39 +0000962 sqp->sqf = sqf = sqp->seq->ob_type->tp_as_sequence;
963 if (sqf == NULL ||
964 sqf->sq_length == NULL ||
965 sqf->sq_item == NULL)
966 {
Guido van Rossum12d12c51993-10-26 17:58:25 +0000967 static char errmsg[] =
968 "argument %d to map() must be a sequence object";
Guido van Rossum15e33a41997-04-30 19:00:27 +0000969 char errbuf[sizeof(errmsg) + 25];
Guido van Rossum12d12c51993-10-26 17:58:25 +0000970
971 sprintf(errbuf, errmsg, i+2);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000972 PyErr_SetString(PyExc_TypeError, errbuf);
Guido van Rossum12d12c51993-10-26 17:58:25 +0000973 goto Fail_2;
974 }
975
976 if ((curlen = sqp->len = (*sqp->sqf->sq_length)(sqp->seq)) < 0)
977 goto Fail_2;
978
979 if (curlen > len)
980 len = curlen;
981 }
982
Guido van Rossum79f25d91997-04-29 20:08:16 +0000983 if ((result = (PyObject *) PyList_New(len)) == NULL)
Guido van Rossum12d12c51993-10-26 17:58:25 +0000984 goto Fail_2;
985
Guido van Rossum2d951851994-08-29 12:52:16 +0000986 for (i = 0; ; ++i) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000987 PyObject *alist, *item=NULL, *value;
Guido van Rossum2d951851994-08-29 12:52:16 +0000988 int any = 0;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000989
Guido van Rossum79f25d91997-04-29 20:08:16 +0000990 if (func == Py_None && n == 1)
Guido van Rossum32120311995-07-10 13:52:21 +0000991 alist = NULL;
Guido van Rossum2d951851994-08-29 12:52:16 +0000992 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000993 if ((alist = PyTuple_New(n)) == NULL)
Guido van Rossum2d951851994-08-29 12:52:16 +0000994 goto Fail_1;
995 }
Guido van Rossum12d12c51993-10-26 17:58:25 +0000996
997 for (j = 0, sqp = seqs; j < n; ++j, ++sqp) {
Guido van Rossum2d951851994-08-29 12:52:16 +0000998 if (sqp->len < 0) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000999 Py_INCREF(Py_None);
1000 item = Py_None;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001001 }
Guido van Rossum12d12c51993-10-26 17:58:25 +00001002 else {
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00001003 item = (*sqp->sqf->sq_item)(sqp->seq, i);
Guido van Rossum2d951851994-08-29 12:52:16 +00001004 if (item == NULL) {
Barry Warsawcde8b1b1997-08-22 21:14:38 +00001005 if (PyErr_ExceptionMatches(
1006 PyExc_IndexError))
1007 {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001008 PyErr_Clear();
1009 Py_INCREF(Py_None);
1010 item = Py_None;
Guido van Rossum2d951851994-08-29 12:52:16 +00001011 sqp->len = -1;
1012 }
1013 else {
1014 goto Fail_0;
1015 }
1016 }
1017 else
1018 any = 1;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001019
Guido van Rossum12d12c51993-10-26 17:58:25 +00001020 }
Guido van Rossum32120311995-07-10 13:52:21 +00001021 if (!alist)
Guido van Rossum2d951851994-08-29 12:52:16 +00001022 break;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001023 if (PyTuple_SetItem(alist, j, item) < 0) {
1024 Py_DECREF(item);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00001025 goto Fail_0;
Guido van Rossum2d951851994-08-29 12:52:16 +00001026 }
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00001027 continue;
1028
1029 Fail_0:
Guido van Rossum79f25d91997-04-29 20:08:16 +00001030 Py_XDECREF(alist);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00001031 goto Fail_1;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001032 }
1033
Guido van Rossum32120311995-07-10 13:52:21 +00001034 if (!alist)
1035 alist = item;
Guido van Rossum2d951851994-08-29 12:52:16 +00001036
1037 if (!any) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001038 Py_DECREF(alist);
Guido van Rossum2d951851994-08-29 12:52:16 +00001039 break;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001040 }
Guido van Rossum2d951851994-08-29 12:52:16 +00001041
Guido van Rossum79f25d91997-04-29 20:08:16 +00001042 if (func == Py_None)
Guido van Rossum32120311995-07-10 13:52:21 +00001043 value = alist;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001044 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001045 value = PyEval_CallObject(func, alist);
1046 Py_DECREF(alist);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00001047 if (value == NULL)
1048 goto Fail_1;
Guido van Rossum2d951851994-08-29 12:52:16 +00001049 }
1050 if (i >= len) {
Barry Warsawfa77e091999-01-28 18:49:12 +00001051 int status = PyList_Append(result, value);
Barry Warsaw21332871999-01-28 04:21:35 +00001052 Py_DECREF(value);
Barry Warsawfa77e091999-01-28 18:49:12 +00001053 if (status < 0)
1054 goto Fail_1;
Guido van Rossum2d951851994-08-29 12:52:16 +00001055 }
1056 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001057 if (PyList_SetItem(result, i, value) < 0)
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00001058 goto Fail_1;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001059 }
1060 }
1061
Guido van Rossumfa4ac711998-07-10 17:37:30 +00001062 if (i < len && PyList_SetSlice(result, i, len, NULL) < 0)
1063 goto Fail_1;
1064
Guido van Rossum79f25d91997-04-29 20:08:16 +00001065 PyMem_DEL(seqs);
Guido van Rossum12d12c51993-10-26 17:58:25 +00001066 return result;
1067
Guido van Rossum12d12c51993-10-26 17:58:25 +00001068Fail_1:
Guido van Rossum79f25d91997-04-29 20:08:16 +00001069 Py_DECREF(result);
Guido van Rossum12d12c51993-10-26 17:58:25 +00001070Fail_2:
Guido van Rossum79f25d91997-04-29 20:08:16 +00001071 if (seqs) PyMem_DEL(seqs);
Guido van Rossum12d12c51993-10-26 17:58:25 +00001072 return NULL;
1073}
1074
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001075static char map_doc[] =
1076"map(function, sequence[, sequence, ...]) -> list\n\
1077\n\
1078Return a list of the results of applying the function to the items of\n\
1079the argument sequence(s). If more than one sequence is given, the\n\
1080function is called with an argument list consisting of the corresponding\n\
1081item of each sequence, substituting None for missing values when not all\n\
1082sequences have the same length. If the function is None, return a list of\n\
1083the items of the sequence (or a list of tuples if more than one sequence).";
1084
1085
Guido van Rossum79f25d91997-04-29 20:08:16 +00001086static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001087builtin_setattr(PyObject *self, PyObject *args)
Guido van Rossum33894be1992-01-27 16:53:09 +00001088{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001089 PyObject *v;
1090 PyObject *name;
1091 PyObject *value;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001092
Marc-André Lemburg691270f2000-09-18 16:22:27 +00001093 if (!PyArg_ParseTuple(args, "OOO:setattr", &v, &name, &value))
Guido van Rossum33894be1992-01-27 16:53:09 +00001094 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001095 if (PyObject_SetAttr(v, name, value) != 0)
Guido van Rossum33894be1992-01-27 16:53:09 +00001096 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001097 Py_INCREF(Py_None);
1098 return Py_None;
Guido van Rossum33894be1992-01-27 16:53:09 +00001099}
1100
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001101static char setattr_doc[] =
1102"setattr(object, name, value)\n\
1103\n\
1104Set a named attribute on an object; setattr(x, 'y', v) is equivalent to\n\
1105``x.y = v''.";
1106
1107
Guido van Rossum79f25d91997-04-29 20:08:16 +00001108static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001109builtin_delattr(PyObject *self, PyObject *args)
Guido van Rossum14144fc1994-08-29 12:53:40 +00001110{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001111 PyObject *v;
1112 PyObject *name;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001113
Marc-André Lemburg691270f2000-09-18 16:22:27 +00001114 if (!PyArg_ParseTuple(args, "OO:delattr", &v, &name))
Guido van Rossum14144fc1994-08-29 12:53:40 +00001115 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001116 if (PyObject_SetAttr(v, name, (PyObject *)NULL) != 0)
Guido van Rossum14144fc1994-08-29 12:53:40 +00001117 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001118 Py_INCREF(Py_None);
1119 return Py_None;
Guido van Rossum14144fc1994-08-29 12:53:40 +00001120}
1121
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001122static char delattr_doc[] =
Guido van Rossumdf12a591998-11-23 22:13:04 +00001123"delattr(object, name)\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001124\n\
1125Delete a named attribute on an object; delattr(x, 'y') is equivalent to\n\
1126``del x.y''.";
1127
1128
Guido van Rossum79f25d91997-04-29 20:08:16 +00001129static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001130builtin_hash(PyObject *self, PyObject *args)
Guido van Rossum9bfef441993-03-29 10:43:31 +00001131{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001132 PyObject *v;
Guido van Rossum9bfef441993-03-29 10:43:31 +00001133 long x;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001134
Guido van Rossum79f25d91997-04-29 20:08:16 +00001135 if (!PyArg_ParseTuple(args, "O:hash", &v))
Guido van Rossum9bfef441993-03-29 10:43:31 +00001136 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001137 x = PyObject_Hash(v);
Guido van Rossum9bfef441993-03-29 10:43:31 +00001138 if (x == -1)
1139 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001140 return PyInt_FromLong(x);
Guido van Rossum9bfef441993-03-29 10:43:31 +00001141}
1142
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001143static char hash_doc[] =
1144"hash(object) -> integer\n\
1145\n\
1146Return a hash value for the object. Two objects with the same value have\n\
1147the same hash value. The reverse is not necessarily true, but likely.";
1148
1149
Guido van Rossum79f25d91997-04-29 20:08:16 +00001150static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001151builtin_hex(PyObject *self, PyObject *args)
Guido van Rossum006bcd41991-10-24 14:54:44 +00001152{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001153 PyObject *v;
1154 PyNumberMethods *nb;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001155
Guido van Rossum79f25d91997-04-29 20:08:16 +00001156 if (!PyArg_ParseTuple(args, "O:hex", &v))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001157 return NULL;
Guido van Rossumad991772001-01-12 16:03:05 +00001158
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001159 if ((nb = v->ob_type->tp_as_number) == NULL ||
Guido van Rossum1899c2e1992-09-12 11:09:23 +00001160 nb->nb_hex == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001161 PyErr_SetString(PyExc_TypeError,
Guido van Rossum1899c2e1992-09-12 11:09:23 +00001162 "hex() argument can't be converted to hex");
1163 return NULL;
Guido van Rossum006bcd41991-10-24 14:54:44 +00001164 }
Guido van Rossum1899c2e1992-09-12 11:09:23 +00001165 return (*nb->nb_hex)(v);
Guido van Rossum006bcd41991-10-24 14:54:44 +00001166}
1167
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001168static char hex_doc[] =
1169"hex(number) -> string\n\
1170\n\
1171Return the hexadecimal representation of an integer or long integer.";
1172
1173
Tim Petersdbd9ba62000-07-09 03:09:57 +00001174static PyObject *builtin_raw_input(PyObject *, PyObject *);
Guido van Rossum3165fe61992-09-25 21:59:05 +00001175
Guido van Rossum79f25d91997-04-29 20:08:16 +00001176static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001177builtin_input(PyObject *self, PyObject *args)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001178{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001179 PyObject *line;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001180 char *str;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001181 PyObject *res;
1182 PyObject *globals, *locals;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001183
1184 line = builtin_raw_input(self, args);
Guido van Rossum3165fe61992-09-25 21:59:05 +00001185 if (line == NULL)
1186 return line;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001187 if (!PyArg_Parse(line, "s;embedded '\\0' in input line", &str))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001188 return NULL;
1189 while (*str == ' ' || *str == '\t')
1190 str++;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001191 globals = PyEval_GetGlobals();
1192 locals = PyEval_GetLocals();
1193 if (PyDict_GetItemString(globals, "__builtins__") == NULL) {
1194 if (PyDict_SetItemString(globals, "__builtins__",
1195 PyEval_GetBuiltins()) != 0)
Guido van Rossum6135a871995-01-09 17:53:26 +00001196 return NULL;
1197 }
Guido van Rossumb05a5c71997-05-07 17:46:13 +00001198 res = PyRun_String(str, Py_eval_input, globals, locals);
Guido van Rossum79f25d91997-04-29 20:08:16 +00001199 Py_DECREF(line);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001200 return res;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001201}
1202
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001203static char input_doc[] =
1204"input([prompt]) -> value\n\
1205\n\
1206Equivalent to eval(raw_input(prompt)).";
1207
1208
Guido van Rossume8811f81997-02-14 15:48:05 +00001209static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001210builtin_intern(PyObject *self, PyObject *args)
Guido van Rossume8811f81997-02-14 15:48:05 +00001211{
1212 PyObject *s;
Guido van Rossum43713e52000-02-29 13:59:29 +00001213 if (!PyArg_ParseTuple(args, "S:intern", &s))
Guido van Rossume8811f81997-02-14 15:48:05 +00001214 return NULL;
1215 Py_INCREF(s);
1216 PyString_InternInPlace(&s);
1217 return s;
1218}
1219
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001220static char intern_doc[] =
1221"intern(string) -> string\n\
1222\n\
1223``Intern'' the given string. This enters the string in the (global)\n\
1224table of interned strings whose purpose is to speed up dictionary lookups.\n\
1225Return the string itself or the previously interned string object with the\n\
1226same value.";
1227
1228
Guido van Rossum79f25d91997-04-29 20:08:16 +00001229static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001230builtin_int(PyObject *self, PyObject *args)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001231{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001232 PyObject *v;
Barry Warsaw226ae6c1999-10-12 19:54:53 +00001233 int base = -909; /* unlikely! */
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001234
Barry Warsaw226ae6c1999-10-12 19:54:53 +00001235 if (!PyArg_ParseTuple(args, "O|i:int", &v, &base))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001236 return NULL;
Barry Warsaw226ae6c1999-10-12 19:54:53 +00001237 if (base == -909)
1238 return PyNumber_Int(v);
Guido van Rossum9e896b32000-04-05 20:11:21 +00001239 else if (PyString_Check(v))
1240 return PyInt_FromString(PyString_AS_STRING(v), NULL, base);
1241 else if (PyUnicode_Check(v))
1242 return PyInt_FromUnicode(PyUnicode_AS_UNICODE(v),
1243 PyUnicode_GET_SIZE(v),
1244 base);
1245 else {
Barry Warsaw226ae6c1999-10-12 19:54:53 +00001246 PyErr_SetString(PyExc_TypeError,
Fred Drake661ea262000-10-24 19:57:45 +00001247 "int() can't convert non-string with explicit base");
Barry Warsaw226ae6c1999-10-12 19:54:53 +00001248 return NULL;
1249 }
Guido van Rossum3f5da241990-12-20 15:06:42 +00001250}
1251
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001252static char int_doc[] =
Barry Warsaw226ae6c1999-10-12 19:54:53 +00001253"int(x[, base]) -> integer\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001254\n\
Barry Warsaw226ae6c1999-10-12 19:54:53 +00001255Convert a string or number to an integer, if possible. A floating point\n\
1256argument will be truncated towards zero (this does not include a string\n\
1257representation of a floating point number!) When converting a string, use\n\
1258the optional base. It is an error to supply a base when converting a\n\
1259non-string.";
1260
1261
1262static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001263builtin_long(PyObject *self, PyObject *args)
Barry Warsaw226ae6c1999-10-12 19:54:53 +00001264{
1265 PyObject *v;
1266 int base = -909; /* unlikely! */
Guido van Rossumad991772001-01-12 16:03:05 +00001267
Barry Warsaw226ae6c1999-10-12 19:54:53 +00001268 if (!PyArg_ParseTuple(args, "O|i:long", &v, &base))
1269 return NULL;
1270 if (base == -909)
1271 return PyNumber_Long(v);
Guido van Rossum9e896b32000-04-05 20:11:21 +00001272 else if (PyString_Check(v))
1273 return PyLong_FromString(PyString_AS_STRING(v), NULL, base);
1274 else if (PyUnicode_Check(v))
1275 return PyLong_FromUnicode(PyUnicode_AS_UNICODE(v),
1276 PyUnicode_GET_SIZE(v),
1277 base);
1278 else {
Barry Warsaw226ae6c1999-10-12 19:54:53 +00001279 PyErr_SetString(PyExc_TypeError,
Fred Drake661ea262000-10-24 19:57:45 +00001280 "long() can't convert non-string with explicit base");
Barry Warsaw226ae6c1999-10-12 19:54:53 +00001281 return NULL;
1282 }
Barry Warsaw226ae6c1999-10-12 19:54:53 +00001283}
1284
1285static char long_doc[] =
1286"long(x) -> long integer\n\
1287long(x, base) -> long integer\n\
1288\n\
1289Convert a string or number to a long integer, if possible. A floating\n\
1290point argument will be truncated towards zero (this does not include a\n\
1291string representation of a floating point number!) When converting a\n\
1292string, use the given base. It is an error to supply a base when\n\
1293converting a non-string.";
1294
1295
1296static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001297builtin_float(PyObject *self, PyObject *args)
Barry Warsaw226ae6c1999-10-12 19:54:53 +00001298{
1299 PyObject *v;
1300
1301 if (!PyArg_ParseTuple(args, "O:float", &v))
1302 return NULL;
1303 if (PyString_Check(v))
1304 return PyFloat_FromString(v, NULL);
1305 return PyNumber_Float(v);
1306}
1307
1308static char float_doc[] =
1309"float(x) -> floating point number\n\
1310\n\
1311Convert a string or number to a floating point number, if possible.";
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001312
1313
Guido van Rossum79f25d91997-04-29 20:08:16 +00001314static PyObject *
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001315builtin_iter(PyObject *self, PyObject *args)
1316{
1317 PyObject *v, *w = NULL;
1318
1319 if (!PyArg_ParseTuple(args, "O|O:iter", &v, &w))
1320 return NULL;
1321 if (w == NULL)
1322 return PyObject_GetIter(v);
1323 if (!PyCallable_Check(v)) {
1324 PyErr_SetString(PyExc_TypeError,
1325 "iter(v, w): v must be callable");
1326 return NULL;
1327 }
1328 return PyCallIter_New(v, w);
1329}
1330
1331static char iter_doc[] =
1332"iter(collection) -> iterator\n\
1333iter(callable, sentinel) -> iterator\n\
1334\n\
1335Get an iterator from an object. In the first form, the argument must\n\
1336supply its own iterator, or be a sequence.\n\
1337In the second form, the callable is called until it returns the sentinel.";
1338
1339
1340static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001341builtin_len(PyObject *self, PyObject *args)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001342{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001343 PyObject *v;
Guido van Rossum8ea9f4d1998-06-29 22:26:50 +00001344 long res;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001345
Guido van Rossum79f25d91997-04-29 20:08:16 +00001346 if (!PyArg_ParseTuple(args, "O:len", &v))
Guido van Rossum3f5da241990-12-20 15:06:42 +00001347 return NULL;
Jeremy Hylton03657cf2000-07-12 13:05:33 +00001348 res = PyObject_Size(v);
Guido van Rossum8ea9f4d1998-06-29 22:26:50 +00001349 if (res < 0 && PyErr_Occurred())
1350 return NULL;
1351 return PyInt_FromLong(res);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001352}
1353
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001354static char len_doc[] =
1355"len(object) -> integer\n\
1356\n\
1357Return the number of items of a sequence or mapping.";
1358
1359
Guido van Rossum79f25d91997-04-29 20:08:16 +00001360static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001361builtin_list(PyObject *self, PyObject *args)
Guido van Rossumd1705771996-04-09 02:41:06 +00001362{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001363 PyObject *v;
Guido van Rossumd1705771996-04-09 02:41:06 +00001364
Guido van Rossum79f25d91997-04-29 20:08:16 +00001365 if (!PyArg_ParseTuple(args, "O:list", &v))
Guido van Rossumd1705771996-04-09 02:41:06 +00001366 return NULL;
Guido van Rossum09df08a1998-05-22 00:51:39 +00001367 return PySequence_List(v);
Guido van Rossumd1705771996-04-09 02:41:06 +00001368}
1369
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001370static char list_doc[] =
1371"list(sequence) -> list\n\
1372\n\
1373Return a new list whose items are the same as those of the argument sequence.";
1374
Guido van Rossum8861b741996-07-30 16:49:37 +00001375
1376static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001377builtin_slice(PyObject *self, PyObject *args)
Guido van Rossum8861b741996-07-30 16:49:37 +00001378{
Guido van Rossum09df08a1998-05-22 00:51:39 +00001379 PyObject *start, *stop, *step;
Guido van Rossum8861b741996-07-30 16:49:37 +00001380
Guido van Rossum09df08a1998-05-22 00:51:39 +00001381 start = stop = step = NULL;
Guido van Rossum8861b741996-07-30 16:49:37 +00001382
Guido van Rossum09df08a1998-05-22 00:51:39 +00001383 if (!PyArg_ParseTuple(args, "O|OO:slice", &start, &stop, &step))
1384 return NULL;
Guido van Rossum8861b741996-07-30 16:49:37 +00001385
Guido van Rossum09df08a1998-05-22 00:51:39 +00001386 /* This swapping of stop and start is to maintain similarity with
1387 range(). */
1388 if (stop == NULL) {
1389 stop = start;
1390 start = NULL;
1391 }
1392 return PySlice_New(start, stop, step);
Guido van Rossum8861b741996-07-30 16:49:37 +00001393}
1394
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001395static char slice_doc[] =
Fred Drake3d587441999-07-19 15:21:16 +00001396"slice([start,] stop[, step]) -> slice object\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001397\n\
1398Create a slice object. This is used for slicing by the Numeric extensions.";
1399
1400
Guido van Rossum79f25d91997-04-29 20:08:16 +00001401static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001402builtin_locals(PyObject *self, PyObject *args)
Guido van Rossum872537c1995-07-07 22:43:42 +00001403{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001404 PyObject *d;
Guido van Rossum872537c1995-07-07 22:43:42 +00001405
Guido van Rossum43713e52000-02-29 13:59:29 +00001406 if (!PyArg_ParseTuple(args, ":locals"))
Guido van Rossum872537c1995-07-07 22:43:42 +00001407 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001408 d = PyEval_GetLocals();
1409 Py_INCREF(d);
Guido van Rossum872537c1995-07-07 22:43:42 +00001410 return d;
1411}
1412
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001413static char locals_doc[] =
1414"locals() -> dictionary\n\
1415\n\
1416Return the dictionary containing the current scope's local variables.";
1417
1418
Guido van Rossum79f25d91997-04-29 20:08:16 +00001419static PyObject *
Guido van Rossum53451b32001-01-17 15:47:24 +00001420min_max(PyObject *args, int op)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001421{
Guido van Rossum2d951851994-08-29 12:52:16 +00001422 int i;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001423 PyObject *v, *w, *x;
1424 PySequenceMethods *sq;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001425
Guido van Rossum79f25d91997-04-29 20:08:16 +00001426 if (PyTuple_Size(args) > 1)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001427 v = args;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001428 else if (!PyArg_ParseTuple(args, "O:min/max", &v))
Guido van Rossum3f5da241990-12-20 15:06:42 +00001429 return NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001430 sq = v->ob_type->tp_as_sequence;
Guido van Rossum09df08a1998-05-22 00:51:39 +00001431 if (sq == NULL || sq->sq_item == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001432 PyErr_SetString(PyExc_TypeError,
Fred Drake661ea262000-10-24 19:57:45 +00001433 "min() or max() arg must be a sequence");
Guido van Rossum3f5da241990-12-20 15:06:42 +00001434 return NULL;
1435 }
Guido van Rossum2d951851994-08-29 12:52:16 +00001436 w = NULL;
1437 for (i = 0; ; i++) {
Guido van Rossum3f5da241990-12-20 15:06:42 +00001438 x = (*sq->sq_item)(v, i); /* Implies INCREF */
Guido van Rossum2d951851994-08-29 12:52:16 +00001439 if (x == NULL) {
Barry Warsawcde8b1b1997-08-22 21:14:38 +00001440 if (PyErr_ExceptionMatches(PyExc_IndexError)) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001441 PyErr_Clear();
Guido van Rossum2d951851994-08-29 12:52:16 +00001442 break;
1443 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00001444 Py_XDECREF(w);
Guido van Rossum2d951851994-08-29 12:52:16 +00001445 return NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001446 }
Guido van Rossum2d951851994-08-29 12:52:16 +00001447 if (w == NULL)
1448 w = x;
1449 else {
Guido van Rossum53451b32001-01-17 15:47:24 +00001450 int cmp = PyObject_RichCompareBool(x, w, op);
1451 if (cmp > 0) {
1452 Py_DECREF(w);
1453 w = x;
1454 }
1455 else if (cmp < 0) {
Guido van Rossumc8b6df91997-05-23 00:06:51 +00001456 Py_DECREF(x);
1457 Py_XDECREF(w);
1458 return NULL;
1459 }
Guido van Rossum2d951851994-08-29 12:52:16 +00001460 else
Guido van Rossum79f25d91997-04-29 20:08:16 +00001461 Py_DECREF(x);
Guido van Rossum2d951851994-08-29 12:52:16 +00001462 }
Guido van Rossum3f5da241990-12-20 15:06:42 +00001463 }
Guido van Rossum2d951851994-08-29 12:52:16 +00001464 if (w == NULL)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001465 PyErr_SetString(PyExc_ValueError,
Fred Drake661ea262000-10-24 19:57:45 +00001466 "min() or max() arg is an empty sequence");
Guido van Rossum3f5da241990-12-20 15:06:42 +00001467 return w;
1468}
1469
Guido van Rossum79f25d91997-04-29 20:08:16 +00001470static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001471builtin_min(PyObject *self, PyObject *v)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001472{
Guido van Rossum53451b32001-01-17 15:47:24 +00001473 return min_max(v, Py_LT);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001474}
1475
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001476static char min_doc[] =
1477"min(sequence) -> value\n\
1478min(a, b, c, ...) -> value\n\
1479\n\
1480With a single sequence argument, return its smallest item.\n\
1481With two or more arguments, return the smallest argument.";
1482
1483
Guido van Rossum79f25d91997-04-29 20:08:16 +00001484static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001485builtin_max(PyObject *self, PyObject *v)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001486{
Guido van Rossum53451b32001-01-17 15:47:24 +00001487 return min_max(v, Py_GT);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001488}
1489
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001490static char max_doc[] =
1491"max(sequence) -> value\n\
1492max(a, b, c, ...) -> value\n\
1493\n\
1494With a single sequence argument, return its largest item.\n\
1495With two or more arguments, return the largest argument.";
1496
1497
Guido van Rossum79f25d91997-04-29 20:08:16 +00001498static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001499builtin_oct(PyObject *self, PyObject *args)
Guido van Rossum006bcd41991-10-24 14:54:44 +00001500{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001501 PyObject *v;
1502 PyNumberMethods *nb;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001503
Guido van Rossum79f25d91997-04-29 20:08:16 +00001504 if (!PyArg_ParseTuple(args, "O:oct", &v))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001505 return NULL;
Guido van Rossum1899c2e1992-09-12 11:09:23 +00001506 if (v == NULL || (nb = v->ob_type->tp_as_number) == NULL ||
1507 nb->nb_oct == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001508 PyErr_SetString(PyExc_TypeError,
Guido van Rossum1899c2e1992-09-12 11:09:23 +00001509 "oct() argument can't be converted to oct");
1510 return NULL;
Guido van Rossum006bcd41991-10-24 14:54:44 +00001511 }
Guido van Rossum1899c2e1992-09-12 11:09:23 +00001512 return (*nb->nb_oct)(v);
Guido van Rossum006bcd41991-10-24 14:54:44 +00001513}
1514
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001515static char oct_doc[] =
1516"oct(number) -> string\n\
1517\n\
1518Return the octal representation of an integer or long integer.";
1519
1520
Guido van Rossum79f25d91997-04-29 20:08:16 +00001521static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001522builtin_open(PyObject *self, PyObject *args)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001523{
Guido van Rossum2d951851994-08-29 12:52:16 +00001524 char *name;
1525 char *mode = "r";
1526 int bufsize = -1;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001527 PyObject *f;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001528
Guido van Rossum79f25d91997-04-29 20:08:16 +00001529 if (!PyArg_ParseTuple(args, "s|si:open", &name, &mode, &bufsize))
Guido van Rossum3f5da241990-12-20 15:06:42 +00001530 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001531 f = PyFile_FromString(name, mode);
Guido van Rossum2d951851994-08-29 12:52:16 +00001532 if (f != NULL)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001533 PyFile_SetBufSize(f, bufsize);
Guido van Rossum2d951851994-08-29 12:52:16 +00001534 return f;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001535}
1536
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001537static char open_doc[] =
1538"open(filename[, mode[, buffering]]) -> file object\n\
1539\n\
1540Open a file. The mode can be 'r', 'w' or 'a' for reading (default),\n\
1541writing or appending. The file will be created if it doesn't exist\n\
1542when opened for writing or appending; it will be truncated when\n\
1543opened for writing. Add a 'b' to the mode for binary files.\n\
1544Add a '+' to the mode to allow simultaneous reading and writing.\n\
1545If the buffering argument is given, 0 means unbuffered, 1 means line\n\
1546buffered, and larger numbers specify the buffer size.";
1547
1548
Guido van Rossum79f25d91997-04-29 20:08:16 +00001549static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001550builtin_ord(PyObject *self, PyObject *args)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001551{
Guido van Rossum09095f32000-03-10 23:00:52 +00001552 PyObject *obj;
1553 long ord;
Jeremy Hylton394b54d2000-04-12 21:19:47 +00001554 int size;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001555
Guido van Rossum09095f32000-03-10 23:00:52 +00001556 if (!PyArg_ParseTuple(args, "O:ord", &obj))
Guido van Rossum3f5da241990-12-20 15:06:42 +00001557 return NULL;
Guido van Rossum09095f32000-03-10 23:00:52 +00001558
Jeremy Hylton394b54d2000-04-12 21:19:47 +00001559 if (PyString_Check(obj)) {
1560 size = PyString_GET_SIZE(obj);
Andrew M. Kuchling34c20cf2000-12-20 14:36:56 +00001561 if (size == 1) {
Jeremy Hylton394b54d2000-04-12 21:19:47 +00001562 ord = (long)((unsigned char)*PyString_AS_STRING(obj));
Andrew M. Kuchling34c20cf2000-12-20 14:36:56 +00001563 return PyInt_FromLong(ord);
1564 }
Jeremy Hylton394b54d2000-04-12 21:19:47 +00001565 } else if (PyUnicode_Check(obj)) {
1566 size = PyUnicode_GET_SIZE(obj);
Andrew M. Kuchling34c20cf2000-12-20 14:36:56 +00001567 if (size == 1) {
Jeremy Hylton394b54d2000-04-12 21:19:47 +00001568 ord = (long)*PyUnicode_AS_UNICODE(obj);
Andrew M. Kuchling34c20cf2000-12-20 14:36:56 +00001569 return PyInt_FromLong(ord);
1570 }
Jeremy Hylton394b54d2000-04-12 21:19:47 +00001571 } else {
1572 PyErr_Format(PyExc_TypeError,
Andrew M. Kuchlingf07aad12000-12-23 14:11:28 +00001573 "ord() expected string of length 1, but " \
Jeremy Hylton394b54d2000-04-12 21:19:47 +00001574 "%.200s found", obj->ob_type->tp_name);
Guido van Rossum09095f32000-03-10 23:00:52 +00001575 return NULL;
1576 }
1577
Guido van Rossumad991772001-01-12 16:03:05 +00001578 PyErr_Format(PyExc_TypeError,
Andrew M. Kuchlingf07aad12000-12-23 14:11:28 +00001579 "ord() expected a character, "
1580 "but string of length %d found",
Jeremy Hylton394b54d2000-04-12 21:19:47 +00001581 size);
1582 return NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001583}
1584
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001585static char ord_doc[] =
1586"ord(c) -> integer\n\
1587\n\
Guido van Rossumad991772001-01-12 16:03:05 +00001588Return the integer ordinal of a one-character string.";
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001589
1590
Guido van Rossum79f25d91997-04-29 20:08:16 +00001591static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001592builtin_pow(PyObject *self, PyObject *args)
Guido van Rossum6a00cd81995-01-07 12:39:01 +00001593{
Guido van Rossum09df08a1998-05-22 00:51:39 +00001594 PyObject *v, *w, *z = Py_None;
Guido van Rossum6a00cd81995-01-07 12:39:01 +00001595
Guido van Rossum79f25d91997-04-29 20:08:16 +00001596 if (!PyArg_ParseTuple(args, "OO|O:pow", &v, &w, &z))
Guido van Rossum6a00cd81995-01-07 12:39:01 +00001597 return NULL;
Guido van Rossum09df08a1998-05-22 00:51:39 +00001598 return PyNumber_Power(v, w, z);
Guido van Rossumd4905451991-05-05 20:00:36 +00001599}
1600
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001601static char pow_doc[] =
1602"pow(x, y[, z]) -> number\n\
1603\n\
1604With two arguments, equivalent to x**y. With three arguments,\n\
1605equivalent to (x**y) % z, but may be more efficient (e.g. for longs).";
1606
1607
Guido van Rossum124eff01999-02-23 16:11:01 +00001608/* Return number of items in range/xrange (lo, hi, step). step > 0
1609 * required. Return a value < 0 if & only if the true value is too
1610 * large to fit in a signed long.
1611 */
1612static long
Thomas Wouterse28c2962000-07-23 22:21:32 +00001613get_len_of_range(long lo, long hi, long step)
Guido van Rossum124eff01999-02-23 16:11:01 +00001614{
1615 /* -------------------------------------------------------------
1616 If lo >= hi, the range is empty.
1617 Else if n values are in the range, the last one is
1618 lo + (n-1)*step, which must be <= hi-1. Rearranging,
1619 n <= (hi - lo - 1)/step + 1, so taking the floor of the RHS gives
1620 the proper value. Since lo < hi in this case, hi-lo-1 >= 0, so
1621 the RHS is non-negative and so truncation is the same as the
1622 floor. Letting M be the largest positive long, the worst case
1623 for the RHS numerator is hi=M, lo=-M-1, and then
1624 hi-lo-1 = M-(-M-1)-1 = 2*M. Therefore unsigned long has enough
1625 precision to compute the RHS exactly.
1626 ---------------------------------------------------------------*/
1627 long n = 0;
1628 if (lo < hi) {
1629 unsigned long uhi = (unsigned long)hi;
1630 unsigned long ulo = (unsigned long)lo;
1631 unsigned long diff = uhi - ulo - 1;
1632 n = (long)(diff / (unsigned long)step + 1);
1633 }
1634 return n;
1635}
1636
Guido van Rossum79f25d91997-04-29 20:08:16 +00001637static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001638builtin_range(PyObject *self, PyObject *args)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001639{
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001640 long ilow = 0, ihigh = 0, istep = 1;
Guido van Rossum124eff01999-02-23 16:11:01 +00001641 long bign;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001642 int i, n;
Guido van Rossum124eff01999-02-23 16:11:01 +00001643
Guido van Rossum79f25d91997-04-29 20:08:16 +00001644 PyObject *v;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001645
Guido van Rossum79f25d91997-04-29 20:08:16 +00001646 if (PyTuple_Size(args) <= 1) {
1647 if (!PyArg_ParseTuple(args,
Guido van Rossum0865dd91995-01-17 16:30:22 +00001648 "l;range() requires 1-3 int arguments",
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001649 &ihigh))
1650 return NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001651 }
1652 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001653 if (!PyArg_ParseTuple(args,
Guido van Rossum0865dd91995-01-17 16:30:22 +00001654 "ll|l;range() requires 1-3 int arguments",
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001655 &ilow, &ihigh, &istep))
Guido van Rossum3f5da241990-12-20 15:06:42 +00001656 return NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001657 }
1658 if (istep == 0) {
Fred Drake661ea262000-10-24 19:57:45 +00001659 PyErr_SetString(PyExc_ValueError, "range() arg 3 must not be zero");
Guido van Rossum3f5da241990-12-20 15:06:42 +00001660 return NULL;
1661 }
Guido van Rossum124eff01999-02-23 16:11:01 +00001662 if (istep > 0)
1663 bign = get_len_of_range(ilow, ihigh, istep);
1664 else
1665 bign = get_len_of_range(ihigh, ilow, -istep);
1666 n = (int)bign;
1667 if (bign < 0 || (long)n != bign) {
Guido van Rossume23cde21999-01-12 05:07:47 +00001668 PyErr_SetString(PyExc_OverflowError,
Fred Drake661ea262000-10-24 19:57:45 +00001669 "range() result has too many items");
Guido van Rossume23cde21999-01-12 05:07:47 +00001670 return NULL;
1671 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00001672 v = PyList_New(n);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001673 if (v == NULL)
1674 return NULL;
1675 for (i = 0; i < n; i++) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001676 PyObject *w = PyInt_FromLong(ilow);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001677 if (w == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001678 Py_DECREF(v);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001679 return NULL;
1680 }
Guido van Rossuma937d141998-04-24 18:22:02 +00001681 PyList_SET_ITEM(v, i, w);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001682 ilow += istep;
1683 }
1684 return v;
1685}
1686
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001687static char range_doc[] =
1688"range([start,] stop[, step]) -> list of integers\n\
1689\n\
1690Return a list containing an arithmetic progression of integers.\n\
1691range(i, j) returns [i, i+1, i+2, ..., j-1]; start (!) defaults to 0.\n\
1692When step is given, it specifies the increment (or decrement).\n\
1693For example, range(4) returns [0, 1, 2, 3]. The end point is omitted!\n\
1694These are exactly the valid indices for a list of 4 elements.";
1695
1696
Guido van Rossum79f25d91997-04-29 20:08:16 +00001697static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001698builtin_xrange(PyObject *self, PyObject *args)
Guido van Rossum12d12c51993-10-26 17:58:25 +00001699{
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001700 long ilow = 0, ihigh = 0, istep = 1;
Guido van Rossum0865dd91995-01-17 16:30:22 +00001701 long n;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001702
Guido van Rossum79f25d91997-04-29 20:08:16 +00001703 if (PyTuple_Size(args) <= 1) {
1704 if (!PyArg_ParseTuple(args,
Guido van Rossum0865dd91995-01-17 16:30:22 +00001705 "l;xrange() requires 1-3 int arguments",
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001706 &ihigh))
1707 return NULL;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001708 }
1709 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001710 if (!PyArg_ParseTuple(args,
Guido van Rossum0865dd91995-01-17 16:30:22 +00001711 "ll|l;xrange() requires 1-3 int arguments",
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001712 &ilow, &ihigh, &istep))
Guido van Rossum12d12c51993-10-26 17:58:25 +00001713 return NULL;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001714 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001715 if (istep == 0) {
Fred Drake661ea262000-10-24 19:57:45 +00001716 PyErr_SetString(PyExc_ValueError, "xrange() arg 3 must not be zero");
Guido van Rossum12d12c51993-10-26 17:58:25 +00001717 return NULL;
1718 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001719 if (istep > 0)
Guido van Rossum124eff01999-02-23 16:11:01 +00001720 n = get_len_of_range(ilow, ihigh, istep);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001721 else
Guido van Rossum124eff01999-02-23 16:11:01 +00001722 n = get_len_of_range(ihigh, ilow, -istep);
1723 if (n < 0) {
1724 PyErr_SetString(PyExc_OverflowError,
Fred Drake661ea262000-10-24 19:57:45 +00001725 "xrange() result has too many items");
Guido van Rossum124eff01999-02-23 16:11:01 +00001726 return NULL;
1727 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00001728 return PyRange_New(ilow, n, istep, 1);
Guido van Rossum12d12c51993-10-26 17:58:25 +00001729}
1730
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001731static char xrange_doc[] =
1732"xrange([start,] stop[, step]) -> xrange object\n\
1733\n\
1734Like range(), but instead of returning a list, returns an object that\n\
1735generates the numbers in the range on demand. This is slightly slower\n\
1736than range() but more memory efficient.";
1737
1738
Guido van Rossum79f25d91997-04-29 20:08:16 +00001739static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001740builtin_raw_input(PyObject *self, PyObject *args)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001741{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001742 PyObject *v = NULL;
1743 PyObject *f;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001744
Guido van Rossum79f25d91997-04-29 20:08:16 +00001745 if (!PyArg_ParseTuple(args, "|O:[raw_]input", &v))
Guido van Rossum3165fe61992-09-25 21:59:05 +00001746 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001747 if (PyFile_AsFile(PySys_GetObject("stdin")) == stdin &&
1748 PyFile_AsFile(PySys_GetObject("stdout")) == stdout &&
Guido van Rossum53bb7ff1995-07-26 16:26:31 +00001749 isatty(fileno(stdin)) && isatty(fileno(stdout))) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001750 PyObject *po;
Guido van Rossum872537c1995-07-07 22:43:42 +00001751 char *prompt;
1752 char *s;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001753 PyObject *result;
Guido van Rossum872537c1995-07-07 22:43:42 +00001754 if (v != NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001755 po = PyObject_Str(v);
Guido van Rossum872537c1995-07-07 22:43:42 +00001756 if (po == NULL)
1757 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001758 prompt = PyString_AsString(po);
Guido van Rossumd9b52081998-06-26 18:25:38 +00001759 if (prompt == NULL)
1760 return NULL;
Guido van Rossum872537c1995-07-07 22:43:42 +00001761 }
1762 else {
1763 po = NULL;
1764 prompt = "";
1765 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00001766 s = PyOS_Readline(prompt);
1767 Py_XDECREF(po);
Guido van Rossum872537c1995-07-07 22:43:42 +00001768 if (s == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001769 PyErr_SetNone(PyExc_KeyboardInterrupt);
Guido van Rossum872537c1995-07-07 22:43:42 +00001770 return NULL;
1771 }
1772 if (*s == '\0') {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001773 PyErr_SetNone(PyExc_EOFError);
Guido van Rossum872537c1995-07-07 22:43:42 +00001774 result = NULL;
1775 }
1776 else { /* strip trailing '\n' */
Guido van Rossum106f2da2000-06-28 21:12:25 +00001777 size_t len = strlen(s);
1778 if (len > INT_MAX) {
1779 PyErr_SetString(PyExc_OverflowError, "input too long");
1780 result = NULL;
1781 }
1782 else {
1783 result = PyString_FromStringAndSize(s, (int)(len-1));
1784 }
Guido van Rossum872537c1995-07-07 22:43:42 +00001785 }
Guido van Rossumb18618d2000-05-03 23:44:39 +00001786 PyMem_FREE(s);
Guido van Rossum872537c1995-07-07 22:43:42 +00001787 return result;
1788 }
Guido van Rossum90933611991-06-07 16:10:43 +00001789 if (v != NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001790 f = PySys_GetObject("stdout");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001791 if (f == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001792 PyErr_SetString(PyExc_RuntimeError, "lost sys.stdout");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001793 return NULL;
1794 }
Guido van Rossumc8b6df91997-05-23 00:06:51 +00001795 if (Py_FlushLine() != 0 ||
1796 PyFile_WriteObject(v, f, Py_PRINT_RAW) != 0)
Guido van Rossum90933611991-06-07 16:10:43 +00001797 return NULL;
1798 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00001799 f = PySys_GetObject("stdin");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001800 if (f == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001801 PyErr_SetString(PyExc_RuntimeError, "lost sys.stdin");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001802 return NULL;
1803 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00001804 return PyFile_GetLine(f, -1);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001805}
1806
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001807static char raw_input_doc[] =
1808"raw_input([prompt]) -> string\n\
1809\n\
1810Read a string from standard input. The trailing newline is stripped.\n\
1811If the user hits EOF (Unix: Ctl-D, Windows: Ctl-Z+Return), raise EOFError.\n\
1812On Unix, GNU readline is used if enabled. The prompt string, if given,\n\
1813is printed without a trailing newline before reading.";
1814
1815
Guido van Rossum79f25d91997-04-29 20:08:16 +00001816static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001817builtin_reduce(PyObject *self, PyObject *args)
Guido van Rossum12d12c51993-10-26 17:58:25 +00001818{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001819 PyObject *seq, *func, *result = NULL;
1820 PySequenceMethods *sqf;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001821 register int i;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001822
Guido van Rossum79f25d91997-04-29 20:08:16 +00001823 if (!PyArg_ParseTuple(args, "OO|O:reduce", &func, &seq, &result))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001824 return NULL;
1825 if (result != NULL)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001826 Py_INCREF(result);
Guido van Rossum12d12c51993-10-26 17:58:25 +00001827
Guido van Rossum09df08a1998-05-22 00:51:39 +00001828 sqf = seq->ob_type->tp_as_sequence;
1829 if (sqf == NULL || sqf->sq_item == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001830 PyErr_SetString(PyExc_TypeError,
Fred Drake661ea262000-10-24 19:57:45 +00001831 "reduce() arg 2 must be a sequence");
Guido van Rossum12d12c51993-10-26 17:58:25 +00001832 return NULL;
1833 }
1834
Guido van Rossum79f25d91997-04-29 20:08:16 +00001835 if ((args = PyTuple_New(2)) == NULL)
Guido van Rossum2d951851994-08-29 12:52:16 +00001836 goto Fail;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001837
Guido van Rossum2d951851994-08-29 12:52:16 +00001838 for (i = 0; ; ++i) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001839 PyObject *op2;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001840
1841 if (args->ob_refcnt > 1) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001842 Py_DECREF(args);
1843 if ((args = PyTuple_New(2)) == NULL)
Guido van Rossum2d951851994-08-29 12:52:16 +00001844 goto Fail;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001845 }
1846
Guido van Rossum2d951851994-08-29 12:52:16 +00001847 if ((op2 = (*sqf->sq_item)(seq, i)) == NULL) {
Barry Warsawcde8b1b1997-08-22 21:14:38 +00001848 if (PyErr_ExceptionMatches(PyExc_IndexError)) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001849 PyErr_Clear();
Guido van Rossum2d951851994-08-29 12:52:16 +00001850 break;
1851 }
1852 goto Fail;
1853 }
Guido van Rossum12d12c51993-10-26 17:58:25 +00001854
Guido van Rossum2d951851994-08-29 12:52:16 +00001855 if (result == NULL)
1856 result = op2;
1857 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001858 PyTuple_SetItem(args, 0, result);
1859 PyTuple_SetItem(args, 1, op2);
1860 if ((result = PyEval_CallObject(func, args)) == NULL)
Guido van Rossum2d951851994-08-29 12:52:16 +00001861 goto Fail;
1862 }
Guido van Rossum12d12c51993-10-26 17:58:25 +00001863 }
1864
Guido van Rossum79f25d91997-04-29 20:08:16 +00001865 Py_DECREF(args);
Guido van Rossum12d12c51993-10-26 17:58:25 +00001866
Guido van Rossum2d951851994-08-29 12:52:16 +00001867 if (result == NULL)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001868 PyErr_SetString(PyExc_TypeError,
Fred Drake661ea262000-10-24 19:57:45 +00001869 "reduce() of empty sequence with no initial value");
Guido van Rossum2d951851994-08-29 12:52:16 +00001870
Guido van Rossum12d12c51993-10-26 17:58:25 +00001871 return result;
1872
Guido van Rossum2d951851994-08-29 12:52:16 +00001873Fail:
Guido van Rossum79f25d91997-04-29 20:08:16 +00001874 Py_XDECREF(args);
1875 Py_XDECREF(result);
Guido van Rossum12d12c51993-10-26 17:58:25 +00001876 return NULL;
1877}
1878
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001879static char reduce_doc[] =
1880"reduce(function, sequence[, initial]) -> value\n\
1881\n\
1882Apply a function of two arguments cumulatively to the items of a sequence,\n\
1883from left to right, so as to reduce the sequence to a single value.\n\
1884For example, reduce(lambda x, y: x+y, [1, 2, 3, 4, 5]) calculates\n\
1885((((1+2)+3)+4)+5). If initial is present, it is placed before the items\n\
1886of the sequence in the calculation, and serves as a default when the\n\
1887sequence is empty.";
1888
1889
Guido van Rossum79f25d91997-04-29 20:08:16 +00001890static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001891builtin_reload(PyObject *self, PyObject *args)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001892{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001893 PyObject *v;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001894
Guido van Rossum79f25d91997-04-29 20:08:16 +00001895 if (!PyArg_ParseTuple(args, "O:reload", &v))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001896 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001897 return PyImport_ReloadModule(v);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001898}
1899
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001900static char reload_doc[] =
1901"reload(module) -> module\n\
1902\n\
1903Reload the module. The module must have been successfully imported before.";
1904
1905
Guido van Rossum79f25d91997-04-29 20:08:16 +00001906static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001907builtin_repr(PyObject *self, PyObject *args)
Guido van Rossumc89705d1992-11-26 08:54:07 +00001908{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001909 PyObject *v;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001910
Guido van Rossum79f25d91997-04-29 20:08:16 +00001911 if (!PyArg_ParseTuple(args, "O:repr", &v))
Guido van Rossumc89705d1992-11-26 08:54:07 +00001912 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001913 return PyObject_Repr(v);
Guido van Rossumc89705d1992-11-26 08:54:07 +00001914}
1915
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001916static char repr_doc[] =
1917"repr(object) -> string\n\
1918\n\
1919Return the canonical string representation of the object.\n\
1920For most object types, eval(repr(object)) == object.";
1921
1922
Guido van Rossum79f25d91997-04-29 20:08:16 +00001923static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001924builtin_round(PyObject *self, PyObject *args)
Guido van Rossum9e51f9b1993-02-12 16:29:05 +00001925{
Guido van Rossum9e51f9b1993-02-12 16:29:05 +00001926 double x;
1927 double f;
1928 int ndigits = 0;
Guido van Rossum9e51f9b1993-02-12 16:29:05 +00001929 int i;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001930
Guido van Rossum79f25d91997-04-29 20:08:16 +00001931 if (!PyArg_ParseTuple(args, "d|i:round", &x, &ndigits))
Guido van Rossum9e51f9b1993-02-12 16:29:05 +00001932 return NULL;
Guido van Rossum9e51f9b1993-02-12 16:29:05 +00001933 f = 1.0;
Guido van Rossum1e162d31998-05-09 14:42:25 +00001934 i = abs(ndigits);
1935 while (--i >= 0)
Guido van Rossum9e51f9b1993-02-12 16:29:05 +00001936 f = f*10.0;
Guido van Rossum1e162d31998-05-09 14:42:25 +00001937 if (ndigits < 0)
1938 x /= f;
Guido van Rossum9e51f9b1993-02-12 16:29:05 +00001939 else
Guido van Rossum1e162d31998-05-09 14:42:25 +00001940 x *= f;
1941 if (x >= 0.0)
1942 x = floor(x + 0.5);
1943 else
1944 x = ceil(x - 0.5);
1945 if (ndigits < 0)
1946 x *= f;
1947 else
1948 x /= f;
1949 return PyFloat_FromDouble(x);
Guido van Rossum9e51f9b1993-02-12 16:29:05 +00001950}
1951
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001952static char round_doc[] =
1953"round(number[, ndigits]) -> floating point number\n\
1954\n\
1955Round a number to a given precision in decimal digits (default 0 digits).\n\
1956This always returns a floating point number. Precision may be negative.";
1957
1958
Guido van Rossum79f25d91997-04-29 20:08:16 +00001959static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001960builtin_str(PyObject *self, PyObject *args)
Guido van Rossumc89705d1992-11-26 08:54:07 +00001961{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001962 PyObject *v;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001963
Guido van Rossum79f25d91997-04-29 20:08:16 +00001964 if (!PyArg_ParseTuple(args, "O:str", &v))
Guido van Rossumc89705d1992-11-26 08:54:07 +00001965 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001966 return PyObject_Str(v);
Guido van Rossumc89705d1992-11-26 08:54:07 +00001967}
1968
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001969static char str_doc[] =
1970"str(object) -> string\n\
1971\n\
1972Return a nice string representation of the object.\n\
1973If the argument is a string, the return value is the same object.";
1974
1975
Guido van Rossum79f25d91997-04-29 20:08:16 +00001976static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001977builtin_tuple(PyObject *self, PyObject *args)
Guido van Rossumcae027b1994-08-29 12:53:11 +00001978{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001979 PyObject *v;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001980
Guido van Rossum79f25d91997-04-29 20:08:16 +00001981 if (!PyArg_ParseTuple(args, "O:tuple", &v))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001982 return NULL;
Guido van Rossum09df08a1998-05-22 00:51:39 +00001983 return PySequence_Tuple(v);
Guido van Rossumcae027b1994-08-29 12:53:11 +00001984}
1985
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001986static char tuple_doc[] =
1987"tuple(sequence) -> list\n\
1988\n\
1989Return a tuple whose items are the same as those of the argument sequence.\n\
1990If the argument is a tuple, the return value is the same object.";
1991
1992
Guido van Rossum79f25d91997-04-29 20:08:16 +00001993static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001994builtin_type(PyObject *self, PyObject *args)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001995{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001996 PyObject *v;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001997
Guido van Rossum79f25d91997-04-29 20:08:16 +00001998 if (!PyArg_ParseTuple(args, "O:type", &v))
Guido van Rossum3f5da241990-12-20 15:06:42 +00001999 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002000 v = (PyObject *)v->ob_type;
2001 Py_INCREF(v);
Guido van Rossum3f5da241990-12-20 15:06:42 +00002002 return v;
2003}
2004
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002005static char type_doc[] =
2006"type(object) -> type object\n\
2007\n\
2008Return the type of the object.";
2009
2010
Guido van Rossum79f25d91997-04-29 20:08:16 +00002011static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002012builtin_vars(PyObject *self, PyObject *args)
Guido van Rossum2d951851994-08-29 12:52:16 +00002013{
Guido van Rossum79f25d91997-04-29 20:08:16 +00002014 PyObject *v = NULL;
2015 PyObject *d;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002016
Guido van Rossum79f25d91997-04-29 20:08:16 +00002017 if (!PyArg_ParseTuple(args, "|O:vars", &v))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002018 return NULL;
Guido van Rossum2d951851994-08-29 12:52:16 +00002019 if (v == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002020 d = PyEval_GetLocals();
Guido van Rossum53bb7ff1995-07-26 16:26:31 +00002021 if (d == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002022 if (!PyErr_Occurred())
2023 PyErr_SetString(PyExc_SystemError,
2024 "no locals!?");
Guido van Rossum53bb7ff1995-07-26 16:26:31 +00002025 }
2026 else
Guido van Rossum79f25d91997-04-29 20:08:16 +00002027 Py_INCREF(d);
Guido van Rossum2d951851994-08-29 12:52:16 +00002028 }
2029 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002030 d = PyObject_GetAttrString(v, "__dict__");
Guido van Rossum2d951851994-08-29 12:52:16 +00002031 if (d == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002032 PyErr_SetString(PyExc_TypeError,
Guido van Rossum2d951851994-08-29 12:52:16 +00002033 "vars() argument must have __dict__ attribute");
2034 return NULL;
2035 }
2036 }
2037 return d;
2038}
2039
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002040static char vars_doc[] =
2041"vars([object]) -> dictionary\n\
2042\n\
2043Without arguments, equivalent to locals().\n\
2044With an argument, equivalent to object.__dict__.";
2045
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002046static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002047builtin_isinstance(PyObject *self, PyObject *args)
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002048{
2049 PyObject *inst;
2050 PyObject *cls;
Guido van Rossum823649d2001-03-21 18:40:58 +00002051 int retval;
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002052
Guido van Rossum43713e52000-02-29 13:59:29 +00002053 if (!PyArg_ParseTuple(args, "OO:isinstance", &inst, &cls))
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002054 return NULL;
Guido van Rossumf5dd9141997-12-02 19:11:45 +00002055
Guido van Rossum823649d2001-03-21 18:40:58 +00002056 retval = PyObject_IsInstance(inst, cls);
2057 if (retval < 0)
Guido van Rossumad991772001-01-12 16:03:05 +00002058 return NULL;
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002059 return PyInt_FromLong(retval);
2060}
2061
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002062static char isinstance_doc[] =
2063"isinstance(object, class-or-type) -> Boolean\n\
2064\n\
2065Return whether an object is an instance of a class or of a subclass thereof.\n\
2066With a type as second argument, return whether that is the object's type.";
2067
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002068
2069static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002070builtin_issubclass(PyObject *self, PyObject *args)
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002071{
2072 PyObject *derived;
2073 PyObject *cls;
2074 int retval;
2075
Guido van Rossum43713e52000-02-29 13:59:29 +00002076 if (!PyArg_ParseTuple(args, "OO:issubclass", &derived, &cls))
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002077 return NULL;
Guido van Rossum668213d1999-06-16 17:28:37 +00002078
Guido van Rossum823649d2001-03-21 18:40:58 +00002079 retval = PyObject_IsSubclass(derived, cls);
2080 if (retval < 0)
2081 return NULL;
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002082 return PyInt_FromLong(retval);
2083}
2084
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002085static char issubclass_doc[] =
2086"issubclass(C, B) -> Boolean\n\
2087\n\
2088Return whether class C is a subclass (i.e., a derived class) of class B.";
2089
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002090
Barry Warsawbd599b52000-08-03 15:45:29 +00002091static PyObject*
2092builtin_zip(PyObject *self, PyObject *args)
2093{
2094 PyObject *ret;
2095 int itemsize = PySequence_Length(args);
2096 int i, j;
2097
2098 if (itemsize < 1) {
2099 PyErr_SetString(PyExc_TypeError,
Fred Drake661ea262000-10-24 19:57:45 +00002100 "zip() requires at least one sequence");
Barry Warsawbd599b52000-08-03 15:45:29 +00002101 return NULL;
2102 }
2103 /* args must be a tuple */
2104 assert(PyTuple_Check(args));
2105
2106 if ((ret = PyList_New(0)) == NULL)
2107 return NULL;
2108
2109 for (i = 0;; i++) {
2110 PyObject *next = PyTuple_New(itemsize);
2111 if (!next) {
2112 Py_DECREF(ret);
2113 return NULL;
2114 }
2115 for (j = 0; j < itemsize; j++) {
2116 PyObject *seq = PyTuple_GET_ITEM(args, j);
2117 PyObject *item = PySequence_GetItem(seq, i);
2118
2119 if (!item) {
2120 if (PyErr_ExceptionMatches(PyExc_IndexError)) {
2121 PyErr_Clear();
2122 Py_DECREF(next);
2123 return ret;
2124 }
2125 Py_DECREF(next);
2126 Py_DECREF(ret);
2127 return NULL;
2128 }
2129 PyTuple_SET_ITEM(next, j, item);
2130 }
2131 PyList_Append(ret, next);
2132 Py_DECREF(next);
2133 }
2134 /* no return */
2135}
2136
2137
2138static char zip_doc[] =
2139"zip(seq1 [, seq2 [...]]) -> [(seq1[0], seq2[0] ...), (...)]\n\
2140\n\
2141Return a list of tuples, where each tuple contains the i-th element\n\
2142from each of the argument sequences. The returned list is truncated\n\
2143in length to the length of the shortest argument sequence.";
2144
2145
Guido van Rossum79f25d91997-04-29 20:08:16 +00002146static PyMethodDef builtin_methods[] = {
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002147 {"__import__", builtin___import__, 1, import_doc},
2148 {"abs", builtin_abs, 1, abs_doc},
2149 {"apply", builtin_apply, 1, apply_doc},
Guido van Rossum0daf0221999-03-19 19:07:19 +00002150 {"buffer", builtin_buffer, 1, buffer_doc},
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002151 {"callable", builtin_callable, 1, callable_doc},
2152 {"chr", builtin_chr, 1, chr_doc},
2153 {"cmp", builtin_cmp, 1, cmp_doc},
2154 {"coerce", builtin_coerce, 1, coerce_doc},
2155 {"compile", builtin_compile, 1, compile_doc},
Guido van Rossum8a5c5d21996-01-12 01:09:56 +00002156#ifndef WITHOUT_COMPLEX
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002157 {"complex", builtin_complex, 1, complex_doc},
Guido van Rossum8a5c5d21996-01-12 01:09:56 +00002158#endif
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002159 {"delattr", builtin_delattr, 1, delattr_doc},
2160 {"dir", builtin_dir, 1, dir_doc},
2161 {"divmod", builtin_divmod, 1, divmod_doc},
2162 {"eval", builtin_eval, 1, eval_doc},
2163 {"execfile", builtin_execfile, 1, execfile_doc},
2164 {"filter", builtin_filter, 1, filter_doc},
2165 {"float", builtin_float, 1, float_doc},
2166 {"getattr", builtin_getattr, 1, getattr_doc},
2167 {"globals", builtin_globals, 1, globals_doc},
2168 {"hasattr", builtin_hasattr, 1, hasattr_doc},
2169 {"hash", builtin_hash, 1, hash_doc},
2170 {"hex", builtin_hex, 1, hex_doc},
2171 {"id", builtin_id, 1, id_doc},
2172 {"input", builtin_input, 1, input_doc},
2173 {"intern", builtin_intern, 1, intern_doc},
2174 {"int", builtin_int, 1, int_doc},
2175 {"isinstance", builtin_isinstance, 1, isinstance_doc},
2176 {"issubclass", builtin_issubclass, 1, issubclass_doc},
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002177 {"iter", builtin_iter, 1, iter_doc},
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002178 {"len", builtin_len, 1, len_doc},
2179 {"list", builtin_list, 1, list_doc},
2180 {"locals", builtin_locals, 1, locals_doc},
2181 {"long", builtin_long, 1, long_doc},
2182 {"map", builtin_map, 1, map_doc},
2183 {"max", builtin_max, 1, max_doc},
2184 {"min", builtin_min, 1, min_doc},
2185 {"oct", builtin_oct, 1, oct_doc},
2186 {"open", builtin_open, 1, open_doc},
2187 {"ord", builtin_ord, 1, ord_doc},
2188 {"pow", builtin_pow, 1, pow_doc},
2189 {"range", builtin_range, 1, range_doc},
2190 {"raw_input", builtin_raw_input, 1, raw_input_doc},
2191 {"reduce", builtin_reduce, 1, reduce_doc},
2192 {"reload", builtin_reload, 1, reload_doc},
2193 {"repr", builtin_repr, 1, repr_doc},
2194 {"round", builtin_round, 1, round_doc},
2195 {"setattr", builtin_setattr, 1, setattr_doc},
2196 {"slice", builtin_slice, 1, slice_doc},
2197 {"str", builtin_str, 1, str_doc},
2198 {"tuple", builtin_tuple, 1, tuple_doc},
2199 {"type", builtin_type, 1, type_doc},
Guido van Rossum09095f32000-03-10 23:00:52 +00002200 {"unicode", builtin_unicode, 1, unicode_doc},
2201 {"unichr", builtin_unichr, 1, unichr_doc},
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002202 {"vars", builtin_vars, 1, vars_doc},
2203 {"xrange", builtin_xrange, 1, xrange_doc},
Barry Warsawbd599b52000-08-03 15:45:29 +00002204 {"zip", builtin_zip, 1, zip_doc},
Guido van Rossumc02e15c1991-12-16 13:03:00 +00002205 {NULL, NULL},
Guido van Rossum3f5da241990-12-20 15:06:42 +00002206};
2207
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002208static char builtin_doc[] =
2209"Built-in functions, exceptions, and other objects.\n\
2210\n\
2211Noteworthy: None is the `nil' object; Ellipsis represents `...' in slices.";
2212
Guido van Rossum25ce5661997-08-02 03:10:38 +00002213PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002214_PyBuiltin_Init(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +00002215{
Fred Drake5550de32000-06-20 04:54:19 +00002216 PyObject *mod, *dict, *debug;
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002217 mod = Py_InitModule4("__builtin__", builtin_methods,
2218 builtin_doc, (PyObject *)NULL,
2219 PYTHON_API_VERSION);
Guido van Rossum25ce5661997-08-02 03:10:38 +00002220 if (mod == NULL)
2221 return NULL;
2222 dict = PyModule_GetDict(mod);
Guido van Rossum25ce5661997-08-02 03:10:38 +00002223 if (PyDict_SetItemString(dict, "None", Py_None) < 0)
2224 return NULL;
2225 if (PyDict_SetItemString(dict, "Ellipsis", Py_Ellipsis) < 0)
2226 return NULL;
Guido van Rossumad991772001-01-12 16:03:05 +00002227 if (PyDict_SetItemString(dict, "NotImplemented",
Neil Schemenauer23ab1992001-01-04 01:48:42 +00002228 Py_NotImplemented) < 0)
2229 return NULL;
Fred Drake5550de32000-06-20 04:54:19 +00002230 debug = PyInt_FromLong(Py_OptimizeFlag == 0);
2231 if (PyDict_SetItemString(dict, "__debug__", debug) < 0) {
2232 Py_XDECREF(debug);
Guido van Rossum25ce5661997-08-02 03:10:38 +00002233 return NULL;
Fred Drake5550de32000-06-20 04:54:19 +00002234 }
2235 Py_XDECREF(debug);
Barry Warsaw757af0e1997-08-29 22:13:51 +00002236
Guido van Rossum25ce5661997-08-02 03:10:38 +00002237 return mod;
Guido van Rossum3f5da241990-12-20 15:06:42 +00002238}
2239
Guido van Rossume77a7571993-11-03 15:01:26 +00002240/* Helper for filter(): filter a tuple through a function */
Guido van Rossum12d12c51993-10-26 17:58:25 +00002241
Guido van Rossum79f25d91997-04-29 20:08:16 +00002242static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002243filtertuple(PyObject *func, PyObject *tuple)
Guido van Rossum12d12c51993-10-26 17:58:25 +00002244{
Guido van Rossum79f25d91997-04-29 20:08:16 +00002245 PyObject *result;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002246 register int i, j;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002247 int len = PyTuple_Size(tuple);
Guido van Rossum12d12c51993-10-26 17:58:25 +00002248
Guido van Rossumb7b45621995-08-04 04:07:45 +00002249 if (len == 0) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002250 Py_INCREF(tuple);
Guido van Rossumb7b45621995-08-04 04:07:45 +00002251 return tuple;
2252 }
2253
Guido van Rossum79f25d91997-04-29 20:08:16 +00002254 if ((result = PyTuple_New(len)) == NULL)
Guido van Rossum2586bf01993-11-01 16:21:44 +00002255 return NULL;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002256
Guido van Rossum12d12c51993-10-26 17:58:25 +00002257 for (i = j = 0; i < len; ++i) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002258 PyObject *item, *good;
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002259 int ok;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002260
Guido van Rossum79f25d91997-04-29 20:08:16 +00002261 if ((item = PyTuple_GetItem(tuple, i)) == NULL)
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002262 goto Fail_1;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002263 if (func == Py_None) {
2264 Py_INCREF(item);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002265 good = item;
2266 }
2267 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002268 PyObject *arg = Py_BuildValue("(O)", item);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002269 if (arg == NULL)
2270 goto Fail_1;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002271 good = PyEval_CallObject(func, arg);
2272 Py_DECREF(arg);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002273 if (good == NULL)
Guido van Rossum12d12c51993-10-26 17:58:25 +00002274 goto Fail_1;
2275 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002276 ok = PyObject_IsTrue(good);
2277 Py_DECREF(good);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002278 if (ok) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002279 Py_INCREF(item);
2280 if (PyTuple_SetItem(result, j++, item) < 0)
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002281 goto Fail_1;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002282 }
Guido van Rossum12d12c51993-10-26 17:58:25 +00002283 }
2284
Guido van Rossum79f25d91997-04-29 20:08:16 +00002285 if (_PyTuple_Resize(&result, j, 0) < 0)
Guido van Rossum12d12c51993-10-26 17:58:25 +00002286 return NULL;
2287
Guido van Rossum12d12c51993-10-26 17:58:25 +00002288 return result;
2289
Guido van Rossum12d12c51993-10-26 17:58:25 +00002290Fail_1:
Guido van Rossum79f25d91997-04-29 20:08:16 +00002291 Py_DECREF(result);
Guido van Rossum12d12c51993-10-26 17:58:25 +00002292 return NULL;
2293}
2294
2295
Guido van Rossume77a7571993-11-03 15:01:26 +00002296/* Helper for filter(): filter a string through a function */
Guido van Rossum12d12c51993-10-26 17:58:25 +00002297
Guido van Rossum79f25d91997-04-29 20:08:16 +00002298static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002299filterstring(PyObject *func, PyObject *strobj)
Guido van Rossum12d12c51993-10-26 17:58:25 +00002300{
Guido van Rossum79f25d91997-04-29 20:08:16 +00002301 PyObject *result;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002302 register int i, j;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002303 int len = PyString_Size(strobj);
Guido van Rossum12d12c51993-10-26 17:58:25 +00002304
Guido van Rossum79f25d91997-04-29 20:08:16 +00002305 if (func == Py_None) {
Guido van Rossum2586bf01993-11-01 16:21:44 +00002306 /* No character is ever false -- share input string */
Guido van Rossum79f25d91997-04-29 20:08:16 +00002307 Py_INCREF(strobj);
Guido van Rossum2d951851994-08-29 12:52:16 +00002308 return strobj;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002309 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002310 if ((result = PyString_FromStringAndSize(NULL, len)) == NULL)
Guido van Rossum2586bf01993-11-01 16:21:44 +00002311 return NULL;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002312
Guido van Rossum12d12c51993-10-26 17:58:25 +00002313 for (i = j = 0; i < len; ++i) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002314 PyObject *item, *arg, *good;
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002315 int ok;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002316
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002317 item = (*strobj->ob_type->tp_as_sequence->sq_item)(strobj, i);
2318 if (item == NULL)
2319 goto Fail_1;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002320 arg = Py_BuildValue("(O)", item);
Tim Peters388ed082001-04-07 20:34:48 +00002321 if (arg == NULL) {
2322 Py_DECREF(item);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002323 goto Fail_1;
Tim Peters388ed082001-04-07 20:34:48 +00002324 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002325 good = PyEval_CallObject(func, arg);
2326 Py_DECREF(arg);
Tim Peters388ed082001-04-07 20:34:48 +00002327 if (good == NULL) {
2328 Py_DECREF(item);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002329 goto Fail_1;
Tim Peters388ed082001-04-07 20:34:48 +00002330 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002331 ok = PyObject_IsTrue(good);
2332 Py_DECREF(good);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002333 if (ok)
Guido van Rossum79f25d91997-04-29 20:08:16 +00002334 PyString_AS_STRING((PyStringObject *)result)[j++] =
2335 PyString_AS_STRING((PyStringObject *)item)[0];
Tim Peters388ed082001-04-07 20:34:48 +00002336 Py_DECREF(item);
Guido van Rossum12d12c51993-10-26 17:58:25 +00002337 }
2338
Guido van Rossum79f25d91997-04-29 20:08:16 +00002339 if (j < len && _PyString_Resize(&result, j) < 0)
Guido van Rossum12d12c51993-10-26 17:58:25 +00002340 return NULL;
2341
Guido van Rossum12d12c51993-10-26 17:58:25 +00002342 return result;
2343
Guido van Rossum12d12c51993-10-26 17:58:25 +00002344Fail_1:
Guido van Rossum79f25d91997-04-29 20:08:16 +00002345 Py_DECREF(result);
Guido van Rossum12d12c51993-10-26 17:58:25 +00002346 return NULL;
2347}