blob: 9a09c8c4367c1c37652d083b06e5f1986aafa4a0 [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 *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001315builtin_len(PyObject *self, PyObject *args)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001316{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001317 PyObject *v;
Guido van Rossum8ea9f4d1998-06-29 22:26:50 +00001318 long res;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001319
Guido van Rossum79f25d91997-04-29 20:08:16 +00001320 if (!PyArg_ParseTuple(args, "O:len", &v))
Guido van Rossum3f5da241990-12-20 15:06:42 +00001321 return NULL;
Jeremy Hylton03657cf2000-07-12 13:05:33 +00001322 res = PyObject_Size(v);
Guido van Rossum8ea9f4d1998-06-29 22:26:50 +00001323 if (res < 0 && PyErr_Occurred())
1324 return NULL;
1325 return PyInt_FromLong(res);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001326}
1327
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001328static char len_doc[] =
1329"len(object) -> integer\n\
1330\n\
1331Return the number of items of a sequence or mapping.";
1332
1333
Guido van Rossum79f25d91997-04-29 20:08:16 +00001334static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001335builtin_list(PyObject *self, PyObject *args)
Guido van Rossumd1705771996-04-09 02:41:06 +00001336{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001337 PyObject *v;
Guido van Rossumd1705771996-04-09 02:41:06 +00001338
Guido van Rossum79f25d91997-04-29 20:08:16 +00001339 if (!PyArg_ParseTuple(args, "O:list", &v))
Guido van Rossumd1705771996-04-09 02:41:06 +00001340 return NULL;
Guido van Rossum09df08a1998-05-22 00:51:39 +00001341 return PySequence_List(v);
Guido van Rossumd1705771996-04-09 02:41:06 +00001342}
1343
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001344static char list_doc[] =
1345"list(sequence) -> list\n\
1346\n\
1347Return a new list whose items are the same as those of the argument sequence.";
1348
Guido van Rossum8861b741996-07-30 16:49:37 +00001349
1350static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001351builtin_slice(PyObject *self, PyObject *args)
Guido van Rossum8861b741996-07-30 16:49:37 +00001352{
Guido van Rossum09df08a1998-05-22 00:51:39 +00001353 PyObject *start, *stop, *step;
Guido van Rossum8861b741996-07-30 16:49:37 +00001354
Guido van Rossum09df08a1998-05-22 00:51:39 +00001355 start = stop = step = NULL;
Guido van Rossum8861b741996-07-30 16:49:37 +00001356
Guido van Rossum09df08a1998-05-22 00:51:39 +00001357 if (!PyArg_ParseTuple(args, "O|OO:slice", &start, &stop, &step))
1358 return NULL;
Guido van Rossum8861b741996-07-30 16:49:37 +00001359
Guido van Rossum09df08a1998-05-22 00:51:39 +00001360 /* This swapping of stop and start is to maintain similarity with
1361 range(). */
1362 if (stop == NULL) {
1363 stop = start;
1364 start = NULL;
1365 }
1366 return PySlice_New(start, stop, step);
Guido van Rossum8861b741996-07-30 16:49:37 +00001367}
1368
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001369static char slice_doc[] =
Fred Drake3d587441999-07-19 15:21:16 +00001370"slice([start,] stop[, step]) -> slice object\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001371\n\
1372Create a slice object. This is used for slicing by the Numeric extensions.";
1373
1374
Guido van Rossum79f25d91997-04-29 20:08:16 +00001375static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001376builtin_locals(PyObject *self, PyObject *args)
Guido van Rossum872537c1995-07-07 22:43:42 +00001377{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001378 PyObject *d;
Guido van Rossum872537c1995-07-07 22:43:42 +00001379
Guido van Rossum43713e52000-02-29 13:59:29 +00001380 if (!PyArg_ParseTuple(args, ":locals"))
Guido van Rossum872537c1995-07-07 22:43:42 +00001381 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001382 d = PyEval_GetLocals();
1383 Py_INCREF(d);
Guido van Rossum872537c1995-07-07 22:43:42 +00001384 return d;
1385}
1386
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001387static char locals_doc[] =
1388"locals() -> dictionary\n\
1389\n\
1390Return the dictionary containing the current scope's local variables.";
1391
1392
Guido van Rossum79f25d91997-04-29 20:08:16 +00001393static PyObject *
Guido van Rossum53451b32001-01-17 15:47:24 +00001394min_max(PyObject *args, int op)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001395{
Guido van Rossum2d951851994-08-29 12:52:16 +00001396 int i;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001397 PyObject *v, *w, *x;
1398 PySequenceMethods *sq;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001399
Guido van Rossum79f25d91997-04-29 20:08:16 +00001400 if (PyTuple_Size(args) > 1)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001401 v = args;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001402 else if (!PyArg_ParseTuple(args, "O:min/max", &v))
Guido van Rossum3f5da241990-12-20 15:06:42 +00001403 return NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001404 sq = v->ob_type->tp_as_sequence;
Guido van Rossum09df08a1998-05-22 00:51:39 +00001405 if (sq == NULL || sq->sq_item == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001406 PyErr_SetString(PyExc_TypeError,
Fred Drake661ea262000-10-24 19:57:45 +00001407 "min() or max() arg must be a sequence");
Guido van Rossum3f5da241990-12-20 15:06:42 +00001408 return NULL;
1409 }
Guido van Rossum2d951851994-08-29 12:52:16 +00001410 w = NULL;
1411 for (i = 0; ; i++) {
Guido van Rossum3f5da241990-12-20 15:06:42 +00001412 x = (*sq->sq_item)(v, i); /* Implies INCREF */
Guido van Rossum2d951851994-08-29 12:52:16 +00001413 if (x == NULL) {
Barry Warsawcde8b1b1997-08-22 21:14:38 +00001414 if (PyErr_ExceptionMatches(PyExc_IndexError)) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001415 PyErr_Clear();
Guido van Rossum2d951851994-08-29 12:52:16 +00001416 break;
1417 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00001418 Py_XDECREF(w);
Guido van Rossum2d951851994-08-29 12:52:16 +00001419 return NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001420 }
Guido van Rossum2d951851994-08-29 12:52:16 +00001421 if (w == NULL)
1422 w = x;
1423 else {
Guido van Rossum53451b32001-01-17 15:47:24 +00001424 int cmp = PyObject_RichCompareBool(x, w, op);
1425 if (cmp > 0) {
1426 Py_DECREF(w);
1427 w = x;
1428 }
1429 else if (cmp < 0) {
Guido van Rossumc8b6df91997-05-23 00:06:51 +00001430 Py_DECREF(x);
1431 Py_XDECREF(w);
1432 return NULL;
1433 }
Guido van Rossum2d951851994-08-29 12:52:16 +00001434 else
Guido van Rossum79f25d91997-04-29 20:08:16 +00001435 Py_DECREF(x);
Guido van Rossum2d951851994-08-29 12:52:16 +00001436 }
Guido van Rossum3f5da241990-12-20 15:06:42 +00001437 }
Guido van Rossum2d951851994-08-29 12:52:16 +00001438 if (w == NULL)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001439 PyErr_SetString(PyExc_ValueError,
Fred Drake661ea262000-10-24 19:57:45 +00001440 "min() or max() arg is an empty sequence");
Guido van Rossum3f5da241990-12-20 15:06:42 +00001441 return w;
1442}
1443
Guido van Rossum79f25d91997-04-29 20:08:16 +00001444static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001445builtin_min(PyObject *self, PyObject *v)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001446{
Guido van Rossum53451b32001-01-17 15:47:24 +00001447 return min_max(v, Py_LT);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001448}
1449
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001450static char min_doc[] =
1451"min(sequence) -> value\n\
1452min(a, b, c, ...) -> value\n\
1453\n\
1454With a single sequence argument, return its smallest item.\n\
1455With two or more arguments, return the smallest argument.";
1456
1457
Guido van Rossum79f25d91997-04-29 20:08:16 +00001458static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001459builtin_max(PyObject *self, PyObject *v)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001460{
Guido van Rossum53451b32001-01-17 15:47:24 +00001461 return min_max(v, Py_GT);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001462}
1463
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001464static char max_doc[] =
1465"max(sequence) -> value\n\
1466max(a, b, c, ...) -> value\n\
1467\n\
1468With a single sequence argument, return its largest item.\n\
1469With two or more arguments, return the largest argument.";
1470
1471
Guido van Rossum79f25d91997-04-29 20:08:16 +00001472static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001473builtin_oct(PyObject *self, PyObject *args)
Guido van Rossum006bcd41991-10-24 14:54:44 +00001474{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001475 PyObject *v;
1476 PyNumberMethods *nb;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001477
Guido van Rossum79f25d91997-04-29 20:08:16 +00001478 if (!PyArg_ParseTuple(args, "O:oct", &v))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001479 return NULL;
Guido van Rossum1899c2e1992-09-12 11:09:23 +00001480 if (v == NULL || (nb = v->ob_type->tp_as_number) == NULL ||
1481 nb->nb_oct == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001482 PyErr_SetString(PyExc_TypeError,
Guido van Rossum1899c2e1992-09-12 11:09:23 +00001483 "oct() argument can't be converted to oct");
1484 return NULL;
Guido van Rossum006bcd41991-10-24 14:54:44 +00001485 }
Guido van Rossum1899c2e1992-09-12 11:09:23 +00001486 return (*nb->nb_oct)(v);
Guido van Rossum006bcd41991-10-24 14:54:44 +00001487}
1488
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001489static char oct_doc[] =
1490"oct(number) -> string\n\
1491\n\
1492Return the octal representation of an integer or long integer.";
1493
1494
Guido van Rossum79f25d91997-04-29 20:08:16 +00001495static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001496builtin_open(PyObject *self, PyObject *args)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001497{
Guido van Rossum2d951851994-08-29 12:52:16 +00001498 char *name;
1499 char *mode = "r";
1500 int bufsize = -1;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001501 PyObject *f;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001502
Guido van Rossum79f25d91997-04-29 20:08:16 +00001503 if (!PyArg_ParseTuple(args, "s|si:open", &name, &mode, &bufsize))
Guido van Rossum3f5da241990-12-20 15:06:42 +00001504 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001505 f = PyFile_FromString(name, mode);
Guido van Rossum2d951851994-08-29 12:52:16 +00001506 if (f != NULL)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001507 PyFile_SetBufSize(f, bufsize);
Guido van Rossum2d951851994-08-29 12:52:16 +00001508 return f;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001509}
1510
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001511static char open_doc[] =
1512"open(filename[, mode[, buffering]]) -> file object\n\
1513\n\
1514Open a file. The mode can be 'r', 'w' or 'a' for reading (default),\n\
1515writing or appending. The file will be created if it doesn't exist\n\
1516when opened for writing or appending; it will be truncated when\n\
1517opened for writing. Add a 'b' to the mode for binary files.\n\
1518Add a '+' to the mode to allow simultaneous reading and writing.\n\
1519If the buffering argument is given, 0 means unbuffered, 1 means line\n\
1520buffered, and larger numbers specify the buffer size.";
1521
1522
Guido van Rossum79f25d91997-04-29 20:08:16 +00001523static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001524builtin_ord(PyObject *self, PyObject *args)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001525{
Guido van Rossum09095f32000-03-10 23:00:52 +00001526 PyObject *obj;
1527 long ord;
Jeremy Hylton394b54d2000-04-12 21:19:47 +00001528 int size;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001529
Guido van Rossum09095f32000-03-10 23:00:52 +00001530 if (!PyArg_ParseTuple(args, "O:ord", &obj))
Guido van Rossum3f5da241990-12-20 15:06:42 +00001531 return NULL;
Guido van Rossum09095f32000-03-10 23:00:52 +00001532
Jeremy Hylton394b54d2000-04-12 21:19:47 +00001533 if (PyString_Check(obj)) {
1534 size = PyString_GET_SIZE(obj);
Andrew M. Kuchling34c20cf2000-12-20 14:36:56 +00001535 if (size == 1) {
Jeremy Hylton394b54d2000-04-12 21:19:47 +00001536 ord = (long)((unsigned char)*PyString_AS_STRING(obj));
Andrew M. Kuchling34c20cf2000-12-20 14:36:56 +00001537 return PyInt_FromLong(ord);
1538 }
Jeremy Hylton394b54d2000-04-12 21:19:47 +00001539 } else if (PyUnicode_Check(obj)) {
1540 size = PyUnicode_GET_SIZE(obj);
Andrew M. Kuchling34c20cf2000-12-20 14:36:56 +00001541 if (size == 1) {
Jeremy Hylton394b54d2000-04-12 21:19:47 +00001542 ord = (long)*PyUnicode_AS_UNICODE(obj);
Andrew M. Kuchling34c20cf2000-12-20 14:36:56 +00001543 return PyInt_FromLong(ord);
1544 }
Jeremy Hylton394b54d2000-04-12 21:19:47 +00001545 } else {
1546 PyErr_Format(PyExc_TypeError,
Andrew M. Kuchlingf07aad12000-12-23 14:11:28 +00001547 "ord() expected string of length 1, but " \
Jeremy Hylton394b54d2000-04-12 21:19:47 +00001548 "%.200s found", obj->ob_type->tp_name);
Guido van Rossum09095f32000-03-10 23:00:52 +00001549 return NULL;
1550 }
1551
Guido van Rossumad991772001-01-12 16:03:05 +00001552 PyErr_Format(PyExc_TypeError,
Andrew M. Kuchlingf07aad12000-12-23 14:11:28 +00001553 "ord() expected a character, "
1554 "but string of length %d found",
Jeremy Hylton394b54d2000-04-12 21:19:47 +00001555 size);
1556 return NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001557}
1558
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001559static char ord_doc[] =
1560"ord(c) -> integer\n\
1561\n\
Guido van Rossumad991772001-01-12 16:03:05 +00001562Return the integer ordinal of a one-character string.";
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001563
1564
Guido van Rossum79f25d91997-04-29 20:08:16 +00001565static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001566builtin_pow(PyObject *self, PyObject *args)
Guido van Rossum6a00cd81995-01-07 12:39:01 +00001567{
Guido van Rossum09df08a1998-05-22 00:51:39 +00001568 PyObject *v, *w, *z = Py_None;
Guido van Rossum6a00cd81995-01-07 12:39:01 +00001569
Guido van Rossum79f25d91997-04-29 20:08:16 +00001570 if (!PyArg_ParseTuple(args, "OO|O:pow", &v, &w, &z))
Guido van Rossum6a00cd81995-01-07 12:39:01 +00001571 return NULL;
Guido van Rossum09df08a1998-05-22 00:51:39 +00001572 return PyNumber_Power(v, w, z);
Guido van Rossumd4905451991-05-05 20:00:36 +00001573}
1574
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001575static char pow_doc[] =
1576"pow(x, y[, z]) -> number\n\
1577\n\
1578With two arguments, equivalent to x**y. With three arguments,\n\
1579equivalent to (x**y) % z, but may be more efficient (e.g. for longs).";
1580
1581
Guido van Rossum124eff01999-02-23 16:11:01 +00001582/* Return number of items in range/xrange (lo, hi, step). step > 0
1583 * required. Return a value < 0 if & only if the true value is too
1584 * large to fit in a signed long.
1585 */
1586static long
Thomas Wouterse28c2962000-07-23 22:21:32 +00001587get_len_of_range(long lo, long hi, long step)
Guido van Rossum124eff01999-02-23 16:11:01 +00001588{
1589 /* -------------------------------------------------------------
1590 If lo >= hi, the range is empty.
1591 Else if n values are in the range, the last one is
1592 lo + (n-1)*step, which must be <= hi-1. Rearranging,
1593 n <= (hi - lo - 1)/step + 1, so taking the floor of the RHS gives
1594 the proper value. Since lo < hi in this case, hi-lo-1 >= 0, so
1595 the RHS is non-negative and so truncation is the same as the
1596 floor. Letting M be the largest positive long, the worst case
1597 for the RHS numerator is hi=M, lo=-M-1, and then
1598 hi-lo-1 = M-(-M-1)-1 = 2*M. Therefore unsigned long has enough
1599 precision to compute the RHS exactly.
1600 ---------------------------------------------------------------*/
1601 long n = 0;
1602 if (lo < hi) {
1603 unsigned long uhi = (unsigned long)hi;
1604 unsigned long ulo = (unsigned long)lo;
1605 unsigned long diff = uhi - ulo - 1;
1606 n = (long)(diff / (unsigned long)step + 1);
1607 }
1608 return n;
1609}
1610
Guido van Rossum79f25d91997-04-29 20:08:16 +00001611static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001612builtin_range(PyObject *self, PyObject *args)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001613{
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001614 long ilow = 0, ihigh = 0, istep = 1;
Guido van Rossum124eff01999-02-23 16:11:01 +00001615 long bign;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001616 int i, n;
Guido van Rossum124eff01999-02-23 16:11:01 +00001617
Guido van Rossum79f25d91997-04-29 20:08:16 +00001618 PyObject *v;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001619
Guido van Rossum79f25d91997-04-29 20:08:16 +00001620 if (PyTuple_Size(args) <= 1) {
1621 if (!PyArg_ParseTuple(args,
Guido van Rossum0865dd91995-01-17 16:30:22 +00001622 "l;range() requires 1-3 int arguments",
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001623 &ihigh))
1624 return NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001625 }
1626 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001627 if (!PyArg_ParseTuple(args,
Guido van Rossum0865dd91995-01-17 16:30:22 +00001628 "ll|l;range() requires 1-3 int arguments",
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001629 &ilow, &ihigh, &istep))
Guido van Rossum3f5da241990-12-20 15:06:42 +00001630 return NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001631 }
1632 if (istep == 0) {
Fred Drake661ea262000-10-24 19:57:45 +00001633 PyErr_SetString(PyExc_ValueError, "range() arg 3 must not be zero");
Guido van Rossum3f5da241990-12-20 15:06:42 +00001634 return NULL;
1635 }
Guido van Rossum124eff01999-02-23 16:11:01 +00001636 if (istep > 0)
1637 bign = get_len_of_range(ilow, ihigh, istep);
1638 else
1639 bign = get_len_of_range(ihigh, ilow, -istep);
1640 n = (int)bign;
1641 if (bign < 0 || (long)n != bign) {
Guido van Rossume23cde21999-01-12 05:07:47 +00001642 PyErr_SetString(PyExc_OverflowError,
Fred Drake661ea262000-10-24 19:57:45 +00001643 "range() result has too many items");
Guido van Rossume23cde21999-01-12 05:07:47 +00001644 return NULL;
1645 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00001646 v = PyList_New(n);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001647 if (v == NULL)
1648 return NULL;
1649 for (i = 0; i < n; i++) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001650 PyObject *w = PyInt_FromLong(ilow);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001651 if (w == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001652 Py_DECREF(v);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001653 return NULL;
1654 }
Guido van Rossuma937d141998-04-24 18:22:02 +00001655 PyList_SET_ITEM(v, i, w);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001656 ilow += istep;
1657 }
1658 return v;
1659}
1660
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001661static char range_doc[] =
1662"range([start,] stop[, step]) -> list of integers\n\
1663\n\
1664Return a list containing an arithmetic progression of integers.\n\
1665range(i, j) returns [i, i+1, i+2, ..., j-1]; start (!) defaults to 0.\n\
1666When step is given, it specifies the increment (or decrement).\n\
1667For example, range(4) returns [0, 1, 2, 3]. The end point is omitted!\n\
1668These are exactly the valid indices for a list of 4 elements.";
1669
1670
Guido van Rossum79f25d91997-04-29 20:08:16 +00001671static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001672builtin_xrange(PyObject *self, PyObject *args)
Guido van Rossum12d12c51993-10-26 17:58:25 +00001673{
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001674 long ilow = 0, ihigh = 0, istep = 1;
Guido van Rossum0865dd91995-01-17 16:30:22 +00001675 long n;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001676
Guido van Rossum79f25d91997-04-29 20:08:16 +00001677 if (PyTuple_Size(args) <= 1) {
1678 if (!PyArg_ParseTuple(args,
Guido van Rossum0865dd91995-01-17 16:30:22 +00001679 "l;xrange() requires 1-3 int arguments",
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001680 &ihigh))
1681 return NULL;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001682 }
1683 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001684 if (!PyArg_ParseTuple(args,
Guido van Rossum0865dd91995-01-17 16:30:22 +00001685 "ll|l;xrange() requires 1-3 int arguments",
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001686 &ilow, &ihigh, &istep))
Guido van Rossum12d12c51993-10-26 17:58:25 +00001687 return NULL;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001688 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001689 if (istep == 0) {
Fred Drake661ea262000-10-24 19:57:45 +00001690 PyErr_SetString(PyExc_ValueError, "xrange() arg 3 must not be zero");
Guido van Rossum12d12c51993-10-26 17:58:25 +00001691 return NULL;
1692 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001693 if (istep > 0)
Guido van Rossum124eff01999-02-23 16:11:01 +00001694 n = get_len_of_range(ilow, ihigh, istep);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001695 else
Guido van Rossum124eff01999-02-23 16:11:01 +00001696 n = get_len_of_range(ihigh, ilow, -istep);
1697 if (n < 0) {
1698 PyErr_SetString(PyExc_OverflowError,
Fred Drake661ea262000-10-24 19:57:45 +00001699 "xrange() result has too many items");
Guido van Rossum124eff01999-02-23 16:11:01 +00001700 return NULL;
1701 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00001702 return PyRange_New(ilow, n, istep, 1);
Guido van Rossum12d12c51993-10-26 17:58:25 +00001703}
1704
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001705static char xrange_doc[] =
1706"xrange([start,] stop[, step]) -> xrange object\n\
1707\n\
1708Like range(), but instead of returning a list, returns an object that\n\
1709generates the numbers in the range on demand. This is slightly slower\n\
1710than range() but more memory efficient.";
1711
1712
Guido van Rossum79f25d91997-04-29 20:08:16 +00001713static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001714builtin_raw_input(PyObject *self, PyObject *args)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001715{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001716 PyObject *v = NULL;
1717 PyObject *f;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001718
Guido van Rossum79f25d91997-04-29 20:08:16 +00001719 if (!PyArg_ParseTuple(args, "|O:[raw_]input", &v))
Guido van Rossum3165fe61992-09-25 21:59:05 +00001720 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001721 if (PyFile_AsFile(PySys_GetObject("stdin")) == stdin &&
1722 PyFile_AsFile(PySys_GetObject("stdout")) == stdout &&
Guido van Rossum53bb7ff1995-07-26 16:26:31 +00001723 isatty(fileno(stdin)) && isatty(fileno(stdout))) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001724 PyObject *po;
Guido van Rossum872537c1995-07-07 22:43:42 +00001725 char *prompt;
1726 char *s;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001727 PyObject *result;
Guido van Rossum872537c1995-07-07 22:43:42 +00001728 if (v != NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001729 po = PyObject_Str(v);
Guido van Rossum872537c1995-07-07 22:43:42 +00001730 if (po == NULL)
1731 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001732 prompt = PyString_AsString(po);
Guido van Rossumd9b52081998-06-26 18:25:38 +00001733 if (prompt == NULL)
1734 return NULL;
Guido van Rossum872537c1995-07-07 22:43:42 +00001735 }
1736 else {
1737 po = NULL;
1738 prompt = "";
1739 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00001740 s = PyOS_Readline(prompt);
1741 Py_XDECREF(po);
Guido van Rossum872537c1995-07-07 22:43:42 +00001742 if (s == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001743 PyErr_SetNone(PyExc_KeyboardInterrupt);
Guido van Rossum872537c1995-07-07 22:43:42 +00001744 return NULL;
1745 }
1746 if (*s == '\0') {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001747 PyErr_SetNone(PyExc_EOFError);
Guido van Rossum872537c1995-07-07 22:43:42 +00001748 result = NULL;
1749 }
1750 else { /* strip trailing '\n' */
Guido van Rossum106f2da2000-06-28 21:12:25 +00001751 size_t len = strlen(s);
1752 if (len > INT_MAX) {
1753 PyErr_SetString(PyExc_OverflowError, "input too long");
1754 result = NULL;
1755 }
1756 else {
1757 result = PyString_FromStringAndSize(s, (int)(len-1));
1758 }
Guido van Rossum872537c1995-07-07 22:43:42 +00001759 }
Guido van Rossumb18618d2000-05-03 23:44:39 +00001760 PyMem_FREE(s);
Guido van Rossum872537c1995-07-07 22:43:42 +00001761 return result;
1762 }
Guido van Rossum90933611991-06-07 16:10:43 +00001763 if (v != NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001764 f = PySys_GetObject("stdout");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001765 if (f == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001766 PyErr_SetString(PyExc_RuntimeError, "lost sys.stdout");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001767 return NULL;
1768 }
Guido van Rossumc8b6df91997-05-23 00:06:51 +00001769 if (Py_FlushLine() != 0 ||
1770 PyFile_WriteObject(v, f, Py_PRINT_RAW) != 0)
Guido van Rossum90933611991-06-07 16:10:43 +00001771 return NULL;
1772 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00001773 f = PySys_GetObject("stdin");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001774 if (f == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001775 PyErr_SetString(PyExc_RuntimeError, "lost sys.stdin");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001776 return NULL;
1777 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00001778 return PyFile_GetLine(f, -1);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001779}
1780
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001781static char raw_input_doc[] =
1782"raw_input([prompt]) -> string\n\
1783\n\
1784Read a string from standard input. The trailing newline is stripped.\n\
1785If the user hits EOF (Unix: Ctl-D, Windows: Ctl-Z+Return), raise EOFError.\n\
1786On Unix, GNU readline is used if enabled. The prompt string, if given,\n\
1787is printed without a trailing newline before reading.";
1788
1789
Guido van Rossum79f25d91997-04-29 20:08:16 +00001790static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001791builtin_reduce(PyObject *self, PyObject *args)
Guido van Rossum12d12c51993-10-26 17:58:25 +00001792{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001793 PyObject *seq, *func, *result = NULL;
1794 PySequenceMethods *sqf;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001795 register int i;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001796
Guido van Rossum79f25d91997-04-29 20:08:16 +00001797 if (!PyArg_ParseTuple(args, "OO|O:reduce", &func, &seq, &result))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001798 return NULL;
1799 if (result != NULL)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001800 Py_INCREF(result);
Guido van Rossum12d12c51993-10-26 17:58:25 +00001801
Guido van Rossum09df08a1998-05-22 00:51:39 +00001802 sqf = seq->ob_type->tp_as_sequence;
1803 if (sqf == NULL || sqf->sq_item == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001804 PyErr_SetString(PyExc_TypeError,
Fred Drake661ea262000-10-24 19:57:45 +00001805 "reduce() arg 2 must be a sequence");
Guido van Rossum12d12c51993-10-26 17:58:25 +00001806 return NULL;
1807 }
1808
Guido van Rossum79f25d91997-04-29 20:08:16 +00001809 if ((args = PyTuple_New(2)) == NULL)
Guido van Rossum2d951851994-08-29 12:52:16 +00001810 goto Fail;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001811
Guido van Rossum2d951851994-08-29 12:52:16 +00001812 for (i = 0; ; ++i) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001813 PyObject *op2;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001814
1815 if (args->ob_refcnt > 1) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001816 Py_DECREF(args);
1817 if ((args = PyTuple_New(2)) == NULL)
Guido van Rossum2d951851994-08-29 12:52:16 +00001818 goto Fail;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001819 }
1820
Guido van Rossum2d951851994-08-29 12:52:16 +00001821 if ((op2 = (*sqf->sq_item)(seq, i)) == NULL) {
Barry Warsawcde8b1b1997-08-22 21:14:38 +00001822 if (PyErr_ExceptionMatches(PyExc_IndexError)) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001823 PyErr_Clear();
Guido van Rossum2d951851994-08-29 12:52:16 +00001824 break;
1825 }
1826 goto Fail;
1827 }
Guido van Rossum12d12c51993-10-26 17:58:25 +00001828
Guido van Rossum2d951851994-08-29 12:52:16 +00001829 if (result == NULL)
1830 result = op2;
1831 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001832 PyTuple_SetItem(args, 0, result);
1833 PyTuple_SetItem(args, 1, op2);
1834 if ((result = PyEval_CallObject(func, args)) == NULL)
Guido van Rossum2d951851994-08-29 12:52:16 +00001835 goto Fail;
1836 }
Guido van Rossum12d12c51993-10-26 17:58:25 +00001837 }
1838
Guido van Rossum79f25d91997-04-29 20:08:16 +00001839 Py_DECREF(args);
Guido van Rossum12d12c51993-10-26 17:58:25 +00001840
Guido van Rossum2d951851994-08-29 12:52:16 +00001841 if (result == NULL)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001842 PyErr_SetString(PyExc_TypeError,
Fred Drake661ea262000-10-24 19:57:45 +00001843 "reduce() of empty sequence with no initial value");
Guido van Rossum2d951851994-08-29 12:52:16 +00001844
Guido van Rossum12d12c51993-10-26 17:58:25 +00001845 return result;
1846
Guido van Rossum2d951851994-08-29 12:52:16 +00001847Fail:
Guido van Rossum79f25d91997-04-29 20:08:16 +00001848 Py_XDECREF(args);
1849 Py_XDECREF(result);
Guido van Rossum12d12c51993-10-26 17:58:25 +00001850 return NULL;
1851}
1852
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001853static char reduce_doc[] =
1854"reduce(function, sequence[, initial]) -> value\n\
1855\n\
1856Apply a function of two arguments cumulatively to the items of a sequence,\n\
1857from left to right, so as to reduce the sequence to a single value.\n\
1858For example, reduce(lambda x, y: x+y, [1, 2, 3, 4, 5]) calculates\n\
1859((((1+2)+3)+4)+5). If initial is present, it is placed before the items\n\
1860of the sequence in the calculation, and serves as a default when the\n\
1861sequence is empty.";
1862
1863
Guido van Rossum79f25d91997-04-29 20:08:16 +00001864static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001865builtin_reload(PyObject *self, PyObject *args)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001866{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001867 PyObject *v;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001868
Guido van Rossum79f25d91997-04-29 20:08:16 +00001869 if (!PyArg_ParseTuple(args, "O:reload", &v))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001870 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001871 return PyImport_ReloadModule(v);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001872}
1873
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001874static char reload_doc[] =
1875"reload(module) -> module\n\
1876\n\
1877Reload the module. The module must have been successfully imported before.";
1878
1879
Guido van Rossum79f25d91997-04-29 20:08:16 +00001880static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001881builtin_repr(PyObject *self, PyObject *args)
Guido van Rossumc89705d1992-11-26 08:54:07 +00001882{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001883 PyObject *v;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001884
Guido van Rossum79f25d91997-04-29 20:08:16 +00001885 if (!PyArg_ParseTuple(args, "O:repr", &v))
Guido van Rossumc89705d1992-11-26 08:54:07 +00001886 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001887 return PyObject_Repr(v);
Guido van Rossumc89705d1992-11-26 08:54:07 +00001888}
1889
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001890static char repr_doc[] =
1891"repr(object) -> string\n\
1892\n\
1893Return the canonical string representation of the object.\n\
1894For most object types, eval(repr(object)) == object.";
1895
1896
Guido van Rossum79f25d91997-04-29 20:08:16 +00001897static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001898builtin_round(PyObject *self, PyObject *args)
Guido van Rossum9e51f9b1993-02-12 16:29:05 +00001899{
Guido van Rossum9e51f9b1993-02-12 16:29:05 +00001900 double x;
1901 double f;
1902 int ndigits = 0;
Guido van Rossum9e51f9b1993-02-12 16:29:05 +00001903 int i;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001904
Guido van Rossum79f25d91997-04-29 20:08:16 +00001905 if (!PyArg_ParseTuple(args, "d|i:round", &x, &ndigits))
Guido van Rossum9e51f9b1993-02-12 16:29:05 +00001906 return NULL;
Guido van Rossum9e51f9b1993-02-12 16:29:05 +00001907 f = 1.0;
Guido van Rossum1e162d31998-05-09 14:42:25 +00001908 i = abs(ndigits);
1909 while (--i >= 0)
Guido van Rossum9e51f9b1993-02-12 16:29:05 +00001910 f = f*10.0;
Guido van Rossum1e162d31998-05-09 14:42:25 +00001911 if (ndigits < 0)
1912 x /= f;
Guido van Rossum9e51f9b1993-02-12 16:29:05 +00001913 else
Guido van Rossum1e162d31998-05-09 14:42:25 +00001914 x *= f;
1915 if (x >= 0.0)
1916 x = floor(x + 0.5);
1917 else
1918 x = ceil(x - 0.5);
1919 if (ndigits < 0)
1920 x *= f;
1921 else
1922 x /= f;
1923 return PyFloat_FromDouble(x);
Guido van Rossum9e51f9b1993-02-12 16:29:05 +00001924}
1925
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001926static char round_doc[] =
1927"round(number[, ndigits]) -> floating point number\n\
1928\n\
1929Round a number to a given precision in decimal digits (default 0 digits).\n\
1930This always returns a floating point number. Precision may be negative.";
1931
1932
Guido van Rossum79f25d91997-04-29 20:08:16 +00001933static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001934builtin_str(PyObject *self, PyObject *args)
Guido van Rossumc89705d1992-11-26 08:54:07 +00001935{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001936 PyObject *v;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001937
Guido van Rossum79f25d91997-04-29 20:08:16 +00001938 if (!PyArg_ParseTuple(args, "O:str", &v))
Guido van Rossumc89705d1992-11-26 08:54:07 +00001939 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001940 return PyObject_Str(v);
Guido van Rossumc89705d1992-11-26 08:54:07 +00001941}
1942
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001943static char str_doc[] =
1944"str(object) -> string\n\
1945\n\
1946Return a nice string representation of the object.\n\
1947If the argument is a string, the return value is the same object.";
1948
1949
Guido van Rossum79f25d91997-04-29 20:08:16 +00001950static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001951builtin_tuple(PyObject *self, PyObject *args)
Guido van Rossumcae027b1994-08-29 12:53:11 +00001952{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001953 PyObject *v;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001954
Guido van Rossum79f25d91997-04-29 20:08:16 +00001955 if (!PyArg_ParseTuple(args, "O:tuple", &v))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001956 return NULL;
Guido van Rossum09df08a1998-05-22 00:51:39 +00001957 return PySequence_Tuple(v);
Guido van Rossumcae027b1994-08-29 12:53:11 +00001958}
1959
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001960static char tuple_doc[] =
1961"tuple(sequence) -> list\n\
1962\n\
1963Return a tuple whose items are the same as those of the argument sequence.\n\
1964If the argument is a tuple, the return value is the same object.";
1965
1966
Guido van Rossum79f25d91997-04-29 20:08:16 +00001967static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001968builtin_type(PyObject *self, PyObject *args)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001969{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001970 PyObject *v;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001971
Guido van Rossum79f25d91997-04-29 20:08:16 +00001972 if (!PyArg_ParseTuple(args, "O:type", &v))
Guido van Rossum3f5da241990-12-20 15:06:42 +00001973 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001974 v = (PyObject *)v->ob_type;
1975 Py_INCREF(v);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001976 return v;
1977}
1978
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001979static char type_doc[] =
1980"type(object) -> type object\n\
1981\n\
1982Return the type of the object.";
1983
1984
Guido van Rossum79f25d91997-04-29 20:08:16 +00001985static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001986builtin_vars(PyObject *self, PyObject *args)
Guido van Rossum2d951851994-08-29 12:52:16 +00001987{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001988 PyObject *v = NULL;
1989 PyObject *d;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001990
Guido van Rossum79f25d91997-04-29 20:08:16 +00001991 if (!PyArg_ParseTuple(args, "|O:vars", &v))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001992 return NULL;
Guido van Rossum2d951851994-08-29 12:52:16 +00001993 if (v == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001994 d = PyEval_GetLocals();
Guido van Rossum53bb7ff1995-07-26 16:26:31 +00001995 if (d == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001996 if (!PyErr_Occurred())
1997 PyErr_SetString(PyExc_SystemError,
1998 "no locals!?");
Guido van Rossum53bb7ff1995-07-26 16:26:31 +00001999 }
2000 else
Guido van Rossum79f25d91997-04-29 20:08:16 +00002001 Py_INCREF(d);
Guido van Rossum2d951851994-08-29 12:52:16 +00002002 }
2003 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002004 d = PyObject_GetAttrString(v, "__dict__");
Guido van Rossum2d951851994-08-29 12:52:16 +00002005 if (d == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002006 PyErr_SetString(PyExc_TypeError,
Guido van Rossum2d951851994-08-29 12:52:16 +00002007 "vars() argument must have __dict__ attribute");
2008 return NULL;
2009 }
2010 }
2011 return d;
2012}
2013
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002014static char vars_doc[] =
2015"vars([object]) -> dictionary\n\
2016\n\
2017Without arguments, equivalent to locals().\n\
2018With an argument, equivalent to object.__dict__.";
2019
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002020static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002021builtin_isinstance(PyObject *self, PyObject *args)
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002022{
2023 PyObject *inst;
2024 PyObject *cls;
Guido van Rossum823649d2001-03-21 18:40:58 +00002025 int retval;
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002026
Guido van Rossum43713e52000-02-29 13:59:29 +00002027 if (!PyArg_ParseTuple(args, "OO:isinstance", &inst, &cls))
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002028 return NULL;
Guido van Rossumf5dd9141997-12-02 19:11:45 +00002029
Guido van Rossum823649d2001-03-21 18:40:58 +00002030 retval = PyObject_IsInstance(inst, cls);
2031 if (retval < 0)
Guido van Rossumad991772001-01-12 16:03:05 +00002032 return NULL;
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002033 return PyInt_FromLong(retval);
2034}
2035
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002036static char isinstance_doc[] =
2037"isinstance(object, class-or-type) -> Boolean\n\
2038\n\
2039Return whether an object is an instance of a class or of a subclass thereof.\n\
2040With a type as second argument, return whether that is the object's type.";
2041
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002042
2043static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002044builtin_issubclass(PyObject *self, PyObject *args)
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002045{
2046 PyObject *derived;
2047 PyObject *cls;
2048 int retval;
2049
Guido van Rossum43713e52000-02-29 13:59:29 +00002050 if (!PyArg_ParseTuple(args, "OO:issubclass", &derived, &cls))
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002051 return NULL;
Guido van Rossum668213d1999-06-16 17:28:37 +00002052
Guido van Rossum823649d2001-03-21 18:40:58 +00002053 retval = PyObject_IsSubclass(derived, cls);
2054 if (retval < 0)
2055 return NULL;
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002056 return PyInt_FromLong(retval);
2057}
2058
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002059static char issubclass_doc[] =
2060"issubclass(C, B) -> Boolean\n\
2061\n\
2062Return whether class C is a subclass (i.e., a derived class) of class B.";
2063
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002064
Barry Warsawbd599b52000-08-03 15:45:29 +00002065static PyObject*
2066builtin_zip(PyObject *self, PyObject *args)
2067{
2068 PyObject *ret;
2069 int itemsize = PySequence_Length(args);
2070 int i, j;
2071
2072 if (itemsize < 1) {
2073 PyErr_SetString(PyExc_TypeError,
Fred Drake661ea262000-10-24 19:57:45 +00002074 "zip() requires at least one sequence");
Barry Warsawbd599b52000-08-03 15:45:29 +00002075 return NULL;
2076 }
2077 /* args must be a tuple */
2078 assert(PyTuple_Check(args));
2079
2080 if ((ret = PyList_New(0)) == NULL)
2081 return NULL;
2082
2083 for (i = 0;; i++) {
2084 PyObject *next = PyTuple_New(itemsize);
2085 if (!next) {
2086 Py_DECREF(ret);
2087 return NULL;
2088 }
2089 for (j = 0; j < itemsize; j++) {
2090 PyObject *seq = PyTuple_GET_ITEM(args, j);
2091 PyObject *item = PySequence_GetItem(seq, i);
2092
2093 if (!item) {
2094 if (PyErr_ExceptionMatches(PyExc_IndexError)) {
2095 PyErr_Clear();
2096 Py_DECREF(next);
2097 return ret;
2098 }
2099 Py_DECREF(next);
2100 Py_DECREF(ret);
2101 return NULL;
2102 }
2103 PyTuple_SET_ITEM(next, j, item);
2104 }
2105 PyList_Append(ret, next);
2106 Py_DECREF(next);
2107 }
2108 /* no return */
2109}
2110
2111
2112static char zip_doc[] =
2113"zip(seq1 [, seq2 [...]]) -> [(seq1[0], seq2[0] ...), (...)]\n\
2114\n\
2115Return a list of tuples, where each tuple contains the i-th element\n\
2116from each of the argument sequences. The returned list is truncated\n\
2117in length to the length of the shortest argument sequence.";
2118
2119
Guido van Rossum79f25d91997-04-29 20:08:16 +00002120static PyMethodDef builtin_methods[] = {
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002121 {"__import__", builtin___import__, 1, import_doc},
2122 {"abs", builtin_abs, 1, abs_doc},
2123 {"apply", builtin_apply, 1, apply_doc},
Guido van Rossum0daf0221999-03-19 19:07:19 +00002124 {"buffer", builtin_buffer, 1, buffer_doc},
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002125 {"callable", builtin_callable, 1, callable_doc},
2126 {"chr", builtin_chr, 1, chr_doc},
2127 {"cmp", builtin_cmp, 1, cmp_doc},
2128 {"coerce", builtin_coerce, 1, coerce_doc},
2129 {"compile", builtin_compile, 1, compile_doc},
Guido van Rossum8a5c5d21996-01-12 01:09:56 +00002130#ifndef WITHOUT_COMPLEX
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002131 {"complex", builtin_complex, 1, complex_doc},
Guido van Rossum8a5c5d21996-01-12 01:09:56 +00002132#endif
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002133 {"delattr", builtin_delattr, 1, delattr_doc},
2134 {"dir", builtin_dir, 1, dir_doc},
2135 {"divmod", builtin_divmod, 1, divmod_doc},
2136 {"eval", builtin_eval, 1, eval_doc},
2137 {"execfile", builtin_execfile, 1, execfile_doc},
2138 {"filter", builtin_filter, 1, filter_doc},
2139 {"float", builtin_float, 1, float_doc},
2140 {"getattr", builtin_getattr, 1, getattr_doc},
2141 {"globals", builtin_globals, 1, globals_doc},
2142 {"hasattr", builtin_hasattr, 1, hasattr_doc},
2143 {"hash", builtin_hash, 1, hash_doc},
2144 {"hex", builtin_hex, 1, hex_doc},
2145 {"id", builtin_id, 1, id_doc},
2146 {"input", builtin_input, 1, input_doc},
2147 {"intern", builtin_intern, 1, intern_doc},
2148 {"int", builtin_int, 1, int_doc},
2149 {"isinstance", builtin_isinstance, 1, isinstance_doc},
2150 {"issubclass", builtin_issubclass, 1, issubclass_doc},
2151 {"len", builtin_len, 1, len_doc},
2152 {"list", builtin_list, 1, list_doc},
2153 {"locals", builtin_locals, 1, locals_doc},
2154 {"long", builtin_long, 1, long_doc},
2155 {"map", builtin_map, 1, map_doc},
2156 {"max", builtin_max, 1, max_doc},
2157 {"min", builtin_min, 1, min_doc},
2158 {"oct", builtin_oct, 1, oct_doc},
2159 {"open", builtin_open, 1, open_doc},
2160 {"ord", builtin_ord, 1, ord_doc},
2161 {"pow", builtin_pow, 1, pow_doc},
2162 {"range", builtin_range, 1, range_doc},
2163 {"raw_input", builtin_raw_input, 1, raw_input_doc},
2164 {"reduce", builtin_reduce, 1, reduce_doc},
2165 {"reload", builtin_reload, 1, reload_doc},
2166 {"repr", builtin_repr, 1, repr_doc},
2167 {"round", builtin_round, 1, round_doc},
2168 {"setattr", builtin_setattr, 1, setattr_doc},
2169 {"slice", builtin_slice, 1, slice_doc},
2170 {"str", builtin_str, 1, str_doc},
2171 {"tuple", builtin_tuple, 1, tuple_doc},
2172 {"type", builtin_type, 1, type_doc},
Guido van Rossum09095f32000-03-10 23:00:52 +00002173 {"unicode", builtin_unicode, 1, unicode_doc},
2174 {"unichr", builtin_unichr, 1, unichr_doc},
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002175 {"vars", builtin_vars, 1, vars_doc},
2176 {"xrange", builtin_xrange, 1, xrange_doc},
Barry Warsawbd599b52000-08-03 15:45:29 +00002177 {"zip", builtin_zip, 1, zip_doc},
Guido van Rossumc02e15c1991-12-16 13:03:00 +00002178 {NULL, NULL},
Guido van Rossum3f5da241990-12-20 15:06:42 +00002179};
2180
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002181static char builtin_doc[] =
2182"Built-in functions, exceptions, and other objects.\n\
2183\n\
2184Noteworthy: None is the `nil' object; Ellipsis represents `...' in slices.";
2185
Guido van Rossum25ce5661997-08-02 03:10:38 +00002186PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002187_PyBuiltin_Init(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +00002188{
Fred Drake5550de32000-06-20 04:54:19 +00002189 PyObject *mod, *dict, *debug;
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002190 mod = Py_InitModule4("__builtin__", builtin_methods,
2191 builtin_doc, (PyObject *)NULL,
2192 PYTHON_API_VERSION);
Guido van Rossum25ce5661997-08-02 03:10:38 +00002193 if (mod == NULL)
2194 return NULL;
2195 dict = PyModule_GetDict(mod);
Guido van Rossum25ce5661997-08-02 03:10:38 +00002196 if (PyDict_SetItemString(dict, "None", Py_None) < 0)
2197 return NULL;
2198 if (PyDict_SetItemString(dict, "Ellipsis", Py_Ellipsis) < 0)
2199 return NULL;
Guido van Rossumad991772001-01-12 16:03:05 +00002200 if (PyDict_SetItemString(dict, "NotImplemented",
Neil Schemenauer23ab1992001-01-04 01:48:42 +00002201 Py_NotImplemented) < 0)
2202 return NULL;
Fred Drake5550de32000-06-20 04:54:19 +00002203 debug = PyInt_FromLong(Py_OptimizeFlag == 0);
2204 if (PyDict_SetItemString(dict, "__debug__", debug) < 0) {
2205 Py_XDECREF(debug);
Guido van Rossum25ce5661997-08-02 03:10:38 +00002206 return NULL;
Fred Drake5550de32000-06-20 04:54:19 +00002207 }
2208 Py_XDECREF(debug);
Barry Warsaw757af0e1997-08-29 22:13:51 +00002209
Guido van Rossum25ce5661997-08-02 03:10:38 +00002210 return mod;
Guido van Rossum3f5da241990-12-20 15:06:42 +00002211}
2212
Guido van Rossume77a7571993-11-03 15:01:26 +00002213/* Helper for filter(): filter a tuple through a function */
Guido van Rossum12d12c51993-10-26 17:58:25 +00002214
Guido van Rossum79f25d91997-04-29 20:08:16 +00002215static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002216filtertuple(PyObject *func, PyObject *tuple)
Guido van Rossum12d12c51993-10-26 17:58:25 +00002217{
Guido van Rossum79f25d91997-04-29 20:08:16 +00002218 PyObject *result;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002219 register int i, j;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002220 int len = PyTuple_Size(tuple);
Guido van Rossum12d12c51993-10-26 17:58:25 +00002221
Guido van Rossumb7b45621995-08-04 04:07:45 +00002222 if (len == 0) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002223 Py_INCREF(tuple);
Guido van Rossumb7b45621995-08-04 04:07:45 +00002224 return tuple;
2225 }
2226
Guido van Rossum79f25d91997-04-29 20:08:16 +00002227 if ((result = PyTuple_New(len)) == NULL)
Guido van Rossum2586bf01993-11-01 16:21:44 +00002228 return NULL;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002229
Guido van Rossum12d12c51993-10-26 17:58:25 +00002230 for (i = j = 0; i < len; ++i) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002231 PyObject *item, *good;
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002232 int ok;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002233
Guido van Rossum79f25d91997-04-29 20:08:16 +00002234 if ((item = PyTuple_GetItem(tuple, i)) == NULL)
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002235 goto Fail_1;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002236 if (func == Py_None) {
2237 Py_INCREF(item);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002238 good = item;
2239 }
2240 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002241 PyObject *arg = Py_BuildValue("(O)", item);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002242 if (arg == NULL)
2243 goto Fail_1;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002244 good = PyEval_CallObject(func, arg);
2245 Py_DECREF(arg);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002246 if (good == NULL)
Guido van Rossum12d12c51993-10-26 17:58:25 +00002247 goto Fail_1;
2248 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002249 ok = PyObject_IsTrue(good);
2250 Py_DECREF(good);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002251 if (ok) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002252 Py_INCREF(item);
2253 if (PyTuple_SetItem(result, j++, item) < 0)
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002254 goto Fail_1;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002255 }
Guido van Rossum12d12c51993-10-26 17:58:25 +00002256 }
2257
Guido van Rossum79f25d91997-04-29 20:08:16 +00002258 if (_PyTuple_Resize(&result, j, 0) < 0)
Guido van Rossum12d12c51993-10-26 17:58:25 +00002259 return NULL;
2260
Guido van Rossum12d12c51993-10-26 17:58:25 +00002261 return result;
2262
Guido van Rossum12d12c51993-10-26 17:58:25 +00002263Fail_1:
Guido van Rossum79f25d91997-04-29 20:08:16 +00002264 Py_DECREF(result);
Guido van Rossum12d12c51993-10-26 17:58:25 +00002265 return NULL;
2266}
2267
2268
Guido van Rossume77a7571993-11-03 15:01:26 +00002269/* Helper for filter(): filter a string through a function */
Guido van Rossum12d12c51993-10-26 17:58:25 +00002270
Guido van Rossum79f25d91997-04-29 20:08:16 +00002271static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002272filterstring(PyObject *func, PyObject *strobj)
Guido van Rossum12d12c51993-10-26 17:58:25 +00002273{
Guido van Rossum79f25d91997-04-29 20:08:16 +00002274 PyObject *result;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002275 register int i, j;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002276 int len = PyString_Size(strobj);
Guido van Rossum12d12c51993-10-26 17:58:25 +00002277
Guido van Rossum79f25d91997-04-29 20:08:16 +00002278 if (func == Py_None) {
Guido van Rossum2586bf01993-11-01 16:21:44 +00002279 /* No character is ever false -- share input string */
Guido van Rossum79f25d91997-04-29 20:08:16 +00002280 Py_INCREF(strobj);
Guido van Rossum2d951851994-08-29 12:52:16 +00002281 return strobj;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002282 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002283 if ((result = PyString_FromStringAndSize(NULL, len)) == NULL)
Guido van Rossum2586bf01993-11-01 16:21:44 +00002284 return NULL;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002285
Guido van Rossum12d12c51993-10-26 17:58:25 +00002286 for (i = j = 0; i < len; ++i) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002287 PyObject *item, *arg, *good;
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002288 int ok;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002289
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002290 item = (*strobj->ob_type->tp_as_sequence->sq_item)(strobj, i);
2291 if (item == NULL)
2292 goto Fail_1;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002293 arg = Py_BuildValue("(O)", item);
2294 Py_DECREF(item);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002295 if (arg == NULL)
2296 goto Fail_1;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002297 good = PyEval_CallObject(func, arg);
2298 Py_DECREF(arg);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002299 if (good == NULL)
2300 goto Fail_1;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002301 ok = PyObject_IsTrue(good);
2302 Py_DECREF(good);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002303 if (ok)
Guido van Rossum79f25d91997-04-29 20:08:16 +00002304 PyString_AS_STRING((PyStringObject *)result)[j++] =
2305 PyString_AS_STRING((PyStringObject *)item)[0];
Guido van Rossum12d12c51993-10-26 17:58:25 +00002306 }
2307
Guido van Rossum79f25d91997-04-29 20:08:16 +00002308 if (j < len && _PyString_Resize(&result, j) < 0)
Guido van Rossum12d12c51993-10-26 17:58:25 +00002309 return NULL;
2310
Guido van Rossum12d12c51993-10-26 17:58:25 +00002311 return result;
2312
Guido van Rossum12d12c51993-10-26 17:58:25 +00002313Fail_1:
Guido van Rossum79f25d91997-04-29 20:08:16 +00002314 Py_DECREF(result);
Guido van Rossum12d12c51993-10-26 17:58:25 +00002315 return NULL;
2316}