blob: e1242fdede475489623fa1ee8b313e6a903504eb [file] [log] [blame]
Guido van Rossum3f5da241990-12-20 15:06:42 +00001/* Built-in functions */
2
Guido van Rossum79f25d91997-04-29 20:08:16 +00003#include "Python.h"
Guido van Rossum3f5da241990-12-20 15:06:42 +00004
5#include "node.h"
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006#include "code.h"
Guido van Rossum5b722181993-03-30 17:46:03 +00007#include "eval.h"
Guido van Rossum3f5da241990-12-20 15:06:42 +00008
Guido van Rossum6bf62da1997-04-11 20:37:35 +00009#include <ctype.h>
10
Guido van Rossume2ae77b2001-10-24 20:42:55 +000011#ifdef RISCOS
12#include "unixstuff.h"
13#endif
14
Mark Hammond26cffde42001-05-14 12:17:34 +000015/* The default encoding used by the platform file system APIs
16 Can remain NULL for all platforms that don't have such a concept
17*/
Martin v. Löwis6238d2b2002-06-30 15:26:10 +000018#if defined(MS_WINDOWS) && defined(HAVE_USABLE_WCHAR_T)
Mark Hammond26cffde42001-05-14 12:17:34 +000019const char *Py_FileSystemDefaultEncoding = "mbcs";
Just van Rossumb9b8e9c2003-02-10 09:22:01 +000020#elif defined(__APPLE__)
21const char *Py_FileSystemDefaultEncoding = "utf-8";
Mark Hammond26cffde42001-05-14 12:17:34 +000022#else
23const char *Py_FileSystemDefaultEncoding = NULL; /* use default */
24#endif
Mark Hammondef8b6542001-05-13 08:04:26 +000025
Guido van Rossum12d12c51993-10-26 17:58:25 +000026/* Forward */
Tim Petersdbd9ba62000-07-09 03:09:57 +000027static PyObject *filterstring(PyObject *, PyObject *);
Martin v. Löwis8afd7572003-01-25 22:46:11 +000028#ifdef Py_USING_UNICODE
29static PyObject *filterunicode(PyObject *, PyObject *);
30#endif
Tim Petersdbd9ba62000-07-09 03:09:57 +000031static PyObject *filtertuple (PyObject *, PyObject *);
Guido van Rossum12d12c51993-10-26 17:58:25 +000032
Guido van Rossum79f25d91997-04-29 20:08:16 +000033static PyObject *
Neal Norwitz92e212f2006-04-03 04:48:37 +000034builtin___import__(PyObject *self, PyObject *args, PyObject *kwds)
Guido van Rossum3f5da241990-12-20 15:06:42 +000035{
Neal Norwitz92e212f2006-04-03 04:48:37 +000036 static char *kwlist[] = {"name", "globals", "locals", "fromlist",
37 "level", 0};
Guido van Rossum1ae940a1995-01-02 19:04:15 +000038 char *name;
Guido van Rossum79f25d91997-04-29 20:08:16 +000039 PyObject *globals = NULL;
40 PyObject *locals = NULL;
41 PyObject *fromlist = NULL;
Thomas Woutersf7f438b2006-02-28 16:09:29 +000042 int level = -1;
Guido van Rossum1ae940a1995-01-02 19:04:15 +000043
Neal Norwitz92e212f2006-04-03 04:48:37 +000044 if (!PyArg_ParseTupleAndKeywords(args, kwds, "s|OOOi:__import__",
45 kwlist, &name, &globals, &locals, &fromlist, &level))
Guido van Rossum1ae940a1995-01-02 19:04:15 +000046 return NULL;
Thomas Woutersf7f438b2006-02-28 16:09:29 +000047 return PyImport_ImportModuleLevel(name, globals, locals,
48 fromlist, level);
Guido van Rossum1ae940a1995-01-02 19:04:15 +000049}
50
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000051PyDoc_STRVAR(import_doc,
Neal Norwitz92e212f2006-04-03 04:48:37 +000052"__import__(name, globals={}, locals={}, fromlist=[], level=-1) -> module\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +000053\n\
54Import a module. The globals are only used to determine the context;\n\
55they are not modified. The locals are currently unused. The fromlist\n\
56should be a list of names to emulate ``from name import ...'', or an\n\
57empty list to emulate ``import name''.\n\
58When importing a module from a package, note that __import__('A.B', ...)\n\
59returns package A when fromlist is empty, but its submodule B when\n\
Neal Norwitz92e212f2006-04-03 04:48:37 +000060fromlist is not empty. Level is used to determine whether to perform \n\
61absolute or relative imports. -1 is the original strategy of attempting\n\
62both absolute and relative imports, 0 is absolute, a positive number\n\
63is the number of parent directories to search relative to the current module.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +000064
Guido van Rossum1ae940a1995-01-02 19:04:15 +000065
Guido van Rossum79f25d91997-04-29 20:08:16 +000066static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +000067builtin_abs(PyObject *self, PyObject *v)
Guido van Rossum1ae940a1995-01-02 19:04:15 +000068{
Guido van Rossum09df08a1998-05-22 00:51:39 +000069 return PyNumber_Absolute(v);
Guido van Rossum3f5da241990-12-20 15:06:42 +000070}
71
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000072PyDoc_STRVAR(abs_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +000073"abs(number) -> number\n\
74\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000075Return the absolute value of the argument.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +000076
Raymond Hettinger96229b12005-03-11 06:49:40 +000077static PyObject *
78builtin_all(PyObject *self, PyObject *v)
79{
80 PyObject *it, *item;
81
82 it = PyObject_GetIter(v);
83 if (it == NULL)
84 return NULL;
85
86 while ((item = PyIter_Next(it)) != NULL) {
87 int cmp = PyObject_IsTrue(item);
88 Py_DECREF(item);
89 if (cmp < 0) {
90 Py_DECREF(it);
91 return NULL;
92 }
93 if (cmp == 0) {
94 Py_DECREF(it);
95 Py_RETURN_FALSE;
96 }
97 }
98 Py_DECREF(it);
99 if (PyErr_Occurred())
100 return NULL;
101 Py_RETURN_TRUE;
102}
103
104PyDoc_STRVAR(all_doc,
105"all(iterable) -> bool\n\
106\n\
107Return True if bool(x) is True for all values x in the iterable.");
108
109static PyObject *
110builtin_any(PyObject *self, PyObject *v)
111{
112 PyObject *it, *item;
113
114 it = PyObject_GetIter(v);
115 if (it == NULL)
116 return NULL;
117
118 while ((item = PyIter_Next(it)) != NULL) {
119 int cmp = PyObject_IsTrue(item);
120 Py_DECREF(item);
121 if (cmp < 0) {
122 Py_DECREF(it);
123 return NULL;
124 }
125 if (cmp == 1) {
126 Py_DECREF(it);
127 Py_RETURN_TRUE;
128 }
129 }
130 Py_DECREF(it);
131 if (PyErr_Occurred())
132 return NULL;
133 Py_RETURN_FALSE;
134}
135
136PyDoc_STRVAR(any_doc,
137"any(iterable) -> bool\n\
138\n\
139Return True if bool(x) is True for any x in the iterable.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000140
Guido van Rossum79f25d91997-04-29 20:08:16 +0000141static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000142builtin_apply(PyObject *self, PyObject *args)
Guido van Rossumc02e15c1991-12-16 13:03:00 +0000143{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000144 PyObject *func, *alist = NULL, *kwdict = NULL;
Barry Warsaw968f8cb1998-10-01 15:33:12 +0000145 PyObject *t = NULL, *retval = NULL;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000146
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{
Neal Norwitzdf25efe2007-05-23 06:58:36 +0000194 if (Py_Py3kWarningFlag &&
195 PyErr_Warn(PyExc_DeprecationWarning,
196 "callable() not supported in 3.x") < 0)
197 return NULL;
Guido van Rossum77f6a652002-04-03 22:41:51 +0000198 return PyBool_FromLong((long)PyCallable_Check(v));
Guido van Rossum2d951851994-08-29 12:52:16 +0000199}
200
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000201PyDoc_STRVAR(callable_doc,
Guido van Rossum77f6a652002-04-03 22:41:51 +0000202"callable(object) -> bool\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000203\n\
204Return whether the object is callable (i.e., some kind of function).\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000205Note that classes are callable, as are instances with a __call__() method.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000206
207
Guido van Rossum79f25d91997-04-29 20:08:16 +0000208static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000209builtin_filter(PyObject *self, PyObject *args)
Guido van Rossum12d12c51993-10-26 17:58:25 +0000210{
Guido van Rossumc7903a12002-08-16 07:04:56 +0000211 PyObject *func, *seq, *result, *it, *arg;
Martin v. Löwisd96ee902006-02-16 14:37:16 +0000212 Py_ssize_t len; /* guess for result list size */
213 register Py_ssize_t j;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000214
Raymond Hettingerea3fdf42002-12-29 16:33:45 +0000215 if (!PyArg_UnpackTuple(args, "filter", 2, 2, &func, &seq))
Guido van Rossum12d12c51993-10-26 17:58:25 +0000216 return NULL;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000217
Tim Peters0e57abf2001-05-02 07:39:38 +0000218 /* Strings and tuples return a result of the same type. */
219 if (PyString_Check(seq))
220 return filterstring(func, seq);
Martin v. Löwis8afd7572003-01-25 22:46:11 +0000221#ifdef Py_USING_UNICODE
222 if (PyUnicode_Check(seq))
223 return filterunicode(func, seq);
224#endif
Tim Peters0e57abf2001-05-02 07:39:38 +0000225 if (PyTuple_Check(seq))
226 return filtertuple(func, seq);
227
Georg Brandle35b6572005-07-19 22:20:20 +0000228 /* Pre-allocate argument list tuple. */
229 arg = PyTuple_New(1);
230 if (arg == NULL)
231 return NULL;
232
Tim Peters0e57abf2001-05-02 07:39:38 +0000233 /* Get iterator. */
234 it = PyObject_GetIter(seq);
235 if (it == NULL)
Georg Brandle35b6572005-07-19 22:20:20 +0000236 goto Fail_arg;
Tim Peters0e57abf2001-05-02 07:39:38 +0000237
238 /* Guess a result list size. */
Raymond Hettinger4e2f7142007-12-06 00:56:53 +0000239 len = _PyObject_LengthHint(seq, 8);
Guido van Rossum12d12c51993-10-26 17:58:25 +0000240
Tim Peters0e57abf2001-05-02 07:39:38 +0000241 /* Get a result list. */
Guido van Rossum79f25d91997-04-29 20:08:16 +0000242 if (PyList_Check(seq) && seq->ob_refcnt == 1) {
Tim Peters0e57abf2001-05-02 07:39:38 +0000243 /* Eww - can modify the list in-place. */
Guido van Rossum79f25d91997-04-29 20:08:16 +0000244 Py_INCREF(seq);
Guido van Rossum12d12c51993-10-26 17:58:25 +0000245 result = seq;
246 }
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000247 else {
Tim Peters0e57abf2001-05-02 07:39:38 +0000248 result = PyList_New(len);
249 if (result == NULL)
250 goto Fail_it;
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000251 }
Guido van Rossum12d12c51993-10-26 17:58:25 +0000252
Tim Peters0e57abf2001-05-02 07:39:38 +0000253 /* Build the result list. */
Tim Petersf4848da2001-05-05 00:14:56 +0000254 j = 0;
255 for (;;) {
Guido van Rossumc7903a12002-08-16 07:04:56 +0000256 PyObject *item;
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000257 int ok;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000258
Tim Peters0e57abf2001-05-02 07:39:38 +0000259 item = PyIter_Next(it);
260 if (item == NULL) {
Tim Petersf4848da2001-05-05 00:14:56 +0000261 if (PyErr_Occurred())
262 goto Fail_result_it;
Tim Peters0e57abf2001-05-02 07:39:38 +0000263 break;
Guido van Rossum2d951851994-08-29 12:52:16 +0000264 }
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000265
Neil Schemenauer68973552003-08-14 20:37:34 +0000266 if (func == (PyObject *)&PyBool_Type || func == Py_None) {
Guido van Rossumc7903a12002-08-16 07:04:56 +0000267 ok = PyObject_IsTrue(item);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000268 }
269 else {
Guido van Rossumc7903a12002-08-16 07:04:56 +0000270 PyObject *good;
271 PyTuple_SET_ITEM(arg, 0, item);
272 good = PyObject_Call(func, arg, NULL);
273 PyTuple_SET_ITEM(arg, 0, NULL);
Guido van Rossum58b68731995-01-10 17:40:55 +0000274 if (good == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000275 Py_DECREF(item);
Tim Peters0e57abf2001-05-02 07:39:38 +0000276 goto Fail_result_it;
Guido van Rossum58b68731995-01-10 17:40:55 +0000277 }
Guido van Rossumc7903a12002-08-16 07:04:56 +0000278 ok = PyObject_IsTrue(good);
279 Py_DECREF(good);
Guido van Rossum12d12c51993-10-26 17:58:25 +0000280 }
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000281 if (ok) {
Tim Peters0e57abf2001-05-02 07:39:38 +0000282 if (j < len)
283 PyList_SET_ITEM(result, j, item);
Guido van Rossum2d951851994-08-29 12:52:16 +0000284 else {
Barry Warsawfa77e091999-01-28 18:49:12 +0000285 int status = PyList_Append(result, item);
Barry Warsawfa77e091999-01-28 18:49:12 +0000286 Py_DECREF(item);
287 if (status < 0)
Tim Peters0e57abf2001-05-02 07:39:38 +0000288 goto Fail_result_it;
Guido van Rossum2d951851994-08-29 12:52:16 +0000289 }
Tim Peters0e57abf2001-05-02 07:39:38 +0000290 ++j;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000291 }
Tim Peters0e57abf2001-05-02 07:39:38 +0000292 else
293 Py_DECREF(item);
Guido van Rossum12d12c51993-10-26 17:58:25 +0000294 }
295
Guido van Rossum12d12c51993-10-26 17:58:25 +0000296
Tim Peters0e57abf2001-05-02 07:39:38 +0000297 /* Cut back result list if len is too big. */
Guido van Rossum79f25d91997-04-29 20:08:16 +0000298 if (j < len && PyList_SetSlice(result, j, len, NULL) < 0)
Tim Peters0e57abf2001-05-02 07:39:38 +0000299 goto Fail_result_it;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000300
Tim Peters3c6b1482001-05-21 08:07:05 +0000301 Py_DECREF(it);
Guido van Rossumc7903a12002-08-16 07:04:56 +0000302 Py_DECREF(arg);
Guido van Rossum12d12c51993-10-26 17:58:25 +0000303 return result;
304
Tim Peters0e57abf2001-05-02 07:39:38 +0000305Fail_result_it:
Guido van Rossum79f25d91997-04-29 20:08:16 +0000306 Py_DECREF(result);
Tim Peters0e57abf2001-05-02 07:39:38 +0000307Fail_it:
308 Py_DECREF(it);
Guido van Rossumc7903a12002-08-16 07:04:56 +0000309Fail_arg:
310 Py_DECREF(arg);
Guido van Rossum12d12c51993-10-26 17:58:25 +0000311 return NULL;
312}
313
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000314PyDoc_STRVAR(filter_doc,
Tim Petersd50e5442002-03-09 00:06:26 +0000315"filter(function or None, sequence) -> list, tuple, or string\n"
316"\n"
317"Return those items of sequence for which function(item) is true. If\n"
318"function is None, return the items that are true. If sequence is a tuple\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000319"or string, return the same type, else return a list.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000320
Guido van Rossum79f25d91997-04-29 20:08:16 +0000321static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000322builtin_chr(PyObject *self, PyObject *args)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000323{
324 long x;
325 char s[1];
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000326
Guido van Rossum79f25d91997-04-29 20:08:16 +0000327 if (!PyArg_ParseTuple(args, "l:chr", &x))
Guido van Rossum3f5da241990-12-20 15:06:42 +0000328 return NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000329 if (x < 0 || x >= 256) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000330 PyErr_SetString(PyExc_ValueError,
331 "chr() arg not in range(256)");
Guido van Rossum3f5da241990-12-20 15:06:42 +0000332 return NULL;
333 }
Guido van Rossum6bf62da1997-04-11 20:37:35 +0000334 s[0] = (char)x;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000335 return PyString_FromStringAndSize(s, 1);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000336}
337
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000338PyDoc_STRVAR(chr_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000339"chr(i) -> character\n\
340\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000341Return a string of one character with ordinal i; 0 <= i < 256.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000342
343
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000344#ifdef Py_USING_UNICODE
Guido van Rossum79f25d91997-04-29 20:08:16 +0000345static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000346builtin_unichr(PyObject *self, PyObject *args)
Guido van Rossum09095f32000-03-10 23:00:52 +0000347{
348 long x;
Guido van Rossum09095f32000-03-10 23:00:52 +0000349
350 if (!PyArg_ParseTuple(args, "l:unichr", &x))
351 return NULL;
Fredrik Lundh0dcf67e2001-06-26 20:01:56 +0000352
Marc-André Lemburgcc8764c2002-08-11 12:23:04 +0000353 return PyUnicode_FromOrdinal(x);
Guido van Rossum09095f32000-03-10 23:00:52 +0000354}
355
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000356PyDoc_STRVAR(unichr_doc,
Fred Drake078b24f2000-04-13 02:42:50 +0000357"unichr(i) -> Unicode character\n\
Guido van Rossum09095f32000-03-10 23:00:52 +0000358\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000359Return a Unicode string of one character with ordinal i; 0 <= i <= 0x10ffff.");
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000360#endif
Guido van Rossum09095f32000-03-10 23:00:52 +0000361
362
363static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000364builtin_cmp(PyObject *self, PyObject *args)
Guido van Rossuma9e7dc11992-10-18 18:53:57 +0000365{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000366 PyObject *a, *b;
Guido van Rossum09df08a1998-05-22 00:51:39 +0000367 int c;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000368
Raymond Hettingerea3fdf42002-12-29 16:33:45 +0000369 if (!PyArg_UnpackTuple(args, "cmp", 2, 2, &a, &b))
Guido van Rossuma9e7dc11992-10-18 18:53:57 +0000370 return NULL;
Guido van Rossum09df08a1998-05-22 00:51:39 +0000371 if (PyObject_Cmp(a, b, &c) < 0)
Guido van Rossumc8b6df91997-05-23 00:06:51 +0000372 return NULL;
Guido van Rossum09df08a1998-05-22 00:51:39 +0000373 return PyInt_FromLong((long)c);
Guido van Rossuma9e7dc11992-10-18 18:53:57 +0000374}
375
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000376PyDoc_STRVAR(cmp_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000377"cmp(x, y) -> integer\n\
378\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000379Return negative if x<y, zero if x==y, positive if x>y.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000380
381
Guido van Rossum79f25d91997-04-29 20:08:16 +0000382static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000383builtin_coerce(PyObject *self, PyObject *args)
Guido van Rossum6a00cd81995-01-07 12:39:01 +0000384{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000385 PyObject *v, *w;
386 PyObject *res;
Guido van Rossum5524a591995-01-10 15:26:20 +0000387
Neal Norwitzdf25efe2007-05-23 06:58:36 +0000388 if (Py_Py3kWarningFlag &&
389 PyErr_Warn(PyExc_DeprecationWarning,
390 "coerce() not supported in 3.x") < 0)
391 return NULL;
392
Raymond Hettingerea3fdf42002-12-29 16:33:45 +0000393 if (!PyArg_UnpackTuple(args, "coerce", 2, 2, &v, &w))
Guido van Rossum5524a591995-01-10 15:26:20 +0000394 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000395 if (PyNumber_Coerce(&v, &w) < 0)
Guido van Rossum04691fc1992-08-12 15:35:34 +0000396 return NULL;
Raymond Hettinger8ae46892003-10-12 19:09:37 +0000397 res = PyTuple_Pack(2, v, w);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000398 Py_DECREF(v);
399 Py_DECREF(w);
Guido van Rossum04691fc1992-08-12 15:35:34 +0000400 return res;
401}
402
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000403PyDoc_STRVAR(coerce_doc,
Martin v. Löwis8d494f32004-08-25 10:42:41 +0000404"coerce(x, y) -> (x1, y1)\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000405\n\
Martin v. Löwis8d494f32004-08-25 10:42:41 +0000406Return a tuple consisting of the two numeric arguments converted to\n\
407a common type, using the same rules as used by arithmetic operations.\n\
408If coercion is not possible, raise TypeError.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000409
Guido van Rossum79f25d91997-04-29 20:08:16 +0000410static PyObject *
Georg Brandl5240d742007-03-13 20:46:32 +0000411builtin_compile(PyObject *self, PyObject *args, PyObject *kwds)
Guido van Rossum5b722181993-03-30 17:46:03 +0000412{
413 char *str;
414 char *filename;
415 char *startstr;
416 int start;
Tim Peters6cd6a822001-08-17 22:11:27 +0000417 int dont_inherit = 0;
418 int supplied_flags = 0;
Tim Peters5ba58662001-07-16 02:29:45 +0000419 PyCompilerFlags cf;
Neal Norwitz3a9a3e72005-11-27 20:38:31 +0000420 PyObject *result = NULL, *cmd, *tmp = NULL;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000421 Py_ssize_t length;
Georg Brandl5240d742007-03-13 20:46:32 +0000422 static char *kwlist[] = {"source", "filename", "mode", "flags",
423 "dont_inherit", NULL};
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000424
Georg Brandl5240d742007-03-13 20:46:32 +0000425 if (!PyArg_ParseTupleAndKeywords(args, kwds, "Oss|ii:compile",
426 kwlist, &cmd, &filename, &startstr,
427 &supplied_flags, &dont_inherit))
Guido van Rossum5b722181993-03-30 17:46:03 +0000428 return NULL;
Tim Peters6cd6a822001-08-17 22:11:27 +0000429
Just van Rossum3aaf42c2003-02-10 08:21:10 +0000430 cf.cf_flags = supplied_flags;
431
432#ifdef Py_USING_UNICODE
433 if (PyUnicode_Check(cmd)) {
434 tmp = PyUnicode_AsUTF8String(cmd);
435 if (tmp == NULL)
436 return NULL;
437 cmd = tmp;
438 cf.cf_flags |= PyCF_SOURCE_IS_UTF8;
439 }
440#endif
Just van Rossumb9b8e9c2003-02-10 09:22:01 +0000441 if (PyObject_AsReadBuffer(cmd, (const void **)&str, &length))
442 return NULL;
Tim Peters2c646c92003-02-10 14:48:29 +0000443 if ((size_t)length != strlen(str)) {
Just van Rossum3aaf42c2003-02-10 08:21:10 +0000444 PyErr_SetString(PyExc_TypeError,
Alex Martellia9b9c9f2003-04-23 13:34:35 +0000445 "compile() expected string without null bytes");
Neal Norwitz3a9a3e72005-11-27 20:38:31 +0000446 goto cleanup;
Just van Rossum3aaf42c2003-02-10 08:21:10 +0000447 }
448
Guido van Rossum5b722181993-03-30 17:46:03 +0000449 if (strcmp(startstr, "exec") == 0)
Guido van Rossumb05a5c71997-05-07 17:46:13 +0000450 start = Py_file_input;
Guido van Rossum5b722181993-03-30 17:46:03 +0000451 else if (strcmp(startstr, "eval") == 0)
Guido van Rossumb05a5c71997-05-07 17:46:13 +0000452 start = Py_eval_input;
Guido van Rossum872537c1995-07-07 22:43:42 +0000453 else if (strcmp(startstr, "single") == 0)
Guido van Rossumb05a5c71997-05-07 17:46:13 +0000454 start = Py_single_input;
Guido van Rossum5b722181993-03-30 17:46:03 +0000455 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000456 PyErr_SetString(PyExc_ValueError,
Fred Drake661ea262000-10-24 19:57:45 +0000457 "compile() arg 3 must be 'exec' or 'eval' or 'single'");
Neal Norwitz3a9a3e72005-11-27 20:38:31 +0000458 goto cleanup;
Guido van Rossum5b722181993-03-30 17:46:03 +0000459 }
Tim Peters6cd6a822001-08-17 22:11:27 +0000460
Guido van Rossum4b499dd32003-02-13 22:07:59 +0000461 if (supplied_flags &
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000462 ~(PyCF_MASK | PyCF_MASK_OBSOLETE | PyCF_DONT_IMPLY_DEDENT | PyCF_ONLY_AST))
Guido van Rossum4b499dd32003-02-13 22:07:59 +0000463 {
Tim Peters6cd6a822001-08-17 22:11:27 +0000464 PyErr_SetString(PyExc_ValueError,
465 "compile(): unrecognised flags");
Neal Norwitz3a9a3e72005-11-27 20:38:31 +0000466 goto cleanup;
Tim Peters6cd6a822001-08-17 22:11:27 +0000467 }
468 /* XXX Warn if (supplied_flags & PyCF_MASK_OBSOLETE) != 0? */
469
Tim Peters6cd6a822001-08-17 22:11:27 +0000470 if (!dont_inherit) {
471 PyEval_MergeCompilerFlags(&cf);
472 }
Just van Rossum3aaf42c2003-02-10 08:21:10 +0000473 result = Py_CompileStringFlags(str, filename, start, &cf);
Neal Norwitz3a9a3e72005-11-27 20:38:31 +0000474cleanup:
Just van Rossum3aaf42c2003-02-10 08:21:10 +0000475 Py_XDECREF(tmp);
476 return result;
Guido van Rossum5b722181993-03-30 17:46:03 +0000477}
478
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000479PyDoc_STRVAR(compile_doc,
Tim Peters6cd6a822001-08-17 22:11:27 +0000480"compile(source, filename, mode[, flags[, dont_inherit]]) -> code object\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000481\n\
482Compile the source string (a Python module, statement or expression)\n\
483into a code object that can be executed by the exec statement or eval().\n\
484The filename will be used for run-time error messages.\n\
485The mode must be 'exec' to compile a module, 'single' to compile a\n\
Tim Peters6cd6a822001-08-17 22:11:27 +0000486single (interactive) statement, or 'eval' to compile an expression.\n\
487The flags argument, if present, controls which future statements influence\n\
488the compilation of the code.\n\
489The dont_inherit argument, if non-zero, stops the compilation inheriting\n\
490the effects of any future statements in effect in the code calling\n\
491compile; if absent or zero these statements do influence the compilation,\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000492in addition to any features explicitly specified.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000493
Guido van Rossum79f25d91997-04-29 20:08:16 +0000494static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000495builtin_dir(PyObject *self, PyObject *args)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000496{
Tim Peters5d2b77c2001-09-03 05:47:38 +0000497 PyObject *arg = NULL;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000498
Raymond Hettingerea3fdf42002-12-29 16:33:45 +0000499 if (!PyArg_UnpackTuple(args, "dir", 0, 1, &arg))
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000500 return NULL;
Tim Peters7eea37e2001-09-04 22:08:56 +0000501 return PyObject_Dir(arg);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000502}
503
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000504PyDoc_STRVAR(dir_doc,
Tim Peters5d2b77c2001-09-03 05:47:38 +0000505"dir([object]) -> list of strings\n"
506"\n"
Georg Brandl871f1bc2007-03-12 13:17:36 +0000507"If called without an argument, return the names in the current scope.\n"
508"Else, return an alphabetized list of names comprising (some of) the attributes\n"
509"of the given object, and of attributes reachable from it.\n"
510"If the object supplies a method named __dir__, it will be used; otherwise\n"
511"the default dir() logic is used and returns:\n"
512" for a module object: the module's attributes.\n"
513" for a class object: its attributes, and recursively the attributes\n"
514" of its bases.\n"
Georg Brandl3bb15672007-03-13 07:23:16 +0000515" for any other object: its attributes, its class's attributes, and\n"
Georg Brandl871f1bc2007-03-12 13:17:36 +0000516" recursively the attributes of its class's base classes.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000517
Guido van Rossum79f25d91997-04-29 20:08:16 +0000518static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000519builtin_divmod(PyObject *self, PyObject *args)
Guido van Rossum6a00cd81995-01-07 12:39:01 +0000520{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000521 PyObject *v, *w;
Guido van Rossum6a00cd81995-01-07 12:39:01 +0000522
Raymond Hettingerea3fdf42002-12-29 16:33:45 +0000523 if (!PyArg_UnpackTuple(args, "divmod", 2, 2, &v, &w))
Guido van Rossum6a00cd81995-01-07 12:39:01 +0000524 return NULL;
Guido van Rossum09df08a1998-05-22 00:51:39 +0000525 return PyNumber_Divmod(v, w);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000526}
527
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000528PyDoc_STRVAR(divmod_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000529"divmod(x, y) -> (div, mod)\n\
530\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000531Return the tuple ((x-x%y)/y, x%y). Invariant: div*y + mod == x.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000532
533
Guido van Rossum79f25d91997-04-29 20:08:16 +0000534static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000535builtin_eval(PyObject *self, PyObject *args)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000536{
Just van Rossum3aaf42c2003-02-10 08:21:10 +0000537 PyObject *cmd, *result, *tmp = NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000538 PyObject *globals = Py_None, *locals = Py_None;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000539 char *str;
Tim Peters9fa96be2001-08-17 23:04:59 +0000540 PyCompilerFlags cf;
Guido van Rossum590baa41993-11-30 13:40:46 +0000541
Raymond Hettinger214b1c32004-07-02 06:41:07 +0000542 if (!PyArg_UnpackTuple(args, "eval", 1, 3, &cmd, &globals, &locals))
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000543 return NULL;
Raymond Hettinger214b1c32004-07-02 06:41:07 +0000544 if (locals != Py_None && !PyMapping_Check(locals)) {
545 PyErr_SetString(PyExc_TypeError, "locals must be a mapping");
Raymond Hettinger513ffe82004-07-06 13:44:41 +0000546 return NULL;
Raymond Hettinger214b1c32004-07-02 06:41:07 +0000547 }
548 if (globals != Py_None && !PyDict_Check(globals)) {
Georg Brandl99d7e4e2005-08-31 22:21:15 +0000549 PyErr_SetString(PyExc_TypeError, PyMapping_Check(globals) ?
Raymond Hettinger214b1c32004-07-02 06:41:07 +0000550 "globals must be a real dict; try eval(expr, {}, mapping)"
551 : "globals must be a dict");
Raymond Hettinger513ffe82004-07-06 13:44:41 +0000552 return NULL;
Raymond Hettinger214b1c32004-07-02 06:41:07 +0000553 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000554 if (globals == Py_None) {
555 globals = PyEval_GetGlobals();
556 if (locals == Py_None)
557 locals = PyEval_GetLocals();
Guido van Rossum6135a871995-01-09 17:53:26 +0000558 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000559 else if (locals == Py_None)
Guido van Rossum6135a871995-01-09 17:53:26 +0000560 locals = globals;
Tim Peters9fa96be2001-08-17 23:04:59 +0000561
Georg Brandl77c85e62005-09-15 10:46:13 +0000562 if (globals == NULL || locals == NULL) {
563 PyErr_SetString(PyExc_TypeError,
564 "eval must be given globals and locals "
565 "when called without a frame");
566 return NULL;
567 }
568
Guido van Rossum79f25d91997-04-29 20:08:16 +0000569 if (PyDict_GetItemString(globals, "__builtins__") == NULL) {
570 if (PyDict_SetItemString(globals, "__builtins__",
571 PyEval_GetBuiltins()) != 0)
Guido van Rossum6135a871995-01-09 17:53:26 +0000572 return NULL;
573 }
Tim Peters9fa96be2001-08-17 23:04:59 +0000574
Jeremy Hylton15c1c4f2001-07-30 21:50:55 +0000575 if (PyCode_Check(cmd)) {
Jeremy Hylton733c8932001-12-13 19:51:56 +0000576 if (PyCode_GetNumFree((PyCodeObject *)cmd) > 0) {
Jeremy Hylton15c1c4f2001-07-30 21:50:55 +0000577 PyErr_SetString(PyExc_TypeError,
578 "code object passed to eval() may not contain free variables");
579 return NULL;
580 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000581 return PyEval_EvalCode((PyCodeObject *) cmd, globals, locals);
Jeremy Hylton15c1c4f2001-07-30 21:50:55 +0000582 }
Tim Peters9fa96be2001-08-17 23:04:59 +0000583
Marc-André Lemburgd1ba4432000-09-19 21:04:18 +0000584 if (!PyString_Check(cmd) &&
585 !PyUnicode_Check(cmd)) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000586 PyErr_SetString(PyExc_TypeError,
Fred Drake661ea262000-10-24 19:57:45 +0000587 "eval() arg 1 must be a string or code object");
Guido van Rossum94390a41992-08-14 15:14:30 +0000588 return NULL;
589 }
Just van Rossum3aaf42c2003-02-10 08:21:10 +0000590 cf.cf_flags = 0;
591
592#ifdef Py_USING_UNICODE
593 if (PyUnicode_Check(cmd)) {
594 tmp = PyUnicode_AsUTF8String(cmd);
595 if (tmp == NULL)
596 return NULL;
597 cmd = tmp;
598 cf.cf_flags |= PyCF_SOURCE_IS_UTF8;
599 }
600#endif
Neal Norwitz3a9a3e72005-11-27 20:38:31 +0000601 if (PyString_AsStringAndSize(cmd, &str, NULL)) {
602 Py_XDECREF(tmp);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000603 return NULL;
Neal Norwitz3a9a3e72005-11-27 20:38:31 +0000604 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000605 while (*str == ' ' || *str == '\t')
606 str++;
Tim Peters9fa96be2001-08-17 23:04:59 +0000607
Tim Peters9fa96be2001-08-17 23:04:59 +0000608 (void)PyEval_MergeCompilerFlags(&cf);
Just van Rossum3aaf42c2003-02-10 08:21:10 +0000609 result = PyRun_StringFlags(str, Py_eval_input, globals, locals, &cf);
610 Py_XDECREF(tmp);
611 return result;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000612}
613
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000614PyDoc_STRVAR(eval_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000615"eval(source[, globals[, locals]]) -> value\n\
616\n\
617Evaluate the source in the context of globals and locals.\n\
618The source may be a string representing a Python expression\n\
619or a code object as returned by compile().\n\
Neal Norwitz477ca1c2006-09-05 02:25:41 +0000620The globals must be a dictionary and locals can be any mapping,\n\
Raymond Hettinger214b1c32004-07-02 06:41:07 +0000621defaulting to the current globals and locals.\n\
622If only globals is given, locals defaults to it.\n");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000623
624
Guido van Rossum79f25d91997-04-29 20:08:16 +0000625static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000626builtin_execfile(PyObject *self, PyObject *args)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000627{
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000628 char *filename;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000629 PyObject *globals = Py_None, *locals = Py_None;
630 PyObject *res;
Guido van Rossumb3a639e2001-09-05 13:37:47 +0000631 FILE* fp = NULL;
Tim Peters5ba58662001-07-16 02:29:45 +0000632 PyCompilerFlags cf;
Martin v. Löwis6b3a2c42001-08-08 05:30:36 +0000633 int exists;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000634
Neal Norwitzdf25efe2007-05-23 06:58:36 +0000635 if (Py_Py3kWarningFlag &&
636 PyErr_Warn(PyExc_DeprecationWarning,
637 "execfile() not supported in 3.x") < 0)
638 return NULL;
639
Raymond Hettinger66bd2332004-08-02 08:30:07 +0000640 if (!PyArg_ParseTuple(args, "s|O!O:execfile",
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000641 &filename,
Guido van Rossum79f25d91997-04-29 20:08:16 +0000642 &PyDict_Type, &globals,
Raymond Hettinger66bd2332004-08-02 08:30:07 +0000643 &locals))
Guido van Rossum0f61f8a1992-02-25 18:55:05 +0000644 return NULL;
Raymond Hettinger66bd2332004-08-02 08:30:07 +0000645 if (locals != Py_None && !PyMapping_Check(locals)) {
646 PyErr_SetString(PyExc_TypeError, "locals must be a mapping");
647 return NULL;
648 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000649 if (globals == Py_None) {
650 globals = PyEval_GetGlobals();
651 if (locals == Py_None)
652 locals = PyEval_GetLocals();
Guido van Rossum6135a871995-01-09 17:53:26 +0000653 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000654 else if (locals == Py_None)
Guido van Rossum6135a871995-01-09 17:53:26 +0000655 locals = globals;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000656 if (PyDict_GetItemString(globals, "__builtins__") == NULL) {
657 if (PyDict_SetItemString(globals, "__builtins__",
658 PyEval_GetBuiltins()) != 0)
Guido van Rossum6135a871995-01-09 17:53:26 +0000659 return NULL;
660 }
Martin v. Löwis6b3a2c42001-08-08 05:30:36 +0000661
662 exists = 0;
663 /* Test for existence or directory. */
Martin v. Löwis3484a182002-03-09 12:07:51 +0000664#if defined(PLAN9)
665 {
666 Dir *d;
667
668 if ((d = dirstat(filename))!=nil) {
669 if(d->mode & DMDIR)
670 werrstr("is a directory");
671 else
672 exists = 1;
673 free(d);
674 }
Martin v. Löwis6b3a2c42001-08-08 05:30:36 +0000675 }
Martin v. Löwis3484a182002-03-09 12:07:51 +0000676#elif defined(RISCOS)
Guido van Rossume2ae77b2001-10-24 20:42:55 +0000677 if (object_exists(filename)) {
678 if (isdir(filename))
679 errno = EISDIR;
680 else
681 exists = 1;
682 }
Martin v. Löwis3484a182002-03-09 12:07:51 +0000683#else /* standard Posix */
684 {
685 struct stat s;
686 if (stat(filename, &s) == 0) {
687 if (S_ISDIR(s.st_mode))
Andrew MacIntyreda4d6cb2004-03-29 11:53:38 +0000688# if defined(PYOS_OS2) && defined(PYCC_VACPP)
Martin v. Löwis3484a182002-03-09 12:07:51 +0000689 errno = EOS2ERR;
690# else
691 errno = EISDIR;
692# endif
693 else
694 exists = 1;
695 }
696 }
697#endif
Martin v. Löwis6b3a2c42001-08-08 05:30:36 +0000698
699 if (exists) {
700 Py_BEGIN_ALLOW_THREADS
Jack Jansen7b8c7542002-04-14 20:12:41 +0000701 fp = fopen(filename, "r" PY_STDIOTEXTMODE);
Martin v. Löwis6b3a2c42001-08-08 05:30:36 +0000702 Py_END_ALLOW_THREADS
703
704 if (fp == NULL) {
705 exists = 0;
706 }
707 }
708
709 if (!exists) {
Peter Schneider-Kamp4c013422002-08-27 16:58:00 +0000710 PyErr_SetFromErrnoWithFilename(PyExc_IOError, filename);
Guido van Rossum0f61f8a1992-02-25 18:55:05 +0000711 return NULL;
712 }
Tim Peters5ba58662001-07-16 02:29:45 +0000713 cf.cf_flags = 0;
714 if (PyEval_MergeCompilerFlags(&cf))
Tim Peters748b8bb2001-04-28 08:20:22 +0000715 res = PyRun_FileExFlags(fp, filename, Py_file_input, globals,
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000716 locals, 1, &cf);
Tim Peters5ba58662001-07-16 02:29:45 +0000717 else
Tim Peters748b8bb2001-04-28 08:20:22 +0000718 res = PyRun_FileEx(fp, filename, Py_file_input, globals,
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000719 locals, 1);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000720 return res;
Guido van Rossum0f61f8a1992-02-25 18:55:05 +0000721}
722
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000723PyDoc_STRVAR(execfile_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000724"execfile(filename[, globals[, locals]])\n\
725\n\
726Read and execute a Python script from a file.\n\
727The globals and locals are dictionaries, defaulting to the current\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000728globals and locals. If only globals is given, locals defaults to it.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000729
730
Guido van Rossum79f25d91997-04-29 20:08:16 +0000731static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000732builtin_getattr(PyObject *self, PyObject *args)
Guido van Rossum33894be1992-01-27 16:53:09 +0000733{
Guido van Rossum950ff291998-06-29 13:38:57 +0000734 PyObject *v, *result, *dflt = NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000735 PyObject *name;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000736
Raymond Hettingerea3fdf42002-12-29 16:33:45 +0000737 if (!PyArg_UnpackTuple(args, "getattr", 2, 3, &v, &name, &dflt))
Guido van Rossum33894be1992-01-27 16:53:09 +0000738 return NULL;
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000739#ifdef Py_USING_UNICODE
Jeremy Hylton0eb11152001-07-30 22:39:31 +0000740 if (PyUnicode_Check(name)) {
741 name = _PyUnicode_AsDefaultEncodedString(name, NULL);
742 if (name == NULL)
743 return NULL;
744 }
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000745#endif
Jeremy Hylton0eb11152001-07-30 22:39:31 +0000746
747 if (!PyString_Check(name)) {
748 PyErr_SetString(PyExc_TypeError,
Alex Martellia9b9c9f2003-04-23 13:34:35 +0000749 "getattr(): attribute name must be string");
Jeremy Hylton0eb11152001-07-30 22:39:31 +0000750 return NULL;
751 }
Guido van Rossum950ff291998-06-29 13:38:57 +0000752 result = PyObject_GetAttr(v, name);
Guido van Rossumd8923572001-10-16 21:31:32 +0000753 if (result == NULL && dflt != NULL &&
754 PyErr_ExceptionMatches(PyExc_AttributeError))
755 {
Guido van Rossum950ff291998-06-29 13:38:57 +0000756 PyErr_Clear();
757 Py_INCREF(dflt);
758 result = dflt;
759 }
760 return result;
Guido van Rossum9bfef441993-03-29 10:43:31 +0000761}
762
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000763PyDoc_STRVAR(getattr_doc,
Guido van Rossum950ff291998-06-29 13:38:57 +0000764"getattr(object, name[, default]) -> value\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000765\n\
Guido van Rossum950ff291998-06-29 13:38:57 +0000766Get a named attribute from an object; getattr(x, 'y') is equivalent to x.y.\n\
767When a default argument is given, it is returned when the attribute doesn't\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000768exist; without it, an exception is raised in that case.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000769
770
Guido van Rossum79f25d91997-04-29 20:08:16 +0000771static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000772builtin_globals(PyObject *self)
Guido van Rossum872537c1995-07-07 22:43:42 +0000773{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000774 PyObject *d;
Guido van Rossum872537c1995-07-07 22:43:42 +0000775
Guido van Rossum79f25d91997-04-29 20:08:16 +0000776 d = PyEval_GetGlobals();
Neal Norwitz43bd4db2006-08-12 01:46:42 +0000777 Py_XINCREF(d);
Guido van Rossum872537c1995-07-07 22:43:42 +0000778 return d;
779}
780
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000781PyDoc_STRVAR(globals_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000782"globals() -> dictionary\n\
783\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000784Return the dictionary containing the current scope's global variables.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000785
786
Guido van Rossum79f25d91997-04-29 20:08:16 +0000787static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000788builtin_hasattr(PyObject *self, PyObject *args)
Guido van Rossum9bfef441993-03-29 10:43:31 +0000789{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000790 PyObject *v;
791 PyObject *name;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000792
Raymond Hettingerea3fdf42002-12-29 16:33:45 +0000793 if (!PyArg_UnpackTuple(args, "hasattr", 2, 2, &v, &name))
Guido van Rossum9bfef441993-03-29 10:43:31 +0000794 return NULL;
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000795#ifdef Py_USING_UNICODE
Jeremy Hylton302b54a2001-07-30 22:45:19 +0000796 if (PyUnicode_Check(name)) {
797 name = _PyUnicode_AsDefaultEncodedString(name, NULL);
798 if (name == NULL)
799 return NULL;
800 }
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000801#endif
Jeremy Hylton302b54a2001-07-30 22:45:19 +0000802
803 if (!PyString_Check(name)) {
804 PyErr_SetString(PyExc_TypeError,
Alex Martellia9b9c9f2003-04-23 13:34:35 +0000805 "hasattr(): attribute name must be string");
Jeremy Hylton302b54a2001-07-30 22:45:19 +0000806 return NULL;
807 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000808 v = PyObject_GetAttr(v, name);
Guido van Rossum9bfef441993-03-29 10:43:31 +0000809 if (v == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000810 PyErr_Clear();
Guido van Rossum09df08a1998-05-22 00:51:39 +0000811 Py_INCREF(Py_False);
812 return Py_False;
Guido van Rossum9bfef441993-03-29 10:43:31 +0000813 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000814 Py_DECREF(v);
Guido van Rossum09df08a1998-05-22 00:51:39 +0000815 Py_INCREF(Py_True);
816 return Py_True;
Guido van Rossum33894be1992-01-27 16:53:09 +0000817}
818
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000819PyDoc_STRVAR(hasattr_doc,
Guido van Rossum77f6a652002-04-03 22:41:51 +0000820"hasattr(object, name) -> bool\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000821\n\
822Return whether the object has an attribute with the given name.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000823(This is done by calling getattr(object, name) and catching exceptions.)");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000824
825
Guido van Rossum79f25d91997-04-29 20:08:16 +0000826static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000827builtin_id(PyObject *self, PyObject *v)
Guido van Rossum5b722181993-03-30 17:46:03 +0000828{
Guido van Rossum106f2da2000-06-28 21:12:25 +0000829 return PyLong_FromVoidPtr(v);
Guido van Rossum5b722181993-03-30 17:46:03 +0000830}
831
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000832PyDoc_STRVAR(id_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000833"id(object) -> integer\n\
834\n\
835Return the identity of an object. This is guaranteed to be unique among\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000836simultaneously existing objects. (Hint: it's the object's memory address.)");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000837
838
Guido van Rossum79f25d91997-04-29 20:08:16 +0000839static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000840builtin_map(PyObject *self, PyObject *args)
Guido van Rossum12d12c51993-10-26 17:58:25 +0000841{
842 typedef struct {
Tim Peters4e9afdc2001-05-03 23:54:49 +0000843 PyObject *it; /* the iterator object */
844 int saw_StopIteration; /* bool: did the iterator end? */
Guido van Rossum12d12c51993-10-26 17:58:25 +0000845 } sequence;
846
Guido van Rossum79f25d91997-04-29 20:08:16 +0000847 PyObject *func, *result;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000848 sequence *seqs = NULL, *sqp;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000849 Py_ssize_t n, len;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000850 register int i, j;
851
Guido van Rossum79f25d91997-04-29 20:08:16 +0000852 n = PyTuple_Size(args);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000853 if (n < 2) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000854 PyErr_SetString(PyExc_TypeError,
855 "map() requires at least two args");
Guido van Rossum12d12c51993-10-26 17:58:25 +0000856 return NULL;
857 }
858
Guido van Rossum79f25d91997-04-29 20:08:16 +0000859 func = PyTuple_GetItem(args, 0);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000860 n--;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000861
Guido van Rossumfa4ac711998-07-10 17:37:30 +0000862 if (func == Py_None && n == 1) {
863 /* map(None, S) is the same as list(S). */
864 return PySequence_List(PyTuple_GetItem(args, 1));
865 }
866
Tim Peters4e9afdc2001-05-03 23:54:49 +0000867 /* Get space for sequence descriptors. Must NULL out the iterator
868 * pointers so that jumping to Fail_2 later doesn't see trash.
869 */
Guido van Rossum79f25d91997-04-29 20:08:16 +0000870 if ((seqs = PyMem_NEW(sequence, n)) == NULL) {
871 PyErr_NoMemory();
Tim Peters4e9afdc2001-05-03 23:54:49 +0000872 return NULL;
873 }
874 for (i = 0; i < n; ++i) {
875 seqs[i].it = (PyObject*)NULL;
876 seqs[i].saw_StopIteration = 0;
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000877 }
Guido van Rossum12d12c51993-10-26 17:58:25 +0000878
Tim Peters4e9afdc2001-05-03 23:54:49 +0000879 /* Do a first pass to obtain iterators for the arguments, and set len
880 * to the largest of their lengths.
Tim Peters748b8bb2001-04-28 08:20:22 +0000881 */
Tim Peters4e9afdc2001-05-03 23:54:49 +0000882 len = 0;
883 for (i = 0, sqp = seqs; i < n; ++i, ++sqp) {
884 PyObject *curseq;
Martin v. Löwisd96ee902006-02-16 14:37:16 +0000885 Py_ssize_t curlen;
Guido van Rossumad991772001-01-12 16:03:05 +0000886
Tim Peters4e9afdc2001-05-03 23:54:49 +0000887 /* Get iterator. */
888 curseq = PyTuple_GetItem(args, i+1);
889 sqp->it = PyObject_GetIter(curseq);
890 if (sqp->it == NULL) {
Guido van Rossum12d12c51993-10-26 17:58:25 +0000891 static char errmsg[] =
Tim Peters4e9afdc2001-05-03 23:54:49 +0000892 "argument %d to map() must support iteration";
Guido van Rossum15e33a41997-04-30 19:00:27 +0000893 char errbuf[sizeof(errmsg) + 25];
Jeremy Hylton518ab1c2001-11-28 20:42:20 +0000894 PyOS_snprintf(errbuf, sizeof(errbuf), errmsg, i+2);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000895 PyErr_SetString(PyExc_TypeError, errbuf);
Guido van Rossum12d12c51993-10-26 17:58:25 +0000896 goto Fail_2;
897 }
898
Tim Peters4e9afdc2001-05-03 23:54:49 +0000899 /* Update len. */
Raymond Hettinger4e2f7142007-12-06 00:56:53 +0000900 curlen = _PyObject_LengthHint(curseq, 8);
Raymond Hettinger77f3c872004-01-04 08:54:44 +0000901 if (curlen > len)
902 len = curlen;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000903 }
904
Tim Peters4e9afdc2001-05-03 23:54:49 +0000905 /* Get space for the result list. */
Guido van Rossum79f25d91997-04-29 20:08:16 +0000906 if ((result = (PyObject *) PyList_New(len)) == NULL)
Guido van Rossum12d12c51993-10-26 17:58:25 +0000907 goto Fail_2;
908
Tim Peters4e9afdc2001-05-03 23:54:49 +0000909 /* Iterate over the sequences until all have stopped. */
Guido van Rossum2d951851994-08-29 12:52:16 +0000910 for (i = 0; ; ++i) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000911 PyObject *alist, *item=NULL, *value;
Tim Peters4e9afdc2001-05-03 23:54:49 +0000912 int numactive = 0;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000913
Guido van Rossum79f25d91997-04-29 20:08:16 +0000914 if (func == Py_None && n == 1)
Guido van Rossum32120311995-07-10 13:52:21 +0000915 alist = NULL;
Tim Peters4e9afdc2001-05-03 23:54:49 +0000916 else if ((alist = PyTuple_New(n)) == NULL)
917 goto Fail_1;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000918
919 for (j = 0, sqp = seqs; j < n; ++j, ++sqp) {
Tim Peters4e9afdc2001-05-03 23:54:49 +0000920 if (sqp->saw_StopIteration) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000921 Py_INCREF(Py_None);
922 item = Py_None;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000923 }
Guido van Rossum12d12c51993-10-26 17:58:25 +0000924 else {
Tim Peters4e9afdc2001-05-03 23:54:49 +0000925 item = PyIter_Next(sqp->it);
926 if (item)
927 ++numactive;
928 else {
Tim Peters4e9afdc2001-05-03 23:54:49 +0000929 if (PyErr_Occurred()) {
Tim Petersf4848da2001-05-05 00:14:56 +0000930 Py_XDECREF(alist);
931 goto Fail_1;
Guido van Rossum2d951851994-08-29 12:52:16 +0000932 }
Tim Peters4e9afdc2001-05-03 23:54:49 +0000933 Py_INCREF(Py_None);
934 item = Py_None;
935 sqp->saw_StopIteration = 1;
Guido van Rossum2d951851994-08-29 12:52:16 +0000936 }
Guido van Rossum12d12c51993-10-26 17:58:25 +0000937 }
Tim Peters4e9afdc2001-05-03 23:54:49 +0000938 if (alist)
939 PyTuple_SET_ITEM(alist, j, item);
940 else
Guido van Rossum2d951851994-08-29 12:52:16 +0000941 break;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000942 }
943
Guido van Rossum32120311995-07-10 13:52:21 +0000944 if (!alist)
945 alist = item;
Guido van Rossum2d951851994-08-29 12:52:16 +0000946
Tim Peters4e9afdc2001-05-03 23:54:49 +0000947 if (numactive == 0) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000948 Py_DECREF(alist);
Guido van Rossum2d951851994-08-29 12:52:16 +0000949 break;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000950 }
Guido van Rossum2d951851994-08-29 12:52:16 +0000951
Guido van Rossum79f25d91997-04-29 20:08:16 +0000952 if (func == Py_None)
Guido van Rossum32120311995-07-10 13:52:21 +0000953 value = alist;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000954 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000955 value = PyEval_CallObject(func, alist);
956 Py_DECREF(alist);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000957 if (value == NULL)
958 goto Fail_1;
Guido van Rossum2d951851994-08-29 12:52:16 +0000959 }
960 if (i >= len) {
Barry Warsawfa77e091999-01-28 18:49:12 +0000961 int status = PyList_Append(result, value);
Barry Warsaw21332871999-01-28 04:21:35 +0000962 Py_DECREF(value);
Barry Warsawfa77e091999-01-28 18:49:12 +0000963 if (status < 0)
964 goto Fail_1;
Guido van Rossum2d951851994-08-29 12:52:16 +0000965 }
Tim Peters4e9afdc2001-05-03 23:54:49 +0000966 else if (PyList_SetItem(result, i, value) < 0)
967 goto Fail_1;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000968 }
969
Guido van Rossumfa4ac711998-07-10 17:37:30 +0000970 if (i < len && PyList_SetSlice(result, i, len, NULL) < 0)
971 goto Fail_1;
972
Tim Peters4e9afdc2001-05-03 23:54:49 +0000973 goto Succeed;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000974
Guido van Rossum12d12c51993-10-26 17:58:25 +0000975Fail_1:
Guido van Rossum79f25d91997-04-29 20:08:16 +0000976 Py_DECREF(result);
Guido van Rossum12d12c51993-10-26 17:58:25 +0000977Fail_2:
Tim Peters4e9afdc2001-05-03 23:54:49 +0000978 result = NULL;
979Succeed:
980 assert(seqs);
981 for (i = 0; i < n; ++i)
982 Py_XDECREF(seqs[i].it);
983 PyMem_DEL(seqs);
984 return result;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000985}
986
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000987PyDoc_STRVAR(map_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000988"map(function, sequence[, sequence, ...]) -> list\n\
989\n\
990Return a list of the results of applying the function to the items of\n\
991the argument sequence(s). If more than one sequence is given, the\n\
992function is called with an argument list consisting of the corresponding\n\
993item of each sequence, substituting None for missing values when not all\n\
994sequences have the same length. If the function is None, return a list of\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000995the items of the sequence (or a list of tuples if more than one sequence).");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000996
997
Guido van Rossum79f25d91997-04-29 20:08:16 +0000998static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000999builtin_setattr(PyObject *self, PyObject *args)
Guido van Rossum33894be1992-01-27 16:53:09 +00001000{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001001 PyObject *v;
1002 PyObject *name;
1003 PyObject *value;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001004
Raymond Hettingerea3fdf42002-12-29 16:33:45 +00001005 if (!PyArg_UnpackTuple(args, "setattr", 3, 3, &v, &name, &value))
Guido van Rossum33894be1992-01-27 16:53:09 +00001006 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001007 if (PyObject_SetAttr(v, name, value) != 0)
Guido van Rossum33894be1992-01-27 16:53:09 +00001008 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001009 Py_INCREF(Py_None);
1010 return Py_None;
Guido van Rossum33894be1992-01-27 16:53:09 +00001011}
1012
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001013PyDoc_STRVAR(setattr_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001014"setattr(object, name, value)\n\
1015\n\
1016Set a named attribute on an object; setattr(x, 'y', v) is equivalent to\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001017``x.y = v''.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001018
1019
Guido van Rossum79f25d91997-04-29 20:08:16 +00001020static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001021builtin_delattr(PyObject *self, PyObject *args)
Guido van Rossum14144fc1994-08-29 12:53:40 +00001022{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001023 PyObject *v;
1024 PyObject *name;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001025
Raymond Hettingerea3fdf42002-12-29 16:33:45 +00001026 if (!PyArg_UnpackTuple(args, "delattr", 2, 2, &v, &name))
Guido van Rossum14144fc1994-08-29 12:53:40 +00001027 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001028 if (PyObject_SetAttr(v, name, (PyObject *)NULL) != 0)
Guido van Rossum14144fc1994-08-29 12:53:40 +00001029 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001030 Py_INCREF(Py_None);
1031 return Py_None;
Guido van Rossum14144fc1994-08-29 12:53:40 +00001032}
1033
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001034PyDoc_STRVAR(delattr_doc,
Guido van Rossumdf12a591998-11-23 22:13:04 +00001035"delattr(object, name)\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001036\n\
1037Delete a named attribute on an object; delattr(x, 'y') is equivalent to\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001038``del x.y''.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001039
1040
Guido van Rossum79f25d91997-04-29 20:08:16 +00001041static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001042builtin_hash(PyObject *self, PyObject *v)
Guido van Rossum9bfef441993-03-29 10:43:31 +00001043{
Guido van Rossum9bfef441993-03-29 10:43:31 +00001044 long x;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001045
Guido van Rossum79f25d91997-04-29 20:08:16 +00001046 x = PyObject_Hash(v);
Guido van Rossum9bfef441993-03-29 10:43:31 +00001047 if (x == -1)
1048 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001049 return PyInt_FromLong(x);
Guido van Rossum9bfef441993-03-29 10:43:31 +00001050}
1051
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001052PyDoc_STRVAR(hash_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001053"hash(object) -> integer\n\
1054\n\
1055Return a hash value for the object. Two objects with the same value have\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001056the same hash value. The reverse is not necessarily true, but likely.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001057
1058
Guido van Rossum79f25d91997-04-29 20:08:16 +00001059static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001060builtin_hex(PyObject *self, PyObject *v)
Guido van Rossum006bcd41991-10-24 14:54:44 +00001061{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001062 PyNumberMethods *nb;
Neil Schemenauer3a313e32004-07-19 16:29:17 +00001063 PyObject *res;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001064
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001065 if ((nb = v->ob_type->tp_as_number) == NULL ||
Guido van Rossum1899c2e1992-09-12 11:09:23 +00001066 nb->nb_hex == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001067 PyErr_SetString(PyExc_TypeError,
Guido van Rossum1899c2e1992-09-12 11:09:23 +00001068 "hex() argument can't be converted to hex");
1069 return NULL;
Guido van Rossum006bcd41991-10-24 14:54:44 +00001070 }
Neil Schemenauer3a313e32004-07-19 16:29:17 +00001071 res = (*nb->nb_hex)(v);
1072 if (res && !PyString_Check(res)) {
1073 PyErr_Format(PyExc_TypeError,
1074 "__hex__ returned non-string (type %.200s)",
1075 res->ob_type->tp_name);
1076 Py_DECREF(res);
1077 return NULL;
1078 }
1079 return res;
Guido van Rossum006bcd41991-10-24 14:54:44 +00001080}
1081
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001082PyDoc_STRVAR(hex_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001083"hex(number) -> string\n\
1084\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001085Return the hexadecimal representation of an integer or long integer.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001086
1087
Tim Petersdbd9ba62000-07-09 03:09:57 +00001088static PyObject *builtin_raw_input(PyObject *, PyObject *);
Guido van Rossum3165fe61992-09-25 21:59:05 +00001089
Guido van Rossum79f25d91997-04-29 20:08:16 +00001090static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001091builtin_input(PyObject *self, PyObject *args)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001092{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001093 PyObject *line;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001094 char *str;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001095 PyObject *res;
1096 PyObject *globals, *locals;
Hye-Shik Changff83c2b2004-02-02 13:39:01 +00001097 PyCompilerFlags cf;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001098
1099 line = builtin_raw_input(self, args);
Guido van Rossum3165fe61992-09-25 21:59:05 +00001100 if (line == NULL)
1101 return line;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001102 if (!PyArg_Parse(line, "s;embedded '\\0' in input line", &str))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001103 return NULL;
1104 while (*str == ' ' || *str == '\t')
1105 str++;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001106 globals = PyEval_GetGlobals();
1107 locals = PyEval_GetLocals();
1108 if (PyDict_GetItemString(globals, "__builtins__") == NULL) {
1109 if (PyDict_SetItemString(globals, "__builtins__",
1110 PyEval_GetBuiltins()) != 0)
Guido van Rossum6135a871995-01-09 17:53:26 +00001111 return NULL;
1112 }
Hye-Shik Changff83c2b2004-02-02 13:39:01 +00001113 cf.cf_flags = 0;
1114 PyEval_MergeCompilerFlags(&cf);
1115 res = PyRun_StringFlags(str, Py_eval_input, globals, locals, &cf);
Guido van Rossum79f25d91997-04-29 20:08:16 +00001116 Py_DECREF(line);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001117 return res;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001118}
1119
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001120PyDoc_STRVAR(input_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001121"input([prompt]) -> value\n\
1122\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001123Equivalent to eval(raw_input(prompt)).");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001124
1125
Guido van Rossume8811f81997-02-14 15:48:05 +00001126static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001127builtin_intern(PyObject *self, PyObject *args)
Guido van Rossume8811f81997-02-14 15:48:05 +00001128{
1129 PyObject *s;
Guido van Rossum43713e52000-02-29 13:59:29 +00001130 if (!PyArg_ParseTuple(args, "S:intern", &s))
Guido van Rossume8811f81997-02-14 15:48:05 +00001131 return NULL;
Jeremy Hylton4c989dd2004-08-07 19:20:05 +00001132 if (!PyString_CheckExact(s)) {
1133 PyErr_SetString(PyExc_TypeError,
1134 "can't intern subclass of string");
1135 return NULL;
1136 }
Guido van Rossume8811f81997-02-14 15:48:05 +00001137 Py_INCREF(s);
1138 PyString_InternInPlace(&s);
1139 return s;
1140}
1141
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001142PyDoc_STRVAR(intern_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001143"intern(string) -> string\n\
1144\n\
1145``Intern'' the given string. This enters the string in the (global)\n\
1146table of interned strings whose purpose is to speed up dictionary lookups.\n\
1147Return the string itself or the previously interned string object with the\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001148same value.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001149
1150
Guido van Rossum79f25d91997-04-29 20:08:16 +00001151static PyObject *
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001152builtin_iter(PyObject *self, PyObject *args)
1153{
1154 PyObject *v, *w = NULL;
1155
Raymond Hettingerea3fdf42002-12-29 16:33:45 +00001156 if (!PyArg_UnpackTuple(args, "iter", 1, 2, &v, &w))
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001157 return NULL;
1158 if (w == NULL)
1159 return PyObject_GetIter(v);
1160 if (!PyCallable_Check(v)) {
1161 PyErr_SetString(PyExc_TypeError,
1162 "iter(v, w): v must be callable");
1163 return NULL;
1164 }
1165 return PyCallIter_New(v, w);
1166}
1167
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001168PyDoc_STRVAR(iter_doc,
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001169"iter(collection) -> iterator\n\
1170iter(callable, sentinel) -> iterator\n\
1171\n\
1172Get an iterator from an object. In the first form, the argument must\n\
1173supply its own iterator, or be a sequence.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001174In the second form, the callable is called until it returns the sentinel.");
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001175
1176
1177static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001178builtin_len(PyObject *self, PyObject *v)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001179{
Martin v. Löwis18e16552006-02-15 17:27:45 +00001180 Py_ssize_t res;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001181
Jeremy Hylton03657cf2000-07-12 13:05:33 +00001182 res = PyObject_Size(v);
Guido van Rossum8ea9f4d1998-06-29 22:26:50 +00001183 if (res < 0 && PyErr_Occurred())
1184 return NULL;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001185 return PyInt_FromSsize_t(res);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001186}
1187
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001188PyDoc_STRVAR(len_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001189"len(object) -> integer\n\
1190\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001191Return the number of items of a sequence or mapping.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001192
1193
Guido van Rossum79f25d91997-04-29 20:08:16 +00001194static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001195builtin_locals(PyObject *self)
Guido van Rossum872537c1995-07-07 22:43:42 +00001196{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001197 PyObject *d;
Guido van Rossum872537c1995-07-07 22:43:42 +00001198
Guido van Rossum79f25d91997-04-29 20:08:16 +00001199 d = PyEval_GetLocals();
Neal Norwitz43bd4db2006-08-12 01:46:42 +00001200 Py_XINCREF(d);
Guido van Rossum872537c1995-07-07 22:43:42 +00001201 return d;
1202}
1203
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001204PyDoc_STRVAR(locals_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001205"locals() -> dictionary\n\
1206\n\
Raymond Hettinger69bf8f32003-01-04 02:16:22 +00001207Update and return a dictionary containing the current scope's local variables.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001208
1209
Guido van Rossum79f25d91997-04-29 20:08:16 +00001210static PyObject *
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001211min_max(PyObject *args, PyObject *kwds, int op)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001212{
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001213 PyObject *v, *it, *item, *val, *maxitem, *maxval, *keyfunc=NULL;
Martin v. Löwisfd39ad42004-08-12 14:42:37 +00001214 const char *name = op == Py_LT ? "min" : "max";
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001215
Guido van Rossum79f25d91997-04-29 20:08:16 +00001216 if (PyTuple_Size(args) > 1)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001217 v = args;
Martin v. Löwisfd39ad42004-08-12 14:42:37 +00001218 else if (!PyArg_UnpackTuple(args, (char *)name, 1, 1, &v))
Guido van Rossum3f5da241990-12-20 15:06:42 +00001219 return NULL;
Tim Peters67d687a2002-04-29 21:27:32 +00001220
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001221 if (kwds != NULL && PyDict_Check(kwds) && PyDict_Size(kwds)) {
1222 keyfunc = PyDict_GetItemString(kwds, "key");
1223 if (PyDict_Size(kwds)!=1 || keyfunc == NULL) {
Georg Brandl99d7e4e2005-08-31 22:21:15 +00001224 PyErr_Format(PyExc_TypeError,
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001225 "%s() got an unexpected keyword argument", name);
1226 return NULL;
1227 }
Georg Brandl99d7e4e2005-08-31 22:21:15 +00001228 }
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001229
Tim Petersc3074532001-05-03 07:00:32 +00001230 it = PyObject_GetIter(v);
1231 if (it == NULL)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001232 return NULL;
Tim Petersc3074532001-05-03 07:00:32 +00001233
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001234 maxitem = NULL; /* the result */
1235 maxval = NULL; /* the value associated with the result */
Brett Cannon9e635cf2004-12-07 00:25:35 +00001236 while (( item = PyIter_Next(it) )) {
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001237 /* get the value from the key function */
1238 if (keyfunc != NULL) {
1239 val = PyObject_CallFunctionObjArgs(keyfunc, item, NULL);
1240 if (val == NULL)
1241 goto Fail_it_item;
1242 }
1243 /* no key function; the value is the item */
1244 else {
1245 val = item;
1246 Py_INCREF(val);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001247 }
Tim Petersc3074532001-05-03 07:00:32 +00001248
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001249 /* maximum value and item are unset; set them */
1250 if (maxval == NULL) {
1251 maxitem = item;
1252 maxval = val;
1253 }
1254 /* maximum value and item are set; update them as necessary */
Guido van Rossum2d951851994-08-29 12:52:16 +00001255 else {
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001256 int cmp = PyObject_RichCompareBool(val, maxval, op);
1257 if (cmp < 0)
1258 goto Fail_it_item_and_val;
1259 else if (cmp > 0) {
1260 Py_DECREF(maxval);
1261 Py_DECREF(maxitem);
1262 maxval = val;
1263 maxitem = item;
Guido van Rossum53451b32001-01-17 15:47:24 +00001264 }
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001265 else {
1266 Py_DECREF(item);
1267 Py_DECREF(val);
Guido van Rossumc8b6df91997-05-23 00:06:51 +00001268 }
Guido van Rossum2d951851994-08-29 12:52:16 +00001269 }
Guido van Rossum3f5da241990-12-20 15:06:42 +00001270 }
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001271 if (PyErr_Occurred())
1272 goto Fail_it;
1273 if (maxval == NULL) {
Martin v. Löwisfd39ad42004-08-12 14:42:37 +00001274 PyErr_Format(PyExc_ValueError,
1275 "%s() arg is an empty sequence", name);
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001276 assert(maxitem == NULL);
1277 }
1278 else
1279 Py_DECREF(maxval);
Tim Petersc3074532001-05-03 07:00:32 +00001280 Py_DECREF(it);
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001281 return maxitem;
1282
1283Fail_it_item_and_val:
1284 Py_DECREF(val);
1285Fail_it_item:
1286 Py_DECREF(item);
1287Fail_it:
1288 Py_XDECREF(maxval);
1289 Py_XDECREF(maxitem);
1290 Py_DECREF(it);
1291 return NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001292}
1293
Guido van Rossum79f25d91997-04-29 20:08:16 +00001294static PyObject *
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001295builtin_min(PyObject *self, PyObject *args, PyObject *kwds)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001296{
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001297 return min_max(args, kwds, Py_LT);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001298}
1299
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001300PyDoc_STRVAR(min_doc,
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001301"min(iterable[, key=func]) -> value\n\
1302min(a, b, c, ...[, key=func]) -> value\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001303\n\
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001304With a single iterable argument, return its smallest item.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001305With two or more arguments, return the smallest argument.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001306
1307
Guido van Rossum79f25d91997-04-29 20:08:16 +00001308static PyObject *
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001309builtin_max(PyObject *self, PyObject *args, PyObject *kwds)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001310{
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001311 return min_max(args, kwds, Py_GT);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001312}
1313
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001314PyDoc_STRVAR(max_doc,
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001315"max(iterable[, key=func]) -> value\n\
1316max(a, b, c, ...[, key=func]) -> value\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001317\n\
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001318With a single iterable argument, return its largest item.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001319With two or more arguments, return the largest argument.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001320
1321
Guido van Rossum79f25d91997-04-29 20:08:16 +00001322static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001323builtin_oct(PyObject *self, PyObject *v)
Guido van Rossum006bcd41991-10-24 14:54:44 +00001324{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001325 PyNumberMethods *nb;
Neil Schemenauer3a313e32004-07-19 16:29:17 +00001326 PyObject *res;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001327
Guido van Rossum1899c2e1992-09-12 11:09:23 +00001328 if (v == NULL || (nb = v->ob_type->tp_as_number) == NULL ||
1329 nb->nb_oct == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001330 PyErr_SetString(PyExc_TypeError,
Guido van Rossum1899c2e1992-09-12 11:09:23 +00001331 "oct() argument can't be converted to oct");
1332 return NULL;
Guido van Rossum006bcd41991-10-24 14:54:44 +00001333 }
Neil Schemenauer3a313e32004-07-19 16:29:17 +00001334 res = (*nb->nb_oct)(v);
1335 if (res && !PyString_Check(res)) {
1336 PyErr_Format(PyExc_TypeError,
1337 "__oct__ returned non-string (type %.200s)",
1338 res->ob_type->tp_name);
1339 Py_DECREF(res);
1340 return NULL;
1341 }
1342 return res;
Guido van Rossum006bcd41991-10-24 14:54:44 +00001343}
1344
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001345PyDoc_STRVAR(oct_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001346"oct(number) -> string\n\
1347\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001348Return the octal representation of an integer or long integer.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001349
1350
Guido van Rossum79f25d91997-04-29 20:08:16 +00001351static PyObject *
Neal Norwitzc4edb0e2006-05-02 04:43:14 +00001352builtin_open(PyObject *self, PyObject *args, PyObject *kwds)
1353{
1354 return PyObject_Call((PyObject*)&PyFile_Type, args, kwds);
1355}
1356
1357PyDoc_STRVAR(open_doc,
1358"open(name[, mode[, buffering]]) -> file object\n\
1359\n\
1360Open a file using the file() type, returns a file object.");
1361
1362
1363static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001364builtin_ord(PyObject *self, PyObject* obj)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001365{
Guido van Rossum09095f32000-03-10 23:00:52 +00001366 long ord;
Martin v. Löwisd96ee902006-02-16 14:37:16 +00001367 Py_ssize_t size;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001368
Jeremy Hylton394b54d2000-04-12 21:19:47 +00001369 if (PyString_Check(obj)) {
1370 size = PyString_GET_SIZE(obj);
Andrew M. Kuchling34c20cf2000-12-20 14:36:56 +00001371 if (size == 1) {
Jeremy Hylton394b54d2000-04-12 21:19:47 +00001372 ord = (long)((unsigned char)*PyString_AS_STRING(obj));
Andrew M. Kuchling34c20cf2000-12-20 14:36:56 +00001373 return PyInt_FromLong(ord);
1374 }
Martin v. Löwis339d0f72001-08-17 18:39:25 +00001375#ifdef Py_USING_UNICODE
Jeremy Hylton394b54d2000-04-12 21:19:47 +00001376 } else if (PyUnicode_Check(obj)) {
1377 size = PyUnicode_GET_SIZE(obj);
Andrew M. Kuchling34c20cf2000-12-20 14:36:56 +00001378 if (size == 1) {
Jeremy Hylton394b54d2000-04-12 21:19:47 +00001379 ord = (long)*PyUnicode_AS_UNICODE(obj);
Andrew M. Kuchling34c20cf2000-12-20 14:36:56 +00001380 return PyInt_FromLong(ord);
1381 }
Martin v. Löwis339d0f72001-08-17 18:39:25 +00001382#endif
Jeremy Hylton394b54d2000-04-12 21:19:47 +00001383 } else {
1384 PyErr_Format(PyExc_TypeError,
Andrew M. Kuchlingf07aad12000-12-23 14:11:28 +00001385 "ord() expected string of length 1, but " \
Jeremy Hylton394b54d2000-04-12 21:19:47 +00001386 "%.200s found", obj->ob_type->tp_name);
Guido van Rossum09095f32000-03-10 23:00:52 +00001387 return NULL;
1388 }
1389
Guido van Rossumad991772001-01-12 16:03:05 +00001390 PyErr_Format(PyExc_TypeError,
Andrew M. Kuchlingf07aad12000-12-23 14:11:28 +00001391 "ord() expected a character, "
Martin v. Löwisd96ee902006-02-16 14:37:16 +00001392 "but string of length %zd found",
Jeremy Hylton394b54d2000-04-12 21:19:47 +00001393 size);
1394 return NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001395}
1396
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001397PyDoc_STRVAR(ord_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001398"ord(c) -> integer\n\
1399\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001400Return the integer ordinal of a one-character string.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001401
1402
Guido van Rossum79f25d91997-04-29 20:08:16 +00001403static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001404builtin_pow(PyObject *self, PyObject *args)
Guido van Rossum6a00cd81995-01-07 12:39:01 +00001405{
Guido van Rossum09df08a1998-05-22 00:51:39 +00001406 PyObject *v, *w, *z = Py_None;
Guido van Rossum6a00cd81995-01-07 12:39:01 +00001407
Raymond Hettingerea3fdf42002-12-29 16:33:45 +00001408 if (!PyArg_UnpackTuple(args, "pow", 2, 3, &v, &w, &z))
Guido van Rossum6a00cd81995-01-07 12:39:01 +00001409 return NULL;
Guido van Rossum09df08a1998-05-22 00:51:39 +00001410 return PyNumber_Power(v, w, z);
Guido van Rossumd4905451991-05-05 20:00:36 +00001411}
1412
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001413PyDoc_STRVAR(pow_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001414"pow(x, y[, z]) -> number\n\
1415\n\
1416With two arguments, equivalent to x**y. With three arguments,\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001417equivalent to (x**y) % z, but may be more efficient (e.g. for longs).");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001418
1419
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001420
1421/* Return number of items in range (lo, hi, step), when arguments are
1422 * PyInt or PyLong objects. step > 0 required. Return a value < 0 if
1423 * & only if the true value is too large to fit in a signed long.
1424 * Arguments MUST return 1 with either PyInt_Check() or
1425 * PyLong_Check(). Return -1 when there is an error.
1426 */
1427static long
1428get_len_of_range_longs(PyObject *lo, PyObject *hi, PyObject *step)
1429{
1430 /* -------------------------------------------------------------
1431 Algorithm is equal to that of get_len_of_range(), but it operates
1432 on PyObjects (which are assumed to be PyLong or PyInt objects).
1433 ---------------------------------------------------------------*/
1434 long n;
1435 PyObject *diff = NULL;
1436 PyObject *one = NULL;
1437 PyObject *tmp1 = NULL, *tmp2 = NULL, *tmp3 = NULL;
1438 /* holds sub-expression evaluations */
1439
1440 /* if (lo >= hi), return length of 0. */
1441 if (PyObject_Compare(lo, hi) >= 0)
1442 return 0;
1443
1444 if ((one = PyLong_FromLong(1L)) == NULL)
1445 goto Fail;
1446
1447 if ((tmp1 = PyNumber_Subtract(hi, lo)) == NULL)
1448 goto Fail;
1449
1450 if ((diff = PyNumber_Subtract(tmp1, one)) == NULL)
1451 goto Fail;
1452
1453 if ((tmp2 = PyNumber_FloorDivide(diff, step)) == NULL)
1454 goto Fail;
1455
1456 if ((tmp3 = PyNumber_Add(tmp2, one)) == NULL)
1457 goto Fail;
1458
1459 n = PyLong_AsLong(tmp3);
1460 if (PyErr_Occurred()) { /* Check for Overflow */
1461 PyErr_Clear();
1462 goto Fail;
1463 }
1464
1465 Py_DECREF(tmp3);
1466 Py_DECREF(tmp2);
1467 Py_DECREF(diff);
1468 Py_DECREF(tmp1);
1469 Py_DECREF(one);
1470 return n;
1471
1472 Fail:
1473 Py_XDECREF(tmp3);
1474 Py_XDECREF(tmp2);
1475 Py_XDECREF(diff);
1476 Py_XDECREF(tmp1);
1477 Py_XDECREF(one);
1478 return -1;
1479}
1480
1481/* An extension of builtin_range() that handles the case when PyLong
1482 * arguments are given. */
1483static PyObject *
1484handle_range_longs(PyObject *self, PyObject *args)
1485{
1486 PyObject *ilow;
Tim Peters874e1f72003-04-13 22:13:08 +00001487 PyObject *ihigh = NULL;
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001488 PyObject *istep = NULL;
Tim Peters874e1f72003-04-13 22:13:08 +00001489
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001490 PyObject *curnum = NULL;
1491 PyObject *v = NULL;
1492 long bign;
1493 int i, n;
1494 int cmp_result;
1495
Tim Peters874e1f72003-04-13 22:13:08 +00001496 PyObject *zero = PyLong_FromLong(0);
1497
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001498 if (zero == NULL)
1499 return NULL;
1500
Tim Peters874e1f72003-04-13 22:13:08 +00001501 if (!PyArg_UnpackTuple(args, "range", 1, 3, &ilow, &ihigh, &istep)) {
1502 Py_DECREF(zero);
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001503 return NULL;
1504 }
1505
Tim Peters874e1f72003-04-13 22:13:08 +00001506 /* Figure out which way we were called, supply defaults, and be
1507 * sure to incref everything so that the decrefs at the end
1508 * are correct.
1509 */
1510 assert(ilow != NULL);
1511 if (ihigh == NULL) {
1512 /* only 1 arg -- it's the upper limit */
1513 ihigh = ilow;
1514 ilow = NULL;
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001515 }
Tim Peters874e1f72003-04-13 22:13:08 +00001516 assert(ihigh != NULL);
1517 Py_INCREF(ihigh);
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001518
Tim Peters874e1f72003-04-13 22:13:08 +00001519 /* ihigh correct now; do ilow */
1520 if (ilow == NULL)
1521 ilow = zero;
1522 Py_INCREF(ilow);
1523
1524 /* ilow and ihigh correct now; do istep */
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001525 if (istep == NULL) {
1526 istep = PyLong_FromLong(1L);
1527 if (istep == NULL)
1528 goto Fail;
1529 }
1530 else {
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001531 Py_INCREF(istep);
1532 }
1533
Tim Peters874e1f72003-04-13 22:13:08 +00001534 if (!PyInt_Check(ilow) && !PyLong_Check(ilow)) {
Guido van Rossum28e83e32003-04-15 12:43:26 +00001535 PyErr_Format(PyExc_TypeError,
Alex Martellif471d472003-04-23 13:00:44 +00001536 "range() integer start argument expected, got %s.",
Guido van Rossum817d6c92003-04-14 18:25:04 +00001537 ilow->ob_type->tp_name);
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001538 goto Fail;
1539 }
1540
Tim Peters874e1f72003-04-13 22:13:08 +00001541 if (!PyInt_Check(ihigh) && !PyLong_Check(ihigh)) {
Guido van Rossum28e83e32003-04-15 12:43:26 +00001542 PyErr_Format(PyExc_TypeError,
Alex Martellif471d472003-04-23 13:00:44 +00001543 "range() integer end argument expected, got %s.",
Guido van Rossum817d6c92003-04-14 18:25:04 +00001544 ihigh->ob_type->tp_name);
Tim Peters874e1f72003-04-13 22:13:08 +00001545 goto Fail;
1546 }
1547
1548 if (!PyInt_Check(istep) && !PyLong_Check(istep)) {
Guido van Rossum28e83e32003-04-15 12:43:26 +00001549 PyErr_Format(PyExc_TypeError,
Alex Martellif471d472003-04-23 13:00:44 +00001550 "range() integer step argument expected, got %s.",
Guido van Rossum817d6c92003-04-14 18:25:04 +00001551 istep->ob_type->tp_name);
Tim Peters874e1f72003-04-13 22:13:08 +00001552 goto Fail;
1553 }
1554
1555 if (PyObject_Cmp(istep, zero, &cmp_result) == -1)
1556 goto Fail;
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001557 if (cmp_result == 0) {
1558 PyErr_SetString(PyExc_ValueError,
Alex Martellif471d472003-04-23 13:00:44 +00001559 "range() step argument must not be zero");
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001560 goto Fail;
1561 }
1562
1563 if (cmp_result > 0)
1564 bign = get_len_of_range_longs(ilow, ihigh, istep);
1565 else {
1566 PyObject *neg_istep = PyNumber_Negative(istep);
1567 if (neg_istep == NULL)
1568 goto Fail;
1569 bign = get_len_of_range_longs(ihigh, ilow, neg_istep);
1570 Py_DECREF(neg_istep);
1571 }
1572
1573 n = (int)bign;
1574 if (bign < 0 || (long)n != bign) {
1575 PyErr_SetString(PyExc_OverflowError,
1576 "range() result has too many items");
1577 goto Fail;
1578 }
1579
1580 v = PyList_New(n);
1581 if (v == NULL)
1582 goto Fail;
1583
1584 curnum = ilow;
1585 Py_INCREF(curnum);
1586
1587 for (i = 0; i < n; i++) {
1588 PyObject *w = PyNumber_Long(curnum);
1589 PyObject *tmp_num;
1590 if (w == NULL)
1591 goto Fail;
1592
1593 PyList_SET_ITEM(v, i, w);
1594
1595 tmp_num = PyNumber_Add(curnum, istep);
1596 if (tmp_num == NULL)
1597 goto Fail;
1598
1599 Py_DECREF(curnum);
1600 curnum = tmp_num;
1601 }
Tim Peters874e1f72003-04-13 22:13:08 +00001602 Py_DECREF(ilow);
1603 Py_DECREF(ihigh);
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001604 Py_DECREF(istep);
1605 Py_DECREF(zero);
Tim Peters874e1f72003-04-13 22:13:08 +00001606 Py_DECREF(curnum);
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001607 return v;
1608
1609 Fail:
Tim Peters874e1f72003-04-13 22:13:08 +00001610 Py_DECREF(ilow);
1611 Py_DECREF(ihigh);
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001612 Py_XDECREF(istep);
Tim Peters874e1f72003-04-13 22:13:08 +00001613 Py_DECREF(zero);
1614 Py_XDECREF(curnum);
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001615 Py_XDECREF(v);
1616 return NULL;
1617}
1618
Guido van Rossum124eff01999-02-23 16:11:01 +00001619/* Return number of items in range/xrange (lo, hi, step). step > 0
1620 * required. Return a value < 0 if & only if the true value is too
1621 * large to fit in a signed long.
1622 */
1623static long
Thomas Wouterse28c2962000-07-23 22:21:32 +00001624get_len_of_range(long lo, long hi, long step)
Guido van Rossum124eff01999-02-23 16:11:01 +00001625{
1626 /* -------------------------------------------------------------
1627 If lo >= hi, the range is empty.
1628 Else if n values are in the range, the last one is
1629 lo + (n-1)*step, which must be <= hi-1. Rearranging,
1630 n <= (hi - lo - 1)/step + 1, so taking the floor of the RHS gives
1631 the proper value. Since lo < hi in this case, hi-lo-1 >= 0, so
1632 the RHS is non-negative and so truncation is the same as the
1633 floor. Letting M be the largest positive long, the worst case
1634 for the RHS numerator is hi=M, lo=-M-1, and then
1635 hi-lo-1 = M-(-M-1)-1 = 2*M. Therefore unsigned long has enough
1636 precision to compute the RHS exactly.
1637 ---------------------------------------------------------------*/
1638 long n = 0;
1639 if (lo < hi) {
1640 unsigned long uhi = (unsigned long)hi;
1641 unsigned long ulo = (unsigned long)lo;
1642 unsigned long diff = uhi - ulo - 1;
1643 n = (long)(diff / (unsigned long)step + 1);
1644 }
1645 return n;
1646}
1647
Guido van Rossum79f25d91997-04-29 20:08:16 +00001648static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001649builtin_range(PyObject *self, PyObject *args)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001650{
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001651 long ilow = 0, ihigh = 0, istep = 1;
Guido van Rossum124eff01999-02-23 16:11:01 +00001652 long bign;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001653 int i, n;
Guido van Rossum124eff01999-02-23 16:11:01 +00001654
Guido van Rossum79f25d91997-04-29 20:08:16 +00001655 PyObject *v;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001656
Guido van Rossum79f25d91997-04-29 20:08:16 +00001657 if (PyTuple_Size(args) <= 1) {
1658 if (!PyArg_ParseTuple(args,
Guido van Rossum0865dd91995-01-17 16:30:22 +00001659 "l;range() requires 1-3 int arguments",
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001660 &ihigh)) {
1661 PyErr_Clear();
1662 return handle_range_longs(self, args);
1663 }
Guido van Rossum3f5da241990-12-20 15:06:42 +00001664 }
1665 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001666 if (!PyArg_ParseTuple(args,
Guido van Rossum0865dd91995-01-17 16:30:22 +00001667 "ll|l;range() requires 1-3 int arguments",
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001668 &ilow, &ihigh, &istep)) {
1669 PyErr_Clear();
1670 return handle_range_longs(self, args);
1671 }
Guido van Rossum3f5da241990-12-20 15:06:42 +00001672 }
1673 if (istep == 0) {
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001674 PyErr_SetString(PyExc_ValueError,
Alex Martellif471d472003-04-23 13:00:44 +00001675 "range() step argument must not be zero");
Guido van Rossum3f5da241990-12-20 15:06:42 +00001676 return NULL;
1677 }
Guido van Rossum124eff01999-02-23 16:11:01 +00001678 if (istep > 0)
1679 bign = get_len_of_range(ilow, ihigh, istep);
1680 else
1681 bign = get_len_of_range(ihigh, ilow, -istep);
1682 n = (int)bign;
1683 if (bign < 0 || (long)n != bign) {
Guido van Rossume23cde21999-01-12 05:07:47 +00001684 PyErr_SetString(PyExc_OverflowError,
Fred Drake661ea262000-10-24 19:57:45 +00001685 "range() result has too many items");
Guido van Rossume23cde21999-01-12 05:07:47 +00001686 return NULL;
1687 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00001688 v = PyList_New(n);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001689 if (v == NULL)
1690 return NULL;
1691 for (i = 0; i < n; i++) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001692 PyObject *w = PyInt_FromLong(ilow);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001693 if (w == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001694 Py_DECREF(v);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001695 return NULL;
1696 }
Guido van Rossuma937d141998-04-24 18:22:02 +00001697 PyList_SET_ITEM(v, i, w);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001698 ilow += istep;
1699 }
1700 return v;
1701}
1702
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001703PyDoc_STRVAR(range_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001704"range([start,] stop[, step]) -> list of integers\n\
1705\n\
1706Return a list containing an arithmetic progression of integers.\n\
1707range(i, j) returns [i, i+1, i+2, ..., j-1]; start (!) defaults to 0.\n\
1708When step is given, it specifies the increment (or decrement).\n\
1709For example, range(4) returns [0, 1, 2, 3]. The end point is omitted!\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001710These are exactly the valid indices for a list of 4 elements.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001711
1712
Guido van Rossum79f25d91997-04-29 20:08:16 +00001713static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001714builtin_raw_input(PyObject *self, PyObject *args)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001715{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001716 PyObject *v = NULL;
Martin v. Löwis31d2df52002-08-14 15:46:02 +00001717 PyObject *fin = PySys_GetObject("stdin");
1718 PyObject *fout = PySys_GetObject("stdout");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001719
Raymond Hettingerea3fdf42002-12-29 16:33:45 +00001720 if (!PyArg_UnpackTuple(args, "[raw_]input", 0, 1, &v))
Guido van Rossum3165fe61992-09-25 21:59:05 +00001721 return NULL;
Martin v. Löwis31d2df52002-08-14 15:46:02 +00001722
1723 if (fin == NULL) {
Alex Martellia9b9c9f2003-04-23 13:34:35 +00001724 PyErr_SetString(PyExc_RuntimeError, "[raw_]input: lost sys.stdin");
Martin v. Löwis31d2df52002-08-14 15:46:02 +00001725 return NULL;
1726 }
1727 if (fout == NULL) {
Alex Martellia9b9c9f2003-04-23 13:34:35 +00001728 PyErr_SetString(PyExc_RuntimeError, "[raw_]input: lost sys.stdout");
Martin v. Löwis31d2df52002-08-14 15:46:02 +00001729 return NULL;
1730 }
1731 if (PyFile_SoftSpace(fout, 0)) {
1732 if (PyFile_WriteString(" ", fout) != 0)
1733 return NULL;
1734 }
Georg Brandl7e3ba2a2006-08-06 08:23:54 +00001735 if (PyFile_AsFile(fin) && PyFile_AsFile(fout)
Martin v. Löwis566f6af2002-10-26 14:39:10 +00001736 && isatty(fileno(PyFile_AsFile(fin)))
1737 && isatty(fileno(PyFile_AsFile(fout)))) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001738 PyObject *po;
Guido van Rossum872537c1995-07-07 22:43:42 +00001739 char *prompt;
1740 char *s;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001741 PyObject *result;
Guido van Rossum872537c1995-07-07 22:43:42 +00001742 if (v != NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001743 po = PyObject_Str(v);
Guido van Rossum872537c1995-07-07 22:43:42 +00001744 if (po == NULL)
1745 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001746 prompt = PyString_AsString(po);
Guido van Rossumd9b52081998-06-26 18:25:38 +00001747 if (prompt == NULL)
1748 return NULL;
Guido van Rossum872537c1995-07-07 22:43:42 +00001749 }
1750 else {
1751 po = NULL;
1752 prompt = "";
1753 }
Michael W. Hudson52db5192004-08-02 13:21:09 +00001754 s = PyOS_Readline(PyFile_AsFile(fin), PyFile_AsFile(fout),
Martin v. Löwis566f6af2002-10-26 14:39:10 +00001755 prompt);
Guido van Rossum79f25d91997-04-29 20:08:16 +00001756 Py_XDECREF(po);
Guido van Rossum872537c1995-07-07 22:43:42 +00001757 if (s == NULL) {
Michael W. Hudson30ea2f22004-07-07 17:44:12 +00001758 if (!PyErr_Occurred())
1759 PyErr_SetNone(PyExc_KeyboardInterrupt);
Guido van Rossum872537c1995-07-07 22:43:42 +00001760 return NULL;
1761 }
1762 if (*s == '\0') {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001763 PyErr_SetNone(PyExc_EOFError);
Guido van Rossum872537c1995-07-07 22:43:42 +00001764 result = NULL;
1765 }
1766 else { /* strip trailing '\n' */
Guido van Rossum106f2da2000-06-28 21:12:25 +00001767 size_t len = strlen(s);
Martin v. Löwisb1ed7fa2006-04-13 07:52:27 +00001768 if (len > PY_SSIZE_T_MAX) {
Martin v. Löwis31d2df52002-08-14 15:46:02 +00001769 PyErr_SetString(PyExc_OverflowError,
Alex Martellia9b9c9f2003-04-23 13:34:35 +00001770 "[raw_]input: input too long");
Guido van Rossum106f2da2000-06-28 21:12:25 +00001771 result = NULL;
1772 }
1773 else {
Martin v. Löwisb1ed7fa2006-04-13 07:52:27 +00001774 result = PyString_FromStringAndSize(s, len-1);
Guido van Rossum106f2da2000-06-28 21:12:25 +00001775 }
Guido van Rossum872537c1995-07-07 22:43:42 +00001776 }
Guido van Rossumb18618d2000-05-03 23:44:39 +00001777 PyMem_FREE(s);
Guido van Rossum872537c1995-07-07 22:43:42 +00001778 return result;
1779 }
Guido van Rossum90933611991-06-07 16:10:43 +00001780 if (v != NULL) {
Martin v. Löwis31d2df52002-08-14 15:46:02 +00001781 if (PyFile_WriteObject(v, fout, Py_PRINT_RAW) != 0)
Guido van Rossum90933611991-06-07 16:10:43 +00001782 return NULL;
1783 }
Martin v. Löwis31d2df52002-08-14 15:46:02 +00001784 return PyFile_GetLine(fin, -1);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001785}
1786
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001787PyDoc_STRVAR(raw_input_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001788"raw_input([prompt]) -> string\n\
1789\n\
1790Read a string from standard input. The trailing newline is stripped.\n\
1791If the user hits EOF (Unix: Ctl-D, Windows: Ctl-Z+Return), raise EOFError.\n\
1792On Unix, GNU readline is used if enabled. The prompt string, if given,\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001793is printed without a trailing newline before reading.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001794
1795
Guido van Rossum79f25d91997-04-29 20:08:16 +00001796static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001797builtin_reduce(PyObject *self, PyObject *args)
Guido van Rossum12d12c51993-10-26 17:58:25 +00001798{
Tim Peters15d81ef2001-05-04 04:39:21 +00001799 PyObject *seq, *func, *result = NULL, *it;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001800
Neal Norwitzdf25efe2007-05-23 06:58:36 +00001801 if (Py_Py3kWarningFlag &&
1802 PyErr_Warn(PyExc_DeprecationWarning,
1803 "reduce() not supported in 3.x") < 0)
1804 return NULL;
1805
Raymond Hettingerea3fdf42002-12-29 16:33:45 +00001806 if (!PyArg_UnpackTuple(args, "reduce", 2, 3, &func, &seq, &result))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001807 return NULL;
1808 if (result != NULL)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001809 Py_INCREF(result);
Guido van Rossum12d12c51993-10-26 17:58:25 +00001810
Tim Peters15d81ef2001-05-04 04:39:21 +00001811 it = PyObject_GetIter(seq);
1812 if (it == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001813 PyErr_SetString(PyExc_TypeError,
Tim Peters15d81ef2001-05-04 04:39:21 +00001814 "reduce() arg 2 must support iteration");
1815 Py_XDECREF(result);
Guido van Rossum12d12c51993-10-26 17:58:25 +00001816 return NULL;
1817 }
1818
Guido van Rossum79f25d91997-04-29 20:08:16 +00001819 if ((args = PyTuple_New(2)) == NULL)
Guido van Rossum2d951851994-08-29 12:52:16 +00001820 goto Fail;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001821
Tim Peters15d81ef2001-05-04 04:39:21 +00001822 for (;;) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001823 PyObject *op2;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001824
1825 if (args->ob_refcnt > 1) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001826 Py_DECREF(args);
1827 if ((args = PyTuple_New(2)) == NULL)
Guido van Rossum2d951851994-08-29 12:52:16 +00001828 goto Fail;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001829 }
1830
Tim Peters15d81ef2001-05-04 04:39:21 +00001831 op2 = PyIter_Next(it);
1832 if (op2 == NULL) {
Tim Petersf4848da2001-05-05 00:14:56 +00001833 if (PyErr_Occurred())
1834 goto Fail;
1835 break;
Guido van Rossum2d951851994-08-29 12:52:16 +00001836 }
Guido van Rossum12d12c51993-10-26 17:58:25 +00001837
Guido van Rossum2d951851994-08-29 12:52:16 +00001838 if (result == NULL)
1839 result = op2;
1840 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001841 PyTuple_SetItem(args, 0, result);
1842 PyTuple_SetItem(args, 1, op2);
1843 if ((result = PyEval_CallObject(func, args)) == NULL)
Guido van Rossum2d951851994-08-29 12:52:16 +00001844 goto Fail;
1845 }
Guido van Rossum12d12c51993-10-26 17:58:25 +00001846 }
1847
Guido van Rossum79f25d91997-04-29 20:08:16 +00001848 Py_DECREF(args);
Guido van Rossum12d12c51993-10-26 17:58:25 +00001849
Guido van Rossum2d951851994-08-29 12:52:16 +00001850 if (result == NULL)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001851 PyErr_SetString(PyExc_TypeError,
Fred Drake661ea262000-10-24 19:57:45 +00001852 "reduce() of empty sequence with no initial value");
Guido van Rossum2d951851994-08-29 12:52:16 +00001853
Tim Peters15d81ef2001-05-04 04:39:21 +00001854 Py_DECREF(it);
Guido van Rossum12d12c51993-10-26 17:58:25 +00001855 return result;
1856
Guido van Rossum2d951851994-08-29 12:52:16 +00001857Fail:
Guido van Rossum79f25d91997-04-29 20:08:16 +00001858 Py_XDECREF(args);
1859 Py_XDECREF(result);
Tim Peters15d81ef2001-05-04 04:39:21 +00001860 Py_DECREF(it);
Guido van Rossum12d12c51993-10-26 17:58:25 +00001861 return NULL;
1862}
1863
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001864PyDoc_STRVAR(reduce_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001865"reduce(function, sequence[, initial]) -> value\n\
1866\n\
1867Apply a function of two arguments cumulatively to the items of a sequence,\n\
1868from left to right, so as to reduce the sequence to a single value.\n\
1869For example, reduce(lambda x, y: x+y, [1, 2, 3, 4, 5]) calculates\n\
1870((((1+2)+3)+4)+5). If initial is present, it is placed before the items\n\
1871of the sequence in the calculation, and serves as a default when the\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001872sequence is empty.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001873
1874
Guido van Rossum79f25d91997-04-29 20:08:16 +00001875static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001876builtin_reload(PyObject *self, PyObject *v)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001877{
Neal Norwitzdf25efe2007-05-23 06:58:36 +00001878 if (Py_Py3kWarningFlag &&
1879 PyErr_Warn(PyExc_DeprecationWarning,
1880 "reload() not supported in 3.x") < 0)
1881 return NULL;
1882
Guido van Rossum79f25d91997-04-29 20:08:16 +00001883 return PyImport_ReloadModule(v);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001884}
1885
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001886PyDoc_STRVAR(reload_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001887"reload(module) -> module\n\
1888\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001889Reload the module. The module must have been successfully imported before.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001890
1891
Guido van Rossum79f25d91997-04-29 20:08:16 +00001892static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001893builtin_repr(PyObject *self, PyObject *v)
Guido van Rossumc89705d1992-11-26 08:54:07 +00001894{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001895 return PyObject_Repr(v);
Guido van Rossumc89705d1992-11-26 08:54:07 +00001896}
1897
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001898PyDoc_STRVAR(repr_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001899"repr(object) -> string\n\
1900\n\
1901Return the canonical string representation of the object.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001902For most object types, eval(repr(object)) == object.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001903
1904
Guido van Rossum79f25d91997-04-29 20:08:16 +00001905static PyObject *
Georg Brandlccadf842006-03-31 18:54:53 +00001906builtin_round(PyObject *self, PyObject *args, PyObject *kwds)
Guido van Rossum9e51f9b1993-02-12 16:29:05 +00001907{
Georg Brandlccadf842006-03-31 18:54:53 +00001908 double number;
Guido van Rossum9e51f9b1993-02-12 16:29:05 +00001909 double f;
1910 int ndigits = 0;
Guido van Rossum9e51f9b1993-02-12 16:29:05 +00001911 int i;
Georg Brandlccadf842006-03-31 18:54:53 +00001912 static char *kwlist[] = {"number", "ndigits", 0};
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001913
Georg Brandlccadf842006-03-31 18:54:53 +00001914 if (!PyArg_ParseTupleAndKeywords(args, kwds, "d|i:round",
1915 kwlist, &number, &ndigits))
1916 return NULL;
Guido van Rossum9e51f9b1993-02-12 16:29:05 +00001917 f = 1.0;
Guido van Rossum1e162d31998-05-09 14:42:25 +00001918 i = abs(ndigits);
1919 while (--i >= 0)
Guido van Rossum9e51f9b1993-02-12 16:29:05 +00001920 f = f*10.0;
Guido van Rossum1e162d31998-05-09 14:42:25 +00001921 if (ndigits < 0)
Georg Brandlccadf842006-03-31 18:54:53 +00001922 number /= f;
Guido van Rossum9e51f9b1993-02-12 16:29:05 +00001923 else
Georg Brandlccadf842006-03-31 18:54:53 +00001924 number *= f;
1925 if (number >= 0.0)
1926 number = floor(number + 0.5);
Guido van Rossum1e162d31998-05-09 14:42:25 +00001927 else
Georg Brandlccadf842006-03-31 18:54:53 +00001928 number = ceil(number - 0.5);
Guido van Rossum1e162d31998-05-09 14:42:25 +00001929 if (ndigits < 0)
Georg Brandlccadf842006-03-31 18:54:53 +00001930 number *= f;
Guido van Rossum1e162d31998-05-09 14:42:25 +00001931 else
Georg Brandlccadf842006-03-31 18:54:53 +00001932 number /= f;
1933 return PyFloat_FromDouble(number);
Guido van Rossum9e51f9b1993-02-12 16:29:05 +00001934}
1935
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001936PyDoc_STRVAR(round_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001937"round(number[, ndigits]) -> floating point number\n\
1938\n\
1939Round a number to a given precision in decimal digits (default 0 digits).\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001940This always returns a floating point number. Precision may be negative.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001941
Raymond Hettinger64958a12003-12-17 20:43:33 +00001942static PyObject *
1943builtin_sorted(PyObject *self, PyObject *args, PyObject *kwds)
1944{
1945 PyObject *newlist, *v, *seq, *compare=NULL, *keyfunc=NULL, *newargs;
1946 PyObject *callable;
Martin v. Löwis15e62742006-02-27 16:46:16 +00001947 static char *kwlist[] = {"iterable", "cmp", "key", "reverse", 0};
Neal Norwitz6f0d4792005-12-15 06:40:36 +00001948 int reverse;
Raymond Hettinger64958a12003-12-17 20:43:33 +00001949
Neal Norwitz6f0d4792005-12-15 06:40:36 +00001950 /* args 1-4 should match listsort in Objects/listobject.c */
Armin Rigo71d7e702005-09-20 18:13:03 +00001951 if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|OOi:sorted",
1952 kwlist, &seq, &compare, &keyfunc, &reverse))
1953 return NULL;
Raymond Hettinger64958a12003-12-17 20:43:33 +00001954
1955 newlist = PySequence_List(seq);
1956 if (newlist == NULL)
1957 return NULL;
1958
1959 callable = PyObject_GetAttrString(newlist, "sort");
1960 if (callable == NULL) {
1961 Py_DECREF(newlist);
1962 return NULL;
1963 }
Georg Brandl99d7e4e2005-08-31 22:21:15 +00001964
Raymond Hettinger64958a12003-12-17 20:43:33 +00001965 newargs = PyTuple_GetSlice(args, 1, 4);
1966 if (newargs == NULL) {
1967 Py_DECREF(newlist);
1968 Py_DECREF(callable);
1969 return NULL;
1970 }
1971
1972 v = PyObject_Call(callable, newargs, kwds);
1973 Py_DECREF(newargs);
1974 Py_DECREF(callable);
1975 if (v == NULL) {
1976 Py_DECREF(newlist);
1977 return NULL;
1978 }
1979 Py_DECREF(v);
1980 return newlist;
1981}
1982
1983PyDoc_STRVAR(sorted_doc,
1984"sorted(iterable, cmp=None, key=None, reverse=False) --> new sorted list");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001985
Guido van Rossum79f25d91997-04-29 20:08:16 +00001986static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001987builtin_vars(PyObject *self, PyObject *args)
Guido van Rossum2d951851994-08-29 12:52:16 +00001988{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001989 PyObject *v = NULL;
1990 PyObject *d;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001991
Raymond Hettingerea3fdf42002-12-29 16:33:45 +00001992 if (!PyArg_UnpackTuple(args, "vars", 0, 1, &v))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001993 return NULL;
Guido van Rossum2d951851994-08-29 12:52:16 +00001994 if (v == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001995 d = PyEval_GetLocals();
Guido van Rossum53bb7ff1995-07-26 16:26:31 +00001996 if (d == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001997 if (!PyErr_Occurred())
1998 PyErr_SetString(PyExc_SystemError,
Alex Martellia9b9c9f2003-04-23 13:34:35 +00001999 "vars(): no locals!?");
Guido van Rossum53bb7ff1995-07-26 16:26:31 +00002000 }
2001 else
Guido van Rossum79f25d91997-04-29 20:08:16 +00002002 Py_INCREF(d);
Guido van Rossum2d951851994-08-29 12:52:16 +00002003 }
2004 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002005 d = PyObject_GetAttrString(v, "__dict__");
Guido van Rossum2d951851994-08-29 12:52:16 +00002006 if (d == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002007 PyErr_SetString(PyExc_TypeError,
Guido van Rossum2d951851994-08-29 12:52:16 +00002008 "vars() argument must have __dict__ attribute");
2009 return NULL;
2010 }
2011 }
2012 return d;
2013}
2014
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002015PyDoc_STRVAR(vars_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002016"vars([object]) -> dictionary\n\
2017\n\
2018Without arguments, equivalent to locals().\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002019With an argument, equivalent to object.__dict__.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002020
Alex Martellia70b1912003-04-22 08:12:33 +00002021
2022static PyObject*
2023builtin_sum(PyObject *self, PyObject *args)
2024{
2025 PyObject *seq;
2026 PyObject *result = NULL;
2027 PyObject *temp, *item, *iter;
2028
Raymond Hettinger5cf63942003-10-25 06:41:37 +00002029 if (!PyArg_UnpackTuple(args, "sum", 1, 2, &seq, &result))
Alex Martellia70b1912003-04-22 08:12:33 +00002030 return NULL;
2031
2032 iter = PyObject_GetIter(seq);
2033 if (iter == NULL)
2034 return NULL;
2035
2036 if (result == NULL) {
2037 result = PyInt_FromLong(0);
2038 if (result == NULL) {
2039 Py_DECREF(iter);
2040 return NULL;
2041 }
2042 } else {
2043 /* reject string values for 'start' parameter */
2044 if (PyObject_TypeCheck(result, &PyBaseString_Type)) {
2045 PyErr_SetString(PyExc_TypeError,
Alex Martellia9b9c9f2003-04-23 13:34:35 +00002046 "sum() can't sum strings [use ''.join(seq) instead]");
Alex Martellia70b1912003-04-22 08:12:33 +00002047 Py_DECREF(iter);
2048 return NULL;
2049 }
Alex Martelli41c9f882003-04-22 09:24:48 +00002050 Py_INCREF(result);
Alex Martellia70b1912003-04-22 08:12:33 +00002051 }
2052
Raymond Hettinger3f8caa32007-10-24 01:28:33 +00002053#ifndef SLOW_SUM
2054 /* Fast addition by keeping temporary sums in C instead of new Python objects.
2055 Assumes all inputs are the same type. If the assumption fails, default
2056 to the more general routine.
2057 */
2058 if (PyInt_CheckExact(result)) {
2059 long i_result = PyInt_AS_LONG(result);
2060 Py_DECREF(result);
2061 result = NULL;
2062 while(result == NULL) {
2063 item = PyIter_Next(iter);
2064 if (item == NULL) {
2065 Py_DECREF(iter);
2066 if (PyErr_Occurred())
2067 return NULL;
2068 return PyInt_FromLong(i_result);
2069 }
2070 if (PyInt_CheckExact(item)) {
2071 long b = PyInt_AS_LONG(item);
2072 long x = i_result + b;
2073 if ((x^i_result) >= 0 || (x^b) >= 0) {
2074 i_result = x;
2075 Py_DECREF(item);
2076 continue;
2077 }
2078 }
2079 /* Either overflowed or is not an int. Restore real objects and process normally */
2080 result = PyInt_FromLong(i_result);
2081 temp = PyNumber_Add(result, item);
2082 Py_DECREF(result);
2083 Py_DECREF(item);
2084 result = temp;
2085 if (result == NULL) {
2086 Py_DECREF(iter);
2087 return NULL;
2088 }
2089 }
2090 }
2091
2092 if (PyFloat_CheckExact(result)) {
2093 double f_result = PyFloat_AS_DOUBLE(result);
2094 Py_DECREF(result);
2095 result = NULL;
2096 while(result == NULL) {
2097 item = PyIter_Next(iter);
2098 if (item == NULL) {
2099 Py_DECREF(iter);
2100 if (PyErr_Occurred())
2101 return NULL;
2102 return PyFloat_FromDouble(f_result);
2103 }
2104 if (PyFloat_CheckExact(item)) {
2105 PyFPE_START_PROTECT("add", return 0)
2106 f_result += PyFloat_AS_DOUBLE(item);
Raymond Hettinger3a8daf52007-10-24 02:05:51 +00002107 PyFPE_END_PROTECT(f_result)
Raymond Hettingera45c4872007-10-25 02:26:58 +00002108 Py_DECREF(item);
Raymond Hettinger3a8daf52007-10-24 02:05:51 +00002109 continue;
2110 }
2111 if (PyInt_CheckExact(item)) {
2112 PyFPE_START_PROTECT("add", return 0)
2113 f_result += (double)PyInt_AS_LONG(item);
2114 PyFPE_END_PROTECT(f_result)
Raymond Hettingera45c4872007-10-25 02:26:58 +00002115 Py_DECREF(item);
Raymond Hettinger3f8caa32007-10-24 01:28:33 +00002116 continue;
2117 }
2118 result = PyFloat_FromDouble(f_result);
2119 temp = PyNumber_Add(result, item);
2120 Py_DECREF(result);
2121 Py_DECREF(item);
2122 result = temp;
2123 if (result == NULL) {
2124 Py_DECREF(iter);
2125 return NULL;
2126 }
2127 }
2128 }
2129#endif
2130
Alex Martellia70b1912003-04-22 08:12:33 +00002131 for(;;) {
2132 item = PyIter_Next(iter);
2133 if (item == NULL) {
2134 /* error, or end-of-sequence */
2135 if (PyErr_Occurred()) {
2136 Py_DECREF(result);
2137 result = NULL;
2138 }
2139 break;
2140 }
Alex Martellia253e182003-10-25 23:24:14 +00002141 temp = PyNumber_Add(result, item);
Alex Martellia70b1912003-04-22 08:12:33 +00002142 Py_DECREF(result);
2143 Py_DECREF(item);
2144 result = temp;
2145 if (result == NULL)
2146 break;
2147 }
2148 Py_DECREF(iter);
2149 return result;
2150}
2151
2152PyDoc_STRVAR(sum_doc,
Georg Brandl8134d062006-10-12 12:33:07 +00002153"sum(sequence[, start]) -> value\n\
Alex Martellia70b1912003-04-22 08:12:33 +00002154\n\
2155Returns the sum of a sequence of numbers (NOT strings) plus the value\n\
Georg Brandl8134d062006-10-12 12:33:07 +00002156of parameter 'start' (which defaults to 0). When the sequence is\n\
2157empty, returns start.");
Alex Martellia70b1912003-04-22 08:12:33 +00002158
2159
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002160static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002161builtin_isinstance(PyObject *self, PyObject *args)
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002162{
2163 PyObject *inst;
2164 PyObject *cls;
Guido van Rossum823649d2001-03-21 18:40:58 +00002165 int retval;
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002166
Raymond Hettingerea3fdf42002-12-29 16:33:45 +00002167 if (!PyArg_UnpackTuple(args, "isinstance", 2, 2, &inst, &cls))
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002168 return NULL;
Guido van Rossumf5dd9141997-12-02 19:11:45 +00002169
Guido van Rossum823649d2001-03-21 18:40:58 +00002170 retval = PyObject_IsInstance(inst, cls);
2171 if (retval < 0)
Guido van Rossumad991772001-01-12 16:03:05 +00002172 return NULL;
Guido van Rossum77f6a652002-04-03 22:41:51 +00002173 return PyBool_FromLong(retval);
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002174}
2175
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002176PyDoc_STRVAR(isinstance_doc,
Guido van Rossum77f6a652002-04-03 22:41:51 +00002177"isinstance(object, class-or-type-or-tuple) -> bool\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002178\n\
2179Return whether an object is an instance of a class or of a subclass thereof.\n\
Guido van Rossum03290ec2001-10-07 20:54:12 +00002180With a type as second argument, return whether that is the object's type.\n\
2181The form using a tuple, isinstance(x, (A, B, ...)), is a shortcut for\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002182isinstance(x, A) or isinstance(x, B) or ... (etc.).");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002183
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002184
2185static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002186builtin_issubclass(PyObject *self, PyObject *args)
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002187{
2188 PyObject *derived;
2189 PyObject *cls;
2190 int retval;
2191
Raymond Hettingerea3fdf42002-12-29 16:33:45 +00002192 if (!PyArg_UnpackTuple(args, "issubclass", 2, 2, &derived, &cls))
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002193 return NULL;
Guido van Rossum668213d1999-06-16 17:28:37 +00002194
Guido van Rossum823649d2001-03-21 18:40:58 +00002195 retval = PyObject_IsSubclass(derived, cls);
2196 if (retval < 0)
2197 return NULL;
Guido van Rossum77f6a652002-04-03 22:41:51 +00002198 return PyBool_FromLong(retval);
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002199}
2200
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002201PyDoc_STRVAR(issubclass_doc,
Guido van Rossum77f6a652002-04-03 22:41:51 +00002202"issubclass(C, B) -> bool\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002203\n\
Walter Dörwaldd9a6ad32002-12-12 16:41:44 +00002204Return whether class C is a subclass (i.e., a derived class) of class B.\n\
2205When using a tuple as the second argument issubclass(X, (A, B, ...)),\n\
2206is a shortcut for issubclass(X, A) or issubclass(X, B) or ... (etc.).");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002207
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002208
Barry Warsawbd599b52000-08-03 15:45:29 +00002209static PyObject*
2210builtin_zip(PyObject *self, PyObject *args)
2211{
2212 PyObject *ret;
Martin v. Löwisd96ee902006-02-16 14:37:16 +00002213 const Py_ssize_t itemsize = PySequence_Length(args);
2214 Py_ssize_t i;
Tim Peters8572b4f2001-05-06 01:05:02 +00002215 PyObject *itlist; /* tuple of iterators */
Martin v. Löwisd96ee902006-02-16 14:37:16 +00002216 Py_ssize_t len; /* guess at result length */
Barry Warsawbd599b52000-08-03 15:45:29 +00002217
Raymond Hettingereaef6152003-08-02 07:42:57 +00002218 if (itemsize == 0)
2219 return PyList_New(0);
2220
Barry Warsawbd599b52000-08-03 15:45:29 +00002221 /* args must be a tuple */
2222 assert(PyTuple_Check(args));
2223
Tim Peters39a86c22002-05-12 07:19:38 +00002224 /* Guess at result length: the shortest of the input lengths.
2225 If some argument refuses to say, we refuse to guess too, lest
2226 an argument like xrange(sys.maxint) lead us astray.*/
Tim Peters67d687a2002-04-29 21:27:32 +00002227 len = -1; /* unknown */
2228 for (i = 0; i < itemsize; ++i) {
2229 PyObject *item = PyTuple_GET_ITEM(args, i);
Raymond Hettinger4e2f7142007-12-06 00:56:53 +00002230 Py_ssize_t thislen = _PyObject_LengthHint(item, -1);
Tim Peters39a86c22002-05-12 07:19:38 +00002231 if (thislen < 0) {
Tim Peters39a86c22002-05-12 07:19:38 +00002232 len = -1;
2233 break;
2234 }
Tim Peters67d687a2002-04-29 21:27:32 +00002235 else if (len < 0 || thislen < len)
2236 len = thislen;
2237 }
2238
Tim Peters8572b4f2001-05-06 01:05:02 +00002239 /* allocate result list */
Tim Peters67d687a2002-04-29 21:27:32 +00002240 if (len < 0)
2241 len = 10; /* arbitrary */
2242 if ((ret = PyList_New(len)) == NULL)
Barry Warsawbd599b52000-08-03 15:45:29 +00002243 return NULL;
2244
Tim Peters8572b4f2001-05-06 01:05:02 +00002245 /* obtain iterators */
2246 itlist = PyTuple_New(itemsize);
2247 if (itlist == NULL)
2248 goto Fail_ret;
2249 for (i = 0; i < itemsize; ++i) {
2250 PyObject *item = PyTuple_GET_ITEM(args, i);
2251 PyObject *it = PyObject_GetIter(item);
2252 if (it == NULL) {
2253 if (PyErr_ExceptionMatches(PyExc_TypeError))
2254 PyErr_Format(PyExc_TypeError,
Neal Norwitza361bd82006-02-19 19:31:50 +00002255 "zip argument #%zd must support iteration",
Tim Peters8572b4f2001-05-06 01:05:02 +00002256 i+1);
2257 goto Fail_ret_itlist;
Barry Warsawbd599b52000-08-03 15:45:29 +00002258 }
Tim Peters8572b4f2001-05-06 01:05:02 +00002259 PyTuple_SET_ITEM(itlist, i, it);
2260 }
Barry Warsawbd599b52000-08-03 15:45:29 +00002261
Tim Peters8572b4f2001-05-06 01:05:02 +00002262 /* build result into ret list */
Tim Peters67d687a2002-04-29 21:27:32 +00002263 for (i = 0; ; ++i) {
2264 int j;
Tim Peters8572b4f2001-05-06 01:05:02 +00002265 PyObject *next = PyTuple_New(itemsize);
2266 if (!next)
2267 goto Fail_ret_itlist;
2268
Tim Peters67d687a2002-04-29 21:27:32 +00002269 for (j = 0; j < itemsize; j++) {
2270 PyObject *it = PyTuple_GET_ITEM(itlist, j);
Tim Peters8572b4f2001-05-06 01:05:02 +00002271 PyObject *item = PyIter_Next(it);
Barry Warsawbd599b52000-08-03 15:45:29 +00002272 if (!item) {
Tim Peters8572b4f2001-05-06 01:05:02 +00002273 if (PyErr_Occurred()) {
2274 Py_DECREF(ret);
2275 ret = NULL;
Barry Warsawbd599b52000-08-03 15:45:29 +00002276 }
2277 Py_DECREF(next);
Tim Peters8572b4f2001-05-06 01:05:02 +00002278 Py_DECREF(itlist);
Tim Peters67d687a2002-04-29 21:27:32 +00002279 goto Done;
Barry Warsawbd599b52000-08-03 15:45:29 +00002280 }
Tim Peters67d687a2002-04-29 21:27:32 +00002281 PyTuple_SET_ITEM(next, j, item);
Barry Warsawbd599b52000-08-03 15:45:29 +00002282 }
Tim Peters8572b4f2001-05-06 01:05:02 +00002283
Tim Peters67d687a2002-04-29 21:27:32 +00002284 if (i < len)
2285 PyList_SET_ITEM(ret, i, next);
2286 else {
2287 int status = PyList_Append(ret, next);
2288 Py_DECREF(next);
2289 ++len;
2290 if (status < 0)
2291 goto Fail_ret_itlist;
2292 }
Barry Warsawbd599b52000-08-03 15:45:29 +00002293 }
Tim Peters8572b4f2001-05-06 01:05:02 +00002294
Tim Peters67d687a2002-04-29 21:27:32 +00002295Done:
2296 if (ret != NULL && i < len) {
2297 /* The list is too big. */
2298 if (PyList_SetSlice(ret, i, len, NULL) < 0)
2299 return NULL;
2300 }
2301 return ret;
2302
Tim Peters8572b4f2001-05-06 01:05:02 +00002303Fail_ret_itlist:
2304 Py_DECREF(itlist);
2305Fail_ret:
2306 Py_DECREF(ret);
2307 return NULL;
Barry Warsawbd599b52000-08-03 15:45:29 +00002308}
2309
2310
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002311PyDoc_STRVAR(zip_doc,
Barry Warsawbd599b52000-08-03 15:45:29 +00002312"zip(seq1 [, seq2 [...]]) -> [(seq1[0], seq2[0] ...), (...)]\n\
2313\n\
2314Return a list of tuples, where each tuple contains the i-th element\n\
2315from each of the argument sequences. The returned list is truncated\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002316in length to the length of the shortest argument sequence.");
Barry Warsawbd599b52000-08-03 15:45:29 +00002317
2318
Guido van Rossum79f25d91997-04-29 20:08:16 +00002319static PyMethodDef builtin_methods[] = {
Neal Norwitz92e212f2006-04-03 04:48:37 +00002320 {"__import__", (PyCFunction)builtin___import__, METH_VARARGS | METH_KEYWORDS, import_doc},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002321 {"abs", builtin_abs, METH_O, abs_doc},
Raymond Hettinger96229b12005-03-11 06:49:40 +00002322 {"all", builtin_all, METH_O, all_doc},
2323 {"any", builtin_any, METH_O, any_doc},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002324 {"apply", builtin_apply, METH_VARARGS, apply_doc},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002325 {"callable", builtin_callable, METH_O, callable_doc},
2326 {"chr", builtin_chr, METH_VARARGS, chr_doc},
2327 {"cmp", builtin_cmp, METH_VARARGS, cmp_doc},
2328 {"coerce", builtin_coerce, METH_VARARGS, coerce_doc},
Georg Brandl5240d742007-03-13 20:46:32 +00002329 {"compile", (PyCFunction)builtin_compile, METH_VARARGS | METH_KEYWORDS, compile_doc},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002330 {"delattr", builtin_delattr, METH_VARARGS, delattr_doc},
2331 {"dir", builtin_dir, METH_VARARGS, dir_doc},
2332 {"divmod", builtin_divmod, METH_VARARGS, divmod_doc},
2333 {"eval", builtin_eval, METH_VARARGS, eval_doc},
2334 {"execfile", builtin_execfile, METH_VARARGS, execfile_doc},
2335 {"filter", builtin_filter, METH_VARARGS, filter_doc},
2336 {"getattr", builtin_getattr, METH_VARARGS, getattr_doc},
2337 {"globals", (PyCFunction)builtin_globals, METH_NOARGS, globals_doc},
2338 {"hasattr", builtin_hasattr, METH_VARARGS, hasattr_doc},
2339 {"hash", builtin_hash, METH_O, hash_doc},
2340 {"hex", builtin_hex, METH_O, hex_doc},
2341 {"id", builtin_id, METH_O, id_doc},
2342 {"input", builtin_input, METH_VARARGS, input_doc},
2343 {"intern", builtin_intern, METH_VARARGS, intern_doc},
2344 {"isinstance", builtin_isinstance, METH_VARARGS, isinstance_doc},
2345 {"issubclass", builtin_issubclass, METH_VARARGS, issubclass_doc},
2346 {"iter", builtin_iter, METH_VARARGS, iter_doc},
2347 {"len", builtin_len, METH_O, len_doc},
2348 {"locals", (PyCFunction)builtin_locals, METH_NOARGS, locals_doc},
2349 {"map", builtin_map, METH_VARARGS, map_doc},
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00002350 {"max", (PyCFunction)builtin_max, METH_VARARGS | METH_KEYWORDS, max_doc},
2351 {"min", (PyCFunction)builtin_min, METH_VARARGS | METH_KEYWORDS, min_doc},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002352 {"oct", builtin_oct, METH_O, oct_doc},
Neal Norwitzc4edb0e2006-05-02 04:43:14 +00002353 {"open", (PyCFunction)builtin_open, METH_VARARGS | METH_KEYWORDS, open_doc},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002354 {"ord", builtin_ord, METH_O, ord_doc},
2355 {"pow", builtin_pow, METH_VARARGS, pow_doc},
2356 {"range", builtin_range, METH_VARARGS, range_doc},
2357 {"raw_input", builtin_raw_input, METH_VARARGS, raw_input_doc},
2358 {"reduce", builtin_reduce, METH_VARARGS, reduce_doc},
2359 {"reload", builtin_reload, METH_O, reload_doc},
2360 {"repr", builtin_repr, METH_O, repr_doc},
Georg Brandlccadf842006-03-31 18:54:53 +00002361 {"round", (PyCFunction)builtin_round, METH_VARARGS | METH_KEYWORDS, round_doc},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002362 {"setattr", builtin_setattr, METH_VARARGS, setattr_doc},
Raymond Hettinger64958a12003-12-17 20:43:33 +00002363 {"sorted", (PyCFunction)builtin_sorted, METH_VARARGS | METH_KEYWORDS, sorted_doc},
Alex Martellia70b1912003-04-22 08:12:33 +00002364 {"sum", builtin_sum, METH_VARARGS, sum_doc},
Martin v. Löwis339d0f72001-08-17 18:39:25 +00002365#ifdef Py_USING_UNICODE
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002366 {"unichr", builtin_unichr, METH_VARARGS, unichr_doc},
Martin v. Löwis339d0f72001-08-17 18:39:25 +00002367#endif
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002368 {"vars", builtin_vars, METH_VARARGS, vars_doc},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002369 {"zip", builtin_zip, METH_VARARGS, zip_doc},
Guido van Rossumc02e15c1991-12-16 13:03:00 +00002370 {NULL, NULL},
Guido van Rossum3f5da241990-12-20 15:06:42 +00002371};
2372
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002373PyDoc_STRVAR(builtin_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002374"Built-in functions, exceptions, and other objects.\n\
2375\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002376Noteworthy: None is the `nil' object; Ellipsis represents `...' in slices.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002377
Guido van Rossum25ce5661997-08-02 03:10:38 +00002378PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002379_PyBuiltin_Init(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +00002380{
Fred Drake5550de32000-06-20 04:54:19 +00002381 PyObject *mod, *dict, *debug;
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002382 mod = Py_InitModule4("__builtin__", builtin_methods,
2383 builtin_doc, (PyObject *)NULL,
2384 PYTHON_API_VERSION);
Guido van Rossum25ce5661997-08-02 03:10:38 +00002385 if (mod == NULL)
2386 return NULL;
2387 dict = PyModule_GetDict(mod);
Tim Peters4b7625e2001-09-13 21:37:17 +00002388
Tim Peters7571a0f2003-03-23 17:52:28 +00002389#ifdef Py_TRACE_REFS
2390 /* __builtin__ exposes a number of statically allocated objects
2391 * that, before this code was added in 2.3, never showed up in
2392 * the list of "all objects" maintained by Py_TRACE_REFS. As a
2393 * result, programs leaking references to None and False (etc)
2394 * couldn't be diagnosed by examining sys.getobjects(0).
2395 */
2396#define ADD_TO_ALL(OBJECT) _Py_AddToAllObjects((PyObject *)(OBJECT), 0)
2397#else
2398#define ADD_TO_ALL(OBJECT) (void)0
2399#endif
2400
Tim Peters4b7625e2001-09-13 21:37:17 +00002401#define SETBUILTIN(NAME, OBJECT) \
Tim Peters7571a0f2003-03-23 17:52:28 +00002402 if (PyDict_SetItemString(dict, NAME, (PyObject *)OBJECT) < 0) \
2403 return NULL; \
2404 ADD_TO_ALL(OBJECT)
Tim Peters4b7625e2001-09-13 21:37:17 +00002405
2406 SETBUILTIN("None", Py_None);
2407 SETBUILTIN("Ellipsis", Py_Ellipsis);
2408 SETBUILTIN("NotImplemented", Py_NotImplemented);
Guido van Rossum77f6a652002-04-03 22:41:51 +00002409 SETBUILTIN("False", Py_False);
2410 SETBUILTIN("True", Py_True);
Neal Norwitz32a7e7f2002-05-31 19:58:02 +00002411 SETBUILTIN("basestring", &PyBaseString_Type);
Guido van Rossum77f6a652002-04-03 22:41:51 +00002412 SETBUILTIN("bool", &PyBool_Type);
Guido van Rossumbea18cc2002-06-14 20:41:17 +00002413 SETBUILTIN("buffer", &PyBuffer_Type);
Tim Peters4b7625e2001-09-13 21:37:17 +00002414 SETBUILTIN("classmethod", &PyClassMethod_Type);
Tim Peters6d6c1a32001-08-02 04:15:00 +00002415#ifndef WITHOUT_COMPLEX
Tim Peters4b7625e2001-09-13 21:37:17 +00002416 SETBUILTIN("complex", &PyComplex_Type);
Tim Peters6d6c1a32001-08-02 04:15:00 +00002417#endif
Tim Petersa427a2b2001-10-29 22:25:45 +00002418 SETBUILTIN("dict", &PyDict_Type);
Tim Peters67d687a2002-04-29 21:27:32 +00002419 SETBUILTIN("enumerate", &PyEnum_Type);
Neal Norwitzc4edb0e2006-05-02 04:43:14 +00002420 SETBUILTIN("file", &PyFile_Type);
Tim Peters4b7625e2001-09-13 21:37:17 +00002421 SETBUILTIN("float", &PyFloat_Type);
Raymond Hettingera690a992003-11-16 16:17:49 +00002422 SETBUILTIN("frozenset", &PyFrozenSet_Type);
Tim Peters4b7625e2001-09-13 21:37:17 +00002423 SETBUILTIN("property", &PyProperty_Type);
2424 SETBUILTIN("int", &PyInt_Type);
2425 SETBUILTIN("list", &PyList_Type);
2426 SETBUILTIN("long", &PyLong_Type);
2427 SETBUILTIN("object", &PyBaseObject_Type);
Raymond Hettinger85c20a42003-11-06 14:06:48 +00002428 SETBUILTIN("reversed", &PyReversed_Type);
Raymond Hettingera690a992003-11-16 16:17:49 +00002429 SETBUILTIN("set", &PySet_Type);
Guido van Rossumbea18cc2002-06-14 20:41:17 +00002430 SETBUILTIN("slice", &PySlice_Type);
Tim Peters4b7625e2001-09-13 21:37:17 +00002431 SETBUILTIN("staticmethod", &PyStaticMethod_Type);
2432 SETBUILTIN("str", &PyString_Type);
2433 SETBUILTIN("super", &PySuper_Type);
2434 SETBUILTIN("tuple", &PyTuple_Type);
2435 SETBUILTIN("type", &PyType_Type);
Raymond Hettingerc4c453f2002-06-05 23:12:45 +00002436 SETBUILTIN("xrange", &PyRange_Type);
Martin v. Löwis339d0f72001-08-17 18:39:25 +00002437#ifdef Py_USING_UNICODE
Tim Peters4b7625e2001-09-13 21:37:17 +00002438 SETBUILTIN("unicode", &PyUnicode_Type);
Martin v. Löwis339d0f72001-08-17 18:39:25 +00002439#endif
Guido van Rossum77f6a652002-04-03 22:41:51 +00002440 debug = PyBool_FromLong(Py_OptimizeFlag == 0);
Fred Drake5550de32000-06-20 04:54:19 +00002441 if (PyDict_SetItemString(dict, "__debug__", debug) < 0) {
2442 Py_XDECREF(debug);
Guido van Rossum25ce5661997-08-02 03:10:38 +00002443 return NULL;
Fred Drake5550de32000-06-20 04:54:19 +00002444 }
2445 Py_XDECREF(debug);
Barry Warsaw757af0e1997-08-29 22:13:51 +00002446
Guido van Rossum25ce5661997-08-02 03:10:38 +00002447 return mod;
Tim Peters7571a0f2003-03-23 17:52:28 +00002448#undef ADD_TO_ALL
Tim Peters4b7625e2001-09-13 21:37:17 +00002449#undef SETBUILTIN
Guido van Rossum3f5da241990-12-20 15:06:42 +00002450}
2451
Guido van Rossume77a7571993-11-03 15:01:26 +00002452/* Helper for filter(): filter a tuple through a function */
Guido van Rossum12d12c51993-10-26 17:58:25 +00002453
Guido van Rossum79f25d91997-04-29 20:08:16 +00002454static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002455filtertuple(PyObject *func, PyObject *tuple)
Guido van Rossum12d12c51993-10-26 17:58:25 +00002456{
Guido van Rossum79f25d91997-04-29 20:08:16 +00002457 PyObject *result;
Martin v. Löwis18e16552006-02-15 17:27:45 +00002458 Py_ssize_t i, j;
2459 Py_ssize_t len = PyTuple_Size(tuple);
Guido van Rossum12d12c51993-10-26 17:58:25 +00002460
Guido van Rossumb7b45621995-08-04 04:07:45 +00002461 if (len == 0) {
Walter Dörwaldc3da83f2003-02-04 20:24:45 +00002462 if (PyTuple_CheckExact(tuple))
2463 Py_INCREF(tuple);
2464 else
2465 tuple = PyTuple_New(0);
Guido van Rossumb7b45621995-08-04 04:07:45 +00002466 return tuple;
2467 }
2468
Guido van Rossum79f25d91997-04-29 20:08:16 +00002469 if ((result = PyTuple_New(len)) == NULL)
Guido van Rossum2586bf01993-11-01 16:21:44 +00002470 return NULL;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002471
Guido van Rossum12d12c51993-10-26 17:58:25 +00002472 for (i = j = 0; i < len; ++i) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002473 PyObject *item, *good;
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002474 int ok;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002475
Walter Dörwald8dd19322003-02-10 17:36:40 +00002476 if (tuple->ob_type->tp_as_sequence &&
2477 tuple->ob_type->tp_as_sequence->sq_item) {
2478 item = tuple->ob_type->tp_as_sequence->sq_item(tuple, i);
Walter Dörwaldc58a3a12003-08-18 18:28:45 +00002479 if (item == NULL)
2480 goto Fail_1;
Walter Dörwald8dd19322003-02-10 17:36:40 +00002481 } else {
Alex Martellia9b9c9f2003-04-23 13:34:35 +00002482 PyErr_SetString(PyExc_TypeError, "filter(): unsubscriptable tuple");
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002483 goto Fail_1;
Walter Dörwald8dd19322003-02-10 17:36:40 +00002484 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002485 if (func == Py_None) {
2486 Py_INCREF(item);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002487 good = item;
2488 }
2489 else {
Raymond Hettinger8ae46892003-10-12 19:09:37 +00002490 PyObject *arg = PyTuple_Pack(1, item);
Walter Dörwaldc58a3a12003-08-18 18:28:45 +00002491 if (arg == NULL) {
2492 Py_DECREF(item);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002493 goto Fail_1;
Walter Dörwaldc58a3a12003-08-18 18:28:45 +00002494 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002495 good = PyEval_CallObject(func, arg);
2496 Py_DECREF(arg);
Walter Dörwaldc58a3a12003-08-18 18:28:45 +00002497 if (good == NULL) {
2498 Py_DECREF(item);
Guido van Rossum12d12c51993-10-26 17:58:25 +00002499 goto Fail_1;
Walter Dörwaldc58a3a12003-08-18 18:28:45 +00002500 }
Guido van Rossum12d12c51993-10-26 17:58:25 +00002501 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002502 ok = PyObject_IsTrue(good);
2503 Py_DECREF(good);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002504 if (ok) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002505 if (PyTuple_SetItem(result, j++, item) < 0)
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002506 goto Fail_1;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002507 }
Walter Dörwaldc58a3a12003-08-18 18:28:45 +00002508 else
2509 Py_DECREF(item);
Guido van Rossum12d12c51993-10-26 17:58:25 +00002510 }
2511
Tim Peters4324aa32001-05-28 22:30:08 +00002512 if (_PyTuple_Resize(&result, j) < 0)
Guido van Rossum12d12c51993-10-26 17:58:25 +00002513 return NULL;
2514
Guido van Rossum12d12c51993-10-26 17:58:25 +00002515 return result;
2516
Guido van Rossum12d12c51993-10-26 17:58:25 +00002517Fail_1:
Guido van Rossum79f25d91997-04-29 20:08:16 +00002518 Py_DECREF(result);
Guido van Rossum12d12c51993-10-26 17:58:25 +00002519 return NULL;
2520}
2521
2522
Guido van Rossume77a7571993-11-03 15:01:26 +00002523/* Helper for filter(): filter a string through a function */
Guido van Rossum12d12c51993-10-26 17:58:25 +00002524
Guido van Rossum79f25d91997-04-29 20:08:16 +00002525static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002526filterstring(PyObject *func, PyObject *strobj)
Guido van Rossum12d12c51993-10-26 17:58:25 +00002527{
Guido van Rossum79f25d91997-04-29 20:08:16 +00002528 PyObject *result;
Martin v. Löwis18e16552006-02-15 17:27:45 +00002529 Py_ssize_t i, j;
2530 Py_ssize_t len = PyString_Size(strobj);
2531 Py_ssize_t outlen = len;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002532
Guido van Rossum79f25d91997-04-29 20:08:16 +00002533 if (func == Py_None) {
Walter Dörwald1918f772003-02-10 13:19:13 +00002534 /* If it's a real string we can return the original,
2535 * as no character is ever false and __getitem__
2536 * does return this character. If it's a subclass
2537 * we must go through the __getitem__ loop */
2538 if (PyString_CheckExact(strobj)) {
Walter Dörwaldc3da83f2003-02-04 20:24:45 +00002539 Py_INCREF(strobj);
Walter Dörwald1918f772003-02-10 13:19:13 +00002540 return strobj;
2541 }
Guido van Rossum12d12c51993-10-26 17:58:25 +00002542 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002543 if ((result = PyString_FromStringAndSize(NULL, len)) == NULL)
Guido van Rossum2586bf01993-11-01 16:21:44 +00002544 return NULL;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002545
Guido van Rossum12d12c51993-10-26 17:58:25 +00002546 for (i = j = 0; i < len; ++i) {
Walter Dörwald1918f772003-02-10 13:19:13 +00002547 PyObject *item;
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002548 int ok;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002549
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002550 item = (*strobj->ob_type->tp_as_sequence->sq_item)(strobj, i);
2551 if (item == NULL)
2552 goto Fail_1;
Walter Dörwald1918f772003-02-10 13:19:13 +00002553 if (func==Py_None) {
2554 ok = 1;
2555 } else {
2556 PyObject *arg, *good;
Raymond Hettinger8ae46892003-10-12 19:09:37 +00002557 arg = PyTuple_Pack(1, item);
Walter Dörwald1918f772003-02-10 13:19:13 +00002558 if (arg == NULL) {
2559 Py_DECREF(item);
2560 goto Fail_1;
2561 }
2562 good = PyEval_CallObject(func, arg);
2563 Py_DECREF(arg);
2564 if (good == NULL) {
2565 Py_DECREF(item);
2566 goto Fail_1;
2567 }
2568 ok = PyObject_IsTrue(good);
2569 Py_DECREF(good);
Tim Peters388ed082001-04-07 20:34:48 +00002570 }
Walter Dörwald903f1e02003-02-04 16:28:00 +00002571 if (ok) {
Martin v. Löwisd96ee902006-02-16 14:37:16 +00002572 Py_ssize_t reslen;
Walter Dörwald903f1e02003-02-04 16:28:00 +00002573 if (!PyString_Check(item)) {
2574 PyErr_SetString(PyExc_TypeError, "can't filter str to str:"
2575 " __getitem__ returned different type");
2576 Py_DECREF(item);
2577 goto Fail_1;
2578 }
2579 reslen = PyString_GET_SIZE(item);
2580 if (reslen == 1) {
2581 PyString_AS_STRING(result)[j++] =
2582 PyString_AS_STRING(item)[0];
2583 } else {
2584 /* do we need more space? */
Martin v. Löwisd96ee902006-02-16 14:37:16 +00002585 Py_ssize_t need = j + reslen + len-i-1;
Walter Dörwald903f1e02003-02-04 16:28:00 +00002586 if (need > outlen) {
2587 /* overallocate, to avoid reallocations */
2588 if (need<2*outlen)
2589 need = 2*outlen;
2590 if (_PyString_Resize(&result, need)) {
2591 Py_DECREF(item);
2592 return NULL;
2593 }
2594 outlen = need;
2595 }
2596 memcpy(
2597 PyString_AS_STRING(result) + j,
2598 PyString_AS_STRING(item),
2599 reslen
2600 );
2601 j += reslen;
2602 }
2603 }
Tim Peters388ed082001-04-07 20:34:48 +00002604 Py_DECREF(item);
Guido van Rossum12d12c51993-10-26 17:58:25 +00002605 }
2606
Walter Dörwald903f1e02003-02-04 16:28:00 +00002607 if (j < outlen)
Tim Peters5de98422002-04-27 18:44:32 +00002608 _PyString_Resize(&result, j);
Guido van Rossum12d12c51993-10-26 17:58:25 +00002609
Guido van Rossum12d12c51993-10-26 17:58:25 +00002610 return result;
2611
Guido van Rossum12d12c51993-10-26 17:58:25 +00002612Fail_1:
Guido van Rossum79f25d91997-04-29 20:08:16 +00002613 Py_DECREF(result);
Guido van Rossum12d12c51993-10-26 17:58:25 +00002614 return NULL;
2615}
Martin v. Löwis8afd7572003-01-25 22:46:11 +00002616
2617#ifdef Py_USING_UNICODE
2618/* Helper for filter(): filter a Unicode object through a function */
2619
2620static PyObject *
2621filterunicode(PyObject *func, PyObject *strobj)
2622{
2623 PyObject *result;
Martin v. Löwis725507b2006-03-07 12:08:51 +00002624 register Py_ssize_t i, j;
Martin v. Löwisd96ee902006-02-16 14:37:16 +00002625 Py_ssize_t len = PyUnicode_GetSize(strobj);
2626 Py_ssize_t outlen = len;
Martin v. Löwis8afd7572003-01-25 22:46:11 +00002627
2628 if (func == Py_None) {
Walter Dörwald1918f772003-02-10 13:19:13 +00002629 /* If it's a real string we can return the original,
2630 * as no character is ever false and __getitem__
2631 * does return this character. If it's a subclass
2632 * we must go through the __getitem__ loop */
2633 if (PyUnicode_CheckExact(strobj)) {
Walter Dörwaldc3da83f2003-02-04 20:24:45 +00002634 Py_INCREF(strobj);
Walter Dörwald1918f772003-02-10 13:19:13 +00002635 return strobj;
2636 }
Martin v. Löwis8afd7572003-01-25 22:46:11 +00002637 }
2638 if ((result = PyUnicode_FromUnicode(NULL, len)) == NULL)
2639 return NULL;
2640
2641 for (i = j = 0; i < len; ++i) {
2642 PyObject *item, *arg, *good;
2643 int ok;
2644
2645 item = (*strobj->ob_type->tp_as_sequence->sq_item)(strobj, i);
2646 if (item == NULL)
2647 goto Fail_1;
Walter Dörwald1918f772003-02-10 13:19:13 +00002648 if (func == Py_None) {
2649 ok = 1;
2650 } else {
Raymond Hettinger8ae46892003-10-12 19:09:37 +00002651 arg = PyTuple_Pack(1, item);
Walter Dörwald1918f772003-02-10 13:19:13 +00002652 if (arg == NULL) {
2653 Py_DECREF(item);
2654 goto Fail_1;
2655 }
2656 good = PyEval_CallObject(func, arg);
2657 Py_DECREF(arg);
2658 if (good == NULL) {
2659 Py_DECREF(item);
2660 goto Fail_1;
2661 }
2662 ok = PyObject_IsTrue(good);
2663 Py_DECREF(good);
Martin v. Löwis8afd7572003-01-25 22:46:11 +00002664 }
Walter Dörwald903f1e02003-02-04 16:28:00 +00002665 if (ok) {
Martin v. Löwisd96ee902006-02-16 14:37:16 +00002666 Py_ssize_t reslen;
Walter Dörwald903f1e02003-02-04 16:28:00 +00002667 if (!PyUnicode_Check(item)) {
Georg Brandl99d7e4e2005-08-31 22:21:15 +00002668 PyErr_SetString(PyExc_TypeError,
Jeremy Hylton1aad9c72003-09-16 03:10:59 +00002669 "can't filter unicode to unicode:"
2670 " __getitem__ returned different type");
Walter Dörwald903f1e02003-02-04 16:28:00 +00002671 Py_DECREF(item);
2672 goto Fail_1;
2673 }
2674 reslen = PyUnicode_GET_SIZE(item);
Georg Brandl99d7e4e2005-08-31 22:21:15 +00002675 if (reslen == 1)
Walter Dörwald903f1e02003-02-04 16:28:00 +00002676 PyUnicode_AS_UNICODE(result)[j++] =
2677 PyUnicode_AS_UNICODE(item)[0];
Jeremy Hylton1aad9c72003-09-16 03:10:59 +00002678 else {
Walter Dörwald903f1e02003-02-04 16:28:00 +00002679 /* do we need more space? */
Martin v. Löwisd96ee902006-02-16 14:37:16 +00002680 Py_ssize_t need = j + reslen + len - i - 1;
Walter Dörwald903f1e02003-02-04 16:28:00 +00002681 if (need > outlen) {
Georg Brandl99d7e4e2005-08-31 22:21:15 +00002682 /* overallocate,
Jeremy Hylton1aad9c72003-09-16 03:10:59 +00002683 to avoid reallocations */
2684 if (need < 2 * outlen)
2685 need = 2 * outlen;
Jeremy Hylton364f6be2003-09-16 03:17:16 +00002686 if (PyUnicode_Resize(
2687 &result, need) < 0) {
Walter Dörwald903f1e02003-02-04 16:28:00 +00002688 Py_DECREF(item);
Walter Dörwald531e0002003-02-04 16:57:49 +00002689 goto Fail_1;
Walter Dörwald903f1e02003-02-04 16:28:00 +00002690 }
2691 outlen = need;
2692 }
Jeremy Hylton1aad9c72003-09-16 03:10:59 +00002693 memcpy(PyUnicode_AS_UNICODE(result) + j,
2694 PyUnicode_AS_UNICODE(item),
2695 reslen*sizeof(Py_UNICODE));
Walter Dörwald903f1e02003-02-04 16:28:00 +00002696 j += reslen;
2697 }
2698 }
Martin v. Löwis8afd7572003-01-25 22:46:11 +00002699 Py_DECREF(item);
2700 }
2701
Walter Dörwald903f1e02003-02-04 16:28:00 +00002702 if (j < outlen)
Martin v. Löwis8afd7572003-01-25 22:46:11 +00002703 PyUnicode_Resize(&result, j);
2704
2705 return result;
2706
2707Fail_1:
2708 Py_DECREF(result);
2709 return NULL;
2710}
2711#endif