blob: cf47c30889bf995c87779a8af8d01de5eca19933 [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 Hammond26cffde2001-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 Hammond26cffde2001-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 Hammond26cffde2001-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
Neal Norwitz8b2bfbc2007-05-23 06:35:32 +0000147 if (Py_Py3kWarningFlag &&
148 PyErr_Warn(PyExc_DeprecationWarning,
149 "apply() not supported in 3.x") < 0)
150 return NULL;
151
Raymond Hettingerea3fdf42002-12-29 16:33:45 +0000152 if (!PyArg_UnpackTuple(args, "apply", 1, 3, &func, &alist, &kwdict))
Guido van Rossumc02e15c1991-12-16 13:03:00 +0000153 return NULL;
Barry Warsaw968f8cb1998-10-01 15:33:12 +0000154 if (alist != NULL) {
155 if (!PyTuple_Check(alist)) {
156 if (!PySequence_Check(alist)) {
Jeremy Hyltonc862cf42001-01-19 03:25:05 +0000157 PyErr_Format(PyExc_TypeError,
Alex Martellia9b9c9f2003-04-23 13:34:35 +0000158 "apply() arg 2 expected sequence, found %s",
Jeremy Hyltonc862cf42001-01-19 03:25:05 +0000159 alist->ob_type->tp_name);
Barry Warsaw968f8cb1998-10-01 15:33:12 +0000160 return NULL;
161 }
162 t = PySequence_Tuple(alist);
163 if (t == NULL)
164 return NULL;
165 alist = t;
166 }
Guido van Rossum2d951851994-08-29 12:52:16 +0000167 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000168 if (kwdict != NULL && !PyDict_Check(kwdict)) {
Jeremy Hyltonc862cf42001-01-19 03:25:05 +0000169 PyErr_Format(PyExc_TypeError,
170 "apply() arg 3 expected dictionary, found %s",
171 kwdict->ob_type->tp_name);
Barry Warsaw968f8cb1998-10-01 15:33:12 +0000172 goto finally;
Guido van Rossum681d79a1995-07-18 14:51:37 +0000173 }
Barry Warsaw968f8cb1998-10-01 15:33:12 +0000174 retval = PyEval_CallObjectWithKeywords(func, alist, kwdict);
175 finally:
176 Py_XDECREF(t);
177 return retval;
Guido van Rossumc02e15c1991-12-16 13:03:00 +0000178}
179
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000180PyDoc_STRVAR(apply_doc,
Fred Drakef1fbc622001-01-12 17:05:05 +0000181"apply(object[, args[, kwargs]]) -> value\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000182\n\
Fred Drake7b912121999-12-23 14:16:55 +0000183Call a callable object with positional arguments taken from the tuple args,\n\
184and keyword arguments taken from the optional dictionary kwargs.\n\
Raymond Hettingerff41c482003-04-06 09:01:11 +0000185Note that classes are callable, as are instances with a __call__() method.\n\
186\n\
187Deprecated since release 2.3. Instead, use the extended call syntax:\n\
188 function(*args, **keywords).");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000189
190
Guido van Rossum79f25d91997-04-29 20:08:16 +0000191static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000192builtin_callable(PyObject *self, PyObject *v)
Guido van Rossum2d951851994-08-29 12:52:16 +0000193{
Guido van Rossum77f6a652002-04-03 22:41:51 +0000194 return PyBool_FromLong((long)PyCallable_Check(v));
Guido van Rossum2d951851994-08-29 12:52:16 +0000195}
196
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000197PyDoc_STRVAR(callable_doc,
Guido van Rossum77f6a652002-04-03 22:41:51 +0000198"callable(object) -> bool\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000199\n\
200Return whether the object is callable (i.e., some kind of function).\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000201Note that classes are callable, as are instances with a __call__() method.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000202
203
Guido van Rossum79f25d91997-04-29 20:08:16 +0000204static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000205builtin_filter(PyObject *self, PyObject *args)
Guido van Rossum12d12c51993-10-26 17:58:25 +0000206{
Guido van Rossumc7903a12002-08-16 07:04:56 +0000207 PyObject *func, *seq, *result, *it, *arg;
Martin v. Löwisd96ee902006-02-16 14:37:16 +0000208 Py_ssize_t len; /* guess for result list size */
209 register Py_ssize_t j;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000210
Raymond Hettingerea3fdf42002-12-29 16:33:45 +0000211 if (!PyArg_UnpackTuple(args, "filter", 2, 2, &func, &seq))
Guido van Rossum12d12c51993-10-26 17:58:25 +0000212 return NULL;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000213
Tim Peters0e57abf2001-05-02 07:39:38 +0000214 /* Strings and tuples return a result of the same type. */
215 if (PyString_Check(seq))
216 return filterstring(func, seq);
Martin v. Löwis8afd7572003-01-25 22:46:11 +0000217#ifdef Py_USING_UNICODE
218 if (PyUnicode_Check(seq))
219 return filterunicode(func, seq);
220#endif
Tim Peters0e57abf2001-05-02 07:39:38 +0000221 if (PyTuple_Check(seq))
222 return filtertuple(func, seq);
223
Georg Brandle35b6572005-07-19 22:20:20 +0000224 /* Pre-allocate argument list tuple. */
225 arg = PyTuple_New(1);
226 if (arg == NULL)
227 return NULL;
228
Tim Peters0e57abf2001-05-02 07:39:38 +0000229 /* Get iterator. */
230 it = PyObject_GetIter(seq);
231 if (it == NULL)
Georg Brandle35b6572005-07-19 22:20:20 +0000232 goto Fail_arg;
Tim Peters0e57abf2001-05-02 07:39:38 +0000233
234 /* Guess a result list size. */
Armin Rigof5b3e362006-02-11 21:32:43 +0000235 len = _PyObject_LengthHint(seq);
Raymond Hettingerb86269d2004-01-04 11:00:08 +0000236 if (len < 0) {
Raymond Hettingera710b332005-08-21 11:03:59 +0000237 if (!PyErr_ExceptionMatches(PyExc_TypeError) &&
238 !PyErr_ExceptionMatches(PyExc_AttributeError)) {
239 goto Fail_it;
240 }
Raymond Hettingerb86269d2004-01-04 11:00:08 +0000241 PyErr_Clear();
242 len = 8; /* arbitrary */
Guido van Rossum12d12c51993-10-26 17:58:25 +0000243 }
244
Tim Peters0e57abf2001-05-02 07:39:38 +0000245 /* Get a result list. */
Guido van Rossum79f25d91997-04-29 20:08:16 +0000246 if (PyList_Check(seq) && seq->ob_refcnt == 1) {
Tim Peters0e57abf2001-05-02 07:39:38 +0000247 /* Eww - can modify the list in-place. */
Guido van Rossum79f25d91997-04-29 20:08:16 +0000248 Py_INCREF(seq);
Guido van Rossum12d12c51993-10-26 17:58:25 +0000249 result = seq;
250 }
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000251 else {
Tim Peters0e57abf2001-05-02 07:39:38 +0000252 result = PyList_New(len);
253 if (result == NULL)
254 goto Fail_it;
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000255 }
Guido van Rossum12d12c51993-10-26 17:58:25 +0000256
Tim Peters0e57abf2001-05-02 07:39:38 +0000257 /* Build the result list. */
Tim Petersf4848da2001-05-05 00:14:56 +0000258 j = 0;
259 for (;;) {
Guido van Rossumc7903a12002-08-16 07:04:56 +0000260 PyObject *item;
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000261 int ok;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000262
Tim Peters0e57abf2001-05-02 07:39:38 +0000263 item = PyIter_Next(it);
264 if (item == NULL) {
Tim Petersf4848da2001-05-05 00:14:56 +0000265 if (PyErr_Occurred())
266 goto Fail_result_it;
Tim Peters0e57abf2001-05-02 07:39:38 +0000267 break;
Guido van Rossum2d951851994-08-29 12:52:16 +0000268 }
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000269
Neil Schemenauer68973552003-08-14 20:37:34 +0000270 if (func == (PyObject *)&PyBool_Type || func == Py_None) {
Guido van Rossumc7903a12002-08-16 07:04:56 +0000271 ok = PyObject_IsTrue(item);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000272 }
273 else {
Guido van Rossumc7903a12002-08-16 07:04:56 +0000274 PyObject *good;
275 PyTuple_SET_ITEM(arg, 0, item);
276 good = PyObject_Call(func, arg, NULL);
277 PyTuple_SET_ITEM(arg, 0, NULL);
Guido van Rossum58b68731995-01-10 17:40:55 +0000278 if (good == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000279 Py_DECREF(item);
Tim Peters0e57abf2001-05-02 07:39:38 +0000280 goto Fail_result_it;
Guido van Rossum58b68731995-01-10 17:40:55 +0000281 }
Guido van Rossumc7903a12002-08-16 07:04:56 +0000282 ok = PyObject_IsTrue(good);
283 Py_DECREF(good);
Guido van Rossum12d12c51993-10-26 17:58:25 +0000284 }
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000285 if (ok) {
Tim Peters0e57abf2001-05-02 07:39:38 +0000286 if (j < len)
287 PyList_SET_ITEM(result, j, item);
Guido van Rossum2d951851994-08-29 12:52:16 +0000288 else {
Barry Warsawfa77e091999-01-28 18:49:12 +0000289 int status = PyList_Append(result, item);
Barry Warsawfa77e091999-01-28 18:49:12 +0000290 Py_DECREF(item);
291 if (status < 0)
Tim Peters0e57abf2001-05-02 07:39:38 +0000292 goto Fail_result_it;
Guido van Rossum2d951851994-08-29 12:52:16 +0000293 }
Tim Peters0e57abf2001-05-02 07:39:38 +0000294 ++j;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000295 }
Tim Peters0e57abf2001-05-02 07:39:38 +0000296 else
297 Py_DECREF(item);
Guido van Rossum12d12c51993-10-26 17:58:25 +0000298 }
299
Guido van Rossum12d12c51993-10-26 17:58:25 +0000300
Tim Peters0e57abf2001-05-02 07:39:38 +0000301 /* Cut back result list if len is too big. */
Guido van Rossum79f25d91997-04-29 20:08:16 +0000302 if (j < len && PyList_SetSlice(result, j, len, NULL) < 0)
Tim Peters0e57abf2001-05-02 07:39:38 +0000303 goto Fail_result_it;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000304
Tim Peters3c6b1482001-05-21 08:07:05 +0000305 Py_DECREF(it);
Guido van Rossumc7903a12002-08-16 07:04:56 +0000306 Py_DECREF(arg);
Guido van Rossum12d12c51993-10-26 17:58:25 +0000307 return result;
308
Tim Peters0e57abf2001-05-02 07:39:38 +0000309Fail_result_it:
Guido van Rossum79f25d91997-04-29 20:08:16 +0000310 Py_DECREF(result);
Tim Peters0e57abf2001-05-02 07:39:38 +0000311Fail_it:
312 Py_DECREF(it);
Guido van Rossumc7903a12002-08-16 07:04:56 +0000313Fail_arg:
314 Py_DECREF(arg);
Guido van Rossum12d12c51993-10-26 17:58:25 +0000315 return NULL;
316}
317
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000318PyDoc_STRVAR(filter_doc,
Tim Petersd50e5442002-03-09 00:06:26 +0000319"filter(function or None, sequence) -> list, tuple, or string\n"
320"\n"
321"Return those items of sequence for which function(item) is true. If\n"
322"function is None, return the items that are true. If sequence is a tuple\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000323"or string, return the same type, else return a list.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000324
Guido van Rossum79f25d91997-04-29 20:08:16 +0000325static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000326builtin_chr(PyObject *self, PyObject *args)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000327{
328 long x;
329 char s[1];
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000330
Guido van Rossum79f25d91997-04-29 20:08:16 +0000331 if (!PyArg_ParseTuple(args, "l:chr", &x))
Guido van Rossum3f5da241990-12-20 15:06:42 +0000332 return NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000333 if (x < 0 || x >= 256) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000334 PyErr_SetString(PyExc_ValueError,
335 "chr() arg not in range(256)");
Guido van Rossum3f5da241990-12-20 15:06:42 +0000336 return NULL;
337 }
Guido van Rossum6bf62da1997-04-11 20:37:35 +0000338 s[0] = (char)x;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000339 return PyString_FromStringAndSize(s, 1);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000340}
341
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000342PyDoc_STRVAR(chr_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000343"chr(i) -> character\n\
344\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000345Return a string of one character with ordinal i; 0 <= i < 256.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000346
347
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000348#ifdef Py_USING_UNICODE
Guido van Rossum79f25d91997-04-29 20:08:16 +0000349static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000350builtin_unichr(PyObject *self, PyObject *args)
Guido van Rossum09095f32000-03-10 23:00:52 +0000351{
352 long x;
Guido van Rossum09095f32000-03-10 23:00:52 +0000353
354 if (!PyArg_ParseTuple(args, "l:unichr", &x))
355 return NULL;
Fredrik Lundh0dcf67e2001-06-26 20:01:56 +0000356
Marc-André Lemburgcc8764c2002-08-11 12:23:04 +0000357 return PyUnicode_FromOrdinal(x);
Guido van Rossum09095f32000-03-10 23:00:52 +0000358}
359
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000360PyDoc_STRVAR(unichr_doc,
Fred Drake078b24f2000-04-13 02:42:50 +0000361"unichr(i) -> Unicode character\n\
Guido van Rossum09095f32000-03-10 23:00:52 +0000362\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000363Return a Unicode string of one character with ordinal i; 0 <= i <= 0x10ffff.");
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000364#endif
Guido van Rossum09095f32000-03-10 23:00:52 +0000365
366
367static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000368builtin_cmp(PyObject *self, PyObject *args)
Guido van Rossuma9e7dc11992-10-18 18:53:57 +0000369{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000370 PyObject *a, *b;
Guido van Rossum09df08a1998-05-22 00:51:39 +0000371 int c;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000372
Raymond Hettingerea3fdf42002-12-29 16:33:45 +0000373 if (!PyArg_UnpackTuple(args, "cmp", 2, 2, &a, &b))
Guido van Rossuma9e7dc11992-10-18 18:53:57 +0000374 return NULL;
Guido van Rossum09df08a1998-05-22 00:51:39 +0000375 if (PyObject_Cmp(a, b, &c) < 0)
Guido van Rossumc8b6df91997-05-23 00:06:51 +0000376 return NULL;
Guido van Rossum09df08a1998-05-22 00:51:39 +0000377 return PyInt_FromLong((long)c);
Guido van Rossuma9e7dc11992-10-18 18:53:57 +0000378}
379
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000380PyDoc_STRVAR(cmp_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000381"cmp(x, y) -> integer\n\
382\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000383Return negative if x<y, zero if x==y, positive if x>y.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000384
385
Guido van Rossum79f25d91997-04-29 20:08:16 +0000386static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000387builtin_coerce(PyObject *self, PyObject *args)
Guido van Rossum6a00cd81995-01-07 12:39:01 +0000388{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000389 PyObject *v, *w;
390 PyObject *res;
Guido van Rossum5524a591995-01-10 15:26:20 +0000391
Raymond Hettingerea3fdf42002-12-29 16:33:45 +0000392 if (!PyArg_UnpackTuple(args, "coerce", 2, 2, &v, &w))
Guido van Rossum5524a591995-01-10 15:26:20 +0000393 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000394 if (PyNumber_Coerce(&v, &w) < 0)
Guido van Rossum04691fc1992-08-12 15:35:34 +0000395 return NULL;
Raymond Hettinger8ae46892003-10-12 19:09:37 +0000396 res = PyTuple_Pack(2, v, w);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000397 Py_DECREF(v);
398 Py_DECREF(w);
Guido van Rossum04691fc1992-08-12 15:35:34 +0000399 return res;
400}
401
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000402PyDoc_STRVAR(coerce_doc,
Martin v. Löwis8d494f32004-08-25 10:42:41 +0000403"coerce(x, y) -> (x1, y1)\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000404\n\
Martin v. Löwis8d494f32004-08-25 10:42:41 +0000405Return a tuple consisting of the two numeric arguments converted to\n\
406a common type, using the same rules as used by arithmetic operations.\n\
407If coercion is not possible, raise TypeError.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000408
Guido van Rossum79f25d91997-04-29 20:08:16 +0000409static PyObject *
Georg Brandl5240d742007-03-13 20:46:32 +0000410builtin_compile(PyObject *self, PyObject *args, PyObject *kwds)
Guido van Rossum5b722181993-03-30 17:46:03 +0000411{
412 char *str;
413 char *filename;
414 char *startstr;
415 int start;
Tim Peters6cd6a822001-08-17 22:11:27 +0000416 int dont_inherit = 0;
417 int supplied_flags = 0;
Tim Peters5ba58662001-07-16 02:29:45 +0000418 PyCompilerFlags cf;
Neal Norwitz3a9a3e72005-11-27 20:38:31 +0000419 PyObject *result = NULL, *cmd, *tmp = NULL;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000420 Py_ssize_t length;
Georg Brandl5240d742007-03-13 20:46:32 +0000421 static char *kwlist[] = {"source", "filename", "mode", "flags",
422 "dont_inherit", NULL};
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000423
Georg Brandl5240d742007-03-13 20:46:32 +0000424 if (!PyArg_ParseTupleAndKeywords(args, kwds, "Oss|ii:compile",
425 kwlist, &cmd, &filename, &startstr,
426 &supplied_flags, &dont_inherit))
Guido van Rossum5b722181993-03-30 17:46:03 +0000427 return NULL;
Tim Peters6cd6a822001-08-17 22:11:27 +0000428
Just van Rossum3aaf42c2003-02-10 08:21:10 +0000429 cf.cf_flags = supplied_flags;
430
431#ifdef Py_USING_UNICODE
432 if (PyUnicode_Check(cmd)) {
433 tmp = PyUnicode_AsUTF8String(cmd);
434 if (tmp == NULL)
435 return NULL;
436 cmd = tmp;
437 cf.cf_flags |= PyCF_SOURCE_IS_UTF8;
438 }
439#endif
Just van Rossumb9b8e9c2003-02-10 09:22:01 +0000440 if (PyObject_AsReadBuffer(cmd, (const void **)&str, &length))
441 return NULL;
Tim Peters2c646c92003-02-10 14:48:29 +0000442 if ((size_t)length != strlen(str)) {
Just van Rossum3aaf42c2003-02-10 08:21:10 +0000443 PyErr_SetString(PyExc_TypeError,
Alex Martellia9b9c9f2003-04-23 13:34:35 +0000444 "compile() expected string without null bytes");
Neal Norwitz3a9a3e72005-11-27 20:38:31 +0000445 goto cleanup;
Just van Rossum3aaf42c2003-02-10 08:21:10 +0000446 }
447
Guido van Rossum5b722181993-03-30 17:46:03 +0000448 if (strcmp(startstr, "exec") == 0)
Guido van Rossumb05a5c71997-05-07 17:46:13 +0000449 start = Py_file_input;
Guido van Rossum5b722181993-03-30 17:46:03 +0000450 else if (strcmp(startstr, "eval") == 0)
Guido van Rossumb05a5c71997-05-07 17:46:13 +0000451 start = Py_eval_input;
Guido van Rossum872537c1995-07-07 22:43:42 +0000452 else if (strcmp(startstr, "single") == 0)
Guido van Rossumb05a5c71997-05-07 17:46:13 +0000453 start = Py_single_input;
Guido van Rossum5b722181993-03-30 17:46:03 +0000454 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000455 PyErr_SetString(PyExc_ValueError,
Fred Drake661ea262000-10-24 19:57:45 +0000456 "compile() arg 3 must be 'exec' or 'eval' or 'single'");
Neal Norwitz3a9a3e72005-11-27 20:38:31 +0000457 goto cleanup;
Guido van Rossum5b722181993-03-30 17:46:03 +0000458 }
Tim Peters6cd6a822001-08-17 22:11:27 +0000459
Guido van Rossum4b499dd32003-02-13 22:07:59 +0000460 if (supplied_flags &
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000461 ~(PyCF_MASK | PyCF_MASK_OBSOLETE | PyCF_DONT_IMPLY_DEDENT | PyCF_ONLY_AST))
Guido van Rossum4b499dd32003-02-13 22:07:59 +0000462 {
Tim Peters6cd6a822001-08-17 22:11:27 +0000463 PyErr_SetString(PyExc_ValueError,
464 "compile(): unrecognised flags");
Neal Norwitz3a9a3e72005-11-27 20:38:31 +0000465 goto cleanup;
Tim Peters6cd6a822001-08-17 22:11:27 +0000466 }
467 /* XXX Warn if (supplied_flags & PyCF_MASK_OBSOLETE) != 0? */
468
Tim Peters6cd6a822001-08-17 22:11:27 +0000469 if (!dont_inherit) {
470 PyEval_MergeCompilerFlags(&cf);
471 }
Just van Rossum3aaf42c2003-02-10 08:21:10 +0000472 result = Py_CompileStringFlags(str, filename, start, &cf);
Neal Norwitz3a9a3e72005-11-27 20:38:31 +0000473cleanup:
Just van Rossum3aaf42c2003-02-10 08:21:10 +0000474 Py_XDECREF(tmp);
475 return result;
Guido van Rossum5b722181993-03-30 17:46:03 +0000476}
477
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000478PyDoc_STRVAR(compile_doc,
Tim Peters6cd6a822001-08-17 22:11:27 +0000479"compile(source, filename, mode[, flags[, dont_inherit]]) -> code object\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000480\n\
481Compile the source string (a Python module, statement or expression)\n\
482into a code object that can be executed by the exec statement or eval().\n\
483The filename will be used for run-time error messages.\n\
484The mode must be 'exec' to compile a module, 'single' to compile a\n\
Tim Peters6cd6a822001-08-17 22:11:27 +0000485single (interactive) statement, or 'eval' to compile an expression.\n\
486The flags argument, if present, controls which future statements influence\n\
487the compilation of the code.\n\
488The dont_inherit argument, if non-zero, stops the compilation inheriting\n\
489the effects of any future statements in effect in the code calling\n\
490compile; if absent or zero these statements do influence the compilation,\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000491in addition to any features explicitly specified.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000492
Guido van Rossum79f25d91997-04-29 20:08:16 +0000493static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000494builtin_dir(PyObject *self, PyObject *args)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000495{
Tim Peters5d2b77c2001-09-03 05:47:38 +0000496 PyObject *arg = NULL;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000497
Raymond Hettingerea3fdf42002-12-29 16:33:45 +0000498 if (!PyArg_UnpackTuple(args, "dir", 0, 1, &arg))
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000499 return NULL;
Tim Peters7eea37e2001-09-04 22:08:56 +0000500 return PyObject_Dir(arg);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000501}
502
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000503PyDoc_STRVAR(dir_doc,
Tim Peters5d2b77c2001-09-03 05:47:38 +0000504"dir([object]) -> list of strings\n"
505"\n"
Georg Brandl871f1bc2007-03-12 13:17:36 +0000506"If called without an argument, return the names in the current scope.\n"
507"Else, return an alphabetized list of names comprising (some of) the attributes\n"
508"of the given object, and of attributes reachable from it.\n"
509"If the object supplies a method named __dir__, it will be used; otherwise\n"
510"the default dir() logic is used and returns:\n"
511" for a module object: the module's attributes.\n"
512" for a class object: its attributes, and recursively the attributes\n"
513" of its bases.\n"
Georg Brandl3bb15672007-03-13 07:23:16 +0000514" for any other object: its attributes, its class's attributes, and\n"
Georg Brandl871f1bc2007-03-12 13:17:36 +0000515" recursively the attributes of its class's base classes.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000516
Guido van Rossum79f25d91997-04-29 20:08:16 +0000517static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000518builtin_divmod(PyObject *self, PyObject *args)
Guido van Rossum6a00cd81995-01-07 12:39:01 +0000519{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000520 PyObject *v, *w;
Guido van Rossum6a00cd81995-01-07 12:39:01 +0000521
Raymond Hettingerea3fdf42002-12-29 16:33:45 +0000522 if (!PyArg_UnpackTuple(args, "divmod", 2, 2, &v, &w))
Guido van Rossum6a00cd81995-01-07 12:39:01 +0000523 return NULL;
Guido van Rossum09df08a1998-05-22 00:51:39 +0000524 return PyNumber_Divmod(v, w);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000525}
526
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000527PyDoc_STRVAR(divmod_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000528"divmod(x, y) -> (div, mod)\n\
529\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000530Return the tuple ((x-x%y)/y, x%y). Invariant: div*y + mod == x.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000531
532
Guido van Rossum79f25d91997-04-29 20:08:16 +0000533static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000534builtin_eval(PyObject *self, PyObject *args)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000535{
Just van Rossum3aaf42c2003-02-10 08:21:10 +0000536 PyObject *cmd, *result, *tmp = NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000537 PyObject *globals = Py_None, *locals = Py_None;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000538 char *str;
Tim Peters9fa96be2001-08-17 23:04:59 +0000539 PyCompilerFlags cf;
Guido van Rossum590baa41993-11-30 13:40:46 +0000540
Raymond Hettinger214b1c32004-07-02 06:41:07 +0000541 if (!PyArg_UnpackTuple(args, "eval", 1, 3, &cmd, &globals, &locals))
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000542 return NULL;
Raymond Hettinger214b1c32004-07-02 06:41:07 +0000543 if (locals != Py_None && !PyMapping_Check(locals)) {
544 PyErr_SetString(PyExc_TypeError, "locals must be a mapping");
Raymond Hettinger513ffe82004-07-06 13:44:41 +0000545 return NULL;
Raymond Hettinger214b1c32004-07-02 06:41:07 +0000546 }
547 if (globals != Py_None && !PyDict_Check(globals)) {
Georg Brandl99d7e4e2005-08-31 22:21:15 +0000548 PyErr_SetString(PyExc_TypeError, PyMapping_Check(globals) ?
Raymond Hettinger214b1c32004-07-02 06:41:07 +0000549 "globals must be a real dict; try eval(expr, {}, mapping)"
550 : "globals must be a dict");
Raymond Hettinger513ffe82004-07-06 13:44:41 +0000551 return NULL;
Raymond Hettinger214b1c32004-07-02 06:41:07 +0000552 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000553 if (globals == Py_None) {
554 globals = PyEval_GetGlobals();
555 if (locals == Py_None)
556 locals = PyEval_GetLocals();
Guido van Rossum6135a871995-01-09 17:53:26 +0000557 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000558 else if (locals == Py_None)
Guido van Rossum6135a871995-01-09 17:53:26 +0000559 locals = globals;
Tim Peters9fa96be2001-08-17 23:04:59 +0000560
Georg Brandl77c85e62005-09-15 10:46:13 +0000561 if (globals == NULL || locals == NULL) {
562 PyErr_SetString(PyExc_TypeError,
563 "eval must be given globals and locals "
564 "when called without a frame");
565 return NULL;
566 }
567
Guido van Rossum79f25d91997-04-29 20:08:16 +0000568 if (PyDict_GetItemString(globals, "__builtins__") == NULL) {
569 if (PyDict_SetItemString(globals, "__builtins__",
570 PyEval_GetBuiltins()) != 0)
Guido van Rossum6135a871995-01-09 17:53:26 +0000571 return NULL;
572 }
Tim Peters9fa96be2001-08-17 23:04:59 +0000573
Jeremy Hylton15c1c4f2001-07-30 21:50:55 +0000574 if (PyCode_Check(cmd)) {
Jeremy Hylton733c8932001-12-13 19:51:56 +0000575 if (PyCode_GetNumFree((PyCodeObject *)cmd) > 0) {
Jeremy Hylton15c1c4f2001-07-30 21:50:55 +0000576 PyErr_SetString(PyExc_TypeError,
577 "code object passed to eval() may not contain free variables");
578 return NULL;
579 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000580 return PyEval_EvalCode((PyCodeObject *) cmd, globals, locals);
Jeremy Hylton15c1c4f2001-07-30 21:50:55 +0000581 }
Tim Peters9fa96be2001-08-17 23:04:59 +0000582
Marc-André Lemburgd1ba4432000-09-19 21:04:18 +0000583 if (!PyString_Check(cmd) &&
584 !PyUnicode_Check(cmd)) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000585 PyErr_SetString(PyExc_TypeError,
Fred Drake661ea262000-10-24 19:57:45 +0000586 "eval() arg 1 must be a string or code object");
Guido van Rossum94390a41992-08-14 15:14:30 +0000587 return NULL;
588 }
Just van Rossum3aaf42c2003-02-10 08:21:10 +0000589 cf.cf_flags = 0;
590
591#ifdef Py_USING_UNICODE
592 if (PyUnicode_Check(cmd)) {
593 tmp = PyUnicode_AsUTF8String(cmd);
594 if (tmp == NULL)
595 return NULL;
596 cmd = tmp;
597 cf.cf_flags |= PyCF_SOURCE_IS_UTF8;
598 }
599#endif
Neal Norwitz3a9a3e72005-11-27 20:38:31 +0000600 if (PyString_AsStringAndSize(cmd, &str, NULL)) {
601 Py_XDECREF(tmp);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000602 return NULL;
Neal Norwitz3a9a3e72005-11-27 20:38:31 +0000603 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000604 while (*str == ' ' || *str == '\t')
605 str++;
Tim Peters9fa96be2001-08-17 23:04:59 +0000606
Tim Peters9fa96be2001-08-17 23:04:59 +0000607 (void)PyEval_MergeCompilerFlags(&cf);
Just van Rossum3aaf42c2003-02-10 08:21:10 +0000608 result = PyRun_StringFlags(str, Py_eval_input, globals, locals, &cf);
609 Py_XDECREF(tmp);
610 return result;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000611}
612
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000613PyDoc_STRVAR(eval_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000614"eval(source[, globals[, locals]]) -> value\n\
615\n\
616Evaluate the source in the context of globals and locals.\n\
617The source may be a string representing a Python expression\n\
618or a code object as returned by compile().\n\
Neal Norwitz477ca1c2006-09-05 02:25:41 +0000619The globals must be a dictionary and locals can be any mapping,\n\
Raymond Hettinger214b1c32004-07-02 06:41:07 +0000620defaulting to the current globals and locals.\n\
621If only globals is given, locals defaults to it.\n");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000622
623
Guido van Rossum79f25d91997-04-29 20:08:16 +0000624static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000625builtin_execfile(PyObject *self, PyObject *args)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000626{
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000627 char *filename;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000628 PyObject *globals = Py_None, *locals = Py_None;
629 PyObject *res;
Guido van Rossumb3a639e2001-09-05 13:37:47 +0000630 FILE* fp = NULL;
Tim Peters5ba58662001-07-16 02:29:45 +0000631 PyCompilerFlags cf;
Martin v. Löwis6b3a2c42001-08-08 05:30:36 +0000632 int exists;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000633
Raymond Hettinger66bd2332004-08-02 08:30:07 +0000634 if (!PyArg_ParseTuple(args, "s|O!O:execfile",
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000635 &filename,
Guido van Rossum79f25d91997-04-29 20:08:16 +0000636 &PyDict_Type, &globals,
Raymond Hettinger66bd2332004-08-02 08:30:07 +0000637 &locals))
Guido van Rossum0f61f8a1992-02-25 18:55:05 +0000638 return NULL;
Raymond Hettinger66bd2332004-08-02 08:30:07 +0000639 if (locals != Py_None && !PyMapping_Check(locals)) {
640 PyErr_SetString(PyExc_TypeError, "locals must be a mapping");
641 return NULL;
642 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000643 if (globals == Py_None) {
644 globals = PyEval_GetGlobals();
645 if (locals == Py_None)
646 locals = PyEval_GetLocals();
Guido van Rossum6135a871995-01-09 17:53:26 +0000647 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000648 else if (locals == Py_None)
Guido van Rossum6135a871995-01-09 17:53:26 +0000649 locals = globals;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000650 if (PyDict_GetItemString(globals, "__builtins__") == NULL) {
651 if (PyDict_SetItemString(globals, "__builtins__",
652 PyEval_GetBuiltins()) != 0)
Guido van Rossum6135a871995-01-09 17:53:26 +0000653 return NULL;
654 }
Martin v. Löwis6b3a2c42001-08-08 05:30:36 +0000655
656 exists = 0;
657 /* Test for existence or directory. */
Martin v. Löwis3484a182002-03-09 12:07:51 +0000658#if defined(PLAN9)
659 {
660 Dir *d;
661
662 if ((d = dirstat(filename))!=nil) {
663 if(d->mode & DMDIR)
664 werrstr("is a directory");
665 else
666 exists = 1;
667 free(d);
668 }
Martin v. Löwis6b3a2c42001-08-08 05:30:36 +0000669 }
Martin v. Löwis3484a182002-03-09 12:07:51 +0000670#elif defined(RISCOS)
Guido van Rossume2ae77b2001-10-24 20:42:55 +0000671 if (object_exists(filename)) {
672 if (isdir(filename))
673 errno = EISDIR;
674 else
675 exists = 1;
676 }
Martin v. Löwis3484a182002-03-09 12:07:51 +0000677#else /* standard Posix */
678 {
679 struct stat s;
680 if (stat(filename, &s) == 0) {
681 if (S_ISDIR(s.st_mode))
Andrew MacIntyreda4d6cb2004-03-29 11:53:38 +0000682# if defined(PYOS_OS2) && defined(PYCC_VACPP)
Martin v. Löwis3484a182002-03-09 12:07:51 +0000683 errno = EOS2ERR;
684# else
685 errno = EISDIR;
686# endif
687 else
688 exists = 1;
689 }
690 }
691#endif
Martin v. Löwis6b3a2c42001-08-08 05:30:36 +0000692
693 if (exists) {
694 Py_BEGIN_ALLOW_THREADS
Jack Jansen7b8c7542002-04-14 20:12:41 +0000695 fp = fopen(filename, "r" PY_STDIOTEXTMODE);
Martin v. Löwis6b3a2c42001-08-08 05:30:36 +0000696 Py_END_ALLOW_THREADS
697
698 if (fp == NULL) {
699 exists = 0;
700 }
701 }
702
703 if (!exists) {
Peter Schneider-Kamp4c013422002-08-27 16:58:00 +0000704 PyErr_SetFromErrnoWithFilename(PyExc_IOError, filename);
Guido van Rossum0f61f8a1992-02-25 18:55:05 +0000705 return NULL;
706 }
Tim Peters5ba58662001-07-16 02:29:45 +0000707 cf.cf_flags = 0;
708 if (PyEval_MergeCompilerFlags(&cf))
Tim Peters748b8bb2001-04-28 08:20:22 +0000709 res = PyRun_FileExFlags(fp, filename, Py_file_input, globals,
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000710 locals, 1, &cf);
Tim Peters5ba58662001-07-16 02:29:45 +0000711 else
Tim Peters748b8bb2001-04-28 08:20:22 +0000712 res = PyRun_FileEx(fp, filename, Py_file_input, globals,
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000713 locals, 1);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000714 return res;
Guido van Rossum0f61f8a1992-02-25 18:55:05 +0000715}
716
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000717PyDoc_STRVAR(execfile_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000718"execfile(filename[, globals[, locals]])\n\
719\n\
720Read and execute a Python script from a file.\n\
721The globals and locals are dictionaries, defaulting to the current\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000722globals and locals. If only globals is given, locals defaults to it.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000723
724
Guido van Rossum79f25d91997-04-29 20:08:16 +0000725static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000726builtin_getattr(PyObject *self, PyObject *args)
Guido van Rossum33894be1992-01-27 16:53:09 +0000727{
Guido van Rossum950ff291998-06-29 13:38:57 +0000728 PyObject *v, *result, *dflt = NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000729 PyObject *name;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000730
Raymond Hettingerea3fdf42002-12-29 16:33:45 +0000731 if (!PyArg_UnpackTuple(args, "getattr", 2, 3, &v, &name, &dflt))
Guido van Rossum33894be1992-01-27 16:53:09 +0000732 return NULL;
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000733#ifdef Py_USING_UNICODE
Jeremy Hylton0eb11152001-07-30 22:39:31 +0000734 if (PyUnicode_Check(name)) {
735 name = _PyUnicode_AsDefaultEncodedString(name, NULL);
736 if (name == NULL)
737 return NULL;
738 }
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000739#endif
Jeremy Hylton0eb11152001-07-30 22:39:31 +0000740
741 if (!PyString_Check(name)) {
742 PyErr_SetString(PyExc_TypeError,
Alex Martellia9b9c9f2003-04-23 13:34:35 +0000743 "getattr(): attribute name must be string");
Jeremy Hylton0eb11152001-07-30 22:39:31 +0000744 return NULL;
745 }
Guido van Rossum950ff291998-06-29 13:38:57 +0000746 result = PyObject_GetAttr(v, name);
Guido van Rossumd8923572001-10-16 21:31:32 +0000747 if (result == NULL && dflt != NULL &&
748 PyErr_ExceptionMatches(PyExc_AttributeError))
749 {
Guido van Rossum950ff291998-06-29 13:38:57 +0000750 PyErr_Clear();
751 Py_INCREF(dflt);
752 result = dflt;
753 }
754 return result;
Guido van Rossum9bfef441993-03-29 10:43:31 +0000755}
756
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000757PyDoc_STRVAR(getattr_doc,
Guido van Rossum950ff291998-06-29 13:38:57 +0000758"getattr(object, name[, default]) -> value\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000759\n\
Guido van Rossum950ff291998-06-29 13:38:57 +0000760Get a named attribute from an object; getattr(x, 'y') is equivalent to x.y.\n\
761When a default argument is given, it is returned when the attribute doesn't\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000762exist; without it, an exception is raised in that case.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000763
764
Guido van Rossum79f25d91997-04-29 20:08:16 +0000765static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000766builtin_globals(PyObject *self)
Guido van Rossum872537c1995-07-07 22:43:42 +0000767{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000768 PyObject *d;
Guido van Rossum872537c1995-07-07 22:43:42 +0000769
Guido van Rossum79f25d91997-04-29 20:08:16 +0000770 d = PyEval_GetGlobals();
Neal Norwitz43bd4db2006-08-12 01:46:42 +0000771 Py_XINCREF(d);
Guido van Rossum872537c1995-07-07 22:43:42 +0000772 return d;
773}
774
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000775PyDoc_STRVAR(globals_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000776"globals() -> dictionary\n\
777\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000778Return the dictionary containing the current scope's global variables.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000779
780
Guido van Rossum79f25d91997-04-29 20:08:16 +0000781static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000782builtin_hasattr(PyObject *self, PyObject *args)
Guido van Rossum9bfef441993-03-29 10:43:31 +0000783{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000784 PyObject *v;
785 PyObject *name;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000786
Raymond Hettingerea3fdf42002-12-29 16:33:45 +0000787 if (!PyArg_UnpackTuple(args, "hasattr", 2, 2, &v, &name))
Guido van Rossum9bfef441993-03-29 10:43:31 +0000788 return NULL;
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000789#ifdef Py_USING_UNICODE
Jeremy Hylton302b54a2001-07-30 22:45:19 +0000790 if (PyUnicode_Check(name)) {
791 name = _PyUnicode_AsDefaultEncodedString(name, NULL);
792 if (name == NULL)
793 return NULL;
794 }
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000795#endif
Jeremy Hylton302b54a2001-07-30 22:45:19 +0000796
797 if (!PyString_Check(name)) {
798 PyErr_SetString(PyExc_TypeError,
Alex Martellia9b9c9f2003-04-23 13:34:35 +0000799 "hasattr(): attribute name must be string");
Jeremy Hylton302b54a2001-07-30 22:45:19 +0000800 return NULL;
801 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000802 v = PyObject_GetAttr(v, name);
Guido van Rossum9bfef441993-03-29 10:43:31 +0000803 if (v == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000804 PyErr_Clear();
Guido van Rossum09df08a1998-05-22 00:51:39 +0000805 Py_INCREF(Py_False);
806 return Py_False;
Guido van Rossum9bfef441993-03-29 10:43:31 +0000807 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000808 Py_DECREF(v);
Guido van Rossum09df08a1998-05-22 00:51:39 +0000809 Py_INCREF(Py_True);
810 return Py_True;
Guido van Rossum33894be1992-01-27 16:53:09 +0000811}
812
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000813PyDoc_STRVAR(hasattr_doc,
Guido van Rossum77f6a652002-04-03 22:41:51 +0000814"hasattr(object, name) -> bool\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000815\n\
816Return whether the object has an attribute with the given name.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000817(This is done by calling getattr(object, name) and catching exceptions.)");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000818
819
Guido van Rossum79f25d91997-04-29 20:08:16 +0000820static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000821builtin_id(PyObject *self, PyObject *v)
Guido van Rossum5b722181993-03-30 17:46:03 +0000822{
Guido van Rossum106f2da2000-06-28 21:12:25 +0000823 return PyLong_FromVoidPtr(v);
Guido van Rossum5b722181993-03-30 17:46:03 +0000824}
825
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000826PyDoc_STRVAR(id_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000827"id(object) -> integer\n\
828\n\
829Return the identity of an object. This is guaranteed to be unique among\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000830simultaneously existing objects. (Hint: it's the object's memory address.)");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000831
832
Guido van Rossum79f25d91997-04-29 20:08:16 +0000833static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000834builtin_map(PyObject *self, PyObject *args)
Guido van Rossum12d12c51993-10-26 17:58:25 +0000835{
836 typedef struct {
Tim Peters4e9afdc2001-05-03 23:54:49 +0000837 PyObject *it; /* the iterator object */
838 int saw_StopIteration; /* bool: did the iterator end? */
Guido van Rossum12d12c51993-10-26 17:58:25 +0000839 } sequence;
840
Guido van Rossum79f25d91997-04-29 20:08:16 +0000841 PyObject *func, *result;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000842 sequence *seqs = NULL, *sqp;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000843 Py_ssize_t n, len;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000844 register int i, j;
845
Guido van Rossum79f25d91997-04-29 20:08:16 +0000846 n = PyTuple_Size(args);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000847 if (n < 2) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000848 PyErr_SetString(PyExc_TypeError,
849 "map() requires at least two args");
Guido van Rossum12d12c51993-10-26 17:58:25 +0000850 return NULL;
851 }
852
Guido van Rossum79f25d91997-04-29 20:08:16 +0000853 func = PyTuple_GetItem(args, 0);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000854 n--;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000855
Guido van Rossumfa4ac711998-07-10 17:37:30 +0000856 if (func == Py_None && n == 1) {
857 /* map(None, S) is the same as list(S). */
858 return PySequence_List(PyTuple_GetItem(args, 1));
859 }
860
Tim Peters4e9afdc2001-05-03 23:54:49 +0000861 /* Get space for sequence descriptors. Must NULL out the iterator
862 * pointers so that jumping to Fail_2 later doesn't see trash.
863 */
Guido van Rossum79f25d91997-04-29 20:08:16 +0000864 if ((seqs = PyMem_NEW(sequence, n)) == NULL) {
865 PyErr_NoMemory();
Tim Peters4e9afdc2001-05-03 23:54:49 +0000866 return NULL;
867 }
868 for (i = 0; i < n; ++i) {
869 seqs[i].it = (PyObject*)NULL;
870 seqs[i].saw_StopIteration = 0;
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000871 }
Guido van Rossum12d12c51993-10-26 17:58:25 +0000872
Tim Peters4e9afdc2001-05-03 23:54:49 +0000873 /* Do a first pass to obtain iterators for the arguments, and set len
874 * to the largest of their lengths.
Tim Peters748b8bb2001-04-28 08:20:22 +0000875 */
Tim Peters4e9afdc2001-05-03 23:54:49 +0000876 len = 0;
877 for (i = 0, sqp = seqs; i < n; ++i, ++sqp) {
878 PyObject *curseq;
Martin v. Löwisd96ee902006-02-16 14:37:16 +0000879 Py_ssize_t curlen;
Guido van Rossumad991772001-01-12 16:03:05 +0000880
Tim Peters4e9afdc2001-05-03 23:54:49 +0000881 /* Get iterator. */
882 curseq = PyTuple_GetItem(args, i+1);
883 sqp->it = PyObject_GetIter(curseq);
884 if (sqp->it == NULL) {
Guido van Rossum12d12c51993-10-26 17:58:25 +0000885 static char errmsg[] =
Tim Peters4e9afdc2001-05-03 23:54:49 +0000886 "argument %d to map() must support iteration";
Guido van Rossum15e33a41997-04-30 19:00:27 +0000887 char errbuf[sizeof(errmsg) + 25];
Jeremy Hylton518ab1c2001-11-28 20:42:20 +0000888 PyOS_snprintf(errbuf, sizeof(errbuf), errmsg, i+2);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000889 PyErr_SetString(PyExc_TypeError, errbuf);
Guido van Rossum12d12c51993-10-26 17:58:25 +0000890 goto Fail_2;
891 }
892
Tim Peters4e9afdc2001-05-03 23:54:49 +0000893 /* Update len. */
Armin Rigof5b3e362006-02-11 21:32:43 +0000894 curlen = _PyObject_LengthHint(curseq);
Raymond Hettinger77f3c872004-01-04 08:54:44 +0000895 if (curlen < 0) {
Raymond Hettingera710b332005-08-21 11:03:59 +0000896 if (!PyErr_ExceptionMatches(PyExc_TypeError) &&
897 !PyErr_ExceptionMatches(PyExc_AttributeError)) {
898 goto Fail_2;
899 }
Raymond Hettinger77f3c872004-01-04 08:54:44 +0000900 PyErr_Clear();
Tim Peters4e9afdc2001-05-03 23:54:49 +0000901 curlen = 8; /* arbitrary */
Raymond Hettinger77f3c872004-01-04 08:54:44 +0000902 }
903 if (curlen > len)
904 len = curlen;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000905 }
906
Tim Peters4e9afdc2001-05-03 23:54:49 +0000907 /* Get space for the result list. */
Guido van Rossum79f25d91997-04-29 20:08:16 +0000908 if ((result = (PyObject *) PyList_New(len)) == NULL)
Guido van Rossum12d12c51993-10-26 17:58:25 +0000909 goto Fail_2;
910
Tim Peters4e9afdc2001-05-03 23:54:49 +0000911 /* Iterate over the sequences until all have stopped. */
Guido van Rossum2d951851994-08-29 12:52:16 +0000912 for (i = 0; ; ++i) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000913 PyObject *alist, *item=NULL, *value;
Tim Peters4e9afdc2001-05-03 23:54:49 +0000914 int numactive = 0;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000915
Guido van Rossum79f25d91997-04-29 20:08:16 +0000916 if (func == Py_None && n == 1)
Guido van Rossum32120311995-07-10 13:52:21 +0000917 alist = NULL;
Tim Peters4e9afdc2001-05-03 23:54:49 +0000918 else if ((alist = PyTuple_New(n)) == NULL)
919 goto Fail_1;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000920
921 for (j = 0, sqp = seqs; j < n; ++j, ++sqp) {
Tim Peters4e9afdc2001-05-03 23:54:49 +0000922 if (sqp->saw_StopIteration) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000923 Py_INCREF(Py_None);
924 item = Py_None;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000925 }
Guido van Rossum12d12c51993-10-26 17:58:25 +0000926 else {
Tim Peters4e9afdc2001-05-03 23:54:49 +0000927 item = PyIter_Next(sqp->it);
928 if (item)
929 ++numactive;
930 else {
Tim Peters4e9afdc2001-05-03 23:54:49 +0000931 if (PyErr_Occurred()) {
Tim Petersf4848da2001-05-05 00:14:56 +0000932 Py_XDECREF(alist);
933 goto Fail_1;
Guido van Rossum2d951851994-08-29 12:52:16 +0000934 }
Tim Peters4e9afdc2001-05-03 23:54:49 +0000935 Py_INCREF(Py_None);
936 item = Py_None;
937 sqp->saw_StopIteration = 1;
Guido van Rossum2d951851994-08-29 12:52:16 +0000938 }
Guido van Rossum12d12c51993-10-26 17:58:25 +0000939 }
Tim Peters4e9afdc2001-05-03 23:54:49 +0000940 if (alist)
941 PyTuple_SET_ITEM(alist, j, item);
942 else
Guido van Rossum2d951851994-08-29 12:52:16 +0000943 break;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000944 }
945
Guido van Rossum32120311995-07-10 13:52:21 +0000946 if (!alist)
947 alist = item;
Guido van Rossum2d951851994-08-29 12:52:16 +0000948
Tim Peters4e9afdc2001-05-03 23:54:49 +0000949 if (numactive == 0) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000950 Py_DECREF(alist);
Guido van Rossum2d951851994-08-29 12:52:16 +0000951 break;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000952 }
Guido van Rossum2d951851994-08-29 12:52:16 +0000953
Guido van Rossum79f25d91997-04-29 20:08:16 +0000954 if (func == Py_None)
Guido van Rossum32120311995-07-10 13:52:21 +0000955 value = alist;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000956 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000957 value = PyEval_CallObject(func, alist);
958 Py_DECREF(alist);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000959 if (value == NULL)
960 goto Fail_1;
Guido van Rossum2d951851994-08-29 12:52:16 +0000961 }
962 if (i >= len) {
Barry Warsawfa77e091999-01-28 18:49:12 +0000963 int status = PyList_Append(result, value);
Barry Warsaw21332871999-01-28 04:21:35 +0000964 Py_DECREF(value);
Barry Warsawfa77e091999-01-28 18:49:12 +0000965 if (status < 0)
966 goto Fail_1;
Guido van Rossum2d951851994-08-29 12:52:16 +0000967 }
Tim Peters4e9afdc2001-05-03 23:54:49 +0000968 else if (PyList_SetItem(result, i, value) < 0)
969 goto Fail_1;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000970 }
971
Guido van Rossumfa4ac711998-07-10 17:37:30 +0000972 if (i < len && PyList_SetSlice(result, i, len, NULL) < 0)
973 goto Fail_1;
974
Tim Peters4e9afdc2001-05-03 23:54:49 +0000975 goto Succeed;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000976
Guido van Rossum12d12c51993-10-26 17:58:25 +0000977Fail_1:
Guido van Rossum79f25d91997-04-29 20:08:16 +0000978 Py_DECREF(result);
Guido van Rossum12d12c51993-10-26 17:58:25 +0000979Fail_2:
Tim Peters4e9afdc2001-05-03 23:54:49 +0000980 result = NULL;
981Succeed:
982 assert(seqs);
983 for (i = 0; i < n; ++i)
984 Py_XDECREF(seqs[i].it);
985 PyMem_DEL(seqs);
986 return result;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000987}
988
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000989PyDoc_STRVAR(map_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000990"map(function, sequence[, sequence, ...]) -> list\n\
991\n\
992Return a list of the results of applying the function to the items of\n\
993the argument sequence(s). If more than one sequence is given, the\n\
994function is called with an argument list consisting of the corresponding\n\
995item of each sequence, substituting None for missing values when not all\n\
996sequences have the same length. If the function is None, return a list of\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000997the items of the sequence (or a list of tuples if more than one sequence).");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000998
999
Guido van Rossum79f25d91997-04-29 20:08:16 +00001000static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001001builtin_setattr(PyObject *self, PyObject *args)
Guido van Rossum33894be1992-01-27 16:53:09 +00001002{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001003 PyObject *v;
1004 PyObject *name;
1005 PyObject *value;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001006
Raymond Hettingerea3fdf42002-12-29 16:33:45 +00001007 if (!PyArg_UnpackTuple(args, "setattr", 3, 3, &v, &name, &value))
Guido van Rossum33894be1992-01-27 16:53:09 +00001008 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001009 if (PyObject_SetAttr(v, name, value) != 0)
Guido van Rossum33894be1992-01-27 16:53:09 +00001010 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001011 Py_INCREF(Py_None);
1012 return Py_None;
Guido van Rossum33894be1992-01-27 16:53:09 +00001013}
1014
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001015PyDoc_STRVAR(setattr_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001016"setattr(object, name, value)\n\
1017\n\
1018Set a named attribute on an object; setattr(x, 'y', v) is equivalent to\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001019``x.y = v''.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001020
1021
Guido van Rossum79f25d91997-04-29 20:08:16 +00001022static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001023builtin_delattr(PyObject *self, PyObject *args)
Guido van Rossum14144fc1994-08-29 12:53:40 +00001024{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001025 PyObject *v;
1026 PyObject *name;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001027
Raymond Hettingerea3fdf42002-12-29 16:33:45 +00001028 if (!PyArg_UnpackTuple(args, "delattr", 2, 2, &v, &name))
Guido van Rossum14144fc1994-08-29 12:53:40 +00001029 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001030 if (PyObject_SetAttr(v, name, (PyObject *)NULL) != 0)
Guido van Rossum14144fc1994-08-29 12:53:40 +00001031 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001032 Py_INCREF(Py_None);
1033 return Py_None;
Guido van Rossum14144fc1994-08-29 12:53:40 +00001034}
1035
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001036PyDoc_STRVAR(delattr_doc,
Guido van Rossumdf12a591998-11-23 22:13:04 +00001037"delattr(object, name)\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001038\n\
1039Delete a named attribute on an object; delattr(x, 'y') is equivalent to\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001040``del x.y''.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001041
1042
Guido van Rossum79f25d91997-04-29 20:08:16 +00001043static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001044builtin_hash(PyObject *self, PyObject *v)
Guido van Rossum9bfef441993-03-29 10:43:31 +00001045{
Guido van Rossum9bfef441993-03-29 10:43:31 +00001046 long x;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001047
Guido van Rossum79f25d91997-04-29 20:08:16 +00001048 x = PyObject_Hash(v);
Guido van Rossum9bfef441993-03-29 10:43:31 +00001049 if (x == -1)
1050 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001051 return PyInt_FromLong(x);
Guido van Rossum9bfef441993-03-29 10:43:31 +00001052}
1053
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001054PyDoc_STRVAR(hash_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001055"hash(object) -> integer\n\
1056\n\
1057Return a hash value for the object. Two objects with the same value have\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001058the same hash value. The reverse is not necessarily true, but likely.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001059
1060
Guido van Rossum79f25d91997-04-29 20:08:16 +00001061static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001062builtin_hex(PyObject *self, PyObject *v)
Guido van Rossum006bcd41991-10-24 14:54:44 +00001063{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001064 PyNumberMethods *nb;
Neil Schemenauer3a313e32004-07-19 16:29:17 +00001065 PyObject *res;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001066
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001067 if ((nb = v->ob_type->tp_as_number) == NULL ||
Guido van Rossum1899c2e1992-09-12 11:09:23 +00001068 nb->nb_hex == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001069 PyErr_SetString(PyExc_TypeError,
Guido van Rossum1899c2e1992-09-12 11:09:23 +00001070 "hex() argument can't be converted to hex");
1071 return NULL;
Guido van Rossum006bcd41991-10-24 14:54:44 +00001072 }
Neil Schemenauer3a313e32004-07-19 16:29:17 +00001073 res = (*nb->nb_hex)(v);
1074 if (res && !PyString_Check(res)) {
1075 PyErr_Format(PyExc_TypeError,
1076 "__hex__ returned non-string (type %.200s)",
1077 res->ob_type->tp_name);
1078 Py_DECREF(res);
1079 return NULL;
1080 }
1081 return res;
Guido van Rossum006bcd41991-10-24 14:54:44 +00001082}
1083
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001084PyDoc_STRVAR(hex_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001085"hex(number) -> string\n\
1086\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001087Return the hexadecimal representation of an integer or long integer.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001088
1089
Tim Petersdbd9ba62000-07-09 03:09:57 +00001090static PyObject *builtin_raw_input(PyObject *, PyObject *);
Guido van Rossum3165fe61992-09-25 21:59:05 +00001091
Guido van Rossum79f25d91997-04-29 20:08:16 +00001092static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001093builtin_input(PyObject *self, PyObject *args)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001094{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001095 PyObject *line;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001096 char *str;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001097 PyObject *res;
1098 PyObject *globals, *locals;
Hye-Shik Changff83c2b2004-02-02 13:39:01 +00001099 PyCompilerFlags cf;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001100
1101 line = builtin_raw_input(self, args);
Guido van Rossum3165fe61992-09-25 21:59:05 +00001102 if (line == NULL)
1103 return line;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001104 if (!PyArg_Parse(line, "s;embedded '\\0' in input line", &str))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001105 return NULL;
1106 while (*str == ' ' || *str == '\t')
1107 str++;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001108 globals = PyEval_GetGlobals();
1109 locals = PyEval_GetLocals();
1110 if (PyDict_GetItemString(globals, "__builtins__") == NULL) {
1111 if (PyDict_SetItemString(globals, "__builtins__",
1112 PyEval_GetBuiltins()) != 0)
Guido van Rossum6135a871995-01-09 17:53:26 +00001113 return NULL;
1114 }
Hye-Shik Changff83c2b2004-02-02 13:39:01 +00001115 cf.cf_flags = 0;
1116 PyEval_MergeCompilerFlags(&cf);
1117 res = PyRun_StringFlags(str, Py_eval_input, globals, locals, &cf);
Guido van Rossum79f25d91997-04-29 20:08:16 +00001118 Py_DECREF(line);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001119 return res;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001120}
1121
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001122PyDoc_STRVAR(input_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001123"input([prompt]) -> value\n\
1124\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001125Equivalent to eval(raw_input(prompt)).");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001126
1127
Guido van Rossume8811f81997-02-14 15:48:05 +00001128static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001129builtin_intern(PyObject *self, PyObject *args)
Guido van Rossume8811f81997-02-14 15:48:05 +00001130{
1131 PyObject *s;
Guido van Rossum43713e52000-02-29 13:59:29 +00001132 if (!PyArg_ParseTuple(args, "S:intern", &s))
Guido van Rossume8811f81997-02-14 15:48:05 +00001133 return NULL;
Jeremy Hylton4c989dd2004-08-07 19:20:05 +00001134 if (!PyString_CheckExact(s)) {
1135 PyErr_SetString(PyExc_TypeError,
1136 "can't intern subclass of string");
1137 return NULL;
1138 }
Guido van Rossume8811f81997-02-14 15:48:05 +00001139 Py_INCREF(s);
1140 PyString_InternInPlace(&s);
1141 return s;
1142}
1143
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001144PyDoc_STRVAR(intern_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001145"intern(string) -> string\n\
1146\n\
1147``Intern'' the given string. This enters the string in the (global)\n\
1148table of interned strings whose purpose is to speed up dictionary lookups.\n\
1149Return the string itself or the previously interned string object with the\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001150same value.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001151
1152
Guido van Rossum79f25d91997-04-29 20:08:16 +00001153static PyObject *
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001154builtin_iter(PyObject *self, PyObject *args)
1155{
1156 PyObject *v, *w = NULL;
1157
Raymond Hettingerea3fdf42002-12-29 16:33:45 +00001158 if (!PyArg_UnpackTuple(args, "iter", 1, 2, &v, &w))
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001159 return NULL;
1160 if (w == NULL)
1161 return PyObject_GetIter(v);
1162 if (!PyCallable_Check(v)) {
1163 PyErr_SetString(PyExc_TypeError,
1164 "iter(v, w): v must be callable");
1165 return NULL;
1166 }
1167 return PyCallIter_New(v, w);
1168}
1169
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001170PyDoc_STRVAR(iter_doc,
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001171"iter(collection) -> iterator\n\
1172iter(callable, sentinel) -> iterator\n\
1173\n\
1174Get an iterator from an object. In the first form, the argument must\n\
1175supply its own iterator, or be a sequence.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001176In the second form, the callable is called until it returns the sentinel.");
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001177
1178
1179static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001180builtin_len(PyObject *self, PyObject *v)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001181{
Martin v. Löwis18e16552006-02-15 17:27:45 +00001182 Py_ssize_t res;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001183
Jeremy Hylton03657cf2000-07-12 13:05:33 +00001184 res = PyObject_Size(v);
Guido van Rossum8ea9f4d1998-06-29 22:26:50 +00001185 if (res < 0 && PyErr_Occurred())
1186 return NULL;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001187 return PyInt_FromSsize_t(res);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001188}
1189
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001190PyDoc_STRVAR(len_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001191"len(object) -> integer\n\
1192\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001193Return the number of items of a sequence or mapping.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001194
1195
Guido van Rossum79f25d91997-04-29 20:08:16 +00001196static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001197builtin_locals(PyObject *self)
Guido van Rossum872537c1995-07-07 22:43:42 +00001198{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001199 PyObject *d;
Guido van Rossum872537c1995-07-07 22:43:42 +00001200
Guido van Rossum79f25d91997-04-29 20:08:16 +00001201 d = PyEval_GetLocals();
Neal Norwitz43bd4db2006-08-12 01:46:42 +00001202 Py_XINCREF(d);
Guido van Rossum872537c1995-07-07 22:43:42 +00001203 return d;
1204}
1205
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001206PyDoc_STRVAR(locals_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001207"locals() -> dictionary\n\
1208\n\
Raymond Hettinger69bf8f32003-01-04 02:16:22 +00001209Update and return a dictionary containing the current scope's local variables.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001210
1211
Guido van Rossum79f25d91997-04-29 20:08:16 +00001212static PyObject *
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001213min_max(PyObject *args, PyObject *kwds, int op)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001214{
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001215 PyObject *v, *it, *item, *val, *maxitem, *maxval, *keyfunc=NULL;
Martin v. Löwisfd39ad42004-08-12 14:42:37 +00001216 const char *name = op == Py_LT ? "min" : "max";
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001217
Guido van Rossum79f25d91997-04-29 20:08:16 +00001218 if (PyTuple_Size(args) > 1)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001219 v = args;
Martin v. Löwisfd39ad42004-08-12 14:42:37 +00001220 else if (!PyArg_UnpackTuple(args, (char *)name, 1, 1, &v))
Guido van Rossum3f5da241990-12-20 15:06:42 +00001221 return NULL;
Tim Peters67d687a2002-04-29 21:27:32 +00001222
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001223 if (kwds != NULL && PyDict_Check(kwds) && PyDict_Size(kwds)) {
1224 keyfunc = PyDict_GetItemString(kwds, "key");
1225 if (PyDict_Size(kwds)!=1 || keyfunc == NULL) {
Georg Brandl99d7e4e2005-08-31 22:21:15 +00001226 PyErr_Format(PyExc_TypeError,
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001227 "%s() got an unexpected keyword argument", name);
1228 return NULL;
1229 }
Georg Brandl99d7e4e2005-08-31 22:21:15 +00001230 }
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001231
Tim Petersc3074532001-05-03 07:00:32 +00001232 it = PyObject_GetIter(v);
1233 if (it == NULL)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001234 return NULL;
Tim Petersc3074532001-05-03 07:00:32 +00001235
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001236 maxitem = NULL; /* the result */
1237 maxval = NULL; /* the value associated with the result */
Brett Cannon9e635cf2004-12-07 00:25:35 +00001238 while (( item = PyIter_Next(it) )) {
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001239 /* get the value from the key function */
1240 if (keyfunc != NULL) {
1241 val = PyObject_CallFunctionObjArgs(keyfunc, item, NULL);
1242 if (val == NULL)
1243 goto Fail_it_item;
1244 }
1245 /* no key function; the value is the item */
1246 else {
1247 val = item;
1248 Py_INCREF(val);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001249 }
Tim Petersc3074532001-05-03 07:00:32 +00001250
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001251 /* maximum value and item are unset; set them */
1252 if (maxval == NULL) {
1253 maxitem = item;
1254 maxval = val;
1255 }
1256 /* maximum value and item are set; update them as necessary */
Guido van Rossum2d951851994-08-29 12:52:16 +00001257 else {
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001258 int cmp = PyObject_RichCompareBool(val, maxval, op);
1259 if (cmp < 0)
1260 goto Fail_it_item_and_val;
1261 else if (cmp > 0) {
1262 Py_DECREF(maxval);
1263 Py_DECREF(maxitem);
1264 maxval = val;
1265 maxitem = item;
Guido van Rossum53451b32001-01-17 15:47:24 +00001266 }
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001267 else {
1268 Py_DECREF(item);
1269 Py_DECREF(val);
Guido van Rossumc8b6df91997-05-23 00:06:51 +00001270 }
Guido van Rossum2d951851994-08-29 12:52:16 +00001271 }
Guido van Rossum3f5da241990-12-20 15:06:42 +00001272 }
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001273 if (PyErr_Occurred())
1274 goto Fail_it;
1275 if (maxval == NULL) {
Martin v. Löwisfd39ad42004-08-12 14:42:37 +00001276 PyErr_Format(PyExc_ValueError,
1277 "%s() arg is an empty sequence", name);
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001278 assert(maxitem == NULL);
1279 }
1280 else
1281 Py_DECREF(maxval);
Tim Petersc3074532001-05-03 07:00:32 +00001282 Py_DECREF(it);
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001283 return maxitem;
1284
1285Fail_it_item_and_val:
1286 Py_DECREF(val);
1287Fail_it_item:
1288 Py_DECREF(item);
1289Fail_it:
1290 Py_XDECREF(maxval);
1291 Py_XDECREF(maxitem);
1292 Py_DECREF(it);
1293 return NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001294}
1295
Guido van Rossum79f25d91997-04-29 20:08:16 +00001296static PyObject *
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001297builtin_min(PyObject *self, PyObject *args, PyObject *kwds)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001298{
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001299 return min_max(args, kwds, Py_LT);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001300}
1301
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001302PyDoc_STRVAR(min_doc,
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001303"min(iterable[, key=func]) -> value\n\
1304min(a, b, c, ...[, key=func]) -> value\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001305\n\
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001306With a single iterable argument, return its smallest item.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001307With two or more arguments, return the smallest argument.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001308
1309
Guido van Rossum79f25d91997-04-29 20:08:16 +00001310static PyObject *
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001311builtin_max(PyObject *self, PyObject *args, PyObject *kwds)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001312{
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001313 return min_max(args, kwds, Py_GT);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001314}
1315
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001316PyDoc_STRVAR(max_doc,
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001317"max(iterable[, key=func]) -> value\n\
1318max(a, b, c, ...[, key=func]) -> value\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001319\n\
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001320With a single iterable argument, return its largest item.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001321With two or more arguments, return the largest argument.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001322
1323
Guido van Rossum79f25d91997-04-29 20:08:16 +00001324static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001325builtin_oct(PyObject *self, PyObject *v)
Guido van Rossum006bcd41991-10-24 14:54:44 +00001326{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001327 PyNumberMethods *nb;
Neil Schemenauer3a313e32004-07-19 16:29:17 +00001328 PyObject *res;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001329
Guido van Rossum1899c2e1992-09-12 11:09:23 +00001330 if (v == NULL || (nb = v->ob_type->tp_as_number) == NULL ||
1331 nb->nb_oct == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001332 PyErr_SetString(PyExc_TypeError,
Guido van Rossum1899c2e1992-09-12 11:09:23 +00001333 "oct() argument can't be converted to oct");
1334 return NULL;
Guido van Rossum006bcd41991-10-24 14:54:44 +00001335 }
Neil Schemenauer3a313e32004-07-19 16:29:17 +00001336 res = (*nb->nb_oct)(v);
1337 if (res && !PyString_Check(res)) {
1338 PyErr_Format(PyExc_TypeError,
1339 "__oct__ returned non-string (type %.200s)",
1340 res->ob_type->tp_name);
1341 Py_DECREF(res);
1342 return NULL;
1343 }
1344 return res;
Guido van Rossum006bcd41991-10-24 14:54:44 +00001345}
1346
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001347PyDoc_STRVAR(oct_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001348"oct(number) -> string\n\
1349\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001350Return the octal representation of an integer or long integer.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001351
1352
Guido van Rossum79f25d91997-04-29 20:08:16 +00001353static PyObject *
Neal Norwitzc4edb0e2006-05-02 04:43:14 +00001354builtin_open(PyObject *self, PyObject *args, PyObject *kwds)
1355{
1356 return PyObject_Call((PyObject*)&PyFile_Type, args, kwds);
1357}
1358
1359PyDoc_STRVAR(open_doc,
1360"open(name[, mode[, buffering]]) -> file object\n\
1361\n\
1362Open a file using the file() type, returns a file object.");
1363
1364
1365static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001366builtin_ord(PyObject *self, PyObject* obj)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001367{
Guido van Rossum09095f32000-03-10 23:00:52 +00001368 long ord;
Martin v. Löwisd96ee902006-02-16 14:37:16 +00001369 Py_ssize_t size;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001370
Jeremy Hylton394b54d2000-04-12 21:19:47 +00001371 if (PyString_Check(obj)) {
1372 size = PyString_GET_SIZE(obj);
Andrew M. Kuchling34c20cf2000-12-20 14:36:56 +00001373 if (size == 1) {
Jeremy Hylton394b54d2000-04-12 21:19:47 +00001374 ord = (long)((unsigned char)*PyString_AS_STRING(obj));
Andrew M. Kuchling34c20cf2000-12-20 14:36:56 +00001375 return PyInt_FromLong(ord);
1376 }
Martin v. Löwis339d0f72001-08-17 18:39:25 +00001377#ifdef Py_USING_UNICODE
Jeremy Hylton394b54d2000-04-12 21:19:47 +00001378 } else if (PyUnicode_Check(obj)) {
1379 size = PyUnicode_GET_SIZE(obj);
Andrew M. Kuchling34c20cf2000-12-20 14:36:56 +00001380 if (size == 1) {
Jeremy Hylton394b54d2000-04-12 21:19:47 +00001381 ord = (long)*PyUnicode_AS_UNICODE(obj);
Andrew M. Kuchling34c20cf2000-12-20 14:36:56 +00001382 return PyInt_FromLong(ord);
1383 }
Martin v. Löwis339d0f72001-08-17 18:39:25 +00001384#endif
Jeremy Hylton394b54d2000-04-12 21:19:47 +00001385 } else {
1386 PyErr_Format(PyExc_TypeError,
Andrew M. Kuchlingf07aad12000-12-23 14:11:28 +00001387 "ord() expected string of length 1, but " \
Jeremy Hylton394b54d2000-04-12 21:19:47 +00001388 "%.200s found", obj->ob_type->tp_name);
Guido van Rossum09095f32000-03-10 23:00:52 +00001389 return NULL;
1390 }
1391
Guido van Rossumad991772001-01-12 16:03:05 +00001392 PyErr_Format(PyExc_TypeError,
Andrew M. Kuchlingf07aad12000-12-23 14:11:28 +00001393 "ord() expected a character, "
Martin v. Löwisd96ee902006-02-16 14:37:16 +00001394 "but string of length %zd found",
Jeremy Hylton394b54d2000-04-12 21:19:47 +00001395 size);
1396 return NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001397}
1398
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001399PyDoc_STRVAR(ord_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001400"ord(c) -> integer\n\
1401\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001402Return the integer ordinal of a one-character string.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001403
1404
Guido van Rossum79f25d91997-04-29 20:08:16 +00001405static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001406builtin_pow(PyObject *self, PyObject *args)
Guido van Rossum6a00cd81995-01-07 12:39:01 +00001407{
Guido van Rossum09df08a1998-05-22 00:51:39 +00001408 PyObject *v, *w, *z = Py_None;
Guido van Rossum6a00cd81995-01-07 12:39:01 +00001409
Raymond Hettingerea3fdf42002-12-29 16:33:45 +00001410 if (!PyArg_UnpackTuple(args, "pow", 2, 3, &v, &w, &z))
Guido van Rossum6a00cd81995-01-07 12:39:01 +00001411 return NULL;
Guido van Rossum09df08a1998-05-22 00:51:39 +00001412 return PyNumber_Power(v, w, z);
Guido van Rossumd4905451991-05-05 20:00:36 +00001413}
1414
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001415PyDoc_STRVAR(pow_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001416"pow(x, y[, z]) -> number\n\
1417\n\
1418With two arguments, equivalent to x**y. With three arguments,\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001419equivalent to (x**y) % z, but may be more efficient (e.g. for longs).");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001420
1421
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001422
1423/* Return number of items in range (lo, hi, step), when arguments are
1424 * PyInt or PyLong objects. step > 0 required. Return a value < 0 if
1425 * & only if the true value is too large to fit in a signed long.
1426 * Arguments MUST return 1 with either PyInt_Check() or
1427 * PyLong_Check(). Return -1 when there is an error.
1428 */
1429static long
1430get_len_of_range_longs(PyObject *lo, PyObject *hi, PyObject *step)
1431{
1432 /* -------------------------------------------------------------
1433 Algorithm is equal to that of get_len_of_range(), but it operates
1434 on PyObjects (which are assumed to be PyLong or PyInt objects).
1435 ---------------------------------------------------------------*/
1436 long n;
1437 PyObject *diff = NULL;
1438 PyObject *one = NULL;
1439 PyObject *tmp1 = NULL, *tmp2 = NULL, *tmp3 = NULL;
1440 /* holds sub-expression evaluations */
1441
1442 /* if (lo >= hi), return length of 0. */
1443 if (PyObject_Compare(lo, hi) >= 0)
1444 return 0;
1445
1446 if ((one = PyLong_FromLong(1L)) == NULL)
1447 goto Fail;
1448
1449 if ((tmp1 = PyNumber_Subtract(hi, lo)) == NULL)
1450 goto Fail;
1451
1452 if ((diff = PyNumber_Subtract(tmp1, one)) == NULL)
1453 goto Fail;
1454
1455 if ((tmp2 = PyNumber_FloorDivide(diff, step)) == NULL)
1456 goto Fail;
1457
1458 if ((tmp3 = PyNumber_Add(tmp2, one)) == NULL)
1459 goto Fail;
1460
1461 n = PyLong_AsLong(tmp3);
1462 if (PyErr_Occurred()) { /* Check for Overflow */
1463 PyErr_Clear();
1464 goto Fail;
1465 }
1466
1467 Py_DECREF(tmp3);
1468 Py_DECREF(tmp2);
1469 Py_DECREF(diff);
1470 Py_DECREF(tmp1);
1471 Py_DECREF(one);
1472 return n;
1473
1474 Fail:
1475 Py_XDECREF(tmp3);
1476 Py_XDECREF(tmp2);
1477 Py_XDECREF(diff);
1478 Py_XDECREF(tmp1);
1479 Py_XDECREF(one);
1480 return -1;
1481}
1482
1483/* An extension of builtin_range() that handles the case when PyLong
1484 * arguments are given. */
1485static PyObject *
1486handle_range_longs(PyObject *self, PyObject *args)
1487{
1488 PyObject *ilow;
Tim Peters874e1f72003-04-13 22:13:08 +00001489 PyObject *ihigh = NULL;
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001490 PyObject *istep = NULL;
Tim Peters874e1f72003-04-13 22:13:08 +00001491
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001492 PyObject *curnum = NULL;
1493 PyObject *v = NULL;
1494 long bign;
1495 int i, n;
1496 int cmp_result;
1497
Tim Peters874e1f72003-04-13 22:13:08 +00001498 PyObject *zero = PyLong_FromLong(0);
1499
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001500 if (zero == NULL)
1501 return NULL;
1502
Tim Peters874e1f72003-04-13 22:13:08 +00001503 if (!PyArg_UnpackTuple(args, "range", 1, 3, &ilow, &ihigh, &istep)) {
1504 Py_DECREF(zero);
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001505 return NULL;
1506 }
1507
Tim Peters874e1f72003-04-13 22:13:08 +00001508 /* Figure out which way we were called, supply defaults, and be
1509 * sure to incref everything so that the decrefs at the end
1510 * are correct.
1511 */
1512 assert(ilow != NULL);
1513 if (ihigh == NULL) {
1514 /* only 1 arg -- it's the upper limit */
1515 ihigh = ilow;
1516 ilow = NULL;
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001517 }
Tim Peters874e1f72003-04-13 22:13:08 +00001518 assert(ihigh != NULL);
1519 Py_INCREF(ihigh);
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001520
Tim Peters874e1f72003-04-13 22:13:08 +00001521 /* ihigh correct now; do ilow */
1522 if (ilow == NULL)
1523 ilow = zero;
1524 Py_INCREF(ilow);
1525
1526 /* ilow and ihigh correct now; do istep */
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001527 if (istep == NULL) {
1528 istep = PyLong_FromLong(1L);
1529 if (istep == NULL)
1530 goto Fail;
1531 }
1532 else {
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001533 Py_INCREF(istep);
1534 }
1535
Tim Peters874e1f72003-04-13 22:13:08 +00001536 if (!PyInt_Check(ilow) && !PyLong_Check(ilow)) {
Guido van Rossum28e83e32003-04-15 12:43:26 +00001537 PyErr_Format(PyExc_TypeError,
Alex Martellif471d472003-04-23 13:00:44 +00001538 "range() integer start argument expected, got %s.",
Guido van Rossum817d6c92003-04-14 18:25:04 +00001539 ilow->ob_type->tp_name);
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001540 goto Fail;
1541 }
1542
Tim Peters874e1f72003-04-13 22:13:08 +00001543 if (!PyInt_Check(ihigh) && !PyLong_Check(ihigh)) {
Guido van Rossum28e83e32003-04-15 12:43:26 +00001544 PyErr_Format(PyExc_TypeError,
Alex Martellif471d472003-04-23 13:00:44 +00001545 "range() integer end argument expected, got %s.",
Guido van Rossum817d6c92003-04-14 18:25:04 +00001546 ihigh->ob_type->tp_name);
Tim Peters874e1f72003-04-13 22:13:08 +00001547 goto Fail;
1548 }
1549
1550 if (!PyInt_Check(istep) && !PyLong_Check(istep)) {
Guido van Rossum28e83e32003-04-15 12:43:26 +00001551 PyErr_Format(PyExc_TypeError,
Alex Martellif471d472003-04-23 13:00:44 +00001552 "range() integer step argument expected, got %s.",
Guido van Rossum817d6c92003-04-14 18:25:04 +00001553 istep->ob_type->tp_name);
Tim Peters874e1f72003-04-13 22:13:08 +00001554 goto Fail;
1555 }
1556
1557 if (PyObject_Cmp(istep, zero, &cmp_result) == -1)
1558 goto Fail;
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001559 if (cmp_result == 0) {
1560 PyErr_SetString(PyExc_ValueError,
Alex Martellif471d472003-04-23 13:00:44 +00001561 "range() step argument must not be zero");
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001562 goto Fail;
1563 }
1564
1565 if (cmp_result > 0)
1566 bign = get_len_of_range_longs(ilow, ihigh, istep);
1567 else {
1568 PyObject *neg_istep = PyNumber_Negative(istep);
1569 if (neg_istep == NULL)
1570 goto Fail;
1571 bign = get_len_of_range_longs(ihigh, ilow, neg_istep);
1572 Py_DECREF(neg_istep);
1573 }
1574
1575 n = (int)bign;
1576 if (bign < 0 || (long)n != bign) {
1577 PyErr_SetString(PyExc_OverflowError,
1578 "range() result has too many items");
1579 goto Fail;
1580 }
1581
1582 v = PyList_New(n);
1583 if (v == NULL)
1584 goto Fail;
1585
1586 curnum = ilow;
1587 Py_INCREF(curnum);
1588
1589 for (i = 0; i < n; i++) {
1590 PyObject *w = PyNumber_Long(curnum);
1591 PyObject *tmp_num;
1592 if (w == NULL)
1593 goto Fail;
1594
1595 PyList_SET_ITEM(v, i, w);
1596
1597 tmp_num = PyNumber_Add(curnum, istep);
1598 if (tmp_num == NULL)
1599 goto Fail;
1600
1601 Py_DECREF(curnum);
1602 curnum = tmp_num;
1603 }
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_DECREF(istep);
1607 Py_DECREF(zero);
Tim Peters874e1f72003-04-13 22:13:08 +00001608 Py_DECREF(curnum);
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001609 return v;
1610
1611 Fail:
Tim Peters874e1f72003-04-13 22:13:08 +00001612 Py_DECREF(ilow);
1613 Py_DECREF(ihigh);
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001614 Py_XDECREF(istep);
Tim Peters874e1f72003-04-13 22:13:08 +00001615 Py_DECREF(zero);
1616 Py_XDECREF(curnum);
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001617 Py_XDECREF(v);
1618 return NULL;
1619}
1620
Guido van Rossum124eff01999-02-23 16:11:01 +00001621/* Return number of items in range/xrange (lo, hi, step). step > 0
1622 * required. Return a value < 0 if & only if the true value is too
1623 * large to fit in a signed long.
1624 */
1625static long
Thomas Wouterse28c2962000-07-23 22:21:32 +00001626get_len_of_range(long lo, long hi, long step)
Guido van Rossum124eff01999-02-23 16:11:01 +00001627{
1628 /* -------------------------------------------------------------
1629 If lo >= hi, the range is empty.
1630 Else if n values are in the range, the last one is
1631 lo + (n-1)*step, which must be <= hi-1. Rearranging,
1632 n <= (hi - lo - 1)/step + 1, so taking the floor of the RHS gives
1633 the proper value. Since lo < hi in this case, hi-lo-1 >= 0, so
1634 the RHS is non-negative and so truncation is the same as the
1635 floor. Letting M be the largest positive long, the worst case
1636 for the RHS numerator is hi=M, lo=-M-1, and then
1637 hi-lo-1 = M-(-M-1)-1 = 2*M. Therefore unsigned long has enough
1638 precision to compute the RHS exactly.
1639 ---------------------------------------------------------------*/
1640 long n = 0;
1641 if (lo < hi) {
1642 unsigned long uhi = (unsigned long)hi;
1643 unsigned long ulo = (unsigned long)lo;
1644 unsigned long diff = uhi - ulo - 1;
1645 n = (long)(diff / (unsigned long)step + 1);
1646 }
1647 return n;
1648}
1649
Guido van Rossum79f25d91997-04-29 20:08:16 +00001650static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001651builtin_range(PyObject *self, PyObject *args)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001652{
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001653 long ilow = 0, ihigh = 0, istep = 1;
Guido van Rossum124eff01999-02-23 16:11:01 +00001654 long bign;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001655 int i, n;
Guido van Rossum124eff01999-02-23 16:11:01 +00001656
Guido van Rossum79f25d91997-04-29 20:08:16 +00001657 PyObject *v;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001658
Guido van Rossum79f25d91997-04-29 20:08:16 +00001659 if (PyTuple_Size(args) <= 1) {
1660 if (!PyArg_ParseTuple(args,
Guido van Rossum0865dd91995-01-17 16:30:22 +00001661 "l;range() requires 1-3 int arguments",
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001662 &ihigh)) {
1663 PyErr_Clear();
1664 return handle_range_longs(self, args);
1665 }
Guido van Rossum3f5da241990-12-20 15:06:42 +00001666 }
1667 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001668 if (!PyArg_ParseTuple(args,
Guido van Rossum0865dd91995-01-17 16:30:22 +00001669 "ll|l;range() requires 1-3 int arguments",
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001670 &ilow, &ihigh, &istep)) {
1671 PyErr_Clear();
1672 return handle_range_longs(self, args);
1673 }
Guido van Rossum3f5da241990-12-20 15:06:42 +00001674 }
1675 if (istep == 0) {
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001676 PyErr_SetString(PyExc_ValueError,
Alex Martellif471d472003-04-23 13:00:44 +00001677 "range() step argument must not be zero");
Guido van Rossum3f5da241990-12-20 15:06:42 +00001678 return NULL;
1679 }
Guido van Rossum124eff01999-02-23 16:11:01 +00001680 if (istep > 0)
1681 bign = get_len_of_range(ilow, ihigh, istep);
1682 else
1683 bign = get_len_of_range(ihigh, ilow, -istep);
1684 n = (int)bign;
1685 if (bign < 0 || (long)n != bign) {
Guido van Rossume23cde21999-01-12 05:07:47 +00001686 PyErr_SetString(PyExc_OverflowError,
Fred Drake661ea262000-10-24 19:57:45 +00001687 "range() result has too many items");
Guido van Rossume23cde21999-01-12 05:07:47 +00001688 return NULL;
1689 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00001690 v = PyList_New(n);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001691 if (v == NULL)
1692 return NULL;
1693 for (i = 0; i < n; i++) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001694 PyObject *w = PyInt_FromLong(ilow);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001695 if (w == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001696 Py_DECREF(v);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001697 return NULL;
1698 }
Guido van Rossuma937d141998-04-24 18:22:02 +00001699 PyList_SET_ITEM(v, i, w);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001700 ilow += istep;
1701 }
1702 return v;
1703}
1704
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001705PyDoc_STRVAR(range_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001706"range([start,] stop[, step]) -> list of integers\n\
1707\n\
1708Return a list containing an arithmetic progression of integers.\n\
1709range(i, j) returns [i, i+1, i+2, ..., j-1]; start (!) defaults to 0.\n\
1710When step is given, it specifies the increment (or decrement).\n\
1711For example, range(4) returns [0, 1, 2, 3]. The end point is omitted!\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001712These are exactly the valid indices for a list of 4 elements.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001713
1714
Guido van Rossum79f25d91997-04-29 20:08:16 +00001715static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001716builtin_raw_input(PyObject *self, PyObject *args)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001717{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001718 PyObject *v = NULL;
Martin v. Löwis31d2df52002-08-14 15:46:02 +00001719 PyObject *fin = PySys_GetObject("stdin");
1720 PyObject *fout = PySys_GetObject("stdout");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001721
Raymond Hettingerea3fdf42002-12-29 16:33:45 +00001722 if (!PyArg_UnpackTuple(args, "[raw_]input", 0, 1, &v))
Guido van Rossum3165fe61992-09-25 21:59:05 +00001723 return NULL;
Martin v. Löwis31d2df52002-08-14 15:46:02 +00001724
1725 if (fin == NULL) {
Alex Martellia9b9c9f2003-04-23 13:34:35 +00001726 PyErr_SetString(PyExc_RuntimeError, "[raw_]input: lost sys.stdin");
Martin v. Löwis31d2df52002-08-14 15:46:02 +00001727 return NULL;
1728 }
1729 if (fout == NULL) {
Alex Martellia9b9c9f2003-04-23 13:34:35 +00001730 PyErr_SetString(PyExc_RuntimeError, "[raw_]input: lost sys.stdout");
Martin v. Löwis31d2df52002-08-14 15:46:02 +00001731 return NULL;
1732 }
1733 if (PyFile_SoftSpace(fout, 0)) {
1734 if (PyFile_WriteString(" ", fout) != 0)
1735 return NULL;
1736 }
Georg Brandl7e3ba2a2006-08-06 08:23:54 +00001737 if (PyFile_AsFile(fin) && PyFile_AsFile(fout)
Martin v. Löwis566f6af2002-10-26 14:39:10 +00001738 && isatty(fileno(PyFile_AsFile(fin)))
1739 && isatty(fileno(PyFile_AsFile(fout)))) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001740 PyObject *po;
Guido van Rossum872537c1995-07-07 22:43:42 +00001741 char *prompt;
1742 char *s;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001743 PyObject *result;
Guido van Rossum872537c1995-07-07 22:43:42 +00001744 if (v != NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001745 po = PyObject_Str(v);
Guido van Rossum872537c1995-07-07 22:43:42 +00001746 if (po == NULL)
1747 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001748 prompt = PyString_AsString(po);
Guido van Rossumd9b52081998-06-26 18:25:38 +00001749 if (prompt == NULL)
1750 return NULL;
Guido van Rossum872537c1995-07-07 22:43:42 +00001751 }
1752 else {
1753 po = NULL;
1754 prompt = "";
1755 }
Michael W. Hudson52db5192004-08-02 13:21:09 +00001756 s = PyOS_Readline(PyFile_AsFile(fin), PyFile_AsFile(fout),
Martin v. Löwis566f6af2002-10-26 14:39:10 +00001757 prompt);
Guido van Rossum79f25d91997-04-29 20:08:16 +00001758 Py_XDECREF(po);
Guido van Rossum872537c1995-07-07 22:43:42 +00001759 if (s == NULL) {
Michael W. Hudson30ea2f22004-07-07 17:44:12 +00001760 if (!PyErr_Occurred())
1761 PyErr_SetNone(PyExc_KeyboardInterrupt);
Guido van Rossum872537c1995-07-07 22:43:42 +00001762 return NULL;
1763 }
1764 if (*s == '\0') {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001765 PyErr_SetNone(PyExc_EOFError);
Guido van Rossum872537c1995-07-07 22:43:42 +00001766 result = NULL;
1767 }
1768 else { /* strip trailing '\n' */
Guido van Rossum106f2da2000-06-28 21:12:25 +00001769 size_t len = strlen(s);
Martin v. Löwisb1ed7fa2006-04-13 07:52:27 +00001770 if (len > PY_SSIZE_T_MAX) {
Martin v. Löwis31d2df52002-08-14 15:46:02 +00001771 PyErr_SetString(PyExc_OverflowError,
Alex Martellia9b9c9f2003-04-23 13:34:35 +00001772 "[raw_]input: input too long");
Guido van Rossum106f2da2000-06-28 21:12:25 +00001773 result = NULL;
1774 }
1775 else {
Martin v. Löwisb1ed7fa2006-04-13 07:52:27 +00001776 result = PyString_FromStringAndSize(s, len-1);
Guido van Rossum106f2da2000-06-28 21:12:25 +00001777 }
Guido van Rossum872537c1995-07-07 22:43:42 +00001778 }
Guido van Rossumb18618d2000-05-03 23:44:39 +00001779 PyMem_FREE(s);
Guido van Rossum872537c1995-07-07 22:43:42 +00001780 return result;
1781 }
Guido van Rossum90933611991-06-07 16:10:43 +00001782 if (v != NULL) {
Martin v. Löwis31d2df52002-08-14 15:46:02 +00001783 if (PyFile_WriteObject(v, fout, Py_PRINT_RAW) != 0)
Guido van Rossum90933611991-06-07 16:10:43 +00001784 return NULL;
1785 }
Martin v. Löwis31d2df52002-08-14 15:46:02 +00001786 return PyFile_GetLine(fin, -1);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001787}
1788
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001789PyDoc_STRVAR(raw_input_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001790"raw_input([prompt]) -> string\n\
1791\n\
1792Read a string from standard input. The trailing newline is stripped.\n\
1793If the user hits EOF (Unix: Ctl-D, Windows: Ctl-Z+Return), raise EOFError.\n\
1794On Unix, GNU readline is used if enabled. The prompt string, if given,\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001795is printed without a trailing newline before reading.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001796
1797
Guido van Rossum79f25d91997-04-29 20:08:16 +00001798static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001799builtin_reduce(PyObject *self, PyObject *args)
Guido van Rossum12d12c51993-10-26 17:58:25 +00001800{
Tim Peters15d81ef2001-05-04 04:39:21 +00001801 PyObject *seq, *func, *result = NULL, *it;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001802
Raymond Hettingerea3fdf42002-12-29 16:33:45 +00001803 if (!PyArg_UnpackTuple(args, "reduce", 2, 3, &func, &seq, &result))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001804 return NULL;
1805 if (result != NULL)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001806 Py_INCREF(result);
Guido van Rossum12d12c51993-10-26 17:58:25 +00001807
Tim Peters15d81ef2001-05-04 04:39:21 +00001808 it = PyObject_GetIter(seq);
1809 if (it == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001810 PyErr_SetString(PyExc_TypeError,
Tim Peters15d81ef2001-05-04 04:39:21 +00001811 "reduce() arg 2 must support iteration");
1812 Py_XDECREF(result);
Guido van Rossum12d12c51993-10-26 17:58:25 +00001813 return NULL;
1814 }
1815
Guido van Rossum79f25d91997-04-29 20:08:16 +00001816 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
Tim Peters15d81ef2001-05-04 04:39:21 +00001819 for (;;) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001820 PyObject *op2;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001821
1822 if (args->ob_refcnt > 1) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001823 Py_DECREF(args);
1824 if ((args = PyTuple_New(2)) == NULL)
Guido van Rossum2d951851994-08-29 12:52:16 +00001825 goto Fail;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001826 }
1827
Tim Peters15d81ef2001-05-04 04:39:21 +00001828 op2 = PyIter_Next(it);
1829 if (op2 == NULL) {
Tim Petersf4848da2001-05-05 00:14:56 +00001830 if (PyErr_Occurred())
1831 goto Fail;
1832 break;
Guido van Rossum2d951851994-08-29 12:52:16 +00001833 }
Guido van Rossum12d12c51993-10-26 17:58:25 +00001834
Guido van Rossum2d951851994-08-29 12:52:16 +00001835 if (result == NULL)
1836 result = op2;
1837 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001838 PyTuple_SetItem(args, 0, result);
1839 PyTuple_SetItem(args, 1, op2);
1840 if ((result = PyEval_CallObject(func, args)) == NULL)
Guido van Rossum2d951851994-08-29 12:52:16 +00001841 goto Fail;
1842 }
Guido van Rossum12d12c51993-10-26 17:58:25 +00001843 }
1844
Guido van Rossum79f25d91997-04-29 20:08:16 +00001845 Py_DECREF(args);
Guido van Rossum12d12c51993-10-26 17:58:25 +00001846
Guido van Rossum2d951851994-08-29 12:52:16 +00001847 if (result == NULL)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001848 PyErr_SetString(PyExc_TypeError,
Fred Drake661ea262000-10-24 19:57:45 +00001849 "reduce() of empty sequence with no initial value");
Guido van Rossum2d951851994-08-29 12:52:16 +00001850
Tim Peters15d81ef2001-05-04 04:39:21 +00001851 Py_DECREF(it);
Guido van Rossum12d12c51993-10-26 17:58:25 +00001852 return result;
1853
Guido van Rossum2d951851994-08-29 12:52:16 +00001854Fail:
Guido van Rossum79f25d91997-04-29 20:08:16 +00001855 Py_XDECREF(args);
1856 Py_XDECREF(result);
Tim Peters15d81ef2001-05-04 04:39:21 +00001857 Py_DECREF(it);
Guido van Rossum12d12c51993-10-26 17:58:25 +00001858 return NULL;
1859}
1860
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001861PyDoc_STRVAR(reduce_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001862"reduce(function, sequence[, initial]) -> value\n\
1863\n\
1864Apply a function of two arguments cumulatively to the items of a sequence,\n\
1865from left to right, so as to reduce the sequence to a single value.\n\
1866For example, reduce(lambda x, y: x+y, [1, 2, 3, 4, 5]) calculates\n\
1867((((1+2)+3)+4)+5). If initial is present, it is placed before the items\n\
1868of the sequence in the calculation, and serves as a default when the\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001869sequence is empty.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001870
1871
Guido van Rossum79f25d91997-04-29 20:08:16 +00001872static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001873builtin_reload(PyObject *self, PyObject *v)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001874{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001875 return PyImport_ReloadModule(v);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001876}
1877
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001878PyDoc_STRVAR(reload_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001879"reload(module) -> module\n\
1880\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001881Reload the module. The module must have been successfully imported before.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001882
1883
Guido van Rossum79f25d91997-04-29 20:08:16 +00001884static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001885builtin_repr(PyObject *self, PyObject *v)
Guido van Rossumc89705d1992-11-26 08:54:07 +00001886{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001887 return PyObject_Repr(v);
Guido van Rossumc89705d1992-11-26 08:54:07 +00001888}
1889
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001890PyDoc_STRVAR(repr_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001891"repr(object) -> string\n\
1892\n\
1893Return the canonical string representation of the object.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001894For most object types, eval(repr(object)) == object.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001895
1896
Guido van Rossum79f25d91997-04-29 20:08:16 +00001897static PyObject *
Georg Brandlccadf842006-03-31 18:54:53 +00001898builtin_round(PyObject *self, PyObject *args, PyObject *kwds)
Guido van Rossum9e51f9b1993-02-12 16:29:05 +00001899{
Georg Brandlccadf842006-03-31 18:54:53 +00001900 double number;
Guido van Rossum9e51f9b1993-02-12 16:29:05 +00001901 double f;
1902 int ndigits = 0;
Guido van Rossum9e51f9b1993-02-12 16:29:05 +00001903 int i;
Georg Brandlccadf842006-03-31 18:54:53 +00001904 static char *kwlist[] = {"number", "ndigits", 0};
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001905
Georg Brandlccadf842006-03-31 18:54:53 +00001906 if (!PyArg_ParseTupleAndKeywords(args, kwds, "d|i:round",
1907 kwlist, &number, &ndigits))
1908 return NULL;
Guido van Rossum9e51f9b1993-02-12 16:29:05 +00001909 f = 1.0;
Guido van Rossum1e162d31998-05-09 14:42:25 +00001910 i = abs(ndigits);
1911 while (--i >= 0)
Guido van Rossum9e51f9b1993-02-12 16:29:05 +00001912 f = f*10.0;
Guido van Rossum1e162d31998-05-09 14:42:25 +00001913 if (ndigits < 0)
Georg Brandlccadf842006-03-31 18:54:53 +00001914 number /= f;
Guido van Rossum9e51f9b1993-02-12 16:29:05 +00001915 else
Georg Brandlccadf842006-03-31 18:54:53 +00001916 number *= f;
1917 if (number >= 0.0)
1918 number = floor(number + 0.5);
Guido van Rossum1e162d31998-05-09 14:42:25 +00001919 else
Georg Brandlccadf842006-03-31 18:54:53 +00001920 number = ceil(number - 0.5);
Guido van Rossum1e162d31998-05-09 14:42:25 +00001921 if (ndigits < 0)
Georg Brandlccadf842006-03-31 18:54:53 +00001922 number *= f;
Guido van Rossum1e162d31998-05-09 14:42:25 +00001923 else
Georg Brandlccadf842006-03-31 18:54:53 +00001924 number /= f;
1925 return PyFloat_FromDouble(number);
Guido van Rossum9e51f9b1993-02-12 16:29:05 +00001926}
1927
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001928PyDoc_STRVAR(round_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001929"round(number[, ndigits]) -> floating point number\n\
1930\n\
1931Round a number to a given precision in decimal digits (default 0 digits).\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001932This always returns a floating point number. Precision may be negative.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001933
Raymond Hettinger64958a12003-12-17 20:43:33 +00001934static PyObject *
1935builtin_sorted(PyObject *self, PyObject *args, PyObject *kwds)
1936{
1937 PyObject *newlist, *v, *seq, *compare=NULL, *keyfunc=NULL, *newargs;
1938 PyObject *callable;
Martin v. Löwis15e62742006-02-27 16:46:16 +00001939 static char *kwlist[] = {"iterable", "cmp", "key", "reverse", 0};
Neal Norwitz6f0d4792005-12-15 06:40:36 +00001940 int reverse;
Raymond Hettinger64958a12003-12-17 20:43:33 +00001941
Neal Norwitz6f0d4792005-12-15 06:40:36 +00001942 /* args 1-4 should match listsort in Objects/listobject.c */
Armin Rigo71d7e702005-09-20 18:13:03 +00001943 if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|OOi:sorted",
1944 kwlist, &seq, &compare, &keyfunc, &reverse))
1945 return NULL;
Raymond Hettinger64958a12003-12-17 20:43:33 +00001946
1947 newlist = PySequence_List(seq);
1948 if (newlist == NULL)
1949 return NULL;
1950
1951 callable = PyObject_GetAttrString(newlist, "sort");
1952 if (callable == NULL) {
1953 Py_DECREF(newlist);
1954 return NULL;
1955 }
Georg Brandl99d7e4e2005-08-31 22:21:15 +00001956
Raymond Hettinger64958a12003-12-17 20:43:33 +00001957 newargs = PyTuple_GetSlice(args, 1, 4);
1958 if (newargs == NULL) {
1959 Py_DECREF(newlist);
1960 Py_DECREF(callable);
1961 return NULL;
1962 }
1963
1964 v = PyObject_Call(callable, newargs, kwds);
1965 Py_DECREF(newargs);
1966 Py_DECREF(callable);
1967 if (v == NULL) {
1968 Py_DECREF(newlist);
1969 return NULL;
1970 }
1971 Py_DECREF(v);
1972 return newlist;
1973}
1974
1975PyDoc_STRVAR(sorted_doc,
1976"sorted(iterable, cmp=None, key=None, reverse=False) --> new sorted list");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001977
Guido van Rossum79f25d91997-04-29 20:08:16 +00001978static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001979builtin_vars(PyObject *self, PyObject *args)
Guido van Rossum2d951851994-08-29 12:52:16 +00001980{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001981 PyObject *v = NULL;
1982 PyObject *d;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001983
Raymond Hettingerea3fdf42002-12-29 16:33:45 +00001984 if (!PyArg_UnpackTuple(args, "vars", 0, 1, &v))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001985 return NULL;
Guido van Rossum2d951851994-08-29 12:52:16 +00001986 if (v == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001987 d = PyEval_GetLocals();
Guido van Rossum53bb7ff1995-07-26 16:26:31 +00001988 if (d == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001989 if (!PyErr_Occurred())
1990 PyErr_SetString(PyExc_SystemError,
Alex Martellia9b9c9f2003-04-23 13:34:35 +00001991 "vars(): no locals!?");
Guido van Rossum53bb7ff1995-07-26 16:26:31 +00001992 }
1993 else
Guido van Rossum79f25d91997-04-29 20:08:16 +00001994 Py_INCREF(d);
Guido van Rossum2d951851994-08-29 12:52:16 +00001995 }
1996 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001997 d = PyObject_GetAttrString(v, "__dict__");
Guido van Rossum2d951851994-08-29 12:52:16 +00001998 if (d == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001999 PyErr_SetString(PyExc_TypeError,
Guido van Rossum2d951851994-08-29 12:52:16 +00002000 "vars() argument must have __dict__ attribute");
2001 return NULL;
2002 }
2003 }
2004 return d;
2005}
2006
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002007PyDoc_STRVAR(vars_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002008"vars([object]) -> dictionary\n\
2009\n\
2010Without arguments, equivalent to locals().\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002011With an argument, equivalent to object.__dict__.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002012
Alex Martellia70b1912003-04-22 08:12:33 +00002013
2014static PyObject*
2015builtin_sum(PyObject *self, PyObject *args)
2016{
2017 PyObject *seq;
2018 PyObject *result = NULL;
2019 PyObject *temp, *item, *iter;
2020
Raymond Hettinger5cf63942003-10-25 06:41:37 +00002021 if (!PyArg_UnpackTuple(args, "sum", 1, 2, &seq, &result))
Alex Martellia70b1912003-04-22 08:12:33 +00002022 return NULL;
2023
2024 iter = PyObject_GetIter(seq);
2025 if (iter == NULL)
2026 return NULL;
2027
2028 if (result == NULL) {
2029 result = PyInt_FromLong(0);
2030 if (result == NULL) {
2031 Py_DECREF(iter);
2032 return NULL;
2033 }
2034 } else {
2035 /* reject string values for 'start' parameter */
2036 if (PyObject_TypeCheck(result, &PyBaseString_Type)) {
2037 PyErr_SetString(PyExc_TypeError,
Alex Martellia9b9c9f2003-04-23 13:34:35 +00002038 "sum() can't sum strings [use ''.join(seq) instead]");
Alex Martellia70b1912003-04-22 08:12:33 +00002039 Py_DECREF(iter);
2040 return NULL;
2041 }
Alex Martelli41c9f882003-04-22 09:24:48 +00002042 Py_INCREF(result);
Alex Martellia70b1912003-04-22 08:12:33 +00002043 }
2044
2045 for(;;) {
2046 item = PyIter_Next(iter);
2047 if (item == NULL) {
2048 /* error, or end-of-sequence */
2049 if (PyErr_Occurred()) {
2050 Py_DECREF(result);
2051 result = NULL;
2052 }
2053 break;
2054 }
Alex Martellia253e182003-10-25 23:24:14 +00002055 temp = PyNumber_Add(result, item);
Alex Martellia70b1912003-04-22 08:12:33 +00002056 Py_DECREF(result);
2057 Py_DECREF(item);
2058 result = temp;
2059 if (result == NULL)
2060 break;
2061 }
2062 Py_DECREF(iter);
2063 return result;
2064}
2065
2066PyDoc_STRVAR(sum_doc,
Georg Brandl8134d062006-10-12 12:33:07 +00002067"sum(sequence[, start]) -> value\n\
Alex Martellia70b1912003-04-22 08:12:33 +00002068\n\
2069Returns the sum of a sequence of numbers (NOT strings) plus the value\n\
Georg Brandl8134d062006-10-12 12:33:07 +00002070of parameter 'start' (which defaults to 0). When the sequence is\n\
2071empty, returns start.");
Alex Martellia70b1912003-04-22 08:12:33 +00002072
2073
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002074static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002075builtin_isinstance(PyObject *self, PyObject *args)
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002076{
2077 PyObject *inst;
2078 PyObject *cls;
Guido van Rossum823649d2001-03-21 18:40:58 +00002079 int retval;
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002080
Raymond Hettingerea3fdf42002-12-29 16:33:45 +00002081 if (!PyArg_UnpackTuple(args, "isinstance", 2, 2, &inst, &cls))
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002082 return NULL;
Guido van Rossumf5dd9141997-12-02 19:11:45 +00002083
Guido van Rossum823649d2001-03-21 18:40:58 +00002084 retval = PyObject_IsInstance(inst, cls);
2085 if (retval < 0)
Guido van Rossumad991772001-01-12 16:03:05 +00002086 return NULL;
Guido van Rossum77f6a652002-04-03 22:41:51 +00002087 return PyBool_FromLong(retval);
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002088}
2089
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002090PyDoc_STRVAR(isinstance_doc,
Guido van Rossum77f6a652002-04-03 22:41:51 +00002091"isinstance(object, class-or-type-or-tuple) -> bool\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002092\n\
2093Return whether an object is an instance of a class or of a subclass thereof.\n\
Guido van Rossum03290ec2001-10-07 20:54:12 +00002094With a type as second argument, return whether that is the object's type.\n\
2095The form using a tuple, isinstance(x, (A, B, ...)), is a shortcut for\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002096isinstance(x, A) or isinstance(x, B) or ... (etc.).");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002097
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002098
2099static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002100builtin_issubclass(PyObject *self, PyObject *args)
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002101{
2102 PyObject *derived;
2103 PyObject *cls;
2104 int retval;
2105
Raymond Hettingerea3fdf42002-12-29 16:33:45 +00002106 if (!PyArg_UnpackTuple(args, "issubclass", 2, 2, &derived, &cls))
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002107 return NULL;
Guido van Rossum668213d1999-06-16 17:28:37 +00002108
Guido van Rossum823649d2001-03-21 18:40:58 +00002109 retval = PyObject_IsSubclass(derived, cls);
2110 if (retval < 0)
2111 return NULL;
Guido van Rossum77f6a652002-04-03 22:41:51 +00002112 return PyBool_FromLong(retval);
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002113}
2114
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002115PyDoc_STRVAR(issubclass_doc,
Guido van Rossum77f6a652002-04-03 22:41:51 +00002116"issubclass(C, B) -> bool\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002117\n\
Walter Dörwaldd9a6ad32002-12-12 16:41:44 +00002118Return whether class C is a subclass (i.e., a derived class) of class B.\n\
2119When using a tuple as the second argument issubclass(X, (A, B, ...)),\n\
2120is a shortcut for issubclass(X, A) or issubclass(X, B) or ... (etc.).");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002121
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002122
Barry Warsawbd599b52000-08-03 15:45:29 +00002123static PyObject*
2124builtin_zip(PyObject *self, PyObject *args)
2125{
2126 PyObject *ret;
Martin v. Löwisd96ee902006-02-16 14:37:16 +00002127 const Py_ssize_t itemsize = PySequence_Length(args);
2128 Py_ssize_t i;
Tim Peters8572b4f2001-05-06 01:05:02 +00002129 PyObject *itlist; /* tuple of iterators */
Martin v. Löwisd96ee902006-02-16 14:37:16 +00002130 Py_ssize_t len; /* guess at result length */
Barry Warsawbd599b52000-08-03 15:45:29 +00002131
Raymond Hettingereaef6152003-08-02 07:42:57 +00002132 if (itemsize == 0)
2133 return PyList_New(0);
2134
Barry Warsawbd599b52000-08-03 15:45:29 +00002135 /* args must be a tuple */
2136 assert(PyTuple_Check(args));
2137
Tim Peters39a86c22002-05-12 07:19:38 +00002138 /* Guess at result length: the shortest of the input lengths.
2139 If some argument refuses to say, we refuse to guess too, lest
2140 an argument like xrange(sys.maxint) lead us astray.*/
Tim Peters67d687a2002-04-29 21:27:32 +00002141 len = -1; /* unknown */
2142 for (i = 0; i < itemsize; ++i) {
2143 PyObject *item = PyTuple_GET_ITEM(args, i);
Martin v. Löwisd96ee902006-02-16 14:37:16 +00002144 Py_ssize_t thislen = _PyObject_LengthHint(item);
Tim Peters39a86c22002-05-12 07:19:38 +00002145 if (thislen < 0) {
Raymond Hettingera710b332005-08-21 11:03:59 +00002146 if (!PyErr_ExceptionMatches(PyExc_TypeError) &&
2147 !PyErr_ExceptionMatches(PyExc_AttributeError)) {
2148 return NULL;
2149 }
Tim Peters67d687a2002-04-29 21:27:32 +00002150 PyErr_Clear();
Tim Peters39a86c22002-05-12 07:19:38 +00002151 len = -1;
2152 break;
2153 }
Tim Peters67d687a2002-04-29 21:27:32 +00002154 else if (len < 0 || thislen < len)
2155 len = thislen;
2156 }
2157
Tim Peters8572b4f2001-05-06 01:05:02 +00002158 /* allocate result list */
Tim Peters67d687a2002-04-29 21:27:32 +00002159 if (len < 0)
2160 len = 10; /* arbitrary */
2161 if ((ret = PyList_New(len)) == NULL)
Barry Warsawbd599b52000-08-03 15:45:29 +00002162 return NULL;
2163
Tim Peters8572b4f2001-05-06 01:05:02 +00002164 /* obtain iterators */
2165 itlist = PyTuple_New(itemsize);
2166 if (itlist == NULL)
2167 goto Fail_ret;
2168 for (i = 0; i < itemsize; ++i) {
2169 PyObject *item = PyTuple_GET_ITEM(args, i);
2170 PyObject *it = PyObject_GetIter(item);
2171 if (it == NULL) {
2172 if (PyErr_ExceptionMatches(PyExc_TypeError))
2173 PyErr_Format(PyExc_TypeError,
Neal Norwitza361bd82006-02-19 19:31:50 +00002174 "zip argument #%zd must support iteration",
Tim Peters8572b4f2001-05-06 01:05:02 +00002175 i+1);
2176 goto Fail_ret_itlist;
Barry Warsawbd599b52000-08-03 15:45:29 +00002177 }
Tim Peters8572b4f2001-05-06 01:05:02 +00002178 PyTuple_SET_ITEM(itlist, i, it);
2179 }
Barry Warsawbd599b52000-08-03 15:45:29 +00002180
Tim Peters8572b4f2001-05-06 01:05:02 +00002181 /* build result into ret list */
Tim Peters67d687a2002-04-29 21:27:32 +00002182 for (i = 0; ; ++i) {
2183 int j;
Tim Peters8572b4f2001-05-06 01:05:02 +00002184 PyObject *next = PyTuple_New(itemsize);
2185 if (!next)
2186 goto Fail_ret_itlist;
2187
Tim Peters67d687a2002-04-29 21:27:32 +00002188 for (j = 0; j < itemsize; j++) {
2189 PyObject *it = PyTuple_GET_ITEM(itlist, j);
Tim Peters8572b4f2001-05-06 01:05:02 +00002190 PyObject *item = PyIter_Next(it);
Barry Warsawbd599b52000-08-03 15:45:29 +00002191 if (!item) {
Tim Peters8572b4f2001-05-06 01:05:02 +00002192 if (PyErr_Occurred()) {
2193 Py_DECREF(ret);
2194 ret = NULL;
Barry Warsawbd599b52000-08-03 15:45:29 +00002195 }
2196 Py_DECREF(next);
Tim Peters8572b4f2001-05-06 01:05:02 +00002197 Py_DECREF(itlist);
Tim Peters67d687a2002-04-29 21:27:32 +00002198 goto Done;
Barry Warsawbd599b52000-08-03 15:45:29 +00002199 }
Tim Peters67d687a2002-04-29 21:27:32 +00002200 PyTuple_SET_ITEM(next, j, item);
Barry Warsawbd599b52000-08-03 15:45:29 +00002201 }
Tim Peters8572b4f2001-05-06 01:05:02 +00002202
Tim Peters67d687a2002-04-29 21:27:32 +00002203 if (i < len)
2204 PyList_SET_ITEM(ret, i, next);
2205 else {
2206 int status = PyList_Append(ret, next);
2207 Py_DECREF(next);
2208 ++len;
2209 if (status < 0)
2210 goto Fail_ret_itlist;
2211 }
Barry Warsawbd599b52000-08-03 15:45:29 +00002212 }
Tim Peters8572b4f2001-05-06 01:05:02 +00002213
Tim Peters67d687a2002-04-29 21:27:32 +00002214Done:
2215 if (ret != NULL && i < len) {
2216 /* The list is too big. */
2217 if (PyList_SetSlice(ret, i, len, NULL) < 0)
2218 return NULL;
2219 }
2220 return ret;
2221
Tim Peters8572b4f2001-05-06 01:05:02 +00002222Fail_ret_itlist:
2223 Py_DECREF(itlist);
2224Fail_ret:
2225 Py_DECREF(ret);
2226 return NULL;
Barry Warsawbd599b52000-08-03 15:45:29 +00002227}
2228
2229
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002230PyDoc_STRVAR(zip_doc,
Barry Warsawbd599b52000-08-03 15:45:29 +00002231"zip(seq1 [, seq2 [...]]) -> [(seq1[0], seq2[0] ...), (...)]\n\
2232\n\
2233Return a list of tuples, where each tuple contains the i-th element\n\
2234from each of the argument sequences. The returned list is truncated\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002235in length to the length of the shortest argument sequence.");
Barry Warsawbd599b52000-08-03 15:45:29 +00002236
2237
Guido van Rossum79f25d91997-04-29 20:08:16 +00002238static PyMethodDef builtin_methods[] = {
Neal Norwitz92e212f2006-04-03 04:48:37 +00002239 {"__import__", (PyCFunction)builtin___import__, METH_VARARGS | METH_KEYWORDS, import_doc},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002240 {"abs", builtin_abs, METH_O, abs_doc},
Raymond Hettinger96229b12005-03-11 06:49:40 +00002241 {"all", builtin_all, METH_O, all_doc},
2242 {"any", builtin_any, METH_O, any_doc},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002243 {"apply", builtin_apply, METH_VARARGS, apply_doc},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002244 {"callable", builtin_callable, METH_O, callable_doc},
2245 {"chr", builtin_chr, METH_VARARGS, chr_doc},
2246 {"cmp", builtin_cmp, METH_VARARGS, cmp_doc},
2247 {"coerce", builtin_coerce, METH_VARARGS, coerce_doc},
Georg Brandl5240d742007-03-13 20:46:32 +00002248 {"compile", (PyCFunction)builtin_compile, METH_VARARGS | METH_KEYWORDS, compile_doc},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002249 {"delattr", builtin_delattr, METH_VARARGS, delattr_doc},
2250 {"dir", builtin_dir, METH_VARARGS, dir_doc},
2251 {"divmod", builtin_divmod, METH_VARARGS, divmod_doc},
2252 {"eval", builtin_eval, METH_VARARGS, eval_doc},
2253 {"execfile", builtin_execfile, METH_VARARGS, execfile_doc},
2254 {"filter", builtin_filter, METH_VARARGS, filter_doc},
2255 {"getattr", builtin_getattr, METH_VARARGS, getattr_doc},
2256 {"globals", (PyCFunction)builtin_globals, METH_NOARGS, globals_doc},
2257 {"hasattr", builtin_hasattr, METH_VARARGS, hasattr_doc},
2258 {"hash", builtin_hash, METH_O, hash_doc},
2259 {"hex", builtin_hex, METH_O, hex_doc},
2260 {"id", builtin_id, METH_O, id_doc},
2261 {"input", builtin_input, METH_VARARGS, input_doc},
2262 {"intern", builtin_intern, METH_VARARGS, intern_doc},
2263 {"isinstance", builtin_isinstance, METH_VARARGS, isinstance_doc},
2264 {"issubclass", builtin_issubclass, METH_VARARGS, issubclass_doc},
2265 {"iter", builtin_iter, METH_VARARGS, iter_doc},
2266 {"len", builtin_len, METH_O, len_doc},
2267 {"locals", (PyCFunction)builtin_locals, METH_NOARGS, locals_doc},
2268 {"map", builtin_map, METH_VARARGS, map_doc},
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00002269 {"max", (PyCFunction)builtin_max, METH_VARARGS | METH_KEYWORDS, max_doc},
2270 {"min", (PyCFunction)builtin_min, METH_VARARGS | METH_KEYWORDS, min_doc},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002271 {"oct", builtin_oct, METH_O, oct_doc},
Neal Norwitzc4edb0e2006-05-02 04:43:14 +00002272 {"open", (PyCFunction)builtin_open, METH_VARARGS | METH_KEYWORDS, open_doc},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002273 {"ord", builtin_ord, METH_O, ord_doc},
2274 {"pow", builtin_pow, METH_VARARGS, pow_doc},
2275 {"range", builtin_range, METH_VARARGS, range_doc},
2276 {"raw_input", builtin_raw_input, METH_VARARGS, raw_input_doc},
2277 {"reduce", builtin_reduce, METH_VARARGS, reduce_doc},
2278 {"reload", builtin_reload, METH_O, reload_doc},
2279 {"repr", builtin_repr, METH_O, repr_doc},
Georg Brandlccadf842006-03-31 18:54:53 +00002280 {"round", (PyCFunction)builtin_round, METH_VARARGS | METH_KEYWORDS, round_doc},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002281 {"setattr", builtin_setattr, METH_VARARGS, setattr_doc},
Raymond Hettinger64958a12003-12-17 20:43:33 +00002282 {"sorted", (PyCFunction)builtin_sorted, METH_VARARGS | METH_KEYWORDS, sorted_doc},
Alex Martellia70b1912003-04-22 08:12:33 +00002283 {"sum", builtin_sum, METH_VARARGS, sum_doc},
Martin v. Löwis339d0f72001-08-17 18:39:25 +00002284#ifdef Py_USING_UNICODE
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002285 {"unichr", builtin_unichr, METH_VARARGS, unichr_doc},
Martin v. Löwis339d0f72001-08-17 18:39:25 +00002286#endif
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002287 {"vars", builtin_vars, METH_VARARGS, vars_doc},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002288 {"zip", builtin_zip, METH_VARARGS, zip_doc},
Guido van Rossumc02e15c1991-12-16 13:03:00 +00002289 {NULL, NULL},
Guido van Rossum3f5da241990-12-20 15:06:42 +00002290};
2291
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002292PyDoc_STRVAR(builtin_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002293"Built-in functions, exceptions, and other objects.\n\
2294\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002295Noteworthy: None is the `nil' object; Ellipsis represents `...' in slices.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002296
Guido van Rossum25ce5661997-08-02 03:10:38 +00002297PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002298_PyBuiltin_Init(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +00002299{
Fred Drake5550de32000-06-20 04:54:19 +00002300 PyObject *mod, *dict, *debug;
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002301 mod = Py_InitModule4("__builtin__", builtin_methods,
2302 builtin_doc, (PyObject *)NULL,
2303 PYTHON_API_VERSION);
Guido van Rossum25ce5661997-08-02 03:10:38 +00002304 if (mod == NULL)
2305 return NULL;
2306 dict = PyModule_GetDict(mod);
Tim Peters4b7625e2001-09-13 21:37:17 +00002307
Tim Peters7571a0f2003-03-23 17:52:28 +00002308#ifdef Py_TRACE_REFS
2309 /* __builtin__ exposes a number of statically allocated objects
2310 * that, before this code was added in 2.3, never showed up in
2311 * the list of "all objects" maintained by Py_TRACE_REFS. As a
2312 * result, programs leaking references to None and False (etc)
2313 * couldn't be diagnosed by examining sys.getobjects(0).
2314 */
2315#define ADD_TO_ALL(OBJECT) _Py_AddToAllObjects((PyObject *)(OBJECT), 0)
2316#else
2317#define ADD_TO_ALL(OBJECT) (void)0
2318#endif
2319
Tim Peters4b7625e2001-09-13 21:37:17 +00002320#define SETBUILTIN(NAME, OBJECT) \
Tim Peters7571a0f2003-03-23 17:52:28 +00002321 if (PyDict_SetItemString(dict, NAME, (PyObject *)OBJECT) < 0) \
2322 return NULL; \
2323 ADD_TO_ALL(OBJECT)
Tim Peters4b7625e2001-09-13 21:37:17 +00002324
2325 SETBUILTIN("None", Py_None);
2326 SETBUILTIN("Ellipsis", Py_Ellipsis);
2327 SETBUILTIN("NotImplemented", Py_NotImplemented);
Guido van Rossum77f6a652002-04-03 22:41:51 +00002328 SETBUILTIN("False", Py_False);
2329 SETBUILTIN("True", Py_True);
Neal Norwitz32a7e7f2002-05-31 19:58:02 +00002330 SETBUILTIN("basestring", &PyBaseString_Type);
Guido van Rossum77f6a652002-04-03 22:41:51 +00002331 SETBUILTIN("bool", &PyBool_Type);
Guido van Rossumbea18cc2002-06-14 20:41:17 +00002332 SETBUILTIN("buffer", &PyBuffer_Type);
Tim Peters4b7625e2001-09-13 21:37:17 +00002333 SETBUILTIN("classmethod", &PyClassMethod_Type);
Tim Peters6d6c1a32001-08-02 04:15:00 +00002334#ifndef WITHOUT_COMPLEX
Tim Peters4b7625e2001-09-13 21:37:17 +00002335 SETBUILTIN("complex", &PyComplex_Type);
Tim Peters6d6c1a32001-08-02 04:15:00 +00002336#endif
Tim Petersa427a2b2001-10-29 22:25:45 +00002337 SETBUILTIN("dict", &PyDict_Type);
Tim Peters67d687a2002-04-29 21:27:32 +00002338 SETBUILTIN("enumerate", &PyEnum_Type);
Neal Norwitzc4edb0e2006-05-02 04:43:14 +00002339 SETBUILTIN("file", &PyFile_Type);
Tim Peters4b7625e2001-09-13 21:37:17 +00002340 SETBUILTIN("float", &PyFloat_Type);
Raymond Hettingera690a992003-11-16 16:17:49 +00002341 SETBUILTIN("frozenset", &PyFrozenSet_Type);
Tim Peters4b7625e2001-09-13 21:37:17 +00002342 SETBUILTIN("property", &PyProperty_Type);
2343 SETBUILTIN("int", &PyInt_Type);
2344 SETBUILTIN("list", &PyList_Type);
2345 SETBUILTIN("long", &PyLong_Type);
2346 SETBUILTIN("object", &PyBaseObject_Type);
Raymond Hettinger85c20a42003-11-06 14:06:48 +00002347 SETBUILTIN("reversed", &PyReversed_Type);
Raymond Hettingera690a992003-11-16 16:17:49 +00002348 SETBUILTIN("set", &PySet_Type);
Guido van Rossumbea18cc2002-06-14 20:41:17 +00002349 SETBUILTIN("slice", &PySlice_Type);
Tim Peters4b7625e2001-09-13 21:37:17 +00002350 SETBUILTIN("staticmethod", &PyStaticMethod_Type);
2351 SETBUILTIN("str", &PyString_Type);
2352 SETBUILTIN("super", &PySuper_Type);
2353 SETBUILTIN("tuple", &PyTuple_Type);
2354 SETBUILTIN("type", &PyType_Type);
Raymond Hettingerc4c453f2002-06-05 23:12:45 +00002355 SETBUILTIN("xrange", &PyRange_Type);
Martin v. Löwis339d0f72001-08-17 18:39:25 +00002356#ifdef Py_USING_UNICODE
Tim Peters4b7625e2001-09-13 21:37:17 +00002357 SETBUILTIN("unicode", &PyUnicode_Type);
Martin v. Löwis339d0f72001-08-17 18:39:25 +00002358#endif
Guido van Rossum77f6a652002-04-03 22:41:51 +00002359 debug = PyBool_FromLong(Py_OptimizeFlag == 0);
Fred Drake5550de32000-06-20 04:54:19 +00002360 if (PyDict_SetItemString(dict, "__debug__", debug) < 0) {
2361 Py_XDECREF(debug);
Guido van Rossum25ce5661997-08-02 03:10:38 +00002362 return NULL;
Fred Drake5550de32000-06-20 04:54:19 +00002363 }
2364 Py_XDECREF(debug);
Barry Warsaw757af0e1997-08-29 22:13:51 +00002365
Guido van Rossum25ce5661997-08-02 03:10:38 +00002366 return mod;
Tim Peters7571a0f2003-03-23 17:52:28 +00002367#undef ADD_TO_ALL
Tim Peters4b7625e2001-09-13 21:37:17 +00002368#undef SETBUILTIN
Guido van Rossum3f5da241990-12-20 15:06:42 +00002369}
2370
Guido van Rossume77a7571993-11-03 15:01:26 +00002371/* Helper for filter(): filter a tuple through a function */
Guido van Rossum12d12c51993-10-26 17:58:25 +00002372
Guido van Rossum79f25d91997-04-29 20:08:16 +00002373static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002374filtertuple(PyObject *func, PyObject *tuple)
Guido van Rossum12d12c51993-10-26 17:58:25 +00002375{
Guido van Rossum79f25d91997-04-29 20:08:16 +00002376 PyObject *result;
Martin v. Löwis18e16552006-02-15 17:27:45 +00002377 Py_ssize_t i, j;
2378 Py_ssize_t len = PyTuple_Size(tuple);
Guido van Rossum12d12c51993-10-26 17:58:25 +00002379
Guido van Rossumb7b45621995-08-04 04:07:45 +00002380 if (len == 0) {
Walter Dörwaldc3da83f2003-02-04 20:24:45 +00002381 if (PyTuple_CheckExact(tuple))
2382 Py_INCREF(tuple);
2383 else
2384 tuple = PyTuple_New(0);
Guido van Rossumb7b45621995-08-04 04:07:45 +00002385 return tuple;
2386 }
2387
Guido van Rossum79f25d91997-04-29 20:08:16 +00002388 if ((result = PyTuple_New(len)) == NULL)
Guido van Rossum2586bf01993-11-01 16:21:44 +00002389 return NULL;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002390
Guido van Rossum12d12c51993-10-26 17:58:25 +00002391 for (i = j = 0; i < len; ++i) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002392 PyObject *item, *good;
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002393 int ok;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002394
Walter Dörwald8dd19322003-02-10 17:36:40 +00002395 if (tuple->ob_type->tp_as_sequence &&
2396 tuple->ob_type->tp_as_sequence->sq_item) {
2397 item = tuple->ob_type->tp_as_sequence->sq_item(tuple, i);
Walter Dörwaldc58a3a12003-08-18 18:28:45 +00002398 if (item == NULL)
2399 goto Fail_1;
Walter Dörwald8dd19322003-02-10 17:36:40 +00002400 } else {
Alex Martellia9b9c9f2003-04-23 13:34:35 +00002401 PyErr_SetString(PyExc_TypeError, "filter(): unsubscriptable tuple");
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002402 goto Fail_1;
Walter Dörwald8dd19322003-02-10 17:36:40 +00002403 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002404 if (func == Py_None) {
2405 Py_INCREF(item);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002406 good = item;
2407 }
2408 else {
Raymond Hettinger8ae46892003-10-12 19:09:37 +00002409 PyObject *arg = PyTuple_Pack(1, item);
Walter Dörwaldc58a3a12003-08-18 18:28:45 +00002410 if (arg == NULL) {
2411 Py_DECREF(item);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002412 goto Fail_1;
Walter Dörwaldc58a3a12003-08-18 18:28:45 +00002413 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002414 good = PyEval_CallObject(func, arg);
2415 Py_DECREF(arg);
Walter Dörwaldc58a3a12003-08-18 18:28:45 +00002416 if (good == NULL) {
2417 Py_DECREF(item);
Guido van Rossum12d12c51993-10-26 17:58:25 +00002418 goto Fail_1;
Walter Dörwaldc58a3a12003-08-18 18:28:45 +00002419 }
Guido van Rossum12d12c51993-10-26 17:58:25 +00002420 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002421 ok = PyObject_IsTrue(good);
2422 Py_DECREF(good);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002423 if (ok) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002424 if (PyTuple_SetItem(result, j++, item) < 0)
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002425 goto Fail_1;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002426 }
Walter Dörwaldc58a3a12003-08-18 18:28:45 +00002427 else
2428 Py_DECREF(item);
Guido van Rossum12d12c51993-10-26 17:58:25 +00002429 }
2430
Tim Peters4324aa32001-05-28 22:30:08 +00002431 if (_PyTuple_Resize(&result, j) < 0)
Guido van Rossum12d12c51993-10-26 17:58:25 +00002432 return NULL;
2433
Guido van Rossum12d12c51993-10-26 17:58:25 +00002434 return result;
2435
Guido van Rossum12d12c51993-10-26 17:58:25 +00002436Fail_1:
Guido van Rossum79f25d91997-04-29 20:08:16 +00002437 Py_DECREF(result);
Guido van Rossum12d12c51993-10-26 17:58:25 +00002438 return NULL;
2439}
2440
2441
Guido van Rossume77a7571993-11-03 15:01:26 +00002442/* Helper for filter(): filter a string through a function */
Guido van Rossum12d12c51993-10-26 17:58:25 +00002443
Guido van Rossum79f25d91997-04-29 20:08:16 +00002444static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002445filterstring(PyObject *func, PyObject *strobj)
Guido van Rossum12d12c51993-10-26 17:58:25 +00002446{
Guido van Rossum79f25d91997-04-29 20:08:16 +00002447 PyObject *result;
Martin v. Löwis18e16552006-02-15 17:27:45 +00002448 Py_ssize_t i, j;
2449 Py_ssize_t len = PyString_Size(strobj);
2450 Py_ssize_t outlen = len;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002451
Guido van Rossum79f25d91997-04-29 20:08:16 +00002452 if (func == Py_None) {
Walter Dörwald1918f772003-02-10 13:19:13 +00002453 /* If it's a real string we can return the original,
2454 * as no character is ever false and __getitem__
2455 * does return this character. If it's a subclass
2456 * we must go through the __getitem__ loop */
2457 if (PyString_CheckExact(strobj)) {
Walter Dörwaldc3da83f2003-02-04 20:24:45 +00002458 Py_INCREF(strobj);
Walter Dörwald1918f772003-02-10 13:19:13 +00002459 return strobj;
2460 }
Guido van Rossum12d12c51993-10-26 17:58:25 +00002461 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002462 if ((result = PyString_FromStringAndSize(NULL, len)) == NULL)
Guido van Rossum2586bf01993-11-01 16:21:44 +00002463 return NULL;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002464
Guido van Rossum12d12c51993-10-26 17:58:25 +00002465 for (i = j = 0; i < len; ++i) {
Walter Dörwald1918f772003-02-10 13:19:13 +00002466 PyObject *item;
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002467 int ok;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002468
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002469 item = (*strobj->ob_type->tp_as_sequence->sq_item)(strobj, i);
2470 if (item == NULL)
2471 goto Fail_1;
Walter Dörwald1918f772003-02-10 13:19:13 +00002472 if (func==Py_None) {
2473 ok = 1;
2474 } else {
2475 PyObject *arg, *good;
Raymond Hettinger8ae46892003-10-12 19:09:37 +00002476 arg = PyTuple_Pack(1, item);
Walter Dörwald1918f772003-02-10 13:19:13 +00002477 if (arg == NULL) {
2478 Py_DECREF(item);
2479 goto Fail_1;
2480 }
2481 good = PyEval_CallObject(func, arg);
2482 Py_DECREF(arg);
2483 if (good == NULL) {
2484 Py_DECREF(item);
2485 goto Fail_1;
2486 }
2487 ok = PyObject_IsTrue(good);
2488 Py_DECREF(good);
Tim Peters388ed082001-04-07 20:34:48 +00002489 }
Walter Dörwald903f1e02003-02-04 16:28:00 +00002490 if (ok) {
Martin v. Löwisd96ee902006-02-16 14:37:16 +00002491 Py_ssize_t reslen;
Walter Dörwald903f1e02003-02-04 16:28:00 +00002492 if (!PyString_Check(item)) {
2493 PyErr_SetString(PyExc_TypeError, "can't filter str to str:"
2494 " __getitem__ returned different type");
2495 Py_DECREF(item);
2496 goto Fail_1;
2497 }
2498 reslen = PyString_GET_SIZE(item);
2499 if (reslen == 1) {
2500 PyString_AS_STRING(result)[j++] =
2501 PyString_AS_STRING(item)[0];
2502 } else {
2503 /* do we need more space? */
Martin v. Löwisd96ee902006-02-16 14:37:16 +00002504 Py_ssize_t need = j + reslen + len-i-1;
Walter Dörwald903f1e02003-02-04 16:28:00 +00002505 if (need > outlen) {
2506 /* overallocate, to avoid reallocations */
2507 if (need<2*outlen)
2508 need = 2*outlen;
2509 if (_PyString_Resize(&result, need)) {
2510 Py_DECREF(item);
2511 return NULL;
2512 }
2513 outlen = need;
2514 }
2515 memcpy(
2516 PyString_AS_STRING(result) + j,
2517 PyString_AS_STRING(item),
2518 reslen
2519 );
2520 j += reslen;
2521 }
2522 }
Tim Peters388ed082001-04-07 20:34:48 +00002523 Py_DECREF(item);
Guido van Rossum12d12c51993-10-26 17:58:25 +00002524 }
2525
Walter Dörwald903f1e02003-02-04 16:28:00 +00002526 if (j < outlen)
Tim Peters5de98422002-04-27 18:44:32 +00002527 _PyString_Resize(&result, j);
Guido van Rossum12d12c51993-10-26 17:58:25 +00002528
Guido van Rossum12d12c51993-10-26 17:58:25 +00002529 return result;
2530
Guido van Rossum12d12c51993-10-26 17:58:25 +00002531Fail_1:
Guido van Rossum79f25d91997-04-29 20:08:16 +00002532 Py_DECREF(result);
Guido van Rossum12d12c51993-10-26 17:58:25 +00002533 return NULL;
2534}
Martin v. Löwis8afd7572003-01-25 22:46:11 +00002535
2536#ifdef Py_USING_UNICODE
2537/* Helper for filter(): filter a Unicode object through a function */
2538
2539static PyObject *
2540filterunicode(PyObject *func, PyObject *strobj)
2541{
2542 PyObject *result;
Martin v. Löwis725507b2006-03-07 12:08:51 +00002543 register Py_ssize_t i, j;
Martin v. Löwisd96ee902006-02-16 14:37:16 +00002544 Py_ssize_t len = PyUnicode_GetSize(strobj);
2545 Py_ssize_t outlen = len;
Martin v. Löwis8afd7572003-01-25 22:46:11 +00002546
2547 if (func == Py_None) {
Walter Dörwald1918f772003-02-10 13:19:13 +00002548 /* If it's a real string we can return the original,
2549 * as no character is ever false and __getitem__
2550 * does return this character. If it's a subclass
2551 * we must go through the __getitem__ loop */
2552 if (PyUnicode_CheckExact(strobj)) {
Walter Dörwaldc3da83f2003-02-04 20:24:45 +00002553 Py_INCREF(strobj);
Walter Dörwald1918f772003-02-10 13:19:13 +00002554 return strobj;
2555 }
Martin v. Löwis8afd7572003-01-25 22:46:11 +00002556 }
2557 if ((result = PyUnicode_FromUnicode(NULL, len)) == NULL)
2558 return NULL;
2559
2560 for (i = j = 0; i < len; ++i) {
2561 PyObject *item, *arg, *good;
2562 int ok;
2563
2564 item = (*strobj->ob_type->tp_as_sequence->sq_item)(strobj, i);
2565 if (item == NULL)
2566 goto Fail_1;
Walter Dörwald1918f772003-02-10 13:19:13 +00002567 if (func == Py_None) {
2568 ok = 1;
2569 } else {
Raymond Hettinger8ae46892003-10-12 19:09:37 +00002570 arg = PyTuple_Pack(1, item);
Walter Dörwald1918f772003-02-10 13:19:13 +00002571 if (arg == NULL) {
2572 Py_DECREF(item);
2573 goto Fail_1;
2574 }
2575 good = PyEval_CallObject(func, arg);
2576 Py_DECREF(arg);
2577 if (good == NULL) {
2578 Py_DECREF(item);
2579 goto Fail_1;
2580 }
2581 ok = PyObject_IsTrue(good);
2582 Py_DECREF(good);
Martin v. Löwis8afd7572003-01-25 22:46:11 +00002583 }
Walter Dörwald903f1e02003-02-04 16:28:00 +00002584 if (ok) {
Martin v. Löwisd96ee902006-02-16 14:37:16 +00002585 Py_ssize_t reslen;
Walter Dörwald903f1e02003-02-04 16:28:00 +00002586 if (!PyUnicode_Check(item)) {
Georg Brandl99d7e4e2005-08-31 22:21:15 +00002587 PyErr_SetString(PyExc_TypeError,
Jeremy Hylton1aad9c72003-09-16 03:10:59 +00002588 "can't filter unicode to unicode:"
2589 " __getitem__ returned different type");
Walter Dörwald903f1e02003-02-04 16:28:00 +00002590 Py_DECREF(item);
2591 goto Fail_1;
2592 }
2593 reslen = PyUnicode_GET_SIZE(item);
Georg Brandl99d7e4e2005-08-31 22:21:15 +00002594 if (reslen == 1)
Walter Dörwald903f1e02003-02-04 16:28:00 +00002595 PyUnicode_AS_UNICODE(result)[j++] =
2596 PyUnicode_AS_UNICODE(item)[0];
Jeremy Hylton1aad9c72003-09-16 03:10:59 +00002597 else {
Walter Dörwald903f1e02003-02-04 16:28:00 +00002598 /* do we need more space? */
Martin v. Löwisd96ee902006-02-16 14:37:16 +00002599 Py_ssize_t need = j + reslen + len - i - 1;
Walter Dörwald903f1e02003-02-04 16:28:00 +00002600 if (need > outlen) {
Georg Brandl99d7e4e2005-08-31 22:21:15 +00002601 /* overallocate,
Jeremy Hylton1aad9c72003-09-16 03:10:59 +00002602 to avoid reallocations */
2603 if (need < 2 * outlen)
2604 need = 2 * outlen;
Jeremy Hylton364f6be2003-09-16 03:17:16 +00002605 if (PyUnicode_Resize(
2606 &result, need) < 0) {
Walter Dörwald903f1e02003-02-04 16:28:00 +00002607 Py_DECREF(item);
Walter Dörwald531e0002003-02-04 16:57:49 +00002608 goto Fail_1;
Walter Dörwald903f1e02003-02-04 16:28:00 +00002609 }
2610 outlen = need;
2611 }
Jeremy Hylton1aad9c72003-09-16 03:10:59 +00002612 memcpy(PyUnicode_AS_UNICODE(result) + j,
2613 PyUnicode_AS_UNICODE(item),
2614 reslen*sizeof(Py_UNICODE));
Walter Dörwald903f1e02003-02-04 16:28:00 +00002615 j += reslen;
2616 }
2617 }
Martin v. Löwis8afd7572003-01-25 22:46:11 +00002618 Py_DECREF(item);
2619 }
2620
Walter Dörwald903f1e02003-02-04 16:28:00 +00002621 if (j < outlen)
Martin v. Löwis8afd7572003-01-25 22:46:11 +00002622 PyUnicode_Resize(&result, j);
2623
2624 return result;
2625
2626Fail_1:
2627 Py_DECREF(result);
2628 return NULL;
2629}
2630#endif