blob: 082f64c344747290a030cffeca774fbc96499e98 [file] [log] [blame]
Guido van Rossum3f5da241990-12-20 15:06:42 +00001/* Built-in functions */
2
Guido van Rossum79f25d91997-04-29 20:08:16 +00003#include "Python.h"
Guido van Rossum3f5da241990-12-20 15:06:42 +00004
5#include "node.h"
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006#include "code.h"
Guido van Rossum5b722181993-03-30 17:46:03 +00007#include "eval.h"
Guido van Rossum3f5da241990-12-20 15:06:42 +00008
Guido van Rossum6bf62da1997-04-11 20:37:35 +00009#include <ctype.h>
10
Guido van Rossume2ae77b2001-10-24 20:42:55 +000011#ifdef RISCOS
12#include "unixstuff.h"
13#endif
14
Mark Hammond26cffde42001-05-14 12:17:34 +000015/* The default encoding used by the platform file system APIs
16 Can remain NULL for all platforms that don't have such a concept
17*/
Martin v. Löwis6238d2b2002-06-30 15:26:10 +000018#if defined(MS_WINDOWS) && defined(HAVE_USABLE_WCHAR_T)
Mark Hammond26cffde42001-05-14 12:17:34 +000019const char *Py_FileSystemDefaultEncoding = "mbcs";
Just van Rossumb9b8e9c2003-02-10 09:22:01 +000020#elif defined(__APPLE__)
21const char *Py_FileSystemDefaultEncoding = "utf-8";
Mark Hammond26cffde42001-05-14 12:17:34 +000022#else
23const char *Py_FileSystemDefaultEncoding = NULL; /* use default */
24#endif
Mark Hammondef8b6542001-05-13 08:04:26 +000025
Guido van Rossum12d12c51993-10-26 17:58:25 +000026/* Forward */
Tim Petersdbd9ba62000-07-09 03:09:57 +000027static PyObject *filterstring(PyObject *, PyObject *);
Martin v. Löwis8afd7572003-01-25 22:46:11 +000028#ifdef Py_USING_UNICODE
29static PyObject *filterunicode(PyObject *, PyObject *);
30#endif
Tim Petersdbd9ba62000-07-09 03:09:57 +000031static PyObject *filtertuple (PyObject *, PyObject *);
Guido van Rossum12d12c51993-10-26 17:58:25 +000032
Guido van Rossum79f25d91997-04-29 20:08:16 +000033static PyObject *
Neal Norwitz92e212f2006-04-03 04:48:37 +000034builtin___import__(PyObject *self, PyObject *args, PyObject *kwds)
Guido van Rossum3f5da241990-12-20 15:06:42 +000035{
Neal Norwitz92e212f2006-04-03 04:48:37 +000036 static char *kwlist[] = {"name", "globals", "locals", "fromlist",
37 "level", 0};
Guido van Rossum1ae940a1995-01-02 19:04:15 +000038 char *name;
Guido van Rossum79f25d91997-04-29 20:08:16 +000039 PyObject *globals = NULL;
40 PyObject *locals = NULL;
41 PyObject *fromlist = NULL;
Thomas Woutersf7f438b2006-02-28 16:09:29 +000042 int level = -1;
Guido van Rossum1ae940a1995-01-02 19:04:15 +000043
Neal Norwitz92e212f2006-04-03 04:48:37 +000044 if (!PyArg_ParseTupleAndKeywords(args, kwds, "s|OOOi:__import__",
45 kwlist, &name, &globals, &locals, &fromlist, &level))
Guido van Rossum1ae940a1995-01-02 19:04:15 +000046 return NULL;
Thomas Woutersf7f438b2006-02-28 16:09:29 +000047 return PyImport_ImportModuleLevel(name, globals, locals,
48 fromlist, level);
Guido van Rossum1ae940a1995-01-02 19:04:15 +000049}
50
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000051PyDoc_STRVAR(import_doc,
Neal Norwitz92e212f2006-04-03 04:48:37 +000052"__import__(name, globals={}, locals={}, fromlist=[], level=-1) -> module\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +000053\n\
54Import a module. The globals are only used to determine the context;\n\
55they are not modified. The locals are currently unused. The fromlist\n\
56should be a list of names to emulate ``from name import ...'', or an\n\
57empty list to emulate ``import name''.\n\
58When importing a module from a package, note that __import__('A.B', ...)\n\
59returns package A when fromlist is empty, but its submodule B when\n\
Neal Norwitz92e212f2006-04-03 04:48:37 +000060fromlist is not empty. Level is used to determine whether to perform \n\
61absolute or relative imports. -1 is the original strategy of attempting\n\
62both absolute and relative imports, 0 is absolute, a positive number\n\
63is the number of parent directories to search relative to the current module.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +000064
Guido van Rossum1ae940a1995-01-02 19:04:15 +000065
Guido van Rossum79f25d91997-04-29 20:08:16 +000066static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +000067builtin_abs(PyObject *self, PyObject *v)
Guido van Rossum1ae940a1995-01-02 19:04:15 +000068{
Guido van Rossum09df08a1998-05-22 00:51:39 +000069 return PyNumber_Absolute(v);
Guido van Rossum3f5da241990-12-20 15:06:42 +000070}
71
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000072PyDoc_STRVAR(abs_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +000073"abs(number) -> number\n\
74\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000075Return the absolute value of the argument.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +000076
Raymond Hettinger96229b12005-03-11 06:49:40 +000077static PyObject *
78builtin_all(PyObject *self, PyObject *v)
79{
80 PyObject *it, *item;
81
82 it = PyObject_GetIter(v);
83 if (it == NULL)
84 return NULL;
85
86 while ((item = PyIter_Next(it)) != NULL) {
87 int cmp = PyObject_IsTrue(item);
88 Py_DECREF(item);
89 if (cmp < 0) {
90 Py_DECREF(it);
91 return NULL;
92 }
93 if (cmp == 0) {
94 Py_DECREF(it);
95 Py_RETURN_FALSE;
96 }
97 }
98 Py_DECREF(it);
99 if (PyErr_Occurred())
100 return NULL;
101 Py_RETURN_TRUE;
102}
103
104PyDoc_STRVAR(all_doc,
105"all(iterable) -> bool\n\
106\n\
107Return True if bool(x) is True for all values x in the iterable.");
108
109static PyObject *
110builtin_any(PyObject *self, PyObject *v)
111{
112 PyObject *it, *item;
113
114 it = PyObject_GetIter(v);
115 if (it == NULL)
116 return NULL;
117
118 while ((item = PyIter_Next(it)) != NULL) {
119 int cmp = PyObject_IsTrue(item);
120 Py_DECREF(item);
121 if (cmp < 0) {
122 Py_DECREF(it);
123 return NULL;
124 }
125 if (cmp == 1) {
126 Py_DECREF(it);
127 Py_RETURN_TRUE;
128 }
129 }
130 Py_DECREF(it);
131 if (PyErr_Occurred())
132 return NULL;
133 Py_RETURN_FALSE;
134}
135
136PyDoc_STRVAR(any_doc,
137"any(iterable) -> bool\n\
138\n\
139Return True if bool(x) is True for any x in the iterable.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000140
Guido van Rossum79f25d91997-04-29 20:08:16 +0000141static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000142builtin_apply(PyObject *self, PyObject *args)
Guido van Rossumc02e15c1991-12-16 13:03:00 +0000143{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000144 PyObject *func, *alist = NULL, *kwdict = NULL;
Barry Warsaw968f8cb1998-10-01 15:33:12 +0000145 PyObject *t = NULL, *retval = NULL;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000146
Raymond Hettingerea3fdf42002-12-29 16:33:45 +0000147 if (!PyArg_UnpackTuple(args, "apply", 1, 3, &func, &alist, &kwdict))
Guido van Rossumc02e15c1991-12-16 13:03:00 +0000148 return NULL;
Barry Warsaw968f8cb1998-10-01 15:33:12 +0000149 if (alist != NULL) {
150 if (!PyTuple_Check(alist)) {
151 if (!PySequence_Check(alist)) {
Jeremy Hyltonc862cf42001-01-19 03:25:05 +0000152 PyErr_Format(PyExc_TypeError,
Alex Martellia9b9c9f2003-04-23 13:34:35 +0000153 "apply() arg 2 expected sequence, found %s",
Jeremy Hyltonc862cf42001-01-19 03:25:05 +0000154 alist->ob_type->tp_name);
Barry Warsaw968f8cb1998-10-01 15:33:12 +0000155 return NULL;
156 }
157 t = PySequence_Tuple(alist);
158 if (t == NULL)
159 return NULL;
160 alist = t;
161 }
Guido van Rossum2d951851994-08-29 12:52:16 +0000162 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000163 if (kwdict != NULL && !PyDict_Check(kwdict)) {
Jeremy Hyltonc862cf42001-01-19 03:25:05 +0000164 PyErr_Format(PyExc_TypeError,
165 "apply() arg 3 expected dictionary, found %s",
166 kwdict->ob_type->tp_name);
Barry Warsaw968f8cb1998-10-01 15:33:12 +0000167 goto finally;
Guido van Rossum681d79a1995-07-18 14:51:37 +0000168 }
Barry Warsaw968f8cb1998-10-01 15:33:12 +0000169 retval = PyEval_CallObjectWithKeywords(func, alist, kwdict);
170 finally:
171 Py_XDECREF(t);
172 return retval;
Guido van Rossumc02e15c1991-12-16 13:03:00 +0000173}
174
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000175PyDoc_STRVAR(apply_doc,
Fred Drakef1fbc622001-01-12 17:05:05 +0000176"apply(object[, args[, kwargs]]) -> value\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000177\n\
Fred Drake7b912121999-12-23 14:16:55 +0000178Call a callable object with positional arguments taken from the tuple args,\n\
179and keyword arguments taken from the optional dictionary kwargs.\n\
Raymond Hettingerff41c482003-04-06 09:01:11 +0000180Note that classes are callable, as are instances with a __call__() method.\n\
181\n\
182Deprecated since release 2.3. Instead, use the extended call syntax:\n\
183 function(*args, **keywords).");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000184
185
Guido van Rossum79f25d91997-04-29 20:08:16 +0000186static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000187builtin_callable(PyObject *self, PyObject *v)
Guido van Rossum2d951851994-08-29 12:52:16 +0000188{
Guido van Rossum77f6a652002-04-03 22:41:51 +0000189 return PyBool_FromLong((long)PyCallable_Check(v));
Guido van Rossum2d951851994-08-29 12:52:16 +0000190}
191
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000192PyDoc_STRVAR(callable_doc,
Guido van Rossum77f6a652002-04-03 22:41:51 +0000193"callable(object) -> bool\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000194\n\
195Return whether the object is callable (i.e., some kind of function).\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000196Note that classes are callable, as are instances with a __call__() method.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000197
198
Guido van Rossum79f25d91997-04-29 20:08:16 +0000199static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000200builtin_filter(PyObject *self, PyObject *args)
Guido van Rossum12d12c51993-10-26 17:58:25 +0000201{
Guido van Rossumc7903a12002-08-16 07:04:56 +0000202 PyObject *func, *seq, *result, *it, *arg;
Martin v. Löwisd96ee902006-02-16 14:37:16 +0000203 Py_ssize_t len; /* guess for result list size */
204 register Py_ssize_t j;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000205
Raymond Hettingerea3fdf42002-12-29 16:33:45 +0000206 if (!PyArg_UnpackTuple(args, "filter", 2, 2, &func, &seq))
Guido van Rossum12d12c51993-10-26 17:58:25 +0000207 return NULL;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000208
Tim Peters0e57abf2001-05-02 07:39:38 +0000209 /* Strings and tuples return a result of the same type. */
210 if (PyString_Check(seq))
211 return filterstring(func, seq);
Martin v. Löwis8afd7572003-01-25 22:46:11 +0000212#ifdef Py_USING_UNICODE
213 if (PyUnicode_Check(seq))
214 return filterunicode(func, seq);
215#endif
Tim Peters0e57abf2001-05-02 07:39:38 +0000216 if (PyTuple_Check(seq))
217 return filtertuple(func, seq);
218
Georg Brandle35b6572005-07-19 22:20:20 +0000219 /* Pre-allocate argument list tuple. */
220 arg = PyTuple_New(1);
221 if (arg == NULL)
222 return NULL;
223
Tim Peters0e57abf2001-05-02 07:39:38 +0000224 /* Get iterator. */
225 it = PyObject_GetIter(seq);
226 if (it == NULL)
Georg Brandle35b6572005-07-19 22:20:20 +0000227 goto Fail_arg;
Tim Peters0e57abf2001-05-02 07:39:38 +0000228
229 /* Guess a result list size. */
Armin Rigof5b3e362006-02-11 21:32:43 +0000230 len = _PyObject_LengthHint(seq);
Raymond Hettingerb86269d2004-01-04 11:00:08 +0000231 if (len < 0) {
Raymond Hettingera710b332005-08-21 11:03:59 +0000232 if (!PyErr_ExceptionMatches(PyExc_TypeError) &&
233 !PyErr_ExceptionMatches(PyExc_AttributeError)) {
234 goto Fail_it;
235 }
Raymond Hettingerb86269d2004-01-04 11:00:08 +0000236 PyErr_Clear();
237 len = 8; /* arbitrary */
Guido van Rossum12d12c51993-10-26 17:58:25 +0000238 }
239
Tim Peters0e57abf2001-05-02 07:39:38 +0000240 /* Get a result list. */
Guido van Rossum79f25d91997-04-29 20:08:16 +0000241 if (PyList_Check(seq) && seq->ob_refcnt == 1) {
Tim Peters0e57abf2001-05-02 07:39:38 +0000242 /* Eww - can modify the list in-place. */
Guido van Rossum79f25d91997-04-29 20:08:16 +0000243 Py_INCREF(seq);
Guido van Rossum12d12c51993-10-26 17:58:25 +0000244 result = seq;
245 }
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000246 else {
Tim Peters0e57abf2001-05-02 07:39:38 +0000247 result = PyList_New(len);
248 if (result == NULL)
249 goto Fail_it;
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000250 }
Guido van Rossum12d12c51993-10-26 17:58:25 +0000251
Tim Peters0e57abf2001-05-02 07:39:38 +0000252 /* Build the result list. */
Tim Petersf4848da2001-05-05 00:14:56 +0000253 j = 0;
254 for (;;) {
Guido van Rossumc7903a12002-08-16 07:04:56 +0000255 PyObject *item;
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000256 int ok;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000257
Tim Peters0e57abf2001-05-02 07:39:38 +0000258 item = PyIter_Next(it);
259 if (item == NULL) {
Tim Petersf4848da2001-05-05 00:14:56 +0000260 if (PyErr_Occurred())
261 goto Fail_result_it;
Tim Peters0e57abf2001-05-02 07:39:38 +0000262 break;
Guido van Rossum2d951851994-08-29 12:52:16 +0000263 }
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000264
Neil Schemenauer68973552003-08-14 20:37:34 +0000265 if (func == (PyObject *)&PyBool_Type || func == Py_None) {
Guido van Rossumc7903a12002-08-16 07:04:56 +0000266 ok = PyObject_IsTrue(item);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000267 }
268 else {
Guido van Rossumc7903a12002-08-16 07:04:56 +0000269 PyObject *good;
270 PyTuple_SET_ITEM(arg, 0, item);
271 good = PyObject_Call(func, arg, NULL);
272 PyTuple_SET_ITEM(arg, 0, NULL);
Guido van Rossum58b68731995-01-10 17:40:55 +0000273 if (good == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000274 Py_DECREF(item);
Tim Peters0e57abf2001-05-02 07:39:38 +0000275 goto Fail_result_it;
Guido van Rossum58b68731995-01-10 17:40:55 +0000276 }
Guido van Rossumc7903a12002-08-16 07:04:56 +0000277 ok = PyObject_IsTrue(good);
278 Py_DECREF(good);
Guido van Rossum12d12c51993-10-26 17:58:25 +0000279 }
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000280 if (ok) {
Tim Peters0e57abf2001-05-02 07:39:38 +0000281 if (j < len)
282 PyList_SET_ITEM(result, j, item);
Guido van Rossum2d951851994-08-29 12:52:16 +0000283 else {
Barry Warsawfa77e091999-01-28 18:49:12 +0000284 int status = PyList_Append(result, item);
Barry Warsawfa77e091999-01-28 18:49:12 +0000285 Py_DECREF(item);
286 if (status < 0)
Tim Peters0e57abf2001-05-02 07:39:38 +0000287 goto Fail_result_it;
Guido van Rossum2d951851994-08-29 12:52:16 +0000288 }
Tim Peters0e57abf2001-05-02 07:39:38 +0000289 ++j;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000290 }
Tim Peters0e57abf2001-05-02 07:39:38 +0000291 else
292 Py_DECREF(item);
Guido van Rossum12d12c51993-10-26 17:58:25 +0000293 }
294
Guido van Rossum12d12c51993-10-26 17:58:25 +0000295
Tim Peters0e57abf2001-05-02 07:39:38 +0000296 /* Cut back result list if len is too big. */
Guido van Rossum79f25d91997-04-29 20:08:16 +0000297 if (j < len && PyList_SetSlice(result, j, len, NULL) < 0)
Tim Peters0e57abf2001-05-02 07:39:38 +0000298 goto Fail_result_it;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000299
Tim Peters3c6b1482001-05-21 08:07:05 +0000300 Py_DECREF(it);
Guido van Rossumc7903a12002-08-16 07:04:56 +0000301 Py_DECREF(arg);
Guido van Rossum12d12c51993-10-26 17:58:25 +0000302 return result;
303
Tim Peters0e57abf2001-05-02 07:39:38 +0000304Fail_result_it:
Guido van Rossum79f25d91997-04-29 20:08:16 +0000305 Py_DECREF(result);
Tim Peters0e57abf2001-05-02 07:39:38 +0000306Fail_it:
307 Py_DECREF(it);
Guido van Rossumc7903a12002-08-16 07:04:56 +0000308Fail_arg:
309 Py_DECREF(arg);
Guido van Rossum12d12c51993-10-26 17:58:25 +0000310 return NULL;
311}
312
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000313PyDoc_STRVAR(filter_doc,
Tim Petersd50e5442002-03-09 00:06:26 +0000314"filter(function or None, sequence) -> list, tuple, or string\n"
315"\n"
316"Return those items of sequence for which function(item) is true. If\n"
317"function is None, return the items that are true. If sequence is a tuple\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000318"or string, return the same type, else return a list.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000319
Guido van Rossum79f25d91997-04-29 20:08:16 +0000320static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000321builtin_chr(PyObject *self, PyObject *args)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000322{
323 long x;
324 char s[1];
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000325
Guido van Rossum79f25d91997-04-29 20:08:16 +0000326 if (!PyArg_ParseTuple(args, "l:chr", &x))
Guido van Rossum3f5da241990-12-20 15:06:42 +0000327 return NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000328 if (x < 0 || x >= 256) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000329 PyErr_SetString(PyExc_ValueError,
330 "chr() arg not in range(256)");
Guido van Rossum3f5da241990-12-20 15:06:42 +0000331 return NULL;
332 }
Guido van Rossum6bf62da1997-04-11 20:37:35 +0000333 s[0] = (char)x;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000334 return PyString_FromStringAndSize(s, 1);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000335}
336
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000337PyDoc_STRVAR(chr_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000338"chr(i) -> character\n\
339\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000340Return a string of one character with ordinal i; 0 <= i < 256.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000341
342
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000343#ifdef Py_USING_UNICODE
Guido van Rossum79f25d91997-04-29 20:08:16 +0000344static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000345builtin_unichr(PyObject *self, PyObject *args)
Guido van Rossum09095f32000-03-10 23:00:52 +0000346{
347 long x;
Guido van Rossum09095f32000-03-10 23:00:52 +0000348
349 if (!PyArg_ParseTuple(args, "l:unichr", &x))
350 return NULL;
Fredrik Lundh0dcf67e2001-06-26 20:01:56 +0000351
Marc-André Lemburgcc8764c2002-08-11 12:23:04 +0000352 return PyUnicode_FromOrdinal(x);
Guido van Rossum09095f32000-03-10 23:00:52 +0000353}
354
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000355PyDoc_STRVAR(unichr_doc,
Fred Drake078b24f2000-04-13 02:42:50 +0000356"unichr(i) -> Unicode character\n\
Guido van Rossum09095f32000-03-10 23:00:52 +0000357\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000358Return a Unicode string of one character with ordinal i; 0 <= i <= 0x10ffff.");
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000359#endif
Guido van Rossum09095f32000-03-10 23:00:52 +0000360
361
362static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000363builtin_cmp(PyObject *self, PyObject *args)
Guido van Rossuma9e7dc11992-10-18 18:53:57 +0000364{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000365 PyObject *a, *b;
Guido van Rossum09df08a1998-05-22 00:51:39 +0000366 int c;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000367
Raymond Hettingerea3fdf42002-12-29 16:33:45 +0000368 if (!PyArg_UnpackTuple(args, "cmp", 2, 2, &a, &b))
Guido van Rossuma9e7dc11992-10-18 18:53:57 +0000369 return NULL;
Guido van Rossum09df08a1998-05-22 00:51:39 +0000370 if (PyObject_Cmp(a, b, &c) < 0)
Guido van Rossumc8b6df91997-05-23 00:06:51 +0000371 return NULL;
Guido van Rossum09df08a1998-05-22 00:51:39 +0000372 return PyInt_FromLong((long)c);
Guido van Rossuma9e7dc11992-10-18 18:53:57 +0000373}
374
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000375PyDoc_STRVAR(cmp_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000376"cmp(x, y) -> integer\n\
377\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000378Return negative if x<y, zero if x==y, positive if x>y.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000379
380
Guido van Rossum79f25d91997-04-29 20:08:16 +0000381static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000382builtin_coerce(PyObject *self, PyObject *args)
Guido van Rossum6a00cd81995-01-07 12:39:01 +0000383{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000384 PyObject *v, *w;
385 PyObject *res;
Guido van Rossum5524a591995-01-10 15:26:20 +0000386
Raymond Hettingerea3fdf42002-12-29 16:33:45 +0000387 if (!PyArg_UnpackTuple(args, "coerce", 2, 2, &v, &w))
Guido van Rossum5524a591995-01-10 15:26:20 +0000388 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000389 if (PyNumber_Coerce(&v, &w) < 0)
Guido van Rossum04691fc1992-08-12 15:35:34 +0000390 return NULL;
Raymond Hettinger8ae46892003-10-12 19:09:37 +0000391 res = PyTuple_Pack(2, v, w);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000392 Py_DECREF(v);
393 Py_DECREF(w);
Guido van Rossum04691fc1992-08-12 15:35:34 +0000394 return res;
395}
396
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000397PyDoc_STRVAR(coerce_doc,
Martin v. Löwis8d494f32004-08-25 10:42:41 +0000398"coerce(x, y) -> (x1, y1)\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000399\n\
Martin v. Löwis8d494f32004-08-25 10:42:41 +0000400Return a tuple consisting of the two numeric arguments converted to\n\
401a common type, using the same rules as used by arithmetic operations.\n\
402If coercion is not possible, raise TypeError.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000403
Guido van Rossum79f25d91997-04-29 20:08:16 +0000404static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000405builtin_compile(PyObject *self, PyObject *args)
Guido van Rossum5b722181993-03-30 17:46:03 +0000406{
407 char *str;
408 char *filename;
409 char *startstr;
410 int start;
Tim Peters6cd6a822001-08-17 22:11:27 +0000411 int dont_inherit = 0;
412 int supplied_flags = 0;
Tim Peters5ba58662001-07-16 02:29:45 +0000413 PyCompilerFlags cf;
Neal Norwitz3a9a3e72005-11-27 20:38:31 +0000414 PyObject *result = NULL, *cmd, *tmp = NULL;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000415 Py_ssize_t length;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000416
Just van Rossum3aaf42c2003-02-10 08:21:10 +0000417 if (!PyArg_ParseTuple(args, "Oss|ii:compile", &cmd, &filename,
Tim Peters6cd6a822001-08-17 22:11:27 +0000418 &startstr, &supplied_flags, &dont_inherit))
Guido van Rossum5b722181993-03-30 17:46:03 +0000419 return NULL;
Tim Peters6cd6a822001-08-17 22:11:27 +0000420
Just van Rossum3aaf42c2003-02-10 08:21:10 +0000421 cf.cf_flags = supplied_flags;
422
423#ifdef Py_USING_UNICODE
424 if (PyUnicode_Check(cmd)) {
425 tmp = PyUnicode_AsUTF8String(cmd);
426 if (tmp == NULL)
427 return NULL;
428 cmd = tmp;
429 cf.cf_flags |= PyCF_SOURCE_IS_UTF8;
430 }
431#endif
Just van Rossumb9b8e9c2003-02-10 09:22:01 +0000432 if (PyObject_AsReadBuffer(cmd, (const void **)&str, &length))
433 return NULL;
Tim Peters2c646c92003-02-10 14:48:29 +0000434 if ((size_t)length != strlen(str)) {
Just van Rossum3aaf42c2003-02-10 08:21:10 +0000435 PyErr_SetString(PyExc_TypeError,
Alex Martellia9b9c9f2003-04-23 13:34:35 +0000436 "compile() expected string without null bytes");
Neal Norwitz3a9a3e72005-11-27 20:38:31 +0000437 goto cleanup;
Just van Rossum3aaf42c2003-02-10 08:21:10 +0000438 }
439
Guido van Rossum5b722181993-03-30 17:46:03 +0000440 if (strcmp(startstr, "exec") == 0)
Guido van Rossumb05a5c71997-05-07 17:46:13 +0000441 start = Py_file_input;
Guido van Rossum5b722181993-03-30 17:46:03 +0000442 else if (strcmp(startstr, "eval") == 0)
Guido van Rossumb05a5c71997-05-07 17:46:13 +0000443 start = Py_eval_input;
Guido van Rossum872537c1995-07-07 22:43:42 +0000444 else if (strcmp(startstr, "single") == 0)
Guido van Rossumb05a5c71997-05-07 17:46:13 +0000445 start = Py_single_input;
Guido van Rossum5b722181993-03-30 17:46:03 +0000446 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000447 PyErr_SetString(PyExc_ValueError,
Fred Drake661ea262000-10-24 19:57:45 +0000448 "compile() arg 3 must be 'exec' or 'eval' or 'single'");
Neal Norwitz3a9a3e72005-11-27 20:38:31 +0000449 goto cleanup;
Guido van Rossum5b722181993-03-30 17:46:03 +0000450 }
Tim Peters6cd6a822001-08-17 22:11:27 +0000451
Guido van Rossum4b499dd32003-02-13 22:07:59 +0000452 if (supplied_flags &
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000453 ~(PyCF_MASK | PyCF_MASK_OBSOLETE | PyCF_DONT_IMPLY_DEDENT | PyCF_ONLY_AST))
Guido van Rossum4b499dd32003-02-13 22:07:59 +0000454 {
Tim Peters6cd6a822001-08-17 22:11:27 +0000455 PyErr_SetString(PyExc_ValueError,
456 "compile(): unrecognised flags");
Neal Norwitz3a9a3e72005-11-27 20:38:31 +0000457 goto cleanup;
Tim Peters6cd6a822001-08-17 22:11:27 +0000458 }
459 /* XXX Warn if (supplied_flags & PyCF_MASK_OBSOLETE) != 0? */
460
Tim Peters6cd6a822001-08-17 22:11:27 +0000461 if (!dont_inherit) {
462 PyEval_MergeCompilerFlags(&cf);
463 }
Just van Rossum3aaf42c2003-02-10 08:21:10 +0000464 result = Py_CompileStringFlags(str, filename, start, &cf);
Neal Norwitz3a9a3e72005-11-27 20:38:31 +0000465cleanup:
Just van Rossum3aaf42c2003-02-10 08:21:10 +0000466 Py_XDECREF(tmp);
467 return result;
Guido van Rossum5b722181993-03-30 17:46:03 +0000468}
469
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000470PyDoc_STRVAR(compile_doc,
Tim Peters6cd6a822001-08-17 22:11:27 +0000471"compile(source, filename, mode[, flags[, dont_inherit]]) -> code object\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000472\n\
473Compile the source string (a Python module, statement or expression)\n\
474into a code object that can be executed by the exec statement or eval().\n\
475The filename will be used for run-time error messages.\n\
476The mode must be 'exec' to compile a module, 'single' to compile a\n\
Tim Peters6cd6a822001-08-17 22:11:27 +0000477single (interactive) statement, or 'eval' to compile an expression.\n\
478The flags argument, if present, controls which future statements influence\n\
479the compilation of the code.\n\
480The dont_inherit argument, if non-zero, stops the compilation inheriting\n\
481the effects of any future statements in effect in the code calling\n\
482compile; if absent or zero these statements do influence the compilation,\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000483in addition to any features explicitly specified.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000484
Guido van Rossum79f25d91997-04-29 20:08:16 +0000485static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000486builtin_dir(PyObject *self, PyObject *args)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000487{
Tim Peters5d2b77c2001-09-03 05:47:38 +0000488 PyObject *arg = NULL;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000489
Raymond Hettingerea3fdf42002-12-29 16:33:45 +0000490 if (!PyArg_UnpackTuple(args, "dir", 0, 1, &arg))
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000491 return NULL;
Tim Peters7eea37e2001-09-04 22:08:56 +0000492 return PyObject_Dir(arg);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000493}
494
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000495PyDoc_STRVAR(dir_doc,
Tim Peters5d2b77c2001-09-03 05:47:38 +0000496"dir([object]) -> list of strings\n"
497"\n"
Georg Brandl871f1bc2007-03-12 13:17:36 +0000498"If called without an argument, return the names in the current scope.\n"
499"Else, return an alphabetized list of names comprising (some of) the attributes\n"
500"of the given object, and of attributes reachable from it.\n"
501"If the object supplies a method named __dir__, it will be used; otherwise\n"
502"the default dir() logic is used and returns:\n"
503" for a module object: the module's attributes.\n"
504" for a class object: its attributes, and recursively the attributes\n"
505" of its bases.\n"
Georg Brandl3bb15672007-03-13 07:23:16 +0000506" for any other object: its attributes, its class's attributes, and\n"
Georg Brandl871f1bc2007-03-12 13:17:36 +0000507" recursively the attributes of its class's base classes.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000508
Guido van Rossum79f25d91997-04-29 20:08:16 +0000509static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000510builtin_divmod(PyObject *self, PyObject *args)
Guido van Rossum6a00cd81995-01-07 12:39:01 +0000511{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000512 PyObject *v, *w;
Guido van Rossum6a00cd81995-01-07 12:39:01 +0000513
Raymond Hettingerea3fdf42002-12-29 16:33:45 +0000514 if (!PyArg_UnpackTuple(args, "divmod", 2, 2, &v, &w))
Guido van Rossum6a00cd81995-01-07 12:39:01 +0000515 return NULL;
Guido van Rossum09df08a1998-05-22 00:51:39 +0000516 return PyNumber_Divmod(v, w);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000517}
518
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000519PyDoc_STRVAR(divmod_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000520"divmod(x, y) -> (div, mod)\n\
521\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000522Return the tuple ((x-x%y)/y, x%y). Invariant: div*y + mod == x.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000523
524
Guido van Rossum79f25d91997-04-29 20:08:16 +0000525static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000526builtin_eval(PyObject *self, PyObject *args)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000527{
Just van Rossum3aaf42c2003-02-10 08:21:10 +0000528 PyObject *cmd, *result, *tmp = NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000529 PyObject *globals = Py_None, *locals = Py_None;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000530 char *str;
Tim Peters9fa96be2001-08-17 23:04:59 +0000531 PyCompilerFlags cf;
Guido van Rossum590baa41993-11-30 13:40:46 +0000532
Raymond Hettinger214b1c32004-07-02 06:41:07 +0000533 if (!PyArg_UnpackTuple(args, "eval", 1, 3, &cmd, &globals, &locals))
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000534 return NULL;
Raymond Hettinger214b1c32004-07-02 06:41:07 +0000535 if (locals != Py_None && !PyMapping_Check(locals)) {
536 PyErr_SetString(PyExc_TypeError, "locals must be a mapping");
Raymond Hettinger513ffe82004-07-06 13:44:41 +0000537 return NULL;
Raymond Hettinger214b1c32004-07-02 06:41:07 +0000538 }
539 if (globals != Py_None && !PyDict_Check(globals)) {
Georg Brandl99d7e4e2005-08-31 22:21:15 +0000540 PyErr_SetString(PyExc_TypeError, PyMapping_Check(globals) ?
Raymond Hettinger214b1c32004-07-02 06:41:07 +0000541 "globals must be a real dict; try eval(expr, {}, mapping)"
542 : "globals must be a dict");
Raymond Hettinger513ffe82004-07-06 13:44:41 +0000543 return NULL;
Raymond Hettinger214b1c32004-07-02 06:41:07 +0000544 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000545 if (globals == Py_None) {
546 globals = PyEval_GetGlobals();
547 if (locals == Py_None)
548 locals = PyEval_GetLocals();
Guido van Rossum6135a871995-01-09 17:53:26 +0000549 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000550 else if (locals == Py_None)
Guido van Rossum6135a871995-01-09 17:53:26 +0000551 locals = globals;
Tim Peters9fa96be2001-08-17 23:04:59 +0000552
Georg Brandl77c85e62005-09-15 10:46:13 +0000553 if (globals == NULL || locals == NULL) {
554 PyErr_SetString(PyExc_TypeError,
555 "eval must be given globals and locals "
556 "when called without a frame");
557 return NULL;
558 }
559
Guido van Rossum79f25d91997-04-29 20:08:16 +0000560 if (PyDict_GetItemString(globals, "__builtins__") == NULL) {
561 if (PyDict_SetItemString(globals, "__builtins__",
562 PyEval_GetBuiltins()) != 0)
Guido van Rossum6135a871995-01-09 17:53:26 +0000563 return NULL;
564 }
Tim Peters9fa96be2001-08-17 23:04:59 +0000565
Jeremy Hylton15c1c4f2001-07-30 21:50:55 +0000566 if (PyCode_Check(cmd)) {
Jeremy Hylton733c8932001-12-13 19:51:56 +0000567 if (PyCode_GetNumFree((PyCodeObject *)cmd) > 0) {
Jeremy Hylton15c1c4f2001-07-30 21:50:55 +0000568 PyErr_SetString(PyExc_TypeError,
569 "code object passed to eval() may not contain free variables");
570 return NULL;
571 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000572 return PyEval_EvalCode((PyCodeObject *) cmd, globals, locals);
Jeremy Hylton15c1c4f2001-07-30 21:50:55 +0000573 }
Tim Peters9fa96be2001-08-17 23:04:59 +0000574
Marc-André Lemburgd1ba4432000-09-19 21:04:18 +0000575 if (!PyString_Check(cmd) &&
576 !PyUnicode_Check(cmd)) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000577 PyErr_SetString(PyExc_TypeError,
Fred Drake661ea262000-10-24 19:57:45 +0000578 "eval() arg 1 must be a string or code object");
Guido van Rossum94390a41992-08-14 15:14:30 +0000579 return NULL;
580 }
Just van Rossum3aaf42c2003-02-10 08:21:10 +0000581 cf.cf_flags = 0;
582
583#ifdef Py_USING_UNICODE
584 if (PyUnicode_Check(cmd)) {
585 tmp = PyUnicode_AsUTF8String(cmd);
586 if (tmp == NULL)
587 return NULL;
588 cmd = tmp;
589 cf.cf_flags |= PyCF_SOURCE_IS_UTF8;
590 }
591#endif
Neal Norwitz3a9a3e72005-11-27 20:38:31 +0000592 if (PyString_AsStringAndSize(cmd, &str, NULL)) {
593 Py_XDECREF(tmp);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000594 return NULL;
Neal Norwitz3a9a3e72005-11-27 20:38:31 +0000595 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000596 while (*str == ' ' || *str == '\t')
597 str++;
Tim Peters9fa96be2001-08-17 23:04:59 +0000598
Tim Peters9fa96be2001-08-17 23:04:59 +0000599 (void)PyEval_MergeCompilerFlags(&cf);
Just van Rossum3aaf42c2003-02-10 08:21:10 +0000600 result = PyRun_StringFlags(str, Py_eval_input, globals, locals, &cf);
601 Py_XDECREF(tmp);
602 return result;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000603}
604
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000605PyDoc_STRVAR(eval_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000606"eval(source[, globals[, locals]]) -> value\n\
607\n\
608Evaluate the source in the context of globals and locals.\n\
609The source may be a string representing a Python expression\n\
610or a code object as returned by compile().\n\
Neal Norwitz477ca1c2006-09-05 02:25:41 +0000611The globals must be a dictionary and locals can be any mapping,\n\
Raymond Hettinger214b1c32004-07-02 06:41:07 +0000612defaulting to the current globals and locals.\n\
613If only globals is given, locals defaults to it.\n");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000614
615
Guido van Rossum79f25d91997-04-29 20:08:16 +0000616static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000617builtin_execfile(PyObject *self, PyObject *args)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000618{
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000619 char *filename;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000620 PyObject *globals = Py_None, *locals = Py_None;
621 PyObject *res;
Guido van Rossumb3a639e2001-09-05 13:37:47 +0000622 FILE* fp = NULL;
Tim Peters5ba58662001-07-16 02:29:45 +0000623 PyCompilerFlags cf;
Martin v. Löwis6b3a2c42001-08-08 05:30:36 +0000624 int exists;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000625
Raymond Hettinger66bd2332004-08-02 08:30:07 +0000626 if (!PyArg_ParseTuple(args, "s|O!O:execfile",
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000627 &filename,
Guido van Rossum79f25d91997-04-29 20:08:16 +0000628 &PyDict_Type, &globals,
Raymond Hettinger66bd2332004-08-02 08:30:07 +0000629 &locals))
Guido van Rossum0f61f8a1992-02-25 18:55:05 +0000630 return NULL;
Raymond Hettinger66bd2332004-08-02 08:30:07 +0000631 if (locals != Py_None && !PyMapping_Check(locals)) {
632 PyErr_SetString(PyExc_TypeError, "locals must be a mapping");
633 return NULL;
634 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000635 if (globals == Py_None) {
636 globals = PyEval_GetGlobals();
637 if (locals == Py_None)
638 locals = PyEval_GetLocals();
Guido van Rossum6135a871995-01-09 17:53:26 +0000639 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000640 else if (locals == Py_None)
Guido van Rossum6135a871995-01-09 17:53:26 +0000641 locals = globals;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000642 if (PyDict_GetItemString(globals, "__builtins__") == NULL) {
643 if (PyDict_SetItemString(globals, "__builtins__",
644 PyEval_GetBuiltins()) != 0)
Guido van Rossum6135a871995-01-09 17:53:26 +0000645 return NULL;
646 }
Martin v. Löwis6b3a2c42001-08-08 05:30:36 +0000647
648 exists = 0;
649 /* Test for existence or directory. */
Martin v. Löwis3484a182002-03-09 12:07:51 +0000650#if defined(PLAN9)
651 {
652 Dir *d;
653
654 if ((d = dirstat(filename))!=nil) {
655 if(d->mode & DMDIR)
656 werrstr("is a directory");
657 else
658 exists = 1;
659 free(d);
660 }
Martin v. Löwis6b3a2c42001-08-08 05:30:36 +0000661 }
Martin v. Löwis3484a182002-03-09 12:07:51 +0000662#elif defined(RISCOS)
Guido van Rossume2ae77b2001-10-24 20:42:55 +0000663 if (object_exists(filename)) {
664 if (isdir(filename))
665 errno = EISDIR;
666 else
667 exists = 1;
668 }
Martin v. Löwis3484a182002-03-09 12:07:51 +0000669#else /* standard Posix */
670 {
671 struct stat s;
672 if (stat(filename, &s) == 0) {
673 if (S_ISDIR(s.st_mode))
Andrew MacIntyreda4d6cb2004-03-29 11:53:38 +0000674# if defined(PYOS_OS2) && defined(PYCC_VACPP)
Martin v. Löwis3484a182002-03-09 12:07:51 +0000675 errno = EOS2ERR;
676# else
677 errno = EISDIR;
678# endif
679 else
680 exists = 1;
681 }
682 }
683#endif
Martin v. Löwis6b3a2c42001-08-08 05:30:36 +0000684
685 if (exists) {
686 Py_BEGIN_ALLOW_THREADS
Jack Jansen7b8c7542002-04-14 20:12:41 +0000687 fp = fopen(filename, "r" PY_STDIOTEXTMODE);
Martin v. Löwis6b3a2c42001-08-08 05:30:36 +0000688 Py_END_ALLOW_THREADS
689
690 if (fp == NULL) {
691 exists = 0;
692 }
693 }
694
695 if (!exists) {
Peter Schneider-Kamp4c013422002-08-27 16:58:00 +0000696 PyErr_SetFromErrnoWithFilename(PyExc_IOError, filename);
Guido van Rossum0f61f8a1992-02-25 18:55:05 +0000697 return NULL;
698 }
Tim Peters5ba58662001-07-16 02:29:45 +0000699 cf.cf_flags = 0;
700 if (PyEval_MergeCompilerFlags(&cf))
Tim Peters748b8bb2001-04-28 08:20:22 +0000701 res = PyRun_FileExFlags(fp, filename, Py_file_input, globals,
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000702 locals, 1, &cf);
Tim Peters5ba58662001-07-16 02:29:45 +0000703 else
Tim Peters748b8bb2001-04-28 08:20:22 +0000704 res = PyRun_FileEx(fp, filename, Py_file_input, globals,
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000705 locals, 1);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000706 return res;
Guido van Rossum0f61f8a1992-02-25 18:55:05 +0000707}
708
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000709PyDoc_STRVAR(execfile_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000710"execfile(filename[, globals[, locals]])\n\
711\n\
712Read and execute a Python script from a file.\n\
713The globals and locals are dictionaries, defaulting to the current\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000714globals and locals. If only globals is given, locals defaults to it.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000715
716
Guido van Rossum79f25d91997-04-29 20:08:16 +0000717static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000718builtin_getattr(PyObject *self, PyObject *args)
Guido van Rossum33894be1992-01-27 16:53:09 +0000719{
Guido van Rossum950ff291998-06-29 13:38:57 +0000720 PyObject *v, *result, *dflt = NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000721 PyObject *name;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000722
Raymond Hettingerea3fdf42002-12-29 16:33:45 +0000723 if (!PyArg_UnpackTuple(args, "getattr", 2, 3, &v, &name, &dflt))
Guido van Rossum33894be1992-01-27 16:53:09 +0000724 return NULL;
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000725#ifdef Py_USING_UNICODE
Jeremy Hylton0eb11152001-07-30 22:39:31 +0000726 if (PyUnicode_Check(name)) {
727 name = _PyUnicode_AsDefaultEncodedString(name, NULL);
728 if (name == NULL)
729 return NULL;
730 }
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000731#endif
Jeremy Hylton0eb11152001-07-30 22:39:31 +0000732
733 if (!PyString_Check(name)) {
734 PyErr_SetString(PyExc_TypeError,
Alex Martellia9b9c9f2003-04-23 13:34:35 +0000735 "getattr(): attribute name must be string");
Jeremy Hylton0eb11152001-07-30 22:39:31 +0000736 return NULL;
737 }
Guido van Rossum950ff291998-06-29 13:38:57 +0000738 result = PyObject_GetAttr(v, name);
Guido van Rossumd8923572001-10-16 21:31:32 +0000739 if (result == NULL && dflt != NULL &&
740 PyErr_ExceptionMatches(PyExc_AttributeError))
741 {
Guido van Rossum950ff291998-06-29 13:38:57 +0000742 PyErr_Clear();
743 Py_INCREF(dflt);
744 result = dflt;
745 }
746 return result;
Guido van Rossum9bfef441993-03-29 10:43:31 +0000747}
748
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000749PyDoc_STRVAR(getattr_doc,
Guido van Rossum950ff291998-06-29 13:38:57 +0000750"getattr(object, name[, default]) -> value\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000751\n\
Guido van Rossum950ff291998-06-29 13:38:57 +0000752Get a named attribute from an object; getattr(x, 'y') is equivalent to x.y.\n\
753When a default argument is given, it is returned when the attribute doesn't\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000754exist; without it, an exception is raised in that case.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000755
756
Guido van Rossum79f25d91997-04-29 20:08:16 +0000757static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000758builtin_globals(PyObject *self)
Guido van Rossum872537c1995-07-07 22:43:42 +0000759{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000760 PyObject *d;
Guido van Rossum872537c1995-07-07 22:43:42 +0000761
Guido van Rossum79f25d91997-04-29 20:08:16 +0000762 d = PyEval_GetGlobals();
Neal Norwitz43bd4db2006-08-12 01:46:42 +0000763 Py_XINCREF(d);
Guido van Rossum872537c1995-07-07 22:43:42 +0000764 return d;
765}
766
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000767PyDoc_STRVAR(globals_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000768"globals() -> dictionary\n\
769\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000770Return the dictionary containing the current scope's global variables.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000771
772
Guido van Rossum79f25d91997-04-29 20:08:16 +0000773static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000774builtin_hasattr(PyObject *self, PyObject *args)
Guido van Rossum9bfef441993-03-29 10:43:31 +0000775{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000776 PyObject *v;
777 PyObject *name;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000778
Raymond Hettingerea3fdf42002-12-29 16:33:45 +0000779 if (!PyArg_UnpackTuple(args, "hasattr", 2, 2, &v, &name))
Guido van Rossum9bfef441993-03-29 10:43:31 +0000780 return NULL;
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000781#ifdef Py_USING_UNICODE
Jeremy Hylton302b54a2001-07-30 22:45:19 +0000782 if (PyUnicode_Check(name)) {
783 name = _PyUnicode_AsDefaultEncodedString(name, NULL);
784 if (name == NULL)
785 return NULL;
786 }
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000787#endif
Jeremy Hylton302b54a2001-07-30 22:45:19 +0000788
789 if (!PyString_Check(name)) {
790 PyErr_SetString(PyExc_TypeError,
Alex Martellia9b9c9f2003-04-23 13:34:35 +0000791 "hasattr(): attribute name must be string");
Jeremy Hylton302b54a2001-07-30 22:45:19 +0000792 return NULL;
793 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000794 v = PyObject_GetAttr(v, name);
Guido van Rossum9bfef441993-03-29 10:43:31 +0000795 if (v == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000796 PyErr_Clear();
Guido van Rossum09df08a1998-05-22 00:51:39 +0000797 Py_INCREF(Py_False);
798 return Py_False;
Guido van Rossum9bfef441993-03-29 10:43:31 +0000799 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000800 Py_DECREF(v);
Guido van Rossum09df08a1998-05-22 00:51:39 +0000801 Py_INCREF(Py_True);
802 return Py_True;
Guido van Rossum33894be1992-01-27 16:53:09 +0000803}
804
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000805PyDoc_STRVAR(hasattr_doc,
Guido van Rossum77f6a652002-04-03 22:41:51 +0000806"hasattr(object, name) -> bool\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000807\n\
808Return whether the object has an attribute with the given name.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000809(This is done by calling getattr(object, name) and catching exceptions.)");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000810
811
Guido van Rossum79f25d91997-04-29 20:08:16 +0000812static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000813builtin_id(PyObject *self, PyObject *v)
Guido van Rossum5b722181993-03-30 17:46:03 +0000814{
Guido van Rossum106f2da2000-06-28 21:12:25 +0000815 return PyLong_FromVoidPtr(v);
Guido van Rossum5b722181993-03-30 17:46:03 +0000816}
817
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000818PyDoc_STRVAR(id_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000819"id(object) -> integer\n\
820\n\
821Return the identity of an object. This is guaranteed to be unique among\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000822simultaneously existing objects. (Hint: it's the object's memory address.)");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000823
824
Guido van Rossum79f25d91997-04-29 20:08:16 +0000825static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000826builtin_map(PyObject *self, PyObject *args)
Guido van Rossum12d12c51993-10-26 17:58:25 +0000827{
828 typedef struct {
Tim Peters4e9afdc2001-05-03 23:54:49 +0000829 PyObject *it; /* the iterator object */
830 int saw_StopIteration; /* bool: did the iterator end? */
Guido van Rossum12d12c51993-10-26 17:58:25 +0000831 } sequence;
832
Guido van Rossum79f25d91997-04-29 20:08:16 +0000833 PyObject *func, *result;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000834 sequence *seqs = NULL, *sqp;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000835 Py_ssize_t n, len;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000836 register int i, j;
837
Guido van Rossum79f25d91997-04-29 20:08:16 +0000838 n = PyTuple_Size(args);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000839 if (n < 2) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000840 PyErr_SetString(PyExc_TypeError,
841 "map() requires at least two args");
Guido van Rossum12d12c51993-10-26 17:58:25 +0000842 return NULL;
843 }
844
Guido van Rossum79f25d91997-04-29 20:08:16 +0000845 func = PyTuple_GetItem(args, 0);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000846 n--;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000847
Guido van Rossumfa4ac711998-07-10 17:37:30 +0000848 if (func == Py_None && n == 1) {
849 /* map(None, S) is the same as list(S). */
850 return PySequence_List(PyTuple_GetItem(args, 1));
851 }
852
Tim Peters4e9afdc2001-05-03 23:54:49 +0000853 /* Get space for sequence descriptors. Must NULL out the iterator
854 * pointers so that jumping to Fail_2 later doesn't see trash.
855 */
Guido van Rossum79f25d91997-04-29 20:08:16 +0000856 if ((seqs = PyMem_NEW(sequence, n)) == NULL) {
857 PyErr_NoMemory();
Tim Peters4e9afdc2001-05-03 23:54:49 +0000858 return NULL;
859 }
860 for (i = 0; i < n; ++i) {
861 seqs[i].it = (PyObject*)NULL;
862 seqs[i].saw_StopIteration = 0;
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000863 }
Guido van Rossum12d12c51993-10-26 17:58:25 +0000864
Tim Peters4e9afdc2001-05-03 23:54:49 +0000865 /* Do a first pass to obtain iterators for the arguments, and set len
866 * to the largest of their lengths.
Tim Peters748b8bb2001-04-28 08:20:22 +0000867 */
Tim Peters4e9afdc2001-05-03 23:54:49 +0000868 len = 0;
869 for (i = 0, sqp = seqs; i < n; ++i, ++sqp) {
870 PyObject *curseq;
Martin v. Löwisd96ee902006-02-16 14:37:16 +0000871 Py_ssize_t curlen;
Guido van Rossumad991772001-01-12 16:03:05 +0000872
Tim Peters4e9afdc2001-05-03 23:54:49 +0000873 /* Get iterator. */
874 curseq = PyTuple_GetItem(args, i+1);
875 sqp->it = PyObject_GetIter(curseq);
876 if (sqp->it == NULL) {
Guido van Rossum12d12c51993-10-26 17:58:25 +0000877 static char errmsg[] =
Tim Peters4e9afdc2001-05-03 23:54:49 +0000878 "argument %d to map() must support iteration";
Guido van Rossum15e33a41997-04-30 19:00:27 +0000879 char errbuf[sizeof(errmsg) + 25];
Jeremy Hylton518ab1c2001-11-28 20:42:20 +0000880 PyOS_snprintf(errbuf, sizeof(errbuf), errmsg, i+2);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000881 PyErr_SetString(PyExc_TypeError, errbuf);
Guido van Rossum12d12c51993-10-26 17:58:25 +0000882 goto Fail_2;
883 }
884
Tim Peters4e9afdc2001-05-03 23:54:49 +0000885 /* Update len. */
Armin Rigof5b3e362006-02-11 21:32:43 +0000886 curlen = _PyObject_LengthHint(curseq);
Raymond Hettinger77f3c872004-01-04 08:54:44 +0000887 if (curlen < 0) {
Raymond Hettingera710b332005-08-21 11:03:59 +0000888 if (!PyErr_ExceptionMatches(PyExc_TypeError) &&
889 !PyErr_ExceptionMatches(PyExc_AttributeError)) {
890 goto Fail_2;
891 }
Raymond Hettinger77f3c872004-01-04 08:54:44 +0000892 PyErr_Clear();
Tim Peters4e9afdc2001-05-03 23:54:49 +0000893 curlen = 8; /* arbitrary */
Raymond Hettinger77f3c872004-01-04 08:54:44 +0000894 }
895 if (curlen > len)
896 len = curlen;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000897 }
898
Tim Peters4e9afdc2001-05-03 23:54:49 +0000899 /* Get space for the result list. */
Guido van Rossum79f25d91997-04-29 20:08:16 +0000900 if ((result = (PyObject *) PyList_New(len)) == NULL)
Guido van Rossum12d12c51993-10-26 17:58:25 +0000901 goto Fail_2;
902
Tim Peters4e9afdc2001-05-03 23:54:49 +0000903 /* Iterate over the sequences until all have stopped. */
Guido van Rossum2d951851994-08-29 12:52:16 +0000904 for (i = 0; ; ++i) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000905 PyObject *alist, *item=NULL, *value;
Tim Peters4e9afdc2001-05-03 23:54:49 +0000906 int numactive = 0;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000907
Guido van Rossum79f25d91997-04-29 20:08:16 +0000908 if (func == Py_None && n == 1)
Guido van Rossum32120311995-07-10 13:52:21 +0000909 alist = NULL;
Tim Peters4e9afdc2001-05-03 23:54:49 +0000910 else if ((alist = PyTuple_New(n)) == NULL)
911 goto Fail_1;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000912
913 for (j = 0, sqp = seqs; j < n; ++j, ++sqp) {
Tim Peters4e9afdc2001-05-03 23:54:49 +0000914 if (sqp->saw_StopIteration) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000915 Py_INCREF(Py_None);
916 item = Py_None;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000917 }
Guido van Rossum12d12c51993-10-26 17:58:25 +0000918 else {
Tim Peters4e9afdc2001-05-03 23:54:49 +0000919 item = PyIter_Next(sqp->it);
920 if (item)
921 ++numactive;
922 else {
Tim Peters4e9afdc2001-05-03 23:54:49 +0000923 if (PyErr_Occurred()) {
Tim Petersf4848da2001-05-05 00:14:56 +0000924 Py_XDECREF(alist);
925 goto Fail_1;
Guido van Rossum2d951851994-08-29 12:52:16 +0000926 }
Tim Peters4e9afdc2001-05-03 23:54:49 +0000927 Py_INCREF(Py_None);
928 item = Py_None;
929 sqp->saw_StopIteration = 1;
Guido van Rossum2d951851994-08-29 12:52:16 +0000930 }
Guido van Rossum12d12c51993-10-26 17:58:25 +0000931 }
Tim Peters4e9afdc2001-05-03 23:54:49 +0000932 if (alist)
933 PyTuple_SET_ITEM(alist, j, item);
934 else
Guido van Rossum2d951851994-08-29 12:52:16 +0000935 break;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000936 }
937
Guido van Rossum32120311995-07-10 13:52:21 +0000938 if (!alist)
939 alist = item;
Guido van Rossum2d951851994-08-29 12:52:16 +0000940
Tim Peters4e9afdc2001-05-03 23:54:49 +0000941 if (numactive == 0) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000942 Py_DECREF(alist);
Guido van Rossum2d951851994-08-29 12:52:16 +0000943 break;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000944 }
Guido van Rossum2d951851994-08-29 12:52:16 +0000945
Guido van Rossum79f25d91997-04-29 20:08:16 +0000946 if (func == Py_None)
Guido van Rossum32120311995-07-10 13:52:21 +0000947 value = alist;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000948 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000949 value = PyEval_CallObject(func, alist);
950 Py_DECREF(alist);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000951 if (value == NULL)
952 goto Fail_1;
Guido van Rossum2d951851994-08-29 12:52:16 +0000953 }
954 if (i >= len) {
Barry Warsawfa77e091999-01-28 18:49:12 +0000955 int status = PyList_Append(result, value);
Barry Warsaw21332871999-01-28 04:21:35 +0000956 Py_DECREF(value);
Barry Warsawfa77e091999-01-28 18:49:12 +0000957 if (status < 0)
958 goto Fail_1;
Guido van Rossum2d951851994-08-29 12:52:16 +0000959 }
Tim Peters4e9afdc2001-05-03 23:54:49 +0000960 else if (PyList_SetItem(result, i, value) < 0)
961 goto Fail_1;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000962 }
963
Guido van Rossumfa4ac711998-07-10 17:37:30 +0000964 if (i < len && PyList_SetSlice(result, i, len, NULL) < 0)
965 goto Fail_1;
966
Tim Peters4e9afdc2001-05-03 23:54:49 +0000967 goto Succeed;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000968
Guido van Rossum12d12c51993-10-26 17:58:25 +0000969Fail_1:
Guido van Rossum79f25d91997-04-29 20:08:16 +0000970 Py_DECREF(result);
Guido van Rossum12d12c51993-10-26 17:58:25 +0000971Fail_2:
Tim Peters4e9afdc2001-05-03 23:54:49 +0000972 result = NULL;
973Succeed:
974 assert(seqs);
975 for (i = 0; i < n; ++i)
976 Py_XDECREF(seqs[i].it);
977 PyMem_DEL(seqs);
978 return result;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000979}
980
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000981PyDoc_STRVAR(map_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000982"map(function, sequence[, sequence, ...]) -> list\n\
983\n\
984Return a list of the results of applying the function to the items of\n\
985the argument sequence(s). If more than one sequence is given, the\n\
986function is called with an argument list consisting of the corresponding\n\
987item of each sequence, substituting None for missing values when not all\n\
988sequences have the same length. If the function is None, return a list of\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000989the items of the sequence (or a list of tuples if more than one sequence).");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000990
991
Guido van Rossum79f25d91997-04-29 20:08:16 +0000992static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000993builtin_setattr(PyObject *self, PyObject *args)
Guido van Rossum33894be1992-01-27 16:53:09 +0000994{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000995 PyObject *v;
996 PyObject *name;
997 PyObject *value;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000998
Raymond Hettingerea3fdf42002-12-29 16:33:45 +0000999 if (!PyArg_UnpackTuple(args, "setattr", 3, 3, &v, &name, &value))
Guido van Rossum33894be1992-01-27 16:53:09 +00001000 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001001 if (PyObject_SetAttr(v, name, value) != 0)
Guido van Rossum33894be1992-01-27 16:53:09 +00001002 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001003 Py_INCREF(Py_None);
1004 return Py_None;
Guido van Rossum33894be1992-01-27 16:53:09 +00001005}
1006
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001007PyDoc_STRVAR(setattr_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001008"setattr(object, name, value)\n\
1009\n\
1010Set a named attribute on an object; setattr(x, 'y', v) is equivalent to\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001011``x.y = v''.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001012
1013
Guido van Rossum79f25d91997-04-29 20:08:16 +00001014static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001015builtin_delattr(PyObject *self, PyObject *args)
Guido van Rossum14144fc1994-08-29 12:53:40 +00001016{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001017 PyObject *v;
1018 PyObject *name;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001019
Raymond Hettingerea3fdf42002-12-29 16:33:45 +00001020 if (!PyArg_UnpackTuple(args, "delattr", 2, 2, &v, &name))
Guido van Rossum14144fc1994-08-29 12:53:40 +00001021 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001022 if (PyObject_SetAttr(v, name, (PyObject *)NULL) != 0)
Guido van Rossum14144fc1994-08-29 12:53:40 +00001023 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001024 Py_INCREF(Py_None);
1025 return Py_None;
Guido van Rossum14144fc1994-08-29 12:53:40 +00001026}
1027
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001028PyDoc_STRVAR(delattr_doc,
Guido van Rossumdf12a591998-11-23 22:13:04 +00001029"delattr(object, name)\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001030\n\
1031Delete a named attribute on an object; delattr(x, 'y') is equivalent to\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001032``del x.y''.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001033
1034
Guido van Rossum79f25d91997-04-29 20:08:16 +00001035static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001036builtin_hash(PyObject *self, PyObject *v)
Guido van Rossum9bfef441993-03-29 10:43:31 +00001037{
Guido van Rossum9bfef441993-03-29 10:43:31 +00001038 long x;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001039
Guido van Rossum79f25d91997-04-29 20:08:16 +00001040 x = PyObject_Hash(v);
Guido van Rossum9bfef441993-03-29 10:43:31 +00001041 if (x == -1)
1042 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001043 return PyInt_FromLong(x);
Guido van Rossum9bfef441993-03-29 10:43:31 +00001044}
1045
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001046PyDoc_STRVAR(hash_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001047"hash(object) -> integer\n\
1048\n\
1049Return a hash value for the object. Two objects with the same value have\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001050the same hash value. The reverse is not necessarily true, but likely.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001051
1052
Guido van Rossum79f25d91997-04-29 20:08:16 +00001053static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001054builtin_hex(PyObject *self, PyObject *v)
Guido van Rossum006bcd41991-10-24 14:54:44 +00001055{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001056 PyNumberMethods *nb;
Neil Schemenauer3a313e32004-07-19 16:29:17 +00001057 PyObject *res;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001058
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001059 if ((nb = v->ob_type->tp_as_number) == NULL ||
Guido van Rossum1899c2e1992-09-12 11:09:23 +00001060 nb->nb_hex == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001061 PyErr_SetString(PyExc_TypeError,
Guido van Rossum1899c2e1992-09-12 11:09:23 +00001062 "hex() argument can't be converted to hex");
1063 return NULL;
Guido van Rossum006bcd41991-10-24 14:54:44 +00001064 }
Neil Schemenauer3a313e32004-07-19 16:29:17 +00001065 res = (*nb->nb_hex)(v);
1066 if (res && !PyString_Check(res)) {
1067 PyErr_Format(PyExc_TypeError,
1068 "__hex__ returned non-string (type %.200s)",
1069 res->ob_type->tp_name);
1070 Py_DECREF(res);
1071 return NULL;
1072 }
1073 return res;
Guido van Rossum006bcd41991-10-24 14:54:44 +00001074}
1075
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001076PyDoc_STRVAR(hex_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001077"hex(number) -> string\n\
1078\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001079Return the hexadecimal representation of an integer or long integer.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001080
1081
Tim Petersdbd9ba62000-07-09 03:09:57 +00001082static PyObject *builtin_raw_input(PyObject *, PyObject *);
Guido van Rossum3165fe61992-09-25 21:59:05 +00001083
Guido van Rossum79f25d91997-04-29 20:08:16 +00001084static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001085builtin_input(PyObject *self, PyObject *args)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001086{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001087 PyObject *line;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001088 char *str;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001089 PyObject *res;
1090 PyObject *globals, *locals;
Hye-Shik Changff83c2b2004-02-02 13:39:01 +00001091 PyCompilerFlags cf;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001092
1093 line = builtin_raw_input(self, args);
Guido van Rossum3165fe61992-09-25 21:59:05 +00001094 if (line == NULL)
1095 return line;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001096 if (!PyArg_Parse(line, "s;embedded '\\0' in input line", &str))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001097 return NULL;
1098 while (*str == ' ' || *str == '\t')
1099 str++;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001100 globals = PyEval_GetGlobals();
1101 locals = PyEval_GetLocals();
1102 if (PyDict_GetItemString(globals, "__builtins__") == NULL) {
1103 if (PyDict_SetItemString(globals, "__builtins__",
1104 PyEval_GetBuiltins()) != 0)
Guido van Rossum6135a871995-01-09 17:53:26 +00001105 return NULL;
1106 }
Hye-Shik Changff83c2b2004-02-02 13:39:01 +00001107 cf.cf_flags = 0;
1108 PyEval_MergeCompilerFlags(&cf);
1109 res = PyRun_StringFlags(str, Py_eval_input, globals, locals, &cf);
Guido van Rossum79f25d91997-04-29 20:08:16 +00001110 Py_DECREF(line);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001111 return res;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001112}
1113
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001114PyDoc_STRVAR(input_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001115"input([prompt]) -> value\n\
1116\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001117Equivalent to eval(raw_input(prompt)).");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001118
1119
Guido van Rossume8811f81997-02-14 15:48:05 +00001120static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001121builtin_intern(PyObject *self, PyObject *args)
Guido van Rossume8811f81997-02-14 15:48:05 +00001122{
1123 PyObject *s;
Guido van Rossum43713e52000-02-29 13:59:29 +00001124 if (!PyArg_ParseTuple(args, "S:intern", &s))
Guido van Rossume8811f81997-02-14 15:48:05 +00001125 return NULL;
Jeremy Hylton4c989dd2004-08-07 19:20:05 +00001126 if (!PyString_CheckExact(s)) {
1127 PyErr_SetString(PyExc_TypeError,
1128 "can't intern subclass of string");
1129 return NULL;
1130 }
Guido van Rossume8811f81997-02-14 15:48:05 +00001131 Py_INCREF(s);
1132 PyString_InternInPlace(&s);
1133 return s;
1134}
1135
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001136PyDoc_STRVAR(intern_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001137"intern(string) -> string\n\
1138\n\
1139``Intern'' the given string. This enters the string in the (global)\n\
1140table of interned strings whose purpose is to speed up dictionary lookups.\n\
1141Return the string itself or the previously interned string object with the\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001142same value.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001143
1144
Guido van Rossum79f25d91997-04-29 20:08:16 +00001145static PyObject *
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001146builtin_iter(PyObject *self, PyObject *args)
1147{
1148 PyObject *v, *w = NULL;
1149
Raymond Hettingerea3fdf42002-12-29 16:33:45 +00001150 if (!PyArg_UnpackTuple(args, "iter", 1, 2, &v, &w))
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001151 return NULL;
1152 if (w == NULL)
1153 return PyObject_GetIter(v);
1154 if (!PyCallable_Check(v)) {
1155 PyErr_SetString(PyExc_TypeError,
1156 "iter(v, w): v must be callable");
1157 return NULL;
1158 }
1159 return PyCallIter_New(v, w);
1160}
1161
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001162PyDoc_STRVAR(iter_doc,
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001163"iter(collection) -> iterator\n\
1164iter(callable, sentinel) -> iterator\n\
1165\n\
1166Get an iterator from an object. In the first form, the argument must\n\
1167supply its own iterator, or be a sequence.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001168In the second form, the callable is called until it returns the sentinel.");
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001169
1170
1171static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001172builtin_len(PyObject *self, PyObject *v)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001173{
Martin v. Löwis18e16552006-02-15 17:27:45 +00001174 Py_ssize_t res;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001175
Jeremy Hylton03657cf2000-07-12 13:05:33 +00001176 res = PyObject_Size(v);
Guido van Rossum8ea9f4d1998-06-29 22:26:50 +00001177 if (res < 0 && PyErr_Occurred())
1178 return NULL;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001179 return PyInt_FromSsize_t(res);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001180}
1181
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001182PyDoc_STRVAR(len_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001183"len(object) -> integer\n\
1184\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001185Return the number of items of a sequence or mapping.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001186
1187
Guido van Rossum79f25d91997-04-29 20:08:16 +00001188static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001189builtin_locals(PyObject *self)
Guido van Rossum872537c1995-07-07 22:43:42 +00001190{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001191 PyObject *d;
Guido van Rossum872537c1995-07-07 22:43:42 +00001192
Guido van Rossum79f25d91997-04-29 20:08:16 +00001193 d = PyEval_GetLocals();
Neal Norwitz43bd4db2006-08-12 01:46:42 +00001194 Py_XINCREF(d);
Guido van Rossum872537c1995-07-07 22:43:42 +00001195 return d;
1196}
1197
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001198PyDoc_STRVAR(locals_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001199"locals() -> dictionary\n\
1200\n\
Raymond Hettinger69bf8f32003-01-04 02:16:22 +00001201Update and return a dictionary containing the current scope's local variables.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001202
1203
Guido van Rossum79f25d91997-04-29 20:08:16 +00001204static PyObject *
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001205min_max(PyObject *args, PyObject *kwds, int op)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001206{
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001207 PyObject *v, *it, *item, *val, *maxitem, *maxval, *keyfunc=NULL;
Martin v. Löwisfd39ad42004-08-12 14:42:37 +00001208 const char *name = op == Py_LT ? "min" : "max";
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001209
Guido van Rossum79f25d91997-04-29 20:08:16 +00001210 if (PyTuple_Size(args) > 1)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001211 v = args;
Martin v. Löwisfd39ad42004-08-12 14:42:37 +00001212 else if (!PyArg_UnpackTuple(args, (char *)name, 1, 1, &v))
Guido van Rossum3f5da241990-12-20 15:06:42 +00001213 return NULL;
Tim Peters67d687a2002-04-29 21:27:32 +00001214
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001215 if (kwds != NULL && PyDict_Check(kwds) && PyDict_Size(kwds)) {
1216 keyfunc = PyDict_GetItemString(kwds, "key");
1217 if (PyDict_Size(kwds)!=1 || keyfunc == NULL) {
Georg Brandl99d7e4e2005-08-31 22:21:15 +00001218 PyErr_Format(PyExc_TypeError,
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001219 "%s() got an unexpected keyword argument", name);
1220 return NULL;
1221 }
Georg Brandl99d7e4e2005-08-31 22:21:15 +00001222 }
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001223
Tim Petersc3074532001-05-03 07:00:32 +00001224 it = PyObject_GetIter(v);
1225 if (it == NULL)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001226 return NULL;
Tim Petersc3074532001-05-03 07:00:32 +00001227
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001228 maxitem = NULL; /* the result */
1229 maxval = NULL; /* the value associated with the result */
Brett Cannon9e635cf2004-12-07 00:25:35 +00001230 while (( item = PyIter_Next(it) )) {
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001231 /* get the value from the key function */
1232 if (keyfunc != NULL) {
1233 val = PyObject_CallFunctionObjArgs(keyfunc, item, NULL);
1234 if (val == NULL)
1235 goto Fail_it_item;
1236 }
1237 /* no key function; the value is the item */
1238 else {
1239 val = item;
1240 Py_INCREF(val);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001241 }
Tim Petersc3074532001-05-03 07:00:32 +00001242
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001243 /* maximum value and item are unset; set them */
1244 if (maxval == NULL) {
1245 maxitem = item;
1246 maxval = val;
1247 }
1248 /* maximum value and item are set; update them as necessary */
Guido van Rossum2d951851994-08-29 12:52:16 +00001249 else {
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001250 int cmp = PyObject_RichCompareBool(val, maxval, op);
1251 if (cmp < 0)
1252 goto Fail_it_item_and_val;
1253 else if (cmp > 0) {
1254 Py_DECREF(maxval);
1255 Py_DECREF(maxitem);
1256 maxval = val;
1257 maxitem = item;
Guido van Rossum53451b32001-01-17 15:47:24 +00001258 }
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001259 else {
1260 Py_DECREF(item);
1261 Py_DECREF(val);
Guido van Rossumc8b6df91997-05-23 00:06:51 +00001262 }
Guido van Rossum2d951851994-08-29 12:52:16 +00001263 }
Guido van Rossum3f5da241990-12-20 15:06:42 +00001264 }
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001265 if (PyErr_Occurred())
1266 goto Fail_it;
1267 if (maxval == NULL) {
Martin v. Löwisfd39ad42004-08-12 14:42:37 +00001268 PyErr_Format(PyExc_ValueError,
1269 "%s() arg is an empty sequence", name);
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001270 assert(maxitem == NULL);
1271 }
1272 else
1273 Py_DECREF(maxval);
Tim Petersc3074532001-05-03 07:00:32 +00001274 Py_DECREF(it);
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001275 return maxitem;
1276
1277Fail_it_item_and_val:
1278 Py_DECREF(val);
1279Fail_it_item:
1280 Py_DECREF(item);
1281Fail_it:
1282 Py_XDECREF(maxval);
1283 Py_XDECREF(maxitem);
1284 Py_DECREF(it);
1285 return NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001286}
1287
Guido van Rossum79f25d91997-04-29 20:08:16 +00001288static PyObject *
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001289builtin_min(PyObject *self, PyObject *args, PyObject *kwds)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001290{
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001291 return min_max(args, kwds, Py_LT);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001292}
1293
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001294PyDoc_STRVAR(min_doc,
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001295"min(iterable[, key=func]) -> value\n\
1296min(a, b, c, ...[, key=func]) -> value\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001297\n\
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001298With a single iterable argument, return its smallest item.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001299With two or more arguments, return the smallest argument.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001300
1301
Guido van Rossum79f25d91997-04-29 20:08:16 +00001302static PyObject *
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001303builtin_max(PyObject *self, PyObject *args, PyObject *kwds)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001304{
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001305 return min_max(args, kwds, Py_GT);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001306}
1307
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001308PyDoc_STRVAR(max_doc,
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001309"max(iterable[, key=func]) -> value\n\
1310max(a, b, c, ...[, key=func]) -> value\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001311\n\
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001312With a single iterable argument, return its largest item.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001313With two or more arguments, return the largest argument.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001314
1315
Guido van Rossum79f25d91997-04-29 20:08:16 +00001316static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001317builtin_oct(PyObject *self, PyObject *v)
Guido van Rossum006bcd41991-10-24 14:54:44 +00001318{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001319 PyNumberMethods *nb;
Neil Schemenauer3a313e32004-07-19 16:29:17 +00001320 PyObject *res;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001321
Guido van Rossum1899c2e1992-09-12 11:09:23 +00001322 if (v == NULL || (nb = v->ob_type->tp_as_number) == NULL ||
1323 nb->nb_oct == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001324 PyErr_SetString(PyExc_TypeError,
Guido van Rossum1899c2e1992-09-12 11:09:23 +00001325 "oct() argument can't be converted to oct");
1326 return NULL;
Guido van Rossum006bcd41991-10-24 14:54:44 +00001327 }
Neil Schemenauer3a313e32004-07-19 16:29:17 +00001328 res = (*nb->nb_oct)(v);
1329 if (res && !PyString_Check(res)) {
1330 PyErr_Format(PyExc_TypeError,
1331 "__oct__ returned non-string (type %.200s)",
1332 res->ob_type->tp_name);
1333 Py_DECREF(res);
1334 return NULL;
1335 }
1336 return res;
Guido van Rossum006bcd41991-10-24 14:54:44 +00001337}
1338
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001339PyDoc_STRVAR(oct_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001340"oct(number) -> string\n\
1341\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001342Return the octal representation of an integer or long integer.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001343
1344
Guido van Rossum79f25d91997-04-29 20:08:16 +00001345static PyObject *
Neal Norwitzc4edb0e2006-05-02 04:43:14 +00001346builtin_open(PyObject *self, PyObject *args, PyObject *kwds)
1347{
1348 return PyObject_Call((PyObject*)&PyFile_Type, args, kwds);
1349}
1350
1351PyDoc_STRVAR(open_doc,
1352"open(name[, mode[, buffering]]) -> file object\n\
1353\n\
1354Open a file using the file() type, returns a file object.");
1355
1356
1357static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001358builtin_ord(PyObject *self, PyObject* obj)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001359{
Guido van Rossum09095f32000-03-10 23:00:52 +00001360 long ord;
Martin v. Löwisd96ee902006-02-16 14:37:16 +00001361 Py_ssize_t size;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001362
Jeremy Hylton394b54d2000-04-12 21:19:47 +00001363 if (PyString_Check(obj)) {
1364 size = PyString_GET_SIZE(obj);
Andrew M. Kuchling34c20cf2000-12-20 14:36:56 +00001365 if (size == 1) {
Jeremy Hylton394b54d2000-04-12 21:19:47 +00001366 ord = (long)((unsigned char)*PyString_AS_STRING(obj));
Andrew M. Kuchling34c20cf2000-12-20 14:36:56 +00001367 return PyInt_FromLong(ord);
1368 }
Martin v. Löwis339d0f72001-08-17 18:39:25 +00001369#ifdef Py_USING_UNICODE
Jeremy Hylton394b54d2000-04-12 21:19:47 +00001370 } else if (PyUnicode_Check(obj)) {
1371 size = PyUnicode_GET_SIZE(obj);
Andrew M. Kuchling34c20cf2000-12-20 14:36:56 +00001372 if (size == 1) {
Jeremy Hylton394b54d2000-04-12 21:19:47 +00001373 ord = (long)*PyUnicode_AS_UNICODE(obj);
Andrew M. Kuchling34c20cf2000-12-20 14:36:56 +00001374 return PyInt_FromLong(ord);
1375 }
Martin v. Löwis339d0f72001-08-17 18:39:25 +00001376#endif
Jeremy Hylton394b54d2000-04-12 21:19:47 +00001377 } else {
1378 PyErr_Format(PyExc_TypeError,
Andrew M. Kuchlingf07aad12000-12-23 14:11:28 +00001379 "ord() expected string of length 1, but " \
Jeremy Hylton394b54d2000-04-12 21:19:47 +00001380 "%.200s found", obj->ob_type->tp_name);
Guido van Rossum09095f32000-03-10 23:00:52 +00001381 return NULL;
1382 }
1383
Guido van Rossumad991772001-01-12 16:03:05 +00001384 PyErr_Format(PyExc_TypeError,
Andrew M. Kuchlingf07aad12000-12-23 14:11:28 +00001385 "ord() expected a character, "
Martin v. Löwisd96ee902006-02-16 14:37:16 +00001386 "but string of length %zd found",
Jeremy Hylton394b54d2000-04-12 21:19:47 +00001387 size);
1388 return NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001389}
1390
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001391PyDoc_STRVAR(ord_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001392"ord(c) -> integer\n\
1393\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001394Return the integer ordinal of a one-character string.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001395
1396
Guido van Rossum79f25d91997-04-29 20:08:16 +00001397static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001398builtin_pow(PyObject *self, PyObject *args)
Guido van Rossum6a00cd81995-01-07 12:39:01 +00001399{
Guido van Rossum09df08a1998-05-22 00:51:39 +00001400 PyObject *v, *w, *z = Py_None;
Guido van Rossum6a00cd81995-01-07 12:39:01 +00001401
Raymond Hettingerea3fdf42002-12-29 16:33:45 +00001402 if (!PyArg_UnpackTuple(args, "pow", 2, 3, &v, &w, &z))
Guido van Rossum6a00cd81995-01-07 12:39:01 +00001403 return NULL;
Guido van Rossum09df08a1998-05-22 00:51:39 +00001404 return PyNumber_Power(v, w, z);
Guido van Rossumd4905451991-05-05 20:00:36 +00001405}
1406
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001407PyDoc_STRVAR(pow_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001408"pow(x, y[, z]) -> number\n\
1409\n\
1410With two arguments, equivalent to x**y. With three arguments,\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001411equivalent to (x**y) % z, but may be more efficient (e.g. for longs).");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001412
1413
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001414
1415/* Return number of items in range (lo, hi, step), when arguments are
1416 * PyInt or PyLong objects. step > 0 required. Return a value < 0 if
1417 * & only if the true value is too large to fit in a signed long.
1418 * Arguments MUST return 1 with either PyInt_Check() or
1419 * PyLong_Check(). Return -1 when there is an error.
1420 */
1421static long
1422get_len_of_range_longs(PyObject *lo, PyObject *hi, PyObject *step)
1423{
1424 /* -------------------------------------------------------------
1425 Algorithm is equal to that of get_len_of_range(), but it operates
1426 on PyObjects (which are assumed to be PyLong or PyInt objects).
1427 ---------------------------------------------------------------*/
1428 long n;
1429 PyObject *diff = NULL;
1430 PyObject *one = NULL;
1431 PyObject *tmp1 = NULL, *tmp2 = NULL, *tmp3 = NULL;
1432 /* holds sub-expression evaluations */
1433
1434 /* if (lo >= hi), return length of 0. */
1435 if (PyObject_Compare(lo, hi) >= 0)
1436 return 0;
1437
1438 if ((one = PyLong_FromLong(1L)) == NULL)
1439 goto Fail;
1440
1441 if ((tmp1 = PyNumber_Subtract(hi, lo)) == NULL)
1442 goto Fail;
1443
1444 if ((diff = PyNumber_Subtract(tmp1, one)) == NULL)
1445 goto Fail;
1446
1447 if ((tmp2 = PyNumber_FloorDivide(diff, step)) == NULL)
1448 goto Fail;
1449
1450 if ((tmp3 = PyNumber_Add(tmp2, one)) == NULL)
1451 goto Fail;
1452
1453 n = PyLong_AsLong(tmp3);
1454 if (PyErr_Occurred()) { /* Check for Overflow */
1455 PyErr_Clear();
1456 goto Fail;
1457 }
1458
1459 Py_DECREF(tmp3);
1460 Py_DECREF(tmp2);
1461 Py_DECREF(diff);
1462 Py_DECREF(tmp1);
1463 Py_DECREF(one);
1464 return n;
1465
1466 Fail:
1467 Py_XDECREF(tmp3);
1468 Py_XDECREF(tmp2);
1469 Py_XDECREF(diff);
1470 Py_XDECREF(tmp1);
1471 Py_XDECREF(one);
1472 return -1;
1473}
1474
1475/* An extension of builtin_range() that handles the case when PyLong
1476 * arguments are given. */
1477static PyObject *
1478handle_range_longs(PyObject *self, PyObject *args)
1479{
1480 PyObject *ilow;
Tim Peters874e1f72003-04-13 22:13:08 +00001481 PyObject *ihigh = NULL;
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001482 PyObject *istep = NULL;
Tim Peters874e1f72003-04-13 22:13:08 +00001483
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001484 PyObject *curnum = NULL;
1485 PyObject *v = NULL;
1486 long bign;
1487 int i, n;
1488 int cmp_result;
1489
Tim Peters874e1f72003-04-13 22:13:08 +00001490 PyObject *zero = PyLong_FromLong(0);
1491
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001492 if (zero == NULL)
1493 return NULL;
1494
Tim Peters874e1f72003-04-13 22:13:08 +00001495 if (!PyArg_UnpackTuple(args, "range", 1, 3, &ilow, &ihigh, &istep)) {
1496 Py_DECREF(zero);
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001497 return NULL;
1498 }
1499
Tim Peters874e1f72003-04-13 22:13:08 +00001500 /* Figure out which way we were called, supply defaults, and be
1501 * sure to incref everything so that the decrefs at the end
1502 * are correct.
1503 */
1504 assert(ilow != NULL);
1505 if (ihigh == NULL) {
1506 /* only 1 arg -- it's the upper limit */
1507 ihigh = ilow;
1508 ilow = NULL;
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001509 }
Tim Peters874e1f72003-04-13 22:13:08 +00001510 assert(ihigh != NULL);
1511 Py_INCREF(ihigh);
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001512
Tim Peters874e1f72003-04-13 22:13:08 +00001513 /* ihigh correct now; do ilow */
1514 if (ilow == NULL)
1515 ilow = zero;
1516 Py_INCREF(ilow);
1517
1518 /* ilow and ihigh correct now; do istep */
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001519 if (istep == NULL) {
1520 istep = PyLong_FromLong(1L);
1521 if (istep == NULL)
1522 goto Fail;
1523 }
1524 else {
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001525 Py_INCREF(istep);
1526 }
1527
Tim Peters874e1f72003-04-13 22:13:08 +00001528 if (!PyInt_Check(ilow) && !PyLong_Check(ilow)) {
Guido van Rossum28e83e32003-04-15 12:43:26 +00001529 PyErr_Format(PyExc_TypeError,
Alex Martellif471d472003-04-23 13:00:44 +00001530 "range() integer start argument expected, got %s.",
Guido van Rossum817d6c92003-04-14 18:25:04 +00001531 ilow->ob_type->tp_name);
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001532 goto Fail;
1533 }
1534
Tim Peters874e1f72003-04-13 22:13:08 +00001535 if (!PyInt_Check(ihigh) && !PyLong_Check(ihigh)) {
Guido van Rossum28e83e32003-04-15 12:43:26 +00001536 PyErr_Format(PyExc_TypeError,
Alex Martellif471d472003-04-23 13:00:44 +00001537 "range() integer end argument expected, got %s.",
Guido van Rossum817d6c92003-04-14 18:25:04 +00001538 ihigh->ob_type->tp_name);
Tim Peters874e1f72003-04-13 22:13:08 +00001539 goto Fail;
1540 }
1541
1542 if (!PyInt_Check(istep) && !PyLong_Check(istep)) {
Guido van Rossum28e83e32003-04-15 12:43:26 +00001543 PyErr_Format(PyExc_TypeError,
Alex Martellif471d472003-04-23 13:00:44 +00001544 "range() integer step argument expected, got %s.",
Guido van Rossum817d6c92003-04-14 18:25:04 +00001545 istep->ob_type->tp_name);
Tim Peters874e1f72003-04-13 22:13:08 +00001546 goto Fail;
1547 }
1548
1549 if (PyObject_Cmp(istep, zero, &cmp_result) == -1)
1550 goto Fail;
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001551 if (cmp_result == 0) {
1552 PyErr_SetString(PyExc_ValueError,
Alex Martellif471d472003-04-23 13:00:44 +00001553 "range() step argument must not be zero");
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001554 goto Fail;
1555 }
1556
1557 if (cmp_result > 0)
1558 bign = get_len_of_range_longs(ilow, ihigh, istep);
1559 else {
1560 PyObject *neg_istep = PyNumber_Negative(istep);
1561 if (neg_istep == NULL)
1562 goto Fail;
1563 bign = get_len_of_range_longs(ihigh, ilow, neg_istep);
1564 Py_DECREF(neg_istep);
1565 }
1566
1567 n = (int)bign;
1568 if (bign < 0 || (long)n != bign) {
1569 PyErr_SetString(PyExc_OverflowError,
1570 "range() result has too many items");
1571 goto Fail;
1572 }
1573
1574 v = PyList_New(n);
1575 if (v == NULL)
1576 goto Fail;
1577
1578 curnum = ilow;
1579 Py_INCREF(curnum);
1580
1581 for (i = 0; i < n; i++) {
1582 PyObject *w = PyNumber_Long(curnum);
1583 PyObject *tmp_num;
1584 if (w == NULL)
1585 goto Fail;
1586
1587 PyList_SET_ITEM(v, i, w);
1588
1589 tmp_num = PyNumber_Add(curnum, istep);
1590 if (tmp_num == NULL)
1591 goto Fail;
1592
1593 Py_DECREF(curnum);
1594 curnum = tmp_num;
1595 }
Tim Peters874e1f72003-04-13 22:13:08 +00001596 Py_DECREF(ilow);
1597 Py_DECREF(ihigh);
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001598 Py_DECREF(istep);
1599 Py_DECREF(zero);
Tim Peters874e1f72003-04-13 22:13:08 +00001600 Py_DECREF(curnum);
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001601 return v;
1602
1603 Fail:
Tim Peters874e1f72003-04-13 22:13:08 +00001604 Py_DECREF(ilow);
1605 Py_DECREF(ihigh);
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001606 Py_XDECREF(istep);
Tim Peters874e1f72003-04-13 22:13:08 +00001607 Py_DECREF(zero);
1608 Py_XDECREF(curnum);
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001609 Py_XDECREF(v);
1610 return NULL;
1611}
1612
Guido van Rossum124eff01999-02-23 16:11:01 +00001613/* Return number of items in range/xrange (lo, hi, step). step > 0
1614 * required. Return a value < 0 if & only if the true value is too
1615 * large to fit in a signed long.
1616 */
1617static long
Thomas Wouterse28c2962000-07-23 22:21:32 +00001618get_len_of_range(long lo, long hi, long step)
Guido van Rossum124eff01999-02-23 16:11:01 +00001619{
1620 /* -------------------------------------------------------------
1621 If lo >= hi, the range is empty.
1622 Else if n values are in the range, the last one is
1623 lo + (n-1)*step, which must be <= hi-1. Rearranging,
1624 n <= (hi - lo - 1)/step + 1, so taking the floor of the RHS gives
1625 the proper value. Since lo < hi in this case, hi-lo-1 >= 0, so
1626 the RHS is non-negative and so truncation is the same as the
1627 floor. Letting M be the largest positive long, the worst case
1628 for the RHS numerator is hi=M, lo=-M-1, and then
1629 hi-lo-1 = M-(-M-1)-1 = 2*M. Therefore unsigned long has enough
1630 precision to compute the RHS exactly.
1631 ---------------------------------------------------------------*/
1632 long n = 0;
1633 if (lo < hi) {
1634 unsigned long uhi = (unsigned long)hi;
1635 unsigned long ulo = (unsigned long)lo;
1636 unsigned long diff = uhi - ulo - 1;
1637 n = (long)(diff / (unsigned long)step + 1);
1638 }
1639 return n;
1640}
1641
Guido van Rossum79f25d91997-04-29 20:08:16 +00001642static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001643builtin_range(PyObject *self, PyObject *args)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001644{
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001645 long ilow = 0, ihigh = 0, istep = 1;
Guido van Rossum124eff01999-02-23 16:11:01 +00001646 long bign;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001647 int i, n;
Guido van Rossum124eff01999-02-23 16:11:01 +00001648
Guido van Rossum79f25d91997-04-29 20:08:16 +00001649 PyObject *v;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001650
Guido van Rossum79f25d91997-04-29 20:08:16 +00001651 if (PyTuple_Size(args) <= 1) {
1652 if (!PyArg_ParseTuple(args,
Guido van Rossum0865dd91995-01-17 16:30:22 +00001653 "l;range() requires 1-3 int arguments",
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001654 &ihigh)) {
1655 PyErr_Clear();
1656 return handle_range_longs(self, args);
1657 }
Guido van Rossum3f5da241990-12-20 15:06:42 +00001658 }
1659 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001660 if (!PyArg_ParseTuple(args,
Guido van Rossum0865dd91995-01-17 16:30:22 +00001661 "ll|l;range() requires 1-3 int arguments",
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001662 &ilow, &ihigh, &istep)) {
1663 PyErr_Clear();
1664 return handle_range_longs(self, args);
1665 }
Guido van Rossum3f5da241990-12-20 15:06:42 +00001666 }
1667 if (istep == 0) {
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001668 PyErr_SetString(PyExc_ValueError,
Alex Martellif471d472003-04-23 13:00:44 +00001669 "range() step argument must not be zero");
Guido van Rossum3f5da241990-12-20 15:06:42 +00001670 return NULL;
1671 }
Guido van Rossum124eff01999-02-23 16:11:01 +00001672 if (istep > 0)
1673 bign = get_len_of_range(ilow, ihigh, istep);
1674 else
1675 bign = get_len_of_range(ihigh, ilow, -istep);
1676 n = (int)bign;
1677 if (bign < 0 || (long)n != bign) {
Guido van Rossume23cde21999-01-12 05:07:47 +00001678 PyErr_SetString(PyExc_OverflowError,
Fred Drake661ea262000-10-24 19:57:45 +00001679 "range() result has too many items");
Guido van Rossume23cde21999-01-12 05:07:47 +00001680 return NULL;
1681 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00001682 v = PyList_New(n);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001683 if (v == NULL)
1684 return NULL;
1685 for (i = 0; i < n; i++) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001686 PyObject *w = PyInt_FromLong(ilow);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001687 if (w == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001688 Py_DECREF(v);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001689 return NULL;
1690 }
Guido van Rossuma937d141998-04-24 18:22:02 +00001691 PyList_SET_ITEM(v, i, w);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001692 ilow += istep;
1693 }
1694 return v;
1695}
1696
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001697PyDoc_STRVAR(range_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001698"range([start,] stop[, step]) -> list of integers\n\
1699\n\
1700Return a list containing an arithmetic progression of integers.\n\
1701range(i, j) returns [i, i+1, i+2, ..., j-1]; start (!) defaults to 0.\n\
1702When step is given, it specifies the increment (or decrement).\n\
1703For example, range(4) returns [0, 1, 2, 3]. The end point is omitted!\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001704These are exactly the valid indices for a list of 4 elements.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001705
1706
Guido van Rossum79f25d91997-04-29 20:08:16 +00001707static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001708builtin_raw_input(PyObject *self, PyObject *args)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001709{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001710 PyObject *v = NULL;
Martin v. Löwis31d2df52002-08-14 15:46:02 +00001711 PyObject *fin = PySys_GetObject("stdin");
1712 PyObject *fout = PySys_GetObject("stdout");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001713
Raymond Hettingerea3fdf42002-12-29 16:33:45 +00001714 if (!PyArg_UnpackTuple(args, "[raw_]input", 0, 1, &v))
Guido van Rossum3165fe61992-09-25 21:59:05 +00001715 return NULL;
Martin v. Löwis31d2df52002-08-14 15:46:02 +00001716
1717 if (fin == NULL) {
Alex Martellia9b9c9f2003-04-23 13:34:35 +00001718 PyErr_SetString(PyExc_RuntimeError, "[raw_]input: lost sys.stdin");
Martin v. Löwis31d2df52002-08-14 15:46:02 +00001719 return NULL;
1720 }
1721 if (fout == NULL) {
Alex Martellia9b9c9f2003-04-23 13:34:35 +00001722 PyErr_SetString(PyExc_RuntimeError, "[raw_]input: lost sys.stdout");
Martin v. Löwis31d2df52002-08-14 15:46:02 +00001723 return NULL;
1724 }
1725 if (PyFile_SoftSpace(fout, 0)) {
1726 if (PyFile_WriteString(" ", fout) != 0)
1727 return NULL;
1728 }
Georg Brandl7e3ba2a2006-08-06 08:23:54 +00001729 if (PyFile_AsFile(fin) && PyFile_AsFile(fout)
Martin v. Löwis566f6af2002-10-26 14:39:10 +00001730 && isatty(fileno(PyFile_AsFile(fin)))
1731 && isatty(fileno(PyFile_AsFile(fout)))) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001732 PyObject *po;
Guido van Rossum872537c1995-07-07 22:43:42 +00001733 char *prompt;
1734 char *s;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001735 PyObject *result;
Guido van Rossum872537c1995-07-07 22:43:42 +00001736 if (v != NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001737 po = PyObject_Str(v);
Guido van Rossum872537c1995-07-07 22:43:42 +00001738 if (po == NULL)
1739 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001740 prompt = PyString_AsString(po);
Guido van Rossumd9b52081998-06-26 18:25:38 +00001741 if (prompt == NULL)
1742 return NULL;
Guido van Rossum872537c1995-07-07 22:43:42 +00001743 }
1744 else {
1745 po = NULL;
1746 prompt = "";
1747 }
Michael W. Hudson52db5192004-08-02 13:21:09 +00001748 s = PyOS_Readline(PyFile_AsFile(fin), PyFile_AsFile(fout),
Martin v. Löwis566f6af2002-10-26 14:39:10 +00001749 prompt);
Guido van Rossum79f25d91997-04-29 20:08:16 +00001750 Py_XDECREF(po);
Guido van Rossum872537c1995-07-07 22:43:42 +00001751 if (s == NULL) {
Michael W. Hudson30ea2f22004-07-07 17:44:12 +00001752 if (!PyErr_Occurred())
1753 PyErr_SetNone(PyExc_KeyboardInterrupt);
Guido van Rossum872537c1995-07-07 22:43:42 +00001754 return NULL;
1755 }
1756 if (*s == '\0') {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001757 PyErr_SetNone(PyExc_EOFError);
Guido van Rossum872537c1995-07-07 22:43:42 +00001758 result = NULL;
1759 }
1760 else { /* strip trailing '\n' */
Guido van Rossum106f2da2000-06-28 21:12:25 +00001761 size_t len = strlen(s);
Martin v. Löwisb1ed7fa2006-04-13 07:52:27 +00001762 if (len > PY_SSIZE_T_MAX) {
Martin v. Löwis31d2df52002-08-14 15:46:02 +00001763 PyErr_SetString(PyExc_OverflowError,
Alex Martellia9b9c9f2003-04-23 13:34:35 +00001764 "[raw_]input: input too long");
Guido van Rossum106f2da2000-06-28 21:12:25 +00001765 result = NULL;
1766 }
1767 else {
Martin v. Löwisb1ed7fa2006-04-13 07:52:27 +00001768 result = PyString_FromStringAndSize(s, len-1);
Guido van Rossum106f2da2000-06-28 21:12:25 +00001769 }
Guido van Rossum872537c1995-07-07 22:43:42 +00001770 }
Guido van Rossumb18618d2000-05-03 23:44:39 +00001771 PyMem_FREE(s);
Guido van Rossum872537c1995-07-07 22:43:42 +00001772 return result;
1773 }
Guido van Rossum90933611991-06-07 16:10:43 +00001774 if (v != NULL) {
Martin v. Löwis31d2df52002-08-14 15:46:02 +00001775 if (PyFile_WriteObject(v, fout, Py_PRINT_RAW) != 0)
Guido van Rossum90933611991-06-07 16:10:43 +00001776 return NULL;
1777 }
Martin v. Löwis31d2df52002-08-14 15:46:02 +00001778 return PyFile_GetLine(fin, -1);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001779}
1780
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001781PyDoc_STRVAR(raw_input_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001782"raw_input([prompt]) -> string\n\
1783\n\
1784Read a string from standard input. The trailing newline is stripped.\n\
1785If the user hits EOF (Unix: Ctl-D, Windows: Ctl-Z+Return), raise EOFError.\n\
1786On Unix, GNU readline is used if enabled. The prompt string, if given,\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001787is printed without a trailing newline before reading.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001788
1789
Guido van Rossum79f25d91997-04-29 20:08:16 +00001790static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001791builtin_reduce(PyObject *self, PyObject *args)
Guido van Rossum12d12c51993-10-26 17:58:25 +00001792{
Tim Peters15d81ef2001-05-04 04:39:21 +00001793 PyObject *seq, *func, *result = NULL, *it;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001794
Raymond Hettingerea3fdf42002-12-29 16:33:45 +00001795 if (!PyArg_UnpackTuple(args, "reduce", 2, 3, &func, &seq, &result))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001796 return NULL;
1797 if (result != NULL)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001798 Py_INCREF(result);
Guido van Rossum12d12c51993-10-26 17:58:25 +00001799
Tim Peters15d81ef2001-05-04 04:39:21 +00001800 it = PyObject_GetIter(seq);
1801 if (it == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001802 PyErr_SetString(PyExc_TypeError,
Tim Peters15d81ef2001-05-04 04:39:21 +00001803 "reduce() arg 2 must support iteration");
1804 Py_XDECREF(result);
Guido van Rossum12d12c51993-10-26 17:58:25 +00001805 return NULL;
1806 }
1807
Guido van Rossum79f25d91997-04-29 20:08:16 +00001808 if ((args = PyTuple_New(2)) == NULL)
Guido van Rossum2d951851994-08-29 12:52:16 +00001809 goto Fail;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001810
Tim Peters15d81ef2001-05-04 04:39:21 +00001811 for (;;) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001812 PyObject *op2;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001813
1814 if (args->ob_refcnt > 1) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001815 Py_DECREF(args);
1816 if ((args = PyTuple_New(2)) == NULL)
Guido van Rossum2d951851994-08-29 12:52:16 +00001817 goto Fail;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001818 }
1819
Tim Peters15d81ef2001-05-04 04:39:21 +00001820 op2 = PyIter_Next(it);
1821 if (op2 == NULL) {
Tim Petersf4848da2001-05-05 00:14:56 +00001822 if (PyErr_Occurred())
1823 goto Fail;
1824 break;
Guido van Rossum2d951851994-08-29 12:52:16 +00001825 }
Guido van Rossum12d12c51993-10-26 17:58:25 +00001826
Guido van Rossum2d951851994-08-29 12:52:16 +00001827 if (result == NULL)
1828 result = op2;
1829 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001830 PyTuple_SetItem(args, 0, result);
1831 PyTuple_SetItem(args, 1, op2);
1832 if ((result = PyEval_CallObject(func, args)) == NULL)
Guido van Rossum2d951851994-08-29 12:52:16 +00001833 goto Fail;
1834 }
Guido van Rossum12d12c51993-10-26 17:58:25 +00001835 }
1836
Guido van Rossum79f25d91997-04-29 20:08:16 +00001837 Py_DECREF(args);
Guido van Rossum12d12c51993-10-26 17:58:25 +00001838
Guido van Rossum2d951851994-08-29 12:52:16 +00001839 if (result == NULL)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001840 PyErr_SetString(PyExc_TypeError,
Fred Drake661ea262000-10-24 19:57:45 +00001841 "reduce() of empty sequence with no initial value");
Guido van Rossum2d951851994-08-29 12:52:16 +00001842
Tim Peters15d81ef2001-05-04 04:39:21 +00001843 Py_DECREF(it);
Guido van Rossum12d12c51993-10-26 17:58:25 +00001844 return result;
1845
Guido van Rossum2d951851994-08-29 12:52:16 +00001846Fail:
Guido van Rossum79f25d91997-04-29 20:08:16 +00001847 Py_XDECREF(args);
1848 Py_XDECREF(result);
Tim Peters15d81ef2001-05-04 04:39:21 +00001849 Py_DECREF(it);
Guido van Rossum12d12c51993-10-26 17:58:25 +00001850 return NULL;
1851}
1852
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001853PyDoc_STRVAR(reduce_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001854"reduce(function, sequence[, initial]) -> value\n\
1855\n\
1856Apply a function of two arguments cumulatively to the items of a sequence,\n\
1857from left to right, so as to reduce the sequence to a single value.\n\
1858For example, reduce(lambda x, y: x+y, [1, 2, 3, 4, 5]) calculates\n\
1859((((1+2)+3)+4)+5). If initial is present, it is placed before the items\n\
1860of the sequence in the calculation, and serves as a default when the\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001861sequence is empty.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001862
1863
Guido van Rossum79f25d91997-04-29 20:08:16 +00001864static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001865builtin_reload(PyObject *self, PyObject *v)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001866{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001867 return PyImport_ReloadModule(v);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001868}
1869
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001870PyDoc_STRVAR(reload_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001871"reload(module) -> module\n\
1872\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001873Reload the module. The module must have been successfully imported before.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001874
1875
Guido van Rossum79f25d91997-04-29 20:08:16 +00001876static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001877builtin_repr(PyObject *self, PyObject *v)
Guido van Rossumc89705d1992-11-26 08:54:07 +00001878{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001879 return PyObject_Repr(v);
Guido van Rossumc89705d1992-11-26 08:54:07 +00001880}
1881
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001882PyDoc_STRVAR(repr_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001883"repr(object) -> string\n\
1884\n\
1885Return the canonical string representation of the object.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001886For most object types, eval(repr(object)) == object.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001887
1888
Guido van Rossum79f25d91997-04-29 20:08:16 +00001889static PyObject *
Georg Brandlccadf842006-03-31 18:54:53 +00001890builtin_round(PyObject *self, PyObject *args, PyObject *kwds)
Guido van Rossum9e51f9b1993-02-12 16:29:05 +00001891{
Georg Brandlccadf842006-03-31 18:54:53 +00001892 double number;
Guido van Rossum9e51f9b1993-02-12 16:29:05 +00001893 double f;
1894 int ndigits = 0;
Guido van Rossum9e51f9b1993-02-12 16:29:05 +00001895 int i;
Georg Brandlccadf842006-03-31 18:54:53 +00001896 static char *kwlist[] = {"number", "ndigits", 0};
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001897
Georg Brandlccadf842006-03-31 18:54:53 +00001898 if (!PyArg_ParseTupleAndKeywords(args, kwds, "d|i:round",
1899 kwlist, &number, &ndigits))
1900 return NULL;
Guido van Rossum9e51f9b1993-02-12 16:29:05 +00001901 f = 1.0;
Guido van Rossum1e162d31998-05-09 14:42:25 +00001902 i = abs(ndigits);
1903 while (--i >= 0)
Guido van Rossum9e51f9b1993-02-12 16:29:05 +00001904 f = f*10.0;
Guido van Rossum1e162d31998-05-09 14:42:25 +00001905 if (ndigits < 0)
Georg Brandlccadf842006-03-31 18:54:53 +00001906 number /= f;
Guido van Rossum9e51f9b1993-02-12 16:29:05 +00001907 else
Georg Brandlccadf842006-03-31 18:54:53 +00001908 number *= f;
1909 if (number >= 0.0)
1910 number = floor(number + 0.5);
Guido van Rossum1e162d31998-05-09 14:42:25 +00001911 else
Georg Brandlccadf842006-03-31 18:54:53 +00001912 number = ceil(number - 0.5);
Guido van Rossum1e162d31998-05-09 14:42:25 +00001913 if (ndigits < 0)
Georg Brandlccadf842006-03-31 18:54:53 +00001914 number *= f;
Guido van Rossum1e162d31998-05-09 14:42:25 +00001915 else
Georg Brandlccadf842006-03-31 18:54:53 +00001916 number /= f;
1917 return PyFloat_FromDouble(number);
Guido van Rossum9e51f9b1993-02-12 16:29:05 +00001918}
1919
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001920PyDoc_STRVAR(round_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001921"round(number[, ndigits]) -> floating point number\n\
1922\n\
1923Round a number to a given precision in decimal digits (default 0 digits).\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001924This always returns a floating point number. Precision may be negative.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001925
Raymond Hettinger64958a12003-12-17 20:43:33 +00001926static PyObject *
1927builtin_sorted(PyObject *self, PyObject *args, PyObject *kwds)
1928{
1929 PyObject *newlist, *v, *seq, *compare=NULL, *keyfunc=NULL, *newargs;
1930 PyObject *callable;
Martin v. Löwis15e62742006-02-27 16:46:16 +00001931 static char *kwlist[] = {"iterable", "cmp", "key", "reverse", 0};
Neal Norwitz6f0d4792005-12-15 06:40:36 +00001932 int reverse;
Raymond Hettinger64958a12003-12-17 20:43:33 +00001933
Neal Norwitz6f0d4792005-12-15 06:40:36 +00001934 /* args 1-4 should match listsort in Objects/listobject.c */
Armin Rigo71d7e702005-09-20 18:13:03 +00001935 if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|OOi:sorted",
1936 kwlist, &seq, &compare, &keyfunc, &reverse))
1937 return NULL;
Raymond Hettinger64958a12003-12-17 20:43:33 +00001938
1939 newlist = PySequence_List(seq);
1940 if (newlist == NULL)
1941 return NULL;
1942
1943 callable = PyObject_GetAttrString(newlist, "sort");
1944 if (callable == NULL) {
1945 Py_DECREF(newlist);
1946 return NULL;
1947 }
Georg Brandl99d7e4e2005-08-31 22:21:15 +00001948
Raymond Hettinger64958a12003-12-17 20:43:33 +00001949 newargs = PyTuple_GetSlice(args, 1, 4);
1950 if (newargs == NULL) {
1951 Py_DECREF(newlist);
1952 Py_DECREF(callable);
1953 return NULL;
1954 }
1955
1956 v = PyObject_Call(callable, newargs, kwds);
1957 Py_DECREF(newargs);
1958 Py_DECREF(callable);
1959 if (v == NULL) {
1960 Py_DECREF(newlist);
1961 return NULL;
1962 }
1963 Py_DECREF(v);
1964 return newlist;
1965}
1966
1967PyDoc_STRVAR(sorted_doc,
1968"sorted(iterable, cmp=None, key=None, reverse=False) --> new sorted list");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001969
Guido van Rossum79f25d91997-04-29 20:08:16 +00001970static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001971builtin_vars(PyObject *self, PyObject *args)
Guido van Rossum2d951851994-08-29 12:52:16 +00001972{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001973 PyObject *v = NULL;
1974 PyObject *d;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001975
Raymond Hettingerea3fdf42002-12-29 16:33:45 +00001976 if (!PyArg_UnpackTuple(args, "vars", 0, 1, &v))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001977 return NULL;
Guido van Rossum2d951851994-08-29 12:52:16 +00001978 if (v == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001979 d = PyEval_GetLocals();
Guido van Rossum53bb7ff1995-07-26 16:26:31 +00001980 if (d == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001981 if (!PyErr_Occurred())
1982 PyErr_SetString(PyExc_SystemError,
Alex Martellia9b9c9f2003-04-23 13:34:35 +00001983 "vars(): no locals!?");
Guido van Rossum53bb7ff1995-07-26 16:26:31 +00001984 }
1985 else
Guido van Rossum79f25d91997-04-29 20:08:16 +00001986 Py_INCREF(d);
Guido van Rossum2d951851994-08-29 12:52:16 +00001987 }
1988 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001989 d = PyObject_GetAttrString(v, "__dict__");
Guido van Rossum2d951851994-08-29 12:52:16 +00001990 if (d == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001991 PyErr_SetString(PyExc_TypeError,
Guido van Rossum2d951851994-08-29 12:52:16 +00001992 "vars() argument must have __dict__ attribute");
1993 return NULL;
1994 }
1995 }
1996 return d;
1997}
1998
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001999PyDoc_STRVAR(vars_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002000"vars([object]) -> dictionary\n\
2001\n\
2002Without arguments, equivalent to locals().\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002003With an argument, equivalent to object.__dict__.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002004
Alex Martellia70b1912003-04-22 08:12:33 +00002005
2006static PyObject*
2007builtin_sum(PyObject *self, PyObject *args)
2008{
2009 PyObject *seq;
2010 PyObject *result = NULL;
2011 PyObject *temp, *item, *iter;
2012
Raymond Hettinger5cf63942003-10-25 06:41:37 +00002013 if (!PyArg_UnpackTuple(args, "sum", 1, 2, &seq, &result))
Alex Martellia70b1912003-04-22 08:12:33 +00002014 return NULL;
2015
2016 iter = PyObject_GetIter(seq);
2017 if (iter == NULL)
2018 return NULL;
2019
2020 if (result == NULL) {
2021 result = PyInt_FromLong(0);
2022 if (result == NULL) {
2023 Py_DECREF(iter);
2024 return NULL;
2025 }
2026 } else {
2027 /* reject string values for 'start' parameter */
2028 if (PyObject_TypeCheck(result, &PyBaseString_Type)) {
2029 PyErr_SetString(PyExc_TypeError,
Alex Martellia9b9c9f2003-04-23 13:34:35 +00002030 "sum() can't sum strings [use ''.join(seq) instead]");
Alex Martellia70b1912003-04-22 08:12:33 +00002031 Py_DECREF(iter);
2032 return NULL;
2033 }
Alex Martelli41c9f882003-04-22 09:24:48 +00002034 Py_INCREF(result);
Alex Martellia70b1912003-04-22 08:12:33 +00002035 }
2036
2037 for(;;) {
2038 item = PyIter_Next(iter);
2039 if (item == NULL) {
2040 /* error, or end-of-sequence */
2041 if (PyErr_Occurred()) {
2042 Py_DECREF(result);
2043 result = NULL;
2044 }
2045 break;
2046 }
Alex Martellia253e182003-10-25 23:24:14 +00002047 temp = PyNumber_Add(result, item);
Alex Martellia70b1912003-04-22 08:12:33 +00002048 Py_DECREF(result);
2049 Py_DECREF(item);
2050 result = temp;
2051 if (result == NULL)
2052 break;
2053 }
2054 Py_DECREF(iter);
2055 return result;
2056}
2057
2058PyDoc_STRVAR(sum_doc,
Georg Brandl8134d062006-10-12 12:33:07 +00002059"sum(sequence[, start]) -> value\n\
Alex Martellia70b1912003-04-22 08:12:33 +00002060\n\
2061Returns the sum of a sequence of numbers (NOT strings) plus the value\n\
Georg Brandl8134d062006-10-12 12:33:07 +00002062of parameter 'start' (which defaults to 0). When the sequence is\n\
2063empty, returns start.");
Alex Martellia70b1912003-04-22 08:12:33 +00002064
2065
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002066static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002067builtin_isinstance(PyObject *self, PyObject *args)
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002068{
2069 PyObject *inst;
2070 PyObject *cls;
Guido van Rossum823649d2001-03-21 18:40:58 +00002071 int retval;
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002072
Raymond Hettingerea3fdf42002-12-29 16:33:45 +00002073 if (!PyArg_UnpackTuple(args, "isinstance", 2, 2, &inst, &cls))
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002074 return NULL;
Guido van Rossumf5dd9141997-12-02 19:11:45 +00002075
Guido van Rossum823649d2001-03-21 18:40:58 +00002076 retval = PyObject_IsInstance(inst, cls);
2077 if (retval < 0)
Guido van Rossumad991772001-01-12 16:03:05 +00002078 return NULL;
Guido van Rossum77f6a652002-04-03 22:41:51 +00002079 return PyBool_FromLong(retval);
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002080}
2081
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002082PyDoc_STRVAR(isinstance_doc,
Guido van Rossum77f6a652002-04-03 22:41:51 +00002083"isinstance(object, class-or-type-or-tuple) -> bool\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002084\n\
2085Return whether an object is an instance of a class or of a subclass thereof.\n\
Guido van Rossum03290ec2001-10-07 20:54:12 +00002086With a type as second argument, return whether that is the object's type.\n\
2087The form using a tuple, isinstance(x, (A, B, ...)), is a shortcut for\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002088isinstance(x, A) or isinstance(x, B) or ... (etc.).");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002089
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002090
2091static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002092builtin_issubclass(PyObject *self, PyObject *args)
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002093{
2094 PyObject *derived;
2095 PyObject *cls;
2096 int retval;
2097
Raymond Hettingerea3fdf42002-12-29 16:33:45 +00002098 if (!PyArg_UnpackTuple(args, "issubclass", 2, 2, &derived, &cls))
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002099 return NULL;
Guido van Rossum668213d1999-06-16 17:28:37 +00002100
Guido van Rossum823649d2001-03-21 18:40:58 +00002101 retval = PyObject_IsSubclass(derived, cls);
2102 if (retval < 0)
2103 return NULL;
Guido van Rossum77f6a652002-04-03 22:41:51 +00002104 return PyBool_FromLong(retval);
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002105}
2106
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002107PyDoc_STRVAR(issubclass_doc,
Guido van Rossum77f6a652002-04-03 22:41:51 +00002108"issubclass(C, B) -> bool\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002109\n\
Walter Dörwaldd9a6ad32002-12-12 16:41:44 +00002110Return whether class C is a subclass (i.e., a derived class) of class B.\n\
2111When using a tuple as the second argument issubclass(X, (A, B, ...)),\n\
2112is a shortcut for issubclass(X, A) or issubclass(X, B) or ... (etc.).");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002113
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002114
Barry Warsawbd599b52000-08-03 15:45:29 +00002115static PyObject*
2116builtin_zip(PyObject *self, PyObject *args)
2117{
2118 PyObject *ret;
Martin v. Löwisd96ee902006-02-16 14:37:16 +00002119 const Py_ssize_t itemsize = PySequence_Length(args);
2120 Py_ssize_t i;
Tim Peters8572b4f2001-05-06 01:05:02 +00002121 PyObject *itlist; /* tuple of iterators */
Martin v. Löwisd96ee902006-02-16 14:37:16 +00002122 Py_ssize_t len; /* guess at result length */
Barry Warsawbd599b52000-08-03 15:45:29 +00002123
Raymond Hettingereaef6152003-08-02 07:42:57 +00002124 if (itemsize == 0)
2125 return PyList_New(0);
2126
Barry Warsawbd599b52000-08-03 15:45:29 +00002127 /* args must be a tuple */
2128 assert(PyTuple_Check(args));
2129
Tim Peters39a86c22002-05-12 07:19:38 +00002130 /* Guess at result length: the shortest of the input lengths.
2131 If some argument refuses to say, we refuse to guess too, lest
2132 an argument like xrange(sys.maxint) lead us astray.*/
Tim Peters67d687a2002-04-29 21:27:32 +00002133 len = -1; /* unknown */
2134 for (i = 0; i < itemsize; ++i) {
2135 PyObject *item = PyTuple_GET_ITEM(args, i);
Martin v. Löwisd96ee902006-02-16 14:37:16 +00002136 Py_ssize_t thislen = _PyObject_LengthHint(item);
Tim Peters39a86c22002-05-12 07:19:38 +00002137 if (thislen < 0) {
Raymond Hettingera710b332005-08-21 11:03:59 +00002138 if (!PyErr_ExceptionMatches(PyExc_TypeError) &&
2139 !PyErr_ExceptionMatches(PyExc_AttributeError)) {
2140 return NULL;
2141 }
Tim Peters67d687a2002-04-29 21:27:32 +00002142 PyErr_Clear();
Tim Peters39a86c22002-05-12 07:19:38 +00002143 len = -1;
2144 break;
2145 }
Tim Peters67d687a2002-04-29 21:27:32 +00002146 else if (len < 0 || thislen < len)
2147 len = thislen;
2148 }
2149
Tim Peters8572b4f2001-05-06 01:05:02 +00002150 /* allocate result list */
Tim Peters67d687a2002-04-29 21:27:32 +00002151 if (len < 0)
2152 len = 10; /* arbitrary */
2153 if ((ret = PyList_New(len)) == NULL)
Barry Warsawbd599b52000-08-03 15:45:29 +00002154 return NULL;
2155
Tim Peters8572b4f2001-05-06 01:05:02 +00002156 /* obtain iterators */
2157 itlist = PyTuple_New(itemsize);
2158 if (itlist == NULL)
2159 goto Fail_ret;
2160 for (i = 0; i < itemsize; ++i) {
2161 PyObject *item = PyTuple_GET_ITEM(args, i);
2162 PyObject *it = PyObject_GetIter(item);
2163 if (it == NULL) {
2164 if (PyErr_ExceptionMatches(PyExc_TypeError))
2165 PyErr_Format(PyExc_TypeError,
Neal Norwitza361bd82006-02-19 19:31:50 +00002166 "zip argument #%zd must support iteration",
Tim Peters8572b4f2001-05-06 01:05:02 +00002167 i+1);
2168 goto Fail_ret_itlist;
Barry Warsawbd599b52000-08-03 15:45:29 +00002169 }
Tim Peters8572b4f2001-05-06 01:05:02 +00002170 PyTuple_SET_ITEM(itlist, i, it);
2171 }
Barry Warsawbd599b52000-08-03 15:45:29 +00002172
Tim Peters8572b4f2001-05-06 01:05:02 +00002173 /* build result into ret list */
Tim Peters67d687a2002-04-29 21:27:32 +00002174 for (i = 0; ; ++i) {
2175 int j;
Tim Peters8572b4f2001-05-06 01:05:02 +00002176 PyObject *next = PyTuple_New(itemsize);
2177 if (!next)
2178 goto Fail_ret_itlist;
2179
Tim Peters67d687a2002-04-29 21:27:32 +00002180 for (j = 0; j < itemsize; j++) {
2181 PyObject *it = PyTuple_GET_ITEM(itlist, j);
Tim Peters8572b4f2001-05-06 01:05:02 +00002182 PyObject *item = PyIter_Next(it);
Barry Warsawbd599b52000-08-03 15:45:29 +00002183 if (!item) {
Tim Peters8572b4f2001-05-06 01:05:02 +00002184 if (PyErr_Occurred()) {
2185 Py_DECREF(ret);
2186 ret = NULL;
Barry Warsawbd599b52000-08-03 15:45:29 +00002187 }
2188 Py_DECREF(next);
Tim Peters8572b4f2001-05-06 01:05:02 +00002189 Py_DECREF(itlist);
Tim Peters67d687a2002-04-29 21:27:32 +00002190 goto Done;
Barry Warsawbd599b52000-08-03 15:45:29 +00002191 }
Tim Peters67d687a2002-04-29 21:27:32 +00002192 PyTuple_SET_ITEM(next, j, item);
Barry Warsawbd599b52000-08-03 15:45:29 +00002193 }
Tim Peters8572b4f2001-05-06 01:05:02 +00002194
Tim Peters67d687a2002-04-29 21:27:32 +00002195 if (i < len)
2196 PyList_SET_ITEM(ret, i, next);
2197 else {
2198 int status = PyList_Append(ret, next);
2199 Py_DECREF(next);
2200 ++len;
2201 if (status < 0)
2202 goto Fail_ret_itlist;
2203 }
Barry Warsawbd599b52000-08-03 15:45:29 +00002204 }
Tim Peters8572b4f2001-05-06 01:05:02 +00002205
Tim Peters67d687a2002-04-29 21:27:32 +00002206Done:
2207 if (ret != NULL && i < len) {
2208 /* The list is too big. */
2209 if (PyList_SetSlice(ret, i, len, NULL) < 0)
2210 return NULL;
2211 }
2212 return ret;
2213
Tim Peters8572b4f2001-05-06 01:05:02 +00002214Fail_ret_itlist:
2215 Py_DECREF(itlist);
2216Fail_ret:
2217 Py_DECREF(ret);
2218 return NULL;
Barry Warsawbd599b52000-08-03 15:45:29 +00002219}
2220
2221
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002222PyDoc_STRVAR(zip_doc,
Barry Warsawbd599b52000-08-03 15:45:29 +00002223"zip(seq1 [, seq2 [...]]) -> [(seq1[0], seq2[0] ...), (...)]\n\
2224\n\
2225Return a list of tuples, where each tuple contains the i-th element\n\
2226from each of the argument sequences. The returned list is truncated\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002227in length to the length of the shortest argument sequence.");
Barry Warsawbd599b52000-08-03 15:45:29 +00002228
2229
Guido van Rossum79f25d91997-04-29 20:08:16 +00002230static PyMethodDef builtin_methods[] = {
Neal Norwitz92e212f2006-04-03 04:48:37 +00002231 {"__import__", (PyCFunction)builtin___import__, METH_VARARGS | METH_KEYWORDS, import_doc},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002232 {"abs", builtin_abs, METH_O, abs_doc},
Raymond Hettinger96229b12005-03-11 06:49:40 +00002233 {"all", builtin_all, METH_O, all_doc},
2234 {"any", builtin_any, METH_O, any_doc},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002235 {"apply", builtin_apply, METH_VARARGS, apply_doc},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002236 {"callable", builtin_callable, METH_O, callable_doc},
2237 {"chr", builtin_chr, METH_VARARGS, chr_doc},
2238 {"cmp", builtin_cmp, METH_VARARGS, cmp_doc},
2239 {"coerce", builtin_coerce, METH_VARARGS, coerce_doc},
2240 {"compile", builtin_compile, METH_VARARGS, compile_doc},
2241 {"delattr", builtin_delattr, METH_VARARGS, delattr_doc},
2242 {"dir", builtin_dir, METH_VARARGS, dir_doc},
2243 {"divmod", builtin_divmod, METH_VARARGS, divmod_doc},
2244 {"eval", builtin_eval, METH_VARARGS, eval_doc},
2245 {"execfile", builtin_execfile, METH_VARARGS, execfile_doc},
2246 {"filter", builtin_filter, METH_VARARGS, filter_doc},
2247 {"getattr", builtin_getattr, METH_VARARGS, getattr_doc},
2248 {"globals", (PyCFunction)builtin_globals, METH_NOARGS, globals_doc},
2249 {"hasattr", builtin_hasattr, METH_VARARGS, hasattr_doc},
2250 {"hash", builtin_hash, METH_O, hash_doc},
2251 {"hex", builtin_hex, METH_O, hex_doc},
2252 {"id", builtin_id, METH_O, id_doc},
2253 {"input", builtin_input, METH_VARARGS, input_doc},
2254 {"intern", builtin_intern, METH_VARARGS, intern_doc},
2255 {"isinstance", builtin_isinstance, METH_VARARGS, isinstance_doc},
2256 {"issubclass", builtin_issubclass, METH_VARARGS, issubclass_doc},
2257 {"iter", builtin_iter, METH_VARARGS, iter_doc},
2258 {"len", builtin_len, METH_O, len_doc},
2259 {"locals", (PyCFunction)builtin_locals, METH_NOARGS, locals_doc},
2260 {"map", builtin_map, METH_VARARGS, map_doc},
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00002261 {"max", (PyCFunction)builtin_max, METH_VARARGS | METH_KEYWORDS, max_doc},
2262 {"min", (PyCFunction)builtin_min, METH_VARARGS | METH_KEYWORDS, min_doc},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002263 {"oct", builtin_oct, METH_O, oct_doc},
Neal Norwitzc4edb0e2006-05-02 04:43:14 +00002264 {"open", (PyCFunction)builtin_open, METH_VARARGS | METH_KEYWORDS, open_doc},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002265 {"ord", builtin_ord, METH_O, ord_doc},
2266 {"pow", builtin_pow, METH_VARARGS, pow_doc},
2267 {"range", builtin_range, METH_VARARGS, range_doc},
2268 {"raw_input", builtin_raw_input, METH_VARARGS, raw_input_doc},
2269 {"reduce", builtin_reduce, METH_VARARGS, reduce_doc},
2270 {"reload", builtin_reload, METH_O, reload_doc},
2271 {"repr", builtin_repr, METH_O, repr_doc},
Georg Brandlccadf842006-03-31 18:54:53 +00002272 {"round", (PyCFunction)builtin_round, METH_VARARGS | METH_KEYWORDS, round_doc},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002273 {"setattr", builtin_setattr, METH_VARARGS, setattr_doc},
Raymond Hettinger64958a12003-12-17 20:43:33 +00002274 {"sorted", (PyCFunction)builtin_sorted, METH_VARARGS | METH_KEYWORDS, sorted_doc},
Alex Martellia70b1912003-04-22 08:12:33 +00002275 {"sum", builtin_sum, METH_VARARGS, sum_doc},
Martin v. Löwis339d0f72001-08-17 18:39:25 +00002276#ifdef Py_USING_UNICODE
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002277 {"unichr", builtin_unichr, METH_VARARGS, unichr_doc},
Martin v. Löwis339d0f72001-08-17 18:39:25 +00002278#endif
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002279 {"vars", builtin_vars, METH_VARARGS, vars_doc},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002280 {"zip", builtin_zip, METH_VARARGS, zip_doc},
Guido van Rossumc02e15c1991-12-16 13:03:00 +00002281 {NULL, NULL},
Guido van Rossum3f5da241990-12-20 15:06:42 +00002282};
2283
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002284PyDoc_STRVAR(builtin_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002285"Built-in functions, exceptions, and other objects.\n\
2286\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002287Noteworthy: None is the `nil' object; Ellipsis represents `...' in slices.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002288
Guido van Rossum25ce5661997-08-02 03:10:38 +00002289PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002290_PyBuiltin_Init(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +00002291{
Fred Drake5550de32000-06-20 04:54:19 +00002292 PyObject *mod, *dict, *debug;
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002293 mod = Py_InitModule4("__builtin__", builtin_methods,
2294 builtin_doc, (PyObject *)NULL,
2295 PYTHON_API_VERSION);
Guido van Rossum25ce5661997-08-02 03:10:38 +00002296 if (mod == NULL)
2297 return NULL;
2298 dict = PyModule_GetDict(mod);
Tim Peters4b7625e2001-09-13 21:37:17 +00002299
Tim Peters7571a0f2003-03-23 17:52:28 +00002300#ifdef Py_TRACE_REFS
2301 /* __builtin__ exposes a number of statically allocated objects
2302 * that, before this code was added in 2.3, never showed up in
2303 * the list of "all objects" maintained by Py_TRACE_REFS. As a
2304 * result, programs leaking references to None and False (etc)
2305 * couldn't be diagnosed by examining sys.getobjects(0).
2306 */
2307#define ADD_TO_ALL(OBJECT) _Py_AddToAllObjects((PyObject *)(OBJECT), 0)
2308#else
2309#define ADD_TO_ALL(OBJECT) (void)0
2310#endif
2311
Tim Peters4b7625e2001-09-13 21:37:17 +00002312#define SETBUILTIN(NAME, OBJECT) \
Tim Peters7571a0f2003-03-23 17:52:28 +00002313 if (PyDict_SetItemString(dict, NAME, (PyObject *)OBJECT) < 0) \
2314 return NULL; \
2315 ADD_TO_ALL(OBJECT)
Tim Peters4b7625e2001-09-13 21:37:17 +00002316
2317 SETBUILTIN("None", Py_None);
2318 SETBUILTIN("Ellipsis", Py_Ellipsis);
2319 SETBUILTIN("NotImplemented", Py_NotImplemented);
Guido van Rossum77f6a652002-04-03 22:41:51 +00002320 SETBUILTIN("False", Py_False);
2321 SETBUILTIN("True", Py_True);
Neal Norwitz32a7e7f2002-05-31 19:58:02 +00002322 SETBUILTIN("basestring", &PyBaseString_Type);
Guido van Rossum77f6a652002-04-03 22:41:51 +00002323 SETBUILTIN("bool", &PyBool_Type);
Guido van Rossumbea18cc2002-06-14 20:41:17 +00002324 SETBUILTIN("buffer", &PyBuffer_Type);
Tim Peters4b7625e2001-09-13 21:37:17 +00002325 SETBUILTIN("classmethod", &PyClassMethod_Type);
Tim Peters6d6c1a32001-08-02 04:15:00 +00002326#ifndef WITHOUT_COMPLEX
Tim Peters4b7625e2001-09-13 21:37:17 +00002327 SETBUILTIN("complex", &PyComplex_Type);
Tim Peters6d6c1a32001-08-02 04:15:00 +00002328#endif
Tim Petersa427a2b2001-10-29 22:25:45 +00002329 SETBUILTIN("dict", &PyDict_Type);
Tim Peters67d687a2002-04-29 21:27:32 +00002330 SETBUILTIN("enumerate", &PyEnum_Type);
Neal Norwitzc4edb0e2006-05-02 04:43:14 +00002331 SETBUILTIN("file", &PyFile_Type);
Tim Peters4b7625e2001-09-13 21:37:17 +00002332 SETBUILTIN("float", &PyFloat_Type);
Raymond Hettingera690a992003-11-16 16:17:49 +00002333 SETBUILTIN("frozenset", &PyFrozenSet_Type);
Tim Peters4b7625e2001-09-13 21:37:17 +00002334 SETBUILTIN("property", &PyProperty_Type);
2335 SETBUILTIN("int", &PyInt_Type);
2336 SETBUILTIN("list", &PyList_Type);
2337 SETBUILTIN("long", &PyLong_Type);
2338 SETBUILTIN("object", &PyBaseObject_Type);
Raymond Hettinger85c20a42003-11-06 14:06:48 +00002339 SETBUILTIN("reversed", &PyReversed_Type);
Raymond Hettingera690a992003-11-16 16:17:49 +00002340 SETBUILTIN("set", &PySet_Type);
Guido van Rossumbea18cc2002-06-14 20:41:17 +00002341 SETBUILTIN("slice", &PySlice_Type);
Tim Peters4b7625e2001-09-13 21:37:17 +00002342 SETBUILTIN("staticmethod", &PyStaticMethod_Type);
2343 SETBUILTIN("str", &PyString_Type);
2344 SETBUILTIN("super", &PySuper_Type);
2345 SETBUILTIN("tuple", &PyTuple_Type);
2346 SETBUILTIN("type", &PyType_Type);
Raymond Hettingerc4c453f2002-06-05 23:12:45 +00002347 SETBUILTIN("xrange", &PyRange_Type);
Martin v. Löwis339d0f72001-08-17 18:39:25 +00002348#ifdef Py_USING_UNICODE
Tim Peters4b7625e2001-09-13 21:37:17 +00002349 SETBUILTIN("unicode", &PyUnicode_Type);
Martin v. Löwis339d0f72001-08-17 18:39:25 +00002350#endif
Guido van Rossum77f6a652002-04-03 22:41:51 +00002351 debug = PyBool_FromLong(Py_OptimizeFlag == 0);
Fred Drake5550de32000-06-20 04:54:19 +00002352 if (PyDict_SetItemString(dict, "__debug__", debug) < 0) {
2353 Py_XDECREF(debug);
Guido van Rossum25ce5661997-08-02 03:10:38 +00002354 return NULL;
Fred Drake5550de32000-06-20 04:54:19 +00002355 }
2356 Py_XDECREF(debug);
Barry Warsaw757af0e1997-08-29 22:13:51 +00002357
Guido van Rossum25ce5661997-08-02 03:10:38 +00002358 return mod;
Tim Peters7571a0f2003-03-23 17:52:28 +00002359#undef ADD_TO_ALL
Tim Peters4b7625e2001-09-13 21:37:17 +00002360#undef SETBUILTIN
Guido van Rossum3f5da241990-12-20 15:06:42 +00002361}
2362
Guido van Rossume77a7571993-11-03 15:01:26 +00002363/* Helper for filter(): filter a tuple through a function */
Guido van Rossum12d12c51993-10-26 17:58:25 +00002364
Guido van Rossum79f25d91997-04-29 20:08:16 +00002365static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002366filtertuple(PyObject *func, PyObject *tuple)
Guido van Rossum12d12c51993-10-26 17:58:25 +00002367{
Guido van Rossum79f25d91997-04-29 20:08:16 +00002368 PyObject *result;
Martin v. Löwis18e16552006-02-15 17:27:45 +00002369 Py_ssize_t i, j;
2370 Py_ssize_t len = PyTuple_Size(tuple);
Guido van Rossum12d12c51993-10-26 17:58:25 +00002371
Guido van Rossumb7b45621995-08-04 04:07:45 +00002372 if (len == 0) {
Walter Dörwaldc3da83f2003-02-04 20:24:45 +00002373 if (PyTuple_CheckExact(tuple))
2374 Py_INCREF(tuple);
2375 else
2376 tuple = PyTuple_New(0);
Guido van Rossumb7b45621995-08-04 04:07:45 +00002377 return tuple;
2378 }
2379
Guido van Rossum79f25d91997-04-29 20:08:16 +00002380 if ((result = PyTuple_New(len)) == NULL)
Guido van Rossum2586bf01993-11-01 16:21:44 +00002381 return NULL;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002382
Guido van Rossum12d12c51993-10-26 17:58:25 +00002383 for (i = j = 0; i < len; ++i) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002384 PyObject *item, *good;
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002385 int ok;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002386
Walter Dörwald8dd19322003-02-10 17:36:40 +00002387 if (tuple->ob_type->tp_as_sequence &&
2388 tuple->ob_type->tp_as_sequence->sq_item) {
2389 item = tuple->ob_type->tp_as_sequence->sq_item(tuple, i);
Walter Dörwaldc58a3a12003-08-18 18:28:45 +00002390 if (item == NULL)
2391 goto Fail_1;
Walter Dörwald8dd19322003-02-10 17:36:40 +00002392 } else {
Alex Martellia9b9c9f2003-04-23 13:34:35 +00002393 PyErr_SetString(PyExc_TypeError, "filter(): unsubscriptable tuple");
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002394 goto Fail_1;
Walter Dörwald8dd19322003-02-10 17:36:40 +00002395 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002396 if (func == Py_None) {
2397 Py_INCREF(item);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002398 good = item;
2399 }
2400 else {
Raymond Hettinger8ae46892003-10-12 19:09:37 +00002401 PyObject *arg = PyTuple_Pack(1, item);
Walter Dörwaldc58a3a12003-08-18 18:28:45 +00002402 if (arg == NULL) {
2403 Py_DECREF(item);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002404 goto Fail_1;
Walter Dörwaldc58a3a12003-08-18 18:28:45 +00002405 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002406 good = PyEval_CallObject(func, arg);
2407 Py_DECREF(arg);
Walter Dörwaldc58a3a12003-08-18 18:28:45 +00002408 if (good == NULL) {
2409 Py_DECREF(item);
Guido van Rossum12d12c51993-10-26 17:58:25 +00002410 goto Fail_1;
Walter Dörwaldc58a3a12003-08-18 18:28:45 +00002411 }
Guido van Rossum12d12c51993-10-26 17:58:25 +00002412 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002413 ok = PyObject_IsTrue(good);
2414 Py_DECREF(good);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002415 if (ok) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002416 if (PyTuple_SetItem(result, j++, item) < 0)
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002417 goto Fail_1;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002418 }
Walter Dörwaldc58a3a12003-08-18 18:28:45 +00002419 else
2420 Py_DECREF(item);
Guido van Rossum12d12c51993-10-26 17:58:25 +00002421 }
2422
Tim Peters4324aa32001-05-28 22:30:08 +00002423 if (_PyTuple_Resize(&result, j) < 0)
Guido van Rossum12d12c51993-10-26 17:58:25 +00002424 return NULL;
2425
Guido van Rossum12d12c51993-10-26 17:58:25 +00002426 return result;
2427
Guido van Rossum12d12c51993-10-26 17:58:25 +00002428Fail_1:
Guido van Rossum79f25d91997-04-29 20:08:16 +00002429 Py_DECREF(result);
Guido van Rossum12d12c51993-10-26 17:58:25 +00002430 return NULL;
2431}
2432
2433
Guido van Rossume77a7571993-11-03 15:01:26 +00002434/* Helper for filter(): filter a string through a function */
Guido van Rossum12d12c51993-10-26 17:58:25 +00002435
Guido van Rossum79f25d91997-04-29 20:08:16 +00002436static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002437filterstring(PyObject *func, PyObject *strobj)
Guido van Rossum12d12c51993-10-26 17:58:25 +00002438{
Guido van Rossum79f25d91997-04-29 20:08:16 +00002439 PyObject *result;
Martin v. Löwis18e16552006-02-15 17:27:45 +00002440 Py_ssize_t i, j;
2441 Py_ssize_t len = PyString_Size(strobj);
2442 Py_ssize_t outlen = len;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002443
Guido van Rossum79f25d91997-04-29 20:08:16 +00002444 if (func == Py_None) {
Walter Dörwald1918f772003-02-10 13:19:13 +00002445 /* If it's a real string we can return the original,
2446 * as no character is ever false and __getitem__
2447 * does return this character. If it's a subclass
2448 * we must go through the __getitem__ loop */
2449 if (PyString_CheckExact(strobj)) {
Walter Dörwaldc3da83f2003-02-04 20:24:45 +00002450 Py_INCREF(strobj);
Walter Dörwald1918f772003-02-10 13:19:13 +00002451 return strobj;
2452 }
Guido van Rossum12d12c51993-10-26 17:58:25 +00002453 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002454 if ((result = PyString_FromStringAndSize(NULL, len)) == NULL)
Guido van Rossum2586bf01993-11-01 16:21:44 +00002455 return NULL;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002456
Guido van Rossum12d12c51993-10-26 17:58:25 +00002457 for (i = j = 0; i < len; ++i) {
Walter Dörwald1918f772003-02-10 13:19:13 +00002458 PyObject *item;
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002459 int ok;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002460
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002461 item = (*strobj->ob_type->tp_as_sequence->sq_item)(strobj, i);
2462 if (item == NULL)
2463 goto Fail_1;
Walter Dörwald1918f772003-02-10 13:19:13 +00002464 if (func==Py_None) {
2465 ok = 1;
2466 } else {
2467 PyObject *arg, *good;
Raymond Hettinger8ae46892003-10-12 19:09:37 +00002468 arg = PyTuple_Pack(1, item);
Walter Dörwald1918f772003-02-10 13:19:13 +00002469 if (arg == NULL) {
2470 Py_DECREF(item);
2471 goto Fail_1;
2472 }
2473 good = PyEval_CallObject(func, arg);
2474 Py_DECREF(arg);
2475 if (good == NULL) {
2476 Py_DECREF(item);
2477 goto Fail_1;
2478 }
2479 ok = PyObject_IsTrue(good);
2480 Py_DECREF(good);
Tim Peters388ed082001-04-07 20:34:48 +00002481 }
Walter Dörwald903f1e02003-02-04 16:28:00 +00002482 if (ok) {
Martin v. Löwisd96ee902006-02-16 14:37:16 +00002483 Py_ssize_t reslen;
Walter Dörwald903f1e02003-02-04 16:28:00 +00002484 if (!PyString_Check(item)) {
2485 PyErr_SetString(PyExc_TypeError, "can't filter str to str:"
2486 " __getitem__ returned different type");
2487 Py_DECREF(item);
2488 goto Fail_1;
2489 }
2490 reslen = PyString_GET_SIZE(item);
2491 if (reslen == 1) {
2492 PyString_AS_STRING(result)[j++] =
2493 PyString_AS_STRING(item)[0];
2494 } else {
2495 /* do we need more space? */
Martin v. Löwisd96ee902006-02-16 14:37:16 +00002496 Py_ssize_t need = j + reslen + len-i-1;
Walter Dörwald903f1e02003-02-04 16:28:00 +00002497 if (need > outlen) {
2498 /* overallocate, to avoid reallocations */
2499 if (need<2*outlen)
2500 need = 2*outlen;
2501 if (_PyString_Resize(&result, need)) {
2502 Py_DECREF(item);
2503 return NULL;
2504 }
2505 outlen = need;
2506 }
2507 memcpy(
2508 PyString_AS_STRING(result) + j,
2509 PyString_AS_STRING(item),
2510 reslen
2511 );
2512 j += reslen;
2513 }
2514 }
Tim Peters388ed082001-04-07 20:34:48 +00002515 Py_DECREF(item);
Guido van Rossum12d12c51993-10-26 17:58:25 +00002516 }
2517
Walter Dörwald903f1e02003-02-04 16:28:00 +00002518 if (j < outlen)
Tim Peters5de98422002-04-27 18:44:32 +00002519 _PyString_Resize(&result, j);
Guido van Rossum12d12c51993-10-26 17:58:25 +00002520
Guido van Rossum12d12c51993-10-26 17:58:25 +00002521 return result;
2522
Guido van Rossum12d12c51993-10-26 17:58:25 +00002523Fail_1:
Guido van Rossum79f25d91997-04-29 20:08:16 +00002524 Py_DECREF(result);
Guido van Rossum12d12c51993-10-26 17:58:25 +00002525 return NULL;
2526}
Martin v. Löwis8afd7572003-01-25 22:46:11 +00002527
2528#ifdef Py_USING_UNICODE
2529/* Helper for filter(): filter a Unicode object through a function */
2530
2531static PyObject *
2532filterunicode(PyObject *func, PyObject *strobj)
2533{
2534 PyObject *result;
Martin v. Löwis725507b2006-03-07 12:08:51 +00002535 register Py_ssize_t i, j;
Martin v. Löwisd96ee902006-02-16 14:37:16 +00002536 Py_ssize_t len = PyUnicode_GetSize(strobj);
2537 Py_ssize_t outlen = len;
Martin v. Löwis8afd7572003-01-25 22:46:11 +00002538
2539 if (func == Py_None) {
Walter Dörwald1918f772003-02-10 13:19:13 +00002540 /* If it's a real string we can return the original,
2541 * as no character is ever false and __getitem__
2542 * does return this character. If it's a subclass
2543 * we must go through the __getitem__ loop */
2544 if (PyUnicode_CheckExact(strobj)) {
Walter Dörwaldc3da83f2003-02-04 20:24:45 +00002545 Py_INCREF(strobj);
Walter Dörwald1918f772003-02-10 13:19:13 +00002546 return strobj;
2547 }
Martin v. Löwis8afd7572003-01-25 22:46:11 +00002548 }
2549 if ((result = PyUnicode_FromUnicode(NULL, len)) == NULL)
2550 return NULL;
2551
2552 for (i = j = 0; i < len; ++i) {
2553 PyObject *item, *arg, *good;
2554 int ok;
2555
2556 item = (*strobj->ob_type->tp_as_sequence->sq_item)(strobj, i);
2557 if (item == NULL)
2558 goto Fail_1;
Walter Dörwald1918f772003-02-10 13:19:13 +00002559 if (func == Py_None) {
2560 ok = 1;
2561 } else {
Raymond Hettinger8ae46892003-10-12 19:09:37 +00002562 arg = PyTuple_Pack(1, item);
Walter Dörwald1918f772003-02-10 13:19:13 +00002563 if (arg == NULL) {
2564 Py_DECREF(item);
2565 goto Fail_1;
2566 }
2567 good = PyEval_CallObject(func, arg);
2568 Py_DECREF(arg);
2569 if (good == NULL) {
2570 Py_DECREF(item);
2571 goto Fail_1;
2572 }
2573 ok = PyObject_IsTrue(good);
2574 Py_DECREF(good);
Martin v. Löwis8afd7572003-01-25 22:46:11 +00002575 }
Walter Dörwald903f1e02003-02-04 16:28:00 +00002576 if (ok) {
Martin v. Löwisd96ee902006-02-16 14:37:16 +00002577 Py_ssize_t reslen;
Walter Dörwald903f1e02003-02-04 16:28:00 +00002578 if (!PyUnicode_Check(item)) {
Georg Brandl99d7e4e2005-08-31 22:21:15 +00002579 PyErr_SetString(PyExc_TypeError,
Jeremy Hylton1aad9c72003-09-16 03:10:59 +00002580 "can't filter unicode to unicode:"
2581 " __getitem__ returned different type");
Walter Dörwald903f1e02003-02-04 16:28:00 +00002582 Py_DECREF(item);
2583 goto Fail_1;
2584 }
2585 reslen = PyUnicode_GET_SIZE(item);
Georg Brandl99d7e4e2005-08-31 22:21:15 +00002586 if (reslen == 1)
Walter Dörwald903f1e02003-02-04 16:28:00 +00002587 PyUnicode_AS_UNICODE(result)[j++] =
2588 PyUnicode_AS_UNICODE(item)[0];
Jeremy Hylton1aad9c72003-09-16 03:10:59 +00002589 else {
Walter Dörwald903f1e02003-02-04 16:28:00 +00002590 /* do we need more space? */
Martin v. Löwisd96ee902006-02-16 14:37:16 +00002591 Py_ssize_t need = j + reslen + len - i - 1;
Walter Dörwald903f1e02003-02-04 16:28:00 +00002592 if (need > outlen) {
Georg Brandl99d7e4e2005-08-31 22:21:15 +00002593 /* overallocate,
Jeremy Hylton1aad9c72003-09-16 03:10:59 +00002594 to avoid reallocations */
2595 if (need < 2 * outlen)
2596 need = 2 * outlen;
Jeremy Hylton364f6be2003-09-16 03:17:16 +00002597 if (PyUnicode_Resize(
2598 &result, need) < 0) {
Walter Dörwald903f1e02003-02-04 16:28:00 +00002599 Py_DECREF(item);
Walter Dörwald531e0002003-02-04 16:57:49 +00002600 goto Fail_1;
Walter Dörwald903f1e02003-02-04 16:28:00 +00002601 }
2602 outlen = need;
2603 }
Jeremy Hylton1aad9c72003-09-16 03:10:59 +00002604 memcpy(PyUnicode_AS_UNICODE(result) + j,
2605 PyUnicode_AS_UNICODE(item),
2606 reslen*sizeof(Py_UNICODE));
Walter Dörwald903f1e02003-02-04 16:28:00 +00002607 j += reslen;
2608 }
2609 }
Martin v. Löwis8afd7572003-01-25 22:46:11 +00002610 Py_DECREF(item);
2611 }
2612
Walter Dörwald903f1e02003-02-04 16:28:00 +00002613 if (j < outlen)
Martin v. Löwis8afd7572003-01-25 22:46:11 +00002614 PyUnicode_Resize(&result, j);
2615
2616 return result;
2617
2618Fail_1:
2619 Py_DECREF(result);
2620 return NULL;
2621}
2622#endif