blob: 7647523b580feca999f76aa72453e1460443eb55 [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"
Georg Brandlfc8eef32008-03-28 12:11:56 +00004#include "Python-ast.h"
Guido van Rossum3f5da241990-12-20 15:06:42 +00005
6#include "node.h"
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00007#include "code.h"
Guido van Rossum5b722181993-03-30 17:46:03 +00008#include "eval.h"
Guido van Rossum3f5da241990-12-20 15:06:42 +00009
Guido van Rossum6bf62da1997-04-11 20:37:35 +000010#include <ctype.h>
11
Guido van Rossume2ae77b2001-10-24 20:42:55 +000012#ifdef RISCOS
13#include "unixstuff.h"
14#endif
15
Mark Hammond26cffde2001-05-14 12:17:34 +000016/* The default encoding used by the platform file system APIs
17 Can remain NULL for all platforms that don't have such a concept
18*/
Martin v. Löwis6238d2b2002-06-30 15:26:10 +000019#if defined(MS_WINDOWS) && defined(HAVE_USABLE_WCHAR_T)
Mark Hammond26cffde2001-05-14 12:17:34 +000020const char *Py_FileSystemDefaultEncoding = "mbcs";
Just van Rossumb9b8e9c2003-02-10 09:22:01 +000021#elif defined(__APPLE__)
22const char *Py_FileSystemDefaultEncoding = "utf-8";
Mark Hammond26cffde2001-05-14 12:17:34 +000023#else
24const char *Py_FileSystemDefaultEncoding = NULL; /* use default */
25#endif
Mark Hammondef8b6542001-05-13 08:04:26 +000026
Guido van Rossum12d12c51993-10-26 17:58:25 +000027/* Forward */
Tim Petersdbd9ba62000-07-09 03:09:57 +000028static PyObject *filterstring(PyObject *, PyObject *);
Martin v. Löwis8afd7572003-01-25 22:46:11 +000029#ifdef Py_USING_UNICODE
30static PyObject *filterunicode(PyObject *, PyObject *);
31#endif
Tim Petersdbd9ba62000-07-09 03:09:57 +000032static PyObject *filtertuple (PyObject *, PyObject *);
Guido van Rossum12d12c51993-10-26 17:58:25 +000033
Guido van Rossum79f25d91997-04-29 20:08:16 +000034static PyObject *
Neal Norwitz92e212f2006-04-03 04:48:37 +000035builtin___import__(PyObject *self, PyObject *args, PyObject *kwds)
Guido van Rossum3f5da241990-12-20 15:06:42 +000036{
Neal Norwitz92e212f2006-04-03 04:48:37 +000037 static char *kwlist[] = {"name", "globals", "locals", "fromlist",
38 "level", 0};
Guido van Rossum1ae940a1995-01-02 19:04:15 +000039 char *name;
Guido van Rossum79f25d91997-04-29 20:08:16 +000040 PyObject *globals = NULL;
41 PyObject *locals = NULL;
42 PyObject *fromlist = NULL;
Thomas Woutersf7f438b2006-02-28 16:09:29 +000043 int level = -1;
Guido van Rossum1ae940a1995-01-02 19:04:15 +000044
Neal Norwitz92e212f2006-04-03 04:48:37 +000045 if (!PyArg_ParseTupleAndKeywords(args, kwds, "s|OOOi:__import__",
46 kwlist, &name, &globals, &locals, &fromlist, &level))
Guido van Rossum1ae940a1995-01-02 19:04:15 +000047 return NULL;
Thomas Woutersf7f438b2006-02-28 16:09:29 +000048 return PyImport_ImportModuleLevel(name, globals, locals,
49 fromlist, level);
Guido van Rossum1ae940a1995-01-02 19:04:15 +000050}
51
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000052PyDoc_STRVAR(import_doc,
Neal Norwitz92e212f2006-04-03 04:48:37 +000053"__import__(name, globals={}, locals={}, fromlist=[], level=-1) -> module\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +000054\n\
55Import a module. The globals are only used to determine the context;\n\
56they are not modified. The locals are currently unused. The fromlist\n\
57should be a list of names to emulate ``from name import ...'', or an\n\
58empty list to emulate ``import name''.\n\
59When importing a module from a package, note that __import__('A.B', ...)\n\
60returns package A when fromlist is empty, but its submodule B when\n\
Neal Norwitz92e212f2006-04-03 04:48:37 +000061fromlist is not empty. Level is used to determine whether to perform \n\
62absolute or relative imports. -1 is the original strategy of attempting\n\
63both absolute and relative imports, 0 is absolute, a positive number\n\
64is the number of parent directories to search relative to the current module.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +000065
Guido van Rossum1ae940a1995-01-02 19:04:15 +000066
Guido van Rossum79f25d91997-04-29 20:08:16 +000067static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +000068builtin_abs(PyObject *self, PyObject *v)
Guido van Rossum1ae940a1995-01-02 19:04:15 +000069{
Guido van Rossum09df08a1998-05-22 00:51:39 +000070 return PyNumber_Absolute(v);
Guido van Rossum3f5da241990-12-20 15:06:42 +000071}
72
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000073PyDoc_STRVAR(abs_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +000074"abs(number) -> number\n\
75\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000076Return the absolute value of the argument.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +000077
Raymond Hettinger96229b12005-03-11 06:49:40 +000078static PyObject *
79builtin_all(PyObject *self, PyObject *v)
80{
81 PyObject *it, *item;
Guido van Rossum01dbc102007-12-20 23:48:28 +000082 PyObject *(*iternext)(PyObject *);
83 int cmp;
Raymond Hettinger96229b12005-03-11 06:49:40 +000084
85 it = PyObject_GetIter(v);
86 if (it == NULL)
87 return NULL;
Guido van Rossum01dbc102007-12-20 23:48:28 +000088 iternext = *Py_TYPE(it)->tp_iternext;
Raymond Hettinger96229b12005-03-11 06:49:40 +000089
Guido van Rossum01dbc102007-12-20 23:48:28 +000090 for (;;) {
91 item = iternext(it);
92 if (item == NULL)
93 break;
94 cmp = PyObject_IsTrue(item);
Raymond Hettinger96229b12005-03-11 06:49:40 +000095 Py_DECREF(item);
96 if (cmp < 0) {
97 Py_DECREF(it);
98 return NULL;
99 }
100 if (cmp == 0) {
101 Py_DECREF(it);
102 Py_RETURN_FALSE;
103 }
104 }
105 Py_DECREF(it);
Guido van Rossum01dbc102007-12-20 23:48:28 +0000106 if (PyErr_Occurred()) {
107 if (PyErr_ExceptionMatches(PyExc_StopIteration))
108 PyErr_Clear();
109 else
110 return NULL;
111 }
Raymond Hettinger96229b12005-03-11 06:49:40 +0000112 Py_RETURN_TRUE;
113}
114
115PyDoc_STRVAR(all_doc,
116"all(iterable) -> bool\n\
117\n\
118Return True if bool(x) is True for all values x in the iterable.");
119
120static PyObject *
121builtin_any(PyObject *self, PyObject *v)
122{
123 PyObject *it, *item;
Guido van Rossum01dbc102007-12-20 23:48:28 +0000124 PyObject *(*iternext)(PyObject *);
125 int cmp;
Raymond Hettinger96229b12005-03-11 06:49:40 +0000126
127 it = PyObject_GetIter(v);
128 if (it == NULL)
129 return NULL;
Guido van Rossum01dbc102007-12-20 23:48:28 +0000130 iternext = *Py_TYPE(it)->tp_iternext;
Raymond Hettinger96229b12005-03-11 06:49:40 +0000131
Guido van Rossum01dbc102007-12-20 23:48:28 +0000132 for (;;) {
133 item = iternext(it);
134 if (item == NULL)
135 break;
136 cmp = PyObject_IsTrue(item);
Raymond Hettinger96229b12005-03-11 06:49:40 +0000137 Py_DECREF(item);
138 if (cmp < 0) {
139 Py_DECREF(it);
140 return NULL;
141 }
142 if (cmp == 1) {
143 Py_DECREF(it);
144 Py_RETURN_TRUE;
145 }
146 }
147 Py_DECREF(it);
Guido van Rossum01dbc102007-12-20 23:48:28 +0000148 if (PyErr_Occurred()) {
149 if (PyErr_ExceptionMatches(PyExc_StopIteration))
150 PyErr_Clear();
151 else
152 return NULL;
153 }
Raymond Hettinger96229b12005-03-11 06:49:40 +0000154 Py_RETURN_FALSE;
155}
156
157PyDoc_STRVAR(any_doc,
158"any(iterable) -> bool\n\
159\n\
160Return True if bool(x) is True for any x in the iterable.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000161
Guido van Rossum79f25d91997-04-29 20:08:16 +0000162static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000163builtin_apply(PyObject *self, PyObject *args)
Guido van Rossumc02e15c1991-12-16 13:03:00 +0000164{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000165 PyObject *func, *alist = NULL, *kwdict = NULL;
Barry Warsaw968f8cb1998-10-01 15:33:12 +0000166 PyObject *t = NULL, *retval = NULL;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000167
Benjamin Peterson9f4f4812008-04-27 03:01:45 +0000168 if (PyErr_WarnPy3k("apply() not supported in 3.x; "
Benjamin Petersonf19a7b92008-04-27 18:40:21 +0000169 "use func(*args, **kwargs)", 1) < 0)
Neal Norwitz8b2bfbc2007-05-23 06:35:32 +0000170 return NULL;
171
Raymond Hettingerea3fdf42002-12-29 16:33:45 +0000172 if (!PyArg_UnpackTuple(args, "apply", 1, 3, &func, &alist, &kwdict))
Guido van Rossumc02e15c1991-12-16 13:03:00 +0000173 return NULL;
Barry Warsaw968f8cb1998-10-01 15:33:12 +0000174 if (alist != NULL) {
175 if (!PyTuple_Check(alist)) {
176 if (!PySequence_Check(alist)) {
Jeremy Hyltonc862cf42001-01-19 03:25:05 +0000177 PyErr_Format(PyExc_TypeError,
Alex Martellia9b9c9f2003-04-23 13:34:35 +0000178 "apply() arg 2 expected sequence, found %s",
Jeremy Hyltonc862cf42001-01-19 03:25:05 +0000179 alist->ob_type->tp_name);
Barry Warsaw968f8cb1998-10-01 15:33:12 +0000180 return NULL;
181 }
182 t = PySequence_Tuple(alist);
183 if (t == NULL)
184 return NULL;
185 alist = t;
186 }
Guido van Rossum2d951851994-08-29 12:52:16 +0000187 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000188 if (kwdict != NULL && !PyDict_Check(kwdict)) {
Jeremy Hyltonc862cf42001-01-19 03:25:05 +0000189 PyErr_Format(PyExc_TypeError,
190 "apply() arg 3 expected dictionary, found %s",
191 kwdict->ob_type->tp_name);
Barry Warsaw968f8cb1998-10-01 15:33:12 +0000192 goto finally;
Guido van Rossum681d79a1995-07-18 14:51:37 +0000193 }
Barry Warsaw968f8cb1998-10-01 15:33:12 +0000194 retval = PyEval_CallObjectWithKeywords(func, alist, kwdict);
195 finally:
196 Py_XDECREF(t);
197 return retval;
Guido van Rossumc02e15c1991-12-16 13:03:00 +0000198}
199
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000200PyDoc_STRVAR(apply_doc,
Fred Drakef1fbc622001-01-12 17:05:05 +0000201"apply(object[, args[, kwargs]]) -> value\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000202\n\
Fred Drake7b912121999-12-23 14:16:55 +0000203Call a callable object with positional arguments taken from the tuple args,\n\
204and keyword arguments taken from the optional dictionary kwargs.\n\
Raymond Hettingerff41c482003-04-06 09:01:11 +0000205Note that classes are callable, as are instances with a __call__() method.\n\
206\n\
207Deprecated since release 2.3. Instead, use the extended call syntax:\n\
208 function(*args, **keywords).");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000209
210
Guido van Rossum79f25d91997-04-29 20:08:16 +0000211static PyObject *
Eric Smith3cd81942008-02-22 16:30:22 +0000212builtin_bin(PyObject *self, PyObject *v)
213{
Raymond Hettingere3ae6552008-06-20 04:18:15 +0000214 PyNumberMethods *nb;
215 PyObject *res;
216
217 if ((nb = v->ob_type->tp_as_number) == NULL ||
218 nb->nb_hex == NULL) {
219 PyErr_SetString(PyExc_TypeError,
220 "bin() argument can't be converted to hex");
221 return NULL;
222 }
223 res = (*nb->nb_bin)(v);
224 if (res && !PyString_Check(res)) {
225 PyErr_Format(PyExc_TypeError,
226 "__bin__ returned non-string (type %.200s)",
227 res->ob_type->tp_name);
228 Py_DECREF(res);
229 return NULL;
230 }
231 return res;
Eric Smith3cd81942008-02-22 16:30:22 +0000232}
233
234PyDoc_STRVAR(bin_doc,
235"bin(number) -> string\n\
236\n\
237Return the binary representation of an integer or long integer.");
238
239
240static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000241builtin_callable(PyObject *self, PyObject *v)
Guido van Rossum2d951851994-08-29 12:52:16 +0000242{
Benjamin Peterson9f4f4812008-04-27 03:01:45 +0000243 if (PyErr_WarnPy3k("callable() not supported in 3.x; "
Benjamin Petersonf19a7b92008-04-27 18:40:21 +0000244 "use hasattr(o, '__call__')", 1) < 0)
Neal Norwitzdf25efe2007-05-23 06:58:36 +0000245 return NULL;
Guido van Rossum77f6a652002-04-03 22:41:51 +0000246 return PyBool_FromLong((long)PyCallable_Check(v));
Guido van Rossum2d951851994-08-29 12:52:16 +0000247}
248
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000249PyDoc_STRVAR(callable_doc,
Guido van Rossum77f6a652002-04-03 22:41:51 +0000250"callable(object) -> bool\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000251\n\
252Return whether the object is callable (i.e., some kind of function).\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000253Note that classes are callable, as are instances with a __call__() method.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000254
255
Guido van Rossum79f25d91997-04-29 20:08:16 +0000256static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000257builtin_filter(PyObject *self, PyObject *args)
Guido van Rossum12d12c51993-10-26 17:58:25 +0000258{
Guido van Rossumc7903a12002-08-16 07:04:56 +0000259 PyObject *func, *seq, *result, *it, *arg;
Martin v. Löwisd96ee902006-02-16 14:37:16 +0000260 Py_ssize_t len; /* guess for result list size */
261 register Py_ssize_t j;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000262
Raymond Hettingerea3fdf42002-12-29 16:33:45 +0000263 if (!PyArg_UnpackTuple(args, "filter", 2, 2, &func, &seq))
Guido van Rossum12d12c51993-10-26 17:58:25 +0000264 return NULL;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000265
Tim Peters0e57abf2001-05-02 07:39:38 +0000266 /* Strings and tuples return a result of the same type. */
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000267 if (PyString_Check(seq))
Tim Peters0e57abf2001-05-02 07:39:38 +0000268 return filterstring(func, seq);
Martin v. Löwis8afd7572003-01-25 22:46:11 +0000269#ifdef Py_USING_UNICODE
270 if (PyUnicode_Check(seq))
271 return filterunicode(func, seq);
272#endif
Tim Peters0e57abf2001-05-02 07:39:38 +0000273 if (PyTuple_Check(seq))
274 return filtertuple(func, seq);
275
Georg Brandle35b6572005-07-19 22:20:20 +0000276 /* Pre-allocate argument list tuple. */
277 arg = PyTuple_New(1);
278 if (arg == NULL)
279 return NULL;
280
Tim Peters0e57abf2001-05-02 07:39:38 +0000281 /* Get iterator. */
282 it = PyObject_GetIter(seq);
283 if (it == NULL)
Georg Brandle35b6572005-07-19 22:20:20 +0000284 goto Fail_arg;
Tim Peters0e57abf2001-05-02 07:39:38 +0000285
286 /* Guess a result list size. */
Raymond Hettinger4e2f7142007-12-06 00:56:53 +0000287 len = _PyObject_LengthHint(seq, 8);
Guido van Rossum12d12c51993-10-26 17:58:25 +0000288
Tim Peters0e57abf2001-05-02 07:39:38 +0000289 /* Get a result list. */
Guido van Rossum79f25d91997-04-29 20:08:16 +0000290 if (PyList_Check(seq) && seq->ob_refcnt == 1) {
Tim Peters0e57abf2001-05-02 07:39:38 +0000291 /* Eww - can modify the list in-place. */
Guido van Rossum79f25d91997-04-29 20:08:16 +0000292 Py_INCREF(seq);
Guido van Rossum12d12c51993-10-26 17:58:25 +0000293 result = seq;
294 }
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000295 else {
Tim Peters0e57abf2001-05-02 07:39:38 +0000296 result = PyList_New(len);
297 if (result == NULL)
298 goto Fail_it;
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000299 }
Guido van Rossum12d12c51993-10-26 17:58:25 +0000300
Tim Peters0e57abf2001-05-02 07:39:38 +0000301 /* Build the result list. */
Tim Petersf4848da2001-05-05 00:14:56 +0000302 j = 0;
303 for (;;) {
Guido van Rossumc7903a12002-08-16 07:04:56 +0000304 PyObject *item;
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000305 int ok;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000306
Tim Peters0e57abf2001-05-02 07:39:38 +0000307 item = PyIter_Next(it);
308 if (item == NULL) {
Tim Petersf4848da2001-05-05 00:14:56 +0000309 if (PyErr_Occurred())
310 goto Fail_result_it;
Tim Peters0e57abf2001-05-02 07:39:38 +0000311 break;
Guido van Rossum2d951851994-08-29 12:52:16 +0000312 }
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000313
Neil Schemenauer68973552003-08-14 20:37:34 +0000314 if (func == (PyObject *)&PyBool_Type || func == Py_None) {
Guido van Rossumc7903a12002-08-16 07:04:56 +0000315 ok = PyObject_IsTrue(item);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000316 }
317 else {
Guido van Rossumc7903a12002-08-16 07:04:56 +0000318 PyObject *good;
319 PyTuple_SET_ITEM(arg, 0, item);
320 good = PyObject_Call(func, arg, NULL);
321 PyTuple_SET_ITEM(arg, 0, NULL);
Guido van Rossum58b68731995-01-10 17:40:55 +0000322 if (good == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000323 Py_DECREF(item);
Tim Peters0e57abf2001-05-02 07:39:38 +0000324 goto Fail_result_it;
Guido van Rossum58b68731995-01-10 17:40:55 +0000325 }
Guido van Rossumc7903a12002-08-16 07:04:56 +0000326 ok = PyObject_IsTrue(good);
327 Py_DECREF(good);
Guido van Rossum12d12c51993-10-26 17:58:25 +0000328 }
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000329 if (ok) {
Tim Peters0e57abf2001-05-02 07:39:38 +0000330 if (j < len)
331 PyList_SET_ITEM(result, j, item);
Guido van Rossum2d951851994-08-29 12:52:16 +0000332 else {
Barry Warsawfa77e091999-01-28 18:49:12 +0000333 int status = PyList_Append(result, item);
Barry Warsawfa77e091999-01-28 18:49:12 +0000334 Py_DECREF(item);
335 if (status < 0)
Tim Peters0e57abf2001-05-02 07:39:38 +0000336 goto Fail_result_it;
Guido van Rossum2d951851994-08-29 12:52:16 +0000337 }
Tim Peters0e57abf2001-05-02 07:39:38 +0000338 ++j;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000339 }
Tim Peters0e57abf2001-05-02 07:39:38 +0000340 else
341 Py_DECREF(item);
Guido van Rossum12d12c51993-10-26 17:58:25 +0000342 }
343
Guido van Rossum12d12c51993-10-26 17:58:25 +0000344
Tim Peters0e57abf2001-05-02 07:39:38 +0000345 /* Cut back result list if len is too big. */
Guido van Rossum79f25d91997-04-29 20:08:16 +0000346 if (j < len && PyList_SetSlice(result, j, len, NULL) < 0)
Tim Peters0e57abf2001-05-02 07:39:38 +0000347 goto Fail_result_it;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000348
Tim Peters3c6b1482001-05-21 08:07:05 +0000349 Py_DECREF(it);
Guido van Rossumc7903a12002-08-16 07:04:56 +0000350 Py_DECREF(arg);
Guido van Rossum12d12c51993-10-26 17:58:25 +0000351 return result;
352
Tim Peters0e57abf2001-05-02 07:39:38 +0000353Fail_result_it:
Guido van Rossum79f25d91997-04-29 20:08:16 +0000354 Py_DECREF(result);
Tim Peters0e57abf2001-05-02 07:39:38 +0000355Fail_it:
356 Py_DECREF(it);
Guido van Rossumc7903a12002-08-16 07:04:56 +0000357Fail_arg:
358 Py_DECREF(arg);
Guido van Rossum12d12c51993-10-26 17:58:25 +0000359 return NULL;
360}
361
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000362PyDoc_STRVAR(filter_doc,
Tim Petersd50e5442002-03-09 00:06:26 +0000363"filter(function or None, sequence) -> list, tuple, or string\n"
364"\n"
365"Return those items of sequence for which function(item) is true. If\n"
366"function is None, return the items that are true. If sequence is a tuple\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000367"or string, return the same type, else return a list.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000368
Guido van Rossum79f25d91997-04-29 20:08:16 +0000369static PyObject *
Eric Smitha9f7d622008-02-17 19:46:49 +0000370builtin_format(PyObject *self, PyObject *args)
371{
372 PyObject *value;
373 PyObject *format_spec = NULL;
374
375 if (!PyArg_ParseTuple(args, "O|O:format", &value, &format_spec))
376 return NULL;
377
378 return PyObject_Format(value, format_spec);
379}
380
381PyDoc_STRVAR(format_doc,
382"format(value[, format_spec]) -> string\n\
383\n\
384Returns value.__format__(format_spec)\n\
385format_spec defaults to \"\"");
386
387static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000388builtin_chr(PyObject *self, PyObject *args)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000389{
390 long x;
391 char s[1];
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000392
Guido van Rossum79f25d91997-04-29 20:08:16 +0000393 if (!PyArg_ParseTuple(args, "l:chr", &x))
Guido van Rossum3f5da241990-12-20 15:06:42 +0000394 return NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000395 if (x < 0 || x >= 256) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000396 PyErr_SetString(PyExc_ValueError,
397 "chr() arg not in range(256)");
Guido van Rossum3f5da241990-12-20 15:06:42 +0000398 return NULL;
399 }
Guido van Rossum6bf62da1997-04-11 20:37:35 +0000400 s[0] = (char)x;
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000401 return PyString_FromStringAndSize(s, 1);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000402}
403
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000404PyDoc_STRVAR(chr_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000405"chr(i) -> character\n\
406\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000407Return a string of one character with ordinal i; 0 <= i < 256.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000408
409
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000410#ifdef Py_USING_UNICODE
Guido van Rossum79f25d91997-04-29 20:08:16 +0000411static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000412builtin_unichr(PyObject *self, PyObject *args)
Guido van Rossum09095f32000-03-10 23:00:52 +0000413{
414 long x;
Guido van Rossum09095f32000-03-10 23:00:52 +0000415
416 if (!PyArg_ParseTuple(args, "l:unichr", &x))
417 return NULL;
Fredrik Lundh0dcf67e2001-06-26 20:01:56 +0000418
Marc-André Lemburgcc8764c2002-08-11 12:23:04 +0000419 return PyUnicode_FromOrdinal(x);
Guido van Rossum09095f32000-03-10 23:00:52 +0000420}
421
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000422PyDoc_STRVAR(unichr_doc,
Fred Drake078b24f2000-04-13 02:42:50 +0000423"unichr(i) -> Unicode character\n\
Guido van Rossum09095f32000-03-10 23:00:52 +0000424\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000425Return a Unicode string of one character with ordinal i; 0 <= i <= 0x10ffff.");
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000426#endif
Guido van Rossum09095f32000-03-10 23:00:52 +0000427
428
429static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000430builtin_cmp(PyObject *self, PyObject *args)
Guido van Rossuma9e7dc11992-10-18 18:53:57 +0000431{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000432 PyObject *a, *b;
Guido van Rossum09df08a1998-05-22 00:51:39 +0000433 int c;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000434
Raymond Hettingerea3fdf42002-12-29 16:33:45 +0000435 if (!PyArg_UnpackTuple(args, "cmp", 2, 2, &a, &b))
Guido van Rossuma9e7dc11992-10-18 18:53:57 +0000436 return NULL;
Guido van Rossum09df08a1998-05-22 00:51:39 +0000437 if (PyObject_Cmp(a, b, &c) < 0)
Guido van Rossumc8b6df91997-05-23 00:06:51 +0000438 return NULL;
Guido van Rossum09df08a1998-05-22 00:51:39 +0000439 return PyInt_FromLong((long)c);
Guido van Rossuma9e7dc11992-10-18 18:53:57 +0000440}
441
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000442PyDoc_STRVAR(cmp_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000443"cmp(x, y) -> integer\n\
444\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000445Return negative if x<y, zero if x==y, positive if x>y.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000446
447
Guido van Rossum79f25d91997-04-29 20:08:16 +0000448static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000449builtin_coerce(PyObject *self, PyObject *args)
Guido van Rossum6a00cd81995-01-07 12:39:01 +0000450{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000451 PyObject *v, *w;
452 PyObject *res;
Guido van Rossum5524a591995-01-10 15:26:20 +0000453
Benjamin Peterson9f4f4812008-04-27 03:01:45 +0000454 if (PyErr_WarnPy3k("coerce() not supported in 3.x", 1) < 0)
Neal Norwitzdf25efe2007-05-23 06:58:36 +0000455 return NULL;
456
Raymond Hettingerea3fdf42002-12-29 16:33:45 +0000457 if (!PyArg_UnpackTuple(args, "coerce", 2, 2, &v, &w))
Guido van Rossum5524a591995-01-10 15:26:20 +0000458 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000459 if (PyNumber_Coerce(&v, &w) < 0)
Guido van Rossum04691fc1992-08-12 15:35:34 +0000460 return NULL;
Raymond Hettinger8ae46892003-10-12 19:09:37 +0000461 res = PyTuple_Pack(2, v, w);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000462 Py_DECREF(v);
463 Py_DECREF(w);
Guido van Rossum04691fc1992-08-12 15:35:34 +0000464 return res;
465}
466
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000467PyDoc_STRVAR(coerce_doc,
Martin v. Löwis8d494f32004-08-25 10:42:41 +0000468"coerce(x, y) -> (x1, y1)\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000469\n\
Martin v. Löwis8d494f32004-08-25 10:42:41 +0000470Return a tuple consisting of the two numeric arguments converted to\n\
471a common type, using the same rules as used by arithmetic operations.\n\
472If coercion is not possible, raise TypeError.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000473
Guido van Rossum79f25d91997-04-29 20:08:16 +0000474static PyObject *
Georg Brandl5240d742007-03-13 20:46:32 +0000475builtin_compile(PyObject *self, PyObject *args, PyObject *kwds)
Guido van Rossum5b722181993-03-30 17:46:03 +0000476{
477 char *str;
478 char *filename;
479 char *startstr;
Georg Brandlf2bfd542008-03-29 13:24:23 +0000480 int mode = -1;
Tim Peters6cd6a822001-08-17 22:11:27 +0000481 int dont_inherit = 0;
482 int supplied_flags = 0;
Tim Peters5ba58662001-07-16 02:29:45 +0000483 PyCompilerFlags cf;
Neal Norwitz3a9a3e72005-11-27 20:38:31 +0000484 PyObject *result = NULL, *cmd, *tmp = NULL;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000485 Py_ssize_t length;
Georg Brandl5240d742007-03-13 20:46:32 +0000486 static char *kwlist[] = {"source", "filename", "mode", "flags",
487 "dont_inherit", NULL};
Georg Brandlf2bfd542008-03-29 13:24:23 +0000488 int start[] = {Py_file_input, Py_eval_input, Py_single_input};
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000489
Georg Brandl5240d742007-03-13 20:46:32 +0000490 if (!PyArg_ParseTupleAndKeywords(args, kwds, "Oss|ii:compile",
491 kwlist, &cmd, &filename, &startstr,
492 &supplied_flags, &dont_inherit))
Guido van Rossum5b722181993-03-30 17:46:03 +0000493 return NULL;
Tim Peters6cd6a822001-08-17 22:11:27 +0000494
Just van Rossum3aaf42c2003-02-10 08:21:10 +0000495 cf.cf_flags = supplied_flags;
496
Georg Brandlfc8eef32008-03-28 12:11:56 +0000497 if (supplied_flags &
498 ~(PyCF_MASK | PyCF_MASK_OBSOLETE | PyCF_DONT_IMPLY_DEDENT | PyCF_ONLY_AST))
499 {
500 PyErr_SetString(PyExc_ValueError,
501 "compile(): unrecognised flags");
502 return NULL;
503 }
504 /* XXX Warn if (supplied_flags & PyCF_MASK_OBSOLETE) != 0? */
505
506 if (!dont_inherit) {
507 PyEval_MergeCompilerFlags(&cf);
508 }
509
Georg Brandlf2bfd542008-03-29 13:24:23 +0000510 if (strcmp(startstr, "exec") == 0)
511 mode = 0;
512 else if (strcmp(startstr, "eval") == 0)
513 mode = 1;
514 else if (strcmp(startstr, "single") == 0)
515 mode = 2;
516 else {
517 PyErr_SetString(PyExc_ValueError,
518 "compile() arg 3 must be 'exec', 'eval' or 'single'");
519 return NULL;
520 }
521
Georg Brandlfc8eef32008-03-28 12:11:56 +0000522 if (PyAST_Check(cmd)) {
523 if (supplied_flags & PyCF_ONLY_AST) {
524 Py_INCREF(cmd);
525 result = cmd;
526 }
527 else {
528 PyArena *arena;
529 mod_ty mod;
530
531 arena = PyArena_New();
Georg Brandlf2bfd542008-03-29 13:24:23 +0000532 mod = PyAST_obj2mod(cmd, arena, mode);
Georg Brandlfc8eef32008-03-28 12:11:56 +0000533 if (mod == NULL) {
534 PyArena_Free(arena);
535 return NULL;
536 }
537 result = (PyObject*)PyAST_Compile(mod, filename,
538 &cf, arena);
539 PyArena_Free(arena);
540 }
541 return result;
542 }
543
Just van Rossum3aaf42c2003-02-10 08:21:10 +0000544#ifdef Py_USING_UNICODE
545 if (PyUnicode_Check(cmd)) {
546 tmp = PyUnicode_AsUTF8String(cmd);
547 if (tmp == NULL)
548 return NULL;
549 cmd = tmp;
550 cf.cf_flags |= PyCF_SOURCE_IS_UTF8;
551 }
552#endif
Tim Peters6cd6a822001-08-17 22:11:27 +0000553
Georg Brandlfc8eef32008-03-28 12:11:56 +0000554 if (PyObject_AsReadBuffer(cmd, (const void **)&str, &length))
Neal Norwitz3a9a3e72005-11-27 20:38:31 +0000555 goto cleanup;
Georg Brandlfc8eef32008-03-28 12:11:56 +0000556 if ((size_t)length != strlen(str)) {
557 PyErr_SetString(PyExc_TypeError,
558 "compile() expected string without null bytes");
559 goto cleanup;
Tim Peters6cd6a822001-08-17 22:11:27 +0000560 }
Georg Brandlf2bfd542008-03-29 13:24:23 +0000561 result = Py_CompileStringFlags(str, filename, start[mode], &cf);
Neal Norwitz3a9a3e72005-11-27 20:38:31 +0000562cleanup:
Just van Rossum3aaf42c2003-02-10 08:21:10 +0000563 Py_XDECREF(tmp);
564 return result;
Guido van Rossum5b722181993-03-30 17:46:03 +0000565}
566
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000567PyDoc_STRVAR(compile_doc,
Tim Peters6cd6a822001-08-17 22:11:27 +0000568"compile(source, filename, mode[, flags[, dont_inherit]]) -> code object\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000569\n\
570Compile the source string (a Python module, statement or expression)\n\
571into a code object that can be executed by the exec statement or eval().\n\
572The filename will be used for run-time error messages.\n\
573The mode must be 'exec' to compile a module, 'single' to compile a\n\
Tim Peters6cd6a822001-08-17 22:11:27 +0000574single (interactive) statement, or 'eval' to compile an expression.\n\
575The flags argument, if present, controls which future statements influence\n\
576the compilation of the code.\n\
577The dont_inherit argument, if non-zero, stops the compilation inheriting\n\
578the effects of any future statements in effect in the code calling\n\
579compile; if absent or zero these statements do influence the compilation,\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000580in addition to any features explicitly specified.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000581
Guido van Rossum79f25d91997-04-29 20:08:16 +0000582static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000583builtin_dir(PyObject *self, PyObject *args)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000584{
Tim Peters5d2b77c2001-09-03 05:47:38 +0000585 PyObject *arg = NULL;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000586
Raymond Hettingerea3fdf42002-12-29 16:33:45 +0000587 if (!PyArg_UnpackTuple(args, "dir", 0, 1, &arg))
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000588 return NULL;
Tim Peters7eea37e2001-09-04 22:08:56 +0000589 return PyObject_Dir(arg);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000590}
591
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000592PyDoc_STRVAR(dir_doc,
Tim Peters5d2b77c2001-09-03 05:47:38 +0000593"dir([object]) -> list of strings\n"
594"\n"
Georg Brandl871f1bc2007-03-12 13:17:36 +0000595"If called without an argument, return the names in the current scope.\n"
596"Else, return an alphabetized list of names comprising (some of) the attributes\n"
597"of the given object, and of attributes reachable from it.\n"
598"If the object supplies a method named __dir__, it will be used; otherwise\n"
599"the default dir() logic is used and returns:\n"
600" for a module object: the module's attributes.\n"
601" for a class object: its attributes, and recursively the attributes\n"
602" of its bases.\n"
Georg Brandl3bb15672007-03-13 07:23:16 +0000603" for any other object: its attributes, its class's attributes, and\n"
Georg Brandl871f1bc2007-03-12 13:17:36 +0000604" recursively the attributes of its class's base classes.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000605
Guido van Rossum79f25d91997-04-29 20:08:16 +0000606static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000607builtin_divmod(PyObject *self, PyObject *args)
Guido van Rossum6a00cd81995-01-07 12:39:01 +0000608{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000609 PyObject *v, *w;
Guido van Rossum6a00cd81995-01-07 12:39:01 +0000610
Raymond Hettingerea3fdf42002-12-29 16:33:45 +0000611 if (!PyArg_UnpackTuple(args, "divmod", 2, 2, &v, &w))
Guido van Rossum6a00cd81995-01-07 12:39:01 +0000612 return NULL;
Guido van Rossum09df08a1998-05-22 00:51:39 +0000613 return PyNumber_Divmod(v, w);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000614}
615
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000616PyDoc_STRVAR(divmod_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000617"divmod(x, y) -> (div, mod)\n\
618\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000619Return the tuple ((x-x%y)/y, x%y). Invariant: div*y + mod == x.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000620
621
Guido van Rossum79f25d91997-04-29 20:08:16 +0000622static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000623builtin_eval(PyObject *self, PyObject *args)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000624{
Just van Rossum3aaf42c2003-02-10 08:21:10 +0000625 PyObject *cmd, *result, *tmp = NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000626 PyObject *globals = Py_None, *locals = Py_None;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000627 char *str;
Tim Peters9fa96be2001-08-17 23:04:59 +0000628 PyCompilerFlags cf;
Guido van Rossum590baa41993-11-30 13:40:46 +0000629
Raymond Hettinger214b1c32004-07-02 06:41:07 +0000630 if (!PyArg_UnpackTuple(args, "eval", 1, 3, &cmd, &globals, &locals))
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000631 return NULL;
Raymond Hettinger214b1c32004-07-02 06:41:07 +0000632 if (locals != Py_None && !PyMapping_Check(locals)) {
633 PyErr_SetString(PyExc_TypeError, "locals must be a mapping");
Raymond Hettinger513ffe82004-07-06 13:44:41 +0000634 return NULL;
Raymond Hettinger214b1c32004-07-02 06:41:07 +0000635 }
636 if (globals != Py_None && !PyDict_Check(globals)) {
Georg Brandl99d7e4e2005-08-31 22:21:15 +0000637 PyErr_SetString(PyExc_TypeError, PyMapping_Check(globals) ?
Raymond Hettinger214b1c32004-07-02 06:41:07 +0000638 "globals must be a real dict; try eval(expr, {}, mapping)"
639 : "globals must be a dict");
Raymond Hettinger513ffe82004-07-06 13:44:41 +0000640 return NULL;
Raymond Hettinger214b1c32004-07-02 06:41:07 +0000641 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000642 if (globals == Py_None) {
643 globals = PyEval_GetGlobals();
644 if (locals == Py_None)
645 locals = PyEval_GetLocals();
Guido van Rossum6135a871995-01-09 17:53:26 +0000646 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000647 else if (locals == Py_None)
Guido van Rossum6135a871995-01-09 17:53:26 +0000648 locals = globals;
Tim Peters9fa96be2001-08-17 23:04:59 +0000649
Georg Brandl77c85e62005-09-15 10:46:13 +0000650 if (globals == NULL || locals == NULL) {
651 PyErr_SetString(PyExc_TypeError,
652 "eval must be given globals and locals "
653 "when called without a frame");
654 return NULL;
655 }
656
Guido van Rossum79f25d91997-04-29 20:08:16 +0000657 if (PyDict_GetItemString(globals, "__builtins__") == NULL) {
658 if (PyDict_SetItemString(globals, "__builtins__",
659 PyEval_GetBuiltins()) != 0)
Guido van Rossum6135a871995-01-09 17:53:26 +0000660 return NULL;
661 }
Tim Peters9fa96be2001-08-17 23:04:59 +0000662
Jeremy Hylton15c1c4f2001-07-30 21:50:55 +0000663 if (PyCode_Check(cmd)) {
Jeremy Hylton733c8932001-12-13 19:51:56 +0000664 if (PyCode_GetNumFree((PyCodeObject *)cmd) > 0) {
Jeremy Hylton15c1c4f2001-07-30 21:50:55 +0000665 PyErr_SetString(PyExc_TypeError,
666 "code object passed to eval() may not contain free variables");
667 return NULL;
668 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000669 return PyEval_EvalCode((PyCodeObject *) cmd, globals, locals);
Jeremy Hylton15c1c4f2001-07-30 21:50:55 +0000670 }
Tim Peters9fa96be2001-08-17 23:04:59 +0000671
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000672 if (!PyString_Check(cmd) &&
Marc-André Lemburgd1ba4432000-09-19 21:04:18 +0000673 !PyUnicode_Check(cmd)) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000674 PyErr_SetString(PyExc_TypeError,
Fred Drake661ea262000-10-24 19:57:45 +0000675 "eval() arg 1 must be a string or code object");
Guido van Rossum94390a41992-08-14 15:14:30 +0000676 return NULL;
677 }
Just van Rossum3aaf42c2003-02-10 08:21:10 +0000678 cf.cf_flags = 0;
679
680#ifdef Py_USING_UNICODE
681 if (PyUnicode_Check(cmd)) {
682 tmp = PyUnicode_AsUTF8String(cmd);
683 if (tmp == NULL)
684 return NULL;
685 cmd = tmp;
686 cf.cf_flags |= PyCF_SOURCE_IS_UTF8;
687 }
688#endif
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000689 if (PyString_AsStringAndSize(cmd, &str, NULL)) {
Neal Norwitz3a9a3e72005-11-27 20:38:31 +0000690 Py_XDECREF(tmp);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000691 return NULL;
Neal Norwitz3a9a3e72005-11-27 20:38:31 +0000692 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000693 while (*str == ' ' || *str == '\t')
694 str++;
Tim Peters9fa96be2001-08-17 23:04:59 +0000695
Tim Peters9fa96be2001-08-17 23:04:59 +0000696 (void)PyEval_MergeCompilerFlags(&cf);
Just van Rossum3aaf42c2003-02-10 08:21:10 +0000697 result = PyRun_StringFlags(str, Py_eval_input, globals, locals, &cf);
698 Py_XDECREF(tmp);
699 return result;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000700}
701
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000702PyDoc_STRVAR(eval_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000703"eval(source[, globals[, locals]]) -> value\n\
704\n\
705Evaluate the source in the context of globals and locals.\n\
706The source may be a string representing a Python expression\n\
707or a code object as returned by compile().\n\
Neal Norwitz477ca1c2006-09-05 02:25:41 +0000708The globals must be a dictionary and locals can be any mapping,\n\
Raymond Hettinger214b1c32004-07-02 06:41:07 +0000709defaulting to the current globals and locals.\n\
710If only globals is given, locals defaults to it.\n");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000711
712
Guido van Rossum79f25d91997-04-29 20:08:16 +0000713static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000714builtin_execfile(PyObject *self, PyObject *args)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000715{
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000716 char *filename;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000717 PyObject *globals = Py_None, *locals = Py_None;
718 PyObject *res;
Guido van Rossumb3a639e2001-09-05 13:37:47 +0000719 FILE* fp = NULL;
Tim Peters5ba58662001-07-16 02:29:45 +0000720 PyCompilerFlags cf;
Martin v. Löwis6b3a2c42001-08-08 05:30:36 +0000721 int exists;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000722
Benjamin Peterson9f4f4812008-04-27 03:01:45 +0000723 if (PyErr_WarnPy3k("execfile() not supported in 3.x; use exec()",
Benjamin Petersonf19a7b92008-04-27 18:40:21 +0000724 1) < 0)
Neal Norwitzdf25efe2007-05-23 06:58:36 +0000725 return NULL;
726
Raymond Hettinger66bd2332004-08-02 08:30:07 +0000727 if (!PyArg_ParseTuple(args, "s|O!O:execfile",
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000728 &filename,
Guido van Rossum79f25d91997-04-29 20:08:16 +0000729 &PyDict_Type, &globals,
Raymond Hettinger66bd2332004-08-02 08:30:07 +0000730 &locals))
Guido van Rossum0f61f8a1992-02-25 18:55:05 +0000731 return NULL;
Raymond Hettinger66bd2332004-08-02 08:30:07 +0000732 if (locals != Py_None && !PyMapping_Check(locals)) {
733 PyErr_SetString(PyExc_TypeError, "locals must be a mapping");
734 return NULL;
735 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000736 if (globals == Py_None) {
737 globals = PyEval_GetGlobals();
738 if (locals == Py_None)
739 locals = PyEval_GetLocals();
Guido van Rossum6135a871995-01-09 17:53:26 +0000740 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000741 else if (locals == Py_None)
Guido van Rossum6135a871995-01-09 17:53:26 +0000742 locals = globals;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000743 if (PyDict_GetItemString(globals, "__builtins__") == NULL) {
744 if (PyDict_SetItemString(globals, "__builtins__",
745 PyEval_GetBuiltins()) != 0)
Guido van Rossum6135a871995-01-09 17:53:26 +0000746 return NULL;
747 }
Martin v. Löwis6b3a2c42001-08-08 05:30:36 +0000748
749 exists = 0;
750 /* Test for existence or directory. */
Martin v. Löwis3484a182002-03-09 12:07:51 +0000751#if defined(PLAN9)
752 {
753 Dir *d;
754
755 if ((d = dirstat(filename))!=nil) {
756 if(d->mode & DMDIR)
757 werrstr("is a directory");
758 else
759 exists = 1;
760 free(d);
761 }
Martin v. Löwis6b3a2c42001-08-08 05:30:36 +0000762 }
Martin v. Löwis3484a182002-03-09 12:07:51 +0000763#elif defined(RISCOS)
Guido van Rossume2ae77b2001-10-24 20:42:55 +0000764 if (object_exists(filename)) {
765 if (isdir(filename))
766 errno = EISDIR;
767 else
768 exists = 1;
769 }
Martin v. Löwis3484a182002-03-09 12:07:51 +0000770#else /* standard Posix */
771 {
772 struct stat s;
773 if (stat(filename, &s) == 0) {
774 if (S_ISDIR(s.st_mode))
Andrew MacIntyreda4d6cb2004-03-29 11:53:38 +0000775# if defined(PYOS_OS2) && defined(PYCC_VACPP)
Martin v. Löwis3484a182002-03-09 12:07:51 +0000776 errno = EOS2ERR;
777# else
778 errno = EISDIR;
779# endif
780 else
781 exists = 1;
782 }
783 }
784#endif
Martin v. Löwis6b3a2c42001-08-08 05:30:36 +0000785
786 if (exists) {
787 Py_BEGIN_ALLOW_THREADS
Jack Jansen7b8c7542002-04-14 20:12:41 +0000788 fp = fopen(filename, "r" PY_STDIOTEXTMODE);
Martin v. Löwis6b3a2c42001-08-08 05:30:36 +0000789 Py_END_ALLOW_THREADS
790
791 if (fp == NULL) {
792 exists = 0;
793 }
794 }
795
796 if (!exists) {
Peter Schneider-Kamp4c013422002-08-27 16:58:00 +0000797 PyErr_SetFromErrnoWithFilename(PyExc_IOError, filename);
Guido van Rossum0f61f8a1992-02-25 18:55:05 +0000798 return NULL;
799 }
Tim Peters5ba58662001-07-16 02:29:45 +0000800 cf.cf_flags = 0;
801 if (PyEval_MergeCompilerFlags(&cf))
Tim Peters748b8bb2001-04-28 08:20:22 +0000802 res = PyRun_FileExFlags(fp, filename, Py_file_input, globals,
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000803 locals, 1, &cf);
Tim Peters5ba58662001-07-16 02:29:45 +0000804 else
Tim Peters748b8bb2001-04-28 08:20:22 +0000805 res = PyRun_FileEx(fp, filename, Py_file_input, globals,
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000806 locals, 1);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000807 return res;
Guido van Rossum0f61f8a1992-02-25 18:55:05 +0000808}
809
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000810PyDoc_STRVAR(execfile_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000811"execfile(filename[, globals[, locals]])\n\
812\n\
813Read and execute a Python script from a file.\n\
814The globals and locals are dictionaries, defaulting to the current\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000815globals and locals. If only globals is given, locals defaults to it.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000816
817
Guido van Rossum79f25d91997-04-29 20:08:16 +0000818static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000819builtin_getattr(PyObject *self, PyObject *args)
Guido van Rossum33894be1992-01-27 16:53:09 +0000820{
Guido van Rossum950ff291998-06-29 13:38:57 +0000821 PyObject *v, *result, *dflt = NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000822 PyObject *name;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000823
Raymond Hettingerea3fdf42002-12-29 16:33:45 +0000824 if (!PyArg_UnpackTuple(args, "getattr", 2, 3, &v, &name, &dflt))
Guido van Rossum33894be1992-01-27 16:53:09 +0000825 return NULL;
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000826#ifdef Py_USING_UNICODE
Jeremy Hylton0eb11152001-07-30 22:39:31 +0000827 if (PyUnicode_Check(name)) {
828 name = _PyUnicode_AsDefaultEncodedString(name, NULL);
829 if (name == NULL)
830 return NULL;
831 }
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000832#endif
Jeremy Hylton0eb11152001-07-30 22:39:31 +0000833
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000834 if (!PyString_Check(name)) {
Jeremy Hylton0eb11152001-07-30 22:39:31 +0000835 PyErr_SetString(PyExc_TypeError,
Alex Martellia9b9c9f2003-04-23 13:34:35 +0000836 "getattr(): attribute name must be string");
Jeremy Hylton0eb11152001-07-30 22:39:31 +0000837 return NULL;
838 }
Guido van Rossum950ff291998-06-29 13:38:57 +0000839 result = PyObject_GetAttr(v, name);
Guido van Rossumd8923572001-10-16 21:31:32 +0000840 if (result == NULL && dflt != NULL &&
841 PyErr_ExceptionMatches(PyExc_AttributeError))
842 {
Guido van Rossum950ff291998-06-29 13:38:57 +0000843 PyErr_Clear();
844 Py_INCREF(dflt);
845 result = dflt;
846 }
847 return result;
Guido van Rossum9bfef441993-03-29 10:43:31 +0000848}
849
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000850PyDoc_STRVAR(getattr_doc,
Guido van Rossum950ff291998-06-29 13:38:57 +0000851"getattr(object, name[, default]) -> value\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000852\n\
Guido van Rossum950ff291998-06-29 13:38:57 +0000853Get a named attribute from an object; getattr(x, 'y') is equivalent to x.y.\n\
854When a default argument is given, it is returned when the attribute doesn't\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000855exist; without it, an exception is raised in that case.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000856
857
Guido van Rossum79f25d91997-04-29 20:08:16 +0000858static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000859builtin_globals(PyObject *self)
Guido van Rossum872537c1995-07-07 22:43:42 +0000860{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000861 PyObject *d;
Guido van Rossum872537c1995-07-07 22:43:42 +0000862
Guido van Rossum79f25d91997-04-29 20:08:16 +0000863 d = PyEval_GetGlobals();
Neal Norwitz43bd4db2006-08-12 01:46:42 +0000864 Py_XINCREF(d);
Guido van Rossum872537c1995-07-07 22:43:42 +0000865 return d;
866}
867
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000868PyDoc_STRVAR(globals_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000869"globals() -> dictionary\n\
870\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000871Return the dictionary containing the current scope's global variables.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000872
873
Guido van Rossum79f25d91997-04-29 20:08:16 +0000874static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000875builtin_hasattr(PyObject *self, PyObject *args)
Guido van Rossum9bfef441993-03-29 10:43:31 +0000876{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000877 PyObject *v;
878 PyObject *name;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000879
Raymond Hettingerea3fdf42002-12-29 16:33:45 +0000880 if (!PyArg_UnpackTuple(args, "hasattr", 2, 2, &v, &name))
Guido van Rossum9bfef441993-03-29 10:43:31 +0000881 return NULL;
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000882#ifdef Py_USING_UNICODE
Jeremy Hylton302b54a2001-07-30 22:45:19 +0000883 if (PyUnicode_Check(name)) {
884 name = _PyUnicode_AsDefaultEncodedString(name, NULL);
885 if (name == NULL)
886 return NULL;
887 }
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000888#endif
Jeremy Hylton302b54a2001-07-30 22:45:19 +0000889
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000890 if (!PyString_Check(name)) {
Jeremy Hylton302b54a2001-07-30 22:45:19 +0000891 PyErr_SetString(PyExc_TypeError,
Alex Martellia9b9c9f2003-04-23 13:34:35 +0000892 "hasattr(): attribute name must be string");
Jeremy Hylton302b54a2001-07-30 22:45:19 +0000893 return NULL;
894 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000895 v = PyObject_GetAttr(v, name);
Guido van Rossum9bfef441993-03-29 10:43:31 +0000896 if (v == NULL) {
Benjamin Petersonb9030f42008-05-12 00:41:23 +0000897 if (!PyErr_ExceptionMatches(PyExc_Exception))
898 return NULL;
899 else {
900 PyErr_Clear();
901 Py_INCREF(Py_False);
902 return Py_False;
903 }
Guido van Rossum9bfef441993-03-29 10:43:31 +0000904 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000905 Py_DECREF(v);
Guido van Rossum09df08a1998-05-22 00:51:39 +0000906 Py_INCREF(Py_True);
907 return Py_True;
Guido van Rossum33894be1992-01-27 16:53:09 +0000908}
909
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000910PyDoc_STRVAR(hasattr_doc,
Guido van Rossum77f6a652002-04-03 22:41:51 +0000911"hasattr(object, name) -> bool\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000912\n\
913Return whether the object has an attribute with the given name.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000914(This is done by calling getattr(object, name) and catching exceptions.)");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000915
916
Guido van Rossum79f25d91997-04-29 20:08:16 +0000917static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000918builtin_id(PyObject *self, PyObject *v)
Guido van Rossum5b722181993-03-30 17:46:03 +0000919{
Guido van Rossum106f2da2000-06-28 21:12:25 +0000920 return PyLong_FromVoidPtr(v);
Guido van Rossum5b722181993-03-30 17:46:03 +0000921}
922
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000923PyDoc_STRVAR(id_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000924"id(object) -> integer\n\
925\n\
926Return the identity of an object. This is guaranteed to be unique among\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000927simultaneously existing objects. (Hint: it's the object's memory address.)");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000928
929
Guido van Rossum79f25d91997-04-29 20:08:16 +0000930static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000931builtin_map(PyObject *self, PyObject *args)
Guido van Rossum12d12c51993-10-26 17:58:25 +0000932{
933 typedef struct {
Tim Peters4e9afdc2001-05-03 23:54:49 +0000934 PyObject *it; /* the iterator object */
935 int saw_StopIteration; /* bool: did the iterator end? */
Guido van Rossum12d12c51993-10-26 17:58:25 +0000936 } sequence;
937
Guido van Rossum79f25d91997-04-29 20:08:16 +0000938 PyObject *func, *result;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000939 sequence *seqs = NULL, *sqp;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000940 Py_ssize_t n, len;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000941 register int i, j;
942
Guido van Rossum79f25d91997-04-29 20:08:16 +0000943 n = PyTuple_Size(args);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000944 if (n < 2) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000945 PyErr_SetString(PyExc_TypeError,
946 "map() requires at least two args");
Guido van Rossum12d12c51993-10-26 17:58:25 +0000947 return NULL;
948 }
949
Guido van Rossum79f25d91997-04-29 20:08:16 +0000950 func = PyTuple_GetItem(args, 0);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000951 n--;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000952
Neal Norwitz53152a12008-02-24 02:20:25 +0000953 if (func == Py_None) {
Benjamin Peterson9f4f4812008-04-27 03:01:45 +0000954 if (PyErr_WarnPy3k("map(None, ...) not supported in 3.x; "
Benjamin Petersonf19a7b92008-04-27 18:40:21 +0000955 "use list(...)", 1) < 0)
Neal Norwitz53152a12008-02-24 02:20:25 +0000956 return NULL;
957 if (n == 1) {
958 /* map(None, S) is the same as list(S). */
959 return PySequence_List(PyTuple_GetItem(args, 1));
960 }
Guido van Rossumfa4ac711998-07-10 17:37:30 +0000961 }
962
Tim Peters4e9afdc2001-05-03 23:54:49 +0000963 /* Get space for sequence descriptors. Must NULL out the iterator
964 * pointers so that jumping to Fail_2 later doesn't see trash.
965 */
Guido van Rossum79f25d91997-04-29 20:08:16 +0000966 if ((seqs = PyMem_NEW(sequence, n)) == NULL) {
967 PyErr_NoMemory();
Tim Peters4e9afdc2001-05-03 23:54:49 +0000968 return NULL;
969 }
970 for (i = 0; i < n; ++i) {
971 seqs[i].it = (PyObject*)NULL;
972 seqs[i].saw_StopIteration = 0;
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000973 }
Guido van Rossum12d12c51993-10-26 17:58:25 +0000974
Tim Peters4e9afdc2001-05-03 23:54:49 +0000975 /* Do a first pass to obtain iterators for the arguments, and set len
976 * to the largest of their lengths.
Tim Peters748b8bb2001-04-28 08:20:22 +0000977 */
Tim Peters4e9afdc2001-05-03 23:54:49 +0000978 len = 0;
979 for (i = 0, sqp = seqs; i < n; ++i, ++sqp) {
980 PyObject *curseq;
Martin v. Löwisd96ee902006-02-16 14:37:16 +0000981 Py_ssize_t curlen;
Guido van Rossumad991772001-01-12 16:03:05 +0000982
Tim Peters4e9afdc2001-05-03 23:54:49 +0000983 /* Get iterator. */
984 curseq = PyTuple_GetItem(args, i+1);
985 sqp->it = PyObject_GetIter(curseq);
986 if (sqp->it == NULL) {
Guido van Rossum12d12c51993-10-26 17:58:25 +0000987 static char errmsg[] =
Tim Peters4e9afdc2001-05-03 23:54:49 +0000988 "argument %d to map() must support iteration";
Guido van Rossum15e33a41997-04-30 19:00:27 +0000989 char errbuf[sizeof(errmsg) + 25];
Jeremy Hylton518ab1c2001-11-28 20:42:20 +0000990 PyOS_snprintf(errbuf, sizeof(errbuf), errmsg, i+2);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000991 PyErr_SetString(PyExc_TypeError, errbuf);
Guido van Rossum12d12c51993-10-26 17:58:25 +0000992 goto Fail_2;
993 }
994
Tim Peters4e9afdc2001-05-03 23:54:49 +0000995 /* Update len. */
Raymond Hettinger4e2f7142007-12-06 00:56:53 +0000996 curlen = _PyObject_LengthHint(curseq, 8);
Raymond Hettinger77f3c872004-01-04 08:54:44 +0000997 if (curlen > len)
998 len = curlen;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000999 }
1000
Tim Peters4e9afdc2001-05-03 23:54:49 +00001001 /* Get space for the result list. */
Guido van Rossum79f25d91997-04-29 20:08:16 +00001002 if ((result = (PyObject *) PyList_New(len)) == NULL)
Guido van Rossum12d12c51993-10-26 17:58:25 +00001003 goto Fail_2;
1004
Tim Peters4e9afdc2001-05-03 23:54:49 +00001005 /* Iterate over the sequences until all have stopped. */
Guido van Rossum2d951851994-08-29 12:52:16 +00001006 for (i = 0; ; ++i) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001007 PyObject *alist, *item=NULL, *value;
Tim Peters4e9afdc2001-05-03 23:54:49 +00001008 int numactive = 0;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001009
Guido van Rossum79f25d91997-04-29 20:08:16 +00001010 if (func == Py_None && n == 1)
Guido van Rossum32120311995-07-10 13:52:21 +00001011 alist = NULL;
Tim Peters4e9afdc2001-05-03 23:54:49 +00001012 else if ((alist = PyTuple_New(n)) == NULL)
1013 goto Fail_1;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001014
1015 for (j = 0, sqp = seqs; j < n; ++j, ++sqp) {
Tim Peters4e9afdc2001-05-03 23:54:49 +00001016 if (sqp->saw_StopIteration) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001017 Py_INCREF(Py_None);
1018 item = Py_None;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001019 }
Guido van Rossum12d12c51993-10-26 17:58:25 +00001020 else {
Tim Peters4e9afdc2001-05-03 23:54:49 +00001021 item = PyIter_Next(sqp->it);
1022 if (item)
1023 ++numactive;
1024 else {
Tim Peters4e9afdc2001-05-03 23:54:49 +00001025 if (PyErr_Occurred()) {
Tim Petersf4848da2001-05-05 00:14:56 +00001026 Py_XDECREF(alist);
1027 goto Fail_1;
Guido van Rossum2d951851994-08-29 12:52:16 +00001028 }
Tim Peters4e9afdc2001-05-03 23:54:49 +00001029 Py_INCREF(Py_None);
1030 item = Py_None;
1031 sqp->saw_StopIteration = 1;
Guido van Rossum2d951851994-08-29 12:52:16 +00001032 }
Guido van Rossum12d12c51993-10-26 17:58:25 +00001033 }
Tim Peters4e9afdc2001-05-03 23:54:49 +00001034 if (alist)
1035 PyTuple_SET_ITEM(alist, j, item);
1036 else
Guido van Rossum2d951851994-08-29 12:52:16 +00001037 break;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001038 }
1039
Guido van Rossum32120311995-07-10 13:52:21 +00001040 if (!alist)
1041 alist = item;
Guido van Rossum2d951851994-08-29 12:52:16 +00001042
Tim Peters4e9afdc2001-05-03 23:54:49 +00001043 if (numactive == 0) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001044 Py_DECREF(alist);
Guido van Rossum2d951851994-08-29 12:52:16 +00001045 break;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001046 }
Guido van Rossum2d951851994-08-29 12:52:16 +00001047
Guido van Rossum79f25d91997-04-29 20:08:16 +00001048 if (func == Py_None)
Guido van Rossum32120311995-07-10 13:52:21 +00001049 value = alist;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001050 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001051 value = PyEval_CallObject(func, alist);
1052 Py_DECREF(alist);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00001053 if (value == NULL)
1054 goto Fail_1;
Guido van Rossum2d951851994-08-29 12:52:16 +00001055 }
1056 if (i >= len) {
Barry Warsawfa77e091999-01-28 18:49:12 +00001057 int status = PyList_Append(result, value);
Barry Warsaw21332871999-01-28 04:21:35 +00001058 Py_DECREF(value);
Barry Warsawfa77e091999-01-28 18:49:12 +00001059 if (status < 0)
1060 goto Fail_1;
Guido van Rossum2d951851994-08-29 12:52:16 +00001061 }
Tim Peters4e9afdc2001-05-03 23:54:49 +00001062 else if (PyList_SetItem(result, i, value) < 0)
1063 goto Fail_1;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001064 }
1065
Guido van Rossumfa4ac711998-07-10 17:37:30 +00001066 if (i < len && PyList_SetSlice(result, i, len, NULL) < 0)
1067 goto Fail_1;
1068
Tim Peters4e9afdc2001-05-03 23:54:49 +00001069 goto Succeed;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001070
Guido van Rossum12d12c51993-10-26 17:58:25 +00001071Fail_1:
Guido van Rossum79f25d91997-04-29 20:08:16 +00001072 Py_DECREF(result);
Guido van Rossum12d12c51993-10-26 17:58:25 +00001073Fail_2:
Tim Peters4e9afdc2001-05-03 23:54:49 +00001074 result = NULL;
1075Succeed:
1076 assert(seqs);
1077 for (i = 0; i < n; ++i)
1078 Py_XDECREF(seqs[i].it);
1079 PyMem_DEL(seqs);
1080 return result;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001081}
1082
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001083PyDoc_STRVAR(map_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001084"map(function, sequence[, sequence, ...]) -> list\n\
1085\n\
1086Return a list of the results of applying the function to the items of\n\
1087the argument sequence(s). If more than one sequence is given, the\n\
1088function is called with an argument list consisting of the corresponding\n\
1089item of each sequence, substituting None for missing values when not all\n\
1090sequences have the same length. If the function is None, return a list of\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001091the items of the sequence (or a list of tuples if more than one sequence).");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001092
1093
Guido van Rossum79f25d91997-04-29 20:08:16 +00001094static PyObject *
Georg Brandl28e08732008-04-30 19:47:09 +00001095builtin_next(PyObject *self, PyObject *args)
1096{
1097 PyObject *it, *res;
1098 PyObject *def = NULL;
1099
1100 if (!PyArg_UnpackTuple(args, "next", 1, 2, &it, &def))
1101 return NULL;
1102 if (!PyIter_Check(it)) {
1103 PyErr_Format(PyExc_TypeError,
1104 "%.200s object is not an iterator",
1105 it->ob_type->tp_name);
1106 return NULL;
1107 }
1108
1109 res = (*it->ob_type->tp_iternext)(it);
1110 if (res != NULL) {
1111 return res;
1112 } else if (def != NULL) {
1113 if (PyErr_Occurred()) {
1114 if (!PyErr_ExceptionMatches(PyExc_StopIteration))
1115 return NULL;
1116 PyErr_Clear();
1117 }
1118 Py_INCREF(def);
1119 return def;
1120 } else if (PyErr_Occurred()) {
1121 return NULL;
1122 } else {
1123 PyErr_SetNone(PyExc_StopIteration);
1124 return NULL;
1125 }
1126}
1127
1128PyDoc_STRVAR(next_doc,
1129"next(iterator[, default])\n\
1130\n\
1131Return the next item from the iterator. If default is given and the iterator\n\
1132is exhausted, it is returned instead of raising StopIteration.");
1133
1134
1135static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001136builtin_setattr(PyObject *self, PyObject *args)
Guido van Rossum33894be1992-01-27 16:53:09 +00001137{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001138 PyObject *v;
1139 PyObject *name;
1140 PyObject *value;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001141
Raymond Hettingerea3fdf42002-12-29 16:33:45 +00001142 if (!PyArg_UnpackTuple(args, "setattr", 3, 3, &v, &name, &value))
Guido van Rossum33894be1992-01-27 16:53:09 +00001143 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001144 if (PyObject_SetAttr(v, name, value) != 0)
Guido van Rossum33894be1992-01-27 16:53:09 +00001145 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001146 Py_INCREF(Py_None);
1147 return Py_None;
Guido van Rossum33894be1992-01-27 16:53:09 +00001148}
1149
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001150PyDoc_STRVAR(setattr_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001151"setattr(object, name, value)\n\
1152\n\
1153Set a named attribute on an object; setattr(x, 'y', v) is equivalent to\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001154``x.y = v''.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001155
1156
Guido van Rossum79f25d91997-04-29 20:08:16 +00001157static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001158builtin_delattr(PyObject *self, PyObject *args)
Guido van Rossum14144fc1994-08-29 12:53:40 +00001159{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001160 PyObject *v;
1161 PyObject *name;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001162
Raymond Hettingerea3fdf42002-12-29 16:33:45 +00001163 if (!PyArg_UnpackTuple(args, "delattr", 2, 2, &v, &name))
Guido van Rossum14144fc1994-08-29 12:53:40 +00001164 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001165 if (PyObject_SetAttr(v, name, (PyObject *)NULL) != 0)
Guido van Rossum14144fc1994-08-29 12:53:40 +00001166 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001167 Py_INCREF(Py_None);
1168 return Py_None;
Guido van Rossum14144fc1994-08-29 12:53:40 +00001169}
1170
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001171PyDoc_STRVAR(delattr_doc,
Guido van Rossumdf12a591998-11-23 22:13:04 +00001172"delattr(object, name)\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001173\n\
1174Delete a named attribute on an object; delattr(x, 'y') is equivalent to\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001175``del x.y''.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001176
1177
Guido van Rossum79f25d91997-04-29 20:08:16 +00001178static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001179builtin_hash(PyObject *self, PyObject *v)
Guido van Rossum9bfef441993-03-29 10:43:31 +00001180{
Guido van Rossum9bfef441993-03-29 10:43:31 +00001181 long x;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001182
Guido van Rossum79f25d91997-04-29 20:08:16 +00001183 x = PyObject_Hash(v);
Guido van Rossum9bfef441993-03-29 10:43:31 +00001184 if (x == -1)
1185 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001186 return PyInt_FromLong(x);
Guido van Rossum9bfef441993-03-29 10:43:31 +00001187}
1188
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001189PyDoc_STRVAR(hash_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001190"hash(object) -> integer\n\
1191\n\
1192Return a hash value for the object. Two objects with the same value have\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001193the same hash value. The reverse is not necessarily true, but likely.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001194
1195
Guido van Rossum79f25d91997-04-29 20:08:16 +00001196static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001197builtin_hex(PyObject *self, PyObject *v)
Guido van Rossum006bcd41991-10-24 14:54:44 +00001198{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001199 PyNumberMethods *nb;
Neil Schemenauer3a313e32004-07-19 16:29:17 +00001200 PyObject *res;
Benjamin Petersonc6d64ec2008-05-17 20:09:42 +00001201
1202 if ((nb = v->ob_type->tp_as_number) == NULL ||
1203 nb->nb_hex == NULL) {
1204 PyErr_SetString(PyExc_TypeError,
1205 "hex() argument can't be converted to hex");
1206 return NULL;
Guido van Rossum006bcd41991-10-24 14:54:44 +00001207 }
Benjamin Petersonc6d64ec2008-05-17 20:09:42 +00001208 res = (*nb->nb_hex)(v);
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001209 if (res && !PyString_Check(res)) {
Benjamin Petersonc6d64ec2008-05-17 20:09:42 +00001210 PyErr_Format(PyExc_TypeError,
1211 "__hex__ returned non-string (type %.200s)",
1212 res->ob_type->tp_name);
1213 Py_DECREF(res);
1214 return NULL;
1215 }
1216 return res;
Guido van Rossum006bcd41991-10-24 14:54:44 +00001217}
1218
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001219PyDoc_STRVAR(hex_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001220"hex(number) -> string\n\
1221\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001222Return the hexadecimal representation of an integer or long integer.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001223
1224
Tim Petersdbd9ba62000-07-09 03:09:57 +00001225static PyObject *builtin_raw_input(PyObject *, PyObject *);
Guido van Rossum3165fe61992-09-25 21:59:05 +00001226
Guido van Rossum79f25d91997-04-29 20:08:16 +00001227static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001228builtin_input(PyObject *self, PyObject *args)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001229{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001230 PyObject *line;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001231 char *str;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001232 PyObject *res;
1233 PyObject *globals, *locals;
Hye-Shik Changff83c2b2004-02-02 13:39:01 +00001234 PyCompilerFlags cf;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001235
1236 line = builtin_raw_input(self, args);
Guido van Rossum3165fe61992-09-25 21:59:05 +00001237 if (line == NULL)
1238 return line;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001239 if (!PyArg_Parse(line, "s;embedded '\\0' in input line", &str))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001240 return NULL;
1241 while (*str == ' ' || *str == '\t')
1242 str++;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001243 globals = PyEval_GetGlobals();
1244 locals = PyEval_GetLocals();
1245 if (PyDict_GetItemString(globals, "__builtins__") == NULL) {
1246 if (PyDict_SetItemString(globals, "__builtins__",
1247 PyEval_GetBuiltins()) != 0)
Guido van Rossum6135a871995-01-09 17:53:26 +00001248 return NULL;
1249 }
Hye-Shik Changff83c2b2004-02-02 13:39:01 +00001250 cf.cf_flags = 0;
1251 PyEval_MergeCompilerFlags(&cf);
1252 res = PyRun_StringFlags(str, Py_eval_input, globals, locals, &cf);
Guido van Rossum79f25d91997-04-29 20:08:16 +00001253 Py_DECREF(line);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001254 return res;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001255}
1256
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001257PyDoc_STRVAR(input_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001258"input([prompt]) -> value\n\
1259\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001260Equivalent to eval(raw_input(prompt)).");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001261
1262
Guido van Rossume8811f81997-02-14 15:48:05 +00001263static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001264builtin_intern(PyObject *self, PyObject *args)
Guido van Rossume8811f81997-02-14 15:48:05 +00001265{
1266 PyObject *s;
Guido van Rossum43713e52000-02-29 13:59:29 +00001267 if (!PyArg_ParseTuple(args, "S:intern", &s))
Guido van Rossume8811f81997-02-14 15:48:05 +00001268 return NULL;
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001269 if (!PyString_CheckExact(s)) {
Jeremy Hylton4c989dd2004-08-07 19:20:05 +00001270 PyErr_SetString(PyExc_TypeError,
1271 "can't intern subclass of string");
1272 return NULL;
1273 }
Guido van Rossume8811f81997-02-14 15:48:05 +00001274 Py_INCREF(s);
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001275 PyString_InternInPlace(&s);
Guido van Rossume8811f81997-02-14 15:48:05 +00001276 return s;
1277}
1278
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001279PyDoc_STRVAR(intern_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001280"intern(string) -> string\n\
1281\n\
1282``Intern'' the given string. This enters the string in the (global)\n\
1283table of interned strings whose purpose is to speed up dictionary lookups.\n\
1284Return the string itself or the previously interned string object with the\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001285same value.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001286
1287
Guido van Rossum79f25d91997-04-29 20:08:16 +00001288static PyObject *
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001289builtin_iter(PyObject *self, PyObject *args)
1290{
1291 PyObject *v, *w = NULL;
1292
Raymond Hettingerea3fdf42002-12-29 16:33:45 +00001293 if (!PyArg_UnpackTuple(args, "iter", 1, 2, &v, &w))
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001294 return NULL;
1295 if (w == NULL)
1296 return PyObject_GetIter(v);
1297 if (!PyCallable_Check(v)) {
1298 PyErr_SetString(PyExc_TypeError,
1299 "iter(v, w): v must be callable");
1300 return NULL;
1301 }
1302 return PyCallIter_New(v, w);
1303}
1304
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001305PyDoc_STRVAR(iter_doc,
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001306"iter(collection) -> iterator\n\
1307iter(callable, sentinel) -> iterator\n\
1308\n\
1309Get an iterator from an object. In the first form, the argument must\n\
1310supply its own iterator, or be a sequence.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001311In the second form, the callable is called until it returns the sentinel.");
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001312
1313
1314static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001315builtin_len(PyObject *self, PyObject *v)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001316{
Martin v. Löwis18e16552006-02-15 17:27:45 +00001317 Py_ssize_t res;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001318
Jeremy Hylton03657cf2000-07-12 13:05:33 +00001319 res = PyObject_Size(v);
Guido van Rossum8ea9f4d1998-06-29 22:26:50 +00001320 if (res < 0 && PyErr_Occurred())
1321 return NULL;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001322 return PyInt_FromSsize_t(res);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001323}
1324
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001325PyDoc_STRVAR(len_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001326"len(object) -> integer\n\
1327\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001328Return the number of items of a sequence or mapping.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001329
1330
Guido van Rossum79f25d91997-04-29 20:08:16 +00001331static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001332builtin_locals(PyObject *self)
Guido van Rossum872537c1995-07-07 22:43:42 +00001333{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001334 PyObject *d;
Guido van Rossum872537c1995-07-07 22:43:42 +00001335
Guido van Rossum79f25d91997-04-29 20:08:16 +00001336 d = PyEval_GetLocals();
Neal Norwitz43bd4db2006-08-12 01:46:42 +00001337 Py_XINCREF(d);
Guido van Rossum872537c1995-07-07 22:43:42 +00001338 return d;
1339}
1340
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001341PyDoc_STRVAR(locals_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001342"locals() -> dictionary\n\
1343\n\
Raymond Hettinger69bf8f32003-01-04 02:16:22 +00001344Update and return a dictionary containing the current scope's local variables.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001345
1346
Guido van Rossum79f25d91997-04-29 20:08:16 +00001347static PyObject *
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001348min_max(PyObject *args, PyObject *kwds, int op)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001349{
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001350 PyObject *v, *it, *item, *val, *maxitem, *maxval, *keyfunc=NULL;
Martin v. Löwisfd39ad42004-08-12 14:42:37 +00001351 const char *name = op == Py_LT ? "min" : "max";
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001352
Guido van Rossum79f25d91997-04-29 20:08:16 +00001353 if (PyTuple_Size(args) > 1)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001354 v = args;
Martin v. Löwisfd39ad42004-08-12 14:42:37 +00001355 else if (!PyArg_UnpackTuple(args, (char *)name, 1, 1, &v))
Guido van Rossum3f5da241990-12-20 15:06:42 +00001356 return NULL;
Tim Peters67d687a2002-04-29 21:27:32 +00001357
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001358 if (kwds != NULL && PyDict_Check(kwds) && PyDict_Size(kwds)) {
1359 keyfunc = PyDict_GetItemString(kwds, "key");
1360 if (PyDict_Size(kwds)!=1 || keyfunc == NULL) {
Georg Brandl99d7e4e2005-08-31 22:21:15 +00001361 PyErr_Format(PyExc_TypeError,
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001362 "%s() got an unexpected keyword argument", name);
1363 return NULL;
1364 }
Guido van Rossum1d9a9ea2008-01-23 20:19:01 +00001365 Py_INCREF(keyfunc);
Georg Brandl99d7e4e2005-08-31 22:21:15 +00001366 }
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001367
Tim Petersc3074532001-05-03 07:00:32 +00001368 it = PyObject_GetIter(v);
Guido van Rossum1d9a9ea2008-01-23 20:19:01 +00001369 if (it == NULL) {
1370 Py_XDECREF(keyfunc);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001371 return NULL;
Guido van Rossum1d9a9ea2008-01-23 20:19:01 +00001372 }
Tim Petersc3074532001-05-03 07:00:32 +00001373
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001374 maxitem = NULL; /* the result */
1375 maxval = NULL; /* the value associated with the result */
Brett Cannon9e635cf2004-12-07 00:25:35 +00001376 while (( item = PyIter_Next(it) )) {
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001377 /* get the value from the key function */
1378 if (keyfunc != NULL) {
1379 val = PyObject_CallFunctionObjArgs(keyfunc, item, NULL);
1380 if (val == NULL)
1381 goto Fail_it_item;
1382 }
1383 /* no key function; the value is the item */
1384 else {
1385 val = item;
1386 Py_INCREF(val);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001387 }
Tim Petersc3074532001-05-03 07:00:32 +00001388
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001389 /* maximum value and item are unset; set them */
1390 if (maxval == NULL) {
1391 maxitem = item;
1392 maxval = val;
1393 }
1394 /* maximum value and item are set; update them as necessary */
Guido van Rossum2d951851994-08-29 12:52:16 +00001395 else {
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001396 int cmp = PyObject_RichCompareBool(val, maxval, op);
1397 if (cmp < 0)
1398 goto Fail_it_item_and_val;
1399 else if (cmp > 0) {
1400 Py_DECREF(maxval);
1401 Py_DECREF(maxitem);
1402 maxval = val;
1403 maxitem = item;
Guido van Rossum53451b32001-01-17 15:47:24 +00001404 }
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001405 else {
1406 Py_DECREF(item);
1407 Py_DECREF(val);
Guido van Rossumc8b6df91997-05-23 00:06:51 +00001408 }
Guido van Rossum2d951851994-08-29 12:52:16 +00001409 }
Guido van Rossum3f5da241990-12-20 15:06:42 +00001410 }
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001411 if (PyErr_Occurred())
1412 goto Fail_it;
1413 if (maxval == NULL) {
Martin v. Löwisfd39ad42004-08-12 14:42:37 +00001414 PyErr_Format(PyExc_ValueError,
1415 "%s() arg is an empty sequence", name);
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001416 assert(maxitem == NULL);
1417 }
1418 else
1419 Py_DECREF(maxval);
Tim Petersc3074532001-05-03 07:00:32 +00001420 Py_DECREF(it);
Guido van Rossum1d9a9ea2008-01-23 20:19:01 +00001421 Py_XDECREF(keyfunc);
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001422 return maxitem;
1423
1424Fail_it_item_and_val:
1425 Py_DECREF(val);
1426Fail_it_item:
1427 Py_DECREF(item);
1428Fail_it:
1429 Py_XDECREF(maxval);
1430 Py_XDECREF(maxitem);
1431 Py_DECREF(it);
Guido van Rossum1d9a9ea2008-01-23 20:19:01 +00001432 Py_XDECREF(keyfunc);
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001433 return NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001434}
1435
Guido van Rossum79f25d91997-04-29 20:08:16 +00001436static PyObject *
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001437builtin_min(PyObject *self, PyObject *args, PyObject *kwds)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001438{
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001439 return min_max(args, kwds, Py_LT);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001440}
1441
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001442PyDoc_STRVAR(min_doc,
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001443"min(iterable[, key=func]) -> value\n\
1444min(a, b, c, ...[, key=func]) -> value\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001445\n\
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001446With a single iterable argument, return its smallest item.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001447With two or more arguments, return the smallest argument.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001448
1449
Guido van Rossum79f25d91997-04-29 20:08:16 +00001450static PyObject *
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001451builtin_max(PyObject *self, PyObject *args, PyObject *kwds)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001452{
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001453 return min_max(args, kwds, Py_GT);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001454}
1455
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001456PyDoc_STRVAR(max_doc,
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001457"max(iterable[, key=func]) -> value\n\
1458max(a, b, c, ...[, key=func]) -> value\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001459\n\
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001460With a single iterable argument, return its largest item.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001461With two or more arguments, return the largest argument.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001462
1463
Guido van Rossum79f25d91997-04-29 20:08:16 +00001464static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001465builtin_oct(PyObject *self, PyObject *v)
Guido van Rossum006bcd41991-10-24 14:54:44 +00001466{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001467 PyNumberMethods *nb;
Neil Schemenauer3a313e32004-07-19 16:29:17 +00001468 PyObject *res;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001469
Guido van Rossum1899c2e1992-09-12 11:09:23 +00001470 if (v == NULL || (nb = v->ob_type->tp_as_number) == NULL ||
1471 nb->nb_oct == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001472 PyErr_SetString(PyExc_TypeError,
Guido van Rossum1899c2e1992-09-12 11:09:23 +00001473 "oct() argument can't be converted to oct");
1474 return NULL;
Guido van Rossum006bcd41991-10-24 14:54:44 +00001475 }
Neil Schemenauer3a313e32004-07-19 16:29:17 +00001476 res = (*nb->nb_oct)(v);
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001477 if (res && !PyString_Check(res)) {
Neil Schemenauer3a313e32004-07-19 16:29:17 +00001478 PyErr_Format(PyExc_TypeError,
1479 "__oct__ returned non-string (type %.200s)",
1480 res->ob_type->tp_name);
1481 Py_DECREF(res);
1482 return NULL;
1483 }
1484 return res;
Guido van Rossum006bcd41991-10-24 14:54:44 +00001485}
1486
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001487PyDoc_STRVAR(oct_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001488"oct(number) -> string\n\
1489\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001490Return the octal representation of an integer or long integer.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001491
1492
Guido van Rossum79f25d91997-04-29 20:08:16 +00001493static PyObject *
Neal Norwitzc4edb0e2006-05-02 04:43:14 +00001494builtin_open(PyObject *self, PyObject *args, PyObject *kwds)
1495{
1496 return PyObject_Call((PyObject*)&PyFile_Type, args, kwds);
1497}
1498
1499PyDoc_STRVAR(open_doc,
1500"open(name[, mode[, buffering]]) -> file object\n\
1501\n\
Skip Montanaro4e3ebe02007-12-08 14:37:43 +00001502Open a file using the file() type, returns a file object. This is the\n\
1503preferred way to open a file.");
Neal Norwitzc4edb0e2006-05-02 04:43:14 +00001504
1505
1506static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001507builtin_ord(PyObject *self, PyObject* obj)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001508{
Guido van Rossum09095f32000-03-10 23:00:52 +00001509 long ord;
Martin v. Löwisd96ee902006-02-16 14:37:16 +00001510 Py_ssize_t size;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001511
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001512 if (PyString_Check(obj)) {
1513 size = PyString_GET_SIZE(obj);
Andrew M. Kuchling34c20cf2000-12-20 14:36:56 +00001514 if (size == 1) {
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001515 ord = (long)((unsigned char)*PyString_AS_STRING(obj));
Andrew M. Kuchling34c20cf2000-12-20 14:36:56 +00001516 return PyInt_FromLong(ord);
1517 }
Christian Heimes3497f942008-05-26 12:29:14 +00001518 } else if (PyByteArray_Check(obj)) {
1519 size = PyByteArray_GET_SIZE(obj);
Christian Heimes1a6387e2008-03-26 12:49:49 +00001520 if (size == 1) {
Christian Heimes3497f942008-05-26 12:29:14 +00001521 ord = (long)((unsigned char)*PyByteArray_AS_STRING(obj));
Christian Heimes1a6387e2008-03-26 12:49:49 +00001522 return PyInt_FromLong(ord);
1523 }
1524
Martin v. Löwis339d0f72001-08-17 18:39:25 +00001525#ifdef Py_USING_UNICODE
Jeremy Hylton394b54d2000-04-12 21:19:47 +00001526 } else if (PyUnicode_Check(obj)) {
1527 size = PyUnicode_GET_SIZE(obj);
Andrew M. Kuchling34c20cf2000-12-20 14:36:56 +00001528 if (size == 1) {
Jeremy Hylton394b54d2000-04-12 21:19:47 +00001529 ord = (long)*PyUnicode_AS_UNICODE(obj);
Andrew M. Kuchling34c20cf2000-12-20 14:36:56 +00001530 return PyInt_FromLong(ord);
1531 }
Martin v. Löwis339d0f72001-08-17 18:39:25 +00001532#endif
Jeremy Hylton394b54d2000-04-12 21:19:47 +00001533 } else {
1534 PyErr_Format(PyExc_TypeError,
Andrew M. Kuchlingf07aad12000-12-23 14:11:28 +00001535 "ord() expected string of length 1, but " \
Jeremy Hylton394b54d2000-04-12 21:19:47 +00001536 "%.200s found", obj->ob_type->tp_name);
Guido van Rossum09095f32000-03-10 23:00:52 +00001537 return NULL;
1538 }
1539
Guido van Rossumad991772001-01-12 16:03:05 +00001540 PyErr_Format(PyExc_TypeError,
Andrew M. Kuchlingf07aad12000-12-23 14:11:28 +00001541 "ord() expected a character, "
Martin v. Löwisd96ee902006-02-16 14:37:16 +00001542 "but string of length %zd found",
Jeremy Hylton394b54d2000-04-12 21:19:47 +00001543 size);
1544 return NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001545}
1546
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001547PyDoc_STRVAR(ord_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001548"ord(c) -> integer\n\
1549\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001550Return the integer ordinal of a one-character string.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001551
1552
Guido van Rossum79f25d91997-04-29 20:08:16 +00001553static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001554builtin_pow(PyObject *self, PyObject *args)
Guido van Rossum6a00cd81995-01-07 12:39:01 +00001555{
Guido van Rossum09df08a1998-05-22 00:51:39 +00001556 PyObject *v, *w, *z = Py_None;
Guido van Rossum6a00cd81995-01-07 12:39:01 +00001557
Raymond Hettingerea3fdf42002-12-29 16:33:45 +00001558 if (!PyArg_UnpackTuple(args, "pow", 2, 3, &v, &w, &z))
Guido van Rossum6a00cd81995-01-07 12:39:01 +00001559 return NULL;
Guido van Rossum09df08a1998-05-22 00:51:39 +00001560 return PyNumber_Power(v, w, z);
Guido van Rossumd4905451991-05-05 20:00:36 +00001561}
1562
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001563PyDoc_STRVAR(pow_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001564"pow(x, y[, z]) -> number\n\
1565\n\
1566With two arguments, equivalent to x**y. With three arguments,\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001567equivalent to (x**y) % z, but may be more efficient (e.g. for longs).");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001568
1569
Eric Smith7c478942008-03-18 23:45:49 +00001570static PyObject *
1571builtin_print(PyObject *self, PyObject *args, PyObject *kwds)
1572{
1573 static char *kwlist[] = {"sep", "end", "file", 0};
1574 static PyObject *dummy_args;
1575 PyObject *sep = NULL, *end = NULL, *file = NULL;
1576 int i, err;
1577
1578 if (dummy_args == NULL) {
1579 if (!(dummy_args = PyTuple_New(0)))
1580 return NULL;
1581 }
1582 if (!PyArg_ParseTupleAndKeywords(dummy_args, kwds, "|OOO:print",
1583 kwlist, &sep, &end, &file))
1584 return NULL;
1585 if (file == NULL || file == Py_None) {
1586 file = PySys_GetObject("stdout");
1587 /* sys.stdout may be None when FILE* stdout isn't connected */
1588 if (file == Py_None)
1589 Py_RETURN_NONE;
1590 }
1591
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001592 if (sep && sep != Py_None && !PyString_Check(sep) &&
Eric Smith7c478942008-03-18 23:45:49 +00001593 !PyUnicode_Check(sep)) {
1594 PyErr_Format(PyExc_TypeError,
1595 "sep must be None, str or unicode, not %.200s",
1596 sep->ob_type->tp_name);
1597 return NULL;
1598 }
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001599 if (end && end != Py_None && !PyString_Check(end) &&
Eric Smith7c478942008-03-18 23:45:49 +00001600 !PyUnicode_Check(end)) {
1601 PyErr_Format(PyExc_TypeError,
1602 "end must be None, str or unicode, not %.200s",
1603 end->ob_type->tp_name);
1604 return NULL;
1605 }
1606
1607 for (i = 0; i < PyTuple_Size(args); i++) {
1608 if (i > 0) {
1609 if (sep == NULL || sep == Py_None)
1610 err = PyFile_WriteString(" ", file);
1611 else
1612 err = PyFile_WriteObject(sep, file,
1613 Py_PRINT_RAW);
1614 if (err)
1615 return NULL;
1616 }
1617 err = PyFile_WriteObject(PyTuple_GetItem(args, i), file,
1618 Py_PRINT_RAW);
1619 if (err)
1620 return NULL;
1621 }
1622
1623 if (end == NULL || end == Py_None)
1624 err = PyFile_WriteString("\n", file);
1625 else
1626 err = PyFile_WriteObject(end, file, Py_PRINT_RAW);
1627 if (err)
1628 return NULL;
1629
1630 Py_RETURN_NONE;
1631}
1632
1633PyDoc_STRVAR(print_doc,
1634"print(value, ..., sep=' ', end='\\n', file=sys.stdout)\n\
1635\n\
1636Prints the values to a stream, or to sys.stdout by default.\n\
1637Optional keyword arguments:\n\
1638file: a file-like object (stream); defaults to the current sys.stdout.\n\
1639sep: string inserted between values, default a space.\n\
1640end: string appended after the last value, default a newline.");
1641
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001642
1643/* Return number of items in range (lo, hi, step), when arguments are
1644 * PyInt or PyLong objects. step > 0 required. Return a value < 0 if
1645 * & only if the true value is too large to fit in a signed long.
1646 * Arguments MUST return 1 with either PyInt_Check() or
1647 * PyLong_Check(). Return -1 when there is an error.
1648 */
1649static long
1650get_len_of_range_longs(PyObject *lo, PyObject *hi, PyObject *step)
1651{
1652 /* -------------------------------------------------------------
1653 Algorithm is equal to that of get_len_of_range(), but it operates
1654 on PyObjects (which are assumed to be PyLong or PyInt objects).
1655 ---------------------------------------------------------------*/
1656 long n;
1657 PyObject *diff = NULL;
1658 PyObject *one = NULL;
1659 PyObject *tmp1 = NULL, *tmp2 = NULL, *tmp3 = NULL;
1660 /* holds sub-expression evaluations */
1661
1662 /* if (lo >= hi), return length of 0. */
1663 if (PyObject_Compare(lo, hi) >= 0)
1664 return 0;
1665
1666 if ((one = PyLong_FromLong(1L)) == NULL)
1667 goto Fail;
1668
1669 if ((tmp1 = PyNumber_Subtract(hi, lo)) == NULL)
1670 goto Fail;
1671
1672 if ((diff = PyNumber_Subtract(tmp1, one)) == NULL)
1673 goto Fail;
1674
1675 if ((tmp2 = PyNumber_FloorDivide(diff, step)) == NULL)
1676 goto Fail;
1677
1678 if ((tmp3 = PyNumber_Add(tmp2, one)) == NULL)
1679 goto Fail;
1680
1681 n = PyLong_AsLong(tmp3);
1682 if (PyErr_Occurred()) { /* Check for Overflow */
1683 PyErr_Clear();
1684 goto Fail;
1685 }
1686
1687 Py_DECREF(tmp3);
1688 Py_DECREF(tmp2);
1689 Py_DECREF(diff);
1690 Py_DECREF(tmp1);
1691 Py_DECREF(one);
1692 return n;
1693
1694 Fail:
1695 Py_XDECREF(tmp3);
1696 Py_XDECREF(tmp2);
1697 Py_XDECREF(diff);
1698 Py_XDECREF(tmp1);
1699 Py_XDECREF(one);
1700 return -1;
1701}
1702
1703/* An extension of builtin_range() that handles the case when PyLong
1704 * arguments are given. */
1705static PyObject *
1706handle_range_longs(PyObject *self, PyObject *args)
1707{
1708 PyObject *ilow;
Tim Peters874e1f72003-04-13 22:13:08 +00001709 PyObject *ihigh = NULL;
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001710 PyObject *istep = NULL;
Tim Peters874e1f72003-04-13 22:13:08 +00001711
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001712 PyObject *curnum = NULL;
1713 PyObject *v = NULL;
1714 long bign;
1715 int i, n;
1716 int cmp_result;
1717
Tim Peters874e1f72003-04-13 22:13:08 +00001718 PyObject *zero = PyLong_FromLong(0);
1719
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001720 if (zero == NULL)
1721 return NULL;
1722
Tim Peters874e1f72003-04-13 22:13:08 +00001723 if (!PyArg_UnpackTuple(args, "range", 1, 3, &ilow, &ihigh, &istep)) {
1724 Py_DECREF(zero);
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001725 return NULL;
1726 }
1727
Tim Peters874e1f72003-04-13 22:13:08 +00001728 /* Figure out which way we were called, supply defaults, and be
1729 * sure to incref everything so that the decrefs at the end
1730 * are correct.
1731 */
1732 assert(ilow != NULL);
1733 if (ihigh == NULL) {
1734 /* only 1 arg -- it's the upper limit */
1735 ihigh = ilow;
1736 ilow = NULL;
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001737 }
Tim Peters874e1f72003-04-13 22:13:08 +00001738 assert(ihigh != NULL);
1739 Py_INCREF(ihigh);
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001740
Tim Peters874e1f72003-04-13 22:13:08 +00001741 /* ihigh correct now; do ilow */
1742 if (ilow == NULL)
1743 ilow = zero;
1744 Py_INCREF(ilow);
1745
1746 /* ilow and ihigh correct now; do istep */
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001747 if (istep == NULL) {
1748 istep = PyLong_FromLong(1L);
1749 if (istep == NULL)
1750 goto Fail;
1751 }
1752 else {
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001753 Py_INCREF(istep);
1754 }
1755
Tim Peters874e1f72003-04-13 22:13:08 +00001756 if (!PyInt_Check(ilow) && !PyLong_Check(ilow)) {
Guido van Rossum28e83e32003-04-15 12:43:26 +00001757 PyErr_Format(PyExc_TypeError,
Alex Martellif471d472003-04-23 13:00:44 +00001758 "range() integer start argument expected, got %s.",
Guido van Rossum817d6c92003-04-14 18:25:04 +00001759 ilow->ob_type->tp_name);
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001760 goto Fail;
1761 }
1762
Tim Peters874e1f72003-04-13 22:13:08 +00001763 if (!PyInt_Check(ihigh) && !PyLong_Check(ihigh)) {
Guido van Rossum28e83e32003-04-15 12:43:26 +00001764 PyErr_Format(PyExc_TypeError,
Alex Martellif471d472003-04-23 13:00:44 +00001765 "range() integer end argument expected, got %s.",
Guido van Rossum817d6c92003-04-14 18:25:04 +00001766 ihigh->ob_type->tp_name);
Tim Peters874e1f72003-04-13 22:13:08 +00001767 goto Fail;
1768 }
1769
1770 if (!PyInt_Check(istep) && !PyLong_Check(istep)) {
Guido van Rossum28e83e32003-04-15 12:43:26 +00001771 PyErr_Format(PyExc_TypeError,
Alex Martellif471d472003-04-23 13:00:44 +00001772 "range() integer step argument expected, got %s.",
Guido van Rossum817d6c92003-04-14 18:25:04 +00001773 istep->ob_type->tp_name);
Tim Peters874e1f72003-04-13 22:13:08 +00001774 goto Fail;
1775 }
1776
1777 if (PyObject_Cmp(istep, zero, &cmp_result) == -1)
1778 goto Fail;
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001779 if (cmp_result == 0) {
1780 PyErr_SetString(PyExc_ValueError,
Alex Martellif471d472003-04-23 13:00:44 +00001781 "range() step argument must not be zero");
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001782 goto Fail;
1783 }
1784
1785 if (cmp_result > 0)
1786 bign = get_len_of_range_longs(ilow, ihigh, istep);
1787 else {
1788 PyObject *neg_istep = PyNumber_Negative(istep);
1789 if (neg_istep == NULL)
1790 goto Fail;
1791 bign = get_len_of_range_longs(ihigh, ilow, neg_istep);
1792 Py_DECREF(neg_istep);
1793 }
1794
1795 n = (int)bign;
1796 if (bign < 0 || (long)n != bign) {
1797 PyErr_SetString(PyExc_OverflowError,
1798 "range() result has too many items");
1799 goto Fail;
1800 }
1801
1802 v = PyList_New(n);
1803 if (v == NULL)
1804 goto Fail;
1805
1806 curnum = ilow;
1807 Py_INCREF(curnum);
1808
1809 for (i = 0; i < n; i++) {
1810 PyObject *w = PyNumber_Long(curnum);
1811 PyObject *tmp_num;
1812 if (w == NULL)
1813 goto Fail;
1814
1815 PyList_SET_ITEM(v, i, w);
1816
1817 tmp_num = PyNumber_Add(curnum, istep);
1818 if (tmp_num == NULL)
1819 goto Fail;
1820
1821 Py_DECREF(curnum);
1822 curnum = tmp_num;
1823 }
Tim Peters874e1f72003-04-13 22:13:08 +00001824 Py_DECREF(ilow);
1825 Py_DECREF(ihigh);
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001826 Py_DECREF(istep);
1827 Py_DECREF(zero);
Tim Peters874e1f72003-04-13 22:13:08 +00001828 Py_DECREF(curnum);
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001829 return v;
1830
1831 Fail:
Tim Peters874e1f72003-04-13 22:13:08 +00001832 Py_DECREF(ilow);
1833 Py_DECREF(ihigh);
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001834 Py_XDECREF(istep);
Tim Peters874e1f72003-04-13 22:13:08 +00001835 Py_DECREF(zero);
1836 Py_XDECREF(curnum);
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001837 Py_XDECREF(v);
1838 return NULL;
1839}
1840
Guido van Rossum124eff01999-02-23 16:11:01 +00001841/* Return number of items in range/xrange (lo, hi, step). step > 0
1842 * required. Return a value < 0 if & only if the true value is too
1843 * large to fit in a signed long.
1844 */
1845static long
Thomas Wouterse28c2962000-07-23 22:21:32 +00001846get_len_of_range(long lo, long hi, long step)
Guido van Rossum124eff01999-02-23 16:11:01 +00001847{
1848 /* -------------------------------------------------------------
1849 If lo >= hi, the range is empty.
1850 Else if n values are in the range, the last one is
1851 lo + (n-1)*step, which must be <= hi-1. Rearranging,
1852 n <= (hi - lo - 1)/step + 1, so taking the floor of the RHS gives
1853 the proper value. Since lo < hi in this case, hi-lo-1 >= 0, so
1854 the RHS is non-negative and so truncation is the same as the
1855 floor. Letting M be the largest positive long, the worst case
1856 for the RHS numerator is hi=M, lo=-M-1, and then
1857 hi-lo-1 = M-(-M-1)-1 = 2*M. Therefore unsigned long has enough
1858 precision to compute the RHS exactly.
1859 ---------------------------------------------------------------*/
1860 long n = 0;
1861 if (lo < hi) {
1862 unsigned long uhi = (unsigned long)hi;
1863 unsigned long ulo = (unsigned long)lo;
1864 unsigned long diff = uhi - ulo - 1;
1865 n = (long)(diff / (unsigned long)step + 1);
1866 }
1867 return n;
1868}
1869
Guido van Rossum79f25d91997-04-29 20:08:16 +00001870static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001871builtin_range(PyObject *self, PyObject *args)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001872{
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001873 long ilow = 0, ihigh = 0, istep = 1;
Guido van Rossum124eff01999-02-23 16:11:01 +00001874 long bign;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001875 int i, n;
Guido van Rossum124eff01999-02-23 16:11:01 +00001876
Guido van Rossum79f25d91997-04-29 20:08:16 +00001877 PyObject *v;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001878
Guido van Rossum79f25d91997-04-29 20:08:16 +00001879 if (PyTuple_Size(args) <= 1) {
1880 if (!PyArg_ParseTuple(args,
Guido van Rossum0865dd91995-01-17 16:30:22 +00001881 "l;range() requires 1-3 int arguments",
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001882 &ihigh)) {
1883 PyErr_Clear();
1884 return handle_range_longs(self, args);
1885 }
Guido van Rossum3f5da241990-12-20 15:06:42 +00001886 }
1887 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001888 if (!PyArg_ParseTuple(args,
Guido van Rossum0865dd91995-01-17 16:30:22 +00001889 "ll|l;range() requires 1-3 int arguments",
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001890 &ilow, &ihigh, &istep)) {
1891 PyErr_Clear();
1892 return handle_range_longs(self, args);
1893 }
Guido van Rossum3f5da241990-12-20 15:06:42 +00001894 }
1895 if (istep == 0) {
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001896 PyErr_SetString(PyExc_ValueError,
Alex Martellif471d472003-04-23 13:00:44 +00001897 "range() step argument must not be zero");
Guido van Rossum3f5da241990-12-20 15:06:42 +00001898 return NULL;
1899 }
Guido van Rossum124eff01999-02-23 16:11:01 +00001900 if (istep > 0)
1901 bign = get_len_of_range(ilow, ihigh, istep);
1902 else
1903 bign = get_len_of_range(ihigh, ilow, -istep);
1904 n = (int)bign;
1905 if (bign < 0 || (long)n != bign) {
Guido van Rossume23cde21999-01-12 05:07:47 +00001906 PyErr_SetString(PyExc_OverflowError,
Fred Drake661ea262000-10-24 19:57:45 +00001907 "range() result has too many items");
Guido van Rossume23cde21999-01-12 05:07:47 +00001908 return NULL;
1909 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00001910 v = PyList_New(n);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001911 if (v == NULL)
1912 return NULL;
1913 for (i = 0; i < n; i++) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001914 PyObject *w = PyInt_FromLong(ilow);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001915 if (w == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001916 Py_DECREF(v);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001917 return NULL;
1918 }
Guido van Rossuma937d141998-04-24 18:22:02 +00001919 PyList_SET_ITEM(v, i, w);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001920 ilow += istep;
1921 }
1922 return v;
1923}
1924
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001925PyDoc_STRVAR(range_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001926"range([start,] stop[, step]) -> list of integers\n\
1927\n\
1928Return a list containing an arithmetic progression of integers.\n\
1929range(i, j) returns [i, i+1, i+2, ..., j-1]; start (!) defaults to 0.\n\
1930When step is given, it specifies the increment (or decrement).\n\
1931For example, range(4) returns [0, 1, 2, 3]. The end point is omitted!\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001932These are exactly the valid indices for a list of 4 elements.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001933
1934
Guido van Rossum79f25d91997-04-29 20:08:16 +00001935static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001936builtin_raw_input(PyObject *self, PyObject *args)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001937{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001938 PyObject *v = NULL;
Martin v. Löwis31d2df52002-08-14 15:46:02 +00001939 PyObject *fin = PySys_GetObject("stdin");
1940 PyObject *fout = PySys_GetObject("stdout");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001941
Raymond Hettingerea3fdf42002-12-29 16:33:45 +00001942 if (!PyArg_UnpackTuple(args, "[raw_]input", 0, 1, &v))
Guido van Rossum3165fe61992-09-25 21:59:05 +00001943 return NULL;
Martin v. Löwis31d2df52002-08-14 15:46:02 +00001944
1945 if (fin == NULL) {
Alex Martellia9b9c9f2003-04-23 13:34:35 +00001946 PyErr_SetString(PyExc_RuntimeError, "[raw_]input: lost sys.stdin");
Martin v. Löwis31d2df52002-08-14 15:46:02 +00001947 return NULL;
1948 }
1949 if (fout == NULL) {
Alex Martellia9b9c9f2003-04-23 13:34:35 +00001950 PyErr_SetString(PyExc_RuntimeError, "[raw_]input: lost sys.stdout");
Martin v. Löwis31d2df52002-08-14 15:46:02 +00001951 return NULL;
1952 }
1953 if (PyFile_SoftSpace(fout, 0)) {
1954 if (PyFile_WriteString(" ", fout) != 0)
1955 return NULL;
1956 }
Georg Brandl7e3ba2a2006-08-06 08:23:54 +00001957 if (PyFile_AsFile(fin) && PyFile_AsFile(fout)
Martin v. Löwis566f6af2002-10-26 14:39:10 +00001958 && isatty(fileno(PyFile_AsFile(fin)))
1959 && isatty(fileno(PyFile_AsFile(fout)))) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001960 PyObject *po;
Guido van Rossum872537c1995-07-07 22:43:42 +00001961 char *prompt;
1962 char *s;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001963 PyObject *result;
Guido van Rossum872537c1995-07-07 22:43:42 +00001964 if (v != NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001965 po = PyObject_Str(v);
Guido van Rossum872537c1995-07-07 22:43:42 +00001966 if (po == NULL)
1967 return NULL;
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001968 prompt = PyString_AsString(po);
Guido van Rossumd9b52081998-06-26 18:25:38 +00001969 if (prompt == NULL)
1970 return NULL;
Guido van Rossum872537c1995-07-07 22:43:42 +00001971 }
1972 else {
1973 po = NULL;
1974 prompt = "";
1975 }
Michael W. Hudson52db5192004-08-02 13:21:09 +00001976 s = PyOS_Readline(PyFile_AsFile(fin), PyFile_AsFile(fout),
Martin v. Löwis566f6af2002-10-26 14:39:10 +00001977 prompt);
Guido van Rossum79f25d91997-04-29 20:08:16 +00001978 Py_XDECREF(po);
Guido van Rossum872537c1995-07-07 22:43:42 +00001979 if (s == NULL) {
Michael W. Hudson30ea2f22004-07-07 17:44:12 +00001980 if (!PyErr_Occurred())
1981 PyErr_SetNone(PyExc_KeyboardInterrupt);
Guido van Rossum872537c1995-07-07 22:43:42 +00001982 return NULL;
1983 }
1984 if (*s == '\0') {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001985 PyErr_SetNone(PyExc_EOFError);
Guido van Rossum872537c1995-07-07 22:43:42 +00001986 result = NULL;
1987 }
1988 else { /* strip trailing '\n' */
Guido van Rossum106f2da2000-06-28 21:12:25 +00001989 size_t len = strlen(s);
Martin v. Löwisb1ed7fa2006-04-13 07:52:27 +00001990 if (len > PY_SSIZE_T_MAX) {
Martin v. Löwis31d2df52002-08-14 15:46:02 +00001991 PyErr_SetString(PyExc_OverflowError,
Alex Martellia9b9c9f2003-04-23 13:34:35 +00001992 "[raw_]input: input too long");
Guido van Rossum106f2da2000-06-28 21:12:25 +00001993 result = NULL;
1994 }
1995 else {
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001996 result = PyString_FromStringAndSize(s, len-1);
Guido van Rossum106f2da2000-06-28 21:12:25 +00001997 }
Guido van Rossum872537c1995-07-07 22:43:42 +00001998 }
Guido van Rossumb18618d2000-05-03 23:44:39 +00001999 PyMem_FREE(s);
Guido van Rossum872537c1995-07-07 22:43:42 +00002000 return result;
2001 }
Guido van Rossum90933611991-06-07 16:10:43 +00002002 if (v != NULL) {
Martin v. Löwis31d2df52002-08-14 15:46:02 +00002003 if (PyFile_WriteObject(v, fout, Py_PRINT_RAW) != 0)
Guido van Rossum90933611991-06-07 16:10:43 +00002004 return NULL;
2005 }
Martin v. Löwis31d2df52002-08-14 15:46:02 +00002006 return PyFile_GetLine(fin, -1);
Guido van Rossum3f5da241990-12-20 15:06:42 +00002007}
2008
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002009PyDoc_STRVAR(raw_input_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002010"raw_input([prompt]) -> string\n\
2011\n\
2012Read a string from standard input. The trailing newline is stripped.\n\
2013If the user hits EOF (Unix: Ctl-D, Windows: Ctl-Z+Return), raise EOFError.\n\
2014On Unix, GNU readline is used if enabled. The prompt string, if given,\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002015is printed without a trailing newline before reading.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002016
2017
Guido van Rossum79f25d91997-04-29 20:08:16 +00002018static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002019builtin_reduce(PyObject *self, PyObject *args)
Guido van Rossum12d12c51993-10-26 17:58:25 +00002020{
Tim Peters15d81ef2001-05-04 04:39:21 +00002021 PyObject *seq, *func, *result = NULL, *it;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002022
Benjamin Peterson9f4f4812008-04-27 03:01:45 +00002023 if (PyErr_WarnPy3k("reduce() not supported in 3.x; "
Benjamin Petersonf19a7b92008-04-27 18:40:21 +00002024 "use functools.reduce()", 1) < 0)
Neal Norwitzdf25efe2007-05-23 06:58:36 +00002025 return NULL;
2026
Raymond Hettingerea3fdf42002-12-29 16:33:45 +00002027 if (!PyArg_UnpackTuple(args, "reduce", 2, 3, &func, &seq, &result))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002028 return NULL;
2029 if (result != NULL)
Guido van Rossum79f25d91997-04-29 20:08:16 +00002030 Py_INCREF(result);
Guido van Rossum12d12c51993-10-26 17:58:25 +00002031
Tim Peters15d81ef2001-05-04 04:39:21 +00002032 it = PyObject_GetIter(seq);
2033 if (it == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002034 PyErr_SetString(PyExc_TypeError,
Tim Peters15d81ef2001-05-04 04:39:21 +00002035 "reduce() arg 2 must support iteration");
2036 Py_XDECREF(result);
Guido van Rossum12d12c51993-10-26 17:58:25 +00002037 return NULL;
2038 }
2039
Guido van Rossum79f25d91997-04-29 20:08:16 +00002040 if ((args = PyTuple_New(2)) == NULL)
Guido van Rossum2d951851994-08-29 12:52:16 +00002041 goto Fail;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002042
Tim Peters15d81ef2001-05-04 04:39:21 +00002043 for (;;) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002044 PyObject *op2;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002045
2046 if (args->ob_refcnt > 1) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002047 Py_DECREF(args);
2048 if ((args = PyTuple_New(2)) == NULL)
Guido van Rossum2d951851994-08-29 12:52:16 +00002049 goto Fail;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002050 }
2051
Tim Peters15d81ef2001-05-04 04:39:21 +00002052 op2 = PyIter_Next(it);
2053 if (op2 == NULL) {
Tim Petersf4848da2001-05-05 00:14:56 +00002054 if (PyErr_Occurred())
2055 goto Fail;
2056 break;
Guido van Rossum2d951851994-08-29 12:52:16 +00002057 }
Guido van Rossum12d12c51993-10-26 17:58:25 +00002058
Guido van Rossum2d951851994-08-29 12:52:16 +00002059 if (result == NULL)
2060 result = op2;
2061 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002062 PyTuple_SetItem(args, 0, result);
2063 PyTuple_SetItem(args, 1, op2);
2064 if ((result = PyEval_CallObject(func, args)) == NULL)
Guido van Rossum2d951851994-08-29 12:52:16 +00002065 goto Fail;
2066 }
Guido van Rossum12d12c51993-10-26 17:58:25 +00002067 }
2068
Guido van Rossum79f25d91997-04-29 20:08:16 +00002069 Py_DECREF(args);
Guido van Rossum12d12c51993-10-26 17:58:25 +00002070
Guido van Rossum2d951851994-08-29 12:52:16 +00002071 if (result == NULL)
Guido van Rossum79f25d91997-04-29 20:08:16 +00002072 PyErr_SetString(PyExc_TypeError,
Fred Drake661ea262000-10-24 19:57:45 +00002073 "reduce() of empty sequence with no initial value");
Guido van Rossum2d951851994-08-29 12:52:16 +00002074
Tim Peters15d81ef2001-05-04 04:39:21 +00002075 Py_DECREF(it);
Guido van Rossum12d12c51993-10-26 17:58:25 +00002076 return result;
2077
Guido van Rossum2d951851994-08-29 12:52:16 +00002078Fail:
Guido van Rossum79f25d91997-04-29 20:08:16 +00002079 Py_XDECREF(args);
2080 Py_XDECREF(result);
Tim Peters15d81ef2001-05-04 04:39:21 +00002081 Py_DECREF(it);
Guido van Rossum12d12c51993-10-26 17:58:25 +00002082 return NULL;
2083}
2084
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002085PyDoc_STRVAR(reduce_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002086"reduce(function, sequence[, initial]) -> value\n\
2087\n\
2088Apply a function of two arguments cumulatively to the items of a sequence,\n\
2089from left to right, so as to reduce the sequence to a single value.\n\
2090For example, reduce(lambda x, y: x+y, [1, 2, 3, 4, 5]) calculates\n\
2091((((1+2)+3)+4)+5). If initial is present, it is placed before the items\n\
2092of the sequence in the calculation, and serves as a default when the\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002093sequence is empty.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002094
2095
Guido van Rossum79f25d91997-04-29 20:08:16 +00002096static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002097builtin_reload(PyObject *self, PyObject *v)
Guido van Rossum3f5da241990-12-20 15:06:42 +00002098{
Benjamin Peterson9f4f4812008-04-27 03:01:45 +00002099 if (PyErr_WarnPy3k("In 3.x, reload() is renamed to imp.reload()",
Benjamin Petersonf19a7b92008-04-27 18:40:21 +00002100 1) < 0)
Neal Norwitzdf25efe2007-05-23 06:58:36 +00002101 return NULL;
2102
Guido van Rossum79f25d91997-04-29 20:08:16 +00002103 return PyImport_ReloadModule(v);
Guido van Rossum3f5da241990-12-20 15:06:42 +00002104}
2105
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002106PyDoc_STRVAR(reload_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002107"reload(module) -> module\n\
2108\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002109Reload the module. The module must have been successfully imported before.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002110
2111
Guido van Rossum79f25d91997-04-29 20:08:16 +00002112static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002113builtin_repr(PyObject *self, PyObject *v)
Guido van Rossumc89705d1992-11-26 08:54:07 +00002114{
Guido van Rossum79f25d91997-04-29 20:08:16 +00002115 return PyObject_Repr(v);
Guido van Rossumc89705d1992-11-26 08:54:07 +00002116}
2117
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002118PyDoc_STRVAR(repr_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002119"repr(object) -> string\n\
2120\n\
2121Return the canonical string representation of the object.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002122For most object types, eval(repr(object)) == object.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002123
2124
Guido van Rossum79f25d91997-04-29 20:08:16 +00002125static PyObject *
Georg Brandlccadf842006-03-31 18:54:53 +00002126builtin_round(PyObject *self, PyObject *args, PyObject *kwds)
Guido van Rossum9e51f9b1993-02-12 16:29:05 +00002127{
Jeffrey Yasskin9871d8f2008-01-05 08:47:13 +00002128 double number;
2129 double f;
2130 int ndigits = 0;
2131 int i;
Georg Brandlccadf842006-03-31 18:54:53 +00002132 static char *kwlist[] = {"number", "ndigits", 0};
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002133
Jeffrey Yasskin9871d8f2008-01-05 08:47:13 +00002134 if (!PyArg_ParseTupleAndKeywords(args, kwds, "d|i:round",
2135 kwlist, &number, &ndigits))
2136 return NULL;
2137 f = 1.0;
2138 i = abs(ndigits);
2139 while (--i >= 0)
2140 f = f*10.0;
2141 if (ndigits < 0)
2142 number /= f;
2143 else
2144 number *= f;
2145 if (number >= 0.0)
2146 number = floor(number + 0.5);
2147 else
2148 number = ceil(number - 0.5);
2149 if (ndigits < 0)
2150 number *= f;
2151 else
2152 number /= f;
2153 return PyFloat_FromDouble(number);
Guido van Rossum9e51f9b1993-02-12 16:29:05 +00002154}
2155
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002156PyDoc_STRVAR(round_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002157"round(number[, ndigits]) -> floating point number\n\
2158\n\
2159Round a number to a given precision in decimal digits (default 0 digits).\n\
Jeffrey Yasskin9871d8f2008-01-05 08:47:13 +00002160This always returns a floating point number. Precision may be negative.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002161
Raymond Hettinger64958a12003-12-17 20:43:33 +00002162static PyObject *
2163builtin_sorted(PyObject *self, PyObject *args, PyObject *kwds)
2164{
2165 PyObject *newlist, *v, *seq, *compare=NULL, *keyfunc=NULL, *newargs;
2166 PyObject *callable;
Martin v. Löwis15e62742006-02-27 16:46:16 +00002167 static char *kwlist[] = {"iterable", "cmp", "key", "reverse", 0};
Neal Norwitz6f0d4792005-12-15 06:40:36 +00002168 int reverse;
Raymond Hettinger64958a12003-12-17 20:43:33 +00002169
Neal Norwitz6f0d4792005-12-15 06:40:36 +00002170 /* args 1-4 should match listsort in Objects/listobject.c */
Armin Rigo71d7e702005-09-20 18:13:03 +00002171 if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|OOi:sorted",
2172 kwlist, &seq, &compare, &keyfunc, &reverse))
2173 return NULL;
Raymond Hettinger64958a12003-12-17 20:43:33 +00002174
2175 newlist = PySequence_List(seq);
2176 if (newlist == NULL)
2177 return NULL;
2178
2179 callable = PyObject_GetAttrString(newlist, "sort");
2180 if (callable == NULL) {
2181 Py_DECREF(newlist);
2182 return NULL;
2183 }
Georg Brandl99d7e4e2005-08-31 22:21:15 +00002184
Raymond Hettinger64958a12003-12-17 20:43:33 +00002185 newargs = PyTuple_GetSlice(args, 1, 4);
2186 if (newargs == NULL) {
2187 Py_DECREF(newlist);
2188 Py_DECREF(callable);
2189 return NULL;
2190 }
2191
2192 v = PyObject_Call(callable, newargs, kwds);
2193 Py_DECREF(newargs);
2194 Py_DECREF(callable);
2195 if (v == NULL) {
2196 Py_DECREF(newlist);
2197 return NULL;
2198 }
2199 Py_DECREF(v);
2200 return newlist;
2201}
2202
2203PyDoc_STRVAR(sorted_doc,
2204"sorted(iterable, cmp=None, key=None, reverse=False) --> new sorted list");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002205
Guido van Rossum79f25d91997-04-29 20:08:16 +00002206static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002207builtin_vars(PyObject *self, PyObject *args)
Guido van Rossum2d951851994-08-29 12:52:16 +00002208{
Guido van Rossum79f25d91997-04-29 20:08:16 +00002209 PyObject *v = NULL;
2210 PyObject *d;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002211
Raymond Hettingerea3fdf42002-12-29 16:33:45 +00002212 if (!PyArg_UnpackTuple(args, "vars", 0, 1, &v))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002213 return NULL;
Guido van Rossum2d951851994-08-29 12:52:16 +00002214 if (v == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002215 d = PyEval_GetLocals();
Guido van Rossum53bb7ff1995-07-26 16:26:31 +00002216 if (d == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002217 if (!PyErr_Occurred())
2218 PyErr_SetString(PyExc_SystemError,
Alex Martellia9b9c9f2003-04-23 13:34:35 +00002219 "vars(): no locals!?");
Guido van Rossum53bb7ff1995-07-26 16:26:31 +00002220 }
2221 else
Guido van Rossum79f25d91997-04-29 20:08:16 +00002222 Py_INCREF(d);
Guido van Rossum2d951851994-08-29 12:52:16 +00002223 }
2224 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002225 d = PyObject_GetAttrString(v, "__dict__");
Guido van Rossum2d951851994-08-29 12:52:16 +00002226 if (d == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002227 PyErr_SetString(PyExc_TypeError,
Guido van Rossum2d951851994-08-29 12:52:16 +00002228 "vars() argument must have __dict__ attribute");
2229 return NULL;
2230 }
2231 }
2232 return d;
2233}
2234
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002235PyDoc_STRVAR(vars_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002236"vars([object]) -> dictionary\n\
2237\n\
2238Without arguments, equivalent to locals().\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002239With an argument, equivalent to object.__dict__.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002240
Alex Martellia70b1912003-04-22 08:12:33 +00002241
2242static PyObject*
2243builtin_sum(PyObject *self, PyObject *args)
2244{
2245 PyObject *seq;
2246 PyObject *result = NULL;
2247 PyObject *temp, *item, *iter;
2248
Raymond Hettinger5cf63942003-10-25 06:41:37 +00002249 if (!PyArg_UnpackTuple(args, "sum", 1, 2, &seq, &result))
Alex Martellia70b1912003-04-22 08:12:33 +00002250 return NULL;
2251
2252 iter = PyObject_GetIter(seq);
2253 if (iter == NULL)
2254 return NULL;
2255
2256 if (result == NULL) {
2257 result = PyInt_FromLong(0);
2258 if (result == NULL) {
2259 Py_DECREF(iter);
2260 return NULL;
2261 }
2262 } else {
2263 /* reject string values for 'start' parameter */
2264 if (PyObject_TypeCheck(result, &PyBaseString_Type)) {
2265 PyErr_SetString(PyExc_TypeError,
Alex Martellia9b9c9f2003-04-23 13:34:35 +00002266 "sum() can't sum strings [use ''.join(seq) instead]");
Alex Martellia70b1912003-04-22 08:12:33 +00002267 Py_DECREF(iter);
2268 return NULL;
2269 }
Alex Martelli41c9f882003-04-22 09:24:48 +00002270 Py_INCREF(result);
Alex Martellia70b1912003-04-22 08:12:33 +00002271 }
2272
Raymond Hettinger3f8caa32007-10-24 01:28:33 +00002273#ifndef SLOW_SUM
2274 /* Fast addition by keeping temporary sums in C instead of new Python objects.
2275 Assumes all inputs are the same type. If the assumption fails, default
2276 to the more general routine.
2277 */
2278 if (PyInt_CheckExact(result)) {
2279 long i_result = PyInt_AS_LONG(result);
2280 Py_DECREF(result);
2281 result = NULL;
2282 while(result == NULL) {
2283 item = PyIter_Next(iter);
2284 if (item == NULL) {
2285 Py_DECREF(iter);
2286 if (PyErr_Occurred())
2287 return NULL;
2288 return PyInt_FromLong(i_result);
2289 }
2290 if (PyInt_CheckExact(item)) {
2291 long b = PyInt_AS_LONG(item);
2292 long x = i_result + b;
2293 if ((x^i_result) >= 0 || (x^b) >= 0) {
2294 i_result = x;
2295 Py_DECREF(item);
2296 continue;
2297 }
2298 }
2299 /* Either overflowed or is not an int. Restore real objects and process normally */
2300 result = PyInt_FromLong(i_result);
2301 temp = PyNumber_Add(result, item);
2302 Py_DECREF(result);
2303 Py_DECREF(item);
2304 result = temp;
2305 if (result == NULL) {
2306 Py_DECREF(iter);
2307 return NULL;
2308 }
2309 }
2310 }
2311
2312 if (PyFloat_CheckExact(result)) {
2313 double f_result = PyFloat_AS_DOUBLE(result);
2314 Py_DECREF(result);
2315 result = NULL;
2316 while(result == NULL) {
2317 item = PyIter_Next(iter);
2318 if (item == NULL) {
2319 Py_DECREF(iter);
2320 if (PyErr_Occurred())
2321 return NULL;
2322 return PyFloat_FromDouble(f_result);
2323 }
2324 if (PyFloat_CheckExact(item)) {
Raymond Hettinger65856602008-05-30 06:37:27 +00002325 PyFPE_START_PROTECT("add", Py_DECREF(item); Py_DECREF(iter); return 0)
Raymond Hettinger3f8caa32007-10-24 01:28:33 +00002326 f_result += PyFloat_AS_DOUBLE(item);
Raymond Hettinger3a8daf52007-10-24 02:05:51 +00002327 PyFPE_END_PROTECT(f_result)
Raymond Hettingera45c4872007-10-25 02:26:58 +00002328 Py_DECREF(item);
Raymond Hettinger3a8daf52007-10-24 02:05:51 +00002329 continue;
2330 }
2331 if (PyInt_CheckExact(item)) {
Raymond Hettinger65856602008-05-30 06:37:27 +00002332 PyFPE_START_PROTECT("add", Py_DECREF(item); Py_DECREF(iter); return 0)
Raymond Hettinger3a8daf52007-10-24 02:05:51 +00002333 f_result += (double)PyInt_AS_LONG(item);
2334 PyFPE_END_PROTECT(f_result)
Raymond Hettingera45c4872007-10-25 02:26:58 +00002335 Py_DECREF(item);
Raymond Hettinger3f8caa32007-10-24 01:28:33 +00002336 continue;
2337 }
2338 result = PyFloat_FromDouble(f_result);
2339 temp = PyNumber_Add(result, item);
2340 Py_DECREF(result);
2341 Py_DECREF(item);
2342 result = temp;
2343 if (result == NULL) {
2344 Py_DECREF(iter);
2345 return NULL;
2346 }
2347 }
2348 }
2349#endif
2350
Alex Martellia70b1912003-04-22 08:12:33 +00002351 for(;;) {
2352 item = PyIter_Next(iter);
2353 if (item == NULL) {
2354 /* error, or end-of-sequence */
2355 if (PyErr_Occurred()) {
2356 Py_DECREF(result);
2357 result = NULL;
2358 }
2359 break;
2360 }
Alex Martellia253e182003-10-25 23:24:14 +00002361 temp = PyNumber_Add(result, item);
Alex Martellia70b1912003-04-22 08:12:33 +00002362 Py_DECREF(result);
2363 Py_DECREF(item);
2364 result = temp;
2365 if (result == NULL)
2366 break;
2367 }
2368 Py_DECREF(iter);
2369 return result;
2370}
2371
2372PyDoc_STRVAR(sum_doc,
Georg Brandl8134d062006-10-12 12:33:07 +00002373"sum(sequence[, start]) -> value\n\
Alex Martellia70b1912003-04-22 08:12:33 +00002374\n\
2375Returns the sum of a sequence of numbers (NOT strings) plus the value\n\
Georg Brandl8134d062006-10-12 12:33:07 +00002376of parameter 'start' (which defaults to 0). When the sequence is\n\
2377empty, returns start.");
Alex Martellia70b1912003-04-22 08:12:33 +00002378
2379
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002380static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002381builtin_isinstance(PyObject *self, PyObject *args)
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002382{
2383 PyObject *inst;
2384 PyObject *cls;
Guido van Rossum823649d2001-03-21 18:40:58 +00002385 int retval;
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002386
Raymond Hettingerea3fdf42002-12-29 16:33:45 +00002387 if (!PyArg_UnpackTuple(args, "isinstance", 2, 2, &inst, &cls))
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002388 return NULL;
Guido van Rossumf5dd9141997-12-02 19:11:45 +00002389
Guido van Rossum823649d2001-03-21 18:40:58 +00002390 retval = PyObject_IsInstance(inst, cls);
2391 if (retval < 0)
Guido van Rossumad991772001-01-12 16:03:05 +00002392 return NULL;
Guido van Rossum77f6a652002-04-03 22:41:51 +00002393 return PyBool_FromLong(retval);
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002394}
2395
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002396PyDoc_STRVAR(isinstance_doc,
Guido van Rossum77f6a652002-04-03 22:41:51 +00002397"isinstance(object, class-or-type-or-tuple) -> bool\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002398\n\
2399Return whether an object is an instance of a class or of a subclass thereof.\n\
Guido van Rossum03290ec2001-10-07 20:54:12 +00002400With a type as second argument, return whether that is the object's type.\n\
2401The form using a tuple, isinstance(x, (A, B, ...)), is a shortcut for\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002402isinstance(x, A) or isinstance(x, B) or ... (etc.).");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002403
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002404
2405static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002406builtin_issubclass(PyObject *self, PyObject *args)
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002407{
2408 PyObject *derived;
2409 PyObject *cls;
2410 int retval;
2411
Raymond Hettingerea3fdf42002-12-29 16:33:45 +00002412 if (!PyArg_UnpackTuple(args, "issubclass", 2, 2, &derived, &cls))
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002413 return NULL;
Guido van Rossum668213d1999-06-16 17:28:37 +00002414
Guido van Rossum823649d2001-03-21 18:40:58 +00002415 retval = PyObject_IsSubclass(derived, cls);
2416 if (retval < 0)
2417 return NULL;
Guido van Rossum77f6a652002-04-03 22:41:51 +00002418 return PyBool_FromLong(retval);
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002419}
2420
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002421PyDoc_STRVAR(issubclass_doc,
Guido van Rossum77f6a652002-04-03 22:41:51 +00002422"issubclass(C, B) -> bool\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002423\n\
Walter Dörwaldd9a6ad32002-12-12 16:41:44 +00002424Return whether class C is a subclass (i.e., a derived class) of class B.\n\
2425When using a tuple as the second argument issubclass(X, (A, B, ...)),\n\
2426is a shortcut for issubclass(X, A) or issubclass(X, B) or ... (etc.).");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002427
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002428
Barry Warsawbd599b52000-08-03 15:45:29 +00002429static PyObject*
2430builtin_zip(PyObject *self, PyObject *args)
2431{
2432 PyObject *ret;
Martin v. Löwisd96ee902006-02-16 14:37:16 +00002433 const Py_ssize_t itemsize = PySequence_Length(args);
2434 Py_ssize_t i;
Tim Peters8572b4f2001-05-06 01:05:02 +00002435 PyObject *itlist; /* tuple of iterators */
Martin v. Löwisd96ee902006-02-16 14:37:16 +00002436 Py_ssize_t len; /* guess at result length */
Barry Warsawbd599b52000-08-03 15:45:29 +00002437
Raymond Hettingereaef6152003-08-02 07:42:57 +00002438 if (itemsize == 0)
2439 return PyList_New(0);
2440
Barry Warsawbd599b52000-08-03 15:45:29 +00002441 /* args must be a tuple */
2442 assert(PyTuple_Check(args));
2443
Tim Peters39a86c22002-05-12 07:19:38 +00002444 /* Guess at result length: the shortest of the input lengths.
2445 If some argument refuses to say, we refuse to guess too, lest
2446 an argument like xrange(sys.maxint) lead us astray.*/
Tim Peters67d687a2002-04-29 21:27:32 +00002447 len = -1; /* unknown */
2448 for (i = 0; i < itemsize; ++i) {
2449 PyObject *item = PyTuple_GET_ITEM(args, i);
Raymond Hettinger4e2f7142007-12-06 00:56:53 +00002450 Py_ssize_t thislen = _PyObject_LengthHint(item, -1);
Tim Peters39a86c22002-05-12 07:19:38 +00002451 if (thislen < 0) {
Tim Peters39a86c22002-05-12 07:19:38 +00002452 len = -1;
2453 break;
2454 }
Tim Peters67d687a2002-04-29 21:27:32 +00002455 else if (len < 0 || thislen < len)
2456 len = thislen;
2457 }
2458
Tim Peters8572b4f2001-05-06 01:05:02 +00002459 /* allocate result list */
Tim Peters67d687a2002-04-29 21:27:32 +00002460 if (len < 0)
2461 len = 10; /* arbitrary */
2462 if ((ret = PyList_New(len)) == NULL)
Barry Warsawbd599b52000-08-03 15:45:29 +00002463 return NULL;
2464
Tim Peters8572b4f2001-05-06 01:05:02 +00002465 /* obtain iterators */
2466 itlist = PyTuple_New(itemsize);
2467 if (itlist == NULL)
2468 goto Fail_ret;
2469 for (i = 0; i < itemsize; ++i) {
2470 PyObject *item = PyTuple_GET_ITEM(args, i);
2471 PyObject *it = PyObject_GetIter(item);
2472 if (it == NULL) {
2473 if (PyErr_ExceptionMatches(PyExc_TypeError))
2474 PyErr_Format(PyExc_TypeError,
Neal Norwitza361bd82006-02-19 19:31:50 +00002475 "zip argument #%zd must support iteration",
Tim Peters8572b4f2001-05-06 01:05:02 +00002476 i+1);
2477 goto Fail_ret_itlist;
Barry Warsawbd599b52000-08-03 15:45:29 +00002478 }
Tim Peters8572b4f2001-05-06 01:05:02 +00002479 PyTuple_SET_ITEM(itlist, i, it);
2480 }
Barry Warsawbd599b52000-08-03 15:45:29 +00002481
Tim Peters8572b4f2001-05-06 01:05:02 +00002482 /* build result into ret list */
Tim Peters67d687a2002-04-29 21:27:32 +00002483 for (i = 0; ; ++i) {
2484 int j;
Tim Peters8572b4f2001-05-06 01:05:02 +00002485 PyObject *next = PyTuple_New(itemsize);
2486 if (!next)
2487 goto Fail_ret_itlist;
2488
Tim Peters67d687a2002-04-29 21:27:32 +00002489 for (j = 0; j < itemsize; j++) {
2490 PyObject *it = PyTuple_GET_ITEM(itlist, j);
Tim Peters8572b4f2001-05-06 01:05:02 +00002491 PyObject *item = PyIter_Next(it);
Barry Warsawbd599b52000-08-03 15:45:29 +00002492 if (!item) {
Tim Peters8572b4f2001-05-06 01:05:02 +00002493 if (PyErr_Occurred()) {
2494 Py_DECREF(ret);
2495 ret = NULL;
Barry Warsawbd599b52000-08-03 15:45:29 +00002496 }
2497 Py_DECREF(next);
Tim Peters8572b4f2001-05-06 01:05:02 +00002498 Py_DECREF(itlist);
Tim Peters67d687a2002-04-29 21:27:32 +00002499 goto Done;
Barry Warsawbd599b52000-08-03 15:45:29 +00002500 }
Tim Peters67d687a2002-04-29 21:27:32 +00002501 PyTuple_SET_ITEM(next, j, item);
Barry Warsawbd599b52000-08-03 15:45:29 +00002502 }
Tim Peters8572b4f2001-05-06 01:05:02 +00002503
Tim Peters67d687a2002-04-29 21:27:32 +00002504 if (i < len)
2505 PyList_SET_ITEM(ret, i, next);
2506 else {
2507 int status = PyList_Append(ret, next);
2508 Py_DECREF(next);
2509 ++len;
2510 if (status < 0)
2511 goto Fail_ret_itlist;
2512 }
Barry Warsawbd599b52000-08-03 15:45:29 +00002513 }
Tim Peters8572b4f2001-05-06 01:05:02 +00002514
Tim Peters67d687a2002-04-29 21:27:32 +00002515Done:
2516 if (ret != NULL && i < len) {
2517 /* The list is too big. */
2518 if (PyList_SetSlice(ret, i, len, NULL) < 0)
2519 return NULL;
2520 }
2521 return ret;
2522
Tim Peters8572b4f2001-05-06 01:05:02 +00002523Fail_ret_itlist:
2524 Py_DECREF(itlist);
2525Fail_ret:
2526 Py_DECREF(ret);
2527 return NULL;
Barry Warsawbd599b52000-08-03 15:45:29 +00002528}
2529
2530
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002531PyDoc_STRVAR(zip_doc,
Barry Warsawbd599b52000-08-03 15:45:29 +00002532"zip(seq1 [, seq2 [...]]) -> [(seq1[0], seq2[0] ...), (...)]\n\
2533\n\
2534Return a list of tuples, where each tuple contains the i-th element\n\
2535from each of the argument sequences. The returned list is truncated\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002536in length to the length of the shortest argument sequence.");
Barry Warsawbd599b52000-08-03 15:45:29 +00002537
2538
Guido van Rossum79f25d91997-04-29 20:08:16 +00002539static PyMethodDef builtin_methods[] = {
Neal Norwitz92e212f2006-04-03 04:48:37 +00002540 {"__import__", (PyCFunction)builtin___import__, METH_VARARGS | METH_KEYWORDS, import_doc},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002541 {"abs", builtin_abs, METH_O, abs_doc},
Raymond Hettinger96229b12005-03-11 06:49:40 +00002542 {"all", builtin_all, METH_O, all_doc},
2543 {"any", builtin_any, METH_O, any_doc},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002544 {"apply", builtin_apply, METH_VARARGS, apply_doc},
Eric Smith3cd81942008-02-22 16:30:22 +00002545 {"bin", builtin_bin, METH_O, bin_doc},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002546 {"callable", builtin_callable, METH_O, callable_doc},
2547 {"chr", builtin_chr, METH_VARARGS, chr_doc},
2548 {"cmp", builtin_cmp, METH_VARARGS, cmp_doc},
2549 {"coerce", builtin_coerce, METH_VARARGS, coerce_doc},
Georg Brandl5240d742007-03-13 20:46:32 +00002550 {"compile", (PyCFunction)builtin_compile, METH_VARARGS | METH_KEYWORDS, compile_doc},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002551 {"delattr", builtin_delattr, METH_VARARGS, delattr_doc},
2552 {"dir", builtin_dir, METH_VARARGS, dir_doc},
2553 {"divmod", builtin_divmod, METH_VARARGS, divmod_doc},
2554 {"eval", builtin_eval, METH_VARARGS, eval_doc},
2555 {"execfile", builtin_execfile, METH_VARARGS, execfile_doc},
2556 {"filter", builtin_filter, METH_VARARGS, filter_doc},
Eric Smitha9f7d622008-02-17 19:46:49 +00002557 {"format", builtin_format, METH_VARARGS, format_doc},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002558 {"getattr", builtin_getattr, METH_VARARGS, getattr_doc},
2559 {"globals", (PyCFunction)builtin_globals, METH_NOARGS, globals_doc},
2560 {"hasattr", builtin_hasattr, METH_VARARGS, hasattr_doc},
2561 {"hash", builtin_hash, METH_O, hash_doc},
2562 {"hex", builtin_hex, METH_O, hex_doc},
2563 {"id", builtin_id, METH_O, id_doc},
2564 {"input", builtin_input, METH_VARARGS, input_doc},
2565 {"intern", builtin_intern, METH_VARARGS, intern_doc},
2566 {"isinstance", builtin_isinstance, METH_VARARGS, isinstance_doc},
2567 {"issubclass", builtin_issubclass, METH_VARARGS, issubclass_doc},
2568 {"iter", builtin_iter, METH_VARARGS, iter_doc},
2569 {"len", builtin_len, METH_O, len_doc},
2570 {"locals", (PyCFunction)builtin_locals, METH_NOARGS, locals_doc},
2571 {"map", builtin_map, METH_VARARGS, map_doc},
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00002572 {"max", (PyCFunction)builtin_max, METH_VARARGS | METH_KEYWORDS, max_doc},
2573 {"min", (PyCFunction)builtin_min, METH_VARARGS | METH_KEYWORDS, min_doc},
Georg Brandl28e08732008-04-30 19:47:09 +00002574 {"next", builtin_next, METH_VARARGS, next_doc},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002575 {"oct", builtin_oct, METH_O, oct_doc},
Neal Norwitzc4edb0e2006-05-02 04:43:14 +00002576 {"open", (PyCFunction)builtin_open, METH_VARARGS | METH_KEYWORDS, open_doc},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002577 {"ord", builtin_ord, METH_O, ord_doc},
2578 {"pow", builtin_pow, METH_VARARGS, pow_doc},
Eric Smith7c478942008-03-18 23:45:49 +00002579 {"print", (PyCFunction)builtin_print, METH_VARARGS | METH_KEYWORDS, print_doc},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002580 {"range", builtin_range, METH_VARARGS, range_doc},
2581 {"raw_input", builtin_raw_input, METH_VARARGS, raw_input_doc},
2582 {"reduce", builtin_reduce, METH_VARARGS, reduce_doc},
2583 {"reload", builtin_reload, METH_O, reload_doc},
2584 {"repr", builtin_repr, METH_O, repr_doc},
Georg Brandlccadf842006-03-31 18:54:53 +00002585 {"round", (PyCFunction)builtin_round, METH_VARARGS | METH_KEYWORDS, round_doc},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002586 {"setattr", builtin_setattr, METH_VARARGS, setattr_doc},
Raymond Hettinger64958a12003-12-17 20:43:33 +00002587 {"sorted", (PyCFunction)builtin_sorted, METH_VARARGS | METH_KEYWORDS, sorted_doc},
Alex Martellia70b1912003-04-22 08:12:33 +00002588 {"sum", builtin_sum, METH_VARARGS, sum_doc},
Martin v. Löwis339d0f72001-08-17 18:39:25 +00002589#ifdef Py_USING_UNICODE
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002590 {"unichr", builtin_unichr, METH_VARARGS, unichr_doc},
Martin v. Löwis339d0f72001-08-17 18:39:25 +00002591#endif
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002592 {"vars", builtin_vars, METH_VARARGS, vars_doc},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002593 {"zip", builtin_zip, METH_VARARGS, zip_doc},
Guido van Rossumc02e15c1991-12-16 13:03:00 +00002594 {NULL, NULL},
Guido van Rossum3f5da241990-12-20 15:06:42 +00002595};
2596
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002597PyDoc_STRVAR(builtin_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002598"Built-in functions, exceptions, and other objects.\n\
2599\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002600Noteworthy: None is the `nil' object; Ellipsis represents `...' in slices.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002601
Guido van Rossum25ce5661997-08-02 03:10:38 +00002602PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002603_PyBuiltin_Init(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +00002604{
Fred Drake5550de32000-06-20 04:54:19 +00002605 PyObject *mod, *dict, *debug;
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002606 mod = Py_InitModule4("__builtin__", builtin_methods,
2607 builtin_doc, (PyObject *)NULL,
2608 PYTHON_API_VERSION);
Guido van Rossum25ce5661997-08-02 03:10:38 +00002609 if (mod == NULL)
2610 return NULL;
2611 dict = PyModule_GetDict(mod);
Tim Peters4b7625e2001-09-13 21:37:17 +00002612
Tim Peters7571a0f2003-03-23 17:52:28 +00002613#ifdef Py_TRACE_REFS
2614 /* __builtin__ exposes a number of statically allocated objects
2615 * that, before this code was added in 2.3, never showed up in
2616 * the list of "all objects" maintained by Py_TRACE_REFS. As a
2617 * result, programs leaking references to None and False (etc)
2618 * couldn't be diagnosed by examining sys.getobjects(0).
2619 */
2620#define ADD_TO_ALL(OBJECT) _Py_AddToAllObjects((PyObject *)(OBJECT), 0)
2621#else
2622#define ADD_TO_ALL(OBJECT) (void)0
2623#endif
2624
Tim Peters4b7625e2001-09-13 21:37:17 +00002625#define SETBUILTIN(NAME, OBJECT) \
Tim Peters7571a0f2003-03-23 17:52:28 +00002626 if (PyDict_SetItemString(dict, NAME, (PyObject *)OBJECT) < 0) \
2627 return NULL; \
2628 ADD_TO_ALL(OBJECT)
Tim Peters4b7625e2001-09-13 21:37:17 +00002629
2630 SETBUILTIN("None", Py_None);
2631 SETBUILTIN("Ellipsis", Py_Ellipsis);
2632 SETBUILTIN("NotImplemented", Py_NotImplemented);
Guido van Rossum77f6a652002-04-03 22:41:51 +00002633 SETBUILTIN("False", Py_False);
2634 SETBUILTIN("True", Py_True);
Neal Norwitz32a7e7f2002-05-31 19:58:02 +00002635 SETBUILTIN("basestring", &PyBaseString_Type);
Guido van Rossum77f6a652002-04-03 22:41:51 +00002636 SETBUILTIN("bool", &PyBool_Type);
Travis E. Oliphant3781aef2008-03-18 04:44:57 +00002637 /* SETBUILTIN("memoryview", &PyMemoryView_Type); */
Christian Heimes3497f942008-05-26 12:29:14 +00002638 SETBUILTIN("bytearray", &PyByteArray_Type);
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002639 SETBUILTIN("bytes", &PyString_Type);
Guido van Rossumbea18cc2002-06-14 20:41:17 +00002640 SETBUILTIN("buffer", &PyBuffer_Type);
Tim Peters4b7625e2001-09-13 21:37:17 +00002641 SETBUILTIN("classmethod", &PyClassMethod_Type);
Tim Peters6d6c1a32001-08-02 04:15:00 +00002642#ifndef WITHOUT_COMPLEX
Tim Peters4b7625e2001-09-13 21:37:17 +00002643 SETBUILTIN("complex", &PyComplex_Type);
Tim Peters6d6c1a32001-08-02 04:15:00 +00002644#endif
Tim Petersa427a2b2001-10-29 22:25:45 +00002645 SETBUILTIN("dict", &PyDict_Type);
Tim Peters67d687a2002-04-29 21:27:32 +00002646 SETBUILTIN("enumerate", &PyEnum_Type);
Neal Norwitzc4edb0e2006-05-02 04:43:14 +00002647 SETBUILTIN("file", &PyFile_Type);
Tim Peters4b7625e2001-09-13 21:37:17 +00002648 SETBUILTIN("float", &PyFloat_Type);
Raymond Hettingera690a992003-11-16 16:17:49 +00002649 SETBUILTIN("frozenset", &PyFrozenSet_Type);
Tim Peters4b7625e2001-09-13 21:37:17 +00002650 SETBUILTIN("property", &PyProperty_Type);
2651 SETBUILTIN("int", &PyInt_Type);
2652 SETBUILTIN("list", &PyList_Type);
2653 SETBUILTIN("long", &PyLong_Type);
2654 SETBUILTIN("object", &PyBaseObject_Type);
Raymond Hettinger85c20a42003-11-06 14:06:48 +00002655 SETBUILTIN("reversed", &PyReversed_Type);
Raymond Hettingera690a992003-11-16 16:17:49 +00002656 SETBUILTIN("set", &PySet_Type);
Guido van Rossumbea18cc2002-06-14 20:41:17 +00002657 SETBUILTIN("slice", &PySlice_Type);
Tim Peters4b7625e2001-09-13 21:37:17 +00002658 SETBUILTIN("staticmethod", &PyStaticMethod_Type);
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002659 SETBUILTIN("str", &PyString_Type);
Tim Peters4b7625e2001-09-13 21:37:17 +00002660 SETBUILTIN("super", &PySuper_Type);
2661 SETBUILTIN("tuple", &PyTuple_Type);
2662 SETBUILTIN("type", &PyType_Type);
Raymond Hettingerc4c453f2002-06-05 23:12:45 +00002663 SETBUILTIN("xrange", &PyRange_Type);
Martin v. Löwis339d0f72001-08-17 18:39:25 +00002664#ifdef Py_USING_UNICODE
Tim Peters4b7625e2001-09-13 21:37:17 +00002665 SETBUILTIN("unicode", &PyUnicode_Type);
Martin v. Löwis339d0f72001-08-17 18:39:25 +00002666#endif
Guido van Rossum77f6a652002-04-03 22:41:51 +00002667 debug = PyBool_FromLong(Py_OptimizeFlag == 0);
Fred Drake5550de32000-06-20 04:54:19 +00002668 if (PyDict_SetItemString(dict, "__debug__", debug) < 0) {
2669 Py_XDECREF(debug);
Guido van Rossum25ce5661997-08-02 03:10:38 +00002670 return NULL;
Fred Drake5550de32000-06-20 04:54:19 +00002671 }
2672 Py_XDECREF(debug);
Barry Warsaw757af0e1997-08-29 22:13:51 +00002673
Guido van Rossum25ce5661997-08-02 03:10:38 +00002674 return mod;
Tim Peters7571a0f2003-03-23 17:52:28 +00002675#undef ADD_TO_ALL
Tim Peters4b7625e2001-09-13 21:37:17 +00002676#undef SETBUILTIN
Guido van Rossum3f5da241990-12-20 15:06:42 +00002677}
2678
Guido van Rossume77a7571993-11-03 15:01:26 +00002679/* Helper for filter(): filter a tuple through a function */
Guido van Rossum12d12c51993-10-26 17:58:25 +00002680
Guido van Rossum79f25d91997-04-29 20:08:16 +00002681static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002682filtertuple(PyObject *func, PyObject *tuple)
Guido van Rossum12d12c51993-10-26 17:58:25 +00002683{
Guido van Rossum79f25d91997-04-29 20:08:16 +00002684 PyObject *result;
Martin v. Löwis18e16552006-02-15 17:27:45 +00002685 Py_ssize_t i, j;
2686 Py_ssize_t len = PyTuple_Size(tuple);
Guido van Rossum12d12c51993-10-26 17:58:25 +00002687
Guido van Rossumb7b45621995-08-04 04:07:45 +00002688 if (len == 0) {
Walter Dörwaldc3da83f2003-02-04 20:24:45 +00002689 if (PyTuple_CheckExact(tuple))
2690 Py_INCREF(tuple);
2691 else
2692 tuple = PyTuple_New(0);
Guido van Rossumb7b45621995-08-04 04:07:45 +00002693 return tuple;
2694 }
2695
Guido van Rossum79f25d91997-04-29 20:08:16 +00002696 if ((result = PyTuple_New(len)) == NULL)
Guido van Rossum2586bf01993-11-01 16:21:44 +00002697 return NULL;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002698
Guido van Rossum12d12c51993-10-26 17:58:25 +00002699 for (i = j = 0; i < len; ++i) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002700 PyObject *item, *good;
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002701 int ok;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002702
Walter Dörwald8dd19322003-02-10 17:36:40 +00002703 if (tuple->ob_type->tp_as_sequence &&
2704 tuple->ob_type->tp_as_sequence->sq_item) {
2705 item = tuple->ob_type->tp_as_sequence->sq_item(tuple, i);
Walter Dörwaldc58a3a12003-08-18 18:28:45 +00002706 if (item == NULL)
2707 goto Fail_1;
Walter Dörwald8dd19322003-02-10 17:36:40 +00002708 } else {
Alex Martellia9b9c9f2003-04-23 13:34:35 +00002709 PyErr_SetString(PyExc_TypeError, "filter(): unsubscriptable tuple");
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002710 goto Fail_1;
Walter Dörwald8dd19322003-02-10 17:36:40 +00002711 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002712 if (func == Py_None) {
2713 Py_INCREF(item);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002714 good = item;
2715 }
2716 else {
Raymond Hettinger8ae46892003-10-12 19:09:37 +00002717 PyObject *arg = PyTuple_Pack(1, item);
Walter Dörwaldc58a3a12003-08-18 18:28:45 +00002718 if (arg == NULL) {
2719 Py_DECREF(item);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002720 goto Fail_1;
Walter Dörwaldc58a3a12003-08-18 18:28:45 +00002721 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002722 good = PyEval_CallObject(func, arg);
2723 Py_DECREF(arg);
Walter Dörwaldc58a3a12003-08-18 18:28:45 +00002724 if (good == NULL) {
2725 Py_DECREF(item);
Guido van Rossum12d12c51993-10-26 17:58:25 +00002726 goto Fail_1;
Walter Dörwaldc58a3a12003-08-18 18:28:45 +00002727 }
Guido van Rossum12d12c51993-10-26 17:58:25 +00002728 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002729 ok = PyObject_IsTrue(good);
2730 Py_DECREF(good);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002731 if (ok) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002732 if (PyTuple_SetItem(result, j++, item) < 0)
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002733 goto Fail_1;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002734 }
Walter Dörwaldc58a3a12003-08-18 18:28:45 +00002735 else
2736 Py_DECREF(item);
Guido van Rossum12d12c51993-10-26 17:58:25 +00002737 }
2738
Tim Peters4324aa32001-05-28 22:30:08 +00002739 if (_PyTuple_Resize(&result, j) < 0)
Guido van Rossum12d12c51993-10-26 17:58:25 +00002740 return NULL;
2741
Guido van Rossum12d12c51993-10-26 17:58:25 +00002742 return result;
2743
Guido van Rossum12d12c51993-10-26 17:58:25 +00002744Fail_1:
Guido van Rossum79f25d91997-04-29 20:08:16 +00002745 Py_DECREF(result);
Guido van Rossum12d12c51993-10-26 17:58:25 +00002746 return NULL;
2747}
2748
2749
Guido van Rossume77a7571993-11-03 15:01:26 +00002750/* Helper for filter(): filter a string through a function */
Guido van Rossum12d12c51993-10-26 17:58:25 +00002751
Guido van Rossum79f25d91997-04-29 20:08:16 +00002752static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002753filterstring(PyObject *func, PyObject *strobj)
Guido van Rossum12d12c51993-10-26 17:58:25 +00002754{
Guido van Rossum79f25d91997-04-29 20:08:16 +00002755 PyObject *result;
Martin v. Löwis18e16552006-02-15 17:27:45 +00002756 Py_ssize_t i, j;
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002757 Py_ssize_t len = PyString_Size(strobj);
Martin v. Löwis18e16552006-02-15 17:27:45 +00002758 Py_ssize_t outlen = len;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002759
Guido van Rossum79f25d91997-04-29 20:08:16 +00002760 if (func == Py_None) {
Walter Dörwald1918f772003-02-10 13:19:13 +00002761 /* If it's a real string we can return the original,
2762 * as no character is ever false and __getitem__
2763 * does return this character. If it's a subclass
2764 * we must go through the __getitem__ loop */
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002765 if (PyString_CheckExact(strobj)) {
Walter Dörwaldc3da83f2003-02-04 20:24:45 +00002766 Py_INCREF(strobj);
Walter Dörwald1918f772003-02-10 13:19:13 +00002767 return strobj;
2768 }
Guido van Rossum12d12c51993-10-26 17:58:25 +00002769 }
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002770 if ((result = PyString_FromStringAndSize(NULL, len)) == NULL)
Guido van Rossum2586bf01993-11-01 16:21:44 +00002771 return NULL;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002772
Guido van Rossum12d12c51993-10-26 17:58:25 +00002773 for (i = j = 0; i < len; ++i) {
Walter Dörwald1918f772003-02-10 13:19:13 +00002774 PyObject *item;
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002775 int ok;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002776
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002777 item = (*strobj->ob_type->tp_as_sequence->sq_item)(strobj, i);
2778 if (item == NULL)
2779 goto Fail_1;
Walter Dörwald1918f772003-02-10 13:19:13 +00002780 if (func==Py_None) {
2781 ok = 1;
2782 } else {
2783 PyObject *arg, *good;
Raymond Hettinger8ae46892003-10-12 19:09:37 +00002784 arg = PyTuple_Pack(1, item);
Walter Dörwald1918f772003-02-10 13:19:13 +00002785 if (arg == NULL) {
2786 Py_DECREF(item);
2787 goto Fail_1;
2788 }
2789 good = PyEval_CallObject(func, arg);
2790 Py_DECREF(arg);
2791 if (good == NULL) {
2792 Py_DECREF(item);
2793 goto Fail_1;
2794 }
2795 ok = PyObject_IsTrue(good);
2796 Py_DECREF(good);
Tim Peters388ed082001-04-07 20:34:48 +00002797 }
Walter Dörwald903f1e02003-02-04 16:28:00 +00002798 if (ok) {
Martin v. Löwisd96ee902006-02-16 14:37:16 +00002799 Py_ssize_t reslen;
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002800 if (!PyString_Check(item)) {
Walter Dörwald903f1e02003-02-04 16:28:00 +00002801 PyErr_SetString(PyExc_TypeError, "can't filter str to str:"
2802 " __getitem__ returned different type");
2803 Py_DECREF(item);
2804 goto Fail_1;
2805 }
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002806 reslen = PyString_GET_SIZE(item);
Walter Dörwald903f1e02003-02-04 16:28:00 +00002807 if (reslen == 1) {
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002808 PyString_AS_STRING(result)[j++] =
2809 PyString_AS_STRING(item)[0];
Walter Dörwald903f1e02003-02-04 16:28:00 +00002810 } else {
2811 /* do we need more space? */
Gregory P. Smith9d534572008-06-11 07:41:16 +00002812 Py_ssize_t need = j;
2813
2814 /* calculate space requirements while checking for overflow */
2815 if (need > PY_SSIZE_T_MAX - reslen) {
2816 Py_DECREF(item);
2817 goto Fail_1;
2818 }
2819
2820 need += reslen;
2821
2822 if (need > PY_SSIZE_T_MAX - len) {
2823 Py_DECREF(item);
2824 goto Fail_1;
2825 }
2826
2827 need += len;
2828
2829 if (need <= i) {
2830 Py_DECREF(item);
2831 goto Fail_1;
2832 }
2833
2834 need = need - i - 1;
2835
2836 assert(need >= 0);
2837 assert(outlen >= 0);
2838
Walter Dörwald903f1e02003-02-04 16:28:00 +00002839 if (need > outlen) {
2840 /* overallocate, to avoid reallocations */
Gregory P. Smith9d534572008-06-11 07:41:16 +00002841 if (outlen > PY_SSIZE_T_MAX / 2) {
2842 Py_DECREF(item);
2843 return NULL;
2844 }
2845
2846 if (need<2*outlen) {
Walter Dörwald903f1e02003-02-04 16:28:00 +00002847 need = 2*outlen;
Gregory P. Smith9d534572008-06-11 07:41:16 +00002848 }
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002849 if (_PyString_Resize(&result, need)) {
Walter Dörwald903f1e02003-02-04 16:28:00 +00002850 Py_DECREF(item);
2851 return NULL;
2852 }
2853 outlen = need;
2854 }
2855 memcpy(
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002856 PyString_AS_STRING(result) + j,
2857 PyString_AS_STRING(item),
Walter Dörwald903f1e02003-02-04 16:28:00 +00002858 reslen
2859 );
2860 j += reslen;
2861 }
2862 }
Tim Peters388ed082001-04-07 20:34:48 +00002863 Py_DECREF(item);
Guido van Rossum12d12c51993-10-26 17:58:25 +00002864 }
2865
Walter Dörwald903f1e02003-02-04 16:28:00 +00002866 if (j < outlen)
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002867 _PyString_Resize(&result, j);
Guido van Rossum12d12c51993-10-26 17:58:25 +00002868
Guido van Rossum12d12c51993-10-26 17:58:25 +00002869 return result;
2870
Guido van Rossum12d12c51993-10-26 17:58:25 +00002871Fail_1:
Guido van Rossum79f25d91997-04-29 20:08:16 +00002872 Py_DECREF(result);
Guido van Rossum12d12c51993-10-26 17:58:25 +00002873 return NULL;
2874}
Martin v. Löwis8afd7572003-01-25 22:46:11 +00002875
2876#ifdef Py_USING_UNICODE
2877/* Helper for filter(): filter a Unicode object through a function */
2878
2879static PyObject *
2880filterunicode(PyObject *func, PyObject *strobj)
2881{
2882 PyObject *result;
Martin v. Löwis725507b2006-03-07 12:08:51 +00002883 register Py_ssize_t i, j;
Martin v. Löwisd96ee902006-02-16 14:37:16 +00002884 Py_ssize_t len = PyUnicode_GetSize(strobj);
2885 Py_ssize_t outlen = len;
Martin v. Löwis8afd7572003-01-25 22:46:11 +00002886
2887 if (func == Py_None) {
Walter Dörwald1918f772003-02-10 13:19:13 +00002888 /* If it's a real string we can return the original,
2889 * as no character is ever false and __getitem__
2890 * does return this character. If it's a subclass
2891 * we must go through the __getitem__ loop */
2892 if (PyUnicode_CheckExact(strobj)) {
Walter Dörwaldc3da83f2003-02-04 20:24:45 +00002893 Py_INCREF(strobj);
Walter Dörwald1918f772003-02-10 13:19:13 +00002894 return strobj;
2895 }
Martin v. Löwis8afd7572003-01-25 22:46:11 +00002896 }
2897 if ((result = PyUnicode_FromUnicode(NULL, len)) == NULL)
2898 return NULL;
2899
2900 for (i = j = 0; i < len; ++i) {
2901 PyObject *item, *arg, *good;
2902 int ok;
2903
2904 item = (*strobj->ob_type->tp_as_sequence->sq_item)(strobj, i);
2905 if (item == NULL)
2906 goto Fail_1;
Walter Dörwald1918f772003-02-10 13:19:13 +00002907 if (func == Py_None) {
2908 ok = 1;
2909 } else {
Raymond Hettinger8ae46892003-10-12 19:09:37 +00002910 arg = PyTuple_Pack(1, item);
Walter Dörwald1918f772003-02-10 13:19:13 +00002911 if (arg == NULL) {
2912 Py_DECREF(item);
2913 goto Fail_1;
2914 }
2915 good = PyEval_CallObject(func, arg);
2916 Py_DECREF(arg);
2917 if (good == NULL) {
2918 Py_DECREF(item);
2919 goto Fail_1;
2920 }
2921 ok = PyObject_IsTrue(good);
2922 Py_DECREF(good);
Martin v. Löwis8afd7572003-01-25 22:46:11 +00002923 }
Walter Dörwald903f1e02003-02-04 16:28:00 +00002924 if (ok) {
Martin v. Löwisd96ee902006-02-16 14:37:16 +00002925 Py_ssize_t reslen;
Walter Dörwald903f1e02003-02-04 16:28:00 +00002926 if (!PyUnicode_Check(item)) {
Georg Brandl99d7e4e2005-08-31 22:21:15 +00002927 PyErr_SetString(PyExc_TypeError,
Jeremy Hylton1aad9c72003-09-16 03:10:59 +00002928 "can't filter unicode to unicode:"
2929 " __getitem__ returned different type");
Walter Dörwald903f1e02003-02-04 16:28:00 +00002930 Py_DECREF(item);
2931 goto Fail_1;
2932 }
2933 reslen = PyUnicode_GET_SIZE(item);
Georg Brandl99d7e4e2005-08-31 22:21:15 +00002934 if (reslen == 1)
Walter Dörwald903f1e02003-02-04 16:28:00 +00002935 PyUnicode_AS_UNICODE(result)[j++] =
2936 PyUnicode_AS_UNICODE(item)[0];
Jeremy Hylton1aad9c72003-09-16 03:10:59 +00002937 else {
Walter Dörwald903f1e02003-02-04 16:28:00 +00002938 /* do we need more space? */
Martin v. Löwisd96ee902006-02-16 14:37:16 +00002939 Py_ssize_t need = j + reslen + len - i - 1;
Gregory P. Smith9d534572008-06-11 07:41:16 +00002940
2941 /* check that didnt overflow */
2942 if ((j > PY_SSIZE_T_MAX - reslen) ||
2943 ((j + reslen) > PY_SSIZE_T_MAX - len) ||
2944 ((j + reslen + len) < i) ||
2945 ((j + reslen + len - i) <= 0)) {
2946 Py_DECREF(item);
2947 return NULL;
2948 }
2949
2950 assert(need >= 0);
2951 assert(outlen >= 0);
2952
Walter Dörwald903f1e02003-02-04 16:28:00 +00002953 if (need > outlen) {
Georg Brandl99d7e4e2005-08-31 22:21:15 +00002954 /* overallocate,
Jeremy Hylton1aad9c72003-09-16 03:10:59 +00002955 to avoid reallocations */
Gregory P. Smith9d534572008-06-11 07:41:16 +00002956 if (need < 2 * outlen) {
2957 if (outlen > PY_SSIZE_T_MAX / 2) {
2958 Py_DECREF(item);
2959 return NULL;
2960 } else {
2961 need = 2 * outlen;
2962 }
2963 }
2964
Jeremy Hylton364f6be2003-09-16 03:17:16 +00002965 if (PyUnicode_Resize(
2966 &result, need) < 0) {
Walter Dörwald903f1e02003-02-04 16:28:00 +00002967 Py_DECREF(item);
Walter Dörwald531e0002003-02-04 16:57:49 +00002968 goto Fail_1;
Walter Dörwald903f1e02003-02-04 16:28:00 +00002969 }
2970 outlen = need;
2971 }
Jeremy Hylton1aad9c72003-09-16 03:10:59 +00002972 memcpy(PyUnicode_AS_UNICODE(result) + j,
2973 PyUnicode_AS_UNICODE(item),
2974 reslen*sizeof(Py_UNICODE));
Walter Dörwald903f1e02003-02-04 16:28:00 +00002975 j += reslen;
2976 }
2977 }
Martin v. Löwis8afd7572003-01-25 22:46:11 +00002978 Py_DECREF(item);
2979 }
2980
Walter Dörwald903f1e02003-02-04 16:28:00 +00002981 if (j < outlen)
Martin v. Löwis8afd7572003-01-25 22:46:11 +00002982 PyUnicode_Resize(&result, j);
2983
2984 return result;
2985
2986Fail_1:
2987 Py_DECREF(result);
2988 return NULL;
2989}
2990#endif