blob: d23026182e5d799197fd71992bb08fec1563c620 [file] [log] [blame]
Guido van Rossumf70e43a1991-02-19 12:39:46 +00001/***********************************************************
Guido van Rossum6d023c91995-01-04 19:12:13 +00002Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
3The Netherlands.
Guido van Rossumf70e43a1991-02-19 12:39:46 +00004
5 All Rights Reserved
6
Guido van Rossumd266eb41996-10-25 14:44:06 +00007Permission to use, copy, modify, and distribute this software and its
8documentation for any purpose and without fee is hereby granted,
Guido van Rossumf70e43a1991-02-19 12:39:46 +00009provided that the above copyright notice appear in all copies and that
Guido van Rossumd266eb41996-10-25 14:44:06 +000010both that copyright notice and this permission notice appear in
Guido van Rossumf70e43a1991-02-19 12:39:46 +000011supporting documentation, and that the names of Stichting Mathematisch
Guido van Rossumd266eb41996-10-25 14:44:06 +000012Centrum or CWI or Corporation for National Research Initiatives or
13CNRI not be used in advertising or publicity pertaining to
14distribution of the software without specific, written prior
15permission.
Guido van Rossumf70e43a1991-02-19 12:39:46 +000016
Guido van Rossumd266eb41996-10-25 14:44:06 +000017While CWI is the initial source for this software, a modified version
18is made available by the Corporation for National Research Initiatives
19(CNRI) at the Internet address ftp://ftp.python.org.
20
21STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH
22REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF
23MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH
24CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
25DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
26PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
27TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
28PERFORMANCE OF THIS SOFTWARE.
Guido van Rossumf70e43a1991-02-19 12:39:46 +000029
30******************************************************************/
31
Guido van Rossum3f5da241990-12-20 15:06:42 +000032/* Built-in functions */
33
Guido van Rossum79f25d91997-04-29 20:08:16 +000034#include "Python.h"
Guido van Rossum3f5da241990-12-20 15:06:42 +000035
36#include "node.h"
Guido van Rossum5b722181993-03-30 17:46:03 +000037#include "compile.h"
38#include "eval.h"
Guido van Rossum3f5da241990-12-20 15:06:42 +000039
Guido van Rossumfe4b6ee1996-08-08 18:49:41 +000040#include "mymath.h"
41
Guido van Rossum6bf62da1997-04-11 20:37:35 +000042#include <ctype.h>
43
Guido van Rossum1a2c5cb1996-12-10 15:37:36 +000044#ifdef HAVE_UNISTD_H
45#include <unistd.h>
46#endif
47
Guido van Rossum12d12c51993-10-26 17:58:25 +000048/* Forward */
Guido van Rossum79f25d91997-04-29 20:08:16 +000049static PyObject *filterstring Py_PROTO((PyObject *, PyObject *));
50static PyObject *filtertuple Py_PROTO((PyObject *, PyObject *));
Guido van Rossum12d12c51993-10-26 17:58:25 +000051
Guido van Rossum79f25d91997-04-29 20:08:16 +000052static PyObject *
Guido van Rossum1ae940a1995-01-02 19:04:15 +000053builtin___import__(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +000054 PyObject *self;
55 PyObject *args;
Guido van Rossum3f5da241990-12-20 15:06:42 +000056{
Guido van Rossum1ae940a1995-01-02 19:04:15 +000057 char *name;
Guido van Rossum79f25d91997-04-29 20:08:16 +000058 PyObject *globals = NULL;
59 PyObject *locals = NULL;
60 PyObject *fromlist = NULL;
Guido van Rossum1ae940a1995-01-02 19:04:15 +000061
Guido van Rossum79f25d91997-04-29 20:08:16 +000062 if (!PyArg_ParseTuple(args, "s|OOO:__import__",
Guido van Rossum24c13741995-02-14 09:42:43 +000063 &name, &globals, &locals, &fromlist))
Guido van Rossum1ae940a1995-01-02 19:04:15 +000064 return NULL;
Guido van Rossumaee0bad1997-09-05 07:33:22 +000065 return PyImport_ImportModuleEx(name, globals, locals, fromlist);
Guido van Rossum1ae940a1995-01-02 19:04:15 +000066}
67
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +000068static char import_doc[] =
69"__import__(name, globals, locals, fromlist) -> module\n\
70\n\
71Import a module. The globals are only used to determine the context;\n\
72they are not modified. The locals are currently unused. The fromlist\n\
73should be a list of names to emulate ``from name import ...'', or an\n\
74empty list to emulate ``import name''.\n\
75When importing a module from a package, note that __import__('A.B', ...)\n\
76returns package A when fromlist is empty, but its submodule B when\n\
77fromlist is not empty.";
78
Guido van Rossum1ae940a1995-01-02 19:04:15 +000079
Guido van Rossum79f25d91997-04-29 20:08:16 +000080static PyObject *
Guido van Rossum1ae940a1995-01-02 19:04:15 +000081builtin_abs(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +000082 PyObject *self;
83 PyObject *args;
Guido van Rossum1ae940a1995-01-02 19:04:15 +000084{
Guido van Rossum79f25d91997-04-29 20:08:16 +000085 PyObject *v;
Guido van Rossum1ae940a1995-01-02 19:04:15 +000086
Guido van Rossum79f25d91997-04-29 20:08:16 +000087 if (!PyArg_ParseTuple(args, "O:abs", &v))
Guido van Rossum1ae940a1995-01-02 19:04:15 +000088 return NULL;
Guido van Rossum09df08a1998-05-22 00:51:39 +000089 return PyNumber_Absolute(v);
Guido van Rossum3f5da241990-12-20 15:06:42 +000090}
91
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +000092static char abs_doc[] =
93"abs(number) -> number\n\
94\n\
95Return the absolute value of the argument.";
96
97
Guido van Rossum79f25d91997-04-29 20:08:16 +000098static PyObject *
Guido van Rossum94390a41992-08-14 15:14:30 +000099builtin_apply(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000100 PyObject *self;
101 PyObject *args;
Guido van Rossumc02e15c1991-12-16 13:03:00 +0000102{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000103 PyObject *func, *alist = NULL, *kwdict = NULL;
Barry Warsaw968f8cb1998-10-01 15:33:12 +0000104 PyObject *t = NULL, *retval = NULL;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000105
Guido van Rossum79f25d91997-04-29 20:08:16 +0000106 if (!PyArg_ParseTuple(args, "O|OO:apply", &func, &alist, &kwdict))
Guido van Rossumc02e15c1991-12-16 13:03:00 +0000107 return NULL;
Barry Warsaw968f8cb1998-10-01 15:33:12 +0000108 if (alist != NULL) {
109 if (!PyTuple_Check(alist)) {
110 if (!PySequence_Check(alist)) {
111 PyErr_SetString(PyExc_TypeError,
112 "apply() 2nd argument must be a sequence");
113 return NULL;
114 }
115 t = PySequence_Tuple(alist);
116 if (t == NULL)
117 return NULL;
118 alist = t;
119 }
Guido van Rossum2d951851994-08-29 12:52:16 +0000120 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000121 if (kwdict != NULL && !PyDict_Check(kwdict)) {
122 PyErr_SetString(PyExc_TypeError,
Guido van Rossum681d79a1995-07-18 14:51:37 +0000123 "apply() 3rd argument must be dictionary");
Barry Warsaw968f8cb1998-10-01 15:33:12 +0000124 goto finally;
Guido van Rossum681d79a1995-07-18 14:51:37 +0000125 }
Barry Warsaw968f8cb1998-10-01 15:33:12 +0000126 retval = PyEval_CallObjectWithKeywords(func, alist, kwdict);
127 finally:
128 Py_XDECREF(t);
129 return retval;
Guido van Rossumc02e15c1991-12-16 13:03:00 +0000130}
131
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000132static char apply_doc[] =
133"apply(function, args[, kwargs]) -> value\n\
134\n\
135Call a function with positional arguments taken from the tuple args,\n\
136and keyword arguments taken from the optional dictionary kwargs.";
137
138
Guido van Rossum79f25d91997-04-29 20:08:16 +0000139static PyObject *
Guido van Rossum2d951851994-08-29 12:52:16 +0000140builtin_callable(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000141 PyObject *self;
142 PyObject *args;
Guido van Rossum2d951851994-08-29 12:52:16 +0000143{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000144 PyObject *v;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000145
Guido van Rossum79f25d91997-04-29 20:08:16 +0000146 if (!PyArg_ParseTuple(args, "O:callable", &v))
Guido van Rossum2d951851994-08-29 12:52:16 +0000147 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000148 return PyInt_FromLong((long)PyCallable_Check(v));
Guido van Rossum2d951851994-08-29 12:52:16 +0000149}
150
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000151static char callable_doc[] =
152"callable(object) -> Boolean\n\
153\n\
154Return whether the object is callable (i.e., some kind of function).\n\
155Note that classes are callable, as are instances with a __call__() method.";
156
157
Guido van Rossum79f25d91997-04-29 20:08:16 +0000158static PyObject *
Guido van Rossume77a7571993-11-03 15:01:26 +0000159builtin_filter(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000160 PyObject *self;
161 PyObject *args;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000162{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000163 PyObject *func, *seq, *result;
164 PySequenceMethods *sqf;
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000165 int len;
166 register int i, j;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000167
Guido van Rossum79f25d91997-04-29 20:08:16 +0000168 if (!PyArg_ParseTuple(args, "OO:filter", &func, &seq))
Guido van Rossum12d12c51993-10-26 17:58:25 +0000169 return NULL;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000170
Guido van Rossum79f25d91997-04-29 20:08:16 +0000171 if (PyString_Check(seq)) {
172 PyObject *r = filterstring(func, seq);
Guido van Rossum12d12c51993-10-26 17:58:25 +0000173 return r;
174 }
175
Guido van Rossum79f25d91997-04-29 20:08:16 +0000176 if (PyTuple_Check(seq)) {
177 PyObject *r = filtertuple(func, seq);
Guido van Rossum12d12c51993-10-26 17:58:25 +0000178 return r;
179 }
180
Guido van Rossum09df08a1998-05-22 00:51:39 +0000181 sqf = seq->ob_type->tp_as_sequence;
182 if (sqf == NULL || sqf->sq_length == NULL || sqf->sq_item == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000183 PyErr_SetString(PyExc_TypeError,
Guido van Rossume77a7571993-11-03 15:01:26 +0000184 "argument 2 to filter() must be a sequence type");
Guido van Rossum12d12c51993-10-26 17:58:25 +0000185 goto Fail_2;
186 }
187
188 if ((len = (*sqf->sq_length)(seq)) < 0)
189 goto Fail_2;
190
Guido van Rossum79f25d91997-04-29 20:08:16 +0000191 if (PyList_Check(seq) && seq->ob_refcnt == 1) {
192 Py_INCREF(seq);
Guido van Rossum12d12c51993-10-26 17:58:25 +0000193 result = seq;
194 }
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000195 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000196 if ((result = PyList_New(len)) == NULL)
Guido van Rossum12d12c51993-10-26 17:58:25 +0000197 goto Fail_2;
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000198 }
Guido van Rossum12d12c51993-10-26 17:58:25 +0000199
Guido van Rossum2d951851994-08-29 12:52:16 +0000200 for (i = j = 0; ; ++i) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000201 PyObject *item, *good;
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000202 int ok;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000203
Guido van Rossum2d951851994-08-29 12:52:16 +0000204 if ((item = (*sqf->sq_item)(seq, i)) == NULL) {
Barry Warsawcde8b1b1997-08-22 21:14:38 +0000205 if (PyErr_ExceptionMatches(PyExc_IndexError)) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000206 PyErr_Clear();
Guido van Rossum2d951851994-08-29 12:52:16 +0000207 break;
208 }
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000209 goto Fail_1;
Guido van Rossum2d951851994-08-29 12:52:16 +0000210 }
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000211
Guido van Rossum79f25d91997-04-29 20:08:16 +0000212 if (func == Py_None) {
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000213 good = item;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000214 Py_INCREF(good);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000215 }
216 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000217 PyObject *arg = Py_BuildValue("(O)", item);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000218 if (arg == NULL)
219 goto Fail_1;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000220 good = PyEval_CallObject(func, arg);
221 Py_DECREF(arg);
Guido van Rossum58b68731995-01-10 17:40:55 +0000222 if (good == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000223 Py_DECREF(item);
Guido van Rossum12d12c51993-10-26 17:58:25 +0000224 goto Fail_1;
Guido van Rossum58b68731995-01-10 17:40:55 +0000225 }
Guido van Rossum12d12c51993-10-26 17:58:25 +0000226 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000227 ok = PyObject_IsTrue(good);
228 Py_DECREF(good);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000229 if (ok) {
Guido van Rossum2d951851994-08-29 12:52:16 +0000230 if (j < len) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000231 if (PyList_SetItem(result, j++, item) < 0)
Guido van Rossum2d951851994-08-29 12:52:16 +0000232 goto Fail_1;
233 }
234 else {
235 j++;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000236 if (PyList_Append(result, item) < 0)
Guido van Rossum2d951851994-08-29 12:52:16 +0000237 goto Fail_1;
238 }
Guido van Rossum58b68731995-01-10 17:40:55 +0000239 } else {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000240 Py_DECREF(item);
Guido van Rossum12d12c51993-10-26 17:58:25 +0000241 }
Guido van Rossum12d12c51993-10-26 17:58:25 +0000242 }
243
Guido van Rossum12d12c51993-10-26 17:58:25 +0000244
Guido van Rossum79f25d91997-04-29 20:08:16 +0000245 if (j < len && PyList_SetSlice(result, j, len, NULL) < 0)
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000246 goto Fail_1;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000247
Guido van Rossum12d12c51993-10-26 17:58:25 +0000248 return result;
249
Guido van Rossum12d12c51993-10-26 17:58:25 +0000250Fail_1:
Guido van Rossum79f25d91997-04-29 20:08:16 +0000251 Py_DECREF(result);
Guido van Rossum12d12c51993-10-26 17:58:25 +0000252Fail_2:
Guido van Rossum12d12c51993-10-26 17:58:25 +0000253 return NULL;
254}
255
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000256static char filter_doc[] =
257"filter(function, sequence) -> list\n\
258\n\
259Return a list containing those items of sequence for which function(item)\n\
260is true. If function is None, return a list of items that are true.";
261
262
Guido van Rossum79f25d91997-04-29 20:08:16 +0000263static PyObject *
Guido van Rossum94390a41992-08-14 15:14:30 +0000264builtin_chr(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000265 PyObject *self;
266 PyObject *args;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000267{
268 long x;
269 char s[1];
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000270
Guido van Rossum79f25d91997-04-29 20:08:16 +0000271 if (!PyArg_ParseTuple(args, "l:chr", &x))
Guido van Rossum3f5da241990-12-20 15:06:42 +0000272 return NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000273 if (x < 0 || x >= 256) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000274 PyErr_SetString(PyExc_ValueError,
275 "chr() arg not in range(256)");
Guido van Rossum3f5da241990-12-20 15:06:42 +0000276 return NULL;
277 }
Guido van Rossum6bf62da1997-04-11 20:37:35 +0000278 s[0] = (char)x;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000279 return PyString_FromStringAndSize(s, 1);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000280}
281
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000282static char chr_doc[] =
283"chr(i) -> character\n\
284\n\
285Return a string of one character with ordinal i; 0 <= i < 256.";
286
287
Guido van Rossum79f25d91997-04-29 20:08:16 +0000288static PyObject *
Guido van Rossuma9e7dc11992-10-18 18:53:57 +0000289builtin_cmp(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000290 PyObject *self;
291 PyObject *args;
Guido van Rossuma9e7dc11992-10-18 18:53:57 +0000292{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000293 PyObject *a, *b;
Guido van Rossum09df08a1998-05-22 00:51:39 +0000294 int c;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000295
Guido van Rossum79f25d91997-04-29 20:08:16 +0000296 if (!PyArg_ParseTuple(args, "OO:cmp", &a, &b))
Guido van Rossuma9e7dc11992-10-18 18:53:57 +0000297 return NULL;
Guido van Rossum09df08a1998-05-22 00:51:39 +0000298 if (PyObject_Cmp(a, b, &c) < 0)
Guido van Rossumc8b6df91997-05-23 00:06:51 +0000299 return NULL;
Guido van Rossum09df08a1998-05-22 00:51:39 +0000300 return PyInt_FromLong((long)c);
Guido van Rossuma9e7dc11992-10-18 18:53:57 +0000301}
302
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000303static char cmp_doc[] =
304"cmp(x, y) -> integer\n\
305\n\
306Return negative if x<y, zero if x==y, positive if x>y.";
307
308
Guido van Rossum79f25d91997-04-29 20:08:16 +0000309static PyObject *
Guido van Rossum5524a591995-01-10 15:26:20 +0000310builtin_coerce(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000311 PyObject *self;
312 PyObject *args;
Guido van Rossum6a00cd81995-01-07 12:39:01 +0000313{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000314 PyObject *v, *w;
315 PyObject *res;
Guido van Rossum5524a591995-01-10 15:26:20 +0000316
Guido van Rossum79f25d91997-04-29 20:08:16 +0000317 if (!PyArg_ParseTuple(args, "OO:coerce", &v, &w))
Guido van Rossum5524a591995-01-10 15:26:20 +0000318 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000319 if (PyNumber_Coerce(&v, &w) < 0)
Guido van Rossum04691fc1992-08-12 15:35:34 +0000320 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000321 res = Py_BuildValue("(OO)", v, w);
322 Py_DECREF(v);
323 Py_DECREF(w);
Guido van Rossum04691fc1992-08-12 15:35:34 +0000324 return res;
325}
326
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000327static char coerce_doc[] =
328"coerce(x, y) -> None or (x1, y1)\n\
329\n\
330When x and y can be coerced to values of the same type, return a tuple\n\
331containing the coerced values. When they can't be coerced, return None.";
332
333
Guido van Rossum79f25d91997-04-29 20:08:16 +0000334static PyObject *
Guido van Rossum5b722181993-03-30 17:46:03 +0000335builtin_compile(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000336 PyObject *self;
337 PyObject *args;
Guido van Rossum5b722181993-03-30 17:46:03 +0000338{
339 char *str;
340 char *filename;
341 char *startstr;
342 int start;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000343
Guido van Rossum79f25d91997-04-29 20:08:16 +0000344 if (!PyArg_ParseTuple(args, "sss:compile", &str, &filename, &startstr))
Guido van Rossum5b722181993-03-30 17:46:03 +0000345 return NULL;
346 if (strcmp(startstr, "exec") == 0)
Guido van Rossumb05a5c71997-05-07 17:46:13 +0000347 start = Py_file_input;
Guido van Rossum5b722181993-03-30 17:46:03 +0000348 else if (strcmp(startstr, "eval") == 0)
Guido van Rossumb05a5c71997-05-07 17:46:13 +0000349 start = Py_eval_input;
Guido van Rossum872537c1995-07-07 22:43:42 +0000350 else if (strcmp(startstr, "single") == 0)
Guido van Rossumb05a5c71997-05-07 17:46:13 +0000351 start = Py_single_input;
Guido van Rossum5b722181993-03-30 17:46:03 +0000352 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000353 PyErr_SetString(PyExc_ValueError,
Guido van Rossum872537c1995-07-07 22:43:42 +0000354 "compile() mode must be 'exec' or 'eval' or 'single'");
Guido van Rossum5b722181993-03-30 17:46:03 +0000355 return NULL;
356 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000357 return Py_CompileString(str, filename, start);
Guido van Rossum5b722181993-03-30 17:46:03 +0000358}
359
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000360static char compile_doc[] =
361"compile(source, filename, mode) -> code object\n\
362\n\
363Compile the source string (a Python module, statement or expression)\n\
364into a code object that can be executed by the exec statement or eval().\n\
365The filename will be used for run-time error messages.\n\
366The mode must be 'exec' to compile a module, 'single' to compile a\n\
367single (interactive) statement, or 'eval' to compile an expression.";
368
369
Guido van Rossum8a5c5d21996-01-12 01:09:56 +0000370#ifndef WITHOUT_COMPLEX
371
Guido van Rossum79f25d91997-04-29 20:08:16 +0000372static PyObject *
Guido van Rossum8a5c5d21996-01-12 01:09:56 +0000373builtin_complex(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000374 PyObject *self;
375 PyObject *args;
Guido van Rossum8a5c5d21996-01-12 01:09:56 +0000376{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000377 PyObject *r, *i, *tmp;
378 PyNumberMethods *nbr, *nbi = NULL;
Guido van Rossum530956d1996-07-21 02:27:43 +0000379 Py_complex cr, ci;
Guido van Rossumc6472e91997-03-31 17:15:43 +0000380 int own_r = 0;
Guido van Rossum8a5c5d21996-01-12 01:09:56 +0000381
382 i = NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000383 if (!PyArg_ParseTuple(args, "O|O:complex", &r, &i))
Guido van Rossum8a5c5d21996-01-12 01:09:56 +0000384 return NULL;
385 if ((nbr = r->ob_type->tp_as_number) == NULL ||
Guido van Rossumc6472e91997-03-31 17:15:43 +0000386 nbr->nb_float == NULL ||
387 (i != NULL &&
388 ((nbi = i->ob_type->tp_as_number) == NULL ||
389 nbi->nb_float == NULL))) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000390 PyErr_SetString(PyExc_TypeError,
Guido van Rossum8a5c5d21996-01-12 01:09:56 +0000391 "complex() argument can't be converted to complex");
392 return NULL;
393 }
Guido van Rossumed0af8f1996-12-05 23:18:18 +0000394 /* XXX Hack to support classes with __complex__ method */
Guido van Rossum79f25d91997-04-29 20:08:16 +0000395 if (PyInstance_Check(r)) {
396 static PyObject *complexstr;
397 PyObject *f;
Guido van Rossumed0af8f1996-12-05 23:18:18 +0000398 if (complexstr == NULL) {
Guido van Rossum8d751611997-01-18 08:04:16 +0000399 complexstr = PyString_InternFromString("__complex__");
Guido van Rossumed0af8f1996-12-05 23:18:18 +0000400 if (complexstr == NULL)
401 return NULL;
402 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000403 f = PyObject_GetAttr(r, complexstr);
Guido van Rossumed0af8f1996-12-05 23:18:18 +0000404 if (f == NULL)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000405 PyErr_Clear();
Guido van Rossumed0af8f1996-12-05 23:18:18 +0000406 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000407 PyObject *args = Py_BuildValue("()");
Guido van Rossumed0af8f1996-12-05 23:18:18 +0000408 if (args == NULL)
409 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000410 r = PyEval_CallObject(f, args);
411 Py_DECREF(args);
Guido van Rossumed0af8f1996-12-05 23:18:18 +0000412 if (r == NULL)
413 return NULL;
Guido van Rossumc6472e91997-03-31 17:15:43 +0000414 own_r = 1;
Guido van Rossumed0af8f1996-12-05 23:18:18 +0000415 }
416 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000417 if (PyComplex_Check(r)) {
418 cr = ((PyComplexObject*)r)->cval;
Guido van Rossum730806d1998-04-10 22:27:42 +0000419 if (own_r) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000420 Py_DECREF(r);
Guido van Rossum730806d1998-04-10 22:27:42 +0000421 }
Guido van Rossumc6472e91997-03-31 17:15:43 +0000422 }
Guido van Rossum8a5c5d21996-01-12 01:09:56 +0000423 else {
Guido van Rossumfe4b6ee1996-08-08 18:49:41 +0000424 tmp = (*nbr->nb_float)(r);
Guido van Rossum730806d1998-04-10 22:27:42 +0000425 if (own_r) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000426 Py_DECREF(r);
Guido van Rossum730806d1998-04-10 22:27:42 +0000427 }
Guido van Rossumfe4b6ee1996-08-08 18:49:41 +0000428 if (tmp == NULL)
429 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000430 cr.real = PyFloat_AsDouble(tmp);
431 Py_DECREF(tmp);
Guido van Rossum8a5c5d21996-01-12 01:09:56 +0000432 cr.imag = 0.;
433 }
434 if (i == NULL) {
435 ci.real = 0.;
436 ci.imag = 0.;
437 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000438 else if (PyComplex_Check(i))
439 ci = ((PyComplexObject*)i)->cval;
Guido van Rossum8a5c5d21996-01-12 01:09:56 +0000440 else {
Guido van Rossumc6472e91997-03-31 17:15:43 +0000441 tmp = (*nbi->nb_float)(i);
Guido van Rossumfe4b6ee1996-08-08 18:49:41 +0000442 if (tmp == NULL)
443 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000444 ci.real = PyFloat_AsDouble(tmp);
445 Py_DECREF(tmp);
Guido van Rossum8a5c5d21996-01-12 01:09:56 +0000446 ci.imag = 0.;
447 }
448 cr.real -= ci.imag;
449 cr.imag += ci.real;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000450 return PyComplex_FromCComplex(cr);
Guido van Rossum8a5c5d21996-01-12 01:09:56 +0000451}
452
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000453static char complex_doc[] =
454"complex(real[, imag]) -> complex number\n\
455\n\
456Create a complex number from a real part and an optional imaginary part.\n\
457This is equivalent to (real + imag*1j) where imag defaults to 0.";
458
459
Guido van Rossum8a5c5d21996-01-12 01:09:56 +0000460#endif
461
Guido van Rossum79f25d91997-04-29 20:08:16 +0000462static PyObject *
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000463builtin_dir(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000464 PyObject *self;
465 PyObject *args;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000466{
Guido van Rossum666b17a1997-05-06 16:36:57 +0000467 static char *attrlist[] = {"__members__", "__methods__", NULL};
468 PyObject *v = NULL, *l = NULL, *m = NULL;
469 PyObject *d, *x;
470 int i;
471 char **s;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000472
Guido van Rossum79f25d91997-04-29 20:08:16 +0000473 if (!PyArg_ParseTuple(args, "|O:dir", &v))
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000474 return NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000475 if (v == NULL) {
Guido van Rossum666b17a1997-05-06 16:36:57 +0000476 x = PyEval_GetLocals();
477 if (x == NULL)
478 goto error;
479 l = PyMapping_Keys(x);
480 if (l == NULL)
481 goto error;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000482 }
483 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000484 d = PyObject_GetAttrString(v, "__dict__");
Guido van Rossum666b17a1997-05-06 16:36:57 +0000485 if (d == NULL)
Guido van Rossum795ba581996-05-23 22:49:07 +0000486 PyErr_Clear();
Guido van Rossum666b17a1997-05-06 16:36:57 +0000487 else {
488 l = PyMapping_Keys(d);
489 if (l == NULL)
490 PyErr_Clear();
491 Py_DECREF(d);
492 }
493 if (l == NULL) {
494 l = PyList_New(0);
495 if (l == NULL)
496 goto error;
497 }
498 for (s = attrlist; *s != NULL; s++) {
499 m = PyObject_GetAttrString(v, *s);
500 if (m == NULL) {
501 PyErr_Clear();
502 continue;
503 }
504 for (i = 0; ; i++) {
505 x = PySequence_GetItem(m, i);
506 if (x == NULL) {
507 PyErr_Clear();
508 break;
509 }
510 if (PyList_Append(l, x) != 0) {
511 Py_DECREF(x);
512 Py_DECREF(m);
513 goto error;
514 }
515 Py_DECREF(x);
516 }
517 Py_DECREF(m);
Guido van Rossum795ba581996-05-23 22:49:07 +0000518 }
Guido van Rossum006bcd41991-10-24 14:54:44 +0000519 }
Guido van Rossum666b17a1997-05-06 16:36:57 +0000520 if (PyList_Sort(l) != 0)
521 goto error;
522 return l;
523 error:
524 Py_XDECREF(l);
525 return NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000526}
527
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000528static char dir_doc[] =
529"dir([object]) -> list of strings\n\
530\n\
531Return an alphabetized list of names comprising (some of) the attributes\n\
532of the given object. Without an argument, the names in the current scope\n\
533are listed. With an instance argument, only the instance attributes are\n\
534returned. With a class argument, attributes of the base class are not\n\
535returned. For other types or arguments, this may list members or methods.";
536
537
Guido van Rossum79f25d91997-04-29 20:08:16 +0000538static PyObject *
Guido van Rossum6a00cd81995-01-07 12:39:01 +0000539builtin_divmod(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000540 PyObject *self;
541 PyObject *args;
Guido van Rossum6a00cd81995-01-07 12:39:01 +0000542{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000543 PyObject *v, *w;
Guido van Rossum6a00cd81995-01-07 12:39:01 +0000544
Guido van Rossum79f25d91997-04-29 20:08:16 +0000545 if (!PyArg_ParseTuple(args, "OO:divmod", &v, &w))
Guido van Rossum6a00cd81995-01-07 12:39:01 +0000546 return NULL;
Guido van Rossum09df08a1998-05-22 00:51:39 +0000547 return PyNumber_Divmod(v, w);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000548}
549
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000550static char divmod_doc[] =
551"divmod(x, y) -> (div, mod)\n\
552\n\
553Return the tuple ((x-x%y)/y, x%y). Invariant: div*y + mod == x.";
554
555
Guido van Rossum79f25d91997-04-29 20:08:16 +0000556static PyObject *
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000557builtin_eval(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000558 PyObject *self;
559 PyObject *args;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000560{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000561 PyObject *cmd;
562 PyObject *globals = Py_None, *locals = Py_None;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000563 char *str;
Guido van Rossum590baa41993-11-30 13:40:46 +0000564
Guido van Rossum79f25d91997-04-29 20:08:16 +0000565 if (!PyArg_ParseTuple(args, "O|O!O!:eval",
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000566 &cmd,
Guido van Rossum79f25d91997-04-29 20:08:16 +0000567 &PyDict_Type, &globals,
568 &PyDict_Type, &locals))
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000569 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000570 if (globals == Py_None) {
571 globals = PyEval_GetGlobals();
572 if (locals == Py_None)
573 locals = PyEval_GetLocals();
Guido van Rossum6135a871995-01-09 17:53:26 +0000574 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000575 else if (locals == Py_None)
Guido van Rossum6135a871995-01-09 17:53:26 +0000576 locals = globals;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000577 if (PyDict_GetItemString(globals, "__builtins__") == NULL) {
578 if (PyDict_SetItemString(globals, "__builtins__",
579 PyEval_GetBuiltins()) != 0)
Guido van Rossum6135a871995-01-09 17:53:26 +0000580 return NULL;
581 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000582 if (PyCode_Check(cmd))
583 return PyEval_EvalCode((PyCodeObject *) cmd, globals, locals);
584 if (!PyString_Check(cmd)) {
585 PyErr_SetString(PyExc_TypeError,
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000586 "eval() argument 1 must be string or code object");
Guido van Rossum94390a41992-08-14 15:14:30 +0000587 return NULL;
588 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000589 str = PyString_AsString(cmd);
590 if ((int)strlen(str) != PyString_Size(cmd)) {
591 PyErr_SetString(PyExc_ValueError,
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000592 "embedded '\\0' in string arg");
593 return NULL;
Guido van Rossumf08ab0a1992-03-04 16:41:41 +0000594 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000595 while (*str == ' ' || *str == '\t')
596 str++;
Guido van Rossumb05a5c71997-05-07 17:46:13 +0000597 return PyRun_String(str, Py_eval_input, globals, locals);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000598}
599
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000600static char eval_doc[] =
601"eval(source[, globals[, locals]]) -> value\n\
602\n\
603Evaluate the source in the context of globals and locals.\n\
604The source may be a string representing a Python expression\n\
605or a code object as returned by compile().\n\
606The globals and locals are dictionaries, defaulting to the current\n\
607globals and locals. If only globals is given, locals defaults to it.";
608
609
Guido van Rossum79f25d91997-04-29 20:08:16 +0000610static PyObject *
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000611builtin_execfile(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000612 PyObject *self;
613 PyObject *args;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000614{
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000615 char *filename;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000616 PyObject *globals = Py_None, *locals = Py_None;
617 PyObject *res;
Guido van Rossum0f61f8a1992-02-25 18:55:05 +0000618 FILE* fp;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000619
Guido van Rossum79f25d91997-04-29 20:08:16 +0000620 if (!PyArg_ParseTuple(args, "s|O!O!:execfile",
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000621 &filename,
Guido van Rossum79f25d91997-04-29 20:08:16 +0000622 &PyDict_Type, &globals,
623 &PyDict_Type, &locals))
Guido van Rossum0f61f8a1992-02-25 18:55:05 +0000624 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000625 if (globals == Py_None) {
626 globals = PyEval_GetGlobals();
627 if (locals == Py_None)
628 locals = PyEval_GetLocals();
Guido van Rossum6135a871995-01-09 17:53:26 +0000629 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000630 else if (locals == Py_None)
Guido van Rossum6135a871995-01-09 17:53:26 +0000631 locals = globals;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000632 if (PyDict_GetItemString(globals, "__builtins__") == NULL) {
633 if (PyDict_SetItemString(globals, "__builtins__",
634 PyEval_GetBuiltins()) != 0)
Guido van Rossum6135a871995-01-09 17:53:26 +0000635 return NULL;
636 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000637 Py_BEGIN_ALLOW_THREADS
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000638 fp = fopen(filename, "r");
Guido van Rossum79f25d91997-04-29 20:08:16 +0000639 Py_END_ALLOW_THREADS
Guido van Rossum0f61f8a1992-02-25 18:55:05 +0000640 if (fp == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000641 PyErr_SetFromErrno(PyExc_IOError);
Guido van Rossum0f61f8a1992-02-25 18:55:05 +0000642 return NULL;
643 }
Guido van Rossumb05a5c71997-05-07 17:46:13 +0000644 res = PyRun_File(fp, filename, Py_file_input, globals, locals);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000645 Py_BEGIN_ALLOW_THREADS
Guido van Rossum0f61f8a1992-02-25 18:55:05 +0000646 fclose(fp);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000647 Py_END_ALLOW_THREADS
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000648 return res;
Guido van Rossum0f61f8a1992-02-25 18:55:05 +0000649}
650
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000651static char execfile_doc[] =
652"execfile(filename[, globals[, locals]])\n\
653\n\
654Read and execute a Python script from a file.\n\
655The globals and locals are dictionaries, defaulting to the current\n\
656globals and locals. If only globals is given, locals defaults to it.";
657
658
Guido van Rossum79f25d91997-04-29 20:08:16 +0000659static PyObject *
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000660builtin_float(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000661 PyObject *self;
662 PyObject *args;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000663{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000664 PyObject *v;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000665
Guido van Rossum79f25d91997-04-29 20:08:16 +0000666 if (!PyArg_ParseTuple(args, "O:float", &v))
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000667 return NULL;
Guido van Rossum09df08a1998-05-22 00:51:39 +0000668 return PyNumber_Float(v);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000669}
670
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000671static char float_doc[] =
672"float(x) -> floating point number\n\
673\n\
674Convert a string or number to a floating point number, if possible.";
675
676
Guido van Rossum79f25d91997-04-29 20:08:16 +0000677static PyObject *
Guido van Rossum94390a41992-08-14 15:14:30 +0000678builtin_getattr(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000679 PyObject *self;
680 PyObject *args;
Guido van Rossum33894be1992-01-27 16:53:09 +0000681{
Guido van Rossum950ff291998-06-29 13:38:57 +0000682 PyObject *v, *result, *dflt = NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000683 PyObject *name;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000684
Guido van Rossum950ff291998-06-29 13:38:57 +0000685 if (!PyArg_ParseTuple(args, "OS|O:getattr", &v, &name, &dflt))
Guido van Rossum33894be1992-01-27 16:53:09 +0000686 return NULL;
Guido van Rossum950ff291998-06-29 13:38:57 +0000687 result = PyObject_GetAttr(v, name);
688 if (result == NULL && dflt != NULL) {
689 PyErr_Clear();
690 Py_INCREF(dflt);
691 result = dflt;
692 }
693 return result;
Guido van Rossum9bfef441993-03-29 10:43:31 +0000694}
695
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000696static char getattr_doc[] =
Guido van Rossum950ff291998-06-29 13:38:57 +0000697"getattr(object, name[, default]) -> value\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000698\n\
Guido van Rossum950ff291998-06-29 13:38:57 +0000699Get a named attribute from an object; getattr(x, 'y') is equivalent to x.y.\n\
700When a default argument is given, it is returned when the attribute doesn't\n\
701exist; without it, an exception is raised in that case.";
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000702
703
Guido van Rossum79f25d91997-04-29 20:08:16 +0000704static PyObject *
Guido van Rossum872537c1995-07-07 22:43:42 +0000705builtin_globals(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000706 PyObject *self;
707 PyObject *args;
Guido van Rossum872537c1995-07-07 22:43:42 +0000708{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000709 PyObject *d;
Guido van Rossum872537c1995-07-07 22:43:42 +0000710
Guido van Rossum79f25d91997-04-29 20:08:16 +0000711 if (!PyArg_ParseTuple(args, ""))
Guido van Rossum872537c1995-07-07 22:43:42 +0000712 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000713 d = PyEval_GetGlobals();
714 Py_INCREF(d);
Guido van Rossum872537c1995-07-07 22:43:42 +0000715 return d;
716}
717
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000718static char globals_doc[] =
719"globals() -> dictionary\n\
720\n\
721Return the dictionary containing the current scope's global variables.";
722
723
Guido van Rossum79f25d91997-04-29 20:08:16 +0000724static PyObject *
Guido van Rossum9bfef441993-03-29 10:43:31 +0000725builtin_hasattr(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000726 PyObject *self;
727 PyObject *args;
Guido van Rossum9bfef441993-03-29 10:43:31 +0000728{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000729 PyObject *v;
730 PyObject *name;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000731
Guido van Rossum79f25d91997-04-29 20:08:16 +0000732 if (!PyArg_ParseTuple(args, "OS:hasattr", &v, &name))
Guido van Rossum9bfef441993-03-29 10:43:31 +0000733 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000734 v = PyObject_GetAttr(v, name);
Guido van Rossum9bfef441993-03-29 10:43:31 +0000735 if (v == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000736 PyErr_Clear();
Guido van Rossum09df08a1998-05-22 00:51:39 +0000737 Py_INCREF(Py_False);
738 return Py_False;
Guido van Rossum9bfef441993-03-29 10:43:31 +0000739 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000740 Py_DECREF(v);
Guido van Rossum09df08a1998-05-22 00:51:39 +0000741 Py_INCREF(Py_True);
742 return Py_True;
Guido van Rossum33894be1992-01-27 16:53:09 +0000743}
744
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000745static char hasattr_doc[] =
746"hasattr(object, name) -> Boolean\n\
747\n\
748Return whether the object has an attribute with the given name.\n\
749(This is done by calling getattr(object, name) and catching exceptions.)";
750
751
Guido van Rossum79f25d91997-04-29 20:08:16 +0000752static PyObject *
Guido van Rossum5b722181993-03-30 17:46:03 +0000753builtin_id(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000754 PyObject *self;
755 PyObject *args;
Guido van Rossum5b722181993-03-30 17:46:03 +0000756{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000757 PyObject *v;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000758
Guido van Rossum79f25d91997-04-29 20:08:16 +0000759 if (!PyArg_ParseTuple(args, "O:id", &v))
Guido van Rossum5b722181993-03-30 17:46:03 +0000760 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000761 return PyInt_FromLong((long)v);
Guido van Rossum5b722181993-03-30 17:46:03 +0000762}
763
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000764static char id_doc[] =
765"id(object) -> integer\n\
766\n\
767Return the identity of an object. This is guaranteed to be unique among\n\
768simultaneously existing objects. (Hint: it's the object's memory address.)";
769
770
Guido van Rossum79f25d91997-04-29 20:08:16 +0000771static PyObject *
Guido van Rossum12d12c51993-10-26 17:58:25 +0000772builtin_map(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000773 PyObject *self;
774 PyObject *args;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000775{
776 typedef struct {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000777 PyObject *seq;
778 PySequenceMethods *sqf;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000779 int len;
780 } sequence;
781
Guido van Rossum79f25d91997-04-29 20:08:16 +0000782 PyObject *func, *result;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000783 sequence *seqs = NULL, *sqp;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000784 int n, len;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000785 register int i, j;
786
Guido van Rossum79f25d91997-04-29 20:08:16 +0000787 n = PyTuple_Size(args);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000788 if (n < 2) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000789 PyErr_SetString(PyExc_TypeError,
790 "map() requires at least two args");
Guido van Rossum12d12c51993-10-26 17:58:25 +0000791 return NULL;
792 }
793
Guido van Rossum79f25d91997-04-29 20:08:16 +0000794 func = PyTuple_GetItem(args, 0);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000795 n--;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000796
Guido van Rossumfa4ac711998-07-10 17:37:30 +0000797 if (func == Py_None && n == 1) {
798 /* map(None, S) is the same as list(S). */
799 return PySequence_List(PyTuple_GetItem(args, 1));
800 }
801
Guido van Rossum79f25d91997-04-29 20:08:16 +0000802 if ((seqs = PyMem_NEW(sequence, n)) == NULL) {
803 PyErr_NoMemory();
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000804 goto Fail_2;
805 }
Guido van Rossum12d12c51993-10-26 17:58:25 +0000806
Guido van Rossum2d951851994-08-29 12:52:16 +0000807 for (len = 0, i = 0, sqp = seqs; i < n; ++i, ++sqp) {
Guido van Rossum12d12c51993-10-26 17:58:25 +0000808 int curlen;
Guido van Rossum09df08a1998-05-22 00:51:39 +0000809 PySequenceMethods *sqf;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000810
Guido van Rossum79f25d91997-04-29 20:08:16 +0000811 if ((sqp->seq = PyTuple_GetItem(args, i + 1)) == NULL)
Guido van Rossum12d12c51993-10-26 17:58:25 +0000812 goto Fail_2;
813
Guido van Rossum09df08a1998-05-22 00:51:39 +0000814 sqp->sqf = sqf = sqp->seq->ob_type->tp_as_sequence;
815 if (sqf == NULL ||
816 sqf->sq_length == NULL ||
817 sqf->sq_item == NULL)
818 {
Guido van Rossum12d12c51993-10-26 17:58:25 +0000819 static char errmsg[] =
820 "argument %d to map() must be a sequence object";
Guido van Rossum15e33a41997-04-30 19:00:27 +0000821 char errbuf[sizeof(errmsg) + 25];
Guido van Rossum12d12c51993-10-26 17:58:25 +0000822
823 sprintf(errbuf, errmsg, i+2);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000824 PyErr_SetString(PyExc_TypeError, errbuf);
Guido van Rossum12d12c51993-10-26 17:58:25 +0000825 goto Fail_2;
826 }
827
828 if ((curlen = sqp->len = (*sqp->sqf->sq_length)(sqp->seq)) < 0)
829 goto Fail_2;
830
831 if (curlen > len)
832 len = curlen;
833 }
834
Guido van Rossum79f25d91997-04-29 20:08:16 +0000835 if ((result = (PyObject *) PyList_New(len)) == NULL)
Guido van Rossum12d12c51993-10-26 17:58:25 +0000836 goto Fail_2;
837
Guido van Rossum2d951851994-08-29 12:52:16 +0000838 for (i = 0; ; ++i) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000839 PyObject *alist, *item=NULL, *value;
Guido van Rossum2d951851994-08-29 12:52:16 +0000840 int any = 0;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000841
Guido van Rossum79f25d91997-04-29 20:08:16 +0000842 if (func == Py_None && n == 1)
Guido van Rossum32120311995-07-10 13:52:21 +0000843 alist = NULL;
Guido van Rossum2d951851994-08-29 12:52:16 +0000844 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000845 if ((alist = PyTuple_New(n)) == NULL)
Guido van Rossum2d951851994-08-29 12:52:16 +0000846 goto Fail_1;
847 }
Guido van Rossum12d12c51993-10-26 17:58:25 +0000848
849 for (j = 0, sqp = seqs; j < n; ++j, ++sqp) {
Guido van Rossum2d951851994-08-29 12:52:16 +0000850 if (sqp->len < 0) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000851 Py_INCREF(Py_None);
852 item = Py_None;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000853 }
Guido van Rossum12d12c51993-10-26 17:58:25 +0000854 else {
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000855 item = (*sqp->sqf->sq_item)(sqp->seq, i);
Guido van Rossum2d951851994-08-29 12:52:16 +0000856 if (item == NULL) {
Barry Warsawcde8b1b1997-08-22 21:14:38 +0000857 if (PyErr_ExceptionMatches(
858 PyExc_IndexError))
859 {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000860 PyErr_Clear();
861 Py_INCREF(Py_None);
862 item = Py_None;
Guido van Rossum2d951851994-08-29 12:52:16 +0000863 sqp->len = -1;
864 }
865 else {
866 goto Fail_0;
867 }
868 }
869 else
870 any = 1;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000871
Guido van Rossum12d12c51993-10-26 17:58:25 +0000872 }
Guido van Rossum32120311995-07-10 13:52:21 +0000873 if (!alist)
Guido van Rossum2d951851994-08-29 12:52:16 +0000874 break;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000875 if (PyTuple_SetItem(alist, j, item) < 0) {
876 Py_DECREF(item);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000877 goto Fail_0;
Guido van Rossum2d951851994-08-29 12:52:16 +0000878 }
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000879 continue;
880
881 Fail_0:
Guido van Rossum79f25d91997-04-29 20:08:16 +0000882 Py_XDECREF(alist);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000883 goto Fail_1;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000884 }
885
Guido van Rossum32120311995-07-10 13:52:21 +0000886 if (!alist)
887 alist = item;
Guido van Rossum2d951851994-08-29 12:52:16 +0000888
889 if (!any) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000890 Py_DECREF(alist);
Guido van Rossum2d951851994-08-29 12:52:16 +0000891 break;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000892 }
Guido van Rossum2d951851994-08-29 12:52:16 +0000893
Guido van Rossum79f25d91997-04-29 20:08:16 +0000894 if (func == Py_None)
Guido van Rossum32120311995-07-10 13:52:21 +0000895 value = alist;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000896 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000897 value = PyEval_CallObject(func, alist);
898 Py_DECREF(alist);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000899 if (value == NULL)
900 goto Fail_1;
Guido van Rossum2d951851994-08-29 12:52:16 +0000901 }
902 if (i >= len) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000903 if (PyList_Append(result, value) < 0)
Guido van Rossum2d951851994-08-29 12:52:16 +0000904 goto Fail_1;
905 }
906 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000907 if (PyList_SetItem(result, i, value) < 0)
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000908 goto Fail_1;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000909 }
910 }
911
Guido van Rossumfa4ac711998-07-10 17:37:30 +0000912 if (i < len && PyList_SetSlice(result, i, len, NULL) < 0)
913 goto Fail_1;
914
Guido van Rossum79f25d91997-04-29 20:08:16 +0000915 PyMem_DEL(seqs);
Guido van Rossum12d12c51993-10-26 17:58:25 +0000916 return result;
917
Guido van Rossum12d12c51993-10-26 17:58:25 +0000918Fail_1:
Guido van Rossum79f25d91997-04-29 20:08:16 +0000919 Py_DECREF(result);
Guido van Rossum12d12c51993-10-26 17:58:25 +0000920Fail_2:
Guido van Rossum79f25d91997-04-29 20:08:16 +0000921 if (seqs) PyMem_DEL(seqs);
Guido van Rossum12d12c51993-10-26 17:58:25 +0000922 return NULL;
923}
924
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000925static char map_doc[] =
926"map(function, sequence[, sequence, ...]) -> list\n\
927\n\
928Return a list of the results of applying the function to the items of\n\
929the argument sequence(s). If more than one sequence is given, the\n\
930function is called with an argument list consisting of the corresponding\n\
931item of each sequence, substituting None for missing values when not all\n\
932sequences have the same length. If the function is None, return a list of\n\
933the items of the sequence (or a list of tuples if more than one sequence).";
934
935
Guido van Rossum79f25d91997-04-29 20:08:16 +0000936static PyObject *
Guido van Rossum94390a41992-08-14 15:14:30 +0000937builtin_setattr(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000938 PyObject *self;
939 PyObject *args;
Guido van Rossum33894be1992-01-27 16:53:09 +0000940{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000941 PyObject *v;
942 PyObject *name;
943 PyObject *value;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000944
Guido van Rossum79f25d91997-04-29 20:08:16 +0000945 if (!PyArg_ParseTuple(args, "OSO:setattr", &v, &name, &value))
Guido van Rossum33894be1992-01-27 16:53:09 +0000946 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000947 if (PyObject_SetAttr(v, name, value) != 0)
Guido van Rossum33894be1992-01-27 16:53:09 +0000948 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000949 Py_INCREF(Py_None);
950 return Py_None;
Guido van Rossum33894be1992-01-27 16:53:09 +0000951}
952
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000953static char setattr_doc[] =
954"setattr(object, name, value)\n\
955\n\
956Set a named attribute on an object; setattr(x, 'y', v) is equivalent to\n\
957``x.y = v''.";
958
959
Guido van Rossum79f25d91997-04-29 20:08:16 +0000960static PyObject *
Guido van Rossum14144fc1994-08-29 12:53:40 +0000961builtin_delattr(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000962 PyObject *self;
963 PyObject *args;
Guido van Rossum14144fc1994-08-29 12:53:40 +0000964{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000965 PyObject *v;
966 PyObject *name;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000967
Guido van Rossum79f25d91997-04-29 20:08:16 +0000968 if (!PyArg_ParseTuple(args, "OS:delattr", &v, &name))
Guido van Rossum14144fc1994-08-29 12:53:40 +0000969 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000970 if (PyObject_SetAttr(v, name, (PyObject *)NULL) != 0)
Guido van Rossum14144fc1994-08-29 12:53:40 +0000971 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000972 Py_INCREF(Py_None);
973 return Py_None;
Guido van Rossum14144fc1994-08-29 12:53:40 +0000974}
975
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000976static char delattr_doc[] =
Guido van Rossumdf12a591998-11-23 22:13:04 +0000977"delattr(object, name)\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000978\n\
979Delete a named attribute on an object; delattr(x, 'y') is equivalent to\n\
980``del x.y''.";
981
982
Guido van Rossum79f25d91997-04-29 20:08:16 +0000983static PyObject *
Guido van Rossum9bfef441993-03-29 10:43:31 +0000984builtin_hash(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000985 PyObject *self;
986 PyObject *args;
Guido van Rossum9bfef441993-03-29 10:43:31 +0000987{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000988 PyObject *v;
Guido van Rossum9bfef441993-03-29 10:43:31 +0000989 long x;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000990
Guido van Rossum79f25d91997-04-29 20:08:16 +0000991 if (!PyArg_ParseTuple(args, "O:hash", &v))
Guido van Rossum9bfef441993-03-29 10:43:31 +0000992 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000993 x = PyObject_Hash(v);
Guido van Rossum9bfef441993-03-29 10:43:31 +0000994 if (x == -1)
995 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000996 return PyInt_FromLong(x);
Guido van Rossum9bfef441993-03-29 10:43:31 +0000997}
998
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000999static char hash_doc[] =
1000"hash(object) -> integer\n\
1001\n\
1002Return a hash value for the object. Two objects with the same value have\n\
1003the same hash value. The reverse is not necessarily true, but likely.";
1004
1005
Guido van Rossum79f25d91997-04-29 20:08:16 +00001006static PyObject *
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001007builtin_hex(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001008 PyObject *self;
1009 PyObject *args;
Guido van Rossum006bcd41991-10-24 14:54:44 +00001010{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001011 PyObject *v;
1012 PyNumberMethods *nb;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001013
Guido van Rossum79f25d91997-04-29 20:08:16 +00001014 if (!PyArg_ParseTuple(args, "O:hex", &v))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001015 return NULL;
Guido van Rossum1899c2e1992-09-12 11:09:23 +00001016
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001017 if ((nb = v->ob_type->tp_as_number) == NULL ||
Guido van Rossum1899c2e1992-09-12 11:09:23 +00001018 nb->nb_hex == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001019 PyErr_SetString(PyExc_TypeError,
Guido van Rossum1899c2e1992-09-12 11:09:23 +00001020 "hex() argument can't be converted to hex");
1021 return NULL;
Guido van Rossum006bcd41991-10-24 14:54:44 +00001022 }
Guido van Rossum1899c2e1992-09-12 11:09:23 +00001023 return (*nb->nb_hex)(v);
Guido van Rossum006bcd41991-10-24 14:54:44 +00001024}
1025
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001026static char hex_doc[] =
1027"hex(number) -> string\n\
1028\n\
1029Return the hexadecimal representation of an integer or long integer.";
1030
1031
Guido van Rossum79f25d91997-04-29 20:08:16 +00001032static PyObject *builtin_raw_input Py_PROTO((PyObject *, PyObject *));
Guido van Rossum3165fe61992-09-25 21:59:05 +00001033
Guido van Rossum79f25d91997-04-29 20:08:16 +00001034static PyObject *
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001035builtin_input(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001036 PyObject *self;
1037 PyObject *args;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001038{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001039 PyObject *line;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001040 char *str;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001041 PyObject *res;
1042 PyObject *globals, *locals;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001043
1044 line = builtin_raw_input(self, args);
Guido van Rossum3165fe61992-09-25 21:59:05 +00001045 if (line == NULL)
1046 return line;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001047 if (!PyArg_Parse(line, "s;embedded '\\0' in input line", &str))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001048 return NULL;
1049 while (*str == ' ' || *str == '\t')
1050 str++;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001051 globals = PyEval_GetGlobals();
1052 locals = PyEval_GetLocals();
1053 if (PyDict_GetItemString(globals, "__builtins__") == NULL) {
1054 if (PyDict_SetItemString(globals, "__builtins__",
1055 PyEval_GetBuiltins()) != 0)
Guido van Rossum6135a871995-01-09 17:53:26 +00001056 return NULL;
1057 }
Guido van Rossumb05a5c71997-05-07 17:46:13 +00001058 res = PyRun_String(str, Py_eval_input, globals, locals);
Guido van Rossum79f25d91997-04-29 20:08:16 +00001059 Py_DECREF(line);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001060 return res;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001061}
1062
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001063static char input_doc[] =
1064"input([prompt]) -> value\n\
1065\n\
1066Equivalent to eval(raw_input(prompt)).";
1067
1068
Guido van Rossume8811f81997-02-14 15:48:05 +00001069static PyObject *
1070builtin_intern(self, args)
1071 PyObject *self;
1072 PyObject *args;
1073{
1074 PyObject *s;
1075 if (!PyArg_ParseTuple(args, "S", &s))
1076 return NULL;
1077 Py_INCREF(s);
1078 PyString_InternInPlace(&s);
1079 return s;
1080}
1081
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001082static char intern_doc[] =
1083"intern(string) -> string\n\
1084\n\
1085``Intern'' the given string. This enters the string in the (global)\n\
1086table of interned strings whose purpose is to speed up dictionary lookups.\n\
1087Return the string itself or the previously interned string object with the\n\
1088same value.";
1089
1090
Guido van Rossum79f25d91997-04-29 20:08:16 +00001091static PyObject *
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001092builtin_int(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001093 PyObject *self;
1094 PyObject *args;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001095{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001096 PyObject *v;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001097
Guido van Rossum79f25d91997-04-29 20:08:16 +00001098 if (!PyArg_ParseTuple(args, "O:int", &v))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001099 return NULL;
Guido van Rossum09df08a1998-05-22 00:51:39 +00001100 return PyNumber_Int(v);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001101}
1102
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001103static char int_doc[] =
1104"int(x) -> integer\n\
1105\n\
1106Convert a string or number to an integer, if possible.\n\
1107A floating point argument will be truncated towards zero.";
1108
1109
Guido van Rossum79f25d91997-04-29 20:08:16 +00001110static PyObject *
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001111builtin_len(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001112 PyObject *self;
1113 PyObject *args;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001114{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001115 PyObject *v;
Guido van Rossum8ea9f4d1998-06-29 22:26:50 +00001116 long res;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001117
Guido van Rossum79f25d91997-04-29 20:08:16 +00001118 if (!PyArg_ParseTuple(args, "O:len", &v))
Guido van Rossum3f5da241990-12-20 15:06:42 +00001119 return NULL;
Guido van Rossum8ea9f4d1998-06-29 22:26:50 +00001120 res = PyObject_Length(v);
1121 if (res < 0 && PyErr_Occurred())
1122 return NULL;
1123 return PyInt_FromLong(res);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001124}
1125
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001126static char len_doc[] =
1127"len(object) -> integer\n\
1128\n\
1129Return the number of items of a sequence or mapping.";
1130
1131
Guido van Rossum79f25d91997-04-29 20:08:16 +00001132static PyObject *
Guido van Rossumd1705771996-04-09 02:41:06 +00001133builtin_list(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001134 PyObject *self;
1135 PyObject *args;
Guido van Rossumd1705771996-04-09 02:41:06 +00001136{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001137 PyObject *v;
Guido van Rossumd1705771996-04-09 02:41:06 +00001138
Guido van Rossum79f25d91997-04-29 20:08:16 +00001139 if (!PyArg_ParseTuple(args, "O:list", &v))
Guido van Rossumd1705771996-04-09 02:41:06 +00001140 return NULL;
Guido van Rossum09df08a1998-05-22 00:51:39 +00001141 return PySequence_List(v);
Guido van Rossumd1705771996-04-09 02:41:06 +00001142}
1143
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001144static char list_doc[] =
1145"list(sequence) -> list\n\
1146\n\
1147Return a new list whose items are the same as those of the argument sequence.";
1148
Guido van Rossum8861b741996-07-30 16:49:37 +00001149
1150static PyObject *
1151builtin_slice(self, args)
1152 PyObject *self;
1153 PyObject *args;
1154{
Guido van Rossum09df08a1998-05-22 00:51:39 +00001155 PyObject *start, *stop, *step;
Guido van Rossum8861b741996-07-30 16:49:37 +00001156
Guido van Rossum09df08a1998-05-22 00:51:39 +00001157 start = stop = step = NULL;
Guido van Rossum8861b741996-07-30 16:49:37 +00001158
Guido van Rossum09df08a1998-05-22 00:51:39 +00001159 if (!PyArg_ParseTuple(args, "O|OO:slice", &start, &stop, &step))
1160 return NULL;
Guido van Rossum8861b741996-07-30 16:49:37 +00001161
Guido van Rossum09df08a1998-05-22 00:51:39 +00001162 /* This swapping of stop and start is to maintain similarity with
1163 range(). */
1164 if (stop == NULL) {
1165 stop = start;
1166 start = NULL;
1167 }
1168 return PySlice_New(start, stop, step);
Guido van Rossum8861b741996-07-30 16:49:37 +00001169}
1170
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001171static char slice_doc[] =
1172"slice([start,] step[, stop]) -> slice object\n\
1173\n\
1174Create a slice object. This is used for slicing by the Numeric extensions.";
1175
1176
Guido van Rossum79f25d91997-04-29 20:08:16 +00001177static PyObject *
Guido van Rossum872537c1995-07-07 22:43:42 +00001178builtin_locals(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001179 PyObject *self;
1180 PyObject *args;
Guido van Rossum872537c1995-07-07 22:43:42 +00001181{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001182 PyObject *d;
Guido van Rossum872537c1995-07-07 22:43:42 +00001183
Guido van Rossum79f25d91997-04-29 20:08:16 +00001184 if (!PyArg_ParseTuple(args, ""))
Guido van Rossum872537c1995-07-07 22:43:42 +00001185 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001186 d = PyEval_GetLocals();
1187 Py_INCREF(d);
Guido van Rossum872537c1995-07-07 22:43:42 +00001188 return d;
1189}
1190
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001191static char locals_doc[] =
1192"locals() -> dictionary\n\
1193\n\
1194Return the dictionary containing the current scope's local variables.";
1195
1196
Guido van Rossum79f25d91997-04-29 20:08:16 +00001197static PyObject *
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001198builtin_long(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001199 PyObject *self;
1200 PyObject *args;
Guido van Rossumd4905451991-05-05 20:00:36 +00001201{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001202 PyObject *v;
Guido van Rossum1899c2e1992-09-12 11:09:23 +00001203
Guido van Rossum79f25d91997-04-29 20:08:16 +00001204 if (!PyArg_ParseTuple(args, "O:long", &v))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001205 return NULL;
Guido van Rossum09df08a1998-05-22 00:51:39 +00001206 return PyNumber_Long(v);
Guido van Rossumd4905451991-05-05 20:00:36 +00001207}
1208
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001209static char long_doc[] =
1210"long(x) -> long integer\n\
1211\n\
1212Convert a string or number to a long integer, if possible.\n\
1213A floating point argument will be truncated towards zero.";
1214
1215
Guido van Rossum79f25d91997-04-29 20:08:16 +00001216static PyObject *
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001217min_max(args, sign)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001218 PyObject *args;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001219 int sign;
1220{
Guido van Rossum2d951851994-08-29 12:52:16 +00001221 int i;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001222 PyObject *v, *w, *x;
1223 PySequenceMethods *sq;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001224
Guido van Rossum79f25d91997-04-29 20:08:16 +00001225 if (PyTuple_Size(args) > 1)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001226 v = args;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001227 else if (!PyArg_ParseTuple(args, "O:min/max", &v))
Guido van Rossum3f5da241990-12-20 15:06:42 +00001228 return NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001229 sq = v->ob_type->tp_as_sequence;
Guido van Rossum09df08a1998-05-22 00:51:39 +00001230 if (sq == NULL || sq->sq_item == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001231 PyErr_SetString(PyExc_TypeError,
1232 "min() or max() of non-sequence");
Guido van Rossum3f5da241990-12-20 15:06:42 +00001233 return NULL;
1234 }
Guido van Rossum2d951851994-08-29 12:52:16 +00001235 w = NULL;
1236 for (i = 0; ; i++) {
Guido van Rossum3f5da241990-12-20 15:06:42 +00001237 x = (*sq->sq_item)(v, i); /* Implies INCREF */
Guido van Rossum2d951851994-08-29 12:52:16 +00001238 if (x == NULL) {
Barry Warsawcde8b1b1997-08-22 21:14:38 +00001239 if (PyErr_ExceptionMatches(PyExc_IndexError)) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001240 PyErr_Clear();
Guido van Rossum2d951851994-08-29 12:52:16 +00001241 break;
1242 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00001243 Py_XDECREF(w);
Guido van Rossum2d951851994-08-29 12:52:16 +00001244 return NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001245 }
Guido van Rossum2d951851994-08-29 12:52:16 +00001246 if (w == NULL)
1247 w = x;
1248 else {
Guido van Rossumc8b6df91997-05-23 00:06:51 +00001249 int c = PyObject_Compare(x, w);
1250 if (c && PyErr_Occurred()) {
1251 Py_DECREF(x);
1252 Py_XDECREF(w);
1253 return NULL;
1254 }
1255 if (c * sign > 0) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001256 Py_DECREF(w);
Guido van Rossum2d951851994-08-29 12:52:16 +00001257 w = x;
1258 }
1259 else
Guido van Rossum79f25d91997-04-29 20:08:16 +00001260 Py_DECREF(x);
Guido van Rossum2d951851994-08-29 12:52:16 +00001261 }
Guido van Rossum3f5da241990-12-20 15:06:42 +00001262 }
Guido van Rossum2d951851994-08-29 12:52:16 +00001263 if (w == NULL)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001264 PyErr_SetString(PyExc_ValueError,
1265 "min() or max() of empty sequence");
Guido van Rossum3f5da241990-12-20 15:06:42 +00001266 return w;
1267}
1268
Guido van Rossum79f25d91997-04-29 20:08:16 +00001269static PyObject *
Guido van Rossum3f5da241990-12-20 15:06:42 +00001270builtin_min(self, v)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001271 PyObject *self;
1272 PyObject *v;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001273{
1274 return min_max(v, -1);
1275}
1276
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001277static char min_doc[] =
1278"min(sequence) -> value\n\
1279min(a, b, c, ...) -> value\n\
1280\n\
1281With a single sequence argument, return its smallest item.\n\
1282With two or more arguments, return the smallest argument.";
1283
1284
Guido van Rossum79f25d91997-04-29 20:08:16 +00001285static PyObject *
Guido van Rossum3f5da241990-12-20 15:06:42 +00001286builtin_max(self, v)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001287 PyObject *self;
1288 PyObject *v;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001289{
1290 return min_max(v, 1);
1291}
1292
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001293static char max_doc[] =
1294"max(sequence) -> value\n\
1295max(a, b, c, ...) -> value\n\
1296\n\
1297With a single sequence argument, return its largest item.\n\
1298With two or more arguments, return the largest argument.";
1299
1300
Guido van Rossum79f25d91997-04-29 20:08:16 +00001301static PyObject *
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001302builtin_oct(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001303 PyObject *self;
1304 PyObject *args;
Guido van Rossum006bcd41991-10-24 14:54:44 +00001305{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001306 PyObject *v;
1307 PyNumberMethods *nb;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001308
Guido van Rossum79f25d91997-04-29 20:08:16 +00001309 if (!PyArg_ParseTuple(args, "O:oct", &v))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001310 return NULL;
Guido van Rossum1899c2e1992-09-12 11:09:23 +00001311 if (v == NULL || (nb = v->ob_type->tp_as_number) == NULL ||
1312 nb->nb_oct == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001313 PyErr_SetString(PyExc_TypeError,
Guido van Rossum1899c2e1992-09-12 11:09:23 +00001314 "oct() argument can't be converted to oct");
1315 return NULL;
Guido van Rossum006bcd41991-10-24 14:54:44 +00001316 }
Guido van Rossum1899c2e1992-09-12 11:09:23 +00001317 return (*nb->nb_oct)(v);
Guido van Rossum006bcd41991-10-24 14:54:44 +00001318}
1319
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001320static char oct_doc[] =
1321"oct(number) -> string\n\
1322\n\
1323Return the octal representation of an integer or long integer.";
1324
1325
Guido van Rossum79f25d91997-04-29 20:08:16 +00001326static PyObject *
Guido van Rossum94390a41992-08-14 15:14:30 +00001327builtin_open(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001328 PyObject *self;
1329 PyObject *args;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001330{
Guido van Rossum2d951851994-08-29 12:52:16 +00001331 char *name;
1332 char *mode = "r";
1333 int bufsize = -1;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001334 PyObject *f;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001335
Guido van Rossum79f25d91997-04-29 20:08:16 +00001336 if (!PyArg_ParseTuple(args, "s|si:open", &name, &mode, &bufsize))
Guido van Rossum3f5da241990-12-20 15:06:42 +00001337 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001338 f = PyFile_FromString(name, mode);
Guido van Rossum2d951851994-08-29 12:52:16 +00001339 if (f != NULL)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001340 PyFile_SetBufSize(f, bufsize);
Guido van Rossum2d951851994-08-29 12:52:16 +00001341 return f;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001342}
1343
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001344static char open_doc[] =
1345"open(filename[, mode[, buffering]]) -> file object\n\
1346\n\
1347Open a file. The mode can be 'r', 'w' or 'a' for reading (default),\n\
1348writing or appending. The file will be created if it doesn't exist\n\
1349when opened for writing or appending; it will be truncated when\n\
1350opened for writing. Add a 'b' to the mode for binary files.\n\
1351Add a '+' to the mode to allow simultaneous reading and writing.\n\
1352If the buffering argument is given, 0 means unbuffered, 1 means line\n\
1353buffered, and larger numbers specify the buffer size.";
1354
1355
Guido van Rossum79f25d91997-04-29 20:08:16 +00001356static PyObject *
Guido van Rossum94390a41992-08-14 15:14:30 +00001357builtin_ord(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001358 PyObject *self;
1359 PyObject *args;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001360{
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001361 char c;
1362
Guido van Rossum79f25d91997-04-29 20:08:16 +00001363 if (!PyArg_ParseTuple(args, "c:ord", &c))
Guido van Rossum3f5da241990-12-20 15:06:42 +00001364 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001365 return PyInt_FromLong((long)(c & 0xff));
Guido van Rossum3f5da241990-12-20 15:06:42 +00001366}
1367
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001368static char ord_doc[] =
1369"ord(c) -> integer\n\
1370\n\
1371Return the integer ordinal of a one character string.";
1372
1373
Guido van Rossum79f25d91997-04-29 20:08:16 +00001374static PyObject *
Guido van Rossum6a00cd81995-01-07 12:39:01 +00001375builtin_pow(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001376 PyObject *self;
1377 PyObject *args;
Guido van Rossum6a00cd81995-01-07 12:39:01 +00001378{
Guido van Rossum09df08a1998-05-22 00:51:39 +00001379 PyObject *v, *w, *z = Py_None;
Guido van Rossum6a00cd81995-01-07 12:39:01 +00001380
Guido van Rossum79f25d91997-04-29 20:08:16 +00001381 if (!PyArg_ParseTuple(args, "OO|O:pow", &v, &w, &z))
Guido van Rossum6a00cd81995-01-07 12:39:01 +00001382 return NULL;
Guido van Rossum09df08a1998-05-22 00:51:39 +00001383 return PyNumber_Power(v, w, z);
Guido van Rossumd4905451991-05-05 20:00:36 +00001384}
1385
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001386static char pow_doc[] =
1387"pow(x, y[, z]) -> number\n\
1388\n\
1389With two arguments, equivalent to x**y. With three arguments,\n\
1390equivalent to (x**y) % z, but may be more efficient (e.g. for longs).";
1391
1392
Guido van Rossum79f25d91997-04-29 20:08:16 +00001393static PyObject *
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001394builtin_range(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001395 PyObject *self;
1396 PyObject *args;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001397{
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001398 long ilow = 0, ihigh = 0, istep = 1;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001399 int i, n;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001400 PyObject *v;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001401
Guido van Rossum79f25d91997-04-29 20:08:16 +00001402 if (PyTuple_Size(args) <= 1) {
1403 if (!PyArg_ParseTuple(args,
Guido van Rossum0865dd91995-01-17 16:30:22 +00001404 "l;range() requires 1-3 int arguments",
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001405 &ihigh))
1406 return NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001407 }
1408 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001409 if (!PyArg_ParseTuple(args,
Guido van Rossum0865dd91995-01-17 16:30:22 +00001410 "ll|l;range() requires 1-3 int arguments",
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001411 &ilow, &ihigh, &istep))
Guido van Rossum3f5da241990-12-20 15:06:42 +00001412 return NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001413 }
1414 if (istep == 0) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001415 PyErr_SetString(PyExc_ValueError, "zero step for range()");
Guido van Rossum3f5da241990-12-20 15:06:42 +00001416 return NULL;
1417 }
Guido van Rossume23cde21999-01-12 05:07:47 +00001418 /* A bit convoluted because this might overflow; due to Tim Peters */
1419 if (istep > 0) {
1420 if (ihigh <= ilow)
1421 n = 0;
1422 else {
1423 unsigned long hi = (unsigned long)ihigh;
1424 unsigned long lo = (unsigned long)ilow;
1425 unsigned long diff = hi - lo - 1;
1426 n = (long)(diff / istep + 1);
1427 }
1428 }
1429 else {
1430 /* But any errors in this branch are my own --Guido */
1431 if (ihigh >= ilow)
1432 n = 0;
1433 else {
1434 /* Swap lo and hi; use abs(istep) */
1435 unsigned long hi = (unsigned long)ilow;
1436 unsigned long lo = (unsigned long)ihigh;
1437 unsigned long diff = hi - lo - 1;
1438 n = (long)(diff / (-istep) + 1);
1439 }
1440 }
1441 if (n < 0) {
1442 PyErr_SetString(PyExc_OverflowError,
1443 "range() has more than sys.maxint items");
1444 return NULL;
1445 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00001446 v = PyList_New(n);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001447 if (v == NULL)
1448 return NULL;
1449 for (i = 0; i < n; i++) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001450 PyObject *w = PyInt_FromLong(ilow);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001451 if (w == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001452 Py_DECREF(v);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001453 return NULL;
1454 }
Guido van Rossuma937d141998-04-24 18:22:02 +00001455 PyList_SET_ITEM(v, i, w);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001456 ilow += istep;
1457 }
1458 return v;
1459}
1460
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001461static char range_doc[] =
1462"range([start,] stop[, step]) -> list of integers\n\
1463\n\
1464Return a list containing an arithmetic progression of integers.\n\
1465range(i, j) returns [i, i+1, i+2, ..., j-1]; start (!) defaults to 0.\n\
1466When step is given, it specifies the increment (or decrement).\n\
1467For example, range(4) returns [0, 1, 2, 3]. The end point is omitted!\n\
1468These are exactly the valid indices for a list of 4 elements.";
1469
1470
Guido van Rossum79f25d91997-04-29 20:08:16 +00001471static PyObject *
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001472builtin_xrange(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001473 PyObject *self;
1474 PyObject *args;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001475{
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001476 long ilow = 0, ihigh = 0, istep = 1;
Guido van Rossum0865dd91995-01-17 16:30:22 +00001477 long n;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001478
Guido van Rossum79f25d91997-04-29 20:08:16 +00001479 if (PyTuple_Size(args) <= 1) {
1480 if (!PyArg_ParseTuple(args,
Guido van Rossum0865dd91995-01-17 16:30:22 +00001481 "l;xrange() requires 1-3 int arguments",
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001482 &ihigh))
1483 return NULL;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001484 }
1485 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001486 if (!PyArg_ParseTuple(args,
Guido van Rossum0865dd91995-01-17 16:30:22 +00001487 "ll|l;xrange() requires 1-3 int arguments",
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001488 &ilow, &ihigh, &istep))
Guido van Rossum12d12c51993-10-26 17:58:25 +00001489 return NULL;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001490 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001491 if (istep == 0) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001492 PyErr_SetString(PyExc_ValueError, "zero step for xrange()");
Guido van Rossum12d12c51993-10-26 17:58:25 +00001493 return NULL;
1494 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001495 /* XXX ought to check overflow of subtraction */
1496 if (istep > 0)
1497 n = (ihigh - ilow + istep - 1) / istep;
1498 else
1499 n = (ihigh - ilow + istep + 1) / istep;
1500 if (n < 0)
1501 n = 0;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001502 return PyRange_New(ilow, n, istep, 1);
Guido van Rossum12d12c51993-10-26 17:58:25 +00001503}
1504
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001505static char xrange_doc[] =
1506"xrange([start,] stop[, step]) -> xrange object\n\
1507\n\
1508Like range(), but instead of returning a list, returns an object that\n\
1509generates the numbers in the range on demand. This is slightly slower\n\
1510than range() but more memory efficient.";
1511
1512
Guido van Rossum79f25d91997-04-29 20:08:16 +00001513static PyObject *
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001514builtin_raw_input(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001515 PyObject *self;
1516 PyObject *args;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001517{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001518 PyObject *v = NULL;
1519 PyObject *f;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001520
Guido van Rossum79f25d91997-04-29 20:08:16 +00001521 if (!PyArg_ParseTuple(args, "|O:[raw_]input", &v))
Guido van Rossum3165fe61992-09-25 21:59:05 +00001522 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001523 if (PyFile_AsFile(PySys_GetObject("stdin")) == stdin &&
1524 PyFile_AsFile(PySys_GetObject("stdout")) == stdout &&
Guido van Rossum53bb7ff1995-07-26 16:26:31 +00001525 isatty(fileno(stdin)) && isatty(fileno(stdout))) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001526 PyObject *po;
Guido van Rossum872537c1995-07-07 22:43:42 +00001527 char *prompt;
1528 char *s;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001529 PyObject *result;
Guido van Rossum872537c1995-07-07 22:43:42 +00001530 if (v != NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001531 po = PyObject_Str(v);
Guido van Rossum872537c1995-07-07 22:43:42 +00001532 if (po == NULL)
1533 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001534 prompt = PyString_AsString(po);
Guido van Rossumd9b52081998-06-26 18:25:38 +00001535 if (prompt == NULL)
1536 return NULL;
Guido van Rossum872537c1995-07-07 22:43:42 +00001537 }
1538 else {
1539 po = NULL;
1540 prompt = "";
1541 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00001542 s = PyOS_Readline(prompt);
1543 Py_XDECREF(po);
Guido van Rossum872537c1995-07-07 22:43:42 +00001544 if (s == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001545 PyErr_SetNone(PyExc_KeyboardInterrupt);
Guido van Rossum872537c1995-07-07 22:43:42 +00001546 return NULL;
1547 }
1548 if (*s == '\0') {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001549 PyErr_SetNone(PyExc_EOFError);
Guido van Rossum872537c1995-07-07 22:43:42 +00001550 result = NULL;
1551 }
1552 else { /* strip trailing '\n' */
Guido van Rossum79f25d91997-04-29 20:08:16 +00001553 result = PyString_FromStringAndSize(s, strlen(s)-1);
Guido van Rossum872537c1995-07-07 22:43:42 +00001554 }
1555 free(s);
1556 return result;
1557 }
Guido van Rossum90933611991-06-07 16:10:43 +00001558 if (v != NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001559 f = PySys_GetObject("stdout");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001560 if (f == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001561 PyErr_SetString(PyExc_RuntimeError, "lost sys.stdout");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001562 return NULL;
1563 }
Guido van Rossumc8b6df91997-05-23 00:06:51 +00001564 if (Py_FlushLine() != 0 ||
1565 PyFile_WriteObject(v, f, Py_PRINT_RAW) != 0)
Guido van Rossum90933611991-06-07 16:10:43 +00001566 return NULL;
1567 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00001568 f = PySys_GetObject("stdin");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001569 if (f == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001570 PyErr_SetString(PyExc_RuntimeError, "lost sys.stdin");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001571 return NULL;
1572 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00001573 return PyFile_GetLine(f, -1);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001574}
1575
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001576static char raw_input_doc[] =
1577"raw_input([prompt]) -> string\n\
1578\n\
1579Read a string from standard input. The trailing newline is stripped.\n\
1580If the user hits EOF (Unix: Ctl-D, Windows: Ctl-Z+Return), raise EOFError.\n\
1581On Unix, GNU readline is used if enabled. The prompt string, if given,\n\
1582is printed without a trailing newline before reading.";
1583
1584
Guido van Rossum79f25d91997-04-29 20:08:16 +00001585static PyObject *
Guido van Rossum12d12c51993-10-26 17:58:25 +00001586builtin_reduce(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001587 PyObject *self;
1588 PyObject *args;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001589{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001590 PyObject *seq, *func, *result = NULL;
1591 PySequenceMethods *sqf;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001592 register int i;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001593
Guido van Rossum79f25d91997-04-29 20:08:16 +00001594 if (!PyArg_ParseTuple(args, "OO|O:reduce", &func, &seq, &result))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001595 return NULL;
1596 if (result != NULL)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001597 Py_INCREF(result);
Guido van Rossum12d12c51993-10-26 17:58:25 +00001598
Guido van Rossum09df08a1998-05-22 00:51:39 +00001599 sqf = seq->ob_type->tp_as_sequence;
1600 if (sqf == NULL || sqf->sq_item == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001601 PyErr_SetString(PyExc_TypeError,
Guido van Rossum12d12c51993-10-26 17:58:25 +00001602 "2nd argument to reduce() must be a sequence object");
1603 return NULL;
1604 }
1605
Guido van Rossum79f25d91997-04-29 20:08:16 +00001606 if ((args = PyTuple_New(2)) == NULL)
Guido van Rossum2d951851994-08-29 12:52:16 +00001607 goto Fail;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001608
Guido van Rossum2d951851994-08-29 12:52:16 +00001609 for (i = 0; ; ++i) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001610 PyObject *op2;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001611
1612 if (args->ob_refcnt > 1) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001613 Py_DECREF(args);
1614 if ((args = PyTuple_New(2)) == NULL)
Guido van Rossum2d951851994-08-29 12:52:16 +00001615 goto Fail;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001616 }
1617
Guido van Rossum2d951851994-08-29 12:52:16 +00001618 if ((op2 = (*sqf->sq_item)(seq, i)) == NULL) {
Barry Warsawcde8b1b1997-08-22 21:14:38 +00001619 if (PyErr_ExceptionMatches(PyExc_IndexError)) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001620 PyErr_Clear();
Guido van Rossum2d951851994-08-29 12:52:16 +00001621 break;
1622 }
1623 goto Fail;
1624 }
Guido van Rossum12d12c51993-10-26 17:58:25 +00001625
Guido van Rossum2d951851994-08-29 12:52:16 +00001626 if (result == NULL)
1627 result = op2;
1628 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001629 PyTuple_SetItem(args, 0, result);
1630 PyTuple_SetItem(args, 1, op2);
1631 if ((result = PyEval_CallObject(func, args)) == NULL)
Guido van Rossum2d951851994-08-29 12:52:16 +00001632 goto Fail;
1633 }
Guido van Rossum12d12c51993-10-26 17:58:25 +00001634 }
1635
Guido van Rossum79f25d91997-04-29 20:08:16 +00001636 Py_DECREF(args);
Guido van Rossum12d12c51993-10-26 17:58:25 +00001637
Guido van Rossum2d951851994-08-29 12:52:16 +00001638 if (result == NULL)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001639 PyErr_SetString(PyExc_TypeError,
Guido van Rossum2d951851994-08-29 12:52:16 +00001640 "reduce of empty sequence with no initial value");
1641
Guido van Rossum12d12c51993-10-26 17:58:25 +00001642 return result;
1643
Guido van Rossum2d951851994-08-29 12:52:16 +00001644Fail:
Guido van Rossum79f25d91997-04-29 20:08:16 +00001645 Py_XDECREF(args);
1646 Py_XDECREF(result);
Guido van Rossum12d12c51993-10-26 17:58:25 +00001647 return NULL;
1648}
1649
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001650static char reduce_doc[] =
1651"reduce(function, sequence[, initial]) -> value\n\
1652\n\
1653Apply a function of two arguments cumulatively to the items of a sequence,\n\
1654from left to right, so as to reduce the sequence to a single value.\n\
1655For example, reduce(lambda x, y: x+y, [1, 2, 3, 4, 5]) calculates\n\
1656((((1+2)+3)+4)+5). If initial is present, it is placed before the items\n\
1657of the sequence in the calculation, and serves as a default when the\n\
1658sequence is empty.";
1659
1660
Guido van Rossum79f25d91997-04-29 20:08:16 +00001661static PyObject *
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001662builtin_reload(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001663 PyObject *self;
1664 PyObject *args;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001665{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001666 PyObject *v;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001667
Guido van Rossum79f25d91997-04-29 20:08:16 +00001668 if (!PyArg_ParseTuple(args, "O:reload", &v))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001669 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001670 return PyImport_ReloadModule(v);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001671}
1672
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001673static char reload_doc[] =
1674"reload(module) -> module\n\
1675\n\
1676Reload the module. The module must have been successfully imported before.";
1677
1678
Guido van Rossum79f25d91997-04-29 20:08:16 +00001679static PyObject *
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001680builtin_repr(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001681 PyObject *self;
1682 PyObject *args;
Guido van Rossumc89705d1992-11-26 08:54:07 +00001683{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001684 PyObject *v;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001685
Guido van Rossum79f25d91997-04-29 20:08:16 +00001686 if (!PyArg_ParseTuple(args, "O:repr", &v))
Guido van Rossumc89705d1992-11-26 08:54:07 +00001687 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001688 return PyObject_Repr(v);
Guido van Rossumc89705d1992-11-26 08:54:07 +00001689}
1690
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001691static char repr_doc[] =
1692"repr(object) -> string\n\
1693\n\
1694Return the canonical string representation of the object.\n\
1695For most object types, eval(repr(object)) == object.";
1696
1697
Guido van Rossum79f25d91997-04-29 20:08:16 +00001698static PyObject *
Guido van Rossum9e51f9b1993-02-12 16:29:05 +00001699builtin_round(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001700 PyObject *self;
1701 PyObject *args;
Guido van Rossum9e51f9b1993-02-12 16:29:05 +00001702{
Guido van Rossum9e51f9b1993-02-12 16:29:05 +00001703 double x;
1704 double f;
1705 int ndigits = 0;
Guido van Rossum9e51f9b1993-02-12 16:29:05 +00001706 int i;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001707
Guido van Rossum79f25d91997-04-29 20:08:16 +00001708 if (!PyArg_ParseTuple(args, "d|i:round", &x, &ndigits))
Guido van Rossum9e51f9b1993-02-12 16:29:05 +00001709 return NULL;
Guido van Rossum9e51f9b1993-02-12 16:29:05 +00001710 f = 1.0;
Guido van Rossum1e162d31998-05-09 14:42:25 +00001711 i = abs(ndigits);
1712 while (--i >= 0)
Guido van Rossum9e51f9b1993-02-12 16:29:05 +00001713 f = f*10.0;
Guido van Rossum1e162d31998-05-09 14:42:25 +00001714 if (ndigits < 0)
1715 x /= f;
Guido van Rossum9e51f9b1993-02-12 16:29:05 +00001716 else
Guido van Rossum1e162d31998-05-09 14:42:25 +00001717 x *= f;
1718 if (x >= 0.0)
1719 x = floor(x + 0.5);
1720 else
1721 x = ceil(x - 0.5);
1722 if (ndigits < 0)
1723 x *= f;
1724 else
1725 x /= f;
1726 return PyFloat_FromDouble(x);
Guido van Rossum9e51f9b1993-02-12 16:29:05 +00001727}
1728
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001729static char round_doc[] =
1730"round(number[, ndigits]) -> floating point number\n\
1731\n\
1732Round a number to a given precision in decimal digits (default 0 digits).\n\
1733This always returns a floating point number. Precision may be negative.";
1734
1735
Guido van Rossum79f25d91997-04-29 20:08:16 +00001736static PyObject *
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001737builtin_str(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001738 PyObject *self;
1739 PyObject *args;
Guido van Rossumc89705d1992-11-26 08:54:07 +00001740{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001741 PyObject *v;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001742
Guido van Rossum79f25d91997-04-29 20:08:16 +00001743 if (!PyArg_ParseTuple(args, "O:str", &v))
Guido van Rossumc89705d1992-11-26 08:54:07 +00001744 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001745 return PyObject_Str(v);
Guido van Rossumc89705d1992-11-26 08:54:07 +00001746}
1747
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001748static char str_doc[] =
1749"str(object) -> string\n\
1750\n\
1751Return a nice string representation of the object.\n\
1752If the argument is a string, the return value is the same object.";
1753
1754
Guido van Rossum79f25d91997-04-29 20:08:16 +00001755static PyObject *
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001756builtin_tuple(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001757 PyObject *self;
1758 PyObject *args;
Guido van Rossumcae027b1994-08-29 12:53:11 +00001759{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001760 PyObject *v;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001761
Guido van Rossum79f25d91997-04-29 20:08:16 +00001762 if (!PyArg_ParseTuple(args, "O:tuple", &v))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001763 return NULL;
Guido van Rossum09df08a1998-05-22 00:51:39 +00001764 return PySequence_Tuple(v);
Guido van Rossumcae027b1994-08-29 12:53:11 +00001765}
1766
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001767static char tuple_doc[] =
1768"tuple(sequence) -> list\n\
1769\n\
1770Return a tuple whose items are the same as those of the argument sequence.\n\
1771If the argument is a tuple, the return value is the same object.";
1772
1773
Guido van Rossum79f25d91997-04-29 20:08:16 +00001774static PyObject *
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001775builtin_type(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001776 PyObject *self;
1777 PyObject *args;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001778{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001779 PyObject *v;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001780
Guido van Rossum79f25d91997-04-29 20:08:16 +00001781 if (!PyArg_ParseTuple(args, "O:type", &v))
Guido van Rossum3f5da241990-12-20 15:06:42 +00001782 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001783 v = (PyObject *)v->ob_type;
1784 Py_INCREF(v);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001785 return v;
1786}
1787
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001788static char type_doc[] =
1789"type(object) -> type object\n\
1790\n\
1791Return the type of the object.";
1792
1793
Guido van Rossum79f25d91997-04-29 20:08:16 +00001794static PyObject *
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001795builtin_vars(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001796 PyObject *self;
1797 PyObject *args;
Guido van Rossum2d951851994-08-29 12:52:16 +00001798{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001799 PyObject *v = NULL;
1800 PyObject *d;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001801
Guido van Rossum79f25d91997-04-29 20:08:16 +00001802 if (!PyArg_ParseTuple(args, "|O:vars", &v))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001803 return NULL;
Guido van Rossum2d951851994-08-29 12:52:16 +00001804 if (v == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001805 d = PyEval_GetLocals();
Guido van Rossum53bb7ff1995-07-26 16:26:31 +00001806 if (d == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001807 if (!PyErr_Occurred())
1808 PyErr_SetString(PyExc_SystemError,
1809 "no locals!?");
Guido van Rossum53bb7ff1995-07-26 16:26:31 +00001810 }
1811 else
Guido van Rossum79f25d91997-04-29 20:08:16 +00001812 Py_INCREF(d);
Guido van Rossum2d951851994-08-29 12:52:16 +00001813 }
1814 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001815 d = PyObject_GetAttrString(v, "__dict__");
Guido van Rossum2d951851994-08-29 12:52:16 +00001816 if (d == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001817 PyErr_SetString(PyExc_TypeError,
Guido van Rossum2d951851994-08-29 12:52:16 +00001818 "vars() argument must have __dict__ attribute");
1819 return NULL;
1820 }
1821 }
1822 return d;
1823}
1824
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001825static char vars_doc[] =
1826"vars([object]) -> dictionary\n\
1827\n\
1828Without arguments, equivalent to locals().\n\
1829With an argument, equivalent to object.__dict__.";
1830
1831
Barry Warsawcde8b1b1997-08-22 21:14:38 +00001832static PyObject *
1833builtin_isinstance(self, args)
1834 PyObject *self;
1835 PyObject *args;
1836{
1837 PyObject *inst;
1838 PyObject *cls;
1839 int retval;
1840
1841 if (!PyArg_ParseTuple(args, "OO", &inst, &cls))
1842 return NULL;
Guido van Rossumf5dd9141997-12-02 19:11:45 +00001843 if (PyType_Check(cls)) {
Guido van Rossumd6af46d1997-12-10 05:51:47 +00001844 retval = ((PyObject *)(inst->ob_type) == cls);
Barry Warsawcde8b1b1997-08-22 21:14:38 +00001845 }
Barry Warsawcde8b1b1997-08-22 21:14:38 +00001846 else {
Guido van Rossumf5dd9141997-12-02 19:11:45 +00001847 if (!PyClass_Check(cls)) {
1848 PyErr_SetString(PyExc_TypeError,
1849 "second argument must be a class");
1850 return NULL;
1851 }
1852
1853 if (!PyInstance_Check(inst))
1854 retval = 0;
1855 else {
1856 PyObject *inclass =
1857 (PyObject*)((PyInstanceObject*)inst)->in_class;
1858 retval = PyClass_IsSubclass(inclass, cls);
1859 }
Barry Warsawcde8b1b1997-08-22 21:14:38 +00001860 }
1861 return PyInt_FromLong(retval);
1862}
1863
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001864static char isinstance_doc[] =
1865"isinstance(object, class-or-type) -> Boolean\n\
1866\n\
1867Return whether an object is an instance of a class or of a subclass thereof.\n\
1868With a type as second argument, return whether that is the object's type.";
1869
Barry Warsawcde8b1b1997-08-22 21:14:38 +00001870
1871static PyObject *
1872builtin_issubclass(self, args)
1873 PyObject *self;
1874 PyObject *args;
1875{
1876 PyObject *derived;
1877 PyObject *cls;
1878 int retval;
1879
1880 if (!PyArg_ParseTuple(args, "OO", &derived, &cls))
1881 return NULL;
1882 if (!PyClass_Check(derived) || !PyClass_Check(cls)) {
1883 PyErr_SetString(PyExc_TypeError, "arguments must be classes");
1884 return NULL;
1885 }
1886 /* shortcut */
1887 if (!(retval = (derived == cls)))
1888 retval = PyClass_IsSubclass(derived, cls);
1889
1890 return PyInt_FromLong(retval);
1891}
1892
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001893static char issubclass_doc[] =
1894"issubclass(C, B) -> Boolean\n\
1895\n\
1896Return whether class C is a subclass (i.e., a derived class) of class B.";
1897
Barry Warsawcde8b1b1997-08-22 21:14:38 +00001898
Guido van Rossum79f25d91997-04-29 20:08:16 +00001899static PyMethodDef builtin_methods[] = {
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001900 {"__import__", builtin___import__, 1, import_doc},
1901 {"abs", builtin_abs, 1, abs_doc},
1902 {"apply", builtin_apply, 1, apply_doc},
1903 {"callable", builtin_callable, 1, callable_doc},
1904 {"chr", builtin_chr, 1, chr_doc},
1905 {"cmp", builtin_cmp, 1, cmp_doc},
1906 {"coerce", builtin_coerce, 1, coerce_doc},
1907 {"compile", builtin_compile, 1, compile_doc},
Guido van Rossum8a5c5d21996-01-12 01:09:56 +00001908#ifndef WITHOUT_COMPLEX
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001909 {"complex", builtin_complex, 1, complex_doc},
Guido van Rossum8a5c5d21996-01-12 01:09:56 +00001910#endif
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001911 {"delattr", builtin_delattr, 1, delattr_doc},
1912 {"dir", builtin_dir, 1, dir_doc},
1913 {"divmod", builtin_divmod, 1, divmod_doc},
1914 {"eval", builtin_eval, 1, eval_doc},
1915 {"execfile", builtin_execfile, 1, execfile_doc},
1916 {"filter", builtin_filter, 1, filter_doc},
1917 {"float", builtin_float, 1, float_doc},
1918 {"getattr", builtin_getattr, 1, getattr_doc},
1919 {"globals", builtin_globals, 1, globals_doc},
1920 {"hasattr", builtin_hasattr, 1, hasattr_doc},
1921 {"hash", builtin_hash, 1, hash_doc},
1922 {"hex", builtin_hex, 1, hex_doc},
1923 {"id", builtin_id, 1, id_doc},
1924 {"input", builtin_input, 1, input_doc},
1925 {"intern", builtin_intern, 1, intern_doc},
1926 {"int", builtin_int, 1, int_doc},
1927 {"isinstance", builtin_isinstance, 1, isinstance_doc},
1928 {"issubclass", builtin_issubclass, 1, issubclass_doc},
1929 {"len", builtin_len, 1, len_doc},
1930 {"list", builtin_list, 1, list_doc},
1931 {"locals", builtin_locals, 1, locals_doc},
1932 {"long", builtin_long, 1, long_doc},
1933 {"map", builtin_map, 1, map_doc},
1934 {"max", builtin_max, 1, max_doc},
1935 {"min", builtin_min, 1, min_doc},
1936 {"oct", builtin_oct, 1, oct_doc},
1937 {"open", builtin_open, 1, open_doc},
1938 {"ord", builtin_ord, 1, ord_doc},
1939 {"pow", builtin_pow, 1, pow_doc},
1940 {"range", builtin_range, 1, range_doc},
1941 {"raw_input", builtin_raw_input, 1, raw_input_doc},
1942 {"reduce", builtin_reduce, 1, reduce_doc},
1943 {"reload", builtin_reload, 1, reload_doc},
1944 {"repr", builtin_repr, 1, repr_doc},
1945 {"round", builtin_round, 1, round_doc},
1946 {"setattr", builtin_setattr, 1, setattr_doc},
1947 {"slice", builtin_slice, 1, slice_doc},
1948 {"str", builtin_str, 1, str_doc},
1949 {"tuple", builtin_tuple, 1, tuple_doc},
1950 {"type", builtin_type, 1, type_doc},
1951 {"vars", builtin_vars, 1, vars_doc},
1952 {"xrange", builtin_xrange, 1, xrange_doc},
Guido van Rossumc02e15c1991-12-16 13:03:00 +00001953 {NULL, NULL},
Guido van Rossum3f5da241990-12-20 15:06:42 +00001954};
1955
Guido van Rossum3f5da241990-12-20 15:06:42 +00001956/* Predefined exceptions */
1957
Guido van Rossum04748321997-09-16 18:43:15 +00001958PyObject *PyExc_Exception;
Barry Warsaw757af0e1997-08-29 22:13:51 +00001959PyObject *PyExc_StandardError;
Barry Warsaw412cdc21997-09-16 21:51:14 +00001960PyObject *PyExc_ArithmeticError;
Barry Warsaw757af0e1997-08-29 22:13:51 +00001961PyObject *PyExc_LookupError;
1962
Guido van Rossum79f25d91997-04-29 20:08:16 +00001963PyObject *PyExc_AssertionError;
1964PyObject *PyExc_AttributeError;
1965PyObject *PyExc_EOFError;
Guido van Rossumb6a7f771997-05-09 03:03:23 +00001966PyObject *PyExc_FloatingPointError;
Barry Warsawd086a1a1998-07-23 15:59:57 +00001967PyObject *PyExc_EnvironmentError;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001968PyObject *PyExc_IOError;
Barry Warsawd086a1a1998-07-23 15:59:57 +00001969PyObject *PyExc_OSError;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001970PyObject *PyExc_ImportError;
1971PyObject *PyExc_IndexError;
1972PyObject *PyExc_KeyError;
1973PyObject *PyExc_KeyboardInterrupt;
1974PyObject *PyExc_MemoryError;
1975PyObject *PyExc_NameError;
1976PyObject *PyExc_OverflowError;
1977PyObject *PyExc_RuntimeError;
Barry Warsaw344864f1998-12-01 18:52:06 +00001978PyObject *PyExc_NotImplementedError;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001979PyObject *PyExc_SyntaxError;
1980PyObject *PyExc_SystemError;
1981PyObject *PyExc_SystemExit;
1982PyObject *PyExc_TypeError;
1983PyObject *PyExc_ValueError;
1984PyObject *PyExc_ZeroDivisionError;
Guido van Rossum50afb7a1991-12-10 13:52:31 +00001985
Barry Warsaw757af0e1997-08-29 22:13:51 +00001986PyObject *PyExc_MemoryErrorInst;
1987
1988static struct
1989{
1990 char* name;
1991 PyObject** exc;
1992 int leaf_exc;
1993}
1994bltin_exc[] = {
Guido van Rossum04748321997-09-16 18:43:15 +00001995 {"Exception", &PyExc_Exception, 0},
Barry Warsaw757af0e1997-08-29 22:13:51 +00001996 {"StandardError", &PyExc_StandardError, 0},
Barry Warsaw412cdc21997-09-16 21:51:14 +00001997 {"ArithmeticError", &PyExc_ArithmeticError, 0},
Barry Warsaw757af0e1997-08-29 22:13:51 +00001998 {"LookupError", &PyExc_LookupError, 0},
1999 {"AssertionError", &PyExc_AssertionError, 1},
2000 {"AttributeError", &PyExc_AttributeError, 1},
2001 {"EOFError", &PyExc_EOFError, 1},
2002 {"FloatingPointError", &PyExc_FloatingPointError, 1},
Barry Warsawd086a1a1998-07-23 15:59:57 +00002003 {"EnvironmentError", &PyExc_EnvironmentError, 1},
Barry Warsaw757af0e1997-08-29 22:13:51 +00002004 {"IOError", &PyExc_IOError, 1},
Barry Warsawd086a1a1998-07-23 15:59:57 +00002005 {"OSError", &PyExc_OSError, 1},
Barry Warsaw757af0e1997-08-29 22:13:51 +00002006 {"ImportError", &PyExc_ImportError, 1},
2007 {"IndexError", &PyExc_IndexError, 1},
2008 {"KeyError", &PyExc_KeyError, 1},
2009 {"KeyboardInterrupt", &PyExc_KeyboardInterrupt, 1},
2010 {"MemoryError", &PyExc_MemoryError, 1},
2011 {"NameError", &PyExc_NameError, 1},
2012 {"OverflowError", &PyExc_OverflowError, 1},
2013 {"RuntimeError", &PyExc_RuntimeError, 1},
Barry Warsaw344864f1998-12-01 18:52:06 +00002014 {"NotImplementedError",&PyExc_NotImplementedError,1},
Barry Warsaw757af0e1997-08-29 22:13:51 +00002015 {"SyntaxError", &PyExc_SyntaxError, 1},
2016 {"SystemError", &PyExc_SystemError, 1},
2017 {"SystemExit", &PyExc_SystemExit, 1},
2018 {"TypeError", &PyExc_TypeError, 1},
2019 {"ValueError", &PyExc_ValueError, 1},
2020 {"ZeroDivisionError", &PyExc_ZeroDivisionError, 1},
2021 {NULL, NULL}
2022};
2023
2024
Barry Warsaw98b62461998-09-14 18:51:11 +00002025/* import exceptions module to extract class exceptions. on success,
2026 * return 1. on failure return 0 which signals _PyBuiltin_Init_2 to fall
2027 * back to using old-style string based exceptions.
2028 */
2029static int
Barry Warsaw757af0e1997-08-29 22:13:51 +00002030init_class_exc(dict)
2031 PyObject *dict;
2032{
2033 int i;
2034 PyObject *m = PyImport_ImportModule("exceptions");
Barry Warsaw98b62461998-09-14 18:51:11 +00002035 PyObject *args = NULL;
2036 PyObject *d = NULL;
Barry Warsaw757af0e1997-08-29 22:13:51 +00002037
Barry Warsaw98b62461998-09-14 18:51:11 +00002038 /* make sure we got the module and its dictionary */
Barry Warsaw757af0e1997-08-29 22:13:51 +00002039 if (m == NULL ||
2040 (d = PyModule_GetDict(m)) == NULL)
2041 {
Barry Warsaw98b62461998-09-14 18:51:11 +00002042 PySys_WriteStderr("'import exceptions' failed; ");
Barry Warsaw757af0e1997-08-29 22:13:51 +00002043 if (Py_VerboseFlag) {
Barry Warsaw98b62461998-09-14 18:51:11 +00002044 PySys_WriteStderr("traceback:\n");
Barry Warsaw757af0e1997-08-29 22:13:51 +00002045 PyErr_Print();
2046 }
2047 else {
Barry Warsaw98b62461998-09-14 18:51:11 +00002048 PySys_WriteStderr("use -v for traceback\n");
Barry Warsaw757af0e1997-08-29 22:13:51 +00002049 }
Barry Warsaw98b62461998-09-14 18:51:11 +00002050 goto finally;
Barry Warsaw757af0e1997-08-29 22:13:51 +00002051 }
2052 for (i = 0; bltin_exc[i].name; i++) {
2053 /* dig the exception out of the module */
2054 PyObject *exc = PyDict_GetItemString(d, bltin_exc[i].name);
Barry Warsaw98b62461998-09-14 18:51:11 +00002055 if (!exc) {
2056 PySys_WriteStderr(
2057 "Built-in exception class not found: %s. Library mismatch?\n",
2058 bltin_exc[i].name);
2059 goto finally;
2060 }
2061 /* free the old-style exception string object */
Barry Warsaw757af0e1997-08-29 22:13:51 +00002062 Py_XDECREF(*bltin_exc[i].exc);
2063
2064 /* squirrel away a pointer to the exception */
2065 Py_INCREF(exc);
2066 *bltin_exc[i].exc = exc;
2067
2068 /* and insert the name in the __builtin__ module */
Barry Warsaw98b62461998-09-14 18:51:11 +00002069 if (PyDict_SetItemString(dict, bltin_exc[i].name, exc)) {
2070 PySys_WriteStderr(
2071 "Cannot insert exception into __builtin__: %s\n",
2072 bltin_exc[i].name);
2073 goto finally;
2074 }
Barry Warsaw757af0e1997-08-29 22:13:51 +00002075 }
2076
2077 /* we need one pre-allocated instance */
2078 args = Py_BuildValue("()");
Barry Warsaw98b62461998-09-14 18:51:11 +00002079 if (!args ||
2080 !(PyExc_MemoryErrorInst =
2081 PyEval_CallObject(PyExc_MemoryError, args)))
2082 {
2083 PySys_WriteStderr("Cannot pre-allocate MemoryError instance\n");
2084 goto finally;
Barry Warsaw757af0e1997-08-29 22:13:51 +00002085 }
Barry Warsaw98b62461998-09-14 18:51:11 +00002086 Py_DECREF(args);
Barry Warsaw757af0e1997-08-29 22:13:51 +00002087
2088 /* we're done with the exceptions module */
2089 Py_DECREF(m);
2090
Barry Warsaw98b62461998-09-14 18:51:11 +00002091 if (PyErr_Occurred()) {
2092 PySys_WriteStderr("Cannot initialize standard class exceptions; ");
2093 if (Py_VerboseFlag) {
2094 PySys_WriteStderr("traceback:\n");
2095 PyErr_Print();
2096 }
2097 else
2098 PySys_WriteStderr("use -v for traceback\n");
2099 goto finally;
2100 }
2101 return 1;
2102 finally:
2103 Py_XDECREF(m);
2104 Py_XDECREF(args);
2105 PyErr_Clear();
2106 return 0;
Barry Warsaw757af0e1997-08-29 22:13:51 +00002107}
2108
2109
2110static void
2111fini_instances()
2112{
2113 Py_XDECREF(PyExc_MemoryErrorInst);
2114 PyExc_MemoryErrorInst = NULL;
2115}
2116
2117
Guido van Rossum79f25d91997-04-29 20:08:16 +00002118static PyObject *
Guido van Rossum25ce5661997-08-02 03:10:38 +00002119newstdexception(dict, name)
2120 PyObject *dict;
Guido van Rossumfb905c31991-12-16 15:42:38 +00002121 char *name;
Guido van Rossum3f5da241990-12-20 15:06:42 +00002122{
Guido van Rossum79f25d91997-04-29 20:08:16 +00002123 PyObject *v = PyString_FromString(name);
Guido van Rossum25ce5661997-08-02 03:10:38 +00002124 if (v == NULL || PyDict_SetItemString(dict, name, v) != 0)
Barry Warsaw98b62461998-09-14 18:51:11 +00002125 Py_FatalError("Cannot create string-based exceptions");
Guido van Rossum3f5da241990-12-20 15:06:42 +00002126 return v;
2127}
2128
2129static void
Guido van Rossum25ce5661997-08-02 03:10:38 +00002130initerrors(dict)
2131 PyObject *dict;
Guido van Rossum3f5da241990-12-20 15:06:42 +00002132{
Barry Warsaw757af0e1997-08-29 22:13:51 +00002133 int i;
2134 int exccnt = 0;
2135 for (i = 0; bltin_exc[i].name; i++, exccnt++) {
Barry Warsaw98b62461998-09-14 18:51:11 +00002136 Py_XDECREF(*bltin_exc[i].exc);
Barry Warsaw757af0e1997-08-29 22:13:51 +00002137 if (bltin_exc[i].leaf_exc)
2138 *bltin_exc[i].exc =
2139 newstdexception(dict, bltin_exc[i].name);
2140 }
2141
Barry Warsawd086a1a1998-07-23 15:59:57 +00002142 /* This is kind of bogus because we special case the some of the
2143 * new exceptions to be nearly forward compatible. But this means
2144 * we hard code knowledge about exceptions.py into C here. I don't
2145 * have a better solution, though.
2146 */
Barry Warsaw757af0e1997-08-29 22:13:51 +00002147 PyExc_LookupError = PyTuple_New(2);
2148 Py_INCREF(PyExc_IndexError);
2149 PyTuple_SET_ITEM(PyExc_LookupError, 0, PyExc_IndexError);
2150 Py_INCREF(PyExc_KeyError);
2151 PyTuple_SET_ITEM(PyExc_LookupError, 1, PyExc_KeyError);
2152 PyDict_SetItemString(dict, "LookupError", PyExc_LookupError);
2153
Barry Warsaw412cdc21997-09-16 21:51:14 +00002154 PyExc_ArithmeticError = PyTuple_New(3);
Barry Warsaw757af0e1997-08-29 22:13:51 +00002155 Py_INCREF(PyExc_OverflowError);
Barry Warsaw412cdc21997-09-16 21:51:14 +00002156 PyTuple_SET_ITEM(PyExc_ArithmeticError, 0, PyExc_OverflowError);
Barry Warsaw757af0e1997-08-29 22:13:51 +00002157 Py_INCREF(PyExc_ZeroDivisionError);
Barry Warsaw412cdc21997-09-16 21:51:14 +00002158 PyTuple_SET_ITEM(PyExc_ArithmeticError, 1, PyExc_ZeroDivisionError);
Barry Warsaw757af0e1997-08-29 22:13:51 +00002159 Py_INCREF(PyExc_FloatingPointError);
Barry Warsaw412cdc21997-09-16 21:51:14 +00002160 PyTuple_SET_ITEM(PyExc_ArithmeticError, 2, PyExc_FloatingPointError);
2161 PyDict_SetItemString(dict, "ArithmeticError", PyExc_ArithmeticError);
Barry Warsaw757af0e1997-08-29 22:13:51 +00002162
Barry Warsawd086a1a1998-07-23 15:59:57 +00002163 PyExc_EnvironmentError = PyTuple_New(2);
2164 Py_INCREF(PyExc_IOError);
2165 PyTuple_SET_ITEM(PyExc_EnvironmentError, 0, PyExc_IOError);
2166 Py_INCREF(PyExc_OSError);
2167 PyTuple_SET_ITEM(PyExc_EnvironmentError, 1, PyExc_OSError);
2168 PyDict_SetItemString(dict, "EnvironmentError", PyExc_EnvironmentError);
2169
Barry Warsawb01a7fa1997-09-18 03:44:38 +00002170 PyExc_StandardError = PyTuple_New(exccnt-2);
2171 for (i = 2; bltin_exc[i].name; i++) {
Barry Warsaw757af0e1997-08-29 22:13:51 +00002172 PyObject *exc = *bltin_exc[i].exc;
2173 Py_INCREF(exc);
Barry Warsawb01a7fa1997-09-18 03:44:38 +00002174 PyTuple_SET_ITEM(PyExc_StandardError, i-2, exc);
Barry Warsaw757af0e1997-08-29 22:13:51 +00002175 }
2176 PyDict_SetItemString(dict, "StandardError", PyExc_StandardError);
Guido van Rossum04748321997-09-16 18:43:15 +00002177
2178 /* Exception is treated differently; for now, it's == StandardError */
2179 PyExc_Exception = PyExc_StandardError;
2180 Py_INCREF(PyExc_Exception);
2181 PyDict_SetItemString(dict, "Exception", PyExc_Exception);
Barry Warsaw757af0e1997-08-29 22:13:51 +00002182
2183 if (PyErr_Occurred())
2184 Py_FatalError("Could not initialize built-in string exceptions");
Guido van Rossum25ce5661997-08-02 03:10:38 +00002185}
2186
2187static void
2188finierrors()
2189{
Barry Warsaw757af0e1997-08-29 22:13:51 +00002190 int i;
2191 for (i = 0; bltin_exc[i].name; i++) {
2192 PyObject *exc = *bltin_exc[i].exc;
2193 Py_XDECREF(exc);
2194 *bltin_exc[i].exc = NULL;
2195 }
Guido van Rossum25ce5661997-08-02 03:10:38 +00002196}
2197
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002198static char builtin_doc[] =
2199"Built-in functions, exceptions, and other objects.\n\
2200\n\
2201Noteworthy: None is the `nil' object; Ellipsis represents `...' in slices.";
2202
Guido van Rossum25ce5661997-08-02 03:10:38 +00002203PyObject *
Barry Warsaw757af0e1997-08-29 22:13:51 +00002204_PyBuiltin_Init_1()
Guido van Rossum25ce5661997-08-02 03:10:38 +00002205{
2206 PyObject *mod, *dict;
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002207 mod = Py_InitModule4("__builtin__", builtin_methods,
2208 builtin_doc, (PyObject *)NULL,
2209 PYTHON_API_VERSION);
Guido van Rossum25ce5661997-08-02 03:10:38 +00002210 if (mod == NULL)
2211 return NULL;
2212 dict = PyModule_GetDict(mod);
2213 initerrors(dict);
2214 if (PyDict_SetItemString(dict, "None", Py_None) < 0)
2215 return NULL;
2216 if (PyDict_SetItemString(dict, "Ellipsis", Py_Ellipsis) < 0)
2217 return NULL;
2218 if (PyDict_SetItemString(dict, "__debug__",
2219 PyInt_FromLong(Py_OptimizeFlag == 0)) < 0)
2220 return NULL;
Barry Warsaw757af0e1997-08-29 22:13:51 +00002221
Guido van Rossum25ce5661997-08-02 03:10:38 +00002222 return mod;
Guido van Rossum3f5da241990-12-20 15:06:42 +00002223}
2224
2225void
Barry Warsaw757af0e1997-08-29 22:13:51 +00002226_PyBuiltin_Init_2(dict)
2227 PyObject *dict;
2228{
2229 /* if Python was started with -X, initialize the class exceptions */
Barry Warsaw98b62461998-09-14 18:51:11 +00002230 if (Py_UseClassExceptionsFlag) {
2231 if (!init_class_exc(dict)) {
2232 /* class based exceptions could not be
2233 * initialized. Fall back to using string based
2234 * exceptions.
2235 */
2236 PySys_WriteStderr(
2237 "Warning! Falling back to string-based exceptions\n");
2238 initerrors(dict);
2239 }
2240 }
Barry Warsaw757af0e1997-08-29 22:13:51 +00002241}
2242
2243
2244void
2245_PyBuiltin_Fini_1()
2246{
2247 fini_instances();
2248}
2249
2250
2251void
2252_PyBuiltin_Fini_2()
Guido van Rossum3f5da241990-12-20 15:06:42 +00002253{
Guido van Rossum25ce5661997-08-02 03:10:38 +00002254 finierrors();
Guido van Rossum3f5da241990-12-20 15:06:42 +00002255}
Guido van Rossumc6bb8f71991-07-01 18:42:41 +00002256
Guido van Rossum12d12c51993-10-26 17:58:25 +00002257
Guido van Rossume77a7571993-11-03 15:01:26 +00002258/* Helper for filter(): filter a tuple through a function */
Guido van Rossum12d12c51993-10-26 17:58:25 +00002259
Guido van Rossum79f25d91997-04-29 20:08:16 +00002260static PyObject *
Guido van Rossum12d12c51993-10-26 17:58:25 +00002261filtertuple(func, tuple)
Guido van Rossum79f25d91997-04-29 20:08:16 +00002262 PyObject *func;
2263 PyObject *tuple;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002264{
Guido van Rossum79f25d91997-04-29 20:08:16 +00002265 PyObject *result;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002266 register int i, j;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002267 int len = PyTuple_Size(tuple);
Guido van Rossum12d12c51993-10-26 17:58:25 +00002268
Guido van Rossumb7b45621995-08-04 04:07:45 +00002269 if (len == 0) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002270 Py_INCREF(tuple);
Guido van Rossumb7b45621995-08-04 04:07:45 +00002271 return tuple;
2272 }
2273
Guido van Rossum79f25d91997-04-29 20:08:16 +00002274 if ((result = PyTuple_New(len)) == NULL)
Guido van Rossum2586bf01993-11-01 16:21:44 +00002275 return NULL;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002276
Guido van Rossum12d12c51993-10-26 17:58:25 +00002277 for (i = j = 0; i < len; ++i) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002278 PyObject *item, *good;
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002279 int ok;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002280
Guido van Rossum79f25d91997-04-29 20:08:16 +00002281 if ((item = PyTuple_GetItem(tuple, i)) == NULL)
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002282 goto Fail_1;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002283 if (func == Py_None) {
2284 Py_INCREF(item);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002285 good = item;
2286 }
2287 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002288 PyObject *arg = Py_BuildValue("(O)", item);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002289 if (arg == NULL)
2290 goto Fail_1;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002291 good = PyEval_CallObject(func, arg);
2292 Py_DECREF(arg);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002293 if (good == NULL)
Guido van Rossum12d12c51993-10-26 17:58:25 +00002294 goto Fail_1;
2295 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002296 ok = PyObject_IsTrue(good);
2297 Py_DECREF(good);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002298 if (ok) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002299 Py_INCREF(item);
2300 if (PyTuple_SetItem(result, j++, item) < 0)
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002301 goto Fail_1;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002302 }
Guido van Rossum12d12c51993-10-26 17:58:25 +00002303 }
2304
Guido van Rossum79f25d91997-04-29 20:08:16 +00002305 if (_PyTuple_Resize(&result, j, 0) < 0)
Guido van Rossum12d12c51993-10-26 17:58:25 +00002306 return NULL;
2307
Guido van Rossum12d12c51993-10-26 17:58:25 +00002308 return result;
2309
Guido van Rossum12d12c51993-10-26 17:58:25 +00002310Fail_1:
Guido van Rossum79f25d91997-04-29 20:08:16 +00002311 Py_DECREF(result);
Guido van Rossum12d12c51993-10-26 17:58:25 +00002312 return NULL;
2313}
2314
2315
Guido van Rossume77a7571993-11-03 15:01:26 +00002316/* Helper for filter(): filter a string through a function */
Guido van Rossum12d12c51993-10-26 17:58:25 +00002317
Guido van Rossum79f25d91997-04-29 20:08:16 +00002318static PyObject *
Guido van Rossum12d12c51993-10-26 17:58:25 +00002319filterstring(func, strobj)
Guido van Rossum79f25d91997-04-29 20:08:16 +00002320 PyObject *func;
2321 PyObject *strobj;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002322{
Guido van Rossum79f25d91997-04-29 20:08:16 +00002323 PyObject *result;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002324 register int i, j;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002325 int len = PyString_Size(strobj);
Guido van Rossum12d12c51993-10-26 17:58:25 +00002326
Guido van Rossum79f25d91997-04-29 20:08:16 +00002327 if (func == Py_None) {
Guido van Rossum2586bf01993-11-01 16:21:44 +00002328 /* No character is ever false -- share input string */
Guido van Rossum79f25d91997-04-29 20:08:16 +00002329 Py_INCREF(strobj);
Guido van Rossum2d951851994-08-29 12:52:16 +00002330 return strobj;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002331 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002332 if ((result = PyString_FromStringAndSize(NULL, len)) == NULL)
Guido van Rossum2586bf01993-11-01 16:21:44 +00002333 return NULL;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002334
Guido van Rossum12d12c51993-10-26 17:58:25 +00002335 for (i = j = 0; i < len; ++i) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002336 PyObject *item, *arg, *good;
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002337 int ok;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002338
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002339 item = (*strobj->ob_type->tp_as_sequence->sq_item)(strobj, i);
2340 if (item == NULL)
2341 goto Fail_1;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002342 arg = Py_BuildValue("(O)", item);
2343 Py_DECREF(item);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002344 if (arg == NULL)
2345 goto Fail_1;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002346 good = PyEval_CallObject(func, arg);
2347 Py_DECREF(arg);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002348 if (good == NULL)
2349 goto Fail_1;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002350 ok = PyObject_IsTrue(good);
2351 Py_DECREF(good);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002352 if (ok)
Guido van Rossum79f25d91997-04-29 20:08:16 +00002353 PyString_AS_STRING((PyStringObject *)result)[j++] =
2354 PyString_AS_STRING((PyStringObject *)item)[0];
Guido van Rossum12d12c51993-10-26 17:58:25 +00002355 }
2356
Guido van Rossum79f25d91997-04-29 20:08:16 +00002357 if (j < len && _PyString_Resize(&result, j) < 0)
Guido van Rossum12d12c51993-10-26 17:58:25 +00002358 return NULL;
2359
Guido van Rossum12d12c51993-10-26 17:58:25 +00002360 return result;
2361
Guido van Rossum12d12c51993-10-26 17:58:25 +00002362Fail_1:
Guido van Rossum79f25d91997-04-29 20:08:16 +00002363 Py_DECREF(result);
Guido van Rossum12d12c51993-10-26 17:58:25 +00002364 return NULL;
2365}