blob: a4c14ba635cd2307d1fa9b6d11bbff77defde9c0 [file] [log] [blame]
Guido van Rossumf70e43a1991-02-19 12:39:46 +00001/***********************************************************
Guido van Rossum6d023c91995-01-04 19:12:13 +00002Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
3The Netherlands.
Guido van Rossumf70e43a1991-02-19 12:39:46 +00004
5 All Rights Reserved
6
Guido van Rossumd266eb41996-10-25 14:44:06 +00007Permission to use, copy, modify, and distribute this software and its
8documentation for any purpose and without fee is hereby granted,
Guido van Rossumf70e43a1991-02-19 12:39:46 +00009provided that the above copyright notice appear in all copies and that
Guido van Rossumd266eb41996-10-25 14:44:06 +000010both that copyright notice and this permission notice appear in
Guido van Rossumf70e43a1991-02-19 12:39:46 +000011supporting documentation, and that the names of Stichting Mathematisch
Guido van Rossumd266eb41996-10-25 14:44:06 +000012Centrum or CWI or Corporation for National Research Initiatives or
13CNRI not be used in advertising or publicity pertaining to
14distribution of the software without specific, written prior
15permission.
Guido van Rossumf70e43a1991-02-19 12:39:46 +000016
Guido van Rossumd266eb41996-10-25 14:44:06 +000017While CWI is the initial source for this software, a modified version
18is made available by the Corporation for National Research Initiatives
19(CNRI) at the Internet address ftp://ftp.python.org.
20
21STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH
22REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF
23MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH
24CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
25DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
26PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
27TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
28PERFORMANCE OF THIS SOFTWARE.
Guido van Rossumf70e43a1991-02-19 12:39:46 +000029
30******************************************************************/
31
Guido van Rossum3f5da241990-12-20 15:06:42 +000032/* Built-in functions */
33
Guido van Rossum79f25d91997-04-29 20:08:16 +000034#include "Python.h"
Guido van Rossum3f5da241990-12-20 15:06:42 +000035
36#include "node.h"
Guido van Rossum5b722181993-03-30 17:46:03 +000037#include "compile.h"
38#include "eval.h"
Guido van Rossum3f5da241990-12-20 15:06:42 +000039
Guido van Rossumfe4b6ee1996-08-08 18:49:41 +000040#include "mymath.h"
41
Guido van Rossum6bf62da1997-04-11 20:37:35 +000042#include <ctype.h>
43
Guido van Rossum1a2c5cb1996-12-10 15:37:36 +000044#ifdef HAVE_UNISTD_H
45#include <unistd.h>
46#endif
47
Guido van Rossum12d12c51993-10-26 17:58:25 +000048/* Forward */
Guido van Rossum79f25d91997-04-29 20:08:16 +000049static PyObject *filterstring Py_PROTO((PyObject *, PyObject *));
50static PyObject *filtertuple Py_PROTO((PyObject *, PyObject *));
Guido van Rossum12d12c51993-10-26 17:58:25 +000051
Guido van Rossum79f25d91997-04-29 20:08:16 +000052static PyObject *
Guido van Rossum1ae940a1995-01-02 19:04:15 +000053builtin___import__(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +000054 PyObject *self;
55 PyObject *args;
Guido van Rossum3f5da241990-12-20 15:06:42 +000056{
Guido van Rossum1ae940a1995-01-02 19:04:15 +000057 char *name;
Guido van Rossum79f25d91997-04-29 20:08:16 +000058 PyObject *globals = NULL;
59 PyObject *locals = NULL;
60 PyObject *fromlist = NULL;
Guido van Rossum1ae940a1995-01-02 19:04:15 +000061
Guido van Rossum79f25d91997-04-29 20:08:16 +000062 if (!PyArg_ParseTuple(args, "s|OOO:__import__",
Guido van Rossum24c13741995-02-14 09:42:43 +000063 &name, &globals, &locals, &fromlist))
Guido van Rossum1ae940a1995-01-02 19:04:15 +000064 return NULL;
Guido van Rossumaee0bad1997-09-05 07:33:22 +000065 return PyImport_ImportModuleEx(name, globals, locals, fromlist);
Guido van Rossum1ae940a1995-01-02 19:04:15 +000066}
67
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +000068static char import_doc[] =
69"__import__(name, globals, locals, fromlist) -> module\n\
70\n\
71Import a module. The globals are only used to determine the context;\n\
72they are not modified. The locals are currently unused. The fromlist\n\
73should be a list of names to emulate ``from name import ...'', or an\n\
74empty list to emulate ``import name''.\n\
75When importing a module from a package, note that __import__('A.B', ...)\n\
76returns package A when fromlist is empty, but its submodule B when\n\
77fromlist is not empty.";
78
Guido van Rossum1ae940a1995-01-02 19:04:15 +000079
Guido van Rossum79f25d91997-04-29 20:08:16 +000080static PyObject *
Guido van Rossum1ae940a1995-01-02 19:04:15 +000081builtin_abs(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +000082 PyObject *self;
83 PyObject *args;
Guido van Rossum1ae940a1995-01-02 19:04:15 +000084{
Guido van Rossum79f25d91997-04-29 20:08:16 +000085 PyObject *v;
Guido van Rossum1ae940a1995-01-02 19:04:15 +000086
Guido van Rossum79f25d91997-04-29 20:08:16 +000087 if (!PyArg_ParseTuple(args, "O:abs", &v))
Guido van Rossum1ae940a1995-01-02 19:04:15 +000088 return NULL;
Guido van Rossum09df08a1998-05-22 00:51:39 +000089 return PyNumber_Absolute(v);
Guido van Rossum3f5da241990-12-20 15:06:42 +000090}
91
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +000092static char abs_doc[] =
93"abs(number) -> number\n\
94\n\
95Return the absolute value of the argument.";
96
97
Guido van Rossum79f25d91997-04-29 20:08:16 +000098static PyObject *
Guido van Rossum94390a41992-08-14 15:14:30 +000099builtin_apply(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000100 PyObject *self;
101 PyObject *args;
Guido van Rossumc02e15c1991-12-16 13:03:00 +0000102{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000103 PyObject *func, *alist = NULL, *kwdict = NULL;
Barry Warsaw968f8cb1998-10-01 15:33:12 +0000104 PyObject *t = NULL, *retval = NULL;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000105
Guido van Rossum79f25d91997-04-29 20:08:16 +0000106 if (!PyArg_ParseTuple(args, "O|OO:apply", &func, &alist, &kwdict))
Guido van Rossumc02e15c1991-12-16 13:03:00 +0000107 return NULL;
Barry Warsaw968f8cb1998-10-01 15:33:12 +0000108 if (alist != NULL) {
109 if (!PyTuple_Check(alist)) {
110 if (!PySequence_Check(alist)) {
111 PyErr_SetString(PyExc_TypeError,
112 "apply() 2nd argument must be a sequence");
113 return NULL;
114 }
115 t = PySequence_Tuple(alist);
116 if (t == NULL)
117 return NULL;
118 alist = t;
119 }
Guido van Rossum2d951851994-08-29 12:52:16 +0000120 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000121 if (kwdict != NULL && !PyDict_Check(kwdict)) {
122 PyErr_SetString(PyExc_TypeError,
Guido van Rossum681d79a1995-07-18 14:51:37 +0000123 "apply() 3rd argument must be dictionary");
Barry Warsaw968f8cb1998-10-01 15:33:12 +0000124 goto finally;
Guido van Rossum681d79a1995-07-18 14:51:37 +0000125 }
Barry Warsaw968f8cb1998-10-01 15:33:12 +0000126 retval = PyEval_CallObjectWithKeywords(func, alist, kwdict);
127 finally:
128 Py_XDECREF(t);
129 return retval;
Guido van Rossumc02e15c1991-12-16 13:03:00 +0000130}
131
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000132static char apply_doc[] =
133"apply(function, args[, kwargs]) -> value\n\
134\n\
135Call a function with positional arguments taken from the tuple args,\n\
136and keyword arguments taken from the optional dictionary kwargs.";
137
138
Guido van Rossum79f25d91997-04-29 20:08:16 +0000139static PyObject *
Guido van Rossum2d951851994-08-29 12:52:16 +0000140builtin_callable(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000141 PyObject *self;
142 PyObject *args;
Guido van Rossum2d951851994-08-29 12:52:16 +0000143{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000144 PyObject *v;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000145
Guido van Rossum79f25d91997-04-29 20:08:16 +0000146 if (!PyArg_ParseTuple(args, "O:callable", &v))
Guido van Rossum2d951851994-08-29 12:52:16 +0000147 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000148 return PyInt_FromLong((long)PyCallable_Check(v));
Guido van Rossum2d951851994-08-29 12:52:16 +0000149}
150
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000151static char callable_doc[] =
152"callable(object) -> Boolean\n\
153\n\
154Return whether the object is callable (i.e., some kind of function).\n\
155Note that classes are callable, as are instances with a __call__() method.";
156
157
Guido van Rossum79f25d91997-04-29 20:08:16 +0000158static PyObject *
Guido van Rossume77a7571993-11-03 15:01:26 +0000159builtin_filter(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000160 PyObject *self;
161 PyObject *args;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000162{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000163 PyObject *func, *seq, *result;
164 PySequenceMethods *sqf;
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000165 int len;
166 register int i, j;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000167
Guido van Rossum79f25d91997-04-29 20:08:16 +0000168 if (!PyArg_ParseTuple(args, "OO:filter", &func, &seq))
Guido van Rossum12d12c51993-10-26 17:58:25 +0000169 return NULL;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000170
Guido van Rossum79f25d91997-04-29 20:08:16 +0000171 if (PyString_Check(seq)) {
172 PyObject *r = filterstring(func, seq);
Guido van Rossum12d12c51993-10-26 17:58:25 +0000173 return r;
174 }
175
Guido van Rossum79f25d91997-04-29 20:08:16 +0000176 if (PyTuple_Check(seq)) {
177 PyObject *r = filtertuple(func, seq);
Guido van Rossum12d12c51993-10-26 17:58:25 +0000178 return r;
179 }
180
Guido van Rossum09df08a1998-05-22 00:51:39 +0000181 sqf = seq->ob_type->tp_as_sequence;
182 if (sqf == NULL || sqf->sq_length == NULL || sqf->sq_item == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000183 PyErr_SetString(PyExc_TypeError,
Guido van Rossume77a7571993-11-03 15:01:26 +0000184 "argument 2 to filter() must be a sequence type");
Guido van Rossum12d12c51993-10-26 17:58:25 +0000185 goto Fail_2;
186 }
187
188 if ((len = (*sqf->sq_length)(seq)) < 0)
189 goto Fail_2;
190
Guido van Rossum79f25d91997-04-29 20:08:16 +0000191 if (PyList_Check(seq) && seq->ob_refcnt == 1) {
192 Py_INCREF(seq);
Guido van Rossum12d12c51993-10-26 17:58:25 +0000193 result = seq;
194 }
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000195 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000196 if ((result = PyList_New(len)) == NULL)
Guido van Rossum12d12c51993-10-26 17:58:25 +0000197 goto Fail_2;
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000198 }
Guido van Rossum12d12c51993-10-26 17:58:25 +0000199
Guido van Rossum2d951851994-08-29 12:52:16 +0000200 for (i = j = 0; ; ++i) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000201 PyObject *item, *good;
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000202 int ok;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000203
Guido van Rossum2d951851994-08-29 12:52:16 +0000204 if ((item = (*sqf->sq_item)(seq, i)) == NULL) {
Barry Warsawcde8b1b1997-08-22 21:14:38 +0000205 if (PyErr_ExceptionMatches(PyExc_IndexError)) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000206 PyErr_Clear();
Guido van Rossum2d951851994-08-29 12:52:16 +0000207 break;
208 }
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000209 goto Fail_1;
Guido van Rossum2d951851994-08-29 12:52:16 +0000210 }
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000211
Guido van Rossum79f25d91997-04-29 20:08:16 +0000212 if (func == Py_None) {
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000213 good = item;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000214 Py_INCREF(good);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000215 }
216 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000217 PyObject *arg = Py_BuildValue("(O)", item);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000218 if (arg == NULL)
219 goto Fail_1;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000220 good = PyEval_CallObject(func, arg);
221 Py_DECREF(arg);
Guido van Rossum58b68731995-01-10 17:40:55 +0000222 if (good == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000223 Py_DECREF(item);
Guido van Rossum12d12c51993-10-26 17:58:25 +0000224 goto Fail_1;
Guido van Rossum58b68731995-01-10 17:40:55 +0000225 }
Guido van Rossum12d12c51993-10-26 17:58:25 +0000226 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000227 ok = PyObject_IsTrue(good);
228 Py_DECREF(good);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000229 if (ok) {
Guido van Rossum2d951851994-08-29 12:52:16 +0000230 if (j < len) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000231 if (PyList_SetItem(result, j++, item) < 0)
Guido van Rossum2d951851994-08-29 12:52:16 +0000232 goto Fail_1;
233 }
234 else {
235 j++;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000236 if (PyList_Append(result, item) < 0)
Guido van Rossum2d951851994-08-29 12:52:16 +0000237 goto Fail_1;
238 }
Guido van Rossum58b68731995-01-10 17:40:55 +0000239 } else {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000240 Py_DECREF(item);
Guido van Rossum12d12c51993-10-26 17:58:25 +0000241 }
Guido van Rossum12d12c51993-10-26 17:58:25 +0000242 }
243
Guido van Rossum12d12c51993-10-26 17:58:25 +0000244
Guido van Rossum79f25d91997-04-29 20:08:16 +0000245 if (j < len && PyList_SetSlice(result, j, len, NULL) < 0)
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000246 goto Fail_1;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000247
Guido van Rossum12d12c51993-10-26 17:58:25 +0000248 return result;
249
Guido van Rossum12d12c51993-10-26 17:58:25 +0000250Fail_1:
Guido van Rossum79f25d91997-04-29 20:08:16 +0000251 Py_DECREF(result);
Guido van Rossum12d12c51993-10-26 17:58:25 +0000252Fail_2:
Guido van Rossum12d12c51993-10-26 17:58:25 +0000253 return NULL;
254}
255
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000256static char filter_doc[] =
257"filter(function, sequence) -> list\n\
258\n\
259Return a list containing those items of sequence for which function(item)\n\
260is true. If function is None, return a list of items that are true.";
261
262
Guido van Rossum79f25d91997-04-29 20:08:16 +0000263static PyObject *
Guido van Rossum94390a41992-08-14 15:14:30 +0000264builtin_chr(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000265 PyObject *self;
266 PyObject *args;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000267{
268 long x;
269 char s[1];
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000270
Guido van Rossum79f25d91997-04-29 20:08:16 +0000271 if (!PyArg_ParseTuple(args, "l:chr", &x))
Guido van Rossum3f5da241990-12-20 15:06:42 +0000272 return NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000273 if (x < 0 || x >= 256) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000274 PyErr_SetString(PyExc_ValueError,
275 "chr() arg not in range(256)");
Guido van Rossum3f5da241990-12-20 15:06:42 +0000276 return NULL;
277 }
Guido van Rossum6bf62da1997-04-11 20:37:35 +0000278 s[0] = (char)x;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000279 return PyString_FromStringAndSize(s, 1);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000280}
281
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000282static char chr_doc[] =
283"chr(i) -> character\n\
284\n\
285Return a string of one character with ordinal i; 0 <= i < 256.";
286
287
Guido van Rossum79f25d91997-04-29 20:08:16 +0000288static PyObject *
Guido van Rossuma9e7dc11992-10-18 18:53:57 +0000289builtin_cmp(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000290 PyObject *self;
291 PyObject *args;
Guido van Rossuma9e7dc11992-10-18 18:53:57 +0000292{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000293 PyObject *a, *b;
Guido van Rossum09df08a1998-05-22 00:51:39 +0000294 int c;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000295
Guido van Rossum79f25d91997-04-29 20:08:16 +0000296 if (!PyArg_ParseTuple(args, "OO:cmp", &a, &b))
Guido van Rossuma9e7dc11992-10-18 18:53:57 +0000297 return NULL;
Guido van Rossum09df08a1998-05-22 00:51:39 +0000298 if (PyObject_Cmp(a, b, &c) < 0)
Guido van Rossumc8b6df91997-05-23 00:06:51 +0000299 return NULL;
Guido van Rossum09df08a1998-05-22 00:51:39 +0000300 return PyInt_FromLong((long)c);
Guido van Rossuma9e7dc11992-10-18 18:53:57 +0000301}
302
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000303static char cmp_doc[] =
304"cmp(x, y) -> integer\n\
305\n\
306Return negative if x<y, zero if x==y, positive if x>y.";
307
308
Guido van Rossum79f25d91997-04-29 20:08:16 +0000309static PyObject *
Guido van Rossum5524a591995-01-10 15:26:20 +0000310builtin_coerce(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000311 PyObject *self;
312 PyObject *args;
Guido van Rossum6a00cd81995-01-07 12:39:01 +0000313{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000314 PyObject *v, *w;
315 PyObject *res;
Guido van Rossum5524a591995-01-10 15:26:20 +0000316
Guido van Rossum79f25d91997-04-29 20:08:16 +0000317 if (!PyArg_ParseTuple(args, "OO:coerce", &v, &w))
Guido van Rossum5524a591995-01-10 15:26:20 +0000318 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000319 if (PyNumber_Coerce(&v, &w) < 0)
Guido van Rossum04691fc1992-08-12 15:35:34 +0000320 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000321 res = Py_BuildValue("(OO)", v, w);
322 Py_DECREF(v);
323 Py_DECREF(w);
Guido van Rossum04691fc1992-08-12 15:35:34 +0000324 return res;
325}
326
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000327static char coerce_doc[] =
328"coerce(x, y) -> None or (x1, y1)\n\
329\n\
330When x and y can be coerced to values of the same type, return a tuple\n\
331containing the coerced values. When they can't be coerced, return None.";
332
333
Guido van Rossum79f25d91997-04-29 20:08:16 +0000334static PyObject *
Guido van Rossum5b722181993-03-30 17:46:03 +0000335builtin_compile(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000336 PyObject *self;
337 PyObject *args;
Guido van Rossum5b722181993-03-30 17:46:03 +0000338{
339 char *str;
340 char *filename;
341 char *startstr;
342 int start;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000343
Guido van Rossum79f25d91997-04-29 20:08:16 +0000344 if (!PyArg_ParseTuple(args, "sss:compile", &str, &filename, &startstr))
Guido van Rossum5b722181993-03-30 17:46:03 +0000345 return NULL;
346 if (strcmp(startstr, "exec") == 0)
Guido van Rossumb05a5c71997-05-07 17:46:13 +0000347 start = Py_file_input;
Guido van Rossum5b722181993-03-30 17:46:03 +0000348 else if (strcmp(startstr, "eval") == 0)
Guido van Rossumb05a5c71997-05-07 17:46:13 +0000349 start = Py_eval_input;
Guido van Rossum872537c1995-07-07 22:43:42 +0000350 else if (strcmp(startstr, "single") == 0)
Guido van Rossumb05a5c71997-05-07 17:46:13 +0000351 start = Py_single_input;
Guido van Rossum5b722181993-03-30 17:46:03 +0000352 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000353 PyErr_SetString(PyExc_ValueError,
Guido van Rossum872537c1995-07-07 22:43:42 +0000354 "compile() mode must be 'exec' or 'eval' or 'single'");
Guido van Rossum5b722181993-03-30 17:46:03 +0000355 return NULL;
356 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000357 return Py_CompileString(str, filename, start);
Guido van Rossum5b722181993-03-30 17:46:03 +0000358}
359
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000360static char compile_doc[] =
361"compile(source, filename, mode) -> code object\n\
362\n\
363Compile the source string (a Python module, statement or expression)\n\
364into a code object that can be executed by the exec statement or eval().\n\
365The filename will be used for run-time error messages.\n\
366The mode must be 'exec' to compile a module, 'single' to compile a\n\
367single (interactive) statement, or 'eval' to compile an expression.";
368
369
Guido van Rossum8a5c5d21996-01-12 01:09:56 +0000370#ifndef WITHOUT_COMPLEX
371
Guido van Rossum79f25d91997-04-29 20:08:16 +0000372static PyObject *
Guido van Rossum8a5c5d21996-01-12 01:09:56 +0000373builtin_complex(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000374 PyObject *self;
375 PyObject *args;
Guido van Rossum8a5c5d21996-01-12 01:09:56 +0000376{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000377 PyObject *r, *i, *tmp;
378 PyNumberMethods *nbr, *nbi = NULL;
Guido van Rossum530956d1996-07-21 02:27:43 +0000379 Py_complex cr, ci;
Guido van Rossumc6472e91997-03-31 17:15:43 +0000380 int own_r = 0;
Guido van Rossum8a5c5d21996-01-12 01:09:56 +0000381
382 i = NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000383 if (!PyArg_ParseTuple(args, "O|O:complex", &r, &i))
Guido van Rossum8a5c5d21996-01-12 01:09:56 +0000384 return NULL;
385 if ((nbr = r->ob_type->tp_as_number) == NULL ||
Guido van Rossumc6472e91997-03-31 17:15:43 +0000386 nbr->nb_float == NULL ||
387 (i != NULL &&
388 ((nbi = i->ob_type->tp_as_number) == NULL ||
389 nbi->nb_float == NULL))) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000390 PyErr_SetString(PyExc_TypeError,
Guido van Rossum8a5c5d21996-01-12 01:09:56 +0000391 "complex() argument can't be converted to complex");
392 return NULL;
393 }
Guido van Rossumed0af8f1996-12-05 23:18:18 +0000394 /* XXX Hack to support classes with __complex__ method */
Guido van Rossum79f25d91997-04-29 20:08:16 +0000395 if (PyInstance_Check(r)) {
396 static PyObject *complexstr;
397 PyObject *f;
Guido van Rossumed0af8f1996-12-05 23:18:18 +0000398 if (complexstr == NULL) {
Guido van Rossum8d751611997-01-18 08:04:16 +0000399 complexstr = PyString_InternFromString("__complex__");
Guido van Rossumed0af8f1996-12-05 23:18:18 +0000400 if (complexstr == NULL)
401 return NULL;
402 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000403 f = PyObject_GetAttr(r, complexstr);
Guido van Rossumed0af8f1996-12-05 23:18:18 +0000404 if (f == NULL)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000405 PyErr_Clear();
Guido van Rossumed0af8f1996-12-05 23:18:18 +0000406 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000407 PyObject *args = Py_BuildValue("()");
Guido van Rossumed0af8f1996-12-05 23:18:18 +0000408 if (args == NULL)
409 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000410 r = PyEval_CallObject(f, args);
411 Py_DECREF(args);
Guido van Rossumed0af8f1996-12-05 23:18:18 +0000412 if (r == NULL)
413 return NULL;
Guido van Rossumc6472e91997-03-31 17:15:43 +0000414 own_r = 1;
Guido van Rossumed0af8f1996-12-05 23:18:18 +0000415 }
416 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000417 if (PyComplex_Check(r)) {
418 cr = ((PyComplexObject*)r)->cval;
Guido van Rossum730806d1998-04-10 22:27:42 +0000419 if (own_r) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000420 Py_DECREF(r);
Guido van Rossum730806d1998-04-10 22:27:42 +0000421 }
Guido van Rossumc6472e91997-03-31 17:15:43 +0000422 }
Guido van Rossum8a5c5d21996-01-12 01:09:56 +0000423 else {
Guido van Rossumfe4b6ee1996-08-08 18:49:41 +0000424 tmp = (*nbr->nb_float)(r);
Guido van Rossum730806d1998-04-10 22:27:42 +0000425 if (own_r) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000426 Py_DECREF(r);
Guido van Rossum730806d1998-04-10 22:27:42 +0000427 }
Guido van Rossumfe4b6ee1996-08-08 18:49:41 +0000428 if (tmp == NULL)
429 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000430 cr.real = PyFloat_AsDouble(tmp);
431 Py_DECREF(tmp);
Guido van Rossum8a5c5d21996-01-12 01:09:56 +0000432 cr.imag = 0.;
433 }
434 if (i == NULL) {
435 ci.real = 0.;
436 ci.imag = 0.;
437 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000438 else if (PyComplex_Check(i))
439 ci = ((PyComplexObject*)i)->cval;
Guido van Rossum8a5c5d21996-01-12 01:09:56 +0000440 else {
Guido van Rossumc6472e91997-03-31 17:15:43 +0000441 tmp = (*nbi->nb_float)(i);
Guido van Rossumfe4b6ee1996-08-08 18:49:41 +0000442 if (tmp == NULL)
443 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000444 ci.real = PyFloat_AsDouble(tmp);
445 Py_DECREF(tmp);
Guido van Rossum8a5c5d21996-01-12 01:09:56 +0000446 ci.imag = 0.;
447 }
448 cr.real -= ci.imag;
449 cr.imag += ci.real;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000450 return PyComplex_FromCComplex(cr);
Guido van Rossum8a5c5d21996-01-12 01:09:56 +0000451}
452
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000453static char complex_doc[] =
454"complex(real[, imag]) -> complex number\n\
455\n\
456Create a complex number from a real part and an optional imaginary part.\n\
457This is equivalent to (real + imag*1j) where imag defaults to 0.";
458
459
Guido van Rossum8a5c5d21996-01-12 01:09:56 +0000460#endif
461
Guido van Rossum79f25d91997-04-29 20:08:16 +0000462static PyObject *
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000463builtin_dir(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000464 PyObject *self;
465 PyObject *args;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000466{
Guido van Rossum666b17a1997-05-06 16:36:57 +0000467 static char *attrlist[] = {"__members__", "__methods__", NULL};
468 PyObject *v = NULL, *l = NULL, *m = NULL;
469 PyObject *d, *x;
470 int i;
471 char **s;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000472
Guido van Rossum79f25d91997-04-29 20:08:16 +0000473 if (!PyArg_ParseTuple(args, "|O:dir", &v))
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000474 return NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000475 if (v == NULL) {
Guido van Rossum666b17a1997-05-06 16:36:57 +0000476 x = PyEval_GetLocals();
477 if (x == NULL)
478 goto error;
479 l = PyMapping_Keys(x);
480 if (l == NULL)
481 goto error;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000482 }
483 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000484 d = PyObject_GetAttrString(v, "__dict__");
Guido van Rossum666b17a1997-05-06 16:36:57 +0000485 if (d == NULL)
Guido van Rossum795ba581996-05-23 22:49:07 +0000486 PyErr_Clear();
Guido van Rossum666b17a1997-05-06 16:36:57 +0000487 else {
488 l = PyMapping_Keys(d);
489 if (l == NULL)
490 PyErr_Clear();
491 Py_DECREF(d);
492 }
493 if (l == NULL) {
494 l = PyList_New(0);
495 if (l == NULL)
496 goto error;
497 }
498 for (s = attrlist; *s != NULL; s++) {
499 m = PyObject_GetAttrString(v, *s);
500 if (m == NULL) {
501 PyErr_Clear();
502 continue;
503 }
504 for (i = 0; ; i++) {
505 x = PySequence_GetItem(m, i);
506 if (x == NULL) {
507 PyErr_Clear();
508 break;
509 }
510 if (PyList_Append(l, x) != 0) {
511 Py_DECREF(x);
512 Py_DECREF(m);
513 goto error;
514 }
515 Py_DECREF(x);
516 }
517 Py_DECREF(m);
Guido van Rossum795ba581996-05-23 22:49:07 +0000518 }
Guido van Rossum006bcd41991-10-24 14:54:44 +0000519 }
Guido van Rossum666b17a1997-05-06 16:36:57 +0000520 if (PyList_Sort(l) != 0)
521 goto error;
522 return l;
523 error:
524 Py_XDECREF(l);
525 return NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000526}
527
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000528static char dir_doc[] =
529"dir([object]) -> list of strings\n\
530\n\
531Return an alphabetized list of names comprising (some of) the attributes\n\
532of the given object. Without an argument, the names in the current scope\n\
533are listed. With an instance argument, only the instance attributes are\n\
534returned. With a class argument, attributes of the base class are not\n\
535returned. For other types or arguments, this may list members or methods.";
536
537
Guido van Rossum79f25d91997-04-29 20:08:16 +0000538static PyObject *
Guido van Rossum6a00cd81995-01-07 12:39:01 +0000539builtin_divmod(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000540 PyObject *self;
541 PyObject *args;
Guido van Rossum6a00cd81995-01-07 12:39:01 +0000542{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000543 PyObject *v, *w;
Guido van Rossum6a00cd81995-01-07 12:39:01 +0000544
Guido van Rossum79f25d91997-04-29 20:08:16 +0000545 if (!PyArg_ParseTuple(args, "OO:divmod", &v, &w))
Guido van Rossum6a00cd81995-01-07 12:39:01 +0000546 return NULL;
Guido van Rossum09df08a1998-05-22 00:51:39 +0000547 return PyNumber_Divmod(v, w);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000548}
549
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000550static char divmod_doc[] =
551"divmod(x, y) -> (div, mod)\n\
552\n\
553Return the tuple ((x-x%y)/y, x%y). Invariant: div*y + mod == x.";
554
555
Guido van Rossum79f25d91997-04-29 20:08:16 +0000556static PyObject *
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000557builtin_eval(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000558 PyObject *self;
559 PyObject *args;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000560{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000561 PyObject *cmd;
562 PyObject *globals = Py_None, *locals = Py_None;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000563 char *str;
Guido van Rossum590baa41993-11-30 13:40:46 +0000564
Guido van Rossum79f25d91997-04-29 20:08:16 +0000565 if (!PyArg_ParseTuple(args, "O|O!O!:eval",
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000566 &cmd,
Guido van Rossum79f25d91997-04-29 20:08:16 +0000567 &PyDict_Type, &globals,
568 &PyDict_Type, &locals))
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000569 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000570 if (globals == Py_None) {
571 globals = PyEval_GetGlobals();
572 if (locals == Py_None)
573 locals = PyEval_GetLocals();
Guido van Rossum6135a871995-01-09 17:53:26 +0000574 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000575 else if (locals == Py_None)
Guido van Rossum6135a871995-01-09 17:53:26 +0000576 locals = globals;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000577 if (PyDict_GetItemString(globals, "__builtins__") == NULL) {
578 if (PyDict_SetItemString(globals, "__builtins__",
579 PyEval_GetBuiltins()) != 0)
Guido van Rossum6135a871995-01-09 17:53:26 +0000580 return NULL;
581 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000582 if (PyCode_Check(cmd))
583 return PyEval_EvalCode((PyCodeObject *) cmd, globals, locals);
584 if (!PyString_Check(cmd)) {
585 PyErr_SetString(PyExc_TypeError,
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000586 "eval() argument 1 must be string or code object");
Guido van Rossum94390a41992-08-14 15:14:30 +0000587 return NULL;
588 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000589 str = PyString_AsString(cmd);
590 if ((int)strlen(str) != PyString_Size(cmd)) {
591 PyErr_SetString(PyExc_ValueError,
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000592 "embedded '\\0' in string arg");
593 return NULL;
Guido van Rossumf08ab0a1992-03-04 16:41:41 +0000594 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000595 while (*str == ' ' || *str == '\t')
596 str++;
Guido van Rossumb05a5c71997-05-07 17:46:13 +0000597 return PyRun_String(str, Py_eval_input, globals, locals);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000598}
599
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000600static char eval_doc[] =
601"eval(source[, globals[, locals]]) -> value\n\
602\n\
603Evaluate the source in the context of globals and locals.\n\
604The source may be a string representing a Python expression\n\
605or a code object as returned by compile().\n\
606The globals and locals are dictionaries, defaulting to the current\n\
607globals and locals. If only globals is given, locals defaults to it.";
608
609
Guido van Rossum79f25d91997-04-29 20:08:16 +0000610static PyObject *
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000611builtin_execfile(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000612 PyObject *self;
613 PyObject *args;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000614{
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000615 char *filename;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000616 PyObject *globals = Py_None, *locals = Py_None;
617 PyObject *res;
Guido van Rossum0f61f8a1992-02-25 18:55:05 +0000618 FILE* fp;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000619
Guido van Rossum79f25d91997-04-29 20:08:16 +0000620 if (!PyArg_ParseTuple(args, "s|O!O!:execfile",
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000621 &filename,
Guido van Rossum79f25d91997-04-29 20:08:16 +0000622 &PyDict_Type, &globals,
623 &PyDict_Type, &locals))
Guido van Rossum0f61f8a1992-02-25 18:55:05 +0000624 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000625 if (globals == Py_None) {
626 globals = PyEval_GetGlobals();
627 if (locals == Py_None)
628 locals = PyEval_GetLocals();
Guido van Rossum6135a871995-01-09 17:53:26 +0000629 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000630 else if (locals == Py_None)
Guido van Rossum6135a871995-01-09 17:53:26 +0000631 locals = globals;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000632 if (PyDict_GetItemString(globals, "__builtins__") == NULL) {
633 if (PyDict_SetItemString(globals, "__builtins__",
634 PyEval_GetBuiltins()) != 0)
Guido van Rossum6135a871995-01-09 17:53:26 +0000635 return NULL;
636 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000637 Py_BEGIN_ALLOW_THREADS
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000638 fp = fopen(filename, "r");
Guido van Rossum79f25d91997-04-29 20:08:16 +0000639 Py_END_ALLOW_THREADS
Guido van Rossum0f61f8a1992-02-25 18:55:05 +0000640 if (fp == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000641 PyErr_SetFromErrno(PyExc_IOError);
Guido van Rossum0f61f8a1992-02-25 18:55:05 +0000642 return NULL;
643 }
Guido van Rossumb05a5c71997-05-07 17:46:13 +0000644 res = PyRun_File(fp, filename, Py_file_input, globals, locals);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000645 Py_BEGIN_ALLOW_THREADS
Guido van Rossum0f61f8a1992-02-25 18:55:05 +0000646 fclose(fp);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000647 Py_END_ALLOW_THREADS
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000648 return res;
Guido van Rossum0f61f8a1992-02-25 18:55:05 +0000649}
650
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000651static char execfile_doc[] =
652"execfile(filename[, globals[, locals]])\n\
653\n\
654Read and execute a Python script from a file.\n\
655The globals and locals are dictionaries, defaulting to the current\n\
656globals and locals. If only globals is given, locals defaults to it.";
657
658
Guido van Rossum79f25d91997-04-29 20:08:16 +0000659static PyObject *
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000660builtin_float(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000661 PyObject *self;
662 PyObject *args;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000663{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000664 PyObject *v;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000665
Guido van Rossum79f25d91997-04-29 20:08:16 +0000666 if (!PyArg_ParseTuple(args, "O:float", &v))
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000667 return NULL;
Guido van Rossum09df08a1998-05-22 00:51:39 +0000668 return PyNumber_Float(v);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000669}
670
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000671static char float_doc[] =
672"float(x) -> floating point number\n\
673\n\
674Convert a string or number to a floating point number, if possible.";
675
676
Guido van Rossum79f25d91997-04-29 20:08:16 +0000677static PyObject *
Guido van Rossum94390a41992-08-14 15:14:30 +0000678builtin_getattr(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000679 PyObject *self;
680 PyObject *args;
Guido van Rossum33894be1992-01-27 16:53:09 +0000681{
Guido van Rossum950ff291998-06-29 13:38:57 +0000682 PyObject *v, *result, *dflt = NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000683 PyObject *name;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000684
Guido van Rossum950ff291998-06-29 13:38:57 +0000685 if (!PyArg_ParseTuple(args, "OS|O:getattr", &v, &name, &dflt))
Guido van Rossum33894be1992-01-27 16:53:09 +0000686 return NULL;
Guido van Rossum950ff291998-06-29 13:38:57 +0000687 result = PyObject_GetAttr(v, name);
688 if (result == NULL && dflt != NULL) {
689 PyErr_Clear();
690 Py_INCREF(dflt);
691 result = dflt;
692 }
693 return result;
Guido van Rossum9bfef441993-03-29 10:43:31 +0000694}
695
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000696static char getattr_doc[] =
Guido van Rossum950ff291998-06-29 13:38:57 +0000697"getattr(object, name[, default]) -> value\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000698\n\
Guido van Rossum950ff291998-06-29 13:38:57 +0000699Get a named attribute from an object; getattr(x, 'y') is equivalent to x.y.\n\
700When a default argument is given, it is returned when the attribute doesn't\n\
701exist; without it, an exception is raised in that case.";
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000702
703
Guido van Rossum79f25d91997-04-29 20:08:16 +0000704static PyObject *
Guido van Rossum872537c1995-07-07 22:43:42 +0000705builtin_globals(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000706 PyObject *self;
707 PyObject *args;
Guido van Rossum872537c1995-07-07 22:43:42 +0000708{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000709 PyObject *d;
Guido van Rossum872537c1995-07-07 22:43:42 +0000710
Guido van Rossum79f25d91997-04-29 20:08:16 +0000711 if (!PyArg_ParseTuple(args, ""))
Guido van Rossum872537c1995-07-07 22:43:42 +0000712 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000713 d = PyEval_GetGlobals();
714 Py_INCREF(d);
Guido van Rossum872537c1995-07-07 22:43:42 +0000715 return d;
716}
717
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000718static char globals_doc[] =
719"globals() -> dictionary\n\
720\n\
721Return the dictionary containing the current scope's global variables.";
722
723
Guido van Rossum79f25d91997-04-29 20:08:16 +0000724static PyObject *
Guido van Rossum9bfef441993-03-29 10:43:31 +0000725builtin_hasattr(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000726 PyObject *self;
727 PyObject *args;
Guido van Rossum9bfef441993-03-29 10:43:31 +0000728{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000729 PyObject *v;
730 PyObject *name;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000731
Guido van Rossum79f25d91997-04-29 20:08:16 +0000732 if (!PyArg_ParseTuple(args, "OS:hasattr", &v, &name))
Guido van Rossum9bfef441993-03-29 10:43:31 +0000733 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000734 v = PyObject_GetAttr(v, name);
Guido van Rossum9bfef441993-03-29 10:43:31 +0000735 if (v == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000736 PyErr_Clear();
Guido van Rossum09df08a1998-05-22 00:51:39 +0000737 Py_INCREF(Py_False);
738 return Py_False;
Guido van Rossum9bfef441993-03-29 10:43:31 +0000739 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000740 Py_DECREF(v);
Guido van Rossum09df08a1998-05-22 00:51:39 +0000741 Py_INCREF(Py_True);
742 return Py_True;
Guido van Rossum33894be1992-01-27 16:53:09 +0000743}
744
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000745static char hasattr_doc[] =
746"hasattr(object, name) -> Boolean\n\
747\n\
748Return whether the object has an attribute with the given name.\n\
749(This is done by calling getattr(object, name) and catching exceptions.)";
750
751
Guido van Rossum79f25d91997-04-29 20:08:16 +0000752static PyObject *
Guido van Rossum5b722181993-03-30 17:46:03 +0000753builtin_id(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000754 PyObject *self;
755 PyObject *args;
Guido van Rossum5b722181993-03-30 17:46:03 +0000756{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000757 PyObject *v;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000758
Guido van Rossum79f25d91997-04-29 20:08:16 +0000759 if (!PyArg_ParseTuple(args, "O:id", &v))
Guido van Rossum5b722181993-03-30 17:46:03 +0000760 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000761 return PyInt_FromLong((long)v);
Guido van Rossum5b722181993-03-30 17:46:03 +0000762}
763
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000764static char id_doc[] =
765"id(object) -> integer\n\
766\n\
767Return the identity of an object. This is guaranteed to be unique among\n\
768simultaneously existing objects. (Hint: it's the object's memory address.)";
769
770
Guido van Rossum79f25d91997-04-29 20:08:16 +0000771static PyObject *
Guido van Rossum12d12c51993-10-26 17:58:25 +0000772builtin_map(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000773 PyObject *self;
774 PyObject *args;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000775{
776 typedef struct {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000777 PyObject *seq;
778 PySequenceMethods *sqf;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000779 int len;
780 } sequence;
781
Guido van Rossum79f25d91997-04-29 20:08:16 +0000782 PyObject *func, *result;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000783 sequence *seqs = NULL, *sqp;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000784 int n, len;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000785 register int i, j;
786
Guido van Rossum79f25d91997-04-29 20:08:16 +0000787 n = PyTuple_Size(args);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000788 if (n < 2) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000789 PyErr_SetString(PyExc_TypeError,
790 "map() requires at least two args");
Guido van Rossum12d12c51993-10-26 17:58:25 +0000791 return NULL;
792 }
793
Guido van Rossum79f25d91997-04-29 20:08:16 +0000794 func = PyTuple_GetItem(args, 0);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000795 n--;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000796
Guido van Rossumfa4ac711998-07-10 17:37:30 +0000797 if (func == Py_None && n == 1) {
798 /* map(None, S) is the same as list(S). */
799 return PySequence_List(PyTuple_GetItem(args, 1));
800 }
801
Guido van Rossum79f25d91997-04-29 20:08:16 +0000802 if ((seqs = PyMem_NEW(sequence, n)) == NULL) {
803 PyErr_NoMemory();
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000804 goto Fail_2;
805 }
Guido van Rossum12d12c51993-10-26 17:58:25 +0000806
Guido van Rossum2d951851994-08-29 12:52:16 +0000807 for (len = 0, i = 0, sqp = seqs; i < n; ++i, ++sqp) {
Guido van Rossum12d12c51993-10-26 17:58:25 +0000808 int curlen;
Guido van Rossum09df08a1998-05-22 00:51:39 +0000809 PySequenceMethods *sqf;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000810
Guido van Rossum79f25d91997-04-29 20:08:16 +0000811 if ((sqp->seq = PyTuple_GetItem(args, i + 1)) == NULL)
Guido van Rossum12d12c51993-10-26 17:58:25 +0000812 goto Fail_2;
813
Guido van Rossum09df08a1998-05-22 00:51:39 +0000814 sqp->sqf = sqf = sqp->seq->ob_type->tp_as_sequence;
815 if (sqf == NULL ||
816 sqf->sq_length == NULL ||
817 sqf->sq_item == NULL)
818 {
Guido van Rossum12d12c51993-10-26 17:58:25 +0000819 static char errmsg[] =
820 "argument %d to map() must be a sequence object";
Guido van Rossum15e33a41997-04-30 19:00:27 +0000821 char errbuf[sizeof(errmsg) + 25];
Guido van Rossum12d12c51993-10-26 17:58:25 +0000822
823 sprintf(errbuf, errmsg, i+2);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000824 PyErr_SetString(PyExc_TypeError, errbuf);
Guido van Rossum12d12c51993-10-26 17:58:25 +0000825 goto Fail_2;
826 }
827
828 if ((curlen = sqp->len = (*sqp->sqf->sq_length)(sqp->seq)) < 0)
829 goto Fail_2;
830
831 if (curlen > len)
832 len = curlen;
833 }
834
Guido van Rossum79f25d91997-04-29 20:08:16 +0000835 if ((result = (PyObject *) PyList_New(len)) == NULL)
Guido van Rossum12d12c51993-10-26 17:58:25 +0000836 goto Fail_2;
837
Guido van Rossum2d951851994-08-29 12:52:16 +0000838 for (i = 0; ; ++i) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000839 PyObject *alist, *item=NULL, *value;
Guido van Rossum2d951851994-08-29 12:52:16 +0000840 int any = 0;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000841
Guido van Rossum79f25d91997-04-29 20:08:16 +0000842 if (func == Py_None && n == 1)
Guido van Rossum32120311995-07-10 13:52:21 +0000843 alist = NULL;
Guido van Rossum2d951851994-08-29 12:52:16 +0000844 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000845 if ((alist = PyTuple_New(n)) == NULL)
Guido van Rossum2d951851994-08-29 12:52:16 +0000846 goto Fail_1;
847 }
Guido van Rossum12d12c51993-10-26 17:58:25 +0000848
849 for (j = 0, sqp = seqs; j < n; ++j, ++sqp) {
Guido van Rossum2d951851994-08-29 12:52:16 +0000850 if (sqp->len < 0) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000851 Py_INCREF(Py_None);
852 item = Py_None;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000853 }
Guido van Rossum12d12c51993-10-26 17:58:25 +0000854 else {
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000855 item = (*sqp->sqf->sq_item)(sqp->seq, i);
Guido van Rossum2d951851994-08-29 12:52:16 +0000856 if (item == NULL) {
Barry Warsawcde8b1b1997-08-22 21:14:38 +0000857 if (PyErr_ExceptionMatches(
858 PyExc_IndexError))
859 {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000860 PyErr_Clear();
861 Py_INCREF(Py_None);
862 item = Py_None;
Guido van Rossum2d951851994-08-29 12:52:16 +0000863 sqp->len = -1;
864 }
865 else {
866 goto Fail_0;
867 }
868 }
869 else
870 any = 1;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000871
Guido van Rossum12d12c51993-10-26 17:58:25 +0000872 }
Guido van Rossum32120311995-07-10 13:52:21 +0000873 if (!alist)
Guido van Rossum2d951851994-08-29 12:52:16 +0000874 break;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000875 if (PyTuple_SetItem(alist, j, item) < 0) {
876 Py_DECREF(item);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000877 goto Fail_0;
Guido van Rossum2d951851994-08-29 12:52:16 +0000878 }
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000879 continue;
880
881 Fail_0:
Guido van Rossum79f25d91997-04-29 20:08:16 +0000882 Py_XDECREF(alist);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000883 goto Fail_1;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000884 }
885
Guido van Rossum32120311995-07-10 13:52:21 +0000886 if (!alist)
887 alist = item;
Guido van Rossum2d951851994-08-29 12:52:16 +0000888
889 if (!any) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000890 Py_DECREF(alist);
Guido van Rossum2d951851994-08-29 12:52:16 +0000891 break;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000892 }
Guido van Rossum2d951851994-08-29 12:52:16 +0000893
Guido van Rossum79f25d91997-04-29 20:08:16 +0000894 if (func == Py_None)
Guido van Rossum32120311995-07-10 13:52:21 +0000895 value = alist;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000896 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000897 value = PyEval_CallObject(func, alist);
898 Py_DECREF(alist);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000899 if (value == NULL)
900 goto Fail_1;
Guido van Rossum2d951851994-08-29 12:52:16 +0000901 }
902 if (i >= len) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000903 if (PyList_Append(result, value) < 0)
Guido van Rossum2d951851994-08-29 12:52:16 +0000904 goto Fail_1;
905 }
906 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000907 if (PyList_SetItem(result, i, value) < 0)
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000908 goto Fail_1;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000909 }
910 }
911
Guido van Rossumfa4ac711998-07-10 17:37:30 +0000912 if (i < len && PyList_SetSlice(result, i, len, NULL) < 0)
913 goto Fail_1;
914
Guido van Rossum79f25d91997-04-29 20:08:16 +0000915 PyMem_DEL(seqs);
Guido van Rossum12d12c51993-10-26 17:58:25 +0000916 return result;
917
Guido van Rossum12d12c51993-10-26 17:58:25 +0000918Fail_1:
Guido van Rossum79f25d91997-04-29 20:08:16 +0000919 Py_DECREF(result);
Guido van Rossum12d12c51993-10-26 17:58:25 +0000920Fail_2:
Guido van Rossum79f25d91997-04-29 20:08:16 +0000921 if (seqs) PyMem_DEL(seqs);
Guido van Rossum12d12c51993-10-26 17:58:25 +0000922 return NULL;
923}
924
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000925static char map_doc[] =
926"map(function, sequence[, sequence, ...]) -> list\n\
927\n\
928Return a list of the results of applying the function to the items of\n\
929the argument sequence(s). If more than one sequence is given, the\n\
930function is called with an argument list consisting of the corresponding\n\
931item of each sequence, substituting None for missing values when not all\n\
932sequences have the same length. If the function is None, return a list of\n\
933the items of the sequence (or a list of tuples if more than one sequence).";
934
935
Guido van Rossum79f25d91997-04-29 20:08:16 +0000936static PyObject *
Guido van Rossum94390a41992-08-14 15:14:30 +0000937builtin_setattr(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000938 PyObject *self;
939 PyObject *args;
Guido van Rossum33894be1992-01-27 16:53:09 +0000940{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000941 PyObject *v;
942 PyObject *name;
943 PyObject *value;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000944
Guido van Rossum79f25d91997-04-29 20:08:16 +0000945 if (!PyArg_ParseTuple(args, "OSO:setattr", &v, &name, &value))
Guido van Rossum33894be1992-01-27 16:53:09 +0000946 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000947 if (PyObject_SetAttr(v, name, value) != 0)
Guido van Rossum33894be1992-01-27 16:53:09 +0000948 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000949 Py_INCREF(Py_None);
950 return Py_None;
Guido van Rossum33894be1992-01-27 16:53:09 +0000951}
952
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000953static char setattr_doc[] =
954"setattr(object, name, value)\n\
955\n\
956Set a named attribute on an object; setattr(x, 'y', v) is equivalent to\n\
957``x.y = v''.";
958
959
Guido van Rossum79f25d91997-04-29 20:08:16 +0000960static PyObject *
Guido van Rossum14144fc1994-08-29 12:53:40 +0000961builtin_delattr(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000962 PyObject *self;
963 PyObject *args;
Guido van Rossum14144fc1994-08-29 12:53:40 +0000964{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000965 PyObject *v;
966 PyObject *name;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000967
Guido van Rossum79f25d91997-04-29 20:08:16 +0000968 if (!PyArg_ParseTuple(args, "OS:delattr", &v, &name))
Guido van Rossum14144fc1994-08-29 12:53:40 +0000969 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000970 if (PyObject_SetAttr(v, name, (PyObject *)NULL) != 0)
Guido van Rossum14144fc1994-08-29 12:53:40 +0000971 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000972 Py_INCREF(Py_None);
973 return Py_None;
Guido van Rossum14144fc1994-08-29 12:53:40 +0000974}
975
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000976static char delattr_doc[] =
Guido van Rossumdf12a591998-11-23 22:13:04 +0000977"delattr(object, name)\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000978\n\
979Delete a named attribute on an object; delattr(x, 'y') is equivalent to\n\
980``del x.y''.";
981
982
Guido van Rossum79f25d91997-04-29 20:08:16 +0000983static PyObject *
Guido van Rossum9bfef441993-03-29 10:43:31 +0000984builtin_hash(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000985 PyObject *self;
986 PyObject *args;
Guido van Rossum9bfef441993-03-29 10:43:31 +0000987{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000988 PyObject *v;
Guido van Rossum9bfef441993-03-29 10:43:31 +0000989 long x;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000990
Guido van Rossum79f25d91997-04-29 20:08:16 +0000991 if (!PyArg_ParseTuple(args, "O:hash", &v))
Guido van Rossum9bfef441993-03-29 10:43:31 +0000992 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000993 x = PyObject_Hash(v);
Guido van Rossum9bfef441993-03-29 10:43:31 +0000994 if (x == -1)
995 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000996 return PyInt_FromLong(x);
Guido van Rossum9bfef441993-03-29 10:43:31 +0000997}
998
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000999static char hash_doc[] =
1000"hash(object) -> integer\n\
1001\n\
1002Return a hash value for the object. Two objects with the same value have\n\
1003the same hash value. The reverse is not necessarily true, but likely.";
1004
1005
Guido van Rossum79f25d91997-04-29 20:08:16 +00001006static PyObject *
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001007builtin_hex(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001008 PyObject *self;
1009 PyObject *args;
Guido van Rossum006bcd41991-10-24 14:54:44 +00001010{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001011 PyObject *v;
1012 PyNumberMethods *nb;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001013
Guido van Rossum79f25d91997-04-29 20:08:16 +00001014 if (!PyArg_ParseTuple(args, "O:hex", &v))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001015 return NULL;
Guido van Rossum1899c2e1992-09-12 11:09:23 +00001016
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001017 if ((nb = v->ob_type->tp_as_number) == NULL ||
Guido van Rossum1899c2e1992-09-12 11:09:23 +00001018 nb->nb_hex == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001019 PyErr_SetString(PyExc_TypeError,
Guido van Rossum1899c2e1992-09-12 11:09:23 +00001020 "hex() argument can't be converted to hex");
1021 return NULL;
Guido van Rossum006bcd41991-10-24 14:54:44 +00001022 }
Guido van Rossum1899c2e1992-09-12 11:09:23 +00001023 return (*nb->nb_hex)(v);
Guido van Rossum006bcd41991-10-24 14:54:44 +00001024}
1025
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001026static char hex_doc[] =
1027"hex(number) -> string\n\
1028\n\
1029Return the hexadecimal representation of an integer or long integer.";
1030
1031
Guido van Rossum79f25d91997-04-29 20:08:16 +00001032static PyObject *builtin_raw_input Py_PROTO((PyObject *, PyObject *));
Guido van Rossum3165fe61992-09-25 21:59:05 +00001033
Guido van Rossum79f25d91997-04-29 20:08:16 +00001034static PyObject *
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001035builtin_input(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001036 PyObject *self;
1037 PyObject *args;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001038{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001039 PyObject *line;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001040 char *str;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001041 PyObject *res;
1042 PyObject *globals, *locals;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001043
1044 line = builtin_raw_input(self, args);
Guido van Rossum3165fe61992-09-25 21:59:05 +00001045 if (line == NULL)
1046 return line;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001047 if (!PyArg_Parse(line, "s;embedded '\\0' in input line", &str))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001048 return NULL;
1049 while (*str == ' ' || *str == '\t')
1050 str++;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001051 globals = PyEval_GetGlobals();
1052 locals = PyEval_GetLocals();
1053 if (PyDict_GetItemString(globals, "__builtins__") == NULL) {
1054 if (PyDict_SetItemString(globals, "__builtins__",
1055 PyEval_GetBuiltins()) != 0)
Guido van Rossum6135a871995-01-09 17:53:26 +00001056 return NULL;
1057 }
Guido van Rossumb05a5c71997-05-07 17:46:13 +00001058 res = PyRun_String(str, Py_eval_input, globals, locals);
Guido van Rossum79f25d91997-04-29 20:08:16 +00001059 Py_DECREF(line);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001060 return res;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001061}
1062
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001063static char input_doc[] =
1064"input([prompt]) -> value\n\
1065\n\
1066Equivalent to eval(raw_input(prompt)).";
1067
1068
Guido van Rossume8811f81997-02-14 15:48:05 +00001069static PyObject *
1070builtin_intern(self, args)
1071 PyObject *self;
1072 PyObject *args;
1073{
1074 PyObject *s;
1075 if (!PyArg_ParseTuple(args, "S", &s))
1076 return NULL;
1077 Py_INCREF(s);
1078 PyString_InternInPlace(&s);
1079 return s;
1080}
1081
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001082static char intern_doc[] =
1083"intern(string) -> string\n\
1084\n\
1085``Intern'' the given string. This enters the string in the (global)\n\
1086table of interned strings whose purpose is to speed up dictionary lookups.\n\
1087Return the string itself or the previously interned string object with the\n\
1088same value.";
1089
1090
Guido van Rossum79f25d91997-04-29 20:08:16 +00001091static PyObject *
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001092builtin_int(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001093 PyObject *self;
1094 PyObject *args;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001095{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001096 PyObject *v;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001097
Guido van Rossum79f25d91997-04-29 20:08:16 +00001098 if (!PyArg_ParseTuple(args, "O:int", &v))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001099 return NULL;
Guido van Rossum09df08a1998-05-22 00:51:39 +00001100 return PyNumber_Int(v);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001101}
1102
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001103static char int_doc[] =
1104"int(x) -> integer\n\
1105\n\
1106Convert a string or number to an integer, if possible.\n\
1107A floating point argument will be truncated towards zero.";
1108
1109
Guido van Rossum79f25d91997-04-29 20:08:16 +00001110static PyObject *
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001111builtin_len(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001112 PyObject *self;
1113 PyObject *args;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001114{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001115 PyObject *v;
Guido van Rossum8ea9f4d1998-06-29 22:26:50 +00001116 long res;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001117
Guido van Rossum79f25d91997-04-29 20:08:16 +00001118 if (!PyArg_ParseTuple(args, "O:len", &v))
Guido van Rossum3f5da241990-12-20 15:06:42 +00001119 return NULL;
Guido van Rossum8ea9f4d1998-06-29 22:26:50 +00001120 res = PyObject_Length(v);
1121 if (res < 0 && PyErr_Occurred())
1122 return NULL;
1123 return PyInt_FromLong(res);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001124}
1125
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001126static char len_doc[] =
1127"len(object) -> integer\n\
1128\n\
1129Return the number of items of a sequence or mapping.";
1130
1131
Guido van Rossum79f25d91997-04-29 20:08:16 +00001132static PyObject *
Guido van Rossumd1705771996-04-09 02:41:06 +00001133builtin_list(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001134 PyObject *self;
1135 PyObject *args;
Guido van Rossumd1705771996-04-09 02:41:06 +00001136{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001137 PyObject *v;
Guido van Rossumd1705771996-04-09 02:41:06 +00001138
Guido van Rossum79f25d91997-04-29 20:08:16 +00001139 if (!PyArg_ParseTuple(args, "O:list", &v))
Guido van Rossumd1705771996-04-09 02:41:06 +00001140 return NULL;
Guido van Rossum09df08a1998-05-22 00:51:39 +00001141 return PySequence_List(v);
Guido van Rossumd1705771996-04-09 02:41:06 +00001142}
1143
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001144static char list_doc[] =
1145"list(sequence) -> list\n\
1146\n\
1147Return a new list whose items are the same as those of the argument sequence.";
1148
Guido van Rossum8861b741996-07-30 16:49:37 +00001149
1150static PyObject *
1151builtin_slice(self, args)
1152 PyObject *self;
1153 PyObject *args;
1154{
Guido van Rossum09df08a1998-05-22 00:51:39 +00001155 PyObject *start, *stop, *step;
Guido van Rossum8861b741996-07-30 16:49:37 +00001156
Guido van Rossum09df08a1998-05-22 00:51:39 +00001157 start = stop = step = NULL;
Guido van Rossum8861b741996-07-30 16:49:37 +00001158
Guido van Rossum09df08a1998-05-22 00:51:39 +00001159 if (!PyArg_ParseTuple(args, "O|OO:slice", &start, &stop, &step))
1160 return NULL;
Guido van Rossum8861b741996-07-30 16:49:37 +00001161
Guido van Rossum09df08a1998-05-22 00:51:39 +00001162 /* This swapping of stop and start is to maintain similarity with
1163 range(). */
1164 if (stop == NULL) {
1165 stop = start;
1166 start = NULL;
1167 }
1168 return PySlice_New(start, stop, step);
Guido van Rossum8861b741996-07-30 16:49:37 +00001169}
1170
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001171static char slice_doc[] =
1172"slice([start,] step[, stop]) -> slice object\n\
1173\n\
1174Create a slice object. This is used for slicing by the Numeric extensions.";
1175
1176
Guido van Rossum79f25d91997-04-29 20:08:16 +00001177static PyObject *
Guido van Rossum872537c1995-07-07 22:43:42 +00001178builtin_locals(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001179 PyObject *self;
1180 PyObject *args;
Guido van Rossum872537c1995-07-07 22:43:42 +00001181{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001182 PyObject *d;
Guido van Rossum872537c1995-07-07 22:43:42 +00001183
Guido van Rossum79f25d91997-04-29 20:08:16 +00001184 if (!PyArg_ParseTuple(args, ""))
Guido van Rossum872537c1995-07-07 22:43:42 +00001185 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001186 d = PyEval_GetLocals();
1187 Py_INCREF(d);
Guido van Rossum872537c1995-07-07 22:43:42 +00001188 return d;
1189}
1190
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001191static char locals_doc[] =
1192"locals() -> dictionary\n\
1193\n\
1194Return the dictionary containing the current scope's local variables.";
1195
1196
Guido van Rossum79f25d91997-04-29 20:08:16 +00001197static PyObject *
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001198builtin_long(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001199 PyObject *self;
1200 PyObject *args;
Guido van Rossumd4905451991-05-05 20:00:36 +00001201{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001202 PyObject *v;
Guido van Rossum1899c2e1992-09-12 11:09:23 +00001203
Guido van Rossum79f25d91997-04-29 20:08:16 +00001204 if (!PyArg_ParseTuple(args, "O:long", &v))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001205 return NULL;
Guido van Rossum09df08a1998-05-22 00:51:39 +00001206 return PyNumber_Long(v);
Guido van Rossumd4905451991-05-05 20:00:36 +00001207}
1208
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001209static char long_doc[] =
1210"long(x) -> long integer\n\
1211\n\
1212Convert a string or number to a long integer, if possible.\n\
1213A floating point argument will be truncated towards zero.";
1214
1215
Guido van Rossum79f25d91997-04-29 20:08:16 +00001216static PyObject *
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001217min_max(args, sign)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001218 PyObject *args;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001219 int sign;
1220{
Guido van Rossum2d951851994-08-29 12:52:16 +00001221 int i;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001222 PyObject *v, *w, *x;
1223 PySequenceMethods *sq;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001224
Guido van Rossum79f25d91997-04-29 20:08:16 +00001225 if (PyTuple_Size(args) > 1)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001226 v = args;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001227 else if (!PyArg_ParseTuple(args, "O:min/max", &v))
Guido van Rossum3f5da241990-12-20 15:06:42 +00001228 return NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001229 sq = v->ob_type->tp_as_sequence;
Guido van Rossum09df08a1998-05-22 00:51:39 +00001230 if (sq == NULL || sq->sq_item == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001231 PyErr_SetString(PyExc_TypeError,
1232 "min() or max() of non-sequence");
Guido van Rossum3f5da241990-12-20 15:06:42 +00001233 return NULL;
1234 }
Guido van Rossum2d951851994-08-29 12:52:16 +00001235 w = NULL;
1236 for (i = 0; ; i++) {
Guido van Rossum3f5da241990-12-20 15:06:42 +00001237 x = (*sq->sq_item)(v, i); /* Implies INCREF */
Guido van Rossum2d951851994-08-29 12:52:16 +00001238 if (x == NULL) {
Barry Warsawcde8b1b1997-08-22 21:14:38 +00001239 if (PyErr_ExceptionMatches(PyExc_IndexError)) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001240 PyErr_Clear();
Guido van Rossum2d951851994-08-29 12:52:16 +00001241 break;
1242 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00001243 Py_XDECREF(w);
Guido van Rossum2d951851994-08-29 12:52:16 +00001244 return NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001245 }
Guido van Rossum2d951851994-08-29 12:52:16 +00001246 if (w == NULL)
1247 w = x;
1248 else {
Guido van Rossumc8b6df91997-05-23 00:06:51 +00001249 int c = PyObject_Compare(x, w);
1250 if (c && PyErr_Occurred()) {
1251 Py_DECREF(x);
1252 Py_XDECREF(w);
1253 return NULL;
1254 }
1255 if (c * sign > 0) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001256 Py_DECREF(w);
Guido van Rossum2d951851994-08-29 12:52:16 +00001257 w = x;
1258 }
1259 else
Guido van Rossum79f25d91997-04-29 20:08:16 +00001260 Py_DECREF(x);
Guido van Rossum2d951851994-08-29 12:52:16 +00001261 }
Guido van Rossum3f5da241990-12-20 15:06:42 +00001262 }
Guido van Rossum2d951851994-08-29 12:52:16 +00001263 if (w == NULL)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001264 PyErr_SetString(PyExc_ValueError,
1265 "min() or max() of empty sequence");
Guido van Rossum3f5da241990-12-20 15:06:42 +00001266 return w;
1267}
1268
Guido van Rossum79f25d91997-04-29 20:08:16 +00001269static PyObject *
Guido van Rossum3f5da241990-12-20 15:06:42 +00001270builtin_min(self, v)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001271 PyObject *self;
1272 PyObject *v;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001273{
1274 return min_max(v, -1);
1275}
1276
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001277static char min_doc[] =
1278"min(sequence) -> value\n\
1279min(a, b, c, ...) -> value\n\
1280\n\
1281With a single sequence argument, return its smallest item.\n\
1282With two or more arguments, return the smallest argument.";
1283
1284
Guido van Rossum79f25d91997-04-29 20:08:16 +00001285static PyObject *
Guido van Rossum3f5da241990-12-20 15:06:42 +00001286builtin_max(self, v)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001287 PyObject *self;
1288 PyObject *v;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001289{
1290 return min_max(v, 1);
1291}
1292
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001293static char max_doc[] =
1294"max(sequence) -> value\n\
1295max(a, b, c, ...) -> value\n\
1296\n\
1297With a single sequence argument, return its largest item.\n\
1298With two or more arguments, return the largest argument.";
1299
1300
Guido van Rossum79f25d91997-04-29 20:08:16 +00001301static PyObject *
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001302builtin_oct(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001303 PyObject *self;
1304 PyObject *args;
Guido van Rossum006bcd41991-10-24 14:54:44 +00001305{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001306 PyObject *v;
1307 PyNumberMethods *nb;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001308
Guido van Rossum79f25d91997-04-29 20:08:16 +00001309 if (!PyArg_ParseTuple(args, "O:oct", &v))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001310 return NULL;
Guido van Rossum1899c2e1992-09-12 11:09:23 +00001311 if (v == NULL || (nb = v->ob_type->tp_as_number) == NULL ||
1312 nb->nb_oct == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001313 PyErr_SetString(PyExc_TypeError,
Guido van Rossum1899c2e1992-09-12 11:09:23 +00001314 "oct() argument can't be converted to oct");
1315 return NULL;
Guido van Rossum006bcd41991-10-24 14:54:44 +00001316 }
Guido van Rossum1899c2e1992-09-12 11:09:23 +00001317 return (*nb->nb_oct)(v);
Guido van Rossum006bcd41991-10-24 14:54:44 +00001318}
1319
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001320static char oct_doc[] =
1321"oct(number) -> string\n\
1322\n\
1323Return the octal representation of an integer or long integer.";
1324
1325
Guido van Rossum79f25d91997-04-29 20:08:16 +00001326static PyObject *
Guido van Rossum94390a41992-08-14 15:14:30 +00001327builtin_open(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001328 PyObject *self;
1329 PyObject *args;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001330{
Guido van Rossum2d951851994-08-29 12:52:16 +00001331 char *name;
1332 char *mode = "r";
1333 int bufsize = -1;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001334 PyObject *f;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001335
Guido van Rossum79f25d91997-04-29 20:08:16 +00001336 if (!PyArg_ParseTuple(args, "s|si:open", &name, &mode, &bufsize))
Guido van Rossum3f5da241990-12-20 15:06:42 +00001337 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001338 f = PyFile_FromString(name, mode);
Guido van Rossum2d951851994-08-29 12:52:16 +00001339 if (f != NULL)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001340 PyFile_SetBufSize(f, bufsize);
Guido van Rossum2d951851994-08-29 12:52:16 +00001341 return f;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001342}
1343
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001344static char open_doc[] =
1345"open(filename[, mode[, buffering]]) -> file object\n\
1346\n\
1347Open a file. The mode can be 'r', 'w' or 'a' for reading (default),\n\
1348writing or appending. The file will be created if it doesn't exist\n\
1349when opened for writing or appending; it will be truncated when\n\
1350opened for writing. Add a 'b' to the mode for binary files.\n\
1351Add a '+' to the mode to allow simultaneous reading and writing.\n\
1352If the buffering argument is given, 0 means unbuffered, 1 means line\n\
1353buffered, and larger numbers specify the buffer size.";
1354
1355
Guido van Rossum79f25d91997-04-29 20:08:16 +00001356static PyObject *
Guido van Rossum94390a41992-08-14 15:14:30 +00001357builtin_ord(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001358 PyObject *self;
1359 PyObject *args;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001360{
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001361 char c;
1362
Guido van Rossum79f25d91997-04-29 20:08:16 +00001363 if (!PyArg_ParseTuple(args, "c:ord", &c))
Guido van Rossum3f5da241990-12-20 15:06:42 +00001364 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001365 return PyInt_FromLong((long)(c & 0xff));
Guido van Rossum3f5da241990-12-20 15:06:42 +00001366}
1367
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001368static char ord_doc[] =
1369"ord(c) -> integer\n\
1370\n\
1371Return the integer ordinal of a one character string.";
1372
1373
Guido van Rossum79f25d91997-04-29 20:08:16 +00001374static PyObject *
Guido van Rossum6a00cd81995-01-07 12:39:01 +00001375builtin_pow(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001376 PyObject *self;
1377 PyObject *args;
Guido van Rossum6a00cd81995-01-07 12:39:01 +00001378{
Guido van Rossum09df08a1998-05-22 00:51:39 +00001379 PyObject *v, *w, *z = Py_None;
Guido van Rossum6a00cd81995-01-07 12:39:01 +00001380
Guido van Rossum79f25d91997-04-29 20:08:16 +00001381 if (!PyArg_ParseTuple(args, "OO|O:pow", &v, &w, &z))
Guido van Rossum6a00cd81995-01-07 12:39:01 +00001382 return NULL;
Guido van Rossum09df08a1998-05-22 00:51:39 +00001383 return PyNumber_Power(v, w, z);
Guido van Rossumd4905451991-05-05 20:00:36 +00001384}
1385
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001386static char pow_doc[] =
1387"pow(x, y[, z]) -> number\n\
1388\n\
1389With two arguments, equivalent to x**y. With three arguments,\n\
1390equivalent to (x**y) % z, but may be more efficient (e.g. for longs).";
1391
1392
Guido van Rossum79f25d91997-04-29 20:08:16 +00001393static PyObject *
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001394builtin_range(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001395 PyObject *self;
1396 PyObject *args;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001397{
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001398 long ilow = 0, ihigh = 0, istep = 1;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001399 int i, n;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001400 PyObject *v;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001401
Guido van Rossum79f25d91997-04-29 20:08:16 +00001402 if (PyTuple_Size(args) <= 1) {
1403 if (!PyArg_ParseTuple(args,
Guido van Rossum0865dd91995-01-17 16:30:22 +00001404 "l;range() requires 1-3 int arguments",
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001405 &ihigh))
1406 return NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001407 }
1408 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001409 if (!PyArg_ParseTuple(args,
Guido van Rossum0865dd91995-01-17 16:30:22 +00001410 "ll|l;range() requires 1-3 int arguments",
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001411 &ilow, &ihigh, &istep))
Guido van Rossum3f5da241990-12-20 15:06:42 +00001412 return NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001413 }
1414 if (istep == 0) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001415 PyErr_SetString(PyExc_ValueError, "zero step for range()");
Guido van Rossum3f5da241990-12-20 15:06:42 +00001416 return NULL;
1417 }
1418 /* XXX ought to check overflow of subtraction */
1419 if (istep > 0)
1420 n = (ihigh - ilow + istep - 1) / istep;
1421 else
1422 n = (ihigh - ilow + istep + 1) / istep;
1423 if (n < 0)
1424 n = 0;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001425 v = PyList_New(n);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001426 if (v == NULL)
1427 return NULL;
1428 for (i = 0; i < n; i++) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001429 PyObject *w = PyInt_FromLong(ilow);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001430 if (w == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001431 Py_DECREF(v);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001432 return NULL;
1433 }
Guido van Rossuma937d141998-04-24 18:22:02 +00001434 PyList_SET_ITEM(v, i, w);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001435 ilow += istep;
1436 }
1437 return v;
1438}
1439
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001440static char range_doc[] =
1441"range([start,] stop[, step]) -> list of integers\n\
1442\n\
1443Return a list containing an arithmetic progression of integers.\n\
1444range(i, j) returns [i, i+1, i+2, ..., j-1]; start (!) defaults to 0.\n\
1445When step is given, it specifies the increment (or decrement).\n\
1446For example, range(4) returns [0, 1, 2, 3]. The end point is omitted!\n\
1447These are exactly the valid indices for a list of 4 elements.";
1448
1449
Guido van Rossum79f25d91997-04-29 20:08:16 +00001450static PyObject *
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001451builtin_xrange(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001452 PyObject *self;
1453 PyObject *args;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001454{
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001455 long ilow = 0, ihigh = 0, istep = 1;
Guido van Rossum0865dd91995-01-17 16:30:22 +00001456 long n;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001457
Guido van Rossum79f25d91997-04-29 20:08:16 +00001458 if (PyTuple_Size(args) <= 1) {
1459 if (!PyArg_ParseTuple(args,
Guido van Rossum0865dd91995-01-17 16:30:22 +00001460 "l;xrange() requires 1-3 int arguments",
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001461 &ihigh))
1462 return NULL;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001463 }
1464 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001465 if (!PyArg_ParseTuple(args,
Guido van Rossum0865dd91995-01-17 16:30:22 +00001466 "ll|l;xrange() requires 1-3 int arguments",
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001467 &ilow, &ihigh, &istep))
Guido van Rossum12d12c51993-10-26 17:58:25 +00001468 return NULL;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001469 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001470 if (istep == 0) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001471 PyErr_SetString(PyExc_ValueError, "zero step for xrange()");
Guido van Rossum12d12c51993-10-26 17:58:25 +00001472 return NULL;
1473 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001474 /* XXX ought to check overflow of subtraction */
1475 if (istep > 0)
1476 n = (ihigh - ilow + istep - 1) / istep;
1477 else
1478 n = (ihigh - ilow + istep + 1) / istep;
1479 if (n < 0)
1480 n = 0;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001481 return PyRange_New(ilow, n, istep, 1);
Guido van Rossum12d12c51993-10-26 17:58:25 +00001482}
1483
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001484static char xrange_doc[] =
1485"xrange([start,] stop[, step]) -> xrange object\n\
1486\n\
1487Like range(), but instead of returning a list, returns an object that\n\
1488generates the numbers in the range on demand. This is slightly slower\n\
1489than range() but more memory efficient.";
1490
1491
Guido van Rossum79f25d91997-04-29 20:08:16 +00001492static PyObject *
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001493builtin_raw_input(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001494 PyObject *self;
1495 PyObject *args;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001496{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001497 PyObject *v = NULL;
1498 PyObject *f;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001499
Guido van Rossum79f25d91997-04-29 20:08:16 +00001500 if (!PyArg_ParseTuple(args, "|O:[raw_]input", &v))
Guido van Rossum3165fe61992-09-25 21:59:05 +00001501 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001502 if (PyFile_AsFile(PySys_GetObject("stdin")) == stdin &&
1503 PyFile_AsFile(PySys_GetObject("stdout")) == stdout &&
Guido van Rossum53bb7ff1995-07-26 16:26:31 +00001504 isatty(fileno(stdin)) && isatty(fileno(stdout))) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001505 PyObject *po;
Guido van Rossum872537c1995-07-07 22:43:42 +00001506 char *prompt;
1507 char *s;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001508 PyObject *result;
Guido van Rossum872537c1995-07-07 22:43:42 +00001509 if (v != NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001510 po = PyObject_Str(v);
Guido van Rossum872537c1995-07-07 22:43:42 +00001511 if (po == NULL)
1512 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001513 prompt = PyString_AsString(po);
Guido van Rossumd9b52081998-06-26 18:25:38 +00001514 if (prompt == NULL)
1515 return NULL;
Guido van Rossum872537c1995-07-07 22:43:42 +00001516 }
1517 else {
1518 po = NULL;
1519 prompt = "";
1520 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00001521 s = PyOS_Readline(prompt);
1522 Py_XDECREF(po);
Guido van Rossum872537c1995-07-07 22:43:42 +00001523 if (s == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001524 PyErr_SetNone(PyExc_KeyboardInterrupt);
Guido van Rossum872537c1995-07-07 22:43:42 +00001525 return NULL;
1526 }
1527 if (*s == '\0') {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001528 PyErr_SetNone(PyExc_EOFError);
Guido van Rossum872537c1995-07-07 22:43:42 +00001529 result = NULL;
1530 }
1531 else { /* strip trailing '\n' */
Guido van Rossum79f25d91997-04-29 20:08:16 +00001532 result = PyString_FromStringAndSize(s, strlen(s)-1);
Guido van Rossum872537c1995-07-07 22:43:42 +00001533 }
1534 free(s);
1535 return result;
1536 }
Guido van Rossum90933611991-06-07 16:10:43 +00001537 if (v != NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001538 f = PySys_GetObject("stdout");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001539 if (f == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001540 PyErr_SetString(PyExc_RuntimeError, "lost sys.stdout");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001541 return NULL;
1542 }
Guido van Rossumc8b6df91997-05-23 00:06:51 +00001543 if (Py_FlushLine() != 0 ||
1544 PyFile_WriteObject(v, f, Py_PRINT_RAW) != 0)
Guido van Rossum90933611991-06-07 16:10:43 +00001545 return NULL;
1546 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00001547 f = PySys_GetObject("stdin");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001548 if (f == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001549 PyErr_SetString(PyExc_RuntimeError, "lost sys.stdin");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001550 return NULL;
1551 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00001552 return PyFile_GetLine(f, -1);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001553}
1554
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001555static char raw_input_doc[] =
1556"raw_input([prompt]) -> string\n\
1557\n\
1558Read a string from standard input. The trailing newline is stripped.\n\
1559If the user hits EOF (Unix: Ctl-D, Windows: Ctl-Z+Return), raise EOFError.\n\
1560On Unix, GNU readline is used if enabled. The prompt string, if given,\n\
1561is printed without a trailing newline before reading.";
1562
1563
Guido van Rossum79f25d91997-04-29 20:08:16 +00001564static PyObject *
Guido van Rossum12d12c51993-10-26 17:58:25 +00001565builtin_reduce(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001566 PyObject *self;
1567 PyObject *args;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001568{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001569 PyObject *seq, *func, *result = NULL;
1570 PySequenceMethods *sqf;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001571 register int i;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001572
Guido van Rossum79f25d91997-04-29 20:08:16 +00001573 if (!PyArg_ParseTuple(args, "OO|O:reduce", &func, &seq, &result))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001574 return NULL;
1575 if (result != NULL)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001576 Py_INCREF(result);
Guido van Rossum12d12c51993-10-26 17:58:25 +00001577
Guido van Rossum09df08a1998-05-22 00:51:39 +00001578 sqf = seq->ob_type->tp_as_sequence;
1579 if (sqf == NULL || sqf->sq_item == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001580 PyErr_SetString(PyExc_TypeError,
Guido van Rossum12d12c51993-10-26 17:58:25 +00001581 "2nd argument to reduce() must be a sequence object");
1582 return NULL;
1583 }
1584
Guido van Rossum79f25d91997-04-29 20:08:16 +00001585 if ((args = PyTuple_New(2)) == NULL)
Guido van Rossum2d951851994-08-29 12:52:16 +00001586 goto Fail;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001587
Guido van Rossum2d951851994-08-29 12:52:16 +00001588 for (i = 0; ; ++i) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001589 PyObject *op2;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001590
1591 if (args->ob_refcnt > 1) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001592 Py_DECREF(args);
1593 if ((args = PyTuple_New(2)) == NULL)
Guido van Rossum2d951851994-08-29 12:52:16 +00001594 goto Fail;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001595 }
1596
Guido van Rossum2d951851994-08-29 12:52:16 +00001597 if ((op2 = (*sqf->sq_item)(seq, i)) == NULL) {
Barry Warsawcde8b1b1997-08-22 21:14:38 +00001598 if (PyErr_ExceptionMatches(PyExc_IndexError)) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001599 PyErr_Clear();
Guido van Rossum2d951851994-08-29 12:52:16 +00001600 break;
1601 }
1602 goto Fail;
1603 }
Guido van Rossum12d12c51993-10-26 17:58:25 +00001604
Guido van Rossum2d951851994-08-29 12:52:16 +00001605 if (result == NULL)
1606 result = op2;
1607 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001608 PyTuple_SetItem(args, 0, result);
1609 PyTuple_SetItem(args, 1, op2);
1610 if ((result = PyEval_CallObject(func, args)) == NULL)
Guido van Rossum2d951851994-08-29 12:52:16 +00001611 goto Fail;
1612 }
Guido van Rossum12d12c51993-10-26 17:58:25 +00001613 }
1614
Guido van Rossum79f25d91997-04-29 20:08:16 +00001615 Py_DECREF(args);
Guido van Rossum12d12c51993-10-26 17:58:25 +00001616
Guido van Rossum2d951851994-08-29 12:52:16 +00001617 if (result == NULL)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001618 PyErr_SetString(PyExc_TypeError,
Guido van Rossum2d951851994-08-29 12:52:16 +00001619 "reduce of empty sequence with no initial value");
1620
Guido van Rossum12d12c51993-10-26 17:58:25 +00001621 return result;
1622
Guido van Rossum2d951851994-08-29 12:52:16 +00001623Fail:
Guido van Rossum79f25d91997-04-29 20:08:16 +00001624 Py_XDECREF(args);
1625 Py_XDECREF(result);
Guido van Rossum12d12c51993-10-26 17:58:25 +00001626 return NULL;
1627}
1628
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001629static char reduce_doc[] =
1630"reduce(function, sequence[, initial]) -> value\n\
1631\n\
1632Apply a function of two arguments cumulatively to the items of a sequence,\n\
1633from left to right, so as to reduce the sequence to a single value.\n\
1634For example, reduce(lambda x, y: x+y, [1, 2, 3, 4, 5]) calculates\n\
1635((((1+2)+3)+4)+5). If initial is present, it is placed before the items\n\
1636of the sequence in the calculation, and serves as a default when the\n\
1637sequence is empty.";
1638
1639
Guido van Rossum79f25d91997-04-29 20:08:16 +00001640static PyObject *
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001641builtin_reload(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001642 PyObject *self;
1643 PyObject *args;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001644{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001645 PyObject *v;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001646
Guido van Rossum79f25d91997-04-29 20:08:16 +00001647 if (!PyArg_ParseTuple(args, "O:reload", &v))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001648 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001649 return PyImport_ReloadModule(v);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001650}
1651
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001652static char reload_doc[] =
1653"reload(module) -> module\n\
1654\n\
1655Reload the module. The module must have been successfully imported before.";
1656
1657
Guido van Rossum79f25d91997-04-29 20:08:16 +00001658static PyObject *
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001659builtin_repr(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001660 PyObject *self;
1661 PyObject *args;
Guido van Rossumc89705d1992-11-26 08:54:07 +00001662{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001663 PyObject *v;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001664
Guido van Rossum79f25d91997-04-29 20:08:16 +00001665 if (!PyArg_ParseTuple(args, "O:repr", &v))
Guido van Rossumc89705d1992-11-26 08:54:07 +00001666 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001667 return PyObject_Repr(v);
Guido van Rossumc89705d1992-11-26 08:54:07 +00001668}
1669
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001670static char repr_doc[] =
1671"repr(object) -> string\n\
1672\n\
1673Return the canonical string representation of the object.\n\
1674For most object types, eval(repr(object)) == object.";
1675
1676
Guido van Rossum79f25d91997-04-29 20:08:16 +00001677static PyObject *
Guido van Rossum9e51f9b1993-02-12 16:29:05 +00001678builtin_round(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001679 PyObject *self;
1680 PyObject *args;
Guido van Rossum9e51f9b1993-02-12 16:29:05 +00001681{
Guido van Rossum9e51f9b1993-02-12 16:29:05 +00001682 double x;
1683 double f;
1684 int ndigits = 0;
Guido van Rossum9e51f9b1993-02-12 16:29:05 +00001685 int i;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001686
Guido van Rossum79f25d91997-04-29 20:08:16 +00001687 if (!PyArg_ParseTuple(args, "d|i:round", &x, &ndigits))
Guido van Rossum9e51f9b1993-02-12 16:29:05 +00001688 return NULL;
Guido van Rossum9e51f9b1993-02-12 16:29:05 +00001689 f = 1.0;
Guido van Rossum1e162d31998-05-09 14:42:25 +00001690 i = abs(ndigits);
1691 while (--i >= 0)
Guido van Rossum9e51f9b1993-02-12 16:29:05 +00001692 f = f*10.0;
Guido van Rossum1e162d31998-05-09 14:42:25 +00001693 if (ndigits < 0)
1694 x /= f;
Guido van Rossum9e51f9b1993-02-12 16:29:05 +00001695 else
Guido van Rossum1e162d31998-05-09 14:42:25 +00001696 x *= f;
1697 if (x >= 0.0)
1698 x = floor(x + 0.5);
1699 else
1700 x = ceil(x - 0.5);
1701 if (ndigits < 0)
1702 x *= f;
1703 else
1704 x /= f;
1705 return PyFloat_FromDouble(x);
Guido van Rossum9e51f9b1993-02-12 16:29:05 +00001706}
1707
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001708static char round_doc[] =
1709"round(number[, ndigits]) -> floating point number\n\
1710\n\
1711Round a number to a given precision in decimal digits (default 0 digits).\n\
1712This always returns a floating point number. Precision may be negative.";
1713
1714
Guido van Rossum79f25d91997-04-29 20:08:16 +00001715static PyObject *
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001716builtin_str(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001717 PyObject *self;
1718 PyObject *args;
Guido van Rossumc89705d1992-11-26 08:54:07 +00001719{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001720 PyObject *v;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001721
Guido van Rossum79f25d91997-04-29 20:08:16 +00001722 if (!PyArg_ParseTuple(args, "O:str", &v))
Guido van Rossumc89705d1992-11-26 08:54:07 +00001723 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001724 return PyObject_Str(v);
Guido van Rossumc89705d1992-11-26 08:54:07 +00001725}
1726
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001727static char str_doc[] =
1728"str(object) -> string\n\
1729\n\
1730Return a nice string representation of the object.\n\
1731If the argument is a string, the return value is the same object.";
1732
1733
Guido van Rossum79f25d91997-04-29 20:08:16 +00001734static PyObject *
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001735builtin_tuple(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001736 PyObject *self;
1737 PyObject *args;
Guido van Rossumcae027b1994-08-29 12:53:11 +00001738{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001739 PyObject *v;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001740
Guido van Rossum79f25d91997-04-29 20:08:16 +00001741 if (!PyArg_ParseTuple(args, "O:tuple", &v))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001742 return NULL;
Guido van Rossum09df08a1998-05-22 00:51:39 +00001743 return PySequence_Tuple(v);
Guido van Rossumcae027b1994-08-29 12:53:11 +00001744}
1745
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001746static char tuple_doc[] =
1747"tuple(sequence) -> list\n\
1748\n\
1749Return a tuple whose items are the same as those of the argument sequence.\n\
1750If the argument is a tuple, the return value is the same object.";
1751
1752
Guido van Rossum79f25d91997-04-29 20:08:16 +00001753static PyObject *
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001754builtin_type(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001755 PyObject *self;
1756 PyObject *args;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001757{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001758 PyObject *v;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001759
Guido van Rossum79f25d91997-04-29 20:08:16 +00001760 if (!PyArg_ParseTuple(args, "O:type", &v))
Guido van Rossum3f5da241990-12-20 15:06:42 +00001761 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001762 v = (PyObject *)v->ob_type;
1763 Py_INCREF(v);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001764 return v;
1765}
1766
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001767static char type_doc[] =
1768"type(object) -> type object\n\
1769\n\
1770Return the type of the object.";
1771
1772
Guido van Rossum79f25d91997-04-29 20:08:16 +00001773static PyObject *
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001774builtin_vars(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001775 PyObject *self;
1776 PyObject *args;
Guido van Rossum2d951851994-08-29 12:52:16 +00001777{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001778 PyObject *v = NULL;
1779 PyObject *d;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001780
Guido van Rossum79f25d91997-04-29 20:08:16 +00001781 if (!PyArg_ParseTuple(args, "|O:vars", &v))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001782 return NULL;
Guido van Rossum2d951851994-08-29 12:52:16 +00001783 if (v == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001784 d = PyEval_GetLocals();
Guido van Rossum53bb7ff1995-07-26 16:26:31 +00001785 if (d == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001786 if (!PyErr_Occurred())
1787 PyErr_SetString(PyExc_SystemError,
1788 "no locals!?");
Guido van Rossum53bb7ff1995-07-26 16:26:31 +00001789 }
1790 else
Guido van Rossum79f25d91997-04-29 20:08:16 +00001791 Py_INCREF(d);
Guido van Rossum2d951851994-08-29 12:52:16 +00001792 }
1793 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001794 d = PyObject_GetAttrString(v, "__dict__");
Guido van Rossum2d951851994-08-29 12:52:16 +00001795 if (d == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001796 PyErr_SetString(PyExc_TypeError,
Guido van Rossum2d951851994-08-29 12:52:16 +00001797 "vars() argument must have __dict__ attribute");
1798 return NULL;
1799 }
1800 }
1801 return d;
1802}
1803
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001804static char vars_doc[] =
1805"vars([object]) -> dictionary\n\
1806\n\
1807Without arguments, equivalent to locals().\n\
1808With an argument, equivalent to object.__dict__.";
1809
1810
Barry Warsawcde8b1b1997-08-22 21:14:38 +00001811static PyObject *
1812builtin_isinstance(self, args)
1813 PyObject *self;
1814 PyObject *args;
1815{
1816 PyObject *inst;
1817 PyObject *cls;
1818 int retval;
1819
1820 if (!PyArg_ParseTuple(args, "OO", &inst, &cls))
1821 return NULL;
Guido van Rossumf5dd9141997-12-02 19:11:45 +00001822 if (PyType_Check(cls)) {
Guido van Rossumd6af46d1997-12-10 05:51:47 +00001823 retval = ((PyObject *)(inst->ob_type) == cls);
Barry Warsawcde8b1b1997-08-22 21:14:38 +00001824 }
Barry Warsawcde8b1b1997-08-22 21:14:38 +00001825 else {
Guido van Rossumf5dd9141997-12-02 19:11:45 +00001826 if (!PyClass_Check(cls)) {
1827 PyErr_SetString(PyExc_TypeError,
1828 "second argument must be a class");
1829 return NULL;
1830 }
1831
1832 if (!PyInstance_Check(inst))
1833 retval = 0;
1834 else {
1835 PyObject *inclass =
1836 (PyObject*)((PyInstanceObject*)inst)->in_class;
1837 retval = PyClass_IsSubclass(inclass, cls);
1838 }
Barry Warsawcde8b1b1997-08-22 21:14:38 +00001839 }
1840 return PyInt_FromLong(retval);
1841}
1842
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001843static char isinstance_doc[] =
1844"isinstance(object, class-or-type) -> Boolean\n\
1845\n\
1846Return whether an object is an instance of a class or of a subclass thereof.\n\
1847With a type as second argument, return whether that is the object's type.";
1848
Barry Warsawcde8b1b1997-08-22 21:14:38 +00001849
1850static PyObject *
1851builtin_issubclass(self, args)
1852 PyObject *self;
1853 PyObject *args;
1854{
1855 PyObject *derived;
1856 PyObject *cls;
1857 int retval;
1858
1859 if (!PyArg_ParseTuple(args, "OO", &derived, &cls))
1860 return NULL;
1861 if (!PyClass_Check(derived) || !PyClass_Check(cls)) {
1862 PyErr_SetString(PyExc_TypeError, "arguments must be classes");
1863 return NULL;
1864 }
1865 /* shortcut */
1866 if (!(retval = (derived == cls)))
1867 retval = PyClass_IsSubclass(derived, cls);
1868
1869 return PyInt_FromLong(retval);
1870}
1871
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001872static char issubclass_doc[] =
1873"issubclass(C, B) -> Boolean\n\
1874\n\
1875Return whether class C is a subclass (i.e., a derived class) of class B.";
1876
Barry Warsawcde8b1b1997-08-22 21:14:38 +00001877
Guido van Rossum79f25d91997-04-29 20:08:16 +00001878static PyMethodDef builtin_methods[] = {
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001879 {"__import__", builtin___import__, 1, import_doc},
1880 {"abs", builtin_abs, 1, abs_doc},
1881 {"apply", builtin_apply, 1, apply_doc},
1882 {"callable", builtin_callable, 1, callable_doc},
1883 {"chr", builtin_chr, 1, chr_doc},
1884 {"cmp", builtin_cmp, 1, cmp_doc},
1885 {"coerce", builtin_coerce, 1, coerce_doc},
1886 {"compile", builtin_compile, 1, compile_doc},
Guido van Rossum8a5c5d21996-01-12 01:09:56 +00001887#ifndef WITHOUT_COMPLEX
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001888 {"complex", builtin_complex, 1, complex_doc},
Guido van Rossum8a5c5d21996-01-12 01:09:56 +00001889#endif
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001890 {"delattr", builtin_delattr, 1, delattr_doc},
1891 {"dir", builtin_dir, 1, dir_doc},
1892 {"divmod", builtin_divmod, 1, divmod_doc},
1893 {"eval", builtin_eval, 1, eval_doc},
1894 {"execfile", builtin_execfile, 1, execfile_doc},
1895 {"filter", builtin_filter, 1, filter_doc},
1896 {"float", builtin_float, 1, float_doc},
1897 {"getattr", builtin_getattr, 1, getattr_doc},
1898 {"globals", builtin_globals, 1, globals_doc},
1899 {"hasattr", builtin_hasattr, 1, hasattr_doc},
1900 {"hash", builtin_hash, 1, hash_doc},
1901 {"hex", builtin_hex, 1, hex_doc},
1902 {"id", builtin_id, 1, id_doc},
1903 {"input", builtin_input, 1, input_doc},
1904 {"intern", builtin_intern, 1, intern_doc},
1905 {"int", builtin_int, 1, int_doc},
1906 {"isinstance", builtin_isinstance, 1, isinstance_doc},
1907 {"issubclass", builtin_issubclass, 1, issubclass_doc},
1908 {"len", builtin_len, 1, len_doc},
1909 {"list", builtin_list, 1, list_doc},
1910 {"locals", builtin_locals, 1, locals_doc},
1911 {"long", builtin_long, 1, long_doc},
1912 {"map", builtin_map, 1, map_doc},
1913 {"max", builtin_max, 1, max_doc},
1914 {"min", builtin_min, 1, min_doc},
1915 {"oct", builtin_oct, 1, oct_doc},
1916 {"open", builtin_open, 1, open_doc},
1917 {"ord", builtin_ord, 1, ord_doc},
1918 {"pow", builtin_pow, 1, pow_doc},
1919 {"range", builtin_range, 1, range_doc},
1920 {"raw_input", builtin_raw_input, 1, raw_input_doc},
1921 {"reduce", builtin_reduce, 1, reduce_doc},
1922 {"reload", builtin_reload, 1, reload_doc},
1923 {"repr", builtin_repr, 1, repr_doc},
1924 {"round", builtin_round, 1, round_doc},
1925 {"setattr", builtin_setattr, 1, setattr_doc},
1926 {"slice", builtin_slice, 1, slice_doc},
1927 {"str", builtin_str, 1, str_doc},
1928 {"tuple", builtin_tuple, 1, tuple_doc},
1929 {"type", builtin_type, 1, type_doc},
1930 {"vars", builtin_vars, 1, vars_doc},
1931 {"xrange", builtin_xrange, 1, xrange_doc},
Guido van Rossumc02e15c1991-12-16 13:03:00 +00001932 {NULL, NULL},
Guido van Rossum3f5da241990-12-20 15:06:42 +00001933};
1934
Guido van Rossum3f5da241990-12-20 15:06:42 +00001935/* Predefined exceptions */
1936
Guido van Rossum04748321997-09-16 18:43:15 +00001937PyObject *PyExc_Exception;
Barry Warsaw757af0e1997-08-29 22:13:51 +00001938PyObject *PyExc_StandardError;
Barry Warsaw412cdc21997-09-16 21:51:14 +00001939PyObject *PyExc_ArithmeticError;
Barry Warsaw757af0e1997-08-29 22:13:51 +00001940PyObject *PyExc_LookupError;
1941
Guido van Rossum79f25d91997-04-29 20:08:16 +00001942PyObject *PyExc_AssertionError;
1943PyObject *PyExc_AttributeError;
1944PyObject *PyExc_EOFError;
Guido van Rossumb6a7f771997-05-09 03:03:23 +00001945PyObject *PyExc_FloatingPointError;
Barry Warsawd086a1a1998-07-23 15:59:57 +00001946PyObject *PyExc_EnvironmentError;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001947PyObject *PyExc_IOError;
Barry Warsawd086a1a1998-07-23 15:59:57 +00001948PyObject *PyExc_OSError;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001949PyObject *PyExc_ImportError;
1950PyObject *PyExc_IndexError;
1951PyObject *PyExc_KeyError;
1952PyObject *PyExc_KeyboardInterrupt;
1953PyObject *PyExc_MemoryError;
1954PyObject *PyExc_NameError;
1955PyObject *PyExc_OverflowError;
1956PyObject *PyExc_RuntimeError;
Barry Warsaw344864f1998-12-01 18:52:06 +00001957PyObject *PyExc_NotImplementedError;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001958PyObject *PyExc_SyntaxError;
1959PyObject *PyExc_SystemError;
1960PyObject *PyExc_SystemExit;
1961PyObject *PyExc_TypeError;
1962PyObject *PyExc_ValueError;
1963PyObject *PyExc_ZeroDivisionError;
Guido van Rossum50afb7a1991-12-10 13:52:31 +00001964
Barry Warsaw757af0e1997-08-29 22:13:51 +00001965PyObject *PyExc_MemoryErrorInst;
1966
1967static struct
1968{
1969 char* name;
1970 PyObject** exc;
1971 int leaf_exc;
1972}
1973bltin_exc[] = {
Guido van Rossum04748321997-09-16 18:43:15 +00001974 {"Exception", &PyExc_Exception, 0},
Barry Warsaw757af0e1997-08-29 22:13:51 +00001975 {"StandardError", &PyExc_StandardError, 0},
Barry Warsaw412cdc21997-09-16 21:51:14 +00001976 {"ArithmeticError", &PyExc_ArithmeticError, 0},
Barry Warsaw757af0e1997-08-29 22:13:51 +00001977 {"LookupError", &PyExc_LookupError, 0},
1978 {"AssertionError", &PyExc_AssertionError, 1},
1979 {"AttributeError", &PyExc_AttributeError, 1},
1980 {"EOFError", &PyExc_EOFError, 1},
1981 {"FloatingPointError", &PyExc_FloatingPointError, 1},
Barry Warsawd086a1a1998-07-23 15:59:57 +00001982 {"EnvironmentError", &PyExc_EnvironmentError, 1},
Barry Warsaw757af0e1997-08-29 22:13:51 +00001983 {"IOError", &PyExc_IOError, 1},
Barry Warsawd086a1a1998-07-23 15:59:57 +00001984 {"OSError", &PyExc_OSError, 1},
Barry Warsaw757af0e1997-08-29 22:13:51 +00001985 {"ImportError", &PyExc_ImportError, 1},
1986 {"IndexError", &PyExc_IndexError, 1},
1987 {"KeyError", &PyExc_KeyError, 1},
1988 {"KeyboardInterrupt", &PyExc_KeyboardInterrupt, 1},
1989 {"MemoryError", &PyExc_MemoryError, 1},
1990 {"NameError", &PyExc_NameError, 1},
1991 {"OverflowError", &PyExc_OverflowError, 1},
1992 {"RuntimeError", &PyExc_RuntimeError, 1},
Barry Warsaw344864f1998-12-01 18:52:06 +00001993 {"NotImplementedError",&PyExc_NotImplementedError,1},
Barry Warsaw757af0e1997-08-29 22:13:51 +00001994 {"SyntaxError", &PyExc_SyntaxError, 1},
1995 {"SystemError", &PyExc_SystemError, 1},
1996 {"SystemExit", &PyExc_SystemExit, 1},
1997 {"TypeError", &PyExc_TypeError, 1},
1998 {"ValueError", &PyExc_ValueError, 1},
1999 {"ZeroDivisionError", &PyExc_ZeroDivisionError, 1},
2000 {NULL, NULL}
2001};
2002
2003
Barry Warsaw98b62461998-09-14 18:51:11 +00002004/* import exceptions module to extract class exceptions. on success,
2005 * return 1. on failure return 0 which signals _PyBuiltin_Init_2 to fall
2006 * back to using old-style string based exceptions.
2007 */
2008static int
Barry Warsaw757af0e1997-08-29 22:13:51 +00002009init_class_exc(dict)
2010 PyObject *dict;
2011{
2012 int i;
2013 PyObject *m = PyImport_ImportModule("exceptions");
Barry Warsaw98b62461998-09-14 18:51:11 +00002014 PyObject *args = NULL;
2015 PyObject *d = NULL;
Barry Warsaw757af0e1997-08-29 22:13:51 +00002016
Barry Warsaw98b62461998-09-14 18:51:11 +00002017 /* make sure we got the module and its dictionary */
Barry Warsaw757af0e1997-08-29 22:13:51 +00002018 if (m == NULL ||
2019 (d = PyModule_GetDict(m)) == NULL)
2020 {
Barry Warsaw98b62461998-09-14 18:51:11 +00002021 PySys_WriteStderr("'import exceptions' failed; ");
Barry Warsaw757af0e1997-08-29 22:13:51 +00002022 if (Py_VerboseFlag) {
Barry Warsaw98b62461998-09-14 18:51:11 +00002023 PySys_WriteStderr("traceback:\n");
Barry Warsaw757af0e1997-08-29 22:13:51 +00002024 PyErr_Print();
2025 }
2026 else {
Barry Warsaw98b62461998-09-14 18:51:11 +00002027 PySys_WriteStderr("use -v for traceback\n");
Barry Warsaw757af0e1997-08-29 22:13:51 +00002028 }
Barry Warsaw98b62461998-09-14 18:51:11 +00002029 goto finally;
Barry Warsaw757af0e1997-08-29 22:13:51 +00002030 }
2031 for (i = 0; bltin_exc[i].name; i++) {
2032 /* dig the exception out of the module */
2033 PyObject *exc = PyDict_GetItemString(d, bltin_exc[i].name);
Barry Warsaw98b62461998-09-14 18:51:11 +00002034 if (!exc) {
2035 PySys_WriteStderr(
2036 "Built-in exception class not found: %s. Library mismatch?\n",
2037 bltin_exc[i].name);
2038 goto finally;
2039 }
2040 /* free the old-style exception string object */
Barry Warsaw757af0e1997-08-29 22:13:51 +00002041 Py_XDECREF(*bltin_exc[i].exc);
2042
2043 /* squirrel away a pointer to the exception */
2044 Py_INCREF(exc);
2045 *bltin_exc[i].exc = exc;
2046
2047 /* and insert the name in the __builtin__ module */
Barry Warsaw98b62461998-09-14 18:51:11 +00002048 if (PyDict_SetItemString(dict, bltin_exc[i].name, exc)) {
2049 PySys_WriteStderr(
2050 "Cannot insert exception into __builtin__: %s\n",
2051 bltin_exc[i].name);
2052 goto finally;
2053 }
Barry Warsaw757af0e1997-08-29 22:13:51 +00002054 }
2055
2056 /* we need one pre-allocated instance */
2057 args = Py_BuildValue("()");
Barry Warsaw98b62461998-09-14 18:51:11 +00002058 if (!args ||
2059 !(PyExc_MemoryErrorInst =
2060 PyEval_CallObject(PyExc_MemoryError, args)))
2061 {
2062 PySys_WriteStderr("Cannot pre-allocate MemoryError instance\n");
2063 goto finally;
Barry Warsaw757af0e1997-08-29 22:13:51 +00002064 }
Barry Warsaw98b62461998-09-14 18:51:11 +00002065 Py_DECREF(args);
Barry Warsaw757af0e1997-08-29 22:13:51 +00002066
2067 /* we're done with the exceptions module */
2068 Py_DECREF(m);
2069
Barry Warsaw98b62461998-09-14 18:51:11 +00002070 if (PyErr_Occurred()) {
2071 PySys_WriteStderr("Cannot initialize standard class exceptions; ");
2072 if (Py_VerboseFlag) {
2073 PySys_WriteStderr("traceback:\n");
2074 PyErr_Print();
2075 }
2076 else
2077 PySys_WriteStderr("use -v for traceback\n");
2078 goto finally;
2079 }
2080 return 1;
2081 finally:
2082 Py_XDECREF(m);
2083 Py_XDECREF(args);
2084 PyErr_Clear();
2085 return 0;
Barry Warsaw757af0e1997-08-29 22:13:51 +00002086}
2087
2088
2089static void
2090fini_instances()
2091{
2092 Py_XDECREF(PyExc_MemoryErrorInst);
2093 PyExc_MemoryErrorInst = NULL;
2094}
2095
2096
Guido van Rossum79f25d91997-04-29 20:08:16 +00002097static PyObject *
Guido van Rossum25ce5661997-08-02 03:10:38 +00002098newstdexception(dict, name)
2099 PyObject *dict;
Guido van Rossumfb905c31991-12-16 15:42:38 +00002100 char *name;
Guido van Rossum3f5da241990-12-20 15:06:42 +00002101{
Guido van Rossum79f25d91997-04-29 20:08:16 +00002102 PyObject *v = PyString_FromString(name);
Guido van Rossum25ce5661997-08-02 03:10:38 +00002103 if (v == NULL || PyDict_SetItemString(dict, name, v) != 0)
Barry Warsaw98b62461998-09-14 18:51:11 +00002104 Py_FatalError("Cannot create string-based exceptions");
Guido van Rossum3f5da241990-12-20 15:06:42 +00002105 return v;
2106}
2107
2108static void
Guido van Rossum25ce5661997-08-02 03:10:38 +00002109initerrors(dict)
2110 PyObject *dict;
Guido van Rossum3f5da241990-12-20 15:06:42 +00002111{
Barry Warsaw757af0e1997-08-29 22:13:51 +00002112 int i;
2113 int exccnt = 0;
2114 for (i = 0; bltin_exc[i].name; i++, exccnt++) {
Barry Warsaw98b62461998-09-14 18:51:11 +00002115 Py_XDECREF(*bltin_exc[i].exc);
Barry Warsaw757af0e1997-08-29 22:13:51 +00002116 if (bltin_exc[i].leaf_exc)
2117 *bltin_exc[i].exc =
2118 newstdexception(dict, bltin_exc[i].name);
2119 }
2120
Barry Warsawd086a1a1998-07-23 15:59:57 +00002121 /* This is kind of bogus because we special case the some of the
2122 * new exceptions to be nearly forward compatible. But this means
2123 * we hard code knowledge about exceptions.py into C here. I don't
2124 * have a better solution, though.
2125 */
Barry Warsaw757af0e1997-08-29 22:13:51 +00002126 PyExc_LookupError = PyTuple_New(2);
2127 Py_INCREF(PyExc_IndexError);
2128 PyTuple_SET_ITEM(PyExc_LookupError, 0, PyExc_IndexError);
2129 Py_INCREF(PyExc_KeyError);
2130 PyTuple_SET_ITEM(PyExc_LookupError, 1, PyExc_KeyError);
2131 PyDict_SetItemString(dict, "LookupError", PyExc_LookupError);
2132
Barry Warsaw412cdc21997-09-16 21:51:14 +00002133 PyExc_ArithmeticError = PyTuple_New(3);
Barry Warsaw757af0e1997-08-29 22:13:51 +00002134 Py_INCREF(PyExc_OverflowError);
Barry Warsaw412cdc21997-09-16 21:51:14 +00002135 PyTuple_SET_ITEM(PyExc_ArithmeticError, 0, PyExc_OverflowError);
Barry Warsaw757af0e1997-08-29 22:13:51 +00002136 Py_INCREF(PyExc_ZeroDivisionError);
Barry Warsaw412cdc21997-09-16 21:51:14 +00002137 PyTuple_SET_ITEM(PyExc_ArithmeticError, 1, PyExc_ZeroDivisionError);
Barry Warsaw757af0e1997-08-29 22:13:51 +00002138 Py_INCREF(PyExc_FloatingPointError);
Barry Warsaw412cdc21997-09-16 21:51:14 +00002139 PyTuple_SET_ITEM(PyExc_ArithmeticError, 2, PyExc_FloatingPointError);
2140 PyDict_SetItemString(dict, "ArithmeticError", PyExc_ArithmeticError);
Barry Warsaw757af0e1997-08-29 22:13:51 +00002141
Barry Warsawd086a1a1998-07-23 15:59:57 +00002142 PyExc_EnvironmentError = PyTuple_New(2);
2143 Py_INCREF(PyExc_IOError);
2144 PyTuple_SET_ITEM(PyExc_EnvironmentError, 0, PyExc_IOError);
2145 Py_INCREF(PyExc_OSError);
2146 PyTuple_SET_ITEM(PyExc_EnvironmentError, 1, PyExc_OSError);
2147 PyDict_SetItemString(dict, "EnvironmentError", PyExc_EnvironmentError);
2148
Barry Warsawb01a7fa1997-09-18 03:44:38 +00002149 PyExc_StandardError = PyTuple_New(exccnt-2);
2150 for (i = 2; bltin_exc[i].name; i++) {
Barry Warsaw757af0e1997-08-29 22:13:51 +00002151 PyObject *exc = *bltin_exc[i].exc;
2152 Py_INCREF(exc);
Barry Warsawb01a7fa1997-09-18 03:44:38 +00002153 PyTuple_SET_ITEM(PyExc_StandardError, i-2, exc);
Barry Warsaw757af0e1997-08-29 22:13:51 +00002154 }
2155 PyDict_SetItemString(dict, "StandardError", PyExc_StandardError);
Guido van Rossum04748321997-09-16 18:43:15 +00002156
2157 /* Exception is treated differently; for now, it's == StandardError */
2158 PyExc_Exception = PyExc_StandardError;
2159 Py_INCREF(PyExc_Exception);
2160 PyDict_SetItemString(dict, "Exception", PyExc_Exception);
Barry Warsaw757af0e1997-08-29 22:13:51 +00002161
2162 if (PyErr_Occurred())
2163 Py_FatalError("Could not initialize built-in string exceptions");
Guido van Rossum25ce5661997-08-02 03:10:38 +00002164}
2165
2166static void
2167finierrors()
2168{
Barry Warsaw757af0e1997-08-29 22:13:51 +00002169 int i;
2170 for (i = 0; bltin_exc[i].name; i++) {
2171 PyObject *exc = *bltin_exc[i].exc;
2172 Py_XDECREF(exc);
2173 *bltin_exc[i].exc = NULL;
2174 }
Guido van Rossum25ce5661997-08-02 03:10:38 +00002175}
2176
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002177static char builtin_doc[] =
2178"Built-in functions, exceptions, and other objects.\n\
2179\n\
2180Noteworthy: None is the `nil' object; Ellipsis represents `...' in slices.";
2181
Guido van Rossum25ce5661997-08-02 03:10:38 +00002182PyObject *
Barry Warsaw757af0e1997-08-29 22:13:51 +00002183_PyBuiltin_Init_1()
Guido van Rossum25ce5661997-08-02 03:10:38 +00002184{
2185 PyObject *mod, *dict;
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002186 mod = Py_InitModule4("__builtin__", builtin_methods,
2187 builtin_doc, (PyObject *)NULL,
2188 PYTHON_API_VERSION);
Guido van Rossum25ce5661997-08-02 03:10:38 +00002189 if (mod == NULL)
2190 return NULL;
2191 dict = PyModule_GetDict(mod);
2192 initerrors(dict);
2193 if (PyDict_SetItemString(dict, "None", Py_None) < 0)
2194 return NULL;
2195 if (PyDict_SetItemString(dict, "Ellipsis", Py_Ellipsis) < 0)
2196 return NULL;
2197 if (PyDict_SetItemString(dict, "__debug__",
2198 PyInt_FromLong(Py_OptimizeFlag == 0)) < 0)
2199 return NULL;
Barry Warsaw757af0e1997-08-29 22:13:51 +00002200
Guido van Rossum25ce5661997-08-02 03:10:38 +00002201 return mod;
Guido van Rossum3f5da241990-12-20 15:06:42 +00002202}
2203
2204void
Barry Warsaw757af0e1997-08-29 22:13:51 +00002205_PyBuiltin_Init_2(dict)
2206 PyObject *dict;
2207{
2208 /* if Python was started with -X, initialize the class exceptions */
Barry Warsaw98b62461998-09-14 18:51:11 +00002209 if (Py_UseClassExceptionsFlag) {
2210 if (!init_class_exc(dict)) {
2211 /* class based exceptions could not be
2212 * initialized. Fall back to using string based
2213 * exceptions.
2214 */
2215 PySys_WriteStderr(
2216 "Warning! Falling back to string-based exceptions\n");
2217 initerrors(dict);
2218 }
2219 }
Barry Warsaw757af0e1997-08-29 22:13:51 +00002220}
2221
2222
2223void
2224_PyBuiltin_Fini_1()
2225{
2226 fini_instances();
2227}
2228
2229
2230void
2231_PyBuiltin_Fini_2()
Guido van Rossum3f5da241990-12-20 15:06:42 +00002232{
Guido van Rossum25ce5661997-08-02 03:10:38 +00002233 finierrors();
Guido van Rossum3f5da241990-12-20 15:06:42 +00002234}
Guido van Rossumc6bb8f71991-07-01 18:42:41 +00002235
Guido van Rossum12d12c51993-10-26 17:58:25 +00002236
Guido van Rossume77a7571993-11-03 15:01:26 +00002237/* Helper for filter(): filter a tuple through a function */
Guido van Rossum12d12c51993-10-26 17:58:25 +00002238
Guido van Rossum79f25d91997-04-29 20:08:16 +00002239static PyObject *
Guido van Rossum12d12c51993-10-26 17:58:25 +00002240filtertuple(func, tuple)
Guido van Rossum79f25d91997-04-29 20:08:16 +00002241 PyObject *func;
2242 PyObject *tuple;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002243{
Guido van Rossum79f25d91997-04-29 20:08:16 +00002244 PyObject *result;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002245 register int i, j;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002246 int len = PyTuple_Size(tuple);
Guido van Rossum12d12c51993-10-26 17:58:25 +00002247
Guido van Rossumb7b45621995-08-04 04:07:45 +00002248 if (len == 0) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002249 Py_INCREF(tuple);
Guido van Rossumb7b45621995-08-04 04:07:45 +00002250 return tuple;
2251 }
2252
Guido van Rossum79f25d91997-04-29 20:08:16 +00002253 if ((result = PyTuple_New(len)) == NULL)
Guido van Rossum2586bf01993-11-01 16:21:44 +00002254 return NULL;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002255
Guido van Rossum12d12c51993-10-26 17:58:25 +00002256 for (i = j = 0; i < len; ++i) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002257 PyObject *item, *good;
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002258 int ok;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002259
Guido van Rossum79f25d91997-04-29 20:08:16 +00002260 if ((item = PyTuple_GetItem(tuple, i)) == NULL)
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002261 goto Fail_1;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002262 if (func == Py_None) {
2263 Py_INCREF(item);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002264 good = item;
2265 }
2266 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002267 PyObject *arg = Py_BuildValue("(O)", item);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002268 if (arg == NULL)
2269 goto Fail_1;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002270 good = PyEval_CallObject(func, arg);
2271 Py_DECREF(arg);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002272 if (good == NULL)
Guido van Rossum12d12c51993-10-26 17:58:25 +00002273 goto Fail_1;
2274 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002275 ok = PyObject_IsTrue(good);
2276 Py_DECREF(good);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002277 if (ok) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002278 Py_INCREF(item);
2279 if (PyTuple_SetItem(result, j++, item) < 0)
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002280 goto Fail_1;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002281 }
Guido van Rossum12d12c51993-10-26 17:58:25 +00002282 }
2283
Guido van Rossum79f25d91997-04-29 20:08:16 +00002284 if (_PyTuple_Resize(&result, j, 0) < 0)
Guido van Rossum12d12c51993-10-26 17:58:25 +00002285 return NULL;
2286
Guido van Rossum12d12c51993-10-26 17:58:25 +00002287 return result;
2288
Guido van Rossum12d12c51993-10-26 17:58:25 +00002289Fail_1:
Guido van Rossum79f25d91997-04-29 20:08:16 +00002290 Py_DECREF(result);
Guido van Rossum12d12c51993-10-26 17:58:25 +00002291 return NULL;
2292}
2293
2294
Guido van Rossume77a7571993-11-03 15:01:26 +00002295/* Helper for filter(): filter a string through a function */
Guido van Rossum12d12c51993-10-26 17:58:25 +00002296
Guido van Rossum79f25d91997-04-29 20:08:16 +00002297static PyObject *
Guido van Rossum12d12c51993-10-26 17:58:25 +00002298filterstring(func, strobj)
Guido van Rossum79f25d91997-04-29 20:08:16 +00002299 PyObject *func;
2300 PyObject *strobj;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002301{
Guido van Rossum79f25d91997-04-29 20:08:16 +00002302 PyObject *result;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002303 register int i, j;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002304 int len = PyString_Size(strobj);
Guido van Rossum12d12c51993-10-26 17:58:25 +00002305
Guido van Rossum79f25d91997-04-29 20:08:16 +00002306 if (func == Py_None) {
Guido van Rossum2586bf01993-11-01 16:21:44 +00002307 /* No character is ever false -- share input string */
Guido van Rossum79f25d91997-04-29 20:08:16 +00002308 Py_INCREF(strobj);
Guido van Rossum2d951851994-08-29 12:52:16 +00002309 return strobj;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002310 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002311 if ((result = PyString_FromStringAndSize(NULL, len)) == NULL)
Guido van Rossum2586bf01993-11-01 16:21:44 +00002312 return NULL;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002313
Guido van Rossum12d12c51993-10-26 17:58:25 +00002314 for (i = j = 0; i < len; ++i) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002315 PyObject *item, *arg, *good;
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002316 int ok;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002317
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002318 item = (*strobj->ob_type->tp_as_sequence->sq_item)(strobj, i);
2319 if (item == NULL)
2320 goto Fail_1;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002321 arg = Py_BuildValue("(O)", item);
2322 Py_DECREF(item);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002323 if (arg == NULL)
2324 goto Fail_1;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002325 good = PyEval_CallObject(func, arg);
2326 Py_DECREF(arg);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002327 if (good == NULL)
2328 goto Fail_1;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002329 ok = PyObject_IsTrue(good);
2330 Py_DECREF(good);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002331 if (ok)
Guido van Rossum79f25d91997-04-29 20:08:16 +00002332 PyString_AS_STRING((PyStringObject *)result)[j++] =
2333 PyString_AS_STRING((PyStringObject *)item)[0];
Guido van Rossum12d12c51993-10-26 17:58:25 +00002334 }
2335
Guido van Rossum79f25d91997-04-29 20:08:16 +00002336 if (j < len && _PyString_Resize(&result, j) < 0)
Guido van Rossum12d12c51993-10-26 17:58:25 +00002337 return NULL;
2338
Guido van Rossum12d12c51993-10-26 17:58:25 +00002339 return result;
2340
Guido van Rossum12d12c51993-10-26 17:58:25 +00002341Fail_1:
Guido van Rossum79f25d91997-04-29 20:08:16 +00002342 Py_DECREF(result);
Guido van Rossum12d12c51993-10-26 17:58:25 +00002343 return NULL;
2344}