blob: bcde319f21bf40ac4d3d21ad3366ff741b32527a [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[] =
Fred Drake7b912121999-12-23 14:16:55 +0000133"apply(object, args[, kwargs]) -> value\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000134\n\
Fred Drake7b912121999-12-23 14:16:55 +0000135Call a callable object with positional arguments taken from the tuple args,\n\
136and keyword arguments taken from the optional dictionary kwargs.\n\
137Note that classes are callable, as are instances with a __call__() method.";
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000138
139
Guido van Rossum79f25d91997-04-29 20:08:16 +0000140static PyObject *
Guido van Rossum0daf0221999-03-19 19:07:19 +0000141builtin_buffer(self, args)
142 PyObject *self;
143 PyObject *args;
144{
145 PyObject *ob;
146 int offset = 0;
147 int size = Py_END_OF_BUFFER;
148
149 if ( !PyArg_ParseTuple(args, "O|ii:buffer", &ob, &offset, &size) )
150 return NULL;
151 return PyBuffer_FromObject(ob, offset, size);
152}
153
154static char buffer_doc[] =
Guido van Rossum09095f32000-03-10 23:00:52 +0000155"buffer(object [, offset[, size]]) -> object\n\
Guido van Rossum0daf0221999-03-19 19:07:19 +0000156\n\
157Creates a new buffer object which references the given object.\n\
158The buffer will reference a slice of the target object from the\n\
159start of the object (or at the specified offset). The slice will\n\
160extend to the end of the target object (or with the specified size).";
161
162
163static PyObject *
Guido van Rossum09095f32000-03-10 23:00:52 +0000164builtin_unicode(self, args)
165 PyObject *self;
166 PyObject *args;
167{
Guido van Rossum3afba762000-04-11 15:38:23 +0000168 PyObject *v;
169 const void *buffer;
Guido van Rossum09095f32000-03-10 23:00:52 +0000170 int len;
171 char *encoding = NULL;
172 char *errors = NULL;
173
Guido van Rossum3afba762000-04-11 15:38:23 +0000174 if ( !PyArg_ParseTuple(args, "O|ss:unicode", &v, &encoding, &errors) )
Guido van Rossum09095f32000-03-10 23:00:52 +0000175 return NULL;
Guido van Rossum3afba762000-04-11 15:38:23 +0000176 /* Special case: Unicode will stay Unicode */
177 if (PyUnicode_Check(v)) {
178 if (encoding) {
179 PyErr_SetString(PyExc_TypeError,
180 "unicode() does not support decoding of Unicode objects");
181 return NULL;
182 }
183 Py_INCREF(v);
184 return v;
185 }
186 /* Read raw data and decode it */
187 if (PyObject_AsReadBuffer(v, &buffer, &len))
188 return NULL;
189 return PyUnicode_Decode((const char *)buffer, len, encoding, errors);
Guido van Rossum09095f32000-03-10 23:00:52 +0000190}
191
192static char unicode_doc[] =
193"unicode(string [, encoding[, errors]]) -> object\n\
194\n\
Fred Drake078b24f2000-04-13 02:42:50 +0000195Creates a new Unicode object from the given encoded string.\n\
Fred Drakec640b182000-05-09 19:55:16 +0000196encoding defaults to the current default string encoding and \n\
197errors, defining the error handling, to 'strict'.";
Guido van Rossum09095f32000-03-10 23:00:52 +0000198
199
200static PyObject *
Guido van Rossum2d951851994-08-29 12:52:16 +0000201builtin_callable(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000202 PyObject *self;
203 PyObject *args;
Guido van Rossum2d951851994-08-29 12:52:16 +0000204{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000205 PyObject *v;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000206
Guido van Rossum79f25d91997-04-29 20:08:16 +0000207 if (!PyArg_ParseTuple(args, "O:callable", &v))
Guido van Rossum2d951851994-08-29 12:52:16 +0000208 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000209 return PyInt_FromLong((long)PyCallable_Check(v));
Guido van Rossum2d951851994-08-29 12:52:16 +0000210}
211
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000212static char callable_doc[] =
213"callable(object) -> Boolean\n\
214\n\
215Return whether the object is callable (i.e., some kind of function).\n\
216Note that classes are callable, as are instances with a __call__() method.";
217
218
Guido van Rossum79f25d91997-04-29 20:08:16 +0000219static PyObject *
Guido van Rossume77a7571993-11-03 15:01:26 +0000220builtin_filter(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000221 PyObject *self;
222 PyObject *args;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000223{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000224 PyObject *func, *seq, *result;
225 PySequenceMethods *sqf;
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000226 int len;
227 register int i, j;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000228
Guido van Rossum79f25d91997-04-29 20:08:16 +0000229 if (!PyArg_ParseTuple(args, "OO:filter", &func, &seq))
Guido van Rossum12d12c51993-10-26 17:58:25 +0000230 return NULL;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000231
Guido van Rossum79f25d91997-04-29 20:08:16 +0000232 if (PyString_Check(seq)) {
233 PyObject *r = filterstring(func, seq);
Guido van Rossum12d12c51993-10-26 17:58:25 +0000234 return r;
235 }
236
Guido van Rossum79f25d91997-04-29 20:08:16 +0000237 if (PyTuple_Check(seq)) {
238 PyObject *r = filtertuple(func, seq);
Guido van Rossum12d12c51993-10-26 17:58:25 +0000239 return r;
240 }
241
Guido van Rossum09df08a1998-05-22 00:51:39 +0000242 sqf = seq->ob_type->tp_as_sequence;
243 if (sqf == NULL || sqf->sq_length == NULL || sqf->sq_item == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000244 PyErr_SetString(PyExc_TypeError,
Guido van Rossume77a7571993-11-03 15:01:26 +0000245 "argument 2 to filter() must be a sequence type");
Guido van Rossum12d12c51993-10-26 17:58:25 +0000246 goto Fail_2;
247 }
248
249 if ((len = (*sqf->sq_length)(seq)) < 0)
250 goto Fail_2;
251
Guido van Rossum79f25d91997-04-29 20:08:16 +0000252 if (PyList_Check(seq) && seq->ob_refcnt == 1) {
253 Py_INCREF(seq);
Guido van Rossum12d12c51993-10-26 17:58:25 +0000254 result = seq;
255 }
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000256 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000257 if ((result = PyList_New(len)) == NULL)
Guido van Rossum12d12c51993-10-26 17:58:25 +0000258 goto Fail_2;
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000259 }
Guido van Rossum12d12c51993-10-26 17:58:25 +0000260
Guido van Rossum2d951851994-08-29 12:52:16 +0000261 for (i = j = 0; ; ++i) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000262 PyObject *item, *good;
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000263 int ok;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000264
Guido van Rossum2d951851994-08-29 12:52:16 +0000265 if ((item = (*sqf->sq_item)(seq, i)) == NULL) {
Barry Warsawcde8b1b1997-08-22 21:14:38 +0000266 if (PyErr_ExceptionMatches(PyExc_IndexError)) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000267 PyErr_Clear();
Guido van Rossum2d951851994-08-29 12:52:16 +0000268 break;
269 }
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000270 goto Fail_1;
Guido van Rossum2d951851994-08-29 12:52:16 +0000271 }
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000272
Guido van Rossum79f25d91997-04-29 20:08:16 +0000273 if (func == Py_None) {
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000274 good = item;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000275 Py_INCREF(good);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000276 }
277 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000278 PyObject *arg = Py_BuildValue("(O)", item);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000279 if (arg == NULL)
280 goto Fail_1;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000281 good = PyEval_CallObject(func, arg);
282 Py_DECREF(arg);
Guido van Rossum58b68731995-01-10 17:40:55 +0000283 if (good == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000284 Py_DECREF(item);
Guido van Rossum12d12c51993-10-26 17:58:25 +0000285 goto Fail_1;
Guido van Rossum58b68731995-01-10 17:40:55 +0000286 }
Guido van Rossum12d12c51993-10-26 17:58:25 +0000287 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000288 ok = PyObject_IsTrue(good);
289 Py_DECREF(good);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000290 if (ok) {
Guido van Rossum2d951851994-08-29 12:52:16 +0000291 if (j < len) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000292 if (PyList_SetItem(result, j++, item) < 0)
Guido van Rossum2d951851994-08-29 12:52:16 +0000293 goto Fail_1;
294 }
295 else {
Barry Warsawfa77e091999-01-28 18:49:12 +0000296 int status = PyList_Append(result, item);
Guido van Rossum2d951851994-08-29 12:52:16 +0000297 j++;
Barry Warsawfa77e091999-01-28 18:49:12 +0000298 Py_DECREF(item);
299 if (status < 0)
Guido van Rossum2d951851994-08-29 12:52:16 +0000300 goto Fail_1;
301 }
Guido van Rossum58b68731995-01-10 17:40:55 +0000302 } else {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000303 Py_DECREF(item);
Guido van Rossum12d12c51993-10-26 17:58:25 +0000304 }
Guido van Rossum12d12c51993-10-26 17:58:25 +0000305 }
306
Guido van Rossum12d12c51993-10-26 17:58:25 +0000307
Guido van Rossum79f25d91997-04-29 20:08:16 +0000308 if (j < len && PyList_SetSlice(result, j, len, NULL) < 0)
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000309 goto Fail_1;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000310
Guido van Rossum12d12c51993-10-26 17:58:25 +0000311 return result;
312
Guido van Rossum12d12c51993-10-26 17:58:25 +0000313Fail_1:
Guido van Rossum79f25d91997-04-29 20:08:16 +0000314 Py_DECREF(result);
Guido van Rossum12d12c51993-10-26 17:58:25 +0000315Fail_2:
Guido van Rossum12d12c51993-10-26 17:58:25 +0000316 return NULL;
317}
318
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000319static char filter_doc[] =
320"filter(function, sequence) -> list\n\
321\n\
322Return a list containing those items of sequence for which function(item)\n\
323is true. If function is None, return a list of items that are true.";
324
325
Guido van Rossum79f25d91997-04-29 20:08:16 +0000326static PyObject *
Guido van Rossum94390a41992-08-14 15:14:30 +0000327builtin_chr(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000328 PyObject *self;
329 PyObject *args;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000330{
331 long x;
332 char s[1];
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000333
Guido van Rossum79f25d91997-04-29 20:08:16 +0000334 if (!PyArg_ParseTuple(args, "l:chr", &x))
Guido van Rossum3f5da241990-12-20 15:06:42 +0000335 return NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000336 if (x < 0 || x >= 256) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000337 PyErr_SetString(PyExc_ValueError,
338 "chr() arg not in range(256)");
Guido van Rossum3f5da241990-12-20 15:06:42 +0000339 return NULL;
340 }
Guido van Rossum6bf62da1997-04-11 20:37:35 +0000341 s[0] = (char)x;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000342 return PyString_FromStringAndSize(s, 1);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000343}
344
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000345static char chr_doc[] =
346"chr(i) -> character\n\
347\n\
348Return a string of one character with ordinal i; 0 <= i < 256.";
349
350
Guido van Rossum79f25d91997-04-29 20:08:16 +0000351static PyObject *
Guido van Rossum09095f32000-03-10 23:00:52 +0000352builtin_unichr(self, args)
353 PyObject *self;
354 PyObject *args;
355{
356 long x;
357 Py_UNICODE s[1];
358
359 if (!PyArg_ParseTuple(args, "l:unichr", &x))
360 return NULL;
361 if (x < 0 || x >= 65536) {
362 PyErr_SetString(PyExc_ValueError,
363 "unichr() arg not in range(65536)");
364 return NULL;
365 }
366 s[0] = (Py_UNICODE)x;
367 return PyUnicode_FromUnicode(s, 1);
368}
369
370static char unichr_doc[] =
Fred Drake078b24f2000-04-13 02:42:50 +0000371"unichr(i) -> Unicode character\n\
Guido van Rossum09095f32000-03-10 23:00:52 +0000372\n\
Fred Drake078b24f2000-04-13 02:42:50 +0000373Return a Unicode string of one character with ordinal i; 0 <= i < 65536.";
Guido van Rossum09095f32000-03-10 23:00:52 +0000374
375
376static PyObject *
Guido van Rossuma9e7dc11992-10-18 18:53:57 +0000377builtin_cmp(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000378 PyObject *self;
379 PyObject *args;
Guido van Rossuma9e7dc11992-10-18 18:53:57 +0000380{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000381 PyObject *a, *b;
Guido van Rossum09df08a1998-05-22 00:51:39 +0000382 int c;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000383
Guido van Rossum79f25d91997-04-29 20:08:16 +0000384 if (!PyArg_ParseTuple(args, "OO:cmp", &a, &b))
Guido van Rossuma9e7dc11992-10-18 18:53:57 +0000385 return NULL;
Guido van Rossum09df08a1998-05-22 00:51:39 +0000386 if (PyObject_Cmp(a, b, &c) < 0)
Guido van Rossumc8b6df91997-05-23 00:06:51 +0000387 return NULL;
Guido van Rossum09df08a1998-05-22 00:51:39 +0000388 return PyInt_FromLong((long)c);
Guido van Rossuma9e7dc11992-10-18 18:53:57 +0000389}
390
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000391static char cmp_doc[] =
392"cmp(x, y) -> integer\n\
393\n\
394Return negative if x<y, zero if x==y, positive if x>y.";
395
396
Guido van Rossum79f25d91997-04-29 20:08:16 +0000397static PyObject *
Guido van Rossum5524a591995-01-10 15:26:20 +0000398builtin_coerce(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000399 PyObject *self;
400 PyObject *args;
Guido van Rossum6a00cd81995-01-07 12:39:01 +0000401{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000402 PyObject *v, *w;
403 PyObject *res;
Guido van Rossum5524a591995-01-10 15:26:20 +0000404
Guido van Rossum79f25d91997-04-29 20:08:16 +0000405 if (!PyArg_ParseTuple(args, "OO:coerce", &v, &w))
Guido van Rossum5524a591995-01-10 15:26:20 +0000406 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000407 if (PyNumber_Coerce(&v, &w) < 0)
Guido van Rossum04691fc1992-08-12 15:35:34 +0000408 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000409 res = Py_BuildValue("(OO)", v, w);
410 Py_DECREF(v);
411 Py_DECREF(w);
Guido van Rossum04691fc1992-08-12 15:35:34 +0000412 return res;
413}
414
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000415static char coerce_doc[] =
416"coerce(x, y) -> None or (x1, y1)\n\
417\n\
418When x and y can be coerced to values of the same type, return a tuple\n\
419containing the coerced values. When they can't be coerced, return None.";
420
421
Guido van Rossum79f25d91997-04-29 20:08:16 +0000422static PyObject *
Guido van Rossum5b722181993-03-30 17:46:03 +0000423builtin_compile(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000424 PyObject *self;
425 PyObject *args;
Guido van Rossum5b722181993-03-30 17:46:03 +0000426{
427 char *str;
428 char *filename;
429 char *startstr;
430 int start;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000431
Guido van Rossum79f25d91997-04-29 20:08:16 +0000432 if (!PyArg_ParseTuple(args, "sss:compile", &str, &filename, &startstr))
Guido van Rossum5b722181993-03-30 17:46:03 +0000433 return NULL;
434 if (strcmp(startstr, "exec") == 0)
Guido van Rossumb05a5c71997-05-07 17:46:13 +0000435 start = Py_file_input;
Guido van Rossum5b722181993-03-30 17:46:03 +0000436 else if (strcmp(startstr, "eval") == 0)
Guido van Rossumb05a5c71997-05-07 17:46:13 +0000437 start = Py_eval_input;
Guido van Rossum872537c1995-07-07 22:43:42 +0000438 else if (strcmp(startstr, "single") == 0)
Guido van Rossumb05a5c71997-05-07 17:46:13 +0000439 start = Py_single_input;
Guido van Rossum5b722181993-03-30 17:46:03 +0000440 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000441 PyErr_SetString(PyExc_ValueError,
Guido van Rossum872537c1995-07-07 22:43:42 +0000442 "compile() mode must be 'exec' or 'eval' or 'single'");
Guido van Rossum5b722181993-03-30 17:46:03 +0000443 return NULL;
444 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000445 return Py_CompileString(str, filename, start);
Guido van Rossum5b722181993-03-30 17:46:03 +0000446}
447
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000448static char compile_doc[] =
449"compile(source, filename, mode) -> code object\n\
450\n\
451Compile the source string (a Python module, statement or expression)\n\
452into a code object that can be executed by the exec statement or eval().\n\
453The filename will be used for run-time error messages.\n\
454The mode must be 'exec' to compile a module, 'single' to compile a\n\
455single (interactive) statement, or 'eval' to compile an expression.";
456
457
Guido van Rossum8a5c5d21996-01-12 01:09:56 +0000458#ifndef WITHOUT_COMPLEX
459
Guido van Rossum79f25d91997-04-29 20:08:16 +0000460static PyObject *
Guido van Rossum11950231999-03-25 21:16:07 +0000461complex_from_string(v)
462 PyObject *v;
463{
464 extern double strtod Py_PROTO((const char *, char **));
Guido van Rossum9e896b32000-04-05 20:11:21 +0000465 const char *s, *start;
466 char *end;
Guido van Rossum11950231999-03-25 21:16:07 +0000467 double x=0.0, y=0.0, z;
468 int got_re=0, got_im=0, done=0;
469 int digit_or_dot;
470 int sw_error=0;
471 int sign;
472 char buffer[256]; /* For errors */
Guido van Rossum9e896b32000-04-05 20:11:21 +0000473 int len;
Guido van Rossum11950231999-03-25 21:16:07 +0000474
Guido van Rossum9e896b32000-04-05 20:11:21 +0000475 if (PyString_Check(v)) {
476 s = PyString_AS_STRING(v);
477 len = PyString_GET_SIZE(v);
478 }
479 else if (PyUnicode_Check(v)) {
480 char s_buffer[256];
481
482 if (PyUnicode_GET_SIZE(v) >= sizeof(s_buffer)) {
483 PyErr_SetString(PyExc_ValueError,
484 "complex() literal too large to convert");
485 return NULL;
486 }
487 if (PyUnicode_EncodeDecimal(PyUnicode_AS_UNICODE(v),
488 PyUnicode_GET_SIZE(v),
489 s_buffer,
490 NULL))
491 return NULL;
492 s = s_buffer;
493 len = strlen(s);
494 }
495 else if (PyObject_AsCharBuffer(v, &s, &len)) {
496 PyErr_SetString(PyExc_TypeError,
497 "complex() needs a string first argument");
498 return NULL;
499 }
Guido van Rossum11950231999-03-25 21:16:07 +0000500
501 /* position on first nonblank */
Guido van Rossum9e896b32000-04-05 20:11:21 +0000502 start = s;
Guido van Rossum11950231999-03-25 21:16:07 +0000503 while (*s && isspace(Py_CHARMASK(*s)))
504 s++;
505 if (s[0] == '\0') {
506 PyErr_SetString(PyExc_ValueError,
507 "empty string for complex()");
508 return NULL;
509 }
510
511 z = -1.0;
512 sign = 1;
513 do {
514
515 switch (*s) {
516
517 case '\0':
Guido van Rossum9e896b32000-04-05 20:11:21 +0000518 if (s-start != len) {
Guido van Rossum11950231999-03-25 21:16:07 +0000519 PyErr_SetString(
520 PyExc_ValueError,
521 "null byte in argument for complex()");
522 return NULL;
523 }
524 if(!done) sw_error=1;
525 break;
526
527 case '-':
528 sign = -1;
529 /* Fallthrough */
530 case '+':
531 if (done) sw_error=1;
532 s++;
533 if ( *s=='\0'||*s=='+'||*s=='-' ||
534 isspace(Py_CHARMASK(*s)) ) sw_error=1;
535 break;
536
537 case 'J':
538 case 'j':
539 if (got_im || done) {
540 sw_error = 1;
541 break;
542 }
543 if (z<0.0) {
544 y=sign;
545 }
546 else{
547 y=sign*z;
548 }
549 got_im=1;
550 s++;
551 if (*s!='+' && *s!='-' )
552 done=1;
553 break;
554
555 default:
556 if (isspace(Py_CHARMASK(*s))) {
557 while (*s && isspace(Py_CHARMASK(*s)))
558 s++;
559 if (s[0] != '\0')
560 sw_error=1;
561 else
562 done = 1;
563 break;
564 }
565 digit_or_dot =
566 (*s=='.' || isdigit(Py_CHARMASK(*s)));
567 if (done||!digit_or_dot) {
568 sw_error=1;
569 break;
570 }
571 errno = 0;
572 PyFPE_START_PROTECT("strtod", return 0)
573 z = strtod(s, &end) ;
574 PyFPE_END_PROTECT(z)
575 if (errno != 0) {
576 sprintf(buffer,
577 "float() out of range: %.150s", s);
578 PyErr_SetString(
579 PyExc_ValueError,
580 buffer);
581 return NULL;
582 }
583 s=end;
584 if (*s=='J' || *s=='j') {
585
586 break;
587 }
588 if (got_re) {
589 sw_error=1;
590 break;
591 }
592
593 /* accept a real part */
594 x=sign*z;
595 got_re=1;
596 if (got_im) done=1;
597 z = -1.0;
598 sign = 1;
599 break;
600
601 } /* end of switch */
602
603 } while (*s!='\0' && !sw_error);
604
605 if (sw_error) {
606 PyErr_SetString(PyExc_ValueError,
607 "malformed string for complex()");
608 return NULL;
609 }
610
611 return PyComplex_FromDoubles(x,y);
612}
613
614static PyObject *
Guido van Rossum8a5c5d21996-01-12 01:09:56 +0000615builtin_complex(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000616 PyObject *self;
617 PyObject *args;
Guido van Rossum8a5c5d21996-01-12 01:09:56 +0000618{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000619 PyObject *r, *i, *tmp;
620 PyNumberMethods *nbr, *nbi = NULL;
Guido van Rossum530956d1996-07-21 02:27:43 +0000621 Py_complex cr, ci;
Guido van Rossumc6472e91997-03-31 17:15:43 +0000622 int own_r = 0;
Guido van Rossum8a5c5d21996-01-12 01:09:56 +0000623
624 i = NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000625 if (!PyArg_ParseTuple(args, "O|O:complex", &r, &i))
Guido van Rossum8a5c5d21996-01-12 01:09:56 +0000626 return NULL;
Guido van Rossum9e896b32000-04-05 20:11:21 +0000627 if (PyString_Check(r) || PyUnicode_Check(r))
Guido van Rossum11950231999-03-25 21:16:07 +0000628 return complex_from_string(r);
Guido van Rossum8a5c5d21996-01-12 01:09:56 +0000629 if ((nbr = r->ob_type->tp_as_number) == NULL ||
Guido van Rossumc6472e91997-03-31 17:15:43 +0000630 nbr->nb_float == NULL ||
631 (i != NULL &&
632 ((nbi = i->ob_type->tp_as_number) == NULL ||
633 nbi->nb_float == NULL))) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000634 PyErr_SetString(PyExc_TypeError,
Guido van Rossum8a5c5d21996-01-12 01:09:56 +0000635 "complex() argument can't be converted to complex");
636 return NULL;
637 }
Guido van Rossumed0af8f1996-12-05 23:18:18 +0000638 /* XXX Hack to support classes with __complex__ method */
Guido van Rossum79f25d91997-04-29 20:08:16 +0000639 if (PyInstance_Check(r)) {
640 static PyObject *complexstr;
641 PyObject *f;
Guido van Rossumed0af8f1996-12-05 23:18:18 +0000642 if (complexstr == NULL) {
Guido van Rossum8d751611997-01-18 08:04:16 +0000643 complexstr = PyString_InternFromString("__complex__");
Guido van Rossumed0af8f1996-12-05 23:18:18 +0000644 if (complexstr == NULL)
645 return NULL;
646 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000647 f = PyObject_GetAttr(r, complexstr);
Guido van Rossumed0af8f1996-12-05 23:18:18 +0000648 if (f == NULL)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000649 PyErr_Clear();
Guido van Rossumed0af8f1996-12-05 23:18:18 +0000650 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000651 PyObject *args = Py_BuildValue("()");
Guido van Rossumed0af8f1996-12-05 23:18:18 +0000652 if (args == NULL)
653 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000654 r = PyEval_CallObject(f, args);
655 Py_DECREF(args);
Barry Warsawf988e681999-01-27 23:13:59 +0000656 Py_DECREF(f);
Guido van Rossumed0af8f1996-12-05 23:18:18 +0000657 if (r == NULL)
658 return NULL;
Guido van Rossumc6472e91997-03-31 17:15:43 +0000659 own_r = 1;
Guido van Rossumed0af8f1996-12-05 23:18:18 +0000660 }
661 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000662 if (PyComplex_Check(r)) {
663 cr = ((PyComplexObject*)r)->cval;
Guido van Rossum730806d1998-04-10 22:27:42 +0000664 if (own_r) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000665 Py_DECREF(r);
Guido van Rossum730806d1998-04-10 22:27:42 +0000666 }
Guido van Rossumc6472e91997-03-31 17:15:43 +0000667 }
Guido van Rossum8a5c5d21996-01-12 01:09:56 +0000668 else {
Guido van Rossumfe4b6ee1996-08-08 18:49:41 +0000669 tmp = (*nbr->nb_float)(r);
Guido van Rossum730806d1998-04-10 22:27:42 +0000670 if (own_r) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000671 Py_DECREF(r);
Guido van Rossum730806d1998-04-10 22:27:42 +0000672 }
Guido van Rossumfe4b6ee1996-08-08 18:49:41 +0000673 if (tmp == NULL)
674 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000675 cr.real = PyFloat_AsDouble(tmp);
676 Py_DECREF(tmp);
Guido van Rossum11950231999-03-25 21:16:07 +0000677 cr.imag = 0.0;
Guido van Rossum8a5c5d21996-01-12 01:09:56 +0000678 }
679 if (i == NULL) {
Guido van Rossum11950231999-03-25 21:16:07 +0000680 ci.real = 0.0;
681 ci.imag = 0.0;
Guido van Rossum8a5c5d21996-01-12 01:09:56 +0000682 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000683 else if (PyComplex_Check(i))
684 ci = ((PyComplexObject*)i)->cval;
Guido van Rossum8a5c5d21996-01-12 01:09:56 +0000685 else {
Guido van Rossumc6472e91997-03-31 17:15:43 +0000686 tmp = (*nbi->nb_float)(i);
Guido van Rossumfe4b6ee1996-08-08 18:49:41 +0000687 if (tmp == NULL)
688 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000689 ci.real = PyFloat_AsDouble(tmp);
690 Py_DECREF(tmp);
Guido van Rossum8a5c5d21996-01-12 01:09:56 +0000691 ci.imag = 0.;
692 }
693 cr.real -= ci.imag;
694 cr.imag += ci.real;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000695 return PyComplex_FromCComplex(cr);
Guido van Rossum8a5c5d21996-01-12 01:09:56 +0000696}
697
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000698static char complex_doc[] =
699"complex(real[, imag]) -> complex number\n\
700\n\
701Create a complex number from a real part and an optional imaginary part.\n\
702This is equivalent to (real + imag*1j) where imag defaults to 0.";
703
704
Guido van Rossum8a5c5d21996-01-12 01:09:56 +0000705#endif
706
Guido van Rossum79f25d91997-04-29 20:08:16 +0000707static PyObject *
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000708builtin_dir(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000709 PyObject *self;
710 PyObject *args;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000711{
Guido van Rossum666b17a1997-05-06 16:36:57 +0000712 static char *attrlist[] = {"__members__", "__methods__", NULL};
713 PyObject *v = NULL, *l = NULL, *m = NULL;
714 PyObject *d, *x;
715 int i;
716 char **s;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000717
Guido van Rossum79f25d91997-04-29 20:08:16 +0000718 if (!PyArg_ParseTuple(args, "|O:dir", &v))
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000719 return NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000720 if (v == NULL) {
Guido van Rossum666b17a1997-05-06 16:36:57 +0000721 x = PyEval_GetLocals();
722 if (x == NULL)
723 goto error;
724 l = PyMapping_Keys(x);
725 if (l == NULL)
726 goto error;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000727 }
728 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000729 d = PyObject_GetAttrString(v, "__dict__");
Guido van Rossum666b17a1997-05-06 16:36:57 +0000730 if (d == NULL)
Guido van Rossum795ba581996-05-23 22:49:07 +0000731 PyErr_Clear();
Guido van Rossum666b17a1997-05-06 16:36:57 +0000732 else {
733 l = PyMapping_Keys(d);
734 if (l == NULL)
735 PyErr_Clear();
736 Py_DECREF(d);
737 }
738 if (l == NULL) {
739 l = PyList_New(0);
740 if (l == NULL)
741 goto error;
742 }
743 for (s = attrlist; *s != NULL; s++) {
744 m = PyObject_GetAttrString(v, *s);
745 if (m == NULL) {
746 PyErr_Clear();
747 continue;
748 }
749 for (i = 0; ; i++) {
750 x = PySequence_GetItem(m, i);
751 if (x == NULL) {
752 PyErr_Clear();
753 break;
754 }
755 if (PyList_Append(l, x) != 0) {
756 Py_DECREF(x);
757 Py_DECREF(m);
758 goto error;
759 }
760 Py_DECREF(x);
761 }
762 Py_DECREF(m);
Guido van Rossum795ba581996-05-23 22:49:07 +0000763 }
Guido van Rossum006bcd41991-10-24 14:54:44 +0000764 }
Guido van Rossum666b17a1997-05-06 16:36:57 +0000765 if (PyList_Sort(l) != 0)
766 goto error;
767 return l;
768 error:
769 Py_XDECREF(l);
770 return NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000771}
772
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000773static char dir_doc[] =
774"dir([object]) -> list of strings\n\
775\n\
776Return an alphabetized list of names comprising (some of) the attributes\n\
777of the given object. Without an argument, the names in the current scope\n\
778are listed. With an instance argument, only the instance attributes are\n\
779returned. With a class argument, attributes of the base class are not\n\
780returned. For other types or arguments, this may list members or methods.";
781
782
Guido van Rossum79f25d91997-04-29 20:08:16 +0000783static PyObject *
Guido van Rossum6a00cd81995-01-07 12:39:01 +0000784builtin_divmod(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000785 PyObject *self;
786 PyObject *args;
Guido van Rossum6a00cd81995-01-07 12:39:01 +0000787{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000788 PyObject *v, *w;
Guido van Rossum6a00cd81995-01-07 12:39:01 +0000789
Guido van Rossum79f25d91997-04-29 20:08:16 +0000790 if (!PyArg_ParseTuple(args, "OO:divmod", &v, &w))
Guido van Rossum6a00cd81995-01-07 12:39:01 +0000791 return NULL;
Guido van Rossum09df08a1998-05-22 00:51:39 +0000792 return PyNumber_Divmod(v, w);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000793}
794
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000795static char divmod_doc[] =
796"divmod(x, y) -> (div, mod)\n\
797\n\
798Return the tuple ((x-x%y)/y, x%y). Invariant: div*y + mod == x.";
799
800
Guido van Rossum79f25d91997-04-29 20:08:16 +0000801static PyObject *
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000802builtin_eval(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000803 PyObject *self;
804 PyObject *args;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000805{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000806 PyObject *cmd;
807 PyObject *globals = Py_None, *locals = Py_None;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000808 char *str;
Guido van Rossum590baa41993-11-30 13:40:46 +0000809
Guido van Rossum79f25d91997-04-29 20:08:16 +0000810 if (!PyArg_ParseTuple(args, "O|O!O!:eval",
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000811 &cmd,
Guido van Rossum79f25d91997-04-29 20:08:16 +0000812 &PyDict_Type, &globals,
813 &PyDict_Type, &locals))
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000814 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000815 if (globals == Py_None) {
816 globals = PyEval_GetGlobals();
817 if (locals == Py_None)
818 locals = PyEval_GetLocals();
Guido van Rossum6135a871995-01-09 17:53:26 +0000819 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000820 else if (locals == Py_None)
Guido van Rossum6135a871995-01-09 17:53:26 +0000821 locals = globals;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000822 if (PyDict_GetItemString(globals, "__builtins__") == NULL) {
823 if (PyDict_SetItemString(globals, "__builtins__",
824 PyEval_GetBuiltins()) != 0)
Guido van Rossum6135a871995-01-09 17:53:26 +0000825 return NULL;
826 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000827 if (PyCode_Check(cmd))
828 return PyEval_EvalCode((PyCodeObject *) cmd, globals, locals);
829 if (!PyString_Check(cmd)) {
830 PyErr_SetString(PyExc_TypeError,
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000831 "eval() argument 1 must be string or code object");
Guido van Rossum94390a41992-08-14 15:14:30 +0000832 return NULL;
833 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000834 str = PyString_AsString(cmd);
Guido van Rossum106f2da2000-06-28 21:12:25 +0000835 if (strlen(str) != (size_t)PyString_Size(cmd)) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000836 PyErr_SetString(PyExc_ValueError,
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000837 "embedded '\\0' in string arg");
838 return NULL;
Guido van Rossumf08ab0a1992-03-04 16:41:41 +0000839 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000840 while (*str == ' ' || *str == '\t')
841 str++;
Guido van Rossumb05a5c71997-05-07 17:46:13 +0000842 return PyRun_String(str, Py_eval_input, globals, locals);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000843}
844
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000845static char eval_doc[] =
846"eval(source[, globals[, locals]]) -> value\n\
847\n\
848Evaluate the source in the context of globals and locals.\n\
849The source may be a string representing a Python expression\n\
850or a code object as returned by compile().\n\
851The globals and locals are dictionaries, defaulting to the current\n\
852globals and locals. If only globals is given, locals defaults to it.";
853
854
Guido van Rossum79f25d91997-04-29 20:08:16 +0000855static PyObject *
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000856builtin_execfile(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000857 PyObject *self;
858 PyObject *args;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000859{
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000860 char *filename;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000861 PyObject *globals = Py_None, *locals = Py_None;
862 PyObject *res;
Guido van Rossum0f61f8a1992-02-25 18:55:05 +0000863 FILE* fp;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000864
Guido van Rossum79f25d91997-04-29 20:08:16 +0000865 if (!PyArg_ParseTuple(args, "s|O!O!:execfile",
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000866 &filename,
Guido van Rossum79f25d91997-04-29 20:08:16 +0000867 &PyDict_Type, &globals,
868 &PyDict_Type, &locals))
Guido van Rossum0f61f8a1992-02-25 18:55:05 +0000869 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000870 if (globals == Py_None) {
871 globals = PyEval_GetGlobals();
872 if (locals == Py_None)
873 locals = PyEval_GetLocals();
Guido van Rossum6135a871995-01-09 17:53:26 +0000874 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000875 else if (locals == Py_None)
Guido van Rossum6135a871995-01-09 17:53:26 +0000876 locals = globals;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000877 if (PyDict_GetItemString(globals, "__builtins__") == NULL) {
878 if (PyDict_SetItemString(globals, "__builtins__",
879 PyEval_GetBuiltins()) != 0)
Guido van Rossum6135a871995-01-09 17:53:26 +0000880 return NULL;
881 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000882 Py_BEGIN_ALLOW_THREADS
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000883 fp = fopen(filename, "r");
Guido van Rossum79f25d91997-04-29 20:08:16 +0000884 Py_END_ALLOW_THREADS
Guido van Rossum0f61f8a1992-02-25 18:55:05 +0000885 if (fp == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000886 PyErr_SetFromErrno(PyExc_IOError);
Guido van Rossum0f61f8a1992-02-25 18:55:05 +0000887 return NULL;
888 }
Guido van Rossumb05a5c71997-05-07 17:46:13 +0000889 res = PyRun_File(fp, filename, Py_file_input, globals, locals);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000890 Py_BEGIN_ALLOW_THREADS
Guido van Rossum0f61f8a1992-02-25 18:55:05 +0000891 fclose(fp);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000892 Py_END_ALLOW_THREADS
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000893 return res;
Guido van Rossum0f61f8a1992-02-25 18:55:05 +0000894}
895
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000896static char execfile_doc[] =
897"execfile(filename[, globals[, locals]])\n\
898\n\
899Read and execute a Python script from a file.\n\
900The globals and locals are dictionaries, defaulting to the current\n\
901globals and locals. If only globals is given, locals defaults to it.";
902
903
Guido van Rossum79f25d91997-04-29 20:08:16 +0000904static PyObject *
Guido van Rossum94390a41992-08-14 15:14:30 +0000905builtin_getattr(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000906 PyObject *self;
907 PyObject *args;
Guido van Rossum33894be1992-01-27 16:53:09 +0000908{
Guido van Rossum950ff291998-06-29 13:38:57 +0000909 PyObject *v, *result, *dflt = NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000910 PyObject *name;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000911
Guido van Rossum950ff291998-06-29 13:38:57 +0000912 if (!PyArg_ParseTuple(args, "OS|O:getattr", &v, &name, &dflt))
Guido van Rossum33894be1992-01-27 16:53:09 +0000913 return NULL;
Guido van Rossum950ff291998-06-29 13:38:57 +0000914 result = PyObject_GetAttr(v, name);
915 if (result == NULL && dflt != NULL) {
916 PyErr_Clear();
917 Py_INCREF(dflt);
918 result = dflt;
919 }
920 return result;
Guido van Rossum9bfef441993-03-29 10:43:31 +0000921}
922
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000923static char getattr_doc[] =
Guido van Rossum950ff291998-06-29 13:38:57 +0000924"getattr(object, name[, default]) -> value\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000925\n\
Guido van Rossum950ff291998-06-29 13:38:57 +0000926Get a named attribute from an object; getattr(x, 'y') is equivalent to x.y.\n\
927When a default argument is given, it is returned when the attribute doesn't\n\
928exist; without it, an exception is raised in that case.";
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000929
930
Guido van Rossum79f25d91997-04-29 20:08:16 +0000931static PyObject *
Guido van Rossum872537c1995-07-07 22:43:42 +0000932builtin_globals(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000933 PyObject *self;
934 PyObject *args;
Guido van Rossum872537c1995-07-07 22:43:42 +0000935{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000936 PyObject *d;
Guido van Rossum872537c1995-07-07 22:43:42 +0000937
Guido van Rossum43713e52000-02-29 13:59:29 +0000938 if (!PyArg_ParseTuple(args, ":globals"))
Guido van Rossum872537c1995-07-07 22:43:42 +0000939 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000940 d = PyEval_GetGlobals();
941 Py_INCREF(d);
Guido van Rossum872537c1995-07-07 22:43:42 +0000942 return d;
943}
944
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000945static char globals_doc[] =
946"globals() -> dictionary\n\
947\n\
948Return the dictionary containing the current scope's global variables.";
949
950
Guido van Rossum79f25d91997-04-29 20:08:16 +0000951static PyObject *
Guido van Rossum9bfef441993-03-29 10:43:31 +0000952builtin_hasattr(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000953 PyObject *self;
954 PyObject *args;
Guido van Rossum9bfef441993-03-29 10:43:31 +0000955{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000956 PyObject *v;
957 PyObject *name;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000958
Guido van Rossum79f25d91997-04-29 20:08:16 +0000959 if (!PyArg_ParseTuple(args, "OS:hasattr", &v, &name))
Guido van Rossum9bfef441993-03-29 10:43:31 +0000960 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000961 v = PyObject_GetAttr(v, name);
Guido van Rossum9bfef441993-03-29 10:43:31 +0000962 if (v == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000963 PyErr_Clear();
Guido van Rossum09df08a1998-05-22 00:51:39 +0000964 Py_INCREF(Py_False);
965 return Py_False;
Guido van Rossum9bfef441993-03-29 10:43:31 +0000966 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000967 Py_DECREF(v);
Guido van Rossum09df08a1998-05-22 00:51:39 +0000968 Py_INCREF(Py_True);
969 return Py_True;
Guido van Rossum33894be1992-01-27 16:53:09 +0000970}
971
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000972static char hasattr_doc[] =
973"hasattr(object, name) -> Boolean\n\
974\n\
975Return whether the object has an attribute with the given name.\n\
976(This is done by calling getattr(object, name) and catching exceptions.)";
977
978
Guido van Rossum79f25d91997-04-29 20:08:16 +0000979static PyObject *
Guido van Rossum5b722181993-03-30 17:46:03 +0000980builtin_id(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000981 PyObject *self;
982 PyObject *args;
Guido van Rossum5b722181993-03-30 17:46:03 +0000983{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000984 PyObject *v;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000985
Guido van Rossum79f25d91997-04-29 20:08:16 +0000986 if (!PyArg_ParseTuple(args, "O:id", &v))
Guido van Rossum5b722181993-03-30 17:46:03 +0000987 return NULL;
Guido van Rossum106f2da2000-06-28 21:12:25 +0000988 return PyLong_FromVoidPtr(v);
Guido van Rossum5b722181993-03-30 17:46:03 +0000989}
990
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000991static char id_doc[] =
992"id(object) -> integer\n\
993\n\
994Return the identity of an object. This is guaranteed to be unique among\n\
995simultaneously existing objects. (Hint: it's the object's memory address.)";
996
997
Guido van Rossum79f25d91997-04-29 20:08:16 +0000998static PyObject *
Guido van Rossum12d12c51993-10-26 17:58:25 +0000999builtin_map(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001000 PyObject *self;
1001 PyObject *args;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001002{
1003 typedef struct {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001004 PyObject *seq;
1005 PySequenceMethods *sqf;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001006 int len;
1007 } sequence;
1008
Guido van Rossum79f25d91997-04-29 20:08:16 +00001009 PyObject *func, *result;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001010 sequence *seqs = NULL, *sqp;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001011 int n, len;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001012 register int i, j;
1013
Guido van Rossum79f25d91997-04-29 20:08:16 +00001014 n = PyTuple_Size(args);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001015 if (n < 2) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001016 PyErr_SetString(PyExc_TypeError,
1017 "map() requires at least two args");
Guido van Rossum12d12c51993-10-26 17:58:25 +00001018 return NULL;
1019 }
1020
Guido van Rossum79f25d91997-04-29 20:08:16 +00001021 func = PyTuple_GetItem(args, 0);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001022 n--;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001023
Guido van Rossumfa4ac711998-07-10 17:37:30 +00001024 if (func == Py_None && n == 1) {
1025 /* map(None, S) is the same as list(S). */
1026 return PySequence_List(PyTuple_GetItem(args, 1));
1027 }
1028
Guido van Rossum79f25d91997-04-29 20:08:16 +00001029 if ((seqs = PyMem_NEW(sequence, n)) == NULL) {
1030 PyErr_NoMemory();
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00001031 goto Fail_2;
1032 }
Guido van Rossum12d12c51993-10-26 17:58:25 +00001033
Guido van Rossum2d951851994-08-29 12:52:16 +00001034 for (len = 0, i = 0, sqp = seqs; i < n; ++i, ++sqp) {
Guido van Rossum12d12c51993-10-26 17:58:25 +00001035 int curlen;
Guido van Rossum09df08a1998-05-22 00:51:39 +00001036 PySequenceMethods *sqf;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001037
Guido van Rossum79f25d91997-04-29 20:08:16 +00001038 if ((sqp->seq = PyTuple_GetItem(args, i + 1)) == NULL)
Guido van Rossum12d12c51993-10-26 17:58:25 +00001039 goto Fail_2;
1040
Guido van Rossum09df08a1998-05-22 00:51:39 +00001041 sqp->sqf = sqf = sqp->seq->ob_type->tp_as_sequence;
1042 if (sqf == NULL ||
1043 sqf->sq_length == NULL ||
1044 sqf->sq_item == NULL)
1045 {
Guido van Rossum12d12c51993-10-26 17:58:25 +00001046 static char errmsg[] =
1047 "argument %d to map() must be a sequence object";
Guido van Rossum15e33a41997-04-30 19:00:27 +00001048 char errbuf[sizeof(errmsg) + 25];
Guido van Rossum12d12c51993-10-26 17:58:25 +00001049
1050 sprintf(errbuf, errmsg, i+2);
Guido van Rossum79f25d91997-04-29 20:08:16 +00001051 PyErr_SetString(PyExc_TypeError, errbuf);
Guido van Rossum12d12c51993-10-26 17:58:25 +00001052 goto Fail_2;
1053 }
1054
1055 if ((curlen = sqp->len = (*sqp->sqf->sq_length)(sqp->seq)) < 0)
1056 goto Fail_2;
1057
1058 if (curlen > len)
1059 len = curlen;
1060 }
1061
Guido van Rossum79f25d91997-04-29 20:08:16 +00001062 if ((result = (PyObject *) PyList_New(len)) == NULL)
Guido van Rossum12d12c51993-10-26 17:58:25 +00001063 goto Fail_2;
1064
Guido van Rossum2d951851994-08-29 12:52:16 +00001065 for (i = 0; ; ++i) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001066 PyObject *alist, *item=NULL, *value;
Guido van Rossum2d951851994-08-29 12:52:16 +00001067 int any = 0;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001068
Guido van Rossum79f25d91997-04-29 20:08:16 +00001069 if (func == Py_None && n == 1)
Guido van Rossum32120311995-07-10 13:52:21 +00001070 alist = NULL;
Guido van Rossum2d951851994-08-29 12:52:16 +00001071 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001072 if ((alist = PyTuple_New(n)) == NULL)
Guido van Rossum2d951851994-08-29 12:52:16 +00001073 goto Fail_1;
1074 }
Guido van Rossum12d12c51993-10-26 17:58:25 +00001075
1076 for (j = 0, sqp = seqs; j < n; ++j, ++sqp) {
Guido van Rossum2d951851994-08-29 12:52:16 +00001077 if (sqp->len < 0) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001078 Py_INCREF(Py_None);
1079 item = Py_None;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001080 }
Guido van Rossum12d12c51993-10-26 17:58:25 +00001081 else {
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00001082 item = (*sqp->sqf->sq_item)(sqp->seq, i);
Guido van Rossum2d951851994-08-29 12:52:16 +00001083 if (item == NULL) {
Barry Warsawcde8b1b1997-08-22 21:14:38 +00001084 if (PyErr_ExceptionMatches(
1085 PyExc_IndexError))
1086 {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001087 PyErr_Clear();
1088 Py_INCREF(Py_None);
1089 item = Py_None;
Guido van Rossum2d951851994-08-29 12:52:16 +00001090 sqp->len = -1;
1091 }
1092 else {
1093 goto Fail_0;
1094 }
1095 }
1096 else
1097 any = 1;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001098
Guido van Rossum12d12c51993-10-26 17:58:25 +00001099 }
Guido van Rossum32120311995-07-10 13:52:21 +00001100 if (!alist)
Guido van Rossum2d951851994-08-29 12:52:16 +00001101 break;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001102 if (PyTuple_SetItem(alist, j, item) < 0) {
1103 Py_DECREF(item);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00001104 goto Fail_0;
Guido van Rossum2d951851994-08-29 12:52:16 +00001105 }
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00001106 continue;
1107
1108 Fail_0:
Guido van Rossum79f25d91997-04-29 20:08:16 +00001109 Py_XDECREF(alist);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00001110 goto Fail_1;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001111 }
1112
Guido van Rossum32120311995-07-10 13:52:21 +00001113 if (!alist)
1114 alist = item;
Guido van Rossum2d951851994-08-29 12:52:16 +00001115
1116 if (!any) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001117 Py_DECREF(alist);
Guido van Rossum2d951851994-08-29 12:52:16 +00001118 break;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001119 }
Guido van Rossum2d951851994-08-29 12:52:16 +00001120
Guido van Rossum79f25d91997-04-29 20:08:16 +00001121 if (func == Py_None)
Guido van Rossum32120311995-07-10 13:52:21 +00001122 value = alist;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001123 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001124 value = PyEval_CallObject(func, alist);
1125 Py_DECREF(alist);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00001126 if (value == NULL)
1127 goto Fail_1;
Guido van Rossum2d951851994-08-29 12:52:16 +00001128 }
1129 if (i >= len) {
Barry Warsawfa77e091999-01-28 18:49:12 +00001130 int status = PyList_Append(result, value);
Barry Warsaw21332871999-01-28 04:21:35 +00001131 Py_DECREF(value);
Barry Warsawfa77e091999-01-28 18:49:12 +00001132 if (status < 0)
1133 goto Fail_1;
Guido van Rossum2d951851994-08-29 12:52:16 +00001134 }
1135 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001136 if (PyList_SetItem(result, i, value) < 0)
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00001137 goto Fail_1;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001138 }
1139 }
1140
Guido van Rossumfa4ac711998-07-10 17:37:30 +00001141 if (i < len && PyList_SetSlice(result, i, len, NULL) < 0)
1142 goto Fail_1;
1143
Guido van Rossum79f25d91997-04-29 20:08:16 +00001144 PyMem_DEL(seqs);
Guido van Rossum12d12c51993-10-26 17:58:25 +00001145 return result;
1146
Guido van Rossum12d12c51993-10-26 17:58:25 +00001147Fail_1:
Guido van Rossum79f25d91997-04-29 20:08:16 +00001148 Py_DECREF(result);
Guido van Rossum12d12c51993-10-26 17:58:25 +00001149Fail_2:
Guido van Rossum79f25d91997-04-29 20:08:16 +00001150 if (seqs) PyMem_DEL(seqs);
Guido van Rossum12d12c51993-10-26 17:58:25 +00001151 return NULL;
1152}
1153
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001154static char map_doc[] =
1155"map(function, sequence[, sequence, ...]) -> list\n\
1156\n\
1157Return a list of the results of applying the function to the items of\n\
1158the argument sequence(s). If more than one sequence is given, the\n\
1159function is called with an argument list consisting of the corresponding\n\
1160item of each sequence, substituting None for missing values when not all\n\
1161sequences have the same length. If the function is None, return a list of\n\
1162the items of the sequence (or a list of tuples if more than one sequence).";
1163
1164
Guido van Rossum79f25d91997-04-29 20:08:16 +00001165static PyObject *
Guido van Rossum94390a41992-08-14 15:14:30 +00001166builtin_setattr(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001167 PyObject *self;
1168 PyObject *args;
Guido van Rossum33894be1992-01-27 16:53:09 +00001169{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001170 PyObject *v;
1171 PyObject *name;
1172 PyObject *value;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001173
Guido van Rossum79f25d91997-04-29 20:08:16 +00001174 if (!PyArg_ParseTuple(args, "OSO:setattr", &v, &name, &value))
Guido van Rossum33894be1992-01-27 16:53:09 +00001175 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001176 if (PyObject_SetAttr(v, name, value) != 0)
Guido van Rossum33894be1992-01-27 16:53:09 +00001177 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001178 Py_INCREF(Py_None);
1179 return Py_None;
Guido van Rossum33894be1992-01-27 16:53:09 +00001180}
1181
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001182static char setattr_doc[] =
1183"setattr(object, name, value)\n\
1184\n\
1185Set a named attribute on an object; setattr(x, 'y', v) is equivalent to\n\
1186``x.y = v''.";
1187
1188
Guido van Rossum79f25d91997-04-29 20:08:16 +00001189static PyObject *
Guido van Rossum14144fc1994-08-29 12:53:40 +00001190builtin_delattr(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001191 PyObject *self;
1192 PyObject *args;
Guido van Rossum14144fc1994-08-29 12:53:40 +00001193{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001194 PyObject *v;
1195 PyObject *name;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001196
Guido van Rossum79f25d91997-04-29 20:08:16 +00001197 if (!PyArg_ParseTuple(args, "OS:delattr", &v, &name))
Guido van Rossum14144fc1994-08-29 12:53:40 +00001198 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001199 if (PyObject_SetAttr(v, name, (PyObject *)NULL) != 0)
Guido van Rossum14144fc1994-08-29 12:53:40 +00001200 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001201 Py_INCREF(Py_None);
1202 return Py_None;
Guido van Rossum14144fc1994-08-29 12:53:40 +00001203}
1204
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001205static char delattr_doc[] =
Guido van Rossumdf12a591998-11-23 22:13:04 +00001206"delattr(object, name)\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001207\n\
1208Delete a named attribute on an object; delattr(x, 'y') is equivalent to\n\
1209``del x.y''.";
1210
1211
Guido van Rossum79f25d91997-04-29 20:08:16 +00001212static PyObject *
Guido van Rossum9bfef441993-03-29 10:43:31 +00001213builtin_hash(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001214 PyObject *self;
1215 PyObject *args;
Guido van Rossum9bfef441993-03-29 10:43:31 +00001216{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001217 PyObject *v;
Guido van Rossum9bfef441993-03-29 10:43:31 +00001218 long x;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001219
Guido van Rossum79f25d91997-04-29 20:08:16 +00001220 if (!PyArg_ParseTuple(args, "O:hash", &v))
Guido van Rossum9bfef441993-03-29 10:43:31 +00001221 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001222 x = PyObject_Hash(v);
Guido van Rossum9bfef441993-03-29 10:43:31 +00001223 if (x == -1)
1224 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001225 return PyInt_FromLong(x);
Guido van Rossum9bfef441993-03-29 10:43:31 +00001226}
1227
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001228static char hash_doc[] =
1229"hash(object) -> integer\n\
1230\n\
1231Return a hash value for the object. Two objects with the same value have\n\
1232the same hash value. The reverse is not necessarily true, but likely.";
1233
1234
Guido van Rossum79f25d91997-04-29 20:08:16 +00001235static PyObject *
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001236builtin_hex(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001237 PyObject *self;
1238 PyObject *args;
Guido van Rossum006bcd41991-10-24 14:54:44 +00001239{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001240 PyObject *v;
1241 PyNumberMethods *nb;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001242
Guido van Rossum79f25d91997-04-29 20:08:16 +00001243 if (!PyArg_ParseTuple(args, "O:hex", &v))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001244 return NULL;
Guido van Rossum1899c2e1992-09-12 11:09:23 +00001245
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001246 if ((nb = v->ob_type->tp_as_number) == NULL ||
Guido van Rossum1899c2e1992-09-12 11:09:23 +00001247 nb->nb_hex == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001248 PyErr_SetString(PyExc_TypeError,
Guido van Rossum1899c2e1992-09-12 11:09:23 +00001249 "hex() argument can't be converted to hex");
1250 return NULL;
Guido van Rossum006bcd41991-10-24 14:54:44 +00001251 }
Guido van Rossum1899c2e1992-09-12 11:09:23 +00001252 return (*nb->nb_hex)(v);
Guido van Rossum006bcd41991-10-24 14:54:44 +00001253}
1254
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001255static char hex_doc[] =
1256"hex(number) -> string\n\
1257\n\
1258Return the hexadecimal representation of an integer or long integer.";
1259
1260
Guido van Rossum79f25d91997-04-29 20:08:16 +00001261static PyObject *builtin_raw_input Py_PROTO((PyObject *, PyObject *));
Guido van Rossum3165fe61992-09-25 21:59:05 +00001262
Guido van Rossum79f25d91997-04-29 20:08:16 +00001263static PyObject *
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001264builtin_input(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001265 PyObject *self;
1266 PyObject *args;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001267{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001268 PyObject *line;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001269 char *str;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001270 PyObject *res;
1271 PyObject *globals, *locals;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001272
1273 line = builtin_raw_input(self, args);
Guido van Rossum3165fe61992-09-25 21:59:05 +00001274 if (line == NULL)
1275 return line;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001276 if (!PyArg_Parse(line, "s;embedded '\\0' in input line", &str))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001277 return NULL;
1278 while (*str == ' ' || *str == '\t')
1279 str++;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001280 globals = PyEval_GetGlobals();
1281 locals = PyEval_GetLocals();
1282 if (PyDict_GetItemString(globals, "__builtins__") == NULL) {
1283 if (PyDict_SetItemString(globals, "__builtins__",
1284 PyEval_GetBuiltins()) != 0)
Guido van Rossum6135a871995-01-09 17:53:26 +00001285 return NULL;
1286 }
Guido van Rossumb05a5c71997-05-07 17:46:13 +00001287 res = PyRun_String(str, Py_eval_input, globals, locals);
Guido van Rossum79f25d91997-04-29 20:08:16 +00001288 Py_DECREF(line);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001289 return res;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001290}
1291
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001292static char input_doc[] =
1293"input([prompt]) -> value\n\
1294\n\
1295Equivalent to eval(raw_input(prompt)).";
1296
1297
Guido van Rossume8811f81997-02-14 15:48:05 +00001298static PyObject *
1299builtin_intern(self, args)
1300 PyObject *self;
1301 PyObject *args;
1302{
1303 PyObject *s;
Guido van Rossum43713e52000-02-29 13:59:29 +00001304 if (!PyArg_ParseTuple(args, "S:intern", &s))
Guido van Rossume8811f81997-02-14 15:48:05 +00001305 return NULL;
1306 Py_INCREF(s);
1307 PyString_InternInPlace(&s);
1308 return s;
1309}
1310
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001311static char intern_doc[] =
1312"intern(string) -> string\n\
1313\n\
1314``Intern'' the given string. This enters the string in the (global)\n\
1315table of interned strings whose purpose is to speed up dictionary lookups.\n\
1316Return the string itself or the previously interned string object with the\n\
1317same value.";
1318
1319
Guido van Rossum79f25d91997-04-29 20:08:16 +00001320static PyObject *
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001321builtin_int(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001322 PyObject *self;
1323 PyObject *args;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001324{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001325 PyObject *v;
Barry Warsaw226ae6c1999-10-12 19:54:53 +00001326 int base = -909; /* unlikely! */
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001327
Barry Warsaw226ae6c1999-10-12 19:54:53 +00001328 if (!PyArg_ParseTuple(args, "O|i:int", &v, &base))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001329 return NULL;
Barry Warsaw226ae6c1999-10-12 19:54:53 +00001330 if (base == -909)
1331 return PyNumber_Int(v);
Guido van Rossum9e896b32000-04-05 20:11:21 +00001332 else if (PyString_Check(v))
1333 return PyInt_FromString(PyString_AS_STRING(v), NULL, base);
1334 else if (PyUnicode_Check(v))
1335 return PyInt_FromUnicode(PyUnicode_AS_UNICODE(v),
1336 PyUnicode_GET_SIZE(v),
1337 base);
1338 else {
Barry Warsaw226ae6c1999-10-12 19:54:53 +00001339 PyErr_SetString(PyExc_TypeError,
1340 "can't convert non-string with explicit base");
1341 return NULL;
1342 }
Guido van Rossum3f5da241990-12-20 15:06:42 +00001343}
1344
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001345static char int_doc[] =
Barry Warsaw226ae6c1999-10-12 19:54:53 +00001346"int(x[, base]) -> integer\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001347\n\
Barry Warsaw226ae6c1999-10-12 19:54:53 +00001348Convert a string or number to an integer, if possible. A floating point\n\
1349argument will be truncated towards zero (this does not include a string\n\
1350representation of a floating point number!) When converting a string, use\n\
1351the optional base. It is an error to supply a base when converting a\n\
1352non-string.";
1353
1354
1355static PyObject *
1356builtin_long(self, args)
1357 PyObject *self;
1358 PyObject *args;
1359{
1360 PyObject *v;
1361 int base = -909; /* unlikely! */
1362
1363 if (!PyArg_ParseTuple(args, "O|i:long", &v, &base))
1364 return NULL;
1365 if (base == -909)
1366 return PyNumber_Long(v);
Guido van Rossum9e896b32000-04-05 20:11:21 +00001367 else if (PyString_Check(v))
1368 return PyLong_FromString(PyString_AS_STRING(v), NULL, base);
1369 else if (PyUnicode_Check(v))
1370 return PyLong_FromUnicode(PyUnicode_AS_UNICODE(v),
1371 PyUnicode_GET_SIZE(v),
1372 base);
1373 else {
Barry Warsaw226ae6c1999-10-12 19:54:53 +00001374 PyErr_SetString(PyExc_TypeError,
1375 "can't convert non-string with explicit base");
1376 return NULL;
1377 }
Barry Warsaw226ae6c1999-10-12 19:54:53 +00001378}
1379
1380static char long_doc[] =
1381"long(x) -> long integer\n\
1382long(x, base) -> long integer\n\
1383\n\
1384Convert a string or number to a long integer, if possible. A floating\n\
1385point argument will be truncated towards zero (this does not include a\n\
1386string representation of a floating point number!) When converting a\n\
1387string, use the given base. It is an error to supply a base when\n\
1388converting a non-string.";
1389
1390
1391static PyObject *
1392builtin_float(self, args)
1393 PyObject *self;
1394 PyObject *args;
1395{
1396 PyObject *v;
1397
1398 if (!PyArg_ParseTuple(args, "O:float", &v))
1399 return NULL;
1400 if (PyString_Check(v))
1401 return PyFloat_FromString(v, NULL);
1402 return PyNumber_Float(v);
1403}
1404
1405static char float_doc[] =
1406"float(x) -> floating point number\n\
1407\n\
1408Convert a string or number to a floating point number, if possible.";
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001409
1410
Guido van Rossum79f25d91997-04-29 20:08:16 +00001411static PyObject *
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001412builtin_len(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001413 PyObject *self;
1414 PyObject *args;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001415{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001416 PyObject *v;
Guido van Rossum8ea9f4d1998-06-29 22:26:50 +00001417 long res;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001418
Guido van Rossum79f25d91997-04-29 20:08:16 +00001419 if (!PyArg_ParseTuple(args, "O:len", &v))
Guido van Rossum3f5da241990-12-20 15:06:42 +00001420 return NULL;
Guido van Rossum8ea9f4d1998-06-29 22:26:50 +00001421 res = PyObject_Length(v);
1422 if (res < 0 && PyErr_Occurred())
1423 return NULL;
1424 return PyInt_FromLong(res);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001425}
1426
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001427static char len_doc[] =
1428"len(object) -> integer\n\
1429\n\
1430Return the number of items of a sequence or mapping.";
1431
1432
Guido van Rossum79f25d91997-04-29 20:08:16 +00001433static PyObject *
Guido van Rossumd1705771996-04-09 02:41:06 +00001434builtin_list(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001435 PyObject *self;
1436 PyObject *args;
Guido van Rossumd1705771996-04-09 02:41:06 +00001437{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001438 PyObject *v;
Guido van Rossumd1705771996-04-09 02:41:06 +00001439
Guido van Rossum79f25d91997-04-29 20:08:16 +00001440 if (!PyArg_ParseTuple(args, "O:list", &v))
Guido van Rossumd1705771996-04-09 02:41:06 +00001441 return NULL;
Guido van Rossum09df08a1998-05-22 00:51:39 +00001442 return PySequence_List(v);
Guido van Rossumd1705771996-04-09 02:41:06 +00001443}
1444
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001445static char list_doc[] =
1446"list(sequence) -> list\n\
1447\n\
1448Return a new list whose items are the same as those of the argument sequence.";
1449
Guido van Rossum8861b741996-07-30 16:49:37 +00001450
1451static PyObject *
1452builtin_slice(self, args)
1453 PyObject *self;
1454 PyObject *args;
1455{
Guido van Rossum09df08a1998-05-22 00:51:39 +00001456 PyObject *start, *stop, *step;
Guido van Rossum8861b741996-07-30 16:49:37 +00001457
Guido van Rossum09df08a1998-05-22 00:51:39 +00001458 start = stop = step = NULL;
Guido van Rossum8861b741996-07-30 16:49:37 +00001459
Guido van Rossum09df08a1998-05-22 00:51:39 +00001460 if (!PyArg_ParseTuple(args, "O|OO:slice", &start, &stop, &step))
1461 return NULL;
Guido van Rossum8861b741996-07-30 16:49:37 +00001462
Guido van Rossum09df08a1998-05-22 00:51:39 +00001463 /* This swapping of stop and start is to maintain similarity with
1464 range(). */
1465 if (stop == NULL) {
1466 stop = start;
1467 start = NULL;
1468 }
1469 return PySlice_New(start, stop, step);
Guido van Rossum8861b741996-07-30 16:49:37 +00001470}
1471
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001472static char slice_doc[] =
Fred Drake3d587441999-07-19 15:21:16 +00001473"slice([start,] stop[, step]) -> slice object\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001474\n\
1475Create a slice object. This is used for slicing by the Numeric extensions.";
1476
1477
Guido van Rossum79f25d91997-04-29 20:08:16 +00001478static PyObject *
Guido van Rossum872537c1995-07-07 22:43:42 +00001479builtin_locals(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001480 PyObject *self;
1481 PyObject *args;
Guido van Rossum872537c1995-07-07 22:43:42 +00001482{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001483 PyObject *d;
Guido van Rossum872537c1995-07-07 22:43:42 +00001484
Guido van Rossum43713e52000-02-29 13:59:29 +00001485 if (!PyArg_ParseTuple(args, ":locals"))
Guido van Rossum872537c1995-07-07 22:43:42 +00001486 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001487 d = PyEval_GetLocals();
1488 Py_INCREF(d);
Guido van Rossum872537c1995-07-07 22:43:42 +00001489 return d;
1490}
1491
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001492static char locals_doc[] =
1493"locals() -> dictionary\n\
1494\n\
1495Return the dictionary containing the current scope's local variables.";
1496
1497
Guido van Rossum79f25d91997-04-29 20:08:16 +00001498static PyObject *
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001499min_max(args, sign)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001500 PyObject *args;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001501 int sign;
1502{
Guido van Rossum2d951851994-08-29 12:52:16 +00001503 int i;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001504 PyObject *v, *w, *x;
1505 PySequenceMethods *sq;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001506
Guido van Rossum79f25d91997-04-29 20:08:16 +00001507 if (PyTuple_Size(args) > 1)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001508 v = args;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001509 else if (!PyArg_ParseTuple(args, "O:min/max", &v))
Guido van Rossum3f5da241990-12-20 15:06:42 +00001510 return NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001511 sq = v->ob_type->tp_as_sequence;
Guido van Rossum09df08a1998-05-22 00:51:39 +00001512 if (sq == NULL || sq->sq_item == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001513 PyErr_SetString(PyExc_TypeError,
1514 "min() or max() of non-sequence");
Guido van Rossum3f5da241990-12-20 15:06:42 +00001515 return NULL;
1516 }
Guido van Rossum2d951851994-08-29 12:52:16 +00001517 w = NULL;
1518 for (i = 0; ; i++) {
Guido van Rossum3f5da241990-12-20 15:06:42 +00001519 x = (*sq->sq_item)(v, i); /* Implies INCREF */
Guido van Rossum2d951851994-08-29 12:52:16 +00001520 if (x == NULL) {
Barry Warsawcde8b1b1997-08-22 21:14:38 +00001521 if (PyErr_ExceptionMatches(PyExc_IndexError)) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001522 PyErr_Clear();
Guido van Rossum2d951851994-08-29 12:52:16 +00001523 break;
1524 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00001525 Py_XDECREF(w);
Guido van Rossum2d951851994-08-29 12:52:16 +00001526 return NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001527 }
Guido van Rossum2d951851994-08-29 12:52:16 +00001528 if (w == NULL)
1529 w = x;
1530 else {
Guido van Rossumc8b6df91997-05-23 00:06:51 +00001531 int c = PyObject_Compare(x, w);
1532 if (c && PyErr_Occurred()) {
1533 Py_DECREF(x);
1534 Py_XDECREF(w);
1535 return NULL;
1536 }
1537 if (c * sign > 0) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001538 Py_DECREF(w);
Guido van Rossum2d951851994-08-29 12:52:16 +00001539 w = x;
1540 }
1541 else
Guido van Rossum79f25d91997-04-29 20:08:16 +00001542 Py_DECREF(x);
Guido van Rossum2d951851994-08-29 12:52:16 +00001543 }
Guido van Rossum3f5da241990-12-20 15:06:42 +00001544 }
Guido van Rossum2d951851994-08-29 12:52:16 +00001545 if (w == NULL)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001546 PyErr_SetString(PyExc_ValueError,
1547 "min() or max() of empty sequence");
Guido van Rossum3f5da241990-12-20 15:06:42 +00001548 return w;
1549}
1550
Guido van Rossum79f25d91997-04-29 20:08:16 +00001551static PyObject *
Guido van Rossum3f5da241990-12-20 15:06:42 +00001552builtin_min(self, v)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001553 PyObject *self;
1554 PyObject *v;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001555{
1556 return min_max(v, -1);
1557}
1558
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001559static char min_doc[] =
1560"min(sequence) -> value\n\
1561min(a, b, c, ...) -> value\n\
1562\n\
1563With a single sequence argument, return its smallest item.\n\
1564With two or more arguments, return the smallest argument.";
1565
1566
Guido van Rossum79f25d91997-04-29 20:08:16 +00001567static PyObject *
Guido van Rossum3f5da241990-12-20 15:06:42 +00001568builtin_max(self, v)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001569 PyObject *self;
1570 PyObject *v;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001571{
1572 return min_max(v, 1);
1573}
1574
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001575static char max_doc[] =
1576"max(sequence) -> value\n\
1577max(a, b, c, ...) -> value\n\
1578\n\
1579With a single sequence argument, return its largest item.\n\
1580With two or more arguments, return the largest argument.";
1581
1582
Guido van Rossum79f25d91997-04-29 20:08:16 +00001583static PyObject *
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001584builtin_oct(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001585 PyObject *self;
1586 PyObject *args;
Guido van Rossum006bcd41991-10-24 14:54:44 +00001587{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001588 PyObject *v;
1589 PyNumberMethods *nb;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001590
Guido van Rossum79f25d91997-04-29 20:08:16 +00001591 if (!PyArg_ParseTuple(args, "O:oct", &v))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001592 return NULL;
Guido van Rossum1899c2e1992-09-12 11:09:23 +00001593 if (v == NULL || (nb = v->ob_type->tp_as_number) == NULL ||
1594 nb->nb_oct == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001595 PyErr_SetString(PyExc_TypeError,
Guido van Rossum1899c2e1992-09-12 11:09:23 +00001596 "oct() argument can't be converted to oct");
1597 return NULL;
Guido van Rossum006bcd41991-10-24 14:54:44 +00001598 }
Guido van Rossum1899c2e1992-09-12 11:09:23 +00001599 return (*nb->nb_oct)(v);
Guido van Rossum006bcd41991-10-24 14:54:44 +00001600}
1601
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001602static char oct_doc[] =
1603"oct(number) -> string\n\
1604\n\
1605Return the octal representation of an integer or long integer.";
1606
1607
Guido van Rossum79f25d91997-04-29 20:08:16 +00001608static PyObject *
Guido van Rossum94390a41992-08-14 15:14:30 +00001609builtin_open(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001610 PyObject *self;
1611 PyObject *args;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001612{
Guido van Rossum2d951851994-08-29 12:52:16 +00001613 char *name;
1614 char *mode = "r";
1615 int bufsize = -1;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001616 PyObject *f;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001617
Guido van Rossum79f25d91997-04-29 20:08:16 +00001618 if (!PyArg_ParseTuple(args, "s|si:open", &name, &mode, &bufsize))
Guido van Rossum3f5da241990-12-20 15:06:42 +00001619 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001620 f = PyFile_FromString(name, mode);
Guido van Rossum2d951851994-08-29 12:52:16 +00001621 if (f != NULL)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001622 PyFile_SetBufSize(f, bufsize);
Guido van Rossum2d951851994-08-29 12:52:16 +00001623 return f;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001624}
1625
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001626static char open_doc[] =
1627"open(filename[, mode[, buffering]]) -> file object\n\
1628\n\
1629Open a file. The mode can be 'r', 'w' or 'a' for reading (default),\n\
1630writing or appending. The file will be created if it doesn't exist\n\
1631when opened for writing or appending; it will be truncated when\n\
1632opened for writing. Add a 'b' to the mode for binary files.\n\
1633Add a '+' to the mode to allow simultaneous reading and writing.\n\
1634If the buffering argument is given, 0 means unbuffered, 1 means line\n\
1635buffered, and larger numbers specify the buffer size.";
1636
1637
Guido van Rossum79f25d91997-04-29 20:08:16 +00001638static PyObject *
Guido van Rossum94390a41992-08-14 15:14:30 +00001639builtin_ord(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001640 PyObject *self;
1641 PyObject *args;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001642{
Guido van Rossum09095f32000-03-10 23:00:52 +00001643 PyObject *obj;
1644 long ord;
Jeremy Hylton394b54d2000-04-12 21:19:47 +00001645 int size;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001646
Guido van Rossum09095f32000-03-10 23:00:52 +00001647 if (!PyArg_ParseTuple(args, "O:ord", &obj))
Guido van Rossum3f5da241990-12-20 15:06:42 +00001648 return NULL;
Guido van Rossum09095f32000-03-10 23:00:52 +00001649
Jeremy Hylton394b54d2000-04-12 21:19:47 +00001650 if (PyString_Check(obj)) {
1651 size = PyString_GET_SIZE(obj);
1652 if (size == 1)
1653 ord = (long)((unsigned char)*PyString_AS_STRING(obj));
1654 } else if (PyUnicode_Check(obj)) {
1655 size = PyUnicode_GET_SIZE(obj);
1656 if (size == 1)
1657 ord = (long)*PyUnicode_AS_UNICODE(obj);
1658 } else {
1659 PyErr_Format(PyExc_TypeError,
Fred Drake078b24f2000-04-13 02:42:50 +00001660 "expected string or Unicode character, " \
Jeremy Hylton394b54d2000-04-12 21:19:47 +00001661 "%.200s found", obj->ob_type->tp_name);
Guido van Rossum09095f32000-03-10 23:00:52 +00001662 return NULL;
1663 }
Jeremy Hylton394b54d2000-04-12 21:19:47 +00001664 if (size == 1)
1665 return PyInt_FromLong(ord);
Guido van Rossum09095f32000-03-10 23:00:52 +00001666
Jeremy Hylton394b54d2000-04-12 21:19:47 +00001667 PyErr_Format(PyExc_TypeError,
1668 "expected a character, length-%d string found",
1669 size);
1670 return NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001671}
1672
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001673static char ord_doc[] =
1674"ord(c) -> integer\n\
1675\n\
Fred Drake078b24f2000-04-13 02:42:50 +00001676Return the integer ordinal of a one character string.";
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001677
1678
Guido van Rossum79f25d91997-04-29 20:08:16 +00001679static PyObject *
Guido van Rossum6a00cd81995-01-07 12:39:01 +00001680builtin_pow(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001681 PyObject *self;
1682 PyObject *args;
Guido van Rossum6a00cd81995-01-07 12:39:01 +00001683{
Guido van Rossum09df08a1998-05-22 00:51:39 +00001684 PyObject *v, *w, *z = Py_None;
Guido van Rossum6a00cd81995-01-07 12:39:01 +00001685
Guido van Rossum79f25d91997-04-29 20:08:16 +00001686 if (!PyArg_ParseTuple(args, "OO|O:pow", &v, &w, &z))
Guido van Rossum6a00cd81995-01-07 12:39:01 +00001687 return NULL;
Guido van Rossum09df08a1998-05-22 00:51:39 +00001688 return PyNumber_Power(v, w, z);
Guido van Rossumd4905451991-05-05 20:00:36 +00001689}
1690
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001691static char pow_doc[] =
1692"pow(x, y[, z]) -> number\n\
1693\n\
1694With two arguments, equivalent to x**y. With three arguments,\n\
1695equivalent to (x**y) % z, but may be more efficient (e.g. for longs).";
1696
1697
Guido van Rossum124eff01999-02-23 16:11:01 +00001698/* Return number of items in range/xrange (lo, hi, step). step > 0
1699 * required. Return a value < 0 if & only if the true value is too
1700 * large to fit in a signed long.
1701 */
1702static long
1703get_len_of_range(lo, hi, step)
1704 long lo;
1705 long hi;
1706 long step; /* must be > 0 */
1707{
1708 /* -------------------------------------------------------------
1709 If lo >= hi, the range is empty.
1710 Else if n values are in the range, the last one is
1711 lo + (n-1)*step, which must be <= hi-1. Rearranging,
1712 n <= (hi - lo - 1)/step + 1, so taking the floor of the RHS gives
1713 the proper value. Since lo < hi in this case, hi-lo-1 >= 0, so
1714 the RHS is non-negative and so truncation is the same as the
1715 floor. Letting M be the largest positive long, the worst case
1716 for the RHS numerator is hi=M, lo=-M-1, and then
1717 hi-lo-1 = M-(-M-1)-1 = 2*M. Therefore unsigned long has enough
1718 precision to compute the RHS exactly.
1719 ---------------------------------------------------------------*/
1720 long n = 0;
1721 if (lo < hi) {
1722 unsigned long uhi = (unsigned long)hi;
1723 unsigned long ulo = (unsigned long)lo;
1724 unsigned long diff = uhi - ulo - 1;
1725 n = (long)(diff / (unsigned long)step + 1);
1726 }
1727 return n;
1728}
1729
Guido van Rossum79f25d91997-04-29 20:08:16 +00001730static PyObject *
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001731builtin_range(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001732 PyObject *self;
1733 PyObject *args;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001734{
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001735 long ilow = 0, ihigh = 0, istep = 1;
Guido van Rossum124eff01999-02-23 16:11:01 +00001736 long bign;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001737 int i, n;
Guido van Rossum124eff01999-02-23 16:11:01 +00001738
Guido van Rossum79f25d91997-04-29 20:08:16 +00001739 PyObject *v;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001740
Guido van Rossum79f25d91997-04-29 20:08:16 +00001741 if (PyTuple_Size(args) <= 1) {
1742 if (!PyArg_ParseTuple(args,
Guido van Rossum0865dd91995-01-17 16:30:22 +00001743 "l;range() requires 1-3 int arguments",
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001744 &ihigh))
1745 return NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001746 }
1747 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001748 if (!PyArg_ParseTuple(args,
Guido van Rossum0865dd91995-01-17 16:30:22 +00001749 "ll|l;range() requires 1-3 int arguments",
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001750 &ilow, &ihigh, &istep))
Guido van Rossum3f5da241990-12-20 15:06:42 +00001751 return NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001752 }
1753 if (istep == 0) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001754 PyErr_SetString(PyExc_ValueError, "zero step for range()");
Guido van Rossum3f5da241990-12-20 15:06:42 +00001755 return NULL;
1756 }
Guido van Rossum124eff01999-02-23 16:11:01 +00001757 if (istep > 0)
1758 bign = get_len_of_range(ilow, ihigh, istep);
1759 else
1760 bign = get_len_of_range(ihigh, ilow, -istep);
1761 n = (int)bign;
1762 if (bign < 0 || (long)n != bign) {
Guido van Rossume23cde21999-01-12 05:07:47 +00001763 PyErr_SetString(PyExc_OverflowError,
Guido van Rossum124eff01999-02-23 16:11:01 +00001764 "range() has too many items");
Guido van Rossume23cde21999-01-12 05:07:47 +00001765 return NULL;
1766 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00001767 v = PyList_New(n);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001768 if (v == NULL)
1769 return NULL;
1770 for (i = 0; i < n; i++) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001771 PyObject *w = PyInt_FromLong(ilow);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001772 if (w == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001773 Py_DECREF(v);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001774 return NULL;
1775 }
Guido van Rossuma937d141998-04-24 18:22:02 +00001776 PyList_SET_ITEM(v, i, w);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001777 ilow += istep;
1778 }
1779 return v;
1780}
1781
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001782static char range_doc[] =
1783"range([start,] stop[, step]) -> list of integers\n\
1784\n\
1785Return a list containing an arithmetic progression of integers.\n\
1786range(i, j) returns [i, i+1, i+2, ..., j-1]; start (!) defaults to 0.\n\
1787When step is given, it specifies the increment (or decrement).\n\
1788For example, range(4) returns [0, 1, 2, 3]. The end point is omitted!\n\
1789These are exactly the valid indices for a list of 4 elements.";
1790
1791
Guido van Rossum79f25d91997-04-29 20:08:16 +00001792static PyObject *
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001793builtin_xrange(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001794 PyObject *self;
1795 PyObject *args;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001796{
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001797 long ilow = 0, ihigh = 0, istep = 1;
Guido van Rossum0865dd91995-01-17 16:30:22 +00001798 long n;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001799
Guido van Rossum79f25d91997-04-29 20:08:16 +00001800 if (PyTuple_Size(args) <= 1) {
1801 if (!PyArg_ParseTuple(args,
Guido van Rossum0865dd91995-01-17 16:30:22 +00001802 "l;xrange() requires 1-3 int arguments",
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001803 &ihigh))
1804 return NULL;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001805 }
1806 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001807 if (!PyArg_ParseTuple(args,
Guido van Rossum0865dd91995-01-17 16:30:22 +00001808 "ll|l;xrange() requires 1-3 int arguments",
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001809 &ilow, &ihigh, &istep))
Guido van Rossum12d12c51993-10-26 17:58:25 +00001810 return NULL;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001811 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001812 if (istep == 0) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001813 PyErr_SetString(PyExc_ValueError, "zero step for xrange()");
Guido van Rossum12d12c51993-10-26 17:58:25 +00001814 return NULL;
1815 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001816 if (istep > 0)
Guido van Rossum124eff01999-02-23 16:11:01 +00001817 n = get_len_of_range(ilow, ihigh, istep);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001818 else
Guido van Rossum124eff01999-02-23 16:11:01 +00001819 n = get_len_of_range(ihigh, ilow, -istep);
1820 if (n < 0) {
1821 PyErr_SetString(PyExc_OverflowError,
1822 "xrange() has more than sys.maxint items");
1823 return NULL;
1824 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00001825 return PyRange_New(ilow, n, istep, 1);
Guido van Rossum12d12c51993-10-26 17:58:25 +00001826}
1827
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001828static char xrange_doc[] =
1829"xrange([start,] stop[, step]) -> xrange object\n\
1830\n\
1831Like range(), but instead of returning a list, returns an object that\n\
1832generates the numbers in the range on demand. This is slightly slower\n\
1833than range() but more memory efficient.";
1834
1835
Guido van Rossum79f25d91997-04-29 20:08:16 +00001836static PyObject *
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001837builtin_raw_input(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001838 PyObject *self;
1839 PyObject *args;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001840{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001841 PyObject *v = NULL;
1842 PyObject *f;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001843
Guido van Rossum79f25d91997-04-29 20:08:16 +00001844 if (!PyArg_ParseTuple(args, "|O:[raw_]input", &v))
Guido van Rossum3165fe61992-09-25 21:59:05 +00001845 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001846 if (PyFile_AsFile(PySys_GetObject("stdin")) == stdin &&
1847 PyFile_AsFile(PySys_GetObject("stdout")) == stdout &&
Guido van Rossum53bb7ff1995-07-26 16:26:31 +00001848 isatty(fileno(stdin)) && isatty(fileno(stdout))) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001849 PyObject *po;
Guido van Rossum872537c1995-07-07 22:43:42 +00001850 char *prompt;
1851 char *s;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001852 PyObject *result;
Guido van Rossum872537c1995-07-07 22:43:42 +00001853 if (v != NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001854 po = PyObject_Str(v);
Guido van Rossum872537c1995-07-07 22:43:42 +00001855 if (po == NULL)
1856 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001857 prompt = PyString_AsString(po);
Guido van Rossumd9b52081998-06-26 18:25:38 +00001858 if (prompt == NULL)
1859 return NULL;
Guido van Rossum872537c1995-07-07 22:43:42 +00001860 }
1861 else {
1862 po = NULL;
1863 prompt = "";
1864 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00001865 s = PyOS_Readline(prompt);
1866 Py_XDECREF(po);
Guido van Rossum872537c1995-07-07 22:43:42 +00001867 if (s == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001868 PyErr_SetNone(PyExc_KeyboardInterrupt);
Guido van Rossum872537c1995-07-07 22:43:42 +00001869 return NULL;
1870 }
1871 if (*s == '\0') {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001872 PyErr_SetNone(PyExc_EOFError);
Guido van Rossum872537c1995-07-07 22:43:42 +00001873 result = NULL;
1874 }
1875 else { /* strip trailing '\n' */
Guido van Rossum106f2da2000-06-28 21:12:25 +00001876 size_t len = strlen(s);
1877 if (len > INT_MAX) {
1878 PyErr_SetString(PyExc_OverflowError, "input too long");
1879 result = NULL;
1880 }
1881 else {
1882 result = PyString_FromStringAndSize(s, (int)(len-1));
1883 }
Guido van Rossum872537c1995-07-07 22:43:42 +00001884 }
Guido van Rossumb18618d2000-05-03 23:44:39 +00001885 PyMem_FREE(s);
Guido van Rossum872537c1995-07-07 22:43:42 +00001886 return result;
1887 }
Guido van Rossum90933611991-06-07 16:10:43 +00001888 if (v != NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001889 f = PySys_GetObject("stdout");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001890 if (f == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001891 PyErr_SetString(PyExc_RuntimeError, "lost sys.stdout");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001892 return NULL;
1893 }
Guido van Rossumc8b6df91997-05-23 00:06:51 +00001894 if (Py_FlushLine() != 0 ||
1895 PyFile_WriteObject(v, f, Py_PRINT_RAW) != 0)
Guido van Rossum90933611991-06-07 16:10:43 +00001896 return NULL;
1897 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00001898 f = PySys_GetObject("stdin");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001899 if (f == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001900 PyErr_SetString(PyExc_RuntimeError, "lost sys.stdin");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001901 return NULL;
1902 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00001903 return PyFile_GetLine(f, -1);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001904}
1905
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001906static char raw_input_doc[] =
1907"raw_input([prompt]) -> string\n\
1908\n\
1909Read a string from standard input. The trailing newline is stripped.\n\
1910If the user hits EOF (Unix: Ctl-D, Windows: Ctl-Z+Return), raise EOFError.\n\
1911On Unix, GNU readline is used if enabled. The prompt string, if given,\n\
1912is printed without a trailing newline before reading.";
1913
1914
Guido van Rossum79f25d91997-04-29 20:08:16 +00001915static PyObject *
Guido van Rossum12d12c51993-10-26 17:58:25 +00001916builtin_reduce(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001917 PyObject *self;
1918 PyObject *args;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001919{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001920 PyObject *seq, *func, *result = NULL;
1921 PySequenceMethods *sqf;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001922 register int i;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001923
Guido van Rossum79f25d91997-04-29 20:08:16 +00001924 if (!PyArg_ParseTuple(args, "OO|O:reduce", &func, &seq, &result))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001925 return NULL;
1926 if (result != NULL)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001927 Py_INCREF(result);
Guido van Rossum12d12c51993-10-26 17:58:25 +00001928
Guido van Rossum09df08a1998-05-22 00:51:39 +00001929 sqf = seq->ob_type->tp_as_sequence;
1930 if (sqf == NULL || sqf->sq_item == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001931 PyErr_SetString(PyExc_TypeError,
Guido van Rossum12d12c51993-10-26 17:58:25 +00001932 "2nd argument to reduce() must be a sequence object");
1933 return NULL;
1934 }
1935
Guido van Rossum79f25d91997-04-29 20:08:16 +00001936 if ((args = PyTuple_New(2)) == NULL)
Guido van Rossum2d951851994-08-29 12:52:16 +00001937 goto Fail;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001938
Guido van Rossum2d951851994-08-29 12:52:16 +00001939 for (i = 0; ; ++i) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001940 PyObject *op2;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001941
1942 if (args->ob_refcnt > 1) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001943 Py_DECREF(args);
1944 if ((args = PyTuple_New(2)) == NULL)
Guido van Rossum2d951851994-08-29 12:52:16 +00001945 goto Fail;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001946 }
1947
Guido van Rossum2d951851994-08-29 12:52:16 +00001948 if ((op2 = (*sqf->sq_item)(seq, i)) == NULL) {
Barry Warsawcde8b1b1997-08-22 21:14:38 +00001949 if (PyErr_ExceptionMatches(PyExc_IndexError)) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001950 PyErr_Clear();
Guido van Rossum2d951851994-08-29 12:52:16 +00001951 break;
1952 }
1953 goto Fail;
1954 }
Guido van Rossum12d12c51993-10-26 17:58:25 +00001955
Guido van Rossum2d951851994-08-29 12:52:16 +00001956 if (result == NULL)
1957 result = op2;
1958 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001959 PyTuple_SetItem(args, 0, result);
1960 PyTuple_SetItem(args, 1, op2);
1961 if ((result = PyEval_CallObject(func, args)) == NULL)
Guido van Rossum2d951851994-08-29 12:52:16 +00001962 goto Fail;
1963 }
Guido van Rossum12d12c51993-10-26 17:58:25 +00001964 }
1965
Guido van Rossum79f25d91997-04-29 20:08:16 +00001966 Py_DECREF(args);
Guido van Rossum12d12c51993-10-26 17:58:25 +00001967
Guido van Rossum2d951851994-08-29 12:52:16 +00001968 if (result == NULL)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001969 PyErr_SetString(PyExc_TypeError,
Guido van Rossum2d951851994-08-29 12:52:16 +00001970 "reduce of empty sequence with no initial value");
1971
Guido van Rossum12d12c51993-10-26 17:58:25 +00001972 return result;
1973
Guido van Rossum2d951851994-08-29 12:52:16 +00001974Fail:
Guido van Rossum79f25d91997-04-29 20:08:16 +00001975 Py_XDECREF(args);
1976 Py_XDECREF(result);
Guido van Rossum12d12c51993-10-26 17:58:25 +00001977 return NULL;
1978}
1979
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001980static char reduce_doc[] =
1981"reduce(function, sequence[, initial]) -> value\n\
1982\n\
1983Apply a function of two arguments cumulatively to the items of a sequence,\n\
1984from left to right, so as to reduce the sequence to a single value.\n\
1985For example, reduce(lambda x, y: x+y, [1, 2, 3, 4, 5]) calculates\n\
1986((((1+2)+3)+4)+5). If initial is present, it is placed before the items\n\
1987of the sequence in the calculation, and serves as a default when the\n\
1988sequence is empty.";
1989
1990
Guido van Rossum79f25d91997-04-29 20:08:16 +00001991static PyObject *
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001992builtin_reload(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001993 PyObject *self;
1994 PyObject *args;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001995{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001996 PyObject *v;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001997
Guido van Rossum79f25d91997-04-29 20:08:16 +00001998 if (!PyArg_ParseTuple(args, "O:reload", &v))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001999 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002000 return PyImport_ReloadModule(v);
Guido van Rossum3f5da241990-12-20 15:06:42 +00002001}
2002
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002003static char reload_doc[] =
2004"reload(module) -> module\n\
2005\n\
2006Reload the module. The module must have been successfully imported before.";
2007
2008
Guido van Rossum79f25d91997-04-29 20:08:16 +00002009static PyObject *
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002010builtin_repr(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00002011 PyObject *self;
2012 PyObject *args;
Guido van Rossumc89705d1992-11-26 08:54:07 +00002013{
Guido van Rossum79f25d91997-04-29 20:08:16 +00002014 PyObject *v;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002015
Guido van Rossum79f25d91997-04-29 20:08:16 +00002016 if (!PyArg_ParseTuple(args, "O:repr", &v))
Guido van Rossumc89705d1992-11-26 08:54:07 +00002017 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002018 return PyObject_Repr(v);
Guido van Rossumc89705d1992-11-26 08:54:07 +00002019}
2020
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002021static char repr_doc[] =
2022"repr(object) -> string\n\
2023\n\
2024Return the canonical string representation of the object.\n\
2025For most object types, eval(repr(object)) == object.";
2026
2027
Guido van Rossum79f25d91997-04-29 20:08:16 +00002028static PyObject *
Guido van Rossum9e51f9b1993-02-12 16:29:05 +00002029builtin_round(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00002030 PyObject *self;
2031 PyObject *args;
Guido van Rossum9e51f9b1993-02-12 16:29:05 +00002032{
Guido van Rossum9e51f9b1993-02-12 16:29:05 +00002033 double x;
2034 double f;
2035 int ndigits = 0;
Guido van Rossum9e51f9b1993-02-12 16:29:05 +00002036 int i;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002037
Guido van Rossum79f25d91997-04-29 20:08:16 +00002038 if (!PyArg_ParseTuple(args, "d|i:round", &x, &ndigits))
Guido van Rossum9e51f9b1993-02-12 16:29:05 +00002039 return NULL;
Guido van Rossum9e51f9b1993-02-12 16:29:05 +00002040 f = 1.0;
Guido van Rossum1e162d31998-05-09 14:42:25 +00002041 i = abs(ndigits);
2042 while (--i >= 0)
Guido van Rossum9e51f9b1993-02-12 16:29:05 +00002043 f = f*10.0;
Guido van Rossum1e162d31998-05-09 14:42:25 +00002044 if (ndigits < 0)
2045 x /= f;
Guido van Rossum9e51f9b1993-02-12 16:29:05 +00002046 else
Guido van Rossum1e162d31998-05-09 14:42:25 +00002047 x *= f;
2048 if (x >= 0.0)
2049 x = floor(x + 0.5);
2050 else
2051 x = ceil(x - 0.5);
2052 if (ndigits < 0)
2053 x *= f;
2054 else
2055 x /= f;
2056 return PyFloat_FromDouble(x);
Guido van Rossum9e51f9b1993-02-12 16:29:05 +00002057}
2058
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002059static char round_doc[] =
2060"round(number[, ndigits]) -> floating point number\n\
2061\n\
2062Round a number to a given precision in decimal digits (default 0 digits).\n\
2063This always returns a floating point number. Precision may be negative.";
2064
2065
Guido van Rossum79f25d91997-04-29 20:08:16 +00002066static PyObject *
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002067builtin_str(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00002068 PyObject *self;
2069 PyObject *args;
Guido van Rossumc89705d1992-11-26 08:54:07 +00002070{
Guido van Rossum79f25d91997-04-29 20:08:16 +00002071 PyObject *v;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002072
Guido van Rossum79f25d91997-04-29 20:08:16 +00002073 if (!PyArg_ParseTuple(args, "O:str", &v))
Guido van Rossumc89705d1992-11-26 08:54:07 +00002074 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002075 return PyObject_Str(v);
Guido van Rossumc89705d1992-11-26 08:54:07 +00002076}
2077
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002078static char str_doc[] =
2079"str(object) -> string\n\
2080\n\
2081Return a nice string representation of the object.\n\
2082If the argument is a string, the return value is the same object.";
2083
2084
Guido van Rossum79f25d91997-04-29 20:08:16 +00002085static PyObject *
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002086builtin_tuple(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00002087 PyObject *self;
2088 PyObject *args;
Guido van Rossumcae027b1994-08-29 12:53:11 +00002089{
Guido van Rossum79f25d91997-04-29 20:08:16 +00002090 PyObject *v;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002091
Guido van Rossum79f25d91997-04-29 20:08:16 +00002092 if (!PyArg_ParseTuple(args, "O:tuple", &v))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002093 return NULL;
Guido van Rossum09df08a1998-05-22 00:51:39 +00002094 return PySequence_Tuple(v);
Guido van Rossumcae027b1994-08-29 12:53:11 +00002095}
2096
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002097static char tuple_doc[] =
2098"tuple(sequence) -> list\n\
2099\n\
2100Return a tuple whose items are the same as those of the argument sequence.\n\
2101If the argument is a tuple, the return value is the same object.";
2102
2103
Guido van Rossum79f25d91997-04-29 20:08:16 +00002104static PyObject *
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002105builtin_type(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00002106 PyObject *self;
2107 PyObject *args;
Guido van Rossum3f5da241990-12-20 15:06:42 +00002108{
Guido van Rossum79f25d91997-04-29 20:08:16 +00002109 PyObject *v;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002110
Guido van Rossum79f25d91997-04-29 20:08:16 +00002111 if (!PyArg_ParseTuple(args, "O:type", &v))
Guido van Rossum3f5da241990-12-20 15:06:42 +00002112 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002113 v = (PyObject *)v->ob_type;
2114 Py_INCREF(v);
Guido van Rossum3f5da241990-12-20 15:06:42 +00002115 return v;
2116}
2117
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002118static char type_doc[] =
2119"type(object) -> type object\n\
2120\n\
2121Return the type of the object.";
2122
2123
Guido van Rossum79f25d91997-04-29 20:08:16 +00002124static PyObject *
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002125builtin_vars(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +00002126 PyObject *self;
2127 PyObject *args;
Guido van Rossum2d951851994-08-29 12:52:16 +00002128{
Guido van Rossum79f25d91997-04-29 20:08:16 +00002129 PyObject *v = NULL;
2130 PyObject *d;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002131
Guido van Rossum79f25d91997-04-29 20:08:16 +00002132 if (!PyArg_ParseTuple(args, "|O:vars", &v))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002133 return NULL;
Guido van Rossum2d951851994-08-29 12:52:16 +00002134 if (v == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002135 d = PyEval_GetLocals();
Guido van Rossum53bb7ff1995-07-26 16:26:31 +00002136 if (d == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002137 if (!PyErr_Occurred())
2138 PyErr_SetString(PyExc_SystemError,
2139 "no locals!?");
Guido van Rossum53bb7ff1995-07-26 16:26:31 +00002140 }
2141 else
Guido van Rossum79f25d91997-04-29 20:08:16 +00002142 Py_INCREF(d);
Guido van Rossum2d951851994-08-29 12:52:16 +00002143 }
2144 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002145 d = PyObject_GetAttrString(v, "__dict__");
Guido van Rossum2d951851994-08-29 12:52:16 +00002146 if (d == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002147 PyErr_SetString(PyExc_TypeError,
Guido van Rossum2d951851994-08-29 12:52:16 +00002148 "vars() argument must have __dict__ attribute");
2149 return NULL;
2150 }
2151 }
2152 return d;
2153}
2154
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002155static char vars_doc[] =
2156"vars([object]) -> dictionary\n\
2157\n\
2158Without arguments, equivalent to locals().\n\
2159With an argument, equivalent to object.__dict__.";
2160
Guido van Rossum668213d1999-06-16 17:28:37 +00002161static int
2162abstract_issubclass(derived, cls, err, first)
2163 PyObject *derived;
2164 PyObject *cls;
2165 char *err;
2166 int first;
2167{
2168 static PyObject *__bases__ = NULL;
2169 PyObject *bases;
Guido van Rossum7f851861999-06-17 19:12:39 +00002170 int i, n;
Guido van Rossum668213d1999-06-16 17:28:37 +00002171 int r = 0;
2172
2173 if (__bases__ == NULL) {
2174 __bases__ = PyString_FromString("__bases__");
2175 if (__bases__ == NULL)
2176 return -1;
2177 }
2178
2179 if (first) {
2180 bases = PyObject_GetAttr(cls, __bases__);
2181 if (bases == NULL || !PyTuple_Check(bases)) {
2182 Py_XDECREF(bases);
2183 PyErr_SetString(PyExc_TypeError, err);
2184 return -1;
2185 }
2186 Py_DECREF(bases);
2187 }
2188
2189 if (derived == cls)
2190 return 1;
2191
2192 bases = PyObject_GetAttr(derived, __bases__);
2193 if (bases == NULL || !PyTuple_Check(bases)) {
2194 Py_XDECREF(bases);
2195 PyErr_SetString(PyExc_TypeError, err);
2196 return -1;
2197 }
2198
2199 n = PyTuple_GET_SIZE(bases);
2200 for (i = 0; i < n; i++) {
2201 r = abstract_issubclass(PyTuple_GET_ITEM(bases, i),
2202 cls, err, 0);
2203 if (r != 0)
2204 break;
2205 }
2206
2207 Py_DECREF(bases);
2208
2209 return r;
2210}
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002211
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002212static PyObject *
2213builtin_isinstance(self, args)
2214 PyObject *self;
2215 PyObject *args;
2216{
2217 PyObject *inst;
2218 PyObject *cls;
Guido van Rossum668213d1999-06-16 17:28:37 +00002219 PyObject *icls;
2220 static PyObject *__class__ = NULL;
2221 int retval = 0;
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002222
Guido van Rossum43713e52000-02-29 13:59:29 +00002223 if (!PyArg_ParseTuple(args, "OO:isinstance", &inst, &cls))
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002224 return NULL;
Guido van Rossumf5dd9141997-12-02 19:11:45 +00002225
Guido van Rossum668213d1999-06-16 17:28:37 +00002226 if (PyClass_Check(cls)) {
2227 if (PyInstance_Check(inst)) {
Guido van Rossumf5dd9141997-12-02 19:11:45 +00002228 PyObject *inclass =
2229 (PyObject*)((PyInstanceObject*)inst)->in_class;
2230 retval = PyClass_IsSubclass(inclass, cls);
2231 }
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002232 }
Guido van Rossum668213d1999-06-16 17:28:37 +00002233 else if (PyType_Check(cls)) {
2234 retval = ((PyObject *)(inst->ob_type) == cls);
2235 }
2236 else if (!PyInstance_Check(inst)) {
2237 if (__class__ == NULL) {
2238 __class__ = PyString_FromString("__class__");
2239 if (__class__ == NULL)
2240 return NULL;
2241 }
2242 icls = PyObject_GetAttr(inst, __class__);
2243 if (icls != NULL) {
2244 retval = abstract_issubclass(
2245 icls, cls,
2246 "second argument must be a class",
2247 1);
2248 Py_DECREF(icls);
2249 if (retval < 0)
2250 return NULL;
2251 }
2252 else {
2253 PyErr_SetString(PyExc_TypeError,
2254 "second argument must be a class");
2255 return NULL;
2256 }
2257 }
2258 else {
2259 PyErr_SetString(PyExc_TypeError,
2260 "second argument must be a class");
2261 return NULL;
2262 }
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002263 return PyInt_FromLong(retval);
2264}
2265
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002266static char isinstance_doc[] =
2267"isinstance(object, class-or-type) -> Boolean\n\
2268\n\
2269Return whether an object is an instance of a class or of a subclass thereof.\n\
2270With a type as second argument, return whether that is the object's type.";
2271
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002272
2273static PyObject *
2274builtin_issubclass(self, args)
2275 PyObject *self;
2276 PyObject *args;
2277{
2278 PyObject *derived;
2279 PyObject *cls;
2280 int retval;
2281
Guido van Rossum43713e52000-02-29 13:59:29 +00002282 if (!PyArg_ParseTuple(args, "OO:issubclass", &derived, &cls))
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002283 return NULL;
Guido van Rossum668213d1999-06-16 17:28:37 +00002284
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002285 if (!PyClass_Check(derived) || !PyClass_Check(cls)) {
Guido van Rossum668213d1999-06-16 17:28:37 +00002286 retval = abstract_issubclass(
2287 derived, cls, "arguments must be classes", 1);
2288 if (retval < 0)
2289 return NULL;
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002290 }
Guido van Rossum668213d1999-06-16 17:28:37 +00002291 else {
2292 /* shortcut */
2293 if (!(retval = (derived == cls)))
2294 retval = PyClass_IsSubclass(derived, cls);
2295 }
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002296
2297 return PyInt_FromLong(retval);
2298}
2299
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002300static char issubclass_doc[] =
2301"issubclass(C, B) -> Boolean\n\
2302\n\
2303Return whether class C is a subclass (i.e., a derived class) of class B.";
2304
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002305
Guido van Rossum79f25d91997-04-29 20:08:16 +00002306static PyMethodDef builtin_methods[] = {
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002307 {"__import__", builtin___import__, 1, import_doc},
2308 {"abs", builtin_abs, 1, abs_doc},
2309 {"apply", builtin_apply, 1, apply_doc},
Guido van Rossum0daf0221999-03-19 19:07:19 +00002310 {"buffer", builtin_buffer, 1, buffer_doc},
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002311 {"callable", builtin_callable, 1, callable_doc},
2312 {"chr", builtin_chr, 1, chr_doc},
2313 {"cmp", builtin_cmp, 1, cmp_doc},
2314 {"coerce", builtin_coerce, 1, coerce_doc},
2315 {"compile", builtin_compile, 1, compile_doc},
Guido van Rossum8a5c5d21996-01-12 01:09:56 +00002316#ifndef WITHOUT_COMPLEX
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002317 {"complex", builtin_complex, 1, complex_doc},
Guido van Rossum8a5c5d21996-01-12 01:09:56 +00002318#endif
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002319 {"delattr", builtin_delattr, 1, delattr_doc},
2320 {"dir", builtin_dir, 1, dir_doc},
2321 {"divmod", builtin_divmod, 1, divmod_doc},
2322 {"eval", builtin_eval, 1, eval_doc},
2323 {"execfile", builtin_execfile, 1, execfile_doc},
2324 {"filter", builtin_filter, 1, filter_doc},
2325 {"float", builtin_float, 1, float_doc},
2326 {"getattr", builtin_getattr, 1, getattr_doc},
2327 {"globals", builtin_globals, 1, globals_doc},
2328 {"hasattr", builtin_hasattr, 1, hasattr_doc},
2329 {"hash", builtin_hash, 1, hash_doc},
2330 {"hex", builtin_hex, 1, hex_doc},
2331 {"id", builtin_id, 1, id_doc},
2332 {"input", builtin_input, 1, input_doc},
2333 {"intern", builtin_intern, 1, intern_doc},
2334 {"int", builtin_int, 1, int_doc},
2335 {"isinstance", builtin_isinstance, 1, isinstance_doc},
2336 {"issubclass", builtin_issubclass, 1, issubclass_doc},
2337 {"len", builtin_len, 1, len_doc},
2338 {"list", builtin_list, 1, list_doc},
2339 {"locals", builtin_locals, 1, locals_doc},
2340 {"long", builtin_long, 1, long_doc},
2341 {"map", builtin_map, 1, map_doc},
2342 {"max", builtin_max, 1, max_doc},
2343 {"min", builtin_min, 1, min_doc},
2344 {"oct", builtin_oct, 1, oct_doc},
2345 {"open", builtin_open, 1, open_doc},
2346 {"ord", builtin_ord, 1, ord_doc},
2347 {"pow", builtin_pow, 1, pow_doc},
2348 {"range", builtin_range, 1, range_doc},
2349 {"raw_input", builtin_raw_input, 1, raw_input_doc},
2350 {"reduce", builtin_reduce, 1, reduce_doc},
2351 {"reload", builtin_reload, 1, reload_doc},
2352 {"repr", builtin_repr, 1, repr_doc},
2353 {"round", builtin_round, 1, round_doc},
2354 {"setattr", builtin_setattr, 1, setattr_doc},
2355 {"slice", builtin_slice, 1, slice_doc},
2356 {"str", builtin_str, 1, str_doc},
2357 {"tuple", builtin_tuple, 1, tuple_doc},
2358 {"type", builtin_type, 1, type_doc},
Guido van Rossum09095f32000-03-10 23:00:52 +00002359 {"unicode", builtin_unicode, 1, unicode_doc},
2360 {"unichr", builtin_unichr, 1, unichr_doc},
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002361 {"vars", builtin_vars, 1, vars_doc},
2362 {"xrange", builtin_xrange, 1, xrange_doc},
Guido van Rossumc02e15c1991-12-16 13:03:00 +00002363 {NULL, NULL},
Guido van Rossum3f5da241990-12-20 15:06:42 +00002364};
2365
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002366static char builtin_doc[] =
2367"Built-in functions, exceptions, and other objects.\n\
2368\n\
2369Noteworthy: None is the `nil' object; Ellipsis represents `...' in slices.";
2370
Guido van Rossum25ce5661997-08-02 03:10:38 +00002371PyObject *
Barry Warsaw78e6c672000-05-25 23:15:05 +00002372_PyBuiltin_Init()
Guido van Rossum25ce5661997-08-02 03:10:38 +00002373{
Fred Drake5550de32000-06-20 04:54:19 +00002374 PyObject *mod, *dict, *debug;
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002375 mod = Py_InitModule4("__builtin__", builtin_methods,
2376 builtin_doc, (PyObject *)NULL,
2377 PYTHON_API_VERSION);
Guido van Rossum25ce5661997-08-02 03:10:38 +00002378 if (mod == NULL)
2379 return NULL;
2380 dict = PyModule_GetDict(mod);
Guido van Rossum25ce5661997-08-02 03:10:38 +00002381 if (PyDict_SetItemString(dict, "None", Py_None) < 0)
2382 return NULL;
2383 if (PyDict_SetItemString(dict, "Ellipsis", Py_Ellipsis) < 0)
2384 return NULL;
Fred Drake5550de32000-06-20 04:54:19 +00002385 debug = PyInt_FromLong(Py_OptimizeFlag == 0);
2386 if (PyDict_SetItemString(dict, "__debug__", debug) < 0) {
2387 Py_XDECREF(debug);
Guido van Rossum25ce5661997-08-02 03:10:38 +00002388 return NULL;
Fred Drake5550de32000-06-20 04:54:19 +00002389 }
2390 Py_XDECREF(debug);
Barry Warsaw757af0e1997-08-29 22:13:51 +00002391
Guido van Rossum25ce5661997-08-02 03:10:38 +00002392 return mod;
Guido van Rossum3f5da241990-12-20 15:06:42 +00002393}
2394
Guido van Rossume77a7571993-11-03 15:01:26 +00002395/* Helper for filter(): filter a tuple through a function */
Guido van Rossum12d12c51993-10-26 17:58:25 +00002396
Guido van Rossum79f25d91997-04-29 20:08:16 +00002397static PyObject *
Guido van Rossum12d12c51993-10-26 17:58:25 +00002398filtertuple(func, tuple)
Guido van Rossum79f25d91997-04-29 20:08:16 +00002399 PyObject *func;
2400 PyObject *tuple;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002401{
Guido van Rossum79f25d91997-04-29 20:08:16 +00002402 PyObject *result;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002403 register int i, j;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002404 int len = PyTuple_Size(tuple);
Guido van Rossum12d12c51993-10-26 17:58:25 +00002405
Guido van Rossumb7b45621995-08-04 04:07:45 +00002406 if (len == 0) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002407 Py_INCREF(tuple);
Guido van Rossumb7b45621995-08-04 04:07:45 +00002408 return tuple;
2409 }
2410
Guido van Rossum79f25d91997-04-29 20:08:16 +00002411 if ((result = PyTuple_New(len)) == NULL)
Guido van Rossum2586bf01993-11-01 16:21:44 +00002412 return NULL;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002413
Guido van Rossum12d12c51993-10-26 17:58:25 +00002414 for (i = j = 0; i < len; ++i) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002415 PyObject *item, *good;
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002416 int ok;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002417
Guido van Rossum79f25d91997-04-29 20:08:16 +00002418 if ((item = PyTuple_GetItem(tuple, i)) == NULL)
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002419 goto Fail_1;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002420 if (func == Py_None) {
2421 Py_INCREF(item);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002422 good = item;
2423 }
2424 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002425 PyObject *arg = Py_BuildValue("(O)", item);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002426 if (arg == NULL)
2427 goto Fail_1;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002428 good = PyEval_CallObject(func, arg);
2429 Py_DECREF(arg);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002430 if (good == NULL)
Guido van Rossum12d12c51993-10-26 17:58:25 +00002431 goto Fail_1;
2432 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002433 ok = PyObject_IsTrue(good);
2434 Py_DECREF(good);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002435 if (ok) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002436 Py_INCREF(item);
2437 if (PyTuple_SetItem(result, j++, item) < 0)
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002438 goto Fail_1;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002439 }
Guido van Rossum12d12c51993-10-26 17:58:25 +00002440 }
2441
Guido van Rossum79f25d91997-04-29 20:08:16 +00002442 if (_PyTuple_Resize(&result, j, 0) < 0)
Guido van Rossum12d12c51993-10-26 17:58:25 +00002443 return NULL;
2444
Guido van Rossum12d12c51993-10-26 17:58:25 +00002445 return result;
2446
Guido van Rossum12d12c51993-10-26 17:58:25 +00002447Fail_1:
Guido van Rossum79f25d91997-04-29 20:08:16 +00002448 Py_DECREF(result);
Guido van Rossum12d12c51993-10-26 17:58:25 +00002449 return NULL;
2450}
2451
2452
Guido van Rossume77a7571993-11-03 15:01:26 +00002453/* Helper for filter(): filter a string through a function */
Guido van Rossum12d12c51993-10-26 17:58:25 +00002454
Guido van Rossum79f25d91997-04-29 20:08:16 +00002455static PyObject *
Guido van Rossum12d12c51993-10-26 17:58:25 +00002456filterstring(func, strobj)
Guido van Rossum79f25d91997-04-29 20:08:16 +00002457 PyObject *func;
2458 PyObject *strobj;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002459{
Guido van Rossum79f25d91997-04-29 20:08:16 +00002460 PyObject *result;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002461 register int i, j;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002462 int len = PyString_Size(strobj);
Guido van Rossum12d12c51993-10-26 17:58:25 +00002463
Guido van Rossum79f25d91997-04-29 20:08:16 +00002464 if (func == Py_None) {
Guido van Rossum2586bf01993-11-01 16:21:44 +00002465 /* No character is ever false -- share input string */
Guido van Rossum79f25d91997-04-29 20:08:16 +00002466 Py_INCREF(strobj);
Guido van Rossum2d951851994-08-29 12:52:16 +00002467 return strobj;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002468 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002469 if ((result = PyString_FromStringAndSize(NULL, len)) == NULL)
Guido van Rossum2586bf01993-11-01 16:21:44 +00002470 return NULL;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002471
Guido van Rossum12d12c51993-10-26 17:58:25 +00002472 for (i = j = 0; i < len; ++i) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002473 PyObject *item, *arg, *good;
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002474 int ok;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002475
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002476 item = (*strobj->ob_type->tp_as_sequence->sq_item)(strobj, i);
2477 if (item == NULL)
2478 goto Fail_1;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002479 arg = Py_BuildValue("(O)", item);
2480 Py_DECREF(item);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002481 if (arg == NULL)
2482 goto Fail_1;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002483 good = PyEval_CallObject(func, arg);
2484 Py_DECREF(arg);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002485 if (good == NULL)
2486 goto Fail_1;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002487 ok = PyObject_IsTrue(good);
2488 Py_DECREF(good);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002489 if (ok)
Guido van Rossum79f25d91997-04-29 20:08:16 +00002490 PyString_AS_STRING((PyStringObject *)result)[j++] =
2491 PyString_AS_STRING((PyStringObject *)item)[0];
Guido van Rossum12d12c51993-10-26 17:58:25 +00002492 }
2493
Guido van Rossum79f25d91997-04-29 20:08:16 +00002494 if (j < len && _PyString_Resize(&result, j) < 0)
Guido van Rossum12d12c51993-10-26 17:58:25 +00002495 return NULL;
2496
Guido van Rossum12d12c51993-10-26 17:58:25 +00002497 return result;
2498
Guido van Rossum12d12c51993-10-26 17:58:25 +00002499Fail_1:
Guido van Rossum79f25d91997-04-29 20:08:16 +00002500 Py_DECREF(result);
Guido van Rossum12d12c51993-10-26 17:58:25 +00002501 return NULL;
2502}