blob: 1929ae92ba7f3ca375f15f2105a97f971a12239e [file] [log] [blame]
Guido van Rossumf70e43a1991-02-19 12:39:46 +00001/***********************************************************
Guido van Rossum6d023c91995-01-04 19:12:13 +00002Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
3The Netherlands.
Guido van Rossumf70e43a1991-02-19 12:39:46 +00004
5 All Rights Reserved
6
Guido van Rossumd266eb41996-10-25 14:44:06 +00007Permission to use, copy, modify, and distribute this software and its
8documentation for any purpose and without fee is hereby granted,
Guido van Rossumf70e43a1991-02-19 12:39:46 +00009provided that the above copyright notice appear in all copies and that
Guido van Rossumd266eb41996-10-25 14:44:06 +000010both that copyright notice and this permission notice appear in
Guido van Rossumf70e43a1991-02-19 12:39:46 +000011supporting documentation, and that the names of Stichting Mathematisch
Guido van Rossumd266eb41996-10-25 14:44:06 +000012Centrum or CWI or Corporation for National Research Initiatives or
13CNRI not be used in advertising or publicity pertaining to
14distribution of the software without specific, written prior
15permission.
Guido van Rossumf70e43a1991-02-19 12:39:46 +000016
Guido van Rossumd266eb41996-10-25 14:44:06 +000017While CWI is the initial source for this software, a modified version
18is made available by the Corporation for National Research Initiatives
19(CNRI) at the Internet address ftp://ftp.python.org.
20
21STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH
22REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF
23MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH
24CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
25DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
26PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
27TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
28PERFORMANCE OF THIS SOFTWARE.
Guido van Rossumf70e43a1991-02-19 12:39:46 +000029
30******************************************************************/
31
Guido van Rossum3f5da241990-12-20 15:06:42 +000032/* Built-in functions */
33
Guido van Rossum79f25d91997-04-29 20:08:16 +000034#include "Python.h"
Guido van Rossum3f5da241990-12-20 15:06:42 +000035
36#include "node.h"
Guido van Rossum5b722181993-03-30 17:46:03 +000037#include "compile.h"
38#include "eval.h"
Guido van Rossum3f5da241990-12-20 15:06:42 +000039
Guido van Rossumfe4b6ee1996-08-08 18:49:41 +000040#include "mymath.h"
41
Guido van Rossum6bf62da1997-04-11 20:37:35 +000042#include <ctype.h>
43
Guido van Rossum1a2c5cb1996-12-10 15:37:36 +000044#ifdef HAVE_UNISTD_H
45#include <unistd.h>
46#endif
47
Guido van Rossum12d12c51993-10-26 17:58:25 +000048/* Forward */
Guido van Rossum79f25d91997-04-29 20:08:16 +000049static PyObject *filterstring Py_PROTO((PyObject *, PyObject *));
50static PyObject *filtertuple Py_PROTO((PyObject *, PyObject *));
Guido van Rossum12d12c51993-10-26 17:58:25 +000051
Guido van Rossum79f25d91997-04-29 20:08:16 +000052static PyObject *
Guido van Rossum1ae940a1995-01-02 19:04:15 +000053builtin___import__(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +000054 PyObject *self;
55 PyObject *args;
Guido van Rossum3f5da241990-12-20 15:06:42 +000056{
Guido van Rossum1ae940a1995-01-02 19:04:15 +000057 char *name;
Guido van Rossum79f25d91997-04-29 20:08:16 +000058 PyObject *globals = NULL;
59 PyObject *locals = NULL;
60 PyObject *fromlist = NULL;
Guido van Rossum1ae940a1995-01-02 19:04:15 +000061
Guido van Rossum79f25d91997-04-29 20:08:16 +000062 if (!PyArg_ParseTuple(args, "s|OOO:__import__",
Guido van Rossum24c13741995-02-14 09:42:43 +000063 &name, &globals, &locals, &fromlist))
Guido van Rossum1ae940a1995-01-02 19:04:15 +000064 return NULL;
Guido van Rossumaee0bad1997-09-05 07:33:22 +000065 return PyImport_ImportModuleEx(name, globals, locals, fromlist);
Guido van Rossum1ae940a1995-01-02 19:04:15 +000066}
67
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +000068static char import_doc[] =
69"__import__(name, globals, locals, fromlist) -> module\n\
70\n\
71Import a module. The globals are only used to determine the context;\n\
72they are not modified. The locals are currently unused. The fromlist\n\
73should be a list of names to emulate ``from name import ...'', or an\n\
74empty list to emulate ``import name''.\n\
75When importing a module from a package, note that __import__('A.B', ...)\n\
76returns package A when fromlist is empty, but its submodule B when\n\
77fromlist is not empty.";
78
Guido van Rossum1ae940a1995-01-02 19:04:15 +000079
Guido van Rossum79f25d91997-04-29 20:08:16 +000080static PyObject *
Guido van Rossum1ae940a1995-01-02 19:04:15 +000081builtin_abs(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +000082 PyObject *self;
83 PyObject *args;
Guido van Rossum1ae940a1995-01-02 19:04:15 +000084{
Guido van Rossum79f25d91997-04-29 20:08:16 +000085 PyObject *v;
Guido van Rossum1ae940a1995-01-02 19:04:15 +000086
Guido van Rossum79f25d91997-04-29 20:08:16 +000087 if (!PyArg_ParseTuple(args, "O:abs", &v))
Guido van Rossum1ae940a1995-01-02 19:04:15 +000088 return NULL;
Guido van Rossum09df08a1998-05-22 00:51:39 +000089 return PyNumber_Absolute(v);
Guido van Rossum3f5da241990-12-20 15:06:42 +000090}
91
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +000092static char abs_doc[] =
93"abs(number) -> number\n\
94\n\
95Return the absolute value of the argument.";
96
97
Guido van Rossum79f25d91997-04-29 20:08:16 +000098static PyObject *
Guido van Rossum94390a41992-08-14 15:14:30 +000099builtin_apply(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000100 PyObject *self;
101 PyObject *args;
Guido van Rossumc02e15c1991-12-16 13:03:00 +0000102{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000103 PyObject *func, *alist = NULL, *kwdict = NULL;
Barry Warsaw968f8cb1998-10-01 15:33:12 +0000104 PyObject *t = NULL, *retval = NULL;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000105
Guido van Rossum79f25d91997-04-29 20:08:16 +0000106 if (!PyArg_ParseTuple(args, "O|OO:apply", &func, &alist, &kwdict))
Guido van Rossumc02e15c1991-12-16 13:03:00 +0000107 return NULL;
Barry Warsaw968f8cb1998-10-01 15:33:12 +0000108 if (alist != NULL) {
109 if (!PyTuple_Check(alist)) {
110 if (!PySequence_Check(alist)) {
111 PyErr_SetString(PyExc_TypeError,
112 "apply() 2nd argument must be a sequence");
113 return NULL;
114 }
115 t = PySequence_Tuple(alist);
116 if (t == NULL)
117 return NULL;
118 alist = t;
119 }
Guido van Rossum2d951851994-08-29 12:52:16 +0000120 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000121 if (kwdict != NULL && !PyDict_Check(kwdict)) {
122 PyErr_SetString(PyExc_TypeError,
Guido van Rossum681d79a1995-07-18 14:51:37 +0000123 "apply() 3rd argument must be dictionary");
Barry Warsaw968f8cb1998-10-01 15:33:12 +0000124 goto finally;
Guido van Rossum681d79a1995-07-18 14:51:37 +0000125 }
Barry Warsaw968f8cb1998-10-01 15:33:12 +0000126 retval = PyEval_CallObjectWithKeywords(func, alist, kwdict);
127 finally:
128 Py_XDECREF(t);
129 return retval;
Guido van Rossumc02e15c1991-12-16 13:03:00 +0000130}
131
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000132static char apply_doc[] =
133"apply(function, args[, kwargs]) -> value\n\
134\n\
135Call a function with positional arguments taken from the tuple args,\n\
136and keyword arguments taken from the optional dictionary kwargs.";
137
138
Guido van Rossum79f25d91997-04-29 20:08:16 +0000139static PyObject *
Guido van Rossum2d951851994-08-29 12:52:16 +0000140builtin_callable(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000141 PyObject *self;
142 PyObject *args;
Guido van Rossum2d951851994-08-29 12:52:16 +0000143{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000144 PyObject *v;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000145
Guido van Rossum79f25d91997-04-29 20:08:16 +0000146 if (!PyArg_ParseTuple(args, "O:callable", &v))
Guido van Rossum2d951851994-08-29 12:52:16 +0000147 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000148 return PyInt_FromLong((long)PyCallable_Check(v));
Guido van Rossum2d951851994-08-29 12:52:16 +0000149}
150
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000151static char callable_doc[] =
152"callable(object) -> Boolean\n\
153\n\
154Return whether the object is callable (i.e., some kind of function).\n\
155Note that classes are callable, as are instances with a __call__() method.";
156
157
Guido van Rossum79f25d91997-04-29 20:08:16 +0000158static PyObject *
Guido van Rossume77a7571993-11-03 15:01:26 +0000159builtin_filter(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000160 PyObject *self;
161 PyObject *args;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000162{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000163 PyObject *func, *seq, *result;
164 PySequenceMethods *sqf;
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000165 int len;
166 register int i, j;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000167
Guido van Rossum79f25d91997-04-29 20:08:16 +0000168 if (!PyArg_ParseTuple(args, "OO:filter", &func, &seq))
Guido van Rossum12d12c51993-10-26 17:58:25 +0000169 return NULL;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000170
Guido van Rossum79f25d91997-04-29 20:08:16 +0000171 if (PyString_Check(seq)) {
172 PyObject *r = filterstring(func, seq);
Guido van Rossum12d12c51993-10-26 17:58:25 +0000173 return r;
174 }
175
Guido van Rossum79f25d91997-04-29 20:08:16 +0000176 if (PyTuple_Check(seq)) {
177 PyObject *r = filtertuple(func, seq);
Guido van Rossum12d12c51993-10-26 17:58:25 +0000178 return r;
179 }
180
Guido van Rossum09df08a1998-05-22 00:51:39 +0000181 sqf = seq->ob_type->tp_as_sequence;
182 if (sqf == NULL || sqf->sq_length == NULL || sqf->sq_item == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000183 PyErr_SetString(PyExc_TypeError,
Guido van Rossume77a7571993-11-03 15:01:26 +0000184 "argument 2 to filter() must be a sequence type");
Guido van Rossum12d12c51993-10-26 17:58:25 +0000185 goto Fail_2;
186 }
187
188 if ((len = (*sqf->sq_length)(seq)) < 0)
189 goto Fail_2;
190
Guido van Rossum79f25d91997-04-29 20:08:16 +0000191 if (PyList_Check(seq) && seq->ob_refcnt == 1) {
192 Py_INCREF(seq);
Guido van Rossum12d12c51993-10-26 17:58:25 +0000193 result = seq;
194 }
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000195 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000196 if ((result = PyList_New(len)) == NULL)
Guido van Rossum12d12c51993-10-26 17:58:25 +0000197 goto Fail_2;
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000198 }
Guido van Rossum12d12c51993-10-26 17:58:25 +0000199
Guido van Rossum2d951851994-08-29 12:52:16 +0000200 for (i = j = 0; ; ++i) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000201 PyObject *item, *good;
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000202 int ok;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000203
Guido van Rossum2d951851994-08-29 12:52:16 +0000204 if ((item = (*sqf->sq_item)(seq, i)) == NULL) {
Barry Warsawcde8b1b1997-08-22 21:14:38 +0000205 if (PyErr_ExceptionMatches(PyExc_IndexError)) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000206 PyErr_Clear();
Guido van Rossum2d951851994-08-29 12:52:16 +0000207 break;
208 }
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000209 goto Fail_1;
Guido van Rossum2d951851994-08-29 12:52:16 +0000210 }
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000211
Guido van Rossum79f25d91997-04-29 20:08:16 +0000212 if (func == Py_None) {
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000213 good = item;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000214 Py_INCREF(good);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000215 }
216 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000217 PyObject *arg = Py_BuildValue("(O)", item);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000218 if (arg == NULL)
219 goto Fail_1;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000220 good = PyEval_CallObject(func, arg);
221 Py_DECREF(arg);
Guido van Rossum58b68731995-01-10 17:40:55 +0000222 if (good == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000223 Py_DECREF(item);
Guido van Rossum12d12c51993-10-26 17:58:25 +0000224 goto Fail_1;
Guido van Rossum58b68731995-01-10 17:40:55 +0000225 }
Guido van Rossum12d12c51993-10-26 17:58:25 +0000226 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000227 ok = PyObject_IsTrue(good);
228 Py_DECREF(good);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000229 if (ok) {
Guido van Rossum2d951851994-08-29 12:52:16 +0000230 if (j < len) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000231 if (PyList_SetItem(result, j++, item) < 0)
Guido van Rossum2d951851994-08-29 12:52:16 +0000232 goto Fail_1;
233 }
234 else {
235 j++;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000236 if (PyList_Append(result, item) < 0)
Guido van Rossum2d951851994-08-29 12:52:16 +0000237 goto Fail_1;
238 }
Guido van Rossum58b68731995-01-10 17:40:55 +0000239 } else {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000240 Py_DECREF(item);
Guido van Rossum12d12c51993-10-26 17:58:25 +0000241 }
Guido van Rossum12d12c51993-10-26 17:58:25 +0000242 }
243
Guido van Rossum12d12c51993-10-26 17:58:25 +0000244
Guido van Rossum79f25d91997-04-29 20:08:16 +0000245 if (j < len && PyList_SetSlice(result, j, len, NULL) < 0)
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000246 goto Fail_1;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000247
Guido van Rossum12d12c51993-10-26 17:58:25 +0000248 return result;
249
Guido van Rossum12d12c51993-10-26 17:58:25 +0000250Fail_1:
Guido van Rossum79f25d91997-04-29 20:08:16 +0000251 Py_DECREF(result);
Guido van Rossum12d12c51993-10-26 17:58:25 +0000252Fail_2:
Guido van Rossum12d12c51993-10-26 17:58:25 +0000253 return NULL;
254}
255
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000256static char filter_doc[] =
257"filter(function, sequence) -> list\n\
258\n\
259Return a list containing those items of sequence for which function(item)\n\
260is true. If function is None, return a list of items that are true.";
261
262
Guido van Rossum79f25d91997-04-29 20:08:16 +0000263static PyObject *
Guido van Rossum94390a41992-08-14 15:14:30 +0000264builtin_chr(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000265 PyObject *self;
266 PyObject *args;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000267{
268 long x;
269 char s[1];
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000270
Guido van Rossum79f25d91997-04-29 20:08:16 +0000271 if (!PyArg_ParseTuple(args, "l:chr", &x))
Guido van Rossum3f5da241990-12-20 15:06:42 +0000272 return NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000273 if (x < 0 || x >= 256) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000274 PyErr_SetString(PyExc_ValueError,
275 "chr() arg not in range(256)");
Guido van Rossum3f5da241990-12-20 15:06:42 +0000276 return NULL;
277 }
Guido van Rossum6bf62da1997-04-11 20:37:35 +0000278 s[0] = (char)x;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000279 return PyString_FromStringAndSize(s, 1);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000280}
281
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000282static char chr_doc[] =
283"chr(i) -> character\n\
284\n\
285Return a string of one character with ordinal i; 0 <= i < 256.";
286
287
Guido van Rossum79f25d91997-04-29 20:08:16 +0000288static PyObject *
Guido van Rossuma9e7dc11992-10-18 18:53:57 +0000289builtin_cmp(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000290 PyObject *self;
291 PyObject *args;
Guido van Rossuma9e7dc11992-10-18 18:53:57 +0000292{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000293 PyObject *a, *b;
Guido van Rossum09df08a1998-05-22 00:51:39 +0000294 int c;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000295
Guido van Rossum79f25d91997-04-29 20:08:16 +0000296 if (!PyArg_ParseTuple(args, "OO:cmp", &a, &b))
Guido van Rossuma9e7dc11992-10-18 18:53:57 +0000297 return NULL;
Guido van Rossum09df08a1998-05-22 00:51:39 +0000298 if (PyObject_Cmp(a, b, &c) < 0)
Guido van Rossumc8b6df91997-05-23 00:06:51 +0000299 return NULL;
Guido van Rossum09df08a1998-05-22 00:51:39 +0000300 return PyInt_FromLong((long)c);
Guido van Rossuma9e7dc11992-10-18 18:53:57 +0000301}
302
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000303static char cmp_doc[] =
304"cmp(x, y) -> integer\n\
305\n\
306Return negative if x<y, zero if x==y, positive if x>y.";
307
308
Guido van Rossum79f25d91997-04-29 20:08:16 +0000309static PyObject *
Guido van Rossum5524a591995-01-10 15:26:20 +0000310builtin_coerce(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000311 PyObject *self;
312 PyObject *args;
Guido van Rossum6a00cd81995-01-07 12:39:01 +0000313{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000314 PyObject *v, *w;
315 PyObject *res;
Guido van Rossum5524a591995-01-10 15:26:20 +0000316
Guido van Rossum79f25d91997-04-29 20:08:16 +0000317 if (!PyArg_ParseTuple(args, "OO:coerce", &v, &w))
Guido van Rossum5524a591995-01-10 15:26:20 +0000318 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000319 if (PyNumber_Coerce(&v, &w) < 0)
Guido van Rossum04691fc1992-08-12 15:35:34 +0000320 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000321 res = Py_BuildValue("(OO)", v, w);
322 Py_DECREF(v);
323 Py_DECREF(w);
Guido van Rossum04691fc1992-08-12 15:35:34 +0000324 return res;
325}
326
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000327static char coerce_doc[] =
328"coerce(x, y) -> None or (x1, y1)\n\
329\n\
330When x and y can be coerced to values of the same type, return a tuple\n\
331containing the coerced values. When they can't be coerced, return None.";
332
333
Guido van Rossum79f25d91997-04-29 20:08:16 +0000334static PyObject *
Guido van Rossum5b722181993-03-30 17:46:03 +0000335builtin_compile(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000336 PyObject *self;
337 PyObject *args;
Guido van Rossum5b722181993-03-30 17:46:03 +0000338{
339 char *str;
340 char *filename;
341 char *startstr;
342 int start;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000343
Guido van Rossum79f25d91997-04-29 20:08:16 +0000344 if (!PyArg_ParseTuple(args, "sss:compile", &str, &filename, &startstr))
Guido van Rossum5b722181993-03-30 17:46:03 +0000345 return NULL;
346 if (strcmp(startstr, "exec") == 0)
Guido van Rossumb05a5c71997-05-07 17:46:13 +0000347 start = Py_file_input;
Guido van Rossum5b722181993-03-30 17:46:03 +0000348 else if (strcmp(startstr, "eval") == 0)
Guido van Rossumb05a5c71997-05-07 17:46:13 +0000349 start = Py_eval_input;
Guido van Rossum872537c1995-07-07 22:43:42 +0000350 else if (strcmp(startstr, "single") == 0)
Guido van Rossumb05a5c71997-05-07 17:46:13 +0000351 start = Py_single_input;
Guido van Rossum5b722181993-03-30 17:46:03 +0000352 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000353 PyErr_SetString(PyExc_ValueError,
Guido van Rossum872537c1995-07-07 22:43:42 +0000354 "compile() mode must be 'exec' or 'eval' or 'single'");
Guido van Rossum5b722181993-03-30 17:46:03 +0000355 return NULL;
356 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000357 return Py_CompileString(str, filename, start);
Guido van Rossum5b722181993-03-30 17:46:03 +0000358}
359
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000360static char compile_doc[] =
361"compile(source, filename, mode) -> code object\n\
362\n\
363Compile the source string (a Python module, statement or expression)\n\
364into a code object that can be executed by the exec statement or eval().\n\
365The filename will be used for run-time error messages.\n\
366The mode must be 'exec' to compile a module, 'single' to compile a\n\
367single (interactive) statement, or 'eval' to compile an expression.";
368
369
Guido van Rossum8a5c5d21996-01-12 01:09:56 +0000370#ifndef WITHOUT_COMPLEX
371
Guido van Rossum79f25d91997-04-29 20:08:16 +0000372static PyObject *
Guido van Rossum8a5c5d21996-01-12 01:09:56 +0000373builtin_complex(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000374 PyObject *self;
375 PyObject *args;
Guido van Rossum8a5c5d21996-01-12 01:09:56 +0000376{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000377 PyObject *r, *i, *tmp;
378 PyNumberMethods *nbr, *nbi = NULL;
Guido van Rossum530956d1996-07-21 02:27:43 +0000379 Py_complex cr, ci;
Guido van Rossumc6472e91997-03-31 17:15:43 +0000380 int own_r = 0;
Guido van Rossum8a5c5d21996-01-12 01:09:56 +0000381
382 i = NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000383 if (!PyArg_ParseTuple(args, "O|O:complex", &r, &i))
Guido van Rossum8a5c5d21996-01-12 01:09:56 +0000384 return NULL;
385 if ((nbr = r->ob_type->tp_as_number) == NULL ||
Guido van Rossumc6472e91997-03-31 17:15:43 +0000386 nbr->nb_float == NULL ||
387 (i != NULL &&
388 ((nbi = i->ob_type->tp_as_number) == NULL ||
389 nbi->nb_float == NULL))) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000390 PyErr_SetString(PyExc_TypeError,
Guido van Rossum8a5c5d21996-01-12 01:09:56 +0000391 "complex() argument can't be converted to complex");
392 return NULL;
393 }
Guido van Rossumed0af8f1996-12-05 23:18:18 +0000394 /* XXX Hack to support classes with __complex__ method */
Guido van Rossum79f25d91997-04-29 20:08:16 +0000395 if (PyInstance_Check(r)) {
396 static PyObject *complexstr;
397 PyObject *f;
Guido van Rossumed0af8f1996-12-05 23:18:18 +0000398 if (complexstr == NULL) {
Guido van Rossum8d751611997-01-18 08:04:16 +0000399 complexstr = PyString_InternFromString("__complex__");
Guido van Rossumed0af8f1996-12-05 23:18:18 +0000400 if (complexstr == NULL)
401 return NULL;
402 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000403 f = PyObject_GetAttr(r, complexstr);
Guido van Rossumed0af8f1996-12-05 23:18:18 +0000404 if (f == NULL)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000405 PyErr_Clear();
Guido van Rossumed0af8f1996-12-05 23:18:18 +0000406 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000407 PyObject *args = Py_BuildValue("()");
Guido van Rossumed0af8f1996-12-05 23:18:18 +0000408 if (args == NULL)
409 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000410 r = PyEval_CallObject(f, args);
411 Py_DECREF(args);
Barry Warsawf988e681999-01-27 23:13:59 +0000412 Py_DECREF(f);
Guido van Rossumed0af8f1996-12-05 23:18:18 +0000413 if (r == NULL)
414 return NULL;
Guido van Rossumc6472e91997-03-31 17:15:43 +0000415 own_r = 1;
Guido van Rossumed0af8f1996-12-05 23:18:18 +0000416 }
417 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000418 if (PyComplex_Check(r)) {
419 cr = ((PyComplexObject*)r)->cval;
Guido van Rossum730806d1998-04-10 22:27:42 +0000420 if (own_r) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000421 Py_DECREF(r);
Guido van Rossum730806d1998-04-10 22:27:42 +0000422 }
Guido van Rossumc6472e91997-03-31 17:15:43 +0000423 }
Guido van Rossum8a5c5d21996-01-12 01:09:56 +0000424 else {
Guido van Rossumfe4b6ee1996-08-08 18:49:41 +0000425 tmp = (*nbr->nb_float)(r);
Guido van Rossum730806d1998-04-10 22:27:42 +0000426 if (own_r) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000427 Py_DECREF(r);
Guido van Rossum730806d1998-04-10 22:27:42 +0000428 }
Guido van Rossumfe4b6ee1996-08-08 18:49:41 +0000429 if (tmp == NULL)
430 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000431 cr.real = PyFloat_AsDouble(tmp);
432 Py_DECREF(tmp);
Guido van Rossum8a5c5d21996-01-12 01:09:56 +0000433 cr.imag = 0.;
434 }
435 if (i == NULL) {
436 ci.real = 0.;
437 ci.imag = 0.;
438 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000439 else if (PyComplex_Check(i))
440 ci = ((PyComplexObject*)i)->cval;
Guido van Rossum8a5c5d21996-01-12 01:09:56 +0000441 else {
Guido van Rossumc6472e91997-03-31 17:15:43 +0000442 tmp = (*nbi->nb_float)(i);
Guido van Rossumfe4b6ee1996-08-08 18:49:41 +0000443 if (tmp == NULL)
444 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000445 ci.real = PyFloat_AsDouble(tmp);
446 Py_DECREF(tmp);
Guido van Rossum8a5c5d21996-01-12 01:09:56 +0000447 ci.imag = 0.;
448 }
449 cr.real -= ci.imag;
450 cr.imag += ci.real;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000451 return PyComplex_FromCComplex(cr);
Guido van Rossum8a5c5d21996-01-12 01:09:56 +0000452}
453
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000454static char complex_doc[] =
455"complex(real[, imag]) -> complex number\n\
456\n\
457Create a complex number from a real part and an optional imaginary part.\n\
458This is equivalent to (real + imag*1j) where imag defaults to 0.";
459
460
Guido van Rossum8a5c5d21996-01-12 01:09:56 +0000461#endif
462
Guido van Rossum79f25d91997-04-29 20:08:16 +0000463static PyObject *
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000464builtin_dir(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000465 PyObject *self;
466 PyObject *args;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000467{
Guido van Rossum666b17a1997-05-06 16:36:57 +0000468 static char *attrlist[] = {"__members__", "__methods__", NULL};
469 PyObject *v = NULL, *l = NULL, *m = NULL;
470 PyObject *d, *x;
471 int i;
472 char **s;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000473
Guido van Rossum79f25d91997-04-29 20:08:16 +0000474 if (!PyArg_ParseTuple(args, "|O:dir", &v))
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000475 return NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000476 if (v == NULL) {
Guido van Rossum666b17a1997-05-06 16:36:57 +0000477 x = PyEval_GetLocals();
478 if (x == NULL)
479 goto error;
480 l = PyMapping_Keys(x);
481 if (l == NULL)
482 goto error;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000483 }
484 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000485 d = PyObject_GetAttrString(v, "__dict__");
Guido van Rossum666b17a1997-05-06 16:36:57 +0000486 if (d == NULL)
Guido van Rossum795ba581996-05-23 22:49:07 +0000487 PyErr_Clear();
Guido van Rossum666b17a1997-05-06 16:36:57 +0000488 else {
489 l = PyMapping_Keys(d);
490 if (l == NULL)
491 PyErr_Clear();
492 Py_DECREF(d);
493 }
494 if (l == NULL) {
495 l = PyList_New(0);
496 if (l == NULL)
497 goto error;
498 }
499 for (s = attrlist; *s != NULL; s++) {
500 m = PyObject_GetAttrString(v, *s);
501 if (m == NULL) {
502 PyErr_Clear();
503 continue;
504 }
505 for (i = 0; ; i++) {
506 x = PySequence_GetItem(m, i);
507 if (x == NULL) {
508 PyErr_Clear();
509 break;
510 }
511 if (PyList_Append(l, x) != 0) {
512 Py_DECREF(x);
513 Py_DECREF(m);
514 goto error;
515 }
516 Py_DECREF(x);
517 }
518 Py_DECREF(m);
Guido van Rossum795ba581996-05-23 22:49:07 +0000519 }
Guido van Rossum006bcd41991-10-24 14:54:44 +0000520 }
Guido van Rossum666b17a1997-05-06 16:36:57 +0000521 if (PyList_Sort(l) != 0)
522 goto error;
523 return l;
524 error:
525 Py_XDECREF(l);
526 return NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000527}
528
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000529static char dir_doc[] =
530"dir([object]) -> list of strings\n\
531\n\
532Return an alphabetized list of names comprising (some of) the attributes\n\
533of the given object. Without an argument, the names in the current scope\n\
534are listed. With an instance argument, only the instance attributes are\n\
535returned. With a class argument, attributes of the base class are not\n\
536returned. For other types or arguments, this may list members or methods.";
537
538
Guido van Rossum79f25d91997-04-29 20:08:16 +0000539static PyObject *
Guido van Rossum6a00cd81995-01-07 12:39:01 +0000540builtin_divmod(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000541 PyObject *self;
542 PyObject *args;
Guido van Rossum6a00cd81995-01-07 12:39:01 +0000543{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000544 PyObject *v, *w;
Guido van Rossum6a00cd81995-01-07 12:39:01 +0000545
Guido van Rossum79f25d91997-04-29 20:08:16 +0000546 if (!PyArg_ParseTuple(args, "OO:divmod", &v, &w))
Guido van Rossum6a00cd81995-01-07 12:39:01 +0000547 return NULL;
Guido van Rossum09df08a1998-05-22 00:51:39 +0000548 return PyNumber_Divmod(v, w);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000549}
550
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000551static char divmod_doc[] =
552"divmod(x, y) -> (div, mod)\n\
553\n\
554Return the tuple ((x-x%y)/y, x%y). Invariant: div*y + mod == x.";
555
556
Guido van Rossum79f25d91997-04-29 20:08:16 +0000557static PyObject *
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000558builtin_eval(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000559 PyObject *self;
560 PyObject *args;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000561{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000562 PyObject *cmd;
563 PyObject *globals = Py_None, *locals = Py_None;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000564 char *str;
Guido van Rossum590baa41993-11-30 13:40:46 +0000565
Guido van Rossum79f25d91997-04-29 20:08:16 +0000566 if (!PyArg_ParseTuple(args, "O|O!O!:eval",
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000567 &cmd,
Guido van Rossum79f25d91997-04-29 20:08:16 +0000568 &PyDict_Type, &globals,
569 &PyDict_Type, &locals))
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000570 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000571 if (globals == Py_None) {
572 globals = PyEval_GetGlobals();
573 if (locals == Py_None)
574 locals = PyEval_GetLocals();
Guido van Rossum6135a871995-01-09 17:53:26 +0000575 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000576 else if (locals == Py_None)
Guido van Rossum6135a871995-01-09 17:53:26 +0000577 locals = globals;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000578 if (PyDict_GetItemString(globals, "__builtins__") == NULL) {
579 if (PyDict_SetItemString(globals, "__builtins__",
580 PyEval_GetBuiltins()) != 0)
Guido van Rossum6135a871995-01-09 17:53:26 +0000581 return NULL;
582 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000583 if (PyCode_Check(cmd))
584 return PyEval_EvalCode((PyCodeObject *) cmd, globals, locals);
585 if (!PyString_Check(cmd)) {
586 PyErr_SetString(PyExc_TypeError,
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000587 "eval() argument 1 must be string or code object");
Guido van Rossum94390a41992-08-14 15:14:30 +0000588 return NULL;
589 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000590 str = PyString_AsString(cmd);
591 if ((int)strlen(str) != PyString_Size(cmd)) {
592 PyErr_SetString(PyExc_ValueError,
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000593 "embedded '\\0' in string arg");
594 return NULL;
Guido van Rossumf08ab0a1992-03-04 16:41:41 +0000595 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000596 while (*str == ' ' || *str == '\t')
597 str++;
Guido van Rossumb05a5c71997-05-07 17:46:13 +0000598 return PyRun_String(str, Py_eval_input, globals, locals);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000599}
600
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000601static char eval_doc[] =
602"eval(source[, globals[, locals]]) -> value\n\
603\n\
604Evaluate the source in the context of globals and locals.\n\
605The source may be a string representing a Python expression\n\
606or a code object as returned by compile().\n\
607The globals and locals are dictionaries, defaulting to the current\n\
608globals and locals. If only globals is given, locals defaults to it.";
609
610
Guido van Rossum79f25d91997-04-29 20:08:16 +0000611static PyObject *
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000612builtin_execfile(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000613 PyObject *self;
614 PyObject *args;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000615{
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000616 char *filename;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000617 PyObject *globals = Py_None, *locals = Py_None;
618 PyObject *res;
Guido van Rossum0f61f8a1992-02-25 18:55:05 +0000619 FILE* fp;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000620
Guido van Rossum79f25d91997-04-29 20:08:16 +0000621 if (!PyArg_ParseTuple(args, "s|O!O!:execfile",
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000622 &filename,
Guido van Rossum79f25d91997-04-29 20:08:16 +0000623 &PyDict_Type, &globals,
624 &PyDict_Type, &locals))
Guido van Rossum0f61f8a1992-02-25 18:55:05 +0000625 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000626 if (globals == Py_None) {
627 globals = PyEval_GetGlobals();
628 if (locals == Py_None)
629 locals = PyEval_GetLocals();
Guido van Rossum6135a871995-01-09 17:53:26 +0000630 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000631 else if (locals == Py_None)
Guido van Rossum6135a871995-01-09 17:53:26 +0000632 locals = globals;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000633 if (PyDict_GetItemString(globals, "__builtins__") == NULL) {
634 if (PyDict_SetItemString(globals, "__builtins__",
635 PyEval_GetBuiltins()) != 0)
Guido van Rossum6135a871995-01-09 17:53:26 +0000636 return NULL;
637 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000638 Py_BEGIN_ALLOW_THREADS
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000639 fp = fopen(filename, "r");
Guido van Rossum79f25d91997-04-29 20:08:16 +0000640 Py_END_ALLOW_THREADS
Guido van Rossum0f61f8a1992-02-25 18:55:05 +0000641 if (fp == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000642 PyErr_SetFromErrno(PyExc_IOError);
Guido van Rossum0f61f8a1992-02-25 18:55:05 +0000643 return NULL;
644 }
Guido van Rossumb05a5c71997-05-07 17:46:13 +0000645 res = PyRun_File(fp, filename, Py_file_input, globals, locals);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000646 Py_BEGIN_ALLOW_THREADS
Guido van Rossum0f61f8a1992-02-25 18:55:05 +0000647 fclose(fp);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000648 Py_END_ALLOW_THREADS
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000649 return res;
Guido van Rossum0f61f8a1992-02-25 18:55:05 +0000650}
651
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000652static char execfile_doc[] =
653"execfile(filename[, globals[, locals]])\n\
654\n\
655Read and execute a Python script from a file.\n\
656The globals and locals are dictionaries, defaulting to the current\n\
657globals and locals. If only globals is given, locals defaults to it.";
658
659
Guido van Rossum79f25d91997-04-29 20:08:16 +0000660static PyObject *
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000661builtin_float(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000662 PyObject *self;
663 PyObject *args;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000664{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000665 PyObject *v;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000666
Guido van Rossum79f25d91997-04-29 20:08:16 +0000667 if (!PyArg_ParseTuple(args, "O:float", &v))
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000668 return NULL;
Guido van Rossum09df08a1998-05-22 00:51:39 +0000669 return PyNumber_Float(v);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000670}
671
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000672static char float_doc[] =
673"float(x) -> floating point number\n\
674\n\
675Convert a string or number to a floating point number, if possible.";
676
677
Guido van Rossum79f25d91997-04-29 20:08:16 +0000678static PyObject *
Guido van Rossum94390a41992-08-14 15:14:30 +0000679builtin_getattr(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000680 PyObject *self;
681 PyObject *args;
Guido van Rossum33894be1992-01-27 16:53:09 +0000682{
Guido van Rossum950ff291998-06-29 13:38:57 +0000683 PyObject *v, *result, *dflt = NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000684 PyObject *name;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000685
Guido van Rossum950ff291998-06-29 13:38:57 +0000686 if (!PyArg_ParseTuple(args, "OS|O:getattr", &v, &name, &dflt))
Guido van Rossum33894be1992-01-27 16:53:09 +0000687 return NULL;
Guido van Rossum950ff291998-06-29 13:38:57 +0000688 result = PyObject_GetAttr(v, name);
689 if (result == NULL && dflt != NULL) {
690 PyErr_Clear();
691 Py_INCREF(dflt);
692 result = dflt;
693 }
694 return result;
Guido van Rossum9bfef441993-03-29 10:43:31 +0000695}
696
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000697static char getattr_doc[] =
Guido van Rossum950ff291998-06-29 13:38:57 +0000698"getattr(object, name[, default]) -> value\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000699\n\
Guido van Rossum950ff291998-06-29 13:38:57 +0000700Get a named attribute from an object; getattr(x, 'y') is equivalent to x.y.\n\
701When a default argument is given, it is returned when the attribute doesn't\n\
702exist; without it, an exception is raised in that case.";
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000703
704
Guido van Rossum79f25d91997-04-29 20:08:16 +0000705static PyObject *
Guido van Rossum872537c1995-07-07 22:43:42 +0000706builtin_globals(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000707 PyObject *self;
708 PyObject *args;
Guido van Rossum872537c1995-07-07 22:43:42 +0000709{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000710 PyObject *d;
Guido van Rossum872537c1995-07-07 22:43:42 +0000711
Guido van Rossum79f25d91997-04-29 20:08:16 +0000712 if (!PyArg_ParseTuple(args, ""))
Guido van Rossum872537c1995-07-07 22:43:42 +0000713 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000714 d = PyEval_GetGlobals();
715 Py_INCREF(d);
Guido van Rossum872537c1995-07-07 22:43:42 +0000716 return d;
717}
718
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000719static char globals_doc[] =
720"globals() -> dictionary\n\
721\n\
722Return the dictionary containing the current scope's global variables.";
723
724
Guido van Rossum79f25d91997-04-29 20:08:16 +0000725static PyObject *
Guido van Rossum9bfef441993-03-29 10:43:31 +0000726builtin_hasattr(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000727 PyObject *self;
728 PyObject *args;
Guido van Rossum9bfef441993-03-29 10:43:31 +0000729{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000730 PyObject *v;
731 PyObject *name;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000732
Guido van Rossum79f25d91997-04-29 20:08:16 +0000733 if (!PyArg_ParseTuple(args, "OS:hasattr", &v, &name))
Guido van Rossum9bfef441993-03-29 10:43:31 +0000734 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000735 v = PyObject_GetAttr(v, name);
Guido van Rossum9bfef441993-03-29 10:43:31 +0000736 if (v == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000737 PyErr_Clear();
Guido van Rossum09df08a1998-05-22 00:51:39 +0000738 Py_INCREF(Py_False);
739 return Py_False;
Guido van Rossum9bfef441993-03-29 10:43:31 +0000740 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000741 Py_DECREF(v);
Guido van Rossum09df08a1998-05-22 00:51:39 +0000742 Py_INCREF(Py_True);
743 return Py_True;
Guido van Rossum33894be1992-01-27 16:53:09 +0000744}
745
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000746static char hasattr_doc[] =
747"hasattr(object, name) -> Boolean\n\
748\n\
749Return whether the object has an attribute with the given name.\n\
750(This is done by calling getattr(object, name) and catching exceptions.)";
751
752
Guido van Rossum79f25d91997-04-29 20:08:16 +0000753static PyObject *
Guido van Rossum5b722181993-03-30 17:46:03 +0000754builtin_id(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000755 PyObject *self;
756 PyObject *args;
Guido van Rossum5b722181993-03-30 17:46:03 +0000757{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000758 PyObject *v;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000759
Guido van Rossum79f25d91997-04-29 20:08:16 +0000760 if (!PyArg_ParseTuple(args, "O:id", &v))
Guido van Rossum5b722181993-03-30 17:46:03 +0000761 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000762 return PyInt_FromLong((long)v);
Guido van Rossum5b722181993-03-30 17:46:03 +0000763}
764
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000765static char id_doc[] =
766"id(object) -> integer\n\
767\n\
768Return the identity of an object. This is guaranteed to be unique among\n\
769simultaneously existing objects. (Hint: it's the object's memory address.)";
770
771
Guido van Rossum79f25d91997-04-29 20:08:16 +0000772static PyObject *
Guido van Rossum12d12c51993-10-26 17:58:25 +0000773builtin_map(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000774 PyObject *self;
775 PyObject *args;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000776{
777 typedef struct {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000778 PyObject *seq;
779 PySequenceMethods *sqf;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000780 int len;
781 } sequence;
782
Guido van Rossum79f25d91997-04-29 20:08:16 +0000783 PyObject *func, *result;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000784 sequence *seqs = NULL, *sqp;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000785 int n, len;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000786 register int i, j;
787
Guido van Rossum79f25d91997-04-29 20:08:16 +0000788 n = PyTuple_Size(args);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000789 if (n < 2) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000790 PyErr_SetString(PyExc_TypeError,
791 "map() requires at least two args");
Guido van Rossum12d12c51993-10-26 17:58:25 +0000792 return NULL;
793 }
794
Guido van Rossum79f25d91997-04-29 20:08:16 +0000795 func = PyTuple_GetItem(args, 0);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000796 n--;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000797
Guido van Rossumfa4ac711998-07-10 17:37:30 +0000798 if (func == Py_None && n == 1) {
799 /* map(None, S) is the same as list(S). */
800 return PySequence_List(PyTuple_GetItem(args, 1));
801 }
802
Guido van Rossum79f25d91997-04-29 20:08:16 +0000803 if ((seqs = PyMem_NEW(sequence, n)) == NULL) {
804 PyErr_NoMemory();
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000805 goto Fail_2;
806 }
Guido van Rossum12d12c51993-10-26 17:58:25 +0000807
Guido van Rossum2d951851994-08-29 12:52:16 +0000808 for (len = 0, i = 0, sqp = seqs; i < n; ++i, ++sqp) {
Guido van Rossum12d12c51993-10-26 17:58:25 +0000809 int curlen;
Guido van Rossum09df08a1998-05-22 00:51:39 +0000810 PySequenceMethods *sqf;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000811
Guido van Rossum79f25d91997-04-29 20:08:16 +0000812 if ((sqp->seq = PyTuple_GetItem(args, i + 1)) == NULL)
Guido van Rossum12d12c51993-10-26 17:58:25 +0000813 goto Fail_2;
814
Guido van Rossum09df08a1998-05-22 00:51:39 +0000815 sqp->sqf = sqf = sqp->seq->ob_type->tp_as_sequence;
816 if (sqf == NULL ||
817 sqf->sq_length == NULL ||
818 sqf->sq_item == NULL)
819 {
Guido van Rossum12d12c51993-10-26 17:58:25 +0000820 static char errmsg[] =
821 "argument %d to map() must be a sequence object";
Guido van Rossum15e33a41997-04-30 19:00:27 +0000822 char errbuf[sizeof(errmsg) + 25];
Guido van Rossum12d12c51993-10-26 17:58:25 +0000823
824 sprintf(errbuf, errmsg, i+2);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000825 PyErr_SetString(PyExc_TypeError, errbuf);
Guido van Rossum12d12c51993-10-26 17:58:25 +0000826 goto Fail_2;
827 }
828
829 if ((curlen = sqp->len = (*sqp->sqf->sq_length)(sqp->seq)) < 0)
830 goto Fail_2;
831
832 if (curlen > len)
833 len = curlen;
834 }
835
Guido van Rossum79f25d91997-04-29 20:08:16 +0000836 if ((result = (PyObject *) PyList_New(len)) == NULL)
Guido van Rossum12d12c51993-10-26 17:58:25 +0000837 goto Fail_2;
838
Guido van Rossum2d951851994-08-29 12:52:16 +0000839 for (i = 0; ; ++i) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000840 PyObject *alist, *item=NULL, *value;
Guido van Rossum2d951851994-08-29 12:52:16 +0000841 int any = 0;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000842
Guido van Rossum79f25d91997-04-29 20:08:16 +0000843 if (func == Py_None && n == 1)
Guido van Rossum32120311995-07-10 13:52:21 +0000844 alist = NULL;
Guido van Rossum2d951851994-08-29 12:52:16 +0000845 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000846 if ((alist = PyTuple_New(n)) == NULL)
Guido van Rossum2d951851994-08-29 12:52:16 +0000847 goto Fail_1;
848 }
Guido van Rossum12d12c51993-10-26 17:58:25 +0000849
850 for (j = 0, sqp = seqs; j < n; ++j, ++sqp) {
Guido van Rossum2d951851994-08-29 12:52:16 +0000851 if (sqp->len < 0) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000852 Py_INCREF(Py_None);
853 item = Py_None;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000854 }
Guido van Rossum12d12c51993-10-26 17:58:25 +0000855 else {
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000856 item = (*sqp->sqf->sq_item)(sqp->seq, i);
Guido van Rossum2d951851994-08-29 12:52:16 +0000857 if (item == NULL) {
Barry Warsawcde8b1b1997-08-22 21:14:38 +0000858 if (PyErr_ExceptionMatches(
859 PyExc_IndexError))
860 {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000861 PyErr_Clear();
862 Py_INCREF(Py_None);
863 item = Py_None;
Guido van Rossum2d951851994-08-29 12:52:16 +0000864 sqp->len = -1;
865 }
866 else {
867 goto Fail_0;
868 }
869 }
870 else
871 any = 1;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000872
Guido van Rossum12d12c51993-10-26 17:58:25 +0000873 }
Guido van Rossum32120311995-07-10 13:52:21 +0000874 if (!alist)
Guido van Rossum2d951851994-08-29 12:52:16 +0000875 break;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000876 if (PyTuple_SetItem(alist, j, item) < 0) {
877 Py_DECREF(item);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000878 goto Fail_0;
Guido van Rossum2d951851994-08-29 12:52:16 +0000879 }
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000880 continue;
881
882 Fail_0:
Guido van Rossum79f25d91997-04-29 20:08:16 +0000883 Py_XDECREF(alist);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000884 goto Fail_1;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000885 }
886
Guido van Rossum32120311995-07-10 13:52:21 +0000887 if (!alist)
888 alist = item;
Guido van Rossum2d951851994-08-29 12:52:16 +0000889
890 if (!any) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000891 Py_DECREF(alist);
Guido van Rossum2d951851994-08-29 12:52:16 +0000892 break;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000893 }
Guido van Rossum2d951851994-08-29 12:52:16 +0000894
Guido van Rossum79f25d91997-04-29 20:08:16 +0000895 if (func == Py_None)
Guido van Rossum32120311995-07-10 13:52:21 +0000896 value = alist;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000897 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000898 value = PyEval_CallObject(func, alist);
899 Py_DECREF(alist);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000900 if (value == NULL)
901 goto Fail_1;
Guido van Rossum2d951851994-08-29 12:52:16 +0000902 }
903 if (i >= len) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000904 if (PyList_Append(result, value) < 0)
Guido van Rossum2d951851994-08-29 12:52:16 +0000905 goto Fail_1;
Barry Warsaw21332871999-01-28 04:21:35 +0000906 Py_DECREF(value);
Guido van Rossum2d951851994-08-29 12:52:16 +0000907 }
908 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000909 if (PyList_SetItem(result, i, value) < 0)
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000910 goto Fail_1;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000911 }
912 }
913
Guido van Rossumfa4ac711998-07-10 17:37:30 +0000914 if (i < len && PyList_SetSlice(result, i, len, NULL) < 0)
915 goto Fail_1;
916
Guido van Rossum79f25d91997-04-29 20:08:16 +0000917 PyMem_DEL(seqs);
Guido van Rossum12d12c51993-10-26 17:58:25 +0000918 return result;
919
Guido van Rossum12d12c51993-10-26 17:58:25 +0000920Fail_1:
Guido van Rossum79f25d91997-04-29 20:08:16 +0000921 Py_DECREF(result);
Guido van Rossum12d12c51993-10-26 17:58:25 +0000922Fail_2:
Guido van Rossum79f25d91997-04-29 20:08:16 +0000923 if (seqs) PyMem_DEL(seqs);
Guido van Rossum12d12c51993-10-26 17:58:25 +0000924 return NULL;
925}
926
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000927static char map_doc[] =
928"map(function, sequence[, sequence, ...]) -> list\n\
929\n\
930Return a list of the results of applying the function to the items of\n\
931the argument sequence(s). If more than one sequence is given, the\n\
932function is called with an argument list consisting of the corresponding\n\
933item of each sequence, substituting None for missing values when not all\n\
934sequences have the same length. If the function is None, return a list of\n\
935the items of the sequence (or a list of tuples if more than one sequence).";
936
937
Guido van Rossum79f25d91997-04-29 20:08:16 +0000938static PyObject *
Guido van Rossum94390a41992-08-14 15:14:30 +0000939builtin_setattr(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000940 PyObject *self;
941 PyObject *args;
Guido van Rossum33894be1992-01-27 16:53:09 +0000942{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000943 PyObject *v;
944 PyObject *name;
945 PyObject *value;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000946
Guido van Rossum79f25d91997-04-29 20:08:16 +0000947 if (!PyArg_ParseTuple(args, "OSO:setattr", &v, &name, &value))
Guido van Rossum33894be1992-01-27 16:53:09 +0000948 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000949 if (PyObject_SetAttr(v, name, value) != 0)
Guido van Rossum33894be1992-01-27 16:53:09 +0000950 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000951 Py_INCREF(Py_None);
952 return Py_None;
Guido van Rossum33894be1992-01-27 16:53:09 +0000953}
954
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000955static char setattr_doc[] =
956"setattr(object, name, value)\n\
957\n\
958Set a named attribute on an object; setattr(x, 'y', v) is equivalent to\n\
959``x.y = v''.";
960
961
Guido van Rossum79f25d91997-04-29 20:08:16 +0000962static PyObject *
Guido van Rossum14144fc1994-08-29 12:53:40 +0000963builtin_delattr(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000964 PyObject *self;
965 PyObject *args;
Guido van Rossum14144fc1994-08-29 12:53:40 +0000966{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000967 PyObject *v;
968 PyObject *name;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000969
Guido van Rossum79f25d91997-04-29 20:08:16 +0000970 if (!PyArg_ParseTuple(args, "OS:delattr", &v, &name))
Guido van Rossum14144fc1994-08-29 12:53:40 +0000971 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000972 if (PyObject_SetAttr(v, name, (PyObject *)NULL) != 0)
Guido van Rossum14144fc1994-08-29 12:53:40 +0000973 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000974 Py_INCREF(Py_None);
975 return Py_None;
Guido van Rossum14144fc1994-08-29 12:53:40 +0000976}
977
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000978static char delattr_doc[] =
Guido van Rossumdf12a591998-11-23 22:13:04 +0000979"delattr(object, name)\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000980\n\
981Delete a named attribute on an object; delattr(x, 'y') is equivalent to\n\
982``del x.y''.";
983
984
Guido van Rossum79f25d91997-04-29 20:08:16 +0000985static PyObject *
Guido van Rossum9bfef441993-03-29 10:43:31 +0000986builtin_hash(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000987 PyObject *self;
988 PyObject *args;
Guido van Rossum9bfef441993-03-29 10:43:31 +0000989{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000990 PyObject *v;
Guido van Rossum9bfef441993-03-29 10:43:31 +0000991 long x;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000992
Guido van Rossum79f25d91997-04-29 20:08:16 +0000993 if (!PyArg_ParseTuple(args, "O:hash", &v))
Guido van Rossum9bfef441993-03-29 10:43:31 +0000994 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000995 x = PyObject_Hash(v);
Guido van Rossum9bfef441993-03-29 10:43:31 +0000996 if (x == -1)
997 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000998 return PyInt_FromLong(x);
Guido van Rossum9bfef441993-03-29 10:43:31 +0000999}
1000
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001001static char hash_doc[] =
1002"hash(object) -> integer\n\
1003\n\
1004Return a hash value for the object. Two objects with the same value have\n\
1005the same hash value. The reverse is not necessarily true, but likely.";
1006
1007
Guido van Rossum79f25d91997-04-29 20:08:16 +00001008static PyObject *
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001009builtin_hex(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001010 PyObject *self;
1011 PyObject *args;
Guido van Rossum006bcd41991-10-24 14:54:44 +00001012{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001013 PyObject *v;
1014 PyNumberMethods *nb;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001015
Guido van Rossum79f25d91997-04-29 20:08:16 +00001016 if (!PyArg_ParseTuple(args, "O:hex", &v))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001017 return NULL;
Guido van Rossum1899c2e1992-09-12 11:09:23 +00001018
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001019 if ((nb = v->ob_type->tp_as_number) == NULL ||
Guido van Rossum1899c2e1992-09-12 11:09:23 +00001020 nb->nb_hex == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001021 PyErr_SetString(PyExc_TypeError,
Guido van Rossum1899c2e1992-09-12 11:09:23 +00001022 "hex() argument can't be converted to hex");
1023 return NULL;
Guido van Rossum006bcd41991-10-24 14:54:44 +00001024 }
Guido van Rossum1899c2e1992-09-12 11:09:23 +00001025 return (*nb->nb_hex)(v);
Guido van Rossum006bcd41991-10-24 14:54:44 +00001026}
1027
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001028static char hex_doc[] =
1029"hex(number) -> string\n\
1030\n\
1031Return the hexadecimal representation of an integer or long integer.";
1032
1033
Guido van Rossum79f25d91997-04-29 20:08:16 +00001034static PyObject *builtin_raw_input Py_PROTO((PyObject *, PyObject *));
Guido van Rossum3165fe61992-09-25 21:59:05 +00001035
Guido van Rossum79f25d91997-04-29 20:08:16 +00001036static PyObject *
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001037builtin_input(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001038 PyObject *self;
1039 PyObject *args;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001040{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001041 PyObject *line;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001042 char *str;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001043 PyObject *res;
1044 PyObject *globals, *locals;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001045
1046 line = builtin_raw_input(self, args);
Guido van Rossum3165fe61992-09-25 21:59:05 +00001047 if (line == NULL)
1048 return line;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001049 if (!PyArg_Parse(line, "s;embedded '\\0' in input line", &str))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001050 return NULL;
1051 while (*str == ' ' || *str == '\t')
1052 str++;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001053 globals = PyEval_GetGlobals();
1054 locals = PyEval_GetLocals();
1055 if (PyDict_GetItemString(globals, "__builtins__") == NULL) {
1056 if (PyDict_SetItemString(globals, "__builtins__",
1057 PyEval_GetBuiltins()) != 0)
Guido van Rossum6135a871995-01-09 17:53:26 +00001058 return NULL;
1059 }
Guido van Rossumb05a5c71997-05-07 17:46:13 +00001060 res = PyRun_String(str, Py_eval_input, globals, locals);
Guido van Rossum79f25d91997-04-29 20:08:16 +00001061 Py_DECREF(line);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001062 return res;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001063}
1064
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001065static char input_doc[] =
1066"input([prompt]) -> value\n\
1067\n\
1068Equivalent to eval(raw_input(prompt)).";
1069
1070
Guido van Rossume8811f81997-02-14 15:48:05 +00001071static PyObject *
1072builtin_intern(self, args)
1073 PyObject *self;
1074 PyObject *args;
1075{
1076 PyObject *s;
1077 if (!PyArg_ParseTuple(args, "S", &s))
1078 return NULL;
1079 Py_INCREF(s);
1080 PyString_InternInPlace(&s);
1081 return s;
1082}
1083
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001084static char intern_doc[] =
1085"intern(string) -> string\n\
1086\n\
1087``Intern'' the given string. This enters the string in the (global)\n\
1088table of interned strings whose purpose is to speed up dictionary lookups.\n\
1089Return the string itself or the previously interned string object with the\n\
1090same value.";
1091
1092
Guido van Rossum79f25d91997-04-29 20:08:16 +00001093static PyObject *
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001094builtin_int(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001095 PyObject *self;
1096 PyObject *args;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001097{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001098 PyObject *v;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001099
Guido van Rossum79f25d91997-04-29 20:08:16 +00001100 if (!PyArg_ParseTuple(args, "O:int", &v))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001101 return NULL;
Guido van Rossum09df08a1998-05-22 00:51:39 +00001102 return PyNumber_Int(v);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001103}
1104
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001105static char int_doc[] =
1106"int(x) -> integer\n\
1107\n\
1108Convert a string or number to an integer, if possible.\n\
1109A floating point argument will be truncated towards zero.";
1110
1111
Guido van Rossum79f25d91997-04-29 20:08:16 +00001112static PyObject *
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001113builtin_len(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001114 PyObject *self;
1115 PyObject *args;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001116{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001117 PyObject *v;
Guido van Rossum8ea9f4d1998-06-29 22:26:50 +00001118 long res;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001119
Guido van Rossum79f25d91997-04-29 20:08:16 +00001120 if (!PyArg_ParseTuple(args, "O:len", &v))
Guido van Rossum3f5da241990-12-20 15:06:42 +00001121 return NULL;
Guido van Rossum8ea9f4d1998-06-29 22:26:50 +00001122 res = PyObject_Length(v);
1123 if (res < 0 && PyErr_Occurred())
1124 return NULL;
1125 return PyInt_FromLong(res);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001126}
1127
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001128static char len_doc[] =
1129"len(object) -> integer\n\
1130\n\
1131Return the number of items of a sequence or mapping.";
1132
1133
Guido van Rossum79f25d91997-04-29 20:08:16 +00001134static PyObject *
Guido van Rossumd1705771996-04-09 02:41:06 +00001135builtin_list(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001136 PyObject *self;
1137 PyObject *args;
Guido van Rossumd1705771996-04-09 02:41:06 +00001138{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001139 PyObject *v;
Guido van Rossumd1705771996-04-09 02:41:06 +00001140
Guido van Rossum79f25d91997-04-29 20:08:16 +00001141 if (!PyArg_ParseTuple(args, "O:list", &v))
Guido van Rossumd1705771996-04-09 02:41:06 +00001142 return NULL;
Guido van Rossum09df08a1998-05-22 00:51:39 +00001143 return PySequence_List(v);
Guido van Rossumd1705771996-04-09 02:41:06 +00001144}
1145
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001146static char list_doc[] =
1147"list(sequence) -> list\n\
1148\n\
1149Return a new list whose items are the same as those of the argument sequence.";
1150
Guido van Rossum8861b741996-07-30 16:49:37 +00001151
1152static PyObject *
1153builtin_slice(self, args)
1154 PyObject *self;
1155 PyObject *args;
1156{
Guido van Rossum09df08a1998-05-22 00:51:39 +00001157 PyObject *start, *stop, *step;
Guido van Rossum8861b741996-07-30 16:49:37 +00001158
Guido van Rossum09df08a1998-05-22 00:51:39 +00001159 start = stop = step = NULL;
Guido van Rossum8861b741996-07-30 16:49:37 +00001160
Guido van Rossum09df08a1998-05-22 00:51:39 +00001161 if (!PyArg_ParseTuple(args, "O|OO:slice", &start, &stop, &step))
1162 return NULL;
Guido van Rossum8861b741996-07-30 16:49:37 +00001163
Guido van Rossum09df08a1998-05-22 00:51:39 +00001164 /* This swapping of stop and start is to maintain similarity with
1165 range(). */
1166 if (stop == NULL) {
1167 stop = start;
1168 start = NULL;
1169 }
1170 return PySlice_New(start, stop, step);
Guido van Rossum8861b741996-07-30 16:49:37 +00001171}
1172
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001173static char slice_doc[] =
1174"slice([start,] step[, stop]) -> slice object\n\
1175\n\
1176Create a slice object. This is used for slicing by the Numeric extensions.";
1177
1178
Guido van Rossum79f25d91997-04-29 20:08:16 +00001179static PyObject *
Guido van Rossum872537c1995-07-07 22:43:42 +00001180builtin_locals(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001181 PyObject *self;
1182 PyObject *args;
Guido van Rossum872537c1995-07-07 22:43:42 +00001183{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001184 PyObject *d;
Guido van Rossum872537c1995-07-07 22:43:42 +00001185
Guido van Rossum79f25d91997-04-29 20:08:16 +00001186 if (!PyArg_ParseTuple(args, ""))
Guido van Rossum872537c1995-07-07 22:43:42 +00001187 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001188 d = PyEval_GetLocals();
1189 Py_INCREF(d);
Guido van Rossum872537c1995-07-07 22:43:42 +00001190 return d;
1191}
1192
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001193static char locals_doc[] =
1194"locals() -> dictionary\n\
1195\n\
1196Return the dictionary containing the current scope's local variables.";
1197
1198
Guido van Rossum79f25d91997-04-29 20:08:16 +00001199static PyObject *
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001200builtin_long(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001201 PyObject *self;
1202 PyObject *args;
Guido van Rossumd4905451991-05-05 20:00:36 +00001203{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001204 PyObject *v;
Guido van Rossum1899c2e1992-09-12 11:09:23 +00001205
Guido van Rossum79f25d91997-04-29 20:08:16 +00001206 if (!PyArg_ParseTuple(args, "O:long", &v))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001207 return NULL;
Guido van Rossum09df08a1998-05-22 00:51:39 +00001208 return PyNumber_Long(v);
Guido van Rossumd4905451991-05-05 20:00:36 +00001209}
1210
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001211static char long_doc[] =
1212"long(x) -> long integer\n\
1213\n\
1214Convert a string or number to a long integer, if possible.\n\
1215A floating point argument will be truncated towards zero.";
1216
1217
Guido van Rossum79f25d91997-04-29 20:08:16 +00001218static PyObject *
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001219min_max(args, sign)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001220 PyObject *args;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001221 int sign;
1222{
Guido van Rossum2d951851994-08-29 12:52:16 +00001223 int i;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001224 PyObject *v, *w, *x;
1225 PySequenceMethods *sq;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001226
Guido van Rossum79f25d91997-04-29 20:08:16 +00001227 if (PyTuple_Size(args) > 1)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001228 v = args;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001229 else if (!PyArg_ParseTuple(args, "O:min/max", &v))
Guido van Rossum3f5da241990-12-20 15:06:42 +00001230 return NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001231 sq = v->ob_type->tp_as_sequence;
Guido van Rossum09df08a1998-05-22 00:51:39 +00001232 if (sq == NULL || sq->sq_item == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001233 PyErr_SetString(PyExc_TypeError,
1234 "min() or max() of non-sequence");
Guido van Rossum3f5da241990-12-20 15:06:42 +00001235 return NULL;
1236 }
Guido van Rossum2d951851994-08-29 12:52:16 +00001237 w = NULL;
1238 for (i = 0; ; i++) {
Guido van Rossum3f5da241990-12-20 15:06:42 +00001239 x = (*sq->sq_item)(v, i); /* Implies INCREF */
Guido van Rossum2d951851994-08-29 12:52:16 +00001240 if (x == NULL) {
Barry Warsawcde8b1b1997-08-22 21:14:38 +00001241 if (PyErr_ExceptionMatches(PyExc_IndexError)) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001242 PyErr_Clear();
Guido van Rossum2d951851994-08-29 12:52:16 +00001243 break;
1244 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00001245 Py_XDECREF(w);
Guido van Rossum2d951851994-08-29 12:52:16 +00001246 return NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001247 }
Guido van Rossum2d951851994-08-29 12:52:16 +00001248 if (w == NULL)
1249 w = x;
1250 else {
Guido van Rossumc8b6df91997-05-23 00:06:51 +00001251 int c = PyObject_Compare(x, w);
1252 if (c && PyErr_Occurred()) {
1253 Py_DECREF(x);
1254 Py_XDECREF(w);
1255 return NULL;
1256 }
1257 if (c * sign > 0) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001258 Py_DECREF(w);
Guido van Rossum2d951851994-08-29 12:52:16 +00001259 w = x;
1260 }
1261 else
Guido van Rossum79f25d91997-04-29 20:08:16 +00001262 Py_DECREF(x);
Guido van Rossum2d951851994-08-29 12:52:16 +00001263 }
Guido van Rossum3f5da241990-12-20 15:06:42 +00001264 }
Guido van Rossum2d951851994-08-29 12:52:16 +00001265 if (w == NULL)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001266 PyErr_SetString(PyExc_ValueError,
1267 "min() or max() of empty sequence");
Guido van Rossum3f5da241990-12-20 15:06:42 +00001268 return w;
1269}
1270
Guido van Rossum79f25d91997-04-29 20:08:16 +00001271static PyObject *
Guido van Rossum3f5da241990-12-20 15:06:42 +00001272builtin_min(self, v)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001273 PyObject *self;
1274 PyObject *v;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001275{
1276 return min_max(v, -1);
1277}
1278
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001279static char min_doc[] =
1280"min(sequence) -> value\n\
1281min(a, b, c, ...) -> value\n\
1282\n\
1283With a single sequence argument, return its smallest item.\n\
1284With two or more arguments, return the smallest argument.";
1285
1286
Guido van Rossum79f25d91997-04-29 20:08:16 +00001287static PyObject *
Guido van Rossum3f5da241990-12-20 15:06:42 +00001288builtin_max(self, v)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001289 PyObject *self;
1290 PyObject *v;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001291{
1292 return min_max(v, 1);
1293}
1294
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001295static char max_doc[] =
1296"max(sequence) -> value\n\
1297max(a, b, c, ...) -> value\n\
1298\n\
1299With a single sequence argument, return its largest item.\n\
1300With two or more arguments, return the largest argument.";
1301
1302
Guido van Rossum79f25d91997-04-29 20:08:16 +00001303static PyObject *
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001304builtin_oct(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001305 PyObject *self;
1306 PyObject *args;
Guido van Rossum006bcd41991-10-24 14:54:44 +00001307{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001308 PyObject *v;
1309 PyNumberMethods *nb;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001310
Guido van Rossum79f25d91997-04-29 20:08:16 +00001311 if (!PyArg_ParseTuple(args, "O:oct", &v))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001312 return NULL;
Guido van Rossum1899c2e1992-09-12 11:09:23 +00001313 if (v == NULL || (nb = v->ob_type->tp_as_number) == NULL ||
1314 nb->nb_oct == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001315 PyErr_SetString(PyExc_TypeError,
Guido van Rossum1899c2e1992-09-12 11:09:23 +00001316 "oct() argument can't be converted to oct");
1317 return NULL;
Guido van Rossum006bcd41991-10-24 14:54:44 +00001318 }
Guido van Rossum1899c2e1992-09-12 11:09:23 +00001319 return (*nb->nb_oct)(v);
Guido van Rossum006bcd41991-10-24 14:54:44 +00001320}
1321
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001322static char oct_doc[] =
1323"oct(number) -> string\n\
1324\n\
1325Return the octal representation of an integer or long integer.";
1326
1327
Guido van Rossum79f25d91997-04-29 20:08:16 +00001328static PyObject *
Guido van Rossum94390a41992-08-14 15:14:30 +00001329builtin_open(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001330 PyObject *self;
1331 PyObject *args;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001332{
Guido van Rossum2d951851994-08-29 12:52:16 +00001333 char *name;
1334 char *mode = "r";
1335 int bufsize = -1;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001336 PyObject *f;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001337
Guido van Rossum79f25d91997-04-29 20:08:16 +00001338 if (!PyArg_ParseTuple(args, "s|si:open", &name, &mode, &bufsize))
Guido van Rossum3f5da241990-12-20 15:06:42 +00001339 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001340 f = PyFile_FromString(name, mode);
Guido van Rossum2d951851994-08-29 12:52:16 +00001341 if (f != NULL)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001342 PyFile_SetBufSize(f, bufsize);
Guido van Rossum2d951851994-08-29 12:52:16 +00001343 return f;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001344}
1345
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001346static char open_doc[] =
1347"open(filename[, mode[, buffering]]) -> file object\n\
1348\n\
1349Open a file. The mode can be 'r', 'w' or 'a' for reading (default),\n\
1350writing or appending. The file will be created if it doesn't exist\n\
1351when opened for writing or appending; it will be truncated when\n\
1352opened for writing. Add a 'b' to the mode for binary files.\n\
1353Add a '+' to the mode to allow simultaneous reading and writing.\n\
1354If the buffering argument is given, 0 means unbuffered, 1 means line\n\
1355buffered, and larger numbers specify the buffer size.";
1356
1357
Guido van Rossum79f25d91997-04-29 20:08:16 +00001358static PyObject *
Guido van Rossum94390a41992-08-14 15:14:30 +00001359builtin_ord(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001360 PyObject *self;
1361 PyObject *args;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001362{
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001363 char c;
1364
Guido van Rossum79f25d91997-04-29 20:08:16 +00001365 if (!PyArg_ParseTuple(args, "c:ord", &c))
Guido van Rossum3f5da241990-12-20 15:06:42 +00001366 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001367 return PyInt_FromLong((long)(c & 0xff));
Guido van Rossum3f5da241990-12-20 15:06:42 +00001368}
1369
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001370static char ord_doc[] =
1371"ord(c) -> integer\n\
1372\n\
1373Return the integer ordinal of a one character string.";
1374
1375
Guido van Rossum79f25d91997-04-29 20:08:16 +00001376static PyObject *
Guido van Rossum6a00cd81995-01-07 12:39:01 +00001377builtin_pow(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001378 PyObject *self;
1379 PyObject *args;
Guido van Rossum6a00cd81995-01-07 12:39:01 +00001380{
Guido van Rossum09df08a1998-05-22 00:51:39 +00001381 PyObject *v, *w, *z = Py_None;
Guido van Rossum6a00cd81995-01-07 12:39:01 +00001382
Guido van Rossum79f25d91997-04-29 20:08:16 +00001383 if (!PyArg_ParseTuple(args, "OO|O:pow", &v, &w, &z))
Guido van Rossum6a00cd81995-01-07 12:39:01 +00001384 return NULL;
Guido van Rossum09df08a1998-05-22 00:51:39 +00001385 return PyNumber_Power(v, w, z);
Guido van Rossumd4905451991-05-05 20:00:36 +00001386}
1387
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001388static char pow_doc[] =
1389"pow(x, y[, z]) -> number\n\
1390\n\
1391With two arguments, equivalent to x**y. With three arguments,\n\
1392equivalent to (x**y) % z, but may be more efficient (e.g. for longs).";
1393
1394
Guido van Rossum79f25d91997-04-29 20:08:16 +00001395static PyObject *
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001396builtin_range(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001397 PyObject *self;
1398 PyObject *args;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001399{
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001400 long ilow = 0, ihigh = 0, istep = 1;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001401 int i, n;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001402 PyObject *v;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001403
Guido van Rossum79f25d91997-04-29 20:08:16 +00001404 if (PyTuple_Size(args) <= 1) {
1405 if (!PyArg_ParseTuple(args,
Guido van Rossum0865dd91995-01-17 16:30:22 +00001406 "l;range() requires 1-3 int arguments",
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001407 &ihigh))
1408 return NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001409 }
1410 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001411 if (!PyArg_ParseTuple(args,
Guido van Rossum0865dd91995-01-17 16:30:22 +00001412 "ll|l;range() requires 1-3 int arguments",
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001413 &ilow, &ihigh, &istep))
Guido van Rossum3f5da241990-12-20 15:06:42 +00001414 return NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001415 }
1416 if (istep == 0) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001417 PyErr_SetString(PyExc_ValueError, "zero step for range()");
Guido van Rossum3f5da241990-12-20 15:06:42 +00001418 return NULL;
1419 }
Guido van Rossume23cde21999-01-12 05:07:47 +00001420 /* A bit convoluted because this might overflow; due to Tim Peters */
1421 if (istep > 0) {
1422 if (ihigh <= ilow)
1423 n = 0;
1424 else {
1425 unsigned long hi = (unsigned long)ihigh;
1426 unsigned long lo = (unsigned long)ilow;
1427 unsigned long diff = hi - lo - 1;
1428 n = (long)(diff / istep + 1);
1429 }
1430 }
1431 else {
1432 /* But any errors in this branch are my own --Guido */
1433 if (ihigh >= ilow)
1434 n = 0;
1435 else {
1436 /* Swap lo and hi; use abs(istep) */
1437 unsigned long hi = (unsigned long)ilow;
1438 unsigned long lo = (unsigned long)ihigh;
1439 unsigned long diff = hi - lo - 1;
1440 n = (long)(diff / (-istep) + 1);
1441 }
1442 }
1443 if (n < 0) {
1444 PyErr_SetString(PyExc_OverflowError,
1445 "range() has more than sys.maxint items");
1446 return NULL;
1447 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00001448 v = PyList_New(n);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001449 if (v == NULL)
1450 return NULL;
1451 for (i = 0; i < n; i++) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001452 PyObject *w = PyInt_FromLong(ilow);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001453 if (w == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001454 Py_DECREF(v);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001455 return NULL;
1456 }
Guido van Rossuma937d141998-04-24 18:22:02 +00001457 PyList_SET_ITEM(v, i, w);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001458 ilow += istep;
1459 }
1460 return v;
1461}
1462
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001463static char range_doc[] =
1464"range([start,] stop[, step]) -> list of integers\n\
1465\n\
1466Return a list containing an arithmetic progression of integers.\n\
1467range(i, j) returns [i, i+1, i+2, ..., j-1]; start (!) defaults to 0.\n\
1468When step is given, it specifies the increment (or decrement).\n\
1469For example, range(4) returns [0, 1, 2, 3]. The end point is omitted!\n\
1470These are exactly the valid indices for a list of 4 elements.";
1471
1472
Guido van Rossum79f25d91997-04-29 20:08:16 +00001473static PyObject *
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001474builtin_xrange(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001475 PyObject *self;
1476 PyObject *args;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001477{
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001478 long ilow = 0, ihigh = 0, istep = 1;
Guido van Rossum0865dd91995-01-17 16:30:22 +00001479 long n;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001480
Guido van Rossum79f25d91997-04-29 20:08:16 +00001481 if (PyTuple_Size(args) <= 1) {
1482 if (!PyArg_ParseTuple(args,
Guido van Rossum0865dd91995-01-17 16:30:22 +00001483 "l;xrange() requires 1-3 int arguments",
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001484 &ihigh))
1485 return NULL;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001486 }
1487 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001488 if (!PyArg_ParseTuple(args,
Guido van Rossum0865dd91995-01-17 16:30:22 +00001489 "ll|l;xrange() requires 1-3 int arguments",
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001490 &ilow, &ihigh, &istep))
Guido van Rossum12d12c51993-10-26 17:58:25 +00001491 return NULL;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001492 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001493 if (istep == 0) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001494 PyErr_SetString(PyExc_ValueError, "zero step for xrange()");
Guido van Rossum12d12c51993-10-26 17:58:25 +00001495 return NULL;
1496 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001497 /* XXX ought to check overflow of subtraction */
1498 if (istep > 0)
1499 n = (ihigh - ilow + istep - 1) / istep;
1500 else
1501 n = (ihigh - ilow + istep + 1) / istep;
1502 if (n < 0)
1503 n = 0;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001504 return PyRange_New(ilow, n, istep, 1);
Guido van Rossum12d12c51993-10-26 17:58:25 +00001505}
1506
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001507static char xrange_doc[] =
1508"xrange([start,] stop[, step]) -> xrange object\n\
1509\n\
1510Like range(), but instead of returning a list, returns an object that\n\
1511generates the numbers in the range on demand. This is slightly slower\n\
1512than range() but more memory efficient.";
1513
1514
Guido van Rossum79f25d91997-04-29 20:08:16 +00001515static PyObject *
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001516builtin_raw_input(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001517 PyObject *self;
1518 PyObject *args;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001519{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001520 PyObject *v = NULL;
1521 PyObject *f;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001522
Guido van Rossum79f25d91997-04-29 20:08:16 +00001523 if (!PyArg_ParseTuple(args, "|O:[raw_]input", &v))
Guido van Rossum3165fe61992-09-25 21:59:05 +00001524 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001525 if (PyFile_AsFile(PySys_GetObject("stdin")) == stdin &&
1526 PyFile_AsFile(PySys_GetObject("stdout")) == stdout &&
Guido van Rossum53bb7ff1995-07-26 16:26:31 +00001527 isatty(fileno(stdin)) && isatty(fileno(stdout))) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001528 PyObject *po;
Guido van Rossum872537c1995-07-07 22:43:42 +00001529 char *prompt;
1530 char *s;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001531 PyObject *result;
Guido van Rossum872537c1995-07-07 22:43:42 +00001532 if (v != NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001533 po = PyObject_Str(v);
Guido van Rossum872537c1995-07-07 22:43:42 +00001534 if (po == NULL)
1535 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001536 prompt = PyString_AsString(po);
Guido van Rossumd9b52081998-06-26 18:25:38 +00001537 if (prompt == NULL)
1538 return NULL;
Guido van Rossum872537c1995-07-07 22:43:42 +00001539 }
1540 else {
1541 po = NULL;
1542 prompt = "";
1543 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00001544 s = PyOS_Readline(prompt);
1545 Py_XDECREF(po);
Guido van Rossum872537c1995-07-07 22:43:42 +00001546 if (s == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001547 PyErr_SetNone(PyExc_KeyboardInterrupt);
Guido van Rossum872537c1995-07-07 22:43:42 +00001548 return NULL;
1549 }
1550 if (*s == '\0') {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001551 PyErr_SetNone(PyExc_EOFError);
Guido van Rossum872537c1995-07-07 22:43:42 +00001552 result = NULL;
1553 }
1554 else { /* strip trailing '\n' */
Guido van Rossum79f25d91997-04-29 20:08:16 +00001555 result = PyString_FromStringAndSize(s, strlen(s)-1);
Guido van Rossum872537c1995-07-07 22:43:42 +00001556 }
1557 free(s);
1558 return result;
1559 }
Guido van Rossum90933611991-06-07 16:10:43 +00001560 if (v != NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001561 f = PySys_GetObject("stdout");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001562 if (f == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001563 PyErr_SetString(PyExc_RuntimeError, "lost sys.stdout");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001564 return NULL;
1565 }
Guido van Rossumc8b6df91997-05-23 00:06:51 +00001566 if (Py_FlushLine() != 0 ||
1567 PyFile_WriteObject(v, f, Py_PRINT_RAW) != 0)
Guido van Rossum90933611991-06-07 16:10:43 +00001568 return NULL;
1569 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00001570 f = PySys_GetObject("stdin");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001571 if (f == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001572 PyErr_SetString(PyExc_RuntimeError, "lost sys.stdin");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001573 return NULL;
1574 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00001575 return PyFile_GetLine(f, -1);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001576}
1577
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001578static char raw_input_doc[] =
1579"raw_input([prompt]) -> string\n\
1580\n\
1581Read a string from standard input. The trailing newline is stripped.\n\
1582If the user hits EOF (Unix: Ctl-D, Windows: Ctl-Z+Return), raise EOFError.\n\
1583On Unix, GNU readline is used if enabled. The prompt string, if given,\n\
1584is printed without a trailing newline before reading.";
1585
1586
Guido van Rossum79f25d91997-04-29 20:08:16 +00001587static PyObject *
Guido van Rossum12d12c51993-10-26 17:58:25 +00001588builtin_reduce(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001589 PyObject *self;
1590 PyObject *args;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001591{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001592 PyObject *seq, *func, *result = NULL;
1593 PySequenceMethods *sqf;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001594 register int i;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001595
Guido van Rossum79f25d91997-04-29 20:08:16 +00001596 if (!PyArg_ParseTuple(args, "OO|O:reduce", &func, &seq, &result))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001597 return NULL;
1598 if (result != NULL)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001599 Py_INCREF(result);
Guido van Rossum12d12c51993-10-26 17:58:25 +00001600
Guido van Rossum09df08a1998-05-22 00:51:39 +00001601 sqf = seq->ob_type->tp_as_sequence;
1602 if (sqf == NULL || sqf->sq_item == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001603 PyErr_SetString(PyExc_TypeError,
Guido van Rossum12d12c51993-10-26 17:58:25 +00001604 "2nd argument to reduce() must be a sequence object");
1605 return NULL;
1606 }
1607
Guido van Rossum79f25d91997-04-29 20:08:16 +00001608 if ((args = PyTuple_New(2)) == NULL)
Guido van Rossum2d951851994-08-29 12:52:16 +00001609 goto Fail;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001610
Guido van Rossum2d951851994-08-29 12:52:16 +00001611 for (i = 0; ; ++i) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001612 PyObject *op2;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001613
1614 if (args->ob_refcnt > 1) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001615 Py_DECREF(args);
1616 if ((args = PyTuple_New(2)) == NULL)
Guido van Rossum2d951851994-08-29 12:52:16 +00001617 goto Fail;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001618 }
1619
Guido van Rossum2d951851994-08-29 12:52:16 +00001620 if ((op2 = (*sqf->sq_item)(seq, i)) == NULL) {
Barry Warsawcde8b1b1997-08-22 21:14:38 +00001621 if (PyErr_ExceptionMatches(PyExc_IndexError)) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001622 PyErr_Clear();
Guido van Rossum2d951851994-08-29 12:52:16 +00001623 break;
1624 }
1625 goto Fail;
1626 }
Guido van Rossum12d12c51993-10-26 17:58:25 +00001627
Guido van Rossum2d951851994-08-29 12:52:16 +00001628 if (result == NULL)
1629 result = op2;
1630 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001631 PyTuple_SetItem(args, 0, result);
1632 PyTuple_SetItem(args, 1, op2);
1633 if ((result = PyEval_CallObject(func, args)) == NULL)
Guido van Rossum2d951851994-08-29 12:52:16 +00001634 goto Fail;
1635 }
Guido van Rossum12d12c51993-10-26 17:58:25 +00001636 }
1637
Guido van Rossum79f25d91997-04-29 20:08:16 +00001638 Py_DECREF(args);
Guido van Rossum12d12c51993-10-26 17:58:25 +00001639
Guido van Rossum2d951851994-08-29 12:52:16 +00001640 if (result == NULL)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001641 PyErr_SetString(PyExc_TypeError,
Guido van Rossum2d951851994-08-29 12:52:16 +00001642 "reduce of empty sequence with no initial value");
1643
Guido van Rossum12d12c51993-10-26 17:58:25 +00001644 return result;
1645
Guido van Rossum2d951851994-08-29 12:52:16 +00001646Fail:
Guido van Rossum79f25d91997-04-29 20:08:16 +00001647 Py_XDECREF(args);
1648 Py_XDECREF(result);
Guido van Rossum12d12c51993-10-26 17:58:25 +00001649 return NULL;
1650}
1651
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001652static char reduce_doc[] =
1653"reduce(function, sequence[, initial]) -> value\n\
1654\n\
1655Apply a function of two arguments cumulatively to the items of a sequence,\n\
1656from left to right, so as to reduce the sequence to a single value.\n\
1657For example, reduce(lambda x, y: x+y, [1, 2, 3, 4, 5]) calculates\n\
1658((((1+2)+3)+4)+5). If initial is present, it is placed before the items\n\
1659of the sequence in the calculation, and serves as a default when the\n\
1660sequence is empty.";
1661
1662
Guido van Rossum79f25d91997-04-29 20:08:16 +00001663static PyObject *
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001664builtin_reload(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001665 PyObject *self;
1666 PyObject *args;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001667{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001668 PyObject *v;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001669
Guido van Rossum79f25d91997-04-29 20:08:16 +00001670 if (!PyArg_ParseTuple(args, "O:reload", &v))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001671 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001672 return PyImport_ReloadModule(v);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001673}
1674
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001675static char reload_doc[] =
1676"reload(module) -> module\n\
1677\n\
1678Reload the module. The module must have been successfully imported before.";
1679
1680
Guido van Rossum79f25d91997-04-29 20:08:16 +00001681static PyObject *
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001682builtin_repr(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001683 PyObject *self;
1684 PyObject *args;
Guido van Rossumc89705d1992-11-26 08:54:07 +00001685{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001686 PyObject *v;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001687
Guido van Rossum79f25d91997-04-29 20:08:16 +00001688 if (!PyArg_ParseTuple(args, "O:repr", &v))
Guido van Rossumc89705d1992-11-26 08:54:07 +00001689 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001690 return PyObject_Repr(v);
Guido van Rossumc89705d1992-11-26 08:54:07 +00001691}
1692
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001693static char repr_doc[] =
1694"repr(object) -> string\n\
1695\n\
1696Return the canonical string representation of the object.\n\
1697For most object types, eval(repr(object)) == object.";
1698
1699
Guido van Rossum79f25d91997-04-29 20:08:16 +00001700static PyObject *
Guido van Rossum9e51f9b1993-02-12 16:29:05 +00001701builtin_round(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001702 PyObject *self;
1703 PyObject *args;
Guido van Rossum9e51f9b1993-02-12 16:29:05 +00001704{
Guido van Rossum9e51f9b1993-02-12 16:29:05 +00001705 double x;
1706 double f;
1707 int ndigits = 0;
Guido van Rossum9e51f9b1993-02-12 16:29:05 +00001708 int i;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001709
Guido van Rossum79f25d91997-04-29 20:08:16 +00001710 if (!PyArg_ParseTuple(args, "d|i:round", &x, &ndigits))
Guido van Rossum9e51f9b1993-02-12 16:29:05 +00001711 return NULL;
Guido van Rossum9e51f9b1993-02-12 16:29:05 +00001712 f = 1.0;
Guido van Rossum1e162d31998-05-09 14:42:25 +00001713 i = abs(ndigits);
1714 while (--i >= 0)
Guido van Rossum9e51f9b1993-02-12 16:29:05 +00001715 f = f*10.0;
Guido van Rossum1e162d31998-05-09 14:42:25 +00001716 if (ndigits < 0)
1717 x /= f;
Guido van Rossum9e51f9b1993-02-12 16:29:05 +00001718 else
Guido van Rossum1e162d31998-05-09 14:42:25 +00001719 x *= f;
1720 if (x >= 0.0)
1721 x = floor(x + 0.5);
1722 else
1723 x = ceil(x - 0.5);
1724 if (ndigits < 0)
1725 x *= f;
1726 else
1727 x /= f;
1728 return PyFloat_FromDouble(x);
Guido van Rossum9e51f9b1993-02-12 16:29:05 +00001729}
1730
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001731static char round_doc[] =
1732"round(number[, ndigits]) -> floating point number\n\
1733\n\
1734Round a number to a given precision in decimal digits (default 0 digits).\n\
1735This always returns a floating point number. Precision may be negative.";
1736
1737
Guido van Rossum79f25d91997-04-29 20:08:16 +00001738static PyObject *
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001739builtin_str(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001740 PyObject *self;
1741 PyObject *args;
Guido van Rossumc89705d1992-11-26 08:54:07 +00001742{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001743 PyObject *v;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001744
Guido van Rossum79f25d91997-04-29 20:08:16 +00001745 if (!PyArg_ParseTuple(args, "O:str", &v))
Guido van Rossumc89705d1992-11-26 08:54:07 +00001746 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001747 return PyObject_Str(v);
Guido van Rossumc89705d1992-11-26 08:54:07 +00001748}
1749
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001750static char str_doc[] =
1751"str(object) -> string\n\
1752\n\
1753Return a nice string representation of the object.\n\
1754If the argument is a string, the return value is the same object.";
1755
1756
Guido van Rossum79f25d91997-04-29 20:08:16 +00001757static PyObject *
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001758builtin_tuple(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001759 PyObject *self;
1760 PyObject *args;
Guido van Rossumcae027b1994-08-29 12:53:11 +00001761{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001762 PyObject *v;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001763
Guido van Rossum79f25d91997-04-29 20:08:16 +00001764 if (!PyArg_ParseTuple(args, "O:tuple", &v))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001765 return NULL;
Guido van Rossum09df08a1998-05-22 00:51:39 +00001766 return PySequence_Tuple(v);
Guido van Rossumcae027b1994-08-29 12:53:11 +00001767}
1768
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001769static char tuple_doc[] =
1770"tuple(sequence) -> list\n\
1771\n\
1772Return a tuple whose items are the same as those of the argument sequence.\n\
1773If the argument is a tuple, the return value is the same object.";
1774
1775
Guido van Rossum79f25d91997-04-29 20:08:16 +00001776static PyObject *
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001777builtin_type(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001778 PyObject *self;
1779 PyObject *args;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001780{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001781 PyObject *v;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001782
Guido van Rossum79f25d91997-04-29 20:08:16 +00001783 if (!PyArg_ParseTuple(args, "O:type", &v))
Guido van Rossum3f5da241990-12-20 15:06:42 +00001784 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001785 v = (PyObject *)v->ob_type;
1786 Py_INCREF(v);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001787 return v;
1788}
1789
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001790static char type_doc[] =
1791"type(object) -> type object\n\
1792\n\
1793Return the type of the object.";
1794
1795
Guido van Rossum79f25d91997-04-29 20:08:16 +00001796static PyObject *
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001797builtin_vars(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001798 PyObject *self;
1799 PyObject *args;
Guido van Rossum2d951851994-08-29 12:52:16 +00001800{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001801 PyObject *v = NULL;
1802 PyObject *d;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001803
Guido van Rossum79f25d91997-04-29 20:08:16 +00001804 if (!PyArg_ParseTuple(args, "|O:vars", &v))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001805 return NULL;
Guido van Rossum2d951851994-08-29 12:52:16 +00001806 if (v == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001807 d = PyEval_GetLocals();
Guido van Rossum53bb7ff1995-07-26 16:26:31 +00001808 if (d == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001809 if (!PyErr_Occurred())
1810 PyErr_SetString(PyExc_SystemError,
1811 "no locals!?");
Guido van Rossum53bb7ff1995-07-26 16:26:31 +00001812 }
1813 else
Guido van Rossum79f25d91997-04-29 20:08:16 +00001814 Py_INCREF(d);
Guido van Rossum2d951851994-08-29 12:52:16 +00001815 }
1816 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001817 d = PyObject_GetAttrString(v, "__dict__");
Guido van Rossum2d951851994-08-29 12:52:16 +00001818 if (d == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001819 PyErr_SetString(PyExc_TypeError,
Guido van Rossum2d951851994-08-29 12:52:16 +00001820 "vars() argument must have __dict__ attribute");
1821 return NULL;
1822 }
1823 }
1824 return d;
1825}
1826
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001827static char vars_doc[] =
1828"vars([object]) -> dictionary\n\
1829\n\
1830Without arguments, equivalent to locals().\n\
1831With an argument, equivalent to object.__dict__.";
1832
1833
Barry Warsawcde8b1b1997-08-22 21:14:38 +00001834static PyObject *
1835builtin_isinstance(self, args)
1836 PyObject *self;
1837 PyObject *args;
1838{
1839 PyObject *inst;
1840 PyObject *cls;
1841 int retval;
1842
1843 if (!PyArg_ParseTuple(args, "OO", &inst, &cls))
1844 return NULL;
Guido van Rossumf5dd9141997-12-02 19:11:45 +00001845 if (PyType_Check(cls)) {
Guido van Rossumd6af46d1997-12-10 05:51:47 +00001846 retval = ((PyObject *)(inst->ob_type) == cls);
Barry Warsawcde8b1b1997-08-22 21:14:38 +00001847 }
Barry Warsawcde8b1b1997-08-22 21:14:38 +00001848 else {
Guido van Rossumf5dd9141997-12-02 19:11:45 +00001849 if (!PyClass_Check(cls)) {
1850 PyErr_SetString(PyExc_TypeError,
1851 "second argument must be a class");
1852 return NULL;
1853 }
1854
1855 if (!PyInstance_Check(inst))
1856 retval = 0;
1857 else {
1858 PyObject *inclass =
1859 (PyObject*)((PyInstanceObject*)inst)->in_class;
1860 retval = PyClass_IsSubclass(inclass, cls);
1861 }
Barry Warsawcde8b1b1997-08-22 21:14:38 +00001862 }
1863 return PyInt_FromLong(retval);
1864}
1865
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001866static char isinstance_doc[] =
1867"isinstance(object, class-or-type) -> Boolean\n\
1868\n\
1869Return whether an object is an instance of a class or of a subclass thereof.\n\
1870With a type as second argument, return whether that is the object's type.";
1871
Barry Warsawcde8b1b1997-08-22 21:14:38 +00001872
1873static PyObject *
1874builtin_issubclass(self, args)
1875 PyObject *self;
1876 PyObject *args;
1877{
1878 PyObject *derived;
1879 PyObject *cls;
1880 int retval;
1881
1882 if (!PyArg_ParseTuple(args, "OO", &derived, &cls))
1883 return NULL;
1884 if (!PyClass_Check(derived) || !PyClass_Check(cls)) {
1885 PyErr_SetString(PyExc_TypeError, "arguments must be classes");
1886 return NULL;
1887 }
1888 /* shortcut */
1889 if (!(retval = (derived == cls)))
1890 retval = PyClass_IsSubclass(derived, cls);
1891
1892 return PyInt_FromLong(retval);
1893}
1894
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001895static char issubclass_doc[] =
1896"issubclass(C, B) -> Boolean\n\
1897\n\
1898Return whether class C is a subclass (i.e., a derived class) of class B.";
1899
Barry Warsawcde8b1b1997-08-22 21:14:38 +00001900
Guido van Rossum79f25d91997-04-29 20:08:16 +00001901static PyMethodDef builtin_methods[] = {
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001902 {"__import__", builtin___import__, 1, import_doc},
1903 {"abs", builtin_abs, 1, abs_doc},
1904 {"apply", builtin_apply, 1, apply_doc},
1905 {"callable", builtin_callable, 1, callable_doc},
1906 {"chr", builtin_chr, 1, chr_doc},
1907 {"cmp", builtin_cmp, 1, cmp_doc},
1908 {"coerce", builtin_coerce, 1, coerce_doc},
1909 {"compile", builtin_compile, 1, compile_doc},
Guido van Rossum8a5c5d21996-01-12 01:09:56 +00001910#ifndef WITHOUT_COMPLEX
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001911 {"complex", builtin_complex, 1, complex_doc},
Guido van Rossum8a5c5d21996-01-12 01:09:56 +00001912#endif
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001913 {"delattr", builtin_delattr, 1, delattr_doc},
1914 {"dir", builtin_dir, 1, dir_doc},
1915 {"divmod", builtin_divmod, 1, divmod_doc},
1916 {"eval", builtin_eval, 1, eval_doc},
1917 {"execfile", builtin_execfile, 1, execfile_doc},
1918 {"filter", builtin_filter, 1, filter_doc},
1919 {"float", builtin_float, 1, float_doc},
1920 {"getattr", builtin_getattr, 1, getattr_doc},
1921 {"globals", builtin_globals, 1, globals_doc},
1922 {"hasattr", builtin_hasattr, 1, hasattr_doc},
1923 {"hash", builtin_hash, 1, hash_doc},
1924 {"hex", builtin_hex, 1, hex_doc},
1925 {"id", builtin_id, 1, id_doc},
1926 {"input", builtin_input, 1, input_doc},
1927 {"intern", builtin_intern, 1, intern_doc},
1928 {"int", builtin_int, 1, int_doc},
1929 {"isinstance", builtin_isinstance, 1, isinstance_doc},
1930 {"issubclass", builtin_issubclass, 1, issubclass_doc},
1931 {"len", builtin_len, 1, len_doc},
1932 {"list", builtin_list, 1, list_doc},
1933 {"locals", builtin_locals, 1, locals_doc},
1934 {"long", builtin_long, 1, long_doc},
1935 {"map", builtin_map, 1, map_doc},
1936 {"max", builtin_max, 1, max_doc},
1937 {"min", builtin_min, 1, min_doc},
1938 {"oct", builtin_oct, 1, oct_doc},
1939 {"open", builtin_open, 1, open_doc},
1940 {"ord", builtin_ord, 1, ord_doc},
1941 {"pow", builtin_pow, 1, pow_doc},
1942 {"range", builtin_range, 1, range_doc},
1943 {"raw_input", builtin_raw_input, 1, raw_input_doc},
1944 {"reduce", builtin_reduce, 1, reduce_doc},
1945 {"reload", builtin_reload, 1, reload_doc},
1946 {"repr", builtin_repr, 1, repr_doc},
1947 {"round", builtin_round, 1, round_doc},
1948 {"setattr", builtin_setattr, 1, setattr_doc},
1949 {"slice", builtin_slice, 1, slice_doc},
1950 {"str", builtin_str, 1, str_doc},
1951 {"tuple", builtin_tuple, 1, tuple_doc},
1952 {"type", builtin_type, 1, type_doc},
1953 {"vars", builtin_vars, 1, vars_doc},
1954 {"xrange", builtin_xrange, 1, xrange_doc},
Guido van Rossumc02e15c1991-12-16 13:03:00 +00001955 {NULL, NULL},
Guido van Rossum3f5da241990-12-20 15:06:42 +00001956};
1957
Guido van Rossum3f5da241990-12-20 15:06:42 +00001958/* Predefined exceptions */
1959
Guido van Rossum04748321997-09-16 18:43:15 +00001960PyObject *PyExc_Exception;
Barry Warsaw757af0e1997-08-29 22:13:51 +00001961PyObject *PyExc_StandardError;
Barry Warsaw412cdc21997-09-16 21:51:14 +00001962PyObject *PyExc_ArithmeticError;
Barry Warsaw757af0e1997-08-29 22:13:51 +00001963PyObject *PyExc_LookupError;
1964
Guido van Rossum79f25d91997-04-29 20:08:16 +00001965PyObject *PyExc_AssertionError;
1966PyObject *PyExc_AttributeError;
1967PyObject *PyExc_EOFError;
Guido van Rossumb6a7f771997-05-09 03:03:23 +00001968PyObject *PyExc_FloatingPointError;
Barry Warsawd086a1a1998-07-23 15:59:57 +00001969PyObject *PyExc_EnvironmentError;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001970PyObject *PyExc_IOError;
Barry Warsawd086a1a1998-07-23 15:59:57 +00001971PyObject *PyExc_OSError;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001972PyObject *PyExc_ImportError;
1973PyObject *PyExc_IndexError;
1974PyObject *PyExc_KeyError;
1975PyObject *PyExc_KeyboardInterrupt;
1976PyObject *PyExc_MemoryError;
1977PyObject *PyExc_NameError;
1978PyObject *PyExc_OverflowError;
1979PyObject *PyExc_RuntimeError;
Barry Warsaw344864f1998-12-01 18:52:06 +00001980PyObject *PyExc_NotImplementedError;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001981PyObject *PyExc_SyntaxError;
1982PyObject *PyExc_SystemError;
1983PyObject *PyExc_SystemExit;
1984PyObject *PyExc_TypeError;
1985PyObject *PyExc_ValueError;
1986PyObject *PyExc_ZeroDivisionError;
Guido van Rossum50afb7a1991-12-10 13:52:31 +00001987
Barry Warsaw757af0e1997-08-29 22:13:51 +00001988PyObject *PyExc_MemoryErrorInst;
1989
1990static struct
1991{
1992 char* name;
1993 PyObject** exc;
1994 int leaf_exc;
1995}
1996bltin_exc[] = {
Guido van Rossum04748321997-09-16 18:43:15 +00001997 {"Exception", &PyExc_Exception, 0},
Barry Warsaw757af0e1997-08-29 22:13:51 +00001998 {"StandardError", &PyExc_StandardError, 0},
Barry Warsaw412cdc21997-09-16 21:51:14 +00001999 {"ArithmeticError", &PyExc_ArithmeticError, 0},
Barry Warsaw757af0e1997-08-29 22:13:51 +00002000 {"LookupError", &PyExc_LookupError, 0},
2001 {"AssertionError", &PyExc_AssertionError, 1},
2002 {"AttributeError", &PyExc_AttributeError, 1},
2003 {"EOFError", &PyExc_EOFError, 1},
2004 {"FloatingPointError", &PyExc_FloatingPointError, 1},
Barry Warsawd086a1a1998-07-23 15:59:57 +00002005 {"EnvironmentError", &PyExc_EnvironmentError, 1},
Barry Warsaw757af0e1997-08-29 22:13:51 +00002006 {"IOError", &PyExc_IOError, 1},
Barry Warsawd086a1a1998-07-23 15:59:57 +00002007 {"OSError", &PyExc_OSError, 1},
Barry Warsaw757af0e1997-08-29 22:13:51 +00002008 {"ImportError", &PyExc_ImportError, 1},
2009 {"IndexError", &PyExc_IndexError, 1},
2010 {"KeyError", &PyExc_KeyError, 1},
2011 {"KeyboardInterrupt", &PyExc_KeyboardInterrupt, 1},
2012 {"MemoryError", &PyExc_MemoryError, 1},
2013 {"NameError", &PyExc_NameError, 1},
2014 {"OverflowError", &PyExc_OverflowError, 1},
2015 {"RuntimeError", &PyExc_RuntimeError, 1},
Barry Warsaw344864f1998-12-01 18:52:06 +00002016 {"NotImplementedError",&PyExc_NotImplementedError,1},
Barry Warsaw757af0e1997-08-29 22:13:51 +00002017 {"SyntaxError", &PyExc_SyntaxError, 1},
2018 {"SystemError", &PyExc_SystemError, 1},
2019 {"SystemExit", &PyExc_SystemExit, 1},
2020 {"TypeError", &PyExc_TypeError, 1},
2021 {"ValueError", &PyExc_ValueError, 1},
2022 {"ZeroDivisionError", &PyExc_ZeroDivisionError, 1},
2023 {NULL, NULL}
2024};
2025
2026
Barry Warsaw98b62461998-09-14 18:51:11 +00002027/* import exceptions module to extract class exceptions. on success,
2028 * return 1. on failure return 0 which signals _PyBuiltin_Init_2 to fall
2029 * back to using old-style string based exceptions.
2030 */
2031static int
Barry Warsaw757af0e1997-08-29 22:13:51 +00002032init_class_exc(dict)
2033 PyObject *dict;
2034{
2035 int i;
2036 PyObject *m = PyImport_ImportModule("exceptions");
Barry Warsaw98b62461998-09-14 18:51:11 +00002037 PyObject *args = NULL;
2038 PyObject *d = NULL;
Barry Warsaw757af0e1997-08-29 22:13:51 +00002039
Barry Warsaw98b62461998-09-14 18:51:11 +00002040 /* make sure we got the module and its dictionary */
Barry Warsaw757af0e1997-08-29 22:13:51 +00002041 if (m == NULL ||
2042 (d = PyModule_GetDict(m)) == NULL)
2043 {
Barry Warsaw98b62461998-09-14 18:51:11 +00002044 PySys_WriteStderr("'import exceptions' failed; ");
Barry Warsaw757af0e1997-08-29 22:13:51 +00002045 if (Py_VerboseFlag) {
Barry Warsaw98b62461998-09-14 18:51:11 +00002046 PySys_WriteStderr("traceback:\n");
Barry Warsaw757af0e1997-08-29 22:13:51 +00002047 PyErr_Print();
2048 }
2049 else {
Barry Warsaw98b62461998-09-14 18:51:11 +00002050 PySys_WriteStderr("use -v for traceback\n");
Barry Warsaw757af0e1997-08-29 22:13:51 +00002051 }
Barry Warsaw98b62461998-09-14 18:51:11 +00002052 goto finally;
Barry Warsaw757af0e1997-08-29 22:13:51 +00002053 }
2054 for (i = 0; bltin_exc[i].name; i++) {
2055 /* dig the exception out of the module */
2056 PyObject *exc = PyDict_GetItemString(d, bltin_exc[i].name);
Barry Warsaw98b62461998-09-14 18:51:11 +00002057 if (!exc) {
2058 PySys_WriteStderr(
2059 "Built-in exception class not found: %s. Library mismatch?\n",
2060 bltin_exc[i].name);
2061 goto finally;
2062 }
2063 /* free the old-style exception string object */
Barry Warsaw757af0e1997-08-29 22:13:51 +00002064 Py_XDECREF(*bltin_exc[i].exc);
2065
2066 /* squirrel away a pointer to the exception */
2067 Py_INCREF(exc);
2068 *bltin_exc[i].exc = exc;
2069
2070 /* and insert the name in the __builtin__ module */
Barry Warsaw98b62461998-09-14 18:51:11 +00002071 if (PyDict_SetItemString(dict, bltin_exc[i].name, exc)) {
2072 PySys_WriteStderr(
2073 "Cannot insert exception into __builtin__: %s\n",
2074 bltin_exc[i].name);
2075 goto finally;
2076 }
Barry Warsaw757af0e1997-08-29 22:13:51 +00002077 }
2078
2079 /* we need one pre-allocated instance */
2080 args = Py_BuildValue("()");
Barry Warsaw98b62461998-09-14 18:51:11 +00002081 if (!args ||
2082 !(PyExc_MemoryErrorInst =
2083 PyEval_CallObject(PyExc_MemoryError, args)))
2084 {
2085 PySys_WriteStderr("Cannot pre-allocate MemoryError instance\n");
2086 goto finally;
Barry Warsaw757af0e1997-08-29 22:13:51 +00002087 }
Barry Warsaw98b62461998-09-14 18:51:11 +00002088 Py_DECREF(args);
Barry Warsaw757af0e1997-08-29 22:13:51 +00002089
2090 /* we're done with the exceptions module */
2091 Py_DECREF(m);
2092
Barry Warsaw98b62461998-09-14 18:51:11 +00002093 if (PyErr_Occurred()) {
2094 PySys_WriteStderr("Cannot initialize standard class exceptions; ");
2095 if (Py_VerboseFlag) {
2096 PySys_WriteStderr("traceback:\n");
2097 PyErr_Print();
2098 }
2099 else
2100 PySys_WriteStderr("use -v for traceback\n");
2101 goto finally;
2102 }
2103 return 1;
2104 finally:
2105 Py_XDECREF(m);
2106 Py_XDECREF(args);
2107 PyErr_Clear();
2108 return 0;
Barry Warsaw757af0e1997-08-29 22:13:51 +00002109}
2110
2111
2112static void
2113fini_instances()
2114{
2115 Py_XDECREF(PyExc_MemoryErrorInst);
2116 PyExc_MemoryErrorInst = NULL;
2117}
2118
2119
Guido van Rossum79f25d91997-04-29 20:08:16 +00002120static PyObject *
Guido van Rossum25ce5661997-08-02 03:10:38 +00002121newstdexception(dict, name)
2122 PyObject *dict;
Guido van Rossumfb905c31991-12-16 15:42:38 +00002123 char *name;
Guido van Rossum3f5da241990-12-20 15:06:42 +00002124{
Guido van Rossum79f25d91997-04-29 20:08:16 +00002125 PyObject *v = PyString_FromString(name);
Guido van Rossum25ce5661997-08-02 03:10:38 +00002126 if (v == NULL || PyDict_SetItemString(dict, name, v) != 0)
Barry Warsaw98b62461998-09-14 18:51:11 +00002127 Py_FatalError("Cannot create string-based exceptions");
Guido van Rossum3f5da241990-12-20 15:06:42 +00002128 return v;
2129}
2130
2131static void
Guido van Rossum25ce5661997-08-02 03:10:38 +00002132initerrors(dict)
2133 PyObject *dict;
Guido van Rossum3f5da241990-12-20 15:06:42 +00002134{
Barry Warsaw757af0e1997-08-29 22:13:51 +00002135 int i;
2136 int exccnt = 0;
2137 for (i = 0; bltin_exc[i].name; i++, exccnt++) {
Barry Warsaw98b62461998-09-14 18:51:11 +00002138 Py_XDECREF(*bltin_exc[i].exc);
Barry Warsaw757af0e1997-08-29 22:13:51 +00002139 if (bltin_exc[i].leaf_exc)
2140 *bltin_exc[i].exc =
2141 newstdexception(dict, bltin_exc[i].name);
2142 }
2143
Barry Warsawd086a1a1998-07-23 15:59:57 +00002144 /* This is kind of bogus because we special case the some of the
2145 * new exceptions to be nearly forward compatible. But this means
2146 * we hard code knowledge about exceptions.py into C here. I don't
2147 * have a better solution, though.
2148 */
Barry Warsaw757af0e1997-08-29 22:13:51 +00002149 PyExc_LookupError = PyTuple_New(2);
2150 Py_INCREF(PyExc_IndexError);
2151 PyTuple_SET_ITEM(PyExc_LookupError, 0, PyExc_IndexError);
2152 Py_INCREF(PyExc_KeyError);
2153 PyTuple_SET_ITEM(PyExc_LookupError, 1, PyExc_KeyError);
2154 PyDict_SetItemString(dict, "LookupError", PyExc_LookupError);
2155
Barry Warsaw412cdc21997-09-16 21:51:14 +00002156 PyExc_ArithmeticError = PyTuple_New(3);
Barry Warsaw757af0e1997-08-29 22:13:51 +00002157 Py_INCREF(PyExc_OverflowError);
Barry Warsaw412cdc21997-09-16 21:51:14 +00002158 PyTuple_SET_ITEM(PyExc_ArithmeticError, 0, PyExc_OverflowError);
Barry Warsaw757af0e1997-08-29 22:13:51 +00002159 Py_INCREF(PyExc_ZeroDivisionError);
Barry Warsaw412cdc21997-09-16 21:51:14 +00002160 PyTuple_SET_ITEM(PyExc_ArithmeticError, 1, PyExc_ZeroDivisionError);
Barry Warsaw757af0e1997-08-29 22:13:51 +00002161 Py_INCREF(PyExc_FloatingPointError);
Barry Warsaw412cdc21997-09-16 21:51:14 +00002162 PyTuple_SET_ITEM(PyExc_ArithmeticError, 2, PyExc_FloatingPointError);
2163 PyDict_SetItemString(dict, "ArithmeticError", PyExc_ArithmeticError);
Barry Warsaw757af0e1997-08-29 22:13:51 +00002164
Barry Warsawd086a1a1998-07-23 15:59:57 +00002165 PyExc_EnvironmentError = PyTuple_New(2);
2166 Py_INCREF(PyExc_IOError);
2167 PyTuple_SET_ITEM(PyExc_EnvironmentError, 0, PyExc_IOError);
2168 Py_INCREF(PyExc_OSError);
2169 PyTuple_SET_ITEM(PyExc_EnvironmentError, 1, PyExc_OSError);
2170 PyDict_SetItemString(dict, "EnvironmentError", PyExc_EnvironmentError);
2171
Barry Warsawb01a7fa1997-09-18 03:44:38 +00002172 PyExc_StandardError = PyTuple_New(exccnt-2);
2173 for (i = 2; bltin_exc[i].name; i++) {
Barry Warsaw757af0e1997-08-29 22:13:51 +00002174 PyObject *exc = *bltin_exc[i].exc;
2175 Py_INCREF(exc);
Barry Warsawb01a7fa1997-09-18 03:44:38 +00002176 PyTuple_SET_ITEM(PyExc_StandardError, i-2, exc);
Barry Warsaw757af0e1997-08-29 22:13:51 +00002177 }
2178 PyDict_SetItemString(dict, "StandardError", PyExc_StandardError);
Guido van Rossum04748321997-09-16 18:43:15 +00002179
2180 /* Exception is treated differently; for now, it's == StandardError */
2181 PyExc_Exception = PyExc_StandardError;
2182 Py_INCREF(PyExc_Exception);
2183 PyDict_SetItemString(dict, "Exception", PyExc_Exception);
Barry Warsaw757af0e1997-08-29 22:13:51 +00002184
2185 if (PyErr_Occurred())
2186 Py_FatalError("Could not initialize built-in string exceptions");
Guido van Rossum25ce5661997-08-02 03:10:38 +00002187}
2188
2189static void
2190finierrors()
2191{
Barry Warsaw757af0e1997-08-29 22:13:51 +00002192 int i;
2193 for (i = 0; bltin_exc[i].name; i++) {
2194 PyObject *exc = *bltin_exc[i].exc;
2195 Py_XDECREF(exc);
2196 *bltin_exc[i].exc = NULL;
2197 }
Guido van Rossum25ce5661997-08-02 03:10:38 +00002198}
2199
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002200static char builtin_doc[] =
2201"Built-in functions, exceptions, and other objects.\n\
2202\n\
2203Noteworthy: None is the `nil' object; Ellipsis represents `...' in slices.";
2204
Guido van Rossum25ce5661997-08-02 03:10:38 +00002205PyObject *
Barry Warsaw757af0e1997-08-29 22:13:51 +00002206_PyBuiltin_Init_1()
Guido van Rossum25ce5661997-08-02 03:10:38 +00002207{
2208 PyObject *mod, *dict;
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002209 mod = Py_InitModule4("__builtin__", builtin_methods,
2210 builtin_doc, (PyObject *)NULL,
2211 PYTHON_API_VERSION);
Guido van Rossum25ce5661997-08-02 03:10:38 +00002212 if (mod == NULL)
2213 return NULL;
2214 dict = PyModule_GetDict(mod);
2215 initerrors(dict);
2216 if (PyDict_SetItemString(dict, "None", Py_None) < 0)
2217 return NULL;
2218 if (PyDict_SetItemString(dict, "Ellipsis", Py_Ellipsis) < 0)
2219 return NULL;
2220 if (PyDict_SetItemString(dict, "__debug__",
2221 PyInt_FromLong(Py_OptimizeFlag == 0)) < 0)
2222 return NULL;
Barry Warsaw757af0e1997-08-29 22:13:51 +00002223
Guido van Rossum25ce5661997-08-02 03:10:38 +00002224 return mod;
Guido van Rossum3f5da241990-12-20 15:06:42 +00002225}
2226
2227void
Barry Warsaw757af0e1997-08-29 22:13:51 +00002228_PyBuiltin_Init_2(dict)
2229 PyObject *dict;
2230{
2231 /* if Python was started with -X, initialize the class exceptions */
Barry Warsaw98b62461998-09-14 18:51:11 +00002232 if (Py_UseClassExceptionsFlag) {
2233 if (!init_class_exc(dict)) {
2234 /* class based exceptions could not be
2235 * initialized. Fall back to using string based
2236 * exceptions.
2237 */
2238 PySys_WriteStderr(
2239 "Warning! Falling back to string-based exceptions\n");
2240 initerrors(dict);
2241 }
2242 }
Barry Warsaw757af0e1997-08-29 22:13:51 +00002243}
2244
2245
2246void
2247_PyBuiltin_Fini_1()
2248{
2249 fini_instances();
2250}
2251
2252
2253void
2254_PyBuiltin_Fini_2()
Guido van Rossum3f5da241990-12-20 15:06:42 +00002255{
Guido van Rossum25ce5661997-08-02 03:10:38 +00002256 finierrors();
Guido van Rossum3f5da241990-12-20 15:06:42 +00002257}
Guido van Rossumc6bb8f71991-07-01 18:42:41 +00002258
Guido van Rossum12d12c51993-10-26 17:58:25 +00002259
Guido van Rossume77a7571993-11-03 15:01:26 +00002260/* Helper for filter(): filter a tuple through a function */
Guido van Rossum12d12c51993-10-26 17:58:25 +00002261
Guido van Rossum79f25d91997-04-29 20:08:16 +00002262static PyObject *
Guido van Rossum12d12c51993-10-26 17:58:25 +00002263filtertuple(func, tuple)
Guido van Rossum79f25d91997-04-29 20:08:16 +00002264 PyObject *func;
2265 PyObject *tuple;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002266{
Guido van Rossum79f25d91997-04-29 20:08:16 +00002267 PyObject *result;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002268 register int i, j;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002269 int len = PyTuple_Size(tuple);
Guido van Rossum12d12c51993-10-26 17:58:25 +00002270
Guido van Rossumb7b45621995-08-04 04:07:45 +00002271 if (len == 0) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002272 Py_INCREF(tuple);
Guido van Rossumb7b45621995-08-04 04:07:45 +00002273 return tuple;
2274 }
2275
Guido van Rossum79f25d91997-04-29 20:08:16 +00002276 if ((result = PyTuple_New(len)) == NULL)
Guido van Rossum2586bf01993-11-01 16:21:44 +00002277 return NULL;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002278
Guido van Rossum12d12c51993-10-26 17:58:25 +00002279 for (i = j = 0; i < len; ++i) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002280 PyObject *item, *good;
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002281 int ok;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002282
Guido van Rossum79f25d91997-04-29 20:08:16 +00002283 if ((item = PyTuple_GetItem(tuple, i)) == NULL)
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002284 goto Fail_1;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002285 if (func == Py_None) {
2286 Py_INCREF(item);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002287 good = item;
2288 }
2289 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002290 PyObject *arg = Py_BuildValue("(O)", item);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002291 if (arg == NULL)
2292 goto Fail_1;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002293 good = PyEval_CallObject(func, arg);
2294 Py_DECREF(arg);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002295 if (good == NULL)
Guido van Rossum12d12c51993-10-26 17:58:25 +00002296 goto Fail_1;
2297 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002298 ok = PyObject_IsTrue(good);
2299 Py_DECREF(good);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002300 if (ok) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002301 Py_INCREF(item);
2302 if (PyTuple_SetItem(result, j++, item) < 0)
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002303 goto Fail_1;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002304 }
Guido van Rossum12d12c51993-10-26 17:58:25 +00002305 }
2306
Guido van Rossum79f25d91997-04-29 20:08:16 +00002307 if (_PyTuple_Resize(&result, j, 0) < 0)
Guido van Rossum12d12c51993-10-26 17:58:25 +00002308 return NULL;
2309
Guido van Rossum12d12c51993-10-26 17:58:25 +00002310 return result;
2311
Guido van Rossum12d12c51993-10-26 17:58:25 +00002312Fail_1:
Guido van Rossum79f25d91997-04-29 20:08:16 +00002313 Py_DECREF(result);
Guido van Rossum12d12c51993-10-26 17:58:25 +00002314 return NULL;
2315}
2316
2317
Guido van Rossume77a7571993-11-03 15:01:26 +00002318/* Helper for filter(): filter a string through a function */
Guido van Rossum12d12c51993-10-26 17:58:25 +00002319
Guido van Rossum79f25d91997-04-29 20:08:16 +00002320static PyObject *
Guido van Rossum12d12c51993-10-26 17:58:25 +00002321filterstring(func, strobj)
Guido van Rossum79f25d91997-04-29 20:08:16 +00002322 PyObject *func;
2323 PyObject *strobj;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002324{
Guido van Rossum79f25d91997-04-29 20:08:16 +00002325 PyObject *result;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002326 register int i, j;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002327 int len = PyString_Size(strobj);
Guido van Rossum12d12c51993-10-26 17:58:25 +00002328
Guido van Rossum79f25d91997-04-29 20:08:16 +00002329 if (func == Py_None) {
Guido van Rossum2586bf01993-11-01 16:21:44 +00002330 /* No character is ever false -- share input string */
Guido van Rossum79f25d91997-04-29 20:08:16 +00002331 Py_INCREF(strobj);
Guido van Rossum2d951851994-08-29 12:52:16 +00002332 return strobj;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002333 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002334 if ((result = PyString_FromStringAndSize(NULL, len)) == NULL)
Guido van Rossum2586bf01993-11-01 16:21:44 +00002335 return NULL;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002336
Guido van Rossum12d12c51993-10-26 17:58:25 +00002337 for (i = j = 0; i < len; ++i) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002338 PyObject *item, *arg, *good;
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002339 int ok;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002340
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002341 item = (*strobj->ob_type->tp_as_sequence->sq_item)(strobj, i);
2342 if (item == NULL)
2343 goto Fail_1;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002344 arg = Py_BuildValue("(O)", item);
2345 Py_DECREF(item);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002346 if (arg == NULL)
2347 goto Fail_1;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002348 good = PyEval_CallObject(func, arg);
2349 Py_DECREF(arg);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002350 if (good == NULL)
2351 goto Fail_1;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002352 ok = PyObject_IsTrue(good);
2353 Py_DECREF(good);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002354 if (ok)
Guido van Rossum79f25d91997-04-29 20:08:16 +00002355 PyString_AS_STRING((PyStringObject *)result)[j++] =
2356 PyString_AS_STRING((PyStringObject *)item)[0];
Guido van Rossum12d12c51993-10-26 17:58:25 +00002357 }
2358
Guido van Rossum79f25d91997-04-29 20:08:16 +00002359 if (j < len && _PyString_Resize(&result, j) < 0)
Guido van Rossum12d12c51993-10-26 17:58:25 +00002360 return NULL;
2361
Guido van Rossum12d12c51993-10-26 17:58:25 +00002362 return result;
2363
Guido van Rossum12d12c51993-10-26 17:58:25 +00002364Fail_1:
Guido van Rossum79f25d91997-04-29 20:08:16 +00002365 Py_DECREF(result);
Guido van Rossum12d12c51993-10-26 17:58:25 +00002366 return NULL;
2367}