blob: b07597c582504ad0a9e223a0192070bc5daaeb47 [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 Rossum0daf0221999-03-19 19:07:19 +0000140builtin_buffer(self, args)
141 PyObject *self;
142 PyObject *args;
143{
144 PyObject *ob;
145 int offset = 0;
146 int size = Py_END_OF_BUFFER;
147
148 if ( !PyArg_ParseTuple(args, "O|ii:buffer", &ob, &offset, &size) )
149 return NULL;
150 return PyBuffer_FromObject(ob, offset, size);
151}
152
153static char buffer_doc[] =
154"buffer(object [, offset[, size]) -> object\n\
155\n\
156Creates a new buffer object which references the given object.\n\
157The buffer will reference a slice of the target object from the\n\
158start of the object (or at the specified offset). The slice will\n\
159extend to the end of the target object (or with the specified size).";
160
161
162static PyObject *
Guido van Rossum2d951851994-08-29 12:52:16 +0000163builtin_callable(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000164 PyObject *self;
165 PyObject *args;
Guido van Rossum2d951851994-08-29 12:52:16 +0000166{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000167 PyObject *v;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000168
Guido van Rossum79f25d91997-04-29 20:08:16 +0000169 if (!PyArg_ParseTuple(args, "O:callable", &v))
Guido van Rossum2d951851994-08-29 12:52:16 +0000170 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000171 return PyInt_FromLong((long)PyCallable_Check(v));
Guido van Rossum2d951851994-08-29 12:52:16 +0000172}
173
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000174static char callable_doc[] =
175"callable(object) -> Boolean\n\
176\n\
177Return whether the object is callable (i.e., some kind of function).\n\
178Note that classes are callable, as are instances with a __call__() method.";
179
180
Guido van Rossum79f25d91997-04-29 20:08:16 +0000181static PyObject *
Guido van Rossume77a7571993-11-03 15:01:26 +0000182builtin_filter(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000183 PyObject *self;
184 PyObject *args;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000185{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000186 PyObject *func, *seq, *result;
187 PySequenceMethods *sqf;
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000188 int len;
189 register int i, j;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000190
Guido van Rossum79f25d91997-04-29 20:08:16 +0000191 if (!PyArg_ParseTuple(args, "OO:filter", &func, &seq))
Guido van Rossum12d12c51993-10-26 17:58:25 +0000192 return NULL;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000193
Guido van Rossum79f25d91997-04-29 20:08:16 +0000194 if (PyString_Check(seq)) {
195 PyObject *r = filterstring(func, seq);
Guido van Rossum12d12c51993-10-26 17:58:25 +0000196 return r;
197 }
198
Guido van Rossum79f25d91997-04-29 20:08:16 +0000199 if (PyTuple_Check(seq)) {
200 PyObject *r = filtertuple(func, seq);
Guido van Rossum12d12c51993-10-26 17:58:25 +0000201 return r;
202 }
203
Guido van Rossum09df08a1998-05-22 00:51:39 +0000204 sqf = seq->ob_type->tp_as_sequence;
205 if (sqf == NULL || sqf->sq_length == NULL || sqf->sq_item == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000206 PyErr_SetString(PyExc_TypeError,
Guido van Rossume77a7571993-11-03 15:01:26 +0000207 "argument 2 to filter() must be a sequence type");
Guido van Rossum12d12c51993-10-26 17:58:25 +0000208 goto Fail_2;
209 }
210
211 if ((len = (*sqf->sq_length)(seq)) < 0)
212 goto Fail_2;
213
Guido van Rossum79f25d91997-04-29 20:08:16 +0000214 if (PyList_Check(seq) && seq->ob_refcnt == 1) {
215 Py_INCREF(seq);
Guido van Rossum12d12c51993-10-26 17:58:25 +0000216 result = seq;
217 }
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000218 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000219 if ((result = PyList_New(len)) == NULL)
Guido van Rossum12d12c51993-10-26 17:58:25 +0000220 goto Fail_2;
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000221 }
Guido van Rossum12d12c51993-10-26 17:58:25 +0000222
Guido van Rossum2d951851994-08-29 12:52:16 +0000223 for (i = j = 0; ; ++i) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000224 PyObject *item, *good;
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000225 int ok;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000226
Guido van Rossum2d951851994-08-29 12:52:16 +0000227 if ((item = (*sqf->sq_item)(seq, i)) == NULL) {
Barry Warsawcde8b1b1997-08-22 21:14:38 +0000228 if (PyErr_ExceptionMatches(PyExc_IndexError)) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000229 PyErr_Clear();
Guido van Rossum2d951851994-08-29 12:52:16 +0000230 break;
231 }
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000232 goto Fail_1;
Guido van Rossum2d951851994-08-29 12:52:16 +0000233 }
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000234
Guido van Rossum79f25d91997-04-29 20:08:16 +0000235 if (func == Py_None) {
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000236 good = item;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000237 Py_INCREF(good);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000238 }
239 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000240 PyObject *arg = Py_BuildValue("(O)", item);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000241 if (arg == NULL)
242 goto Fail_1;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000243 good = PyEval_CallObject(func, arg);
244 Py_DECREF(arg);
Guido van Rossum58b68731995-01-10 17:40:55 +0000245 if (good == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000246 Py_DECREF(item);
Guido van Rossum12d12c51993-10-26 17:58:25 +0000247 goto Fail_1;
Guido van Rossum58b68731995-01-10 17:40:55 +0000248 }
Guido van Rossum12d12c51993-10-26 17:58:25 +0000249 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000250 ok = PyObject_IsTrue(good);
251 Py_DECREF(good);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000252 if (ok) {
Guido van Rossum2d951851994-08-29 12:52:16 +0000253 if (j < len) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000254 if (PyList_SetItem(result, j++, item) < 0)
Guido van Rossum2d951851994-08-29 12:52:16 +0000255 goto Fail_1;
256 }
257 else {
Barry Warsawfa77e091999-01-28 18:49:12 +0000258 int status = PyList_Append(result, item);
Guido van Rossum2d951851994-08-29 12:52:16 +0000259 j++;
Barry Warsawfa77e091999-01-28 18:49:12 +0000260 Py_DECREF(item);
261 if (status < 0)
Guido van Rossum2d951851994-08-29 12:52:16 +0000262 goto Fail_1;
263 }
Guido van Rossum58b68731995-01-10 17:40:55 +0000264 } else {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000265 Py_DECREF(item);
Guido van Rossum12d12c51993-10-26 17:58:25 +0000266 }
Guido van Rossum12d12c51993-10-26 17:58:25 +0000267 }
268
Guido van Rossum12d12c51993-10-26 17:58:25 +0000269
Guido van Rossum79f25d91997-04-29 20:08:16 +0000270 if (j < len && PyList_SetSlice(result, j, len, NULL) < 0)
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000271 goto Fail_1;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000272
Guido van Rossum12d12c51993-10-26 17:58:25 +0000273 return result;
274
Guido van Rossum12d12c51993-10-26 17:58:25 +0000275Fail_1:
Guido van Rossum79f25d91997-04-29 20:08:16 +0000276 Py_DECREF(result);
Guido van Rossum12d12c51993-10-26 17:58:25 +0000277Fail_2:
Guido van Rossum12d12c51993-10-26 17:58:25 +0000278 return NULL;
279}
280
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000281static char filter_doc[] =
282"filter(function, sequence) -> list\n\
283\n\
284Return a list containing those items of sequence for which function(item)\n\
285is true. If function is None, return a list of items that are true.";
286
287
Guido van Rossum79f25d91997-04-29 20:08:16 +0000288static PyObject *
Guido van Rossum94390a41992-08-14 15:14:30 +0000289builtin_chr(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000290 PyObject *self;
291 PyObject *args;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000292{
293 long x;
294 char s[1];
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000295
Guido van Rossum79f25d91997-04-29 20:08:16 +0000296 if (!PyArg_ParseTuple(args, "l:chr", &x))
Guido van Rossum3f5da241990-12-20 15:06:42 +0000297 return NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000298 if (x < 0 || x >= 256) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000299 PyErr_SetString(PyExc_ValueError,
300 "chr() arg not in range(256)");
Guido van Rossum3f5da241990-12-20 15:06:42 +0000301 return NULL;
302 }
Guido van Rossum6bf62da1997-04-11 20:37:35 +0000303 s[0] = (char)x;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000304 return PyString_FromStringAndSize(s, 1);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000305}
306
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000307static char chr_doc[] =
308"chr(i) -> character\n\
309\n\
310Return a string of one character with ordinal i; 0 <= i < 256.";
311
312
Guido van Rossum79f25d91997-04-29 20:08:16 +0000313static PyObject *
Guido van Rossuma9e7dc11992-10-18 18:53:57 +0000314builtin_cmp(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000315 PyObject *self;
316 PyObject *args;
Guido van Rossuma9e7dc11992-10-18 18:53:57 +0000317{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000318 PyObject *a, *b;
Guido van Rossum09df08a1998-05-22 00:51:39 +0000319 int c;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000320
Guido van Rossum79f25d91997-04-29 20:08:16 +0000321 if (!PyArg_ParseTuple(args, "OO:cmp", &a, &b))
Guido van Rossuma9e7dc11992-10-18 18:53:57 +0000322 return NULL;
Guido van Rossum09df08a1998-05-22 00:51:39 +0000323 if (PyObject_Cmp(a, b, &c) < 0)
Guido van Rossumc8b6df91997-05-23 00:06:51 +0000324 return NULL;
Guido van Rossum09df08a1998-05-22 00:51:39 +0000325 return PyInt_FromLong((long)c);
Guido van Rossuma9e7dc11992-10-18 18:53:57 +0000326}
327
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000328static char cmp_doc[] =
329"cmp(x, y) -> integer\n\
330\n\
331Return negative if x<y, zero if x==y, positive if x>y.";
332
333
Guido van Rossum79f25d91997-04-29 20:08:16 +0000334static PyObject *
Guido van Rossum5524a591995-01-10 15:26:20 +0000335builtin_coerce(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000336 PyObject *self;
337 PyObject *args;
Guido van Rossum6a00cd81995-01-07 12:39:01 +0000338{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000339 PyObject *v, *w;
340 PyObject *res;
Guido van Rossum5524a591995-01-10 15:26:20 +0000341
Guido van Rossum79f25d91997-04-29 20:08:16 +0000342 if (!PyArg_ParseTuple(args, "OO:coerce", &v, &w))
Guido van Rossum5524a591995-01-10 15:26:20 +0000343 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000344 if (PyNumber_Coerce(&v, &w) < 0)
Guido van Rossum04691fc1992-08-12 15:35:34 +0000345 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000346 res = Py_BuildValue("(OO)", v, w);
347 Py_DECREF(v);
348 Py_DECREF(w);
Guido van Rossum04691fc1992-08-12 15:35:34 +0000349 return res;
350}
351
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000352static char coerce_doc[] =
353"coerce(x, y) -> None or (x1, y1)\n\
354\n\
355When x and y can be coerced to values of the same type, return a tuple\n\
356containing the coerced values. When they can't be coerced, return None.";
357
358
Guido van Rossum79f25d91997-04-29 20:08:16 +0000359static PyObject *
Guido van Rossum5b722181993-03-30 17:46:03 +0000360builtin_compile(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000361 PyObject *self;
362 PyObject *args;
Guido van Rossum5b722181993-03-30 17:46:03 +0000363{
364 char *str;
365 char *filename;
366 char *startstr;
367 int start;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000368
Guido van Rossum79f25d91997-04-29 20:08:16 +0000369 if (!PyArg_ParseTuple(args, "sss:compile", &str, &filename, &startstr))
Guido van Rossum5b722181993-03-30 17:46:03 +0000370 return NULL;
371 if (strcmp(startstr, "exec") == 0)
Guido van Rossumb05a5c71997-05-07 17:46:13 +0000372 start = Py_file_input;
Guido van Rossum5b722181993-03-30 17:46:03 +0000373 else if (strcmp(startstr, "eval") == 0)
Guido van Rossumb05a5c71997-05-07 17:46:13 +0000374 start = Py_eval_input;
Guido van Rossum872537c1995-07-07 22:43:42 +0000375 else if (strcmp(startstr, "single") == 0)
Guido van Rossumb05a5c71997-05-07 17:46:13 +0000376 start = Py_single_input;
Guido van Rossum5b722181993-03-30 17:46:03 +0000377 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000378 PyErr_SetString(PyExc_ValueError,
Guido van Rossum872537c1995-07-07 22:43:42 +0000379 "compile() mode must be 'exec' or 'eval' or 'single'");
Guido van Rossum5b722181993-03-30 17:46:03 +0000380 return NULL;
381 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000382 return Py_CompileString(str, filename, start);
Guido van Rossum5b722181993-03-30 17:46:03 +0000383}
384
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000385static char compile_doc[] =
386"compile(source, filename, mode) -> code object\n\
387\n\
388Compile the source string (a Python module, statement or expression)\n\
389into a code object that can be executed by the exec statement or eval().\n\
390The filename will be used for run-time error messages.\n\
391The mode must be 'exec' to compile a module, 'single' to compile a\n\
392single (interactive) statement, or 'eval' to compile an expression.";
393
394
Guido van Rossum8a5c5d21996-01-12 01:09:56 +0000395#ifndef WITHOUT_COMPLEX
396
Guido van Rossum79f25d91997-04-29 20:08:16 +0000397static PyObject *
Guido van Rossum8a5c5d21996-01-12 01:09:56 +0000398builtin_complex(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000399 PyObject *self;
400 PyObject *args;
Guido van Rossum8a5c5d21996-01-12 01:09:56 +0000401{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000402 PyObject *r, *i, *tmp;
403 PyNumberMethods *nbr, *nbi = NULL;
Guido van Rossum530956d1996-07-21 02:27:43 +0000404 Py_complex cr, ci;
Guido van Rossumc6472e91997-03-31 17:15:43 +0000405 int own_r = 0;
Guido van Rossum8a5c5d21996-01-12 01:09:56 +0000406
407 i = NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000408 if (!PyArg_ParseTuple(args, "O|O:complex", &r, &i))
Guido van Rossum8a5c5d21996-01-12 01:09:56 +0000409 return NULL;
410 if ((nbr = r->ob_type->tp_as_number) == NULL ||
Guido van Rossumc6472e91997-03-31 17:15:43 +0000411 nbr->nb_float == NULL ||
412 (i != NULL &&
413 ((nbi = i->ob_type->tp_as_number) == NULL ||
414 nbi->nb_float == NULL))) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000415 PyErr_SetString(PyExc_TypeError,
Guido van Rossum8a5c5d21996-01-12 01:09:56 +0000416 "complex() argument can't be converted to complex");
417 return NULL;
418 }
Guido van Rossumed0af8f1996-12-05 23:18:18 +0000419 /* XXX Hack to support classes with __complex__ method */
Guido van Rossum79f25d91997-04-29 20:08:16 +0000420 if (PyInstance_Check(r)) {
421 static PyObject *complexstr;
422 PyObject *f;
Guido van Rossumed0af8f1996-12-05 23:18:18 +0000423 if (complexstr == NULL) {
Guido van Rossum8d751611997-01-18 08:04:16 +0000424 complexstr = PyString_InternFromString("__complex__");
Guido van Rossumed0af8f1996-12-05 23:18:18 +0000425 if (complexstr == NULL)
426 return NULL;
427 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000428 f = PyObject_GetAttr(r, complexstr);
Guido van Rossumed0af8f1996-12-05 23:18:18 +0000429 if (f == NULL)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000430 PyErr_Clear();
Guido van Rossumed0af8f1996-12-05 23:18:18 +0000431 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000432 PyObject *args = Py_BuildValue("()");
Guido van Rossumed0af8f1996-12-05 23:18:18 +0000433 if (args == NULL)
434 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000435 r = PyEval_CallObject(f, args);
436 Py_DECREF(args);
Barry Warsawf988e681999-01-27 23:13:59 +0000437 Py_DECREF(f);
Guido van Rossumed0af8f1996-12-05 23:18:18 +0000438 if (r == NULL)
439 return NULL;
Guido van Rossumc6472e91997-03-31 17:15:43 +0000440 own_r = 1;
Guido van Rossumed0af8f1996-12-05 23:18:18 +0000441 }
442 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000443 if (PyComplex_Check(r)) {
444 cr = ((PyComplexObject*)r)->cval;
Guido van Rossum730806d1998-04-10 22:27:42 +0000445 if (own_r) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000446 Py_DECREF(r);
Guido van Rossum730806d1998-04-10 22:27:42 +0000447 }
Guido van Rossumc6472e91997-03-31 17:15:43 +0000448 }
Guido van Rossum8a5c5d21996-01-12 01:09:56 +0000449 else {
Guido van Rossumfe4b6ee1996-08-08 18:49:41 +0000450 tmp = (*nbr->nb_float)(r);
Guido van Rossum730806d1998-04-10 22:27:42 +0000451 if (own_r) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000452 Py_DECREF(r);
Guido van Rossum730806d1998-04-10 22:27:42 +0000453 }
Guido van Rossumfe4b6ee1996-08-08 18:49:41 +0000454 if (tmp == NULL)
455 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000456 cr.real = PyFloat_AsDouble(tmp);
457 Py_DECREF(tmp);
Guido van Rossum8a5c5d21996-01-12 01:09:56 +0000458 cr.imag = 0.;
459 }
460 if (i == NULL) {
461 ci.real = 0.;
462 ci.imag = 0.;
463 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000464 else if (PyComplex_Check(i))
465 ci = ((PyComplexObject*)i)->cval;
Guido van Rossum8a5c5d21996-01-12 01:09:56 +0000466 else {
Guido van Rossumc6472e91997-03-31 17:15:43 +0000467 tmp = (*nbi->nb_float)(i);
Guido van Rossumfe4b6ee1996-08-08 18:49:41 +0000468 if (tmp == NULL)
469 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000470 ci.real = PyFloat_AsDouble(tmp);
471 Py_DECREF(tmp);
Guido van Rossum8a5c5d21996-01-12 01:09:56 +0000472 ci.imag = 0.;
473 }
474 cr.real -= ci.imag;
475 cr.imag += ci.real;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000476 return PyComplex_FromCComplex(cr);
Guido van Rossum8a5c5d21996-01-12 01:09:56 +0000477}
478
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000479static char complex_doc[] =
480"complex(real[, imag]) -> complex number\n\
481\n\
482Create a complex number from a real part and an optional imaginary part.\n\
483This is equivalent to (real + imag*1j) where imag defaults to 0.";
484
485
Guido van Rossum8a5c5d21996-01-12 01:09:56 +0000486#endif
487
Guido van Rossum79f25d91997-04-29 20:08:16 +0000488static PyObject *
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000489builtin_dir(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000490 PyObject *self;
491 PyObject *args;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000492{
Guido van Rossum666b17a1997-05-06 16:36:57 +0000493 static char *attrlist[] = {"__members__", "__methods__", NULL};
494 PyObject *v = NULL, *l = NULL, *m = NULL;
495 PyObject *d, *x;
496 int i;
497 char **s;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000498
Guido van Rossum79f25d91997-04-29 20:08:16 +0000499 if (!PyArg_ParseTuple(args, "|O:dir", &v))
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000500 return NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000501 if (v == NULL) {
Guido van Rossum666b17a1997-05-06 16:36:57 +0000502 x = PyEval_GetLocals();
503 if (x == NULL)
504 goto error;
505 l = PyMapping_Keys(x);
506 if (l == NULL)
507 goto error;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000508 }
509 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000510 d = PyObject_GetAttrString(v, "__dict__");
Guido van Rossum666b17a1997-05-06 16:36:57 +0000511 if (d == NULL)
Guido van Rossum795ba581996-05-23 22:49:07 +0000512 PyErr_Clear();
Guido van Rossum666b17a1997-05-06 16:36:57 +0000513 else {
514 l = PyMapping_Keys(d);
515 if (l == NULL)
516 PyErr_Clear();
517 Py_DECREF(d);
518 }
519 if (l == NULL) {
520 l = PyList_New(0);
521 if (l == NULL)
522 goto error;
523 }
524 for (s = attrlist; *s != NULL; s++) {
525 m = PyObject_GetAttrString(v, *s);
526 if (m == NULL) {
527 PyErr_Clear();
528 continue;
529 }
530 for (i = 0; ; i++) {
531 x = PySequence_GetItem(m, i);
532 if (x == NULL) {
533 PyErr_Clear();
534 break;
535 }
536 if (PyList_Append(l, x) != 0) {
537 Py_DECREF(x);
538 Py_DECREF(m);
539 goto error;
540 }
541 Py_DECREF(x);
542 }
543 Py_DECREF(m);
Guido van Rossum795ba581996-05-23 22:49:07 +0000544 }
Guido van Rossum006bcd41991-10-24 14:54:44 +0000545 }
Guido van Rossum666b17a1997-05-06 16:36:57 +0000546 if (PyList_Sort(l) != 0)
547 goto error;
548 return l;
549 error:
550 Py_XDECREF(l);
551 return NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000552}
553
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000554static char dir_doc[] =
555"dir([object]) -> list of strings\n\
556\n\
557Return an alphabetized list of names comprising (some of) the attributes\n\
558of the given object. Without an argument, the names in the current scope\n\
559are listed. With an instance argument, only the instance attributes are\n\
560returned. With a class argument, attributes of the base class are not\n\
561returned. For other types or arguments, this may list members or methods.";
562
563
Guido van Rossum79f25d91997-04-29 20:08:16 +0000564static PyObject *
Guido van Rossum6a00cd81995-01-07 12:39:01 +0000565builtin_divmod(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000566 PyObject *self;
567 PyObject *args;
Guido van Rossum6a00cd81995-01-07 12:39:01 +0000568{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000569 PyObject *v, *w;
Guido van Rossum6a00cd81995-01-07 12:39:01 +0000570
Guido van Rossum79f25d91997-04-29 20:08:16 +0000571 if (!PyArg_ParseTuple(args, "OO:divmod", &v, &w))
Guido van Rossum6a00cd81995-01-07 12:39:01 +0000572 return NULL;
Guido van Rossum09df08a1998-05-22 00:51:39 +0000573 return PyNumber_Divmod(v, w);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000574}
575
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000576static char divmod_doc[] =
577"divmod(x, y) -> (div, mod)\n\
578\n\
579Return the tuple ((x-x%y)/y, x%y). Invariant: div*y + mod == x.";
580
581
Guido van Rossum79f25d91997-04-29 20:08:16 +0000582static PyObject *
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000583builtin_eval(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000584 PyObject *self;
585 PyObject *args;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000586{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000587 PyObject *cmd;
588 PyObject *globals = Py_None, *locals = Py_None;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000589 char *str;
Guido van Rossum590baa41993-11-30 13:40:46 +0000590
Guido van Rossum79f25d91997-04-29 20:08:16 +0000591 if (!PyArg_ParseTuple(args, "O|O!O!:eval",
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000592 &cmd,
Guido van Rossum79f25d91997-04-29 20:08:16 +0000593 &PyDict_Type, &globals,
594 &PyDict_Type, &locals))
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000595 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000596 if (globals == Py_None) {
597 globals = PyEval_GetGlobals();
598 if (locals == Py_None)
599 locals = PyEval_GetLocals();
Guido van Rossum6135a871995-01-09 17:53:26 +0000600 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000601 else if (locals == Py_None)
Guido van Rossum6135a871995-01-09 17:53:26 +0000602 locals = globals;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000603 if (PyDict_GetItemString(globals, "__builtins__") == NULL) {
604 if (PyDict_SetItemString(globals, "__builtins__",
605 PyEval_GetBuiltins()) != 0)
Guido van Rossum6135a871995-01-09 17:53:26 +0000606 return NULL;
607 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000608 if (PyCode_Check(cmd))
609 return PyEval_EvalCode((PyCodeObject *) cmd, globals, locals);
610 if (!PyString_Check(cmd)) {
611 PyErr_SetString(PyExc_TypeError,
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000612 "eval() argument 1 must be string or code object");
Guido van Rossum94390a41992-08-14 15:14:30 +0000613 return NULL;
614 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000615 str = PyString_AsString(cmd);
616 if ((int)strlen(str) != PyString_Size(cmd)) {
617 PyErr_SetString(PyExc_ValueError,
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000618 "embedded '\\0' in string arg");
619 return NULL;
Guido van Rossumf08ab0a1992-03-04 16:41:41 +0000620 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000621 while (*str == ' ' || *str == '\t')
622 str++;
Guido van Rossumb05a5c71997-05-07 17:46:13 +0000623 return PyRun_String(str, Py_eval_input, globals, locals);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000624}
625
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000626static char eval_doc[] =
627"eval(source[, globals[, locals]]) -> value\n\
628\n\
629Evaluate the source in the context of globals and locals.\n\
630The source may be a string representing a Python expression\n\
631or a code object as returned by compile().\n\
632The globals and locals are dictionaries, defaulting to the current\n\
633globals and locals. If only globals is given, locals defaults to it.";
634
635
Guido van Rossum79f25d91997-04-29 20:08:16 +0000636static PyObject *
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000637builtin_execfile(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000638 PyObject *self;
639 PyObject *args;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000640{
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000641 char *filename;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000642 PyObject *globals = Py_None, *locals = Py_None;
643 PyObject *res;
Guido van Rossum0f61f8a1992-02-25 18:55:05 +0000644 FILE* fp;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000645
Guido van Rossum79f25d91997-04-29 20:08:16 +0000646 if (!PyArg_ParseTuple(args, "s|O!O!:execfile",
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000647 &filename,
Guido van Rossum79f25d91997-04-29 20:08:16 +0000648 &PyDict_Type, &globals,
649 &PyDict_Type, &locals))
Guido van Rossum0f61f8a1992-02-25 18:55:05 +0000650 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000651 if (globals == Py_None) {
652 globals = PyEval_GetGlobals();
653 if (locals == Py_None)
654 locals = PyEval_GetLocals();
Guido van Rossum6135a871995-01-09 17:53:26 +0000655 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000656 else if (locals == Py_None)
Guido van Rossum6135a871995-01-09 17:53:26 +0000657 locals = globals;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000658 if (PyDict_GetItemString(globals, "__builtins__") == NULL) {
659 if (PyDict_SetItemString(globals, "__builtins__",
660 PyEval_GetBuiltins()) != 0)
Guido van Rossum6135a871995-01-09 17:53:26 +0000661 return NULL;
662 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000663 Py_BEGIN_ALLOW_THREADS
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000664 fp = fopen(filename, "r");
Guido van Rossum79f25d91997-04-29 20:08:16 +0000665 Py_END_ALLOW_THREADS
Guido van Rossum0f61f8a1992-02-25 18:55:05 +0000666 if (fp == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000667 PyErr_SetFromErrno(PyExc_IOError);
Guido van Rossum0f61f8a1992-02-25 18:55:05 +0000668 return NULL;
669 }
Guido van Rossumb05a5c71997-05-07 17:46:13 +0000670 res = PyRun_File(fp, filename, Py_file_input, globals, locals);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000671 Py_BEGIN_ALLOW_THREADS
Guido van Rossum0f61f8a1992-02-25 18:55:05 +0000672 fclose(fp);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000673 Py_END_ALLOW_THREADS
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000674 return res;
Guido van Rossum0f61f8a1992-02-25 18:55:05 +0000675}
676
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000677static char execfile_doc[] =
678"execfile(filename[, globals[, locals]])\n\
679\n\
680Read and execute a Python script from a file.\n\
681The globals and locals are dictionaries, defaulting to the current\n\
682globals and locals. If only globals is given, locals defaults to it.";
683
684
Guido van Rossum79f25d91997-04-29 20:08:16 +0000685static PyObject *
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000686builtin_float(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000687 PyObject *self;
688 PyObject *args;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000689{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000690 PyObject *v;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000691
Guido van Rossum79f25d91997-04-29 20:08:16 +0000692 if (!PyArg_ParseTuple(args, "O:float", &v))
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000693 return NULL;
Guido van Rossum09df08a1998-05-22 00:51:39 +0000694 return PyNumber_Float(v);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000695}
696
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000697static char float_doc[] =
698"float(x) -> floating point number\n\
699\n\
700Convert a string or number to a floating point number, if possible.";
701
702
Guido van Rossum79f25d91997-04-29 20:08:16 +0000703static PyObject *
Guido van Rossum94390a41992-08-14 15:14:30 +0000704builtin_getattr(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000705 PyObject *self;
706 PyObject *args;
Guido van Rossum33894be1992-01-27 16:53:09 +0000707{
Guido van Rossum950ff291998-06-29 13:38:57 +0000708 PyObject *v, *result, *dflt = NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000709 PyObject *name;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000710
Guido van Rossum950ff291998-06-29 13:38:57 +0000711 if (!PyArg_ParseTuple(args, "OS|O:getattr", &v, &name, &dflt))
Guido van Rossum33894be1992-01-27 16:53:09 +0000712 return NULL;
Guido van Rossum950ff291998-06-29 13:38:57 +0000713 result = PyObject_GetAttr(v, name);
714 if (result == NULL && dflt != NULL) {
715 PyErr_Clear();
716 Py_INCREF(dflt);
717 result = dflt;
718 }
719 return result;
Guido van Rossum9bfef441993-03-29 10:43:31 +0000720}
721
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000722static char getattr_doc[] =
Guido van Rossum950ff291998-06-29 13:38:57 +0000723"getattr(object, name[, default]) -> value\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000724\n\
Guido van Rossum950ff291998-06-29 13:38:57 +0000725Get a named attribute from an object; getattr(x, 'y') is equivalent to x.y.\n\
726When a default argument is given, it is returned when the attribute doesn't\n\
727exist; without it, an exception is raised in that case.";
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000728
729
Guido van Rossum79f25d91997-04-29 20:08:16 +0000730static PyObject *
Guido van Rossum872537c1995-07-07 22:43:42 +0000731builtin_globals(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000732 PyObject *self;
733 PyObject *args;
Guido van Rossum872537c1995-07-07 22:43:42 +0000734{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000735 PyObject *d;
Guido van Rossum872537c1995-07-07 22:43:42 +0000736
Guido van Rossum79f25d91997-04-29 20:08:16 +0000737 if (!PyArg_ParseTuple(args, ""))
Guido van Rossum872537c1995-07-07 22:43:42 +0000738 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000739 d = PyEval_GetGlobals();
740 Py_INCREF(d);
Guido van Rossum872537c1995-07-07 22:43:42 +0000741 return d;
742}
743
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000744static char globals_doc[] =
745"globals() -> dictionary\n\
746\n\
747Return the dictionary containing the current scope's global variables.";
748
749
Guido van Rossum79f25d91997-04-29 20:08:16 +0000750static PyObject *
Guido van Rossum9bfef441993-03-29 10:43:31 +0000751builtin_hasattr(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000752 PyObject *self;
753 PyObject *args;
Guido van Rossum9bfef441993-03-29 10:43:31 +0000754{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000755 PyObject *v;
756 PyObject *name;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000757
Guido van Rossum79f25d91997-04-29 20:08:16 +0000758 if (!PyArg_ParseTuple(args, "OS:hasattr", &v, &name))
Guido van Rossum9bfef441993-03-29 10:43:31 +0000759 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000760 v = PyObject_GetAttr(v, name);
Guido van Rossum9bfef441993-03-29 10:43:31 +0000761 if (v == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000762 PyErr_Clear();
Guido van Rossum09df08a1998-05-22 00:51:39 +0000763 Py_INCREF(Py_False);
764 return Py_False;
Guido van Rossum9bfef441993-03-29 10:43:31 +0000765 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000766 Py_DECREF(v);
Guido van Rossum09df08a1998-05-22 00:51:39 +0000767 Py_INCREF(Py_True);
768 return Py_True;
Guido van Rossum33894be1992-01-27 16:53:09 +0000769}
770
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000771static char hasattr_doc[] =
772"hasattr(object, name) -> Boolean\n\
773\n\
774Return whether the object has an attribute with the given name.\n\
775(This is done by calling getattr(object, name) and catching exceptions.)";
776
777
Guido van Rossum79f25d91997-04-29 20:08:16 +0000778static PyObject *
Guido van Rossum5b722181993-03-30 17:46:03 +0000779builtin_id(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000780 PyObject *self;
781 PyObject *args;
Guido van Rossum5b722181993-03-30 17:46:03 +0000782{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000783 PyObject *v;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000784
Guido van Rossum79f25d91997-04-29 20:08:16 +0000785 if (!PyArg_ParseTuple(args, "O:id", &v))
Guido van Rossum5b722181993-03-30 17:46:03 +0000786 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000787 return PyInt_FromLong((long)v);
Guido van Rossum5b722181993-03-30 17:46:03 +0000788}
789
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000790static char id_doc[] =
791"id(object) -> integer\n\
792\n\
793Return the identity of an object. This is guaranteed to be unique among\n\
794simultaneously existing objects. (Hint: it's the object's memory address.)";
795
796
Guido van Rossum79f25d91997-04-29 20:08:16 +0000797static PyObject *
Guido van Rossum12d12c51993-10-26 17:58:25 +0000798builtin_map(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000799 PyObject *self;
800 PyObject *args;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000801{
802 typedef struct {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000803 PyObject *seq;
804 PySequenceMethods *sqf;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000805 int len;
806 } sequence;
807
Guido van Rossum79f25d91997-04-29 20:08:16 +0000808 PyObject *func, *result;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000809 sequence *seqs = NULL, *sqp;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000810 int n, len;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000811 register int i, j;
812
Guido van Rossum79f25d91997-04-29 20:08:16 +0000813 n = PyTuple_Size(args);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000814 if (n < 2) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000815 PyErr_SetString(PyExc_TypeError,
816 "map() requires at least two args");
Guido van Rossum12d12c51993-10-26 17:58:25 +0000817 return NULL;
818 }
819
Guido van Rossum79f25d91997-04-29 20:08:16 +0000820 func = PyTuple_GetItem(args, 0);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000821 n--;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000822
Guido van Rossumfa4ac711998-07-10 17:37:30 +0000823 if (func == Py_None && n == 1) {
824 /* map(None, S) is the same as list(S). */
825 return PySequence_List(PyTuple_GetItem(args, 1));
826 }
827
Guido van Rossum79f25d91997-04-29 20:08:16 +0000828 if ((seqs = PyMem_NEW(sequence, n)) == NULL) {
829 PyErr_NoMemory();
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000830 goto Fail_2;
831 }
Guido van Rossum12d12c51993-10-26 17:58:25 +0000832
Guido van Rossum2d951851994-08-29 12:52:16 +0000833 for (len = 0, i = 0, sqp = seqs; i < n; ++i, ++sqp) {
Guido van Rossum12d12c51993-10-26 17:58:25 +0000834 int curlen;
Guido van Rossum09df08a1998-05-22 00:51:39 +0000835 PySequenceMethods *sqf;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000836
Guido van Rossum79f25d91997-04-29 20:08:16 +0000837 if ((sqp->seq = PyTuple_GetItem(args, i + 1)) == NULL)
Guido van Rossum12d12c51993-10-26 17:58:25 +0000838 goto Fail_2;
839
Guido van Rossum09df08a1998-05-22 00:51:39 +0000840 sqp->sqf = sqf = sqp->seq->ob_type->tp_as_sequence;
841 if (sqf == NULL ||
842 sqf->sq_length == NULL ||
843 sqf->sq_item == NULL)
844 {
Guido van Rossum12d12c51993-10-26 17:58:25 +0000845 static char errmsg[] =
846 "argument %d to map() must be a sequence object";
Guido van Rossum15e33a41997-04-30 19:00:27 +0000847 char errbuf[sizeof(errmsg) + 25];
Guido van Rossum12d12c51993-10-26 17:58:25 +0000848
849 sprintf(errbuf, errmsg, i+2);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000850 PyErr_SetString(PyExc_TypeError, errbuf);
Guido van Rossum12d12c51993-10-26 17:58:25 +0000851 goto Fail_2;
852 }
853
854 if ((curlen = sqp->len = (*sqp->sqf->sq_length)(sqp->seq)) < 0)
855 goto Fail_2;
856
857 if (curlen > len)
858 len = curlen;
859 }
860
Guido van Rossum79f25d91997-04-29 20:08:16 +0000861 if ((result = (PyObject *) PyList_New(len)) == NULL)
Guido van Rossum12d12c51993-10-26 17:58:25 +0000862 goto Fail_2;
863
Guido van Rossum2d951851994-08-29 12:52:16 +0000864 for (i = 0; ; ++i) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000865 PyObject *alist, *item=NULL, *value;
Guido van Rossum2d951851994-08-29 12:52:16 +0000866 int any = 0;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000867
Guido van Rossum79f25d91997-04-29 20:08:16 +0000868 if (func == Py_None && n == 1)
Guido van Rossum32120311995-07-10 13:52:21 +0000869 alist = NULL;
Guido van Rossum2d951851994-08-29 12:52:16 +0000870 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000871 if ((alist = PyTuple_New(n)) == NULL)
Guido van Rossum2d951851994-08-29 12:52:16 +0000872 goto Fail_1;
873 }
Guido van Rossum12d12c51993-10-26 17:58:25 +0000874
875 for (j = 0, sqp = seqs; j < n; ++j, ++sqp) {
Guido van Rossum2d951851994-08-29 12:52:16 +0000876 if (sqp->len < 0) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000877 Py_INCREF(Py_None);
878 item = Py_None;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000879 }
Guido van Rossum12d12c51993-10-26 17:58:25 +0000880 else {
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000881 item = (*sqp->sqf->sq_item)(sqp->seq, i);
Guido van Rossum2d951851994-08-29 12:52:16 +0000882 if (item == NULL) {
Barry Warsawcde8b1b1997-08-22 21:14:38 +0000883 if (PyErr_ExceptionMatches(
884 PyExc_IndexError))
885 {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000886 PyErr_Clear();
887 Py_INCREF(Py_None);
888 item = Py_None;
Guido van Rossum2d951851994-08-29 12:52:16 +0000889 sqp->len = -1;
890 }
891 else {
892 goto Fail_0;
893 }
894 }
895 else
896 any = 1;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000897
Guido van Rossum12d12c51993-10-26 17:58:25 +0000898 }
Guido van Rossum32120311995-07-10 13:52:21 +0000899 if (!alist)
Guido van Rossum2d951851994-08-29 12:52:16 +0000900 break;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000901 if (PyTuple_SetItem(alist, j, item) < 0) {
902 Py_DECREF(item);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000903 goto Fail_0;
Guido van Rossum2d951851994-08-29 12:52:16 +0000904 }
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000905 continue;
906
907 Fail_0:
Guido van Rossum79f25d91997-04-29 20:08:16 +0000908 Py_XDECREF(alist);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000909 goto Fail_1;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000910 }
911
Guido van Rossum32120311995-07-10 13:52:21 +0000912 if (!alist)
913 alist = item;
Guido van Rossum2d951851994-08-29 12:52:16 +0000914
915 if (!any) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000916 Py_DECREF(alist);
Guido van Rossum2d951851994-08-29 12:52:16 +0000917 break;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000918 }
Guido van Rossum2d951851994-08-29 12:52:16 +0000919
Guido van Rossum79f25d91997-04-29 20:08:16 +0000920 if (func == Py_None)
Guido van Rossum32120311995-07-10 13:52:21 +0000921 value = alist;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000922 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000923 value = PyEval_CallObject(func, alist);
924 Py_DECREF(alist);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000925 if (value == NULL)
926 goto Fail_1;
Guido van Rossum2d951851994-08-29 12:52:16 +0000927 }
928 if (i >= len) {
Barry Warsawfa77e091999-01-28 18:49:12 +0000929 int status = PyList_Append(result, value);
Barry Warsaw21332871999-01-28 04:21:35 +0000930 Py_DECREF(value);
Barry Warsawfa77e091999-01-28 18:49:12 +0000931 if (status < 0)
932 goto Fail_1;
Guido van Rossum2d951851994-08-29 12:52:16 +0000933 }
934 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000935 if (PyList_SetItem(result, i, value) < 0)
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000936 goto Fail_1;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000937 }
938 }
939
Guido van Rossumfa4ac711998-07-10 17:37:30 +0000940 if (i < len && PyList_SetSlice(result, i, len, NULL) < 0)
941 goto Fail_1;
942
Guido van Rossum79f25d91997-04-29 20:08:16 +0000943 PyMem_DEL(seqs);
Guido van Rossum12d12c51993-10-26 17:58:25 +0000944 return result;
945
Guido van Rossum12d12c51993-10-26 17:58:25 +0000946Fail_1:
Guido van Rossum79f25d91997-04-29 20:08:16 +0000947 Py_DECREF(result);
Guido van Rossum12d12c51993-10-26 17:58:25 +0000948Fail_2:
Guido van Rossum79f25d91997-04-29 20:08:16 +0000949 if (seqs) PyMem_DEL(seqs);
Guido van Rossum12d12c51993-10-26 17:58:25 +0000950 return NULL;
951}
952
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000953static char map_doc[] =
954"map(function, sequence[, sequence, ...]) -> list\n\
955\n\
956Return a list of the results of applying the function to the items of\n\
957the argument sequence(s). If more than one sequence is given, the\n\
958function is called with an argument list consisting of the corresponding\n\
959item of each sequence, substituting None for missing values when not all\n\
960sequences have the same length. If the function is None, return a list of\n\
961the items of the sequence (or a list of tuples if more than one sequence).";
962
963
Guido van Rossum79f25d91997-04-29 20:08:16 +0000964static PyObject *
Guido van Rossum94390a41992-08-14 15:14:30 +0000965builtin_setattr(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000966 PyObject *self;
967 PyObject *args;
Guido van Rossum33894be1992-01-27 16:53:09 +0000968{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000969 PyObject *v;
970 PyObject *name;
971 PyObject *value;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000972
Guido van Rossum79f25d91997-04-29 20:08:16 +0000973 if (!PyArg_ParseTuple(args, "OSO:setattr", &v, &name, &value))
Guido van Rossum33894be1992-01-27 16:53:09 +0000974 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000975 if (PyObject_SetAttr(v, name, value) != 0)
Guido van Rossum33894be1992-01-27 16:53:09 +0000976 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000977 Py_INCREF(Py_None);
978 return Py_None;
Guido van Rossum33894be1992-01-27 16:53:09 +0000979}
980
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000981static char setattr_doc[] =
982"setattr(object, name, value)\n\
983\n\
984Set a named attribute on an object; setattr(x, 'y', v) is equivalent to\n\
985``x.y = v''.";
986
987
Guido van Rossum79f25d91997-04-29 20:08:16 +0000988static PyObject *
Guido van Rossum14144fc1994-08-29 12:53:40 +0000989builtin_delattr(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000990 PyObject *self;
991 PyObject *args;
Guido van Rossum14144fc1994-08-29 12:53:40 +0000992{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000993 PyObject *v;
994 PyObject *name;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000995
Guido van Rossum79f25d91997-04-29 20:08:16 +0000996 if (!PyArg_ParseTuple(args, "OS:delattr", &v, &name))
Guido van Rossum14144fc1994-08-29 12:53:40 +0000997 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000998 if (PyObject_SetAttr(v, name, (PyObject *)NULL) != 0)
Guido van Rossum14144fc1994-08-29 12:53:40 +0000999 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001000 Py_INCREF(Py_None);
1001 return Py_None;
Guido van Rossum14144fc1994-08-29 12:53:40 +00001002}
1003
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001004static char delattr_doc[] =
Guido van Rossumdf12a591998-11-23 22:13:04 +00001005"delattr(object, name)\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001006\n\
1007Delete a named attribute on an object; delattr(x, 'y') is equivalent to\n\
1008``del x.y''.";
1009
1010
Guido van Rossum79f25d91997-04-29 20:08:16 +00001011static PyObject *
Guido van Rossum9bfef441993-03-29 10:43:31 +00001012builtin_hash(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001013 PyObject *self;
1014 PyObject *args;
Guido van Rossum9bfef441993-03-29 10:43:31 +00001015{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001016 PyObject *v;
Guido van Rossum9bfef441993-03-29 10:43:31 +00001017 long x;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001018
Guido van Rossum79f25d91997-04-29 20:08:16 +00001019 if (!PyArg_ParseTuple(args, "O:hash", &v))
Guido van Rossum9bfef441993-03-29 10:43:31 +00001020 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001021 x = PyObject_Hash(v);
Guido van Rossum9bfef441993-03-29 10:43:31 +00001022 if (x == -1)
1023 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001024 return PyInt_FromLong(x);
Guido van Rossum9bfef441993-03-29 10:43:31 +00001025}
1026
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001027static char hash_doc[] =
1028"hash(object) -> integer\n\
1029\n\
1030Return a hash value for the object. Two objects with the same value have\n\
1031the same hash value. The reverse is not necessarily true, but likely.";
1032
1033
Guido van Rossum79f25d91997-04-29 20:08:16 +00001034static PyObject *
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001035builtin_hex(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001036 PyObject *self;
1037 PyObject *args;
Guido van Rossum006bcd41991-10-24 14:54:44 +00001038{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001039 PyObject *v;
1040 PyNumberMethods *nb;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001041
Guido van Rossum79f25d91997-04-29 20:08:16 +00001042 if (!PyArg_ParseTuple(args, "O:hex", &v))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001043 return NULL;
Guido van Rossum1899c2e1992-09-12 11:09:23 +00001044
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001045 if ((nb = v->ob_type->tp_as_number) == NULL ||
Guido van Rossum1899c2e1992-09-12 11:09:23 +00001046 nb->nb_hex == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001047 PyErr_SetString(PyExc_TypeError,
Guido van Rossum1899c2e1992-09-12 11:09:23 +00001048 "hex() argument can't be converted to hex");
1049 return NULL;
Guido van Rossum006bcd41991-10-24 14:54:44 +00001050 }
Guido van Rossum1899c2e1992-09-12 11:09:23 +00001051 return (*nb->nb_hex)(v);
Guido van Rossum006bcd41991-10-24 14:54:44 +00001052}
1053
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001054static char hex_doc[] =
1055"hex(number) -> string\n\
1056\n\
1057Return the hexadecimal representation of an integer or long integer.";
1058
1059
Guido van Rossum79f25d91997-04-29 20:08:16 +00001060static PyObject *builtin_raw_input Py_PROTO((PyObject *, PyObject *));
Guido van Rossum3165fe61992-09-25 21:59:05 +00001061
Guido van Rossum79f25d91997-04-29 20:08:16 +00001062static PyObject *
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001063builtin_input(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001064 PyObject *self;
1065 PyObject *args;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001066{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001067 PyObject *line;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001068 char *str;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001069 PyObject *res;
1070 PyObject *globals, *locals;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001071
1072 line = builtin_raw_input(self, args);
Guido van Rossum3165fe61992-09-25 21:59:05 +00001073 if (line == NULL)
1074 return line;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001075 if (!PyArg_Parse(line, "s;embedded '\\0' in input line", &str))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001076 return NULL;
1077 while (*str == ' ' || *str == '\t')
1078 str++;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001079 globals = PyEval_GetGlobals();
1080 locals = PyEval_GetLocals();
1081 if (PyDict_GetItemString(globals, "__builtins__") == NULL) {
1082 if (PyDict_SetItemString(globals, "__builtins__",
1083 PyEval_GetBuiltins()) != 0)
Guido van Rossum6135a871995-01-09 17:53:26 +00001084 return NULL;
1085 }
Guido van Rossumb05a5c71997-05-07 17:46:13 +00001086 res = PyRun_String(str, Py_eval_input, globals, locals);
Guido van Rossum79f25d91997-04-29 20:08:16 +00001087 Py_DECREF(line);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001088 return res;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001089}
1090
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001091static char input_doc[] =
1092"input([prompt]) -> value\n\
1093\n\
1094Equivalent to eval(raw_input(prompt)).";
1095
1096
Guido van Rossume8811f81997-02-14 15:48:05 +00001097static PyObject *
1098builtin_intern(self, args)
1099 PyObject *self;
1100 PyObject *args;
1101{
1102 PyObject *s;
1103 if (!PyArg_ParseTuple(args, "S", &s))
1104 return NULL;
1105 Py_INCREF(s);
1106 PyString_InternInPlace(&s);
1107 return s;
1108}
1109
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001110static char intern_doc[] =
1111"intern(string) -> string\n\
1112\n\
1113``Intern'' the given string. This enters the string in the (global)\n\
1114table of interned strings whose purpose is to speed up dictionary lookups.\n\
1115Return the string itself or the previously interned string object with the\n\
1116same value.";
1117
1118
Guido van Rossum79f25d91997-04-29 20:08:16 +00001119static PyObject *
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001120builtin_int(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001121 PyObject *self;
1122 PyObject *args;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001123{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001124 PyObject *v;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001125
Guido van Rossum79f25d91997-04-29 20:08:16 +00001126 if (!PyArg_ParseTuple(args, "O:int", &v))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001127 return NULL;
Guido van Rossum09df08a1998-05-22 00:51:39 +00001128 return PyNumber_Int(v);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001129}
1130
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001131static char int_doc[] =
1132"int(x) -> integer\n\
1133\n\
1134Convert a string or number to an integer, if possible.\n\
1135A floating point argument will be truncated towards zero.";
1136
1137
Guido van Rossum79f25d91997-04-29 20:08:16 +00001138static PyObject *
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001139builtin_len(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001140 PyObject *self;
1141 PyObject *args;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001142{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001143 PyObject *v;
Guido van Rossum8ea9f4d1998-06-29 22:26:50 +00001144 long res;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001145
Guido van Rossum79f25d91997-04-29 20:08:16 +00001146 if (!PyArg_ParseTuple(args, "O:len", &v))
Guido van Rossum3f5da241990-12-20 15:06:42 +00001147 return NULL;
Guido van Rossum8ea9f4d1998-06-29 22:26:50 +00001148 res = PyObject_Length(v);
1149 if (res < 0 && PyErr_Occurred())
1150 return NULL;
1151 return PyInt_FromLong(res);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001152}
1153
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001154static char len_doc[] =
1155"len(object) -> integer\n\
1156\n\
1157Return the number of items of a sequence or mapping.";
1158
1159
Guido van Rossum79f25d91997-04-29 20:08:16 +00001160static PyObject *
Guido van Rossumd1705771996-04-09 02:41:06 +00001161builtin_list(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001162 PyObject *self;
1163 PyObject *args;
Guido van Rossumd1705771996-04-09 02:41:06 +00001164{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001165 PyObject *v;
Guido van Rossumd1705771996-04-09 02:41:06 +00001166
Guido van Rossum79f25d91997-04-29 20:08:16 +00001167 if (!PyArg_ParseTuple(args, "O:list", &v))
Guido van Rossumd1705771996-04-09 02:41:06 +00001168 return NULL;
Guido van Rossum09df08a1998-05-22 00:51:39 +00001169 return PySequence_List(v);
Guido van Rossumd1705771996-04-09 02:41:06 +00001170}
1171
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001172static char list_doc[] =
1173"list(sequence) -> list\n\
1174\n\
1175Return a new list whose items are the same as those of the argument sequence.";
1176
Guido van Rossum8861b741996-07-30 16:49:37 +00001177
1178static PyObject *
1179builtin_slice(self, args)
1180 PyObject *self;
1181 PyObject *args;
1182{
Guido van Rossum09df08a1998-05-22 00:51:39 +00001183 PyObject *start, *stop, *step;
Guido van Rossum8861b741996-07-30 16:49:37 +00001184
Guido van Rossum09df08a1998-05-22 00:51:39 +00001185 start = stop = step = NULL;
Guido van Rossum8861b741996-07-30 16:49:37 +00001186
Guido van Rossum09df08a1998-05-22 00:51:39 +00001187 if (!PyArg_ParseTuple(args, "O|OO:slice", &start, &stop, &step))
1188 return NULL;
Guido van Rossum8861b741996-07-30 16:49:37 +00001189
Guido van Rossum09df08a1998-05-22 00:51:39 +00001190 /* This swapping of stop and start is to maintain similarity with
1191 range(). */
1192 if (stop == NULL) {
1193 stop = start;
1194 start = NULL;
1195 }
1196 return PySlice_New(start, stop, step);
Guido van Rossum8861b741996-07-30 16:49:37 +00001197}
1198
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001199static char slice_doc[] =
1200"slice([start,] step[, stop]) -> slice object\n\
1201\n\
1202Create a slice object. This is used for slicing by the Numeric extensions.";
1203
1204
Guido van Rossum79f25d91997-04-29 20:08:16 +00001205static PyObject *
Guido van Rossum872537c1995-07-07 22:43:42 +00001206builtin_locals(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001207 PyObject *self;
1208 PyObject *args;
Guido van Rossum872537c1995-07-07 22:43:42 +00001209{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001210 PyObject *d;
Guido van Rossum872537c1995-07-07 22:43:42 +00001211
Guido van Rossum79f25d91997-04-29 20:08:16 +00001212 if (!PyArg_ParseTuple(args, ""))
Guido van Rossum872537c1995-07-07 22:43:42 +00001213 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001214 d = PyEval_GetLocals();
1215 Py_INCREF(d);
Guido van Rossum872537c1995-07-07 22:43:42 +00001216 return d;
1217}
1218
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001219static char locals_doc[] =
1220"locals() -> dictionary\n\
1221\n\
1222Return the dictionary containing the current scope's local variables.";
1223
1224
Guido van Rossum79f25d91997-04-29 20:08:16 +00001225static PyObject *
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001226builtin_long(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001227 PyObject *self;
1228 PyObject *args;
Guido van Rossumd4905451991-05-05 20:00:36 +00001229{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001230 PyObject *v;
Guido van Rossum1899c2e1992-09-12 11:09:23 +00001231
Guido van Rossum79f25d91997-04-29 20:08:16 +00001232 if (!PyArg_ParseTuple(args, "O:long", &v))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001233 return NULL;
Guido van Rossum09df08a1998-05-22 00:51:39 +00001234 return PyNumber_Long(v);
Guido van Rossumd4905451991-05-05 20:00:36 +00001235}
1236
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001237static char long_doc[] =
1238"long(x) -> long integer\n\
1239\n\
1240Convert a string or number to a long integer, if possible.\n\
1241A floating point argument will be truncated towards zero.";
1242
1243
Guido van Rossum79f25d91997-04-29 20:08:16 +00001244static PyObject *
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001245min_max(args, sign)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001246 PyObject *args;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001247 int sign;
1248{
Guido van Rossum2d951851994-08-29 12:52:16 +00001249 int i;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001250 PyObject *v, *w, *x;
1251 PySequenceMethods *sq;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001252
Guido van Rossum79f25d91997-04-29 20:08:16 +00001253 if (PyTuple_Size(args) > 1)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001254 v = args;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001255 else if (!PyArg_ParseTuple(args, "O:min/max", &v))
Guido van Rossum3f5da241990-12-20 15:06:42 +00001256 return NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001257 sq = v->ob_type->tp_as_sequence;
Guido van Rossum09df08a1998-05-22 00:51:39 +00001258 if (sq == NULL || sq->sq_item == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001259 PyErr_SetString(PyExc_TypeError,
1260 "min() or max() of non-sequence");
Guido van Rossum3f5da241990-12-20 15:06:42 +00001261 return NULL;
1262 }
Guido van Rossum2d951851994-08-29 12:52:16 +00001263 w = NULL;
1264 for (i = 0; ; i++) {
Guido van Rossum3f5da241990-12-20 15:06:42 +00001265 x = (*sq->sq_item)(v, i); /* Implies INCREF */
Guido van Rossum2d951851994-08-29 12:52:16 +00001266 if (x == NULL) {
Barry Warsawcde8b1b1997-08-22 21:14:38 +00001267 if (PyErr_ExceptionMatches(PyExc_IndexError)) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001268 PyErr_Clear();
Guido van Rossum2d951851994-08-29 12:52:16 +00001269 break;
1270 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00001271 Py_XDECREF(w);
Guido van Rossum2d951851994-08-29 12:52:16 +00001272 return NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001273 }
Guido van Rossum2d951851994-08-29 12:52:16 +00001274 if (w == NULL)
1275 w = x;
1276 else {
Guido van Rossumc8b6df91997-05-23 00:06:51 +00001277 int c = PyObject_Compare(x, w);
1278 if (c && PyErr_Occurred()) {
1279 Py_DECREF(x);
1280 Py_XDECREF(w);
1281 return NULL;
1282 }
1283 if (c * sign > 0) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001284 Py_DECREF(w);
Guido van Rossum2d951851994-08-29 12:52:16 +00001285 w = x;
1286 }
1287 else
Guido van Rossum79f25d91997-04-29 20:08:16 +00001288 Py_DECREF(x);
Guido van Rossum2d951851994-08-29 12:52:16 +00001289 }
Guido van Rossum3f5da241990-12-20 15:06:42 +00001290 }
Guido van Rossum2d951851994-08-29 12:52:16 +00001291 if (w == NULL)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001292 PyErr_SetString(PyExc_ValueError,
1293 "min() or max() of empty sequence");
Guido van Rossum3f5da241990-12-20 15:06:42 +00001294 return w;
1295}
1296
Guido van Rossum79f25d91997-04-29 20:08:16 +00001297static PyObject *
Guido van Rossum3f5da241990-12-20 15:06:42 +00001298builtin_min(self, v)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001299 PyObject *self;
1300 PyObject *v;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001301{
1302 return min_max(v, -1);
1303}
1304
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001305static char min_doc[] =
1306"min(sequence) -> value\n\
1307min(a, b, c, ...) -> value\n\
1308\n\
1309With a single sequence argument, return its smallest item.\n\
1310With two or more arguments, return the smallest argument.";
1311
1312
Guido van Rossum79f25d91997-04-29 20:08:16 +00001313static PyObject *
Guido van Rossum3f5da241990-12-20 15:06:42 +00001314builtin_max(self, v)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001315 PyObject *self;
1316 PyObject *v;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001317{
1318 return min_max(v, 1);
1319}
1320
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001321static char max_doc[] =
1322"max(sequence) -> value\n\
1323max(a, b, c, ...) -> value\n\
1324\n\
1325With a single sequence argument, return its largest item.\n\
1326With two or more arguments, return the largest argument.";
1327
1328
Guido van Rossum79f25d91997-04-29 20:08:16 +00001329static PyObject *
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001330builtin_oct(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001331 PyObject *self;
1332 PyObject *args;
Guido van Rossum006bcd41991-10-24 14:54:44 +00001333{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001334 PyObject *v;
1335 PyNumberMethods *nb;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001336
Guido van Rossum79f25d91997-04-29 20:08:16 +00001337 if (!PyArg_ParseTuple(args, "O:oct", &v))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001338 return NULL;
Guido van Rossum1899c2e1992-09-12 11:09:23 +00001339 if (v == NULL || (nb = v->ob_type->tp_as_number) == NULL ||
1340 nb->nb_oct == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001341 PyErr_SetString(PyExc_TypeError,
Guido van Rossum1899c2e1992-09-12 11:09:23 +00001342 "oct() argument can't be converted to oct");
1343 return NULL;
Guido van Rossum006bcd41991-10-24 14:54:44 +00001344 }
Guido van Rossum1899c2e1992-09-12 11:09:23 +00001345 return (*nb->nb_oct)(v);
Guido van Rossum006bcd41991-10-24 14:54:44 +00001346}
1347
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001348static char oct_doc[] =
1349"oct(number) -> string\n\
1350\n\
1351Return the octal representation of an integer or long integer.";
1352
1353
Guido van Rossum79f25d91997-04-29 20:08:16 +00001354static PyObject *
Guido van Rossum94390a41992-08-14 15:14:30 +00001355builtin_open(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001356 PyObject *self;
1357 PyObject *args;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001358{
Guido van Rossum2d951851994-08-29 12:52:16 +00001359 char *name;
1360 char *mode = "r";
1361 int bufsize = -1;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001362 PyObject *f;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001363
Guido van Rossum79f25d91997-04-29 20:08:16 +00001364 if (!PyArg_ParseTuple(args, "s|si:open", &name, &mode, &bufsize))
Guido van Rossum3f5da241990-12-20 15:06:42 +00001365 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001366 f = PyFile_FromString(name, mode);
Guido van Rossum2d951851994-08-29 12:52:16 +00001367 if (f != NULL)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001368 PyFile_SetBufSize(f, bufsize);
Guido van Rossum2d951851994-08-29 12:52:16 +00001369 return f;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001370}
1371
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001372static char open_doc[] =
1373"open(filename[, mode[, buffering]]) -> file object\n\
1374\n\
1375Open a file. The mode can be 'r', 'w' or 'a' for reading (default),\n\
1376writing or appending. The file will be created if it doesn't exist\n\
1377when opened for writing or appending; it will be truncated when\n\
1378opened for writing. Add a 'b' to the mode for binary files.\n\
1379Add a '+' to the mode to allow simultaneous reading and writing.\n\
1380If the buffering argument is given, 0 means unbuffered, 1 means line\n\
1381buffered, and larger numbers specify the buffer size.";
1382
1383
Guido van Rossum79f25d91997-04-29 20:08:16 +00001384static PyObject *
Guido van Rossum94390a41992-08-14 15:14:30 +00001385builtin_ord(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001386 PyObject *self;
1387 PyObject *args;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001388{
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001389 char c;
1390
Guido van Rossum79f25d91997-04-29 20:08:16 +00001391 if (!PyArg_ParseTuple(args, "c:ord", &c))
Guido van Rossum3f5da241990-12-20 15:06:42 +00001392 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001393 return PyInt_FromLong((long)(c & 0xff));
Guido van Rossum3f5da241990-12-20 15:06:42 +00001394}
1395
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001396static char ord_doc[] =
1397"ord(c) -> integer\n\
1398\n\
1399Return the integer ordinal of a one character string.";
1400
1401
Guido van Rossum79f25d91997-04-29 20:08:16 +00001402static PyObject *
Guido van Rossum6a00cd81995-01-07 12:39:01 +00001403builtin_pow(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001404 PyObject *self;
1405 PyObject *args;
Guido van Rossum6a00cd81995-01-07 12:39:01 +00001406{
Guido van Rossum09df08a1998-05-22 00:51:39 +00001407 PyObject *v, *w, *z = Py_None;
Guido van Rossum6a00cd81995-01-07 12:39:01 +00001408
Guido van Rossum79f25d91997-04-29 20:08:16 +00001409 if (!PyArg_ParseTuple(args, "OO|O:pow", &v, &w, &z))
Guido van Rossum6a00cd81995-01-07 12:39:01 +00001410 return NULL;
Guido van Rossum09df08a1998-05-22 00:51:39 +00001411 return PyNumber_Power(v, w, z);
Guido van Rossumd4905451991-05-05 20:00:36 +00001412}
1413
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001414static char pow_doc[] =
1415"pow(x, y[, z]) -> number\n\
1416\n\
1417With two arguments, equivalent to x**y. With three arguments,\n\
1418equivalent to (x**y) % z, but may be more efficient (e.g. for longs).";
1419
1420
Guido van Rossum124eff01999-02-23 16:11:01 +00001421/* Return number of items in range/xrange (lo, hi, step). step > 0
1422 * required. Return a value < 0 if & only if the true value is too
1423 * large to fit in a signed long.
1424 */
1425static long
1426get_len_of_range(lo, hi, step)
1427 long lo;
1428 long hi;
1429 long step; /* must be > 0 */
1430{
1431 /* -------------------------------------------------------------
1432 If lo >= hi, the range is empty.
1433 Else if n values are in the range, the last one is
1434 lo + (n-1)*step, which must be <= hi-1. Rearranging,
1435 n <= (hi - lo - 1)/step + 1, so taking the floor of the RHS gives
1436 the proper value. Since lo < hi in this case, hi-lo-1 >= 0, so
1437 the RHS is non-negative and so truncation is the same as the
1438 floor. Letting M be the largest positive long, the worst case
1439 for the RHS numerator is hi=M, lo=-M-1, and then
1440 hi-lo-1 = M-(-M-1)-1 = 2*M. Therefore unsigned long has enough
1441 precision to compute the RHS exactly.
1442 ---------------------------------------------------------------*/
1443 long n = 0;
1444 if (lo < hi) {
1445 unsigned long uhi = (unsigned long)hi;
1446 unsigned long ulo = (unsigned long)lo;
1447 unsigned long diff = uhi - ulo - 1;
1448 n = (long)(diff / (unsigned long)step + 1);
1449 }
1450 return n;
1451}
1452
Guido van Rossum79f25d91997-04-29 20:08:16 +00001453static PyObject *
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001454builtin_range(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001455 PyObject *self;
1456 PyObject *args;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001457{
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001458 long ilow = 0, ihigh = 0, istep = 1;
Guido van Rossum124eff01999-02-23 16:11:01 +00001459 long bign;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001460 int i, n;
Guido van Rossum124eff01999-02-23 16:11:01 +00001461
Guido van Rossum79f25d91997-04-29 20:08:16 +00001462 PyObject *v;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001463
Guido van Rossum79f25d91997-04-29 20:08:16 +00001464 if (PyTuple_Size(args) <= 1) {
1465 if (!PyArg_ParseTuple(args,
Guido van Rossum0865dd91995-01-17 16:30:22 +00001466 "l;range() requires 1-3 int arguments",
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001467 &ihigh))
1468 return NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001469 }
1470 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001471 if (!PyArg_ParseTuple(args,
Guido van Rossum0865dd91995-01-17 16:30:22 +00001472 "ll|l;range() requires 1-3 int arguments",
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001473 &ilow, &ihigh, &istep))
Guido van Rossum3f5da241990-12-20 15:06:42 +00001474 return NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001475 }
1476 if (istep == 0) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001477 PyErr_SetString(PyExc_ValueError, "zero step for range()");
Guido van Rossum3f5da241990-12-20 15:06:42 +00001478 return NULL;
1479 }
Guido van Rossum124eff01999-02-23 16:11:01 +00001480 if (istep > 0)
1481 bign = get_len_of_range(ilow, ihigh, istep);
1482 else
1483 bign = get_len_of_range(ihigh, ilow, -istep);
1484 n = (int)bign;
1485 if (bign < 0 || (long)n != bign) {
Guido van Rossume23cde21999-01-12 05:07:47 +00001486 PyErr_SetString(PyExc_OverflowError,
Guido van Rossum124eff01999-02-23 16:11:01 +00001487 "range() has too many items");
Guido van Rossume23cde21999-01-12 05:07:47 +00001488 return NULL;
1489 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00001490 v = PyList_New(n);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001491 if (v == NULL)
1492 return NULL;
1493 for (i = 0; i < n; i++) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001494 PyObject *w = PyInt_FromLong(ilow);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001495 if (w == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001496 Py_DECREF(v);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001497 return NULL;
1498 }
Guido van Rossuma937d141998-04-24 18:22:02 +00001499 PyList_SET_ITEM(v, i, w);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001500 ilow += istep;
1501 }
1502 return v;
1503}
1504
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001505static char range_doc[] =
1506"range([start,] stop[, step]) -> list of integers\n\
1507\n\
1508Return a list containing an arithmetic progression of integers.\n\
1509range(i, j) returns [i, i+1, i+2, ..., j-1]; start (!) defaults to 0.\n\
1510When step is given, it specifies the increment (or decrement).\n\
1511For example, range(4) returns [0, 1, 2, 3]. The end point is omitted!\n\
1512These are exactly the valid indices for a list of 4 elements.";
1513
1514
Guido van Rossum79f25d91997-04-29 20:08:16 +00001515static PyObject *
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001516builtin_xrange(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001517 PyObject *self;
1518 PyObject *args;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001519{
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001520 long ilow = 0, ihigh = 0, istep = 1;
Guido van Rossum0865dd91995-01-17 16:30:22 +00001521 long n;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001522
Guido van Rossum79f25d91997-04-29 20:08:16 +00001523 if (PyTuple_Size(args) <= 1) {
1524 if (!PyArg_ParseTuple(args,
Guido van Rossum0865dd91995-01-17 16:30:22 +00001525 "l;xrange() requires 1-3 int arguments",
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001526 &ihigh))
1527 return NULL;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001528 }
1529 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001530 if (!PyArg_ParseTuple(args,
Guido van Rossum0865dd91995-01-17 16:30:22 +00001531 "ll|l;xrange() requires 1-3 int arguments",
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001532 &ilow, &ihigh, &istep))
Guido van Rossum12d12c51993-10-26 17:58:25 +00001533 return NULL;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001534 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001535 if (istep == 0) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001536 PyErr_SetString(PyExc_ValueError, "zero step for xrange()");
Guido van Rossum12d12c51993-10-26 17:58:25 +00001537 return NULL;
1538 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001539 if (istep > 0)
Guido van Rossum124eff01999-02-23 16:11:01 +00001540 n = get_len_of_range(ilow, ihigh, istep);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001541 else
Guido van Rossum124eff01999-02-23 16:11:01 +00001542 n = get_len_of_range(ihigh, ilow, -istep);
1543 if (n < 0) {
1544 PyErr_SetString(PyExc_OverflowError,
1545 "xrange() has more than sys.maxint items");
1546 return NULL;
1547 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00001548 return PyRange_New(ilow, n, istep, 1);
Guido van Rossum12d12c51993-10-26 17:58:25 +00001549}
1550
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001551static char xrange_doc[] =
1552"xrange([start,] stop[, step]) -> xrange object\n\
1553\n\
1554Like range(), but instead of returning a list, returns an object that\n\
1555generates the numbers in the range on demand. This is slightly slower\n\
1556than range() but more memory efficient.";
1557
1558
Guido van Rossum79f25d91997-04-29 20:08:16 +00001559static PyObject *
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001560builtin_raw_input(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001561 PyObject *self;
1562 PyObject *args;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001563{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001564 PyObject *v = NULL;
1565 PyObject *f;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001566
Guido van Rossum79f25d91997-04-29 20:08:16 +00001567 if (!PyArg_ParseTuple(args, "|O:[raw_]input", &v))
Guido van Rossum3165fe61992-09-25 21:59:05 +00001568 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001569 if (PyFile_AsFile(PySys_GetObject("stdin")) == stdin &&
1570 PyFile_AsFile(PySys_GetObject("stdout")) == stdout &&
Guido van Rossum53bb7ff1995-07-26 16:26:31 +00001571 isatty(fileno(stdin)) && isatty(fileno(stdout))) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001572 PyObject *po;
Guido van Rossum872537c1995-07-07 22:43:42 +00001573 char *prompt;
1574 char *s;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001575 PyObject *result;
Guido van Rossum872537c1995-07-07 22:43:42 +00001576 if (v != NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001577 po = PyObject_Str(v);
Guido van Rossum872537c1995-07-07 22:43:42 +00001578 if (po == NULL)
1579 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001580 prompt = PyString_AsString(po);
Guido van Rossumd9b52081998-06-26 18:25:38 +00001581 if (prompt == NULL)
1582 return NULL;
Guido van Rossum872537c1995-07-07 22:43:42 +00001583 }
1584 else {
1585 po = NULL;
1586 prompt = "";
1587 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00001588 s = PyOS_Readline(prompt);
1589 Py_XDECREF(po);
Guido van Rossum872537c1995-07-07 22:43:42 +00001590 if (s == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001591 PyErr_SetNone(PyExc_KeyboardInterrupt);
Guido van Rossum872537c1995-07-07 22:43:42 +00001592 return NULL;
1593 }
1594 if (*s == '\0') {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001595 PyErr_SetNone(PyExc_EOFError);
Guido van Rossum872537c1995-07-07 22:43:42 +00001596 result = NULL;
1597 }
1598 else { /* strip trailing '\n' */
Guido van Rossum79f25d91997-04-29 20:08:16 +00001599 result = PyString_FromStringAndSize(s, strlen(s)-1);
Guido van Rossum872537c1995-07-07 22:43:42 +00001600 }
1601 free(s);
1602 return result;
1603 }
Guido van Rossum90933611991-06-07 16:10:43 +00001604 if (v != NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001605 f = PySys_GetObject("stdout");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001606 if (f == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001607 PyErr_SetString(PyExc_RuntimeError, "lost sys.stdout");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001608 return NULL;
1609 }
Guido van Rossumc8b6df91997-05-23 00:06:51 +00001610 if (Py_FlushLine() != 0 ||
1611 PyFile_WriteObject(v, f, Py_PRINT_RAW) != 0)
Guido van Rossum90933611991-06-07 16:10:43 +00001612 return NULL;
1613 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00001614 f = PySys_GetObject("stdin");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001615 if (f == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001616 PyErr_SetString(PyExc_RuntimeError, "lost sys.stdin");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001617 return NULL;
1618 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00001619 return PyFile_GetLine(f, -1);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001620}
1621
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001622static char raw_input_doc[] =
1623"raw_input([prompt]) -> string\n\
1624\n\
1625Read a string from standard input. The trailing newline is stripped.\n\
1626If the user hits EOF (Unix: Ctl-D, Windows: Ctl-Z+Return), raise EOFError.\n\
1627On Unix, GNU readline is used if enabled. The prompt string, if given,\n\
1628is printed without a trailing newline before reading.";
1629
1630
Guido van Rossum79f25d91997-04-29 20:08:16 +00001631static PyObject *
Guido van Rossum12d12c51993-10-26 17:58:25 +00001632builtin_reduce(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001633 PyObject *self;
1634 PyObject *args;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001635{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001636 PyObject *seq, *func, *result = NULL;
1637 PySequenceMethods *sqf;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001638 register int i;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001639
Guido van Rossum79f25d91997-04-29 20:08:16 +00001640 if (!PyArg_ParseTuple(args, "OO|O:reduce", &func, &seq, &result))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001641 return NULL;
1642 if (result != NULL)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001643 Py_INCREF(result);
Guido van Rossum12d12c51993-10-26 17:58:25 +00001644
Guido van Rossum09df08a1998-05-22 00:51:39 +00001645 sqf = seq->ob_type->tp_as_sequence;
1646 if (sqf == NULL || sqf->sq_item == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001647 PyErr_SetString(PyExc_TypeError,
Guido van Rossum12d12c51993-10-26 17:58:25 +00001648 "2nd argument to reduce() must be a sequence object");
1649 return NULL;
1650 }
1651
Guido van Rossum79f25d91997-04-29 20:08:16 +00001652 if ((args = PyTuple_New(2)) == NULL)
Guido van Rossum2d951851994-08-29 12:52:16 +00001653 goto Fail;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001654
Guido van Rossum2d951851994-08-29 12:52:16 +00001655 for (i = 0; ; ++i) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001656 PyObject *op2;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001657
1658 if (args->ob_refcnt > 1) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001659 Py_DECREF(args);
1660 if ((args = PyTuple_New(2)) == NULL)
Guido van Rossum2d951851994-08-29 12:52:16 +00001661 goto Fail;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001662 }
1663
Guido van Rossum2d951851994-08-29 12:52:16 +00001664 if ((op2 = (*sqf->sq_item)(seq, i)) == NULL) {
Barry Warsawcde8b1b1997-08-22 21:14:38 +00001665 if (PyErr_ExceptionMatches(PyExc_IndexError)) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001666 PyErr_Clear();
Guido van Rossum2d951851994-08-29 12:52:16 +00001667 break;
1668 }
1669 goto Fail;
1670 }
Guido van Rossum12d12c51993-10-26 17:58:25 +00001671
Guido van Rossum2d951851994-08-29 12:52:16 +00001672 if (result == NULL)
1673 result = op2;
1674 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001675 PyTuple_SetItem(args, 0, result);
1676 PyTuple_SetItem(args, 1, op2);
1677 if ((result = PyEval_CallObject(func, args)) == NULL)
Guido van Rossum2d951851994-08-29 12:52:16 +00001678 goto Fail;
1679 }
Guido van Rossum12d12c51993-10-26 17:58:25 +00001680 }
1681
Guido van Rossum79f25d91997-04-29 20:08:16 +00001682 Py_DECREF(args);
Guido van Rossum12d12c51993-10-26 17:58:25 +00001683
Guido van Rossum2d951851994-08-29 12:52:16 +00001684 if (result == NULL)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001685 PyErr_SetString(PyExc_TypeError,
Guido van Rossum2d951851994-08-29 12:52:16 +00001686 "reduce of empty sequence with no initial value");
1687
Guido van Rossum12d12c51993-10-26 17:58:25 +00001688 return result;
1689
Guido van Rossum2d951851994-08-29 12:52:16 +00001690Fail:
Guido van Rossum79f25d91997-04-29 20:08:16 +00001691 Py_XDECREF(args);
1692 Py_XDECREF(result);
Guido van Rossum12d12c51993-10-26 17:58:25 +00001693 return NULL;
1694}
1695
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001696static char reduce_doc[] =
1697"reduce(function, sequence[, initial]) -> value\n\
1698\n\
1699Apply a function of two arguments cumulatively to the items of a sequence,\n\
1700from left to right, so as to reduce the sequence to a single value.\n\
1701For example, reduce(lambda x, y: x+y, [1, 2, 3, 4, 5]) calculates\n\
1702((((1+2)+3)+4)+5). If initial is present, it is placed before the items\n\
1703of the sequence in the calculation, and serves as a default when the\n\
1704sequence is empty.";
1705
1706
Guido van Rossum79f25d91997-04-29 20:08:16 +00001707static PyObject *
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001708builtin_reload(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001709 PyObject *self;
1710 PyObject *args;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001711{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001712 PyObject *v;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001713
Guido van Rossum79f25d91997-04-29 20:08:16 +00001714 if (!PyArg_ParseTuple(args, "O:reload", &v))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001715 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001716 return PyImport_ReloadModule(v);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001717}
1718
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001719static char reload_doc[] =
1720"reload(module) -> module\n\
1721\n\
1722Reload the module. The module must have been successfully imported before.";
1723
1724
Guido van Rossum79f25d91997-04-29 20:08:16 +00001725static PyObject *
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001726builtin_repr(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001727 PyObject *self;
1728 PyObject *args;
Guido van Rossumc89705d1992-11-26 08:54:07 +00001729{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001730 PyObject *v;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001731
Guido van Rossum79f25d91997-04-29 20:08:16 +00001732 if (!PyArg_ParseTuple(args, "O:repr", &v))
Guido van Rossumc89705d1992-11-26 08:54:07 +00001733 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001734 return PyObject_Repr(v);
Guido van Rossumc89705d1992-11-26 08:54:07 +00001735}
1736
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001737static char repr_doc[] =
1738"repr(object) -> string\n\
1739\n\
1740Return the canonical string representation of the object.\n\
1741For most object types, eval(repr(object)) == object.";
1742
1743
Guido van Rossum79f25d91997-04-29 20:08:16 +00001744static PyObject *
Guido van Rossum9e51f9b1993-02-12 16:29:05 +00001745builtin_round(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001746 PyObject *self;
1747 PyObject *args;
Guido van Rossum9e51f9b1993-02-12 16:29:05 +00001748{
Guido van Rossum9e51f9b1993-02-12 16:29:05 +00001749 double x;
1750 double f;
1751 int ndigits = 0;
Guido van Rossum9e51f9b1993-02-12 16:29:05 +00001752 int i;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001753
Guido van Rossum79f25d91997-04-29 20:08:16 +00001754 if (!PyArg_ParseTuple(args, "d|i:round", &x, &ndigits))
Guido van Rossum9e51f9b1993-02-12 16:29:05 +00001755 return NULL;
Guido van Rossum9e51f9b1993-02-12 16:29:05 +00001756 f = 1.0;
Guido van Rossum1e162d31998-05-09 14:42:25 +00001757 i = abs(ndigits);
1758 while (--i >= 0)
Guido van Rossum9e51f9b1993-02-12 16:29:05 +00001759 f = f*10.0;
Guido van Rossum1e162d31998-05-09 14:42:25 +00001760 if (ndigits < 0)
1761 x /= f;
Guido van Rossum9e51f9b1993-02-12 16:29:05 +00001762 else
Guido van Rossum1e162d31998-05-09 14:42:25 +00001763 x *= f;
1764 if (x >= 0.0)
1765 x = floor(x + 0.5);
1766 else
1767 x = ceil(x - 0.5);
1768 if (ndigits < 0)
1769 x *= f;
1770 else
1771 x /= f;
1772 return PyFloat_FromDouble(x);
Guido van Rossum9e51f9b1993-02-12 16:29:05 +00001773}
1774
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001775static char round_doc[] =
1776"round(number[, ndigits]) -> floating point number\n\
1777\n\
1778Round a number to a given precision in decimal digits (default 0 digits).\n\
1779This always returns a floating point number. Precision may be negative.";
1780
1781
Guido van Rossum79f25d91997-04-29 20:08:16 +00001782static PyObject *
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001783builtin_str(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001784 PyObject *self;
1785 PyObject *args;
Guido van Rossumc89705d1992-11-26 08:54:07 +00001786{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001787 PyObject *v;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001788
Guido van Rossum79f25d91997-04-29 20:08:16 +00001789 if (!PyArg_ParseTuple(args, "O:str", &v))
Guido van Rossumc89705d1992-11-26 08:54:07 +00001790 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001791 return PyObject_Str(v);
Guido van Rossumc89705d1992-11-26 08:54:07 +00001792}
1793
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001794static char str_doc[] =
1795"str(object) -> string\n\
1796\n\
1797Return a nice string representation of the object.\n\
1798If the argument is a string, the return value is the same object.";
1799
1800
Guido van Rossum79f25d91997-04-29 20:08:16 +00001801static PyObject *
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001802builtin_tuple(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001803 PyObject *self;
1804 PyObject *args;
Guido van Rossumcae027b1994-08-29 12:53:11 +00001805{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001806 PyObject *v;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001807
Guido van Rossum79f25d91997-04-29 20:08:16 +00001808 if (!PyArg_ParseTuple(args, "O:tuple", &v))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001809 return NULL;
Guido van Rossum09df08a1998-05-22 00:51:39 +00001810 return PySequence_Tuple(v);
Guido van Rossumcae027b1994-08-29 12:53:11 +00001811}
1812
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001813static char tuple_doc[] =
1814"tuple(sequence) -> list\n\
1815\n\
1816Return a tuple whose items are the same as those of the argument sequence.\n\
1817If the argument is a tuple, the return value is the same object.";
1818
1819
Guido van Rossum79f25d91997-04-29 20:08:16 +00001820static PyObject *
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001821builtin_type(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001822 PyObject *self;
1823 PyObject *args;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001824{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001825 PyObject *v;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001826
Guido van Rossum79f25d91997-04-29 20:08:16 +00001827 if (!PyArg_ParseTuple(args, "O:type", &v))
Guido van Rossum3f5da241990-12-20 15:06:42 +00001828 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001829 v = (PyObject *)v->ob_type;
1830 Py_INCREF(v);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001831 return v;
1832}
1833
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001834static char type_doc[] =
1835"type(object) -> type object\n\
1836\n\
1837Return the type of the object.";
1838
1839
Guido van Rossum79f25d91997-04-29 20:08:16 +00001840static PyObject *
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001841builtin_vars(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001842 PyObject *self;
1843 PyObject *args;
Guido van Rossum2d951851994-08-29 12:52:16 +00001844{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001845 PyObject *v = NULL;
1846 PyObject *d;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001847
Guido van Rossum79f25d91997-04-29 20:08:16 +00001848 if (!PyArg_ParseTuple(args, "|O:vars", &v))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001849 return NULL;
Guido van Rossum2d951851994-08-29 12:52:16 +00001850 if (v == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001851 d = PyEval_GetLocals();
Guido van Rossum53bb7ff1995-07-26 16:26:31 +00001852 if (d == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001853 if (!PyErr_Occurred())
1854 PyErr_SetString(PyExc_SystemError,
1855 "no locals!?");
Guido van Rossum53bb7ff1995-07-26 16:26:31 +00001856 }
1857 else
Guido van Rossum79f25d91997-04-29 20:08:16 +00001858 Py_INCREF(d);
Guido van Rossum2d951851994-08-29 12:52:16 +00001859 }
1860 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001861 d = PyObject_GetAttrString(v, "__dict__");
Guido van Rossum2d951851994-08-29 12:52:16 +00001862 if (d == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001863 PyErr_SetString(PyExc_TypeError,
Guido van Rossum2d951851994-08-29 12:52:16 +00001864 "vars() argument must have __dict__ attribute");
1865 return NULL;
1866 }
1867 }
1868 return d;
1869}
1870
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001871static char vars_doc[] =
1872"vars([object]) -> dictionary\n\
1873\n\
1874Without arguments, equivalent to locals().\n\
1875With an argument, equivalent to object.__dict__.";
1876
1877
Barry Warsawcde8b1b1997-08-22 21:14:38 +00001878static PyObject *
1879builtin_isinstance(self, args)
1880 PyObject *self;
1881 PyObject *args;
1882{
1883 PyObject *inst;
1884 PyObject *cls;
1885 int retval;
1886
1887 if (!PyArg_ParseTuple(args, "OO", &inst, &cls))
1888 return NULL;
Guido van Rossumf5dd9141997-12-02 19:11:45 +00001889 if (PyType_Check(cls)) {
Guido van Rossumd6af46d1997-12-10 05:51:47 +00001890 retval = ((PyObject *)(inst->ob_type) == cls);
Barry Warsawcde8b1b1997-08-22 21:14:38 +00001891 }
Barry Warsawcde8b1b1997-08-22 21:14:38 +00001892 else {
Guido van Rossumf5dd9141997-12-02 19:11:45 +00001893 if (!PyClass_Check(cls)) {
1894 PyErr_SetString(PyExc_TypeError,
1895 "second argument must be a class");
1896 return NULL;
1897 }
1898
1899 if (!PyInstance_Check(inst))
1900 retval = 0;
1901 else {
1902 PyObject *inclass =
1903 (PyObject*)((PyInstanceObject*)inst)->in_class;
1904 retval = PyClass_IsSubclass(inclass, cls);
1905 }
Barry Warsawcde8b1b1997-08-22 21:14:38 +00001906 }
1907 return PyInt_FromLong(retval);
1908}
1909
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001910static char isinstance_doc[] =
1911"isinstance(object, class-or-type) -> Boolean\n\
1912\n\
1913Return whether an object is an instance of a class or of a subclass thereof.\n\
1914With a type as second argument, return whether that is the object's type.";
1915
Barry Warsawcde8b1b1997-08-22 21:14:38 +00001916
1917static PyObject *
1918builtin_issubclass(self, args)
1919 PyObject *self;
1920 PyObject *args;
1921{
1922 PyObject *derived;
1923 PyObject *cls;
1924 int retval;
1925
1926 if (!PyArg_ParseTuple(args, "OO", &derived, &cls))
1927 return NULL;
1928 if (!PyClass_Check(derived) || !PyClass_Check(cls)) {
1929 PyErr_SetString(PyExc_TypeError, "arguments must be classes");
1930 return NULL;
1931 }
1932 /* shortcut */
1933 if (!(retval = (derived == cls)))
1934 retval = PyClass_IsSubclass(derived, cls);
1935
1936 return PyInt_FromLong(retval);
1937}
1938
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001939static char issubclass_doc[] =
1940"issubclass(C, B) -> Boolean\n\
1941\n\
1942Return whether class C is a subclass (i.e., a derived class) of class B.";
1943
Barry Warsawcde8b1b1997-08-22 21:14:38 +00001944
Guido van Rossum79f25d91997-04-29 20:08:16 +00001945static PyMethodDef builtin_methods[] = {
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001946 {"__import__", builtin___import__, 1, import_doc},
1947 {"abs", builtin_abs, 1, abs_doc},
1948 {"apply", builtin_apply, 1, apply_doc},
Guido van Rossum0daf0221999-03-19 19:07:19 +00001949 {"buffer", builtin_buffer, 1, buffer_doc},
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001950 {"callable", builtin_callable, 1, callable_doc},
1951 {"chr", builtin_chr, 1, chr_doc},
1952 {"cmp", builtin_cmp, 1, cmp_doc},
1953 {"coerce", builtin_coerce, 1, coerce_doc},
1954 {"compile", builtin_compile, 1, compile_doc},
Guido van Rossum8a5c5d21996-01-12 01:09:56 +00001955#ifndef WITHOUT_COMPLEX
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001956 {"complex", builtin_complex, 1, complex_doc},
Guido van Rossum8a5c5d21996-01-12 01:09:56 +00001957#endif
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001958 {"delattr", builtin_delattr, 1, delattr_doc},
1959 {"dir", builtin_dir, 1, dir_doc},
1960 {"divmod", builtin_divmod, 1, divmod_doc},
1961 {"eval", builtin_eval, 1, eval_doc},
1962 {"execfile", builtin_execfile, 1, execfile_doc},
1963 {"filter", builtin_filter, 1, filter_doc},
1964 {"float", builtin_float, 1, float_doc},
1965 {"getattr", builtin_getattr, 1, getattr_doc},
1966 {"globals", builtin_globals, 1, globals_doc},
1967 {"hasattr", builtin_hasattr, 1, hasattr_doc},
1968 {"hash", builtin_hash, 1, hash_doc},
1969 {"hex", builtin_hex, 1, hex_doc},
1970 {"id", builtin_id, 1, id_doc},
1971 {"input", builtin_input, 1, input_doc},
1972 {"intern", builtin_intern, 1, intern_doc},
1973 {"int", builtin_int, 1, int_doc},
1974 {"isinstance", builtin_isinstance, 1, isinstance_doc},
1975 {"issubclass", builtin_issubclass, 1, issubclass_doc},
1976 {"len", builtin_len, 1, len_doc},
1977 {"list", builtin_list, 1, list_doc},
1978 {"locals", builtin_locals, 1, locals_doc},
1979 {"long", builtin_long, 1, long_doc},
1980 {"map", builtin_map, 1, map_doc},
1981 {"max", builtin_max, 1, max_doc},
1982 {"min", builtin_min, 1, min_doc},
1983 {"oct", builtin_oct, 1, oct_doc},
1984 {"open", builtin_open, 1, open_doc},
1985 {"ord", builtin_ord, 1, ord_doc},
1986 {"pow", builtin_pow, 1, pow_doc},
1987 {"range", builtin_range, 1, range_doc},
1988 {"raw_input", builtin_raw_input, 1, raw_input_doc},
1989 {"reduce", builtin_reduce, 1, reduce_doc},
1990 {"reload", builtin_reload, 1, reload_doc},
1991 {"repr", builtin_repr, 1, repr_doc},
1992 {"round", builtin_round, 1, round_doc},
1993 {"setattr", builtin_setattr, 1, setattr_doc},
1994 {"slice", builtin_slice, 1, slice_doc},
1995 {"str", builtin_str, 1, str_doc},
1996 {"tuple", builtin_tuple, 1, tuple_doc},
1997 {"type", builtin_type, 1, type_doc},
1998 {"vars", builtin_vars, 1, vars_doc},
1999 {"xrange", builtin_xrange, 1, xrange_doc},
Guido van Rossumc02e15c1991-12-16 13:03:00 +00002000 {NULL, NULL},
Guido van Rossum3f5da241990-12-20 15:06:42 +00002001};
2002
Guido van Rossum3f5da241990-12-20 15:06:42 +00002003/* Predefined exceptions */
2004
Guido van Rossum04748321997-09-16 18:43:15 +00002005PyObject *PyExc_Exception;
Barry Warsaw757af0e1997-08-29 22:13:51 +00002006PyObject *PyExc_StandardError;
Barry Warsaw412cdc21997-09-16 21:51:14 +00002007PyObject *PyExc_ArithmeticError;
Barry Warsaw757af0e1997-08-29 22:13:51 +00002008PyObject *PyExc_LookupError;
2009
Guido van Rossum79f25d91997-04-29 20:08:16 +00002010PyObject *PyExc_AssertionError;
2011PyObject *PyExc_AttributeError;
2012PyObject *PyExc_EOFError;
Guido van Rossumb6a7f771997-05-09 03:03:23 +00002013PyObject *PyExc_FloatingPointError;
Barry Warsawd086a1a1998-07-23 15:59:57 +00002014PyObject *PyExc_EnvironmentError;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002015PyObject *PyExc_IOError;
Barry Warsawd086a1a1998-07-23 15:59:57 +00002016PyObject *PyExc_OSError;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002017PyObject *PyExc_ImportError;
2018PyObject *PyExc_IndexError;
2019PyObject *PyExc_KeyError;
2020PyObject *PyExc_KeyboardInterrupt;
2021PyObject *PyExc_MemoryError;
2022PyObject *PyExc_NameError;
2023PyObject *PyExc_OverflowError;
2024PyObject *PyExc_RuntimeError;
Barry Warsaw344864f1998-12-01 18:52:06 +00002025PyObject *PyExc_NotImplementedError;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002026PyObject *PyExc_SyntaxError;
2027PyObject *PyExc_SystemError;
2028PyObject *PyExc_SystemExit;
2029PyObject *PyExc_TypeError;
2030PyObject *PyExc_ValueError;
2031PyObject *PyExc_ZeroDivisionError;
Guido van Rossum50afb7a1991-12-10 13:52:31 +00002032
Barry Warsaw757af0e1997-08-29 22:13:51 +00002033PyObject *PyExc_MemoryErrorInst;
2034
2035static struct
2036{
2037 char* name;
2038 PyObject** exc;
2039 int leaf_exc;
2040}
2041bltin_exc[] = {
Guido van Rossum04748321997-09-16 18:43:15 +00002042 {"Exception", &PyExc_Exception, 0},
Barry Warsaw757af0e1997-08-29 22:13:51 +00002043 {"StandardError", &PyExc_StandardError, 0},
Barry Warsaw412cdc21997-09-16 21:51:14 +00002044 {"ArithmeticError", &PyExc_ArithmeticError, 0},
Barry Warsaw757af0e1997-08-29 22:13:51 +00002045 {"LookupError", &PyExc_LookupError, 0},
2046 {"AssertionError", &PyExc_AssertionError, 1},
2047 {"AttributeError", &PyExc_AttributeError, 1},
2048 {"EOFError", &PyExc_EOFError, 1},
2049 {"FloatingPointError", &PyExc_FloatingPointError, 1},
Barry Warsaw78902031999-01-29 20:29:49 +00002050 {"EnvironmentError", &PyExc_EnvironmentError, 0},
Barry Warsaw757af0e1997-08-29 22:13:51 +00002051 {"IOError", &PyExc_IOError, 1},
Barry Warsawd086a1a1998-07-23 15:59:57 +00002052 {"OSError", &PyExc_OSError, 1},
Barry Warsaw757af0e1997-08-29 22:13:51 +00002053 {"ImportError", &PyExc_ImportError, 1},
2054 {"IndexError", &PyExc_IndexError, 1},
2055 {"KeyError", &PyExc_KeyError, 1},
2056 {"KeyboardInterrupt", &PyExc_KeyboardInterrupt, 1},
2057 {"MemoryError", &PyExc_MemoryError, 1},
2058 {"NameError", &PyExc_NameError, 1},
2059 {"OverflowError", &PyExc_OverflowError, 1},
2060 {"RuntimeError", &PyExc_RuntimeError, 1},
Barry Warsaw344864f1998-12-01 18:52:06 +00002061 {"NotImplementedError",&PyExc_NotImplementedError,1},
Barry Warsaw757af0e1997-08-29 22:13:51 +00002062 {"SyntaxError", &PyExc_SyntaxError, 1},
2063 {"SystemError", &PyExc_SystemError, 1},
2064 {"SystemExit", &PyExc_SystemExit, 1},
2065 {"TypeError", &PyExc_TypeError, 1},
2066 {"ValueError", &PyExc_ValueError, 1},
2067 {"ZeroDivisionError", &PyExc_ZeroDivisionError, 1},
2068 {NULL, NULL}
2069};
2070
2071
Barry Warsaw98b62461998-09-14 18:51:11 +00002072/* import exceptions module to extract class exceptions. on success,
2073 * return 1. on failure return 0 which signals _PyBuiltin_Init_2 to fall
2074 * back to using old-style string based exceptions.
2075 */
2076static int
Barry Warsaw757af0e1997-08-29 22:13:51 +00002077init_class_exc(dict)
2078 PyObject *dict;
2079{
2080 int i;
2081 PyObject *m = PyImport_ImportModule("exceptions");
Barry Warsaw98b62461998-09-14 18:51:11 +00002082 PyObject *args = NULL;
2083 PyObject *d = NULL;
Barry Warsaw757af0e1997-08-29 22:13:51 +00002084
Barry Warsaw98b62461998-09-14 18:51:11 +00002085 /* make sure we got the module and its dictionary */
Barry Warsaw757af0e1997-08-29 22:13:51 +00002086 if (m == NULL ||
2087 (d = PyModule_GetDict(m)) == NULL)
2088 {
Barry Warsaw98b62461998-09-14 18:51:11 +00002089 PySys_WriteStderr("'import exceptions' failed; ");
Barry Warsaw757af0e1997-08-29 22:13:51 +00002090 if (Py_VerboseFlag) {
Barry Warsaw98b62461998-09-14 18:51:11 +00002091 PySys_WriteStderr("traceback:\n");
Barry Warsaw757af0e1997-08-29 22:13:51 +00002092 PyErr_Print();
2093 }
2094 else {
Barry Warsaw98b62461998-09-14 18:51:11 +00002095 PySys_WriteStderr("use -v for traceback\n");
Barry Warsaw757af0e1997-08-29 22:13:51 +00002096 }
Barry Warsaw98b62461998-09-14 18:51:11 +00002097 goto finally;
Barry Warsaw757af0e1997-08-29 22:13:51 +00002098 }
2099 for (i = 0; bltin_exc[i].name; i++) {
2100 /* dig the exception out of the module */
2101 PyObject *exc = PyDict_GetItemString(d, bltin_exc[i].name);
Barry Warsaw98b62461998-09-14 18:51:11 +00002102 if (!exc) {
2103 PySys_WriteStderr(
2104 "Built-in exception class not found: %s. Library mismatch?\n",
2105 bltin_exc[i].name);
2106 goto finally;
2107 }
2108 /* free the old-style exception string object */
Barry Warsaw757af0e1997-08-29 22:13:51 +00002109 Py_XDECREF(*bltin_exc[i].exc);
2110
2111 /* squirrel away a pointer to the exception */
2112 Py_INCREF(exc);
2113 *bltin_exc[i].exc = exc;
2114
2115 /* and insert the name in the __builtin__ module */
Barry Warsaw98b62461998-09-14 18:51:11 +00002116 if (PyDict_SetItemString(dict, bltin_exc[i].name, exc)) {
2117 PySys_WriteStderr(
2118 "Cannot insert exception into __builtin__: %s\n",
2119 bltin_exc[i].name);
2120 goto finally;
2121 }
Barry Warsaw757af0e1997-08-29 22:13:51 +00002122 }
2123
2124 /* we need one pre-allocated instance */
2125 args = Py_BuildValue("()");
Barry Warsaw98b62461998-09-14 18:51:11 +00002126 if (!args ||
2127 !(PyExc_MemoryErrorInst =
2128 PyEval_CallObject(PyExc_MemoryError, args)))
2129 {
2130 PySys_WriteStderr("Cannot pre-allocate MemoryError instance\n");
2131 goto finally;
Barry Warsaw757af0e1997-08-29 22:13:51 +00002132 }
Barry Warsaw98b62461998-09-14 18:51:11 +00002133 Py_DECREF(args);
Barry Warsaw757af0e1997-08-29 22:13:51 +00002134
2135 /* we're done with the exceptions module */
2136 Py_DECREF(m);
2137
Barry Warsaw98b62461998-09-14 18:51:11 +00002138 if (PyErr_Occurred()) {
2139 PySys_WriteStderr("Cannot initialize standard class exceptions; ");
2140 if (Py_VerboseFlag) {
2141 PySys_WriteStderr("traceback:\n");
2142 PyErr_Print();
2143 }
2144 else
2145 PySys_WriteStderr("use -v for traceback\n");
2146 goto finally;
2147 }
2148 return 1;
2149 finally:
2150 Py_XDECREF(m);
2151 Py_XDECREF(args);
2152 PyErr_Clear();
2153 return 0;
Barry Warsaw757af0e1997-08-29 22:13:51 +00002154}
2155
2156
2157static void
2158fini_instances()
2159{
2160 Py_XDECREF(PyExc_MemoryErrorInst);
2161 PyExc_MemoryErrorInst = NULL;
2162}
2163
2164
Guido van Rossum79f25d91997-04-29 20:08:16 +00002165static PyObject *
Guido van Rossum25ce5661997-08-02 03:10:38 +00002166newstdexception(dict, name)
2167 PyObject *dict;
Guido van Rossumfb905c31991-12-16 15:42:38 +00002168 char *name;
Guido van Rossum3f5da241990-12-20 15:06:42 +00002169{
Guido van Rossum79f25d91997-04-29 20:08:16 +00002170 PyObject *v = PyString_FromString(name);
Guido van Rossum25ce5661997-08-02 03:10:38 +00002171 if (v == NULL || PyDict_SetItemString(dict, name, v) != 0)
Barry Warsaw98b62461998-09-14 18:51:11 +00002172 Py_FatalError("Cannot create string-based exceptions");
Guido van Rossum3f5da241990-12-20 15:06:42 +00002173 return v;
2174}
2175
2176static void
Guido van Rossum25ce5661997-08-02 03:10:38 +00002177initerrors(dict)
2178 PyObject *dict;
Guido van Rossum3f5da241990-12-20 15:06:42 +00002179{
Barry Warsaw72b715d1999-02-24 00:35:43 +00002180 int i, j;
Barry Warsaw757af0e1997-08-29 22:13:51 +00002181 int exccnt = 0;
2182 for (i = 0; bltin_exc[i].name; i++, exccnt++) {
Barry Warsaw98b62461998-09-14 18:51:11 +00002183 Py_XDECREF(*bltin_exc[i].exc);
Barry Warsaw757af0e1997-08-29 22:13:51 +00002184 if (bltin_exc[i].leaf_exc)
2185 *bltin_exc[i].exc =
2186 newstdexception(dict, bltin_exc[i].name);
2187 }
2188
Barry Warsawd086a1a1998-07-23 15:59:57 +00002189 /* This is kind of bogus because we special case the some of the
2190 * new exceptions to be nearly forward compatible. But this means
2191 * we hard code knowledge about exceptions.py into C here. I don't
2192 * have a better solution, though.
2193 */
Barry Warsaw757af0e1997-08-29 22:13:51 +00002194 PyExc_LookupError = PyTuple_New(2);
2195 Py_INCREF(PyExc_IndexError);
2196 PyTuple_SET_ITEM(PyExc_LookupError, 0, PyExc_IndexError);
2197 Py_INCREF(PyExc_KeyError);
2198 PyTuple_SET_ITEM(PyExc_LookupError, 1, PyExc_KeyError);
2199 PyDict_SetItemString(dict, "LookupError", PyExc_LookupError);
2200
Barry Warsaw412cdc21997-09-16 21:51:14 +00002201 PyExc_ArithmeticError = PyTuple_New(3);
Barry Warsaw757af0e1997-08-29 22:13:51 +00002202 Py_INCREF(PyExc_OverflowError);
Barry Warsaw412cdc21997-09-16 21:51:14 +00002203 PyTuple_SET_ITEM(PyExc_ArithmeticError, 0, PyExc_OverflowError);
Barry Warsaw757af0e1997-08-29 22:13:51 +00002204 Py_INCREF(PyExc_ZeroDivisionError);
Barry Warsaw412cdc21997-09-16 21:51:14 +00002205 PyTuple_SET_ITEM(PyExc_ArithmeticError, 1, PyExc_ZeroDivisionError);
Barry Warsaw757af0e1997-08-29 22:13:51 +00002206 Py_INCREF(PyExc_FloatingPointError);
Barry Warsaw412cdc21997-09-16 21:51:14 +00002207 PyTuple_SET_ITEM(PyExc_ArithmeticError, 2, PyExc_FloatingPointError);
2208 PyDict_SetItemString(dict, "ArithmeticError", PyExc_ArithmeticError);
Barry Warsaw757af0e1997-08-29 22:13:51 +00002209
Barry Warsawd086a1a1998-07-23 15:59:57 +00002210 PyExc_EnvironmentError = PyTuple_New(2);
2211 Py_INCREF(PyExc_IOError);
2212 PyTuple_SET_ITEM(PyExc_EnvironmentError, 0, PyExc_IOError);
2213 Py_INCREF(PyExc_OSError);
2214 PyTuple_SET_ITEM(PyExc_EnvironmentError, 1, PyExc_OSError);
2215 PyDict_SetItemString(dict, "EnvironmentError", PyExc_EnvironmentError);
2216
Barry Warsaw72b715d1999-02-24 00:35:43 +00002217 /* missing from the StandardError tuple: Exception, StandardError,
2218 * and SystemExit
2219 */
2220 PyExc_StandardError = PyTuple_New(exccnt-3);
2221 for (i = 2, j = 0; bltin_exc[i].name; i++) {
Barry Warsaw757af0e1997-08-29 22:13:51 +00002222 PyObject *exc = *bltin_exc[i].exc;
Barry Warsaw72b715d1999-02-24 00:35:43 +00002223 /* SystemExit is not an error, but it is an exception */
2224 if (exc != PyExc_SystemExit) {
2225 Py_INCREF(exc);
2226 PyTuple_SET_ITEM(PyExc_StandardError, j++, exc);
2227 }
Barry Warsaw757af0e1997-08-29 22:13:51 +00002228 }
2229 PyDict_SetItemString(dict, "StandardError", PyExc_StandardError);
Guido van Rossum04748321997-09-16 18:43:15 +00002230
Barry Warsaw72b715d1999-02-24 00:35:43 +00002231 /* Exception is a 2-tuple */
2232 PyExc_Exception = PyTuple_New(2);
2233 Py_INCREF(PyExc_SystemExit);
2234 PyTuple_SET_ITEM(PyExc_Exception, 0, PyExc_SystemExit);
2235 Py_INCREF(PyExc_StandardError);
2236 PyTuple_SET_ITEM(PyExc_Exception, 1, PyExc_StandardError);
Guido van Rossum04748321997-09-16 18:43:15 +00002237 PyDict_SetItemString(dict, "Exception", PyExc_Exception);
Barry Warsaw757af0e1997-08-29 22:13:51 +00002238
2239 if (PyErr_Occurred())
2240 Py_FatalError("Could not initialize built-in string exceptions");
Guido van Rossum25ce5661997-08-02 03:10:38 +00002241}
2242
Barry Warsaw72b715d1999-02-24 00:35:43 +00002243
Guido van Rossum25ce5661997-08-02 03:10:38 +00002244static void
2245finierrors()
2246{
Barry Warsaw757af0e1997-08-29 22:13:51 +00002247 int i;
2248 for (i = 0; bltin_exc[i].name; i++) {
2249 PyObject *exc = *bltin_exc[i].exc;
2250 Py_XDECREF(exc);
2251 *bltin_exc[i].exc = NULL;
2252 }
Guido van Rossum25ce5661997-08-02 03:10:38 +00002253}
2254
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002255static char builtin_doc[] =
2256"Built-in functions, exceptions, and other objects.\n\
2257\n\
2258Noteworthy: None is the `nil' object; Ellipsis represents `...' in slices.";
2259
Guido van Rossum25ce5661997-08-02 03:10:38 +00002260PyObject *
Barry Warsaw757af0e1997-08-29 22:13:51 +00002261_PyBuiltin_Init_1()
Guido van Rossum25ce5661997-08-02 03:10:38 +00002262{
2263 PyObject *mod, *dict;
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002264 mod = Py_InitModule4("__builtin__", builtin_methods,
2265 builtin_doc, (PyObject *)NULL,
2266 PYTHON_API_VERSION);
Guido van Rossum25ce5661997-08-02 03:10:38 +00002267 if (mod == NULL)
2268 return NULL;
2269 dict = PyModule_GetDict(mod);
2270 initerrors(dict);
2271 if (PyDict_SetItemString(dict, "None", Py_None) < 0)
2272 return NULL;
2273 if (PyDict_SetItemString(dict, "Ellipsis", Py_Ellipsis) < 0)
2274 return NULL;
2275 if (PyDict_SetItemString(dict, "__debug__",
2276 PyInt_FromLong(Py_OptimizeFlag == 0)) < 0)
2277 return NULL;
Barry Warsaw757af0e1997-08-29 22:13:51 +00002278
Guido van Rossum25ce5661997-08-02 03:10:38 +00002279 return mod;
Guido van Rossum3f5da241990-12-20 15:06:42 +00002280}
2281
2282void
Barry Warsaw757af0e1997-08-29 22:13:51 +00002283_PyBuiltin_Init_2(dict)
2284 PyObject *dict;
2285{
2286 /* if Python was started with -X, initialize the class exceptions */
Barry Warsaw98b62461998-09-14 18:51:11 +00002287 if (Py_UseClassExceptionsFlag) {
2288 if (!init_class_exc(dict)) {
2289 /* class based exceptions could not be
2290 * initialized. Fall back to using string based
2291 * exceptions.
2292 */
2293 PySys_WriteStderr(
2294 "Warning! Falling back to string-based exceptions\n");
2295 initerrors(dict);
2296 }
2297 }
Barry Warsaw757af0e1997-08-29 22:13:51 +00002298}
2299
2300
2301void
2302_PyBuiltin_Fini_1()
2303{
2304 fini_instances();
2305}
2306
2307
2308void
2309_PyBuiltin_Fini_2()
Guido van Rossum3f5da241990-12-20 15:06:42 +00002310{
Guido van Rossum25ce5661997-08-02 03:10:38 +00002311 finierrors();
Guido van Rossum3f5da241990-12-20 15:06:42 +00002312}
Guido van Rossumc6bb8f71991-07-01 18:42:41 +00002313
Guido van Rossum12d12c51993-10-26 17:58:25 +00002314
Guido van Rossume77a7571993-11-03 15:01:26 +00002315/* Helper for filter(): filter a tuple through a function */
Guido van Rossum12d12c51993-10-26 17:58:25 +00002316
Guido van Rossum79f25d91997-04-29 20:08:16 +00002317static PyObject *
Guido van Rossum12d12c51993-10-26 17:58:25 +00002318filtertuple(func, tuple)
Guido van Rossum79f25d91997-04-29 20:08:16 +00002319 PyObject *func;
2320 PyObject *tuple;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002321{
Guido van Rossum79f25d91997-04-29 20:08:16 +00002322 PyObject *result;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002323 register int i, j;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002324 int len = PyTuple_Size(tuple);
Guido van Rossum12d12c51993-10-26 17:58:25 +00002325
Guido van Rossumb7b45621995-08-04 04:07:45 +00002326 if (len == 0) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002327 Py_INCREF(tuple);
Guido van Rossumb7b45621995-08-04 04:07:45 +00002328 return tuple;
2329 }
2330
Guido van Rossum79f25d91997-04-29 20:08:16 +00002331 if ((result = PyTuple_New(len)) == NULL)
Guido van Rossum2586bf01993-11-01 16:21:44 +00002332 return NULL;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002333
Guido van Rossum12d12c51993-10-26 17:58:25 +00002334 for (i = j = 0; i < len; ++i) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002335 PyObject *item, *good;
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002336 int ok;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002337
Guido van Rossum79f25d91997-04-29 20:08:16 +00002338 if ((item = PyTuple_GetItem(tuple, i)) == NULL)
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002339 goto Fail_1;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002340 if (func == Py_None) {
2341 Py_INCREF(item);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002342 good = item;
2343 }
2344 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002345 PyObject *arg = Py_BuildValue("(O)", item);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002346 if (arg == NULL)
2347 goto Fail_1;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002348 good = PyEval_CallObject(func, arg);
2349 Py_DECREF(arg);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002350 if (good == NULL)
Guido van Rossum12d12c51993-10-26 17:58:25 +00002351 goto Fail_1;
2352 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002353 ok = PyObject_IsTrue(good);
2354 Py_DECREF(good);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002355 if (ok) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002356 Py_INCREF(item);
2357 if (PyTuple_SetItem(result, j++, item) < 0)
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002358 goto Fail_1;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002359 }
Guido van Rossum12d12c51993-10-26 17:58:25 +00002360 }
2361
Guido van Rossum79f25d91997-04-29 20:08:16 +00002362 if (_PyTuple_Resize(&result, j, 0) < 0)
Guido van Rossum12d12c51993-10-26 17:58:25 +00002363 return NULL;
2364
Guido van Rossum12d12c51993-10-26 17:58:25 +00002365 return result;
2366
Guido van Rossum12d12c51993-10-26 17:58:25 +00002367Fail_1:
Guido van Rossum79f25d91997-04-29 20:08:16 +00002368 Py_DECREF(result);
Guido van Rossum12d12c51993-10-26 17:58:25 +00002369 return NULL;
2370}
2371
2372
Guido van Rossume77a7571993-11-03 15:01:26 +00002373/* Helper for filter(): filter a string through a function */
Guido van Rossum12d12c51993-10-26 17:58:25 +00002374
Guido van Rossum79f25d91997-04-29 20:08:16 +00002375static PyObject *
Guido van Rossum12d12c51993-10-26 17:58:25 +00002376filterstring(func, strobj)
Guido van Rossum79f25d91997-04-29 20:08:16 +00002377 PyObject *func;
2378 PyObject *strobj;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002379{
Guido van Rossum79f25d91997-04-29 20:08:16 +00002380 PyObject *result;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002381 register int i, j;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002382 int len = PyString_Size(strobj);
Guido van Rossum12d12c51993-10-26 17:58:25 +00002383
Guido van Rossum79f25d91997-04-29 20:08:16 +00002384 if (func == Py_None) {
Guido van Rossum2586bf01993-11-01 16:21:44 +00002385 /* No character is ever false -- share input string */
Guido van Rossum79f25d91997-04-29 20:08:16 +00002386 Py_INCREF(strobj);
Guido van Rossum2d951851994-08-29 12:52:16 +00002387 return strobj;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002388 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002389 if ((result = PyString_FromStringAndSize(NULL, len)) == NULL)
Guido van Rossum2586bf01993-11-01 16:21:44 +00002390 return NULL;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002391
Guido van Rossum12d12c51993-10-26 17:58:25 +00002392 for (i = j = 0; i < len; ++i) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002393 PyObject *item, *arg, *good;
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002394 int ok;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002395
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002396 item = (*strobj->ob_type->tp_as_sequence->sq_item)(strobj, i);
2397 if (item == NULL)
2398 goto Fail_1;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002399 arg = Py_BuildValue("(O)", item);
2400 Py_DECREF(item);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002401 if (arg == NULL)
2402 goto Fail_1;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002403 good = PyEval_CallObject(func, arg);
2404 Py_DECREF(arg);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002405 if (good == NULL)
2406 goto Fail_1;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002407 ok = PyObject_IsTrue(good);
2408 Py_DECREF(good);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002409 if (ok)
Guido van Rossum79f25d91997-04-29 20:08:16 +00002410 PyString_AS_STRING((PyStringObject *)result)[j++] =
2411 PyString_AS_STRING((PyStringObject *)item)[0];
Guido van Rossum12d12c51993-10-26 17:58:25 +00002412 }
2413
Guido van Rossum79f25d91997-04-29 20:08:16 +00002414 if (j < len && _PyString_Resize(&result, j) < 0)
Guido van Rossum12d12c51993-10-26 17:58:25 +00002415 return NULL;
2416
Guido van Rossum12d12c51993-10-26 17:58:25 +00002417 return result;
2418
Guido van Rossum12d12c51993-10-26 17:58:25 +00002419Fail_1:
Guido van Rossum79f25d91997-04-29 20:08:16 +00002420 Py_DECREF(result);
Guido van Rossum12d12c51993-10-26 17:58:25 +00002421 return NULL;
2422}