blob: f141f4dbdc06e6abcec75ff1ebd37c98f223bd3b [file] [log] [blame]
Guido van Rossumf70e43a1991-02-19 12:39:46 +00001/***********************************************************
Guido van Rossum6d023c91995-01-04 19:12:13 +00002Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
3The Netherlands.
Guido van Rossumf70e43a1991-02-19 12:39:46 +00004
5 All Rights Reserved
6
Guido van Rossumd266eb41996-10-25 14:44:06 +00007Permission to use, copy, modify, and distribute this software and its
8documentation for any purpose and without fee is hereby granted,
Guido van Rossumf70e43a1991-02-19 12:39:46 +00009provided that the above copyright notice appear in all copies and that
Guido van Rossumd266eb41996-10-25 14:44:06 +000010both that copyright notice and this permission notice appear in
Guido van Rossumf70e43a1991-02-19 12:39:46 +000011supporting documentation, and that the names of Stichting Mathematisch
Guido van Rossumd266eb41996-10-25 14:44:06 +000012Centrum or CWI or Corporation for National Research Initiatives or
13CNRI not be used in advertising or publicity pertaining to
14distribution of the software without specific, written prior
15permission.
Guido van Rossumf70e43a1991-02-19 12:39:46 +000016
Guido van Rossumd266eb41996-10-25 14:44:06 +000017While CWI is the initial source for this software, a modified version
18is made available by the Corporation for National Research Initiatives
19(CNRI) at the Internet address ftp://ftp.python.org.
20
21STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH
22REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF
23MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH
24CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
25DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
26PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
27TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
28PERFORMANCE OF THIS SOFTWARE.
Guido van Rossumf70e43a1991-02-19 12:39:46 +000029
30******************************************************************/
31
Guido van Rossum3f5da241990-12-20 15:06:42 +000032/* Built-in functions */
33
Guido van Rossum79f25d91997-04-29 20:08:16 +000034#include "Python.h"
Guido van Rossum3f5da241990-12-20 15:06:42 +000035
36#include "node.h"
Guido van Rossum5b722181993-03-30 17:46:03 +000037#include "compile.h"
38#include "eval.h"
Guido van Rossum3f5da241990-12-20 15:06:42 +000039
Guido van Rossumfe4b6ee1996-08-08 18:49:41 +000040#include "mymath.h"
41
Guido van Rossum6bf62da1997-04-11 20:37:35 +000042#include <ctype.h>
43
Guido van Rossum1a2c5cb1996-12-10 15:37:36 +000044#ifdef HAVE_UNISTD_H
45#include <unistd.h>
46#endif
47
Guido van Rossum12d12c51993-10-26 17:58:25 +000048/* Forward */
Guido van Rossum79f25d91997-04-29 20:08:16 +000049static PyObject *filterstring Py_PROTO((PyObject *, PyObject *));
50static PyObject *filtertuple Py_PROTO((PyObject *, PyObject *));
Guido van Rossum12d12c51993-10-26 17:58:25 +000051
Guido van Rossum79f25d91997-04-29 20:08:16 +000052static PyObject *
Guido van Rossum1ae940a1995-01-02 19:04:15 +000053builtin___import__(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +000054 PyObject *self;
55 PyObject *args;
Guido van Rossum3f5da241990-12-20 15:06:42 +000056{
Guido van Rossum1ae940a1995-01-02 19:04:15 +000057 char *name;
Guido van Rossum79f25d91997-04-29 20:08:16 +000058 PyObject *globals = NULL;
59 PyObject *locals = NULL;
60 PyObject *fromlist = NULL;
Guido van Rossum1ae940a1995-01-02 19:04:15 +000061
Guido van Rossum79f25d91997-04-29 20:08:16 +000062 if (!PyArg_ParseTuple(args, "s|OOO:__import__",
Guido van Rossum24c13741995-02-14 09:42:43 +000063 &name, &globals, &locals, &fromlist))
Guido van Rossum1ae940a1995-01-02 19:04:15 +000064 return NULL;
Guido van Rossumaee0bad1997-09-05 07:33:22 +000065 return PyImport_ImportModuleEx(name, globals, locals, fromlist);
Guido van Rossum1ae940a1995-01-02 19:04:15 +000066}
67
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +000068static char import_doc[] =
69"__import__(name, globals, locals, fromlist) -> module\n\
70\n\
71Import a module. The globals are only used to determine the context;\n\
72they are not modified. The locals are currently unused. The fromlist\n\
73should be a list of names to emulate ``from name import ...'', or an\n\
74empty list to emulate ``import name''.\n\
75When importing a module from a package, note that __import__('A.B', ...)\n\
76returns package A when fromlist is empty, but its submodule B when\n\
77fromlist is not empty.";
78
Guido van Rossum1ae940a1995-01-02 19:04:15 +000079
Guido van Rossum79f25d91997-04-29 20:08:16 +000080static PyObject *
Guido van Rossum1ae940a1995-01-02 19:04:15 +000081builtin_abs(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +000082 PyObject *self;
83 PyObject *args;
Guido van Rossum1ae940a1995-01-02 19:04:15 +000084{
Guido van Rossum79f25d91997-04-29 20:08:16 +000085 PyObject *v;
Guido van Rossum1ae940a1995-01-02 19:04:15 +000086
Guido van Rossum79f25d91997-04-29 20:08:16 +000087 if (!PyArg_ParseTuple(args, "O:abs", &v))
Guido van Rossum1ae940a1995-01-02 19:04:15 +000088 return NULL;
Guido van Rossum09df08a1998-05-22 00:51:39 +000089 return PyNumber_Absolute(v);
Guido van Rossum3f5da241990-12-20 15:06:42 +000090}
91
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +000092static char abs_doc[] =
93"abs(number) -> number\n\
94\n\
95Return the absolute value of the argument.";
96
97
Guido van Rossum79f25d91997-04-29 20:08:16 +000098static PyObject *
Guido van Rossum94390a41992-08-14 15:14:30 +000099builtin_apply(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000100 PyObject *self;
101 PyObject *args;
Guido van Rossumc02e15c1991-12-16 13:03:00 +0000102{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000103 PyObject *func, *alist = NULL, *kwdict = NULL;
Barry Warsaw968f8cb1998-10-01 15:33:12 +0000104 PyObject *t = NULL, *retval = NULL;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000105
Guido van Rossum79f25d91997-04-29 20:08:16 +0000106 if (!PyArg_ParseTuple(args, "O|OO:apply", &func, &alist, &kwdict))
Guido van Rossumc02e15c1991-12-16 13:03:00 +0000107 return NULL;
Barry Warsaw968f8cb1998-10-01 15:33:12 +0000108 if (alist != NULL) {
109 if (!PyTuple_Check(alist)) {
110 if (!PySequence_Check(alist)) {
111 PyErr_SetString(PyExc_TypeError,
112 "apply() 2nd argument must be a sequence");
113 return NULL;
114 }
115 t = PySequence_Tuple(alist);
116 if (t == NULL)
117 return NULL;
118 alist = t;
119 }
Guido van Rossum2d951851994-08-29 12:52:16 +0000120 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000121 if (kwdict != NULL && !PyDict_Check(kwdict)) {
122 PyErr_SetString(PyExc_TypeError,
Guido van Rossum681d79a1995-07-18 14:51:37 +0000123 "apply() 3rd argument must be dictionary");
Barry Warsaw968f8cb1998-10-01 15:33:12 +0000124 goto finally;
Guido van Rossum681d79a1995-07-18 14:51:37 +0000125 }
Barry Warsaw968f8cb1998-10-01 15:33:12 +0000126 retval = PyEval_CallObjectWithKeywords(func, alist, kwdict);
127 finally:
128 Py_XDECREF(t);
129 return retval;
Guido van Rossumc02e15c1991-12-16 13:03:00 +0000130}
131
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000132static char apply_doc[] =
133"apply(function, args[, kwargs]) -> value\n\
134\n\
135Call a function with positional arguments taken from the tuple args,\n\
136and keyword arguments taken from the optional dictionary kwargs.";
137
138
Guido van Rossum79f25d91997-04-29 20:08:16 +0000139static PyObject *
Guido van Rossum2d951851994-08-29 12:52:16 +0000140builtin_callable(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000141 PyObject *self;
142 PyObject *args;
Guido van Rossum2d951851994-08-29 12:52:16 +0000143{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000144 PyObject *v;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000145
Guido van Rossum79f25d91997-04-29 20:08:16 +0000146 if (!PyArg_ParseTuple(args, "O:callable", &v))
Guido van Rossum2d951851994-08-29 12:52:16 +0000147 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000148 return PyInt_FromLong((long)PyCallable_Check(v));
Guido van Rossum2d951851994-08-29 12:52:16 +0000149}
150
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000151static char callable_doc[] =
152"callable(object) -> Boolean\n\
153\n\
154Return whether the object is callable (i.e., some kind of function).\n\
155Note that classes are callable, as are instances with a __call__() method.";
156
157
Guido van Rossum79f25d91997-04-29 20:08:16 +0000158static PyObject *
Guido van Rossume77a7571993-11-03 15:01:26 +0000159builtin_filter(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000160 PyObject *self;
161 PyObject *args;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000162{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000163 PyObject *func, *seq, *result;
164 PySequenceMethods *sqf;
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000165 int len;
166 register int i, j;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000167
Guido van Rossum79f25d91997-04-29 20:08:16 +0000168 if (!PyArg_ParseTuple(args, "OO:filter", &func, &seq))
Guido van Rossum12d12c51993-10-26 17:58:25 +0000169 return NULL;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000170
Guido van Rossum79f25d91997-04-29 20:08:16 +0000171 if (PyString_Check(seq)) {
172 PyObject *r = filterstring(func, seq);
Guido van Rossum12d12c51993-10-26 17:58:25 +0000173 return r;
174 }
175
Guido van Rossum79f25d91997-04-29 20:08:16 +0000176 if (PyTuple_Check(seq)) {
177 PyObject *r = filtertuple(func, seq);
Guido van Rossum12d12c51993-10-26 17:58:25 +0000178 return r;
179 }
180
Guido van Rossum09df08a1998-05-22 00:51:39 +0000181 sqf = seq->ob_type->tp_as_sequence;
182 if (sqf == NULL || sqf->sq_length == NULL || sqf->sq_item == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000183 PyErr_SetString(PyExc_TypeError,
Guido van Rossume77a7571993-11-03 15:01:26 +0000184 "argument 2 to filter() must be a sequence type");
Guido van Rossum12d12c51993-10-26 17:58:25 +0000185 goto Fail_2;
186 }
187
188 if ((len = (*sqf->sq_length)(seq)) < 0)
189 goto Fail_2;
190
Guido van Rossum79f25d91997-04-29 20:08:16 +0000191 if (PyList_Check(seq) && seq->ob_refcnt == 1) {
192 Py_INCREF(seq);
Guido van Rossum12d12c51993-10-26 17:58:25 +0000193 result = seq;
194 }
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000195 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000196 if ((result = PyList_New(len)) == NULL)
Guido van Rossum12d12c51993-10-26 17:58:25 +0000197 goto Fail_2;
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000198 }
Guido van Rossum12d12c51993-10-26 17:58:25 +0000199
Guido van Rossum2d951851994-08-29 12:52:16 +0000200 for (i = j = 0; ; ++i) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000201 PyObject *item, *good;
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000202 int ok;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000203
Guido van Rossum2d951851994-08-29 12:52:16 +0000204 if ((item = (*sqf->sq_item)(seq, i)) == NULL) {
Barry Warsawcde8b1b1997-08-22 21:14:38 +0000205 if (PyErr_ExceptionMatches(PyExc_IndexError)) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000206 PyErr_Clear();
Guido van Rossum2d951851994-08-29 12:52:16 +0000207 break;
208 }
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000209 goto Fail_1;
Guido van Rossum2d951851994-08-29 12:52:16 +0000210 }
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000211
Guido van Rossum79f25d91997-04-29 20:08:16 +0000212 if (func == Py_None) {
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000213 good = item;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000214 Py_INCREF(good);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000215 }
216 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000217 PyObject *arg = Py_BuildValue("(O)", item);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000218 if (arg == NULL)
219 goto Fail_1;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000220 good = PyEval_CallObject(func, arg);
221 Py_DECREF(arg);
Guido van Rossum58b68731995-01-10 17:40:55 +0000222 if (good == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000223 Py_DECREF(item);
Guido van Rossum12d12c51993-10-26 17:58:25 +0000224 goto Fail_1;
Guido van Rossum58b68731995-01-10 17:40:55 +0000225 }
Guido van Rossum12d12c51993-10-26 17:58:25 +0000226 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000227 ok = PyObject_IsTrue(good);
228 Py_DECREF(good);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000229 if (ok) {
Guido van Rossum2d951851994-08-29 12:52:16 +0000230 if (j < len) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000231 if (PyList_SetItem(result, j++, item) < 0)
Guido van Rossum2d951851994-08-29 12:52:16 +0000232 goto Fail_1;
233 }
234 else {
Barry Warsawfa77e091999-01-28 18:49:12 +0000235 int status = PyList_Append(result, item);
Guido van Rossum2d951851994-08-29 12:52:16 +0000236 j++;
Barry Warsawfa77e091999-01-28 18:49:12 +0000237 Py_DECREF(item);
238 if (status < 0)
Guido van Rossum2d951851994-08-29 12:52:16 +0000239 goto Fail_1;
240 }
Guido van Rossum58b68731995-01-10 17:40:55 +0000241 } else {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000242 Py_DECREF(item);
Guido van Rossum12d12c51993-10-26 17:58:25 +0000243 }
Guido van Rossum12d12c51993-10-26 17:58:25 +0000244 }
245
Guido van Rossum12d12c51993-10-26 17:58:25 +0000246
Guido van Rossum79f25d91997-04-29 20:08:16 +0000247 if (j < len && PyList_SetSlice(result, j, len, NULL) < 0)
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000248 goto Fail_1;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000249
Guido van Rossum12d12c51993-10-26 17:58:25 +0000250 return result;
251
Guido van Rossum12d12c51993-10-26 17:58:25 +0000252Fail_1:
Guido van Rossum79f25d91997-04-29 20:08:16 +0000253 Py_DECREF(result);
Guido van Rossum12d12c51993-10-26 17:58:25 +0000254Fail_2:
Guido van Rossum12d12c51993-10-26 17:58:25 +0000255 return NULL;
256}
257
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000258static char filter_doc[] =
259"filter(function, sequence) -> list\n\
260\n\
261Return a list containing those items of sequence for which function(item)\n\
262is true. If function is None, return a list of items that are true.";
263
264
Guido van Rossum79f25d91997-04-29 20:08:16 +0000265static PyObject *
Guido van Rossum94390a41992-08-14 15:14:30 +0000266builtin_chr(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000267 PyObject *self;
268 PyObject *args;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000269{
270 long x;
271 char s[1];
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000272
Guido van Rossum79f25d91997-04-29 20:08:16 +0000273 if (!PyArg_ParseTuple(args, "l:chr", &x))
Guido van Rossum3f5da241990-12-20 15:06:42 +0000274 return NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000275 if (x < 0 || x >= 256) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000276 PyErr_SetString(PyExc_ValueError,
277 "chr() arg not in range(256)");
Guido van Rossum3f5da241990-12-20 15:06:42 +0000278 return NULL;
279 }
Guido van Rossum6bf62da1997-04-11 20:37:35 +0000280 s[0] = (char)x;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000281 return PyString_FromStringAndSize(s, 1);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000282}
283
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000284static char chr_doc[] =
285"chr(i) -> character\n\
286\n\
287Return a string of one character with ordinal i; 0 <= i < 256.";
288
289
Guido van Rossum79f25d91997-04-29 20:08:16 +0000290static PyObject *
Guido van Rossuma9e7dc11992-10-18 18:53:57 +0000291builtin_cmp(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000292 PyObject *self;
293 PyObject *args;
Guido van Rossuma9e7dc11992-10-18 18:53:57 +0000294{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000295 PyObject *a, *b;
Guido van Rossum09df08a1998-05-22 00:51:39 +0000296 int c;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000297
Guido van Rossum79f25d91997-04-29 20:08:16 +0000298 if (!PyArg_ParseTuple(args, "OO:cmp", &a, &b))
Guido van Rossuma9e7dc11992-10-18 18:53:57 +0000299 return NULL;
Guido van Rossum09df08a1998-05-22 00:51:39 +0000300 if (PyObject_Cmp(a, b, &c) < 0)
Guido van Rossumc8b6df91997-05-23 00:06:51 +0000301 return NULL;
Guido van Rossum09df08a1998-05-22 00:51:39 +0000302 return PyInt_FromLong((long)c);
Guido van Rossuma9e7dc11992-10-18 18:53:57 +0000303}
304
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000305static char cmp_doc[] =
306"cmp(x, y) -> integer\n\
307\n\
308Return negative if x<y, zero if x==y, positive if x>y.";
309
310
Guido van Rossum79f25d91997-04-29 20:08:16 +0000311static PyObject *
Guido van Rossum5524a591995-01-10 15:26:20 +0000312builtin_coerce(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000313 PyObject *self;
314 PyObject *args;
Guido van Rossum6a00cd81995-01-07 12:39:01 +0000315{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000316 PyObject *v, *w;
317 PyObject *res;
Guido van Rossum5524a591995-01-10 15:26:20 +0000318
Guido van Rossum79f25d91997-04-29 20:08:16 +0000319 if (!PyArg_ParseTuple(args, "OO:coerce", &v, &w))
Guido van Rossum5524a591995-01-10 15:26:20 +0000320 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000321 if (PyNumber_Coerce(&v, &w) < 0)
Guido van Rossum04691fc1992-08-12 15:35:34 +0000322 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000323 res = Py_BuildValue("(OO)", v, w);
324 Py_DECREF(v);
325 Py_DECREF(w);
Guido van Rossum04691fc1992-08-12 15:35:34 +0000326 return res;
327}
328
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000329static char coerce_doc[] =
330"coerce(x, y) -> None or (x1, y1)\n\
331\n\
332When x and y can be coerced to values of the same type, return a tuple\n\
333containing the coerced values. When they can't be coerced, return None.";
334
335
Guido van Rossum79f25d91997-04-29 20:08:16 +0000336static PyObject *
Guido van Rossum5b722181993-03-30 17:46:03 +0000337builtin_compile(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000338 PyObject *self;
339 PyObject *args;
Guido van Rossum5b722181993-03-30 17:46:03 +0000340{
341 char *str;
342 char *filename;
343 char *startstr;
344 int start;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000345
Guido van Rossum79f25d91997-04-29 20:08:16 +0000346 if (!PyArg_ParseTuple(args, "sss:compile", &str, &filename, &startstr))
Guido van Rossum5b722181993-03-30 17:46:03 +0000347 return NULL;
348 if (strcmp(startstr, "exec") == 0)
Guido van Rossumb05a5c71997-05-07 17:46:13 +0000349 start = Py_file_input;
Guido van Rossum5b722181993-03-30 17:46:03 +0000350 else if (strcmp(startstr, "eval") == 0)
Guido van Rossumb05a5c71997-05-07 17:46:13 +0000351 start = Py_eval_input;
Guido van Rossum872537c1995-07-07 22:43:42 +0000352 else if (strcmp(startstr, "single") == 0)
Guido van Rossumb05a5c71997-05-07 17:46:13 +0000353 start = Py_single_input;
Guido van Rossum5b722181993-03-30 17:46:03 +0000354 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000355 PyErr_SetString(PyExc_ValueError,
Guido van Rossum872537c1995-07-07 22:43:42 +0000356 "compile() mode must be 'exec' or 'eval' or 'single'");
Guido van Rossum5b722181993-03-30 17:46:03 +0000357 return NULL;
358 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000359 return Py_CompileString(str, filename, start);
Guido van Rossum5b722181993-03-30 17:46:03 +0000360}
361
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000362static char compile_doc[] =
363"compile(source, filename, mode) -> code object\n\
364\n\
365Compile the source string (a Python module, statement or expression)\n\
366into a code object that can be executed by the exec statement or eval().\n\
367The filename will be used for run-time error messages.\n\
368The mode must be 'exec' to compile a module, 'single' to compile a\n\
369single (interactive) statement, or 'eval' to compile an expression.";
370
371
Guido van Rossum8a5c5d21996-01-12 01:09:56 +0000372#ifndef WITHOUT_COMPLEX
373
Guido van Rossum79f25d91997-04-29 20:08:16 +0000374static PyObject *
Guido van Rossum8a5c5d21996-01-12 01:09:56 +0000375builtin_complex(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000376 PyObject *self;
377 PyObject *args;
Guido van Rossum8a5c5d21996-01-12 01:09:56 +0000378{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000379 PyObject *r, *i, *tmp;
380 PyNumberMethods *nbr, *nbi = NULL;
Guido van Rossum530956d1996-07-21 02:27:43 +0000381 Py_complex cr, ci;
Guido van Rossumc6472e91997-03-31 17:15:43 +0000382 int own_r = 0;
Guido van Rossum8a5c5d21996-01-12 01:09:56 +0000383
384 i = NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000385 if (!PyArg_ParseTuple(args, "O|O:complex", &r, &i))
Guido van Rossum8a5c5d21996-01-12 01:09:56 +0000386 return NULL;
387 if ((nbr = r->ob_type->tp_as_number) == NULL ||
Guido van Rossumc6472e91997-03-31 17:15:43 +0000388 nbr->nb_float == NULL ||
389 (i != NULL &&
390 ((nbi = i->ob_type->tp_as_number) == NULL ||
391 nbi->nb_float == NULL))) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000392 PyErr_SetString(PyExc_TypeError,
Guido van Rossum8a5c5d21996-01-12 01:09:56 +0000393 "complex() argument can't be converted to complex");
394 return NULL;
395 }
Guido van Rossumed0af8f1996-12-05 23:18:18 +0000396 /* XXX Hack to support classes with __complex__ method */
Guido van Rossum79f25d91997-04-29 20:08:16 +0000397 if (PyInstance_Check(r)) {
398 static PyObject *complexstr;
399 PyObject *f;
Guido van Rossumed0af8f1996-12-05 23:18:18 +0000400 if (complexstr == NULL) {
Guido van Rossum8d751611997-01-18 08:04:16 +0000401 complexstr = PyString_InternFromString("__complex__");
Guido van Rossumed0af8f1996-12-05 23:18:18 +0000402 if (complexstr == NULL)
403 return NULL;
404 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000405 f = PyObject_GetAttr(r, complexstr);
Guido van Rossumed0af8f1996-12-05 23:18:18 +0000406 if (f == NULL)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000407 PyErr_Clear();
Guido van Rossumed0af8f1996-12-05 23:18:18 +0000408 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000409 PyObject *args = Py_BuildValue("()");
Guido van Rossumed0af8f1996-12-05 23:18:18 +0000410 if (args == NULL)
411 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000412 r = PyEval_CallObject(f, args);
413 Py_DECREF(args);
Barry Warsawf988e681999-01-27 23:13:59 +0000414 Py_DECREF(f);
Guido van Rossumed0af8f1996-12-05 23:18:18 +0000415 if (r == NULL)
416 return NULL;
Guido van Rossumc6472e91997-03-31 17:15:43 +0000417 own_r = 1;
Guido van Rossumed0af8f1996-12-05 23:18:18 +0000418 }
419 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000420 if (PyComplex_Check(r)) {
421 cr = ((PyComplexObject*)r)->cval;
Guido van Rossum730806d1998-04-10 22:27:42 +0000422 if (own_r) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000423 Py_DECREF(r);
Guido van Rossum730806d1998-04-10 22:27:42 +0000424 }
Guido van Rossumc6472e91997-03-31 17:15:43 +0000425 }
Guido van Rossum8a5c5d21996-01-12 01:09:56 +0000426 else {
Guido van Rossumfe4b6ee1996-08-08 18:49:41 +0000427 tmp = (*nbr->nb_float)(r);
Guido van Rossum730806d1998-04-10 22:27:42 +0000428 if (own_r) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000429 Py_DECREF(r);
Guido van Rossum730806d1998-04-10 22:27:42 +0000430 }
Guido van Rossumfe4b6ee1996-08-08 18:49:41 +0000431 if (tmp == NULL)
432 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000433 cr.real = PyFloat_AsDouble(tmp);
434 Py_DECREF(tmp);
Guido van Rossum8a5c5d21996-01-12 01:09:56 +0000435 cr.imag = 0.;
436 }
437 if (i == NULL) {
438 ci.real = 0.;
439 ci.imag = 0.;
440 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000441 else if (PyComplex_Check(i))
442 ci = ((PyComplexObject*)i)->cval;
Guido van Rossum8a5c5d21996-01-12 01:09:56 +0000443 else {
Guido van Rossumc6472e91997-03-31 17:15:43 +0000444 tmp = (*nbi->nb_float)(i);
Guido van Rossumfe4b6ee1996-08-08 18:49:41 +0000445 if (tmp == NULL)
446 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000447 ci.real = PyFloat_AsDouble(tmp);
448 Py_DECREF(tmp);
Guido van Rossum8a5c5d21996-01-12 01:09:56 +0000449 ci.imag = 0.;
450 }
451 cr.real -= ci.imag;
452 cr.imag += ci.real;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000453 return PyComplex_FromCComplex(cr);
Guido van Rossum8a5c5d21996-01-12 01:09:56 +0000454}
455
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000456static char complex_doc[] =
457"complex(real[, imag]) -> complex number\n\
458\n\
459Create a complex number from a real part and an optional imaginary part.\n\
460This is equivalent to (real + imag*1j) where imag defaults to 0.";
461
462
Guido van Rossum8a5c5d21996-01-12 01:09:56 +0000463#endif
464
Guido van Rossum79f25d91997-04-29 20:08:16 +0000465static PyObject *
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000466builtin_dir(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000467 PyObject *self;
468 PyObject *args;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000469{
Guido van Rossum666b17a1997-05-06 16:36:57 +0000470 static char *attrlist[] = {"__members__", "__methods__", NULL};
471 PyObject *v = NULL, *l = NULL, *m = NULL;
472 PyObject *d, *x;
473 int i;
474 char **s;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000475
Guido van Rossum79f25d91997-04-29 20:08:16 +0000476 if (!PyArg_ParseTuple(args, "|O:dir", &v))
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000477 return NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000478 if (v == NULL) {
Guido van Rossum666b17a1997-05-06 16:36:57 +0000479 x = PyEval_GetLocals();
480 if (x == NULL)
481 goto error;
482 l = PyMapping_Keys(x);
483 if (l == NULL)
484 goto error;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000485 }
486 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000487 d = PyObject_GetAttrString(v, "__dict__");
Guido van Rossum666b17a1997-05-06 16:36:57 +0000488 if (d == NULL)
Guido van Rossum795ba581996-05-23 22:49:07 +0000489 PyErr_Clear();
Guido van Rossum666b17a1997-05-06 16:36:57 +0000490 else {
491 l = PyMapping_Keys(d);
492 if (l == NULL)
493 PyErr_Clear();
494 Py_DECREF(d);
495 }
496 if (l == NULL) {
497 l = PyList_New(0);
498 if (l == NULL)
499 goto error;
500 }
501 for (s = attrlist; *s != NULL; s++) {
502 m = PyObject_GetAttrString(v, *s);
503 if (m == NULL) {
504 PyErr_Clear();
505 continue;
506 }
507 for (i = 0; ; i++) {
508 x = PySequence_GetItem(m, i);
509 if (x == NULL) {
510 PyErr_Clear();
511 break;
512 }
513 if (PyList_Append(l, x) != 0) {
514 Py_DECREF(x);
515 Py_DECREF(m);
516 goto error;
517 }
518 Py_DECREF(x);
519 }
520 Py_DECREF(m);
Guido van Rossum795ba581996-05-23 22:49:07 +0000521 }
Guido van Rossum006bcd41991-10-24 14:54:44 +0000522 }
Guido van Rossum666b17a1997-05-06 16:36:57 +0000523 if (PyList_Sort(l) != 0)
524 goto error;
525 return l;
526 error:
527 Py_XDECREF(l);
528 return NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000529}
530
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000531static char dir_doc[] =
532"dir([object]) -> list of strings\n\
533\n\
534Return an alphabetized list of names comprising (some of) the attributes\n\
535of the given object. Without an argument, the names in the current scope\n\
536are listed. With an instance argument, only the instance attributes are\n\
537returned. With a class argument, attributes of the base class are not\n\
538returned. For other types or arguments, this may list members or methods.";
539
540
Guido van Rossum79f25d91997-04-29 20:08:16 +0000541static PyObject *
Guido van Rossum6a00cd81995-01-07 12:39:01 +0000542builtin_divmod(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000543 PyObject *self;
544 PyObject *args;
Guido van Rossum6a00cd81995-01-07 12:39:01 +0000545{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000546 PyObject *v, *w;
Guido van Rossum6a00cd81995-01-07 12:39:01 +0000547
Guido van Rossum79f25d91997-04-29 20:08:16 +0000548 if (!PyArg_ParseTuple(args, "OO:divmod", &v, &w))
Guido van Rossum6a00cd81995-01-07 12:39:01 +0000549 return NULL;
Guido van Rossum09df08a1998-05-22 00:51:39 +0000550 return PyNumber_Divmod(v, w);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000551}
552
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000553static char divmod_doc[] =
554"divmod(x, y) -> (div, mod)\n\
555\n\
556Return the tuple ((x-x%y)/y, x%y). Invariant: div*y + mod == x.";
557
558
Guido van Rossum79f25d91997-04-29 20:08:16 +0000559static PyObject *
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000560builtin_eval(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000561 PyObject *self;
562 PyObject *args;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000563{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000564 PyObject *cmd;
565 PyObject *globals = Py_None, *locals = Py_None;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000566 char *str;
Guido van Rossum590baa41993-11-30 13:40:46 +0000567
Guido van Rossum79f25d91997-04-29 20:08:16 +0000568 if (!PyArg_ParseTuple(args, "O|O!O!:eval",
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000569 &cmd,
Guido van Rossum79f25d91997-04-29 20:08:16 +0000570 &PyDict_Type, &globals,
571 &PyDict_Type, &locals))
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000572 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000573 if (globals == Py_None) {
574 globals = PyEval_GetGlobals();
575 if (locals == Py_None)
576 locals = PyEval_GetLocals();
Guido van Rossum6135a871995-01-09 17:53:26 +0000577 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000578 else if (locals == Py_None)
Guido van Rossum6135a871995-01-09 17:53:26 +0000579 locals = globals;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000580 if (PyDict_GetItemString(globals, "__builtins__") == NULL) {
581 if (PyDict_SetItemString(globals, "__builtins__",
582 PyEval_GetBuiltins()) != 0)
Guido van Rossum6135a871995-01-09 17:53:26 +0000583 return NULL;
584 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000585 if (PyCode_Check(cmd))
586 return PyEval_EvalCode((PyCodeObject *) cmd, globals, locals);
587 if (!PyString_Check(cmd)) {
588 PyErr_SetString(PyExc_TypeError,
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000589 "eval() argument 1 must be string or code object");
Guido van Rossum94390a41992-08-14 15:14:30 +0000590 return NULL;
591 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000592 str = PyString_AsString(cmd);
593 if ((int)strlen(str) != PyString_Size(cmd)) {
594 PyErr_SetString(PyExc_ValueError,
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000595 "embedded '\\0' in string arg");
596 return NULL;
Guido van Rossumf08ab0a1992-03-04 16:41:41 +0000597 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000598 while (*str == ' ' || *str == '\t')
599 str++;
Guido van Rossumb05a5c71997-05-07 17:46:13 +0000600 return PyRun_String(str, Py_eval_input, globals, locals);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000601}
602
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000603static char eval_doc[] =
604"eval(source[, globals[, locals]]) -> value\n\
605\n\
606Evaluate the source in the context of globals and locals.\n\
607The source may be a string representing a Python expression\n\
608or a code object as returned by compile().\n\
609The globals and locals are dictionaries, defaulting to the current\n\
610globals and locals. If only globals is given, locals defaults to it.";
611
612
Guido van Rossum79f25d91997-04-29 20:08:16 +0000613static PyObject *
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000614builtin_execfile(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000615 PyObject *self;
616 PyObject *args;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000617{
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000618 char *filename;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000619 PyObject *globals = Py_None, *locals = Py_None;
620 PyObject *res;
Guido van Rossum0f61f8a1992-02-25 18:55:05 +0000621 FILE* fp;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000622
Guido van Rossum79f25d91997-04-29 20:08:16 +0000623 if (!PyArg_ParseTuple(args, "s|O!O!:execfile",
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000624 &filename,
Guido van Rossum79f25d91997-04-29 20:08:16 +0000625 &PyDict_Type, &globals,
626 &PyDict_Type, &locals))
Guido van Rossum0f61f8a1992-02-25 18:55:05 +0000627 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000628 if (globals == Py_None) {
629 globals = PyEval_GetGlobals();
630 if (locals == Py_None)
631 locals = PyEval_GetLocals();
Guido van Rossum6135a871995-01-09 17:53:26 +0000632 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000633 else if (locals == Py_None)
Guido van Rossum6135a871995-01-09 17:53:26 +0000634 locals = globals;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000635 if (PyDict_GetItemString(globals, "__builtins__") == NULL) {
636 if (PyDict_SetItemString(globals, "__builtins__",
637 PyEval_GetBuiltins()) != 0)
Guido van Rossum6135a871995-01-09 17:53:26 +0000638 return NULL;
639 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000640 Py_BEGIN_ALLOW_THREADS
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000641 fp = fopen(filename, "r");
Guido van Rossum79f25d91997-04-29 20:08:16 +0000642 Py_END_ALLOW_THREADS
Guido van Rossum0f61f8a1992-02-25 18:55:05 +0000643 if (fp == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000644 PyErr_SetFromErrno(PyExc_IOError);
Guido van Rossum0f61f8a1992-02-25 18:55:05 +0000645 return NULL;
646 }
Guido van Rossumb05a5c71997-05-07 17:46:13 +0000647 res = PyRun_File(fp, filename, Py_file_input, globals, locals);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000648 Py_BEGIN_ALLOW_THREADS
Guido van Rossum0f61f8a1992-02-25 18:55:05 +0000649 fclose(fp);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000650 Py_END_ALLOW_THREADS
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000651 return res;
Guido van Rossum0f61f8a1992-02-25 18:55:05 +0000652}
653
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000654static char execfile_doc[] =
655"execfile(filename[, globals[, locals]])\n\
656\n\
657Read and execute a Python script from a file.\n\
658The globals and locals are dictionaries, defaulting to the current\n\
659globals and locals. If only globals is given, locals defaults to it.";
660
661
Guido van Rossum79f25d91997-04-29 20:08:16 +0000662static PyObject *
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000663builtin_float(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000664 PyObject *self;
665 PyObject *args;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000666{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000667 PyObject *v;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000668
Guido van Rossum79f25d91997-04-29 20:08:16 +0000669 if (!PyArg_ParseTuple(args, "O:float", &v))
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000670 return NULL;
Guido van Rossum09df08a1998-05-22 00:51:39 +0000671 return PyNumber_Float(v);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000672}
673
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000674static char float_doc[] =
675"float(x) -> floating point number\n\
676\n\
677Convert a string or number to a floating point number, if possible.";
678
679
Guido van Rossum79f25d91997-04-29 20:08:16 +0000680static PyObject *
Guido van Rossum94390a41992-08-14 15:14:30 +0000681builtin_getattr(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000682 PyObject *self;
683 PyObject *args;
Guido van Rossum33894be1992-01-27 16:53:09 +0000684{
Guido van Rossum950ff291998-06-29 13:38:57 +0000685 PyObject *v, *result, *dflt = NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000686 PyObject *name;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000687
Guido van Rossum950ff291998-06-29 13:38:57 +0000688 if (!PyArg_ParseTuple(args, "OS|O:getattr", &v, &name, &dflt))
Guido van Rossum33894be1992-01-27 16:53:09 +0000689 return NULL;
Guido van Rossum950ff291998-06-29 13:38:57 +0000690 result = PyObject_GetAttr(v, name);
691 if (result == NULL && dflt != NULL) {
692 PyErr_Clear();
693 Py_INCREF(dflt);
694 result = dflt;
695 }
696 return result;
Guido van Rossum9bfef441993-03-29 10:43:31 +0000697}
698
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000699static char getattr_doc[] =
Guido van Rossum950ff291998-06-29 13:38:57 +0000700"getattr(object, name[, default]) -> value\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000701\n\
Guido van Rossum950ff291998-06-29 13:38:57 +0000702Get a named attribute from an object; getattr(x, 'y') is equivalent to x.y.\n\
703When a default argument is given, it is returned when the attribute doesn't\n\
704exist; without it, an exception is raised in that case.";
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000705
706
Guido van Rossum79f25d91997-04-29 20:08:16 +0000707static PyObject *
Guido van Rossum872537c1995-07-07 22:43:42 +0000708builtin_globals(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000709 PyObject *self;
710 PyObject *args;
Guido van Rossum872537c1995-07-07 22:43:42 +0000711{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000712 PyObject *d;
Guido van Rossum872537c1995-07-07 22:43:42 +0000713
Guido van Rossum79f25d91997-04-29 20:08:16 +0000714 if (!PyArg_ParseTuple(args, ""))
Guido van Rossum872537c1995-07-07 22:43:42 +0000715 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000716 d = PyEval_GetGlobals();
717 Py_INCREF(d);
Guido van Rossum872537c1995-07-07 22:43:42 +0000718 return d;
719}
720
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000721static char globals_doc[] =
722"globals() -> dictionary\n\
723\n\
724Return the dictionary containing the current scope's global variables.";
725
726
Guido van Rossum79f25d91997-04-29 20:08:16 +0000727static PyObject *
Guido van Rossum9bfef441993-03-29 10:43:31 +0000728builtin_hasattr(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000729 PyObject *self;
730 PyObject *args;
Guido van Rossum9bfef441993-03-29 10:43:31 +0000731{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000732 PyObject *v;
733 PyObject *name;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000734
Guido van Rossum79f25d91997-04-29 20:08:16 +0000735 if (!PyArg_ParseTuple(args, "OS:hasattr", &v, &name))
Guido van Rossum9bfef441993-03-29 10:43:31 +0000736 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000737 v = PyObject_GetAttr(v, name);
Guido van Rossum9bfef441993-03-29 10:43:31 +0000738 if (v == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000739 PyErr_Clear();
Guido van Rossum09df08a1998-05-22 00:51:39 +0000740 Py_INCREF(Py_False);
741 return Py_False;
Guido van Rossum9bfef441993-03-29 10:43:31 +0000742 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000743 Py_DECREF(v);
Guido van Rossum09df08a1998-05-22 00:51:39 +0000744 Py_INCREF(Py_True);
745 return Py_True;
Guido van Rossum33894be1992-01-27 16:53:09 +0000746}
747
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000748static char hasattr_doc[] =
749"hasattr(object, name) -> Boolean\n\
750\n\
751Return whether the object has an attribute with the given name.\n\
752(This is done by calling getattr(object, name) and catching exceptions.)";
753
754
Guido van Rossum79f25d91997-04-29 20:08:16 +0000755static PyObject *
Guido van Rossum5b722181993-03-30 17:46:03 +0000756builtin_id(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000757 PyObject *self;
758 PyObject *args;
Guido van Rossum5b722181993-03-30 17:46:03 +0000759{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000760 PyObject *v;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000761
Guido van Rossum79f25d91997-04-29 20:08:16 +0000762 if (!PyArg_ParseTuple(args, "O:id", &v))
Guido van Rossum5b722181993-03-30 17:46:03 +0000763 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000764 return PyInt_FromLong((long)v);
Guido van Rossum5b722181993-03-30 17:46:03 +0000765}
766
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000767static char id_doc[] =
768"id(object) -> integer\n\
769\n\
770Return the identity of an object. This is guaranteed to be unique among\n\
771simultaneously existing objects. (Hint: it's the object's memory address.)";
772
773
Guido van Rossum79f25d91997-04-29 20:08:16 +0000774static PyObject *
Guido van Rossum12d12c51993-10-26 17:58:25 +0000775builtin_map(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000776 PyObject *self;
777 PyObject *args;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000778{
779 typedef struct {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000780 PyObject *seq;
781 PySequenceMethods *sqf;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000782 int len;
783 } sequence;
784
Guido van Rossum79f25d91997-04-29 20:08:16 +0000785 PyObject *func, *result;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000786 sequence *seqs = NULL, *sqp;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000787 int n, len;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000788 register int i, j;
789
Guido van Rossum79f25d91997-04-29 20:08:16 +0000790 n = PyTuple_Size(args);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000791 if (n < 2) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000792 PyErr_SetString(PyExc_TypeError,
793 "map() requires at least two args");
Guido van Rossum12d12c51993-10-26 17:58:25 +0000794 return NULL;
795 }
796
Guido van Rossum79f25d91997-04-29 20:08:16 +0000797 func = PyTuple_GetItem(args, 0);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000798 n--;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000799
Guido van Rossumfa4ac711998-07-10 17:37:30 +0000800 if (func == Py_None && n == 1) {
801 /* map(None, S) is the same as list(S). */
802 return PySequence_List(PyTuple_GetItem(args, 1));
803 }
804
Guido van Rossum79f25d91997-04-29 20:08:16 +0000805 if ((seqs = PyMem_NEW(sequence, n)) == NULL) {
806 PyErr_NoMemory();
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000807 goto Fail_2;
808 }
Guido van Rossum12d12c51993-10-26 17:58:25 +0000809
Guido van Rossum2d951851994-08-29 12:52:16 +0000810 for (len = 0, i = 0, sqp = seqs; i < n; ++i, ++sqp) {
Guido van Rossum12d12c51993-10-26 17:58:25 +0000811 int curlen;
Guido van Rossum09df08a1998-05-22 00:51:39 +0000812 PySequenceMethods *sqf;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000813
Guido van Rossum79f25d91997-04-29 20:08:16 +0000814 if ((sqp->seq = PyTuple_GetItem(args, i + 1)) == NULL)
Guido van Rossum12d12c51993-10-26 17:58:25 +0000815 goto Fail_2;
816
Guido van Rossum09df08a1998-05-22 00:51:39 +0000817 sqp->sqf = sqf = sqp->seq->ob_type->tp_as_sequence;
818 if (sqf == NULL ||
819 sqf->sq_length == NULL ||
820 sqf->sq_item == NULL)
821 {
Guido van Rossum12d12c51993-10-26 17:58:25 +0000822 static char errmsg[] =
823 "argument %d to map() must be a sequence object";
Guido van Rossum15e33a41997-04-30 19:00:27 +0000824 char errbuf[sizeof(errmsg) + 25];
Guido van Rossum12d12c51993-10-26 17:58:25 +0000825
826 sprintf(errbuf, errmsg, i+2);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000827 PyErr_SetString(PyExc_TypeError, errbuf);
Guido van Rossum12d12c51993-10-26 17:58:25 +0000828 goto Fail_2;
829 }
830
831 if ((curlen = sqp->len = (*sqp->sqf->sq_length)(sqp->seq)) < 0)
832 goto Fail_2;
833
834 if (curlen > len)
835 len = curlen;
836 }
837
Guido van Rossum79f25d91997-04-29 20:08:16 +0000838 if ((result = (PyObject *) PyList_New(len)) == NULL)
Guido van Rossum12d12c51993-10-26 17:58:25 +0000839 goto Fail_2;
840
Guido van Rossum2d951851994-08-29 12:52:16 +0000841 for (i = 0; ; ++i) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000842 PyObject *alist, *item=NULL, *value;
Guido van Rossum2d951851994-08-29 12:52:16 +0000843 int any = 0;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000844
Guido van Rossum79f25d91997-04-29 20:08:16 +0000845 if (func == Py_None && n == 1)
Guido van Rossum32120311995-07-10 13:52:21 +0000846 alist = NULL;
Guido van Rossum2d951851994-08-29 12:52:16 +0000847 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000848 if ((alist = PyTuple_New(n)) == NULL)
Guido van Rossum2d951851994-08-29 12:52:16 +0000849 goto Fail_1;
850 }
Guido van Rossum12d12c51993-10-26 17:58:25 +0000851
852 for (j = 0, sqp = seqs; j < n; ++j, ++sqp) {
Guido van Rossum2d951851994-08-29 12:52:16 +0000853 if (sqp->len < 0) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000854 Py_INCREF(Py_None);
855 item = Py_None;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000856 }
Guido van Rossum12d12c51993-10-26 17:58:25 +0000857 else {
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000858 item = (*sqp->sqf->sq_item)(sqp->seq, i);
Guido van Rossum2d951851994-08-29 12:52:16 +0000859 if (item == NULL) {
Barry Warsawcde8b1b1997-08-22 21:14:38 +0000860 if (PyErr_ExceptionMatches(
861 PyExc_IndexError))
862 {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000863 PyErr_Clear();
864 Py_INCREF(Py_None);
865 item = Py_None;
Guido van Rossum2d951851994-08-29 12:52:16 +0000866 sqp->len = -1;
867 }
868 else {
869 goto Fail_0;
870 }
871 }
872 else
873 any = 1;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000874
Guido van Rossum12d12c51993-10-26 17:58:25 +0000875 }
Guido van Rossum32120311995-07-10 13:52:21 +0000876 if (!alist)
Guido van Rossum2d951851994-08-29 12:52:16 +0000877 break;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000878 if (PyTuple_SetItem(alist, j, item) < 0) {
879 Py_DECREF(item);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000880 goto Fail_0;
Guido van Rossum2d951851994-08-29 12:52:16 +0000881 }
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000882 continue;
883
884 Fail_0:
Guido van Rossum79f25d91997-04-29 20:08:16 +0000885 Py_XDECREF(alist);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000886 goto Fail_1;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000887 }
888
Guido van Rossum32120311995-07-10 13:52:21 +0000889 if (!alist)
890 alist = item;
Guido van Rossum2d951851994-08-29 12:52:16 +0000891
892 if (!any) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000893 Py_DECREF(alist);
Guido van Rossum2d951851994-08-29 12:52:16 +0000894 break;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000895 }
Guido van Rossum2d951851994-08-29 12:52:16 +0000896
Guido van Rossum79f25d91997-04-29 20:08:16 +0000897 if (func == Py_None)
Guido van Rossum32120311995-07-10 13:52:21 +0000898 value = alist;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000899 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000900 value = PyEval_CallObject(func, alist);
901 Py_DECREF(alist);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000902 if (value == NULL)
903 goto Fail_1;
Guido van Rossum2d951851994-08-29 12:52:16 +0000904 }
905 if (i >= len) {
Barry Warsawfa77e091999-01-28 18:49:12 +0000906 int status = PyList_Append(result, value);
Barry Warsaw21332871999-01-28 04:21:35 +0000907 Py_DECREF(value);
Barry Warsawfa77e091999-01-28 18:49:12 +0000908 if (status < 0)
909 goto Fail_1;
Guido van Rossum2d951851994-08-29 12:52:16 +0000910 }
911 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000912 if (PyList_SetItem(result, i, value) < 0)
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000913 goto Fail_1;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000914 }
915 }
916
Guido van Rossumfa4ac711998-07-10 17:37:30 +0000917 if (i < len && PyList_SetSlice(result, i, len, NULL) < 0)
918 goto Fail_1;
919
Guido van Rossum79f25d91997-04-29 20:08:16 +0000920 PyMem_DEL(seqs);
Guido van Rossum12d12c51993-10-26 17:58:25 +0000921 return result;
922
Guido van Rossum12d12c51993-10-26 17:58:25 +0000923Fail_1:
Guido van Rossum79f25d91997-04-29 20:08:16 +0000924 Py_DECREF(result);
Guido van Rossum12d12c51993-10-26 17:58:25 +0000925Fail_2:
Guido van Rossum79f25d91997-04-29 20:08:16 +0000926 if (seqs) PyMem_DEL(seqs);
Guido van Rossum12d12c51993-10-26 17:58:25 +0000927 return NULL;
928}
929
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000930static char map_doc[] =
931"map(function, sequence[, sequence, ...]) -> list\n\
932\n\
933Return a list of the results of applying the function to the items of\n\
934the argument sequence(s). If more than one sequence is given, the\n\
935function is called with an argument list consisting of the corresponding\n\
936item of each sequence, substituting None for missing values when not all\n\
937sequences have the same length. If the function is None, return a list of\n\
938the items of the sequence (or a list of tuples if more than one sequence).";
939
940
Guido van Rossum79f25d91997-04-29 20:08:16 +0000941static PyObject *
Guido van Rossum94390a41992-08-14 15:14:30 +0000942builtin_setattr(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000943 PyObject *self;
944 PyObject *args;
Guido van Rossum33894be1992-01-27 16:53:09 +0000945{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000946 PyObject *v;
947 PyObject *name;
948 PyObject *value;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000949
Guido van Rossum79f25d91997-04-29 20:08:16 +0000950 if (!PyArg_ParseTuple(args, "OSO:setattr", &v, &name, &value))
Guido van Rossum33894be1992-01-27 16:53:09 +0000951 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000952 if (PyObject_SetAttr(v, name, value) != 0)
Guido van Rossum33894be1992-01-27 16:53:09 +0000953 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000954 Py_INCREF(Py_None);
955 return Py_None;
Guido van Rossum33894be1992-01-27 16:53:09 +0000956}
957
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000958static char setattr_doc[] =
959"setattr(object, name, value)\n\
960\n\
961Set a named attribute on an object; setattr(x, 'y', v) is equivalent to\n\
962``x.y = v''.";
963
964
Guido van Rossum79f25d91997-04-29 20:08:16 +0000965static PyObject *
Guido van Rossum14144fc1994-08-29 12:53:40 +0000966builtin_delattr(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000967 PyObject *self;
968 PyObject *args;
Guido van Rossum14144fc1994-08-29 12:53:40 +0000969{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000970 PyObject *v;
971 PyObject *name;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000972
Guido van Rossum79f25d91997-04-29 20:08:16 +0000973 if (!PyArg_ParseTuple(args, "OS:delattr", &v, &name))
Guido van Rossum14144fc1994-08-29 12:53:40 +0000974 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000975 if (PyObject_SetAttr(v, name, (PyObject *)NULL) != 0)
Guido van Rossum14144fc1994-08-29 12:53:40 +0000976 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000977 Py_INCREF(Py_None);
978 return Py_None;
Guido van Rossum14144fc1994-08-29 12:53:40 +0000979}
980
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000981static char delattr_doc[] =
Guido van Rossumdf12a591998-11-23 22:13:04 +0000982"delattr(object, name)\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000983\n\
984Delete a named attribute on an object; delattr(x, 'y') is equivalent to\n\
985``del x.y''.";
986
987
Guido van Rossum79f25d91997-04-29 20:08:16 +0000988static PyObject *
Guido van Rossum9bfef441993-03-29 10:43:31 +0000989builtin_hash(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000990 PyObject *self;
991 PyObject *args;
Guido van Rossum9bfef441993-03-29 10:43:31 +0000992{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000993 PyObject *v;
Guido van Rossum9bfef441993-03-29 10:43:31 +0000994 long x;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000995
Guido van Rossum79f25d91997-04-29 20:08:16 +0000996 if (!PyArg_ParseTuple(args, "O:hash", &v))
Guido van Rossum9bfef441993-03-29 10:43:31 +0000997 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000998 x = PyObject_Hash(v);
Guido van Rossum9bfef441993-03-29 10:43:31 +0000999 if (x == -1)
1000 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001001 return PyInt_FromLong(x);
Guido van Rossum9bfef441993-03-29 10:43:31 +00001002}
1003
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001004static char hash_doc[] =
1005"hash(object) -> integer\n\
1006\n\
1007Return a hash value for the object. Two objects with the same value have\n\
1008the same hash value. The reverse is not necessarily true, but likely.";
1009
1010
Guido van Rossum79f25d91997-04-29 20:08:16 +00001011static PyObject *
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001012builtin_hex(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001013 PyObject *self;
1014 PyObject *args;
Guido van Rossum006bcd41991-10-24 14:54:44 +00001015{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001016 PyObject *v;
1017 PyNumberMethods *nb;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001018
Guido van Rossum79f25d91997-04-29 20:08:16 +00001019 if (!PyArg_ParseTuple(args, "O:hex", &v))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001020 return NULL;
Guido van Rossum1899c2e1992-09-12 11:09:23 +00001021
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001022 if ((nb = v->ob_type->tp_as_number) == NULL ||
Guido van Rossum1899c2e1992-09-12 11:09:23 +00001023 nb->nb_hex == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001024 PyErr_SetString(PyExc_TypeError,
Guido van Rossum1899c2e1992-09-12 11:09:23 +00001025 "hex() argument can't be converted to hex");
1026 return NULL;
Guido van Rossum006bcd41991-10-24 14:54:44 +00001027 }
Guido van Rossum1899c2e1992-09-12 11:09:23 +00001028 return (*nb->nb_hex)(v);
Guido van Rossum006bcd41991-10-24 14:54:44 +00001029}
1030
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001031static char hex_doc[] =
1032"hex(number) -> string\n\
1033\n\
1034Return the hexadecimal representation of an integer or long integer.";
1035
1036
Guido van Rossum79f25d91997-04-29 20:08:16 +00001037static PyObject *builtin_raw_input Py_PROTO((PyObject *, PyObject *));
Guido van Rossum3165fe61992-09-25 21:59:05 +00001038
Guido van Rossum79f25d91997-04-29 20:08:16 +00001039static PyObject *
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001040builtin_input(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001041 PyObject *self;
1042 PyObject *args;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001043{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001044 PyObject *line;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001045 char *str;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001046 PyObject *res;
1047 PyObject *globals, *locals;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001048
1049 line = builtin_raw_input(self, args);
Guido van Rossum3165fe61992-09-25 21:59:05 +00001050 if (line == NULL)
1051 return line;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001052 if (!PyArg_Parse(line, "s;embedded '\\0' in input line", &str))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001053 return NULL;
1054 while (*str == ' ' || *str == '\t')
1055 str++;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001056 globals = PyEval_GetGlobals();
1057 locals = PyEval_GetLocals();
1058 if (PyDict_GetItemString(globals, "__builtins__") == NULL) {
1059 if (PyDict_SetItemString(globals, "__builtins__",
1060 PyEval_GetBuiltins()) != 0)
Guido van Rossum6135a871995-01-09 17:53:26 +00001061 return NULL;
1062 }
Guido van Rossumb05a5c71997-05-07 17:46:13 +00001063 res = PyRun_String(str, Py_eval_input, globals, locals);
Guido van Rossum79f25d91997-04-29 20:08:16 +00001064 Py_DECREF(line);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001065 return res;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001066}
1067
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001068static char input_doc[] =
1069"input([prompt]) -> value\n\
1070\n\
1071Equivalent to eval(raw_input(prompt)).";
1072
1073
Guido van Rossume8811f81997-02-14 15:48:05 +00001074static PyObject *
1075builtin_intern(self, args)
1076 PyObject *self;
1077 PyObject *args;
1078{
1079 PyObject *s;
1080 if (!PyArg_ParseTuple(args, "S", &s))
1081 return NULL;
1082 Py_INCREF(s);
1083 PyString_InternInPlace(&s);
1084 return s;
1085}
1086
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001087static char intern_doc[] =
1088"intern(string) -> string\n\
1089\n\
1090``Intern'' the given string. This enters the string in the (global)\n\
1091table of interned strings whose purpose is to speed up dictionary lookups.\n\
1092Return the string itself or the previously interned string object with the\n\
1093same value.";
1094
1095
Guido van Rossum79f25d91997-04-29 20:08:16 +00001096static PyObject *
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001097builtin_int(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001098 PyObject *self;
1099 PyObject *args;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001100{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001101 PyObject *v;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001102
Guido van Rossum79f25d91997-04-29 20:08:16 +00001103 if (!PyArg_ParseTuple(args, "O:int", &v))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001104 return NULL;
Guido van Rossum09df08a1998-05-22 00:51:39 +00001105 return PyNumber_Int(v);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001106}
1107
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001108static char int_doc[] =
1109"int(x) -> integer\n\
1110\n\
1111Convert a string or number to an integer, if possible.\n\
1112A floating point argument will be truncated towards zero.";
1113
1114
Guido van Rossum79f25d91997-04-29 20:08:16 +00001115static PyObject *
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001116builtin_len(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001117 PyObject *self;
1118 PyObject *args;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001119{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001120 PyObject *v;
Guido van Rossum8ea9f4d1998-06-29 22:26:50 +00001121 long res;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001122
Guido van Rossum79f25d91997-04-29 20:08:16 +00001123 if (!PyArg_ParseTuple(args, "O:len", &v))
Guido van Rossum3f5da241990-12-20 15:06:42 +00001124 return NULL;
Guido van Rossum8ea9f4d1998-06-29 22:26:50 +00001125 res = PyObject_Length(v);
1126 if (res < 0 && PyErr_Occurred())
1127 return NULL;
1128 return PyInt_FromLong(res);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001129}
1130
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001131static char len_doc[] =
1132"len(object) -> integer\n\
1133\n\
1134Return the number of items of a sequence or mapping.";
1135
1136
Guido van Rossum79f25d91997-04-29 20:08:16 +00001137static PyObject *
Guido van Rossumd1705771996-04-09 02:41:06 +00001138builtin_list(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001139 PyObject *self;
1140 PyObject *args;
Guido van Rossumd1705771996-04-09 02:41:06 +00001141{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001142 PyObject *v;
Guido van Rossumd1705771996-04-09 02:41:06 +00001143
Guido van Rossum79f25d91997-04-29 20:08:16 +00001144 if (!PyArg_ParseTuple(args, "O:list", &v))
Guido van Rossumd1705771996-04-09 02:41:06 +00001145 return NULL;
Guido van Rossum09df08a1998-05-22 00:51:39 +00001146 return PySequence_List(v);
Guido van Rossumd1705771996-04-09 02:41:06 +00001147}
1148
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001149static char list_doc[] =
1150"list(sequence) -> list\n\
1151\n\
1152Return a new list whose items are the same as those of the argument sequence.";
1153
Guido van Rossum8861b741996-07-30 16:49:37 +00001154
1155static PyObject *
1156builtin_slice(self, args)
1157 PyObject *self;
1158 PyObject *args;
1159{
Guido van Rossum09df08a1998-05-22 00:51:39 +00001160 PyObject *start, *stop, *step;
Guido van Rossum8861b741996-07-30 16:49:37 +00001161
Guido van Rossum09df08a1998-05-22 00:51:39 +00001162 start = stop = step = NULL;
Guido van Rossum8861b741996-07-30 16:49:37 +00001163
Guido van Rossum09df08a1998-05-22 00:51:39 +00001164 if (!PyArg_ParseTuple(args, "O|OO:slice", &start, &stop, &step))
1165 return NULL;
Guido van Rossum8861b741996-07-30 16:49:37 +00001166
Guido van Rossum09df08a1998-05-22 00:51:39 +00001167 /* This swapping of stop and start is to maintain similarity with
1168 range(). */
1169 if (stop == NULL) {
1170 stop = start;
1171 start = NULL;
1172 }
1173 return PySlice_New(start, stop, step);
Guido van Rossum8861b741996-07-30 16:49:37 +00001174}
1175
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001176static char slice_doc[] =
1177"slice([start,] step[, stop]) -> slice object\n\
1178\n\
1179Create a slice object. This is used for slicing by the Numeric extensions.";
1180
1181
Guido van Rossum79f25d91997-04-29 20:08:16 +00001182static PyObject *
Guido van Rossum872537c1995-07-07 22:43:42 +00001183builtin_locals(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001184 PyObject *self;
1185 PyObject *args;
Guido van Rossum872537c1995-07-07 22:43:42 +00001186{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001187 PyObject *d;
Guido van Rossum872537c1995-07-07 22:43:42 +00001188
Guido van Rossum79f25d91997-04-29 20:08:16 +00001189 if (!PyArg_ParseTuple(args, ""))
Guido van Rossum872537c1995-07-07 22:43:42 +00001190 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001191 d = PyEval_GetLocals();
1192 Py_INCREF(d);
Guido van Rossum872537c1995-07-07 22:43:42 +00001193 return d;
1194}
1195
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001196static char locals_doc[] =
1197"locals() -> dictionary\n\
1198\n\
1199Return the dictionary containing the current scope's local variables.";
1200
1201
Guido van Rossum79f25d91997-04-29 20:08:16 +00001202static PyObject *
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001203builtin_long(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001204 PyObject *self;
1205 PyObject *args;
Guido van Rossumd4905451991-05-05 20:00:36 +00001206{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001207 PyObject *v;
Guido van Rossum1899c2e1992-09-12 11:09:23 +00001208
Guido van Rossum79f25d91997-04-29 20:08:16 +00001209 if (!PyArg_ParseTuple(args, "O:long", &v))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001210 return NULL;
Guido van Rossum09df08a1998-05-22 00:51:39 +00001211 return PyNumber_Long(v);
Guido van Rossumd4905451991-05-05 20:00:36 +00001212}
1213
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001214static char long_doc[] =
1215"long(x) -> long integer\n\
1216\n\
1217Convert a string or number to a long integer, if possible.\n\
1218A floating point argument will be truncated towards zero.";
1219
1220
Guido van Rossum79f25d91997-04-29 20:08:16 +00001221static PyObject *
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001222min_max(args, sign)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001223 PyObject *args;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001224 int sign;
1225{
Guido van Rossum2d951851994-08-29 12:52:16 +00001226 int i;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001227 PyObject *v, *w, *x;
1228 PySequenceMethods *sq;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001229
Guido van Rossum79f25d91997-04-29 20:08:16 +00001230 if (PyTuple_Size(args) > 1)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001231 v = args;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001232 else if (!PyArg_ParseTuple(args, "O:min/max", &v))
Guido van Rossum3f5da241990-12-20 15:06:42 +00001233 return NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001234 sq = v->ob_type->tp_as_sequence;
Guido van Rossum09df08a1998-05-22 00:51:39 +00001235 if (sq == NULL || sq->sq_item == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001236 PyErr_SetString(PyExc_TypeError,
1237 "min() or max() of non-sequence");
Guido van Rossum3f5da241990-12-20 15:06:42 +00001238 return NULL;
1239 }
Guido van Rossum2d951851994-08-29 12:52:16 +00001240 w = NULL;
1241 for (i = 0; ; i++) {
Guido van Rossum3f5da241990-12-20 15:06:42 +00001242 x = (*sq->sq_item)(v, i); /* Implies INCREF */
Guido van Rossum2d951851994-08-29 12:52:16 +00001243 if (x == NULL) {
Barry Warsawcde8b1b1997-08-22 21:14:38 +00001244 if (PyErr_ExceptionMatches(PyExc_IndexError)) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001245 PyErr_Clear();
Guido van Rossum2d951851994-08-29 12:52:16 +00001246 break;
1247 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00001248 Py_XDECREF(w);
Guido van Rossum2d951851994-08-29 12:52:16 +00001249 return NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001250 }
Guido van Rossum2d951851994-08-29 12:52:16 +00001251 if (w == NULL)
1252 w = x;
1253 else {
Guido van Rossumc8b6df91997-05-23 00:06:51 +00001254 int c = PyObject_Compare(x, w);
1255 if (c && PyErr_Occurred()) {
1256 Py_DECREF(x);
1257 Py_XDECREF(w);
1258 return NULL;
1259 }
1260 if (c * sign > 0) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001261 Py_DECREF(w);
Guido van Rossum2d951851994-08-29 12:52:16 +00001262 w = x;
1263 }
1264 else
Guido van Rossum79f25d91997-04-29 20:08:16 +00001265 Py_DECREF(x);
Guido van Rossum2d951851994-08-29 12:52:16 +00001266 }
Guido van Rossum3f5da241990-12-20 15:06:42 +00001267 }
Guido van Rossum2d951851994-08-29 12:52:16 +00001268 if (w == NULL)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001269 PyErr_SetString(PyExc_ValueError,
1270 "min() or max() of empty sequence");
Guido van Rossum3f5da241990-12-20 15:06:42 +00001271 return w;
1272}
1273
Guido van Rossum79f25d91997-04-29 20:08:16 +00001274static PyObject *
Guido van Rossum3f5da241990-12-20 15:06:42 +00001275builtin_min(self, v)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001276 PyObject *self;
1277 PyObject *v;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001278{
1279 return min_max(v, -1);
1280}
1281
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001282static char min_doc[] =
1283"min(sequence) -> value\n\
1284min(a, b, c, ...) -> value\n\
1285\n\
1286With a single sequence argument, return its smallest item.\n\
1287With two or more arguments, return the smallest argument.";
1288
1289
Guido van Rossum79f25d91997-04-29 20:08:16 +00001290static PyObject *
Guido van Rossum3f5da241990-12-20 15:06:42 +00001291builtin_max(self, v)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001292 PyObject *self;
1293 PyObject *v;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001294{
1295 return min_max(v, 1);
1296}
1297
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001298static char max_doc[] =
1299"max(sequence) -> value\n\
1300max(a, b, c, ...) -> value\n\
1301\n\
1302With a single sequence argument, return its largest item.\n\
1303With two or more arguments, return the largest argument.";
1304
1305
Guido van Rossum79f25d91997-04-29 20:08:16 +00001306static PyObject *
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001307builtin_oct(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001308 PyObject *self;
1309 PyObject *args;
Guido van Rossum006bcd41991-10-24 14:54:44 +00001310{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001311 PyObject *v;
1312 PyNumberMethods *nb;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001313
Guido van Rossum79f25d91997-04-29 20:08:16 +00001314 if (!PyArg_ParseTuple(args, "O:oct", &v))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001315 return NULL;
Guido van Rossum1899c2e1992-09-12 11:09:23 +00001316 if (v == NULL || (nb = v->ob_type->tp_as_number) == NULL ||
1317 nb->nb_oct == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001318 PyErr_SetString(PyExc_TypeError,
Guido van Rossum1899c2e1992-09-12 11:09:23 +00001319 "oct() argument can't be converted to oct");
1320 return NULL;
Guido van Rossum006bcd41991-10-24 14:54:44 +00001321 }
Guido van Rossum1899c2e1992-09-12 11:09:23 +00001322 return (*nb->nb_oct)(v);
Guido van Rossum006bcd41991-10-24 14:54:44 +00001323}
1324
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001325static char oct_doc[] =
1326"oct(number) -> string\n\
1327\n\
1328Return the octal representation of an integer or long integer.";
1329
1330
Guido van Rossum79f25d91997-04-29 20:08:16 +00001331static PyObject *
Guido van Rossum94390a41992-08-14 15:14:30 +00001332builtin_open(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001333 PyObject *self;
1334 PyObject *args;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001335{
Guido van Rossum2d951851994-08-29 12:52:16 +00001336 char *name;
1337 char *mode = "r";
1338 int bufsize = -1;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001339 PyObject *f;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001340
Guido van Rossum79f25d91997-04-29 20:08:16 +00001341 if (!PyArg_ParseTuple(args, "s|si:open", &name, &mode, &bufsize))
Guido van Rossum3f5da241990-12-20 15:06:42 +00001342 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001343 f = PyFile_FromString(name, mode);
Guido van Rossum2d951851994-08-29 12:52:16 +00001344 if (f != NULL)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001345 PyFile_SetBufSize(f, bufsize);
Guido van Rossum2d951851994-08-29 12:52:16 +00001346 return f;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001347}
1348
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001349static char open_doc[] =
1350"open(filename[, mode[, buffering]]) -> file object\n\
1351\n\
1352Open a file. The mode can be 'r', 'w' or 'a' for reading (default),\n\
1353writing or appending. The file will be created if it doesn't exist\n\
1354when opened for writing or appending; it will be truncated when\n\
1355opened for writing. Add a 'b' to the mode for binary files.\n\
1356Add a '+' to the mode to allow simultaneous reading and writing.\n\
1357If the buffering argument is given, 0 means unbuffered, 1 means line\n\
1358buffered, and larger numbers specify the buffer size.";
1359
1360
Guido van Rossum79f25d91997-04-29 20:08:16 +00001361static PyObject *
Guido van Rossum94390a41992-08-14 15:14:30 +00001362builtin_ord(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001363 PyObject *self;
1364 PyObject *args;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001365{
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001366 char c;
1367
Guido van Rossum79f25d91997-04-29 20:08:16 +00001368 if (!PyArg_ParseTuple(args, "c:ord", &c))
Guido van Rossum3f5da241990-12-20 15:06:42 +00001369 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001370 return PyInt_FromLong((long)(c & 0xff));
Guido van Rossum3f5da241990-12-20 15:06:42 +00001371}
1372
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001373static char ord_doc[] =
1374"ord(c) -> integer\n\
1375\n\
1376Return the integer ordinal of a one character string.";
1377
1378
Guido van Rossum79f25d91997-04-29 20:08:16 +00001379static PyObject *
Guido van Rossum6a00cd81995-01-07 12:39:01 +00001380builtin_pow(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001381 PyObject *self;
1382 PyObject *args;
Guido van Rossum6a00cd81995-01-07 12:39:01 +00001383{
Guido van Rossum09df08a1998-05-22 00:51:39 +00001384 PyObject *v, *w, *z = Py_None;
Guido van Rossum6a00cd81995-01-07 12:39:01 +00001385
Guido van Rossum79f25d91997-04-29 20:08:16 +00001386 if (!PyArg_ParseTuple(args, "OO|O:pow", &v, &w, &z))
Guido van Rossum6a00cd81995-01-07 12:39:01 +00001387 return NULL;
Guido van Rossum09df08a1998-05-22 00:51:39 +00001388 return PyNumber_Power(v, w, z);
Guido van Rossumd4905451991-05-05 20:00:36 +00001389}
1390
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001391static char pow_doc[] =
1392"pow(x, y[, z]) -> number\n\
1393\n\
1394With two arguments, equivalent to x**y. With three arguments,\n\
1395equivalent to (x**y) % z, but may be more efficient (e.g. for longs).";
1396
1397
Guido van Rossum124eff01999-02-23 16:11:01 +00001398/* Return number of items in range/xrange (lo, hi, step). step > 0
1399 * required. Return a value < 0 if & only if the true value is too
1400 * large to fit in a signed long.
1401 */
1402static long
1403get_len_of_range(lo, hi, step)
1404 long lo;
1405 long hi;
1406 long step; /* must be > 0 */
1407{
1408 /* -------------------------------------------------------------
1409 If lo >= hi, the range is empty.
1410 Else if n values are in the range, the last one is
1411 lo + (n-1)*step, which must be <= hi-1. Rearranging,
1412 n <= (hi - lo - 1)/step + 1, so taking the floor of the RHS gives
1413 the proper value. Since lo < hi in this case, hi-lo-1 >= 0, so
1414 the RHS is non-negative and so truncation is the same as the
1415 floor. Letting M be the largest positive long, the worst case
1416 for the RHS numerator is hi=M, lo=-M-1, and then
1417 hi-lo-1 = M-(-M-1)-1 = 2*M. Therefore unsigned long has enough
1418 precision to compute the RHS exactly.
1419 ---------------------------------------------------------------*/
1420 long n = 0;
1421 if (lo < hi) {
1422 unsigned long uhi = (unsigned long)hi;
1423 unsigned long ulo = (unsigned long)lo;
1424 unsigned long diff = uhi - ulo - 1;
1425 n = (long)(diff / (unsigned long)step + 1);
1426 }
1427 return n;
1428}
1429
Guido van Rossum79f25d91997-04-29 20:08:16 +00001430static PyObject *
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001431builtin_range(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001432 PyObject *self;
1433 PyObject *args;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001434{
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001435 long ilow = 0, ihigh = 0, istep = 1;
Guido van Rossum124eff01999-02-23 16:11:01 +00001436 long bign;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001437 int i, n;
Guido van Rossum124eff01999-02-23 16:11:01 +00001438
Guido van Rossum79f25d91997-04-29 20:08:16 +00001439 PyObject *v;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001440
Guido van Rossum79f25d91997-04-29 20:08:16 +00001441 if (PyTuple_Size(args) <= 1) {
1442 if (!PyArg_ParseTuple(args,
Guido van Rossum0865dd91995-01-17 16:30:22 +00001443 "l;range() requires 1-3 int arguments",
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001444 &ihigh))
1445 return NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001446 }
1447 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001448 if (!PyArg_ParseTuple(args,
Guido van Rossum0865dd91995-01-17 16:30:22 +00001449 "ll|l;range() requires 1-3 int arguments",
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001450 &ilow, &ihigh, &istep))
Guido van Rossum3f5da241990-12-20 15:06:42 +00001451 return NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001452 }
1453 if (istep == 0) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001454 PyErr_SetString(PyExc_ValueError, "zero step for range()");
Guido van Rossum3f5da241990-12-20 15:06:42 +00001455 return NULL;
1456 }
Guido van Rossum124eff01999-02-23 16:11:01 +00001457 if (istep > 0)
1458 bign = get_len_of_range(ilow, ihigh, istep);
1459 else
1460 bign = get_len_of_range(ihigh, ilow, -istep);
1461 n = (int)bign;
1462 if (bign < 0 || (long)n != bign) {
Guido van Rossume23cde21999-01-12 05:07:47 +00001463 PyErr_SetString(PyExc_OverflowError,
Guido van Rossum124eff01999-02-23 16:11:01 +00001464 "range() has too many items");
Guido van Rossume23cde21999-01-12 05:07:47 +00001465 return NULL;
1466 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00001467 v = PyList_New(n);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001468 if (v == NULL)
1469 return NULL;
1470 for (i = 0; i < n; i++) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001471 PyObject *w = PyInt_FromLong(ilow);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001472 if (w == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001473 Py_DECREF(v);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001474 return NULL;
1475 }
Guido van Rossuma937d141998-04-24 18:22:02 +00001476 PyList_SET_ITEM(v, i, w);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001477 ilow += istep;
1478 }
1479 return v;
1480}
1481
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001482static char range_doc[] =
1483"range([start,] stop[, step]) -> list of integers\n\
1484\n\
1485Return a list containing an arithmetic progression of integers.\n\
1486range(i, j) returns [i, i+1, i+2, ..., j-1]; start (!) defaults to 0.\n\
1487When step is given, it specifies the increment (or decrement).\n\
1488For example, range(4) returns [0, 1, 2, 3]. The end point is omitted!\n\
1489These are exactly the valid indices for a list of 4 elements.";
1490
1491
Guido van Rossum79f25d91997-04-29 20:08:16 +00001492static PyObject *
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001493builtin_xrange(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001494 PyObject *self;
1495 PyObject *args;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001496{
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001497 long ilow = 0, ihigh = 0, istep = 1;
Guido van Rossum0865dd91995-01-17 16:30:22 +00001498 long n;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001499
Guido van Rossum79f25d91997-04-29 20:08:16 +00001500 if (PyTuple_Size(args) <= 1) {
1501 if (!PyArg_ParseTuple(args,
Guido van Rossum0865dd91995-01-17 16:30:22 +00001502 "l;xrange() requires 1-3 int arguments",
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001503 &ihigh))
1504 return NULL;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001505 }
1506 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001507 if (!PyArg_ParseTuple(args,
Guido van Rossum0865dd91995-01-17 16:30:22 +00001508 "ll|l;xrange() requires 1-3 int arguments",
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001509 &ilow, &ihigh, &istep))
Guido van Rossum12d12c51993-10-26 17:58:25 +00001510 return NULL;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001511 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001512 if (istep == 0) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001513 PyErr_SetString(PyExc_ValueError, "zero step for xrange()");
Guido van Rossum12d12c51993-10-26 17:58:25 +00001514 return NULL;
1515 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001516 if (istep > 0)
Guido van Rossum124eff01999-02-23 16:11:01 +00001517 n = get_len_of_range(ilow, ihigh, istep);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001518 else
Guido van Rossum124eff01999-02-23 16:11:01 +00001519 n = get_len_of_range(ihigh, ilow, -istep);
1520 if (n < 0) {
1521 PyErr_SetString(PyExc_OverflowError,
1522 "xrange() has more than sys.maxint items");
1523 return NULL;
1524 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00001525 return PyRange_New(ilow, n, istep, 1);
Guido van Rossum12d12c51993-10-26 17:58:25 +00001526}
1527
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001528static char xrange_doc[] =
1529"xrange([start,] stop[, step]) -> xrange object\n\
1530\n\
1531Like range(), but instead of returning a list, returns an object that\n\
1532generates the numbers in the range on demand. This is slightly slower\n\
1533than range() but more memory efficient.";
1534
1535
Guido van Rossum79f25d91997-04-29 20:08:16 +00001536static PyObject *
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001537builtin_raw_input(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001538 PyObject *self;
1539 PyObject *args;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001540{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001541 PyObject *v = NULL;
1542 PyObject *f;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001543
Guido van Rossum79f25d91997-04-29 20:08:16 +00001544 if (!PyArg_ParseTuple(args, "|O:[raw_]input", &v))
Guido van Rossum3165fe61992-09-25 21:59:05 +00001545 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001546 if (PyFile_AsFile(PySys_GetObject("stdin")) == stdin &&
1547 PyFile_AsFile(PySys_GetObject("stdout")) == stdout &&
Guido van Rossum53bb7ff1995-07-26 16:26:31 +00001548 isatty(fileno(stdin)) && isatty(fileno(stdout))) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001549 PyObject *po;
Guido van Rossum872537c1995-07-07 22:43:42 +00001550 char *prompt;
1551 char *s;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001552 PyObject *result;
Guido van Rossum872537c1995-07-07 22:43:42 +00001553 if (v != NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001554 po = PyObject_Str(v);
Guido van Rossum872537c1995-07-07 22:43:42 +00001555 if (po == NULL)
1556 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001557 prompt = PyString_AsString(po);
Guido van Rossumd9b52081998-06-26 18:25:38 +00001558 if (prompt == NULL)
1559 return NULL;
Guido van Rossum872537c1995-07-07 22:43:42 +00001560 }
1561 else {
1562 po = NULL;
1563 prompt = "";
1564 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00001565 s = PyOS_Readline(prompt);
1566 Py_XDECREF(po);
Guido van Rossum872537c1995-07-07 22:43:42 +00001567 if (s == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001568 PyErr_SetNone(PyExc_KeyboardInterrupt);
Guido van Rossum872537c1995-07-07 22:43:42 +00001569 return NULL;
1570 }
1571 if (*s == '\0') {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001572 PyErr_SetNone(PyExc_EOFError);
Guido van Rossum872537c1995-07-07 22:43:42 +00001573 result = NULL;
1574 }
1575 else { /* strip trailing '\n' */
Guido van Rossum79f25d91997-04-29 20:08:16 +00001576 result = PyString_FromStringAndSize(s, strlen(s)-1);
Guido van Rossum872537c1995-07-07 22:43:42 +00001577 }
1578 free(s);
1579 return result;
1580 }
Guido van Rossum90933611991-06-07 16:10:43 +00001581 if (v != NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001582 f = PySys_GetObject("stdout");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001583 if (f == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001584 PyErr_SetString(PyExc_RuntimeError, "lost sys.stdout");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001585 return NULL;
1586 }
Guido van Rossumc8b6df91997-05-23 00:06:51 +00001587 if (Py_FlushLine() != 0 ||
1588 PyFile_WriteObject(v, f, Py_PRINT_RAW) != 0)
Guido van Rossum90933611991-06-07 16:10:43 +00001589 return NULL;
1590 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00001591 f = PySys_GetObject("stdin");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001592 if (f == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001593 PyErr_SetString(PyExc_RuntimeError, "lost sys.stdin");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001594 return NULL;
1595 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00001596 return PyFile_GetLine(f, -1);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001597}
1598
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001599static char raw_input_doc[] =
1600"raw_input([prompt]) -> string\n\
1601\n\
1602Read a string from standard input. The trailing newline is stripped.\n\
1603If the user hits EOF (Unix: Ctl-D, Windows: Ctl-Z+Return), raise EOFError.\n\
1604On Unix, GNU readline is used if enabled. The prompt string, if given,\n\
1605is printed without a trailing newline before reading.";
1606
1607
Guido van Rossum79f25d91997-04-29 20:08:16 +00001608static PyObject *
Guido van Rossum12d12c51993-10-26 17:58:25 +00001609builtin_reduce(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001610 PyObject *self;
1611 PyObject *args;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001612{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001613 PyObject *seq, *func, *result = NULL;
1614 PySequenceMethods *sqf;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001615 register int i;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001616
Guido van Rossum79f25d91997-04-29 20:08:16 +00001617 if (!PyArg_ParseTuple(args, "OO|O:reduce", &func, &seq, &result))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001618 return NULL;
1619 if (result != NULL)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001620 Py_INCREF(result);
Guido van Rossum12d12c51993-10-26 17:58:25 +00001621
Guido van Rossum09df08a1998-05-22 00:51:39 +00001622 sqf = seq->ob_type->tp_as_sequence;
1623 if (sqf == NULL || sqf->sq_item == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001624 PyErr_SetString(PyExc_TypeError,
Guido van Rossum12d12c51993-10-26 17:58:25 +00001625 "2nd argument to reduce() must be a sequence object");
1626 return NULL;
1627 }
1628
Guido van Rossum79f25d91997-04-29 20:08:16 +00001629 if ((args = PyTuple_New(2)) == NULL)
Guido van Rossum2d951851994-08-29 12:52:16 +00001630 goto Fail;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001631
Guido van Rossum2d951851994-08-29 12:52:16 +00001632 for (i = 0; ; ++i) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001633 PyObject *op2;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001634
1635 if (args->ob_refcnt > 1) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001636 Py_DECREF(args);
1637 if ((args = PyTuple_New(2)) == NULL)
Guido van Rossum2d951851994-08-29 12:52:16 +00001638 goto Fail;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001639 }
1640
Guido van Rossum2d951851994-08-29 12:52:16 +00001641 if ((op2 = (*sqf->sq_item)(seq, i)) == NULL) {
Barry Warsawcde8b1b1997-08-22 21:14:38 +00001642 if (PyErr_ExceptionMatches(PyExc_IndexError)) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001643 PyErr_Clear();
Guido van Rossum2d951851994-08-29 12:52:16 +00001644 break;
1645 }
1646 goto Fail;
1647 }
Guido van Rossum12d12c51993-10-26 17:58:25 +00001648
Guido van Rossum2d951851994-08-29 12:52:16 +00001649 if (result == NULL)
1650 result = op2;
1651 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001652 PyTuple_SetItem(args, 0, result);
1653 PyTuple_SetItem(args, 1, op2);
1654 if ((result = PyEval_CallObject(func, args)) == NULL)
Guido van Rossum2d951851994-08-29 12:52:16 +00001655 goto Fail;
1656 }
Guido van Rossum12d12c51993-10-26 17:58:25 +00001657 }
1658
Guido van Rossum79f25d91997-04-29 20:08:16 +00001659 Py_DECREF(args);
Guido van Rossum12d12c51993-10-26 17:58:25 +00001660
Guido van Rossum2d951851994-08-29 12:52:16 +00001661 if (result == NULL)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001662 PyErr_SetString(PyExc_TypeError,
Guido van Rossum2d951851994-08-29 12:52:16 +00001663 "reduce of empty sequence with no initial value");
1664
Guido van Rossum12d12c51993-10-26 17:58:25 +00001665 return result;
1666
Guido van Rossum2d951851994-08-29 12:52:16 +00001667Fail:
Guido van Rossum79f25d91997-04-29 20:08:16 +00001668 Py_XDECREF(args);
1669 Py_XDECREF(result);
Guido van Rossum12d12c51993-10-26 17:58:25 +00001670 return NULL;
1671}
1672
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001673static char reduce_doc[] =
1674"reduce(function, sequence[, initial]) -> value\n\
1675\n\
1676Apply a function of two arguments cumulatively to the items of a sequence,\n\
1677from left to right, so as to reduce the sequence to a single value.\n\
1678For example, reduce(lambda x, y: x+y, [1, 2, 3, 4, 5]) calculates\n\
1679((((1+2)+3)+4)+5). If initial is present, it is placed before the items\n\
1680of the sequence in the calculation, and serves as a default when the\n\
1681sequence is empty.";
1682
1683
Guido van Rossum79f25d91997-04-29 20:08:16 +00001684static PyObject *
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001685builtin_reload(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001686 PyObject *self;
1687 PyObject *args;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001688{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001689 PyObject *v;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001690
Guido van Rossum79f25d91997-04-29 20:08:16 +00001691 if (!PyArg_ParseTuple(args, "O:reload", &v))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001692 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001693 return PyImport_ReloadModule(v);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001694}
1695
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001696static char reload_doc[] =
1697"reload(module) -> module\n\
1698\n\
1699Reload the module. The module must have been successfully imported before.";
1700
1701
Guido van Rossum79f25d91997-04-29 20:08:16 +00001702static PyObject *
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001703builtin_repr(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001704 PyObject *self;
1705 PyObject *args;
Guido van Rossumc89705d1992-11-26 08:54:07 +00001706{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001707 PyObject *v;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001708
Guido van Rossum79f25d91997-04-29 20:08:16 +00001709 if (!PyArg_ParseTuple(args, "O:repr", &v))
Guido van Rossumc89705d1992-11-26 08:54:07 +00001710 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001711 return PyObject_Repr(v);
Guido van Rossumc89705d1992-11-26 08:54:07 +00001712}
1713
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001714static char repr_doc[] =
1715"repr(object) -> string\n\
1716\n\
1717Return the canonical string representation of the object.\n\
1718For most object types, eval(repr(object)) == object.";
1719
1720
Guido van Rossum79f25d91997-04-29 20:08:16 +00001721static PyObject *
Guido van Rossum9e51f9b1993-02-12 16:29:05 +00001722builtin_round(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001723 PyObject *self;
1724 PyObject *args;
Guido van Rossum9e51f9b1993-02-12 16:29:05 +00001725{
Guido van Rossum9e51f9b1993-02-12 16:29:05 +00001726 double x;
1727 double f;
1728 int ndigits = 0;
Guido van Rossum9e51f9b1993-02-12 16:29:05 +00001729 int i;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001730
Guido van Rossum79f25d91997-04-29 20:08:16 +00001731 if (!PyArg_ParseTuple(args, "d|i:round", &x, &ndigits))
Guido van Rossum9e51f9b1993-02-12 16:29:05 +00001732 return NULL;
Guido van Rossum9e51f9b1993-02-12 16:29:05 +00001733 f = 1.0;
Guido van Rossum1e162d31998-05-09 14:42:25 +00001734 i = abs(ndigits);
1735 while (--i >= 0)
Guido van Rossum9e51f9b1993-02-12 16:29:05 +00001736 f = f*10.0;
Guido van Rossum1e162d31998-05-09 14:42:25 +00001737 if (ndigits < 0)
1738 x /= f;
Guido van Rossum9e51f9b1993-02-12 16:29:05 +00001739 else
Guido van Rossum1e162d31998-05-09 14:42:25 +00001740 x *= f;
1741 if (x >= 0.0)
1742 x = floor(x + 0.5);
1743 else
1744 x = ceil(x - 0.5);
1745 if (ndigits < 0)
1746 x *= f;
1747 else
1748 x /= f;
1749 return PyFloat_FromDouble(x);
Guido van Rossum9e51f9b1993-02-12 16:29:05 +00001750}
1751
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001752static char round_doc[] =
1753"round(number[, ndigits]) -> floating point number\n\
1754\n\
1755Round a number to a given precision in decimal digits (default 0 digits).\n\
1756This always returns a floating point number. Precision may be negative.";
1757
1758
Guido van Rossum79f25d91997-04-29 20:08:16 +00001759static PyObject *
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001760builtin_str(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001761 PyObject *self;
1762 PyObject *args;
Guido van Rossumc89705d1992-11-26 08:54:07 +00001763{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001764 PyObject *v;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001765
Guido van Rossum79f25d91997-04-29 20:08:16 +00001766 if (!PyArg_ParseTuple(args, "O:str", &v))
Guido van Rossumc89705d1992-11-26 08:54:07 +00001767 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001768 return PyObject_Str(v);
Guido van Rossumc89705d1992-11-26 08:54:07 +00001769}
1770
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001771static char str_doc[] =
1772"str(object) -> string\n\
1773\n\
1774Return a nice string representation of the object.\n\
1775If the argument is a string, the return value is the same object.";
1776
1777
Guido van Rossum79f25d91997-04-29 20:08:16 +00001778static PyObject *
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001779builtin_tuple(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001780 PyObject *self;
1781 PyObject *args;
Guido van Rossumcae027b1994-08-29 12:53:11 +00001782{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001783 PyObject *v;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001784
Guido van Rossum79f25d91997-04-29 20:08:16 +00001785 if (!PyArg_ParseTuple(args, "O:tuple", &v))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001786 return NULL;
Guido van Rossum09df08a1998-05-22 00:51:39 +00001787 return PySequence_Tuple(v);
Guido van Rossumcae027b1994-08-29 12:53:11 +00001788}
1789
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001790static char tuple_doc[] =
1791"tuple(sequence) -> list\n\
1792\n\
1793Return a tuple whose items are the same as those of the argument sequence.\n\
1794If the argument is a tuple, the return value is the same object.";
1795
1796
Guido van Rossum79f25d91997-04-29 20:08:16 +00001797static PyObject *
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001798builtin_type(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001799 PyObject *self;
1800 PyObject *args;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001801{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001802 PyObject *v;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001803
Guido van Rossum79f25d91997-04-29 20:08:16 +00001804 if (!PyArg_ParseTuple(args, "O:type", &v))
Guido van Rossum3f5da241990-12-20 15:06:42 +00001805 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001806 v = (PyObject *)v->ob_type;
1807 Py_INCREF(v);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001808 return v;
1809}
1810
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001811static char type_doc[] =
1812"type(object) -> type object\n\
1813\n\
1814Return the type of the object.";
1815
1816
Guido van Rossum79f25d91997-04-29 20:08:16 +00001817static PyObject *
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001818builtin_vars(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001819 PyObject *self;
1820 PyObject *args;
Guido van Rossum2d951851994-08-29 12:52:16 +00001821{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001822 PyObject *v = NULL;
1823 PyObject *d;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001824
Guido van Rossum79f25d91997-04-29 20:08:16 +00001825 if (!PyArg_ParseTuple(args, "|O:vars", &v))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001826 return NULL;
Guido van Rossum2d951851994-08-29 12:52:16 +00001827 if (v == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001828 d = PyEval_GetLocals();
Guido van Rossum53bb7ff1995-07-26 16:26:31 +00001829 if (d == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001830 if (!PyErr_Occurred())
1831 PyErr_SetString(PyExc_SystemError,
1832 "no locals!?");
Guido van Rossum53bb7ff1995-07-26 16:26:31 +00001833 }
1834 else
Guido van Rossum79f25d91997-04-29 20:08:16 +00001835 Py_INCREF(d);
Guido van Rossum2d951851994-08-29 12:52:16 +00001836 }
1837 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001838 d = PyObject_GetAttrString(v, "__dict__");
Guido van Rossum2d951851994-08-29 12:52:16 +00001839 if (d == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001840 PyErr_SetString(PyExc_TypeError,
Guido van Rossum2d951851994-08-29 12:52:16 +00001841 "vars() argument must have __dict__ attribute");
1842 return NULL;
1843 }
1844 }
1845 return d;
1846}
1847
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001848static char vars_doc[] =
1849"vars([object]) -> dictionary\n\
1850\n\
1851Without arguments, equivalent to locals().\n\
1852With an argument, equivalent to object.__dict__.";
1853
1854
Barry Warsawcde8b1b1997-08-22 21:14:38 +00001855static PyObject *
1856builtin_isinstance(self, args)
1857 PyObject *self;
1858 PyObject *args;
1859{
1860 PyObject *inst;
1861 PyObject *cls;
1862 int retval;
1863
1864 if (!PyArg_ParseTuple(args, "OO", &inst, &cls))
1865 return NULL;
Guido van Rossumf5dd9141997-12-02 19:11:45 +00001866 if (PyType_Check(cls)) {
Guido van Rossumd6af46d1997-12-10 05:51:47 +00001867 retval = ((PyObject *)(inst->ob_type) == cls);
Barry Warsawcde8b1b1997-08-22 21:14:38 +00001868 }
Barry Warsawcde8b1b1997-08-22 21:14:38 +00001869 else {
Guido van Rossumf5dd9141997-12-02 19:11:45 +00001870 if (!PyClass_Check(cls)) {
1871 PyErr_SetString(PyExc_TypeError,
1872 "second argument must be a class");
1873 return NULL;
1874 }
1875
1876 if (!PyInstance_Check(inst))
1877 retval = 0;
1878 else {
1879 PyObject *inclass =
1880 (PyObject*)((PyInstanceObject*)inst)->in_class;
1881 retval = PyClass_IsSubclass(inclass, cls);
1882 }
Barry Warsawcde8b1b1997-08-22 21:14:38 +00001883 }
1884 return PyInt_FromLong(retval);
1885}
1886
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001887static char isinstance_doc[] =
1888"isinstance(object, class-or-type) -> Boolean\n\
1889\n\
1890Return whether an object is an instance of a class or of a subclass thereof.\n\
1891With a type as second argument, return whether that is the object's type.";
1892
Barry Warsawcde8b1b1997-08-22 21:14:38 +00001893
1894static PyObject *
1895builtin_issubclass(self, args)
1896 PyObject *self;
1897 PyObject *args;
1898{
1899 PyObject *derived;
1900 PyObject *cls;
1901 int retval;
1902
1903 if (!PyArg_ParseTuple(args, "OO", &derived, &cls))
1904 return NULL;
1905 if (!PyClass_Check(derived) || !PyClass_Check(cls)) {
1906 PyErr_SetString(PyExc_TypeError, "arguments must be classes");
1907 return NULL;
1908 }
1909 /* shortcut */
1910 if (!(retval = (derived == cls)))
1911 retval = PyClass_IsSubclass(derived, cls);
1912
1913 return PyInt_FromLong(retval);
1914}
1915
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001916static char issubclass_doc[] =
1917"issubclass(C, B) -> Boolean\n\
1918\n\
1919Return whether class C is a subclass (i.e., a derived class) of class B.";
1920
Barry Warsawcde8b1b1997-08-22 21:14:38 +00001921
Guido van Rossum79f25d91997-04-29 20:08:16 +00001922static PyMethodDef builtin_methods[] = {
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001923 {"__import__", builtin___import__, 1, import_doc},
1924 {"abs", builtin_abs, 1, abs_doc},
1925 {"apply", builtin_apply, 1, apply_doc},
1926 {"callable", builtin_callable, 1, callable_doc},
1927 {"chr", builtin_chr, 1, chr_doc},
1928 {"cmp", builtin_cmp, 1, cmp_doc},
1929 {"coerce", builtin_coerce, 1, coerce_doc},
1930 {"compile", builtin_compile, 1, compile_doc},
Guido van Rossum8a5c5d21996-01-12 01:09:56 +00001931#ifndef WITHOUT_COMPLEX
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001932 {"complex", builtin_complex, 1, complex_doc},
Guido van Rossum8a5c5d21996-01-12 01:09:56 +00001933#endif
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001934 {"delattr", builtin_delattr, 1, delattr_doc},
1935 {"dir", builtin_dir, 1, dir_doc},
1936 {"divmod", builtin_divmod, 1, divmod_doc},
1937 {"eval", builtin_eval, 1, eval_doc},
1938 {"execfile", builtin_execfile, 1, execfile_doc},
1939 {"filter", builtin_filter, 1, filter_doc},
1940 {"float", builtin_float, 1, float_doc},
1941 {"getattr", builtin_getattr, 1, getattr_doc},
1942 {"globals", builtin_globals, 1, globals_doc},
1943 {"hasattr", builtin_hasattr, 1, hasattr_doc},
1944 {"hash", builtin_hash, 1, hash_doc},
1945 {"hex", builtin_hex, 1, hex_doc},
1946 {"id", builtin_id, 1, id_doc},
1947 {"input", builtin_input, 1, input_doc},
1948 {"intern", builtin_intern, 1, intern_doc},
1949 {"int", builtin_int, 1, int_doc},
1950 {"isinstance", builtin_isinstance, 1, isinstance_doc},
1951 {"issubclass", builtin_issubclass, 1, issubclass_doc},
1952 {"len", builtin_len, 1, len_doc},
1953 {"list", builtin_list, 1, list_doc},
1954 {"locals", builtin_locals, 1, locals_doc},
1955 {"long", builtin_long, 1, long_doc},
1956 {"map", builtin_map, 1, map_doc},
1957 {"max", builtin_max, 1, max_doc},
1958 {"min", builtin_min, 1, min_doc},
1959 {"oct", builtin_oct, 1, oct_doc},
1960 {"open", builtin_open, 1, open_doc},
1961 {"ord", builtin_ord, 1, ord_doc},
1962 {"pow", builtin_pow, 1, pow_doc},
1963 {"range", builtin_range, 1, range_doc},
1964 {"raw_input", builtin_raw_input, 1, raw_input_doc},
1965 {"reduce", builtin_reduce, 1, reduce_doc},
1966 {"reload", builtin_reload, 1, reload_doc},
1967 {"repr", builtin_repr, 1, repr_doc},
1968 {"round", builtin_round, 1, round_doc},
1969 {"setattr", builtin_setattr, 1, setattr_doc},
1970 {"slice", builtin_slice, 1, slice_doc},
1971 {"str", builtin_str, 1, str_doc},
1972 {"tuple", builtin_tuple, 1, tuple_doc},
1973 {"type", builtin_type, 1, type_doc},
1974 {"vars", builtin_vars, 1, vars_doc},
1975 {"xrange", builtin_xrange, 1, xrange_doc},
Guido van Rossumc02e15c1991-12-16 13:03:00 +00001976 {NULL, NULL},
Guido van Rossum3f5da241990-12-20 15:06:42 +00001977};
1978
Guido van Rossum3f5da241990-12-20 15:06:42 +00001979/* Predefined exceptions */
1980
Guido van Rossum04748321997-09-16 18:43:15 +00001981PyObject *PyExc_Exception;
Barry Warsaw757af0e1997-08-29 22:13:51 +00001982PyObject *PyExc_StandardError;
Barry Warsaw412cdc21997-09-16 21:51:14 +00001983PyObject *PyExc_ArithmeticError;
Barry Warsaw757af0e1997-08-29 22:13:51 +00001984PyObject *PyExc_LookupError;
1985
Guido van Rossum79f25d91997-04-29 20:08:16 +00001986PyObject *PyExc_AssertionError;
1987PyObject *PyExc_AttributeError;
1988PyObject *PyExc_EOFError;
Guido van Rossumb6a7f771997-05-09 03:03:23 +00001989PyObject *PyExc_FloatingPointError;
Barry Warsawd086a1a1998-07-23 15:59:57 +00001990PyObject *PyExc_EnvironmentError;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001991PyObject *PyExc_IOError;
Barry Warsawd086a1a1998-07-23 15:59:57 +00001992PyObject *PyExc_OSError;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001993PyObject *PyExc_ImportError;
1994PyObject *PyExc_IndexError;
1995PyObject *PyExc_KeyError;
1996PyObject *PyExc_KeyboardInterrupt;
1997PyObject *PyExc_MemoryError;
1998PyObject *PyExc_NameError;
1999PyObject *PyExc_OverflowError;
2000PyObject *PyExc_RuntimeError;
Barry Warsaw344864f1998-12-01 18:52:06 +00002001PyObject *PyExc_NotImplementedError;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002002PyObject *PyExc_SyntaxError;
2003PyObject *PyExc_SystemError;
2004PyObject *PyExc_SystemExit;
2005PyObject *PyExc_TypeError;
2006PyObject *PyExc_ValueError;
2007PyObject *PyExc_ZeroDivisionError;
Guido van Rossum50afb7a1991-12-10 13:52:31 +00002008
Barry Warsaw757af0e1997-08-29 22:13:51 +00002009PyObject *PyExc_MemoryErrorInst;
2010
2011static struct
2012{
2013 char* name;
2014 PyObject** exc;
2015 int leaf_exc;
2016}
2017bltin_exc[] = {
Guido van Rossum04748321997-09-16 18:43:15 +00002018 {"Exception", &PyExc_Exception, 0},
Barry Warsaw757af0e1997-08-29 22:13:51 +00002019 {"StandardError", &PyExc_StandardError, 0},
Barry Warsaw412cdc21997-09-16 21:51:14 +00002020 {"ArithmeticError", &PyExc_ArithmeticError, 0},
Barry Warsaw757af0e1997-08-29 22:13:51 +00002021 {"LookupError", &PyExc_LookupError, 0},
2022 {"AssertionError", &PyExc_AssertionError, 1},
2023 {"AttributeError", &PyExc_AttributeError, 1},
2024 {"EOFError", &PyExc_EOFError, 1},
2025 {"FloatingPointError", &PyExc_FloatingPointError, 1},
Barry Warsaw78902031999-01-29 20:29:49 +00002026 {"EnvironmentError", &PyExc_EnvironmentError, 0},
Barry Warsaw757af0e1997-08-29 22:13:51 +00002027 {"IOError", &PyExc_IOError, 1},
Barry Warsawd086a1a1998-07-23 15:59:57 +00002028 {"OSError", &PyExc_OSError, 1},
Barry Warsaw757af0e1997-08-29 22:13:51 +00002029 {"ImportError", &PyExc_ImportError, 1},
2030 {"IndexError", &PyExc_IndexError, 1},
2031 {"KeyError", &PyExc_KeyError, 1},
2032 {"KeyboardInterrupt", &PyExc_KeyboardInterrupt, 1},
2033 {"MemoryError", &PyExc_MemoryError, 1},
2034 {"NameError", &PyExc_NameError, 1},
2035 {"OverflowError", &PyExc_OverflowError, 1},
2036 {"RuntimeError", &PyExc_RuntimeError, 1},
Barry Warsaw344864f1998-12-01 18:52:06 +00002037 {"NotImplementedError",&PyExc_NotImplementedError,1},
Barry Warsaw757af0e1997-08-29 22:13:51 +00002038 {"SyntaxError", &PyExc_SyntaxError, 1},
2039 {"SystemError", &PyExc_SystemError, 1},
2040 {"SystemExit", &PyExc_SystemExit, 1},
2041 {"TypeError", &PyExc_TypeError, 1},
2042 {"ValueError", &PyExc_ValueError, 1},
2043 {"ZeroDivisionError", &PyExc_ZeroDivisionError, 1},
2044 {NULL, NULL}
2045};
2046
2047
Barry Warsaw98b62461998-09-14 18:51:11 +00002048/* import exceptions module to extract class exceptions. on success,
2049 * return 1. on failure return 0 which signals _PyBuiltin_Init_2 to fall
2050 * back to using old-style string based exceptions.
2051 */
2052static int
Barry Warsaw757af0e1997-08-29 22:13:51 +00002053init_class_exc(dict)
2054 PyObject *dict;
2055{
2056 int i;
2057 PyObject *m = PyImport_ImportModule("exceptions");
Barry Warsaw98b62461998-09-14 18:51:11 +00002058 PyObject *args = NULL;
2059 PyObject *d = NULL;
Barry Warsaw757af0e1997-08-29 22:13:51 +00002060
Barry Warsaw98b62461998-09-14 18:51:11 +00002061 /* make sure we got the module and its dictionary */
Barry Warsaw757af0e1997-08-29 22:13:51 +00002062 if (m == NULL ||
2063 (d = PyModule_GetDict(m)) == NULL)
2064 {
Barry Warsaw98b62461998-09-14 18:51:11 +00002065 PySys_WriteStderr("'import exceptions' failed; ");
Barry Warsaw757af0e1997-08-29 22:13:51 +00002066 if (Py_VerboseFlag) {
Barry Warsaw98b62461998-09-14 18:51:11 +00002067 PySys_WriteStderr("traceback:\n");
Barry Warsaw757af0e1997-08-29 22:13:51 +00002068 PyErr_Print();
2069 }
2070 else {
Barry Warsaw98b62461998-09-14 18:51:11 +00002071 PySys_WriteStderr("use -v for traceback\n");
Barry Warsaw757af0e1997-08-29 22:13:51 +00002072 }
Barry Warsaw98b62461998-09-14 18:51:11 +00002073 goto finally;
Barry Warsaw757af0e1997-08-29 22:13:51 +00002074 }
2075 for (i = 0; bltin_exc[i].name; i++) {
2076 /* dig the exception out of the module */
2077 PyObject *exc = PyDict_GetItemString(d, bltin_exc[i].name);
Barry Warsaw98b62461998-09-14 18:51:11 +00002078 if (!exc) {
2079 PySys_WriteStderr(
2080 "Built-in exception class not found: %s. Library mismatch?\n",
2081 bltin_exc[i].name);
2082 goto finally;
2083 }
2084 /* free the old-style exception string object */
Barry Warsaw757af0e1997-08-29 22:13:51 +00002085 Py_XDECREF(*bltin_exc[i].exc);
2086
2087 /* squirrel away a pointer to the exception */
2088 Py_INCREF(exc);
2089 *bltin_exc[i].exc = exc;
2090
2091 /* and insert the name in the __builtin__ module */
Barry Warsaw98b62461998-09-14 18:51:11 +00002092 if (PyDict_SetItemString(dict, bltin_exc[i].name, exc)) {
2093 PySys_WriteStderr(
2094 "Cannot insert exception into __builtin__: %s\n",
2095 bltin_exc[i].name);
2096 goto finally;
2097 }
Barry Warsaw757af0e1997-08-29 22:13:51 +00002098 }
2099
2100 /* we need one pre-allocated instance */
2101 args = Py_BuildValue("()");
Barry Warsaw98b62461998-09-14 18:51:11 +00002102 if (!args ||
2103 !(PyExc_MemoryErrorInst =
2104 PyEval_CallObject(PyExc_MemoryError, args)))
2105 {
2106 PySys_WriteStderr("Cannot pre-allocate MemoryError instance\n");
2107 goto finally;
Barry Warsaw757af0e1997-08-29 22:13:51 +00002108 }
Barry Warsaw98b62461998-09-14 18:51:11 +00002109 Py_DECREF(args);
Barry Warsaw757af0e1997-08-29 22:13:51 +00002110
2111 /* we're done with the exceptions module */
2112 Py_DECREF(m);
2113
Barry Warsaw98b62461998-09-14 18:51:11 +00002114 if (PyErr_Occurred()) {
2115 PySys_WriteStderr("Cannot initialize standard class exceptions; ");
2116 if (Py_VerboseFlag) {
2117 PySys_WriteStderr("traceback:\n");
2118 PyErr_Print();
2119 }
2120 else
2121 PySys_WriteStderr("use -v for traceback\n");
2122 goto finally;
2123 }
2124 return 1;
2125 finally:
2126 Py_XDECREF(m);
2127 Py_XDECREF(args);
2128 PyErr_Clear();
2129 return 0;
Barry Warsaw757af0e1997-08-29 22:13:51 +00002130}
2131
2132
2133static void
2134fini_instances()
2135{
2136 Py_XDECREF(PyExc_MemoryErrorInst);
2137 PyExc_MemoryErrorInst = NULL;
2138}
2139
2140
Guido van Rossum79f25d91997-04-29 20:08:16 +00002141static PyObject *
Guido van Rossum25ce5661997-08-02 03:10:38 +00002142newstdexception(dict, name)
2143 PyObject *dict;
Guido van Rossumfb905c31991-12-16 15:42:38 +00002144 char *name;
Guido van Rossum3f5da241990-12-20 15:06:42 +00002145{
Guido van Rossum79f25d91997-04-29 20:08:16 +00002146 PyObject *v = PyString_FromString(name);
Guido van Rossum25ce5661997-08-02 03:10:38 +00002147 if (v == NULL || PyDict_SetItemString(dict, name, v) != 0)
Barry Warsaw98b62461998-09-14 18:51:11 +00002148 Py_FatalError("Cannot create string-based exceptions");
Guido van Rossum3f5da241990-12-20 15:06:42 +00002149 return v;
2150}
2151
2152static void
Guido van Rossum25ce5661997-08-02 03:10:38 +00002153initerrors(dict)
2154 PyObject *dict;
Guido van Rossum3f5da241990-12-20 15:06:42 +00002155{
Barry Warsaw757af0e1997-08-29 22:13:51 +00002156 int i;
2157 int exccnt = 0;
2158 for (i = 0; bltin_exc[i].name; i++, exccnt++) {
Barry Warsaw98b62461998-09-14 18:51:11 +00002159 Py_XDECREF(*bltin_exc[i].exc);
Barry Warsaw757af0e1997-08-29 22:13:51 +00002160 if (bltin_exc[i].leaf_exc)
2161 *bltin_exc[i].exc =
2162 newstdexception(dict, bltin_exc[i].name);
2163 }
2164
Barry Warsawd086a1a1998-07-23 15:59:57 +00002165 /* This is kind of bogus because we special case the some of the
2166 * new exceptions to be nearly forward compatible. But this means
2167 * we hard code knowledge about exceptions.py into C here. I don't
2168 * have a better solution, though.
2169 */
Barry Warsaw757af0e1997-08-29 22:13:51 +00002170 PyExc_LookupError = PyTuple_New(2);
2171 Py_INCREF(PyExc_IndexError);
2172 PyTuple_SET_ITEM(PyExc_LookupError, 0, PyExc_IndexError);
2173 Py_INCREF(PyExc_KeyError);
2174 PyTuple_SET_ITEM(PyExc_LookupError, 1, PyExc_KeyError);
2175 PyDict_SetItemString(dict, "LookupError", PyExc_LookupError);
2176
Barry Warsaw412cdc21997-09-16 21:51:14 +00002177 PyExc_ArithmeticError = PyTuple_New(3);
Barry Warsaw757af0e1997-08-29 22:13:51 +00002178 Py_INCREF(PyExc_OverflowError);
Barry Warsaw412cdc21997-09-16 21:51:14 +00002179 PyTuple_SET_ITEM(PyExc_ArithmeticError, 0, PyExc_OverflowError);
Barry Warsaw757af0e1997-08-29 22:13:51 +00002180 Py_INCREF(PyExc_ZeroDivisionError);
Barry Warsaw412cdc21997-09-16 21:51:14 +00002181 PyTuple_SET_ITEM(PyExc_ArithmeticError, 1, PyExc_ZeroDivisionError);
Barry Warsaw757af0e1997-08-29 22:13:51 +00002182 Py_INCREF(PyExc_FloatingPointError);
Barry Warsaw412cdc21997-09-16 21:51:14 +00002183 PyTuple_SET_ITEM(PyExc_ArithmeticError, 2, PyExc_FloatingPointError);
2184 PyDict_SetItemString(dict, "ArithmeticError", PyExc_ArithmeticError);
Barry Warsaw757af0e1997-08-29 22:13:51 +00002185
Barry Warsawd086a1a1998-07-23 15:59:57 +00002186 PyExc_EnvironmentError = PyTuple_New(2);
2187 Py_INCREF(PyExc_IOError);
2188 PyTuple_SET_ITEM(PyExc_EnvironmentError, 0, PyExc_IOError);
2189 Py_INCREF(PyExc_OSError);
2190 PyTuple_SET_ITEM(PyExc_EnvironmentError, 1, PyExc_OSError);
2191 PyDict_SetItemString(dict, "EnvironmentError", PyExc_EnvironmentError);
2192
Barry Warsawb01a7fa1997-09-18 03:44:38 +00002193 PyExc_StandardError = PyTuple_New(exccnt-2);
2194 for (i = 2; bltin_exc[i].name; i++) {
Barry Warsaw757af0e1997-08-29 22:13:51 +00002195 PyObject *exc = *bltin_exc[i].exc;
2196 Py_INCREF(exc);
Barry Warsawb01a7fa1997-09-18 03:44:38 +00002197 PyTuple_SET_ITEM(PyExc_StandardError, i-2, exc);
Barry Warsaw757af0e1997-08-29 22:13:51 +00002198 }
2199 PyDict_SetItemString(dict, "StandardError", PyExc_StandardError);
Guido van Rossum04748321997-09-16 18:43:15 +00002200
2201 /* Exception is treated differently; for now, it's == StandardError */
2202 PyExc_Exception = PyExc_StandardError;
2203 Py_INCREF(PyExc_Exception);
2204 PyDict_SetItemString(dict, "Exception", PyExc_Exception);
Barry Warsaw757af0e1997-08-29 22:13:51 +00002205
2206 if (PyErr_Occurred())
2207 Py_FatalError("Could not initialize built-in string exceptions");
Guido van Rossum25ce5661997-08-02 03:10:38 +00002208}
2209
2210static void
2211finierrors()
2212{
Barry Warsaw757af0e1997-08-29 22:13:51 +00002213 int i;
2214 for (i = 0; bltin_exc[i].name; i++) {
2215 PyObject *exc = *bltin_exc[i].exc;
2216 Py_XDECREF(exc);
2217 *bltin_exc[i].exc = NULL;
2218 }
Guido van Rossum25ce5661997-08-02 03:10:38 +00002219}
2220
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002221static char builtin_doc[] =
2222"Built-in functions, exceptions, and other objects.\n\
2223\n\
2224Noteworthy: None is the `nil' object; Ellipsis represents `...' in slices.";
2225
Guido van Rossum25ce5661997-08-02 03:10:38 +00002226PyObject *
Barry Warsaw757af0e1997-08-29 22:13:51 +00002227_PyBuiltin_Init_1()
Guido van Rossum25ce5661997-08-02 03:10:38 +00002228{
2229 PyObject *mod, *dict;
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002230 mod = Py_InitModule4("__builtin__", builtin_methods,
2231 builtin_doc, (PyObject *)NULL,
2232 PYTHON_API_VERSION);
Guido van Rossum25ce5661997-08-02 03:10:38 +00002233 if (mod == NULL)
2234 return NULL;
2235 dict = PyModule_GetDict(mod);
2236 initerrors(dict);
2237 if (PyDict_SetItemString(dict, "None", Py_None) < 0)
2238 return NULL;
2239 if (PyDict_SetItemString(dict, "Ellipsis", Py_Ellipsis) < 0)
2240 return NULL;
2241 if (PyDict_SetItemString(dict, "__debug__",
2242 PyInt_FromLong(Py_OptimizeFlag == 0)) < 0)
2243 return NULL;
Barry Warsaw757af0e1997-08-29 22:13:51 +00002244
Guido van Rossum25ce5661997-08-02 03:10:38 +00002245 return mod;
Guido van Rossum3f5da241990-12-20 15:06:42 +00002246}
2247
2248void
Barry Warsaw757af0e1997-08-29 22:13:51 +00002249_PyBuiltin_Init_2(dict)
2250 PyObject *dict;
2251{
2252 /* if Python was started with -X, initialize the class exceptions */
Barry Warsaw98b62461998-09-14 18:51:11 +00002253 if (Py_UseClassExceptionsFlag) {
2254 if (!init_class_exc(dict)) {
2255 /* class based exceptions could not be
2256 * initialized. Fall back to using string based
2257 * exceptions.
2258 */
2259 PySys_WriteStderr(
2260 "Warning! Falling back to string-based exceptions\n");
2261 initerrors(dict);
2262 }
2263 }
Barry Warsaw757af0e1997-08-29 22:13:51 +00002264}
2265
2266
2267void
2268_PyBuiltin_Fini_1()
2269{
2270 fini_instances();
2271}
2272
2273
2274void
2275_PyBuiltin_Fini_2()
Guido van Rossum3f5da241990-12-20 15:06:42 +00002276{
Guido van Rossum25ce5661997-08-02 03:10:38 +00002277 finierrors();
Guido van Rossum3f5da241990-12-20 15:06:42 +00002278}
Guido van Rossumc6bb8f71991-07-01 18:42:41 +00002279
Guido van Rossum12d12c51993-10-26 17:58:25 +00002280
Guido van Rossume77a7571993-11-03 15:01:26 +00002281/* Helper for filter(): filter a tuple through a function */
Guido van Rossum12d12c51993-10-26 17:58:25 +00002282
Guido van Rossum79f25d91997-04-29 20:08:16 +00002283static PyObject *
Guido van Rossum12d12c51993-10-26 17:58:25 +00002284filtertuple(func, tuple)
Guido van Rossum79f25d91997-04-29 20:08:16 +00002285 PyObject *func;
2286 PyObject *tuple;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002287{
Guido van Rossum79f25d91997-04-29 20:08:16 +00002288 PyObject *result;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002289 register int i, j;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002290 int len = PyTuple_Size(tuple);
Guido van Rossum12d12c51993-10-26 17:58:25 +00002291
Guido van Rossumb7b45621995-08-04 04:07:45 +00002292 if (len == 0) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002293 Py_INCREF(tuple);
Guido van Rossumb7b45621995-08-04 04:07:45 +00002294 return tuple;
2295 }
2296
Guido van Rossum79f25d91997-04-29 20:08:16 +00002297 if ((result = PyTuple_New(len)) == NULL)
Guido van Rossum2586bf01993-11-01 16:21:44 +00002298 return NULL;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002299
Guido van Rossum12d12c51993-10-26 17:58:25 +00002300 for (i = j = 0; i < len; ++i) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002301 PyObject *item, *good;
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002302 int ok;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002303
Guido van Rossum79f25d91997-04-29 20:08:16 +00002304 if ((item = PyTuple_GetItem(tuple, i)) == NULL)
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002305 goto Fail_1;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002306 if (func == Py_None) {
2307 Py_INCREF(item);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002308 good = item;
2309 }
2310 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002311 PyObject *arg = Py_BuildValue("(O)", item);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002312 if (arg == NULL)
2313 goto Fail_1;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002314 good = PyEval_CallObject(func, arg);
2315 Py_DECREF(arg);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002316 if (good == NULL)
Guido van Rossum12d12c51993-10-26 17:58:25 +00002317 goto Fail_1;
2318 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002319 ok = PyObject_IsTrue(good);
2320 Py_DECREF(good);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002321 if (ok) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002322 Py_INCREF(item);
2323 if (PyTuple_SetItem(result, j++, item) < 0)
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002324 goto Fail_1;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002325 }
Guido van Rossum12d12c51993-10-26 17:58:25 +00002326 }
2327
Guido van Rossum79f25d91997-04-29 20:08:16 +00002328 if (_PyTuple_Resize(&result, j, 0) < 0)
Guido van Rossum12d12c51993-10-26 17:58:25 +00002329 return NULL;
2330
Guido van Rossum12d12c51993-10-26 17:58:25 +00002331 return result;
2332
Guido van Rossum12d12c51993-10-26 17:58:25 +00002333Fail_1:
Guido van Rossum79f25d91997-04-29 20:08:16 +00002334 Py_DECREF(result);
Guido van Rossum12d12c51993-10-26 17:58:25 +00002335 return NULL;
2336}
2337
2338
Guido van Rossume77a7571993-11-03 15:01:26 +00002339/* Helper for filter(): filter a string through a function */
Guido van Rossum12d12c51993-10-26 17:58:25 +00002340
Guido van Rossum79f25d91997-04-29 20:08:16 +00002341static PyObject *
Guido van Rossum12d12c51993-10-26 17:58:25 +00002342filterstring(func, strobj)
Guido van Rossum79f25d91997-04-29 20:08:16 +00002343 PyObject *func;
2344 PyObject *strobj;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002345{
Guido van Rossum79f25d91997-04-29 20:08:16 +00002346 PyObject *result;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002347 register int i, j;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002348 int len = PyString_Size(strobj);
Guido van Rossum12d12c51993-10-26 17:58:25 +00002349
Guido van Rossum79f25d91997-04-29 20:08:16 +00002350 if (func == Py_None) {
Guido van Rossum2586bf01993-11-01 16:21:44 +00002351 /* No character is ever false -- share input string */
Guido van Rossum79f25d91997-04-29 20:08:16 +00002352 Py_INCREF(strobj);
Guido van Rossum2d951851994-08-29 12:52:16 +00002353 return strobj;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002354 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002355 if ((result = PyString_FromStringAndSize(NULL, len)) == NULL)
Guido van Rossum2586bf01993-11-01 16:21:44 +00002356 return NULL;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002357
Guido van Rossum12d12c51993-10-26 17:58:25 +00002358 for (i = j = 0; i < len; ++i) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002359 PyObject *item, *arg, *good;
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002360 int ok;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002361
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002362 item = (*strobj->ob_type->tp_as_sequence->sq_item)(strobj, i);
2363 if (item == NULL)
2364 goto Fail_1;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002365 arg = Py_BuildValue("(O)", item);
2366 Py_DECREF(item);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002367 if (arg == NULL)
2368 goto Fail_1;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002369 good = PyEval_CallObject(func, arg);
2370 Py_DECREF(arg);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002371 if (good == NULL)
2372 goto Fail_1;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002373 ok = PyObject_IsTrue(good);
2374 Py_DECREF(good);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002375 if (ok)
Guido van Rossum79f25d91997-04-29 20:08:16 +00002376 PyString_AS_STRING((PyStringObject *)result)[j++] =
2377 PyString_AS_STRING((PyStringObject *)item)[0];
Guido van Rossum12d12c51993-10-26 17:58:25 +00002378 }
2379
Guido van Rossum79f25d91997-04-29 20:08:16 +00002380 if (j < len && _PyString_Resize(&result, j) < 0)
Guido van Rossum12d12c51993-10-26 17:58:25 +00002381 return NULL;
2382
Guido van Rossum12d12c51993-10-26 17:58:25 +00002383 return result;
2384
Guido van Rossum12d12c51993-10-26 17:58:25 +00002385Fail_1:
Guido van Rossum79f25d91997-04-29 20:08:16 +00002386 Py_DECREF(result);
Guido van Rossum12d12c51993-10-26 17:58:25 +00002387 return NULL;
2388}