blob: b0d0648c0f0d16b8b6329f845d48e8d56573cdbf [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 {
Barry Warsawfa77e091999-01-28 18:49:12 +0000235 int status = PyList_Append(result, item);
Guido van Rossum2d951851994-08-29 12:52:16 +0000236 j++;
Barry Warsawfa77e091999-01-28 18:49:12 +0000237 Py_DECREF(item);
238 if (status < 0)
Guido van Rossum2d951851994-08-29 12:52:16 +0000239 goto Fail_1;
240 }
Guido van Rossum58b68731995-01-10 17:40:55 +0000241 } else {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000242 Py_DECREF(item);
Guido van Rossum12d12c51993-10-26 17:58:25 +0000243 }
Guido van Rossum12d12c51993-10-26 17:58:25 +0000244 }
245
Guido van Rossum12d12c51993-10-26 17:58:25 +0000246
Guido van Rossum79f25d91997-04-29 20:08:16 +0000247 if (j < len && PyList_SetSlice(result, j, len, NULL) < 0)
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000248 goto Fail_1;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000249
Guido van Rossum12d12c51993-10-26 17:58:25 +0000250 return result;
251
Guido van Rossum12d12c51993-10-26 17:58:25 +0000252Fail_1:
Guido van Rossum79f25d91997-04-29 20:08:16 +0000253 Py_DECREF(result);
Guido van Rossum12d12c51993-10-26 17:58:25 +0000254Fail_2:
Guido van Rossum12d12c51993-10-26 17:58:25 +0000255 return NULL;
256}
257
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000258static char filter_doc[] =
259"filter(function, sequence) -> list\n\
260\n\
261Return a list containing those items of sequence for which function(item)\n\
262is true. If function is None, return a list of items that are true.";
263
264
Guido van Rossum79f25d91997-04-29 20:08:16 +0000265static PyObject *
Guido van Rossum94390a41992-08-14 15:14:30 +0000266builtin_chr(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000267 PyObject *self;
268 PyObject *args;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000269{
270 long x;
271 char s[1];
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000272
Guido van Rossum79f25d91997-04-29 20:08:16 +0000273 if (!PyArg_ParseTuple(args, "l:chr", &x))
Guido van Rossum3f5da241990-12-20 15:06:42 +0000274 return NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000275 if (x < 0 || x >= 256) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000276 PyErr_SetString(PyExc_ValueError,
277 "chr() arg not in range(256)");
Guido van Rossum3f5da241990-12-20 15:06:42 +0000278 return NULL;
279 }
Guido van Rossum6bf62da1997-04-11 20:37:35 +0000280 s[0] = (char)x;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000281 return PyString_FromStringAndSize(s, 1);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000282}
283
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000284static char chr_doc[] =
285"chr(i) -> character\n\
286\n\
287Return a string of one character with ordinal i; 0 <= i < 256.";
288
289
Guido van Rossum79f25d91997-04-29 20:08:16 +0000290static PyObject *
Guido van Rossuma9e7dc11992-10-18 18:53:57 +0000291builtin_cmp(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000292 PyObject *self;
293 PyObject *args;
Guido van Rossuma9e7dc11992-10-18 18:53:57 +0000294{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000295 PyObject *a, *b;
Guido van Rossum09df08a1998-05-22 00:51:39 +0000296 int c;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000297
Guido van Rossum79f25d91997-04-29 20:08:16 +0000298 if (!PyArg_ParseTuple(args, "OO:cmp", &a, &b))
Guido van Rossuma9e7dc11992-10-18 18:53:57 +0000299 return NULL;
Guido van Rossum09df08a1998-05-22 00:51:39 +0000300 if (PyObject_Cmp(a, b, &c) < 0)
Guido van Rossumc8b6df91997-05-23 00:06:51 +0000301 return NULL;
Guido van Rossum09df08a1998-05-22 00:51:39 +0000302 return PyInt_FromLong((long)c);
Guido van Rossuma9e7dc11992-10-18 18:53:57 +0000303}
304
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000305static char cmp_doc[] =
306"cmp(x, y) -> integer\n\
307\n\
308Return negative if x<y, zero if x==y, positive if x>y.";
309
310
Guido van Rossum79f25d91997-04-29 20:08:16 +0000311static PyObject *
Guido van Rossum5524a591995-01-10 15:26:20 +0000312builtin_coerce(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000313 PyObject *self;
314 PyObject *args;
Guido van Rossum6a00cd81995-01-07 12:39:01 +0000315{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000316 PyObject *v, *w;
317 PyObject *res;
Guido van Rossum5524a591995-01-10 15:26:20 +0000318
Guido van Rossum79f25d91997-04-29 20:08:16 +0000319 if (!PyArg_ParseTuple(args, "OO:coerce", &v, &w))
Guido van Rossum5524a591995-01-10 15:26:20 +0000320 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000321 if (PyNumber_Coerce(&v, &w) < 0)
Guido van Rossum04691fc1992-08-12 15:35:34 +0000322 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000323 res = Py_BuildValue("(OO)", v, w);
324 Py_DECREF(v);
325 Py_DECREF(w);
Guido van Rossum04691fc1992-08-12 15:35:34 +0000326 return res;
327}
328
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000329static char coerce_doc[] =
330"coerce(x, y) -> None or (x1, y1)\n\
331\n\
332When x and y can be coerced to values of the same type, return a tuple\n\
333containing the coerced values. When they can't be coerced, return None.";
334
335
Guido van Rossum79f25d91997-04-29 20:08:16 +0000336static PyObject *
Guido van Rossum5b722181993-03-30 17:46:03 +0000337builtin_compile(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000338 PyObject *self;
339 PyObject *args;
Guido van Rossum5b722181993-03-30 17:46:03 +0000340{
341 char *str;
342 char *filename;
343 char *startstr;
344 int start;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000345
Guido van Rossum79f25d91997-04-29 20:08:16 +0000346 if (!PyArg_ParseTuple(args, "sss:compile", &str, &filename, &startstr))
Guido van Rossum5b722181993-03-30 17:46:03 +0000347 return NULL;
348 if (strcmp(startstr, "exec") == 0)
Guido van Rossumb05a5c71997-05-07 17:46:13 +0000349 start = Py_file_input;
Guido van Rossum5b722181993-03-30 17:46:03 +0000350 else if (strcmp(startstr, "eval") == 0)
Guido van Rossumb05a5c71997-05-07 17:46:13 +0000351 start = Py_eval_input;
Guido van Rossum872537c1995-07-07 22:43:42 +0000352 else if (strcmp(startstr, "single") == 0)
Guido van Rossumb05a5c71997-05-07 17:46:13 +0000353 start = Py_single_input;
Guido van Rossum5b722181993-03-30 17:46:03 +0000354 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000355 PyErr_SetString(PyExc_ValueError,
Guido van Rossum872537c1995-07-07 22:43:42 +0000356 "compile() mode must be 'exec' or 'eval' or 'single'");
Guido van Rossum5b722181993-03-30 17:46:03 +0000357 return NULL;
358 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000359 return Py_CompileString(str, filename, start);
Guido van Rossum5b722181993-03-30 17:46:03 +0000360}
361
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000362static char compile_doc[] =
363"compile(source, filename, mode) -> code object\n\
364\n\
365Compile the source string (a Python module, statement or expression)\n\
366into a code object that can be executed by the exec statement or eval().\n\
367The filename will be used for run-time error messages.\n\
368The mode must be 'exec' to compile a module, 'single' to compile a\n\
369single (interactive) statement, or 'eval' to compile an expression.";
370
371
Guido van Rossum8a5c5d21996-01-12 01:09:56 +0000372#ifndef WITHOUT_COMPLEX
373
Guido van Rossum79f25d91997-04-29 20:08:16 +0000374static PyObject *
Guido van Rossum8a5c5d21996-01-12 01:09:56 +0000375builtin_complex(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000376 PyObject *self;
377 PyObject *args;
Guido van Rossum8a5c5d21996-01-12 01:09:56 +0000378{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000379 PyObject *r, *i, *tmp;
380 PyNumberMethods *nbr, *nbi = NULL;
Guido van Rossum530956d1996-07-21 02:27:43 +0000381 Py_complex cr, ci;
Guido van Rossumc6472e91997-03-31 17:15:43 +0000382 int own_r = 0;
Guido van Rossum8a5c5d21996-01-12 01:09:56 +0000383
384 i = NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000385 if (!PyArg_ParseTuple(args, "O|O:complex", &r, &i))
Guido van Rossum8a5c5d21996-01-12 01:09:56 +0000386 return NULL;
387 if ((nbr = r->ob_type->tp_as_number) == NULL ||
Guido van Rossumc6472e91997-03-31 17:15:43 +0000388 nbr->nb_float == NULL ||
389 (i != NULL &&
390 ((nbi = i->ob_type->tp_as_number) == NULL ||
391 nbi->nb_float == NULL))) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000392 PyErr_SetString(PyExc_TypeError,
Guido van Rossum8a5c5d21996-01-12 01:09:56 +0000393 "complex() argument can't be converted to complex");
394 return NULL;
395 }
Guido van Rossumed0af8f1996-12-05 23:18:18 +0000396 /* XXX Hack to support classes with __complex__ method */
Guido van Rossum79f25d91997-04-29 20:08:16 +0000397 if (PyInstance_Check(r)) {
398 static PyObject *complexstr;
399 PyObject *f;
Guido van Rossumed0af8f1996-12-05 23:18:18 +0000400 if (complexstr == NULL) {
Guido van Rossum8d751611997-01-18 08:04:16 +0000401 complexstr = PyString_InternFromString("__complex__");
Guido van Rossumed0af8f1996-12-05 23:18:18 +0000402 if (complexstr == NULL)
403 return NULL;
404 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000405 f = PyObject_GetAttr(r, complexstr);
Guido van Rossumed0af8f1996-12-05 23:18:18 +0000406 if (f == NULL)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000407 PyErr_Clear();
Guido van Rossumed0af8f1996-12-05 23:18:18 +0000408 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000409 PyObject *args = Py_BuildValue("()");
Guido van Rossumed0af8f1996-12-05 23:18:18 +0000410 if (args == NULL)
411 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000412 r = PyEval_CallObject(f, args);
413 Py_DECREF(args);
Barry Warsawf988e681999-01-27 23:13:59 +0000414 Py_DECREF(f);
Guido van Rossumed0af8f1996-12-05 23:18:18 +0000415 if (r == NULL)
416 return NULL;
Guido van Rossumc6472e91997-03-31 17:15:43 +0000417 own_r = 1;
Guido van Rossumed0af8f1996-12-05 23:18:18 +0000418 }
419 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000420 if (PyComplex_Check(r)) {
421 cr = ((PyComplexObject*)r)->cval;
Guido van Rossum730806d1998-04-10 22:27:42 +0000422 if (own_r) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000423 Py_DECREF(r);
Guido van Rossum730806d1998-04-10 22:27:42 +0000424 }
Guido van Rossumc6472e91997-03-31 17:15:43 +0000425 }
Guido van Rossum8a5c5d21996-01-12 01:09:56 +0000426 else {
Guido van Rossumfe4b6ee1996-08-08 18:49:41 +0000427 tmp = (*nbr->nb_float)(r);
Guido van Rossum730806d1998-04-10 22:27:42 +0000428 if (own_r) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000429 Py_DECREF(r);
Guido van Rossum730806d1998-04-10 22:27:42 +0000430 }
Guido van Rossumfe4b6ee1996-08-08 18:49:41 +0000431 if (tmp == NULL)
432 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000433 cr.real = PyFloat_AsDouble(tmp);
434 Py_DECREF(tmp);
Guido van Rossum8a5c5d21996-01-12 01:09:56 +0000435 cr.imag = 0.;
436 }
437 if (i == NULL) {
438 ci.real = 0.;
439 ci.imag = 0.;
440 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000441 else if (PyComplex_Check(i))
442 ci = ((PyComplexObject*)i)->cval;
Guido van Rossum8a5c5d21996-01-12 01:09:56 +0000443 else {
Guido van Rossumc6472e91997-03-31 17:15:43 +0000444 tmp = (*nbi->nb_float)(i);
Guido van Rossumfe4b6ee1996-08-08 18:49:41 +0000445 if (tmp == NULL)
446 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000447 ci.real = PyFloat_AsDouble(tmp);
448 Py_DECREF(tmp);
Guido van Rossum8a5c5d21996-01-12 01:09:56 +0000449 ci.imag = 0.;
450 }
451 cr.real -= ci.imag;
452 cr.imag += ci.real;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000453 return PyComplex_FromCComplex(cr);
Guido van Rossum8a5c5d21996-01-12 01:09:56 +0000454}
455
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000456static char complex_doc[] =
457"complex(real[, imag]) -> complex number\n\
458\n\
459Create a complex number from a real part and an optional imaginary part.\n\
460This is equivalent to (real + imag*1j) where imag defaults to 0.";
461
462
Guido van Rossum8a5c5d21996-01-12 01:09:56 +0000463#endif
464
Guido van Rossum79f25d91997-04-29 20:08:16 +0000465static PyObject *
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000466builtin_dir(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000467 PyObject *self;
468 PyObject *args;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000469{
Guido van Rossum666b17a1997-05-06 16:36:57 +0000470 static char *attrlist[] = {"__members__", "__methods__", NULL};
471 PyObject *v = NULL, *l = NULL, *m = NULL;
472 PyObject *d, *x;
473 int i;
474 char **s;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000475
Guido van Rossum79f25d91997-04-29 20:08:16 +0000476 if (!PyArg_ParseTuple(args, "|O:dir", &v))
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000477 return NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000478 if (v == NULL) {
Guido van Rossum666b17a1997-05-06 16:36:57 +0000479 x = PyEval_GetLocals();
480 if (x == NULL)
481 goto error;
482 l = PyMapping_Keys(x);
483 if (l == NULL)
484 goto error;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000485 }
486 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000487 d = PyObject_GetAttrString(v, "__dict__");
Guido van Rossum666b17a1997-05-06 16:36:57 +0000488 if (d == NULL)
Guido van Rossum795ba581996-05-23 22:49:07 +0000489 PyErr_Clear();
Guido van Rossum666b17a1997-05-06 16:36:57 +0000490 else {
491 l = PyMapping_Keys(d);
492 if (l == NULL)
493 PyErr_Clear();
494 Py_DECREF(d);
495 }
496 if (l == NULL) {
497 l = PyList_New(0);
498 if (l == NULL)
499 goto error;
500 }
501 for (s = attrlist; *s != NULL; s++) {
502 m = PyObject_GetAttrString(v, *s);
503 if (m == NULL) {
504 PyErr_Clear();
505 continue;
506 }
507 for (i = 0; ; i++) {
508 x = PySequence_GetItem(m, i);
509 if (x == NULL) {
510 PyErr_Clear();
511 break;
512 }
513 if (PyList_Append(l, x) != 0) {
514 Py_DECREF(x);
515 Py_DECREF(m);
516 goto error;
517 }
518 Py_DECREF(x);
519 }
520 Py_DECREF(m);
Guido van Rossum795ba581996-05-23 22:49:07 +0000521 }
Guido van Rossum006bcd41991-10-24 14:54:44 +0000522 }
Guido van Rossum666b17a1997-05-06 16:36:57 +0000523 if (PyList_Sort(l) != 0)
524 goto error;
525 return l;
526 error:
527 Py_XDECREF(l);
528 return NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000529}
530
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000531static char dir_doc[] =
532"dir([object]) -> list of strings\n\
533\n\
534Return an alphabetized list of names comprising (some of) the attributes\n\
535of the given object. Without an argument, the names in the current scope\n\
536are listed. With an instance argument, only the instance attributes are\n\
537returned. With a class argument, attributes of the base class are not\n\
538returned. For other types or arguments, this may list members or methods.";
539
540
Guido van Rossum79f25d91997-04-29 20:08:16 +0000541static PyObject *
Guido van Rossum6a00cd81995-01-07 12:39:01 +0000542builtin_divmod(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000543 PyObject *self;
544 PyObject *args;
Guido van Rossum6a00cd81995-01-07 12:39:01 +0000545{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000546 PyObject *v, *w;
Guido van Rossum6a00cd81995-01-07 12:39:01 +0000547
Guido van Rossum79f25d91997-04-29 20:08:16 +0000548 if (!PyArg_ParseTuple(args, "OO:divmod", &v, &w))
Guido van Rossum6a00cd81995-01-07 12:39:01 +0000549 return NULL;
Guido van Rossum09df08a1998-05-22 00:51:39 +0000550 return PyNumber_Divmod(v, w);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000551}
552
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000553static char divmod_doc[] =
554"divmod(x, y) -> (div, mod)\n\
555\n\
556Return the tuple ((x-x%y)/y, x%y). Invariant: div*y + mod == x.";
557
558
Guido van Rossum79f25d91997-04-29 20:08:16 +0000559static PyObject *
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000560builtin_eval(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000561 PyObject *self;
562 PyObject *args;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000563{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000564 PyObject *cmd;
565 PyObject *globals = Py_None, *locals = Py_None;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000566 char *str;
Guido van Rossum590baa41993-11-30 13:40:46 +0000567
Guido van Rossum79f25d91997-04-29 20:08:16 +0000568 if (!PyArg_ParseTuple(args, "O|O!O!:eval",
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000569 &cmd,
Guido van Rossum79f25d91997-04-29 20:08:16 +0000570 &PyDict_Type, &globals,
571 &PyDict_Type, &locals))
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000572 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000573 if (globals == Py_None) {
574 globals = PyEval_GetGlobals();
575 if (locals == Py_None)
576 locals = PyEval_GetLocals();
Guido van Rossum6135a871995-01-09 17:53:26 +0000577 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000578 else if (locals == Py_None)
Guido van Rossum6135a871995-01-09 17:53:26 +0000579 locals = globals;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000580 if (PyDict_GetItemString(globals, "__builtins__") == NULL) {
581 if (PyDict_SetItemString(globals, "__builtins__",
582 PyEval_GetBuiltins()) != 0)
Guido van Rossum6135a871995-01-09 17:53:26 +0000583 return NULL;
584 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000585 if (PyCode_Check(cmd))
586 return PyEval_EvalCode((PyCodeObject *) cmd, globals, locals);
587 if (!PyString_Check(cmd)) {
588 PyErr_SetString(PyExc_TypeError,
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000589 "eval() argument 1 must be string or code object");
Guido van Rossum94390a41992-08-14 15:14:30 +0000590 return NULL;
591 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000592 str = PyString_AsString(cmd);
593 if ((int)strlen(str) != PyString_Size(cmd)) {
594 PyErr_SetString(PyExc_ValueError,
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000595 "embedded '\\0' in string arg");
596 return NULL;
Guido van Rossumf08ab0a1992-03-04 16:41:41 +0000597 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000598 while (*str == ' ' || *str == '\t')
599 str++;
Guido van Rossumb05a5c71997-05-07 17:46:13 +0000600 return PyRun_String(str, Py_eval_input, globals, locals);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000601}
602
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000603static char eval_doc[] =
604"eval(source[, globals[, locals]]) -> value\n\
605\n\
606Evaluate the source in the context of globals and locals.\n\
607The source may be a string representing a Python expression\n\
608or a code object as returned by compile().\n\
609The globals and locals are dictionaries, defaulting to the current\n\
610globals and locals. If only globals is given, locals defaults to it.";
611
612
Guido van Rossum79f25d91997-04-29 20:08:16 +0000613static PyObject *
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000614builtin_execfile(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000615 PyObject *self;
616 PyObject *args;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000617{
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000618 char *filename;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000619 PyObject *globals = Py_None, *locals = Py_None;
620 PyObject *res;
Guido van Rossum0f61f8a1992-02-25 18:55:05 +0000621 FILE* fp;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000622
Guido van Rossum79f25d91997-04-29 20:08:16 +0000623 if (!PyArg_ParseTuple(args, "s|O!O!:execfile",
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000624 &filename,
Guido van Rossum79f25d91997-04-29 20:08:16 +0000625 &PyDict_Type, &globals,
626 &PyDict_Type, &locals))
Guido van Rossum0f61f8a1992-02-25 18:55:05 +0000627 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000628 if (globals == Py_None) {
629 globals = PyEval_GetGlobals();
630 if (locals == Py_None)
631 locals = PyEval_GetLocals();
Guido van Rossum6135a871995-01-09 17:53:26 +0000632 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000633 else if (locals == Py_None)
Guido van Rossum6135a871995-01-09 17:53:26 +0000634 locals = globals;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000635 if (PyDict_GetItemString(globals, "__builtins__") == NULL) {
636 if (PyDict_SetItemString(globals, "__builtins__",
637 PyEval_GetBuiltins()) != 0)
Guido van Rossum6135a871995-01-09 17:53:26 +0000638 return NULL;
639 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000640 Py_BEGIN_ALLOW_THREADS
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000641 fp = fopen(filename, "r");
Guido van Rossum79f25d91997-04-29 20:08:16 +0000642 Py_END_ALLOW_THREADS
Guido van Rossum0f61f8a1992-02-25 18:55:05 +0000643 if (fp == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000644 PyErr_SetFromErrno(PyExc_IOError);
Guido van Rossum0f61f8a1992-02-25 18:55:05 +0000645 return NULL;
646 }
Guido van Rossumb05a5c71997-05-07 17:46:13 +0000647 res = PyRun_File(fp, filename, Py_file_input, globals, locals);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000648 Py_BEGIN_ALLOW_THREADS
Guido van Rossum0f61f8a1992-02-25 18:55:05 +0000649 fclose(fp);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000650 Py_END_ALLOW_THREADS
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000651 return res;
Guido van Rossum0f61f8a1992-02-25 18:55:05 +0000652}
653
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000654static char execfile_doc[] =
655"execfile(filename[, globals[, locals]])\n\
656\n\
657Read and execute a Python script from a file.\n\
658The globals and locals are dictionaries, defaulting to the current\n\
659globals and locals. If only globals is given, locals defaults to it.";
660
661
Guido van Rossum79f25d91997-04-29 20:08:16 +0000662static PyObject *
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000663builtin_float(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000664 PyObject *self;
665 PyObject *args;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000666{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000667 PyObject *v;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000668
Guido van Rossum79f25d91997-04-29 20:08:16 +0000669 if (!PyArg_ParseTuple(args, "O:float", &v))
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000670 return NULL;
Guido van Rossum09df08a1998-05-22 00:51:39 +0000671 return PyNumber_Float(v);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000672}
673
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000674static char float_doc[] =
675"float(x) -> floating point number\n\
676\n\
677Convert a string or number to a floating point number, if possible.";
678
679
Guido van Rossum79f25d91997-04-29 20:08:16 +0000680static PyObject *
Guido van Rossum94390a41992-08-14 15:14:30 +0000681builtin_getattr(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000682 PyObject *self;
683 PyObject *args;
Guido van Rossum33894be1992-01-27 16:53:09 +0000684{
Guido van Rossum950ff291998-06-29 13:38:57 +0000685 PyObject *v, *result, *dflt = NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000686 PyObject *name;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000687
Guido van Rossum950ff291998-06-29 13:38:57 +0000688 if (!PyArg_ParseTuple(args, "OS|O:getattr", &v, &name, &dflt))
Guido van Rossum33894be1992-01-27 16:53:09 +0000689 return NULL;
Guido van Rossum950ff291998-06-29 13:38:57 +0000690 result = PyObject_GetAttr(v, name);
691 if (result == NULL && dflt != NULL) {
692 PyErr_Clear();
693 Py_INCREF(dflt);
694 result = dflt;
695 }
696 return result;
Guido van Rossum9bfef441993-03-29 10:43:31 +0000697}
698
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000699static char getattr_doc[] =
Guido van Rossum950ff291998-06-29 13:38:57 +0000700"getattr(object, name[, default]) -> value\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000701\n\
Guido van Rossum950ff291998-06-29 13:38:57 +0000702Get a named attribute from an object; getattr(x, 'y') is equivalent to x.y.\n\
703When a default argument is given, it is returned when the attribute doesn't\n\
704exist; without it, an exception is raised in that case.";
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000705
706
Guido van Rossum79f25d91997-04-29 20:08:16 +0000707static PyObject *
Guido van Rossum872537c1995-07-07 22:43:42 +0000708builtin_globals(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000709 PyObject *self;
710 PyObject *args;
Guido van Rossum872537c1995-07-07 22:43:42 +0000711{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000712 PyObject *d;
Guido van Rossum872537c1995-07-07 22:43:42 +0000713
Guido van Rossum79f25d91997-04-29 20:08:16 +0000714 if (!PyArg_ParseTuple(args, ""))
Guido van Rossum872537c1995-07-07 22:43:42 +0000715 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000716 d = PyEval_GetGlobals();
717 Py_INCREF(d);
Guido van Rossum872537c1995-07-07 22:43:42 +0000718 return d;
719}
720
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000721static char globals_doc[] =
722"globals() -> dictionary\n\
723\n\
724Return the dictionary containing the current scope's global variables.";
725
726
Guido van Rossum79f25d91997-04-29 20:08:16 +0000727static PyObject *
Guido van Rossum9bfef441993-03-29 10:43:31 +0000728builtin_hasattr(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000729 PyObject *self;
730 PyObject *args;
Guido van Rossum9bfef441993-03-29 10:43:31 +0000731{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000732 PyObject *v;
733 PyObject *name;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000734
Guido van Rossum79f25d91997-04-29 20:08:16 +0000735 if (!PyArg_ParseTuple(args, "OS:hasattr", &v, &name))
Guido van Rossum9bfef441993-03-29 10:43:31 +0000736 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000737 v = PyObject_GetAttr(v, name);
Guido van Rossum9bfef441993-03-29 10:43:31 +0000738 if (v == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000739 PyErr_Clear();
Guido van Rossum09df08a1998-05-22 00:51:39 +0000740 Py_INCREF(Py_False);
741 return Py_False;
Guido van Rossum9bfef441993-03-29 10:43:31 +0000742 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000743 Py_DECREF(v);
Guido van Rossum09df08a1998-05-22 00:51:39 +0000744 Py_INCREF(Py_True);
745 return Py_True;
Guido van Rossum33894be1992-01-27 16:53:09 +0000746}
747
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000748static char hasattr_doc[] =
749"hasattr(object, name) -> Boolean\n\
750\n\
751Return whether the object has an attribute with the given name.\n\
752(This is done by calling getattr(object, name) and catching exceptions.)";
753
754
Guido van Rossum79f25d91997-04-29 20:08:16 +0000755static PyObject *
Guido van Rossum5b722181993-03-30 17:46:03 +0000756builtin_id(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000757 PyObject *self;
758 PyObject *args;
Guido van Rossum5b722181993-03-30 17:46:03 +0000759{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000760 PyObject *v;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000761
Guido van Rossum79f25d91997-04-29 20:08:16 +0000762 if (!PyArg_ParseTuple(args, "O:id", &v))
Guido van Rossum5b722181993-03-30 17:46:03 +0000763 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000764 return PyInt_FromLong((long)v);
Guido van Rossum5b722181993-03-30 17:46:03 +0000765}
766
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000767static char id_doc[] =
768"id(object) -> integer\n\
769\n\
770Return the identity of an object. This is guaranteed to be unique among\n\
771simultaneously existing objects. (Hint: it's the object's memory address.)";
772
773
Guido van Rossum79f25d91997-04-29 20:08:16 +0000774static PyObject *
Guido van Rossum12d12c51993-10-26 17:58:25 +0000775builtin_map(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000776 PyObject *self;
777 PyObject *args;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000778{
779 typedef struct {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000780 PyObject *seq;
781 PySequenceMethods *sqf;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000782 int len;
783 } sequence;
784
Guido van Rossum79f25d91997-04-29 20:08:16 +0000785 PyObject *func, *result;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000786 sequence *seqs = NULL, *sqp;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000787 int n, len;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000788 register int i, j;
789
Guido van Rossum79f25d91997-04-29 20:08:16 +0000790 n = PyTuple_Size(args);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000791 if (n < 2) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000792 PyErr_SetString(PyExc_TypeError,
793 "map() requires at least two args");
Guido van Rossum12d12c51993-10-26 17:58:25 +0000794 return NULL;
795 }
796
Guido van Rossum79f25d91997-04-29 20:08:16 +0000797 func = PyTuple_GetItem(args, 0);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000798 n--;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000799
Guido van Rossumfa4ac711998-07-10 17:37:30 +0000800 if (func == Py_None && n == 1) {
801 /* map(None, S) is the same as list(S). */
802 return PySequence_List(PyTuple_GetItem(args, 1));
803 }
804
Guido van Rossum79f25d91997-04-29 20:08:16 +0000805 if ((seqs = PyMem_NEW(sequence, n)) == NULL) {
806 PyErr_NoMemory();
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000807 goto Fail_2;
808 }
Guido van Rossum12d12c51993-10-26 17:58:25 +0000809
Guido van Rossum2d951851994-08-29 12:52:16 +0000810 for (len = 0, i = 0, sqp = seqs; i < n; ++i, ++sqp) {
Guido van Rossum12d12c51993-10-26 17:58:25 +0000811 int curlen;
Guido van Rossum09df08a1998-05-22 00:51:39 +0000812 PySequenceMethods *sqf;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000813
Guido van Rossum79f25d91997-04-29 20:08:16 +0000814 if ((sqp->seq = PyTuple_GetItem(args, i + 1)) == NULL)
Guido van Rossum12d12c51993-10-26 17:58:25 +0000815 goto Fail_2;
816
Guido van Rossum09df08a1998-05-22 00:51:39 +0000817 sqp->sqf = sqf = sqp->seq->ob_type->tp_as_sequence;
818 if (sqf == NULL ||
819 sqf->sq_length == NULL ||
820 sqf->sq_item == NULL)
821 {
Guido van Rossum12d12c51993-10-26 17:58:25 +0000822 static char errmsg[] =
823 "argument %d to map() must be a sequence object";
Guido van Rossum15e33a41997-04-30 19:00:27 +0000824 char errbuf[sizeof(errmsg) + 25];
Guido van Rossum12d12c51993-10-26 17:58:25 +0000825
826 sprintf(errbuf, errmsg, i+2);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000827 PyErr_SetString(PyExc_TypeError, errbuf);
Guido van Rossum12d12c51993-10-26 17:58:25 +0000828 goto Fail_2;
829 }
830
831 if ((curlen = sqp->len = (*sqp->sqf->sq_length)(sqp->seq)) < 0)
832 goto Fail_2;
833
834 if (curlen > len)
835 len = curlen;
836 }
837
Guido van Rossum79f25d91997-04-29 20:08:16 +0000838 if ((result = (PyObject *) PyList_New(len)) == NULL)
Guido van Rossum12d12c51993-10-26 17:58:25 +0000839 goto Fail_2;
840
Guido van Rossum2d951851994-08-29 12:52:16 +0000841 for (i = 0; ; ++i) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000842 PyObject *alist, *item=NULL, *value;
Guido van Rossum2d951851994-08-29 12:52:16 +0000843 int any = 0;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000844
Guido van Rossum79f25d91997-04-29 20:08:16 +0000845 if (func == Py_None && n == 1)
Guido van Rossum32120311995-07-10 13:52:21 +0000846 alist = NULL;
Guido van Rossum2d951851994-08-29 12:52:16 +0000847 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000848 if ((alist = PyTuple_New(n)) == NULL)
Guido van Rossum2d951851994-08-29 12:52:16 +0000849 goto Fail_1;
850 }
Guido van Rossum12d12c51993-10-26 17:58:25 +0000851
852 for (j = 0, sqp = seqs; j < n; ++j, ++sqp) {
Guido van Rossum2d951851994-08-29 12:52:16 +0000853 if (sqp->len < 0) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000854 Py_INCREF(Py_None);
855 item = Py_None;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000856 }
Guido van Rossum12d12c51993-10-26 17:58:25 +0000857 else {
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000858 item = (*sqp->sqf->sq_item)(sqp->seq, i);
Guido van Rossum2d951851994-08-29 12:52:16 +0000859 if (item == NULL) {
Barry Warsawcde8b1b1997-08-22 21:14:38 +0000860 if (PyErr_ExceptionMatches(
861 PyExc_IndexError))
862 {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000863 PyErr_Clear();
864 Py_INCREF(Py_None);
865 item = Py_None;
Guido van Rossum2d951851994-08-29 12:52:16 +0000866 sqp->len = -1;
867 }
868 else {
869 goto Fail_0;
870 }
871 }
872 else
873 any = 1;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000874
Guido van Rossum12d12c51993-10-26 17:58:25 +0000875 }
Guido van Rossum32120311995-07-10 13:52:21 +0000876 if (!alist)
Guido van Rossum2d951851994-08-29 12:52:16 +0000877 break;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000878 if (PyTuple_SetItem(alist, j, item) < 0) {
879 Py_DECREF(item);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000880 goto Fail_0;
Guido van Rossum2d951851994-08-29 12:52:16 +0000881 }
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000882 continue;
883
884 Fail_0:
Guido van Rossum79f25d91997-04-29 20:08:16 +0000885 Py_XDECREF(alist);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000886 goto Fail_1;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000887 }
888
Guido van Rossum32120311995-07-10 13:52:21 +0000889 if (!alist)
890 alist = item;
Guido van Rossum2d951851994-08-29 12:52:16 +0000891
892 if (!any) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000893 Py_DECREF(alist);
Guido van Rossum2d951851994-08-29 12:52:16 +0000894 break;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000895 }
Guido van Rossum2d951851994-08-29 12:52:16 +0000896
Guido van Rossum79f25d91997-04-29 20:08:16 +0000897 if (func == Py_None)
Guido van Rossum32120311995-07-10 13:52:21 +0000898 value = alist;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000899 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000900 value = PyEval_CallObject(func, alist);
901 Py_DECREF(alist);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000902 if (value == NULL)
903 goto Fail_1;
Guido van Rossum2d951851994-08-29 12:52:16 +0000904 }
905 if (i >= len) {
Barry Warsawfa77e091999-01-28 18:49:12 +0000906 int status = PyList_Append(result, value);
Barry Warsaw21332871999-01-28 04:21:35 +0000907 Py_DECREF(value);
Barry Warsawfa77e091999-01-28 18:49:12 +0000908 if (status < 0)
909 goto Fail_1;
Guido van Rossum2d951851994-08-29 12:52:16 +0000910 }
911 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000912 if (PyList_SetItem(result, i, value) < 0)
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000913 goto Fail_1;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000914 }
915 }
916
Guido van Rossumfa4ac711998-07-10 17:37:30 +0000917 if (i < len && PyList_SetSlice(result, i, len, NULL) < 0)
918 goto Fail_1;
919
Guido van Rossum79f25d91997-04-29 20:08:16 +0000920 PyMem_DEL(seqs);
Guido van Rossum12d12c51993-10-26 17:58:25 +0000921 return result;
922
Guido van Rossum12d12c51993-10-26 17:58:25 +0000923Fail_1:
Guido van Rossum79f25d91997-04-29 20:08:16 +0000924 Py_DECREF(result);
Guido van Rossum12d12c51993-10-26 17:58:25 +0000925Fail_2:
Guido van Rossum79f25d91997-04-29 20:08:16 +0000926 if (seqs) PyMem_DEL(seqs);
Guido van Rossum12d12c51993-10-26 17:58:25 +0000927 return NULL;
928}
929
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000930static char map_doc[] =
931"map(function, sequence[, sequence, ...]) -> list\n\
932\n\
933Return a list of the results of applying the function to the items of\n\
934the argument sequence(s). If more than one sequence is given, the\n\
935function is called with an argument list consisting of the corresponding\n\
936item of each sequence, substituting None for missing values when not all\n\
937sequences have the same length. If the function is None, return a list of\n\
938the items of the sequence (or a list of tuples if more than one sequence).";
939
940
Guido van Rossum79f25d91997-04-29 20:08:16 +0000941static PyObject *
Guido van Rossum94390a41992-08-14 15:14:30 +0000942builtin_setattr(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000943 PyObject *self;
944 PyObject *args;
Guido van Rossum33894be1992-01-27 16:53:09 +0000945{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000946 PyObject *v;
947 PyObject *name;
948 PyObject *value;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000949
Guido van Rossum79f25d91997-04-29 20:08:16 +0000950 if (!PyArg_ParseTuple(args, "OSO:setattr", &v, &name, &value))
Guido van Rossum33894be1992-01-27 16:53:09 +0000951 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000952 if (PyObject_SetAttr(v, name, value) != 0)
Guido van Rossum33894be1992-01-27 16:53:09 +0000953 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000954 Py_INCREF(Py_None);
955 return Py_None;
Guido van Rossum33894be1992-01-27 16:53:09 +0000956}
957
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000958static char setattr_doc[] =
959"setattr(object, name, value)\n\
960\n\
961Set a named attribute on an object; setattr(x, 'y', v) is equivalent to\n\
962``x.y = v''.";
963
964
Guido van Rossum79f25d91997-04-29 20:08:16 +0000965static PyObject *
Guido van Rossum14144fc1994-08-29 12:53:40 +0000966builtin_delattr(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000967 PyObject *self;
968 PyObject *args;
Guido van Rossum14144fc1994-08-29 12:53:40 +0000969{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000970 PyObject *v;
971 PyObject *name;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000972
Guido van Rossum79f25d91997-04-29 20:08:16 +0000973 if (!PyArg_ParseTuple(args, "OS:delattr", &v, &name))
Guido van Rossum14144fc1994-08-29 12:53:40 +0000974 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000975 if (PyObject_SetAttr(v, name, (PyObject *)NULL) != 0)
Guido van Rossum14144fc1994-08-29 12:53:40 +0000976 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000977 Py_INCREF(Py_None);
978 return Py_None;
Guido van Rossum14144fc1994-08-29 12:53:40 +0000979}
980
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000981static char delattr_doc[] =
Guido van Rossumdf12a591998-11-23 22:13:04 +0000982"delattr(object, name)\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000983\n\
984Delete a named attribute on an object; delattr(x, 'y') is equivalent to\n\
985``del x.y''.";
986
987
Guido van Rossum79f25d91997-04-29 20:08:16 +0000988static PyObject *
Guido van Rossum9bfef441993-03-29 10:43:31 +0000989builtin_hash(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000990 PyObject *self;
991 PyObject *args;
Guido van Rossum9bfef441993-03-29 10:43:31 +0000992{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000993 PyObject *v;
Guido van Rossum9bfef441993-03-29 10:43:31 +0000994 long x;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000995
Guido van Rossum79f25d91997-04-29 20:08:16 +0000996 if (!PyArg_ParseTuple(args, "O:hash", &v))
Guido van Rossum9bfef441993-03-29 10:43:31 +0000997 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000998 x = PyObject_Hash(v);
Guido van Rossum9bfef441993-03-29 10:43:31 +0000999 if (x == -1)
1000 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001001 return PyInt_FromLong(x);
Guido van Rossum9bfef441993-03-29 10:43:31 +00001002}
1003
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001004static char hash_doc[] =
1005"hash(object) -> integer\n\
1006\n\
1007Return a hash value for the object. Two objects with the same value have\n\
1008the same hash value. The reverse is not necessarily true, but likely.";
1009
1010
Guido van Rossum79f25d91997-04-29 20:08:16 +00001011static PyObject *
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001012builtin_hex(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001013 PyObject *self;
1014 PyObject *args;
Guido van Rossum006bcd41991-10-24 14:54:44 +00001015{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001016 PyObject *v;
1017 PyNumberMethods *nb;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001018
Guido van Rossum79f25d91997-04-29 20:08:16 +00001019 if (!PyArg_ParseTuple(args, "O:hex", &v))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001020 return NULL;
Guido van Rossum1899c2e1992-09-12 11:09:23 +00001021
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001022 if ((nb = v->ob_type->tp_as_number) == NULL ||
Guido van Rossum1899c2e1992-09-12 11:09:23 +00001023 nb->nb_hex == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001024 PyErr_SetString(PyExc_TypeError,
Guido van Rossum1899c2e1992-09-12 11:09:23 +00001025 "hex() argument can't be converted to hex");
1026 return NULL;
Guido van Rossum006bcd41991-10-24 14:54:44 +00001027 }
Guido van Rossum1899c2e1992-09-12 11:09:23 +00001028 return (*nb->nb_hex)(v);
Guido van Rossum006bcd41991-10-24 14:54:44 +00001029}
1030
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001031static char hex_doc[] =
1032"hex(number) -> string\n\
1033\n\
1034Return the hexadecimal representation of an integer or long integer.";
1035
1036
Guido van Rossum79f25d91997-04-29 20:08:16 +00001037static PyObject *builtin_raw_input Py_PROTO((PyObject *, PyObject *));
Guido van Rossum3165fe61992-09-25 21:59:05 +00001038
Guido van Rossum79f25d91997-04-29 20:08:16 +00001039static PyObject *
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001040builtin_input(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001041 PyObject *self;
1042 PyObject *args;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001043{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001044 PyObject *line;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001045 char *str;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001046 PyObject *res;
1047 PyObject *globals, *locals;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001048
1049 line = builtin_raw_input(self, args);
Guido van Rossum3165fe61992-09-25 21:59:05 +00001050 if (line == NULL)
1051 return line;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001052 if (!PyArg_Parse(line, "s;embedded '\\0' in input line", &str))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001053 return NULL;
1054 while (*str == ' ' || *str == '\t')
1055 str++;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001056 globals = PyEval_GetGlobals();
1057 locals = PyEval_GetLocals();
1058 if (PyDict_GetItemString(globals, "__builtins__") == NULL) {
1059 if (PyDict_SetItemString(globals, "__builtins__",
1060 PyEval_GetBuiltins()) != 0)
Guido van Rossum6135a871995-01-09 17:53:26 +00001061 return NULL;
1062 }
Guido van Rossumb05a5c71997-05-07 17:46:13 +00001063 res = PyRun_String(str, Py_eval_input, globals, locals);
Guido van Rossum79f25d91997-04-29 20:08:16 +00001064 Py_DECREF(line);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001065 return res;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001066}
1067
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001068static char input_doc[] =
1069"input([prompt]) -> value\n\
1070\n\
1071Equivalent to eval(raw_input(prompt)).";
1072
1073
Guido van Rossume8811f81997-02-14 15:48:05 +00001074static PyObject *
1075builtin_intern(self, args)
1076 PyObject *self;
1077 PyObject *args;
1078{
1079 PyObject *s;
1080 if (!PyArg_ParseTuple(args, "S", &s))
1081 return NULL;
1082 Py_INCREF(s);
1083 PyString_InternInPlace(&s);
1084 return s;
1085}
1086
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001087static char intern_doc[] =
1088"intern(string) -> string\n\
1089\n\
1090``Intern'' the given string. This enters the string in the (global)\n\
1091table of interned strings whose purpose is to speed up dictionary lookups.\n\
1092Return the string itself or the previously interned string object with the\n\
1093same value.";
1094
1095
Guido van Rossum79f25d91997-04-29 20:08:16 +00001096static PyObject *
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001097builtin_int(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001098 PyObject *self;
1099 PyObject *args;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001100{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001101 PyObject *v;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001102
Guido van Rossum79f25d91997-04-29 20:08:16 +00001103 if (!PyArg_ParseTuple(args, "O:int", &v))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001104 return NULL;
Guido van Rossum09df08a1998-05-22 00:51:39 +00001105 return PyNumber_Int(v);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001106}
1107
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001108static char int_doc[] =
1109"int(x) -> integer\n\
1110\n\
1111Convert a string or number to an integer, if possible.\n\
1112A floating point argument will be truncated towards zero.";
1113
1114
Guido van Rossum79f25d91997-04-29 20:08:16 +00001115static PyObject *
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001116builtin_len(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001117 PyObject *self;
1118 PyObject *args;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001119{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001120 PyObject *v;
Guido van Rossum8ea9f4d1998-06-29 22:26:50 +00001121 long res;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001122
Guido van Rossum79f25d91997-04-29 20:08:16 +00001123 if (!PyArg_ParseTuple(args, "O:len", &v))
Guido van Rossum3f5da241990-12-20 15:06:42 +00001124 return NULL;
Guido van Rossum8ea9f4d1998-06-29 22:26:50 +00001125 res = PyObject_Length(v);
1126 if (res < 0 && PyErr_Occurred())
1127 return NULL;
1128 return PyInt_FromLong(res);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001129}
1130
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001131static char len_doc[] =
1132"len(object) -> integer\n\
1133\n\
1134Return the number of items of a sequence or mapping.";
1135
1136
Guido van Rossum79f25d91997-04-29 20:08:16 +00001137static PyObject *
Guido van Rossumd1705771996-04-09 02:41:06 +00001138builtin_list(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001139 PyObject *self;
1140 PyObject *args;
Guido van Rossumd1705771996-04-09 02:41:06 +00001141{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001142 PyObject *v;
Guido van Rossumd1705771996-04-09 02:41:06 +00001143
Guido van Rossum79f25d91997-04-29 20:08:16 +00001144 if (!PyArg_ParseTuple(args, "O:list", &v))
Guido van Rossumd1705771996-04-09 02:41:06 +00001145 return NULL;
Guido van Rossum09df08a1998-05-22 00:51:39 +00001146 return PySequence_List(v);
Guido van Rossumd1705771996-04-09 02:41:06 +00001147}
1148
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001149static char list_doc[] =
1150"list(sequence) -> list\n\
1151\n\
1152Return a new list whose items are the same as those of the argument sequence.";
1153
Guido van Rossum8861b741996-07-30 16:49:37 +00001154
1155static PyObject *
1156builtin_slice(self, args)
1157 PyObject *self;
1158 PyObject *args;
1159{
Guido van Rossum09df08a1998-05-22 00:51:39 +00001160 PyObject *start, *stop, *step;
Guido van Rossum8861b741996-07-30 16:49:37 +00001161
Guido van Rossum09df08a1998-05-22 00:51:39 +00001162 start = stop = step = NULL;
Guido van Rossum8861b741996-07-30 16:49:37 +00001163
Guido van Rossum09df08a1998-05-22 00:51:39 +00001164 if (!PyArg_ParseTuple(args, "O|OO:slice", &start, &stop, &step))
1165 return NULL;
Guido van Rossum8861b741996-07-30 16:49:37 +00001166
Guido van Rossum09df08a1998-05-22 00:51:39 +00001167 /* This swapping of stop and start is to maintain similarity with
1168 range(). */
1169 if (stop == NULL) {
1170 stop = start;
1171 start = NULL;
1172 }
1173 return PySlice_New(start, stop, step);
Guido van Rossum8861b741996-07-30 16:49:37 +00001174}
1175
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001176static char slice_doc[] =
1177"slice([start,] step[, stop]) -> slice object\n\
1178\n\
1179Create a slice object. This is used for slicing by the Numeric extensions.";
1180
1181
Guido van Rossum79f25d91997-04-29 20:08:16 +00001182static PyObject *
Guido van Rossum872537c1995-07-07 22:43:42 +00001183builtin_locals(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001184 PyObject *self;
1185 PyObject *args;
Guido van Rossum872537c1995-07-07 22:43:42 +00001186{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001187 PyObject *d;
Guido van Rossum872537c1995-07-07 22:43:42 +00001188
Guido van Rossum79f25d91997-04-29 20:08:16 +00001189 if (!PyArg_ParseTuple(args, ""))
Guido van Rossum872537c1995-07-07 22:43:42 +00001190 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001191 d = PyEval_GetLocals();
1192 Py_INCREF(d);
Guido van Rossum872537c1995-07-07 22:43:42 +00001193 return d;
1194}
1195
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001196static char locals_doc[] =
1197"locals() -> dictionary\n\
1198\n\
1199Return the dictionary containing the current scope's local variables.";
1200
1201
Guido van Rossum79f25d91997-04-29 20:08:16 +00001202static PyObject *
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001203builtin_long(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001204 PyObject *self;
1205 PyObject *args;
Guido van Rossumd4905451991-05-05 20:00:36 +00001206{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001207 PyObject *v;
Guido van Rossum1899c2e1992-09-12 11:09:23 +00001208
Guido van Rossum79f25d91997-04-29 20:08:16 +00001209 if (!PyArg_ParseTuple(args, "O:long", &v))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001210 return NULL;
Guido van Rossum09df08a1998-05-22 00:51:39 +00001211 return PyNumber_Long(v);
Guido van Rossumd4905451991-05-05 20:00:36 +00001212}
1213
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001214static char long_doc[] =
1215"long(x) -> long integer\n\
1216\n\
1217Convert a string or number to a long integer, if possible.\n\
1218A floating point argument will be truncated towards zero.";
1219
1220
Guido van Rossum79f25d91997-04-29 20:08:16 +00001221static PyObject *
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001222min_max(args, sign)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001223 PyObject *args;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001224 int sign;
1225{
Guido van Rossum2d951851994-08-29 12:52:16 +00001226 int i;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001227 PyObject *v, *w, *x;
1228 PySequenceMethods *sq;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001229
Guido van Rossum79f25d91997-04-29 20:08:16 +00001230 if (PyTuple_Size(args) > 1)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001231 v = args;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001232 else if (!PyArg_ParseTuple(args, "O:min/max", &v))
Guido van Rossum3f5da241990-12-20 15:06:42 +00001233 return NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001234 sq = v->ob_type->tp_as_sequence;
Guido van Rossum09df08a1998-05-22 00:51:39 +00001235 if (sq == NULL || sq->sq_item == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001236 PyErr_SetString(PyExc_TypeError,
1237 "min() or max() of non-sequence");
Guido van Rossum3f5da241990-12-20 15:06:42 +00001238 return NULL;
1239 }
Guido van Rossum2d951851994-08-29 12:52:16 +00001240 w = NULL;
1241 for (i = 0; ; i++) {
Guido van Rossum3f5da241990-12-20 15:06:42 +00001242 x = (*sq->sq_item)(v, i); /* Implies INCREF */
Guido van Rossum2d951851994-08-29 12:52:16 +00001243 if (x == NULL) {
Barry Warsawcde8b1b1997-08-22 21:14:38 +00001244 if (PyErr_ExceptionMatches(PyExc_IndexError)) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001245 PyErr_Clear();
Guido van Rossum2d951851994-08-29 12:52:16 +00001246 break;
1247 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00001248 Py_XDECREF(w);
Guido van Rossum2d951851994-08-29 12:52:16 +00001249 return NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001250 }
Guido van Rossum2d951851994-08-29 12:52:16 +00001251 if (w == NULL)
1252 w = x;
1253 else {
Guido van Rossumc8b6df91997-05-23 00:06:51 +00001254 int c = PyObject_Compare(x, w);
1255 if (c && PyErr_Occurred()) {
1256 Py_DECREF(x);
1257 Py_XDECREF(w);
1258 return NULL;
1259 }
1260 if (c * sign > 0) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001261 Py_DECREF(w);
Guido van Rossum2d951851994-08-29 12:52:16 +00001262 w = x;
1263 }
1264 else
Guido van Rossum79f25d91997-04-29 20:08:16 +00001265 Py_DECREF(x);
Guido van Rossum2d951851994-08-29 12:52:16 +00001266 }
Guido van Rossum3f5da241990-12-20 15:06:42 +00001267 }
Guido van Rossum2d951851994-08-29 12:52:16 +00001268 if (w == NULL)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001269 PyErr_SetString(PyExc_ValueError,
1270 "min() or max() of empty sequence");
Guido van Rossum3f5da241990-12-20 15:06:42 +00001271 return w;
1272}
1273
Guido van Rossum79f25d91997-04-29 20:08:16 +00001274static PyObject *
Guido van Rossum3f5da241990-12-20 15:06:42 +00001275builtin_min(self, v)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001276 PyObject *self;
1277 PyObject *v;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001278{
1279 return min_max(v, -1);
1280}
1281
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001282static char min_doc[] =
1283"min(sequence) -> value\n\
1284min(a, b, c, ...) -> value\n\
1285\n\
1286With a single sequence argument, return its smallest item.\n\
1287With two or more arguments, return the smallest argument.";
1288
1289
Guido van Rossum79f25d91997-04-29 20:08:16 +00001290static PyObject *
Guido van Rossum3f5da241990-12-20 15:06:42 +00001291builtin_max(self, v)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001292 PyObject *self;
1293 PyObject *v;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001294{
1295 return min_max(v, 1);
1296}
1297
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001298static char max_doc[] =
1299"max(sequence) -> value\n\
1300max(a, b, c, ...) -> value\n\
1301\n\
1302With a single sequence argument, return its largest item.\n\
1303With two or more arguments, return the largest argument.";
1304
1305
Guido van Rossum79f25d91997-04-29 20:08:16 +00001306static PyObject *
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001307builtin_oct(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001308 PyObject *self;
1309 PyObject *args;
Guido van Rossum006bcd41991-10-24 14:54:44 +00001310{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001311 PyObject *v;
1312 PyNumberMethods *nb;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001313
Guido van Rossum79f25d91997-04-29 20:08:16 +00001314 if (!PyArg_ParseTuple(args, "O:oct", &v))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001315 return NULL;
Guido van Rossum1899c2e1992-09-12 11:09:23 +00001316 if (v == NULL || (nb = v->ob_type->tp_as_number) == NULL ||
1317 nb->nb_oct == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001318 PyErr_SetString(PyExc_TypeError,
Guido van Rossum1899c2e1992-09-12 11:09:23 +00001319 "oct() argument can't be converted to oct");
1320 return NULL;
Guido van Rossum006bcd41991-10-24 14:54:44 +00001321 }
Guido van Rossum1899c2e1992-09-12 11:09:23 +00001322 return (*nb->nb_oct)(v);
Guido van Rossum006bcd41991-10-24 14:54:44 +00001323}
1324
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001325static char oct_doc[] =
1326"oct(number) -> string\n\
1327\n\
1328Return the octal representation of an integer or long integer.";
1329
1330
Guido van Rossum79f25d91997-04-29 20:08:16 +00001331static PyObject *
Guido van Rossum94390a41992-08-14 15:14:30 +00001332builtin_open(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001333 PyObject *self;
1334 PyObject *args;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001335{
Guido van Rossum2d951851994-08-29 12:52:16 +00001336 char *name;
1337 char *mode = "r";
1338 int bufsize = -1;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001339 PyObject *f;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001340
Guido van Rossum79f25d91997-04-29 20:08:16 +00001341 if (!PyArg_ParseTuple(args, "s|si:open", &name, &mode, &bufsize))
Guido van Rossum3f5da241990-12-20 15:06:42 +00001342 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001343 f = PyFile_FromString(name, mode);
Guido van Rossum2d951851994-08-29 12:52:16 +00001344 if (f != NULL)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001345 PyFile_SetBufSize(f, bufsize);
Guido van Rossum2d951851994-08-29 12:52:16 +00001346 return f;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001347}
1348
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001349static char open_doc[] =
1350"open(filename[, mode[, buffering]]) -> file object\n\
1351\n\
1352Open a file. The mode can be 'r', 'w' or 'a' for reading (default),\n\
1353writing or appending. The file will be created if it doesn't exist\n\
1354when opened for writing or appending; it will be truncated when\n\
1355opened for writing. Add a 'b' to the mode for binary files.\n\
1356Add a '+' to the mode to allow simultaneous reading and writing.\n\
1357If the buffering argument is given, 0 means unbuffered, 1 means line\n\
1358buffered, and larger numbers specify the buffer size.";
1359
1360
Guido van Rossum79f25d91997-04-29 20:08:16 +00001361static PyObject *
Guido van Rossum94390a41992-08-14 15:14:30 +00001362builtin_ord(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001363 PyObject *self;
1364 PyObject *args;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001365{
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001366 char c;
1367
Guido van Rossum79f25d91997-04-29 20:08:16 +00001368 if (!PyArg_ParseTuple(args, "c:ord", &c))
Guido van Rossum3f5da241990-12-20 15:06:42 +00001369 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001370 return PyInt_FromLong((long)(c & 0xff));
Guido van Rossum3f5da241990-12-20 15:06:42 +00001371}
1372
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001373static char ord_doc[] =
1374"ord(c) -> integer\n\
1375\n\
1376Return the integer ordinal of a one character string.";
1377
1378
Guido van Rossum79f25d91997-04-29 20:08:16 +00001379static PyObject *
Guido van Rossum6a00cd81995-01-07 12:39:01 +00001380builtin_pow(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001381 PyObject *self;
1382 PyObject *args;
Guido van Rossum6a00cd81995-01-07 12:39:01 +00001383{
Guido van Rossum09df08a1998-05-22 00:51:39 +00001384 PyObject *v, *w, *z = Py_None;
Guido van Rossum6a00cd81995-01-07 12:39:01 +00001385
Guido van Rossum79f25d91997-04-29 20:08:16 +00001386 if (!PyArg_ParseTuple(args, "OO|O:pow", &v, &w, &z))
Guido van Rossum6a00cd81995-01-07 12:39:01 +00001387 return NULL;
Guido van Rossum09df08a1998-05-22 00:51:39 +00001388 return PyNumber_Power(v, w, z);
Guido van Rossumd4905451991-05-05 20:00:36 +00001389}
1390
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001391static char pow_doc[] =
1392"pow(x, y[, z]) -> number\n\
1393\n\
1394With two arguments, equivalent to x**y. With three arguments,\n\
1395equivalent to (x**y) % z, but may be more efficient (e.g. for longs).";
1396
1397
Guido van Rossum79f25d91997-04-29 20:08:16 +00001398static PyObject *
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001399builtin_range(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001400 PyObject *self;
1401 PyObject *args;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001402{
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001403 long ilow = 0, ihigh = 0, istep = 1;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001404 int i, n;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001405 PyObject *v;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001406
Guido van Rossum79f25d91997-04-29 20:08:16 +00001407 if (PyTuple_Size(args) <= 1) {
1408 if (!PyArg_ParseTuple(args,
Guido van Rossum0865dd91995-01-17 16:30:22 +00001409 "l;range() requires 1-3 int arguments",
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001410 &ihigh))
1411 return NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001412 }
1413 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001414 if (!PyArg_ParseTuple(args,
Guido van Rossum0865dd91995-01-17 16:30:22 +00001415 "ll|l;range() requires 1-3 int arguments",
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001416 &ilow, &ihigh, &istep))
Guido van Rossum3f5da241990-12-20 15:06:42 +00001417 return NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001418 }
1419 if (istep == 0) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001420 PyErr_SetString(PyExc_ValueError, "zero step for range()");
Guido van Rossum3f5da241990-12-20 15:06:42 +00001421 return NULL;
1422 }
Guido van Rossume23cde21999-01-12 05:07:47 +00001423 /* A bit convoluted because this might overflow; due to Tim Peters */
1424 if (istep > 0) {
1425 if (ihigh <= ilow)
1426 n = 0;
1427 else {
1428 unsigned long hi = (unsigned long)ihigh;
1429 unsigned long lo = (unsigned long)ilow;
1430 unsigned long diff = hi - lo - 1;
1431 n = (long)(diff / istep + 1);
1432 }
1433 }
1434 else {
1435 /* But any errors in this branch are my own --Guido */
1436 if (ihigh >= ilow)
1437 n = 0;
1438 else {
1439 /* Swap lo and hi; use abs(istep) */
1440 unsigned long hi = (unsigned long)ilow;
1441 unsigned long lo = (unsigned long)ihigh;
1442 unsigned long diff = hi - lo - 1;
1443 n = (long)(diff / (-istep) + 1);
1444 }
1445 }
1446 if (n < 0) {
1447 PyErr_SetString(PyExc_OverflowError,
1448 "range() has more than sys.maxint items");
1449 return NULL;
1450 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00001451 v = PyList_New(n);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001452 if (v == NULL)
1453 return NULL;
1454 for (i = 0; i < n; i++) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001455 PyObject *w = PyInt_FromLong(ilow);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001456 if (w == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001457 Py_DECREF(v);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001458 return NULL;
1459 }
Guido van Rossuma937d141998-04-24 18:22:02 +00001460 PyList_SET_ITEM(v, i, w);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001461 ilow += istep;
1462 }
1463 return v;
1464}
1465
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001466static char range_doc[] =
1467"range([start,] stop[, step]) -> list of integers\n\
1468\n\
1469Return a list containing an arithmetic progression of integers.\n\
1470range(i, j) returns [i, i+1, i+2, ..., j-1]; start (!) defaults to 0.\n\
1471When step is given, it specifies the increment (or decrement).\n\
1472For example, range(4) returns [0, 1, 2, 3]. The end point is omitted!\n\
1473These are exactly the valid indices for a list of 4 elements.";
1474
1475
Guido van Rossum79f25d91997-04-29 20:08:16 +00001476static PyObject *
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001477builtin_xrange(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001478 PyObject *self;
1479 PyObject *args;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001480{
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001481 long ilow = 0, ihigh = 0, istep = 1;
Guido van Rossum0865dd91995-01-17 16:30:22 +00001482 long n;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001483
Guido van Rossum79f25d91997-04-29 20:08:16 +00001484 if (PyTuple_Size(args) <= 1) {
1485 if (!PyArg_ParseTuple(args,
Guido van Rossum0865dd91995-01-17 16:30:22 +00001486 "l;xrange() requires 1-3 int arguments",
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001487 &ihigh))
1488 return NULL;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001489 }
1490 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001491 if (!PyArg_ParseTuple(args,
Guido van Rossum0865dd91995-01-17 16:30:22 +00001492 "ll|l;xrange() requires 1-3 int arguments",
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001493 &ilow, &ihigh, &istep))
Guido van Rossum12d12c51993-10-26 17:58:25 +00001494 return NULL;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001495 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001496 if (istep == 0) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001497 PyErr_SetString(PyExc_ValueError, "zero step for xrange()");
Guido van Rossum12d12c51993-10-26 17:58:25 +00001498 return NULL;
1499 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001500 /* XXX ought to check overflow of subtraction */
1501 if (istep > 0)
1502 n = (ihigh - ilow + istep - 1) / istep;
1503 else
1504 n = (ihigh - ilow + istep + 1) / istep;
1505 if (n < 0)
1506 n = 0;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001507 return PyRange_New(ilow, n, istep, 1);
Guido van Rossum12d12c51993-10-26 17:58:25 +00001508}
1509
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001510static char xrange_doc[] =
1511"xrange([start,] stop[, step]) -> xrange object\n\
1512\n\
1513Like range(), but instead of returning a list, returns an object that\n\
1514generates the numbers in the range on demand. This is slightly slower\n\
1515than range() but more memory efficient.";
1516
1517
Guido van Rossum79f25d91997-04-29 20:08:16 +00001518static PyObject *
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001519builtin_raw_input(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001520 PyObject *self;
1521 PyObject *args;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001522{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001523 PyObject *v = NULL;
1524 PyObject *f;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001525
Guido van Rossum79f25d91997-04-29 20:08:16 +00001526 if (!PyArg_ParseTuple(args, "|O:[raw_]input", &v))
Guido van Rossum3165fe61992-09-25 21:59:05 +00001527 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001528 if (PyFile_AsFile(PySys_GetObject("stdin")) == stdin &&
1529 PyFile_AsFile(PySys_GetObject("stdout")) == stdout &&
Guido van Rossum53bb7ff1995-07-26 16:26:31 +00001530 isatty(fileno(stdin)) && isatty(fileno(stdout))) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001531 PyObject *po;
Guido van Rossum872537c1995-07-07 22:43:42 +00001532 char *prompt;
1533 char *s;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001534 PyObject *result;
Guido van Rossum872537c1995-07-07 22:43:42 +00001535 if (v != NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001536 po = PyObject_Str(v);
Guido van Rossum872537c1995-07-07 22:43:42 +00001537 if (po == NULL)
1538 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001539 prompt = PyString_AsString(po);
Guido van Rossumd9b52081998-06-26 18:25:38 +00001540 if (prompt == NULL)
1541 return NULL;
Guido van Rossum872537c1995-07-07 22:43:42 +00001542 }
1543 else {
1544 po = NULL;
1545 prompt = "";
1546 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00001547 s = PyOS_Readline(prompt);
1548 Py_XDECREF(po);
Guido van Rossum872537c1995-07-07 22:43:42 +00001549 if (s == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001550 PyErr_SetNone(PyExc_KeyboardInterrupt);
Guido van Rossum872537c1995-07-07 22:43:42 +00001551 return NULL;
1552 }
1553 if (*s == '\0') {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001554 PyErr_SetNone(PyExc_EOFError);
Guido van Rossum872537c1995-07-07 22:43:42 +00001555 result = NULL;
1556 }
1557 else { /* strip trailing '\n' */
Guido van Rossum79f25d91997-04-29 20:08:16 +00001558 result = PyString_FromStringAndSize(s, strlen(s)-1);
Guido van Rossum872537c1995-07-07 22:43:42 +00001559 }
1560 free(s);
1561 return result;
1562 }
Guido van Rossum90933611991-06-07 16:10:43 +00001563 if (v != NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001564 f = PySys_GetObject("stdout");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001565 if (f == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001566 PyErr_SetString(PyExc_RuntimeError, "lost sys.stdout");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001567 return NULL;
1568 }
Guido van Rossumc8b6df91997-05-23 00:06:51 +00001569 if (Py_FlushLine() != 0 ||
1570 PyFile_WriteObject(v, f, Py_PRINT_RAW) != 0)
Guido van Rossum90933611991-06-07 16:10:43 +00001571 return NULL;
1572 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00001573 f = PySys_GetObject("stdin");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001574 if (f == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001575 PyErr_SetString(PyExc_RuntimeError, "lost sys.stdin");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001576 return NULL;
1577 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00001578 return PyFile_GetLine(f, -1);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001579}
1580
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001581static char raw_input_doc[] =
1582"raw_input([prompt]) -> string\n\
1583\n\
1584Read a string from standard input. The trailing newline is stripped.\n\
1585If the user hits EOF (Unix: Ctl-D, Windows: Ctl-Z+Return), raise EOFError.\n\
1586On Unix, GNU readline is used if enabled. The prompt string, if given,\n\
1587is printed without a trailing newline before reading.";
1588
1589
Guido van Rossum79f25d91997-04-29 20:08:16 +00001590static PyObject *
Guido van Rossum12d12c51993-10-26 17:58:25 +00001591builtin_reduce(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001592 PyObject *self;
1593 PyObject *args;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001594{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001595 PyObject *seq, *func, *result = NULL;
1596 PySequenceMethods *sqf;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001597 register int i;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001598
Guido van Rossum79f25d91997-04-29 20:08:16 +00001599 if (!PyArg_ParseTuple(args, "OO|O:reduce", &func, &seq, &result))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001600 return NULL;
1601 if (result != NULL)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001602 Py_INCREF(result);
Guido van Rossum12d12c51993-10-26 17:58:25 +00001603
Guido van Rossum09df08a1998-05-22 00:51:39 +00001604 sqf = seq->ob_type->tp_as_sequence;
1605 if (sqf == NULL || sqf->sq_item == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001606 PyErr_SetString(PyExc_TypeError,
Guido van Rossum12d12c51993-10-26 17:58:25 +00001607 "2nd argument to reduce() must be a sequence object");
1608 return NULL;
1609 }
1610
Guido van Rossum79f25d91997-04-29 20:08:16 +00001611 if ((args = PyTuple_New(2)) == NULL)
Guido van Rossum2d951851994-08-29 12:52:16 +00001612 goto Fail;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001613
Guido van Rossum2d951851994-08-29 12:52:16 +00001614 for (i = 0; ; ++i) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001615 PyObject *op2;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001616
1617 if (args->ob_refcnt > 1) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001618 Py_DECREF(args);
1619 if ((args = PyTuple_New(2)) == NULL)
Guido van Rossum2d951851994-08-29 12:52:16 +00001620 goto Fail;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001621 }
1622
Guido van Rossum2d951851994-08-29 12:52:16 +00001623 if ((op2 = (*sqf->sq_item)(seq, i)) == NULL) {
Barry Warsawcde8b1b1997-08-22 21:14:38 +00001624 if (PyErr_ExceptionMatches(PyExc_IndexError)) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001625 PyErr_Clear();
Guido van Rossum2d951851994-08-29 12:52:16 +00001626 break;
1627 }
1628 goto Fail;
1629 }
Guido van Rossum12d12c51993-10-26 17:58:25 +00001630
Guido van Rossum2d951851994-08-29 12:52:16 +00001631 if (result == NULL)
1632 result = op2;
1633 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001634 PyTuple_SetItem(args, 0, result);
1635 PyTuple_SetItem(args, 1, op2);
1636 if ((result = PyEval_CallObject(func, args)) == NULL)
Guido van Rossum2d951851994-08-29 12:52:16 +00001637 goto Fail;
1638 }
Guido van Rossum12d12c51993-10-26 17:58:25 +00001639 }
1640
Guido van Rossum79f25d91997-04-29 20:08:16 +00001641 Py_DECREF(args);
Guido van Rossum12d12c51993-10-26 17:58:25 +00001642
Guido van Rossum2d951851994-08-29 12:52:16 +00001643 if (result == NULL)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001644 PyErr_SetString(PyExc_TypeError,
Guido van Rossum2d951851994-08-29 12:52:16 +00001645 "reduce of empty sequence with no initial value");
1646
Guido van Rossum12d12c51993-10-26 17:58:25 +00001647 return result;
1648
Guido van Rossum2d951851994-08-29 12:52:16 +00001649Fail:
Guido van Rossum79f25d91997-04-29 20:08:16 +00001650 Py_XDECREF(args);
1651 Py_XDECREF(result);
Guido van Rossum12d12c51993-10-26 17:58:25 +00001652 return NULL;
1653}
1654
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001655static char reduce_doc[] =
1656"reduce(function, sequence[, initial]) -> value\n\
1657\n\
1658Apply a function of two arguments cumulatively to the items of a sequence,\n\
1659from left to right, so as to reduce the sequence to a single value.\n\
1660For example, reduce(lambda x, y: x+y, [1, 2, 3, 4, 5]) calculates\n\
1661((((1+2)+3)+4)+5). If initial is present, it is placed before the items\n\
1662of the sequence in the calculation, and serves as a default when the\n\
1663sequence is empty.";
1664
1665
Guido van Rossum79f25d91997-04-29 20:08:16 +00001666static PyObject *
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001667builtin_reload(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001668 PyObject *self;
1669 PyObject *args;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001670{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001671 PyObject *v;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001672
Guido van Rossum79f25d91997-04-29 20:08:16 +00001673 if (!PyArg_ParseTuple(args, "O:reload", &v))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001674 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001675 return PyImport_ReloadModule(v);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001676}
1677
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001678static char reload_doc[] =
1679"reload(module) -> module\n\
1680\n\
1681Reload the module. The module must have been successfully imported before.";
1682
1683
Guido van Rossum79f25d91997-04-29 20:08:16 +00001684static PyObject *
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001685builtin_repr(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001686 PyObject *self;
1687 PyObject *args;
Guido van Rossumc89705d1992-11-26 08:54:07 +00001688{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001689 PyObject *v;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001690
Guido van Rossum79f25d91997-04-29 20:08:16 +00001691 if (!PyArg_ParseTuple(args, "O:repr", &v))
Guido van Rossumc89705d1992-11-26 08:54:07 +00001692 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001693 return PyObject_Repr(v);
Guido van Rossumc89705d1992-11-26 08:54:07 +00001694}
1695
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001696static char repr_doc[] =
1697"repr(object) -> string\n\
1698\n\
1699Return the canonical string representation of the object.\n\
1700For most object types, eval(repr(object)) == object.";
1701
1702
Guido van Rossum79f25d91997-04-29 20:08:16 +00001703static PyObject *
Guido van Rossum9e51f9b1993-02-12 16:29:05 +00001704builtin_round(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001705 PyObject *self;
1706 PyObject *args;
Guido van Rossum9e51f9b1993-02-12 16:29:05 +00001707{
Guido van Rossum9e51f9b1993-02-12 16:29:05 +00001708 double x;
1709 double f;
1710 int ndigits = 0;
Guido van Rossum9e51f9b1993-02-12 16:29:05 +00001711 int i;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001712
Guido van Rossum79f25d91997-04-29 20:08:16 +00001713 if (!PyArg_ParseTuple(args, "d|i:round", &x, &ndigits))
Guido van Rossum9e51f9b1993-02-12 16:29:05 +00001714 return NULL;
Guido van Rossum9e51f9b1993-02-12 16:29:05 +00001715 f = 1.0;
Guido van Rossum1e162d31998-05-09 14:42:25 +00001716 i = abs(ndigits);
1717 while (--i >= 0)
Guido van Rossum9e51f9b1993-02-12 16:29:05 +00001718 f = f*10.0;
Guido van Rossum1e162d31998-05-09 14:42:25 +00001719 if (ndigits < 0)
1720 x /= f;
Guido van Rossum9e51f9b1993-02-12 16:29:05 +00001721 else
Guido van Rossum1e162d31998-05-09 14:42:25 +00001722 x *= f;
1723 if (x >= 0.0)
1724 x = floor(x + 0.5);
1725 else
1726 x = ceil(x - 0.5);
1727 if (ndigits < 0)
1728 x *= f;
1729 else
1730 x /= f;
1731 return PyFloat_FromDouble(x);
Guido van Rossum9e51f9b1993-02-12 16:29:05 +00001732}
1733
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001734static char round_doc[] =
1735"round(number[, ndigits]) -> floating point number\n\
1736\n\
1737Round a number to a given precision in decimal digits (default 0 digits).\n\
1738This always returns a floating point number. Precision may be negative.";
1739
1740
Guido van Rossum79f25d91997-04-29 20:08:16 +00001741static PyObject *
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001742builtin_str(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001743 PyObject *self;
1744 PyObject *args;
Guido van Rossumc89705d1992-11-26 08:54:07 +00001745{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001746 PyObject *v;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001747
Guido van Rossum79f25d91997-04-29 20:08:16 +00001748 if (!PyArg_ParseTuple(args, "O:str", &v))
Guido van Rossumc89705d1992-11-26 08:54:07 +00001749 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001750 return PyObject_Str(v);
Guido van Rossumc89705d1992-11-26 08:54:07 +00001751}
1752
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001753static char str_doc[] =
1754"str(object) -> string\n\
1755\n\
1756Return a nice string representation of the object.\n\
1757If the argument is a string, the return value is the same object.";
1758
1759
Guido van Rossum79f25d91997-04-29 20:08:16 +00001760static PyObject *
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001761builtin_tuple(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001762 PyObject *self;
1763 PyObject *args;
Guido van Rossumcae027b1994-08-29 12:53:11 +00001764{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001765 PyObject *v;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001766
Guido van Rossum79f25d91997-04-29 20:08:16 +00001767 if (!PyArg_ParseTuple(args, "O:tuple", &v))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001768 return NULL;
Guido van Rossum09df08a1998-05-22 00:51:39 +00001769 return PySequence_Tuple(v);
Guido van Rossumcae027b1994-08-29 12:53:11 +00001770}
1771
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001772static char tuple_doc[] =
1773"tuple(sequence) -> list\n\
1774\n\
1775Return a tuple whose items are the same as those of the argument sequence.\n\
1776If the argument is a tuple, the return value is the same object.";
1777
1778
Guido van Rossum79f25d91997-04-29 20:08:16 +00001779static PyObject *
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001780builtin_type(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001781 PyObject *self;
1782 PyObject *args;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001783{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001784 PyObject *v;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001785
Guido van Rossum79f25d91997-04-29 20:08:16 +00001786 if (!PyArg_ParseTuple(args, "O:type", &v))
Guido van Rossum3f5da241990-12-20 15:06:42 +00001787 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001788 v = (PyObject *)v->ob_type;
1789 Py_INCREF(v);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001790 return v;
1791}
1792
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001793static char type_doc[] =
1794"type(object) -> type object\n\
1795\n\
1796Return the type of the object.";
1797
1798
Guido van Rossum79f25d91997-04-29 20:08:16 +00001799static PyObject *
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001800builtin_vars(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001801 PyObject *self;
1802 PyObject *args;
Guido van Rossum2d951851994-08-29 12:52:16 +00001803{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001804 PyObject *v = NULL;
1805 PyObject *d;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001806
Guido van Rossum79f25d91997-04-29 20:08:16 +00001807 if (!PyArg_ParseTuple(args, "|O:vars", &v))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001808 return NULL;
Guido van Rossum2d951851994-08-29 12:52:16 +00001809 if (v == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001810 d = PyEval_GetLocals();
Guido van Rossum53bb7ff1995-07-26 16:26:31 +00001811 if (d == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001812 if (!PyErr_Occurred())
1813 PyErr_SetString(PyExc_SystemError,
1814 "no locals!?");
Guido van Rossum53bb7ff1995-07-26 16:26:31 +00001815 }
1816 else
Guido van Rossum79f25d91997-04-29 20:08:16 +00001817 Py_INCREF(d);
Guido van Rossum2d951851994-08-29 12:52:16 +00001818 }
1819 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001820 d = PyObject_GetAttrString(v, "__dict__");
Guido van Rossum2d951851994-08-29 12:52:16 +00001821 if (d == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001822 PyErr_SetString(PyExc_TypeError,
Guido van Rossum2d951851994-08-29 12:52:16 +00001823 "vars() argument must have __dict__ attribute");
1824 return NULL;
1825 }
1826 }
1827 return d;
1828}
1829
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001830static char vars_doc[] =
1831"vars([object]) -> dictionary\n\
1832\n\
1833Without arguments, equivalent to locals().\n\
1834With an argument, equivalent to object.__dict__.";
1835
1836
Barry Warsawcde8b1b1997-08-22 21:14:38 +00001837static PyObject *
1838builtin_isinstance(self, args)
1839 PyObject *self;
1840 PyObject *args;
1841{
1842 PyObject *inst;
1843 PyObject *cls;
1844 int retval;
1845
1846 if (!PyArg_ParseTuple(args, "OO", &inst, &cls))
1847 return NULL;
Guido van Rossumf5dd9141997-12-02 19:11:45 +00001848 if (PyType_Check(cls)) {
Guido van Rossumd6af46d1997-12-10 05:51:47 +00001849 retval = ((PyObject *)(inst->ob_type) == cls);
Barry Warsawcde8b1b1997-08-22 21:14:38 +00001850 }
Barry Warsawcde8b1b1997-08-22 21:14:38 +00001851 else {
Guido van Rossumf5dd9141997-12-02 19:11:45 +00001852 if (!PyClass_Check(cls)) {
1853 PyErr_SetString(PyExc_TypeError,
1854 "second argument must be a class");
1855 return NULL;
1856 }
1857
1858 if (!PyInstance_Check(inst))
1859 retval = 0;
1860 else {
1861 PyObject *inclass =
1862 (PyObject*)((PyInstanceObject*)inst)->in_class;
1863 retval = PyClass_IsSubclass(inclass, cls);
1864 }
Barry Warsawcde8b1b1997-08-22 21:14:38 +00001865 }
1866 return PyInt_FromLong(retval);
1867}
1868
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001869static char isinstance_doc[] =
1870"isinstance(object, class-or-type) -> Boolean\n\
1871\n\
1872Return whether an object is an instance of a class or of a subclass thereof.\n\
1873With a type as second argument, return whether that is the object's type.";
1874
Barry Warsawcde8b1b1997-08-22 21:14:38 +00001875
1876static PyObject *
1877builtin_issubclass(self, args)
1878 PyObject *self;
1879 PyObject *args;
1880{
1881 PyObject *derived;
1882 PyObject *cls;
1883 int retval;
1884
1885 if (!PyArg_ParseTuple(args, "OO", &derived, &cls))
1886 return NULL;
1887 if (!PyClass_Check(derived) || !PyClass_Check(cls)) {
1888 PyErr_SetString(PyExc_TypeError, "arguments must be classes");
1889 return NULL;
1890 }
1891 /* shortcut */
1892 if (!(retval = (derived == cls)))
1893 retval = PyClass_IsSubclass(derived, cls);
1894
1895 return PyInt_FromLong(retval);
1896}
1897
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001898static char issubclass_doc[] =
1899"issubclass(C, B) -> Boolean\n\
1900\n\
1901Return whether class C is a subclass (i.e., a derived class) of class B.";
1902
Barry Warsawcde8b1b1997-08-22 21:14:38 +00001903
Guido van Rossum79f25d91997-04-29 20:08:16 +00001904static PyMethodDef builtin_methods[] = {
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001905 {"__import__", builtin___import__, 1, import_doc},
1906 {"abs", builtin_abs, 1, abs_doc},
1907 {"apply", builtin_apply, 1, apply_doc},
1908 {"callable", builtin_callable, 1, callable_doc},
1909 {"chr", builtin_chr, 1, chr_doc},
1910 {"cmp", builtin_cmp, 1, cmp_doc},
1911 {"coerce", builtin_coerce, 1, coerce_doc},
1912 {"compile", builtin_compile, 1, compile_doc},
Guido van Rossum8a5c5d21996-01-12 01:09:56 +00001913#ifndef WITHOUT_COMPLEX
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001914 {"complex", builtin_complex, 1, complex_doc},
Guido van Rossum8a5c5d21996-01-12 01:09:56 +00001915#endif
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001916 {"delattr", builtin_delattr, 1, delattr_doc},
1917 {"dir", builtin_dir, 1, dir_doc},
1918 {"divmod", builtin_divmod, 1, divmod_doc},
1919 {"eval", builtin_eval, 1, eval_doc},
1920 {"execfile", builtin_execfile, 1, execfile_doc},
1921 {"filter", builtin_filter, 1, filter_doc},
1922 {"float", builtin_float, 1, float_doc},
1923 {"getattr", builtin_getattr, 1, getattr_doc},
1924 {"globals", builtin_globals, 1, globals_doc},
1925 {"hasattr", builtin_hasattr, 1, hasattr_doc},
1926 {"hash", builtin_hash, 1, hash_doc},
1927 {"hex", builtin_hex, 1, hex_doc},
1928 {"id", builtin_id, 1, id_doc},
1929 {"input", builtin_input, 1, input_doc},
1930 {"intern", builtin_intern, 1, intern_doc},
1931 {"int", builtin_int, 1, int_doc},
1932 {"isinstance", builtin_isinstance, 1, isinstance_doc},
1933 {"issubclass", builtin_issubclass, 1, issubclass_doc},
1934 {"len", builtin_len, 1, len_doc},
1935 {"list", builtin_list, 1, list_doc},
1936 {"locals", builtin_locals, 1, locals_doc},
1937 {"long", builtin_long, 1, long_doc},
1938 {"map", builtin_map, 1, map_doc},
1939 {"max", builtin_max, 1, max_doc},
1940 {"min", builtin_min, 1, min_doc},
1941 {"oct", builtin_oct, 1, oct_doc},
1942 {"open", builtin_open, 1, open_doc},
1943 {"ord", builtin_ord, 1, ord_doc},
1944 {"pow", builtin_pow, 1, pow_doc},
1945 {"range", builtin_range, 1, range_doc},
1946 {"raw_input", builtin_raw_input, 1, raw_input_doc},
1947 {"reduce", builtin_reduce, 1, reduce_doc},
1948 {"reload", builtin_reload, 1, reload_doc},
1949 {"repr", builtin_repr, 1, repr_doc},
1950 {"round", builtin_round, 1, round_doc},
1951 {"setattr", builtin_setattr, 1, setattr_doc},
1952 {"slice", builtin_slice, 1, slice_doc},
1953 {"str", builtin_str, 1, str_doc},
1954 {"tuple", builtin_tuple, 1, tuple_doc},
1955 {"type", builtin_type, 1, type_doc},
1956 {"vars", builtin_vars, 1, vars_doc},
1957 {"xrange", builtin_xrange, 1, xrange_doc},
Guido van Rossumc02e15c1991-12-16 13:03:00 +00001958 {NULL, NULL},
Guido van Rossum3f5da241990-12-20 15:06:42 +00001959};
1960
Guido van Rossum3f5da241990-12-20 15:06:42 +00001961/* Predefined exceptions */
1962
Guido van Rossum04748321997-09-16 18:43:15 +00001963PyObject *PyExc_Exception;
Barry Warsaw757af0e1997-08-29 22:13:51 +00001964PyObject *PyExc_StandardError;
Barry Warsaw412cdc21997-09-16 21:51:14 +00001965PyObject *PyExc_ArithmeticError;
Barry Warsaw757af0e1997-08-29 22:13:51 +00001966PyObject *PyExc_LookupError;
1967
Guido van Rossum79f25d91997-04-29 20:08:16 +00001968PyObject *PyExc_AssertionError;
1969PyObject *PyExc_AttributeError;
1970PyObject *PyExc_EOFError;
Guido van Rossumb6a7f771997-05-09 03:03:23 +00001971PyObject *PyExc_FloatingPointError;
Barry Warsawd086a1a1998-07-23 15:59:57 +00001972PyObject *PyExc_EnvironmentError;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001973PyObject *PyExc_IOError;
Barry Warsawd086a1a1998-07-23 15:59:57 +00001974PyObject *PyExc_OSError;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001975PyObject *PyExc_ImportError;
1976PyObject *PyExc_IndexError;
1977PyObject *PyExc_KeyError;
1978PyObject *PyExc_KeyboardInterrupt;
1979PyObject *PyExc_MemoryError;
1980PyObject *PyExc_NameError;
1981PyObject *PyExc_OverflowError;
1982PyObject *PyExc_RuntimeError;
Barry Warsaw344864f1998-12-01 18:52:06 +00001983PyObject *PyExc_NotImplementedError;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001984PyObject *PyExc_SyntaxError;
1985PyObject *PyExc_SystemError;
1986PyObject *PyExc_SystemExit;
1987PyObject *PyExc_TypeError;
1988PyObject *PyExc_ValueError;
1989PyObject *PyExc_ZeroDivisionError;
Guido van Rossum50afb7a1991-12-10 13:52:31 +00001990
Barry Warsaw757af0e1997-08-29 22:13:51 +00001991PyObject *PyExc_MemoryErrorInst;
1992
1993static struct
1994{
1995 char* name;
1996 PyObject** exc;
1997 int leaf_exc;
1998}
1999bltin_exc[] = {
Guido van Rossum04748321997-09-16 18:43:15 +00002000 {"Exception", &PyExc_Exception, 0},
Barry Warsaw757af0e1997-08-29 22:13:51 +00002001 {"StandardError", &PyExc_StandardError, 0},
Barry Warsaw412cdc21997-09-16 21:51:14 +00002002 {"ArithmeticError", &PyExc_ArithmeticError, 0},
Barry Warsaw757af0e1997-08-29 22:13:51 +00002003 {"LookupError", &PyExc_LookupError, 0},
2004 {"AssertionError", &PyExc_AssertionError, 1},
2005 {"AttributeError", &PyExc_AttributeError, 1},
2006 {"EOFError", &PyExc_EOFError, 1},
2007 {"FloatingPointError", &PyExc_FloatingPointError, 1},
Barry Warsawd086a1a1998-07-23 15:59:57 +00002008 {"EnvironmentError", &PyExc_EnvironmentError, 1},
Barry Warsaw757af0e1997-08-29 22:13:51 +00002009 {"IOError", &PyExc_IOError, 1},
Barry Warsawd086a1a1998-07-23 15:59:57 +00002010 {"OSError", &PyExc_OSError, 1},
Barry Warsaw757af0e1997-08-29 22:13:51 +00002011 {"ImportError", &PyExc_ImportError, 1},
2012 {"IndexError", &PyExc_IndexError, 1},
2013 {"KeyError", &PyExc_KeyError, 1},
2014 {"KeyboardInterrupt", &PyExc_KeyboardInterrupt, 1},
2015 {"MemoryError", &PyExc_MemoryError, 1},
2016 {"NameError", &PyExc_NameError, 1},
2017 {"OverflowError", &PyExc_OverflowError, 1},
2018 {"RuntimeError", &PyExc_RuntimeError, 1},
Barry Warsaw344864f1998-12-01 18:52:06 +00002019 {"NotImplementedError",&PyExc_NotImplementedError,1},
Barry Warsaw757af0e1997-08-29 22:13:51 +00002020 {"SyntaxError", &PyExc_SyntaxError, 1},
2021 {"SystemError", &PyExc_SystemError, 1},
2022 {"SystemExit", &PyExc_SystemExit, 1},
2023 {"TypeError", &PyExc_TypeError, 1},
2024 {"ValueError", &PyExc_ValueError, 1},
2025 {"ZeroDivisionError", &PyExc_ZeroDivisionError, 1},
2026 {NULL, NULL}
2027};
2028
2029
Barry Warsaw98b62461998-09-14 18:51:11 +00002030/* import exceptions module to extract class exceptions. on success,
2031 * return 1. on failure return 0 which signals _PyBuiltin_Init_2 to fall
2032 * back to using old-style string based exceptions.
2033 */
2034static int
Barry Warsaw757af0e1997-08-29 22:13:51 +00002035init_class_exc(dict)
2036 PyObject *dict;
2037{
2038 int i;
2039 PyObject *m = PyImport_ImportModule("exceptions");
Barry Warsaw98b62461998-09-14 18:51:11 +00002040 PyObject *args = NULL;
2041 PyObject *d = NULL;
Barry Warsaw757af0e1997-08-29 22:13:51 +00002042
Barry Warsaw98b62461998-09-14 18:51:11 +00002043 /* make sure we got the module and its dictionary */
Barry Warsaw757af0e1997-08-29 22:13:51 +00002044 if (m == NULL ||
2045 (d = PyModule_GetDict(m)) == NULL)
2046 {
Barry Warsaw98b62461998-09-14 18:51:11 +00002047 PySys_WriteStderr("'import exceptions' failed; ");
Barry Warsaw757af0e1997-08-29 22:13:51 +00002048 if (Py_VerboseFlag) {
Barry Warsaw98b62461998-09-14 18:51:11 +00002049 PySys_WriteStderr("traceback:\n");
Barry Warsaw757af0e1997-08-29 22:13:51 +00002050 PyErr_Print();
2051 }
2052 else {
Barry Warsaw98b62461998-09-14 18:51:11 +00002053 PySys_WriteStderr("use -v for traceback\n");
Barry Warsaw757af0e1997-08-29 22:13:51 +00002054 }
Barry Warsaw98b62461998-09-14 18:51:11 +00002055 goto finally;
Barry Warsaw757af0e1997-08-29 22:13:51 +00002056 }
2057 for (i = 0; bltin_exc[i].name; i++) {
2058 /* dig the exception out of the module */
2059 PyObject *exc = PyDict_GetItemString(d, bltin_exc[i].name);
Barry Warsaw98b62461998-09-14 18:51:11 +00002060 if (!exc) {
2061 PySys_WriteStderr(
2062 "Built-in exception class not found: %s. Library mismatch?\n",
2063 bltin_exc[i].name);
2064 goto finally;
2065 }
2066 /* free the old-style exception string object */
Barry Warsaw757af0e1997-08-29 22:13:51 +00002067 Py_XDECREF(*bltin_exc[i].exc);
2068
2069 /* squirrel away a pointer to the exception */
2070 Py_INCREF(exc);
2071 *bltin_exc[i].exc = exc;
2072
2073 /* and insert the name in the __builtin__ module */
Barry Warsaw98b62461998-09-14 18:51:11 +00002074 if (PyDict_SetItemString(dict, bltin_exc[i].name, exc)) {
2075 PySys_WriteStderr(
2076 "Cannot insert exception into __builtin__: %s\n",
2077 bltin_exc[i].name);
2078 goto finally;
2079 }
Barry Warsaw757af0e1997-08-29 22:13:51 +00002080 }
2081
2082 /* we need one pre-allocated instance */
2083 args = Py_BuildValue("()");
Barry Warsaw98b62461998-09-14 18:51:11 +00002084 if (!args ||
2085 !(PyExc_MemoryErrorInst =
2086 PyEval_CallObject(PyExc_MemoryError, args)))
2087 {
2088 PySys_WriteStderr("Cannot pre-allocate MemoryError instance\n");
2089 goto finally;
Barry Warsaw757af0e1997-08-29 22:13:51 +00002090 }
Barry Warsaw98b62461998-09-14 18:51:11 +00002091 Py_DECREF(args);
Barry Warsaw757af0e1997-08-29 22:13:51 +00002092
2093 /* we're done with the exceptions module */
2094 Py_DECREF(m);
2095
Barry Warsaw98b62461998-09-14 18:51:11 +00002096 if (PyErr_Occurred()) {
2097 PySys_WriteStderr("Cannot initialize standard class exceptions; ");
2098 if (Py_VerboseFlag) {
2099 PySys_WriteStderr("traceback:\n");
2100 PyErr_Print();
2101 }
2102 else
2103 PySys_WriteStderr("use -v for traceback\n");
2104 goto finally;
2105 }
2106 return 1;
2107 finally:
2108 Py_XDECREF(m);
2109 Py_XDECREF(args);
2110 PyErr_Clear();
2111 return 0;
Barry Warsaw757af0e1997-08-29 22:13:51 +00002112}
2113
2114
2115static void
2116fini_instances()
2117{
2118 Py_XDECREF(PyExc_MemoryErrorInst);
2119 PyExc_MemoryErrorInst = NULL;
2120}
2121
2122
Guido van Rossum79f25d91997-04-29 20:08:16 +00002123static PyObject *
Guido van Rossum25ce5661997-08-02 03:10:38 +00002124newstdexception(dict, name)
2125 PyObject *dict;
Guido van Rossumfb905c31991-12-16 15:42:38 +00002126 char *name;
Guido van Rossum3f5da241990-12-20 15:06:42 +00002127{
Guido van Rossum79f25d91997-04-29 20:08:16 +00002128 PyObject *v = PyString_FromString(name);
Guido van Rossum25ce5661997-08-02 03:10:38 +00002129 if (v == NULL || PyDict_SetItemString(dict, name, v) != 0)
Barry Warsaw98b62461998-09-14 18:51:11 +00002130 Py_FatalError("Cannot create string-based exceptions");
Guido van Rossum3f5da241990-12-20 15:06:42 +00002131 return v;
2132}
2133
2134static void
Guido van Rossum25ce5661997-08-02 03:10:38 +00002135initerrors(dict)
2136 PyObject *dict;
Guido van Rossum3f5da241990-12-20 15:06:42 +00002137{
Barry Warsaw757af0e1997-08-29 22:13:51 +00002138 int i;
2139 int exccnt = 0;
2140 for (i = 0; bltin_exc[i].name; i++, exccnt++) {
Barry Warsaw98b62461998-09-14 18:51:11 +00002141 Py_XDECREF(*bltin_exc[i].exc);
Barry Warsaw757af0e1997-08-29 22:13:51 +00002142 if (bltin_exc[i].leaf_exc)
2143 *bltin_exc[i].exc =
2144 newstdexception(dict, bltin_exc[i].name);
2145 }
2146
Barry Warsawd086a1a1998-07-23 15:59:57 +00002147 /* This is kind of bogus because we special case the some of the
2148 * new exceptions to be nearly forward compatible. But this means
2149 * we hard code knowledge about exceptions.py into C here. I don't
2150 * have a better solution, though.
2151 */
Barry Warsaw757af0e1997-08-29 22:13:51 +00002152 PyExc_LookupError = PyTuple_New(2);
2153 Py_INCREF(PyExc_IndexError);
2154 PyTuple_SET_ITEM(PyExc_LookupError, 0, PyExc_IndexError);
2155 Py_INCREF(PyExc_KeyError);
2156 PyTuple_SET_ITEM(PyExc_LookupError, 1, PyExc_KeyError);
2157 PyDict_SetItemString(dict, "LookupError", PyExc_LookupError);
2158
Barry Warsaw412cdc21997-09-16 21:51:14 +00002159 PyExc_ArithmeticError = PyTuple_New(3);
Barry Warsaw757af0e1997-08-29 22:13:51 +00002160 Py_INCREF(PyExc_OverflowError);
Barry Warsaw412cdc21997-09-16 21:51:14 +00002161 PyTuple_SET_ITEM(PyExc_ArithmeticError, 0, PyExc_OverflowError);
Barry Warsaw757af0e1997-08-29 22:13:51 +00002162 Py_INCREF(PyExc_ZeroDivisionError);
Barry Warsaw412cdc21997-09-16 21:51:14 +00002163 PyTuple_SET_ITEM(PyExc_ArithmeticError, 1, PyExc_ZeroDivisionError);
Barry Warsaw757af0e1997-08-29 22:13:51 +00002164 Py_INCREF(PyExc_FloatingPointError);
Barry Warsaw412cdc21997-09-16 21:51:14 +00002165 PyTuple_SET_ITEM(PyExc_ArithmeticError, 2, PyExc_FloatingPointError);
2166 PyDict_SetItemString(dict, "ArithmeticError", PyExc_ArithmeticError);
Barry Warsaw757af0e1997-08-29 22:13:51 +00002167
Barry Warsawd086a1a1998-07-23 15:59:57 +00002168 PyExc_EnvironmentError = PyTuple_New(2);
2169 Py_INCREF(PyExc_IOError);
2170 PyTuple_SET_ITEM(PyExc_EnvironmentError, 0, PyExc_IOError);
2171 Py_INCREF(PyExc_OSError);
2172 PyTuple_SET_ITEM(PyExc_EnvironmentError, 1, PyExc_OSError);
2173 PyDict_SetItemString(dict, "EnvironmentError", PyExc_EnvironmentError);
2174
Barry Warsawb01a7fa1997-09-18 03:44:38 +00002175 PyExc_StandardError = PyTuple_New(exccnt-2);
2176 for (i = 2; bltin_exc[i].name; i++) {
Barry Warsaw757af0e1997-08-29 22:13:51 +00002177 PyObject *exc = *bltin_exc[i].exc;
2178 Py_INCREF(exc);
Barry Warsawb01a7fa1997-09-18 03:44:38 +00002179 PyTuple_SET_ITEM(PyExc_StandardError, i-2, exc);
Barry Warsaw757af0e1997-08-29 22:13:51 +00002180 }
2181 PyDict_SetItemString(dict, "StandardError", PyExc_StandardError);
Guido van Rossum04748321997-09-16 18:43:15 +00002182
2183 /* Exception is treated differently; for now, it's == StandardError */
2184 PyExc_Exception = PyExc_StandardError;
2185 Py_INCREF(PyExc_Exception);
2186 PyDict_SetItemString(dict, "Exception", PyExc_Exception);
Barry Warsaw757af0e1997-08-29 22:13:51 +00002187
2188 if (PyErr_Occurred())
2189 Py_FatalError("Could not initialize built-in string exceptions");
Guido van Rossum25ce5661997-08-02 03:10:38 +00002190}
2191
2192static void
2193finierrors()
2194{
Barry Warsaw757af0e1997-08-29 22:13:51 +00002195 int i;
2196 for (i = 0; bltin_exc[i].name; i++) {
2197 PyObject *exc = *bltin_exc[i].exc;
2198 Py_XDECREF(exc);
2199 *bltin_exc[i].exc = NULL;
2200 }
Guido van Rossum25ce5661997-08-02 03:10:38 +00002201}
2202
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002203static char builtin_doc[] =
2204"Built-in functions, exceptions, and other objects.\n\
2205\n\
2206Noteworthy: None is the `nil' object; Ellipsis represents `...' in slices.";
2207
Guido van Rossum25ce5661997-08-02 03:10:38 +00002208PyObject *
Barry Warsaw757af0e1997-08-29 22:13:51 +00002209_PyBuiltin_Init_1()
Guido van Rossum25ce5661997-08-02 03:10:38 +00002210{
2211 PyObject *mod, *dict;
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002212 mod = Py_InitModule4("__builtin__", builtin_methods,
2213 builtin_doc, (PyObject *)NULL,
2214 PYTHON_API_VERSION);
Guido van Rossum25ce5661997-08-02 03:10:38 +00002215 if (mod == NULL)
2216 return NULL;
2217 dict = PyModule_GetDict(mod);
2218 initerrors(dict);
2219 if (PyDict_SetItemString(dict, "None", Py_None) < 0)
2220 return NULL;
2221 if (PyDict_SetItemString(dict, "Ellipsis", Py_Ellipsis) < 0)
2222 return NULL;
2223 if (PyDict_SetItemString(dict, "__debug__",
2224 PyInt_FromLong(Py_OptimizeFlag == 0)) < 0)
2225 return NULL;
Barry Warsaw757af0e1997-08-29 22:13:51 +00002226
Guido van Rossum25ce5661997-08-02 03:10:38 +00002227 return mod;
Guido van Rossum3f5da241990-12-20 15:06:42 +00002228}
2229
2230void
Barry Warsaw757af0e1997-08-29 22:13:51 +00002231_PyBuiltin_Init_2(dict)
2232 PyObject *dict;
2233{
2234 /* if Python was started with -X, initialize the class exceptions */
Barry Warsaw98b62461998-09-14 18:51:11 +00002235 if (Py_UseClassExceptionsFlag) {
2236 if (!init_class_exc(dict)) {
2237 /* class based exceptions could not be
2238 * initialized. Fall back to using string based
2239 * exceptions.
2240 */
2241 PySys_WriteStderr(
2242 "Warning! Falling back to string-based exceptions\n");
2243 initerrors(dict);
2244 }
2245 }
Barry Warsaw757af0e1997-08-29 22:13:51 +00002246}
2247
2248
2249void
2250_PyBuiltin_Fini_1()
2251{
2252 fini_instances();
2253}
2254
2255
2256void
2257_PyBuiltin_Fini_2()
Guido van Rossum3f5da241990-12-20 15:06:42 +00002258{
Guido van Rossum25ce5661997-08-02 03:10:38 +00002259 finierrors();
Guido van Rossum3f5da241990-12-20 15:06:42 +00002260}
Guido van Rossumc6bb8f71991-07-01 18:42:41 +00002261
Guido van Rossum12d12c51993-10-26 17:58:25 +00002262
Guido van Rossume77a7571993-11-03 15:01:26 +00002263/* Helper for filter(): filter a tuple through a function */
Guido van Rossum12d12c51993-10-26 17:58:25 +00002264
Guido van Rossum79f25d91997-04-29 20:08:16 +00002265static PyObject *
Guido van Rossum12d12c51993-10-26 17:58:25 +00002266filtertuple(func, tuple)
Guido van Rossum79f25d91997-04-29 20:08:16 +00002267 PyObject *func;
2268 PyObject *tuple;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002269{
Guido van Rossum79f25d91997-04-29 20:08:16 +00002270 PyObject *result;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002271 register int i, j;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002272 int len = PyTuple_Size(tuple);
Guido van Rossum12d12c51993-10-26 17:58:25 +00002273
Guido van Rossumb7b45621995-08-04 04:07:45 +00002274 if (len == 0) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002275 Py_INCREF(tuple);
Guido van Rossumb7b45621995-08-04 04:07:45 +00002276 return tuple;
2277 }
2278
Guido van Rossum79f25d91997-04-29 20:08:16 +00002279 if ((result = PyTuple_New(len)) == NULL)
Guido van Rossum2586bf01993-11-01 16:21:44 +00002280 return NULL;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002281
Guido van Rossum12d12c51993-10-26 17:58:25 +00002282 for (i = j = 0; i < len; ++i) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002283 PyObject *item, *good;
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002284 int ok;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002285
Guido van Rossum79f25d91997-04-29 20:08:16 +00002286 if ((item = PyTuple_GetItem(tuple, i)) == NULL)
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002287 goto Fail_1;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002288 if (func == Py_None) {
2289 Py_INCREF(item);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002290 good = item;
2291 }
2292 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002293 PyObject *arg = Py_BuildValue("(O)", item);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002294 if (arg == NULL)
2295 goto Fail_1;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002296 good = PyEval_CallObject(func, arg);
2297 Py_DECREF(arg);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002298 if (good == NULL)
Guido van Rossum12d12c51993-10-26 17:58:25 +00002299 goto Fail_1;
2300 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002301 ok = PyObject_IsTrue(good);
2302 Py_DECREF(good);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002303 if (ok) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002304 Py_INCREF(item);
2305 if (PyTuple_SetItem(result, j++, item) < 0)
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002306 goto Fail_1;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002307 }
Guido van Rossum12d12c51993-10-26 17:58:25 +00002308 }
2309
Guido van Rossum79f25d91997-04-29 20:08:16 +00002310 if (_PyTuple_Resize(&result, j, 0) < 0)
Guido van Rossum12d12c51993-10-26 17:58:25 +00002311 return NULL;
2312
Guido van Rossum12d12c51993-10-26 17:58:25 +00002313 return result;
2314
Guido van Rossum12d12c51993-10-26 17:58:25 +00002315Fail_1:
Guido van Rossum79f25d91997-04-29 20:08:16 +00002316 Py_DECREF(result);
Guido van Rossum12d12c51993-10-26 17:58:25 +00002317 return NULL;
2318}
2319
2320
Guido van Rossume77a7571993-11-03 15:01:26 +00002321/* Helper for filter(): filter a string through a function */
Guido van Rossum12d12c51993-10-26 17:58:25 +00002322
Guido van Rossum79f25d91997-04-29 20:08:16 +00002323static PyObject *
Guido van Rossum12d12c51993-10-26 17:58:25 +00002324filterstring(func, strobj)
Guido van Rossum79f25d91997-04-29 20:08:16 +00002325 PyObject *func;
2326 PyObject *strobj;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002327{
Guido van Rossum79f25d91997-04-29 20:08:16 +00002328 PyObject *result;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002329 register int i, j;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002330 int len = PyString_Size(strobj);
Guido van Rossum12d12c51993-10-26 17:58:25 +00002331
Guido van Rossum79f25d91997-04-29 20:08:16 +00002332 if (func == Py_None) {
Guido van Rossum2586bf01993-11-01 16:21:44 +00002333 /* No character is ever false -- share input string */
Guido van Rossum79f25d91997-04-29 20:08:16 +00002334 Py_INCREF(strobj);
Guido van Rossum2d951851994-08-29 12:52:16 +00002335 return strobj;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002336 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002337 if ((result = PyString_FromStringAndSize(NULL, len)) == NULL)
Guido van Rossum2586bf01993-11-01 16:21:44 +00002338 return NULL;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002339
Guido van Rossum12d12c51993-10-26 17:58:25 +00002340 for (i = j = 0; i < len; ++i) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002341 PyObject *item, *arg, *good;
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002342 int ok;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002343
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002344 item = (*strobj->ob_type->tp_as_sequence->sq_item)(strobj, i);
2345 if (item == NULL)
2346 goto Fail_1;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002347 arg = Py_BuildValue("(O)", item);
2348 Py_DECREF(item);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002349 if (arg == NULL)
2350 goto Fail_1;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002351 good = PyEval_CallObject(func, arg);
2352 Py_DECREF(arg);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002353 if (good == NULL)
2354 goto Fail_1;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002355 ok = PyObject_IsTrue(good);
2356 Py_DECREF(good);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002357 if (ok)
Guido van Rossum79f25d91997-04-29 20:08:16 +00002358 PyString_AS_STRING((PyStringObject *)result)[j++] =
2359 PyString_AS_STRING((PyStringObject *)item)[0];
Guido van Rossum12d12c51993-10-26 17:58:25 +00002360 }
2361
Guido van Rossum79f25d91997-04-29 20:08:16 +00002362 if (j < len && _PyString_Resize(&result, j) < 0)
Guido van Rossum12d12c51993-10-26 17:58:25 +00002363 return NULL;
2364
Guido van Rossum12d12c51993-10-26 17:58:25 +00002365 return result;
2366
Guido van Rossum12d12c51993-10-26 17:58:25 +00002367Fail_1:
Guido van Rossum79f25d91997-04-29 20:08:16 +00002368 Py_DECREF(result);
Guido van Rossum12d12c51993-10-26 17:58:25 +00002369 return NULL;
2370}