blob: af5a55b8e071ad404b5435f51583132f8b757aec [file] [log] [blame]
Andrew M. Kuchling9bcc68c2000-12-20 15:07:34 +00001
Guido van Rossum3f5da241990-12-20 15:06:42 +00002/* Built-in functions */
3
Guido van Rossum79f25d91997-04-29 20:08:16 +00004#include "Python.h"
Guido van Rossum3f5da241990-12-20 15:06:42 +00005
6#include "node.h"
Guido van Rossum5b722181993-03-30 17:46:03 +00007#include "compile.h"
8#include "eval.h"
Guido van Rossum3f5da241990-12-20 15:06:42 +00009
Guido van Rossum6bf62da1997-04-11 20:37:35 +000010#include <ctype.h>
11
Guido van Rossume2ae77b2001-10-24 20:42:55 +000012#ifdef RISCOS
13#include "unixstuff.h"
14#endif
15
Mark Hammond26cffde42001-05-14 12:17:34 +000016/* The default encoding used by the platform file system APIs
17 Can remain NULL for all platforms that don't have such a concept
18*/
Martin v. Löwis6238d2b2002-06-30 15:26:10 +000019#if defined(MS_WINDOWS) && defined(HAVE_USABLE_WCHAR_T)
Mark Hammond26cffde42001-05-14 12:17:34 +000020const char *Py_FileSystemDefaultEncoding = "mbcs";
Just van Rossumb9b8e9c2003-02-10 09:22:01 +000021#elif defined(__APPLE__)
22const char *Py_FileSystemDefaultEncoding = "utf-8";
Mark Hammond26cffde42001-05-14 12:17:34 +000023#else
24const char *Py_FileSystemDefaultEncoding = NULL; /* use default */
25#endif
Mark Hammondef8b6542001-05-13 08:04:26 +000026
Guido van Rossum12d12c51993-10-26 17:58:25 +000027/* Forward */
Tim Petersdbd9ba62000-07-09 03:09:57 +000028static PyObject *filterstring(PyObject *, PyObject *);
Martin v. Löwis8afd7572003-01-25 22:46:11 +000029#ifdef Py_USING_UNICODE
30static PyObject *filterunicode(PyObject *, PyObject *);
31#endif
Tim Petersdbd9ba62000-07-09 03:09:57 +000032static PyObject *filtertuple (PyObject *, PyObject *);
Guido van Rossum12d12c51993-10-26 17:58:25 +000033
Guido van Rossum79f25d91997-04-29 20:08:16 +000034static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +000035builtin___import__(PyObject *self, PyObject *args)
Guido van Rossum3f5da241990-12-20 15:06:42 +000036{
Guido van Rossum1ae940a1995-01-02 19:04:15 +000037 char *name;
Guido van Rossum79f25d91997-04-29 20:08:16 +000038 PyObject *globals = NULL;
39 PyObject *locals = NULL;
40 PyObject *fromlist = NULL;
Guido van Rossum1ae940a1995-01-02 19:04:15 +000041
Guido van Rossum79f25d91997-04-29 20:08:16 +000042 if (!PyArg_ParseTuple(args, "s|OOO:__import__",
Guido van Rossum24c13741995-02-14 09:42:43 +000043 &name, &globals, &locals, &fromlist))
Guido van Rossum1ae940a1995-01-02 19:04:15 +000044 return NULL;
Guido van Rossumaee0bad1997-09-05 07:33:22 +000045 return PyImport_ImportModuleEx(name, globals, locals, fromlist);
Guido van Rossum1ae940a1995-01-02 19:04:15 +000046}
47
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000048PyDoc_STRVAR(import_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +000049"__import__(name, globals, locals, fromlist) -> module\n\
50\n\
51Import a module. The globals are only used to determine the context;\n\
52they are not modified. The locals are currently unused. The fromlist\n\
53should be a list of names to emulate ``from name import ...'', or an\n\
54empty list to emulate ``import name''.\n\
55When importing a module from a package, note that __import__('A.B', ...)\n\
56returns package A when fromlist is empty, but its submodule B when\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000057fromlist is not empty.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +000058
Guido van Rossum1ae940a1995-01-02 19:04:15 +000059
Guido van Rossum79f25d91997-04-29 20:08:16 +000060static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +000061builtin_abs(PyObject *self, PyObject *v)
Guido van Rossum1ae940a1995-01-02 19:04:15 +000062{
Guido van Rossum09df08a1998-05-22 00:51:39 +000063 return PyNumber_Absolute(v);
Guido van Rossum3f5da241990-12-20 15:06:42 +000064}
65
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000066PyDoc_STRVAR(abs_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +000067"abs(number) -> number\n\
68\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000069Return the absolute value of the argument.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +000070
Raymond Hettinger96229b12005-03-11 06:49:40 +000071static PyObject *
72builtin_all(PyObject *self, PyObject *v)
73{
74 PyObject *it, *item;
75
76 it = PyObject_GetIter(v);
77 if (it == NULL)
78 return NULL;
79
80 while ((item = PyIter_Next(it)) != NULL) {
81 int cmp = PyObject_IsTrue(item);
82 Py_DECREF(item);
83 if (cmp < 0) {
84 Py_DECREF(it);
85 return NULL;
86 }
87 if (cmp == 0) {
88 Py_DECREF(it);
89 Py_RETURN_FALSE;
90 }
91 }
92 Py_DECREF(it);
93 if (PyErr_Occurred())
94 return NULL;
95 Py_RETURN_TRUE;
96}
97
98PyDoc_STRVAR(all_doc,
99"all(iterable) -> bool\n\
100\n\
101Return True if bool(x) is True for all values x in the iterable.");
102
103static PyObject *
104builtin_any(PyObject *self, PyObject *v)
105{
106 PyObject *it, *item;
107
108 it = PyObject_GetIter(v);
109 if (it == NULL)
110 return NULL;
111
112 while ((item = PyIter_Next(it)) != NULL) {
113 int cmp = PyObject_IsTrue(item);
114 Py_DECREF(item);
115 if (cmp < 0) {
116 Py_DECREF(it);
117 return NULL;
118 }
119 if (cmp == 1) {
120 Py_DECREF(it);
121 Py_RETURN_TRUE;
122 }
123 }
124 Py_DECREF(it);
125 if (PyErr_Occurred())
126 return NULL;
127 Py_RETURN_FALSE;
128}
129
130PyDoc_STRVAR(any_doc,
131"any(iterable) -> bool\n\
132\n\
133Return True if bool(x) is True for any x in the iterable.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000134
Guido van Rossum79f25d91997-04-29 20:08:16 +0000135static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000136builtin_apply(PyObject *self, PyObject *args)
Guido van Rossumc02e15c1991-12-16 13:03:00 +0000137{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000138 PyObject *func, *alist = NULL, *kwdict = NULL;
Barry Warsaw968f8cb1998-10-01 15:33:12 +0000139 PyObject *t = NULL, *retval = NULL;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000140
Raymond Hettingerea3fdf42002-12-29 16:33:45 +0000141 if (!PyArg_UnpackTuple(args, "apply", 1, 3, &func, &alist, &kwdict))
Guido van Rossumc02e15c1991-12-16 13:03:00 +0000142 return NULL;
Barry Warsaw968f8cb1998-10-01 15:33:12 +0000143 if (alist != NULL) {
144 if (!PyTuple_Check(alist)) {
145 if (!PySequence_Check(alist)) {
Jeremy Hyltonc862cf42001-01-19 03:25:05 +0000146 PyErr_Format(PyExc_TypeError,
Alex Martellia9b9c9f2003-04-23 13:34:35 +0000147 "apply() arg 2 expected sequence, found %s",
Jeremy Hyltonc862cf42001-01-19 03:25:05 +0000148 alist->ob_type->tp_name);
Barry Warsaw968f8cb1998-10-01 15:33:12 +0000149 return NULL;
150 }
151 t = PySequence_Tuple(alist);
152 if (t == NULL)
153 return NULL;
154 alist = t;
155 }
Guido van Rossum2d951851994-08-29 12:52:16 +0000156 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000157 if (kwdict != NULL && !PyDict_Check(kwdict)) {
Jeremy Hyltonc862cf42001-01-19 03:25:05 +0000158 PyErr_Format(PyExc_TypeError,
159 "apply() arg 3 expected dictionary, found %s",
160 kwdict->ob_type->tp_name);
Barry Warsaw968f8cb1998-10-01 15:33:12 +0000161 goto finally;
Guido van Rossum681d79a1995-07-18 14:51:37 +0000162 }
Barry Warsaw968f8cb1998-10-01 15:33:12 +0000163 retval = PyEval_CallObjectWithKeywords(func, alist, kwdict);
164 finally:
165 Py_XDECREF(t);
166 return retval;
Guido van Rossumc02e15c1991-12-16 13:03:00 +0000167}
168
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000169PyDoc_STRVAR(apply_doc,
Fred Drakef1fbc622001-01-12 17:05:05 +0000170"apply(object[, args[, kwargs]]) -> value\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000171\n\
Fred Drake7b912121999-12-23 14:16:55 +0000172Call a callable object with positional arguments taken from the tuple args,\n\
173and keyword arguments taken from the optional dictionary kwargs.\n\
Raymond Hettingerff41c482003-04-06 09:01:11 +0000174Note that classes are callable, as are instances with a __call__() method.\n\
175\n\
176Deprecated since release 2.3. Instead, use the extended call syntax:\n\
177 function(*args, **keywords).");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000178
179
Guido van Rossum79f25d91997-04-29 20:08:16 +0000180static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000181builtin_callable(PyObject *self, PyObject *v)
Guido van Rossum2d951851994-08-29 12:52:16 +0000182{
Guido van Rossum77f6a652002-04-03 22:41:51 +0000183 return PyBool_FromLong((long)PyCallable_Check(v));
Guido van Rossum2d951851994-08-29 12:52:16 +0000184}
185
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000186PyDoc_STRVAR(callable_doc,
Guido van Rossum77f6a652002-04-03 22:41:51 +0000187"callable(object) -> bool\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000188\n\
189Return whether the object is callable (i.e., some kind of function).\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000190Note that classes are callable, as are instances with a __call__() method.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000191
192
Guido van Rossum79f25d91997-04-29 20:08:16 +0000193static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000194builtin_filter(PyObject *self, PyObject *args)
Guido van Rossum12d12c51993-10-26 17:58:25 +0000195{
Guido van Rossumc7903a12002-08-16 07:04:56 +0000196 PyObject *func, *seq, *result, *it, *arg;
Tim Peters0e57abf2001-05-02 07:39:38 +0000197 int len; /* guess for result list size */
Tim Petersf4848da2001-05-05 00:14:56 +0000198 register int j;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000199
Raymond Hettingerea3fdf42002-12-29 16:33:45 +0000200 if (!PyArg_UnpackTuple(args, "filter", 2, 2, &func, &seq))
Guido van Rossum12d12c51993-10-26 17:58:25 +0000201 return NULL;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000202
Tim Peters0e57abf2001-05-02 07:39:38 +0000203 /* Strings and tuples return a result of the same type. */
204 if (PyString_Check(seq))
205 return filterstring(func, seq);
Martin v. Löwis8afd7572003-01-25 22:46:11 +0000206#ifdef Py_USING_UNICODE
207 if (PyUnicode_Check(seq))
208 return filterunicode(func, seq);
209#endif
Tim Peters0e57abf2001-05-02 07:39:38 +0000210 if (PyTuple_Check(seq))
211 return filtertuple(func, seq);
212
Georg Brandle35b6572005-07-19 22:20:20 +0000213 /* Pre-allocate argument list tuple. */
214 arg = PyTuple_New(1);
215 if (arg == NULL)
216 return NULL;
217
Tim Peters0e57abf2001-05-02 07:39:38 +0000218 /* Get iterator. */
219 it = PyObject_GetIter(seq);
220 if (it == NULL)
Georg Brandle35b6572005-07-19 22:20:20 +0000221 goto Fail_arg;
Tim Peters0e57abf2001-05-02 07:39:38 +0000222
223 /* Guess a result list size. */
Raymond Hettingerb86269d2004-01-04 11:00:08 +0000224 len = PyObject_Size(seq);
225 if (len < 0) {
Raymond Hettingera710b332005-08-21 11:03:59 +0000226 if (!PyErr_ExceptionMatches(PyExc_TypeError) &&
227 !PyErr_ExceptionMatches(PyExc_AttributeError)) {
228 goto Fail_it;
229 }
Raymond Hettingerb86269d2004-01-04 11:00:08 +0000230 PyErr_Clear();
231 len = 8; /* arbitrary */
Guido van Rossum12d12c51993-10-26 17:58:25 +0000232 }
233
Tim Peters0e57abf2001-05-02 07:39:38 +0000234 /* Get a result list. */
Guido van Rossum79f25d91997-04-29 20:08:16 +0000235 if (PyList_Check(seq) && seq->ob_refcnt == 1) {
Tim Peters0e57abf2001-05-02 07:39:38 +0000236 /* Eww - can modify the list in-place. */
Guido van Rossum79f25d91997-04-29 20:08:16 +0000237 Py_INCREF(seq);
Guido van Rossum12d12c51993-10-26 17:58:25 +0000238 result = seq;
239 }
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000240 else {
Tim Peters0e57abf2001-05-02 07:39:38 +0000241 result = PyList_New(len);
242 if (result == NULL)
243 goto Fail_it;
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000244 }
Guido van Rossum12d12c51993-10-26 17:58:25 +0000245
Tim Peters0e57abf2001-05-02 07:39:38 +0000246 /* Build the result list. */
Tim Petersf4848da2001-05-05 00:14:56 +0000247 j = 0;
248 for (;;) {
Guido van Rossumc7903a12002-08-16 07:04:56 +0000249 PyObject *item;
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000250 int ok;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000251
Tim Peters0e57abf2001-05-02 07:39:38 +0000252 item = PyIter_Next(it);
253 if (item == NULL) {
Tim Petersf4848da2001-05-05 00:14:56 +0000254 if (PyErr_Occurred())
255 goto Fail_result_it;
Tim Peters0e57abf2001-05-02 07:39:38 +0000256 break;
Guido van Rossum2d951851994-08-29 12:52:16 +0000257 }
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000258
Neil Schemenauer68973552003-08-14 20:37:34 +0000259 if (func == (PyObject *)&PyBool_Type || func == Py_None) {
Guido van Rossumc7903a12002-08-16 07:04:56 +0000260 ok = PyObject_IsTrue(item);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000261 }
262 else {
Guido van Rossumc7903a12002-08-16 07:04:56 +0000263 PyObject *good;
264 PyTuple_SET_ITEM(arg, 0, item);
265 good = PyObject_Call(func, arg, NULL);
266 PyTuple_SET_ITEM(arg, 0, NULL);
Guido van Rossum58b68731995-01-10 17:40:55 +0000267 if (good == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000268 Py_DECREF(item);
Tim Peters0e57abf2001-05-02 07:39:38 +0000269 goto Fail_result_it;
Guido van Rossum58b68731995-01-10 17:40:55 +0000270 }
Guido van Rossumc7903a12002-08-16 07:04:56 +0000271 ok = PyObject_IsTrue(good);
272 Py_DECREF(good);
Guido van Rossum12d12c51993-10-26 17:58:25 +0000273 }
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000274 if (ok) {
Tim Peters0e57abf2001-05-02 07:39:38 +0000275 if (j < len)
276 PyList_SET_ITEM(result, j, item);
Guido van Rossum2d951851994-08-29 12:52:16 +0000277 else {
Barry Warsawfa77e091999-01-28 18:49:12 +0000278 int status = PyList_Append(result, item);
Barry Warsawfa77e091999-01-28 18:49:12 +0000279 Py_DECREF(item);
280 if (status < 0)
Tim Peters0e57abf2001-05-02 07:39:38 +0000281 goto Fail_result_it;
Guido van Rossum2d951851994-08-29 12:52:16 +0000282 }
Tim Peters0e57abf2001-05-02 07:39:38 +0000283 ++j;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000284 }
Tim Peters0e57abf2001-05-02 07:39:38 +0000285 else
286 Py_DECREF(item);
Guido van Rossum12d12c51993-10-26 17:58:25 +0000287 }
288
Guido van Rossum12d12c51993-10-26 17:58:25 +0000289
Tim Peters0e57abf2001-05-02 07:39:38 +0000290 /* Cut back result list if len is too big. */
Guido van Rossum79f25d91997-04-29 20:08:16 +0000291 if (j < len && PyList_SetSlice(result, j, len, NULL) < 0)
Tim Peters0e57abf2001-05-02 07:39:38 +0000292 goto Fail_result_it;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000293
Tim Peters3c6b1482001-05-21 08:07:05 +0000294 Py_DECREF(it);
Guido van Rossumc7903a12002-08-16 07:04:56 +0000295 Py_DECREF(arg);
Guido van Rossum12d12c51993-10-26 17:58:25 +0000296 return result;
297
Tim Peters0e57abf2001-05-02 07:39:38 +0000298Fail_result_it:
Guido van Rossum79f25d91997-04-29 20:08:16 +0000299 Py_DECREF(result);
Tim Peters0e57abf2001-05-02 07:39:38 +0000300Fail_it:
301 Py_DECREF(it);
Guido van Rossumc7903a12002-08-16 07:04:56 +0000302Fail_arg:
303 Py_DECREF(arg);
Guido van Rossum12d12c51993-10-26 17:58:25 +0000304 return NULL;
305}
306
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000307PyDoc_STRVAR(filter_doc,
Tim Petersd50e5442002-03-09 00:06:26 +0000308"filter(function or None, sequence) -> list, tuple, or string\n"
309"\n"
310"Return those items of sequence for which function(item) is true. If\n"
311"function is None, return the items that are true. If sequence is a tuple\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000312"or string, return the same type, else return a list.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000313
Guido van Rossum79f25d91997-04-29 20:08:16 +0000314static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000315builtin_chr(PyObject *self, PyObject *args)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000316{
317 long x;
318 char s[1];
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000319
Guido van Rossum79f25d91997-04-29 20:08:16 +0000320 if (!PyArg_ParseTuple(args, "l:chr", &x))
Guido van Rossum3f5da241990-12-20 15:06:42 +0000321 return NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000322 if (x < 0 || x >= 256) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000323 PyErr_SetString(PyExc_ValueError,
324 "chr() arg not in range(256)");
Guido van Rossum3f5da241990-12-20 15:06:42 +0000325 return NULL;
326 }
Guido van Rossum6bf62da1997-04-11 20:37:35 +0000327 s[0] = (char)x;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000328 return PyString_FromStringAndSize(s, 1);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000329}
330
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000331PyDoc_STRVAR(chr_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000332"chr(i) -> character\n\
333\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000334Return a string of one character with ordinal i; 0 <= i < 256.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000335
336
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000337#ifdef Py_USING_UNICODE
Guido van Rossum79f25d91997-04-29 20:08:16 +0000338static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000339builtin_unichr(PyObject *self, PyObject *args)
Guido van Rossum09095f32000-03-10 23:00:52 +0000340{
341 long x;
Guido van Rossum09095f32000-03-10 23:00:52 +0000342
343 if (!PyArg_ParseTuple(args, "l:unichr", &x))
344 return NULL;
Fredrik Lundh0dcf67e2001-06-26 20:01:56 +0000345
Marc-André Lemburgcc8764c2002-08-11 12:23:04 +0000346 return PyUnicode_FromOrdinal(x);
Guido van Rossum09095f32000-03-10 23:00:52 +0000347}
348
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000349PyDoc_STRVAR(unichr_doc,
Fred Drake078b24f2000-04-13 02:42:50 +0000350"unichr(i) -> Unicode character\n\
Guido van Rossum09095f32000-03-10 23:00:52 +0000351\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000352Return a Unicode string of one character with ordinal i; 0 <= i <= 0x10ffff.");
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000353#endif
Guido van Rossum09095f32000-03-10 23:00:52 +0000354
355
356static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000357builtin_cmp(PyObject *self, PyObject *args)
Guido van Rossuma9e7dc11992-10-18 18:53:57 +0000358{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000359 PyObject *a, *b;
Guido van Rossum09df08a1998-05-22 00:51:39 +0000360 int c;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000361
Raymond Hettingerea3fdf42002-12-29 16:33:45 +0000362 if (!PyArg_UnpackTuple(args, "cmp", 2, 2, &a, &b))
Guido van Rossuma9e7dc11992-10-18 18:53:57 +0000363 return NULL;
Guido van Rossum09df08a1998-05-22 00:51:39 +0000364 if (PyObject_Cmp(a, b, &c) < 0)
Guido van Rossumc8b6df91997-05-23 00:06:51 +0000365 return NULL;
Guido van Rossum09df08a1998-05-22 00:51:39 +0000366 return PyInt_FromLong((long)c);
Guido van Rossuma9e7dc11992-10-18 18:53:57 +0000367}
368
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000369PyDoc_STRVAR(cmp_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000370"cmp(x, y) -> integer\n\
371\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000372Return negative if x<y, zero if x==y, positive if x>y.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000373
374
Guido van Rossum79f25d91997-04-29 20:08:16 +0000375static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000376builtin_coerce(PyObject *self, PyObject *args)
Guido van Rossum6a00cd81995-01-07 12:39:01 +0000377{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000378 PyObject *v, *w;
379 PyObject *res;
Guido van Rossum5524a591995-01-10 15:26:20 +0000380
Raymond Hettingerea3fdf42002-12-29 16:33:45 +0000381 if (!PyArg_UnpackTuple(args, "coerce", 2, 2, &v, &w))
Guido van Rossum5524a591995-01-10 15:26:20 +0000382 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000383 if (PyNumber_Coerce(&v, &w) < 0)
Guido van Rossum04691fc1992-08-12 15:35:34 +0000384 return NULL;
Raymond Hettinger8ae46892003-10-12 19:09:37 +0000385 res = PyTuple_Pack(2, v, w);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000386 Py_DECREF(v);
387 Py_DECREF(w);
Guido van Rossum04691fc1992-08-12 15:35:34 +0000388 return res;
389}
390
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000391PyDoc_STRVAR(coerce_doc,
Martin v. Löwis8d494f32004-08-25 10:42:41 +0000392"coerce(x, y) -> (x1, y1)\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000393\n\
Martin v. Löwis8d494f32004-08-25 10:42:41 +0000394Return a tuple consisting of the two numeric arguments converted to\n\
395a common type, using the same rules as used by arithmetic operations.\n\
396If coercion is not possible, raise TypeError.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000397
Guido van Rossum79f25d91997-04-29 20:08:16 +0000398static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000399builtin_compile(PyObject *self, PyObject *args)
Guido van Rossum5b722181993-03-30 17:46:03 +0000400{
401 char *str;
402 char *filename;
403 char *startstr;
404 int start;
Tim Peters6cd6a822001-08-17 22:11:27 +0000405 int dont_inherit = 0;
406 int supplied_flags = 0;
Tim Peters5ba58662001-07-16 02:29:45 +0000407 PyCompilerFlags cf;
Just van Rossum3aaf42c2003-02-10 08:21:10 +0000408 PyObject *result, *cmd, *tmp = NULL;
Just van Rossumb9b8e9c2003-02-10 09:22:01 +0000409 int length;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000410
Just van Rossum3aaf42c2003-02-10 08:21:10 +0000411 if (!PyArg_ParseTuple(args, "Oss|ii:compile", &cmd, &filename,
Tim Peters6cd6a822001-08-17 22:11:27 +0000412 &startstr, &supplied_flags, &dont_inherit))
Guido van Rossum5b722181993-03-30 17:46:03 +0000413 return NULL;
Tim Peters6cd6a822001-08-17 22:11:27 +0000414
Just van Rossum3aaf42c2003-02-10 08:21:10 +0000415 cf.cf_flags = supplied_flags;
416
417#ifdef Py_USING_UNICODE
418 if (PyUnicode_Check(cmd)) {
419 tmp = PyUnicode_AsUTF8String(cmd);
420 if (tmp == NULL)
421 return NULL;
422 cmd = tmp;
423 cf.cf_flags |= PyCF_SOURCE_IS_UTF8;
424 }
425#endif
Just van Rossumb9b8e9c2003-02-10 09:22:01 +0000426 if (PyObject_AsReadBuffer(cmd, (const void **)&str, &length))
427 return NULL;
Tim Peters2c646c92003-02-10 14:48:29 +0000428 if ((size_t)length != strlen(str)) {
Just van Rossum3aaf42c2003-02-10 08:21:10 +0000429 PyErr_SetString(PyExc_TypeError,
Alex Martellia9b9c9f2003-04-23 13:34:35 +0000430 "compile() expected string without null bytes");
Just van Rossum3aaf42c2003-02-10 08:21:10 +0000431 return NULL;
432 }
433
Guido van Rossum5b722181993-03-30 17:46:03 +0000434 if (strcmp(startstr, "exec") == 0)
Guido van Rossumb05a5c71997-05-07 17:46:13 +0000435 start = Py_file_input;
Guido van Rossum5b722181993-03-30 17:46:03 +0000436 else if (strcmp(startstr, "eval") == 0)
Guido van Rossumb05a5c71997-05-07 17:46:13 +0000437 start = Py_eval_input;
Guido van Rossum872537c1995-07-07 22:43:42 +0000438 else if (strcmp(startstr, "single") == 0)
Guido van Rossumb05a5c71997-05-07 17:46:13 +0000439 start = Py_single_input;
Guido van Rossum5b722181993-03-30 17:46:03 +0000440 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000441 PyErr_SetString(PyExc_ValueError,
Fred Drake661ea262000-10-24 19:57:45 +0000442 "compile() arg 3 must be 'exec' or 'eval' or 'single'");
Guido van Rossum5b722181993-03-30 17:46:03 +0000443 return NULL;
444 }
Tim Peters6cd6a822001-08-17 22:11:27 +0000445
Guido van Rossum4b499dd32003-02-13 22:07:59 +0000446 if (supplied_flags &
447 ~(PyCF_MASK | PyCF_MASK_OBSOLETE | PyCF_DONT_IMPLY_DEDENT))
448 {
Tim Peters6cd6a822001-08-17 22:11:27 +0000449 PyErr_SetString(PyExc_ValueError,
450 "compile(): unrecognised flags");
451 return NULL;
452 }
453 /* XXX Warn if (supplied_flags & PyCF_MASK_OBSOLETE) != 0? */
454
Tim Peters6cd6a822001-08-17 22:11:27 +0000455 if (!dont_inherit) {
456 PyEval_MergeCompilerFlags(&cf);
457 }
Just van Rossum3aaf42c2003-02-10 08:21:10 +0000458 result = Py_CompileStringFlags(str, filename, start, &cf);
459 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)) {
532 PyErr_SetString(PyExc_TypeError, PyMapping_Check(globals) ?
533 "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
Guido van Rossum79f25d91997-04-29 20:08:16 +0000545 if (PyDict_GetItemString(globals, "__builtins__") == NULL) {
546 if (PyDict_SetItemString(globals, "__builtins__",
547 PyEval_GetBuiltins()) != 0)
Guido van Rossum6135a871995-01-09 17:53:26 +0000548 return NULL;
549 }
Tim Peters9fa96be2001-08-17 23:04:59 +0000550
Jeremy Hylton15c1c4f2001-07-30 21:50:55 +0000551 if (PyCode_Check(cmd)) {
Jeremy Hylton733c8932001-12-13 19:51:56 +0000552 if (PyCode_GetNumFree((PyCodeObject *)cmd) > 0) {
Jeremy Hylton15c1c4f2001-07-30 21:50:55 +0000553 PyErr_SetString(PyExc_TypeError,
554 "code object passed to eval() may not contain free variables");
555 return NULL;
556 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000557 return PyEval_EvalCode((PyCodeObject *) cmd, globals, locals);
Jeremy Hylton15c1c4f2001-07-30 21:50:55 +0000558 }
Tim Peters9fa96be2001-08-17 23:04:59 +0000559
Marc-André Lemburgd1ba4432000-09-19 21:04:18 +0000560 if (!PyString_Check(cmd) &&
561 !PyUnicode_Check(cmd)) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000562 PyErr_SetString(PyExc_TypeError,
Fred Drake661ea262000-10-24 19:57:45 +0000563 "eval() arg 1 must be a string or code object");
Guido van Rossum94390a41992-08-14 15:14:30 +0000564 return NULL;
565 }
Just van Rossum3aaf42c2003-02-10 08:21:10 +0000566 cf.cf_flags = 0;
567
568#ifdef Py_USING_UNICODE
569 if (PyUnicode_Check(cmd)) {
570 tmp = PyUnicode_AsUTF8String(cmd);
571 if (tmp == NULL)
572 return NULL;
573 cmd = tmp;
574 cf.cf_flags |= PyCF_SOURCE_IS_UTF8;
575 }
576#endif
Marc-André Lemburgd1ba4432000-09-19 21:04:18 +0000577 if (PyString_AsStringAndSize(cmd, &str, NULL))
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000578 return NULL;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000579 while (*str == ' ' || *str == '\t')
580 str++;
Tim Peters9fa96be2001-08-17 23:04:59 +0000581
Tim Peters9fa96be2001-08-17 23:04:59 +0000582 (void)PyEval_MergeCompilerFlags(&cf);
Just van Rossum3aaf42c2003-02-10 08:21:10 +0000583 result = PyRun_StringFlags(str, Py_eval_input, globals, locals, &cf);
584 Py_XDECREF(tmp);
585 return result;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000586}
587
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000588PyDoc_STRVAR(eval_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000589"eval(source[, globals[, locals]]) -> value\n\
590\n\
591Evaluate the source in the context of globals and locals.\n\
592The source may be a string representing a Python expression\n\
593or a code object as returned by compile().\n\
Raymond Hettinger214b1c32004-07-02 06:41:07 +0000594The globals must be a dictionary and locals can be any mappping,\n\
595defaulting to the current globals and locals.\n\
596If only globals is given, locals defaults to it.\n");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000597
598
Guido van Rossum79f25d91997-04-29 20:08:16 +0000599static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000600builtin_execfile(PyObject *self, PyObject *args)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000601{
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000602 char *filename;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000603 PyObject *globals = Py_None, *locals = Py_None;
604 PyObject *res;
Guido van Rossumb3a639e2001-09-05 13:37:47 +0000605 FILE* fp = NULL;
Tim Peters5ba58662001-07-16 02:29:45 +0000606 PyCompilerFlags cf;
Martin v. Löwis6b3a2c42001-08-08 05:30:36 +0000607 int exists;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000608
Raymond Hettinger66bd2332004-08-02 08:30:07 +0000609 if (!PyArg_ParseTuple(args, "s|O!O:execfile",
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000610 &filename,
Guido van Rossum79f25d91997-04-29 20:08:16 +0000611 &PyDict_Type, &globals,
Raymond Hettinger66bd2332004-08-02 08:30:07 +0000612 &locals))
Guido van Rossum0f61f8a1992-02-25 18:55:05 +0000613 return NULL;
Raymond Hettinger66bd2332004-08-02 08:30:07 +0000614 if (locals != Py_None && !PyMapping_Check(locals)) {
615 PyErr_SetString(PyExc_TypeError, "locals must be a mapping");
616 return NULL;
617 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000618 if (globals == Py_None) {
619 globals = PyEval_GetGlobals();
620 if (locals == Py_None)
621 locals = PyEval_GetLocals();
Guido van Rossum6135a871995-01-09 17:53:26 +0000622 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000623 else if (locals == Py_None)
Guido van Rossum6135a871995-01-09 17:53:26 +0000624 locals = globals;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000625 if (PyDict_GetItemString(globals, "__builtins__") == NULL) {
626 if (PyDict_SetItemString(globals, "__builtins__",
627 PyEval_GetBuiltins()) != 0)
Guido van Rossum6135a871995-01-09 17:53:26 +0000628 return NULL;
629 }
Martin v. Löwis6b3a2c42001-08-08 05:30:36 +0000630
631 exists = 0;
632 /* Test for existence or directory. */
Martin v. Löwis3484a182002-03-09 12:07:51 +0000633#if defined(PLAN9)
634 {
635 Dir *d;
636
637 if ((d = dirstat(filename))!=nil) {
638 if(d->mode & DMDIR)
639 werrstr("is a directory");
640 else
641 exists = 1;
642 free(d);
643 }
Martin v. Löwis6b3a2c42001-08-08 05:30:36 +0000644 }
Martin v. Löwis3484a182002-03-09 12:07:51 +0000645#elif defined(RISCOS)
Guido van Rossume2ae77b2001-10-24 20:42:55 +0000646 if (object_exists(filename)) {
647 if (isdir(filename))
648 errno = EISDIR;
649 else
650 exists = 1;
651 }
Martin v. Löwis3484a182002-03-09 12:07:51 +0000652#else /* standard Posix */
653 {
654 struct stat s;
655 if (stat(filename, &s) == 0) {
656 if (S_ISDIR(s.st_mode))
Andrew MacIntyreda4d6cb2004-03-29 11:53:38 +0000657# if defined(PYOS_OS2) && defined(PYCC_VACPP)
Martin v. Löwis3484a182002-03-09 12:07:51 +0000658 errno = EOS2ERR;
659# else
660 errno = EISDIR;
661# endif
662 else
663 exists = 1;
664 }
665 }
666#endif
Martin v. Löwis6b3a2c42001-08-08 05:30:36 +0000667
668 if (exists) {
669 Py_BEGIN_ALLOW_THREADS
Jack Jansen7b8c7542002-04-14 20:12:41 +0000670 fp = fopen(filename, "r" PY_STDIOTEXTMODE);
Martin v. Löwis6b3a2c42001-08-08 05:30:36 +0000671 Py_END_ALLOW_THREADS
672
673 if (fp == NULL) {
674 exists = 0;
675 }
676 }
677
678 if (!exists) {
Peter Schneider-Kamp4c013422002-08-27 16:58:00 +0000679 PyErr_SetFromErrnoWithFilename(PyExc_IOError, filename);
Guido van Rossum0f61f8a1992-02-25 18:55:05 +0000680 return NULL;
681 }
Tim Peters5ba58662001-07-16 02:29:45 +0000682 cf.cf_flags = 0;
683 if (PyEval_MergeCompilerFlags(&cf))
Tim Peters748b8bb2001-04-28 08:20:22 +0000684 res = PyRun_FileExFlags(fp, filename, Py_file_input, globals,
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000685 locals, 1, &cf);
Tim Peters5ba58662001-07-16 02:29:45 +0000686 else
Tim Peters748b8bb2001-04-28 08:20:22 +0000687 res = PyRun_FileEx(fp, filename, Py_file_input, globals,
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000688 locals, 1);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000689 return res;
Guido van Rossum0f61f8a1992-02-25 18:55:05 +0000690}
691
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000692PyDoc_STRVAR(execfile_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000693"execfile(filename[, globals[, locals]])\n\
694\n\
695Read and execute a Python script from a file.\n\
696The globals and locals are dictionaries, defaulting to the current\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000697globals and locals. If only globals is given, locals defaults to it.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000698
699
Guido van Rossum79f25d91997-04-29 20:08:16 +0000700static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000701builtin_getattr(PyObject *self, PyObject *args)
Guido van Rossum33894be1992-01-27 16:53:09 +0000702{
Guido van Rossum950ff291998-06-29 13:38:57 +0000703 PyObject *v, *result, *dflt = NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000704 PyObject *name;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000705
Raymond Hettingerea3fdf42002-12-29 16:33:45 +0000706 if (!PyArg_UnpackTuple(args, "getattr", 2, 3, &v, &name, &dflt))
Guido van Rossum33894be1992-01-27 16:53:09 +0000707 return NULL;
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000708#ifdef Py_USING_UNICODE
Jeremy Hylton0eb11152001-07-30 22:39:31 +0000709 if (PyUnicode_Check(name)) {
710 name = _PyUnicode_AsDefaultEncodedString(name, NULL);
711 if (name == NULL)
712 return NULL;
713 }
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000714#endif
Jeremy Hylton0eb11152001-07-30 22:39:31 +0000715
716 if (!PyString_Check(name)) {
717 PyErr_SetString(PyExc_TypeError,
Alex Martellia9b9c9f2003-04-23 13:34:35 +0000718 "getattr(): attribute name must be string");
Jeremy Hylton0eb11152001-07-30 22:39:31 +0000719 return NULL;
720 }
Guido van Rossum950ff291998-06-29 13:38:57 +0000721 result = PyObject_GetAttr(v, name);
Guido van Rossumd8923572001-10-16 21:31:32 +0000722 if (result == NULL && dflt != NULL &&
723 PyErr_ExceptionMatches(PyExc_AttributeError))
724 {
Guido van Rossum950ff291998-06-29 13:38:57 +0000725 PyErr_Clear();
726 Py_INCREF(dflt);
727 result = dflt;
728 }
729 return result;
Guido van Rossum9bfef441993-03-29 10:43:31 +0000730}
731
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000732PyDoc_STRVAR(getattr_doc,
Guido van Rossum950ff291998-06-29 13:38:57 +0000733"getattr(object, name[, default]) -> value\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000734\n\
Guido van Rossum950ff291998-06-29 13:38:57 +0000735Get a named attribute from an object; getattr(x, 'y') is equivalent to x.y.\n\
736When a default argument is given, it is returned when the attribute doesn't\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000737exist; without it, an exception is raised in that case.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000738
739
Guido van Rossum79f25d91997-04-29 20:08:16 +0000740static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000741builtin_globals(PyObject *self)
Guido van Rossum872537c1995-07-07 22:43:42 +0000742{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000743 PyObject *d;
Guido van Rossum872537c1995-07-07 22:43:42 +0000744
Guido van Rossum79f25d91997-04-29 20:08:16 +0000745 d = PyEval_GetGlobals();
746 Py_INCREF(d);
Guido van Rossum872537c1995-07-07 22:43:42 +0000747 return d;
748}
749
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000750PyDoc_STRVAR(globals_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000751"globals() -> dictionary\n\
752\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000753Return the dictionary containing the current scope's global variables.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000754
755
Guido van Rossum79f25d91997-04-29 20:08:16 +0000756static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000757builtin_hasattr(PyObject *self, PyObject *args)
Guido van Rossum9bfef441993-03-29 10:43:31 +0000758{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000759 PyObject *v;
760 PyObject *name;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000761
Raymond Hettingerea3fdf42002-12-29 16:33:45 +0000762 if (!PyArg_UnpackTuple(args, "hasattr", 2, 2, &v, &name))
Guido van Rossum9bfef441993-03-29 10:43:31 +0000763 return NULL;
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000764#ifdef Py_USING_UNICODE
Jeremy Hylton302b54a2001-07-30 22:45:19 +0000765 if (PyUnicode_Check(name)) {
766 name = _PyUnicode_AsDefaultEncodedString(name, NULL);
767 if (name == NULL)
768 return NULL;
769 }
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000770#endif
Jeremy Hylton302b54a2001-07-30 22:45:19 +0000771
772 if (!PyString_Check(name)) {
773 PyErr_SetString(PyExc_TypeError,
Alex Martellia9b9c9f2003-04-23 13:34:35 +0000774 "hasattr(): attribute name must be string");
Jeremy Hylton302b54a2001-07-30 22:45:19 +0000775 return NULL;
776 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000777 v = PyObject_GetAttr(v, name);
Guido van Rossum9bfef441993-03-29 10:43:31 +0000778 if (v == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000779 PyErr_Clear();
Guido van Rossum09df08a1998-05-22 00:51:39 +0000780 Py_INCREF(Py_False);
781 return Py_False;
Guido van Rossum9bfef441993-03-29 10:43:31 +0000782 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000783 Py_DECREF(v);
Guido van Rossum09df08a1998-05-22 00:51:39 +0000784 Py_INCREF(Py_True);
785 return Py_True;
Guido van Rossum33894be1992-01-27 16:53:09 +0000786}
787
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000788PyDoc_STRVAR(hasattr_doc,
Guido van Rossum77f6a652002-04-03 22:41:51 +0000789"hasattr(object, name) -> bool\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000790\n\
791Return whether the object has an attribute with the given name.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000792(This is done by calling getattr(object, name) and catching exceptions.)");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000793
794
Guido van Rossum79f25d91997-04-29 20:08:16 +0000795static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000796builtin_id(PyObject *self, PyObject *v)
Guido van Rossum5b722181993-03-30 17:46:03 +0000797{
Guido van Rossum106f2da2000-06-28 21:12:25 +0000798 return PyLong_FromVoidPtr(v);
Guido van Rossum5b722181993-03-30 17:46:03 +0000799}
800
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000801PyDoc_STRVAR(id_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000802"id(object) -> integer\n\
803\n\
804Return the identity of an object. This is guaranteed to be unique among\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000805simultaneously existing objects. (Hint: it's the object's memory address.)");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000806
807
Guido van Rossum79f25d91997-04-29 20:08:16 +0000808static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000809builtin_map(PyObject *self, PyObject *args)
Guido van Rossum12d12c51993-10-26 17:58:25 +0000810{
811 typedef struct {
Tim Peters4e9afdc2001-05-03 23:54:49 +0000812 PyObject *it; /* the iterator object */
813 int saw_StopIteration; /* bool: did the iterator end? */
Guido van Rossum12d12c51993-10-26 17:58:25 +0000814 } sequence;
815
Guido van Rossum79f25d91997-04-29 20:08:16 +0000816 PyObject *func, *result;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000817 sequence *seqs = NULL, *sqp;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000818 int n, len;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000819 register int i, j;
820
Guido van Rossum79f25d91997-04-29 20:08:16 +0000821 n = PyTuple_Size(args);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000822 if (n < 2) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000823 PyErr_SetString(PyExc_TypeError,
824 "map() requires at least two args");
Guido van Rossum12d12c51993-10-26 17:58:25 +0000825 return NULL;
826 }
827
Guido van Rossum79f25d91997-04-29 20:08:16 +0000828 func = PyTuple_GetItem(args, 0);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000829 n--;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000830
Guido van Rossumfa4ac711998-07-10 17:37:30 +0000831 if (func == Py_None && n == 1) {
832 /* map(None, S) is the same as list(S). */
833 return PySequence_List(PyTuple_GetItem(args, 1));
834 }
835
Tim Peters4e9afdc2001-05-03 23:54:49 +0000836 /* Get space for sequence descriptors. Must NULL out the iterator
837 * pointers so that jumping to Fail_2 later doesn't see trash.
838 */
Guido van Rossum79f25d91997-04-29 20:08:16 +0000839 if ((seqs = PyMem_NEW(sequence, n)) == NULL) {
840 PyErr_NoMemory();
Tim Peters4e9afdc2001-05-03 23:54:49 +0000841 return NULL;
842 }
843 for (i = 0; i < n; ++i) {
844 seqs[i].it = (PyObject*)NULL;
845 seqs[i].saw_StopIteration = 0;
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000846 }
Guido van Rossum12d12c51993-10-26 17:58:25 +0000847
Tim Peters4e9afdc2001-05-03 23:54:49 +0000848 /* Do a first pass to obtain iterators for the arguments, and set len
849 * to the largest of their lengths.
Tim Peters748b8bb2001-04-28 08:20:22 +0000850 */
Tim Peters4e9afdc2001-05-03 23:54:49 +0000851 len = 0;
852 for (i = 0, sqp = seqs; i < n; ++i, ++sqp) {
853 PyObject *curseq;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000854 int curlen;
Guido van Rossumad991772001-01-12 16:03:05 +0000855
Tim Peters4e9afdc2001-05-03 23:54:49 +0000856 /* Get iterator. */
857 curseq = PyTuple_GetItem(args, i+1);
858 sqp->it = PyObject_GetIter(curseq);
859 if (sqp->it == NULL) {
Guido van Rossum12d12c51993-10-26 17:58:25 +0000860 static char errmsg[] =
Tim Peters4e9afdc2001-05-03 23:54:49 +0000861 "argument %d to map() must support iteration";
Guido van Rossum15e33a41997-04-30 19:00:27 +0000862 char errbuf[sizeof(errmsg) + 25];
Jeremy Hylton518ab1c2001-11-28 20:42:20 +0000863 PyOS_snprintf(errbuf, sizeof(errbuf), errmsg, i+2);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000864 PyErr_SetString(PyExc_TypeError, errbuf);
Guido van Rossum12d12c51993-10-26 17:58:25 +0000865 goto Fail_2;
866 }
867
Tim Peters4e9afdc2001-05-03 23:54:49 +0000868 /* Update len. */
Raymond Hettinger77f3c872004-01-04 08:54:44 +0000869 curlen = PyObject_Size(curseq);
870 if (curlen < 0) {
Raymond Hettingera710b332005-08-21 11:03:59 +0000871 if (!PyErr_ExceptionMatches(PyExc_TypeError) &&
872 !PyErr_ExceptionMatches(PyExc_AttributeError)) {
873 goto Fail_2;
874 }
Raymond Hettinger77f3c872004-01-04 08:54:44 +0000875 PyErr_Clear();
Tim Peters4e9afdc2001-05-03 23:54:49 +0000876 curlen = 8; /* arbitrary */
Raymond Hettinger77f3c872004-01-04 08:54:44 +0000877 }
878 if (curlen > len)
879 len = curlen;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000880 }
881
Tim Peters4e9afdc2001-05-03 23:54:49 +0000882 /* Get space for the result list. */
Guido van Rossum79f25d91997-04-29 20:08:16 +0000883 if ((result = (PyObject *) PyList_New(len)) == NULL)
Guido van Rossum12d12c51993-10-26 17:58:25 +0000884 goto Fail_2;
885
Tim Peters4e9afdc2001-05-03 23:54:49 +0000886 /* Iterate over the sequences until all have stopped. */
Guido van Rossum2d951851994-08-29 12:52:16 +0000887 for (i = 0; ; ++i) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000888 PyObject *alist, *item=NULL, *value;
Tim Peters4e9afdc2001-05-03 23:54:49 +0000889 int numactive = 0;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000890
Guido van Rossum79f25d91997-04-29 20:08:16 +0000891 if (func == Py_None && n == 1)
Guido van Rossum32120311995-07-10 13:52:21 +0000892 alist = NULL;
Tim Peters4e9afdc2001-05-03 23:54:49 +0000893 else if ((alist = PyTuple_New(n)) == NULL)
894 goto Fail_1;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000895
896 for (j = 0, sqp = seqs; j < n; ++j, ++sqp) {
Tim Peters4e9afdc2001-05-03 23:54:49 +0000897 if (sqp->saw_StopIteration) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000898 Py_INCREF(Py_None);
899 item = Py_None;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000900 }
Guido van Rossum12d12c51993-10-26 17:58:25 +0000901 else {
Tim Peters4e9afdc2001-05-03 23:54:49 +0000902 item = PyIter_Next(sqp->it);
903 if (item)
904 ++numactive;
905 else {
Tim Peters4e9afdc2001-05-03 23:54:49 +0000906 if (PyErr_Occurred()) {
Tim Petersf4848da2001-05-05 00:14:56 +0000907 Py_XDECREF(alist);
908 goto Fail_1;
Guido van Rossum2d951851994-08-29 12:52:16 +0000909 }
Tim Peters4e9afdc2001-05-03 23:54:49 +0000910 Py_INCREF(Py_None);
911 item = Py_None;
912 sqp->saw_StopIteration = 1;
Guido van Rossum2d951851994-08-29 12:52:16 +0000913 }
Guido van Rossum12d12c51993-10-26 17:58:25 +0000914 }
Tim Peters4e9afdc2001-05-03 23:54:49 +0000915 if (alist)
916 PyTuple_SET_ITEM(alist, j, item);
917 else
Guido van Rossum2d951851994-08-29 12:52:16 +0000918 break;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000919 }
920
Guido van Rossum32120311995-07-10 13:52:21 +0000921 if (!alist)
922 alist = item;
Guido van Rossum2d951851994-08-29 12:52:16 +0000923
Tim Peters4e9afdc2001-05-03 23:54:49 +0000924 if (numactive == 0) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000925 Py_DECREF(alist);
Guido van Rossum2d951851994-08-29 12:52:16 +0000926 break;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000927 }
Guido van Rossum2d951851994-08-29 12:52:16 +0000928
Guido van Rossum79f25d91997-04-29 20:08:16 +0000929 if (func == Py_None)
Guido van Rossum32120311995-07-10 13:52:21 +0000930 value = alist;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000931 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000932 value = PyEval_CallObject(func, alist);
933 Py_DECREF(alist);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000934 if (value == NULL)
935 goto Fail_1;
Guido van Rossum2d951851994-08-29 12:52:16 +0000936 }
937 if (i >= len) {
Barry Warsawfa77e091999-01-28 18:49:12 +0000938 int status = PyList_Append(result, value);
Barry Warsaw21332871999-01-28 04:21:35 +0000939 Py_DECREF(value);
Barry Warsawfa77e091999-01-28 18:49:12 +0000940 if (status < 0)
941 goto Fail_1;
Guido van Rossum2d951851994-08-29 12:52:16 +0000942 }
Tim Peters4e9afdc2001-05-03 23:54:49 +0000943 else if (PyList_SetItem(result, i, value) < 0)
944 goto Fail_1;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000945 }
946
Guido van Rossumfa4ac711998-07-10 17:37:30 +0000947 if (i < len && PyList_SetSlice(result, i, len, NULL) < 0)
948 goto Fail_1;
949
Tim Peters4e9afdc2001-05-03 23:54:49 +0000950 goto Succeed;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000951
Guido van Rossum12d12c51993-10-26 17:58:25 +0000952Fail_1:
Guido van Rossum79f25d91997-04-29 20:08:16 +0000953 Py_DECREF(result);
Guido van Rossum12d12c51993-10-26 17:58:25 +0000954Fail_2:
Tim Peters4e9afdc2001-05-03 23:54:49 +0000955 result = NULL;
956Succeed:
957 assert(seqs);
958 for (i = 0; i < n; ++i)
959 Py_XDECREF(seqs[i].it);
960 PyMem_DEL(seqs);
961 return result;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000962}
963
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000964PyDoc_STRVAR(map_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000965"map(function, sequence[, sequence, ...]) -> list\n\
966\n\
967Return a list of the results of applying the function to the items of\n\
968the argument sequence(s). If more than one sequence is given, the\n\
969function is called with an argument list consisting of the corresponding\n\
970item of each sequence, substituting None for missing values when not all\n\
971sequences have the same length. If the function is None, return a list of\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000972the items of the sequence (or a list of tuples if more than one sequence).");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000973
974
Guido van Rossum79f25d91997-04-29 20:08:16 +0000975static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000976builtin_setattr(PyObject *self, PyObject *args)
Guido van Rossum33894be1992-01-27 16:53:09 +0000977{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000978 PyObject *v;
979 PyObject *name;
980 PyObject *value;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000981
Raymond Hettingerea3fdf42002-12-29 16:33:45 +0000982 if (!PyArg_UnpackTuple(args, "setattr", 3, 3, &v, &name, &value))
Guido van Rossum33894be1992-01-27 16:53:09 +0000983 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000984 if (PyObject_SetAttr(v, name, value) != 0)
Guido van Rossum33894be1992-01-27 16:53:09 +0000985 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000986 Py_INCREF(Py_None);
987 return Py_None;
Guido van Rossum33894be1992-01-27 16:53:09 +0000988}
989
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000990PyDoc_STRVAR(setattr_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000991"setattr(object, name, value)\n\
992\n\
993Set a named attribute on an object; setattr(x, 'y', v) is equivalent to\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000994``x.y = v''.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000995
996
Guido van Rossum79f25d91997-04-29 20:08:16 +0000997static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000998builtin_delattr(PyObject *self, PyObject *args)
Guido van Rossum14144fc1994-08-29 12:53:40 +0000999{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001000 PyObject *v;
1001 PyObject *name;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001002
Raymond Hettingerea3fdf42002-12-29 16:33:45 +00001003 if (!PyArg_UnpackTuple(args, "delattr", 2, 2, &v, &name))
Guido van Rossum14144fc1994-08-29 12:53:40 +00001004 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001005 if (PyObject_SetAttr(v, name, (PyObject *)NULL) != 0)
Guido van Rossum14144fc1994-08-29 12:53:40 +00001006 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001007 Py_INCREF(Py_None);
1008 return Py_None;
Guido van Rossum14144fc1994-08-29 12:53:40 +00001009}
1010
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001011PyDoc_STRVAR(delattr_doc,
Guido van Rossumdf12a591998-11-23 22:13:04 +00001012"delattr(object, name)\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001013\n\
1014Delete a named attribute on an object; delattr(x, 'y') is equivalent to\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001015``del x.y''.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001016
1017
Guido van Rossum79f25d91997-04-29 20:08:16 +00001018static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001019builtin_hash(PyObject *self, PyObject *v)
Guido van Rossum9bfef441993-03-29 10:43:31 +00001020{
Guido van Rossum9bfef441993-03-29 10:43:31 +00001021 long x;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001022
Guido van Rossum79f25d91997-04-29 20:08:16 +00001023 x = PyObject_Hash(v);
Guido van Rossum9bfef441993-03-29 10:43:31 +00001024 if (x == -1)
1025 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001026 return PyInt_FromLong(x);
Guido van Rossum9bfef441993-03-29 10:43:31 +00001027}
1028
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001029PyDoc_STRVAR(hash_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001030"hash(object) -> integer\n\
1031\n\
1032Return a hash value for the object. Two objects with the same value have\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001033the same hash value. The reverse is not necessarily true, but likely.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001034
1035
Guido van Rossum79f25d91997-04-29 20:08:16 +00001036static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001037builtin_hex(PyObject *self, PyObject *v)
Guido van Rossum006bcd41991-10-24 14:54:44 +00001038{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001039 PyNumberMethods *nb;
Neil Schemenauer3a313e32004-07-19 16:29:17 +00001040 PyObject *res;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001041
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001042 if ((nb = v->ob_type->tp_as_number) == NULL ||
Guido van Rossum1899c2e1992-09-12 11:09:23 +00001043 nb->nb_hex == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001044 PyErr_SetString(PyExc_TypeError,
Guido van Rossum1899c2e1992-09-12 11:09:23 +00001045 "hex() argument can't be converted to hex");
1046 return NULL;
Guido van Rossum006bcd41991-10-24 14:54:44 +00001047 }
Neil Schemenauer3a313e32004-07-19 16:29:17 +00001048 res = (*nb->nb_hex)(v);
1049 if (res && !PyString_Check(res)) {
1050 PyErr_Format(PyExc_TypeError,
1051 "__hex__ returned non-string (type %.200s)",
1052 res->ob_type->tp_name);
1053 Py_DECREF(res);
1054 return NULL;
1055 }
1056 return res;
Guido van Rossum006bcd41991-10-24 14:54:44 +00001057}
1058
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001059PyDoc_STRVAR(hex_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001060"hex(number) -> string\n\
1061\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001062Return the hexadecimal representation of an integer or long integer.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001063
1064
Tim Petersdbd9ba62000-07-09 03:09:57 +00001065static PyObject *builtin_raw_input(PyObject *, PyObject *);
Guido van Rossum3165fe61992-09-25 21:59:05 +00001066
Guido van Rossum79f25d91997-04-29 20:08:16 +00001067static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001068builtin_input(PyObject *self, PyObject *args)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001069{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001070 PyObject *line;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001071 char *str;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001072 PyObject *res;
1073 PyObject *globals, *locals;
Hye-Shik Changff83c2b2004-02-02 13:39:01 +00001074 PyCompilerFlags cf;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001075
1076 line = builtin_raw_input(self, args);
Guido van Rossum3165fe61992-09-25 21:59:05 +00001077 if (line == NULL)
1078 return line;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001079 if (!PyArg_Parse(line, "s;embedded '\\0' in input line", &str))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001080 return NULL;
1081 while (*str == ' ' || *str == '\t')
1082 str++;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001083 globals = PyEval_GetGlobals();
1084 locals = PyEval_GetLocals();
1085 if (PyDict_GetItemString(globals, "__builtins__") == NULL) {
1086 if (PyDict_SetItemString(globals, "__builtins__",
1087 PyEval_GetBuiltins()) != 0)
Guido van Rossum6135a871995-01-09 17:53:26 +00001088 return NULL;
1089 }
Hye-Shik Changff83c2b2004-02-02 13:39:01 +00001090 cf.cf_flags = 0;
1091 PyEval_MergeCompilerFlags(&cf);
1092 res = PyRun_StringFlags(str, Py_eval_input, globals, locals, &cf);
Guido van Rossum79f25d91997-04-29 20:08:16 +00001093 Py_DECREF(line);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001094 return res;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001095}
1096
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001097PyDoc_STRVAR(input_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001098"input([prompt]) -> value\n\
1099\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001100Equivalent to eval(raw_input(prompt)).");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001101
1102
Guido van Rossume8811f81997-02-14 15:48:05 +00001103static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001104builtin_intern(PyObject *self, PyObject *args)
Guido van Rossume8811f81997-02-14 15:48:05 +00001105{
1106 PyObject *s;
Guido van Rossum43713e52000-02-29 13:59:29 +00001107 if (!PyArg_ParseTuple(args, "S:intern", &s))
Guido van Rossume8811f81997-02-14 15:48:05 +00001108 return NULL;
Jeremy Hylton4c989dd2004-08-07 19:20:05 +00001109 if (!PyString_CheckExact(s)) {
1110 PyErr_SetString(PyExc_TypeError,
1111 "can't intern subclass of string");
1112 return NULL;
1113 }
Guido van Rossume8811f81997-02-14 15:48:05 +00001114 Py_INCREF(s);
1115 PyString_InternInPlace(&s);
1116 return s;
1117}
1118
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001119PyDoc_STRVAR(intern_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001120"intern(string) -> string\n\
1121\n\
1122``Intern'' the given string. This enters the string in the (global)\n\
1123table of interned strings whose purpose is to speed up dictionary lookups.\n\
1124Return the string itself or the previously interned string object with the\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001125same value.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001126
1127
Guido van Rossum79f25d91997-04-29 20:08:16 +00001128static PyObject *
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001129builtin_iter(PyObject *self, PyObject *args)
1130{
1131 PyObject *v, *w = NULL;
1132
Raymond Hettingerea3fdf42002-12-29 16:33:45 +00001133 if (!PyArg_UnpackTuple(args, "iter", 1, 2, &v, &w))
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001134 return NULL;
1135 if (w == NULL)
1136 return PyObject_GetIter(v);
1137 if (!PyCallable_Check(v)) {
1138 PyErr_SetString(PyExc_TypeError,
1139 "iter(v, w): v must be callable");
1140 return NULL;
1141 }
1142 return PyCallIter_New(v, w);
1143}
1144
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001145PyDoc_STRVAR(iter_doc,
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001146"iter(collection) -> iterator\n\
1147iter(callable, sentinel) -> iterator\n\
1148\n\
1149Get an iterator from an object. In the first form, the argument must\n\
1150supply its own iterator, or be a sequence.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001151In the second form, the callable is called until it returns the sentinel.");
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001152
1153
1154static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001155builtin_len(PyObject *self, PyObject *v)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001156{
Guido van Rossum8ea9f4d1998-06-29 22:26:50 +00001157 long res;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001158
Jeremy Hylton03657cf2000-07-12 13:05:33 +00001159 res = PyObject_Size(v);
Guido van Rossum8ea9f4d1998-06-29 22:26:50 +00001160 if (res < 0 && PyErr_Occurred())
1161 return NULL;
1162 return PyInt_FromLong(res);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001163}
1164
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001165PyDoc_STRVAR(len_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001166"len(object) -> integer\n\
1167\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001168Return the number of items of a sequence or mapping.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001169
1170
Guido van Rossum79f25d91997-04-29 20:08:16 +00001171static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001172builtin_locals(PyObject *self)
Guido van Rossum872537c1995-07-07 22:43:42 +00001173{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001174 PyObject *d;
Guido van Rossum872537c1995-07-07 22:43:42 +00001175
Guido van Rossum79f25d91997-04-29 20:08:16 +00001176 d = PyEval_GetLocals();
1177 Py_INCREF(d);
Guido van Rossum872537c1995-07-07 22:43:42 +00001178 return d;
1179}
1180
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001181PyDoc_STRVAR(locals_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001182"locals() -> dictionary\n\
1183\n\
Raymond Hettinger69bf8f32003-01-04 02:16:22 +00001184Update and return a dictionary containing the current scope's local variables.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001185
1186
Guido van Rossum79f25d91997-04-29 20:08:16 +00001187static PyObject *
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001188min_max(PyObject *args, PyObject *kwds, int op)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001189{
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001190 PyObject *v, *it, *item, *val, *maxitem, *maxval, *keyfunc=NULL;
Martin v. Löwisfd39ad42004-08-12 14:42:37 +00001191 const char *name = op == Py_LT ? "min" : "max";
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001192
Guido van Rossum79f25d91997-04-29 20:08:16 +00001193 if (PyTuple_Size(args) > 1)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001194 v = args;
Martin v. Löwisfd39ad42004-08-12 14:42:37 +00001195 else if (!PyArg_UnpackTuple(args, (char *)name, 1, 1, &v))
Guido van Rossum3f5da241990-12-20 15:06:42 +00001196 return NULL;
Tim Peters67d687a2002-04-29 21:27:32 +00001197
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001198 if (kwds != NULL && PyDict_Check(kwds) && PyDict_Size(kwds)) {
1199 keyfunc = PyDict_GetItemString(kwds, "key");
1200 if (PyDict_Size(kwds)!=1 || keyfunc == NULL) {
1201 PyErr_Format(PyExc_TypeError,
1202 "%s() got an unexpected keyword argument", name);
1203 return NULL;
1204 }
1205 }
1206
Tim Petersc3074532001-05-03 07:00:32 +00001207 it = PyObject_GetIter(v);
1208 if (it == NULL)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001209 return NULL;
Tim Petersc3074532001-05-03 07:00:32 +00001210
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001211 maxitem = NULL; /* the result */
1212 maxval = NULL; /* the value associated with the result */
Brett Cannon9e635cf2004-12-07 00:25:35 +00001213 while (( item = PyIter_Next(it) )) {
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001214 /* get the value from the key function */
1215 if (keyfunc != NULL) {
1216 val = PyObject_CallFunctionObjArgs(keyfunc, item, NULL);
1217 if (val == NULL)
1218 goto Fail_it_item;
1219 }
1220 /* no key function; the value is the item */
1221 else {
1222 val = item;
1223 Py_INCREF(val);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001224 }
Tim Petersc3074532001-05-03 07:00:32 +00001225
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001226 /* maximum value and item are unset; set them */
1227 if (maxval == NULL) {
1228 maxitem = item;
1229 maxval = val;
1230 }
1231 /* maximum value and item are set; update them as necessary */
Guido van Rossum2d951851994-08-29 12:52:16 +00001232 else {
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001233 int cmp = PyObject_RichCompareBool(val, maxval, op);
1234 if (cmp < 0)
1235 goto Fail_it_item_and_val;
1236 else if (cmp > 0) {
1237 Py_DECREF(maxval);
1238 Py_DECREF(maxitem);
1239 maxval = val;
1240 maxitem = item;
Guido van Rossum53451b32001-01-17 15:47:24 +00001241 }
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001242 else {
1243 Py_DECREF(item);
1244 Py_DECREF(val);
Guido van Rossumc8b6df91997-05-23 00:06:51 +00001245 }
Guido van Rossum2d951851994-08-29 12:52:16 +00001246 }
Guido van Rossum3f5da241990-12-20 15:06:42 +00001247 }
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001248 if (PyErr_Occurred())
1249 goto Fail_it;
1250 if (maxval == NULL) {
Martin v. Löwisfd39ad42004-08-12 14:42:37 +00001251 PyErr_Format(PyExc_ValueError,
1252 "%s() arg is an empty sequence", name);
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001253 assert(maxitem == NULL);
1254 }
1255 else
1256 Py_DECREF(maxval);
Tim Petersc3074532001-05-03 07:00:32 +00001257 Py_DECREF(it);
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001258 return maxitem;
1259
1260Fail_it_item_and_val:
1261 Py_DECREF(val);
1262Fail_it_item:
1263 Py_DECREF(item);
1264Fail_it:
1265 Py_XDECREF(maxval);
1266 Py_XDECREF(maxitem);
1267 Py_DECREF(it);
1268 return NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001269}
1270
Guido van Rossum79f25d91997-04-29 20:08:16 +00001271static PyObject *
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001272builtin_min(PyObject *self, PyObject *args, PyObject *kwds)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001273{
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001274 return min_max(args, kwds, Py_LT);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001275}
1276
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001277PyDoc_STRVAR(min_doc,
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001278"min(iterable[, key=func]) -> value\n\
1279min(a, b, c, ...[, key=func]) -> value\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001280\n\
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001281With a single iterable argument, return its smallest item.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001282With two or more arguments, return the smallest argument.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001283
1284
Guido van Rossum79f25d91997-04-29 20:08:16 +00001285static PyObject *
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001286builtin_max(PyObject *self, PyObject *args, PyObject *kwds)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001287{
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001288 return min_max(args, kwds, Py_GT);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001289}
1290
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001291PyDoc_STRVAR(max_doc,
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001292"max(iterable[, key=func]) -> value\n\
1293max(a, b, c, ...[, key=func]) -> value\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001294\n\
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001295With a single iterable argument, return its largest item.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001296With two or more arguments, return the largest argument.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001297
1298
Guido van Rossum79f25d91997-04-29 20:08:16 +00001299static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001300builtin_oct(PyObject *self, PyObject *v)
Guido van Rossum006bcd41991-10-24 14:54:44 +00001301{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001302 PyNumberMethods *nb;
Neil Schemenauer3a313e32004-07-19 16:29:17 +00001303 PyObject *res;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001304
Guido van Rossum1899c2e1992-09-12 11:09:23 +00001305 if (v == NULL || (nb = v->ob_type->tp_as_number) == NULL ||
1306 nb->nb_oct == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001307 PyErr_SetString(PyExc_TypeError,
Guido van Rossum1899c2e1992-09-12 11:09:23 +00001308 "oct() argument can't be converted to oct");
1309 return NULL;
Guido van Rossum006bcd41991-10-24 14:54:44 +00001310 }
Neil Schemenauer3a313e32004-07-19 16:29:17 +00001311 res = (*nb->nb_oct)(v);
1312 if (res && !PyString_Check(res)) {
1313 PyErr_Format(PyExc_TypeError,
1314 "__oct__ returned non-string (type %.200s)",
1315 res->ob_type->tp_name);
1316 Py_DECREF(res);
1317 return NULL;
1318 }
1319 return res;
Guido van Rossum006bcd41991-10-24 14:54:44 +00001320}
1321
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001322PyDoc_STRVAR(oct_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001323"oct(number) -> string\n\
1324\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001325Return the octal representation of an integer or long integer.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001326
1327
Guido van Rossum79f25d91997-04-29 20:08:16 +00001328static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001329builtin_ord(PyObject *self, PyObject* obj)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001330{
Guido van Rossum09095f32000-03-10 23:00:52 +00001331 long ord;
Jeremy Hylton394b54d2000-04-12 21:19:47 +00001332 int size;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001333
Jeremy Hylton394b54d2000-04-12 21:19:47 +00001334 if (PyString_Check(obj)) {
1335 size = PyString_GET_SIZE(obj);
Andrew M. Kuchling34c20cf2000-12-20 14:36:56 +00001336 if (size == 1) {
Jeremy Hylton394b54d2000-04-12 21:19:47 +00001337 ord = (long)((unsigned char)*PyString_AS_STRING(obj));
Andrew M. Kuchling34c20cf2000-12-20 14:36:56 +00001338 return PyInt_FromLong(ord);
1339 }
Martin v. Löwis339d0f72001-08-17 18:39:25 +00001340#ifdef Py_USING_UNICODE
Jeremy Hylton394b54d2000-04-12 21:19:47 +00001341 } else if (PyUnicode_Check(obj)) {
1342 size = PyUnicode_GET_SIZE(obj);
Andrew M. Kuchling34c20cf2000-12-20 14:36:56 +00001343 if (size == 1) {
Jeremy Hylton394b54d2000-04-12 21:19:47 +00001344 ord = (long)*PyUnicode_AS_UNICODE(obj);
Andrew M. Kuchling34c20cf2000-12-20 14:36:56 +00001345 return PyInt_FromLong(ord);
1346 }
Martin v. Löwis339d0f72001-08-17 18:39:25 +00001347#endif
Jeremy Hylton394b54d2000-04-12 21:19:47 +00001348 } else {
1349 PyErr_Format(PyExc_TypeError,
Andrew M. Kuchlingf07aad12000-12-23 14:11:28 +00001350 "ord() expected string of length 1, but " \
Jeremy Hylton394b54d2000-04-12 21:19:47 +00001351 "%.200s found", obj->ob_type->tp_name);
Guido van Rossum09095f32000-03-10 23:00:52 +00001352 return NULL;
1353 }
1354
Guido van Rossumad991772001-01-12 16:03:05 +00001355 PyErr_Format(PyExc_TypeError,
Andrew M. Kuchlingf07aad12000-12-23 14:11:28 +00001356 "ord() expected a character, "
1357 "but string of length %d found",
Jeremy Hylton394b54d2000-04-12 21:19:47 +00001358 size);
1359 return NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001360}
1361
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001362PyDoc_STRVAR(ord_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001363"ord(c) -> integer\n\
1364\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001365Return the integer ordinal of a one-character string.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001366
1367
Guido van Rossum79f25d91997-04-29 20:08:16 +00001368static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001369builtin_pow(PyObject *self, PyObject *args)
Guido van Rossum6a00cd81995-01-07 12:39:01 +00001370{
Guido van Rossum09df08a1998-05-22 00:51:39 +00001371 PyObject *v, *w, *z = Py_None;
Guido van Rossum6a00cd81995-01-07 12:39:01 +00001372
Raymond Hettingerea3fdf42002-12-29 16:33:45 +00001373 if (!PyArg_UnpackTuple(args, "pow", 2, 3, &v, &w, &z))
Guido van Rossum6a00cd81995-01-07 12:39:01 +00001374 return NULL;
Guido van Rossum09df08a1998-05-22 00:51:39 +00001375 return PyNumber_Power(v, w, z);
Guido van Rossumd4905451991-05-05 20:00:36 +00001376}
1377
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001378PyDoc_STRVAR(pow_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001379"pow(x, y[, z]) -> number\n\
1380\n\
1381With two arguments, equivalent to x**y. With three arguments,\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001382equivalent to (x**y) % z, but may be more efficient (e.g. for longs).");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001383
1384
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001385
1386/* Return number of items in range (lo, hi, step), when arguments are
1387 * PyInt or PyLong objects. step > 0 required. Return a value < 0 if
1388 * & only if the true value is too large to fit in a signed long.
1389 * Arguments MUST return 1 with either PyInt_Check() or
1390 * PyLong_Check(). Return -1 when there is an error.
1391 */
1392static long
1393get_len_of_range_longs(PyObject *lo, PyObject *hi, PyObject *step)
1394{
1395 /* -------------------------------------------------------------
1396 Algorithm is equal to that of get_len_of_range(), but it operates
1397 on PyObjects (which are assumed to be PyLong or PyInt objects).
1398 ---------------------------------------------------------------*/
1399 long n;
1400 PyObject *diff = NULL;
1401 PyObject *one = NULL;
1402 PyObject *tmp1 = NULL, *tmp2 = NULL, *tmp3 = NULL;
1403 /* holds sub-expression evaluations */
1404
1405 /* if (lo >= hi), return length of 0. */
1406 if (PyObject_Compare(lo, hi) >= 0)
1407 return 0;
1408
1409 if ((one = PyLong_FromLong(1L)) == NULL)
1410 goto Fail;
1411
1412 if ((tmp1 = PyNumber_Subtract(hi, lo)) == NULL)
1413 goto Fail;
1414
1415 if ((diff = PyNumber_Subtract(tmp1, one)) == NULL)
1416 goto Fail;
1417
1418 if ((tmp2 = PyNumber_FloorDivide(diff, step)) == NULL)
1419 goto Fail;
1420
1421 if ((tmp3 = PyNumber_Add(tmp2, one)) == NULL)
1422 goto Fail;
1423
1424 n = PyLong_AsLong(tmp3);
1425 if (PyErr_Occurred()) { /* Check for Overflow */
1426 PyErr_Clear();
1427 goto Fail;
1428 }
1429
1430 Py_DECREF(tmp3);
1431 Py_DECREF(tmp2);
1432 Py_DECREF(diff);
1433 Py_DECREF(tmp1);
1434 Py_DECREF(one);
1435 return n;
1436
1437 Fail:
1438 Py_XDECREF(tmp3);
1439 Py_XDECREF(tmp2);
1440 Py_XDECREF(diff);
1441 Py_XDECREF(tmp1);
1442 Py_XDECREF(one);
1443 return -1;
1444}
1445
1446/* An extension of builtin_range() that handles the case when PyLong
1447 * arguments are given. */
1448static PyObject *
1449handle_range_longs(PyObject *self, PyObject *args)
1450{
1451 PyObject *ilow;
Tim Peters874e1f72003-04-13 22:13:08 +00001452 PyObject *ihigh = NULL;
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001453 PyObject *istep = NULL;
Tim Peters874e1f72003-04-13 22:13:08 +00001454
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001455 PyObject *curnum = NULL;
1456 PyObject *v = NULL;
1457 long bign;
1458 int i, n;
1459 int cmp_result;
1460
Tim Peters874e1f72003-04-13 22:13:08 +00001461 PyObject *zero = PyLong_FromLong(0);
1462
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001463 if (zero == NULL)
1464 return NULL;
1465
Tim Peters874e1f72003-04-13 22:13:08 +00001466 if (!PyArg_UnpackTuple(args, "range", 1, 3, &ilow, &ihigh, &istep)) {
1467 Py_DECREF(zero);
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001468 return NULL;
1469 }
1470
Tim Peters874e1f72003-04-13 22:13:08 +00001471 /* Figure out which way we were called, supply defaults, and be
1472 * sure to incref everything so that the decrefs at the end
1473 * are correct.
1474 */
1475 assert(ilow != NULL);
1476 if (ihigh == NULL) {
1477 /* only 1 arg -- it's the upper limit */
1478 ihigh = ilow;
1479 ilow = NULL;
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001480 }
Tim Peters874e1f72003-04-13 22:13:08 +00001481 assert(ihigh != NULL);
1482 Py_INCREF(ihigh);
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001483
Tim Peters874e1f72003-04-13 22:13:08 +00001484 /* ihigh correct now; do ilow */
1485 if (ilow == NULL)
1486 ilow = zero;
1487 Py_INCREF(ilow);
1488
1489 /* ilow and ihigh correct now; do istep */
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001490 if (istep == NULL) {
1491 istep = PyLong_FromLong(1L);
1492 if (istep == NULL)
1493 goto Fail;
1494 }
1495 else {
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001496 Py_INCREF(istep);
1497 }
1498
Tim Peters874e1f72003-04-13 22:13:08 +00001499 if (!PyInt_Check(ilow) && !PyLong_Check(ilow)) {
Guido van Rossum28e83e32003-04-15 12:43:26 +00001500 PyErr_Format(PyExc_TypeError,
Alex Martellif471d472003-04-23 13:00:44 +00001501 "range() integer start argument expected, got %s.",
Guido van Rossum817d6c92003-04-14 18:25:04 +00001502 ilow->ob_type->tp_name);
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001503 goto Fail;
1504 }
1505
Tim Peters874e1f72003-04-13 22:13:08 +00001506 if (!PyInt_Check(ihigh) && !PyLong_Check(ihigh)) {
Guido van Rossum28e83e32003-04-15 12:43:26 +00001507 PyErr_Format(PyExc_TypeError,
Alex Martellif471d472003-04-23 13:00:44 +00001508 "range() integer end argument expected, got %s.",
Guido van Rossum817d6c92003-04-14 18:25:04 +00001509 ihigh->ob_type->tp_name);
Tim Peters874e1f72003-04-13 22:13:08 +00001510 goto Fail;
1511 }
1512
1513 if (!PyInt_Check(istep) && !PyLong_Check(istep)) {
Guido van Rossum28e83e32003-04-15 12:43:26 +00001514 PyErr_Format(PyExc_TypeError,
Alex Martellif471d472003-04-23 13:00:44 +00001515 "range() integer step argument expected, got %s.",
Guido van Rossum817d6c92003-04-14 18:25:04 +00001516 istep->ob_type->tp_name);
Tim Peters874e1f72003-04-13 22:13:08 +00001517 goto Fail;
1518 }
1519
1520 if (PyObject_Cmp(istep, zero, &cmp_result) == -1)
1521 goto Fail;
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001522 if (cmp_result == 0) {
1523 PyErr_SetString(PyExc_ValueError,
Alex Martellif471d472003-04-23 13:00:44 +00001524 "range() step argument must not be zero");
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001525 goto Fail;
1526 }
1527
1528 if (cmp_result > 0)
1529 bign = get_len_of_range_longs(ilow, ihigh, istep);
1530 else {
1531 PyObject *neg_istep = PyNumber_Negative(istep);
1532 if (neg_istep == NULL)
1533 goto Fail;
1534 bign = get_len_of_range_longs(ihigh, ilow, neg_istep);
1535 Py_DECREF(neg_istep);
1536 }
1537
1538 n = (int)bign;
1539 if (bign < 0 || (long)n != bign) {
1540 PyErr_SetString(PyExc_OverflowError,
1541 "range() result has too many items");
1542 goto Fail;
1543 }
1544
1545 v = PyList_New(n);
1546 if (v == NULL)
1547 goto Fail;
1548
1549 curnum = ilow;
1550 Py_INCREF(curnum);
1551
1552 for (i = 0; i < n; i++) {
1553 PyObject *w = PyNumber_Long(curnum);
1554 PyObject *tmp_num;
1555 if (w == NULL)
1556 goto Fail;
1557
1558 PyList_SET_ITEM(v, i, w);
1559
1560 tmp_num = PyNumber_Add(curnum, istep);
1561 if (tmp_num == NULL)
1562 goto Fail;
1563
1564 Py_DECREF(curnum);
1565 curnum = tmp_num;
1566 }
Tim Peters874e1f72003-04-13 22:13:08 +00001567 Py_DECREF(ilow);
1568 Py_DECREF(ihigh);
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001569 Py_DECREF(istep);
1570 Py_DECREF(zero);
Tim Peters874e1f72003-04-13 22:13:08 +00001571 Py_DECREF(curnum);
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001572 return v;
1573
1574 Fail:
Tim Peters874e1f72003-04-13 22:13:08 +00001575 Py_DECREF(ilow);
1576 Py_DECREF(ihigh);
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001577 Py_XDECREF(istep);
Tim Peters874e1f72003-04-13 22:13:08 +00001578 Py_DECREF(zero);
1579 Py_XDECREF(curnum);
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001580 Py_XDECREF(v);
1581 return NULL;
1582}
1583
Guido van Rossum124eff01999-02-23 16:11:01 +00001584/* Return number of items in range/xrange (lo, hi, step). step > 0
1585 * required. Return a value < 0 if & only if the true value is too
1586 * large to fit in a signed long.
1587 */
1588static long
Thomas Wouterse28c2962000-07-23 22:21:32 +00001589get_len_of_range(long lo, long hi, long step)
Guido van Rossum124eff01999-02-23 16:11:01 +00001590{
1591 /* -------------------------------------------------------------
1592 If lo >= hi, the range is empty.
1593 Else if n values are in the range, the last one is
1594 lo + (n-1)*step, which must be <= hi-1. Rearranging,
1595 n <= (hi - lo - 1)/step + 1, so taking the floor of the RHS gives
1596 the proper value. Since lo < hi in this case, hi-lo-1 >= 0, so
1597 the RHS is non-negative and so truncation is the same as the
1598 floor. Letting M be the largest positive long, the worst case
1599 for the RHS numerator is hi=M, lo=-M-1, and then
1600 hi-lo-1 = M-(-M-1)-1 = 2*M. Therefore unsigned long has enough
1601 precision to compute the RHS exactly.
1602 ---------------------------------------------------------------*/
1603 long n = 0;
1604 if (lo < hi) {
1605 unsigned long uhi = (unsigned long)hi;
1606 unsigned long ulo = (unsigned long)lo;
1607 unsigned long diff = uhi - ulo - 1;
1608 n = (long)(diff / (unsigned long)step + 1);
1609 }
1610 return n;
1611}
1612
Guido van Rossum79f25d91997-04-29 20:08:16 +00001613static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001614builtin_range(PyObject *self, PyObject *args)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001615{
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001616 long ilow = 0, ihigh = 0, istep = 1;
Guido van Rossum124eff01999-02-23 16:11:01 +00001617 long bign;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001618 int i, n;
Guido van Rossum124eff01999-02-23 16:11:01 +00001619
Guido van Rossum79f25d91997-04-29 20:08:16 +00001620 PyObject *v;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001621
Guido van Rossum79f25d91997-04-29 20:08:16 +00001622 if (PyTuple_Size(args) <= 1) {
1623 if (!PyArg_ParseTuple(args,
Guido van Rossum0865dd91995-01-17 16:30:22 +00001624 "l;range() requires 1-3 int arguments",
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001625 &ihigh)) {
1626 PyErr_Clear();
1627 return handle_range_longs(self, args);
1628 }
Guido van Rossum3f5da241990-12-20 15:06:42 +00001629 }
1630 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001631 if (!PyArg_ParseTuple(args,
Guido van Rossum0865dd91995-01-17 16:30:22 +00001632 "ll|l;range() requires 1-3 int arguments",
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001633 &ilow, &ihigh, &istep)) {
1634 PyErr_Clear();
1635 return handle_range_longs(self, args);
1636 }
Guido van Rossum3f5da241990-12-20 15:06:42 +00001637 }
1638 if (istep == 0) {
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001639 PyErr_SetString(PyExc_ValueError,
Alex Martellif471d472003-04-23 13:00:44 +00001640 "range() step argument must not be zero");
Guido van Rossum3f5da241990-12-20 15:06:42 +00001641 return NULL;
1642 }
Guido van Rossum124eff01999-02-23 16:11:01 +00001643 if (istep > 0)
1644 bign = get_len_of_range(ilow, ihigh, istep);
1645 else
1646 bign = get_len_of_range(ihigh, ilow, -istep);
1647 n = (int)bign;
1648 if (bign < 0 || (long)n != bign) {
Guido van Rossume23cde21999-01-12 05:07:47 +00001649 PyErr_SetString(PyExc_OverflowError,
Fred Drake661ea262000-10-24 19:57:45 +00001650 "range() result has too many items");
Guido van Rossume23cde21999-01-12 05:07:47 +00001651 return NULL;
1652 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00001653 v = PyList_New(n);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001654 if (v == NULL)
1655 return NULL;
1656 for (i = 0; i < n; i++) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001657 PyObject *w = PyInt_FromLong(ilow);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001658 if (w == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001659 Py_DECREF(v);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001660 return NULL;
1661 }
Guido van Rossuma937d141998-04-24 18:22:02 +00001662 PyList_SET_ITEM(v, i, w);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001663 ilow += istep;
1664 }
1665 return v;
1666}
1667
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001668PyDoc_STRVAR(range_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001669"range([start,] stop[, step]) -> list of integers\n\
1670\n\
1671Return a list containing an arithmetic progression of integers.\n\
1672range(i, j) returns [i, i+1, i+2, ..., j-1]; start (!) defaults to 0.\n\
1673When step is given, it specifies the increment (or decrement).\n\
1674For example, range(4) returns [0, 1, 2, 3]. The end point is omitted!\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001675These are exactly the valid indices for a list of 4 elements.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001676
1677
Guido van Rossum79f25d91997-04-29 20:08:16 +00001678static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001679builtin_raw_input(PyObject *self, PyObject *args)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001680{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001681 PyObject *v = NULL;
Martin v. Löwis31d2df52002-08-14 15:46:02 +00001682 PyObject *fin = PySys_GetObject("stdin");
1683 PyObject *fout = PySys_GetObject("stdout");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001684
Raymond Hettingerea3fdf42002-12-29 16:33:45 +00001685 if (!PyArg_UnpackTuple(args, "[raw_]input", 0, 1, &v))
Guido van Rossum3165fe61992-09-25 21:59:05 +00001686 return NULL;
Martin v. Löwis31d2df52002-08-14 15:46:02 +00001687
1688 if (fin == NULL) {
Alex Martellia9b9c9f2003-04-23 13:34:35 +00001689 PyErr_SetString(PyExc_RuntimeError, "[raw_]input: lost sys.stdin");
Martin v. Löwis31d2df52002-08-14 15:46:02 +00001690 return NULL;
1691 }
1692 if (fout == NULL) {
Alex Martellia9b9c9f2003-04-23 13:34:35 +00001693 PyErr_SetString(PyExc_RuntimeError, "[raw_]input: lost sys.stdout");
Martin v. Löwis31d2df52002-08-14 15:46:02 +00001694 return NULL;
1695 }
1696 if (PyFile_SoftSpace(fout, 0)) {
1697 if (PyFile_WriteString(" ", fout) != 0)
1698 return NULL;
1699 }
Michael W. Hudson52db5192004-08-02 13:21:09 +00001700 if (PyFile_Check(fin) && PyFile_Check(fout)
Martin v. Löwis566f6af2002-10-26 14:39:10 +00001701 && isatty(fileno(PyFile_AsFile(fin)))
1702 && isatty(fileno(PyFile_AsFile(fout)))) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001703 PyObject *po;
Guido van Rossum872537c1995-07-07 22:43:42 +00001704 char *prompt;
1705 char *s;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001706 PyObject *result;
Guido van Rossum872537c1995-07-07 22:43:42 +00001707 if (v != NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001708 po = PyObject_Str(v);
Guido van Rossum872537c1995-07-07 22:43:42 +00001709 if (po == NULL)
1710 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001711 prompt = PyString_AsString(po);
Guido van Rossumd9b52081998-06-26 18:25:38 +00001712 if (prompt == NULL)
1713 return NULL;
Guido van Rossum872537c1995-07-07 22:43:42 +00001714 }
1715 else {
1716 po = NULL;
1717 prompt = "";
1718 }
Michael W. Hudson52db5192004-08-02 13:21:09 +00001719 s = PyOS_Readline(PyFile_AsFile(fin), PyFile_AsFile(fout),
Martin v. Löwis566f6af2002-10-26 14:39:10 +00001720 prompt);
Guido van Rossum79f25d91997-04-29 20:08:16 +00001721 Py_XDECREF(po);
Guido van Rossum872537c1995-07-07 22:43:42 +00001722 if (s == NULL) {
Michael W. Hudson30ea2f22004-07-07 17:44:12 +00001723 if (!PyErr_Occurred())
1724 PyErr_SetNone(PyExc_KeyboardInterrupt);
Guido van Rossum872537c1995-07-07 22:43:42 +00001725 return NULL;
1726 }
1727 if (*s == '\0') {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001728 PyErr_SetNone(PyExc_EOFError);
Guido van Rossum872537c1995-07-07 22:43:42 +00001729 result = NULL;
1730 }
1731 else { /* strip trailing '\n' */
Guido van Rossum106f2da2000-06-28 21:12:25 +00001732 size_t len = strlen(s);
1733 if (len > INT_MAX) {
Martin v. Löwis31d2df52002-08-14 15:46:02 +00001734 PyErr_SetString(PyExc_OverflowError,
Alex Martellia9b9c9f2003-04-23 13:34:35 +00001735 "[raw_]input: input too long");
Guido van Rossum106f2da2000-06-28 21:12:25 +00001736 result = NULL;
1737 }
1738 else {
Martin v. Löwis31d2df52002-08-14 15:46:02 +00001739 result = PyString_FromStringAndSize(s,
1740 (int)(len-1));
Guido van Rossum106f2da2000-06-28 21:12:25 +00001741 }
Guido van Rossum872537c1995-07-07 22:43:42 +00001742 }
Guido van Rossumb18618d2000-05-03 23:44:39 +00001743 PyMem_FREE(s);
Guido van Rossum872537c1995-07-07 22:43:42 +00001744 return result;
1745 }
Guido van Rossum90933611991-06-07 16:10:43 +00001746 if (v != NULL) {
Martin v. Löwis31d2df52002-08-14 15:46:02 +00001747 if (PyFile_WriteObject(v, fout, Py_PRINT_RAW) != 0)
Guido van Rossum90933611991-06-07 16:10:43 +00001748 return NULL;
1749 }
Martin v. Löwis31d2df52002-08-14 15:46:02 +00001750 return PyFile_GetLine(fin, -1);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001751}
1752
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001753PyDoc_STRVAR(raw_input_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001754"raw_input([prompt]) -> string\n\
1755\n\
1756Read a string from standard input. The trailing newline is stripped.\n\
1757If the user hits EOF (Unix: Ctl-D, Windows: Ctl-Z+Return), raise EOFError.\n\
1758On Unix, GNU readline is used if enabled. The prompt string, if given,\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001759is printed without a trailing newline before reading.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001760
1761
Guido van Rossum79f25d91997-04-29 20:08:16 +00001762static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001763builtin_reduce(PyObject *self, PyObject *args)
Guido van Rossum12d12c51993-10-26 17:58:25 +00001764{
Tim Peters15d81ef2001-05-04 04:39:21 +00001765 PyObject *seq, *func, *result = NULL, *it;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001766
Raymond Hettingerea3fdf42002-12-29 16:33:45 +00001767 if (!PyArg_UnpackTuple(args, "reduce", 2, 3, &func, &seq, &result))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001768 return NULL;
1769 if (result != NULL)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001770 Py_INCREF(result);
Guido van Rossum12d12c51993-10-26 17:58:25 +00001771
Tim Peters15d81ef2001-05-04 04:39:21 +00001772 it = PyObject_GetIter(seq);
1773 if (it == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001774 PyErr_SetString(PyExc_TypeError,
Tim Peters15d81ef2001-05-04 04:39:21 +00001775 "reduce() arg 2 must support iteration");
1776 Py_XDECREF(result);
Guido van Rossum12d12c51993-10-26 17:58:25 +00001777 return NULL;
1778 }
1779
Guido van Rossum79f25d91997-04-29 20:08:16 +00001780 if ((args = PyTuple_New(2)) == NULL)
Guido van Rossum2d951851994-08-29 12:52:16 +00001781 goto Fail;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001782
Tim Peters15d81ef2001-05-04 04:39:21 +00001783 for (;;) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001784 PyObject *op2;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001785
1786 if (args->ob_refcnt > 1) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001787 Py_DECREF(args);
1788 if ((args = PyTuple_New(2)) == NULL)
Guido van Rossum2d951851994-08-29 12:52:16 +00001789 goto Fail;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001790 }
1791
Tim Peters15d81ef2001-05-04 04:39:21 +00001792 op2 = PyIter_Next(it);
1793 if (op2 == NULL) {
Tim Petersf4848da2001-05-05 00:14:56 +00001794 if (PyErr_Occurred())
1795 goto Fail;
1796 break;
Guido van Rossum2d951851994-08-29 12:52:16 +00001797 }
Guido van Rossum12d12c51993-10-26 17:58:25 +00001798
Guido van Rossum2d951851994-08-29 12:52:16 +00001799 if (result == NULL)
1800 result = op2;
1801 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001802 PyTuple_SetItem(args, 0, result);
1803 PyTuple_SetItem(args, 1, op2);
1804 if ((result = PyEval_CallObject(func, args)) == NULL)
Guido van Rossum2d951851994-08-29 12:52:16 +00001805 goto Fail;
1806 }
Guido van Rossum12d12c51993-10-26 17:58:25 +00001807 }
1808
Guido van Rossum79f25d91997-04-29 20:08:16 +00001809 Py_DECREF(args);
Guido van Rossum12d12c51993-10-26 17:58:25 +00001810
Guido van Rossum2d951851994-08-29 12:52:16 +00001811 if (result == NULL)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001812 PyErr_SetString(PyExc_TypeError,
Fred Drake661ea262000-10-24 19:57:45 +00001813 "reduce() of empty sequence with no initial value");
Guido van Rossum2d951851994-08-29 12:52:16 +00001814
Tim Peters15d81ef2001-05-04 04:39:21 +00001815 Py_DECREF(it);
Guido van Rossum12d12c51993-10-26 17:58:25 +00001816 return result;
1817
Guido van Rossum2d951851994-08-29 12:52:16 +00001818Fail:
Guido van Rossum79f25d91997-04-29 20:08:16 +00001819 Py_XDECREF(args);
1820 Py_XDECREF(result);
Tim Peters15d81ef2001-05-04 04:39:21 +00001821 Py_DECREF(it);
Guido van Rossum12d12c51993-10-26 17:58:25 +00001822 return NULL;
1823}
1824
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001825PyDoc_STRVAR(reduce_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001826"reduce(function, sequence[, initial]) -> value\n\
1827\n\
1828Apply a function of two arguments cumulatively to the items of a sequence,\n\
1829from left to right, so as to reduce the sequence to a single value.\n\
1830For example, reduce(lambda x, y: x+y, [1, 2, 3, 4, 5]) calculates\n\
1831((((1+2)+3)+4)+5). If initial is present, it is placed before the items\n\
1832of the sequence in the calculation, and serves as a default when the\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001833sequence is empty.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001834
1835
Guido van Rossum79f25d91997-04-29 20:08:16 +00001836static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001837builtin_reload(PyObject *self, PyObject *v)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001838{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001839 return PyImport_ReloadModule(v);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001840}
1841
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001842PyDoc_STRVAR(reload_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001843"reload(module) -> module\n\
1844\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001845Reload the module. The module must have been successfully imported before.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001846
1847
Guido van Rossum79f25d91997-04-29 20:08:16 +00001848static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001849builtin_repr(PyObject *self, PyObject *v)
Guido van Rossumc89705d1992-11-26 08:54:07 +00001850{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001851 return PyObject_Repr(v);
Guido van Rossumc89705d1992-11-26 08:54:07 +00001852}
1853
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001854PyDoc_STRVAR(repr_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001855"repr(object) -> string\n\
1856\n\
1857Return the canonical string representation of the object.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001858For most object types, eval(repr(object)) == object.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001859
1860
Guido van Rossum79f25d91997-04-29 20:08:16 +00001861static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001862builtin_round(PyObject *self, PyObject *args)
Guido van Rossum9e51f9b1993-02-12 16:29:05 +00001863{
Guido van Rossum9e51f9b1993-02-12 16:29:05 +00001864 double x;
1865 double f;
1866 int ndigits = 0;
Guido van Rossum9e51f9b1993-02-12 16:29:05 +00001867 int i;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001868
Guido van Rossum79f25d91997-04-29 20:08:16 +00001869 if (!PyArg_ParseTuple(args, "d|i:round", &x, &ndigits))
Guido van Rossum9e51f9b1993-02-12 16:29:05 +00001870 return NULL;
Guido van Rossum9e51f9b1993-02-12 16:29:05 +00001871 f = 1.0;
Guido van Rossum1e162d31998-05-09 14:42:25 +00001872 i = abs(ndigits);
1873 while (--i >= 0)
Guido van Rossum9e51f9b1993-02-12 16:29:05 +00001874 f = f*10.0;
Guido van Rossum1e162d31998-05-09 14:42:25 +00001875 if (ndigits < 0)
1876 x /= f;
Guido van Rossum9e51f9b1993-02-12 16:29:05 +00001877 else
Guido van Rossum1e162d31998-05-09 14:42:25 +00001878 x *= f;
1879 if (x >= 0.0)
1880 x = floor(x + 0.5);
1881 else
1882 x = ceil(x - 0.5);
1883 if (ndigits < 0)
1884 x *= f;
1885 else
1886 x /= f;
1887 return PyFloat_FromDouble(x);
Guido van Rossum9e51f9b1993-02-12 16:29:05 +00001888}
1889
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001890PyDoc_STRVAR(round_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001891"round(number[, ndigits]) -> floating point number\n\
1892\n\
1893Round a number to a given precision in decimal digits (default 0 digits).\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001894This always returns a floating point number. Precision may be negative.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001895
Raymond Hettinger64958a12003-12-17 20:43:33 +00001896static PyObject *
1897builtin_sorted(PyObject *self, PyObject *args, PyObject *kwds)
1898{
1899 PyObject *newlist, *v, *seq, *compare=NULL, *keyfunc=NULL, *newargs;
1900 PyObject *callable;
1901 static char *kwlist[] = {"iterable", "cmp", "key", "reverse", 0};
1902 long reverse;
1903
1904 if (args != NULL) {
1905 if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|OOi:sorted",
1906 kwlist, &seq, &compare, &keyfunc, &reverse))
1907 return NULL;
1908 }
1909
1910 newlist = PySequence_List(seq);
1911 if (newlist == NULL)
1912 return NULL;
1913
1914 callable = PyObject_GetAttrString(newlist, "sort");
1915 if (callable == NULL) {
1916 Py_DECREF(newlist);
1917 return NULL;
1918 }
1919
1920 newargs = PyTuple_GetSlice(args, 1, 4);
1921 if (newargs == NULL) {
1922 Py_DECREF(newlist);
1923 Py_DECREF(callable);
1924 return NULL;
1925 }
1926
1927 v = PyObject_Call(callable, newargs, kwds);
1928 Py_DECREF(newargs);
1929 Py_DECREF(callable);
1930 if (v == NULL) {
1931 Py_DECREF(newlist);
1932 return NULL;
1933 }
1934 Py_DECREF(v);
1935 return newlist;
1936}
1937
1938PyDoc_STRVAR(sorted_doc,
1939"sorted(iterable, cmp=None, key=None, reverse=False) --> new sorted list");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001940
Guido van Rossum79f25d91997-04-29 20:08:16 +00001941static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001942builtin_vars(PyObject *self, PyObject *args)
Guido van Rossum2d951851994-08-29 12:52:16 +00001943{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001944 PyObject *v = NULL;
1945 PyObject *d;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001946
Raymond Hettingerea3fdf42002-12-29 16:33:45 +00001947 if (!PyArg_UnpackTuple(args, "vars", 0, 1, &v))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001948 return NULL;
Guido van Rossum2d951851994-08-29 12:52:16 +00001949 if (v == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001950 d = PyEval_GetLocals();
Guido van Rossum53bb7ff1995-07-26 16:26:31 +00001951 if (d == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001952 if (!PyErr_Occurred())
1953 PyErr_SetString(PyExc_SystemError,
Alex Martellia9b9c9f2003-04-23 13:34:35 +00001954 "vars(): no locals!?");
Guido van Rossum53bb7ff1995-07-26 16:26:31 +00001955 }
1956 else
Guido van Rossum79f25d91997-04-29 20:08:16 +00001957 Py_INCREF(d);
Guido van Rossum2d951851994-08-29 12:52:16 +00001958 }
1959 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001960 d = PyObject_GetAttrString(v, "__dict__");
Guido van Rossum2d951851994-08-29 12:52:16 +00001961 if (d == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001962 PyErr_SetString(PyExc_TypeError,
Guido van Rossum2d951851994-08-29 12:52:16 +00001963 "vars() argument must have __dict__ attribute");
1964 return NULL;
1965 }
1966 }
1967 return d;
1968}
1969
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001970PyDoc_STRVAR(vars_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001971"vars([object]) -> dictionary\n\
1972\n\
1973Without arguments, equivalent to locals().\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001974With an argument, equivalent to object.__dict__.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001975
Alex Martellia70b1912003-04-22 08:12:33 +00001976
1977static PyObject*
1978builtin_sum(PyObject *self, PyObject *args)
1979{
1980 PyObject *seq;
1981 PyObject *result = NULL;
1982 PyObject *temp, *item, *iter;
1983
Raymond Hettinger5cf63942003-10-25 06:41:37 +00001984 if (!PyArg_UnpackTuple(args, "sum", 1, 2, &seq, &result))
Alex Martellia70b1912003-04-22 08:12:33 +00001985 return NULL;
1986
1987 iter = PyObject_GetIter(seq);
1988 if (iter == NULL)
1989 return NULL;
1990
1991 if (result == NULL) {
1992 result = PyInt_FromLong(0);
1993 if (result == NULL) {
1994 Py_DECREF(iter);
1995 return NULL;
1996 }
1997 } else {
1998 /* reject string values for 'start' parameter */
1999 if (PyObject_TypeCheck(result, &PyBaseString_Type)) {
2000 PyErr_SetString(PyExc_TypeError,
Alex Martellia9b9c9f2003-04-23 13:34:35 +00002001 "sum() can't sum strings [use ''.join(seq) instead]");
Alex Martellia70b1912003-04-22 08:12:33 +00002002 Py_DECREF(iter);
2003 return NULL;
2004 }
Alex Martelli41c9f882003-04-22 09:24:48 +00002005 Py_INCREF(result);
Alex Martellia70b1912003-04-22 08:12:33 +00002006 }
2007
2008 for(;;) {
2009 item = PyIter_Next(iter);
2010 if (item == NULL) {
2011 /* error, or end-of-sequence */
2012 if (PyErr_Occurred()) {
2013 Py_DECREF(result);
2014 result = NULL;
2015 }
2016 break;
2017 }
Alex Martellia253e182003-10-25 23:24:14 +00002018 temp = PyNumber_Add(result, item);
Alex Martellia70b1912003-04-22 08:12:33 +00002019 Py_DECREF(result);
2020 Py_DECREF(item);
2021 result = temp;
2022 if (result == NULL)
2023 break;
2024 }
2025 Py_DECREF(iter);
2026 return result;
2027}
2028
2029PyDoc_STRVAR(sum_doc,
2030"sum(sequence, start=0) -> value\n\
2031\n\
2032Returns the sum of a sequence of numbers (NOT strings) plus the value\n\
2033of parameter 'start'. When the sequence is empty, returns start.");
2034
2035
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002036static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002037builtin_isinstance(PyObject *self, PyObject *args)
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002038{
2039 PyObject *inst;
2040 PyObject *cls;
Guido van Rossum823649d2001-03-21 18:40:58 +00002041 int retval;
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002042
Raymond Hettingerea3fdf42002-12-29 16:33:45 +00002043 if (!PyArg_UnpackTuple(args, "isinstance", 2, 2, &inst, &cls))
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002044 return NULL;
Guido van Rossumf5dd9141997-12-02 19:11:45 +00002045
Guido van Rossum823649d2001-03-21 18:40:58 +00002046 retval = PyObject_IsInstance(inst, cls);
2047 if (retval < 0)
Guido van Rossumad991772001-01-12 16:03:05 +00002048 return NULL;
Guido van Rossum77f6a652002-04-03 22:41:51 +00002049 return PyBool_FromLong(retval);
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002050}
2051
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002052PyDoc_STRVAR(isinstance_doc,
Guido van Rossum77f6a652002-04-03 22:41:51 +00002053"isinstance(object, class-or-type-or-tuple) -> bool\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002054\n\
2055Return whether an object is an instance of a class or of a subclass thereof.\n\
Guido van Rossum03290ec2001-10-07 20:54:12 +00002056With a type as second argument, return whether that is the object's type.\n\
2057The form using a tuple, isinstance(x, (A, B, ...)), is a shortcut for\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002058isinstance(x, A) or isinstance(x, B) or ... (etc.).");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002059
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002060
2061static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002062builtin_issubclass(PyObject *self, PyObject *args)
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002063{
2064 PyObject *derived;
2065 PyObject *cls;
2066 int retval;
2067
Raymond Hettingerea3fdf42002-12-29 16:33:45 +00002068 if (!PyArg_UnpackTuple(args, "issubclass", 2, 2, &derived, &cls))
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002069 return NULL;
Guido van Rossum668213d1999-06-16 17:28:37 +00002070
Guido van Rossum823649d2001-03-21 18:40:58 +00002071 retval = PyObject_IsSubclass(derived, cls);
2072 if (retval < 0)
2073 return NULL;
Guido van Rossum77f6a652002-04-03 22:41:51 +00002074 return PyBool_FromLong(retval);
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002075}
2076
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002077PyDoc_STRVAR(issubclass_doc,
Guido van Rossum77f6a652002-04-03 22:41:51 +00002078"issubclass(C, B) -> bool\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002079\n\
Walter Dörwaldd9a6ad32002-12-12 16:41:44 +00002080Return whether class C is a subclass (i.e., a derived class) of class B.\n\
2081When using a tuple as the second argument issubclass(X, (A, B, ...)),\n\
2082is a shortcut for issubclass(X, A) or issubclass(X, B) or ... (etc.).");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002083
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002084
Barry Warsawbd599b52000-08-03 15:45:29 +00002085static PyObject*
2086builtin_zip(PyObject *self, PyObject *args)
2087{
2088 PyObject *ret;
Tim Peters67d687a2002-04-29 21:27:32 +00002089 const int itemsize = PySequence_Length(args);
Tim Peters8572b4f2001-05-06 01:05:02 +00002090 int i;
2091 PyObject *itlist; /* tuple of iterators */
Tim Peters67d687a2002-04-29 21:27:32 +00002092 int len; /* guess at result length */
Barry Warsawbd599b52000-08-03 15:45:29 +00002093
Raymond Hettingereaef6152003-08-02 07:42:57 +00002094 if (itemsize == 0)
2095 return PyList_New(0);
2096
Barry Warsawbd599b52000-08-03 15:45:29 +00002097 /* args must be a tuple */
2098 assert(PyTuple_Check(args));
2099
Tim Peters39a86c22002-05-12 07:19:38 +00002100 /* Guess at result length: the shortest of the input lengths.
2101 If some argument refuses to say, we refuse to guess too, lest
2102 an argument like xrange(sys.maxint) lead us astray.*/
Tim Peters67d687a2002-04-29 21:27:32 +00002103 len = -1; /* unknown */
2104 for (i = 0; i < itemsize; ++i) {
2105 PyObject *item = PyTuple_GET_ITEM(args, i);
Raymond Hettinger77f3c872004-01-04 08:54:44 +00002106 int thislen = PyObject_Size(item);
Tim Peters39a86c22002-05-12 07:19:38 +00002107 if (thislen < 0) {
Raymond Hettingera710b332005-08-21 11:03:59 +00002108 if (!PyErr_ExceptionMatches(PyExc_TypeError) &&
2109 !PyErr_ExceptionMatches(PyExc_AttributeError)) {
2110 return NULL;
2111 }
Tim Peters67d687a2002-04-29 21:27:32 +00002112 PyErr_Clear();
Tim Peters39a86c22002-05-12 07:19:38 +00002113 len = -1;
2114 break;
2115 }
Tim Peters67d687a2002-04-29 21:27:32 +00002116 else if (len < 0 || thislen < len)
2117 len = thislen;
2118 }
2119
Tim Peters8572b4f2001-05-06 01:05:02 +00002120 /* allocate result list */
Tim Peters67d687a2002-04-29 21:27:32 +00002121 if (len < 0)
2122 len = 10; /* arbitrary */
2123 if ((ret = PyList_New(len)) == NULL)
Barry Warsawbd599b52000-08-03 15:45:29 +00002124 return NULL;
2125
Tim Peters8572b4f2001-05-06 01:05:02 +00002126 /* obtain iterators */
2127 itlist = PyTuple_New(itemsize);
2128 if (itlist == NULL)
2129 goto Fail_ret;
2130 for (i = 0; i < itemsize; ++i) {
2131 PyObject *item = PyTuple_GET_ITEM(args, i);
2132 PyObject *it = PyObject_GetIter(item);
2133 if (it == NULL) {
2134 if (PyErr_ExceptionMatches(PyExc_TypeError))
2135 PyErr_Format(PyExc_TypeError,
2136 "zip argument #%d must support iteration",
2137 i+1);
2138 goto Fail_ret_itlist;
Barry Warsawbd599b52000-08-03 15:45:29 +00002139 }
Tim Peters8572b4f2001-05-06 01:05:02 +00002140 PyTuple_SET_ITEM(itlist, i, it);
2141 }
Barry Warsawbd599b52000-08-03 15:45:29 +00002142
Tim Peters8572b4f2001-05-06 01:05:02 +00002143 /* build result into ret list */
Tim Peters67d687a2002-04-29 21:27:32 +00002144 for (i = 0; ; ++i) {
2145 int j;
Tim Peters8572b4f2001-05-06 01:05:02 +00002146 PyObject *next = PyTuple_New(itemsize);
2147 if (!next)
2148 goto Fail_ret_itlist;
2149
Tim Peters67d687a2002-04-29 21:27:32 +00002150 for (j = 0; j < itemsize; j++) {
2151 PyObject *it = PyTuple_GET_ITEM(itlist, j);
Tim Peters8572b4f2001-05-06 01:05:02 +00002152 PyObject *item = PyIter_Next(it);
Barry Warsawbd599b52000-08-03 15:45:29 +00002153 if (!item) {
Tim Peters8572b4f2001-05-06 01:05:02 +00002154 if (PyErr_Occurred()) {
2155 Py_DECREF(ret);
2156 ret = NULL;
Barry Warsawbd599b52000-08-03 15:45:29 +00002157 }
2158 Py_DECREF(next);
Tim Peters8572b4f2001-05-06 01:05:02 +00002159 Py_DECREF(itlist);
Tim Peters67d687a2002-04-29 21:27:32 +00002160 goto Done;
Barry Warsawbd599b52000-08-03 15:45:29 +00002161 }
Tim Peters67d687a2002-04-29 21:27:32 +00002162 PyTuple_SET_ITEM(next, j, item);
Barry Warsawbd599b52000-08-03 15:45:29 +00002163 }
Tim Peters8572b4f2001-05-06 01:05:02 +00002164
Tim Peters67d687a2002-04-29 21:27:32 +00002165 if (i < len)
2166 PyList_SET_ITEM(ret, i, next);
2167 else {
2168 int status = PyList_Append(ret, next);
2169 Py_DECREF(next);
2170 ++len;
2171 if (status < 0)
2172 goto Fail_ret_itlist;
2173 }
Barry Warsawbd599b52000-08-03 15:45:29 +00002174 }
Tim Peters8572b4f2001-05-06 01:05:02 +00002175
Tim Peters67d687a2002-04-29 21:27:32 +00002176Done:
2177 if (ret != NULL && i < len) {
2178 /* The list is too big. */
2179 if (PyList_SetSlice(ret, i, len, NULL) < 0)
2180 return NULL;
2181 }
2182 return ret;
2183
Tim Peters8572b4f2001-05-06 01:05:02 +00002184Fail_ret_itlist:
2185 Py_DECREF(itlist);
2186Fail_ret:
2187 Py_DECREF(ret);
2188 return NULL;
Barry Warsawbd599b52000-08-03 15:45:29 +00002189}
2190
2191
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002192PyDoc_STRVAR(zip_doc,
Barry Warsawbd599b52000-08-03 15:45:29 +00002193"zip(seq1 [, seq2 [...]]) -> [(seq1[0], seq2[0] ...), (...)]\n\
2194\n\
2195Return a list of tuples, where each tuple contains the i-th element\n\
2196from each of the argument sequences. The returned list is truncated\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002197in length to the length of the shortest argument sequence.");
Barry Warsawbd599b52000-08-03 15:45:29 +00002198
2199
Guido van Rossum79f25d91997-04-29 20:08:16 +00002200static PyMethodDef builtin_methods[] = {
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002201 {"__import__", builtin___import__, METH_VARARGS, import_doc},
2202 {"abs", builtin_abs, METH_O, abs_doc},
Raymond Hettinger96229b12005-03-11 06:49:40 +00002203 {"all", builtin_all, METH_O, all_doc},
2204 {"any", builtin_any, METH_O, any_doc},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002205 {"apply", builtin_apply, METH_VARARGS, apply_doc},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002206 {"callable", builtin_callable, METH_O, callable_doc},
2207 {"chr", builtin_chr, METH_VARARGS, chr_doc},
2208 {"cmp", builtin_cmp, METH_VARARGS, cmp_doc},
2209 {"coerce", builtin_coerce, METH_VARARGS, coerce_doc},
2210 {"compile", builtin_compile, METH_VARARGS, compile_doc},
2211 {"delattr", builtin_delattr, METH_VARARGS, delattr_doc},
2212 {"dir", builtin_dir, METH_VARARGS, dir_doc},
2213 {"divmod", builtin_divmod, METH_VARARGS, divmod_doc},
2214 {"eval", builtin_eval, METH_VARARGS, eval_doc},
2215 {"execfile", builtin_execfile, METH_VARARGS, execfile_doc},
2216 {"filter", builtin_filter, METH_VARARGS, filter_doc},
2217 {"getattr", builtin_getattr, METH_VARARGS, getattr_doc},
2218 {"globals", (PyCFunction)builtin_globals, METH_NOARGS, globals_doc},
2219 {"hasattr", builtin_hasattr, METH_VARARGS, hasattr_doc},
2220 {"hash", builtin_hash, METH_O, hash_doc},
2221 {"hex", builtin_hex, METH_O, hex_doc},
2222 {"id", builtin_id, METH_O, id_doc},
2223 {"input", builtin_input, METH_VARARGS, input_doc},
2224 {"intern", builtin_intern, METH_VARARGS, intern_doc},
2225 {"isinstance", builtin_isinstance, METH_VARARGS, isinstance_doc},
2226 {"issubclass", builtin_issubclass, METH_VARARGS, issubclass_doc},
2227 {"iter", builtin_iter, METH_VARARGS, iter_doc},
2228 {"len", builtin_len, METH_O, len_doc},
2229 {"locals", (PyCFunction)builtin_locals, METH_NOARGS, locals_doc},
2230 {"map", builtin_map, METH_VARARGS, map_doc},
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00002231 {"max", (PyCFunction)builtin_max, METH_VARARGS | METH_KEYWORDS, max_doc},
2232 {"min", (PyCFunction)builtin_min, METH_VARARGS | METH_KEYWORDS, min_doc},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002233 {"oct", builtin_oct, METH_O, oct_doc},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002234 {"ord", builtin_ord, METH_O, ord_doc},
2235 {"pow", builtin_pow, METH_VARARGS, pow_doc},
2236 {"range", builtin_range, METH_VARARGS, range_doc},
2237 {"raw_input", builtin_raw_input, METH_VARARGS, raw_input_doc},
2238 {"reduce", builtin_reduce, METH_VARARGS, reduce_doc},
2239 {"reload", builtin_reload, METH_O, reload_doc},
2240 {"repr", builtin_repr, METH_O, repr_doc},
2241 {"round", builtin_round, METH_VARARGS, round_doc},
2242 {"setattr", builtin_setattr, METH_VARARGS, setattr_doc},
Raymond Hettinger64958a12003-12-17 20:43:33 +00002243 {"sorted", (PyCFunction)builtin_sorted, METH_VARARGS | METH_KEYWORDS, sorted_doc},
Alex Martellia70b1912003-04-22 08:12:33 +00002244 {"sum", builtin_sum, METH_VARARGS, sum_doc},
Martin v. Löwis339d0f72001-08-17 18:39:25 +00002245#ifdef Py_USING_UNICODE
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002246 {"unichr", builtin_unichr, METH_VARARGS, unichr_doc},
Martin v. Löwis339d0f72001-08-17 18:39:25 +00002247#endif
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002248 {"vars", builtin_vars, METH_VARARGS, vars_doc},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002249 {"zip", builtin_zip, METH_VARARGS, zip_doc},
Guido van Rossumc02e15c1991-12-16 13:03:00 +00002250 {NULL, NULL},
Guido van Rossum3f5da241990-12-20 15:06:42 +00002251};
2252
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002253PyDoc_STRVAR(builtin_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002254"Built-in functions, exceptions, and other objects.\n\
2255\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002256Noteworthy: None is the `nil' object; Ellipsis represents `...' in slices.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002257
Guido van Rossum25ce5661997-08-02 03:10:38 +00002258PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002259_PyBuiltin_Init(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +00002260{
Fred Drake5550de32000-06-20 04:54:19 +00002261 PyObject *mod, *dict, *debug;
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002262 mod = Py_InitModule4("__builtin__", builtin_methods,
2263 builtin_doc, (PyObject *)NULL,
2264 PYTHON_API_VERSION);
Guido van Rossum25ce5661997-08-02 03:10:38 +00002265 if (mod == NULL)
2266 return NULL;
2267 dict = PyModule_GetDict(mod);
Tim Peters4b7625e2001-09-13 21:37:17 +00002268
Tim Peters7571a0f2003-03-23 17:52:28 +00002269#ifdef Py_TRACE_REFS
2270 /* __builtin__ exposes a number of statically allocated objects
2271 * that, before this code was added in 2.3, never showed up in
2272 * the list of "all objects" maintained by Py_TRACE_REFS. As a
2273 * result, programs leaking references to None and False (etc)
2274 * couldn't be diagnosed by examining sys.getobjects(0).
2275 */
2276#define ADD_TO_ALL(OBJECT) _Py_AddToAllObjects((PyObject *)(OBJECT), 0)
2277#else
2278#define ADD_TO_ALL(OBJECT) (void)0
2279#endif
2280
Tim Peters4b7625e2001-09-13 21:37:17 +00002281#define SETBUILTIN(NAME, OBJECT) \
Tim Peters7571a0f2003-03-23 17:52:28 +00002282 if (PyDict_SetItemString(dict, NAME, (PyObject *)OBJECT) < 0) \
2283 return NULL; \
2284 ADD_TO_ALL(OBJECT)
Tim Peters4b7625e2001-09-13 21:37:17 +00002285
2286 SETBUILTIN("None", Py_None);
2287 SETBUILTIN("Ellipsis", Py_Ellipsis);
2288 SETBUILTIN("NotImplemented", Py_NotImplemented);
Guido van Rossum77f6a652002-04-03 22:41:51 +00002289 SETBUILTIN("False", Py_False);
2290 SETBUILTIN("True", Py_True);
Neal Norwitz32a7e7f2002-05-31 19:58:02 +00002291 SETBUILTIN("basestring", &PyBaseString_Type);
Guido van Rossum77f6a652002-04-03 22:41:51 +00002292 SETBUILTIN("bool", &PyBool_Type);
Guido van Rossumbea18cc2002-06-14 20:41:17 +00002293 SETBUILTIN("buffer", &PyBuffer_Type);
Tim Peters4b7625e2001-09-13 21:37:17 +00002294 SETBUILTIN("classmethod", &PyClassMethod_Type);
Tim Peters6d6c1a32001-08-02 04:15:00 +00002295#ifndef WITHOUT_COMPLEX
Tim Peters4b7625e2001-09-13 21:37:17 +00002296 SETBUILTIN("complex", &PyComplex_Type);
Tim Peters6d6c1a32001-08-02 04:15:00 +00002297#endif
Tim Petersa427a2b2001-10-29 22:25:45 +00002298 SETBUILTIN("dict", &PyDict_Type);
Tim Peters67d687a2002-04-29 21:27:32 +00002299 SETBUILTIN("enumerate", &PyEnum_Type);
Tim Peters4b7625e2001-09-13 21:37:17 +00002300 SETBUILTIN("float", &PyFloat_Type);
Raymond Hettingera690a992003-11-16 16:17:49 +00002301 SETBUILTIN("frozenset", &PyFrozenSet_Type);
Tim Peters4b7625e2001-09-13 21:37:17 +00002302 SETBUILTIN("property", &PyProperty_Type);
2303 SETBUILTIN("int", &PyInt_Type);
2304 SETBUILTIN("list", &PyList_Type);
2305 SETBUILTIN("long", &PyLong_Type);
2306 SETBUILTIN("object", &PyBaseObject_Type);
Raymond Hettinger85c20a42003-11-06 14:06:48 +00002307 SETBUILTIN("reversed", &PyReversed_Type);
Raymond Hettingera690a992003-11-16 16:17:49 +00002308 SETBUILTIN("set", &PySet_Type);
Guido van Rossumbea18cc2002-06-14 20:41:17 +00002309 SETBUILTIN("slice", &PySlice_Type);
Tim Peters4b7625e2001-09-13 21:37:17 +00002310 SETBUILTIN("staticmethod", &PyStaticMethod_Type);
2311 SETBUILTIN("str", &PyString_Type);
2312 SETBUILTIN("super", &PySuper_Type);
2313 SETBUILTIN("tuple", &PyTuple_Type);
2314 SETBUILTIN("type", &PyType_Type);
Raymond Hettingerc4c453f2002-06-05 23:12:45 +00002315 SETBUILTIN("xrange", &PyRange_Type);
Tim Peters742dfd62001-09-13 21:49:44 +00002316
2317 /* Note that open() is just an alias of file(). */
2318 SETBUILTIN("open", &PyFile_Type);
Tim Peters4b7625e2001-09-13 21:37:17 +00002319 SETBUILTIN("file", &PyFile_Type);
Martin v. Löwis339d0f72001-08-17 18:39:25 +00002320#ifdef Py_USING_UNICODE
Tim Peters4b7625e2001-09-13 21:37:17 +00002321 SETBUILTIN("unicode", &PyUnicode_Type);
Martin v. Löwis339d0f72001-08-17 18:39:25 +00002322#endif
Guido van Rossum77f6a652002-04-03 22:41:51 +00002323 debug = PyBool_FromLong(Py_OptimizeFlag == 0);
Fred Drake5550de32000-06-20 04:54:19 +00002324 if (PyDict_SetItemString(dict, "__debug__", debug) < 0) {
2325 Py_XDECREF(debug);
Guido van Rossum25ce5661997-08-02 03:10:38 +00002326 return NULL;
Fred Drake5550de32000-06-20 04:54:19 +00002327 }
2328 Py_XDECREF(debug);
Barry Warsaw757af0e1997-08-29 22:13:51 +00002329
Guido van Rossum25ce5661997-08-02 03:10:38 +00002330 return mod;
Tim Peters7571a0f2003-03-23 17:52:28 +00002331#undef ADD_TO_ALL
Tim Peters4b7625e2001-09-13 21:37:17 +00002332#undef SETBUILTIN
Guido van Rossum3f5da241990-12-20 15:06:42 +00002333}
2334
Guido van Rossume77a7571993-11-03 15:01:26 +00002335/* Helper for filter(): filter a tuple through a function */
Guido van Rossum12d12c51993-10-26 17:58:25 +00002336
Guido van Rossum79f25d91997-04-29 20:08:16 +00002337static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002338filtertuple(PyObject *func, PyObject *tuple)
Guido van Rossum12d12c51993-10-26 17:58:25 +00002339{
Guido van Rossum79f25d91997-04-29 20:08:16 +00002340 PyObject *result;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002341 register int i, j;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002342 int len = PyTuple_Size(tuple);
Guido van Rossum12d12c51993-10-26 17:58:25 +00002343
Guido van Rossumb7b45621995-08-04 04:07:45 +00002344 if (len == 0) {
Walter Dörwaldc3da83f2003-02-04 20:24:45 +00002345 if (PyTuple_CheckExact(tuple))
2346 Py_INCREF(tuple);
2347 else
2348 tuple = PyTuple_New(0);
Guido van Rossumb7b45621995-08-04 04:07:45 +00002349 return tuple;
2350 }
2351
Guido van Rossum79f25d91997-04-29 20:08:16 +00002352 if ((result = PyTuple_New(len)) == NULL)
Guido van Rossum2586bf01993-11-01 16:21:44 +00002353 return NULL;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002354
Guido van Rossum12d12c51993-10-26 17:58:25 +00002355 for (i = j = 0; i < len; ++i) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002356 PyObject *item, *good;
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002357 int ok;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002358
Walter Dörwald8dd19322003-02-10 17:36:40 +00002359 if (tuple->ob_type->tp_as_sequence &&
2360 tuple->ob_type->tp_as_sequence->sq_item) {
2361 item = tuple->ob_type->tp_as_sequence->sq_item(tuple, i);
Walter Dörwaldc58a3a12003-08-18 18:28:45 +00002362 if (item == NULL)
2363 goto Fail_1;
Walter Dörwald8dd19322003-02-10 17:36:40 +00002364 } else {
Alex Martellia9b9c9f2003-04-23 13:34:35 +00002365 PyErr_SetString(PyExc_TypeError, "filter(): unsubscriptable tuple");
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002366 goto Fail_1;
Walter Dörwald8dd19322003-02-10 17:36:40 +00002367 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002368 if (func == Py_None) {
2369 Py_INCREF(item);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002370 good = item;
2371 }
2372 else {
Raymond Hettinger8ae46892003-10-12 19:09:37 +00002373 PyObject *arg = PyTuple_Pack(1, item);
Walter Dörwaldc58a3a12003-08-18 18:28:45 +00002374 if (arg == NULL) {
2375 Py_DECREF(item);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002376 goto Fail_1;
Walter Dörwaldc58a3a12003-08-18 18:28:45 +00002377 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002378 good = PyEval_CallObject(func, arg);
2379 Py_DECREF(arg);
Walter Dörwaldc58a3a12003-08-18 18:28:45 +00002380 if (good == NULL) {
2381 Py_DECREF(item);
Guido van Rossum12d12c51993-10-26 17:58:25 +00002382 goto Fail_1;
Walter Dörwaldc58a3a12003-08-18 18:28:45 +00002383 }
Guido van Rossum12d12c51993-10-26 17:58:25 +00002384 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002385 ok = PyObject_IsTrue(good);
2386 Py_DECREF(good);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002387 if (ok) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002388 if (PyTuple_SetItem(result, j++, item) < 0)
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002389 goto Fail_1;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002390 }
Walter Dörwaldc58a3a12003-08-18 18:28:45 +00002391 else
2392 Py_DECREF(item);
Guido van Rossum12d12c51993-10-26 17:58:25 +00002393 }
2394
Tim Peters4324aa32001-05-28 22:30:08 +00002395 if (_PyTuple_Resize(&result, j) < 0)
Guido van Rossum12d12c51993-10-26 17:58:25 +00002396 return NULL;
2397
Guido van Rossum12d12c51993-10-26 17:58:25 +00002398 return result;
2399
Guido van Rossum12d12c51993-10-26 17:58:25 +00002400Fail_1:
Guido van Rossum79f25d91997-04-29 20:08:16 +00002401 Py_DECREF(result);
Guido van Rossum12d12c51993-10-26 17:58:25 +00002402 return NULL;
2403}
2404
2405
Guido van Rossume77a7571993-11-03 15:01:26 +00002406/* Helper for filter(): filter a string through a function */
Guido van Rossum12d12c51993-10-26 17:58:25 +00002407
Guido van Rossum79f25d91997-04-29 20:08:16 +00002408static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002409filterstring(PyObject *func, PyObject *strobj)
Guido van Rossum12d12c51993-10-26 17:58:25 +00002410{
Guido van Rossum79f25d91997-04-29 20:08:16 +00002411 PyObject *result;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002412 register int i, j;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002413 int len = PyString_Size(strobj);
Walter Dörwald903f1e02003-02-04 16:28:00 +00002414 int outlen = len;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002415
Guido van Rossum79f25d91997-04-29 20:08:16 +00002416 if (func == Py_None) {
Walter Dörwald1918f772003-02-10 13:19:13 +00002417 /* If it's a real string we can return the original,
2418 * as no character is ever false and __getitem__
2419 * does return this character. If it's a subclass
2420 * we must go through the __getitem__ loop */
2421 if (PyString_CheckExact(strobj)) {
Walter Dörwaldc3da83f2003-02-04 20:24:45 +00002422 Py_INCREF(strobj);
Walter Dörwald1918f772003-02-10 13:19:13 +00002423 return strobj;
2424 }
Guido van Rossum12d12c51993-10-26 17:58:25 +00002425 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002426 if ((result = PyString_FromStringAndSize(NULL, len)) == NULL)
Guido van Rossum2586bf01993-11-01 16:21:44 +00002427 return NULL;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002428
Guido van Rossum12d12c51993-10-26 17:58:25 +00002429 for (i = j = 0; i < len; ++i) {
Walter Dörwald1918f772003-02-10 13:19:13 +00002430 PyObject *item;
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002431 int ok;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002432
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002433 item = (*strobj->ob_type->tp_as_sequence->sq_item)(strobj, i);
2434 if (item == NULL)
2435 goto Fail_1;
Walter Dörwald1918f772003-02-10 13:19:13 +00002436 if (func==Py_None) {
2437 ok = 1;
2438 } else {
2439 PyObject *arg, *good;
Raymond Hettinger8ae46892003-10-12 19:09:37 +00002440 arg = PyTuple_Pack(1, item);
Walter Dörwald1918f772003-02-10 13:19:13 +00002441 if (arg == NULL) {
2442 Py_DECREF(item);
2443 goto Fail_1;
2444 }
2445 good = PyEval_CallObject(func, arg);
2446 Py_DECREF(arg);
2447 if (good == NULL) {
2448 Py_DECREF(item);
2449 goto Fail_1;
2450 }
2451 ok = PyObject_IsTrue(good);
2452 Py_DECREF(good);
Tim Peters388ed082001-04-07 20:34:48 +00002453 }
Walter Dörwald903f1e02003-02-04 16:28:00 +00002454 if (ok) {
2455 int reslen;
2456 if (!PyString_Check(item)) {
2457 PyErr_SetString(PyExc_TypeError, "can't filter str to str:"
2458 " __getitem__ returned different type");
2459 Py_DECREF(item);
2460 goto Fail_1;
2461 }
2462 reslen = PyString_GET_SIZE(item);
2463 if (reslen == 1) {
2464 PyString_AS_STRING(result)[j++] =
2465 PyString_AS_STRING(item)[0];
2466 } else {
2467 /* do we need more space? */
2468 int need = j + reslen + len-i-1;
2469 if (need > outlen) {
2470 /* overallocate, to avoid reallocations */
2471 if (need<2*outlen)
2472 need = 2*outlen;
2473 if (_PyString_Resize(&result, need)) {
2474 Py_DECREF(item);
2475 return NULL;
2476 }
2477 outlen = need;
2478 }
2479 memcpy(
2480 PyString_AS_STRING(result) + j,
2481 PyString_AS_STRING(item),
2482 reslen
2483 );
2484 j += reslen;
2485 }
2486 }
Tim Peters388ed082001-04-07 20:34:48 +00002487 Py_DECREF(item);
Guido van Rossum12d12c51993-10-26 17:58:25 +00002488 }
2489
Walter Dörwald903f1e02003-02-04 16:28:00 +00002490 if (j < outlen)
Tim Peters5de98422002-04-27 18:44:32 +00002491 _PyString_Resize(&result, j);
Guido van Rossum12d12c51993-10-26 17:58:25 +00002492
Guido van Rossum12d12c51993-10-26 17:58:25 +00002493 return result;
2494
Guido van Rossum12d12c51993-10-26 17:58:25 +00002495Fail_1:
Guido van Rossum79f25d91997-04-29 20:08:16 +00002496 Py_DECREF(result);
Guido van Rossum12d12c51993-10-26 17:58:25 +00002497 return NULL;
2498}
Martin v. Löwis8afd7572003-01-25 22:46:11 +00002499
2500#ifdef Py_USING_UNICODE
2501/* Helper for filter(): filter a Unicode object through a function */
2502
2503static PyObject *
2504filterunicode(PyObject *func, PyObject *strobj)
2505{
2506 PyObject *result;
2507 register int i, j;
2508 int len = PyUnicode_GetSize(strobj);
Walter Dörwald903f1e02003-02-04 16:28:00 +00002509 int outlen = len;
Martin v. Löwis8afd7572003-01-25 22:46:11 +00002510
2511 if (func == Py_None) {
Walter Dörwald1918f772003-02-10 13:19:13 +00002512 /* If it's a real string we can return the original,
2513 * as no character is ever false and __getitem__
2514 * does return this character. If it's a subclass
2515 * we must go through the __getitem__ loop */
2516 if (PyUnicode_CheckExact(strobj)) {
Walter Dörwaldc3da83f2003-02-04 20:24:45 +00002517 Py_INCREF(strobj);
Walter Dörwald1918f772003-02-10 13:19:13 +00002518 return strobj;
2519 }
Martin v. Löwis8afd7572003-01-25 22:46:11 +00002520 }
2521 if ((result = PyUnicode_FromUnicode(NULL, len)) == NULL)
2522 return NULL;
2523
2524 for (i = j = 0; i < len; ++i) {
2525 PyObject *item, *arg, *good;
2526 int ok;
2527
2528 item = (*strobj->ob_type->tp_as_sequence->sq_item)(strobj, i);
2529 if (item == NULL)
2530 goto Fail_1;
Walter Dörwald1918f772003-02-10 13:19:13 +00002531 if (func == Py_None) {
2532 ok = 1;
2533 } else {
Raymond Hettinger8ae46892003-10-12 19:09:37 +00002534 arg = PyTuple_Pack(1, item);
Walter Dörwald1918f772003-02-10 13:19:13 +00002535 if (arg == NULL) {
2536 Py_DECREF(item);
2537 goto Fail_1;
2538 }
2539 good = PyEval_CallObject(func, arg);
2540 Py_DECREF(arg);
2541 if (good == NULL) {
2542 Py_DECREF(item);
2543 goto Fail_1;
2544 }
2545 ok = PyObject_IsTrue(good);
2546 Py_DECREF(good);
Martin v. Löwis8afd7572003-01-25 22:46:11 +00002547 }
Walter Dörwald903f1e02003-02-04 16:28:00 +00002548 if (ok) {
2549 int reslen;
2550 if (!PyUnicode_Check(item)) {
Jeremy Hylton1aad9c72003-09-16 03:10:59 +00002551 PyErr_SetString(PyExc_TypeError,
2552 "can't filter unicode to unicode:"
2553 " __getitem__ returned different type");
Walter Dörwald903f1e02003-02-04 16:28:00 +00002554 Py_DECREF(item);
2555 goto Fail_1;
2556 }
2557 reslen = PyUnicode_GET_SIZE(item);
Jeremy Hylton1aad9c72003-09-16 03:10:59 +00002558 if (reslen == 1)
Walter Dörwald903f1e02003-02-04 16:28:00 +00002559 PyUnicode_AS_UNICODE(result)[j++] =
2560 PyUnicode_AS_UNICODE(item)[0];
Jeremy Hylton1aad9c72003-09-16 03:10:59 +00002561 else {
Walter Dörwald903f1e02003-02-04 16:28:00 +00002562 /* do we need more space? */
Jeremy Hylton1aad9c72003-09-16 03:10:59 +00002563 int need = j + reslen + len - i - 1;
Walter Dörwald903f1e02003-02-04 16:28:00 +00002564 if (need > outlen) {
Jeremy Hylton1aad9c72003-09-16 03:10:59 +00002565 /* overallocate,
2566 to avoid reallocations */
2567 if (need < 2 * outlen)
2568 need = 2 * outlen;
Jeremy Hylton364f6be2003-09-16 03:17:16 +00002569 if (PyUnicode_Resize(
2570 &result, need) < 0) {
Walter Dörwald903f1e02003-02-04 16:28:00 +00002571 Py_DECREF(item);
Walter Dörwald531e0002003-02-04 16:57:49 +00002572 goto Fail_1;
Walter Dörwald903f1e02003-02-04 16:28:00 +00002573 }
2574 outlen = need;
2575 }
Jeremy Hylton1aad9c72003-09-16 03:10:59 +00002576 memcpy(PyUnicode_AS_UNICODE(result) + j,
2577 PyUnicode_AS_UNICODE(item),
2578 reslen*sizeof(Py_UNICODE));
Walter Dörwald903f1e02003-02-04 16:28:00 +00002579 j += reslen;
2580 }
2581 }
Martin v. Löwis8afd7572003-01-25 22:46:11 +00002582 Py_DECREF(item);
2583 }
2584
Walter Dörwald903f1e02003-02-04 16:28:00 +00002585 if (j < outlen)
Martin v. Löwis8afd7572003-01-25 22:46:11 +00002586 PyUnicode_Resize(&result, j);
2587
2588 return result;
2589
2590Fail_1:
2591 Py_DECREF(result);
2592 return NULL;
2593}
2594#endif