blob: 24c99f424470c99db6b3d267b925d616f063e74e [file] [log] [blame]
Guido van Rossum3f5da241990-12-20 15:06:42 +00001/* Built-in functions */
2
Guido van Rossum79f25d91997-04-29 20:08:16 +00003#include "Python.h"
Guido van Rossum3f5da241990-12-20 15:06:42 +00004
5#include "node.h"
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006#include "code.h"
Guido van Rossum5b722181993-03-30 17:46:03 +00007#include "eval.h"
Guido van Rossum3f5da241990-12-20 15:06:42 +00008
Guido van Rossum6bf62da1997-04-11 20:37:35 +00009#include <ctype.h>
10
Guido van Rossume2ae77b2001-10-24 20:42:55 +000011#ifdef RISCOS
12#include "unixstuff.h"
13#endif
14
Mark Hammond26cffde42001-05-14 12:17:34 +000015/* The default encoding used by the platform file system APIs
16 Can remain NULL for all platforms that don't have such a concept
17*/
Martin v. Löwis6238d2b2002-06-30 15:26:10 +000018#if defined(MS_WINDOWS) && defined(HAVE_USABLE_WCHAR_T)
Mark Hammond26cffde42001-05-14 12:17:34 +000019const char *Py_FileSystemDefaultEncoding = "mbcs";
Just van Rossumb9b8e9c2003-02-10 09:22:01 +000020#elif defined(__APPLE__)
21const char *Py_FileSystemDefaultEncoding = "utf-8";
Mark Hammond26cffde42001-05-14 12:17:34 +000022#else
23const char *Py_FileSystemDefaultEncoding = NULL; /* use default */
24#endif
Mark Hammondef8b6542001-05-13 08:04:26 +000025
Guido van Rossum12d12c51993-10-26 17:58:25 +000026/* Forward */
Tim Petersdbd9ba62000-07-09 03:09:57 +000027static PyObject *filterstring(PyObject *, PyObject *);
Martin v. Löwis8afd7572003-01-25 22:46:11 +000028#ifdef Py_USING_UNICODE
29static PyObject *filterunicode(PyObject *, PyObject *);
30#endif
Tim Petersdbd9ba62000-07-09 03:09:57 +000031static PyObject *filtertuple (PyObject *, PyObject *);
Guido van Rossum12d12c51993-10-26 17:58:25 +000032
Guido van Rossum79f25d91997-04-29 20:08:16 +000033static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +000034builtin___import__(PyObject *self, PyObject *args)
Guido van Rossum3f5da241990-12-20 15:06:42 +000035{
Guido van Rossum1ae940a1995-01-02 19:04:15 +000036 char *name;
Guido van Rossum79f25d91997-04-29 20:08:16 +000037 PyObject *globals = NULL;
38 PyObject *locals = NULL;
39 PyObject *fromlist = NULL;
Guido van Rossum1ae940a1995-01-02 19:04:15 +000040
Guido van Rossum79f25d91997-04-29 20:08:16 +000041 if (!PyArg_ParseTuple(args, "s|OOO:__import__",
Guido van Rossum24c13741995-02-14 09:42:43 +000042 &name, &globals, &locals, &fromlist))
Guido van Rossum1ae940a1995-01-02 19:04:15 +000043 return NULL;
Guido van Rossumaee0bad1997-09-05 07:33:22 +000044 return PyImport_ImportModuleEx(name, globals, locals, fromlist);
Guido van Rossum1ae940a1995-01-02 19:04:15 +000045}
46
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000047PyDoc_STRVAR(import_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +000048"__import__(name, globals, locals, fromlist) -> module\n\
49\n\
50Import a module. The globals are only used to determine the context;\n\
51they are not modified. The locals are currently unused. The fromlist\n\
52should be a list of names to emulate ``from name import ...'', or an\n\
53empty list to emulate ``import name''.\n\
54When importing a module from a package, note that __import__('A.B', ...)\n\
55returns package A when fromlist is empty, but its submodule B when\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000056fromlist is not empty.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +000057
Guido van Rossum1ae940a1995-01-02 19:04:15 +000058
Guido van Rossum79f25d91997-04-29 20:08:16 +000059static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +000060builtin_abs(PyObject *self, PyObject *v)
Guido van Rossum1ae940a1995-01-02 19:04:15 +000061{
Guido van Rossum09df08a1998-05-22 00:51:39 +000062 return PyNumber_Absolute(v);
Guido van Rossum3f5da241990-12-20 15:06:42 +000063}
64
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000065PyDoc_STRVAR(abs_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +000066"abs(number) -> number\n\
67\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000068Return the absolute value of the argument.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +000069
Raymond Hettinger96229b12005-03-11 06:49:40 +000070static PyObject *
71builtin_all(PyObject *self, PyObject *v)
72{
73 PyObject *it, *item;
74
75 it = PyObject_GetIter(v);
76 if (it == NULL)
77 return NULL;
78
79 while ((item = PyIter_Next(it)) != NULL) {
80 int cmp = PyObject_IsTrue(item);
81 Py_DECREF(item);
82 if (cmp < 0) {
83 Py_DECREF(it);
84 return NULL;
85 }
86 if (cmp == 0) {
87 Py_DECREF(it);
88 Py_RETURN_FALSE;
89 }
90 }
91 Py_DECREF(it);
92 if (PyErr_Occurred())
93 return NULL;
94 Py_RETURN_TRUE;
95}
96
97PyDoc_STRVAR(all_doc,
98"all(iterable) -> bool\n\
99\n\
100Return True if bool(x) is True for all values x in the iterable.");
101
102static PyObject *
103builtin_any(PyObject *self, PyObject *v)
104{
105 PyObject *it, *item;
106
107 it = PyObject_GetIter(v);
108 if (it == NULL)
109 return NULL;
110
111 while ((item = PyIter_Next(it)) != NULL) {
112 int cmp = PyObject_IsTrue(item);
113 Py_DECREF(item);
114 if (cmp < 0) {
115 Py_DECREF(it);
116 return NULL;
117 }
118 if (cmp == 1) {
119 Py_DECREF(it);
120 Py_RETURN_TRUE;
121 }
122 }
123 Py_DECREF(it);
124 if (PyErr_Occurred())
125 return NULL;
126 Py_RETURN_FALSE;
127}
128
129PyDoc_STRVAR(any_doc,
130"any(iterable) -> bool\n\
131\n\
132Return True if bool(x) is True for any x in the iterable.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000133
Guido van Rossum79f25d91997-04-29 20:08:16 +0000134static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000135builtin_apply(PyObject *self, PyObject *args)
Guido van Rossumc02e15c1991-12-16 13:03:00 +0000136{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000137 PyObject *func, *alist = NULL, *kwdict = NULL;
Barry Warsaw968f8cb1998-10-01 15:33:12 +0000138 PyObject *t = NULL, *retval = NULL;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000139
Raymond Hettingerea3fdf42002-12-29 16:33:45 +0000140 if (!PyArg_UnpackTuple(args, "apply", 1, 3, &func, &alist, &kwdict))
Guido van Rossumc02e15c1991-12-16 13:03:00 +0000141 return NULL;
Barry Warsaw968f8cb1998-10-01 15:33:12 +0000142 if (alist != NULL) {
143 if (!PyTuple_Check(alist)) {
144 if (!PySequence_Check(alist)) {
Jeremy Hyltonc862cf42001-01-19 03:25:05 +0000145 PyErr_Format(PyExc_TypeError,
Alex Martellia9b9c9f2003-04-23 13:34:35 +0000146 "apply() arg 2 expected sequence, found %s",
Jeremy Hyltonc862cf42001-01-19 03:25:05 +0000147 alist->ob_type->tp_name);
Barry Warsaw968f8cb1998-10-01 15:33:12 +0000148 return NULL;
149 }
150 t = PySequence_Tuple(alist);
151 if (t == NULL)
152 return NULL;
153 alist = t;
154 }
Guido van Rossum2d951851994-08-29 12:52:16 +0000155 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000156 if (kwdict != NULL && !PyDict_Check(kwdict)) {
Jeremy Hyltonc862cf42001-01-19 03:25:05 +0000157 PyErr_Format(PyExc_TypeError,
158 "apply() arg 3 expected dictionary, found %s",
159 kwdict->ob_type->tp_name);
Barry Warsaw968f8cb1998-10-01 15:33:12 +0000160 goto finally;
Guido van Rossum681d79a1995-07-18 14:51:37 +0000161 }
Barry Warsaw968f8cb1998-10-01 15:33:12 +0000162 retval = PyEval_CallObjectWithKeywords(func, alist, kwdict);
163 finally:
164 Py_XDECREF(t);
165 return retval;
Guido van Rossumc02e15c1991-12-16 13:03:00 +0000166}
167
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000168PyDoc_STRVAR(apply_doc,
Fred Drakef1fbc622001-01-12 17:05:05 +0000169"apply(object[, args[, kwargs]]) -> value\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000170\n\
Fred Drake7b912121999-12-23 14:16:55 +0000171Call a callable object with positional arguments taken from the tuple args,\n\
172and keyword arguments taken from the optional dictionary kwargs.\n\
Raymond Hettingerff41c482003-04-06 09:01:11 +0000173Note that classes are callable, as are instances with a __call__() method.\n\
174\n\
175Deprecated since release 2.3. Instead, use the extended call syntax:\n\
176 function(*args, **keywords).");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000177
178
Guido van Rossum79f25d91997-04-29 20:08:16 +0000179static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000180builtin_callable(PyObject *self, PyObject *v)
Guido van Rossum2d951851994-08-29 12:52:16 +0000181{
Guido van Rossum77f6a652002-04-03 22:41:51 +0000182 return PyBool_FromLong((long)PyCallable_Check(v));
Guido van Rossum2d951851994-08-29 12:52:16 +0000183}
184
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000185PyDoc_STRVAR(callable_doc,
Guido van Rossum77f6a652002-04-03 22:41:51 +0000186"callable(object) -> bool\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000187\n\
188Return whether the object is callable (i.e., some kind of function).\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000189Note that classes are callable, as are instances with a __call__() method.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000190
191
Guido van Rossum79f25d91997-04-29 20:08:16 +0000192static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000193builtin_filter(PyObject *self, PyObject *args)
Guido van Rossum12d12c51993-10-26 17:58:25 +0000194{
Guido van Rossumc7903a12002-08-16 07:04:56 +0000195 PyObject *func, *seq, *result, *it, *arg;
Martin v. Löwisd96ee902006-02-16 14:37:16 +0000196 Py_ssize_t len; /* guess for result list size */
197 register Py_ssize_t j;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000198
Raymond Hettingerea3fdf42002-12-29 16:33:45 +0000199 if (!PyArg_UnpackTuple(args, "filter", 2, 2, &func, &seq))
Guido van Rossum12d12c51993-10-26 17:58:25 +0000200 return NULL;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000201
Tim Peters0e57abf2001-05-02 07:39:38 +0000202 /* Strings and tuples return a result of the same type. */
203 if (PyString_Check(seq))
204 return filterstring(func, seq);
Martin v. Löwis8afd7572003-01-25 22:46:11 +0000205#ifdef Py_USING_UNICODE
206 if (PyUnicode_Check(seq))
207 return filterunicode(func, seq);
208#endif
Tim Peters0e57abf2001-05-02 07:39:38 +0000209 if (PyTuple_Check(seq))
210 return filtertuple(func, seq);
211
Georg Brandle35b6572005-07-19 22:20:20 +0000212 /* Pre-allocate argument list tuple. */
213 arg = PyTuple_New(1);
214 if (arg == NULL)
215 return NULL;
216
Tim Peters0e57abf2001-05-02 07:39:38 +0000217 /* Get iterator. */
218 it = PyObject_GetIter(seq);
219 if (it == NULL)
Georg Brandle35b6572005-07-19 22:20:20 +0000220 goto Fail_arg;
Tim Peters0e57abf2001-05-02 07:39:38 +0000221
222 /* Guess a result list size. */
Armin Rigof5b3e362006-02-11 21:32:43 +0000223 len = _PyObject_LengthHint(seq);
Raymond Hettingerb86269d2004-01-04 11:00:08 +0000224 if (len < 0) {
Raymond Hettingera710b332005-08-21 11:03:59 +0000225 if (!PyErr_ExceptionMatches(PyExc_TypeError) &&
226 !PyErr_ExceptionMatches(PyExc_AttributeError)) {
227 goto Fail_it;
228 }
Raymond Hettingerb86269d2004-01-04 11:00:08 +0000229 PyErr_Clear();
230 len = 8; /* arbitrary */
Guido van Rossum12d12c51993-10-26 17:58:25 +0000231 }
232
Tim Peters0e57abf2001-05-02 07:39:38 +0000233 /* Get a result list. */
Guido van Rossum79f25d91997-04-29 20:08:16 +0000234 if (PyList_Check(seq) && seq->ob_refcnt == 1) {
Tim Peters0e57abf2001-05-02 07:39:38 +0000235 /* Eww - can modify the list in-place. */
Guido van Rossum79f25d91997-04-29 20:08:16 +0000236 Py_INCREF(seq);
Guido van Rossum12d12c51993-10-26 17:58:25 +0000237 result = seq;
238 }
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000239 else {
Tim Peters0e57abf2001-05-02 07:39:38 +0000240 result = PyList_New(len);
241 if (result == NULL)
242 goto Fail_it;
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000243 }
Guido van Rossum12d12c51993-10-26 17:58:25 +0000244
Tim Peters0e57abf2001-05-02 07:39:38 +0000245 /* Build the result list. */
Tim Petersf4848da2001-05-05 00:14:56 +0000246 j = 0;
247 for (;;) {
Guido van Rossumc7903a12002-08-16 07:04:56 +0000248 PyObject *item;
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000249 int ok;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000250
Tim Peters0e57abf2001-05-02 07:39:38 +0000251 item = PyIter_Next(it);
252 if (item == NULL) {
Tim Petersf4848da2001-05-05 00:14:56 +0000253 if (PyErr_Occurred())
254 goto Fail_result_it;
Tim Peters0e57abf2001-05-02 07:39:38 +0000255 break;
Guido van Rossum2d951851994-08-29 12:52:16 +0000256 }
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000257
Neil Schemenauer68973552003-08-14 20:37:34 +0000258 if (func == (PyObject *)&PyBool_Type || func == Py_None) {
Guido van Rossumc7903a12002-08-16 07:04:56 +0000259 ok = PyObject_IsTrue(item);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000260 }
261 else {
Guido van Rossumc7903a12002-08-16 07:04:56 +0000262 PyObject *good;
263 PyTuple_SET_ITEM(arg, 0, item);
264 good = PyObject_Call(func, arg, NULL);
265 PyTuple_SET_ITEM(arg, 0, NULL);
Guido van Rossum58b68731995-01-10 17:40:55 +0000266 if (good == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000267 Py_DECREF(item);
Tim Peters0e57abf2001-05-02 07:39:38 +0000268 goto Fail_result_it;
Guido van Rossum58b68731995-01-10 17:40:55 +0000269 }
Guido van Rossumc7903a12002-08-16 07:04:56 +0000270 ok = PyObject_IsTrue(good);
271 Py_DECREF(good);
Guido van Rossum12d12c51993-10-26 17:58:25 +0000272 }
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000273 if (ok) {
Tim Peters0e57abf2001-05-02 07:39:38 +0000274 if (j < len)
275 PyList_SET_ITEM(result, j, item);
Guido van Rossum2d951851994-08-29 12:52:16 +0000276 else {
Barry Warsawfa77e091999-01-28 18:49:12 +0000277 int status = PyList_Append(result, item);
Barry Warsawfa77e091999-01-28 18:49:12 +0000278 Py_DECREF(item);
279 if (status < 0)
Tim Peters0e57abf2001-05-02 07:39:38 +0000280 goto Fail_result_it;
Guido van Rossum2d951851994-08-29 12:52:16 +0000281 }
Tim Peters0e57abf2001-05-02 07:39:38 +0000282 ++j;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000283 }
Tim Peters0e57abf2001-05-02 07:39:38 +0000284 else
285 Py_DECREF(item);
Guido van Rossum12d12c51993-10-26 17:58:25 +0000286 }
287
Guido van Rossum12d12c51993-10-26 17:58:25 +0000288
Tim Peters0e57abf2001-05-02 07:39:38 +0000289 /* Cut back result list if len is too big. */
Guido van Rossum79f25d91997-04-29 20:08:16 +0000290 if (j < len && PyList_SetSlice(result, j, len, NULL) < 0)
Tim Peters0e57abf2001-05-02 07:39:38 +0000291 goto Fail_result_it;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000292
Tim Peters3c6b1482001-05-21 08:07:05 +0000293 Py_DECREF(it);
Guido van Rossumc7903a12002-08-16 07:04:56 +0000294 Py_DECREF(arg);
Guido van Rossum12d12c51993-10-26 17:58:25 +0000295 return result;
296
Tim Peters0e57abf2001-05-02 07:39:38 +0000297Fail_result_it:
Guido van Rossum79f25d91997-04-29 20:08:16 +0000298 Py_DECREF(result);
Tim Peters0e57abf2001-05-02 07:39:38 +0000299Fail_it:
300 Py_DECREF(it);
Guido van Rossumc7903a12002-08-16 07:04:56 +0000301Fail_arg:
302 Py_DECREF(arg);
Guido van Rossum12d12c51993-10-26 17:58:25 +0000303 return NULL;
304}
305
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000306PyDoc_STRVAR(filter_doc,
Tim Petersd50e5442002-03-09 00:06:26 +0000307"filter(function or None, sequence) -> list, tuple, or string\n"
308"\n"
309"Return those items of sequence for which function(item) is true. If\n"
310"function is None, return the items that are true. If sequence is a tuple\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000311"or string, return the same type, else return a list.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000312
Guido van Rossum79f25d91997-04-29 20:08:16 +0000313static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000314builtin_chr(PyObject *self, PyObject *args)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000315{
316 long x;
317 char s[1];
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000318
Guido van Rossum79f25d91997-04-29 20:08:16 +0000319 if (!PyArg_ParseTuple(args, "l:chr", &x))
Guido van Rossum3f5da241990-12-20 15:06:42 +0000320 return NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000321 if (x < 0 || x >= 256) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000322 PyErr_SetString(PyExc_ValueError,
323 "chr() arg not in range(256)");
Guido van Rossum3f5da241990-12-20 15:06:42 +0000324 return NULL;
325 }
Guido van Rossum6bf62da1997-04-11 20:37:35 +0000326 s[0] = (char)x;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000327 return PyString_FromStringAndSize(s, 1);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000328}
329
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000330PyDoc_STRVAR(chr_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000331"chr(i) -> character\n\
332\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000333Return a string of one character with ordinal i; 0 <= i < 256.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000334
335
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000336#ifdef Py_USING_UNICODE
Guido van Rossum79f25d91997-04-29 20:08:16 +0000337static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000338builtin_unichr(PyObject *self, PyObject *args)
Guido van Rossum09095f32000-03-10 23:00:52 +0000339{
340 long x;
Guido van Rossum09095f32000-03-10 23:00:52 +0000341
342 if (!PyArg_ParseTuple(args, "l:unichr", &x))
343 return NULL;
Fredrik Lundh0dcf67e2001-06-26 20:01:56 +0000344
Marc-André Lemburgcc8764c2002-08-11 12:23:04 +0000345 return PyUnicode_FromOrdinal(x);
Guido van Rossum09095f32000-03-10 23:00:52 +0000346}
347
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000348PyDoc_STRVAR(unichr_doc,
Fred Drake078b24f2000-04-13 02:42:50 +0000349"unichr(i) -> Unicode character\n\
Guido van Rossum09095f32000-03-10 23:00:52 +0000350\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000351Return a Unicode string of one character with ordinal i; 0 <= i <= 0x10ffff.");
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000352#endif
Guido van Rossum09095f32000-03-10 23:00:52 +0000353
354
355static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000356builtin_cmp(PyObject *self, PyObject *args)
Guido van Rossuma9e7dc11992-10-18 18:53:57 +0000357{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000358 PyObject *a, *b;
Guido van Rossum09df08a1998-05-22 00:51:39 +0000359 int c;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000360
Raymond Hettingerea3fdf42002-12-29 16:33:45 +0000361 if (!PyArg_UnpackTuple(args, "cmp", 2, 2, &a, &b))
Guido van Rossuma9e7dc11992-10-18 18:53:57 +0000362 return NULL;
Guido van Rossum09df08a1998-05-22 00:51:39 +0000363 if (PyObject_Cmp(a, b, &c) < 0)
Guido van Rossumc8b6df91997-05-23 00:06:51 +0000364 return NULL;
Guido van Rossum09df08a1998-05-22 00:51:39 +0000365 return PyInt_FromLong((long)c);
Guido van Rossuma9e7dc11992-10-18 18:53:57 +0000366}
367
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000368PyDoc_STRVAR(cmp_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000369"cmp(x, y) -> integer\n\
370\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000371Return negative if x<y, zero if x==y, positive if x>y.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000372
373
Guido van Rossum79f25d91997-04-29 20:08:16 +0000374static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000375builtin_coerce(PyObject *self, PyObject *args)
Guido van Rossum6a00cd81995-01-07 12:39:01 +0000376{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000377 PyObject *v, *w;
378 PyObject *res;
Guido van Rossum5524a591995-01-10 15:26:20 +0000379
Raymond Hettingerea3fdf42002-12-29 16:33:45 +0000380 if (!PyArg_UnpackTuple(args, "coerce", 2, 2, &v, &w))
Guido van Rossum5524a591995-01-10 15:26:20 +0000381 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000382 if (PyNumber_Coerce(&v, &w) < 0)
Guido van Rossum04691fc1992-08-12 15:35:34 +0000383 return NULL;
Raymond Hettinger8ae46892003-10-12 19:09:37 +0000384 res = PyTuple_Pack(2, v, w);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000385 Py_DECREF(v);
386 Py_DECREF(w);
Guido van Rossum04691fc1992-08-12 15:35:34 +0000387 return res;
388}
389
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000390PyDoc_STRVAR(coerce_doc,
Martin v. Löwis8d494f32004-08-25 10:42:41 +0000391"coerce(x, y) -> (x1, y1)\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000392\n\
Martin v. Löwis8d494f32004-08-25 10:42:41 +0000393Return a tuple consisting of the two numeric arguments converted to\n\
394a common type, using the same rules as used by arithmetic operations.\n\
395If coercion is not possible, raise TypeError.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000396
Guido van Rossum79f25d91997-04-29 20:08:16 +0000397static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000398builtin_compile(PyObject *self, PyObject *args)
Guido van Rossum5b722181993-03-30 17:46:03 +0000399{
400 char *str;
401 char *filename;
402 char *startstr;
403 int start;
Tim Peters6cd6a822001-08-17 22:11:27 +0000404 int dont_inherit = 0;
405 int supplied_flags = 0;
Tim Peters5ba58662001-07-16 02:29:45 +0000406 PyCompilerFlags cf;
Neal Norwitz3a9a3e72005-11-27 20:38:31 +0000407 PyObject *result = NULL, *cmd, *tmp = NULL;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000408 Py_ssize_t length;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000409
Just van Rossum3aaf42c2003-02-10 08:21:10 +0000410 if (!PyArg_ParseTuple(args, "Oss|ii:compile", &cmd, &filename,
Tim Peters6cd6a822001-08-17 22:11:27 +0000411 &startstr, &supplied_flags, &dont_inherit))
Guido van Rossum5b722181993-03-30 17:46:03 +0000412 return NULL;
Tim Peters6cd6a822001-08-17 22:11:27 +0000413
Just van Rossum3aaf42c2003-02-10 08:21:10 +0000414 cf.cf_flags = supplied_flags;
415
416#ifdef Py_USING_UNICODE
417 if (PyUnicode_Check(cmd)) {
418 tmp = PyUnicode_AsUTF8String(cmd);
419 if (tmp == NULL)
420 return NULL;
421 cmd = tmp;
422 cf.cf_flags |= PyCF_SOURCE_IS_UTF8;
423 }
424#endif
Just van Rossumb9b8e9c2003-02-10 09:22:01 +0000425 if (PyObject_AsReadBuffer(cmd, (const void **)&str, &length))
426 return NULL;
Tim Peters2c646c92003-02-10 14:48:29 +0000427 if ((size_t)length != strlen(str)) {
Just van Rossum3aaf42c2003-02-10 08:21:10 +0000428 PyErr_SetString(PyExc_TypeError,
Alex Martellia9b9c9f2003-04-23 13:34:35 +0000429 "compile() expected string without null bytes");
Neal Norwitz3a9a3e72005-11-27 20:38:31 +0000430 goto cleanup;
Just van Rossum3aaf42c2003-02-10 08:21:10 +0000431 }
432
Guido van Rossum5b722181993-03-30 17:46:03 +0000433 if (strcmp(startstr, "exec") == 0)
Guido van Rossumb05a5c71997-05-07 17:46:13 +0000434 start = Py_file_input;
Guido van Rossum5b722181993-03-30 17:46:03 +0000435 else if (strcmp(startstr, "eval") == 0)
Guido van Rossumb05a5c71997-05-07 17:46:13 +0000436 start = Py_eval_input;
Guido van Rossum872537c1995-07-07 22:43:42 +0000437 else if (strcmp(startstr, "single") == 0)
Guido van Rossumb05a5c71997-05-07 17:46:13 +0000438 start = Py_single_input;
Guido van Rossum5b722181993-03-30 17:46:03 +0000439 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000440 PyErr_SetString(PyExc_ValueError,
Fred Drake661ea262000-10-24 19:57:45 +0000441 "compile() arg 3 must be 'exec' or 'eval' or 'single'");
Neal Norwitz3a9a3e72005-11-27 20:38:31 +0000442 goto cleanup;
Guido van Rossum5b722181993-03-30 17:46:03 +0000443 }
Tim Peters6cd6a822001-08-17 22:11:27 +0000444
Guido van Rossum4b499dd32003-02-13 22:07:59 +0000445 if (supplied_flags &
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000446 ~(PyCF_MASK | PyCF_MASK_OBSOLETE | PyCF_DONT_IMPLY_DEDENT | PyCF_ONLY_AST))
Guido van Rossum4b499dd32003-02-13 22:07:59 +0000447 {
Tim Peters6cd6a822001-08-17 22:11:27 +0000448 PyErr_SetString(PyExc_ValueError,
449 "compile(): unrecognised flags");
Neal Norwitz3a9a3e72005-11-27 20:38:31 +0000450 goto cleanup;
Tim Peters6cd6a822001-08-17 22:11:27 +0000451 }
452 /* XXX Warn if (supplied_flags & PyCF_MASK_OBSOLETE) != 0? */
453
Tim Peters6cd6a822001-08-17 22:11:27 +0000454 if (!dont_inherit) {
455 PyEval_MergeCompilerFlags(&cf);
456 }
Just van Rossum3aaf42c2003-02-10 08:21:10 +0000457 result = Py_CompileStringFlags(str, filename, start, &cf);
Neal Norwitz3a9a3e72005-11-27 20:38:31 +0000458cleanup:
Just van Rossum3aaf42c2003-02-10 08:21:10 +0000459 Py_XDECREF(tmp);
460 return result;
Guido van Rossum5b722181993-03-30 17:46:03 +0000461}
462
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000463PyDoc_STRVAR(compile_doc,
Tim Peters6cd6a822001-08-17 22:11:27 +0000464"compile(source, filename, mode[, flags[, dont_inherit]]) -> code object\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000465\n\
466Compile the source string (a Python module, statement or expression)\n\
467into a code object that can be executed by the exec statement or eval().\n\
468The filename will be used for run-time error messages.\n\
469The mode must be 'exec' to compile a module, 'single' to compile a\n\
Tim Peters6cd6a822001-08-17 22:11:27 +0000470single (interactive) statement, or 'eval' to compile an expression.\n\
471The flags argument, if present, controls which future statements influence\n\
472the compilation of the code.\n\
473The dont_inherit argument, if non-zero, stops the compilation inheriting\n\
474the effects of any future statements in effect in the code calling\n\
475compile; if absent or zero these statements do influence the compilation,\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000476in addition to any features explicitly specified.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000477
Guido van Rossum79f25d91997-04-29 20:08:16 +0000478static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000479builtin_dir(PyObject *self, PyObject *args)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000480{
Tim Peters5d2b77c2001-09-03 05:47:38 +0000481 PyObject *arg = NULL;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000482
Raymond Hettingerea3fdf42002-12-29 16:33:45 +0000483 if (!PyArg_UnpackTuple(args, "dir", 0, 1, &arg))
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000484 return NULL;
Tim Peters7eea37e2001-09-04 22:08:56 +0000485 return PyObject_Dir(arg);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000486}
487
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000488PyDoc_STRVAR(dir_doc,
Tim Peters5d2b77c2001-09-03 05:47:38 +0000489"dir([object]) -> list of strings\n"
490"\n"
491"Return an alphabetized list of names comprising (some of) the attributes\n"
492"of the given object, and of attributes reachable from it:\n"
493"\n"
494"No argument: the names in the current scope.\n"
495"Module object: the module attributes.\n"
Tim Peters37a309d2001-09-04 01:20:04 +0000496"Type or class object: its attributes, and recursively the attributes of\n"
497" its bases.\n"
Tim Peters5d2b77c2001-09-03 05:47:38 +0000498"Otherwise: its attributes, its class's attributes, and recursively the\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000499" attributes of its class's base classes.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000500
Guido van Rossum79f25d91997-04-29 20:08:16 +0000501static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000502builtin_divmod(PyObject *self, PyObject *args)
Guido van Rossum6a00cd81995-01-07 12:39:01 +0000503{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000504 PyObject *v, *w;
Guido van Rossum6a00cd81995-01-07 12:39:01 +0000505
Raymond Hettingerea3fdf42002-12-29 16:33:45 +0000506 if (!PyArg_UnpackTuple(args, "divmod", 2, 2, &v, &w))
Guido van Rossum6a00cd81995-01-07 12:39:01 +0000507 return NULL;
Guido van Rossum09df08a1998-05-22 00:51:39 +0000508 return PyNumber_Divmod(v, w);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000509}
510
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000511PyDoc_STRVAR(divmod_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000512"divmod(x, y) -> (div, mod)\n\
513\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000514Return the tuple ((x-x%y)/y, x%y). Invariant: div*y + mod == x.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000515
516
Guido van Rossum79f25d91997-04-29 20:08:16 +0000517static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000518builtin_eval(PyObject *self, PyObject *args)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000519{
Just van Rossum3aaf42c2003-02-10 08:21:10 +0000520 PyObject *cmd, *result, *tmp = NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000521 PyObject *globals = Py_None, *locals = Py_None;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000522 char *str;
Tim Peters9fa96be2001-08-17 23:04:59 +0000523 PyCompilerFlags cf;
Guido van Rossum590baa41993-11-30 13:40:46 +0000524
Raymond Hettinger214b1c32004-07-02 06:41:07 +0000525 if (!PyArg_UnpackTuple(args, "eval", 1, 3, &cmd, &globals, &locals))
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000526 return NULL;
Raymond Hettinger214b1c32004-07-02 06:41:07 +0000527 if (locals != Py_None && !PyMapping_Check(locals)) {
528 PyErr_SetString(PyExc_TypeError, "locals must be a mapping");
Raymond Hettinger513ffe82004-07-06 13:44:41 +0000529 return NULL;
Raymond Hettinger214b1c32004-07-02 06:41:07 +0000530 }
531 if (globals != Py_None && !PyDict_Check(globals)) {
Georg Brandl99d7e4e2005-08-31 22:21:15 +0000532 PyErr_SetString(PyExc_TypeError, PyMapping_Check(globals) ?
Raymond Hettinger214b1c32004-07-02 06:41:07 +0000533 "globals must be a real dict; try eval(expr, {}, mapping)"
534 : "globals must be a dict");
Raymond Hettinger513ffe82004-07-06 13:44:41 +0000535 return NULL;
Raymond Hettinger214b1c32004-07-02 06:41:07 +0000536 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000537 if (globals == Py_None) {
538 globals = PyEval_GetGlobals();
539 if (locals == Py_None)
540 locals = PyEval_GetLocals();
Guido van Rossum6135a871995-01-09 17:53:26 +0000541 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000542 else if (locals == Py_None)
Guido van Rossum6135a871995-01-09 17:53:26 +0000543 locals = globals;
Tim Peters9fa96be2001-08-17 23:04:59 +0000544
Georg Brandl77c85e62005-09-15 10:46:13 +0000545 if (globals == NULL || locals == NULL) {
546 PyErr_SetString(PyExc_TypeError,
547 "eval must be given globals and locals "
548 "when called without a frame");
549 return NULL;
550 }
551
Guido van Rossum79f25d91997-04-29 20:08:16 +0000552 if (PyDict_GetItemString(globals, "__builtins__") == NULL) {
553 if (PyDict_SetItemString(globals, "__builtins__",
554 PyEval_GetBuiltins()) != 0)
Guido van Rossum6135a871995-01-09 17:53:26 +0000555 return NULL;
556 }
Tim Peters9fa96be2001-08-17 23:04:59 +0000557
Jeremy Hylton15c1c4f2001-07-30 21:50:55 +0000558 if (PyCode_Check(cmd)) {
Jeremy Hylton733c8932001-12-13 19:51:56 +0000559 if (PyCode_GetNumFree((PyCodeObject *)cmd) > 0) {
Jeremy Hylton15c1c4f2001-07-30 21:50:55 +0000560 PyErr_SetString(PyExc_TypeError,
561 "code object passed to eval() may not contain free variables");
562 return NULL;
563 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000564 return PyEval_EvalCode((PyCodeObject *) cmd, globals, locals);
Jeremy Hylton15c1c4f2001-07-30 21:50:55 +0000565 }
Tim Peters9fa96be2001-08-17 23:04:59 +0000566
Marc-André Lemburgd1ba4432000-09-19 21:04:18 +0000567 if (!PyString_Check(cmd) &&
568 !PyUnicode_Check(cmd)) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000569 PyErr_SetString(PyExc_TypeError,
Fred Drake661ea262000-10-24 19:57:45 +0000570 "eval() arg 1 must be a string or code object");
Guido van Rossum94390a41992-08-14 15:14:30 +0000571 return NULL;
572 }
Just van Rossum3aaf42c2003-02-10 08:21:10 +0000573 cf.cf_flags = 0;
574
575#ifdef Py_USING_UNICODE
576 if (PyUnicode_Check(cmd)) {
577 tmp = PyUnicode_AsUTF8String(cmd);
578 if (tmp == NULL)
579 return NULL;
580 cmd = tmp;
581 cf.cf_flags |= PyCF_SOURCE_IS_UTF8;
582 }
583#endif
Neal Norwitz3a9a3e72005-11-27 20:38:31 +0000584 if (PyString_AsStringAndSize(cmd, &str, NULL)) {
585 Py_XDECREF(tmp);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000586 return NULL;
Neal Norwitz3a9a3e72005-11-27 20:38:31 +0000587 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000588 while (*str == ' ' || *str == '\t')
589 str++;
Tim Peters9fa96be2001-08-17 23:04:59 +0000590
Tim Peters9fa96be2001-08-17 23:04:59 +0000591 (void)PyEval_MergeCompilerFlags(&cf);
Just van Rossum3aaf42c2003-02-10 08:21:10 +0000592 result = PyRun_StringFlags(str, Py_eval_input, globals, locals, &cf);
593 Py_XDECREF(tmp);
594 return result;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000595}
596
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000597PyDoc_STRVAR(eval_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000598"eval(source[, globals[, locals]]) -> value\n\
599\n\
600Evaluate the source in the context of globals and locals.\n\
601The source may be a string representing a Python expression\n\
602or a code object as returned by compile().\n\
Raymond Hettinger214b1c32004-07-02 06:41:07 +0000603The globals must be a dictionary and locals can be any mappping,\n\
604defaulting to the current globals and locals.\n\
605If only globals is given, locals defaults to it.\n");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000606
607
Guido van Rossum79f25d91997-04-29 20:08:16 +0000608static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000609builtin_execfile(PyObject *self, PyObject *args)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000610{
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000611 char *filename;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000612 PyObject *globals = Py_None, *locals = Py_None;
613 PyObject *res;
Guido van Rossumb3a639e2001-09-05 13:37:47 +0000614 FILE* fp = NULL;
Tim Peters5ba58662001-07-16 02:29:45 +0000615 PyCompilerFlags cf;
Martin v. Löwis6b3a2c42001-08-08 05:30:36 +0000616 int exists;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000617
Raymond Hettinger66bd2332004-08-02 08:30:07 +0000618 if (!PyArg_ParseTuple(args, "s|O!O:execfile",
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000619 &filename,
Guido van Rossum79f25d91997-04-29 20:08:16 +0000620 &PyDict_Type, &globals,
Raymond Hettinger66bd2332004-08-02 08:30:07 +0000621 &locals))
Guido van Rossum0f61f8a1992-02-25 18:55:05 +0000622 return NULL;
Raymond Hettinger66bd2332004-08-02 08:30:07 +0000623 if (locals != Py_None && !PyMapping_Check(locals)) {
624 PyErr_SetString(PyExc_TypeError, "locals must be a mapping");
625 return NULL;
626 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000627 if (globals == Py_None) {
628 globals = PyEval_GetGlobals();
629 if (locals == Py_None)
630 locals = PyEval_GetLocals();
Guido van Rossum6135a871995-01-09 17:53:26 +0000631 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000632 else if (locals == Py_None)
Guido van Rossum6135a871995-01-09 17:53:26 +0000633 locals = globals;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000634 if (PyDict_GetItemString(globals, "__builtins__") == NULL) {
635 if (PyDict_SetItemString(globals, "__builtins__",
636 PyEval_GetBuiltins()) != 0)
Guido van Rossum6135a871995-01-09 17:53:26 +0000637 return NULL;
638 }
Martin v. Löwis6b3a2c42001-08-08 05:30:36 +0000639
640 exists = 0;
641 /* Test for existence or directory. */
Martin v. Löwis3484a182002-03-09 12:07:51 +0000642#if defined(PLAN9)
643 {
644 Dir *d;
645
646 if ((d = dirstat(filename))!=nil) {
647 if(d->mode & DMDIR)
648 werrstr("is a directory");
649 else
650 exists = 1;
651 free(d);
652 }
Martin v. Löwis6b3a2c42001-08-08 05:30:36 +0000653 }
Martin v. Löwis3484a182002-03-09 12:07:51 +0000654#elif defined(RISCOS)
Guido van Rossume2ae77b2001-10-24 20:42:55 +0000655 if (object_exists(filename)) {
656 if (isdir(filename))
657 errno = EISDIR;
658 else
659 exists = 1;
660 }
Martin v. Löwis3484a182002-03-09 12:07:51 +0000661#else /* standard Posix */
662 {
663 struct stat s;
664 if (stat(filename, &s) == 0) {
665 if (S_ISDIR(s.st_mode))
Andrew MacIntyreda4d6cb2004-03-29 11:53:38 +0000666# if defined(PYOS_OS2) && defined(PYCC_VACPP)
Martin v. Löwis3484a182002-03-09 12:07:51 +0000667 errno = EOS2ERR;
668# else
669 errno = EISDIR;
670# endif
671 else
672 exists = 1;
673 }
674 }
675#endif
Martin v. Löwis6b3a2c42001-08-08 05:30:36 +0000676
677 if (exists) {
678 Py_BEGIN_ALLOW_THREADS
Jack Jansen7b8c7542002-04-14 20:12:41 +0000679 fp = fopen(filename, "r" PY_STDIOTEXTMODE);
Martin v. Löwis6b3a2c42001-08-08 05:30:36 +0000680 Py_END_ALLOW_THREADS
681
682 if (fp == NULL) {
683 exists = 0;
684 }
685 }
686
687 if (!exists) {
Peter Schneider-Kamp4c013422002-08-27 16:58:00 +0000688 PyErr_SetFromErrnoWithFilename(PyExc_IOError, filename);
Guido van Rossum0f61f8a1992-02-25 18:55:05 +0000689 return NULL;
690 }
Tim Peters5ba58662001-07-16 02:29:45 +0000691 cf.cf_flags = 0;
692 if (PyEval_MergeCompilerFlags(&cf))
Tim Peters748b8bb2001-04-28 08:20:22 +0000693 res = PyRun_FileExFlags(fp, filename, Py_file_input, globals,
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000694 locals, 1, &cf);
Tim Peters5ba58662001-07-16 02:29:45 +0000695 else
Tim Peters748b8bb2001-04-28 08:20:22 +0000696 res = PyRun_FileEx(fp, filename, Py_file_input, globals,
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000697 locals, 1);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000698 return res;
Guido van Rossum0f61f8a1992-02-25 18:55:05 +0000699}
700
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000701PyDoc_STRVAR(execfile_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000702"execfile(filename[, globals[, locals]])\n\
703\n\
704Read and execute a Python script from a file.\n\
705The globals and locals are dictionaries, defaulting to the current\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000706globals and locals. If only globals is given, locals defaults to it.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000707
708
Guido van Rossum79f25d91997-04-29 20:08:16 +0000709static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000710builtin_getattr(PyObject *self, PyObject *args)
Guido van Rossum33894be1992-01-27 16:53:09 +0000711{
Guido van Rossum950ff291998-06-29 13:38:57 +0000712 PyObject *v, *result, *dflt = NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000713 PyObject *name;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000714
Raymond Hettingerea3fdf42002-12-29 16:33:45 +0000715 if (!PyArg_UnpackTuple(args, "getattr", 2, 3, &v, &name, &dflt))
Guido van Rossum33894be1992-01-27 16:53:09 +0000716 return NULL;
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000717#ifdef Py_USING_UNICODE
Jeremy Hylton0eb11152001-07-30 22:39:31 +0000718 if (PyUnicode_Check(name)) {
719 name = _PyUnicode_AsDefaultEncodedString(name, NULL);
720 if (name == NULL)
721 return NULL;
722 }
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000723#endif
Jeremy Hylton0eb11152001-07-30 22:39:31 +0000724
725 if (!PyString_Check(name)) {
726 PyErr_SetString(PyExc_TypeError,
Alex Martellia9b9c9f2003-04-23 13:34:35 +0000727 "getattr(): attribute name must be string");
Jeremy Hylton0eb11152001-07-30 22:39:31 +0000728 return NULL;
729 }
Guido van Rossum950ff291998-06-29 13:38:57 +0000730 result = PyObject_GetAttr(v, name);
Guido van Rossumd8923572001-10-16 21:31:32 +0000731 if (result == NULL && dflt != NULL &&
732 PyErr_ExceptionMatches(PyExc_AttributeError))
733 {
Guido van Rossum950ff291998-06-29 13:38:57 +0000734 PyErr_Clear();
735 Py_INCREF(dflt);
736 result = dflt;
737 }
738 return result;
Guido van Rossum9bfef441993-03-29 10:43:31 +0000739}
740
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000741PyDoc_STRVAR(getattr_doc,
Guido van Rossum950ff291998-06-29 13:38:57 +0000742"getattr(object, name[, default]) -> value\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000743\n\
Guido van Rossum950ff291998-06-29 13:38:57 +0000744Get a named attribute from an object; getattr(x, 'y') is equivalent to x.y.\n\
745When a default argument is given, it is returned when the attribute doesn't\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000746exist; without it, an exception is raised in that case.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000747
748
Guido van Rossum79f25d91997-04-29 20:08:16 +0000749static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000750builtin_globals(PyObject *self)
Guido van Rossum872537c1995-07-07 22:43:42 +0000751{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000752 PyObject *d;
Guido van Rossum872537c1995-07-07 22:43:42 +0000753
Guido van Rossum79f25d91997-04-29 20:08:16 +0000754 d = PyEval_GetGlobals();
755 Py_INCREF(d);
Guido van Rossum872537c1995-07-07 22:43:42 +0000756 return d;
757}
758
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000759PyDoc_STRVAR(globals_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000760"globals() -> dictionary\n\
761\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000762Return the dictionary containing the current scope's global variables.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000763
764
Guido van Rossum79f25d91997-04-29 20:08:16 +0000765static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000766builtin_hasattr(PyObject *self, PyObject *args)
Guido van Rossum9bfef441993-03-29 10:43:31 +0000767{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000768 PyObject *v;
769 PyObject *name;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000770
Raymond Hettingerea3fdf42002-12-29 16:33:45 +0000771 if (!PyArg_UnpackTuple(args, "hasattr", 2, 2, &v, &name))
Guido van Rossum9bfef441993-03-29 10:43:31 +0000772 return NULL;
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000773#ifdef Py_USING_UNICODE
Jeremy Hylton302b54a2001-07-30 22:45:19 +0000774 if (PyUnicode_Check(name)) {
775 name = _PyUnicode_AsDefaultEncodedString(name, NULL);
776 if (name == NULL)
777 return NULL;
778 }
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000779#endif
Jeremy Hylton302b54a2001-07-30 22:45:19 +0000780
781 if (!PyString_Check(name)) {
782 PyErr_SetString(PyExc_TypeError,
Alex Martellia9b9c9f2003-04-23 13:34:35 +0000783 "hasattr(): attribute name must be string");
Jeremy Hylton302b54a2001-07-30 22:45:19 +0000784 return NULL;
785 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000786 v = PyObject_GetAttr(v, name);
Guido van Rossum9bfef441993-03-29 10:43:31 +0000787 if (v == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000788 PyErr_Clear();
Guido van Rossum09df08a1998-05-22 00:51:39 +0000789 Py_INCREF(Py_False);
790 return Py_False;
Guido van Rossum9bfef441993-03-29 10:43:31 +0000791 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000792 Py_DECREF(v);
Guido van Rossum09df08a1998-05-22 00:51:39 +0000793 Py_INCREF(Py_True);
794 return Py_True;
Guido van Rossum33894be1992-01-27 16:53:09 +0000795}
796
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000797PyDoc_STRVAR(hasattr_doc,
Guido van Rossum77f6a652002-04-03 22:41:51 +0000798"hasattr(object, name) -> bool\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000799\n\
800Return whether the object has an attribute with the given name.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000801(This is done by calling getattr(object, name) and catching exceptions.)");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000802
803
Guido van Rossum79f25d91997-04-29 20:08:16 +0000804static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000805builtin_id(PyObject *self, PyObject *v)
Guido van Rossum5b722181993-03-30 17:46:03 +0000806{
Guido van Rossum106f2da2000-06-28 21:12:25 +0000807 return PyLong_FromVoidPtr(v);
Guido van Rossum5b722181993-03-30 17:46:03 +0000808}
809
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000810PyDoc_STRVAR(id_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000811"id(object) -> integer\n\
812\n\
813Return the identity of an object. This is guaranteed to be unique among\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000814simultaneously existing objects. (Hint: it's the object's memory address.)");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000815
816
Guido van Rossum79f25d91997-04-29 20:08:16 +0000817static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000818builtin_map(PyObject *self, PyObject *args)
Guido van Rossum12d12c51993-10-26 17:58:25 +0000819{
820 typedef struct {
Tim Peters4e9afdc2001-05-03 23:54:49 +0000821 PyObject *it; /* the iterator object */
822 int saw_StopIteration; /* bool: did the iterator end? */
Guido van Rossum12d12c51993-10-26 17:58:25 +0000823 } sequence;
824
Guido van Rossum79f25d91997-04-29 20:08:16 +0000825 PyObject *func, *result;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000826 sequence *seqs = NULL, *sqp;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000827 Py_ssize_t n, len;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000828 register int i, j;
829
Guido van Rossum79f25d91997-04-29 20:08:16 +0000830 n = PyTuple_Size(args);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000831 if (n < 2) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000832 PyErr_SetString(PyExc_TypeError,
833 "map() requires at least two args");
Guido van Rossum12d12c51993-10-26 17:58:25 +0000834 return NULL;
835 }
836
Guido van Rossum79f25d91997-04-29 20:08:16 +0000837 func = PyTuple_GetItem(args, 0);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000838 n--;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000839
Guido van Rossumfa4ac711998-07-10 17:37:30 +0000840 if (func == Py_None && n == 1) {
841 /* map(None, S) is the same as list(S). */
842 return PySequence_List(PyTuple_GetItem(args, 1));
843 }
844
Tim Peters4e9afdc2001-05-03 23:54:49 +0000845 /* Get space for sequence descriptors. Must NULL out the iterator
846 * pointers so that jumping to Fail_2 later doesn't see trash.
847 */
Guido van Rossum79f25d91997-04-29 20:08:16 +0000848 if ((seqs = PyMem_NEW(sequence, n)) == NULL) {
849 PyErr_NoMemory();
Tim Peters4e9afdc2001-05-03 23:54:49 +0000850 return NULL;
851 }
852 for (i = 0; i < n; ++i) {
853 seqs[i].it = (PyObject*)NULL;
854 seqs[i].saw_StopIteration = 0;
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000855 }
Guido van Rossum12d12c51993-10-26 17:58:25 +0000856
Tim Peters4e9afdc2001-05-03 23:54:49 +0000857 /* Do a first pass to obtain iterators for the arguments, and set len
858 * to the largest of their lengths.
Tim Peters748b8bb2001-04-28 08:20:22 +0000859 */
Tim Peters4e9afdc2001-05-03 23:54:49 +0000860 len = 0;
861 for (i = 0, sqp = seqs; i < n; ++i, ++sqp) {
862 PyObject *curseq;
Martin v. Löwisd96ee902006-02-16 14:37:16 +0000863 Py_ssize_t curlen;
Guido van Rossumad991772001-01-12 16:03:05 +0000864
Tim Peters4e9afdc2001-05-03 23:54:49 +0000865 /* Get iterator. */
866 curseq = PyTuple_GetItem(args, i+1);
867 sqp->it = PyObject_GetIter(curseq);
868 if (sqp->it == NULL) {
Guido van Rossum12d12c51993-10-26 17:58:25 +0000869 static char errmsg[] =
Tim Peters4e9afdc2001-05-03 23:54:49 +0000870 "argument %d to map() must support iteration";
Guido van Rossum15e33a41997-04-30 19:00:27 +0000871 char errbuf[sizeof(errmsg) + 25];
Jeremy Hylton518ab1c2001-11-28 20:42:20 +0000872 PyOS_snprintf(errbuf, sizeof(errbuf), errmsg, i+2);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000873 PyErr_SetString(PyExc_TypeError, errbuf);
Guido van Rossum12d12c51993-10-26 17:58:25 +0000874 goto Fail_2;
875 }
876
Tim Peters4e9afdc2001-05-03 23:54:49 +0000877 /* Update len. */
Armin Rigof5b3e362006-02-11 21:32:43 +0000878 curlen = _PyObject_LengthHint(curseq);
Raymond Hettinger77f3c872004-01-04 08:54:44 +0000879 if (curlen < 0) {
Raymond Hettingera710b332005-08-21 11:03:59 +0000880 if (!PyErr_ExceptionMatches(PyExc_TypeError) &&
881 !PyErr_ExceptionMatches(PyExc_AttributeError)) {
882 goto Fail_2;
883 }
Raymond Hettinger77f3c872004-01-04 08:54:44 +0000884 PyErr_Clear();
Tim Peters4e9afdc2001-05-03 23:54:49 +0000885 curlen = 8; /* arbitrary */
Raymond Hettinger77f3c872004-01-04 08:54:44 +0000886 }
887 if (curlen > len)
888 len = curlen;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000889 }
890
Tim Peters4e9afdc2001-05-03 23:54:49 +0000891 /* Get space for the result list. */
Guido van Rossum79f25d91997-04-29 20:08:16 +0000892 if ((result = (PyObject *) PyList_New(len)) == NULL)
Guido van Rossum12d12c51993-10-26 17:58:25 +0000893 goto Fail_2;
894
Tim Peters4e9afdc2001-05-03 23:54:49 +0000895 /* Iterate over the sequences until all have stopped. */
Guido van Rossum2d951851994-08-29 12:52:16 +0000896 for (i = 0; ; ++i) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000897 PyObject *alist, *item=NULL, *value;
Tim Peters4e9afdc2001-05-03 23:54:49 +0000898 int numactive = 0;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000899
Guido van Rossum79f25d91997-04-29 20:08:16 +0000900 if (func == Py_None && n == 1)
Guido van Rossum32120311995-07-10 13:52:21 +0000901 alist = NULL;
Tim Peters4e9afdc2001-05-03 23:54:49 +0000902 else if ((alist = PyTuple_New(n)) == NULL)
903 goto Fail_1;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000904
905 for (j = 0, sqp = seqs; j < n; ++j, ++sqp) {
Tim Peters4e9afdc2001-05-03 23:54:49 +0000906 if (sqp->saw_StopIteration) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000907 Py_INCREF(Py_None);
908 item = Py_None;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000909 }
Guido van Rossum12d12c51993-10-26 17:58:25 +0000910 else {
Tim Peters4e9afdc2001-05-03 23:54:49 +0000911 item = PyIter_Next(sqp->it);
912 if (item)
913 ++numactive;
914 else {
Tim Peters4e9afdc2001-05-03 23:54:49 +0000915 if (PyErr_Occurred()) {
Tim Petersf4848da2001-05-05 00:14:56 +0000916 Py_XDECREF(alist);
917 goto Fail_1;
Guido van Rossum2d951851994-08-29 12:52:16 +0000918 }
Tim Peters4e9afdc2001-05-03 23:54:49 +0000919 Py_INCREF(Py_None);
920 item = Py_None;
921 sqp->saw_StopIteration = 1;
Guido van Rossum2d951851994-08-29 12:52:16 +0000922 }
Guido van Rossum12d12c51993-10-26 17:58:25 +0000923 }
Tim Peters4e9afdc2001-05-03 23:54:49 +0000924 if (alist)
925 PyTuple_SET_ITEM(alist, j, item);
926 else
Guido van Rossum2d951851994-08-29 12:52:16 +0000927 break;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000928 }
929
Guido van Rossum32120311995-07-10 13:52:21 +0000930 if (!alist)
931 alist = item;
Guido van Rossum2d951851994-08-29 12:52:16 +0000932
Tim Peters4e9afdc2001-05-03 23:54:49 +0000933 if (numactive == 0) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000934 Py_DECREF(alist);
Guido van Rossum2d951851994-08-29 12:52:16 +0000935 break;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000936 }
Guido van Rossum2d951851994-08-29 12:52:16 +0000937
Guido van Rossum79f25d91997-04-29 20:08:16 +0000938 if (func == Py_None)
Guido van Rossum32120311995-07-10 13:52:21 +0000939 value = alist;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000940 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000941 value = PyEval_CallObject(func, alist);
942 Py_DECREF(alist);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000943 if (value == NULL)
944 goto Fail_1;
Guido van Rossum2d951851994-08-29 12:52:16 +0000945 }
946 if (i >= len) {
Barry Warsawfa77e091999-01-28 18:49:12 +0000947 int status = PyList_Append(result, value);
Barry Warsaw21332871999-01-28 04:21:35 +0000948 Py_DECREF(value);
Barry Warsawfa77e091999-01-28 18:49:12 +0000949 if (status < 0)
950 goto Fail_1;
Guido van Rossum2d951851994-08-29 12:52:16 +0000951 }
Tim Peters4e9afdc2001-05-03 23:54:49 +0000952 else if (PyList_SetItem(result, i, value) < 0)
953 goto Fail_1;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000954 }
955
Guido van Rossumfa4ac711998-07-10 17:37:30 +0000956 if (i < len && PyList_SetSlice(result, i, len, NULL) < 0)
957 goto Fail_1;
958
Tim Peters4e9afdc2001-05-03 23:54:49 +0000959 goto Succeed;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000960
Guido van Rossum12d12c51993-10-26 17:58:25 +0000961Fail_1:
Guido van Rossum79f25d91997-04-29 20:08:16 +0000962 Py_DECREF(result);
Guido van Rossum12d12c51993-10-26 17:58:25 +0000963Fail_2:
Tim Peters4e9afdc2001-05-03 23:54:49 +0000964 result = NULL;
965Succeed:
966 assert(seqs);
967 for (i = 0; i < n; ++i)
968 Py_XDECREF(seqs[i].it);
969 PyMem_DEL(seqs);
970 return result;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000971}
972
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000973PyDoc_STRVAR(map_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000974"map(function, sequence[, sequence, ...]) -> list\n\
975\n\
976Return a list of the results of applying the function to the items of\n\
977the argument sequence(s). If more than one sequence is given, the\n\
978function is called with an argument list consisting of the corresponding\n\
979item of each sequence, substituting None for missing values when not all\n\
980sequences have the same length. If the function is None, return a list of\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000981the items of the sequence (or a list of tuples if more than one sequence).");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000982
983
Guido van Rossum79f25d91997-04-29 20:08:16 +0000984static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000985builtin_setattr(PyObject *self, PyObject *args)
Guido van Rossum33894be1992-01-27 16:53:09 +0000986{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000987 PyObject *v;
988 PyObject *name;
989 PyObject *value;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000990
Raymond Hettingerea3fdf42002-12-29 16:33:45 +0000991 if (!PyArg_UnpackTuple(args, "setattr", 3, 3, &v, &name, &value))
Guido van Rossum33894be1992-01-27 16:53:09 +0000992 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000993 if (PyObject_SetAttr(v, name, value) != 0)
Guido van Rossum33894be1992-01-27 16:53:09 +0000994 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000995 Py_INCREF(Py_None);
996 return Py_None;
Guido van Rossum33894be1992-01-27 16:53:09 +0000997}
998
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000999PyDoc_STRVAR(setattr_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001000"setattr(object, name, value)\n\
1001\n\
1002Set a named attribute on an object; setattr(x, 'y', v) is equivalent to\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001003``x.y = v''.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001004
1005
Guido van Rossum79f25d91997-04-29 20:08:16 +00001006static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001007builtin_delattr(PyObject *self, PyObject *args)
Guido van Rossum14144fc1994-08-29 12:53:40 +00001008{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001009 PyObject *v;
1010 PyObject *name;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001011
Raymond Hettingerea3fdf42002-12-29 16:33:45 +00001012 if (!PyArg_UnpackTuple(args, "delattr", 2, 2, &v, &name))
Guido van Rossum14144fc1994-08-29 12:53:40 +00001013 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001014 if (PyObject_SetAttr(v, name, (PyObject *)NULL) != 0)
Guido van Rossum14144fc1994-08-29 12:53:40 +00001015 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001016 Py_INCREF(Py_None);
1017 return Py_None;
Guido van Rossum14144fc1994-08-29 12:53:40 +00001018}
1019
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001020PyDoc_STRVAR(delattr_doc,
Guido van Rossumdf12a591998-11-23 22:13:04 +00001021"delattr(object, name)\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001022\n\
1023Delete a named attribute on an object; delattr(x, 'y') is equivalent to\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001024``del x.y''.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001025
1026
Guido van Rossum79f25d91997-04-29 20:08:16 +00001027static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001028builtin_hash(PyObject *self, PyObject *v)
Guido van Rossum9bfef441993-03-29 10:43:31 +00001029{
Guido van Rossum9bfef441993-03-29 10:43:31 +00001030 long x;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001031
Guido van Rossum79f25d91997-04-29 20:08:16 +00001032 x = PyObject_Hash(v);
Guido van Rossum9bfef441993-03-29 10:43:31 +00001033 if (x == -1)
1034 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001035 return PyInt_FromLong(x);
Guido van Rossum9bfef441993-03-29 10:43:31 +00001036}
1037
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001038PyDoc_STRVAR(hash_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001039"hash(object) -> integer\n\
1040\n\
1041Return a hash value for the object. Two objects with the same value have\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001042the same hash value. The reverse is not necessarily true, but likely.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001043
1044
Guido van Rossum79f25d91997-04-29 20:08:16 +00001045static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001046builtin_hex(PyObject *self, PyObject *v)
Guido van Rossum006bcd41991-10-24 14:54:44 +00001047{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001048 PyNumberMethods *nb;
Neil Schemenauer3a313e32004-07-19 16:29:17 +00001049 PyObject *res;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001050
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001051 if ((nb = v->ob_type->tp_as_number) == NULL ||
Guido van Rossum1899c2e1992-09-12 11:09:23 +00001052 nb->nb_hex == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001053 PyErr_SetString(PyExc_TypeError,
Guido van Rossum1899c2e1992-09-12 11:09:23 +00001054 "hex() argument can't be converted to hex");
1055 return NULL;
Guido van Rossum006bcd41991-10-24 14:54:44 +00001056 }
Neil Schemenauer3a313e32004-07-19 16:29:17 +00001057 res = (*nb->nb_hex)(v);
1058 if (res && !PyString_Check(res)) {
1059 PyErr_Format(PyExc_TypeError,
1060 "__hex__ returned non-string (type %.200s)",
1061 res->ob_type->tp_name);
1062 Py_DECREF(res);
1063 return NULL;
1064 }
1065 return res;
Guido van Rossum006bcd41991-10-24 14:54:44 +00001066}
1067
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001068PyDoc_STRVAR(hex_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001069"hex(number) -> string\n\
1070\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001071Return the hexadecimal representation of an integer or long integer.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001072
1073
Tim Petersdbd9ba62000-07-09 03:09:57 +00001074static PyObject *builtin_raw_input(PyObject *, PyObject *);
Guido van Rossum3165fe61992-09-25 21:59:05 +00001075
Guido van Rossum79f25d91997-04-29 20:08:16 +00001076static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001077builtin_input(PyObject *self, PyObject *args)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001078{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001079 PyObject *line;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001080 char *str;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001081 PyObject *res;
1082 PyObject *globals, *locals;
Hye-Shik Changff83c2b2004-02-02 13:39:01 +00001083 PyCompilerFlags cf;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001084
1085 line = builtin_raw_input(self, args);
Guido van Rossum3165fe61992-09-25 21:59:05 +00001086 if (line == NULL)
1087 return line;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001088 if (!PyArg_Parse(line, "s;embedded '\\0' in input line", &str))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001089 return NULL;
1090 while (*str == ' ' || *str == '\t')
1091 str++;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001092 globals = PyEval_GetGlobals();
1093 locals = PyEval_GetLocals();
1094 if (PyDict_GetItemString(globals, "__builtins__") == NULL) {
1095 if (PyDict_SetItemString(globals, "__builtins__",
1096 PyEval_GetBuiltins()) != 0)
Guido van Rossum6135a871995-01-09 17:53:26 +00001097 return NULL;
1098 }
Hye-Shik Changff83c2b2004-02-02 13:39:01 +00001099 cf.cf_flags = 0;
1100 PyEval_MergeCompilerFlags(&cf);
1101 res = PyRun_StringFlags(str, Py_eval_input, globals, locals, &cf);
Guido van Rossum79f25d91997-04-29 20:08:16 +00001102 Py_DECREF(line);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001103 return res;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001104}
1105
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001106PyDoc_STRVAR(input_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001107"input([prompt]) -> value\n\
1108\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001109Equivalent to eval(raw_input(prompt)).");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001110
1111
Guido van Rossume8811f81997-02-14 15:48:05 +00001112static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001113builtin_intern(PyObject *self, PyObject *args)
Guido van Rossume8811f81997-02-14 15:48:05 +00001114{
1115 PyObject *s;
Guido van Rossum43713e52000-02-29 13:59:29 +00001116 if (!PyArg_ParseTuple(args, "S:intern", &s))
Guido van Rossume8811f81997-02-14 15:48:05 +00001117 return NULL;
Jeremy Hylton4c989dd2004-08-07 19:20:05 +00001118 if (!PyString_CheckExact(s)) {
1119 PyErr_SetString(PyExc_TypeError,
1120 "can't intern subclass of string");
1121 return NULL;
1122 }
Guido van Rossume8811f81997-02-14 15:48:05 +00001123 Py_INCREF(s);
1124 PyString_InternInPlace(&s);
1125 return s;
1126}
1127
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001128PyDoc_STRVAR(intern_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001129"intern(string) -> string\n\
1130\n\
1131``Intern'' the given string. This enters the string in the (global)\n\
1132table of interned strings whose purpose is to speed up dictionary lookups.\n\
1133Return the string itself or the previously interned string object with the\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001134same value.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001135
1136
Guido van Rossum79f25d91997-04-29 20:08:16 +00001137static PyObject *
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001138builtin_iter(PyObject *self, PyObject *args)
1139{
1140 PyObject *v, *w = NULL;
1141
Raymond Hettingerea3fdf42002-12-29 16:33:45 +00001142 if (!PyArg_UnpackTuple(args, "iter", 1, 2, &v, &w))
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001143 return NULL;
1144 if (w == NULL)
1145 return PyObject_GetIter(v);
1146 if (!PyCallable_Check(v)) {
1147 PyErr_SetString(PyExc_TypeError,
1148 "iter(v, w): v must be callable");
1149 return NULL;
1150 }
1151 return PyCallIter_New(v, w);
1152}
1153
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001154PyDoc_STRVAR(iter_doc,
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001155"iter(collection) -> iterator\n\
1156iter(callable, sentinel) -> iterator\n\
1157\n\
1158Get an iterator from an object. In the first form, the argument must\n\
1159supply its own iterator, or be a sequence.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001160In the second form, the callable is called until it returns the sentinel.");
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001161
1162
1163static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001164builtin_len(PyObject *self, PyObject *v)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001165{
Martin v. Löwis18e16552006-02-15 17:27:45 +00001166 Py_ssize_t res;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001167
Jeremy Hylton03657cf2000-07-12 13:05:33 +00001168 res = PyObject_Size(v);
Guido van Rossum8ea9f4d1998-06-29 22:26:50 +00001169 if (res < 0 && PyErr_Occurred())
1170 return NULL;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001171 return PyInt_FromSsize_t(res);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001172}
1173
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001174PyDoc_STRVAR(len_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001175"len(object) -> integer\n\
1176\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001177Return the number of items of a sequence or mapping.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001178
1179
Guido van Rossum79f25d91997-04-29 20:08:16 +00001180static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001181builtin_locals(PyObject *self)
Guido van Rossum872537c1995-07-07 22:43:42 +00001182{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001183 PyObject *d;
Guido van Rossum872537c1995-07-07 22:43:42 +00001184
Guido van Rossum79f25d91997-04-29 20:08:16 +00001185 d = PyEval_GetLocals();
1186 Py_INCREF(d);
Guido van Rossum872537c1995-07-07 22:43:42 +00001187 return d;
1188}
1189
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001190PyDoc_STRVAR(locals_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001191"locals() -> dictionary\n\
1192\n\
Raymond Hettinger69bf8f32003-01-04 02:16:22 +00001193Update and return a dictionary containing the current scope's local variables.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001194
1195
Guido van Rossum79f25d91997-04-29 20:08:16 +00001196static PyObject *
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001197min_max(PyObject *args, PyObject *kwds, int op)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001198{
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001199 PyObject *v, *it, *item, *val, *maxitem, *maxval, *keyfunc=NULL;
Martin v. Löwisfd39ad42004-08-12 14:42:37 +00001200 const char *name = op == Py_LT ? "min" : "max";
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001201
Guido van Rossum79f25d91997-04-29 20:08:16 +00001202 if (PyTuple_Size(args) > 1)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001203 v = args;
Martin v. Löwisfd39ad42004-08-12 14:42:37 +00001204 else if (!PyArg_UnpackTuple(args, (char *)name, 1, 1, &v))
Guido van Rossum3f5da241990-12-20 15:06:42 +00001205 return NULL;
Tim Peters67d687a2002-04-29 21:27:32 +00001206
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001207 if (kwds != NULL && PyDict_Check(kwds) && PyDict_Size(kwds)) {
1208 keyfunc = PyDict_GetItemString(kwds, "key");
1209 if (PyDict_Size(kwds)!=1 || keyfunc == NULL) {
Georg Brandl99d7e4e2005-08-31 22:21:15 +00001210 PyErr_Format(PyExc_TypeError,
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001211 "%s() got an unexpected keyword argument", name);
1212 return NULL;
1213 }
Georg Brandl99d7e4e2005-08-31 22:21:15 +00001214 }
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001215
Tim Petersc3074532001-05-03 07:00:32 +00001216 it = PyObject_GetIter(v);
1217 if (it == NULL)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001218 return NULL;
Tim Petersc3074532001-05-03 07:00:32 +00001219
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001220 maxitem = NULL; /* the result */
1221 maxval = NULL; /* the value associated with the result */
Brett Cannon9e635cf2004-12-07 00:25:35 +00001222 while (( item = PyIter_Next(it) )) {
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001223 /* get the value from the key function */
1224 if (keyfunc != NULL) {
1225 val = PyObject_CallFunctionObjArgs(keyfunc, item, NULL);
1226 if (val == NULL)
1227 goto Fail_it_item;
1228 }
1229 /* no key function; the value is the item */
1230 else {
1231 val = item;
1232 Py_INCREF(val);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001233 }
Tim Petersc3074532001-05-03 07:00:32 +00001234
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001235 /* maximum value and item are unset; set them */
1236 if (maxval == NULL) {
1237 maxitem = item;
1238 maxval = val;
1239 }
1240 /* maximum value and item are set; update them as necessary */
Guido van Rossum2d951851994-08-29 12:52:16 +00001241 else {
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001242 int cmp = PyObject_RichCompareBool(val, maxval, op);
1243 if (cmp < 0)
1244 goto Fail_it_item_and_val;
1245 else if (cmp > 0) {
1246 Py_DECREF(maxval);
1247 Py_DECREF(maxitem);
1248 maxval = val;
1249 maxitem = item;
Guido van Rossum53451b32001-01-17 15:47:24 +00001250 }
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001251 else {
1252 Py_DECREF(item);
1253 Py_DECREF(val);
Guido van Rossumc8b6df91997-05-23 00:06:51 +00001254 }
Guido van Rossum2d951851994-08-29 12:52:16 +00001255 }
Guido van Rossum3f5da241990-12-20 15:06:42 +00001256 }
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001257 if (PyErr_Occurred())
1258 goto Fail_it;
1259 if (maxval == NULL) {
Martin v. Löwisfd39ad42004-08-12 14:42:37 +00001260 PyErr_Format(PyExc_ValueError,
1261 "%s() arg is an empty sequence", name);
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001262 assert(maxitem == NULL);
1263 }
1264 else
1265 Py_DECREF(maxval);
Tim Petersc3074532001-05-03 07:00:32 +00001266 Py_DECREF(it);
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001267 return maxitem;
1268
1269Fail_it_item_and_val:
1270 Py_DECREF(val);
1271Fail_it_item:
1272 Py_DECREF(item);
1273Fail_it:
1274 Py_XDECREF(maxval);
1275 Py_XDECREF(maxitem);
1276 Py_DECREF(it);
1277 return NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001278}
1279
Guido van Rossum79f25d91997-04-29 20:08:16 +00001280static PyObject *
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001281builtin_min(PyObject *self, PyObject *args, PyObject *kwds)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001282{
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001283 return min_max(args, kwds, Py_LT);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001284}
1285
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001286PyDoc_STRVAR(min_doc,
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001287"min(iterable[, key=func]) -> value\n\
1288min(a, b, c, ...[, key=func]) -> value\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001289\n\
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001290With a single iterable argument, return its smallest item.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001291With two or more arguments, return the smallest argument.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001292
1293
Guido van Rossum79f25d91997-04-29 20:08:16 +00001294static PyObject *
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001295builtin_max(PyObject *self, PyObject *args, PyObject *kwds)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001296{
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001297 return min_max(args, kwds, Py_GT);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001298}
1299
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001300PyDoc_STRVAR(max_doc,
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001301"max(iterable[, key=func]) -> value\n\
1302max(a, b, c, ...[, key=func]) -> value\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001303\n\
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001304With a single iterable argument, return its largest item.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001305With two or more arguments, return the largest argument.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001306
1307
Guido van Rossum79f25d91997-04-29 20:08:16 +00001308static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001309builtin_oct(PyObject *self, PyObject *v)
Guido van Rossum006bcd41991-10-24 14:54:44 +00001310{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001311 PyNumberMethods *nb;
Neil Schemenauer3a313e32004-07-19 16:29:17 +00001312 PyObject *res;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001313
Guido van Rossum1899c2e1992-09-12 11:09:23 +00001314 if (v == NULL || (nb = v->ob_type->tp_as_number) == NULL ||
1315 nb->nb_oct == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001316 PyErr_SetString(PyExc_TypeError,
Guido van Rossum1899c2e1992-09-12 11:09:23 +00001317 "oct() argument can't be converted to oct");
1318 return NULL;
Guido van Rossum006bcd41991-10-24 14:54:44 +00001319 }
Neil Schemenauer3a313e32004-07-19 16:29:17 +00001320 res = (*nb->nb_oct)(v);
1321 if (res && !PyString_Check(res)) {
1322 PyErr_Format(PyExc_TypeError,
1323 "__oct__ returned non-string (type %.200s)",
1324 res->ob_type->tp_name);
1325 Py_DECREF(res);
1326 return NULL;
1327 }
1328 return res;
Guido van Rossum006bcd41991-10-24 14:54:44 +00001329}
1330
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001331PyDoc_STRVAR(oct_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001332"oct(number) -> string\n\
1333\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001334Return the octal representation of an integer or long integer.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001335
1336
Guido van Rossum79f25d91997-04-29 20:08:16 +00001337static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001338builtin_ord(PyObject *self, PyObject* obj)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001339{
Guido van Rossum09095f32000-03-10 23:00:52 +00001340 long ord;
Martin v. Löwisd96ee902006-02-16 14:37:16 +00001341 Py_ssize_t size;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001342
Jeremy Hylton394b54d2000-04-12 21:19:47 +00001343 if (PyString_Check(obj)) {
1344 size = PyString_GET_SIZE(obj);
Andrew M. Kuchling34c20cf2000-12-20 14:36:56 +00001345 if (size == 1) {
Jeremy Hylton394b54d2000-04-12 21:19:47 +00001346 ord = (long)((unsigned char)*PyString_AS_STRING(obj));
Andrew M. Kuchling34c20cf2000-12-20 14:36:56 +00001347 return PyInt_FromLong(ord);
1348 }
Martin v. Löwis339d0f72001-08-17 18:39:25 +00001349#ifdef Py_USING_UNICODE
Jeremy Hylton394b54d2000-04-12 21:19:47 +00001350 } else if (PyUnicode_Check(obj)) {
1351 size = PyUnicode_GET_SIZE(obj);
Andrew M. Kuchling34c20cf2000-12-20 14:36:56 +00001352 if (size == 1) {
Jeremy Hylton394b54d2000-04-12 21:19:47 +00001353 ord = (long)*PyUnicode_AS_UNICODE(obj);
Andrew M. Kuchling34c20cf2000-12-20 14:36:56 +00001354 return PyInt_FromLong(ord);
1355 }
Martin v. Löwis339d0f72001-08-17 18:39:25 +00001356#endif
Jeremy Hylton394b54d2000-04-12 21:19:47 +00001357 } else {
1358 PyErr_Format(PyExc_TypeError,
Andrew M. Kuchlingf07aad12000-12-23 14:11:28 +00001359 "ord() expected string of length 1, but " \
Jeremy Hylton394b54d2000-04-12 21:19:47 +00001360 "%.200s found", obj->ob_type->tp_name);
Guido van Rossum09095f32000-03-10 23:00:52 +00001361 return NULL;
1362 }
1363
Guido van Rossumad991772001-01-12 16:03:05 +00001364 PyErr_Format(PyExc_TypeError,
Andrew M. Kuchlingf07aad12000-12-23 14:11:28 +00001365 "ord() expected a character, "
Martin v. Löwisd96ee902006-02-16 14:37:16 +00001366 "but string of length %zd found",
Jeremy Hylton394b54d2000-04-12 21:19:47 +00001367 size);
1368 return NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001369}
1370
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001371PyDoc_STRVAR(ord_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001372"ord(c) -> integer\n\
1373\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001374Return the integer ordinal of a one-character string.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001375
1376
Guido van Rossum79f25d91997-04-29 20:08:16 +00001377static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001378builtin_pow(PyObject *self, PyObject *args)
Guido van Rossum6a00cd81995-01-07 12:39:01 +00001379{
Guido van Rossum09df08a1998-05-22 00:51:39 +00001380 PyObject *v, *w, *z = Py_None;
Guido van Rossum6a00cd81995-01-07 12:39:01 +00001381
Raymond Hettingerea3fdf42002-12-29 16:33:45 +00001382 if (!PyArg_UnpackTuple(args, "pow", 2, 3, &v, &w, &z))
Guido van Rossum6a00cd81995-01-07 12:39:01 +00001383 return NULL;
Guido van Rossum09df08a1998-05-22 00:51:39 +00001384 return PyNumber_Power(v, w, z);
Guido van Rossumd4905451991-05-05 20:00:36 +00001385}
1386
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001387PyDoc_STRVAR(pow_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001388"pow(x, y[, z]) -> number\n\
1389\n\
1390With two arguments, equivalent to x**y. With three arguments,\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001391equivalent to (x**y) % z, but may be more efficient (e.g. for longs).");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001392
1393
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001394
1395/* Return number of items in range (lo, hi, step), when arguments are
1396 * PyInt or PyLong objects. step > 0 required. Return a value < 0 if
1397 * & only if the true value is too large to fit in a signed long.
1398 * Arguments MUST return 1 with either PyInt_Check() or
1399 * PyLong_Check(). Return -1 when there is an error.
1400 */
1401static long
1402get_len_of_range_longs(PyObject *lo, PyObject *hi, PyObject *step)
1403{
1404 /* -------------------------------------------------------------
1405 Algorithm is equal to that of get_len_of_range(), but it operates
1406 on PyObjects (which are assumed to be PyLong or PyInt objects).
1407 ---------------------------------------------------------------*/
1408 long n;
1409 PyObject *diff = NULL;
1410 PyObject *one = NULL;
1411 PyObject *tmp1 = NULL, *tmp2 = NULL, *tmp3 = NULL;
1412 /* holds sub-expression evaluations */
1413
1414 /* if (lo >= hi), return length of 0. */
1415 if (PyObject_Compare(lo, hi) >= 0)
1416 return 0;
1417
1418 if ((one = PyLong_FromLong(1L)) == NULL)
1419 goto Fail;
1420
1421 if ((tmp1 = PyNumber_Subtract(hi, lo)) == NULL)
1422 goto Fail;
1423
1424 if ((diff = PyNumber_Subtract(tmp1, one)) == NULL)
1425 goto Fail;
1426
1427 if ((tmp2 = PyNumber_FloorDivide(diff, step)) == NULL)
1428 goto Fail;
1429
1430 if ((tmp3 = PyNumber_Add(tmp2, one)) == NULL)
1431 goto Fail;
1432
1433 n = PyLong_AsLong(tmp3);
1434 if (PyErr_Occurred()) { /* Check for Overflow */
1435 PyErr_Clear();
1436 goto Fail;
1437 }
1438
1439 Py_DECREF(tmp3);
1440 Py_DECREF(tmp2);
1441 Py_DECREF(diff);
1442 Py_DECREF(tmp1);
1443 Py_DECREF(one);
1444 return n;
1445
1446 Fail:
1447 Py_XDECREF(tmp3);
1448 Py_XDECREF(tmp2);
1449 Py_XDECREF(diff);
1450 Py_XDECREF(tmp1);
1451 Py_XDECREF(one);
1452 return -1;
1453}
1454
1455/* An extension of builtin_range() that handles the case when PyLong
1456 * arguments are given. */
1457static PyObject *
1458handle_range_longs(PyObject *self, PyObject *args)
1459{
1460 PyObject *ilow;
Tim Peters874e1f72003-04-13 22:13:08 +00001461 PyObject *ihigh = NULL;
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001462 PyObject *istep = NULL;
Tim Peters874e1f72003-04-13 22:13:08 +00001463
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001464 PyObject *curnum = NULL;
1465 PyObject *v = NULL;
1466 long bign;
1467 int i, n;
1468 int cmp_result;
1469
Tim Peters874e1f72003-04-13 22:13:08 +00001470 PyObject *zero = PyLong_FromLong(0);
1471
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001472 if (zero == NULL)
1473 return NULL;
1474
Tim Peters874e1f72003-04-13 22:13:08 +00001475 if (!PyArg_UnpackTuple(args, "range", 1, 3, &ilow, &ihigh, &istep)) {
1476 Py_DECREF(zero);
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001477 return NULL;
1478 }
1479
Tim Peters874e1f72003-04-13 22:13:08 +00001480 /* Figure out which way we were called, supply defaults, and be
1481 * sure to incref everything so that the decrefs at the end
1482 * are correct.
1483 */
1484 assert(ilow != NULL);
1485 if (ihigh == NULL) {
1486 /* only 1 arg -- it's the upper limit */
1487 ihigh = ilow;
1488 ilow = NULL;
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001489 }
Tim Peters874e1f72003-04-13 22:13:08 +00001490 assert(ihigh != NULL);
1491 Py_INCREF(ihigh);
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001492
Tim Peters874e1f72003-04-13 22:13:08 +00001493 /* ihigh correct now; do ilow */
1494 if (ilow == NULL)
1495 ilow = zero;
1496 Py_INCREF(ilow);
1497
1498 /* ilow and ihigh correct now; do istep */
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001499 if (istep == NULL) {
1500 istep = PyLong_FromLong(1L);
1501 if (istep == NULL)
1502 goto Fail;
1503 }
1504 else {
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001505 Py_INCREF(istep);
1506 }
1507
Tim Peters874e1f72003-04-13 22:13:08 +00001508 if (!PyInt_Check(ilow) && !PyLong_Check(ilow)) {
Guido van Rossum28e83e32003-04-15 12:43:26 +00001509 PyErr_Format(PyExc_TypeError,
Alex Martellif471d472003-04-23 13:00:44 +00001510 "range() integer start argument expected, got %s.",
Guido van Rossum817d6c92003-04-14 18:25:04 +00001511 ilow->ob_type->tp_name);
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001512 goto Fail;
1513 }
1514
Tim Peters874e1f72003-04-13 22:13:08 +00001515 if (!PyInt_Check(ihigh) && !PyLong_Check(ihigh)) {
Guido van Rossum28e83e32003-04-15 12:43:26 +00001516 PyErr_Format(PyExc_TypeError,
Alex Martellif471d472003-04-23 13:00:44 +00001517 "range() integer end argument expected, got %s.",
Guido van Rossum817d6c92003-04-14 18:25:04 +00001518 ihigh->ob_type->tp_name);
Tim Peters874e1f72003-04-13 22:13:08 +00001519 goto Fail;
1520 }
1521
1522 if (!PyInt_Check(istep) && !PyLong_Check(istep)) {
Guido van Rossum28e83e32003-04-15 12:43:26 +00001523 PyErr_Format(PyExc_TypeError,
Alex Martellif471d472003-04-23 13:00:44 +00001524 "range() integer step argument expected, got %s.",
Guido van Rossum817d6c92003-04-14 18:25:04 +00001525 istep->ob_type->tp_name);
Tim Peters874e1f72003-04-13 22:13:08 +00001526 goto Fail;
1527 }
1528
1529 if (PyObject_Cmp(istep, zero, &cmp_result) == -1)
1530 goto Fail;
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001531 if (cmp_result == 0) {
1532 PyErr_SetString(PyExc_ValueError,
Alex Martellif471d472003-04-23 13:00:44 +00001533 "range() step argument must not be zero");
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001534 goto Fail;
1535 }
1536
1537 if (cmp_result > 0)
1538 bign = get_len_of_range_longs(ilow, ihigh, istep);
1539 else {
1540 PyObject *neg_istep = PyNumber_Negative(istep);
1541 if (neg_istep == NULL)
1542 goto Fail;
1543 bign = get_len_of_range_longs(ihigh, ilow, neg_istep);
1544 Py_DECREF(neg_istep);
1545 }
1546
1547 n = (int)bign;
1548 if (bign < 0 || (long)n != bign) {
1549 PyErr_SetString(PyExc_OverflowError,
1550 "range() result has too many items");
1551 goto Fail;
1552 }
1553
1554 v = PyList_New(n);
1555 if (v == NULL)
1556 goto Fail;
1557
1558 curnum = ilow;
1559 Py_INCREF(curnum);
1560
1561 for (i = 0; i < n; i++) {
1562 PyObject *w = PyNumber_Long(curnum);
1563 PyObject *tmp_num;
1564 if (w == NULL)
1565 goto Fail;
1566
1567 PyList_SET_ITEM(v, i, w);
1568
1569 tmp_num = PyNumber_Add(curnum, istep);
1570 if (tmp_num == NULL)
1571 goto Fail;
1572
1573 Py_DECREF(curnum);
1574 curnum = tmp_num;
1575 }
Tim Peters874e1f72003-04-13 22:13:08 +00001576 Py_DECREF(ilow);
1577 Py_DECREF(ihigh);
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001578 Py_DECREF(istep);
1579 Py_DECREF(zero);
Tim Peters874e1f72003-04-13 22:13:08 +00001580 Py_DECREF(curnum);
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001581 return v;
1582
1583 Fail:
Tim Peters874e1f72003-04-13 22:13:08 +00001584 Py_DECREF(ilow);
1585 Py_DECREF(ihigh);
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001586 Py_XDECREF(istep);
Tim Peters874e1f72003-04-13 22:13:08 +00001587 Py_DECREF(zero);
1588 Py_XDECREF(curnum);
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001589 Py_XDECREF(v);
1590 return NULL;
1591}
1592
Guido van Rossum124eff01999-02-23 16:11:01 +00001593/* Return number of items in range/xrange (lo, hi, step). step > 0
1594 * required. Return a value < 0 if & only if the true value is too
1595 * large to fit in a signed long.
1596 */
1597static long
Thomas Wouterse28c2962000-07-23 22:21:32 +00001598get_len_of_range(long lo, long hi, long step)
Guido van Rossum124eff01999-02-23 16:11:01 +00001599{
1600 /* -------------------------------------------------------------
1601 If lo >= hi, the range is empty.
1602 Else if n values are in the range, the last one is
1603 lo + (n-1)*step, which must be <= hi-1. Rearranging,
1604 n <= (hi - lo - 1)/step + 1, so taking the floor of the RHS gives
1605 the proper value. Since lo < hi in this case, hi-lo-1 >= 0, so
1606 the RHS is non-negative and so truncation is the same as the
1607 floor. Letting M be the largest positive long, the worst case
1608 for the RHS numerator is hi=M, lo=-M-1, and then
1609 hi-lo-1 = M-(-M-1)-1 = 2*M. Therefore unsigned long has enough
1610 precision to compute the RHS exactly.
1611 ---------------------------------------------------------------*/
1612 long n = 0;
1613 if (lo < hi) {
1614 unsigned long uhi = (unsigned long)hi;
1615 unsigned long ulo = (unsigned long)lo;
1616 unsigned long diff = uhi - ulo - 1;
1617 n = (long)(diff / (unsigned long)step + 1);
1618 }
1619 return n;
1620}
1621
Guido van Rossum79f25d91997-04-29 20:08:16 +00001622static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001623builtin_range(PyObject *self, PyObject *args)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001624{
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001625 long ilow = 0, ihigh = 0, istep = 1;
Guido van Rossum124eff01999-02-23 16:11:01 +00001626 long bign;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001627 int i, n;
Guido van Rossum124eff01999-02-23 16:11:01 +00001628
Guido van Rossum79f25d91997-04-29 20:08:16 +00001629 PyObject *v;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001630
Guido van Rossum79f25d91997-04-29 20:08:16 +00001631 if (PyTuple_Size(args) <= 1) {
1632 if (!PyArg_ParseTuple(args,
Guido van Rossum0865dd91995-01-17 16:30:22 +00001633 "l;range() requires 1-3 int arguments",
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001634 &ihigh)) {
1635 PyErr_Clear();
1636 return handle_range_longs(self, args);
1637 }
Guido van Rossum3f5da241990-12-20 15:06:42 +00001638 }
1639 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001640 if (!PyArg_ParseTuple(args,
Guido van Rossum0865dd91995-01-17 16:30:22 +00001641 "ll|l;range() requires 1-3 int arguments",
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001642 &ilow, &ihigh, &istep)) {
1643 PyErr_Clear();
1644 return handle_range_longs(self, args);
1645 }
Guido van Rossum3f5da241990-12-20 15:06:42 +00001646 }
1647 if (istep == 0) {
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001648 PyErr_SetString(PyExc_ValueError,
Alex Martellif471d472003-04-23 13:00:44 +00001649 "range() step argument must not be zero");
Guido van Rossum3f5da241990-12-20 15:06:42 +00001650 return NULL;
1651 }
Guido van Rossum124eff01999-02-23 16:11:01 +00001652 if (istep > 0)
1653 bign = get_len_of_range(ilow, ihigh, istep);
1654 else
1655 bign = get_len_of_range(ihigh, ilow, -istep);
1656 n = (int)bign;
1657 if (bign < 0 || (long)n != bign) {
Guido van Rossume23cde21999-01-12 05:07:47 +00001658 PyErr_SetString(PyExc_OverflowError,
Fred Drake661ea262000-10-24 19:57:45 +00001659 "range() result has too many items");
Guido van Rossume23cde21999-01-12 05:07:47 +00001660 return NULL;
1661 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00001662 v = PyList_New(n);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001663 if (v == NULL)
1664 return NULL;
1665 for (i = 0; i < n; i++) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001666 PyObject *w = PyInt_FromLong(ilow);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001667 if (w == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001668 Py_DECREF(v);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001669 return NULL;
1670 }
Guido van Rossuma937d141998-04-24 18:22:02 +00001671 PyList_SET_ITEM(v, i, w);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001672 ilow += istep;
1673 }
1674 return v;
1675}
1676
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001677PyDoc_STRVAR(range_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001678"range([start,] stop[, step]) -> list of integers\n\
1679\n\
1680Return a list containing an arithmetic progression of integers.\n\
1681range(i, j) returns [i, i+1, i+2, ..., j-1]; start (!) defaults to 0.\n\
1682When step is given, it specifies the increment (or decrement).\n\
1683For example, range(4) returns [0, 1, 2, 3]. The end point is omitted!\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001684These are exactly the valid indices for a list of 4 elements.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001685
1686
Guido van Rossum79f25d91997-04-29 20:08:16 +00001687static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001688builtin_raw_input(PyObject *self, PyObject *args)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001689{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001690 PyObject *v = NULL;
Martin v. Löwis31d2df52002-08-14 15:46:02 +00001691 PyObject *fin = PySys_GetObject("stdin");
1692 PyObject *fout = PySys_GetObject("stdout");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001693
Raymond Hettingerea3fdf42002-12-29 16:33:45 +00001694 if (!PyArg_UnpackTuple(args, "[raw_]input", 0, 1, &v))
Guido van Rossum3165fe61992-09-25 21:59:05 +00001695 return NULL;
Martin v. Löwis31d2df52002-08-14 15:46:02 +00001696
1697 if (fin == NULL) {
Alex Martellia9b9c9f2003-04-23 13:34:35 +00001698 PyErr_SetString(PyExc_RuntimeError, "[raw_]input: lost sys.stdin");
Martin v. Löwis31d2df52002-08-14 15:46:02 +00001699 return NULL;
1700 }
1701 if (fout == NULL) {
Alex Martellia9b9c9f2003-04-23 13:34:35 +00001702 PyErr_SetString(PyExc_RuntimeError, "[raw_]input: lost sys.stdout");
Martin v. Löwis31d2df52002-08-14 15:46:02 +00001703 return NULL;
1704 }
1705 if (PyFile_SoftSpace(fout, 0)) {
1706 if (PyFile_WriteString(" ", fout) != 0)
1707 return NULL;
1708 }
Michael W. Hudson52db5192004-08-02 13:21:09 +00001709 if (PyFile_Check(fin) && PyFile_Check(fout)
Martin v. Löwis566f6af2002-10-26 14:39:10 +00001710 && isatty(fileno(PyFile_AsFile(fin)))
1711 && isatty(fileno(PyFile_AsFile(fout)))) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001712 PyObject *po;
Guido van Rossum872537c1995-07-07 22:43:42 +00001713 char *prompt;
1714 char *s;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001715 PyObject *result;
Guido van Rossum872537c1995-07-07 22:43:42 +00001716 if (v != NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001717 po = PyObject_Str(v);
Guido van Rossum872537c1995-07-07 22:43:42 +00001718 if (po == NULL)
1719 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001720 prompt = PyString_AsString(po);
Guido van Rossumd9b52081998-06-26 18:25:38 +00001721 if (prompt == NULL)
1722 return NULL;
Guido van Rossum872537c1995-07-07 22:43:42 +00001723 }
1724 else {
1725 po = NULL;
1726 prompt = "";
1727 }
Michael W. Hudson52db5192004-08-02 13:21:09 +00001728 s = PyOS_Readline(PyFile_AsFile(fin), PyFile_AsFile(fout),
Martin v. Löwis566f6af2002-10-26 14:39:10 +00001729 prompt);
Guido van Rossum79f25d91997-04-29 20:08:16 +00001730 Py_XDECREF(po);
Guido van Rossum872537c1995-07-07 22:43:42 +00001731 if (s == NULL) {
Michael W. Hudson30ea2f22004-07-07 17:44:12 +00001732 if (!PyErr_Occurred())
1733 PyErr_SetNone(PyExc_KeyboardInterrupt);
Guido van Rossum872537c1995-07-07 22:43:42 +00001734 return NULL;
1735 }
1736 if (*s == '\0') {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001737 PyErr_SetNone(PyExc_EOFError);
Guido van Rossum872537c1995-07-07 22:43:42 +00001738 result = NULL;
1739 }
1740 else { /* strip trailing '\n' */
Guido van Rossum106f2da2000-06-28 21:12:25 +00001741 size_t len = strlen(s);
1742 if (len > INT_MAX) {
Martin v. Löwis31d2df52002-08-14 15:46:02 +00001743 PyErr_SetString(PyExc_OverflowError,
Alex Martellia9b9c9f2003-04-23 13:34:35 +00001744 "[raw_]input: input too long");
Guido van Rossum106f2da2000-06-28 21:12:25 +00001745 result = NULL;
1746 }
1747 else {
Martin v. Löwis31d2df52002-08-14 15:46:02 +00001748 result = PyString_FromStringAndSize(s,
1749 (int)(len-1));
Guido van Rossum106f2da2000-06-28 21:12:25 +00001750 }
Guido van Rossum872537c1995-07-07 22:43:42 +00001751 }
Guido van Rossumb18618d2000-05-03 23:44:39 +00001752 PyMem_FREE(s);
Guido van Rossum872537c1995-07-07 22:43:42 +00001753 return result;
1754 }
Guido van Rossum90933611991-06-07 16:10:43 +00001755 if (v != NULL) {
Martin v. Löwis31d2df52002-08-14 15:46:02 +00001756 if (PyFile_WriteObject(v, fout, Py_PRINT_RAW) != 0)
Guido van Rossum90933611991-06-07 16:10:43 +00001757 return NULL;
1758 }
Martin v. Löwis31d2df52002-08-14 15:46:02 +00001759 return PyFile_GetLine(fin, -1);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001760}
1761
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001762PyDoc_STRVAR(raw_input_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001763"raw_input([prompt]) -> string\n\
1764\n\
1765Read a string from standard input. The trailing newline is stripped.\n\
1766If the user hits EOF (Unix: Ctl-D, Windows: Ctl-Z+Return), raise EOFError.\n\
1767On Unix, GNU readline is used if enabled. The prompt string, if given,\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001768is printed without a trailing newline before reading.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001769
1770
Guido van Rossum79f25d91997-04-29 20:08:16 +00001771static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001772builtin_reduce(PyObject *self, PyObject *args)
Guido van Rossum12d12c51993-10-26 17:58:25 +00001773{
Tim Peters15d81ef2001-05-04 04:39:21 +00001774 PyObject *seq, *func, *result = NULL, *it;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001775
Raymond Hettingerea3fdf42002-12-29 16:33:45 +00001776 if (!PyArg_UnpackTuple(args, "reduce", 2, 3, &func, &seq, &result))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001777 return NULL;
1778 if (result != NULL)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001779 Py_INCREF(result);
Guido van Rossum12d12c51993-10-26 17:58:25 +00001780
Tim Peters15d81ef2001-05-04 04:39:21 +00001781 it = PyObject_GetIter(seq);
1782 if (it == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001783 PyErr_SetString(PyExc_TypeError,
Tim Peters15d81ef2001-05-04 04:39:21 +00001784 "reduce() arg 2 must support iteration");
1785 Py_XDECREF(result);
Guido van Rossum12d12c51993-10-26 17:58:25 +00001786 return NULL;
1787 }
1788
Guido van Rossum79f25d91997-04-29 20:08:16 +00001789 if ((args = PyTuple_New(2)) == NULL)
Guido van Rossum2d951851994-08-29 12:52:16 +00001790 goto Fail;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001791
Tim Peters15d81ef2001-05-04 04:39:21 +00001792 for (;;) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001793 PyObject *op2;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001794
1795 if (args->ob_refcnt > 1) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001796 Py_DECREF(args);
1797 if ((args = PyTuple_New(2)) == NULL)
Guido van Rossum2d951851994-08-29 12:52:16 +00001798 goto Fail;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001799 }
1800
Tim Peters15d81ef2001-05-04 04:39:21 +00001801 op2 = PyIter_Next(it);
1802 if (op2 == NULL) {
Tim Petersf4848da2001-05-05 00:14:56 +00001803 if (PyErr_Occurred())
1804 goto Fail;
1805 break;
Guido van Rossum2d951851994-08-29 12:52:16 +00001806 }
Guido van Rossum12d12c51993-10-26 17:58:25 +00001807
Guido van Rossum2d951851994-08-29 12:52:16 +00001808 if (result == NULL)
1809 result = op2;
1810 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001811 PyTuple_SetItem(args, 0, result);
1812 PyTuple_SetItem(args, 1, op2);
1813 if ((result = PyEval_CallObject(func, args)) == NULL)
Guido van Rossum2d951851994-08-29 12:52:16 +00001814 goto Fail;
1815 }
Guido van Rossum12d12c51993-10-26 17:58:25 +00001816 }
1817
Guido van Rossum79f25d91997-04-29 20:08:16 +00001818 Py_DECREF(args);
Guido van Rossum12d12c51993-10-26 17:58:25 +00001819
Guido van Rossum2d951851994-08-29 12:52:16 +00001820 if (result == NULL)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001821 PyErr_SetString(PyExc_TypeError,
Fred Drake661ea262000-10-24 19:57:45 +00001822 "reduce() of empty sequence with no initial value");
Guido van Rossum2d951851994-08-29 12:52:16 +00001823
Tim Peters15d81ef2001-05-04 04:39:21 +00001824 Py_DECREF(it);
Guido van Rossum12d12c51993-10-26 17:58:25 +00001825 return result;
1826
Guido van Rossum2d951851994-08-29 12:52:16 +00001827Fail:
Guido van Rossum79f25d91997-04-29 20:08:16 +00001828 Py_XDECREF(args);
1829 Py_XDECREF(result);
Tim Peters15d81ef2001-05-04 04:39:21 +00001830 Py_DECREF(it);
Guido van Rossum12d12c51993-10-26 17:58:25 +00001831 return NULL;
1832}
1833
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001834PyDoc_STRVAR(reduce_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001835"reduce(function, sequence[, initial]) -> value\n\
1836\n\
1837Apply a function of two arguments cumulatively to the items of a sequence,\n\
1838from left to right, so as to reduce the sequence to a single value.\n\
1839For example, reduce(lambda x, y: x+y, [1, 2, 3, 4, 5]) calculates\n\
1840((((1+2)+3)+4)+5). If initial is present, it is placed before the items\n\
1841of the sequence in the calculation, and serves as a default when the\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001842sequence is empty.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001843
1844
Guido van Rossum79f25d91997-04-29 20:08:16 +00001845static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001846builtin_reload(PyObject *self, PyObject *v)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001847{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001848 return PyImport_ReloadModule(v);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001849}
1850
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001851PyDoc_STRVAR(reload_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001852"reload(module) -> module\n\
1853\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001854Reload the module. The module must have been successfully imported before.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001855
1856
Guido van Rossum79f25d91997-04-29 20:08:16 +00001857static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001858builtin_repr(PyObject *self, PyObject *v)
Guido van Rossumc89705d1992-11-26 08:54:07 +00001859{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001860 return PyObject_Repr(v);
Guido van Rossumc89705d1992-11-26 08:54:07 +00001861}
1862
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001863PyDoc_STRVAR(repr_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001864"repr(object) -> string\n\
1865\n\
1866Return the canonical string representation of the object.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001867For most object types, eval(repr(object)) == object.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001868
1869
Guido van Rossum79f25d91997-04-29 20:08:16 +00001870static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001871builtin_round(PyObject *self, PyObject *args)
Guido van Rossum9e51f9b1993-02-12 16:29:05 +00001872{
Guido van Rossum9e51f9b1993-02-12 16:29:05 +00001873 double x;
1874 double f;
1875 int ndigits = 0;
Guido van Rossum9e51f9b1993-02-12 16:29:05 +00001876 int i;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001877
Guido van Rossum79f25d91997-04-29 20:08:16 +00001878 if (!PyArg_ParseTuple(args, "d|i:round", &x, &ndigits))
Guido van Rossum9e51f9b1993-02-12 16:29:05 +00001879 return NULL;
Guido van Rossum9e51f9b1993-02-12 16:29:05 +00001880 f = 1.0;
Guido van Rossum1e162d31998-05-09 14:42:25 +00001881 i = abs(ndigits);
1882 while (--i >= 0)
Guido van Rossum9e51f9b1993-02-12 16:29:05 +00001883 f = f*10.0;
Guido van Rossum1e162d31998-05-09 14:42:25 +00001884 if (ndigits < 0)
1885 x /= f;
Guido van Rossum9e51f9b1993-02-12 16:29:05 +00001886 else
Guido van Rossum1e162d31998-05-09 14:42:25 +00001887 x *= f;
1888 if (x >= 0.0)
1889 x = floor(x + 0.5);
1890 else
1891 x = ceil(x - 0.5);
1892 if (ndigits < 0)
1893 x *= f;
1894 else
1895 x /= f;
1896 return PyFloat_FromDouble(x);
Guido van Rossum9e51f9b1993-02-12 16:29:05 +00001897}
1898
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001899PyDoc_STRVAR(round_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001900"round(number[, ndigits]) -> floating point number\n\
1901\n\
1902Round a number to a given precision in decimal digits (default 0 digits).\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001903This always returns a floating point number. Precision may be negative.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001904
Raymond Hettinger64958a12003-12-17 20:43:33 +00001905static PyObject *
1906builtin_sorted(PyObject *self, PyObject *args, PyObject *kwds)
1907{
1908 PyObject *newlist, *v, *seq, *compare=NULL, *keyfunc=NULL, *newargs;
1909 PyObject *callable;
Martin v. Löwis15e62742006-02-27 16:46:16 +00001910 static char *kwlist[] = {"iterable", "cmp", "key", "reverse", 0};
Neal Norwitz6f0d4792005-12-15 06:40:36 +00001911 int reverse;
Raymond Hettinger64958a12003-12-17 20:43:33 +00001912
Neal Norwitz6f0d4792005-12-15 06:40:36 +00001913 /* args 1-4 should match listsort in Objects/listobject.c */
Armin Rigo71d7e702005-09-20 18:13:03 +00001914 if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|OOi:sorted",
1915 kwlist, &seq, &compare, &keyfunc, &reverse))
1916 return NULL;
Raymond Hettinger64958a12003-12-17 20:43:33 +00001917
1918 newlist = PySequence_List(seq);
1919 if (newlist == NULL)
1920 return NULL;
1921
1922 callable = PyObject_GetAttrString(newlist, "sort");
1923 if (callable == NULL) {
1924 Py_DECREF(newlist);
1925 return NULL;
1926 }
Georg Brandl99d7e4e2005-08-31 22:21:15 +00001927
Raymond Hettinger64958a12003-12-17 20:43:33 +00001928 newargs = PyTuple_GetSlice(args, 1, 4);
1929 if (newargs == NULL) {
1930 Py_DECREF(newlist);
1931 Py_DECREF(callable);
1932 return NULL;
1933 }
1934
1935 v = PyObject_Call(callable, newargs, kwds);
1936 Py_DECREF(newargs);
1937 Py_DECREF(callable);
1938 if (v == NULL) {
1939 Py_DECREF(newlist);
1940 return NULL;
1941 }
1942 Py_DECREF(v);
1943 return newlist;
1944}
1945
1946PyDoc_STRVAR(sorted_doc,
1947"sorted(iterable, cmp=None, key=None, reverse=False) --> new sorted list");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001948
Guido van Rossum79f25d91997-04-29 20:08:16 +00001949static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001950builtin_vars(PyObject *self, PyObject *args)
Guido van Rossum2d951851994-08-29 12:52:16 +00001951{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001952 PyObject *v = NULL;
1953 PyObject *d;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001954
Raymond Hettingerea3fdf42002-12-29 16:33:45 +00001955 if (!PyArg_UnpackTuple(args, "vars", 0, 1, &v))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001956 return NULL;
Guido van Rossum2d951851994-08-29 12:52:16 +00001957 if (v == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001958 d = PyEval_GetLocals();
Guido van Rossum53bb7ff1995-07-26 16:26:31 +00001959 if (d == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001960 if (!PyErr_Occurred())
1961 PyErr_SetString(PyExc_SystemError,
Alex Martellia9b9c9f2003-04-23 13:34:35 +00001962 "vars(): no locals!?");
Guido van Rossum53bb7ff1995-07-26 16:26:31 +00001963 }
1964 else
Guido van Rossum79f25d91997-04-29 20:08:16 +00001965 Py_INCREF(d);
Guido van Rossum2d951851994-08-29 12:52:16 +00001966 }
1967 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001968 d = PyObject_GetAttrString(v, "__dict__");
Guido van Rossum2d951851994-08-29 12:52:16 +00001969 if (d == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001970 PyErr_SetString(PyExc_TypeError,
Guido van Rossum2d951851994-08-29 12:52:16 +00001971 "vars() argument must have __dict__ attribute");
1972 return NULL;
1973 }
1974 }
1975 return d;
1976}
1977
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001978PyDoc_STRVAR(vars_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001979"vars([object]) -> dictionary\n\
1980\n\
1981Without arguments, equivalent to locals().\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001982With an argument, equivalent to object.__dict__.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001983
Alex Martellia70b1912003-04-22 08:12:33 +00001984
1985static PyObject*
1986builtin_sum(PyObject *self, PyObject *args)
1987{
1988 PyObject *seq;
1989 PyObject *result = NULL;
1990 PyObject *temp, *item, *iter;
1991
Raymond Hettinger5cf63942003-10-25 06:41:37 +00001992 if (!PyArg_UnpackTuple(args, "sum", 1, 2, &seq, &result))
Alex Martellia70b1912003-04-22 08:12:33 +00001993 return NULL;
1994
1995 iter = PyObject_GetIter(seq);
1996 if (iter == NULL)
1997 return NULL;
1998
1999 if (result == NULL) {
2000 result = PyInt_FromLong(0);
2001 if (result == NULL) {
2002 Py_DECREF(iter);
2003 return NULL;
2004 }
2005 } else {
2006 /* reject string values for 'start' parameter */
2007 if (PyObject_TypeCheck(result, &PyBaseString_Type)) {
2008 PyErr_SetString(PyExc_TypeError,
Alex Martellia9b9c9f2003-04-23 13:34:35 +00002009 "sum() can't sum strings [use ''.join(seq) instead]");
Alex Martellia70b1912003-04-22 08:12:33 +00002010 Py_DECREF(iter);
2011 return NULL;
2012 }
Alex Martelli41c9f882003-04-22 09:24:48 +00002013 Py_INCREF(result);
Alex Martellia70b1912003-04-22 08:12:33 +00002014 }
2015
2016 for(;;) {
2017 item = PyIter_Next(iter);
2018 if (item == NULL) {
2019 /* error, or end-of-sequence */
2020 if (PyErr_Occurred()) {
2021 Py_DECREF(result);
2022 result = NULL;
2023 }
2024 break;
2025 }
Alex Martellia253e182003-10-25 23:24:14 +00002026 temp = PyNumber_Add(result, item);
Alex Martellia70b1912003-04-22 08:12:33 +00002027 Py_DECREF(result);
2028 Py_DECREF(item);
2029 result = temp;
2030 if (result == NULL)
2031 break;
2032 }
2033 Py_DECREF(iter);
2034 return result;
2035}
2036
2037PyDoc_STRVAR(sum_doc,
2038"sum(sequence, start=0) -> value\n\
2039\n\
2040Returns the sum of a sequence of numbers (NOT strings) plus the value\n\
2041of parameter 'start'. When the sequence is empty, returns start.");
2042
2043
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002044static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002045builtin_isinstance(PyObject *self, PyObject *args)
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002046{
2047 PyObject *inst;
2048 PyObject *cls;
Guido van Rossum823649d2001-03-21 18:40:58 +00002049 int retval;
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002050
Raymond Hettingerea3fdf42002-12-29 16:33:45 +00002051 if (!PyArg_UnpackTuple(args, "isinstance", 2, 2, &inst, &cls))
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002052 return NULL;
Guido van Rossumf5dd9141997-12-02 19:11:45 +00002053
Guido van Rossum823649d2001-03-21 18:40:58 +00002054 retval = PyObject_IsInstance(inst, cls);
2055 if (retval < 0)
Guido van Rossumad991772001-01-12 16:03:05 +00002056 return NULL;
Guido van Rossum77f6a652002-04-03 22:41:51 +00002057 return PyBool_FromLong(retval);
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002058}
2059
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002060PyDoc_STRVAR(isinstance_doc,
Guido van Rossum77f6a652002-04-03 22:41:51 +00002061"isinstance(object, class-or-type-or-tuple) -> bool\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002062\n\
2063Return whether an object is an instance of a class or of a subclass thereof.\n\
Guido van Rossum03290ec2001-10-07 20:54:12 +00002064With a type as second argument, return whether that is the object's type.\n\
2065The form using a tuple, isinstance(x, (A, B, ...)), is a shortcut for\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002066isinstance(x, A) or isinstance(x, B) or ... (etc.).");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002067
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002068
2069static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002070builtin_issubclass(PyObject *self, PyObject *args)
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002071{
2072 PyObject *derived;
2073 PyObject *cls;
2074 int retval;
2075
Raymond Hettingerea3fdf42002-12-29 16:33:45 +00002076 if (!PyArg_UnpackTuple(args, "issubclass", 2, 2, &derived, &cls))
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002077 return NULL;
Guido van Rossum668213d1999-06-16 17:28:37 +00002078
Guido van Rossum823649d2001-03-21 18:40:58 +00002079 retval = PyObject_IsSubclass(derived, cls);
2080 if (retval < 0)
2081 return NULL;
Guido van Rossum77f6a652002-04-03 22:41:51 +00002082 return PyBool_FromLong(retval);
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002083}
2084
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002085PyDoc_STRVAR(issubclass_doc,
Guido van Rossum77f6a652002-04-03 22:41:51 +00002086"issubclass(C, B) -> bool\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002087\n\
Walter Dörwaldd9a6ad32002-12-12 16:41:44 +00002088Return whether class C is a subclass (i.e., a derived class) of class B.\n\
2089When using a tuple as the second argument issubclass(X, (A, B, ...)),\n\
2090is a shortcut for issubclass(X, A) or issubclass(X, B) or ... (etc.).");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002091
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002092
Barry Warsawbd599b52000-08-03 15:45:29 +00002093static PyObject*
2094builtin_zip(PyObject *self, PyObject *args)
2095{
2096 PyObject *ret;
Martin v. Löwisd96ee902006-02-16 14:37:16 +00002097 const Py_ssize_t itemsize = PySequence_Length(args);
2098 Py_ssize_t i;
Tim Peters8572b4f2001-05-06 01:05:02 +00002099 PyObject *itlist; /* tuple of iterators */
Martin v. Löwisd96ee902006-02-16 14:37:16 +00002100 Py_ssize_t len; /* guess at result length */
Barry Warsawbd599b52000-08-03 15:45:29 +00002101
Raymond Hettingereaef6152003-08-02 07:42:57 +00002102 if (itemsize == 0)
2103 return PyList_New(0);
2104
Barry Warsawbd599b52000-08-03 15:45:29 +00002105 /* args must be a tuple */
2106 assert(PyTuple_Check(args));
2107
Tim Peters39a86c22002-05-12 07:19:38 +00002108 /* Guess at result length: the shortest of the input lengths.
2109 If some argument refuses to say, we refuse to guess too, lest
2110 an argument like xrange(sys.maxint) lead us astray.*/
Tim Peters67d687a2002-04-29 21:27:32 +00002111 len = -1; /* unknown */
2112 for (i = 0; i < itemsize; ++i) {
2113 PyObject *item = PyTuple_GET_ITEM(args, i);
Martin v. Löwisd96ee902006-02-16 14:37:16 +00002114 Py_ssize_t thislen = _PyObject_LengthHint(item);
Tim Peters39a86c22002-05-12 07:19:38 +00002115 if (thislen < 0) {
Raymond Hettingera710b332005-08-21 11:03:59 +00002116 if (!PyErr_ExceptionMatches(PyExc_TypeError) &&
2117 !PyErr_ExceptionMatches(PyExc_AttributeError)) {
2118 return NULL;
2119 }
Tim Peters67d687a2002-04-29 21:27:32 +00002120 PyErr_Clear();
Tim Peters39a86c22002-05-12 07:19:38 +00002121 len = -1;
2122 break;
2123 }
Tim Peters67d687a2002-04-29 21:27:32 +00002124 else if (len < 0 || thislen < len)
2125 len = thislen;
2126 }
2127
Tim Peters8572b4f2001-05-06 01:05:02 +00002128 /* allocate result list */
Tim Peters67d687a2002-04-29 21:27:32 +00002129 if (len < 0)
2130 len = 10; /* arbitrary */
2131 if ((ret = PyList_New(len)) == NULL)
Barry Warsawbd599b52000-08-03 15:45:29 +00002132 return NULL;
2133
Tim Peters8572b4f2001-05-06 01:05:02 +00002134 /* obtain iterators */
2135 itlist = PyTuple_New(itemsize);
2136 if (itlist == NULL)
2137 goto Fail_ret;
2138 for (i = 0; i < itemsize; ++i) {
2139 PyObject *item = PyTuple_GET_ITEM(args, i);
2140 PyObject *it = PyObject_GetIter(item);
2141 if (it == NULL) {
2142 if (PyErr_ExceptionMatches(PyExc_TypeError))
2143 PyErr_Format(PyExc_TypeError,
Neal Norwitza361bd82006-02-19 19:31:50 +00002144 "zip argument #%zd must support iteration",
Tim Peters8572b4f2001-05-06 01:05:02 +00002145 i+1);
2146 goto Fail_ret_itlist;
Barry Warsawbd599b52000-08-03 15:45:29 +00002147 }
Tim Peters8572b4f2001-05-06 01:05:02 +00002148 PyTuple_SET_ITEM(itlist, i, it);
2149 }
Barry Warsawbd599b52000-08-03 15:45:29 +00002150
Tim Peters8572b4f2001-05-06 01:05:02 +00002151 /* build result into ret list */
Tim Peters67d687a2002-04-29 21:27:32 +00002152 for (i = 0; ; ++i) {
2153 int j;
Tim Peters8572b4f2001-05-06 01:05:02 +00002154 PyObject *next = PyTuple_New(itemsize);
2155 if (!next)
2156 goto Fail_ret_itlist;
2157
Tim Peters67d687a2002-04-29 21:27:32 +00002158 for (j = 0; j < itemsize; j++) {
2159 PyObject *it = PyTuple_GET_ITEM(itlist, j);
Tim Peters8572b4f2001-05-06 01:05:02 +00002160 PyObject *item = PyIter_Next(it);
Barry Warsawbd599b52000-08-03 15:45:29 +00002161 if (!item) {
Tim Peters8572b4f2001-05-06 01:05:02 +00002162 if (PyErr_Occurred()) {
2163 Py_DECREF(ret);
2164 ret = NULL;
Barry Warsawbd599b52000-08-03 15:45:29 +00002165 }
2166 Py_DECREF(next);
Tim Peters8572b4f2001-05-06 01:05:02 +00002167 Py_DECREF(itlist);
Tim Peters67d687a2002-04-29 21:27:32 +00002168 goto Done;
Barry Warsawbd599b52000-08-03 15:45:29 +00002169 }
Tim Peters67d687a2002-04-29 21:27:32 +00002170 PyTuple_SET_ITEM(next, j, item);
Barry Warsawbd599b52000-08-03 15:45:29 +00002171 }
Tim Peters8572b4f2001-05-06 01:05:02 +00002172
Tim Peters67d687a2002-04-29 21:27:32 +00002173 if (i < len)
2174 PyList_SET_ITEM(ret, i, next);
2175 else {
2176 int status = PyList_Append(ret, next);
2177 Py_DECREF(next);
2178 ++len;
2179 if (status < 0)
2180 goto Fail_ret_itlist;
2181 }
Barry Warsawbd599b52000-08-03 15:45:29 +00002182 }
Tim Peters8572b4f2001-05-06 01:05:02 +00002183
Tim Peters67d687a2002-04-29 21:27:32 +00002184Done:
2185 if (ret != NULL && i < len) {
2186 /* The list is too big. */
2187 if (PyList_SetSlice(ret, i, len, NULL) < 0)
2188 return NULL;
2189 }
2190 return ret;
2191
Tim Peters8572b4f2001-05-06 01:05:02 +00002192Fail_ret_itlist:
2193 Py_DECREF(itlist);
2194Fail_ret:
2195 Py_DECREF(ret);
2196 return NULL;
Barry Warsawbd599b52000-08-03 15:45:29 +00002197}
2198
2199
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002200PyDoc_STRVAR(zip_doc,
Barry Warsawbd599b52000-08-03 15:45:29 +00002201"zip(seq1 [, seq2 [...]]) -> [(seq1[0], seq2[0] ...), (...)]\n\
2202\n\
2203Return a list of tuples, where each tuple contains the i-th element\n\
2204from each of the argument sequences. The returned list is truncated\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002205in length to the length of the shortest argument sequence.");
Barry Warsawbd599b52000-08-03 15:45:29 +00002206
2207
Guido van Rossum79f25d91997-04-29 20:08:16 +00002208static PyMethodDef builtin_methods[] = {
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002209 {"__import__", builtin___import__, METH_VARARGS, import_doc},
2210 {"abs", builtin_abs, METH_O, abs_doc},
Raymond Hettinger96229b12005-03-11 06:49:40 +00002211 {"all", builtin_all, METH_O, all_doc},
2212 {"any", builtin_any, METH_O, any_doc},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002213 {"apply", builtin_apply, METH_VARARGS, apply_doc},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002214 {"callable", builtin_callable, METH_O, callable_doc},
2215 {"chr", builtin_chr, METH_VARARGS, chr_doc},
2216 {"cmp", builtin_cmp, METH_VARARGS, cmp_doc},
2217 {"coerce", builtin_coerce, METH_VARARGS, coerce_doc},
2218 {"compile", builtin_compile, METH_VARARGS, compile_doc},
2219 {"delattr", builtin_delattr, METH_VARARGS, delattr_doc},
2220 {"dir", builtin_dir, METH_VARARGS, dir_doc},
2221 {"divmod", builtin_divmod, METH_VARARGS, divmod_doc},
2222 {"eval", builtin_eval, METH_VARARGS, eval_doc},
2223 {"execfile", builtin_execfile, METH_VARARGS, execfile_doc},
2224 {"filter", builtin_filter, METH_VARARGS, filter_doc},
2225 {"getattr", builtin_getattr, METH_VARARGS, getattr_doc},
2226 {"globals", (PyCFunction)builtin_globals, METH_NOARGS, globals_doc},
2227 {"hasattr", builtin_hasattr, METH_VARARGS, hasattr_doc},
2228 {"hash", builtin_hash, METH_O, hash_doc},
2229 {"hex", builtin_hex, METH_O, hex_doc},
2230 {"id", builtin_id, METH_O, id_doc},
2231 {"input", builtin_input, METH_VARARGS, input_doc},
2232 {"intern", builtin_intern, METH_VARARGS, intern_doc},
2233 {"isinstance", builtin_isinstance, METH_VARARGS, isinstance_doc},
2234 {"issubclass", builtin_issubclass, METH_VARARGS, issubclass_doc},
2235 {"iter", builtin_iter, METH_VARARGS, iter_doc},
2236 {"len", builtin_len, METH_O, len_doc},
2237 {"locals", (PyCFunction)builtin_locals, METH_NOARGS, locals_doc},
2238 {"map", builtin_map, METH_VARARGS, map_doc},
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00002239 {"max", (PyCFunction)builtin_max, METH_VARARGS | METH_KEYWORDS, max_doc},
2240 {"min", (PyCFunction)builtin_min, METH_VARARGS | METH_KEYWORDS, min_doc},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002241 {"oct", builtin_oct, METH_O, oct_doc},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002242 {"ord", builtin_ord, METH_O, ord_doc},
2243 {"pow", builtin_pow, METH_VARARGS, pow_doc},
2244 {"range", builtin_range, METH_VARARGS, range_doc},
2245 {"raw_input", builtin_raw_input, METH_VARARGS, raw_input_doc},
2246 {"reduce", builtin_reduce, METH_VARARGS, reduce_doc},
2247 {"reload", builtin_reload, METH_O, reload_doc},
2248 {"repr", builtin_repr, METH_O, repr_doc},
2249 {"round", builtin_round, METH_VARARGS, round_doc},
2250 {"setattr", builtin_setattr, METH_VARARGS, setattr_doc},
Raymond Hettinger64958a12003-12-17 20:43:33 +00002251 {"sorted", (PyCFunction)builtin_sorted, METH_VARARGS | METH_KEYWORDS, sorted_doc},
Alex Martellia70b1912003-04-22 08:12:33 +00002252 {"sum", builtin_sum, METH_VARARGS, sum_doc},
Martin v. Löwis339d0f72001-08-17 18:39:25 +00002253#ifdef Py_USING_UNICODE
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002254 {"unichr", builtin_unichr, METH_VARARGS, unichr_doc},
Martin v. Löwis339d0f72001-08-17 18:39:25 +00002255#endif
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002256 {"vars", builtin_vars, METH_VARARGS, vars_doc},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002257 {"zip", builtin_zip, METH_VARARGS, zip_doc},
Guido van Rossumc02e15c1991-12-16 13:03:00 +00002258 {NULL, NULL},
Guido van Rossum3f5da241990-12-20 15:06:42 +00002259};
2260
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002261PyDoc_STRVAR(builtin_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002262"Built-in functions, exceptions, and other objects.\n\
2263\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002264Noteworthy: None is the `nil' object; Ellipsis represents `...' in slices.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002265
Guido van Rossum25ce5661997-08-02 03:10:38 +00002266PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002267_PyBuiltin_Init(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +00002268{
Fred Drake5550de32000-06-20 04:54:19 +00002269 PyObject *mod, *dict, *debug;
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002270 mod = Py_InitModule4("__builtin__", builtin_methods,
2271 builtin_doc, (PyObject *)NULL,
2272 PYTHON_API_VERSION);
Guido van Rossum25ce5661997-08-02 03:10:38 +00002273 if (mod == NULL)
2274 return NULL;
2275 dict = PyModule_GetDict(mod);
Tim Peters4b7625e2001-09-13 21:37:17 +00002276
Tim Peters7571a0f2003-03-23 17:52:28 +00002277#ifdef Py_TRACE_REFS
2278 /* __builtin__ exposes a number of statically allocated objects
2279 * that, before this code was added in 2.3, never showed up in
2280 * the list of "all objects" maintained by Py_TRACE_REFS. As a
2281 * result, programs leaking references to None and False (etc)
2282 * couldn't be diagnosed by examining sys.getobjects(0).
2283 */
2284#define ADD_TO_ALL(OBJECT) _Py_AddToAllObjects((PyObject *)(OBJECT), 0)
2285#else
2286#define ADD_TO_ALL(OBJECT) (void)0
2287#endif
2288
Tim Peters4b7625e2001-09-13 21:37:17 +00002289#define SETBUILTIN(NAME, OBJECT) \
Tim Peters7571a0f2003-03-23 17:52:28 +00002290 if (PyDict_SetItemString(dict, NAME, (PyObject *)OBJECT) < 0) \
2291 return NULL; \
2292 ADD_TO_ALL(OBJECT)
Tim Peters4b7625e2001-09-13 21:37:17 +00002293
2294 SETBUILTIN("None", Py_None);
2295 SETBUILTIN("Ellipsis", Py_Ellipsis);
2296 SETBUILTIN("NotImplemented", Py_NotImplemented);
Guido van Rossum77f6a652002-04-03 22:41:51 +00002297 SETBUILTIN("False", Py_False);
2298 SETBUILTIN("True", Py_True);
Neal Norwitz32a7e7f2002-05-31 19:58:02 +00002299 SETBUILTIN("basestring", &PyBaseString_Type);
Guido van Rossum77f6a652002-04-03 22:41:51 +00002300 SETBUILTIN("bool", &PyBool_Type);
Guido van Rossumbea18cc2002-06-14 20:41:17 +00002301 SETBUILTIN("buffer", &PyBuffer_Type);
Tim Peters4b7625e2001-09-13 21:37:17 +00002302 SETBUILTIN("classmethod", &PyClassMethod_Type);
Tim Peters6d6c1a32001-08-02 04:15:00 +00002303#ifndef WITHOUT_COMPLEX
Tim Peters4b7625e2001-09-13 21:37:17 +00002304 SETBUILTIN("complex", &PyComplex_Type);
Tim Peters6d6c1a32001-08-02 04:15:00 +00002305#endif
Tim Petersa427a2b2001-10-29 22:25:45 +00002306 SETBUILTIN("dict", &PyDict_Type);
Tim Peters67d687a2002-04-29 21:27:32 +00002307 SETBUILTIN("enumerate", &PyEnum_Type);
Tim Peters4b7625e2001-09-13 21:37:17 +00002308 SETBUILTIN("float", &PyFloat_Type);
Raymond Hettingera690a992003-11-16 16:17:49 +00002309 SETBUILTIN("frozenset", &PyFrozenSet_Type);
Tim Peters4b7625e2001-09-13 21:37:17 +00002310 SETBUILTIN("property", &PyProperty_Type);
2311 SETBUILTIN("int", &PyInt_Type);
2312 SETBUILTIN("list", &PyList_Type);
2313 SETBUILTIN("long", &PyLong_Type);
2314 SETBUILTIN("object", &PyBaseObject_Type);
Raymond Hettinger85c20a42003-11-06 14:06:48 +00002315 SETBUILTIN("reversed", &PyReversed_Type);
Raymond Hettingera690a992003-11-16 16:17:49 +00002316 SETBUILTIN("set", &PySet_Type);
Guido van Rossumbea18cc2002-06-14 20:41:17 +00002317 SETBUILTIN("slice", &PySlice_Type);
Tim Peters4b7625e2001-09-13 21:37:17 +00002318 SETBUILTIN("staticmethod", &PyStaticMethod_Type);
2319 SETBUILTIN("str", &PyString_Type);
2320 SETBUILTIN("super", &PySuper_Type);
2321 SETBUILTIN("tuple", &PyTuple_Type);
2322 SETBUILTIN("type", &PyType_Type);
Raymond Hettingerc4c453f2002-06-05 23:12:45 +00002323 SETBUILTIN("xrange", &PyRange_Type);
Tim Peters742dfd62001-09-13 21:49:44 +00002324
2325 /* Note that open() is just an alias of file(). */
2326 SETBUILTIN("open", &PyFile_Type);
Tim Peters4b7625e2001-09-13 21:37:17 +00002327 SETBUILTIN("file", &PyFile_Type);
Martin v. Löwis339d0f72001-08-17 18:39:25 +00002328#ifdef Py_USING_UNICODE
Tim Peters4b7625e2001-09-13 21:37:17 +00002329 SETBUILTIN("unicode", &PyUnicode_Type);
Martin v. Löwis339d0f72001-08-17 18:39:25 +00002330#endif
Guido van Rossum77f6a652002-04-03 22:41:51 +00002331 debug = PyBool_FromLong(Py_OptimizeFlag == 0);
Fred Drake5550de32000-06-20 04:54:19 +00002332 if (PyDict_SetItemString(dict, "__debug__", debug) < 0) {
2333 Py_XDECREF(debug);
Guido van Rossum25ce5661997-08-02 03:10:38 +00002334 return NULL;
Fred Drake5550de32000-06-20 04:54:19 +00002335 }
2336 Py_XDECREF(debug);
Barry Warsaw757af0e1997-08-29 22:13:51 +00002337
Guido van Rossum25ce5661997-08-02 03:10:38 +00002338 return mod;
Tim Peters7571a0f2003-03-23 17:52:28 +00002339#undef ADD_TO_ALL
Tim Peters4b7625e2001-09-13 21:37:17 +00002340#undef SETBUILTIN
Guido van Rossum3f5da241990-12-20 15:06:42 +00002341}
2342
Guido van Rossume77a7571993-11-03 15:01:26 +00002343/* Helper for filter(): filter a tuple through a function */
Guido van Rossum12d12c51993-10-26 17:58:25 +00002344
Guido van Rossum79f25d91997-04-29 20:08:16 +00002345static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002346filtertuple(PyObject *func, PyObject *tuple)
Guido van Rossum12d12c51993-10-26 17:58:25 +00002347{
Guido van Rossum79f25d91997-04-29 20:08:16 +00002348 PyObject *result;
Martin v. Löwis18e16552006-02-15 17:27:45 +00002349 Py_ssize_t i, j;
2350 Py_ssize_t len = PyTuple_Size(tuple);
Guido van Rossum12d12c51993-10-26 17:58:25 +00002351
Guido van Rossumb7b45621995-08-04 04:07:45 +00002352 if (len == 0) {
Walter Dörwaldc3da83f2003-02-04 20:24:45 +00002353 if (PyTuple_CheckExact(tuple))
2354 Py_INCREF(tuple);
2355 else
2356 tuple = PyTuple_New(0);
Guido van Rossumb7b45621995-08-04 04:07:45 +00002357 return tuple;
2358 }
2359
Guido van Rossum79f25d91997-04-29 20:08:16 +00002360 if ((result = PyTuple_New(len)) == NULL)
Guido van Rossum2586bf01993-11-01 16:21:44 +00002361 return NULL;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002362
Guido van Rossum12d12c51993-10-26 17:58:25 +00002363 for (i = j = 0; i < len; ++i) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002364 PyObject *item, *good;
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002365 int ok;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002366
Walter Dörwald8dd19322003-02-10 17:36:40 +00002367 if (tuple->ob_type->tp_as_sequence &&
2368 tuple->ob_type->tp_as_sequence->sq_item) {
2369 item = tuple->ob_type->tp_as_sequence->sq_item(tuple, i);
Walter Dörwaldc58a3a12003-08-18 18:28:45 +00002370 if (item == NULL)
2371 goto Fail_1;
Walter Dörwald8dd19322003-02-10 17:36:40 +00002372 } else {
Alex Martellia9b9c9f2003-04-23 13:34:35 +00002373 PyErr_SetString(PyExc_TypeError, "filter(): unsubscriptable tuple");
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002374 goto Fail_1;
Walter Dörwald8dd19322003-02-10 17:36:40 +00002375 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002376 if (func == Py_None) {
2377 Py_INCREF(item);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002378 good = item;
2379 }
2380 else {
Raymond Hettinger8ae46892003-10-12 19:09:37 +00002381 PyObject *arg = PyTuple_Pack(1, item);
Walter Dörwaldc58a3a12003-08-18 18:28:45 +00002382 if (arg == NULL) {
2383 Py_DECREF(item);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002384 goto Fail_1;
Walter Dörwaldc58a3a12003-08-18 18:28:45 +00002385 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002386 good = PyEval_CallObject(func, arg);
2387 Py_DECREF(arg);
Walter Dörwaldc58a3a12003-08-18 18:28:45 +00002388 if (good == NULL) {
2389 Py_DECREF(item);
Guido van Rossum12d12c51993-10-26 17:58:25 +00002390 goto Fail_1;
Walter Dörwaldc58a3a12003-08-18 18:28:45 +00002391 }
Guido van Rossum12d12c51993-10-26 17:58:25 +00002392 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002393 ok = PyObject_IsTrue(good);
2394 Py_DECREF(good);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002395 if (ok) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002396 if (PyTuple_SetItem(result, j++, item) < 0)
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002397 goto Fail_1;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002398 }
Walter Dörwaldc58a3a12003-08-18 18:28:45 +00002399 else
2400 Py_DECREF(item);
Guido van Rossum12d12c51993-10-26 17:58:25 +00002401 }
2402
Tim Peters4324aa32001-05-28 22:30:08 +00002403 if (_PyTuple_Resize(&result, j) < 0)
Guido van Rossum12d12c51993-10-26 17:58:25 +00002404 return NULL;
2405
Guido van Rossum12d12c51993-10-26 17:58:25 +00002406 return result;
2407
Guido van Rossum12d12c51993-10-26 17:58:25 +00002408Fail_1:
Guido van Rossum79f25d91997-04-29 20:08:16 +00002409 Py_DECREF(result);
Guido van Rossum12d12c51993-10-26 17:58:25 +00002410 return NULL;
2411}
2412
2413
Guido van Rossume77a7571993-11-03 15:01:26 +00002414/* Helper for filter(): filter a string through a function */
Guido van Rossum12d12c51993-10-26 17:58:25 +00002415
Guido van Rossum79f25d91997-04-29 20:08:16 +00002416static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002417filterstring(PyObject *func, PyObject *strobj)
Guido van Rossum12d12c51993-10-26 17:58:25 +00002418{
Guido van Rossum79f25d91997-04-29 20:08:16 +00002419 PyObject *result;
Martin v. Löwis18e16552006-02-15 17:27:45 +00002420 Py_ssize_t i, j;
2421 Py_ssize_t len = PyString_Size(strobj);
2422 Py_ssize_t outlen = len;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002423
Guido van Rossum79f25d91997-04-29 20:08:16 +00002424 if (func == Py_None) {
Walter Dörwald1918f772003-02-10 13:19:13 +00002425 /* If it's a real string we can return the original,
2426 * as no character is ever false and __getitem__
2427 * does return this character. If it's a subclass
2428 * we must go through the __getitem__ loop */
2429 if (PyString_CheckExact(strobj)) {
Walter Dörwaldc3da83f2003-02-04 20:24:45 +00002430 Py_INCREF(strobj);
Walter Dörwald1918f772003-02-10 13:19:13 +00002431 return strobj;
2432 }
Guido van Rossum12d12c51993-10-26 17:58:25 +00002433 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002434 if ((result = PyString_FromStringAndSize(NULL, len)) == NULL)
Guido van Rossum2586bf01993-11-01 16:21:44 +00002435 return NULL;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002436
Guido van Rossum12d12c51993-10-26 17:58:25 +00002437 for (i = j = 0; i < len; ++i) {
Walter Dörwald1918f772003-02-10 13:19:13 +00002438 PyObject *item;
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002439 int ok;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002440
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002441 item = (*strobj->ob_type->tp_as_sequence->sq_item)(strobj, i);
2442 if (item == NULL)
2443 goto Fail_1;
Walter Dörwald1918f772003-02-10 13:19:13 +00002444 if (func==Py_None) {
2445 ok = 1;
2446 } else {
2447 PyObject *arg, *good;
Raymond Hettinger8ae46892003-10-12 19:09:37 +00002448 arg = PyTuple_Pack(1, item);
Walter Dörwald1918f772003-02-10 13:19:13 +00002449 if (arg == NULL) {
2450 Py_DECREF(item);
2451 goto Fail_1;
2452 }
2453 good = PyEval_CallObject(func, arg);
2454 Py_DECREF(arg);
2455 if (good == NULL) {
2456 Py_DECREF(item);
2457 goto Fail_1;
2458 }
2459 ok = PyObject_IsTrue(good);
2460 Py_DECREF(good);
Tim Peters388ed082001-04-07 20:34:48 +00002461 }
Walter Dörwald903f1e02003-02-04 16:28:00 +00002462 if (ok) {
Martin v. Löwisd96ee902006-02-16 14:37:16 +00002463 Py_ssize_t reslen;
Walter Dörwald903f1e02003-02-04 16:28:00 +00002464 if (!PyString_Check(item)) {
2465 PyErr_SetString(PyExc_TypeError, "can't filter str to str:"
2466 " __getitem__ returned different type");
2467 Py_DECREF(item);
2468 goto Fail_1;
2469 }
2470 reslen = PyString_GET_SIZE(item);
2471 if (reslen == 1) {
2472 PyString_AS_STRING(result)[j++] =
2473 PyString_AS_STRING(item)[0];
2474 } else {
2475 /* do we need more space? */
Martin v. Löwisd96ee902006-02-16 14:37:16 +00002476 Py_ssize_t need = j + reslen + len-i-1;
Walter Dörwald903f1e02003-02-04 16:28:00 +00002477 if (need > outlen) {
2478 /* overallocate, to avoid reallocations */
2479 if (need<2*outlen)
2480 need = 2*outlen;
2481 if (_PyString_Resize(&result, need)) {
2482 Py_DECREF(item);
2483 return NULL;
2484 }
2485 outlen = need;
2486 }
2487 memcpy(
2488 PyString_AS_STRING(result) + j,
2489 PyString_AS_STRING(item),
2490 reslen
2491 );
2492 j += reslen;
2493 }
2494 }
Tim Peters388ed082001-04-07 20:34:48 +00002495 Py_DECREF(item);
Guido van Rossum12d12c51993-10-26 17:58:25 +00002496 }
2497
Walter Dörwald903f1e02003-02-04 16:28:00 +00002498 if (j < outlen)
Tim Peters5de98422002-04-27 18:44:32 +00002499 _PyString_Resize(&result, j);
Guido van Rossum12d12c51993-10-26 17:58:25 +00002500
Guido van Rossum12d12c51993-10-26 17:58:25 +00002501 return result;
2502
Guido van Rossum12d12c51993-10-26 17:58:25 +00002503Fail_1:
Guido van Rossum79f25d91997-04-29 20:08:16 +00002504 Py_DECREF(result);
Guido van Rossum12d12c51993-10-26 17:58:25 +00002505 return NULL;
2506}
Martin v. Löwis8afd7572003-01-25 22:46:11 +00002507
2508#ifdef Py_USING_UNICODE
2509/* Helper for filter(): filter a Unicode object through a function */
2510
2511static PyObject *
2512filterunicode(PyObject *func, PyObject *strobj)
2513{
2514 PyObject *result;
2515 register int i, j;
Martin v. Löwisd96ee902006-02-16 14:37:16 +00002516 Py_ssize_t len = PyUnicode_GetSize(strobj);
2517 Py_ssize_t outlen = len;
Martin v. Löwis8afd7572003-01-25 22:46:11 +00002518
2519 if (func == Py_None) {
Walter Dörwald1918f772003-02-10 13:19:13 +00002520 /* If it's a real string we can return the original,
2521 * as no character is ever false and __getitem__
2522 * does return this character. If it's a subclass
2523 * we must go through the __getitem__ loop */
2524 if (PyUnicode_CheckExact(strobj)) {
Walter Dörwaldc3da83f2003-02-04 20:24:45 +00002525 Py_INCREF(strobj);
Walter Dörwald1918f772003-02-10 13:19:13 +00002526 return strobj;
2527 }
Martin v. Löwis8afd7572003-01-25 22:46:11 +00002528 }
2529 if ((result = PyUnicode_FromUnicode(NULL, len)) == NULL)
2530 return NULL;
2531
2532 for (i = j = 0; i < len; ++i) {
2533 PyObject *item, *arg, *good;
2534 int ok;
2535
2536 item = (*strobj->ob_type->tp_as_sequence->sq_item)(strobj, i);
2537 if (item == NULL)
2538 goto Fail_1;
Walter Dörwald1918f772003-02-10 13:19:13 +00002539 if (func == Py_None) {
2540 ok = 1;
2541 } else {
Raymond Hettinger8ae46892003-10-12 19:09:37 +00002542 arg = PyTuple_Pack(1, item);
Walter Dörwald1918f772003-02-10 13:19:13 +00002543 if (arg == NULL) {
2544 Py_DECREF(item);
2545 goto Fail_1;
2546 }
2547 good = PyEval_CallObject(func, arg);
2548 Py_DECREF(arg);
2549 if (good == NULL) {
2550 Py_DECREF(item);
2551 goto Fail_1;
2552 }
2553 ok = PyObject_IsTrue(good);
2554 Py_DECREF(good);
Martin v. Löwis8afd7572003-01-25 22:46:11 +00002555 }
Walter Dörwald903f1e02003-02-04 16:28:00 +00002556 if (ok) {
Martin v. Löwisd96ee902006-02-16 14:37:16 +00002557 Py_ssize_t reslen;
Walter Dörwald903f1e02003-02-04 16:28:00 +00002558 if (!PyUnicode_Check(item)) {
Georg Brandl99d7e4e2005-08-31 22:21:15 +00002559 PyErr_SetString(PyExc_TypeError,
Jeremy Hylton1aad9c72003-09-16 03:10:59 +00002560 "can't filter unicode to unicode:"
2561 " __getitem__ returned different type");
Walter Dörwald903f1e02003-02-04 16:28:00 +00002562 Py_DECREF(item);
2563 goto Fail_1;
2564 }
2565 reslen = PyUnicode_GET_SIZE(item);
Georg Brandl99d7e4e2005-08-31 22:21:15 +00002566 if (reslen == 1)
Walter Dörwald903f1e02003-02-04 16:28:00 +00002567 PyUnicode_AS_UNICODE(result)[j++] =
2568 PyUnicode_AS_UNICODE(item)[0];
Jeremy Hylton1aad9c72003-09-16 03:10:59 +00002569 else {
Walter Dörwald903f1e02003-02-04 16:28:00 +00002570 /* do we need more space? */
Martin v. Löwisd96ee902006-02-16 14:37:16 +00002571 Py_ssize_t need = j + reslen + len - i - 1;
Walter Dörwald903f1e02003-02-04 16:28:00 +00002572 if (need > outlen) {
Georg Brandl99d7e4e2005-08-31 22:21:15 +00002573 /* overallocate,
Jeremy Hylton1aad9c72003-09-16 03:10:59 +00002574 to avoid reallocations */
2575 if (need < 2 * outlen)
2576 need = 2 * outlen;
Jeremy Hylton364f6be2003-09-16 03:17:16 +00002577 if (PyUnicode_Resize(
2578 &result, need) < 0) {
Walter Dörwald903f1e02003-02-04 16:28:00 +00002579 Py_DECREF(item);
Walter Dörwald531e0002003-02-04 16:57:49 +00002580 goto Fail_1;
Walter Dörwald903f1e02003-02-04 16:28:00 +00002581 }
2582 outlen = need;
2583 }
Jeremy Hylton1aad9c72003-09-16 03:10:59 +00002584 memcpy(PyUnicode_AS_UNICODE(result) + j,
2585 PyUnicode_AS_UNICODE(item),
2586 reslen*sizeof(Py_UNICODE));
Walter Dörwald903f1e02003-02-04 16:28:00 +00002587 j += reslen;
2588 }
2589 }
Martin v. Löwis8afd7572003-01-25 22:46:11 +00002590 Py_DECREF(item);
2591 }
2592
Walter Dörwald903f1e02003-02-04 16:28:00 +00002593 if (j < outlen)
Martin v. Löwis8afd7572003-01-25 22:46:11 +00002594 PyUnicode_Resize(&result, j);
2595
2596 return result;
2597
2598Fail_1:
2599 Py_DECREF(result);
2600 return NULL;
2601}
2602#endif