blob: d4e9a22e5f4391cd4d4bb59722ffde88405159c3 [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)) {
Georg Brandl99d7e4e2005-08-31 22:21:15 +0000532 PyErr_SetString(PyExc_TypeError, PyMapping_Check(globals) ?
Raymond Hettinger214b1c32004-07-02 06:41:07 +0000533 "globals must be a real dict; try eval(expr, {}, mapping)"
534 : "globals must be a dict");
Raymond Hettinger513ffe82004-07-06 13:44:41 +0000535 return NULL;
Raymond Hettinger214b1c32004-07-02 06:41:07 +0000536 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000537 if (globals == Py_None) {
538 globals = PyEval_GetGlobals();
539 if (locals == Py_None)
540 locals = PyEval_GetLocals();
Guido van Rossum6135a871995-01-09 17:53:26 +0000541 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000542 else if (locals == Py_None)
Guido van Rossum6135a871995-01-09 17:53:26 +0000543 locals = globals;
Tim Peters9fa96be2001-08-17 23:04:59 +0000544
Georg Brandl77c85e62005-09-15 10:46:13 +0000545 if (globals == NULL || locals == NULL) {
546 PyErr_SetString(PyExc_TypeError,
547 "eval must be given globals and locals "
548 "when called without a frame");
549 return NULL;
550 }
551
Guido van Rossum79f25d91997-04-29 20:08:16 +0000552 if (PyDict_GetItemString(globals, "__builtins__") == NULL) {
553 if (PyDict_SetItemString(globals, "__builtins__",
554 PyEval_GetBuiltins()) != 0)
Guido van Rossum6135a871995-01-09 17:53:26 +0000555 return NULL;
556 }
Tim Peters9fa96be2001-08-17 23:04:59 +0000557
Jeremy Hylton15c1c4f2001-07-30 21:50:55 +0000558 if (PyCode_Check(cmd)) {
Jeremy Hylton733c8932001-12-13 19:51:56 +0000559 if (PyCode_GetNumFree((PyCodeObject *)cmd) > 0) {
Jeremy Hylton15c1c4f2001-07-30 21:50:55 +0000560 PyErr_SetString(PyExc_TypeError,
561 "code object passed to eval() may not contain free variables");
562 return NULL;
563 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000564 return PyEval_EvalCode((PyCodeObject *) cmd, globals, locals);
Jeremy Hylton15c1c4f2001-07-30 21:50:55 +0000565 }
Tim Peters9fa96be2001-08-17 23:04:59 +0000566
Marc-André Lemburgd1ba4432000-09-19 21:04:18 +0000567 if (!PyString_Check(cmd) &&
568 !PyUnicode_Check(cmd)) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000569 PyErr_SetString(PyExc_TypeError,
Fred Drake661ea262000-10-24 19:57:45 +0000570 "eval() arg 1 must be a string or code object");
Guido van Rossum94390a41992-08-14 15:14:30 +0000571 return NULL;
572 }
Just van Rossum3aaf42c2003-02-10 08:21:10 +0000573 cf.cf_flags = 0;
574
575#ifdef Py_USING_UNICODE
576 if (PyUnicode_Check(cmd)) {
577 tmp = PyUnicode_AsUTF8String(cmd);
578 if (tmp == NULL)
579 return NULL;
580 cmd = tmp;
581 cf.cf_flags |= PyCF_SOURCE_IS_UTF8;
582 }
583#endif
Marc-André Lemburgd1ba4432000-09-19 21:04:18 +0000584 if (PyString_AsStringAndSize(cmd, &str, NULL))
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000585 return NULL;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000586 while (*str == ' ' || *str == '\t')
587 str++;
Tim Peters9fa96be2001-08-17 23:04:59 +0000588
Tim Peters9fa96be2001-08-17 23:04:59 +0000589 (void)PyEval_MergeCompilerFlags(&cf);
Just van Rossum3aaf42c2003-02-10 08:21:10 +0000590 result = PyRun_StringFlags(str, Py_eval_input, globals, locals, &cf);
591 Py_XDECREF(tmp);
592 return result;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000593}
594
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000595PyDoc_STRVAR(eval_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000596"eval(source[, globals[, locals]]) -> value\n\
597\n\
598Evaluate the source in the context of globals and locals.\n\
599The source may be a string representing a Python expression\n\
600or a code object as returned by compile().\n\
Raymond Hettinger214b1c32004-07-02 06:41:07 +0000601The globals must be a dictionary and locals can be any mappping,\n\
602defaulting to the current globals and locals.\n\
603If only globals is given, locals defaults to it.\n");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000604
605
Guido van Rossum79f25d91997-04-29 20:08:16 +0000606static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000607builtin_execfile(PyObject *self, PyObject *args)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000608{
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000609 char *filename;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000610 PyObject *globals = Py_None, *locals = Py_None;
611 PyObject *res;
Guido van Rossumb3a639e2001-09-05 13:37:47 +0000612 FILE* fp = NULL;
Tim Peters5ba58662001-07-16 02:29:45 +0000613 PyCompilerFlags cf;
Martin v. Löwis6b3a2c42001-08-08 05:30:36 +0000614 int exists;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000615
Raymond Hettinger66bd2332004-08-02 08:30:07 +0000616 if (!PyArg_ParseTuple(args, "s|O!O:execfile",
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000617 &filename,
Guido van Rossum79f25d91997-04-29 20:08:16 +0000618 &PyDict_Type, &globals,
Raymond Hettinger66bd2332004-08-02 08:30:07 +0000619 &locals))
Guido van Rossum0f61f8a1992-02-25 18:55:05 +0000620 return NULL;
Raymond Hettinger66bd2332004-08-02 08:30:07 +0000621 if (locals != Py_None && !PyMapping_Check(locals)) {
622 PyErr_SetString(PyExc_TypeError, "locals must be a mapping");
623 return NULL;
624 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000625 if (globals == Py_None) {
626 globals = PyEval_GetGlobals();
627 if (locals == Py_None)
628 locals = PyEval_GetLocals();
Guido van Rossum6135a871995-01-09 17:53:26 +0000629 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000630 else if (locals == Py_None)
Guido van Rossum6135a871995-01-09 17:53:26 +0000631 locals = globals;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000632 if (PyDict_GetItemString(globals, "__builtins__") == NULL) {
633 if (PyDict_SetItemString(globals, "__builtins__",
634 PyEval_GetBuiltins()) != 0)
Guido van Rossum6135a871995-01-09 17:53:26 +0000635 return NULL;
636 }
Martin v. Löwis6b3a2c42001-08-08 05:30:36 +0000637
638 exists = 0;
639 /* Test for existence or directory. */
Martin v. Löwis3484a182002-03-09 12:07:51 +0000640#if defined(PLAN9)
641 {
642 Dir *d;
643
644 if ((d = dirstat(filename))!=nil) {
645 if(d->mode & DMDIR)
646 werrstr("is a directory");
647 else
648 exists = 1;
649 free(d);
650 }
Martin v. Löwis6b3a2c42001-08-08 05:30:36 +0000651 }
Martin v. Löwis3484a182002-03-09 12:07:51 +0000652#elif defined(RISCOS)
Guido van Rossume2ae77b2001-10-24 20:42:55 +0000653 if (object_exists(filename)) {
654 if (isdir(filename))
655 errno = EISDIR;
656 else
657 exists = 1;
658 }
Martin v. Löwis3484a182002-03-09 12:07:51 +0000659#else /* standard Posix */
660 {
661 struct stat s;
662 if (stat(filename, &s) == 0) {
663 if (S_ISDIR(s.st_mode))
Andrew MacIntyreda4d6cb2004-03-29 11:53:38 +0000664# if defined(PYOS_OS2) && defined(PYCC_VACPP)
Martin v. Löwis3484a182002-03-09 12:07:51 +0000665 errno = EOS2ERR;
666# else
667 errno = EISDIR;
668# endif
669 else
670 exists = 1;
671 }
672 }
673#endif
Martin v. Löwis6b3a2c42001-08-08 05:30:36 +0000674
675 if (exists) {
676 Py_BEGIN_ALLOW_THREADS
Jack Jansen7b8c7542002-04-14 20:12:41 +0000677 fp = fopen(filename, "r" PY_STDIOTEXTMODE);
Martin v. Löwis6b3a2c42001-08-08 05:30:36 +0000678 Py_END_ALLOW_THREADS
679
680 if (fp == NULL) {
681 exists = 0;
682 }
683 }
684
685 if (!exists) {
Peter Schneider-Kamp4c013422002-08-27 16:58:00 +0000686 PyErr_SetFromErrnoWithFilename(PyExc_IOError, filename);
Guido van Rossum0f61f8a1992-02-25 18:55:05 +0000687 return NULL;
688 }
Tim Peters5ba58662001-07-16 02:29:45 +0000689 cf.cf_flags = 0;
690 if (PyEval_MergeCompilerFlags(&cf))
Tim Peters748b8bb2001-04-28 08:20:22 +0000691 res = PyRun_FileExFlags(fp, filename, Py_file_input, globals,
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000692 locals, 1, &cf);
Tim Peters5ba58662001-07-16 02:29:45 +0000693 else
Tim Peters748b8bb2001-04-28 08:20:22 +0000694 res = PyRun_FileEx(fp, filename, Py_file_input, globals,
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000695 locals, 1);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000696 return res;
Guido van Rossum0f61f8a1992-02-25 18:55:05 +0000697}
698
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000699PyDoc_STRVAR(execfile_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000700"execfile(filename[, globals[, locals]])\n\
701\n\
702Read and execute a Python script from a file.\n\
703The globals and locals are dictionaries, defaulting to the current\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000704globals and locals. If only globals is given, locals defaults to it.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000705
706
Guido van Rossum79f25d91997-04-29 20:08:16 +0000707static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000708builtin_getattr(PyObject *self, PyObject *args)
Guido van Rossum33894be1992-01-27 16:53:09 +0000709{
Guido van Rossum950ff291998-06-29 13:38:57 +0000710 PyObject *v, *result, *dflt = NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000711 PyObject *name;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000712
Raymond Hettingerea3fdf42002-12-29 16:33:45 +0000713 if (!PyArg_UnpackTuple(args, "getattr", 2, 3, &v, &name, &dflt))
Guido van Rossum33894be1992-01-27 16:53:09 +0000714 return NULL;
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000715#ifdef Py_USING_UNICODE
Jeremy Hylton0eb11152001-07-30 22:39:31 +0000716 if (PyUnicode_Check(name)) {
717 name = _PyUnicode_AsDefaultEncodedString(name, NULL);
718 if (name == NULL)
719 return NULL;
720 }
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000721#endif
Jeremy Hylton0eb11152001-07-30 22:39:31 +0000722
723 if (!PyString_Check(name)) {
724 PyErr_SetString(PyExc_TypeError,
Alex Martellia9b9c9f2003-04-23 13:34:35 +0000725 "getattr(): attribute name must be string");
Jeremy Hylton0eb11152001-07-30 22:39:31 +0000726 return NULL;
727 }
Guido van Rossum950ff291998-06-29 13:38:57 +0000728 result = PyObject_GetAttr(v, name);
Guido van Rossumd8923572001-10-16 21:31:32 +0000729 if (result == NULL && dflt != NULL &&
730 PyErr_ExceptionMatches(PyExc_AttributeError))
731 {
Guido van Rossum950ff291998-06-29 13:38:57 +0000732 PyErr_Clear();
733 Py_INCREF(dflt);
734 result = dflt;
735 }
736 return result;
Guido van Rossum9bfef441993-03-29 10:43:31 +0000737}
738
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000739PyDoc_STRVAR(getattr_doc,
Guido van Rossum950ff291998-06-29 13:38:57 +0000740"getattr(object, name[, default]) -> value\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000741\n\
Guido van Rossum950ff291998-06-29 13:38:57 +0000742Get a named attribute from an object; getattr(x, 'y') is equivalent to x.y.\n\
743When a default argument is given, it is returned when the attribute doesn't\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000744exist; without it, an exception is raised in that case.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000745
746
Guido van Rossum79f25d91997-04-29 20:08:16 +0000747static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000748builtin_globals(PyObject *self)
Guido van Rossum872537c1995-07-07 22:43:42 +0000749{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000750 PyObject *d;
Guido van Rossum872537c1995-07-07 22:43:42 +0000751
Guido van Rossum79f25d91997-04-29 20:08:16 +0000752 d = PyEval_GetGlobals();
753 Py_INCREF(d);
Guido van Rossum872537c1995-07-07 22:43:42 +0000754 return d;
755}
756
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000757PyDoc_STRVAR(globals_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000758"globals() -> dictionary\n\
759\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000760Return the dictionary containing the current scope's global variables.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000761
762
Guido van Rossum79f25d91997-04-29 20:08:16 +0000763static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000764builtin_hasattr(PyObject *self, PyObject *args)
Guido van Rossum9bfef441993-03-29 10:43:31 +0000765{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000766 PyObject *v;
767 PyObject *name;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000768
Raymond Hettingerea3fdf42002-12-29 16:33:45 +0000769 if (!PyArg_UnpackTuple(args, "hasattr", 2, 2, &v, &name))
Guido van Rossum9bfef441993-03-29 10:43:31 +0000770 return NULL;
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000771#ifdef Py_USING_UNICODE
Jeremy Hylton302b54a2001-07-30 22:45:19 +0000772 if (PyUnicode_Check(name)) {
773 name = _PyUnicode_AsDefaultEncodedString(name, NULL);
774 if (name == NULL)
775 return NULL;
776 }
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000777#endif
Jeremy Hylton302b54a2001-07-30 22:45:19 +0000778
779 if (!PyString_Check(name)) {
780 PyErr_SetString(PyExc_TypeError,
Alex Martellia9b9c9f2003-04-23 13:34:35 +0000781 "hasattr(): attribute name must be string");
Jeremy Hylton302b54a2001-07-30 22:45:19 +0000782 return NULL;
783 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000784 v = PyObject_GetAttr(v, name);
Guido van Rossum9bfef441993-03-29 10:43:31 +0000785 if (v == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000786 PyErr_Clear();
Guido van Rossum09df08a1998-05-22 00:51:39 +0000787 Py_INCREF(Py_False);
788 return Py_False;
Guido van Rossum9bfef441993-03-29 10:43:31 +0000789 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000790 Py_DECREF(v);
Guido van Rossum09df08a1998-05-22 00:51:39 +0000791 Py_INCREF(Py_True);
792 return Py_True;
Guido van Rossum33894be1992-01-27 16:53:09 +0000793}
794
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000795PyDoc_STRVAR(hasattr_doc,
Guido van Rossum77f6a652002-04-03 22:41:51 +0000796"hasattr(object, name) -> bool\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000797\n\
798Return whether the object has an attribute with the given name.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000799(This is done by calling getattr(object, name) and catching exceptions.)");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000800
801
Guido van Rossum79f25d91997-04-29 20:08:16 +0000802static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000803builtin_id(PyObject *self, PyObject *v)
Guido van Rossum5b722181993-03-30 17:46:03 +0000804{
Guido van Rossum106f2da2000-06-28 21:12:25 +0000805 return PyLong_FromVoidPtr(v);
Guido van Rossum5b722181993-03-30 17:46:03 +0000806}
807
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000808PyDoc_STRVAR(id_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000809"id(object) -> integer\n\
810\n\
811Return the identity of an object. This is guaranteed to be unique among\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000812simultaneously existing objects. (Hint: it's the object's memory address.)");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000813
814
Guido van Rossum79f25d91997-04-29 20:08:16 +0000815static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000816builtin_map(PyObject *self, PyObject *args)
Guido van Rossum12d12c51993-10-26 17:58:25 +0000817{
818 typedef struct {
Tim Peters4e9afdc2001-05-03 23:54:49 +0000819 PyObject *it; /* the iterator object */
820 int saw_StopIteration; /* bool: did the iterator end? */
Guido van Rossum12d12c51993-10-26 17:58:25 +0000821 } sequence;
822
Guido van Rossum79f25d91997-04-29 20:08:16 +0000823 PyObject *func, *result;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000824 sequence *seqs = NULL, *sqp;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000825 int n, len;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000826 register int i, j;
827
Guido van Rossum79f25d91997-04-29 20:08:16 +0000828 n = PyTuple_Size(args);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000829 if (n < 2) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000830 PyErr_SetString(PyExc_TypeError,
831 "map() requires at least two args");
Guido van Rossum12d12c51993-10-26 17:58:25 +0000832 return NULL;
833 }
834
Guido van Rossum79f25d91997-04-29 20:08:16 +0000835 func = PyTuple_GetItem(args, 0);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000836 n--;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000837
Guido van Rossumfa4ac711998-07-10 17:37:30 +0000838 if (func == Py_None && n == 1) {
839 /* map(None, S) is the same as list(S). */
840 return PySequence_List(PyTuple_GetItem(args, 1));
841 }
842
Tim Peters4e9afdc2001-05-03 23:54:49 +0000843 /* Get space for sequence descriptors. Must NULL out the iterator
844 * pointers so that jumping to Fail_2 later doesn't see trash.
845 */
Guido van Rossum79f25d91997-04-29 20:08:16 +0000846 if ((seqs = PyMem_NEW(sequence, n)) == NULL) {
847 PyErr_NoMemory();
Tim Peters4e9afdc2001-05-03 23:54:49 +0000848 return NULL;
849 }
850 for (i = 0; i < n; ++i) {
851 seqs[i].it = (PyObject*)NULL;
852 seqs[i].saw_StopIteration = 0;
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000853 }
Guido van Rossum12d12c51993-10-26 17:58:25 +0000854
Tim Peters4e9afdc2001-05-03 23:54:49 +0000855 /* Do a first pass to obtain iterators for the arguments, and set len
856 * to the largest of their lengths.
Tim Peters748b8bb2001-04-28 08:20:22 +0000857 */
Tim Peters4e9afdc2001-05-03 23:54:49 +0000858 len = 0;
859 for (i = 0, sqp = seqs; i < n; ++i, ++sqp) {
860 PyObject *curseq;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000861 int curlen;
Guido van Rossumad991772001-01-12 16:03:05 +0000862
Tim Peters4e9afdc2001-05-03 23:54:49 +0000863 /* Get iterator. */
864 curseq = PyTuple_GetItem(args, i+1);
865 sqp->it = PyObject_GetIter(curseq);
866 if (sqp->it == NULL) {
Guido van Rossum12d12c51993-10-26 17:58:25 +0000867 static char errmsg[] =
Tim Peters4e9afdc2001-05-03 23:54:49 +0000868 "argument %d to map() must support iteration";
Guido van Rossum15e33a41997-04-30 19:00:27 +0000869 char errbuf[sizeof(errmsg) + 25];
Jeremy Hylton518ab1c2001-11-28 20:42:20 +0000870 PyOS_snprintf(errbuf, sizeof(errbuf), errmsg, i+2);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000871 PyErr_SetString(PyExc_TypeError, errbuf);
Guido van Rossum12d12c51993-10-26 17:58:25 +0000872 goto Fail_2;
873 }
874
Tim Peters4e9afdc2001-05-03 23:54:49 +0000875 /* Update len. */
Raymond Hettinger77f3c872004-01-04 08:54:44 +0000876 curlen = PyObject_Size(curseq);
877 if (curlen < 0) {
Raymond Hettingera710b332005-08-21 11:03:59 +0000878 if (!PyErr_ExceptionMatches(PyExc_TypeError) &&
879 !PyErr_ExceptionMatches(PyExc_AttributeError)) {
880 goto Fail_2;
881 }
Raymond Hettinger77f3c872004-01-04 08:54:44 +0000882 PyErr_Clear();
Tim Peters4e9afdc2001-05-03 23:54:49 +0000883 curlen = 8; /* arbitrary */
Raymond Hettinger77f3c872004-01-04 08:54:44 +0000884 }
885 if (curlen > len)
886 len = curlen;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000887 }
888
Tim Peters4e9afdc2001-05-03 23:54:49 +0000889 /* Get space for the result list. */
Guido van Rossum79f25d91997-04-29 20:08:16 +0000890 if ((result = (PyObject *) PyList_New(len)) == NULL)
Guido van Rossum12d12c51993-10-26 17:58:25 +0000891 goto Fail_2;
892
Tim Peters4e9afdc2001-05-03 23:54:49 +0000893 /* Iterate over the sequences until all have stopped. */
Guido van Rossum2d951851994-08-29 12:52:16 +0000894 for (i = 0; ; ++i) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000895 PyObject *alist, *item=NULL, *value;
Tim Peters4e9afdc2001-05-03 23:54:49 +0000896 int numactive = 0;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000897
Guido van Rossum79f25d91997-04-29 20:08:16 +0000898 if (func == Py_None && n == 1)
Guido van Rossum32120311995-07-10 13:52:21 +0000899 alist = NULL;
Tim Peters4e9afdc2001-05-03 23:54:49 +0000900 else if ((alist = PyTuple_New(n)) == NULL)
901 goto Fail_1;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000902
903 for (j = 0, sqp = seqs; j < n; ++j, ++sqp) {
Tim Peters4e9afdc2001-05-03 23:54:49 +0000904 if (sqp->saw_StopIteration) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000905 Py_INCREF(Py_None);
906 item = Py_None;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000907 }
Guido van Rossum12d12c51993-10-26 17:58:25 +0000908 else {
Tim Peters4e9afdc2001-05-03 23:54:49 +0000909 item = PyIter_Next(sqp->it);
910 if (item)
911 ++numactive;
912 else {
Tim Peters4e9afdc2001-05-03 23:54:49 +0000913 if (PyErr_Occurred()) {
Tim Petersf4848da2001-05-05 00:14:56 +0000914 Py_XDECREF(alist);
915 goto Fail_1;
Guido van Rossum2d951851994-08-29 12:52:16 +0000916 }
Tim Peters4e9afdc2001-05-03 23:54:49 +0000917 Py_INCREF(Py_None);
918 item = Py_None;
919 sqp->saw_StopIteration = 1;
Guido van Rossum2d951851994-08-29 12:52:16 +0000920 }
Guido van Rossum12d12c51993-10-26 17:58:25 +0000921 }
Tim Peters4e9afdc2001-05-03 23:54:49 +0000922 if (alist)
923 PyTuple_SET_ITEM(alist, j, item);
924 else
Guido van Rossum2d951851994-08-29 12:52:16 +0000925 break;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000926 }
927
Guido van Rossum32120311995-07-10 13:52:21 +0000928 if (!alist)
929 alist = item;
Guido van Rossum2d951851994-08-29 12:52:16 +0000930
Tim Peters4e9afdc2001-05-03 23:54:49 +0000931 if (numactive == 0) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000932 Py_DECREF(alist);
Guido van Rossum2d951851994-08-29 12:52:16 +0000933 break;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000934 }
Guido van Rossum2d951851994-08-29 12:52:16 +0000935
Guido van Rossum79f25d91997-04-29 20:08:16 +0000936 if (func == Py_None)
Guido van Rossum32120311995-07-10 13:52:21 +0000937 value = alist;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000938 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000939 value = PyEval_CallObject(func, alist);
940 Py_DECREF(alist);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000941 if (value == NULL)
942 goto Fail_1;
Guido van Rossum2d951851994-08-29 12:52:16 +0000943 }
944 if (i >= len) {
Barry Warsawfa77e091999-01-28 18:49:12 +0000945 int status = PyList_Append(result, value);
Barry Warsaw21332871999-01-28 04:21:35 +0000946 Py_DECREF(value);
Barry Warsawfa77e091999-01-28 18:49:12 +0000947 if (status < 0)
948 goto Fail_1;
Guido van Rossum2d951851994-08-29 12:52:16 +0000949 }
Tim Peters4e9afdc2001-05-03 23:54:49 +0000950 else if (PyList_SetItem(result, i, value) < 0)
951 goto Fail_1;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000952 }
953
Guido van Rossumfa4ac711998-07-10 17:37:30 +0000954 if (i < len && PyList_SetSlice(result, i, len, NULL) < 0)
955 goto Fail_1;
956
Tim Peters4e9afdc2001-05-03 23:54:49 +0000957 goto Succeed;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000958
Guido van Rossum12d12c51993-10-26 17:58:25 +0000959Fail_1:
Guido van Rossum79f25d91997-04-29 20:08:16 +0000960 Py_DECREF(result);
Guido van Rossum12d12c51993-10-26 17:58:25 +0000961Fail_2:
Tim Peters4e9afdc2001-05-03 23:54:49 +0000962 result = NULL;
963Succeed:
964 assert(seqs);
965 for (i = 0; i < n; ++i)
966 Py_XDECREF(seqs[i].it);
967 PyMem_DEL(seqs);
968 return result;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000969}
970
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000971PyDoc_STRVAR(map_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000972"map(function, sequence[, sequence, ...]) -> list\n\
973\n\
974Return a list of the results of applying the function to the items of\n\
975the argument sequence(s). If more than one sequence is given, the\n\
976function is called with an argument list consisting of the corresponding\n\
977item of each sequence, substituting None for missing values when not all\n\
978sequences have the same length. If the function is None, return a list of\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000979the items of the sequence (or a list of tuples if more than one sequence).");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000980
981
Guido van Rossum79f25d91997-04-29 20:08:16 +0000982static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000983builtin_setattr(PyObject *self, PyObject *args)
Guido van Rossum33894be1992-01-27 16:53:09 +0000984{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000985 PyObject *v;
986 PyObject *name;
987 PyObject *value;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000988
Raymond Hettingerea3fdf42002-12-29 16:33:45 +0000989 if (!PyArg_UnpackTuple(args, "setattr", 3, 3, &v, &name, &value))
Guido van Rossum33894be1992-01-27 16:53:09 +0000990 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000991 if (PyObject_SetAttr(v, name, value) != 0)
Guido van Rossum33894be1992-01-27 16:53:09 +0000992 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000993 Py_INCREF(Py_None);
994 return Py_None;
Guido van Rossum33894be1992-01-27 16:53:09 +0000995}
996
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000997PyDoc_STRVAR(setattr_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000998"setattr(object, name, value)\n\
999\n\
1000Set a named attribute on an object; setattr(x, 'y', v) is equivalent to\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001001``x.y = v''.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001002
1003
Guido van Rossum79f25d91997-04-29 20:08:16 +00001004static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001005builtin_delattr(PyObject *self, PyObject *args)
Guido van Rossum14144fc1994-08-29 12:53:40 +00001006{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001007 PyObject *v;
1008 PyObject *name;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001009
Raymond Hettingerea3fdf42002-12-29 16:33:45 +00001010 if (!PyArg_UnpackTuple(args, "delattr", 2, 2, &v, &name))
Guido van Rossum14144fc1994-08-29 12:53:40 +00001011 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001012 if (PyObject_SetAttr(v, name, (PyObject *)NULL) != 0)
Guido van Rossum14144fc1994-08-29 12:53:40 +00001013 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001014 Py_INCREF(Py_None);
1015 return Py_None;
Guido van Rossum14144fc1994-08-29 12:53:40 +00001016}
1017
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001018PyDoc_STRVAR(delattr_doc,
Guido van Rossumdf12a591998-11-23 22:13:04 +00001019"delattr(object, name)\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001020\n\
1021Delete a named attribute on an object; delattr(x, 'y') is equivalent to\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001022``del x.y''.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001023
1024
Guido van Rossum79f25d91997-04-29 20:08:16 +00001025static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001026builtin_hash(PyObject *self, PyObject *v)
Guido van Rossum9bfef441993-03-29 10:43:31 +00001027{
Guido van Rossum9bfef441993-03-29 10:43:31 +00001028 long x;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001029
Guido van Rossum79f25d91997-04-29 20:08:16 +00001030 x = PyObject_Hash(v);
Guido van Rossum9bfef441993-03-29 10:43:31 +00001031 if (x == -1)
1032 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001033 return PyInt_FromLong(x);
Guido van Rossum9bfef441993-03-29 10:43:31 +00001034}
1035
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001036PyDoc_STRVAR(hash_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001037"hash(object) -> integer\n\
1038\n\
1039Return a hash value for the object. Two objects with the same value have\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001040the same hash value. The reverse is not necessarily true, but likely.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001041
1042
Guido van Rossum79f25d91997-04-29 20:08:16 +00001043static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001044builtin_hex(PyObject *self, PyObject *v)
Guido van Rossum006bcd41991-10-24 14:54:44 +00001045{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001046 PyNumberMethods *nb;
Neil Schemenauer3a313e32004-07-19 16:29:17 +00001047 PyObject *res;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001048
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001049 if ((nb = v->ob_type->tp_as_number) == NULL ||
Guido van Rossum1899c2e1992-09-12 11:09:23 +00001050 nb->nb_hex == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001051 PyErr_SetString(PyExc_TypeError,
Guido van Rossum1899c2e1992-09-12 11:09:23 +00001052 "hex() argument can't be converted to hex");
1053 return NULL;
Guido van Rossum006bcd41991-10-24 14:54:44 +00001054 }
Neil Schemenauer3a313e32004-07-19 16:29:17 +00001055 res = (*nb->nb_hex)(v);
1056 if (res && !PyString_Check(res)) {
1057 PyErr_Format(PyExc_TypeError,
1058 "__hex__ returned non-string (type %.200s)",
1059 res->ob_type->tp_name);
1060 Py_DECREF(res);
1061 return NULL;
1062 }
1063 return res;
Guido van Rossum006bcd41991-10-24 14:54:44 +00001064}
1065
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001066PyDoc_STRVAR(hex_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001067"hex(number) -> string\n\
1068\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001069Return the hexadecimal representation of an integer or long integer.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001070
1071
Tim Petersdbd9ba62000-07-09 03:09:57 +00001072static PyObject *builtin_raw_input(PyObject *, PyObject *);
Guido van Rossum3165fe61992-09-25 21:59:05 +00001073
Guido van Rossum79f25d91997-04-29 20:08:16 +00001074static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001075builtin_input(PyObject *self, PyObject *args)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001076{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001077 PyObject *line;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001078 char *str;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001079 PyObject *res;
1080 PyObject *globals, *locals;
Hye-Shik Changff83c2b2004-02-02 13:39:01 +00001081 PyCompilerFlags cf;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001082
1083 line = builtin_raw_input(self, args);
Guido van Rossum3165fe61992-09-25 21:59:05 +00001084 if (line == NULL)
1085 return line;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001086 if (!PyArg_Parse(line, "s;embedded '\\0' in input line", &str))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001087 return NULL;
1088 while (*str == ' ' || *str == '\t')
1089 str++;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001090 globals = PyEval_GetGlobals();
1091 locals = PyEval_GetLocals();
1092 if (PyDict_GetItemString(globals, "__builtins__") == NULL) {
1093 if (PyDict_SetItemString(globals, "__builtins__",
1094 PyEval_GetBuiltins()) != 0)
Guido van Rossum6135a871995-01-09 17:53:26 +00001095 return NULL;
1096 }
Hye-Shik Changff83c2b2004-02-02 13:39:01 +00001097 cf.cf_flags = 0;
1098 PyEval_MergeCompilerFlags(&cf);
1099 res = PyRun_StringFlags(str, Py_eval_input, globals, locals, &cf);
Guido van Rossum79f25d91997-04-29 20:08:16 +00001100 Py_DECREF(line);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001101 return res;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001102}
1103
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001104PyDoc_STRVAR(input_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001105"input([prompt]) -> value\n\
1106\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001107Equivalent to eval(raw_input(prompt)).");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001108
1109
Guido van Rossume8811f81997-02-14 15:48:05 +00001110static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001111builtin_intern(PyObject *self, PyObject *args)
Guido van Rossume8811f81997-02-14 15:48:05 +00001112{
1113 PyObject *s;
Guido van Rossum43713e52000-02-29 13:59:29 +00001114 if (!PyArg_ParseTuple(args, "S:intern", &s))
Guido van Rossume8811f81997-02-14 15:48:05 +00001115 return NULL;
Jeremy Hylton4c989dd2004-08-07 19:20:05 +00001116 if (!PyString_CheckExact(s)) {
1117 PyErr_SetString(PyExc_TypeError,
1118 "can't intern subclass of string");
1119 return NULL;
1120 }
Guido van Rossume8811f81997-02-14 15:48:05 +00001121 Py_INCREF(s);
1122 PyString_InternInPlace(&s);
1123 return s;
1124}
1125
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001126PyDoc_STRVAR(intern_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001127"intern(string) -> string\n\
1128\n\
1129``Intern'' the given string. This enters the string in the (global)\n\
1130table of interned strings whose purpose is to speed up dictionary lookups.\n\
1131Return the string itself or the previously interned string object with the\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001132same value.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001133
1134
Guido van Rossum79f25d91997-04-29 20:08:16 +00001135static PyObject *
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001136builtin_iter(PyObject *self, PyObject *args)
1137{
1138 PyObject *v, *w = NULL;
1139
Raymond Hettingerea3fdf42002-12-29 16:33:45 +00001140 if (!PyArg_UnpackTuple(args, "iter", 1, 2, &v, &w))
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001141 return NULL;
1142 if (w == NULL)
1143 return PyObject_GetIter(v);
1144 if (!PyCallable_Check(v)) {
1145 PyErr_SetString(PyExc_TypeError,
1146 "iter(v, w): v must be callable");
1147 return NULL;
1148 }
1149 return PyCallIter_New(v, w);
1150}
1151
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001152PyDoc_STRVAR(iter_doc,
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001153"iter(collection) -> iterator\n\
1154iter(callable, sentinel) -> iterator\n\
1155\n\
1156Get an iterator from an object. In the first form, the argument must\n\
1157supply its own iterator, or be a sequence.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001158In the second form, the callable is called until it returns the sentinel.");
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001159
1160
1161static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001162builtin_len(PyObject *self, PyObject *v)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001163{
Guido van Rossum8ea9f4d1998-06-29 22:26:50 +00001164 long res;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001165
Jeremy Hylton03657cf2000-07-12 13:05:33 +00001166 res = PyObject_Size(v);
Guido van Rossum8ea9f4d1998-06-29 22:26:50 +00001167 if (res < 0 && PyErr_Occurred())
1168 return NULL;
1169 return PyInt_FromLong(res);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001170}
1171
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001172PyDoc_STRVAR(len_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001173"len(object) -> integer\n\
1174\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001175Return the number of items of a sequence or mapping.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001176
1177
Guido van Rossum79f25d91997-04-29 20:08:16 +00001178static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001179builtin_locals(PyObject *self)
Guido van Rossum872537c1995-07-07 22:43:42 +00001180{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001181 PyObject *d;
Guido van Rossum872537c1995-07-07 22:43:42 +00001182
Guido van Rossum79f25d91997-04-29 20:08:16 +00001183 d = PyEval_GetLocals();
1184 Py_INCREF(d);
Guido van Rossum872537c1995-07-07 22:43:42 +00001185 return d;
1186}
1187
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001188PyDoc_STRVAR(locals_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001189"locals() -> dictionary\n\
1190\n\
Raymond Hettinger69bf8f32003-01-04 02:16:22 +00001191Update and return a dictionary containing the current scope's local variables.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001192
1193
Guido van Rossum79f25d91997-04-29 20:08:16 +00001194static PyObject *
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001195min_max(PyObject *args, PyObject *kwds, int op)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001196{
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001197 PyObject *v, *it, *item, *val, *maxitem, *maxval, *keyfunc=NULL;
Martin v. Löwisfd39ad42004-08-12 14:42:37 +00001198 const char *name = op == Py_LT ? "min" : "max";
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001199
Guido van Rossum79f25d91997-04-29 20:08:16 +00001200 if (PyTuple_Size(args) > 1)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001201 v = args;
Martin v. Löwisfd39ad42004-08-12 14:42:37 +00001202 else if (!PyArg_UnpackTuple(args, (char *)name, 1, 1, &v))
Guido van Rossum3f5da241990-12-20 15:06:42 +00001203 return NULL;
Tim Peters67d687a2002-04-29 21:27:32 +00001204
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001205 if (kwds != NULL && PyDict_Check(kwds) && PyDict_Size(kwds)) {
1206 keyfunc = PyDict_GetItemString(kwds, "key");
1207 if (PyDict_Size(kwds)!=1 || keyfunc == NULL) {
Georg Brandl99d7e4e2005-08-31 22:21:15 +00001208 PyErr_Format(PyExc_TypeError,
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001209 "%s() got an unexpected keyword argument", name);
1210 return NULL;
1211 }
Georg Brandl99d7e4e2005-08-31 22:21:15 +00001212 }
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001213
Tim Petersc3074532001-05-03 07:00:32 +00001214 it = PyObject_GetIter(v);
1215 if (it == NULL)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001216 return NULL;
Tim Petersc3074532001-05-03 07:00:32 +00001217
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001218 maxitem = NULL; /* the result */
1219 maxval = NULL; /* the value associated with the result */
Brett Cannon9e635cf2004-12-07 00:25:35 +00001220 while (( item = PyIter_Next(it) )) {
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001221 /* get the value from the key function */
1222 if (keyfunc != NULL) {
1223 val = PyObject_CallFunctionObjArgs(keyfunc, item, NULL);
1224 if (val == NULL)
1225 goto Fail_it_item;
1226 }
1227 /* no key function; the value is the item */
1228 else {
1229 val = item;
1230 Py_INCREF(val);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001231 }
Tim Petersc3074532001-05-03 07:00:32 +00001232
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001233 /* maximum value and item are unset; set them */
1234 if (maxval == NULL) {
1235 maxitem = item;
1236 maxval = val;
1237 }
1238 /* maximum value and item are set; update them as necessary */
Guido van Rossum2d951851994-08-29 12:52:16 +00001239 else {
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001240 int cmp = PyObject_RichCompareBool(val, maxval, op);
1241 if (cmp < 0)
1242 goto Fail_it_item_and_val;
1243 else if (cmp > 0) {
1244 Py_DECREF(maxval);
1245 Py_DECREF(maxitem);
1246 maxval = val;
1247 maxitem = item;
Guido van Rossum53451b32001-01-17 15:47:24 +00001248 }
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001249 else {
1250 Py_DECREF(item);
1251 Py_DECREF(val);
Guido van Rossumc8b6df91997-05-23 00:06:51 +00001252 }
Guido van Rossum2d951851994-08-29 12:52:16 +00001253 }
Guido van Rossum3f5da241990-12-20 15:06:42 +00001254 }
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001255 if (PyErr_Occurred())
1256 goto Fail_it;
1257 if (maxval == NULL) {
Martin v. Löwisfd39ad42004-08-12 14:42:37 +00001258 PyErr_Format(PyExc_ValueError,
1259 "%s() arg is an empty sequence", name);
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001260 assert(maxitem == NULL);
1261 }
1262 else
1263 Py_DECREF(maxval);
Tim Petersc3074532001-05-03 07:00:32 +00001264 Py_DECREF(it);
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001265 return maxitem;
1266
1267Fail_it_item_and_val:
1268 Py_DECREF(val);
1269Fail_it_item:
1270 Py_DECREF(item);
1271Fail_it:
1272 Py_XDECREF(maxval);
1273 Py_XDECREF(maxitem);
1274 Py_DECREF(it);
1275 return NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001276}
1277
Guido van Rossum79f25d91997-04-29 20:08:16 +00001278static PyObject *
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001279builtin_min(PyObject *self, PyObject *args, PyObject *kwds)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001280{
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001281 return min_max(args, kwds, Py_LT);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001282}
1283
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001284PyDoc_STRVAR(min_doc,
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001285"min(iterable[, key=func]) -> value\n\
1286min(a, b, c, ...[, key=func]) -> value\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001287\n\
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001288With a single iterable argument, return its smallest item.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001289With two or more arguments, return the smallest argument.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001290
1291
Guido van Rossum79f25d91997-04-29 20:08:16 +00001292static PyObject *
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001293builtin_max(PyObject *self, PyObject *args, PyObject *kwds)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001294{
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001295 return min_max(args, kwds, Py_GT);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001296}
1297
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001298PyDoc_STRVAR(max_doc,
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001299"max(iterable[, key=func]) -> value\n\
1300max(a, b, c, ...[, key=func]) -> value\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001301\n\
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001302With a single iterable argument, return its largest item.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001303With two or more arguments, return the largest argument.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001304
1305
Guido van Rossum79f25d91997-04-29 20:08:16 +00001306static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001307builtin_oct(PyObject *self, PyObject *v)
Guido van Rossum006bcd41991-10-24 14:54:44 +00001308{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001309 PyNumberMethods *nb;
Neil Schemenauer3a313e32004-07-19 16:29:17 +00001310 PyObject *res;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001311
Guido van Rossum1899c2e1992-09-12 11:09:23 +00001312 if (v == NULL || (nb = v->ob_type->tp_as_number) == NULL ||
1313 nb->nb_oct == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001314 PyErr_SetString(PyExc_TypeError,
Guido van Rossum1899c2e1992-09-12 11:09:23 +00001315 "oct() argument can't be converted to oct");
1316 return NULL;
Guido van Rossum006bcd41991-10-24 14:54:44 +00001317 }
Neil Schemenauer3a313e32004-07-19 16:29:17 +00001318 res = (*nb->nb_oct)(v);
1319 if (res && !PyString_Check(res)) {
1320 PyErr_Format(PyExc_TypeError,
1321 "__oct__ returned non-string (type %.200s)",
1322 res->ob_type->tp_name);
1323 Py_DECREF(res);
1324 return NULL;
1325 }
1326 return res;
Guido van Rossum006bcd41991-10-24 14:54:44 +00001327}
1328
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001329PyDoc_STRVAR(oct_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001330"oct(number) -> string\n\
1331\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001332Return the octal representation of an integer or long integer.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001333
1334
Guido van Rossum79f25d91997-04-29 20:08:16 +00001335static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001336builtin_ord(PyObject *self, PyObject* obj)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001337{
Guido van Rossum09095f32000-03-10 23:00:52 +00001338 long ord;
Jeremy Hylton394b54d2000-04-12 21:19:47 +00001339 int size;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001340
Jeremy Hylton394b54d2000-04-12 21:19:47 +00001341 if (PyString_Check(obj)) {
1342 size = PyString_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)((unsigned char)*PyString_AS_STRING(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#ifdef Py_USING_UNICODE
Jeremy Hylton394b54d2000-04-12 21:19:47 +00001348 } else if (PyUnicode_Check(obj)) {
1349 size = PyUnicode_GET_SIZE(obj);
Andrew M. Kuchling34c20cf2000-12-20 14:36:56 +00001350 if (size == 1) {
Jeremy Hylton394b54d2000-04-12 21:19:47 +00001351 ord = (long)*PyUnicode_AS_UNICODE(obj);
Andrew M. Kuchling34c20cf2000-12-20 14:36:56 +00001352 return PyInt_FromLong(ord);
1353 }
Martin v. Löwis339d0f72001-08-17 18:39:25 +00001354#endif
Jeremy Hylton394b54d2000-04-12 21:19:47 +00001355 } else {
1356 PyErr_Format(PyExc_TypeError,
Andrew M. Kuchlingf07aad12000-12-23 14:11:28 +00001357 "ord() expected string of length 1, but " \
Jeremy Hylton394b54d2000-04-12 21:19:47 +00001358 "%.200s found", obj->ob_type->tp_name);
Guido van Rossum09095f32000-03-10 23:00:52 +00001359 return NULL;
1360 }
1361
Guido van Rossumad991772001-01-12 16:03:05 +00001362 PyErr_Format(PyExc_TypeError,
Andrew M. Kuchlingf07aad12000-12-23 14:11:28 +00001363 "ord() expected a character, "
1364 "but string of length %d found",
Jeremy Hylton394b54d2000-04-12 21:19:47 +00001365 size);
1366 return NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001367}
1368
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001369PyDoc_STRVAR(ord_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001370"ord(c) -> integer\n\
1371\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001372Return the integer ordinal of a one-character string.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001373
1374
Guido van Rossum79f25d91997-04-29 20:08:16 +00001375static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001376builtin_pow(PyObject *self, PyObject *args)
Guido van Rossum6a00cd81995-01-07 12:39:01 +00001377{
Guido van Rossum09df08a1998-05-22 00:51:39 +00001378 PyObject *v, *w, *z = Py_None;
Guido van Rossum6a00cd81995-01-07 12:39:01 +00001379
Raymond Hettingerea3fdf42002-12-29 16:33:45 +00001380 if (!PyArg_UnpackTuple(args, "pow", 2, 3, &v, &w, &z))
Guido van Rossum6a00cd81995-01-07 12:39:01 +00001381 return NULL;
Guido van Rossum09df08a1998-05-22 00:51:39 +00001382 return PyNumber_Power(v, w, z);
Guido van Rossumd4905451991-05-05 20:00:36 +00001383}
1384
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001385PyDoc_STRVAR(pow_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001386"pow(x, y[, z]) -> number\n\
1387\n\
1388With two arguments, equivalent to x**y. With three arguments,\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001389equivalent to (x**y) % z, but may be more efficient (e.g. for longs).");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001390
1391
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001392
1393/* Return number of items in range (lo, hi, step), when arguments are
1394 * PyInt or PyLong objects. step > 0 required. Return a value < 0 if
1395 * & only if the true value is too large to fit in a signed long.
1396 * Arguments MUST return 1 with either PyInt_Check() or
1397 * PyLong_Check(). Return -1 when there is an error.
1398 */
1399static long
1400get_len_of_range_longs(PyObject *lo, PyObject *hi, PyObject *step)
1401{
1402 /* -------------------------------------------------------------
1403 Algorithm is equal to that of get_len_of_range(), but it operates
1404 on PyObjects (which are assumed to be PyLong or PyInt objects).
1405 ---------------------------------------------------------------*/
1406 long n;
1407 PyObject *diff = NULL;
1408 PyObject *one = NULL;
1409 PyObject *tmp1 = NULL, *tmp2 = NULL, *tmp3 = NULL;
1410 /* holds sub-expression evaluations */
1411
1412 /* if (lo >= hi), return length of 0. */
1413 if (PyObject_Compare(lo, hi) >= 0)
1414 return 0;
1415
1416 if ((one = PyLong_FromLong(1L)) == NULL)
1417 goto Fail;
1418
1419 if ((tmp1 = PyNumber_Subtract(hi, lo)) == NULL)
1420 goto Fail;
1421
1422 if ((diff = PyNumber_Subtract(tmp1, one)) == NULL)
1423 goto Fail;
1424
1425 if ((tmp2 = PyNumber_FloorDivide(diff, step)) == NULL)
1426 goto Fail;
1427
1428 if ((tmp3 = PyNumber_Add(tmp2, one)) == NULL)
1429 goto Fail;
1430
1431 n = PyLong_AsLong(tmp3);
1432 if (PyErr_Occurred()) { /* Check for Overflow */
1433 PyErr_Clear();
1434 goto Fail;
1435 }
1436
1437 Py_DECREF(tmp3);
1438 Py_DECREF(tmp2);
1439 Py_DECREF(diff);
1440 Py_DECREF(tmp1);
1441 Py_DECREF(one);
1442 return n;
1443
1444 Fail:
1445 Py_XDECREF(tmp3);
1446 Py_XDECREF(tmp2);
1447 Py_XDECREF(diff);
1448 Py_XDECREF(tmp1);
1449 Py_XDECREF(one);
1450 return -1;
1451}
1452
1453/* An extension of builtin_range() that handles the case when PyLong
1454 * arguments are given. */
1455static PyObject *
1456handle_range_longs(PyObject *self, PyObject *args)
1457{
1458 PyObject *ilow;
Tim Peters874e1f72003-04-13 22:13:08 +00001459 PyObject *ihigh = NULL;
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001460 PyObject *istep = NULL;
Tim Peters874e1f72003-04-13 22:13:08 +00001461
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001462 PyObject *curnum = NULL;
1463 PyObject *v = NULL;
1464 long bign;
1465 int i, n;
1466 int cmp_result;
1467
Tim Peters874e1f72003-04-13 22:13:08 +00001468 PyObject *zero = PyLong_FromLong(0);
1469
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001470 if (zero == NULL)
1471 return NULL;
1472
Tim Peters874e1f72003-04-13 22:13:08 +00001473 if (!PyArg_UnpackTuple(args, "range", 1, 3, &ilow, &ihigh, &istep)) {
1474 Py_DECREF(zero);
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001475 return NULL;
1476 }
1477
Tim Peters874e1f72003-04-13 22:13:08 +00001478 /* Figure out which way we were called, supply defaults, and be
1479 * sure to incref everything so that the decrefs at the end
1480 * are correct.
1481 */
1482 assert(ilow != NULL);
1483 if (ihigh == NULL) {
1484 /* only 1 arg -- it's the upper limit */
1485 ihigh = ilow;
1486 ilow = NULL;
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001487 }
Tim Peters874e1f72003-04-13 22:13:08 +00001488 assert(ihigh != NULL);
1489 Py_INCREF(ihigh);
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001490
Tim Peters874e1f72003-04-13 22:13:08 +00001491 /* ihigh correct now; do ilow */
1492 if (ilow == NULL)
1493 ilow = zero;
1494 Py_INCREF(ilow);
1495
1496 /* ilow and ihigh correct now; do istep */
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001497 if (istep == NULL) {
1498 istep = PyLong_FromLong(1L);
1499 if (istep == NULL)
1500 goto Fail;
1501 }
1502 else {
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001503 Py_INCREF(istep);
1504 }
1505
Tim Peters874e1f72003-04-13 22:13:08 +00001506 if (!PyInt_Check(ilow) && !PyLong_Check(ilow)) {
Guido van Rossum28e83e32003-04-15 12:43:26 +00001507 PyErr_Format(PyExc_TypeError,
Alex Martellif471d472003-04-23 13:00:44 +00001508 "range() integer start argument expected, got %s.",
Guido van Rossum817d6c92003-04-14 18:25:04 +00001509 ilow->ob_type->tp_name);
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001510 goto Fail;
1511 }
1512
Tim Peters874e1f72003-04-13 22:13:08 +00001513 if (!PyInt_Check(ihigh) && !PyLong_Check(ihigh)) {
Guido van Rossum28e83e32003-04-15 12:43:26 +00001514 PyErr_Format(PyExc_TypeError,
Alex Martellif471d472003-04-23 13:00:44 +00001515 "range() integer end argument expected, got %s.",
Guido van Rossum817d6c92003-04-14 18:25:04 +00001516 ihigh->ob_type->tp_name);
Tim Peters874e1f72003-04-13 22:13:08 +00001517 goto Fail;
1518 }
1519
1520 if (!PyInt_Check(istep) && !PyLong_Check(istep)) {
Guido van Rossum28e83e32003-04-15 12:43:26 +00001521 PyErr_Format(PyExc_TypeError,
Alex Martellif471d472003-04-23 13:00:44 +00001522 "range() integer step argument expected, got %s.",
Guido van Rossum817d6c92003-04-14 18:25:04 +00001523 istep->ob_type->tp_name);
Tim Peters874e1f72003-04-13 22:13:08 +00001524 goto Fail;
1525 }
1526
1527 if (PyObject_Cmp(istep, zero, &cmp_result) == -1)
1528 goto Fail;
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001529 if (cmp_result == 0) {
1530 PyErr_SetString(PyExc_ValueError,
Alex Martellif471d472003-04-23 13:00:44 +00001531 "range() step argument must not be zero");
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001532 goto Fail;
1533 }
1534
1535 if (cmp_result > 0)
1536 bign = get_len_of_range_longs(ilow, ihigh, istep);
1537 else {
1538 PyObject *neg_istep = PyNumber_Negative(istep);
1539 if (neg_istep == NULL)
1540 goto Fail;
1541 bign = get_len_of_range_longs(ihigh, ilow, neg_istep);
1542 Py_DECREF(neg_istep);
1543 }
1544
1545 n = (int)bign;
1546 if (bign < 0 || (long)n != bign) {
1547 PyErr_SetString(PyExc_OverflowError,
1548 "range() result has too many items");
1549 goto Fail;
1550 }
1551
1552 v = PyList_New(n);
1553 if (v == NULL)
1554 goto Fail;
1555
1556 curnum = ilow;
1557 Py_INCREF(curnum);
1558
1559 for (i = 0; i < n; i++) {
1560 PyObject *w = PyNumber_Long(curnum);
1561 PyObject *tmp_num;
1562 if (w == NULL)
1563 goto Fail;
1564
1565 PyList_SET_ITEM(v, i, w);
1566
1567 tmp_num = PyNumber_Add(curnum, istep);
1568 if (tmp_num == NULL)
1569 goto Fail;
1570
1571 Py_DECREF(curnum);
1572 curnum = tmp_num;
1573 }
Tim Peters874e1f72003-04-13 22:13:08 +00001574 Py_DECREF(ilow);
1575 Py_DECREF(ihigh);
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001576 Py_DECREF(istep);
1577 Py_DECREF(zero);
Tim Peters874e1f72003-04-13 22:13:08 +00001578 Py_DECREF(curnum);
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001579 return v;
1580
1581 Fail:
Tim Peters874e1f72003-04-13 22:13:08 +00001582 Py_DECREF(ilow);
1583 Py_DECREF(ihigh);
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001584 Py_XDECREF(istep);
Tim Peters874e1f72003-04-13 22:13:08 +00001585 Py_DECREF(zero);
1586 Py_XDECREF(curnum);
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001587 Py_XDECREF(v);
1588 return NULL;
1589}
1590
Guido van Rossum124eff01999-02-23 16:11:01 +00001591/* Return number of items in range/xrange (lo, hi, step). step > 0
1592 * required. Return a value < 0 if & only if the true value is too
1593 * large to fit in a signed long.
1594 */
1595static long
Thomas Wouterse28c2962000-07-23 22:21:32 +00001596get_len_of_range(long lo, long hi, long step)
Guido van Rossum124eff01999-02-23 16:11:01 +00001597{
1598 /* -------------------------------------------------------------
1599 If lo >= hi, the range is empty.
1600 Else if n values are in the range, the last one is
1601 lo + (n-1)*step, which must be <= hi-1. Rearranging,
1602 n <= (hi - lo - 1)/step + 1, so taking the floor of the RHS gives
1603 the proper value. Since lo < hi in this case, hi-lo-1 >= 0, so
1604 the RHS is non-negative and so truncation is the same as the
1605 floor. Letting M be the largest positive long, the worst case
1606 for the RHS numerator is hi=M, lo=-M-1, and then
1607 hi-lo-1 = M-(-M-1)-1 = 2*M. Therefore unsigned long has enough
1608 precision to compute the RHS exactly.
1609 ---------------------------------------------------------------*/
1610 long n = 0;
1611 if (lo < hi) {
1612 unsigned long uhi = (unsigned long)hi;
1613 unsigned long ulo = (unsigned long)lo;
1614 unsigned long diff = uhi - ulo - 1;
1615 n = (long)(diff / (unsigned long)step + 1);
1616 }
1617 return n;
1618}
1619
Guido van Rossum79f25d91997-04-29 20:08:16 +00001620static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001621builtin_range(PyObject *self, PyObject *args)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001622{
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001623 long ilow = 0, ihigh = 0, istep = 1;
Guido van Rossum124eff01999-02-23 16:11:01 +00001624 long bign;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001625 int i, n;
Guido van Rossum124eff01999-02-23 16:11:01 +00001626
Guido van Rossum79f25d91997-04-29 20:08:16 +00001627 PyObject *v;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001628
Guido van Rossum79f25d91997-04-29 20:08:16 +00001629 if (PyTuple_Size(args) <= 1) {
1630 if (!PyArg_ParseTuple(args,
Guido van Rossum0865dd91995-01-17 16:30:22 +00001631 "l;range() requires 1-3 int arguments",
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001632 &ihigh)) {
1633 PyErr_Clear();
1634 return handle_range_longs(self, args);
1635 }
Guido van Rossum3f5da241990-12-20 15:06:42 +00001636 }
1637 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001638 if (!PyArg_ParseTuple(args,
Guido van Rossum0865dd91995-01-17 16:30:22 +00001639 "ll|l;range() requires 1-3 int arguments",
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001640 &ilow, &ihigh, &istep)) {
1641 PyErr_Clear();
1642 return handle_range_longs(self, args);
1643 }
Guido van Rossum3f5da241990-12-20 15:06:42 +00001644 }
1645 if (istep == 0) {
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001646 PyErr_SetString(PyExc_ValueError,
Alex Martellif471d472003-04-23 13:00:44 +00001647 "range() step argument must not be zero");
Guido van Rossum3f5da241990-12-20 15:06:42 +00001648 return NULL;
1649 }
Guido van Rossum124eff01999-02-23 16:11:01 +00001650 if (istep > 0)
1651 bign = get_len_of_range(ilow, ihigh, istep);
1652 else
1653 bign = get_len_of_range(ihigh, ilow, -istep);
1654 n = (int)bign;
1655 if (bign < 0 || (long)n != bign) {
Guido van Rossume23cde21999-01-12 05:07:47 +00001656 PyErr_SetString(PyExc_OverflowError,
Fred Drake661ea262000-10-24 19:57:45 +00001657 "range() result has too many items");
Guido van Rossume23cde21999-01-12 05:07:47 +00001658 return NULL;
1659 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00001660 v = PyList_New(n);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001661 if (v == NULL)
1662 return NULL;
1663 for (i = 0; i < n; i++) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001664 PyObject *w = PyInt_FromLong(ilow);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001665 if (w == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001666 Py_DECREF(v);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001667 return NULL;
1668 }
Guido van Rossuma937d141998-04-24 18:22:02 +00001669 PyList_SET_ITEM(v, i, w);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001670 ilow += istep;
1671 }
1672 return v;
1673}
1674
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001675PyDoc_STRVAR(range_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001676"range([start,] stop[, step]) -> list of integers\n\
1677\n\
1678Return a list containing an arithmetic progression of integers.\n\
1679range(i, j) returns [i, i+1, i+2, ..., j-1]; start (!) defaults to 0.\n\
1680When step is given, it specifies the increment (or decrement).\n\
1681For example, range(4) returns [0, 1, 2, 3]. The end point is omitted!\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001682These are exactly the valid indices for a list of 4 elements.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001683
1684
Guido van Rossum79f25d91997-04-29 20:08:16 +00001685static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001686builtin_raw_input(PyObject *self, PyObject *args)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001687{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001688 PyObject *v = NULL;
Martin v. Löwis31d2df52002-08-14 15:46:02 +00001689 PyObject *fin = PySys_GetObject("stdin");
1690 PyObject *fout = PySys_GetObject("stdout");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001691
Raymond Hettingerea3fdf42002-12-29 16:33:45 +00001692 if (!PyArg_UnpackTuple(args, "[raw_]input", 0, 1, &v))
Guido van Rossum3165fe61992-09-25 21:59:05 +00001693 return NULL;
Martin v. Löwis31d2df52002-08-14 15:46:02 +00001694
1695 if (fin == NULL) {
Alex Martellia9b9c9f2003-04-23 13:34:35 +00001696 PyErr_SetString(PyExc_RuntimeError, "[raw_]input: lost sys.stdin");
Martin v. Löwis31d2df52002-08-14 15:46:02 +00001697 return NULL;
1698 }
1699 if (fout == NULL) {
Alex Martellia9b9c9f2003-04-23 13:34:35 +00001700 PyErr_SetString(PyExc_RuntimeError, "[raw_]input: lost sys.stdout");
Martin v. Löwis31d2df52002-08-14 15:46:02 +00001701 return NULL;
1702 }
1703 if (PyFile_SoftSpace(fout, 0)) {
1704 if (PyFile_WriteString(" ", fout) != 0)
1705 return NULL;
1706 }
Michael W. Hudson52db5192004-08-02 13:21:09 +00001707 if (PyFile_Check(fin) && PyFile_Check(fout)
Martin v. Löwis566f6af2002-10-26 14:39:10 +00001708 && isatty(fileno(PyFile_AsFile(fin)))
1709 && isatty(fileno(PyFile_AsFile(fout)))) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001710 PyObject *po;
Guido van Rossum872537c1995-07-07 22:43:42 +00001711 char *prompt;
1712 char *s;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001713 PyObject *result;
Guido van Rossum872537c1995-07-07 22:43:42 +00001714 if (v != NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001715 po = PyObject_Str(v);
Guido van Rossum872537c1995-07-07 22:43:42 +00001716 if (po == NULL)
1717 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001718 prompt = PyString_AsString(po);
Guido van Rossumd9b52081998-06-26 18:25:38 +00001719 if (prompt == NULL)
1720 return NULL;
Guido van Rossum872537c1995-07-07 22:43:42 +00001721 }
1722 else {
1723 po = NULL;
1724 prompt = "";
1725 }
Michael W. Hudson52db5192004-08-02 13:21:09 +00001726 s = PyOS_Readline(PyFile_AsFile(fin), PyFile_AsFile(fout),
Martin v. Löwis566f6af2002-10-26 14:39:10 +00001727 prompt);
Guido van Rossum79f25d91997-04-29 20:08:16 +00001728 Py_XDECREF(po);
Guido van Rossum872537c1995-07-07 22:43:42 +00001729 if (s == NULL) {
Michael W. Hudson30ea2f22004-07-07 17:44:12 +00001730 if (!PyErr_Occurred())
1731 PyErr_SetNone(PyExc_KeyboardInterrupt);
Guido van Rossum872537c1995-07-07 22:43:42 +00001732 return NULL;
1733 }
1734 if (*s == '\0') {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001735 PyErr_SetNone(PyExc_EOFError);
Guido van Rossum872537c1995-07-07 22:43:42 +00001736 result = NULL;
1737 }
1738 else { /* strip trailing '\n' */
Guido van Rossum106f2da2000-06-28 21:12:25 +00001739 size_t len = strlen(s);
1740 if (len > INT_MAX) {
Martin v. Löwis31d2df52002-08-14 15:46:02 +00001741 PyErr_SetString(PyExc_OverflowError,
Alex Martellia9b9c9f2003-04-23 13:34:35 +00001742 "[raw_]input: input too long");
Guido van Rossum106f2da2000-06-28 21:12:25 +00001743 result = NULL;
1744 }
1745 else {
Martin v. Löwis31d2df52002-08-14 15:46:02 +00001746 result = PyString_FromStringAndSize(s,
1747 (int)(len-1));
Guido van Rossum106f2da2000-06-28 21:12:25 +00001748 }
Guido van Rossum872537c1995-07-07 22:43:42 +00001749 }
Guido van Rossumb18618d2000-05-03 23:44:39 +00001750 PyMem_FREE(s);
Guido van Rossum872537c1995-07-07 22:43:42 +00001751 return result;
1752 }
Guido van Rossum90933611991-06-07 16:10:43 +00001753 if (v != NULL) {
Martin v. Löwis31d2df52002-08-14 15:46:02 +00001754 if (PyFile_WriteObject(v, fout, Py_PRINT_RAW) != 0)
Guido van Rossum90933611991-06-07 16:10:43 +00001755 return NULL;
1756 }
Martin v. Löwis31d2df52002-08-14 15:46:02 +00001757 return PyFile_GetLine(fin, -1);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001758}
1759
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001760PyDoc_STRVAR(raw_input_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001761"raw_input([prompt]) -> string\n\
1762\n\
1763Read a string from standard input. The trailing newline is stripped.\n\
1764If the user hits EOF (Unix: Ctl-D, Windows: Ctl-Z+Return), raise EOFError.\n\
1765On Unix, GNU readline is used if enabled. The prompt string, if given,\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001766is printed without a trailing newline before reading.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001767
1768
Guido van Rossum79f25d91997-04-29 20:08:16 +00001769static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001770builtin_reduce(PyObject *self, PyObject *args)
Guido van Rossum12d12c51993-10-26 17:58:25 +00001771{
Tim Peters15d81ef2001-05-04 04:39:21 +00001772 PyObject *seq, *func, *result = NULL, *it;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001773
Raymond Hettingerea3fdf42002-12-29 16:33:45 +00001774 if (!PyArg_UnpackTuple(args, "reduce", 2, 3, &func, &seq, &result))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001775 return NULL;
1776 if (result != NULL)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001777 Py_INCREF(result);
Guido van Rossum12d12c51993-10-26 17:58:25 +00001778
Tim Peters15d81ef2001-05-04 04:39:21 +00001779 it = PyObject_GetIter(seq);
1780 if (it == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001781 PyErr_SetString(PyExc_TypeError,
Tim Peters15d81ef2001-05-04 04:39:21 +00001782 "reduce() arg 2 must support iteration");
1783 Py_XDECREF(result);
Guido van Rossum12d12c51993-10-26 17:58:25 +00001784 return NULL;
1785 }
1786
Guido van Rossum79f25d91997-04-29 20:08:16 +00001787 if ((args = PyTuple_New(2)) == NULL)
Guido van Rossum2d951851994-08-29 12:52:16 +00001788 goto Fail;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001789
Tim Peters15d81ef2001-05-04 04:39:21 +00001790 for (;;) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001791 PyObject *op2;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001792
1793 if (args->ob_refcnt > 1) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001794 Py_DECREF(args);
1795 if ((args = PyTuple_New(2)) == NULL)
Guido van Rossum2d951851994-08-29 12:52:16 +00001796 goto Fail;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001797 }
1798
Tim Peters15d81ef2001-05-04 04:39:21 +00001799 op2 = PyIter_Next(it);
1800 if (op2 == NULL) {
Tim Petersf4848da2001-05-05 00:14:56 +00001801 if (PyErr_Occurred())
1802 goto Fail;
1803 break;
Guido van Rossum2d951851994-08-29 12:52:16 +00001804 }
Guido van Rossum12d12c51993-10-26 17:58:25 +00001805
Guido van Rossum2d951851994-08-29 12:52:16 +00001806 if (result == NULL)
1807 result = op2;
1808 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001809 PyTuple_SetItem(args, 0, result);
1810 PyTuple_SetItem(args, 1, op2);
1811 if ((result = PyEval_CallObject(func, args)) == NULL)
Guido van Rossum2d951851994-08-29 12:52:16 +00001812 goto Fail;
1813 }
Guido van Rossum12d12c51993-10-26 17:58:25 +00001814 }
1815
Guido van Rossum79f25d91997-04-29 20:08:16 +00001816 Py_DECREF(args);
Guido van Rossum12d12c51993-10-26 17:58:25 +00001817
Guido van Rossum2d951851994-08-29 12:52:16 +00001818 if (result == NULL)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001819 PyErr_SetString(PyExc_TypeError,
Fred Drake661ea262000-10-24 19:57:45 +00001820 "reduce() of empty sequence with no initial value");
Guido van Rossum2d951851994-08-29 12:52:16 +00001821
Tim Peters15d81ef2001-05-04 04:39:21 +00001822 Py_DECREF(it);
Guido van Rossum12d12c51993-10-26 17:58:25 +00001823 return result;
1824
Guido van Rossum2d951851994-08-29 12:52:16 +00001825Fail:
Guido van Rossum79f25d91997-04-29 20:08:16 +00001826 Py_XDECREF(args);
1827 Py_XDECREF(result);
Tim Peters15d81ef2001-05-04 04:39:21 +00001828 Py_DECREF(it);
Guido van Rossum12d12c51993-10-26 17:58:25 +00001829 return NULL;
1830}
1831
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001832PyDoc_STRVAR(reduce_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001833"reduce(function, sequence[, initial]) -> value\n\
1834\n\
1835Apply a function of two arguments cumulatively to the items of a sequence,\n\
1836from left to right, so as to reduce the sequence to a single value.\n\
1837For example, reduce(lambda x, y: x+y, [1, 2, 3, 4, 5]) calculates\n\
1838((((1+2)+3)+4)+5). If initial is present, it is placed before the items\n\
1839of the sequence in the calculation, and serves as a default when the\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001840sequence is empty.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001841
1842
Guido van Rossum79f25d91997-04-29 20:08:16 +00001843static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001844builtin_reload(PyObject *self, PyObject *v)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001845{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001846 return PyImport_ReloadModule(v);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001847}
1848
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001849PyDoc_STRVAR(reload_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001850"reload(module) -> module\n\
1851\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001852Reload the module. The module must have been successfully imported before.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001853
1854
Guido van Rossum79f25d91997-04-29 20:08:16 +00001855static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001856builtin_repr(PyObject *self, PyObject *v)
Guido van Rossumc89705d1992-11-26 08:54:07 +00001857{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001858 return PyObject_Repr(v);
Guido van Rossumc89705d1992-11-26 08:54:07 +00001859}
1860
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001861PyDoc_STRVAR(repr_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001862"repr(object) -> string\n\
1863\n\
1864Return the canonical string representation of the object.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001865For most object types, eval(repr(object)) == object.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001866
1867
Guido van Rossum79f25d91997-04-29 20:08:16 +00001868static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001869builtin_round(PyObject *self, PyObject *args)
Guido van Rossum9e51f9b1993-02-12 16:29:05 +00001870{
Guido van Rossum9e51f9b1993-02-12 16:29:05 +00001871 double x;
1872 double f;
1873 int ndigits = 0;
Guido van Rossum9e51f9b1993-02-12 16:29:05 +00001874 int i;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001875
Guido van Rossum79f25d91997-04-29 20:08:16 +00001876 if (!PyArg_ParseTuple(args, "d|i:round", &x, &ndigits))
Guido van Rossum9e51f9b1993-02-12 16:29:05 +00001877 return NULL;
Guido van Rossum9e51f9b1993-02-12 16:29:05 +00001878 f = 1.0;
Guido van Rossum1e162d31998-05-09 14:42:25 +00001879 i = abs(ndigits);
1880 while (--i >= 0)
Guido van Rossum9e51f9b1993-02-12 16:29:05 +00001881 f = f*10.0;
Guido van Rossum1e162d31998-05-09 14:42:25 +00001882 if (ndigits < 0)
1883 x /= f;
Guido van Rossum9e51f9b1993-02-12 16:29:05 +00001884 else
Guido van Rossum1e162d31998-05-09 14:42:25 +00001885 x *= f;
1886 if (x >= 0.0)
1887 x = floor(x + 0.5);
1888 else
1889 x = ceil(x - 0.5);
1890 if (ndigits < 0)
1891 x *= f;
1892 else
1893 x /= f;
1894 return PyFloat_FromDouble(x);
Guido van Rossum9e51f9b1993-02-12 16:29:05 +00001895}
1896
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001897PyDoc_STRVAR(round_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001898"round(number[, ndigits]) -> floating point number\n\
1899\n\
1900Round a number to a given precision in decimal digits (default 0 digits).\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001901This always returns a floating point number. Precision may be negative.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001902
Raymond Hettinger64958a12003-12-17 20:43:33 +00001903static PyObject *
1904builtin_sorted(PyObject *self, PyObject *args, PyObject *kwds)
1905{
1906 PyObject *newlist, *v, *seq, *compare=NULL, *keyfunc=NULL, *newargs;
1907 PyObject *callable;
1908 static char *kwlist[] = {"iterable", "cmp", "key", "reverse", 0};
1909 long reverse;
1910
1911 if (args != NULL) {
1912 if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|OOi:sorted",
1913 kwlist, &seq, &compare, &keyfunc, &reverse))
1914 return NULL;
1915 }
1916
1917 newlist = PySequence_List(seq);
1918 if (newlist == NULL)
1919 return NULL;
1920
1921 callable = PyObject_GetAttrString(newlist, "sort");
1922 if (callable == NULL) {
1923 Py_DECREF(newlist);
1924 return NULL;
1925 }
Georg Brandl99d7e4e2005-08-31 22:21:15 +00001926
Raymond Hettinger64958a12003-12-17 20:43:33 +00001927 newargs = PyTuple_GetSlice(args, 1, 4);
1928 if (newargs == NULL) {
1929 Py_DECREF(newlist);
1930 Py_DECREF(callable);
1931 return NULL;
1932 }
1933
1934 v = PyObject_Call(callable, newargs, kwds);
1935 Py_DECREF(newargs);
1936 Py_DECREF(callable);
1937 if (v == NULL) {
1938 Py_DECREF(newlist);
1939 return NULL;
1940 }
1941 Py_DECREF(v);
1942 return newlist;
1943}
1944
1945PyDoc_STRVAR(sorted_doc,
1946"sorted(iterable, cmp=None, key=None, reverse=False) --> new sorted list");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001947
Guido van Rossum79f25d91997-04-29 20:08:16 +00001948static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001949builtin_vars(PyObject *self, PyObject *args)
Guido van Rossum2d951851994-08-29 12:52:16 +00001950{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001951 PyObject *v = NULL;
1952 PyObject *d;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001953
Raymond Hettingerea3fdf42002-12-29 16:33:45 +00001954 if (!PyArg_UnpackTuple(args, "vars", 0, 1, &v))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001955 return NULL;
Guido van Rossum2d951851994-08-29 12:52:16 +00001956 if (v == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001957 d = PyEval_GetLocals();
Guido van Rossum53bb7ff1995-07-26 16:26:31 +00001958 if (d == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001959 if (!PyErr_Occurred())
1960 PyErr_SetString(PyExc_SystemError,
Alex Martellia9b9c9f2003-04-23 13:34:35 +00001961 "vars(): no locals!?");
Guido van Rossum53bb7ff1995-07-26 16:26:31 +00001962 }
1963 else
Guido van Rossum79f25d91997-04-29 20:08:16 +00001964 Py_INCREF(d);
Guido van Rossum2d951851994-08-29 12:52:16 +00001965 }
1966 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001967 d = PyObject_GetAttrString(v, "__dict__");
Guido van Rossum2d951851994-08-29 12:52:16 +00001968 if (d == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001969 PyErr_SetString(PyExc_TypeError,
Guido van Rossum2d951851994-08-29 12:52:16 +00001970 "vars() argument must have __dict__ attribute");
1971 return NULL;
1972 }
1973 }
1974 return d;
1975}
1976
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001977PyDoc_STRVAR(vars_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001978"vars([object]) -> dictionary\n\
1979\n\
1980Without arguments, equivalent to locals().\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001981With an argument, equivalent to object.__dict__.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001982
Alex Martellia70b1912003-04-22 08:12:33 +00001983
1984static PyObject*
1985builtin_sum(PyObject *self, PyObject *args)
1986{
1987 PyObject *seq;
1988 PyObject *result = NULL;
1989 PyObject *temp, *item, *iter;
1990
Raymond Hettinger5cf63942003-10-25 06:41:37 +00001991 if (!PyArg_UnpackTuple(args, "sum", 1, 2, &seq, &result))
Alex Martellia70b1912003-04-22 08:12:33 +00001992 return NULL;
1993
1994 iter = PyObject_GetIter(seq);
1995 if (iter == NULL)
1996 return NULL;
1997
1998 if (result == NULL) {
1999 result = PyInt_FromLong(0);
2000 if (result == NULL) {
2001 Py_DECREF(iter);
2002 return NULL;
2003 }
2004 } else {
2005 /* reject string values for 'start' parameter */
2006 if (PyObject_TypeCheck(result, &PyBaseString_Type)) {
2007 PyErr_SetString(PyExc_TypeError,
Alex Martellia9b9c9f2003-04-23 13:34:35 +00002008 "sum() can't sum strings [use ''.join(seq) instead]");
Alex Martellia70b1912003-04-22 08:12:33 +00002009 Py_DECREF(iter);
2010 return NULL;
2011 }
Alex Martelli41c9f882003-04-22 09:24:48 +00002012 Py_INCREF(result);
Alex Martellia70b1912003-04-22 08:12:33 +00002013 }
2014
2015 for(;;) {
2016 item = PyIter_Next(iter);
2017 if (item == NULL) {
2018 /* error, or end-of-sequence */
2019 if (PyErr_Occurred()) {
2020 Py_DECREF(result);
2021 result = NULL;
2022 }
2023 break;
2024 }
Alex Martellia253e182003-10-25 23:24:14 +00002025 temp = PyNumber_Add(result, item);
Alex Martellia70b1912003-04-22 08:12:33 +00002026 Py_DECREF(result);
2027 Py_DECREF(item);
2028 result = temp;
2029 if (result == NULL)
2030 break;
2031 }
2032 Py_DECREF(iter);
2033 return result;
2034}
2035
2036PyDoc_STRVAR(sum_doc,
2037"sum(sequence, start=0) -> value\n\
2038\n\
2039Returns the sum of a sequence of numbers (NOT strings) plus the value\n\
2040of parameter 'start'. When the sequence is empty, returns start.");
2041
2042
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002043static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002044builtin_isinstance(PyObject *self, PyObject *args)
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002045{
2046 PyObject *inst;
2047 PyObject *cls;
Guido van Rossum823649d2001-03-21 18:40:58 +00002048 int retval;
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002049
Raymond Hettingerea3fdf42002-12-29 16:33:45 +00002050 if (!PyArg_UnpackTuple(args, "isinstance", 2, 2, &inst, &cls))
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002051 return NULL;
Guido van Rossumf5dd9141997-12-02 19:11:45 +00002052
Guido van Rossum823649d2001-03-21 18:40:58 +00002053 retval = PyObject_IsInstance(inst, cls);
2054 if (retval < 0)
Guido van Rossumad991772001-01-12 16:03:05 +00002055 return NULL;
Guido van Rossum77f6a652002-04-03 22:41:51 +00002056 return PyBool_FromLong(retval);
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002057}
2058
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002059PyDoc_STRVAR(isinstance_doc,
Guido van Rossum77f6a652002-04-03 22:41:51 +00002060"isinstance(object, class-or-type-or-tuple) -> bool\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002061\n\
2062Return whether an object is an instance of a class or of a subclass thereof.\n\
Guido van Rossum03290ec2001-10-07 20:54:12 +00002063With a type as second argument, return whether that is the object's type.\n\
2064The form using a tuple, isinstance(x, (A, B, ...)), is a shortcut for\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002065isinstance(x, A) or isinstance(x, B) or ... (etc.).");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002066
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002067
2068static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002069builtin_issubclass(PyObject *self, PyObject *args)
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002070{
2071 PyObject *derived;
2072 PyObject *cls;
2073 int retval;
2074
Raymond Hettingerea3fdf42002-12-29 16:33:45 +00002075 if (!PyArg_UnpackTuple(args, "issubclass", 2, 2, &derived, &cls))
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002076 return NULL;
Guido van Rossum668213d1999-06-16 17:28:37 +00002077
Guido van Rossum823649d2001-03-21 18:40:58 +00002078 retval = PyObject_IsSubclass(derived, cls);
2079 if (retval < 0)
2080 return NULL;
Guido van Rossum77f6a652002-04-03 22:41:51 +00002081 return PyBool_FromLong(retval);
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002082}
2083
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002084PyDoc_STRVAR(issubclass_doc,
Guido van Rossum77f6a652002-04-03 22:41:51 +00002085"issubclass(C, B) -> bool\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002086\n\
Walter Dörwaldd9a6ad32002-12-12 16:41:44 +00002087Return whether class C is a subclass (i.e., a derived class) of class B.\n\
2088When using a tuple as the second argument issubclass(X, (A, B, ...)),\n\
2089is a shortcut for issubclass(X, A) or issubclass(X, B) or ... (etc.).");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002090
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002091
Barry Warsawbd599b52000-08-03 15:45:29 +00002092static PyObject*
2093builtin_zip(PyObject *self, PyObject *args)
2094{
2095 PyObject *ret;
Tim Peters67d687a2002-04-29 21:27:32 +00002096 const int itemsize = PySequence_Length(args);
Tim Peters8572b4f2001-05-06 01:05:02 +00002097 int i;
2098 PyObject *itlist; /* tuple of iterators */
Tim Peters67d687a2002-04-29 21:27:32 +00002099 int len; /* guess at result length */
Barry Warsawbd599b52000-08-03 15:45:29 +00002100
Raymond Hettingereaef6152003-08-02 07:42:57 +00002101 if (itemsize == 0)
2102 return PyList_New(0);
2103
Barry Warsawbd599b52000-08-03 15:45:29 +00002104 /* args must be a tuple */
2105 assert(PyTuple_Check(args));
2106
Tim Peters39a86c22002-05-12 07:19:38 +00002107 /* Guess at result length: the shortest of the input lengths.
2108 If some argument refuses to say, we refuse to guess too, lest
2109 an argument like xrange(sys.maxint) lead us astray.*/
Tim Peters67d687a2002-04-29 21:27:32 +00002110 len = -1; /* unknown */
2111 for (i = 0; i < itemsize; ++i) {
2112 PyObject *item = PyTuple_GET_ITEM(args, i);
Raymond Hettinger77f3c872004-01-04 08:54:44 +00002113 int thislen = PyObject_Size(item);
Tim Peters39a86c22002-05-12 07:19:38 +00002114 if (thislen < 0) {
Raymond Hettingera710b332005-08-21 11:03:59 +00002115 if (!PyErr_ExceptionMatches(PyExc_TypeError) &&
2116 !PyErr_ExceptionMatches(PyExc_AttributeError)) {
2117 return NULL;
2118 }
Tim Peters67d687a2002-04-29 21:27:32 +00002119 PyErr_Clear();
Tim Peters39a86c22002-05-12 07:19:38 +00002120 len = -1;
2121 break;
2122 }
Tim Peters67d687a2002-04-29 21:27:32 +00002123 else if (len < 0 || thislen < len)
2124 len = thislen;
2125 }
2126
Tim Peters8572b4f2001-05-06 01:05:02 +00002127 /* allocate result list */
Tim Peters67d687a2002-04-29 21:27:32 +00002128 if (len < 0)
2129 len = 10; /* arbitrary */
2130 if ((ret = PyList_New(len)) == NULL)
Barry Warsawbd599b52000-08-03 15:45:29 +00002131 return NULL;
2132
Tim Peters8572b4f2001-05-06 01:05:02 +00002133 /* obtain iterators */
2134 itlist = PyTuple_New(itemsize);
2135 if (itlist == NULL)
2136 goto Fail_ret;
2137 for (i = 0; i < itemsize; ++i) {
2138 PyObject *item = PyTuple_GET_ITEM(args, i);
2139 PyObject *it = PyObject_GetIter(item);
2140 if (it == NULL) {
2141 if (PyErr_ExceptionMatches(PyExc_TypeError))
2142 PyErr_Format(PyExc_TypeError,
2143 "zip argument #%d must support iteration",
2144 i+1);
2145 goto Fail_ret_itlist;
Barry Warsawbd599b52000-08-03 15:45:29 +00002146 }
Tim Peters8572b4f2001-05-06 01:05:02 +00002147 PyTuple_SET_ITEM(itlist, i, it);
2148 }
Barry Warsawbd599b52000-08-03 15:45:29 +00002149
Tim Peters8572b4f2001-05-06 01:05:02 +00002150 /* build result into ret list */
Tim Peters67d687a2002-04-29 21:27:32 +00002151 for (i = 0; ; ++i) {
2152 int j;
Tim Peters8572b4f2001-05-06 01:05:02 +00002153 PyObject *next = PyTuple_New(itemsize);
2154 if (!next)
2155 goto Fail_ret_itlist;
2156
Tim Peters67d687a2002-04-29 21:27:32 +00002157 for (j = 0; j < itemsize; j++) {
2158 PyObject *it = PyTuple_GET_ITEM(itlist, j);
Tim Peters8572b4f2001-05-06 01:05:02 +00002159 PyObject *item = PyIter_Next(it);
Barry Warsawbd599b52000-08-03 15:45:29 +00002160 if (!item) {
Tim Peters8572b4f2001-05-06 01:05:02 +00002161 if (PyErr_Occurred()) {
2162 Py_DECREF(ret);
2163 ret = NULL;
Barry Warsawbd599b52000-08-03 15:45:29 +00002164 }
2165 Py_DECREF(next);
Tim Peters8572b4f2001-05-06 01:05:02 +00002166 Py_DECREF(itlist);
Tim Peters67d687a2002-04-29 21:27:32 +00002167 goto Done;
Barry Warsawbd599b52000-08-03 15:45:29 +00002168 }
Tim Peters67d687a2002-04-29 21:27:32 +00002169 PyTuple_SET_ITEM(next, j, item);
Barry Warsawbd599b52000-08-03 15:45:29 +00002170 }
Tim Peters8572b4f2001-05-06 01:05:02 +00002171
Tim Peters67d687a2002-04-29 21:27:32 +00002172 if (i < len)
2173 PyList_SET_ITEM(ret, i, next);
2174 else {
2175 int status = PyList_Append(ret, next);
2176 Py_DECREF(next);
2177 ++len;
2178 if (status < 0)
2179 goto Fail_ret_itlist;
2180 }
Barry Warsawbd599b52000-08-03 15:45:29 +00002181 }
Tim Peters8572b4f2001-05-06 01:05:02 +00002182
Tim Peters67d687a2002-04-29 21:27:32 +00002183Done:
2184 if (ret != NULL && i < len) {
2185 /* The list is too big. */
2186 if (PyList_SetSlice(ret, i, len, NULL) < 0)
2187 return NULL;
2188 }
2189 return ret;
2190
Tim Peters8572b4f2001-05-06 01:05:02 +00002191Fail_ret_itlist:
2192 Py_DECREF(itlist);
2193Fail_ret:
2194 Py_DECREF(ret);
2195 return NULL;
Barry Warsawbd599b52000-08-03 15:45:29 +00002196}
2197
2198
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002199PyDoc_STRVAR(zip_doc,
Barry Warsawbd599b52000-08-03 15:45:29 +00002200"zip(seq1 [, seq2 [...]]) -> [(seq1[0], seq2[0] ...), (...)]\n\
2201\n\
2202Return a list of tuples, where each tuple contains the i-th element\n\
2203from each of the argument sequences. The returned list is truncated\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002204in length to the length of the shortest argument sequence.");
Barry Warsawbd599b52000-08-03 15:45:29 +00002205
2206
Guido van Rossum79f25d91997-04-29 20:08:16 +00002207static PyMethodDef builtin_methods[] = {
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002208 {"__import__", builtin___import__, METH_VARARGS, import_doc},
2209 {"abs", builtin_abs, METH_O, abs_doc},
Raymond Hettinger96229b12005-03-11 06:49:40 +00002210 {"all", builtin_all, METH_O, all_doc},
2211 {"any", builtin_any, METH_O, any_doc},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002212 {"apply", builtin_apply, METH_VARARGS, apply_doc},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002213 {"callable", builtin_callable, METH_O, callable_doc},
2214 {"chr", builtin_chr, METH_VARARGS, chr_doc},
2215 {"cmp", builtin_cmp, METH_VARARGS, cmp_doc},
2216 {"coerce", builtin_coerce, METH_VARARGS, coerce_doc},
2217 {"compile", builtin_compile, METH_VARARGS, compile_doc},
2218 {"delattr", builtin_delattr, METH_VARARGS, delattr_doc},
2219 {"dir", builtin_dir, METH_VARARGS, dir_doc},
2220 {"divmod", builtin_divmod, METH_VARARGS, divmod_doc},
2221 {"eval", builtin_eval, METH_VARARGS, eval_doc},
2222 {"execfile", builtin_execfile, METH_VARARGS, execfile_doc},
2223 {"filter", builtin_filter, METH_VARARGS, filter_doc},
2224 {"getattr", builtin_getattr, METH_VARARGS, getattr_doc},
2225 {"globals", (PyCFunction)builtin_globals, METH_NOARGS, globals_doc},
2226 {"hasattr", builtin_hasattr, METH_VARARGS, hasattr_doc},
2227 {"hash", builtin_hash, METH_O, hash_doc},
2228 {"hex", builtin_hex, METH_O, hex_doc},
2229 {"id", builtin_id, METH_O, id_doc},
2230 {"input", builtin_input, METH_VARARGS, input_doc},
2231 {"intern", builtin_intern, METH_VARARGS, intern_doc},
2232 {"isinstance", builtin_isinstance, METH_VARARGS, isinstance_doc},
2233 {"issubclass", builtin_issubclass, METH_VARARGS, issubclass_doc},
2234 {"iter", builtin_iter, METH_VARARGS, iter_doc},
2235 {"len", builtin_len, METH_O, len_doc},
2236 {"locals", (PyCFunction)builtin_locals, METH_NOARGS, locals_doc},
2237 {"map", builtin_map, METH_VARARGS, map_doc},
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00002238 {"max", (PyCFunction)builtin_max, METH_VARARGS | METH_KEYWORDS, max_doc},
2239 {"min", (PyCFunction)builtin_min, METH_VARARGS | METH_KEYWORDS, min_doc},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002240 {"oct", builtin_oct, METH_O, oct_doc},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002241 {"ord", builtin_ord, METH_O, ord_doc},
2242 {"pow", builtin_pow, METH_VARARGS, pow_doc},
2243 {"range", builtin_range, METH_VARARGS, range_doc},
2244 {"raw_input", builtin_raw_input, METH_VARARGS, raw_input_doc},
2245 {"reduce", builtin_reduce, METH_VARARGS, reduce_doc},
2246 {"reload", builtin_reload, METH_O, reload_doc},
2247 {"repr", builtin_repr, METH_O, repr_doc},
2248 {"round", builtin_round, METH_VARARGS, round_doc},
2249 {"setattr", builtin_setattr, METH_VARARGS, setattr_doc},
Raymond Hettinger64958a12003-12-17 20:43:33 +00002250 {"sorted", (PyCFunction)builtin_sorted, METH_VARARGS | METH_KEYWORDS, sorted_doc},
Alex Martellia70b1912003-04-22 08:12:33 +00002251 {"sum", builtin_sum, METH_VARARGS, sum_doc},
Martin v. Löwis339d0f72001-08-17 18:39:25 +00002252#ifdef Py_USING_UNICODE
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002253 {"unichr", builtin_unichr, METH_VARARGS, unichr_doc},
Martin v. Löwis339d0f72001-08-17 18:39:25 +00002254#endif
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002255 {"vars", builtin_vars, METH_VARARGS, vars_doc},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002256 {"zip", builtin_zip, METH_VARARGS, zip_doc},
Guido van Rossumc02e15c1991-12-16 13:03:00 +00002257 {NULL, NULL},
Guido van Rossum3f5da241990-12-20 15:06:42 +00002258};
2259
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002260PyDoc_STRVAR(builtin_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002261"Built-in functions, exceptions, and other objects.\n\
2262\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002263Noteworthy: None is the `nil' object; Ellipsis represents `...' in slices.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002264
Guido van Rossum25ce5661997-08-02 03:10:38 +00002265PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002266_PyBuiltin_Init(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +00002267{
Fred Drake5550de32000-06-20 04:54:19 +00002268 PyObject *mod, *dict, *debug;
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002269 mod = Py_InitModule4("__builtin__", builtin_methods,
2270 builtin_doc, (PyObject *)NULL,
2271 PYTHON_API_VERSION);
Guido van Rossum25ce5661997-08-02 03:10:38 +00002272 if (mod == NULL)
2273 return NULL;
2274 dict = PyModule_GetDict(mod);
Tim Peters4b7625e2001-09-13 21:37:17 +00002275
Tim Peters7571a0f2003-03-23 17:52:28 +00002276#ifdef Py_TRACE_REFS
2277 /* __builtin__ exposes a number of statically allocated objects
2278 * that, before this code was added in 2.3, never showed up in
2279 * the list of "all objects" maintained by Py_TRACE_REFS. As a
2280 * result, programs leaking references to None and False (etc)
2281 * couldn't be diagnosed by examining sys.getobjects(0).
2282 */
2283#define ADD_TO_ALL(OBJECT) _Py_AddToAllObjects((PyObject *)(OBJECT), 0)
2284#else
2285#define ADD_TO_ALL(OBJECT) (void)0
2286#endif
2287
Tim Peters4b7625e2001-09-13 21:37:17 +00002288#define SETBUILTIN(NAME, OBJECT) \
Tim Peters7571a0f2003-03-23 17:52:28 +00002289 if (PyDict_SetItemString(dict, NAME, (PyObject *)OBJECT) < 0) \
2290 return NULL; \
2291 ADD_TO_ALL(OBJECT)
Tim Peters4b7625e2001-09-13 21:37:17 +00002292
2293 SETBUILTIN("None", Py_None);
2294 SETBUILTIN("Ellipsis", Py_Ellipsis);
2295 SETBUILTIN("NotImplemented", Py_NotImplemented);
Guido van Rossum77f6a652002-04-03 22:41:51 +00002296 SETBUILTIN("False", Py_False);
2297 SETBUILTIN("True", Py_True);
Neal Norwitz32a7e7f2002-05-31 19:58:02 +00002298 SETBUILTIN("basestring", &PyBaseString_Type);
Guido van Rossum77f6a652002-04-03 22:41:51 +00002299 SETBUILTIN("bool", &PyBool_Type);
Guido van Rossumbea18cc2002-06-14 20:41:17 +00002300 SETBUILTIN("buffer", &PyBuffer_Type);
Tim Peters4b7625e2001-09-13 21:37:17 +00002301 SETBUILTIN("classmethod", &PyClassMethod_Type);
Tim Peters6d6c1a32001-08-02 04:15:00 +00002302#ifndef WITHOUT_COMPLEX
Tim Peters4b7625e2001-09-13 21:37:17 +00002303 SETBUILTIN("complex", &PyComplex_Type);
Tim Peters6d6c1a32001-08-02 04:15:00 +00002304#endif
Tim Petersa427a2b2001-10-29 22:25:45 +00002305 SETBUILTIN("dict", &PyDict_Type);
Tim Peters67d687a2002-04-29 21:27:32 +00002306 SETBUILTIN("enumerate", &PyEnum_Type);
Tim Peters4b7625e2001-09-13 21:37:17 +00002307 SETBUILTIN("float", &PyFloat_Type);
Raymond Hettingera690a992003-11-16 16:17:49 +00002308 SETBUILTIN("frozenset", &PyFrozenSet_Type);
Tim Peters4b7625e2001-09-13 21:37:17 +00002309 SETBUILTIN("property", &PyProperty_Type);
2310 SETBUILTIN("int", &PyInt_Type);
2311 SETBUILTIN("list", &PyList_Type);
2312 SETBUILTIN("long", &PyLong_Type);
2313 SETBUILTIN("object", &PyBaseObject_Type);
Raymond Hettinger85c20a42003-11-06 14:06:48 +00002314 SETBUILTIN("reversed", &PyReversed_Type);
Raymond Hettingera690a992003-11-16 16:17:49 +00002315 SETBUILTIN("set", &PySet_Type);
Guido van Rossumbea18cc2002-06-14 20:41:17 +00002316 SETBUILTIN("slice", &PySlice_Type);
Tim Peters4b7625e2001-09-13 21:37:17 +00002317 SETBUILTIN("staticmethod", &PyStaticMethod_Type);
2318 SETBUILTIN("str", &PyString_Type);
2319 SETBUILTIN("super", &PySuper_Type);
2320 SETBUILTIN("tuple", &PyTuple_Type);
2321 SETBUILTIN("type", &PyType_Type);
Raymond Hettingerc4c453f2002-06-05 23:12:45 +00002322 SETBUILTIN("xrange", &PyRange_Type);
Tim Peters742dfd62001-09-13 21:49:44 +00002323
2324 /* Note that open() is just an alias of file(). */
2325 SETBUILTIN("open", &PyFile_Type);
Tim Peters4b7625e2001-09-13 21:37:17 +00002326 SETBUILTIN("file", &PyFile_Type);
Martin v. Löwis339d0f72001-08-17 18:39:25 +00002327#ifdef Py_USING_UNICODE
Tim Peters4b7625e2001-09-13 21:37:17 +00002328 SETBUILTIN("unicode", &PyUnicode_Type);
Martin v. Löwis339d0f72001-08-17 18:39:25 +00002329#endif
Guido van Rossum77f6a652002-04-03 22:41:51 +00002330 debug = PyBool_FromLong(Py_OptimizeFlag == 0);
Fred Drake5550de32000-06-20 04:54:19 +00002331 if (PyDict_SetItemString(dict, "__debug__", debug) < 0) {
2332 Py_XDECREF(debug);
Guido van Rossum25ce5661997-08-02 03:10:38 +00002333 return NULL;
Fred Drake5550de32000-06-20 04:54:19 +00002334 }
2335 Py_XDECREF(debug);
Barry Warsaw757af0e1997-08-29 22:13:51 +00002336
Guido van Rossum25ce5661997-08-02 03:10:38 +00002337 return mod;
Tim Peters7571a0f2003-03-23 17:52:28 +00002338#undef ADD_TO_ALL
Tim Peters4b7625e2001-09-13 21:37:17 +00002339#undef SETBUILTIN
Guido van Rossum3f5da241990-12-20 15:06:42 +00002340}
2341
Guido van Rossume77a7571993-11-03 15:01:26 +00002342/* Helper for filter(): filter a tuple through a function */
Guido van Rossum12d12c51993-10-26 17:58:25 +00002343
Guido van Rossum79f25d91997-04-29 20:08:16 +00002344static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002345filtertuple(PyObject *func, PyObject *tuple)
Guido van Rossum12d12c51993-10-26 17:58:25 +00002346{
Guido van Rossum79f25d91997-04-29 20:08:16 +00002347 PyObject *result;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002348 register int i, j;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002349 int len = PyTuple_Size(tuple);
Guido van Rossum12d12c51993-10-26 17:58:25 +00002350
Guido van Rossumb7b45621995-08-04 04:07:45 +00002351 if (len == 0) {
Walter Dörwaldc3da83f2003-02-04 20:24:45 +00002352 if (PyTuple_CheckExact(tuple))
2353 Py_INCREF(tuple);
2354 else
2355 tuple = PyTuple_New(0);
Guido van Rossumb7b45621995-08-04 04:07:45 +00002356 return tuple;
2357 }
2358
Guido van Rossum79f25d91997-04-29 20:08:16 +00002359 if ((result = PyTuple_New(len)) == NULL)
Guido van Rossum2586bf01993-11-01 16:21:44 +00002360 return NULL;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002361
Guido van Rossum12d12c51993-10-26 17:58:25 +00002362 for (i = j = 0; i < len; ++i) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002363 PyObject *item, *good;
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002364 int ok;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002365
Walter Dörwald8dd19322003-02-10 17:36:40 +00002366 if (tuple->ob_type->tp_as_sequence &&
2367 tuple->ob_type->tp_as_sequence->sq_item) {
2368 item = tuple->ob_type->tp_as_sequence->sq_item(tuple, i);
Walter Dörwaldc58a3a12003-08-18 18:28:45 +00002369 if (item == NULL)
2370 goto Fail_1;
Walter Dörwald8dd19322003-02-10 17:36:40 +00002371 } else {
Alex Martellia9b9c9f2003-04-23 13:34:35 +00002372 PyErr_SetString(PyExc_TypeError, "filter(): unsubscriptable tuple");
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002373 goto Fail_1;
Walter Dörwald8dd19322003-02-10 17:36:40 +00002374 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002375 if (func == Py_None) {
2376 Py_INCREF(item);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002377 good = item;
2378 }
2379 else {
Raymond Hettinger8ae46892003-10-12 19:09:37 +00002380 PyObject *arg = PyTuple_Pack(1, item);
Walter Dörwaldc58a3a12003-08-18 18:28:45 +00002381 if (arg == NULL) {
2382 Py_DECREF(item);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002383 goto Fail_1;
Walter Dörwaldc58a3a12003-08-18 18:28:45 +00002384 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002385 good = PyEval_CallObject(func, arg);
2386 Py_DECREF(arg);
Walter Dörwaldc58a3a12003-08-18 18:28:45 +00002387 if (good == NULL) {
2388 Py_DECREF(item);
Guido van Rossum12d12c51993-10-26 17:58:25 +00002389 goto Fail_1;
Walter Dörwaldc58a3a12003-08-18 18:28:45 +00002390 }
Guido van Rossum12d12c51993-10-26 17:58:25 +00002391 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002392 ok = PyObject_IsTrue(good);
2393 Py_DECREF(good);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002394 if (ok) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002395 if (PyTuple_SetItem(result, j++, item) < 0)
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002396 goto Fail_1;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002397 }
Walter Dörwaldc58a3a12003-08-18 18:28:45 +00002398 else
2399 Py_DECREF(item);
Guido van Rossum12d12c51993-10-26 17:58:25 +00002400 }
2401
Tim Peters4324aa32001-05-28 22:30:08 +00002402 if (_PyTuple_Resize(&result, j) < 0)
Guido van Rossum12d12c51993-10-26 17:58:25 +00002403 return NULL;
2404
Guido van Rossum12d12c51993-10-26 17:58:25 +00002405 return result;
2406
Guido van Rossum12d12c51993-10-26 17:58:25 +00002407Fail_1:
Guido van Rossum79f25d91997-04-29 20:08:16 +00002408 Py_DECREF(result);
Guido van Rossum12d12c51993-10-26 17:58:25 +00002409 return NULL;
2410}
2411
2412
Guido van Rossume77a7571993-11-03 15:01:26 +00002413/* Helper for filter(): filter a string through a function */
Guido van Rossum12d12c51993-10-26 17:58:25 +00002414
Guido van Rossum79f25d91997-04-29 20:08:16 +00002415static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002416filterstring(PyObject *func, PyObject *strobj)
Guido van Rossum12d12c51993-10-26 17:58:25 +00002417{
Guido van Rossum79f25d91997-04-29 20:08:16 +00002418 PyObject *result;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002419 register int i, j;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002420 int len = PyString_Size(strobj);
Walter Dörwald903f1e02003-02-04 16:28:00 +00002421 int outlen = len;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002422
Guido van Rossum79f25d91997-04-29 20:08:16 +00002423 if (func == Py_None) {
Walter Dörwald1918f772003-02-10 13:19:13 +00002424 /* If it's a real string we can return the original,
2425 * as no character is ever false and __getitem__
2426 * does return this character. If it's a subclass
2427 * we must go through the __getitem__ loop */
2428 if (PyString_CheckExact(strobj)) {
Walter Dörwaldc3da83f2003-02-04 20:24:45 +00002429 Py_INCREF(strobj);
Walter Dörwald1918f772003-02-10 13:19:13 +00002430 return strobj;
2431 }
Guido van Rossum12d12c51993-10-26 17:58:25 +00002432 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002433 if ((result = PyString_FromStringAndSize(NULL, len)) == NULL)
Guido van Rossum2586bf01993-11-01 16:21:44 +00002434 return NULL;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002435
Guido van Rossum12d12c51993-10-26 17:58:25 +00002436 for (i = j = 0; i < len; ++i) {
Walter Dörwald1918f772003-02-10 13:19:13 +00002437 PyObject *item;
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002438 int ok;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002439
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002440 item = (*strobj->ob_type->tp_as_sequence->sq_item)(strobj, i);
2441 if (item == NULL)
2442 goto Fail_1;
Walter Dörwald1918f772003-02-10 13:19:13 +00002443 if (func==Py_None) {
2444 ok = 1;
2445 } else {
2446 PyObject *arg, *good;
Raymond Hettinger8ae46892003-10-12 19:09:37 +00002447 arg = PyTuple_Pack(1, item);
Walter Dörwald1918f772003-02-10 13:19:13 +00002448 if (arg == NULL) {
2449 Py_DECREF(item);
2450 goto Fail_1;
2451 }
2452 good = PyEval_CallObject(func, arg);
2453 Py_DECREF(arg);
2454 if (good == NULL) {
2455 Py_DECREF(item);
2456 goto Fail_1;
2457 }
2458 ok = PyObject_IsTrue(good);
2459 Py_DECREF(good);
Tim Peters388ed082001-04-07 20:34:48 +00002460 }
Walter Dörwald903f1e02003-02-04 16:28:00 +00002461 if (ok) {
2462 int reslen;
2463 if (!PyString_Check(item)) {
2464 PyErr_SetString(PyExc_TypeError, "can't filter str to str:"
2465 " __getitem__ returned different type");
2466 Py_DECREF(item);
2467 goto Fail_1;
2468 }
2469 reslen = PyString_GET_SIZE(item);
2470 if (reslen == 1) {
2471 PyString_AS_STRING(result)[j++] =
2472 PyString_AS_STRING(item)[0];
2473 } else {
2474 /* do we need more space? */
2475 int need = j + reslen + len-i-1;
2476 if (need > outlen) {
2477 /* overallocate, to avoid reallocations */
2478 if (need<2*outlen)
2479 need = 2*outlen;
2480 if (_PyString_Resize(&result, need)) {
2481 Py_DECREF(item);
2482 return NULL;
2483 }
2484 outlen = need;
2485 }
2486 memcpy(
2487 PyString_AS_STRING(result) + j,
2488 PyString_AS_STRING(item),
2489 reslen
2490 );
2491 j += reslen;
2492 }
2493 }
Tim Peters388ed082001-04-07 20:34:48 +00002494 Py_DECREF(item);
Guido van Rossum12d12c51993-10-26 17:58:25 +00002495 }
2496
Walter Dörwald903f1e02003-02-04 16:28:00 +00002497 if (j < outlen)
Tim Peters5de98422002-04-27 18:44:32 +00002498 _PyString_Resize(&result, j);
Guido van Rossum12d12c51993-10-26 17:58:25 +00002499
Guido van Rossum12d12c51993-10-26 17:58:25 +00002500 return result;
2501
Guido van Rossum12d12c51993-10-26 17:58:25 +00002502Fail_1:
Guido van Rossum79f25d91997-04-29 20:08:16 +00002503 Py_DECREF(result);
Guido van Rossum12d12c51993-10-26 17:58:25 +00002504 return NULL;
2505}
Martin v. Löwis8afd7572003-01-25 22:46:11 +00002506
2507#ifdef Py_USING_UNICODE
2508/* Helper for filter(): filter a Unicode object through a function */
2509
2510static PyObject *
2511filterunicode(PyObject *func, PyObject *strobj)
2512{
2513 PyObject *result;
2514 register int i, j;
2515 int len = PyUnicode_GetSize(strobj);
Walter Dörwald903f1e02003-02-04 16:28:00 +00002516 int outlen = len;
Martin v. Löwis8afd7572003-01-25 22:46:11 +00002517
2518 if (func == Py_None) {
Walter Dörwald1918f772003-02-10 13:19:13 +00002519 /* If it's a real string we can return the original,
2520 * as no character is ever false and __getitem__
2521 * does return this character. If it's a subclass
2522 * we must go through the __getitem__ loop */
2523 if (PyUnicode_CheckExact(strobj)) {
Walter Dörwaldc3da83f2003-02-04 20:24:45 +00002524 Py_INCREF(strobj);
Walter Dörwald1918f772003-02-10 13:19:13 +00002525 return strobj;
2526 }
Martin v. Löwis8afd7572003-01-25 22:46:11 +00002527 }
2528 if ((result = PyUnicode_FromUnicode(NULL, len)) == NULL)
2529 return NULL;
2530
2531 for (i = j = 0; i < len; ++i) {
2532 PyObject *item, *arg, *good;
2533 int ok;
2534
2535 item = (*strobj->ob_type->tp_as_sequence->sq_item)(strobj, i);
2536 if (item == NULL)
2537 goto Fail_1;
Walter Dörwald1918f772003-02-10 13:19:13 +00002538 if (func == Py_None) {
2539 ok = 1;
2540 } else {
Raymond Hettinger8ae46892003-10-12 19:09:37 +00002541 arg = PyTuple_Pack(1, item);
Walter Dörwald1918f772003-02-10 13:19:13 +00002542 if (arg == NULL) {
2543 Py_DECREF(item);
2544 goto Fail_1;
2545 }
2546 good = PyEval_CallObject(func, arg);
2547 Py_DECREF(arg);
2548 if (good == NULL) {
2549 Py_DECREF(item);
2550 goto Fail_1;
2551 }
2552 ok = PyObject_IsTrue(good);
2553 Py_DECREF(good);
Martin v. Löwis8afd7572003-01-25 22:46:11 +00002554 }
Walter Dörwald903f1e02003-02-04 16:28:00 +00002555 if (ok) {
2556 int reslen;
2557 if (!PyUnicode_Check(item)) {
Georg Brandl99d7e4e2005-08-31 22:21:15 +00002558 PyErr_SetString(PyExc_TypeError,
Jeremy Hylton1aad9c72003-09-16 03:10:59 +00002559 "can't filter unicode to unicode:"
2560 " __getitem__ returned different type");
Walter Dörwald903f1e02003-02-04 16:28:00 +00002561 Py_DECREF(item);
2562 goto Fail_1;
2563 }
2564 reslen = PyUnicode_GET_SIZE(item);
Georg Brandl99d7e4e2005-08-31 22:21:15 +00002565 if (reslen == 1)
Walter Dörwald903f1e02003-02-04 16:28:00 +00002566 PyUnicode_AS_UNICODE(result)[j++] =
2567 PyUnicode_AS_UNICODE(item)[0];
Jeremy Hylton1aad9c72003-09-16 03:10:59 +00002568 else {
Walter Dörwald903f1e02003-02-04 16:28:00 +00002569 /* do we need more space? */
Jeremy Hylton1aad9c72003-09-16 03:10:59 +00002570 int need = j + reslen + len - i - 1;
Walter Dörwald903f1e02003-02-04 16:28:00 +00002571 if (need > outlen) {
Georg Brandl99d7e4e2005-08-31 22:21:15 +00002572 /* overallocate,
Jeremy Hylton1aad9c72003-09-16 03:10:59 +00002573 to avoid reallocations */
2574 if (need < 2 * outlen)
2575 need = 2 * outlen;
Jeremy Hylton364f6be2003-09-16 03:17:16 +00002576 if (PyUnicode_Resize(
2577 &result, need) < 0) {
Walter Dörwald903f1e02003-02-04 16:28:00 +00002578 Py_DECREF(item);
Walter Dörwald531e0002003-02-04 16:57:49 +00002579 goto Fail_1;
Walter Dörwald903f1e02003-02-04 16:28:00 +00002580 }
2581 outlen = need;
2582 }
Jeremy Hylton1aad9c72003-09-16 03:10:59 +00002583 memcpy(PyUnicode_AS_UNICODE(result) + j,
2584 PyUnicode_AS_UNICODE(item),
2585 reslen*sizeof(Py_UNICODE));
Walter Dörwald903f1e02003-02-04 16:28:00 +00002586 j += reslen;
2587 }
2588 }
Martin v. Löwis8afd7572003-01-25 22:46:11 +00002589 Py_DECREF(item);
2590 }
2591
Walter Dörwald903f1e02003-02-04 16:28:00 +00002592 if (j < outlen)
Martin v. Löwis8afd7572003-01-25 22:46:11 +00002593 PyUnicode_Resize(&result, j);
2594
2595 return result;
2596
2597Fail_1:
2598 Py_DECREF(result);
2599 return NULL;
2600}
2601#endif