blob: 0f3392a56f8532fedf861edc229f8f043f709823 [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 Hettinger9c437af2008-06-24 22:46:07 +0000214 return PyNumber_ToBase(v, 2);
Eric Smith3cd81942008-02-22 16:30:22 +0000215}
216
217PyDoc_STRVAR(bin_doc,
218"bin(number) -> string\n\
219\n\
220Return the binary representation of an integer or long integer.");
221
222
223static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000224builtin_callable(PyObject *self, PyObject *v)
Guido van Rossum2d951851994-08-29 12:52:16 +0000225{
Benjamin Peterson9f4f4812008-04-27 03:01:45 +0000226 if (PyErr_WarnPy3k("callable() not supported in 3.x; "
Benjamin Petersonf19a7b92008-04-27 18:40:21 +0000227 "use hasattr(o, '__call__')", 1) < 0)
Neal Norwitzdf25efe2007-05-23 06:58:36 +0000228 return NULL;
Guido van Rossum77f6a652002-04-03 22:41:51 +0000229 return PyBool_FromLong((long)PyCallable_Check(v));
Guido van Rossum2d951851994-08-29 12:52:16 +0000230}
231
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000232PyDoc_STRVAR(callable_doc,
Guido van Rossum77f6a652002-04-03 22:41:51 +0000233"callable(object) -> bool\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000234\n\
235Return whether the object is callable (i.e., some kind of function).\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000236Note that classes are callable, as are instances with a __call__() method.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000237
238
Guido van Rossum79f25d91997-04-29 20:08:16 +0000239static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000240builtin_filter(PyObject *self, PyObject *args)
Guido van Rossum12d12c51993-10-26 17:58:25 +0000241{
Guido van Rossumc7903a12002-08-16 07:04:56 +0000242 PyObject *func, *seq, *result, *it, *arg;
Martin v. Löwisd96ee902006-02-16 14:37:16 +0000243 Py_ssize_t len; /* guess for result list size */
244 register Py_ssize_t j;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000245
Raymond Hettingerea3fdf42002-12-29 16:33:45 +0000246 if (!PyArg_UnpackTuple(args, "filter", 2, 2, &func, &seq))
Guido van Rossum12d12c51993-10-26 17:58:25 +0000247 return NULL;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000248
Tim Peters0e57abf2001-05-02 07:39:38 +0000249 /* Strings and tuples return a result of the same type. */
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000250 if (PyString_Check(seq))
Tim Peters0e57abf2001-05-02 07:39:38 +0000251 return filterstring(func, seq);
Martin v. Löwis8afd7572003-01-25 22:46:11 +0000252#ifdef Py_USING_UNICODE
253 if (PyUnicode_Check(seq))
254 return filterunicode(func, seq);
255#endif
Tim Peters0e57abf2001-05-02 07:39:38 +0000256 if (PyTuple_Check(seq))
257 return filtertuple(func, seq);
258
Georg Brandle35b6572005-07-19 22:20:20 +0000259 /* Pre-allocate argument list tuple. */
260 arg = PyTuple_New(1);
261 if (arg == NULL)
262 return NULL;
263
Tim Peters0e57abf2001-05-02 07:39:38 +0000264 /* Get iterator. */
265 it = PyObject_GetIter(seq);
266 if (it == NULL)
Georg Brandle35b6572005-07-19 22:20:20 +0000267 goto Fail_arg;
Tim Peters0e57abf2001-05-02 07:39:38 +0000268
269 /* Guess a result list size. */
Raymond Hettinger4e2f7142007-12-06 00:56:53 +0000270 len = _PyObject_LengthHint(seq, 8);
Raymond Hettingerb5163702009-02-02 21:50:13 +0000271 if (len == -1)
272 goto Fail_it;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000273
Tim Peters0e57abf2001-05-02 07:39:38 +0000274 /* Get a result list. */
Guido van Rossum79f25d91997-04-29 20:08:16 +0000275 if (PyList_Check(seq) && seq->ob_refcnt == 1) {
Tim Peters0e57abf2001-05-02 07:39:38 +0000276 /* Eww - can modify the list in-place. */
Guido van Rossum79f25d91997-04-29 20:08:16 +0000277 Py_INCREF(seq);
Guido van Rossum12d12c51993-10-26 17:58:25 +0000278 result = seq;
279 }
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000280 else {
Tim Peters0e57abf2001-05-02 07:39:38 +0000281 result = PyList_New(len);
282 if (result == NULL)
283 goto Fail_it;
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000284 }
Guido van Rossum12d12c51993-10-26 17:58:25 +0000285
Tim Peters0e57abf2001-05-02 07:39:38 +0000286 /* Build the result list. */
Tim Petersf4848da2001-05-05 00:14:56 +0000287 j = 0;
288 for (;;) {
Guido van Rossumc7903a12002-08-16 07:04:56 +0000289 PyObject *item;
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000290 int ok;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000291
Tim Peters0e57abf2001-05-02 07:39:38 +0000292 item = PyIter_Next(it);
293 if (item == NULL) {
Tim Petersf4848da2001-05-05 00:14:56 +0000294 if (PyErr_Occurred())
295 goto Fail_result_it;
Tim Peters0e57abf2001-05-02 07:39:38 +0000296 break;
Guido van Rossum2d951851994-08-29 12:52:16 +0000297 }
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000298
Neil Schemenauer68973552003-08-14 20:37:34 +0000299 if (func == (PyObject *)&PyBool_Type || func == Py_None) {
Guido van Rossumc7903a12002-08-16 07:04:56 +0000300 ok = PyObject_IsTrue(item);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000301 }
302 else {
Guido van Rossumc7903a12002-08-16 07:04:56 +0000303 PyObject *good;
304 PyTuple_SET_ITEM(arg, 0, item);
305 good = PyObject_Call(func, arg, NULL);
306 PyTuple_SET_ITEM(arg, 0, NULL);
Guido van Rossum58b68731995-01-10 17:40:55 +0000307 if (good == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000308 Py_DECREF(item);
Tim Peters0e57abf2001-05-02 07:39:38 +0000309 goto Fail_result_it;
Guido van Rossum58b68731995-01-10 17:40:55 +0000310 }
Guido van Rossumc7903a12002-08-16 07:04:56 +0000311 ok = PyObject_IsTrue(good);
312 Py_DECREF(good);
Guido van Rossum12d12c51993-10-26 17:58:25 +0000313 }
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000314 if (ok) {
Tim Peters0e57abf2001-05-02 07:39:38 +0000315 if (j < len)
316 PyList_SET_ITEM(result, j, item);
Guido van Rossum2d951851994-08-29 12:52:16 +0000317 else {
Barry Warsawfa77e091999-01-28 18:49:12 +0000318 int status = PyList_Append(result, item);
Barry Warsawfa77e091999-01-28 18:49:12 +0000319 Py_DECREF(item);
320 if (status < 0)
Tim Peters0e57abf2001-05-02 07:39:38 +0000321 goto Fail_result_it;
Guido van Rossum2d951851994-08-29 12:52:16 +0000322 }
Tim Peters0e57abf2001-05-02 07:39:38 +0000323 ++j;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000324 }
Tim Peters0e57abf2001-05-02 07:39:38 +0000325 else
326 Py_DECREF(item);
Guido van Rossum12d12c51993-10-26 17:58:25 +0000327 }
328
Guido van Rossum12d12c51993-10-26 17:58:25 +0000329
Tim Peters0e57abf2001-05-02 07:39:38 +0000330 /* Cut back result list if len is too big. */
Guido van Rossum79f25d91997-04-29 20:08:16 +0000331 if (j < len && PyList_SetSlice(result, j, len, NULL) < 0)
Tim Peters0e57abf2001-05-02 07:39:38 +0000332 goto Fail_result_it;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000333
Tim Peters3c6b1482001-05-21 08:07:05 +0000334 Py_DECREF(it);
Guido van Rossumc7903a12002-08-16 07:04:56 +0000335 Py_DECREF(arg);
Guido van Rossum12d12c51993-10-26 17:58:25 +0000336 return result;
337
Tim Peters0e57abf2001-05-02 07:39:38 +0000338Fail_result_it:
Guido van Rossum79f25d91997-04-29 20:08:16 +0000339 Py_DECREF(result);
Tim Peters0e57abf2001-05-02 07:39:38 +0000340Fail_it:
341 Py_DECREF(it);
Guido van Rossumc7903a12002-08-16 07:04:56 +0000342Fail_arg:
343 Py_DECREF(arg);
Guido van Rossum12d12c51993-10-26 17:58:25 +0000344 return NULL;
345}
346
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000347PyDoc_STRVAR(filter_doc,
Tim Petersd50e5442002-03-09 00:06:26 +0000348"filter(function or None, sequence) -> list, tuple, or string\n"
349"\n"
350"Return those items of sequence for which function(item) is true. If\n"
351"function is None, return the items that are true. If sequence is a tuple\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000352"or string, return the same type, else return a list.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000353
Guido van Rossum79f25d91997-04-29 20:08:16 +0000354static PyObject *
Eric Smitha9f7d622008-02-17 19:46:49 +0000355builtin_format(PyObject *self, PyObject *args)
356{
357 PyObject *value;
358 PyObject *format_spec = NULL;
359
360 if (!PyArg_ParseTuple(args, "O|O:format", &value, &format_spec))
361 return NULL;
362
363 return PyObject_Format(value, format_spec);
364}
365
366PyDoc_STRVAR(format_doc,
367"format(value[, format_spec]) -> string\n\
368\n\
369Returns value.__format__(format_spec)\n\
370format_spec defaults to \"\"");
371
372static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000373builtin_chr(PyObject *self, PyObject *args)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000374{
375 long x;
376 char s[1];
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000377
Guido van Rossum79f25d91997-04-29 20:08:16 +0000378 if (!PyArg_ParseTuple(args, "l:chr", &x))
Guido van Rossum3f5da241990-12-20 15:06:42 +0000379 return NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000380 if (x < 0 || x >= 256) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000381 PyErr_SetString(PyExc_ValueError,
382 "chr() arg not in range(256)");
Guido van Rossum3f5da241990-12-20 15:06:42 +0000383 return NULL;
384 }
Guido van Rossum6bf62da1997-04-11 20:37:35 +0000385 s[0] = (char)x;
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000386 return PyString_FromStringAndSize(s, 1);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000387}
388
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000389PyDoc_STRVAR(chr_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000390"chr(i) -> character\n\
391\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000392Return a string of one character with ordinal i; 0 <= i < 256.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000393
394
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000395#ifdef Py_USING_UNICODE
Guido van Rossum79f25d91997-04-29 20:08:16 +0000396static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000397builtin_unichr(PyObject *self, PyObject *args)
Guido van Rossum09095f32000-03-10 23:00:52 +0000398{
Amaury Forgeot d'Arc39fd6722008-07-31 21:28:03 +0000399 int x;
Guido van Rossum09095f32000-03-10 23:00:52 +0000400
Amaury Forgeot d'Arc39fd6722008-07-31 21:28:03 +0000401 if (!PyArg_ParseTuple(args, "i:unichr", &x))
Guido van Rossum09095f32000-03-10 23:00:52 +0000402 return NULL;
Fredrik Lundh0dcf67e2001-06-26 20:01:56 +0000403
Marc-André Lemburgcc8764c2002-08-11 12:23:04 +0000404 return PyUnicode_FromOrdinal(x);
Guido van Rossum09095f32000-03-10 23:00:52 +0000405}
406
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000407PyDoc_STRVAR(unichr_doc,
Fred Drake078b24f2000-04-13 02:42:50 +0000408"unichr(i) -> Unicode character\n\
Guido van Rossum09095f32000-03-10 23:00:52 +0000409\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000410Return a Unicode string of one character with ordinal i; 0 <= i <= 0x10ffff.");
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000411#endif
Guido van Rossum09095f32000-03-10 23:00:52 +0000412
413
414static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000415builtin_cmp(PyObject *self, PyObject *args)
Guido van Rossuma9e7dc11992-10-18 18:53:57 +0000416{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000417 PyObject *a, *b;
Guido van Rossum09df08a1998-05-22 00:51:39 +0000418 int c;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000419
Raymond Hettingerea3fdf42002-12-29 16:33:45 +0000420 if (!PyArg_UnpackTuple(args, "cmp", 2, 2, &a, &b))
Guido van Rossuma9e7dc11992-10-18 18:53:57 +0000421 return NULL;
Guido van Rossum09df08a1998-05-22 00:51:39 +0000422 if (PyObject_Cmp(a, b, &c) < 0)
Guido van Rossumc8b6df91997-05-23 00:06:51 +0000423 return NULL;
Guido van Rossum09df08a1998-05-22 00:51:39 +0000424 return PyInt_FromLong((long)c);
Guido van Rossuma9e7dc11992-10-18 18:53:57 +0000425}
426
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000427PyDoc_STRVAR(cmp_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000428"cmp(x, y) -> integer\n\
429\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000430Return negative if x<y, zero if x==y, positive if x>y.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000431
432
Guido van Rossum79f25d91997-04-29 20:08:16 +0000433static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000434builtin_coerce(PyObject *self, PyObject *args)
Guido van Rossum6a00cd81995-01-07 12:39:01 +0000435{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000436 PyObject *v, *w;
437 PyObject *res;
Guido van Rossum5524a591995-01-10 15:26:20 +0000438
Benjamin Peterson9f4f4812008-04-27 03:01:45 +0000439 if (PyErr_WarnPy3k("coerce() not supported in 3.x", 1) < 0)
Neal Norwitzdf25efe2007-05-23 06:58:36 +0000440 return NULL;
441
Raymond Hettingerea3fdf42002-12-29 16:33:45 +0000442 if (!PyArg_UnpackTuple(args, "coerce", 2, 2, &v, &w))
Guido van Rossum5524a591995-01-10 15:26:20 +0000443 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000444 if (PyNumber_Coerce(&v, &w) < 0)
Guido van Rossum04691fc1992-08-12 15:35:34 +0000445 return NULL;
Raymond Hettinger8ae46892003-10-12 19:09:37 +0000446 res = PyTuple_Pack(2, v, w);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000447 Py_DECREF(v);
448 Py_DECREF(w);
Guido van Rossum04691fc1992-08-12 15:35:34 +0000449 return res;
450}
451
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000452PyDoc_STRVAR(coerce_doc,
Martin v. Löwis8d494f32004-08-25 10:42:41 +0000453"coerce(x, y) -> (x1, y1)\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000454\n\
Martin v. Löwis8d494f32004-08-25 10:42:41 +0000455Return a tuple consisting of the two numeric arguments converted to\n\
456a common type, using the same rules as used by arithmetic operations.\n\
457If coercion is not possible, raise TypeError.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000458
Guido van Rossum79f25d91997-04-29 20:08:16 +0000459static PyObject *
Georg Brandl5240d742007-03-13 20:46:32 +0000460builtin_compile(PyObject *self, PyObject *args, PyObject *kwds)
Guido van Rossum5b722181993-03-30 17:46:03 +0000461{
462 char *str;
463 char *filename;
464 char *startstr;
Georg Brandlf2bfd542008-03-29 13:24:23 +0000465 int mode = -1;
Tim Peters6cd6a822001-08-17 22:11:27 +0000466 int dont_inherit = 0;
467 int supplied_flags = 0;
Tim Peters5ba58662001-07-16 02:29:45 +0000468 PyCompilerFlags cf;
Neal Norwitz3a9a3e72005-11-27 20:38:31 +0000469 PyObject *result = NULL, *cmd, *tmp = NULL;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000470 Py_ssize_t length;
Georg Brandl5240d742007-03-13 20:46:32 +0000471 static char *kwlist[] = {"source", "filename", "mode", "flags",
472 "dont_inherit", NULL};
Georg Brandlf2bfd542008-03-29 13:24:23 +0000473 int start[] = {Py_file_input, Py_eval_input, Py_single_input};
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000474
Georg Brandl5240d742007-03-13 20:46:32 +0000475 if (!PyArg_ParseTupleAndKeywords(args, kwds, "Oss|ii:compile",
476 kwlist, &cmd, &filename, &startstr,
477 &supplied_flags, &dont_inherit))
Guido van Rossum5b722181993-03-30 17:46:03 +0000478 return NULL;
Tim Peters6cd6a822001-08-17 22:11:27 +0000479
Just van Rossum3aaf42c2003-02-10 08:21:10 +0000480 cf.cf_flags = supplied_flags;
481
Georg Brandlfc8eef32008-03-28 12:11:56 +0000482 if (supplied_flags &
483 ~(PyCF_MASK | PyCF_MASK_OBSOLETE | PyCF_DONT_IMPLY_DEDENT | PyCF_ONLY_AST))
484 {
485 PyErr_SetString(PyExc_ValueError,
486 "compile(): unrecognised flags");
487 return NULL;
488 }
489 /* XXX Warn if (supplied_flags & PyCF_MASK_OBSOLETE) != 0? */
490
491 if (!dont_inherit) {
492 PyEval_MergeCompilerFlags(&cf);
493 }
494
Georg Brandlf2bfd542008-03-29 13:24:23 +0000495 if (strcmp(startstr, "exec") == 0)
496 mode = 0;
497 else if (strcmp(startstr, "eval") == 0)
498 mode = 1;
499 else if (strcmp(startstr, "single") == 0)
500 mode = 2;
501 else {
502 PyErr_SetString(PyExc_ValueError,
503 "compile() arg 3 must be 'exec', 'eval' or 'single'");
504 return NULL;
505 }
506
Georg Brandlfc8eef32008-03-28 12:11:56 +0000507 if (PyAST_Check(cmd)) {
508 if (supplied_flags & PyCF_ONLY_AST) {
509 Py_INCREF(cmd);
510 result = cmd;
511 }
512 else {
513 PyArena *arena;
514 mod_ty mod;
515
516 arena = PyArena_New();
Georg Brandlf2bfd542008-03-29 13:24:23 +0000517 mod = PyAST_obj2mod(cmd, arena, mode);
Georg Brandlfc8eef32008-03-28 12:11:56 +0000518 if (mod == NULL) {
519 PyArena_Free(arena);
520 return NULL;
521 }
522 result = (PyObject*)PyAST_Compile(mod, filename,
523 &cf, arena);
524 PyArena_Free(arena);
525 }
526 return result;
527 }
528
Just van Rossum3aaf42c2003-02-10 08:21:10 +0000529#ifdef Py_USING_UNICODE
530 if (PyUnicode_Check(cmd)) {
531 tmp = PyUnicode_AsUTF8String(cmd);
532 if (tmp == NULL)
533 return NULL;
534 cmd = tmp;
535 cf.cf_flags |= PyCF_SOURCE_IS_UTF8;
536 }
537#endif
Tim Peters6cd6a822001-08-17 22:11:27 +0000538
Georg Brandlfc8eef32008-03-28 12:11:56 +0000539 if (PyObject_AsReadBuffer(cmd, (const void **)&str, &length))
Neal Norwitz3a9a3e72005-11-27 20:38:31 +0000540 goto cleanup;
Georg Brandlfc8eef32008-03-28 12:11:56 +0000541 if ((size_t)length != strlen(str)) {
542 PyErr_SetString(PyExc_TypeError,
543 "compile() expected string without null bytes");
544 goto cleanup;
Tim Peters6cd6a822001-08-17 22:11:27 +0000545 }
Georg Brandlf2bfd542008-03-29 13:24:23 +0000546 result = Py_CompileStringFlags(str, filename, start[mode], &cf);
Neal Norwitz3a9a3e72005-11-27 20:38:31 +0000547cleanup:
Just van Rossum3aaf42c2003-02-10 08:21:10 +0000548 Py_XDECREF(tmp);
549 return result;
Guido van Rossum5b722181993-03-30 17:46:03 +0000550}
551
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000552PyDoc_STRVAR(compile_doc,
Tim Peters6cd6a822001-08-17 22:11:27 +0000553"compile(source, filename, mode[, flags[, dont_inherit]]) -> code object\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000554\n\
555Compile the source string (a Python module, statement or expression)\n\
556into a code object that can be executed by the exec statement or eval().\n\
557The filename will be used for run-time error messages.\n\
558The mode must be 'exec' to compile a module, 'single' to compile a\n\
Tim Peters6cd6a822001-08-17 22:11:27 +0000559single (interactive) statement, or 'eval' to compile an expression.\n\
560The flags argument, if present, controls which future statements influence\n\
561the compilation of the code.\n\
562The dont_inherit argument, if non-zero, stops the compilation inheriting\n\
563the effects of any future statements in effect in the code calling\n\
564compile; if absent or zero these statements do influence the compilation,\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000565in addition to any features explicitly specified.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000566
Guido van Rossum79f25d91997-04-29 20:08:16 +0000567static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000568builtin_dir(PyObject *self, PyObject *args)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000569{
Tim Peters5d2b77c2001-09-03 05:47:38 +0000570 PyObject *arg = NULL;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000571
Raymond Hettingerea3fdf42002-12-29 16:33:45 +0000572 if (!PyArg_UnpackTuple(args, "dir", 0, 1, &arg))
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000573 return NULL;
Tim Peters7eea37e2001-09-04 22:08:56 +0000574 return PyObject_Dir(arg);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000575}
576
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000577PyDoc_STRVAR(dir_doc,
Tim Peters5d2b77c2001-09-03 05:47:38 +0000578"dir([object]) -> list of strings\n"
579"\n"
Georg Brandl871f1bc2007-03-12 13:17:36 +0000580"If called without an argument, return the names in the current scope.\n"
581"Else, return an alphabetized list of names comprising (some of) the attributes\n"
582"of the given object, and of attributes reachable from it.\n"
583"If the object supplies a method named __dir__, it will be used; otherwise\n"
584"the default dir() logic is used and returns:\n"
585" for a module object: the module's attributes.\n"
586" for a class object: its attributes, and recursively the attributes\n"
587" of its bases.\n"
Georg Brandl3bb15672007-03-13 07:23:16 +0000588" for any other object: its attributes, its class's attributes, and\n"
Georg Brandl871f1bc2007-03-12 13:17:36 +0000589" recursively the attributes of its class's base classes.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000590
Guido van Rossum79f25d91997-04-29 20:08:16 +0000591static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000592builtin_divmod(PyObject *self, PyObject *args)
Guido van Rossum6a00cd81995-01-07 12:39:01 +0000593{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000594 PyObject *v, *w;
Guido van Rossum6a00cd81995-01-07 12:39:01 +0000595
Raymond Hettingerea3fdf42002-12-29 16:33:45 +0000596 if (!PyArg_UnpackTuple(args, "divmod", 2, 2, &v, &w))
Guido van Rossum6a00cd81995-01-07 12:39:01 +0000597 return NULL;
Guido van Rossum09df08a1998-05-22 00:51:39 +0000598 return PyNumber_Divmod(v, w);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000599}
600
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000601PyDoc_STRVAR(divmod_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000602"divmod(x, y) -> (div, mod)\n\
603\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000604Return the tuple ((x-x%y)/y, x%y). Invariant: div*y + mod == x.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000605
606
Guido van Rossum79f25d91997-04-29 20:08:16 +0000607static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000608builtin_eval(PyObject *self, PyObject *args)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000609{
Just van Rossum3aaf42c2003-02-10 08:21:10 +0000610 PyObject *cmd, *result, *tmp = NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000611 PyObject *globals = Py_None, *locals = Py_None;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000612 char *str;
Tim Peters9fa96be2001-08-17 23:04:59 +0000613 PyCompilerFlags cf;
Guido van Rossum590baa41993-11-30 13:40:46 +0000614
Raymond Hettinger214b1c32004-07-02 06:41:07 +0000615 if (!PyArg_UnpackTuple(args, "eval", 1, 3, &cmd, &globals, &locals))
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000616 return NULL;
Raymond Hettinger214b1c32004-07-02 06:41:07 +0000617 if (locals != Py_None && !PyMapping_Check(locals)) {
618 PyErr_SetString(PyExc_TypeError, "locals must be a mapping");
Raymond Hettinger513ffe82004-07-06 13:44:41 +0000619 return NULL;
Raymond Hettinger214b1c32004-07-02 06:41:07 +0000620 }
621 if (globals != Py_None && !PyDict_Check(globals)) {
Georg Brandl99d7e4e2005-08-31 22:21:15 +0000622 PyErr_SetString(PyExc_TypeError, PyMapping_Check(globals) ?
Raymond Hettinger214b1c32004-07-02 06:41:07 +0000623 "globals must be a real dict; try eval(expr, {}, mapping)"
624 : "globals must be a dict");
Raymond Hettinger513ffe82004-07-06 13:44:41 +0000625 return NULL;
Raymond Hettinger214b1c32004-07-02 06:41:07 +0000626 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000627 if (globals == Py_None) {
628 globals = PyEval_GetGlobals();
629 if (locals == Py_None)
630 locals = PyEval_GetLocals();
Guido van Rossum6135a871995-01-09 17:53:26 +0000631 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000632 else if (locals == Py_None)
Guido van Rossum6135a871995-01-09 17:53:26 +0000633 locals = globals;
Tim Peters9fa96be2001-08-17 23:04:59 +0000634
Georg Brandl77c85e62005-09-15 10:46:13 +0000635 if (globals == NULL || locals == NULL) {
636 PyErr_SetString(PyExc_TypeError,
637 "eval must be given globals and locals "
638 "when called without a frame");
639 return NULL;
640 }
641
Guido van Rossum79f25d91997-04-29 20:08:16 +0000642 if (PyDict_GetItemString(globals, "__builtins__") == NULL) {
643 if (PyDict_SetItemString(globals, "__builtins__",
644 PyEval_GetBuiltins()) != 0)
Guido van Rossum6135a871995-01-09 17:53:26 +0000645 return NULL;
646 }
Tim Peters9fa96be2001-08-17 23:04:59 +0000647
Jeremy Hylton15c1c4f2001-07-30 21:50:55 +0000648 if (PyCode_Check(cmd)) {
Jeremy Hylton733c8932001-12-13 19:51:56 +0000649 if (PyCode_GetNumFree((PyCodeObject *)cmd) > 0) {
Jeremy Hylton15c1c4f2001-07-30 21:50:55 +0000650 PyErr_SetString(PyExc_TypeError,
651 "code object passed to eval() may not contain free variables");
652 return NULL;
653 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000654 return PyEval_EvalCode((PyCodeObject *) cmd, globals, locals);
Jeremy Hylton15c1c4f2001-07-30 21:50:55 +0000655 }
Tim Peters9fa96be2001-08-17 23:04:59 +0000656
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000657 if (!PyString_Check(cmd) &&
Marc-André Lemburgd1ba4432000-09-19 21:04:18 +0000658 !PyUnicode_Check(cmd)) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000659 PyErr_SetString(PyExc_TypeError,
Fred Drake661ea262000-10-24 19:57:45 +0000660 "eval() arg 1 must be a string or code object");
Guido van Rossum94390a41992-08-14 15:14:30 +0000661 return NULL;
662 }
Just van Rossum3aaf42c2003-02-10 08:21:10 +0000663 cf.cf_flags = 0;
664
665#ifdef Py_USING_UNICODE
666 if (PyUnicode_Check(cmd)) {
667 tmp = PyUnicode_AsUTF8String(cmd);
668 if (tmp == NULL)
669 return NULL;
670 cmd = tmp;
671 cf.cf_flags |= PyCF_SOURCE_IS_UTF8;
672 }
673#endif
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000674 if (PyString_AsStringAndSize(cmd, &str, NULL)) {
Neal Norwitz3a9a3e72005-11-27 20:38:31 +0000675 Py_XDECREF(tmp);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000676 return NULL;
Neal Norwitz3a9a3e72005-11-27 20:38:31 +0000677 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000678 while (*str == ' ' || *str == '\t')
679 str++;
Tim Peters9fa96be2001-08-17 23:04:59 +0000680
Tim Peters9fa96be2001-08-17 23:04:59 +0000681 (void)PyEval_MergeCompilerFlags(&cf);
Just van Rossum3aaf42c2003-02-10 08:21:10 +0000682 result = PyRun_StringFlags(str, Py_eval_input, globals, locals, &cf);
683 Py_XDECREF(tmp);
684 return result;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000685}
686
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000687PyDoc_STRVAR(eval_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000688"eval(source[, globals[, locals]]) -> value\n\
689\n\
690Evaluate the source in the context of globals and locals.\n\
691The source may be a string representing a Python expression\n\
692or a code object as returned by compile().\n\
Neal Norwitz477ca1c2006-09-05 02:25:41 +0000693The globals must be a dictionary and locals can be any mapping,\n\
Raymond Hettinger214b1c32004-07-02 06:41:07 +0000694defaulting to the current globals and locals.\n\
695If only globals is given, locals defaults to it.\n");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000696
697
Guido van Rossum79f25d91997-04-29 20:08:16 +0000698static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000699builtin_execfile(PyObject *self, PyObject *args)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000700{
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000701 char *filename;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000702 PyObject *globals = Py_None, *locals = Py_None;
703 PyObject *res;
Guido van Rossumb3a639e2001-09-05 13:37:47 +0000704 FILE* fp = NULL;
Tim Peters5ba58662001-07-16 02:29:45 +0000705 PyCompilerFlags cf;
Martin v. Löwis6b3a2c42001-08-08 05:30:36 +0000706 int exists;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000707
Benjamin Peterson9f4f4812008-04-27 03:01:45 +0000708 if (PyErr_WarnPy3k("execfile() not supported in 3.x; use exec()",
Benjamin Petersonf19a7b92008-04-27 18:40:21 +0000709 1) < 0)
Neal Norwitzdf25efe2007-05-23 06:58:36 +0000710 return NULL;
711
Raymond Hettinger66bd2332004-08-02 08:30:07 +0000712 if (!PyArg_ParseTuple(args, "s|O!O:execfile",
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000713 &filename,
Guido van Rossum79f25d91997-04-29 20:08:16 +0000714 &PyDict_Type, &globals,
Raymond Hettinger66bd2332004-08-02 08:30:07 +0000715 &locals))
Guido van Rossum0f61f8a1992-02-25 18:55:05 +0000716 return NULL;
Raymond Hettinger66bd2332004-08-02 08:30:07 +0000717 if (locals != Py_None && !PyMapping_Check(locals)) {
718 PyErr_SetString(PyExc_TypeError, "locals must be a mapping");
719 return NULL;
720 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000721 if (globals == Py_None) {
722 globals = PyEval_GetGlobals();
723 if (locals == Py_None)
724 locals = PyEval_GetLocals();
Guido van Rossum6135a871995-01-09 17:53:26 +0000725 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000726 else if (locals == Py_None)
Guido van Rossum6135a871995-01-09 17:53:26 +0000727 locals = globals;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000728 if (PyDict_GetItemString(globals, "__builtins__") == NULL) {
729 if (PyDict_SetItemString(globals, "__builtins__",
730 PyEval_GetBuiltins()) != 0)
Guido van Rossum6135a871995-01-09 17:53:26 +0000731 return NULL;
732 }
Martin v. Löwis6b3a2c42001-08-08 05:30:36 +0000733
734 exists = 0;
735 /* Test for existence or directory. */
Martin v. Löwis3484a182002-03-09 12:07:51 +0000736#if defined(PLAN9)
737 {
738 Dir *d;
739
740 if ((d = dirstat(filename))!=nil) {
741 if(d->mode & DMDIR)
742 werrstr("is a directory");
743 else
744 exists = 1;
745 free(d);
746 }
Martin v. Löwis6b3a2c42001-08-08 05:30:36 +0000747 }
Martin v. Löwis3484a182002-03-09 12:07:51 +0000748#elif defined(RISCOS)
Guido van Rossume2ae77b2001-10-24 20:42:55 +0000749 if (object_exists(filename)) {
750 if (isdir(filename))
751 errno = EISDIR;
752 else
753 exists = 1;
754 }
Martin v. Löwis3484a182002-03-09 12:07:51 +0000755#else /* standard Posix */
756 {
757 struct stat s;
758 if (stat(filename, &s) == 0) {
759 if (S_ISDIR(s.st_mode))
Andrew MacIntyreda4d6cb2004-03-29 11:53:38 +0000760# if defined(PYOS_OS2) && defined(PYCC_VACPP)
Martin v. Löwis3484a182002-03-09 12:07:51 +0000761 errno = EOS2ERR;
762# else
763 errno = EISDIR;
764# endif
765 else
766 exists = 1;
767 }
768 }
769#endif
Martin v. Löwis6b3a2c42001-08-08 05:30:36 +0000770
771 if (exists) {
772 Py_BEGIN_ALLOW_THREADS
Jack Jansen7b8c7542002-04-14 20:12:41 +0000773 fp = fopen(filename, "r" PY_STDIOTEXTMODE);
Martin v. Löwis6b3a2c42001-08-08 05:30:36 +0000774 Py_END_ALLOW_THREADS
775
776 if (fp == NULL) {
777 exists = 0;
778 }
779 }
780
781 if (!exists) {
Peter Schneider-Kamp4c013422002-08-27 16:58:00 +0000782 PyErr_SetFromErrnoWithFilename(PyExc_IOError, filename);
Guido van Rossum0f61f8a1992-02-25 18:55:05 +0000783 return NULL;
784 }
Tim Peters5ba58662001-07-16 02:29:45 +0000785 cf.cf_flags = 0;
786 if (PyEval_MergeCompilerFlags(&cf))
Tim Peters748b8bb2001-04-28 08:20:22 +0000787 res = PyRun_FileExFlags(fp, filename, Py_file_input, globals,
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000788 locals, 1, &cf);
Tim Peters5ba58662001-07-16 02:29:45 +0000789 else
Tim Peters748b8bb2001-04-28 08:20:22 +0000790 res = PyRun_FileEx(fp, filename, Py_file_input, globals,
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000791 locals, 1);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000792 return res;
Guido van Rossum0f61f8a1992-02-25 18:55:05 +0000793}
794
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000795PyDoc_STRVAR(execfile_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000796"execfile(filename[, globals[, locals]])\n\
797\n\
798Read and execute a Python script from a file.\n\
799The globals and locals are dictionaries, defaulting to the current\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000800globals and locals. If only globals is given, locals defaults to it.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000801
802
Guido van Rossum79f25d91997-04-29 20:08:16 +0000803static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000804builtin_getattr(PyObject *self, PyObject *args)
Guido van Rossum33894be1992-01-27 16:53:09 +0000805{
Guido van Rossum950ff291998-06-29 13:38:57 +0000806 PyObject *v, *result, *dflt = NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000807 PyObject *name;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000808
Raymond Hettingerea3fdf42002-12-29 16:33:45 +0000809 if (!PyArg_UnpackTuple(args, "getattr", 2, 3, &v, &name, &dflt))
Guido van Rossum33894be1992-01-27 16:53:09 +0000810 return NULL;
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000811#ifdef Py_USING_UNICODE
Jeremy Hylton0eb11152001-07-30 22:39:31 +0000812 if (PyUnicode_Check(name)) {
813 name = _PyUnicode_AsDefaultEncodedString(name, NULL);
814 if (name == NULL)
815 return NULL;
816 }
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000817#endif
Jeremy Hylton0eb11152001-07-30 22:39:31 +0000818
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000819 if (!PyString_Check(name)) {
Jeremy Hylton0eb11152001-07-30 22:39:31 +0000820 PyErr_SetString(PyExc_TypeError,
Alex Martellia9b9c9f2003-04-23 13:34:35 +0000821 "getattr(): attribute name must be string");
Jeremy Hylton0eb11152001-07-30 22:39:31 +0000822 return NULL;
823 }
Guido van Rossum950ff291998-06-29 13:38:57 +0000824 result = PyObject_GetAttr(v, name);
Guido van Rossumd8923572001-10-16 21:31:32 +0000825 if (result == NULL && dflt != NULL &&
826 PyErr_ExceptionMatches(PyExc_AttributeError))
827 {
Guido van Rossum950ff291998-06-29 13:38:57 +0000828 PyErr_Clear();
829 Py_INCREF(dflt);
830 result = dflt;
831 }
832 return result;
Guido van Rossum9bfef441993-03-29 10:43:31 +0000833}
834
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000835PyDoc_STRVAR(getattr_doc,
Guido van Rossum950ff291998-06-29 13:38:57 +0000836"getattr(object, name[, default]) -> value\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000837\n\
Guido van Rossum950ff291998-06-29 13:38:57 +0000838Get a named attribute from an object; getattr(x, 'y') is equivalent to x.y.\n\
839When a default argument is given, it is returned when the attribute doesn't\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000840exist; without it, an exception is raised in that case.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000841
842
Guido van Rossum79f25d91997-04-29 20:08:16 +0000843static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000844builtin_globals(PyObject *self)
Guido van Rossum872537c1995-07-07 22:43:42 +0000845{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000846 PyObject *d;
Guido van Rossum872537c1995-07-07 22:43:42 +0000847
Guido van Rossum79f25d91997-04-29 20:08:16 +0000848 d = PyEval_GetGlobals();
Neal Norwitz43bd4db2006-08-12 01:46:42 +0000849 Py_XINCREF(d);
Guido van Rossum872537c1995-07-07 22:43:42 +0000850 return d;
851}
852
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000853PyDoc_STRVAR(globals_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000854"globals() -> dictionary\n\
855\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000856Return the dictionary containing the current scope's global variables.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000857
858
Guido van Rossum79f25d91997-04-29 20:08:16 +0000859static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000860builtin_hasattr(PyObject *self, PyObject *args)
Guido van Rossum9bfef441993-03-29 10:43:31 +0000861{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000862 PyObject *v;
863 PyObject *name;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000864
Raymond Hettingerea3fdf42002-12-29 16:33:45 +0000865 if (!PyArg_UnpackTuple(args, "hasattr", 2, 2, &v, &name))
Guido van Rossum9bfef441993-03-29 10:43:31 +0000866 return NULL;
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000867#ifdef Py_USING_UNICODE
Jeremy Hylton302b54a2001-07-30 22:45:19 +0000868 if (PyUnicode_Check(name)) {
869 name = _PyUnicode_AsDefaultEncodedString(name, NULL);
870 if (name == NULL)
871 return NULL;
872 }
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000873#endif
Jeremy Hylton302b54a2001-07-30 22:45:19 +0000874
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000875 if (!PyString_Check(name)) {
Jeremy Hylton302b54a2001-07-30 22:45:19 +0000876 PyErr_SetString(PyExc_TypeError,
Alex Martellia9b9c9f2003-04-23 13:34:35 +0000877 "hasattr(): attribute name must be string");
Jeremy Hylton302b54a2001-07-30 22:45:19 +0000878 return NULL;
879 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000880 v = PyObject_GetAttr(v, name);
Guido van Rossum9bfef441993-03-29 10:43:31 +0000881 if (v == NULL) {
Benjamin Petersonb9030f42008-05-12 00:41:23 +0000882 if (!PyErr_ExceptionMatches(PyExc_Exception))
883 return NULL;
884 else {
885 PyErr_Clear();
886 Py_INCREF(Py_False);
887 return Py_False;
888 }
Guido van Rossum9bfef441993-03-29 10:43:31 +0000889 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000890 Py_DECREF(v);
Guido van Rossum09df08a1998-05-22 00:51:39 +0000891 Py_INCREF(Py_True);
892 return Py_True;
Guido van Rossum33894be1992-01-27 16:53:09 +0000893}
894
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000895PyDoc_STRVAR(hasattr_doc,
Guido van Rossum77f6a652002-04-03 22:41:51 +0000896"hasattr(object, name) -> bool\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000897\n\
898Return whether the object has an attribute with the given name.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000899(This is done by calling getattr(object, name) and catching exceptions.)");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000900
901
Guido van Rossum79f25d91997-04-29 20:08:16 +0000902static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000903builtin_id(PyObject *self, PyObject *v)
Guido van Rossum5b722181993-03-30 17:46:03 +0000904{
Guido van Rossum106f2da2000-06-28 21:12:25 +0000905 return PyLong_FromVoidPtr(v);
Guido van Rossum5b722181993-03-30 17:46:03 +0000906}
907
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000908PyDoc_STRVAR(id_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000909"id(object) -> integer\n\
910\n\
911Return the identity of an object. This is guaranteed to be unique among\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000912simultaneously existing objects. (Hint: it's the object's memory address.)");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000913
914
Guido van Rossum79f25d91997-04-29 20:08:16 +0000915static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000916builtin_map(PyObject *self, PyObject *args)
Guido van Rossum12d12c51993-10-26 17:58:25 +0000917{
918 typedef struct {
Tim Peters4e9afdc2001-05-03 23:54:49 +0000919 PyObject *it; /* the iterator object */
920 int saw_StopIteration; /* bool: did the iterator end? */
Guido van Rossum12d12c51993-10-26 17:58:25 +0000921 } sequence;
922
Guido van Rossum79f25d91997-04-29 20:08:16 +0000923 PyObject *func, *result;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000924 sequence *seqs = NULL, *sqp;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000925 Py_ssize_t n, len;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000926 register int i, j;
927
Guido van Rossum79f25d91997-04-29 20:08:16 +0000928 n = PyTuple_Size(args);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000929 if (n < 2) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000930 PyErr_SetString(PyExc_TypeError,
931 "map() requires at least two args");
Guido van Rossum12d12c51993-10-26 17:58:25 +0000932 return NULL;
933 }
934
Guido van Rossum79f25d91997-04-29 20:08:16 +0000935 func = PyTuple_GetItem(args, 0);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000936 n--;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000937
Neal Norwitz53152a12008-02-24 02:20:25 +0000938 if (func == Py_None) {
Benjamin Peterson9f4f4812008-04-27 03:01:45 +0000939 if (PyErr_WarnPy3k("map(None, ...) not supported in 3.x; "
Benjamin Petersonf19a7b92008-04-27 18:40:21 +0000940 "use list(...)", 1) < 0)
Neal Norwitz53152a12008-02-24 02:20:25 +0000941 return NULL;
942 if (n == 1) {
943 /* map(None, S) is the same as list(S). */
944 return PySequence_List(PyTuple_GetItem(args, 1));
945 }
Guido van Rossumfa4ac711998-07-10 17:37:30 +0000946 }
947
Tim Peters4e9afdc2001-05-03 23:54:49 +0000948 /* Get space for sequence descriptors. Must NULL out the iterator
949 * pointers so that jumping to Fail_2 later doesn't see trash.
950 */
Guido van Rossum79f25d91997-04-29 20:08:16 +0000951 if ((seqs = PyMem_NEW(sequence, n)) == NULL) {
952 PyErr_NoMemory();
Tim Peters4e9afdc2001-05-03 23:54:49 +0000953 return NULL;
954 }
955 for (i = 0; i < n; ++i) {
956 seqs[i].it = (PyObject*)NULL;
957 seqs[i].saw_StopIteration = 0;
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000958 }
Guido van Rossum12d12c51993-10-26 17:58:25 +0000959
Tim Peters4e9afdc2001-05-03 23:54:49 +0000960 /* Do a first pass to obtain iterators for the arguments, and set len
961 * to the largest of their lengths.
Tim Peters748b8bb2001-04-28 08:20:22 +0000962 */
Tim Peters4e9afdc2001-05-03 23:54:49 +0000963 len = 0;
964 for (i = 0, sqp = seqs; i < n; ++i, ++sqp) {
965 PyObject *curseq;
Martin v. Löwisd96ee902006-02-16 14:37:16 +0000966 Py_ssize_t curlen;
Guido van Rossumad991772001-01-12 16:03:05 +0000967
Tim Peters4e9afdc2001-05-03 23:54:49 +0000968 /* Get iterator. */
969 curseq = PyTuple_GetItem(args, i+1);
970 sqp->it = PyObject_GetIter(curseq);
971 if (sqp->it == NULL) {
Guido van Rossum12d12c51993-10-26 17:58:25 +0000972 static char errmsg[] =
Tim Peters4e9afdc2001-05-03 23:54:49 +0000973 "argument %d to map() must support iteration";
Guido van Rossum15e33a41997-04-30 19:00:27 +0000974 char errbuf[sizeof(errmsg) + 25];
Jeremy Hylton518ab1c2001-11-28 20:42:20 +0000975 PyOS_snprintf(errbuf, sizeof(errbuf), errmsg, i+2);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000976 PyErr_SetString(PyExc_TypeError, errbuf);
Guido van Rossum12d12c51993-10-26 17:58:25 +0000977 goto Fail_2;
978 }
979
Tim Peters4e9afdc2001-05-03 23:54:49 +0000980 /* Update len. */
Raymond Hettinger4e2f7142007-12-06 00:56:53 +0000981 curlen = _PyObject_LengthHint(curseq, 8);
Raymond Hettinger77f3c872004-01-04 08:54:44 +0000982 if (curlen > len)
983 len = curlen;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000984 }
985
Tim Peters4e9afdc2001-05-03 23:54:49 +0000986 /* Get space for the result list. */
Guido van Rossum79f25d91997-04-29 20:08:16 +0000987 if ((result = (PyObject *) PyList_New(len)) == NULL)
Guido van Rossum12d12c51993-10-26 17:58:25 +0000988 goto Fail_2;
989
Tim Peters4e9afdc2001-05-03 23:54:49 +0000990 /* Iterate over the sequences until all have stopped. */
Guido van Rossum2d951851994-08-29 12:52:16 +0000991 for (i = 0; ; ++i) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000992 PyObject *alist, *item=NULL, *value;
Tim Peters4e9afdc2001-05-03 23:54:49 +0000993 int numactive = 0;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000994
Guido van Rossum79f25d91997-04-29 20:08:16 +0000995 if (func == Py_None && n == 1)
Guido van Rossum32120311995-07-10 13:52:21 +0000996 alist = NULL;
Tim Peters4e9afdc2001-05-03 23:54:49 +0000997 else if ((alist = PyTuple_New(n)) == NULL)
998 goto Fail_1;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000999
1000 for (j = 0, sqp = seqs; j < n; ++j, ++sqp) {
Tim Peters4e9afdc2001-05-03 23:54:49 +00001001 if (sqp->saw_StopIteration) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001002 Py_INCREF(Py_None);
1003 item = Py_None;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001004 }
Guido van Rossum12d12c51993-10-26 17:58:25 +00001005 else {
Tim Peters4e9afdc2001-05-03 23:54:49 +00001006 item = PyIter_Next(sqp->it);
1007 if (item)
1008 ++numactive;
1009 else {
Tim Peters4e9afdc2001-05-03 23:54:49 +00001010 if (PyErr_Occurred()) {
Tim Petersf4848da2001-05-05 00:14:56 +00001011 Py_XDECREF(alist);
1012 goto Fail_1;
Guido van Rossum2d951851994-08-29 12:52:16 +00001013 }
Tim Peters4e9afdc2001-05-03 23:54:49 +00001014 Py_INCREF(Py_None);
1015 item = Py_None;
1016 sqp->saw_StopIteration = 1;
Guido van Rossum2d951851994-08-29 12:52:16 +00001017 }
Guido van Rossum12d12c51993-10-26 17:58:25 +00001018 }
Tim Peters4e9afdc2001-05-03 23:54:49 +00001019 if (alist)
1020 PyTuple_SET_ITEM(alist, j, item);
1021 else
Guido van Rossum2d951851994-08-29 12:52:16 +00001022 break;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001023 }
1024
Guido van Rossum32120311995-07-10 13:52:21 +00001025 if (!alist)
1026 alist = item;
Guido van Rossum2d951851994-08-29 12:52:16 +00001027
Tim Peters4e9afdc2001-05-03 23:54:49 +00001028 if (numactive == 0) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001029 Py_DECREF(alist);
Guido van Rossum2d951851994-08-29 12:52:16 +00001030 break;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001031 }
Guido van Rossum2d951851994-08-29 12:52:16 +00001032
Guido van Rossum79f25d91997-04-29 20:08:16 +00001033 if (func == Py_None)
Guido van Rossum32120311995-07-10 13:52:21 +00001034 value = alist;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001035 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001036 value = PyEval_CallObject(func, alist);
1037 Py_DECREF(alist);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00001038 if (value == NULL)
1039 goto Fail_1;
Guido van Rossum2d951851994-08-29 12:52:16 +00001040 }
1041 if (i >= len) {
Barry Warsawfa77e091999-01-28 18:49:12 +00001042 int status = PyList_Append(result, value);
Barry Warsaw21332871999-01-28 04:21:35 +00001043 Py_DECREF(value);
Barry Warsawfa77e091999-01-28 18:49:12 +00001044 if (status < 0)
1045 goto Fail_1;
Guido van Rossum2d951851994-08-29 12:52:16 +00001046 }
Tim Peters4e9afdc2001-05-03 23:54:49 +00001047 else if (PyList_SetItem(result, i, value) < 0)
1048 goto Fail_1;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001049 }
1050
Guido van Rossumfa4ac711998-07-10 17:37:30 +00001051 if (i < len && PyList_SetSlice(result, i, len, NULL) < 0)
1052 goto Fail_1;
1053
Tim Peters4e9afdc2001-05-03 23:54:49 +00001054 goto Succeed;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001055
Guido van Rossum12d12c51993-10-26 17:58:25 +00001056Fail_1:
Guido van Rossum79f25d91997-04-29 20:08:16 +00001057 Py_DECREF(result);
Guido van Rossum12d12c51993-10-26 17:58:25 +00001058Fail_2:
Tim Peters4e9afdc2001-05-03 23:54:49 +00001059 result = NULL;
1060Succeed:
1061 assert(seqs);
1062 for (i = 0; i < n; ++i)
1063 Py_XDECREF(seqs[i].it);
1064 PyMem_DEL(seqs);
1065 return result;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001066}
1067
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001068PyDoc_STRVAR(map_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001069"map(function, sequence[, sequence, ...]) -> list\n\
1070\n\
1071Return a list of the results of applying the function to the items of\n\
1072the argument sequence(s). If more than one sequence is given, the\n\
1073function is called with an argument list consisting of the corresponding\n\
1074item of each sequence, substituting None for missing values when not all\n\
1075sequences have the same length. If the function is None, return a list of\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001076the items of the sequence (or a list of tuples if more than one sequence).");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001077
1078
Guido van Rossum79f25d91997-04-29 20:08:16 +00001079static PyObject *
Georg Brandl28e08732008-04-30 19:47:09 +00001080builtin_next(PyObject *self, PyObject *args)
1081{
1082 PyObject *it, *res;
1083 PyObject *def = NULL;
1084
1085 if (!PyArg_UnpackTuple(args, "next", 1, 2, &it, &def))
1086 return NULL;
1087 if (!PyIter_Check(it)) {
1088 PyErr_Format(PyExc_TypeError,
1089 "%.200s object is not an iterator",
1090 it->ob_type->tp_name);
1091 return NULL;
1092 }
1093
1094 res = (*it->ob_type->tp_iternext)(it);
1095 if (res != NULL) {
1096 return res;
1097 } else if (def != NULL) {
1098 if (PyErr_Occurred()) {
1099 if (!PyErr_ExceptionMatches(PyExc_StopIteration))
1100 return NULL;
1101 PyErr_Clear();
1102 }
1103 Py_INCREF(def);
1104 return def;
1105 } else if (PyErr_Occurred()) {
1106 return NULL;
1107 } else {
1108 PyErr_SetNone(PyExc_StopIteration);
1109 return NULL;
1110 }
1111}
1112
1113PyDoc_STRVAR(next_doc,
1114"next(iterator[, default])\n\
1115\n\
1116Return the next item from the iterator. If default is given and the iterator\n\
1117is exhausted, it is returned instead of raising StopIteration.");
1118
1119
1120static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001121builtin_setattr(PyObject *self, PyObject *args)
Guido van Rossum33894be1992-01-27 16:53:09 +00001122{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001123 PyObject *v;
1124 PyObject *name;
1125 PyObject *value;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001126
Raymond Hettingerea3fdf42002-12-29 16:33:45 +00001127 if (!PyArg_UnpackTuple(args, "setattr", 3, 3, &v, &name, &value))
Guido van Rossum33894be1992-01-27 16:53:09 +00001128 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001129 if (PyObject_SetAttr(v, name, value) != 0)
Guido van Rossum33894be1992-01-27 16:53:09 +00001130 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001131 Py_INCREF(Py_None);
1132 return Py_None;
Guido van Rossum33894be1992-01-27 16:53:09 +00001133}
1134
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001135PyDoc_STRVAR(setattr_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001136"setattr(object, name, value)\n\
1137\n\
1138Set a named attribute on an object; setattr(x, 'y', v) is equivalent to\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001139``x.y = v''.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001140
1141
Guido van Rossum79f25d91997-04-29 20:08:16 +00001142static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001143builtin_delattr(PyObject *self, PyObject *args)
Guido van Rossum14144fc1994-08-29 12:53:40 +00001144{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001145 PyObject *v;
1146 PyObject *name;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001147
Raymond Hettingerea3fdf42002-12-29 16:33:45 +00001148 if (!PyArg_UnpackTuple(args, "delattr", 2, 2, &v, &name))
Guido van Rossum14144fc1994-08-29 12:53:40 +00001149 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001150 if (PyObject_SetAttr(v, name, (PyObject *)NULL) != 0)
Guido van Rossum14144fc1994-08-29 12:53:40 +00001151 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001152 Py_INCREF(Py_None);
1153 return Py_None;
Guido van Rossum14144fc1994-08-29 12:53:40 +00001154}
1155
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001156PyDoc_STRVAR(delattr_doc,
Guido van Rossumdf12a591998-11-23 22:13:04 +00001157"delattr(object, name)\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001158\n\
1159Delete a named attribute on an object; delattr(x, 'y') is equivalent to\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001160``del x.y''.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001161
1162
Guido van Rossum79f25d91997-04-29 20:08:16 +00001163static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001164builtin_hash(PyObject *self, PyObject *v)
Guido van Rossum9bfef441993-03-29 10:43:31 +00001165{
Guido van Rossum9bfef441993-03-29 10:43:31 +00001166 long x;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001167
Guido van Rossum79f25d91997-04-29 20:08:16 +00001168 x = PyObject_Hash(v);
Guido van Rossum9bfef441993-03-29 10:43:31 +00001169 if (x == -1)
1170 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001171 return PyInt_FromLong(x);
Guido van Rossum9bfef441993-03-29 10:43:31 +00001172}
1173
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001174PyDoc_STRVAR(hash_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001175"hash(object) -> integer\n\
1176\n\
1177Return a hash value for the object. Two objects with the same value have\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001178the same hash value. The reverse is not necessarily true, but likely.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001179
1180
Guido van Rossum79f25d91997-04-29 20:08:16 +00001181static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001182builtin_hex(PyObject *self, PyObject *v)
Guido van Rossum006bcd41991-10-24 14:54:44 +00001183{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001184 PyNumberMethods *nb;
Neil Schemenauer3a313e32004-07-19 16:29:17 +00001185 PyObject *res;
Benjamin Petersonc6d64ec2008-05-17 20:09:42 +00001186
1187 if ((nb = v->ob_type->tp_as_number) == NULL ||
1188 nb->nb_hex == NULL) {
1189 PyErr_SetString(PyExc_TypeError,
1190 "hex() argument can't be converted to hex");
1191 return NULL;
Guido van Rossum006bcd41991-10-24 14:54:44 +00001192 }
Benjamin Petersonc6d64ec2008-05-17 20:09:42 +00001193 res = (*nb->nb_hex)(v);
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001194 if (res && !PyString_Check(res)) {
Benjamin Petersonc6d64ec2008-05-17 20:09:42 +00001195 PyErr_Format(PyExc_TypeError,
1196 "__hex__ returned non-string (type %.200s)",
1197 res->ob_type->tp_name);
1198 Py_DECREF(res);
1199 return NULL;
1200 }
1201 return res;
Guido van Rossum006bcd41991-10-24 14:54:44 +00001202}
1203
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001204PyDoc_STRVAR(hex_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001205"hex(number) -> string\n\
1206\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001207Return the hexadecimal representation of an integer or long integer.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001208
1209
Tim Petersdbd9ba62000-07-09 03:09:57 +00001210static PyObject *builtin_raw_input(PyObject *, PyObject *);
Guido van Rossum3165fe61992-09-25 21:59:05 +00001211
Guido van Rossum79f25d91997-04-29 20:08:16 +00001212static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001213builtin_input(PyObject *self, PyObject *args)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001214{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001215 PyObject *line;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001216 char *str;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001217 PyObject *res;
1218 PyObject *globals, *locals;
Hye-Shik Changff83c2b2004-02-02 13:39:01 +00001219 PyCompilerFlags cf;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001220
1221 line = builtin_raw_input(self, args);
Guido van Rossum3165fe61992-09-25 21:59:05 +00001222 if (line == NULL)
1223 return line;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001224 if (!PyArg_Parse(line, "s;embedded '\\0' in input line", &str))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001225 return NULL;
1226 while (*str == ' ' || *str == '\t')
1227 str++;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001228 globals = PyEval_GetGlobals();
1229 locals = PyEval_GetLocals();
1230 if (PyDict_GetItemString(globals, "__builtins__") == NULL) {
1231 if (PyDict_SetItemString(globals, "__builtins__",
1232 PyEval_GetBuiltins()) != 0)
Guido van Rossum6135a871995-01-09 17:53:26 +00001233 return NULL;
1234 }
Hye-Shik Changff83c2b2004-02-02 13:39:01 +00001235 cf.cf_flags = 0;
1236 PyEval_MergeCompilerFlags(&cf);
1237 res = PyRun_StringFlags(str, Py_eval_input, globals, locals, &cf);
Guido van Rossum79f25d91997-04-29 20:08:16 +00001238 Py_DECREF(line);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001239 return res;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001240}
1241
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001242PyDoc_STRVAR(input_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001243"input([prompt]) -> value\n\
1244\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001245Equivalent to eval(raw_input(prompt)).");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001246
1247
Guido van Rossume8811f81997-02-14 15:48:05 +00001248static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001249builtin_intern(PyObject *self, PyObject *args)
Guido van Rossume8811f81997-02-14 15:48:05 +00001250{
1251 PyObject *s;
Guido van Rossum43713e52000-02-29 13:59:29 +00001252 if (!PyArg_ParseTuple(args, "S:intern", &s))
Guido van Rossume8811f81997-02-14 15:48:05 +00001253 return NULL;
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001254 if (!PyString_CheckExact(s)) {
Jeremy Hylton4c989dd2004-08-07 19:20:05 +00001255 PyErr_SetString(PyExc_TypeError,
1256 "can't intern subclass of string");
1257 return NULL;
1258 }
Guido van Rossume8811f81997-02-14 15:48:05 +00001259 Py_INCREF(s);
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001260 PyString_InternInPlace(&s);
Guido van Rossume8811f81997-02-14 15:48:05 +00001261 return s;
1262}
1263
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001264PyDoc_STRVAR(intern_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001265"intern(string) -> string\n\
1266\n\
1267``Intern'' the given string. This enters the string in the (global)\n\
1268table of interned strings whose purpose is to speed up dictionary lookups.\n\
1269Return the string itself or the previously interned string object with the\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001270same value.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001271
1272
Guido van Rossum79f25d91997-04-29 20:08:16 +00001273static PyObject *
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001274builtin_iter(PyObject *self, PyObject *args)
1275{
1276 PyObject *v, *w = NULL;
1277
Raymond Hettingerea3fdf42002-12-29 16:33:45 +00001278 if (!PyArg_UnpackTuple(args, "iter", 1, 2, &v, &w))
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001279 return NULL;
1280 if (w == NULL)
1281 return PyObject_GetIter(v);
1282 if (!PyCallable_Check(v)) {
1283 PyErr_SetString(PyExc_TypeError,
1284 "iter(v, w): v must be callable");
1285 return NULL;
1286 }
1287 return PyCallIter_New(v, w);
1288}
1289
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001290PyDoc_STRVAR(iter_doc,
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001291"iter(collection) -> iterator\n\
1292iter(callable, sentinel) -> iterator\n\
1293\n\
1294Get an iterator from an object. In the first form, the argument must\n\
1295supply its own iterator, or be a sequence.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001296In the second form, the callable is called until it returns the sentinel.");
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001297
1298
1299static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001300builtin_len(PyObject *self, PyObject *v)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001301{
Martin v. Löwis18e16552006-02-15 17:27:45 +00001302 Py_ssize_t res;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001303
Jeremy Hylton03657cf2000-07-12 13:05:33 +00001304 res = PyObject_Size(v);
Guido van Rossum8ea9f4d1998-06-29 22:26:50 +00001305 if (res < 0 && PyErr_Occurred())
1306 return NULL;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001307 return PyInt_FromSsize_t(res);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001308}
1309
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001310PyDoc_STRVAR(len_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001311"len(object) -> integer\n\
1312\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001313Return the number of items of a sequence or mapping.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001314
1315
Guido van Rossum79f25d91997-04-29 20:08:16 +00001316static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001317builtin_locals(PyObject *self)
Guido van Rossum872537c1995-07-07 22:43:42 +00001318{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001319 PyObject *d;
Guido van Rossum872537c1995-07-07 22:43:42 +00001320
Guido van Rossum79f25d91997-04-29 20:08:16 +00001321 d = PyEval_GetLocals();
Neal Norwitz43bd4db2006-08-12 01:46:42 +00001322 Py_XINCREF(d);
Guido van Rossum872537c1995-07-07 22:43:42 +00001323 return d;
1324}
1325
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001326PyDoc_STRVAR(locals_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001327"locals() -> dictionary\n\
1328\n\
Raymond Hettinger69bf8f32003-01-04 02:16:22 +00001329Update and return a dictionary containing the current scope's local variables.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001330
1331
Guido van Rossum79f25d91997-04-29 20:08:16 +00001332static PyObject *
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001333min_max(PyObject *args, PyObject *kwds, int op)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001334{
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001335 PyObject *v, *it, *item, *val, *maxitem, *maxval, *keyfunc=NULL;
Martin v. Löwisfd39ad42004-08-12 14:42:37 +00001336 const char *name = op == Py_LT ? "min" : "max";
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001337
Guido van Rossum79f25d91997-04-29 20:08:16 +00001338 if (PyTuple_Size(args) > 1)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001339 v = args;
Martin v. Löwisfd39ad42004-08-12 14:42:37 +00001340 else if (!PyArg_UnpackTuple(args, (char *)name, 1, 1, &v))
Guido van Rossum3f5da241990-12-20 15:06:42 +00001341 return NULL;
Tim Peters67d687a2002-04-29 21:27:32 +00001342
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001343 if (kwds != NULL && PyDict_Check(kwds) && PyDict_Size(kwds)) {
1344 keyfunc = PyDict_GetItemString(kwds, "key");
1345 if (PyDict_Size(kwds)!=1 || keyfunc == NULL) {
Georg Brandl99d7e4e2005-08-31 22:21:15 +00001346 PyErr_Format(PyExc_TypeError,
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001347 "%s() got an unexpected keyword argument", name);
1348 return NULL;
1349 }
Guido van Rossum1d9a9ea2008-01-23 20:19:01 +00001350 Py_INCREF(keyfunc);
Georg Brandl99d7e4e2005-08-31 22:21:15 +00001351 }
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001352
Tim Petersc3074532001-05-03 07:00:32 +00001353 it = PyObject_GetIter(v);
Guido van Rossum1d9a9ea2008-01-23 20:19:01 +00001354 if (it == NULL) {
1355 Py_XDECREF(keyfunc);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001356 return NULL;
Guido van Rossum1d9a9ea2008-01-23 20:19:01 +00001357 }
Tim Petersc3074532001-05-03 07:00:32 +00001358
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001359 maxitem = NULL; /* the result */
1360 maxval = NULL; /* the value associated with the result */
Brett Cannon9e635cf2004-12-07 00:25:35 +00001361 while (( item = PyIter_Next(it) )) {
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001362 /* get the value from the key function */
1363 if (keyfunc != NULL) {
1364 val = PyObject_CallFunctionObjArgs(keyfunc, item, NULL);
1365 if (val == NULL)
1366 goto Fail_it_item;
1367 }
1368 /* no key function; the value is the item */
1369 else {
1370 val = item;
1371 Py_INCREF(val);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001372 }
Tim Petersc3074532001-05-03 07:00:32 +00001373
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001374 /* maximum value and item are unset; set them */
1375 if (maxval == NULL) {
1376 maxitem = item;
1377 maxval = val;
1378 }
1379 /* maximum value and item are set; update them as necessary */
Guido van Rossum2d951851994-08-29 12:52:16 +00001380 else {
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001381 int cmp = PyObject_RichCompareBool(val, maxval, op);
1382 if (cmp < 0)
1383 goto Fail_it_item_and_val;
1384 else if (cmp > 0) {
1385 Py_DECREF(maxval);
1386 Py_DECREF(maxitem);
1387 maxval = val;
1388 maxitem = item;
Guido van Rossum53451b32001-01-17 15:47:24 +00001389 }
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001390 else {
1391 Py_DECREF(item);
1392 Py_DECREF(val);
Guido van Rossumc8b6df91997-05-23 00:06:51 +00001393 }
Guido van Rossum2d951851994-08-29 12:52:16 +00001394 }
Guido van Rossum3f5da241990-12-20 15:06:42 +00001395 }
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001396 if (PyErr_Occurred())
1397 goto Fail_it;
1398 if (maxval == NULL) {
Martin v. Löwisfd39ad42004-08-12 14:42:37 +00001399 PyErr_Format(PyExc_ValueError,
1400 "%s() arg is an empty sequence", name);
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001401 assert(maxitem == NULL);
1402 }
1403 else
1404 Py_DECREF(maxval);
Tim Petersc3074532001-05-03 07:00:32 +00001405 Py_DECREF(it);
Guido van Rossum1d9a9ea2008-01-23 20:19:01 +00001406 Py_XDECREF(keyfunc);
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001407 return maxitem;
1408
1409Fail_it_item_and_val:
1410 Py_DECREF(val);
1411Fail_it_item:
1412 Py_DECREF(item);
1413Fail_it:
1414 Py_XDECREF(maxval);
1415 Py_XDECREF(maxitem);
1416 Py_DECREF(it);
Guido van Rossum1d9a9ea2008-01-23 20:19:01 +00001417 Py_XDECREF(keyfunc);
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001418 return NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001419}
1420
Guido van Rossum79f25d91997-04-29 20:08:16 +00001421static PyObject *
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001422builtin_min(PyObject *self, PyObject *args, PyObject *kwds)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001423{
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001424 return min_max(args, kwds, Py_LT);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001425}
1426
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001427PyDoc_STRVAR(min_doc,
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001428"min(iterable[, key=func]) -> value\n\
1429min(a, b, c, ...[, key=func]) -> value\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001430\n\
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001431With a single iterable argument, return its smallest item.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001432With two or more arguments, return the smallest argument.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001433
1434
Guido van Rossum79f25d91997-04-29 20:08:16 +00001435static PyObject *
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001436builtin_max(PyObject *self, PyObject *args, PyObject *kwds)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001437{
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001438 return min_max(args, kwds, Py_GT);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001439}
1440
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001441PyDoc_STRVAR(max_doc,
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001442"max(iterable[, key=func]) -> value\n\
1443max(a, b, c, ...[, key=func]) -> value\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001444\n\
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001445With a single iterable argument, return its largest item.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001446With two or more arguments, return the largest argument.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001447
1448
Guido van Rossum79f25d91997-04-29 20:08:16 +00001449static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001450builtin_oct(PyObject *self, PyObject *v)
Guido van Rossum006bcd41991-10-24 14:54:44 +00001451{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001452 PyNumberMethods *nb;
Neil Schemenauer3a313e32004-07-19 16:29:17 +00001453 PyObject *res;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001454
Guido van Rossum1899c2e1992-09-12 11:09:23 +00001455 if (v == NULL || (nb = v->ob_type->tp_as_number) == NULL ||
1456 nb->nb_oct == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001457 PyErr_SetString(PyExc_TypeError,
Guido van Rossum1899c2e1992-09-12 11:09:23 +00001458 "oct() argument can't be converted to oct");
1459 return NULL;
Guido van Rossum006bcd41991-10-24 14:54:44 +00001460 }
Neil Schemenauer3a313e32004-07-19 16:29:17 +00001461 res = (*nb->nb_oct)(v);
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001462 if (res && !PyString_Check(res)) {
Neil Schemenauer3a313e32004-07-19 16:29:17 +00001463 PyErr_Format(PyExc_TypeError,
1464 "__oct__ returned non-string (type %.200s)",
1465 res->ob_type->tp_name);
1466 Py_DECREF(res);
1467 return NULL;
1468 }
1469 return res;
Guido van Rossum006bcd41991-10-24 14:54:44 +00001470}
1471
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001472PyDoc_STRVAR(oct_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001473"oct(number) -> string\n\
1474\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001475Return the octal representation of an integer or long integer.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001476
1477
Guido van Rossum79f25d91997-04-29 20:08:16 +00001478static PyObject *
Neal Norwitzc4edb0e2006-05-02 04:43:14 +00001479builtin_open(PyObject *self, PyObject *args, PyObject *kwds)
1480{
1481 return PyObject_Call((PyObject*)&PyFile_Type, args, kwds);
1482}
1483
1484PyDoc_STRVAR(open_doc,
1485"open(name[, mode[, buffering]]) -> file object\n\
1486\n\
Skip Montanaro4e3ebe02007-12-08 14:37:43 +00001487Open a file using the file() type, returns a file object. This is the\n\
1488preferred way to open a file.");
Neal Norwitzc4edb0e2006-05-02 04:43:14 +00001489
1490
1491static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001492builtin_ord(PyObject *self, PyObject* obj)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001493{
Guido van Rossum09095f32000-03-10 23:00:52 +00001494 long ord;
Martin v. Löwisd96ee902006-02-16 14:37:16 +00001495 Py_ssize_t size;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001496
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001497 if (PyString_Check(obj)) {
1498 size = PyString_GET_SIZE(obj);
Andrew M. Kuchling34c20cf2000-12-20 14:36:56 +00001499 if (size == 1) {
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001500 ord = (long)((unsigned char)*PyString_AS_STRING(obj));
Andrew M. Kuchling34c20cf2000-12-20 14:36:56 +00001501 return PyInt_FromLong(ord);
1502 }
Christian Heimes3497f942008-05-26 12:29:14 +00001503 } else if (PyByteArray_Check(obj)) {
1504 size = PyByteArray_GET_SIZE(obj);
Christian Heimes1a6387e2008-03-26 12:49:49 +00001505 if (size == 1) {
Christian Heimes3497f942008-05-26 12:29:14 +00001506 ord = (long)((unsigned char)*PyByteArray_AS_STRING(obj));
Christian Heimes1a6387e2008-03-26 12:49:49 +00001507 return PyInt_FromLong(ord);
1508 }
1509
Martin v. Löwis339d0f72001-08-17 18:39:25 +00001510#ifdef Py_USING_UNICODE
Jeremy Hylton394b54d2000-04-12 21:19:47 +00001511 } else if (PyUnicode_Check(obj)) {
1512 size = PyUnicode_GET_SIZE(obj);
Andrew M. Kuchling34c20cf2000-12-20 14:36:56 +00001513 if (size == 1) {
Jeremy Hylton394b54d2000-04-12 21:19:47 +00001514 ord = (long)*PyUnicode_AS_UNICODE(obj);
Andrew M. Kuchling34c20cf2000-12-20 14:36:56 +00001515 return PyInt_FromLong(ord);
1516 }
Martin v. Löwis339d0f72001-08-17 18:39:25 +00001517#endif
Jeremy Hylton394b54d2000-04-12 21:19:47 +00001518 } else {
1519 PyErr_Format(PyExc_TypeError,
Andrew M. Kuchlingf07aad12000-12-23 14:11:28 +00001520 "ord() expected string of length 1, but " \
Jeremy Hylton394b54d2000-04-12 21:19:47 +00001521 "%.200s found", obj->ob_type->tp_name);
Guido van Rossum09095f32000-03-10 23:00:52 +00001522 return NULL;
1523 }
1524
Guido van Rossumad991772001-01-12 16:03:05 +00001525 PyErr_Format(PyExc_TypeError,
Andrew M. Kuchlingf07aad12000-12-23 14:11:28 +00001526 "ord() expected a character, "
Martin v. Löwisd96ee902006-02-16 14:37:16 +00001527 "but string of length %zd found",
Jeremy Hylton394b54d2000-04-12 21:19:47 +00001528 size);
1529 return NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001530}
1531
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001532PyDoc_STRVAR(ord_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001533"ord(c) -> integer\n\
1534\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001535Return the integer ordinal of a one-character string.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001536
1537
Guido van Rossum79f25d91997-04-29 20:08:16 +00001538static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001539builtin_pow(PyObject *self, PyObject *args)
Guido van Rossum6a00cd81995-01-07 12:39:01 +00001540{
Guido van Rossum09df08a1998-05-22 00:51:39 +00001541 PyObject *v, *w, *z = Py_None;
Guido van Rossum6a00cd81995-01-07 12:39:01 +00001542
Raymond Hettingerea3fdf42002-12-29 16:33:45 +00001543 if (!PyArg_UnpackTuple(args, "pow", 2, 3, &v, &w, &z))
Guido van Rossum6a00cd81995-01-07 12:39:01 +00001544 return NULL;
Guido van Rossum09df08a1998-05-22 00:51:39 +00001545 return PyNumber_Power(v, w, z);
Guido van Rossumd4905451991-05-05 20:00:36 +00001546}
1547
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001548PyDoc_STRVAR(pow_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001549"pow(x, y[, z]) -> number\n\
1550\n\
1551With two arguments, equivalent to x**y. With three arguments,\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001552equivalent to (x**y) % z, but may be more efficient (e.g. for longs).");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001553
1554
Eric Smith7c478942008-03-18 23:45:49 +00001555static PyObject *
1556builtin_print(PyObject *self, PyObject *args, PyObject *kwds)
1557{
1558 static char *kwlist[] = {"sep", "end", "file", 0};
1559 static PyObject *dummy_args;
1560 PyObject *sep = NULL, *end = NULL, *file = NULL;
1561 int i, err;
1562
1563 if (dummy_args == NULL) {
1564 if (!(dummy_args = PyTuple_New(0)))
1565 return NULL;
1566 }
1567 if (!PyArg_ParseTupleAndKeywords(dummy_args, kwds, "|OOO:print",
1568 kwlist, &sep, &end, &file))
1569 return NULL;
1570 if (file == NULL || file == Py_None) {
1571 file = PySys_GetObject("stdout");
1572 /* sys.stdout may be None when FILE* stdout isn't connected */
1573 if (file == Py_None)
1574 Py_RETURN_NONE;
1575 }
1576
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001577 if (sep && sep != Py_None && !PyString_Check(sep) &&
Eric Smith7c478942008-03-18 23:45:49 +00001578 !PyUnicode_Check(sep)) {
1579 PyErr_Format(PyExc_TypeError,
1580 "sep must be None, str or unicode, not %.200s",
1581 sep->ob_type->tp_name);
1582 return NULL;
1583 }
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001584 if (end && end != Py_None && !PyString_Check(end) &&
Eric Smith7c478942008-03-18 23:45:49 +00001585 !PyUnicode_Check(end)) {
1586 PyErr_Format(PyExc_TypeError,
1587 "end must be None, str or unicode, not %.200s",
1588 end->ob_type->tp_name);
1589 return NULL;
1590 }
1591
1592 for (i = 0; i < PyTuple_Size(args); i++) {
1593 if (i > 0) {
1594 if (sep == NULL || sep == Py_None)
1595 err = PyFile_WriteString(" ", file);
1596 else
1597 err = PyFile_WriteObject(sep, file,
1598 Py_PRINT_RAW);
1599 if (err)
1600 return NULL;
1601 }
1602 err = PyFile_WriteObject(PyTuple_GetItem(args, i), file,
1603 Py_PRINT_RAW);
1604 if (err)
1605 return NULL;
1606 }
1607
1608 if (end == NULL || end == Py_None)
1609 err = PyFile_WriteString("\n", file);
1610 else
1611 err = PyFile_WriteObject(end, file, Py_PRINT_RAW);
1612 if (err)
1613 return NULL;
1614
1615 Py_RETURN_NONE;
1616}
1617
1618PyDoc_STRVAR(print_doc,
1619"print(value, ..., sep=' ', end='\\n', file=sys.stdout)\n\
1620\n\
1621Prints the values to a stream, or to sys.stdout by default.\n\
1622Optional keyword arguments:\n\
1623file: a file-like object (stream); defaults to the current sys.stdout.\n\
1624sep: string inserted between values, default a space.\n\
1625end: string appended after the last value, default a newline.");
1626
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001627
1628/* Return number of items in range (lo, hi, step), when arguments are
1629 * PyInt or PyLong objects. step > 0 required. Return a value < 0 if
1630 * & only if the true value is too large to fit in a signed long.
1631 * Arguments MUST return 1 with either PyInt_Check() or
1632 * PyLong_Check(). Return -1 when there is an error.
1633 */
1634static long
1635get_len_of_range_longs(PyObject *lo, PyObject *hi, PyObject *step)
1636{
1637 /* -------------------------------------------------------------
1638 Algorithm is equal to that of get_len_of_range(), but it operates
1639 on PyObjects (which are assumed to be PyLong or PyInt objects).
1640 ---------------------------------------------------------------*/
1641 long n;
1642 PyObject *diff = NULL;
1643 PyObject *one = NULL;
1644 PyObject *tmp1 = NULL, *tmp2 = NULL, *tmp3 = NULL;
1645 /* holds sub-expression evaluations */
1646
1647 /* if (lo >= hi), return length of 0. */
1648 if (PyObject_Compare(lo, hi) >= 0)
1649 return 0;
1650
1651 if ((one = PyLong_FromLong(1L)) == NULL)
1652 goto Fail;
1653
1654 if ((tmp1 = PyNumber_Subtract(hi, lo)) == NULL)
1655 goto Fail;
1656
1657 if ((diff = PyNumber_Subtract(tmp1, one)) == NULL)
1658 goto Fail;
1659
1660 if ((tmp2 = PyNumber_FloorDivide(diff, step)) == NULL)
1661 goto Fail;
1662
1663 if ((tmp3 = PyNumber_Add(tmp2, one)) == NULL)
1664 goto Fail;
1665
1666 n = PyLong_AsLong(tmp3);
1667 if (PyErr_Occurred()) { /* Check for Overflow */
1668 PyErr_Clear();
1669 goto Fail;
1670 }
1671
1672 Py_DECREF(tmp3);
1673 Py_DECREF(tmp2);
1674 Py_DECREF(diff);
1675 Py_DECREF(tmp1);
1676 Py_DECREF(one);
1677 return n;
1678
1679 Fail:
1680 Py_XDECREF(tmp3);
1681 Py_XDECREF(tmp2);
1682 Py_XDECREF(diff);
1683 Py_XDECREF(tmp1);
1684 Py_XDECREF(one);
1685 return -1;
1686}
1687
1688/* An extension of builtin_range() that handles the case when PyLong
1689 * arguments are given. */
1690static PyObject *
1691handle_range_longs(PyObject *self, PyObject *args)
1692{
1693 PyObject *ilow;
Tim Peters874e1f72003-04-13 22:13:08 +00001694 PyObject *ihigh = NULL;
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001695 PyObject *istep = NULL;
Tim Peters874e1f72003-04-13 22:13:08 +00001696
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001697 PyObject *curnum = NULL;
1698 PyObject *v = NULL;
1699 long bign;
1700 int i, n;
1701 int cmp_result;
1702
Tim Peters874e1f72003-04-13 22:13:08 +00001703 PyObject *zero = PyLong_FromLong(0);
1704
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001705 if (zero == NULL)
1706 return NULL;
1707
Tim Peters874e1f72003-04-13 22:13:08 +00001708 if (!PyArg_UnpackTuple(args, "range", 1, 3, &ilow, &ihigh, &istep)) {
1709 Py_DECREF(zero);
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001710 return NULL;
1711 }
1712
Tim Peters874e1f72003-04-13 22:13:08 +00001713 /* Figure out which way we were called, supply defaults, and be
1714 * sure to incref everything so that the decrefs at the end
1715 * are correct.
1716 */
1717 assert(ilow != NULL);
1718 if (ihigh == NULL) {
1719 /* only 1 arg -- it's the upper limit */
1720 ihigh = ilow;
1721 ilow = NULL;
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001722 }
Tim Peters874e1f72003-04-13 22:13:08 +00001723 assert(ihigh != NULL);
1724 Py_INCREF(ihigh);
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001725
Tim Peters874e1f72003-04-13 22:13:08 +00001726 /* ihigh correct now; do ilow */
1727 if (ilow == NULL)
1728 ilow = zero;
1729 Py_INCREF(ilow);
1730
1731 /* ilow and ihigh correct now; do istep */
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001732 if (istep == NULL) {
1733 istep = PyLong_FromLong(1L);
1734 if (istep == NULL)
1735 goto Fail;
1736 }
1737 else {
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001738 Py_INCREF(istep);
1739 }
1740
Tim Peters874e1f72003-04-13 22:13:08 +00001741 if (!PyInt_Check(ilow) && !PyLong_Check(ilow)) {
Guido van Rossum28e83e32003-04-15 12:43:26 +00001742 PyErr_Format(PyExc_TypeError,
Alex Martellif471d472003-04-23 13:00:44 +00001743 "range() integer start argument expected, got %s.",
Guido van Rossum817d6c92003-04-14 18:25:04 +00001744 ilow->ob_type->tp_name);
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001745 goto Fail;
1746 }
1747
Tim Peters874e1f72003-04-13 22:13:08 +00001748 if (!PyInt_Check(ihigh) && !PyLong_Check(ihigh)) {
Guido van Rossum28e83e32003-04-15 12:43:26 +00001749 PyErr_Format(PyExc_TypeError,
Alex Martellif471d472003-04-23 13:00:44 +00001750 "range() integer end argument expected, got %s.",
Guido van Rossum817d6c92003-04-14 18:25:04 +00001751 ihigh->ob_type->tp_name);
Tim Peters874e1f72003-04-13 22:13:08 +00001752 goto Fail;
1753 }
1754
1755 if (!PyInt_Check(istep) && !PyLong_Check(istep)) {
Guido van Rossum28e83e32003-04-15 12:43:26 +00001756 PyErr_Format(PyExc_TypeError,
Alex Martellif471d472003-04-23 13:00:44 +00001757 "range() integer step argument expected, got %s.",
Guido van Rossum817d6c92003-04-14 18:25:04 +00001758 istep->ob_type->tp_name);
Tim Peters874e1f72003-04-13 22:13:08 +00001759 goto Fail;
1760 }
1761
1762 if (PyObject_Cmp(istep, zero, &cmp_result) == -1)
1763 goto Fail;
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001764 if (cmp_result == 0) {
1765 PyErr_SetString(PyExc_ValueError,
Alex Martellif471d472003-04-23 13:00:44 +00001766 "range() step argument must not be zero");
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001767 goto Fail;
1768 }
1769
1770 if (cmp_result > 0)
1771 bign = get_len_of_range_longs(ilow, ihigh, istep);
1772 else {
1773 PyObject *neg_istep = PyNumber_Negative(istep);
1774 if (neg_istep == NULL)
1775 goto Fail;
1776 bign = get_len_of_range_longs(ihigh, ilow, neg_istep);
1777 Py_DECREF(neg_istep);
1778 }
1779
1780 n = (int)bign;
1781 if (bign < 0 || (long)n != bign) {
1782 PyErr_SetString(PyExc_OverflowError,
1783 "range() result has too many items");
1784 goto Fail;
1785 }
1786
1787 v = PyList_New(n);
1788 if (v == NULL)
1789 goto Fail;
1790
1791 curnum = ilow;
1792 Py_INCREF(curnum);
1793
1794 for (i = 0; i < n; i++) {
1795 PyObject *w = PyNumber_Long(curnum);
1796 PyObject *tmp_num;
1797 if (w == NULL)
1798 goto Fail;
1799
1800 PyList_SET_ITEM(v, i, w);
1801
1802 tmp_num = PyNumber_Add(curnum, istep);
1803 if (tmp_num == NULL)
1804 goto Fail;
1805
1806 Py_DECREF(curnum);
1807 curnum = tmp_num;
1808 }
Tim Peters874e1f72003-04-13 22:13:08 +00001809 Py_DECREF(ilow);
1810 Py_DECREF(ihigh);
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001811 Py_DECREF(istep);
1812 Py_DECREF(zero);
Tim Peters874e1f72003-04-13 22:13:08 +00001813 Py_DECREF(curnum);
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001814 return v;
1815
1816 Fail:
Tim Peters874e1f72003-04-13 22:13:08 +00001817 Py_DECREF(ilow);
1818 Py_DECREF(ihigh);
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001819 Py_XDECREF(istep);
Tim Peters874e1f72003-04-13 22:13:08 +00001820 Py_DECREF(zero);
1821 Py_XDECREF(curnum);
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001822 Py_XDECREF(v);
1823 return NULL;
1824}
1825
Guido van Rossum124eff01999-02-23 16:11:01 +00001826/* Return number of items in range/xrange (lo, hi, step). step > 0
1827 * required. Return a value < 0 if & only if the true value is too
1828 * large to fit in a signed long.
1829 */
1830static long
Thomas Wouterse28c2962000-07-23 22:21:32 +00001831get_len_of_range(long lo, long hi, long step)
Guido van Rossum124eff01999-02-23 16:11:01 +00001832{
1833 /* -------------------------------------------------------------
1834 If lo >= hi, the range is empty.
1835 Else if n values are in the range, the last one is
1836 lo + (n-1)*step, which must be <= hi-1. Rearranging,
1837 n <= (hi - lo - 1)/step + 1, so taking the floor of the RHS gives
1838 the proper value. Since lo < hi in this case, hi-lo-1 >= 0, so
1839 the RHS is non-negative and so truncation is the same as the
1840 floor. Letting M be the largest positive long, the worst case
1841 for the RHS numerator is hi=M, lo=-M-1, and then
1842 hi-lo-1 = M-(-M-1)-1 = 2*M. Therefore unsigned long has enough
1843 precision to compute the RHS exactly.
1844 ---------------------------------------------------------------*/
1845 long n = 0;
1846 if (lo < hi) {
1847 unsigned long uhi = (unsigned long)hi;
1848 unsigned long ulo = (unsigned long)lo;
1849 unsigned long diff = uhi - ulo - 1;
1850 n = (long)(diff / (unsigned long)step + 1);
1851 }
1852 return n;
1853}
1854
Guido van Rossum79f25d91997-04-29 20:08:16 +00001855static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001856builtin_range(PyObject *self, PyObject *args)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001857{
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001858 long ilow = 0, ihigh = 0, istep = 1;
Guido van Rossum124eff01999-02-23 16:11:01 +00001859 long bign;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001860 int i, n;
Guido van Rossum124eff01999-02-23 16:11:01 +00001861
Guido van Rossum79f25d91997-04-29 20:08:16 +00001862 PyObject *v;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001863
Guido van Rossum79f25d91997-04-29 20:08:16 +00001864 if (PyTuple_Size(args) <= 1) {
1865 if (!PyArg_ParseTuple(args,
Guido van Rossum0865dd91995-01-17 16:30:22 +00001866 "l;range() requires 1-3 int arguments",
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001867 &ihigh)) {
1868 PyErr_Clear();
1869 return handle_range_longs(self, args);
1870 }
Guido van Rossum3f5da241990-12-20 15:06:42 +00001871 }
1872 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001873 if (!PyArg_ParseTuple(args,
Guido van Rossum0865dd91995-01-17 16:30:22 +00001874 "ll|l;range() requires 1-3 int arguments",
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001875 &ilow, &ihigh, &istep)) {
1876 PyErr_Clear();
1877 return handle_range_longs(self, args);
1878 }
Guido van Rossum3f5da241990-12-20 15:06:42 +00001879 }
1880 if (istep == 0) {
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001881 PyErr_SetString(PyExc_ValueError,
Alex Martellif471d472003-04-23 13:00:44 +00001882 "range() step argument must not be zero");
Guido van Rossum3f5da241990-12-20 15:06:42 +00001883 return NULL;
1884 }
Guido van Rossum124eff01999-02-23 16:11:01 +00001885 if (istep > 0)
1886 bign = get_len_of_range(ilow, ihigh, istep);
1887 else
1888 bign = get_len_of_range(ihigh, ilow, -istep);
1889 n = (int)bign;
1890 if (bign < 0 || (long)n != bign) {
Guido van Rossume23cde21999-01-12 05:07:47 +00001891 PyErr_SetString(PyExc_OverflowError,
Fred Drake661ea262000-10-24 19:57:45 +00001892 "range() result has too many items");
Guido van Rossume23cde21999-01-12 05:07:47 +00001893 return NULL;
1894 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00001895 v = PyList_New(n);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001896 if (v == NULL)
1897 return NULL;
1898 for (i = 0; i < n; i++) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001899 PyObject *w = PyInt_FromLong(ilow);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001900 if (w == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001901 Py_DECREF(v);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001902 return NULL;
1903 }
Guido van Rossuma937d141998-04-24 18:22:02 +00001904 PyList_SET_ITEM(v, i, w);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001905 ilow += istep;
1906 }
1907 return v;
1908}
1909
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001910PyDoc_STRVAR(range_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001911"range([start,] stop[, step]) -> list of integers\n\
1912\n\
1913Return a list containing an arithmetic progression of integers.\n\
1914range(i, j) returns [i, i+1, i+2, ..., j-1]; start (!) defaults to 0.\n\
1915When step is given, it specifies the increment (or decrement).\n\
1916For example, range(4) returns [0, 1, 2, 3]. The end point is omitted!\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001917These are exactly the valid indices for a list of 4 elements.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001918
1919
Guido van Rossum79f25d91997-04-29 20:08:16 +00001920static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001921builtin_raw_input(PyObject *self, PyObject *args)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001922{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001923 PyObject *v = NULL;
Martin v. Löwis31d2df52002-08-14 15:46:02 +00001924 PyObject *fin = PySys_GetObject("stdin");
1925 PyObject *fout = PySys_GetObject("stdout");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001926
Raymond Hettingerea3fdf42002-12-29 16:33:45 +00001927 if (!PyArg_UnpackTuple(args, "[raw_]input", 0, 1, &v))
Guido van Rossum3165fe61992-09-25 21:59:05 +00001928 return NULL;
Martin v. Löwis31d2df52002-08-14 15:46:02 +00001929
1930 if (fin == NULL) {
Alex Martellia9b9c9f2003-04-23 13:34:35 +00001931 PyErr_SetString(PyExc_RuntimeError, "[raw_]input: lost sys.stdin");
Martin v. Löwis31d2df52002-08-14 15:46:02 +00001932 return NULL;
1933 }
1934 if (fout == NULL) {
Alex Martellia9b9c9f2003-04-23 13:34:35 +00001935 PyErr_SetString(PyExc_RuntimeError, "[raw_]input: lost sys.stdout");
Martin v. Löwis31d2df52002-08-14 15:46:02 +00001936 return NULL;
1937 }
1938 if (PyFile_SoftSpace(fout, 0)) {
1939 if (PyFile_WriteString(" ", fout) != 0)
1940 return NULL;
1941 }
Georg Brandl7e3ba2a2006-08-06 08:23:54 +00001942 if (PyFile_AsFile(fin) && PyFile_AsFile(fout)
Martin v. Löwis566f6af2002-10-26 14:39:10 +00001943 && isatty(fileno(PyFile_AsFile(fin)))
1944 && isatty(fileno(PyFile_AsFile(fout)))) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001945 PyObject *po;
Guido van Rossum872537c1995-07-07 22:43:42 +00001946 char *prompt;
1947 char *s;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001948 PyObject *result;
Guido van Rossum872537c1995-07-07 22:43:42 +00001949 if (v != NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001950 po = PyObject_Str(v);
Guido van Rossum872537c1995-07-07 22:43:42 +00001951 if (po == NULL)
1952 return NULL;
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001953 prompt = PyString_AsString(po);
Guido van Rossumd9b52081998-06-26 18:25:38 +00001954 if (prompt == NULL)
1955 return NULL;
Guido van Rossum872537c1995-07-07 22:43:42 +00001956 }
1957 else {
1958 po = NULL;
1959 prompt = "";
1960 }
Michael W. Hudson52db5192004-08-02 13:21:09 +00001961 s = PyOS_Readline(PyFile_AsFile(fin), PyFile_AsFile(fout),
Martin v. Löwis566f6af2002-10-26 14:39:10 +00001962 prompt);
Guido van Rossum79f25d91997-04-29 20:08:16 +00001963 Py_XDECREF(po);
Guido van Rossum872537c1995-07-07 22:43:42 +00001964 if (s == NULL) {
Michael W. Hudson30ea2f22004-07-07 17:44:12 +00001965 if (!PyErr_Occurred())
1966 PyErr_SetNone(PyExc_KeyboardInterrupt);
Guido van Rossum872537c1995-07-07 22:43:42 +00001967 return NULL;
1968 }
1969 if (*s == '\0') {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001970 PyErr_SetNone(PyExc_EOFError);
Guido van Rossum872537c1995-07-07 22:43:42 +00001971 result = NULL;
1972 }
1973 else { /* strip trailing '\n' */
Guido van Rossum106f2da2000-06-28 21:12:25 +00001974 size_t len = strlen(s);
Martin v. Löwisb1ed7fa2006-04-13 07:52:27 +00001975 if (len > PY_SSIZE_T_MAX) {
Martin v. Löwis31d2df52002-08-14 15:46:02 +00001976 PyErr_SetString(PyExc_OverflowError,
Alex Martellia9b9c9f2003-04-23 13:34:35 +00001977 "[raw_]input: input too long");
Guido van Rossum106f2da2000-06-28 21:12:25 +00001978 result = NULL;
1979 }
1980 else {
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001981 result = PyString_FromStringAndSize(s, len-1);
Guido van Rossum106f2da2000-06-28 21:12:25 +00001982 }
Guido van Rossum872537c1995-07-07 22:43:42 +00001983 }
Guido van Rossumb18618d2000-05-03 23:44:39 +00001984 PyMem_FREE(s);
Guido van Rossum872537c1995-07-07 22:43:42 +00001985 return result;
1986 }
Guido van Rossum90933611991-06-07 16:10:43 +00001987 if (v != NULL) {
Martin v. Löwis31d2df52002-08-14 15:46:02 +00001988 if (PyFile_WriteObject(v, fout, Py_PRINT_RAW) != 0)
Guido van Rossum90933611991-06-07 16:10:43 +00001989 return NULL;
1990 }
Martin v. Löwis31d2df52002-08-14 15:46:02 +00001991 return PyFile_GetLine(fin, -1);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001992}
1993
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001994PyDoc_STRVAR(raw_input_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001995"raw_input([prompt]) -> string\n\
1996\n\
1997Read a string from standard input. The trailing newline is stripped.\n\
1998If the user hits EOF (Unix: Ctl-D, Windows: Ctl-Z+Return), raise EOFError.\n\
1999On Unix, GNU readline is used if enabled. The prompt string, if given,\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002000is printed without a trailing newline before reading.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002001
2002
Guido van Rossum79f25d91997-04-29 20:08:16 +00002003static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002004builtin_reduce(PyObject *self, PyObject *args)
Guido van Rossum12d12c51993-10-26 17:58:25 +00002005{
Benjamin Peterson08336e32008-08-18 02:01:21 +00002006 static PyObject *functools_reduce = NULL;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002007
Benjamin Peterson9f4f4812008-04-27 03:01:45 +00002008 if (PyErr_WarnPy3k("reduce() not supported in 3.x; "
Benjamin Petersonf19a7b92008-04-27 18:40:21 +00002009 "use functools.reduce()", 1) < 0)
Neal Norwitzdf25efe2007-05-23 06:58:36 +00002010 return NULL;
2011
Benjamin Peterson08336e32008-08-18 02:01:21 +00002012 if (functools_reduce == NULL) {
2013 PyObject *functools = PyImport_ImportModule("functools");
2014 if (functools == NULL)
2015 return NULL;
2016 functools_reduce = PyObject_GetAttrString(functools, "reduce");
2017 Py_DECREF(functools);
2018 if (functools_reduce == NULL)
2019 return NULL;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002020 }
Benjamin Peterson08336e32008-08-18 02:01:21 +00002021 return PyObject_Call(functools_reduce, args, NULL);
Guido van Rossum12d12c51993-10-26 17:58:25 +00002022}
2023
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002024PyDoc_STRVAR(reduce_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002025"reduce(function, sequence[, initial]) -> value\n\
2026\n\
2027Apply a function of two arguments cumulatively to the items of a sequence,\n\
2028from left to right, so as to reduce the sequence to a single value.\n\
2029For example, reduce(lambda x, y: x+y, [1, 2, 3, 4, 5]) calculates\n\
2030((((1+2)+3)+4)+5). If initial is present, it is placed before the items\n\
2031of the sequence in the calculation, and serves as a default when the\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002032sequence is empty.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002033
2034
Guido van Rossum79f25d91997-04-29 20:08:16 +00002035static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002036builtin_reload(PyObject *self, PyObject *v)
Guido van Rossum3f5da241990-12-20 15:06:42 +00002037{
Benjamin Peterson9f4f4812008-04-27 03:01:45 +00002038 if (PyErr_WarnPy3k("In 3.x, reload() is renamed to imp.reload()",
Benjamin Petersonf19a7b92008-04-27 18:40:21 +00002039 1) < 0)
Neal Norwitzdf25efe2007-05-23 06:58:36 +00002040 return NULL;
2041
Guido van Rossum79f25d91997-04-29 20:08:16 +00002042 return PyImport_ReloadModule(v);
Guido van Rossum3f5da241990-12-20 15:06:42 +00002043}
2044
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002045PyDoc_STRVAR(reload_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002046"reload(module) -> module\n\
2047\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002048Reload the module. The module must have been successfully imported before.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002049
2050
Guido van Rossum79f25d91997-04-29 20:08:16 +00002051static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002052builtin_repr(PyObject *self, PyObject *v)
Guido van Rossumc89705d1992-11-26 08:54:07 +00002053{
Guido van Rossum79f25d91997-04-29 20:08:16 +00002054 return PyObject_Repr(v);
Guido van Rossumc89705d1992-11-26 08:54:07 +00002055}
2056
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002057PyDoc_STRVAR(repr_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002058"repr(object) -> string\n\
2059\n\
2060Return the canonical string representation of the object.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002061For most object types, eval(repr(object)) == object.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002062
2063
Guido van Rossum79f25d91997-04-29 20:08:16 +00002064static PyObject *
Georg Brandlccadf842006-03-31 18:54:53 +00002065builtin_round(PyObject *self, PyObject *args, PyObject *kwds)
Guido van Rossum9e51f9b1993-02-12 16:29:05 +00002066{
Jeffrey Yasskin9871d8f2008-01-05 08:47:13 +00002067 double number;
2068 double f;
2069 int ndigits = 0;
2070 int i;
Georg Brandlccadf842006-03-31 18:54:53 +00002071 static char *kwlist[] = {"number", "ndigits", 0};
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002072
Jeffrey Yasskin9871d8f2008-01-05 08:47:13 +00002073 if (!PyArg_ParseTupleAndKeywords(args, kwds, "d|i:round",
2074 kwlist, &number, &ndigits))
2075 return NULL;
2076 f = 1.0;
2077 i = abs(ndigits);
2078 while (--i >= 0)
2079 f = f*10.0;
2080 if (ndigits < 0)
2081 number /= f;
2082 else
2083 number *= f;
2084 if (number >= 0.0)
2085 number = floor(number + 0.5);
2086 else
2087 number = ceil(number - 0.5);
2088 if (ndigits < 0)
2089 number *= f;
2090 else
2091 number /= f;
2092 return PyFloat_FromDouble(number);
Guido van Rossum9e51f9b1993-02-12 16:29:05 +00002093}
2094
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002095PyDoc_STRVAR(round_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002096"round(number[, ndigits]) -> floating point number\n\
2097\n\
2098Round a number to a given precision in decimal digits (default 0 digits).\n\
Jeffrey Yasskin9871d8f2008-01-05 08:47:13 +00002099This always returns a floating point number. Precision may be negative.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002100
Raymond Hettinger64958a12003-12-17 20:43:33 +00002101static PyObject *
2102builtin_sorted(PyObject *self, PyObject *args, PyObject *kwds)
2103{
2104 PyObject *newlist, *v, *seq, *compare=NULL, *keyfunc=NULL, *newargs;
2105 PyObject *callable;
Martin v. Löwis15e62742006-02-27 16:46:16 +00002106 static char *kwlist[] = {"iterable", "cmp", "key", "reverse", 0};
Neal Norwitz6f0d4792005-12-15 06:40:36 +00002107 int reverse;
Raymond Hettinger64958a12003-12-17 20:43:33 +00002108
Neal Norwitz6f0d4792005-12-15 06:40:36 +00002109 /* args 1-4 should match listsort in Objects/listobject.c */
Armin Rigo71d7e702005-09-20 18:13:03 +00002110 if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|OOi:sorted",
2111 kwlist, &seq, &compare, &keyfunc, &reverse))
2112 return NULL;
Raymond Hettinger64958a12003-12-17 20:43:33 +00002113
2114 newlist = PySequence_List(seq);
2115 if (newlist == NULL)
2116 return NULL;
2117
2118 callable = PyObject_GetAttrString(newlist, "sort");
2119 if (callable == NULL) {
2120 Py_DECREF(newlist);
2121 return NULL;
2122 }
Georg Brandl99d7e4e2005-08-31 22:21:15 +00002123
Raymond Hettinger64958a12003-12-17 20:43:33 +00002124 newargs = PyTuple_GetSlice(args, 1, 4);
2125 if (newargs == NULL) {
2126 Py_DECREF(newlist);
2127 Py_DECREF(callable);
2128 return NULL;
2129 }
2130
2131 v = PyObject_Call(callable, newargs, kwds);
2132 Py_DECREF(newargs);
2133 Py_DECREF(callable);
2134 if (v == NULL) {
2135 Py_DECREF(newlist);
2136 return NULL;
2137 }
2138 Py_DECREF(v);
2139 return newlist;
2140}
2141
2142PyDoc_STRVAR(sorted_doc,
2143"sorted(iterable, cmp=None, key=None, reverse=False) --> new sorted list");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002144
Guido van Rossum79f25d91997-04-29 20:08:16 +00002145static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002146builtin_vars(PyObject *self, PyObject *args)
Guido van Rossum2d951851994-08-29 12:52:16 +00002147{
Guido van Rossum79f25d91997-04-29 20:08:16 +00002148 PyObject *v = NULL;
2149 PyObject *d;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002150
Raymond Hettingerea3fdf42002-12-29 16:33:45 +00002151 if (!PyArg_UnpackTuple(args, "vars", 0, 1, &v))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002152 return NULL;
Guido van Rossum2d951851994-08-29 12:52:16 +00002153 if (v == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002154 d = PyEval_GetLocals();
Guido van Rossum53bb7ff1995-07-26 16:26:31 +00002155 if (d == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002156 if (!PyErr_Occurred())
2157 PyErr_SetString(PyExc_SystemError,
Alex Martellia9b9c9f2003-04-23 13:34:35 +00002158 "vars(): no locals!?");
Guido van Rossum53bb7ff1995-07-26 16:26:31 +00002159 }
2160 else
Guido van Rossum79f25d91997-04-29 20:08:16 +00002161 Py_INCREF(d);
Guido van Rossum2d951851994-08-29 12:52:16 +00002162 }
2163 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002164 d = PyObject_GetAttrString(v, "__dict__");
Guido van Rossum2d951851994-08-29 12:52:16 +00002165 if (d == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002166 PyErr_SetString(PyExc_TypeError,
Guido van Rossum2d951851994-08-29 12:52:16 +00002167 "vars() argument must have __dict__ attribute");
2168 return NULL;
2169 }
2170 }
2171 return d;
2172}
2173
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002174PyDoc_STRVAR(vars_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002175"vars([object]) -> dictionary\n\
2176\n\
2177Without arguments, equivalent to locals().\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002178With an argument, equivalent to object.__dict__.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002179
Alex Martellia70b1912003-04-22 08:12:33 +00002180
2181static PyObject*
2182builtin_sum(PyObject *self, PyObject *args)
2183{
2184 PyObject *seq;
2185 PyObject *result = NULL;
2186 PyObject *temp, *item, *iter;
2187
Raymond Hettinger5cf63942003-10-25 06:41:37 +00002188 if (!PyArg_UnpackTuple(args, "sum", 1, 2, &seq, &result))
Alex Martellia70b1912003-04-22 08:12:33 +00002189 return NULL;
2190
2191 iter = PyObject_GetIter(seq);
2192 if (iter == NULL)
2193 return NULL;
2194
2195 if (result == NULL) {
2196 result = PyInt_FromLong(0);
2197 if (result == NULL) {
2198 Py_DECREF(iter);
2199 return NULL;
2200 }
2201 } else {
2202 /* reject string values for 'start' parameter */
2203 if (PyObject_TypeCheck(result, &PyBaseString_Type)) {
2204 PyErr_SetString(PyExc_TypeError,
Alex Martellia9b9c9f2003-04-23 13:34:35 +00002205 "sum() can't sum strings [use ''.join(seq) instead]");
Alex Martellia70b1912003-04-22 08:12:33 +00002206 Py_DECREF(iter);
2207 return NULL;
2208 }
Alex Martelli41c9f882003-04-22 09:24:48 +00002209 Py_INCREF(result);
Alex Martellia70b1912003-04-22 08:12:33 +00002210 }
2211
Raymond Hettinger3f8caa32007-10-24 01:28:33 +00002212#ifndef SLOW_SUM
2213 /* Fast addition by keeping temporary sums in C instead of new Python objects.
2214 Assumes all inputs are the same type. If the assumption fails, default
2215 to the more general routine.
2216 */
2217 if (PyInt_CheckExact(result)) {
2218 long i_result = PyInt_AS_LONG(result);
2219 Py_DECREF(result);
2220 result = NULL;
2221 while(result == NULL) {
2222 item = PyIter_Next(iter);
2223 if (item == NULL) {
2224 Py_DECREF(iter);
2225 if (PyErr_Occurred())
2226 return NULL;
2227 return PyInt_FromLong(i_result);
2228 }
2229 if (PyInt_CheckExact(item)) {
2230 long b = PyInt_AS_LONG(item);
2231 long x = i_result + b;
2232 if ((x^i_result) >= 0 || (x^b) >= 0) {
2233 i_result = x;
2234 Py_DECREF(item);
2235 continue;
2236 }
2237 }
2238 /* Either overflowed or is not an int. Restore real objects and process normally */
2239 result = PyInt_FromLong(i_result);
2240 temp = PyNumber_Add(result, item);
2241 Py_DECREF(result);
2242 Py_DECREF(item);
2243 result = temp;
2244 if (result == NULL) {
2245 Py_DECREF(iter);
2246 return NULL;
2247 }
2248 }
2249 }
2250
2251 if (PyFloat_CheckExact(result)) {
2252 double f_result = PyFloat_AS_DOUBLE(result);
2253 Py_DECREF(result);
2254 result = NULL;
2255 while(result == NULL) {
2256 item = PyIter_Next(iter);
2257 if (item == NULL) {
2258 Py_DECREF(iter);
2259 if (PyErr_Occurred())
2260 return NULL;
2261 return PyFloat_FromDouble(f_result);
2262 }
2263 if (PyFloat_CheckExact(item)) {
Raymond Hettinger65856602008-05-30 06:37:27 +00002264 PyFPE_START_PROTECT("add", Py_DECREF(item); Py_DECREF(iter); return 0)
Raymond Hettinger3f8caa32007-10-24 01:28:33 +00002265 f_result += PyFloat_AS_DOUBLE(item);
Raymond Hettinger3a8daf52007-10-24 02:05:51 +00002266 PyFPE_END_PROTECT(f_result)
Raymond Hettingera45c4872007-10-25 02:26:58 +00002267 Py_DECREF(item);
Raymond Hettinger3a8daf52007-10-24 02:05:51 +00002268 continue;
2269 }
2270 if (PyInt_CheckExact(item)) {
Raymond Hettinger65856602008-05-30 06:37:27 +00002271 PyFPE_START_PROTECT("add", Py_DECREF(item); Py_DECREF(iter); return 0)
Raymond Hettinger3a8daf52007-10-24 02:05:51 +00002272 f_result += (double)PyInt_AS_LONG(item);
2273 PyFPE_END_PROTECT(f_result)
Raymond Hettingera45c4872007-10-25 02:26:58 +00002274 Py_DECREF(item);
Raymond Hettinger3f8caa32007-10-24 01:28:33 +00002275 continue;
2276 }
2277 result = PyFloat_FromDouble(f_result);
2278 temp = PyNumber_Add(result, item);
2279 Py_DECREF(result);
2280 Py_DECREF(item);
2281 result = temp;
2282 if (result == NULL) {
2283 Py_DECREF(iter);
2284 return NULL;
2285 }
2286 }
2287 }
2288#endif
2289
Alex Martellia70b1912003-04-22 08:12:33 +00002290 for(;;) {
2291 item = PyIter_Next(iter);
2292 if (item == NULL) {
2293 /* error, or end-of-sequence */
2294 if (PyErr_Occurred()) {
2295 Py_DECREF(result);
2296 result = NULL;
2297 }
2298 break;
2299 }
Alex Martellia253e182003-10-25 23:24:14 +00002300 temp = PyNumber_Add(result, item);
Alex Martellia70b1912003-04-22 08:12:33 +00002301 Py_DECREF(result);
2302 Py_DECREF(item);
2303 result = temp;
2304 if (result == NULL)
2305 break;
2306 }
2307 Py_DECREF(iter);
2308 return result;
2309}
2310
2311PyDoc_STRVAR(sum_doc,
Georg Brandl8134d062006-10-12 12:33:07 +00002312"sum(sequence[, start]) -> value\n\
Alex Martellia70b1912003-04-22 08:12:33 +00002313\n\
2314Returns the sum of a sequence of numbers (NOT strings) plus the value\n\
Georg Brandl8134d062006-10-12 12:33:07 +00002315of parameter 'start' (which defaults to 0). When the sequence is\n\
2316empty, returns start.");
Alex Martellia70b1912003-04-22 08:12:33 +00002317
2318
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002319static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002320builtin_isinstance(PyObject *self, PyObject *args)
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002321{
2322 PyObject *inst;
2323 PyObject *cls;
Guido van Rossum823649d2001-03-21 18:40:58 +00002324 int retval;
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002325
Raymond Hettingerea3fdf42002-12-29 16:33:45 +00002326 if (!PyArg_UnpackTuple(args, "isinstance", 2, 2, &inst, &cls))
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002327 return NULL;
Guido van Rossumf5dd9141997-12-02 19:11:45 +00002328
Guido van Rossum823649d2001-03-21 18:40:58 +00002329 retval = PyObject_IsInstance(inst, cls);
2330 if (retval < 0)
Guido van Rossumad991772001-01-12 16:03:05 +00002331 return NULL;
Guido van Rossum77f6a652002-04-03 22:41:51 +00002332 return PyBool_FromLong(retval);
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002333}
2334
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002335PyDoc_STRVAR(isinstance_doc,
Guido van Rossum77f6a652002-04-03 22:41:51 +00002336"isinstance(object, class-or-type-or-tuple) -> bool\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002337\n\
2338Return whether an object is an instance of a class or of a subclass thereof.\n\
Guido van Rossum03290ec2001-10-07 20:54:12 +00002339With a type as second argument, return whether that is the object's type.\n\
2340The form using a tuple, isinstance(x, (A, B, ...)), is a shortcut for\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002341isinstance(x, A) or isinstance(x, B) or ... (etc.).");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002342
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002343
2344static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002345builtin_issubclass(PyObject *self, PyObject *args)
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002346{
2347 PyObject *derived;
2348 PyObject *cls;
2349 int retval;
2350
Raymond Hettingerea3fdf42002-12-29 16:33:45 +00002351 if (!PyArg_UnpackTuple(args, "issubclass", 2, 2, &derived, &cls))
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002352 return NULL;
Guido van Rossum668213d1999-06-16 17:28:37 +00002353
Guido van Rossum823649d2001-03-21 18:40:58 +00002354 retval = PyObject_IsSubclass(derived, cls);
2355 if (retval < 0)
2356 return NULL;
Guido van Rossum77f6a652002-04-03 22:41:51 +00002357 return PyBool_FromLong(retval);
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002358}
2359
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002360PyDoc_STRVAR(issubclass_doc,
Guido van Rossum77f6a652002-04-03 22:41:51 +00002361"issubclass(C, B) -> bool\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002362\n\
Walter Dörwaldd9a6ad32002-12-12 16:41:44 +00002363Return whether class C is a subclass (i.e., a derived class) of class B.\n\
2364When using a tuple as the second argument issubclass(X, (A, B, ...)),\n\
2365is a shortcut for issubclass(X, A) or issubclass(X, B) or ... (etc.).");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002366
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002367
Barry Warsawbd599b52000-08-03 15:45:29 +00002368static PyObject*
2369builtin_zip(PyObject *self, PyObject *args)
2370{
2371 PyObject *ret;
Martin v. Löwisd96ee902006-02-16 14:37:16 +00002372 const Py_ssize_t itemsize = PySequence_Length(args);
2373 Py_ssize_t i;
Tim Peters8572b4f2001-05-06 01:05:02 +00002374 PyObject *itlist; /* tuple of iterators */
Martin v. Löwisd96ee902006-02-16 14:37:16 +00002375 Py_ssize_t len; /* guess at result length */
Barry Warsawbd599b52000-08-03 15:45:29 +00002376
Raymond Hettingereaef6152003-08-02 07:42:57 +00002377 if (itemsize == 0)
2378 return PyList_New(0);
2379
Barry Warsawbd599b52000-08-03 15:45:29 +00002380 /* args must be a tuple */
2381 assert(PyTuple_Check(args));
2382
Tim Peters39a86c22002-05-12 07:19:38 +00002383 /* Guess at result length: the shortest of the input lengths.
2384 If some argument refuses to say, we refuse to guess too, lest
2385 an argument like xrange(sys.maxint) lead us astray.*/
Tim Peters67d687a2002-04-29 21:27:32 +00002386 len = -1; /* unknown */
2387 for (i = 0; i < itemsize; ++i) {
2388 PyObject *item = PyTuple_GET_ITEM(args, i);
Raymond Hettinger4e2f7142007-12-06 00:56:53 +00002389 Py_ssize_t thislen = _PyObject_LengthHint(item, -1);
Tim Peters39a86c22002-05-12 07:19:38 +00002390 if (thislen < 0) {
Tim Peters39a86c22002-05-12 07:19:38 +00002391 len = -1;
2392 break;
2393 }
Tim Peters67d687a2002-04-29 21:27:32 +00002394 else if (len < 0 || thislen < len)
2395 len = thislen;
2396 }
2397
Tim Peters8572b4f2001-05-06 01:05:02 +00002398 /* allocate result list */
Tim Peters67d687a2002-04-29 21:27:32 +00002399 if (len < 0)
2400 len = 10; /* arbitrary */
2401 if ((ret = PyList_New(len)) == NULL)
Barry Warsawbd599b52000-08-03 15:45:29 +00002402 return NULL;
2403
Tim Peters8572b4f2001-05-06 01:05:02 +00002404 /* obtain iterators */
2405 itlist = PyTuple_New(itemsize);
2406 if (itlist == NULL)
2407 goto Fail_ret;
2408 for (i = 0; i < itemsize; ++i) {
2409 PyObject *item = PyTuple_GET_ITEM(args, i);
2410 PyObject *it = PyObject_GetIter(item);
2411 if (it == NULL) {
2412 if (PyErr_ExceptionMatches(PyExc_TypeError))
2413 PyErr_Format(PyExc_TypeError,
Neal Norwitza361bd82006-02-19 19:31:50 +00002414 "zip argument #%zd must support iteration",
Tim Peters8572b4f2001-05-06 01:05:02 +00002415 i+1);
2416 goto Fail_ret_itlist;
Barry Warsawbd599b52000-08-03 15:45:29 +00002417 }
Tim Peters8572b4f2001-05-06 01:05:02 +00002418 PyTuple_SET_ITEM(itlist, i, it);
2419 }
Barry Warsawbd599b52000-08-03 15:45:29 +00002420
Tim Peters8572b4f2001-05-06 01:05:02 +00002421 /* build result into ret list */
Tim Peters67d687a2002-04-29 21:27:32 +00002422 for (i = 0; ; ++i) {
2423 int j;
Tim Peters8572b4f2001-05-06 01:05:02 +00002424 PyObject *next = PyTuple_New(itemsize);
2425 if (!next)
2426 goto Fail_ret_itlist;
2427
Tim Peters67d687a2002-04-29 21:27:32 +00002428 for (j = 0; j < itemsize; j++) {
2429 PyObject *it = PyTuple_GET_ITEM(itlist, j);
Tim Peters8572b4f2001-05-06 01:05:02 +00002430 PyObject *item = PyIter_Next(it);
Barry Warsawbd599b52000-08-03 15:45:29 +00002431 if (!item) {
Tim Peters8572b4f2001-05-06 01:05:02 +00002432 if (PyErr_Occurred()) {
2433 Py_DECREF(ret);
2434 ret = NULL;
Barry Warsawbd599b52000-08-03 15:45:29 +00002435 }
2436 Py_DECREF(next);
Tim Peters8572b4f2001-05-06 01:05:02 +00002437 Py_DECREF(itlist);
Tim Peters67d687a2002-04-29 21:27:32 +00002438 goto Done;
Barry Warsawbd599b52000-08-03 15:45:29 +00002439 }
Tim Peters67d687a2002-04-29 21:27:32 +00002440 PyTuple_SET_ITEM(next, j, item);
Barry Warsawbd599b52000-08-03 15:45:29 +00002441 }
Tim Peters8572b4f2001-05-06 01:05:02 +00002442
Tim Peters67d687a2002-04-29 21:27:32 +00002443 if (i < len)
2444 PyList_SET_ITEM(ret, i, next);
2445 else {
2446 int status = PyList_Append(ret, next);
2447 Py_DECREF(next);
2448 ++len;
2449 if (status < 0)
2450 goto Fail_ret_itlist;
2451 }
Barry Warsawbd599b52000-08-03 15:45:29 +00002452 }
Tim Peters8572b4f2001-05-06 01:05:02 +00002453
Tim Peters67d687a2002-04-29 21:27:32 +00002454Done:
2455 if (ret != NULL && i < len) {
2456 /* The list is too big. */
2457 if (PyList_SetSlice(ret, i, len, NULL) < 0)
2458 return NULL;
2459 }
2460 return ret;
2461
Tim Peters8572b4f2001-05-06 01:05:02 +00002462Fail_ret_itlist:
2463 Py_DECREF(itlist);
2464Fail_ret:
2465 Py_DECREF(ret);
2466 return NULL;
Barry Warsawbd599b52000-08-03 15:45:29 +00002467}
2468
2469
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002470PyDoc_STRVAR(zip_doc,
Barry Warsawbd599b52000-08-03 15:45:29 +00002471"zip(seq1 [, seq2 [...]]) -> [(seq1[0], seq2[0] ...), (...)]\n\
2472\n\
2473Return a list of tuples, where each tuple contains the i-th element\n\
2474from each of the argument sequences. The returned list is truncated\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002475in length to the length of the shortest argument sequence.");
Barry Warsawbd599b52000-08-03 15:45:29 +00002476
2477
Guido van Rossum79f25d91997-04-29 20:08:16 +00002478static PyMethodDef builtin_methods[] = {
Neal Norwitz92e212f2006-04-03 04:48:37 +00002479 {"__import__", (PyCFunction)builtin___import__, METH_VARARGS | METH_KEYWORDS, import_doc},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002480 {"abs", builtin_abs, METH_O, abs_doc},
Raymond Hettinger96229b12005-03-11 06:49:40 +00002481 {"all", builtin_all, METH_O, all_doc},
2482 {"any", builtin_any, METH_O, any_doc},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002483 {"apply", builtin_apply, METH_VARARGS, apply_doc},
Eric Smith3cd81942008-02-22 16:30:22 +00002484 {"bin", builtin_bin, METH_O, bin_doc},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002485 {"callable", builtin_callable, METH_O, callable_doc},
2486 {"chr", builtin_chr, METH_VARARGS, chr_doc},
2487 {"cmp", builtin_cmp, METH_VARARGS, cmp_doc},
2488 {"coerce", builtin_coerce, METH_VARARGS, coerce_doc},
Georg Brandl5240d742007-03-13 20:46:32 +00002489 {"compile", (PyCFunction)builtin_compile, METH_VARARGS | METH_KEYWORDS, compile_doc},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002490 {"delattr", builtin_delattr, METH_VARARGS, delattr_doc},
2491 {"dir", builtin_dir, METH_VARARGS, dir_doc},
2492 {"divmod", builtin_divmod, METH_VARARGS, divmod_doc},
2493 {"eval", builtin_eval, METH_VARARGS, eval_doc},
2494 {"execfile", builtin_execfile, METH_VARARGS, execfile_doc},
2495 {"filter", builtin_filter, METH_VARARGS, filter_doc},
Eric Smitha9f7d622008-02-17 19:46:49 +00002496 {"format", builtin_format, METH_VARARGS, format_doc},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002497 {"getattr", builtin_getattr, METH_VARARGS, getattr_doc},
2498 {"globals", (PyCFunction)builtin_globals, METH_NOARGS, globals_doc},
2499 {"hasattr", builtin_hasattr, METH_VARARGS, hasattr_doc},
2500 {"hash", builtin_hash, METH_O, hash_doc},
2501 {"hex", builtin_hex, METH_O, hex_doc},
2502 {"id", builtin_id, METH_O, id_doc},
2503 {"input", builtin_input, METH_VARARGS, input_doc},
2504 {"intern", builtin_intern, METH_VARARGS, intern_doc},
2505 {"isinstance", builtin_isinstance, METH_VARARGS, isinstance_doc},
2506 {"issubclass", builtin_issubclass, METH_VARARGS, issubclass_doc},
2507 {"iter", builtin_iter, METH_VARARGS, iter_doc},
2508 {"len", builtin_len, METH_O, len_doc},
2509 {"locals", (PyCFunction)builtin_locals, METH_NOARGS, locals_doc},
2510 {"map", builtin_map, METH_VARARGS, map_doc},
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00002511 {"max", (PyCFunction)builtin_max, METH_VARARGS | METH_KEYWORDS, max_doc},
2512 {"min", (PyCFunction)builtin_min, METH_VARARGS | METH_KEYWORDS, min_doc},
Georg Brandl28e08732008-04-30 19:47:09 +00002513 {"next", builtin_next, METH_VARARGS, next_doc},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002514 {"oct", builtin_oct, METH_O, oct_doc},
Neal Norwitzc4edb0e2006-05-02 04:43:14 +00002515 {"open", (PyCFunction)builtin_open, METH_VARARGS | METH_KEYWORDS, open_doc},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002516 {"ord", builtin_ord, METH_O, ord_doc},
2517 {"pow", builtin_pow, METH_VARARGS, pow_doc},
Eric Smith7c478942008-03-18 23:45:49 +00002518 {"print", (PyCFunction)builtin_print, METH_VARARGS | METH_KEYWORDS, print_doc},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002519 {"range", builtin_range, METH_VARARGS, range_doc},
2520 {"raw_input", builtin_raw_input, METH_VARARGS, raw_input_doc},
2521 {"reduce", builtin_reduce, METH_VARARGS, reduce_doc},
2522 {"reload", builtin_reload, METH_O, reload_doc},
2523 {"repr", builtin_repr, METH_O, repr_doc},
Georg Brandlccadf842006-03-31 18:54:53 +00002524 {"round", (PyCFunction)builtin_round, METH_VARARGS | METH_KEYWORDS, round_doc},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002525 {"setattr", builtin_setattr, METH_VARARGS, setattr_doc},
Raymond Hettinger64958a12003-12-17 20:43:33 +00002526 {"sorted", (PyCFunction)builtin_sorted, METH_VARARGS | METH_KEYWORDS, sorted_doc},
Alex Martellia70b1912003-04-22 08:12:33 +00002527 {"sum", builtin_sum, METH_VARARGS, sum_doc},
Martin v. Löwis339d0f72001-08-17 18:39:25 +00002528#ifdef Py_USING_UNICODE
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002529 {"unichr", builtin_unichr, METH_VARARGS, unichr_doc},
Martin v. Löwis339d0f72001-08-17 18:39:25 +00002530#endif
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002531 {"vars", builtin_vars, METH_VARARGS, vars_doc},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002532 {"zip", builtin_zip, METH_VARARGS, zip_doc},
Guido van Rossumc02e15c1991-12-16 13:03:00 +00002533 {NULL, NULL},
Guido van Rossum3f5da241990-12-20 15:06:42 +00002534};
2535
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002536PyDoc_STRVAR(builtin_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002537"Built-in functions, exceptions, and other objects.\n\
2538\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002539Noteworthy: None is the `nil' object; Ellipsis represents `...' in slices.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002540
Guido van Rossum25ce5661997-08-02 03:10:38 +00002541PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002542_PyBuiltin_Init(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +00002543{
Fred Drake5550de32000-06-20 04:54:19 +00002544 PyObject *mod, *dict, *debug;
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002545 mod = Py_InitModule4("__builtin__", builtin_methods,
2546 builtin_doc, (PyObject *)NULL,
2547 PYTHON_API_VERSION);
Guido van Rossum25ce5661997-08-02 03:10:38 +00002548 if (mod == NULL)
2549 return NULL;
2550 dict = PyModule_GetDict(mod);
Tim Peters4b7625e2001-09-13 21:37:17 +00002551
Tim Peters7571a0f2003-03-23 17:52:28 +00002552#ifdef Py_TRACE_REFS
2553 /* __builtin__ exposes a number of statically allocated objects
2554 * that, before this code was added in 2.3, never showed up in
2555 * the list of "all objects" maintained by Py_TRACE_REFS. As a
2556 * result, programs leaking references to None and False (etc)
2557 * couldn't be diagnosed by examining sys.getobjects(0).
2558 */
2559#define ADD_TO_ALL(OBJECT) _Py_AddToAllObjects((PyObject *)(OBJECT), 0)
2560#else
2561#define ADD_TO_ALL(OBJECT) (void)0
2562#endif
2563
Tim Peters4b7625e2001-09-13 21:37:17 +00002564#define SETBUILTIN(NAME, OBJECT) \
Tim Peters7571a0f2003-03-23 17:52:28 +00002565 if (PyDict_SetItemString(dict, NAME, (PyObject *)OBJECT) < 0) \
2566 return NULL; \
2567 ADD_TO_ALL(OBJECT)
Tim Peters4b7625e2001-09-13 21:37:17 +00002568
2569 SETBUILTIN("None", Py_None);
2570 SETBUILTIN("Ellipsis", Py_Ellipsis);
2571 SETBUILTIN("NotImplemented", Py_NotImplemented);
Guido van Rossum77f6a652002-04-03 22:41:51 +00002572 SETBUILTIN("False", Py_False);
2573 SETBUILTIN("True", Py_True);
Neal Norwitz32a7e7f2002-05-31 19:58:02 +00002574 SETBUILTIN("basestring", &PyBaseString_Type);
Guido van Rossum77f6a652002-04-03 22:41:51 +00002575 SETBUILTIN("bool", &PyBool_Type);
Antoine Pitrou789be0c2009-04-02 21:18:34 +00002576 SETBUILTIN("memoryview", &PyMemoryView_Type);
Christian Heimes3497f942008-05-26 12:29:14 +00002577 SETBUILTIN("bytearray", &PyByteArray_Type);
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002578 SETBUILTIN("bytes", &PyString_Type);
Guido van Rossumbea18cc2002-06-14 20:41:17 +00002579 SETBUILTIN("buffer", &PyBuffer_Type);
Tim Peters4b7625e2001-09-13 21:37:17 +00002580 SETBUILTIN("classmethod", &PyClassMethod_Type);
Tim Peters6d6c1a32001-08-02 04:15:00 +00002581#ifndef WITHOUT_COMPLEX
Tim Peters4b7625e2001-09-13 21:37:17 +00002582 SETBUILTIN("complex", &PyComplex_Type);
Tim Peters6d6c1a32001-08-02 04:15:00 +00002583#endif
Tim Petersa427a2b2001-10-29 22:25:45 +00002584 SETBUILTIN("dict", &PyDict_Type);
Tim Peters67d687a2002-04-29 21:27:32 +00002585 SETBUILTIN("enumerate", &PyEnum_Type);
Neal Norwitzc4edb0e2006-05-02 04:43:14 +00002586 SETBUILTIN("file", &PyFile_Type);
Tim Peters4b7625e2001-09-13 21:37:17 +00002587 SETBUILTIN("float", &PyFloat_Type);
Raymond Hettingera690a992003-11-16 16:17:49 +00002588 SETBUILTIN("frozenset", &PyFrozenSet_Type);
Tim Peters4b7625e2001-09-13 21:37:17 +00002589 SETBUILTIN("property", &PyProperty_Type);
2590 SETBUILTIN("int", &PyInt_Type);
2591 SETBUILTIN("list", &PyList_Type);
2592 SETBUILTIN("long", &PyLong_Type);
2593 SETBUILTIN("object", &PyBaseObject_Type);
Raymond Hettinger85c20a42003-11-06 14:06:48 +00002594 SETBUILTIN("reversed", &PyReversed_Type);
Raymond Hettingera690a992003-11-16 16:17:49 +00002595 SETBUILTIN("set", &PySet_Type);
Guido van Rossumbea18cc2002-06-14 20:41:17 +00002596 SETBUILTIN("slice", &PySlice_Type);
Tim Peters4b7625e2001-09-13 21:37:17 +00002597 SETBUILTIN("staticmethod", &PyStaticMethod_Type);
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002598 SETBUILTIN("str", &PyString_Type);
Tim Peters4b7625e2001-09-13 21:37:17 +00002599 SETBUILTIN("super", &PySuper_Type);
2600 SETBUILTIN("tuple", &PyTuple_Type);
2601 SETBUILTIN("type", &PyType_Type);
Raymond Hettingerc4c453f2002-06-05 23:12:45 +00002602 SETBUILTIN("xrange", &PyRange_Type);
Martin v. Löwis339d0f72001-08-17 18:39:25 +00002603#ifdef Py_USING_UNICODE
Tim Peters4b7625e2001-09-13 21:37:17 +00002604 SETBUILTIN("unicode", &PyUnicode_Type);
Martin v. Löwis339d0f72001-08-17 18:39:25 +00002605#endif
Guido van Rossum77f6a652002-04-03 22:41:51 +00002606 debug = PyBool_FromLong(Py_OptimizeFlag == 0);
Fred Drake5550de32000-06-20 04:54:19 +00002607 if (PyDict_SetItemString(dict, "__debug__", debug) < 0) {
2608 Py_XDECREF(debug);
Guido van Rossum25ce5661997-08-02 03:10:38 +00002609 return NULL;
Fred Drake5550de32000-06-20 04:54:19 +00002610 }
2611 Py_XDECREF(debug);
Barry Warsaw757af0e1997-08-29 22:13:51 +00002612
Guido van Rossum25ce5661997-08-02 03:10:38 +00002613 return mod;
Tim Peters7571a0f2003-03-23 17:52:28 +00002614#undef ADD_TO_ALL
Tim Peters4b7625e2001-09-13 21:37:17 +00002615#undef SETBUILTIN
Guido van Rossum3f5da241990-12-20 15:06:42 +00002616}
2617
Guido van Rossume77a7571993-11-03 15:01:26 +00002618/* Helper for filter(): filter a tuple through a function */
Guido van Rossum12d12c51993-10-26 17:58:25 +00002619
Guido van Rossum79f25d91997-04-29 20:08:16 +00002620static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002621filtertuple(PyObject *func, PyObject *tuple)
Guido van Rossum12d12c51993-10-26 17:58:25 +00002622{
Guido van Rossum79f25d91997-04-29 20:08:16 +00002623 PyObject *result;
Martin v. Löwis18e16552006-02-15 17:27:45 +00002624 Py_ssize_t i, j;
2625 Py_ssize_t len = PyTuple_Size(tuple);
Guido van Rossum12d12c51993-10-26 17:58:25 +00002626
Guido van Rossumb7b45621995-08-04 04:07:45 +00002627 if (len == 0) {
Walter Dörwaldc3da83f2003-02-04 20:24:45 +00002628 if (PyTuple_CheckExact(tuple))
2629 Py_INCREF(tuple);
2630 else
2631 tuple = PyTuple_New(0);
Guido van Rossumb7b45621995-08-04 04:07:45 +00002632 return tuple;
2633 }
2634
Guido van Rossum79f25d91997-04-29 20:08:16 +00002635 if ((result = PyTuple_New(len)) == NULL)
Guido van Rossum2586bf01993-11-01 16:21:44 +00002636 return NULL;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002637
Guido van Rossum12d12c51993-10-26 17:58:25 +00002638 for (i = j = 0; i < len; ++i) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002639 PyObject *item, *good;
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002640 int ok;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002641
Walter Dörwald8dd19322003-02-10 17:36:40 +00002642 if (tuple->ob_type->tp_as_sequence &&
2643 tuple->ob_type->tp_as_sequence->sq_item) {
2644 item = tuple->ob_type->tp_as_sequence->sq_item(tuple, i);
Walter Dörwaldc58a3a12003-08-18 18:28:45 +00002645 if (item == NULL)
2646 goto Fail_1;
Walter Dörwald8dd19322003-02-10 17:36:40 +00002647 } else {
Alex Martellia9b9c9f2003-04-23 13:34:35 +00002648 PyErr_SetString(PyExc_TypeError, "filter(): unsubscriptable tuple");
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002649 goto Fail_1;
Walter Dörwald8dd19322003-02-10 17:36:40 +00002650 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002651 if (func == Py_None) {
2652 Py_INCREF(item);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002653 good = item;
2654 }
2655 else {
Raymond Hettinger8ae46892003-10-12 19:09:37 +00002656 PyObject *arg = PyTuple_Pack(1, item);
Walter Dörwaldc58a3a12003-08-18 18:28:45 +00002657 if (arg == NULL) {
2658 Py_DECREF(item);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002659 goto Fail_1;
Walter Dörwaldc58a3a12003-08-18 18:28:45 +00002660 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002661 good = PyEval_CallObject(func, arg);
2662 Py_DECREF(arg);
Walter Dörwaldc58a3a12003-08-18 18:28:45 +00002663 if (good == NULL) {
2664 Py_DECREF(item);
Guido van Rossum12d12c51993-10-26 17:58:25 +00002665 goto Fail_1;
Walter Dörwaldc58a3a12003-08-18 18:28:45 +00002666 }
Guido van Rossum12d12c51993-10-26 17:58:25 +00002667 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002668 ok = PyObject_IsTrue(good);
2669 Py_DECREF(good);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002670 if (ok) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002671 if (PyTuple_SetItem(result, j++, item) < 0)
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002672 goto Fail_1;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002673 }
Walter Dörwaldc58a3a12003-08-18 18:28:45 +00002674 else
2675 Py_DECREF(item);
Guido van Rossum12d12c51993-10-26 17:58:25 +00002676 }
2677
Tim Peters4324aa32001-05-28 22:30:08 +00002678 if (_PyTuple_Resize(&result, j) < 0)
Guido van Rossum12d12c51993-10-26 17:58:25 +00002679 return NULL;
2680
Guido van Rossum12d12c51993-10-26 17:58:25 +00002681 return result;
2682
Guido van Rossum12d12c51993-10-26 17:58:25 +00002683Fail_1:
Guido van Rossum79f25d91997-04-29 20:08:16 +00002684 Py_DECREF(result);
Guido van Rossum12d12c51993-10-26 17:58:25 +00002685 return NULL;
2686}
2687
2688
Guido van Rossume77a7571993-11-03 15:01:26 +00002689/* Helper for filter(): filter a string through a function */
Guido van Rossum12d12c51993-10-26 17:58:25 +00002690
Guido van Rossum79f25d91997-04-29 20:08:16 +00002691static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002692filterstring(PyObject *func, PyObject *strobj)
Guido van Rossum12d12c51993-10-26 17:58:25 +00002693{
Guido van Rossum79f25d91997-04-29 20:08:16 +00002694 PyObject *result;
Martin v. Löwis18e16552006-02-15 17:27:45 +00002695 Py_ssize_t i, j;
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002696 Py_ssize_t len = PyString_Size(strobj);
Martin v. Löwis18e16552006-02-15 17:27:45 +00002697 Py_ssize_t outlen = len;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002698
Guido van Rossum79f25d91997-04-29 20:08:16 +00002699 if (func == Py_None) {
Walter Dörwald1918f772003-02-10 13:19:13 +00002700 /* If it's a real string we can return the original,
2701 * as no character is ever false and __getitem__
2702 * does return this character. If it's a subclass
2703 * we must go through the __getitem__ loop */
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002704 if (PyString_CheckExact(strobj)) {
Walter Dörwaldc3da83f2003-02-04 20:24:45 +00002705 Py_INCREF(strobj);
Walter Dörwald1918f772003-02-10 13:19:13 +00002706 return strobj;
2707 }
Guido van Rossum12d12c51993-10-26 17:58:25 +00002708 }
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002709 if ((result = PyString_FromStringAndSize(NULL, len)) == NULL)
Guido van Rossum2586bf01993-11-01 16:21:44 +00002710 return NULL;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002711
Guido van Rossum12d12c51993-10-26 17:58:25 +00002712 for (i = j = 0; i < len; ++i) {
Walter Dörwald1918f772003-02-10 13:19:13 +00002713 PyObject *item;
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002714 int ok;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002715
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002716 item = (*strobj->ob_type->tp_as_sequence->sq_item)(strobj, i);
2717 if (item == NULL)
2718 goto Fail_1;
Walter Dörwald1918f772003-02-10 13:19:13 +00002719 if (func==Py_None) {
2720 ok = 1;
2721 } else {
2722 PyObject *arg, *good;
Raymond Hettinger8ae46892003-10-12 19:09:37 +00002723 arg = PyTuple_Pack(1, item);
Walter Dörwald1918f772003-02-10 13:19:13 +00002724 if (arg == NULL) {
2725 Py_DECREF(item);
2726 goto Fail_1;
2727 }
2728 good = PyEval_CallObject(func, arg);
2729 Py_DECREF(arg);
2730 if (good == NULL) {
2731 Py_DECREF(item);
2732 goto Fail_1;
2733 }
2734 ok = PyObject_IsTrue(good);
2735 Py_DECREF(good);
Tim Peters388ed082001-04-07 20:34:48 +00002736 }
Walter Dörwald903f1e02003-02-04 16:28:00 +00002737 if (ok) {
Martin v. Löwisd96ee902006-02-16 14:37:16 +00002738 Py_ssize_t reslen;
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002739 if (!PyString_Check(item)) {
Walter Dörwald903f1e02003-02-04 16:28:00 +00002740 PyErr_SetString(PyExc_TypeError, "can't filter str to str:"
2741 " __getitem__ returned different type");
2742 Py_DECREF(item);
2743 goto Fail_1;
2744 }
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002745 reslen = PyString_GET_SIZE(item);
Walter Dörwald903f1e02003-02-04 16:28:00 +00002746 if (reslen == 1) {
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002747 PyString_AS_STRING(result)[j++] =
2748 PyString_AS_STRING(item)[0];
Walter Dörwald903f1e02003-02-04 16:28:00 +00002749 } else {
2750 /* do we need more space? */
Gregory P. Smith9d534572008-06-11 07:41:16 +00002751 Py_ssize_t need = j;
2752
2753 /* calculate space requirements while checking for overflow */
2754 if (need > PY_SSIZE_T_MAX - reslen) {
2755 Py_DECREF(item);
2756 goto Fail_1;
2757 }
2758
2759 need += reslen;
2760
2761 if (need > PY_SSIZE_T_MAX - len) {
2762 Py_DECREF(item);
2763 goto Fail_1;
2764 }
2765
2766 need += len;
2767
2768 if (need <= i) {
2769 Py_DECREF(item);
2770 goto Fail_1;
2771 }
2772
2773 need = need - i - 1;
2774
2775 assert(need >= 0);
2776 assert(outlen >= 0);
2777
Walter Dörwald903f1e02003-02-04 16:28:00 +00002778 if (need > outlen) {
2779 /* overallocate, to avoid reallocations */
Gregory P. Smith9d534572008-06-11 07:41:16 +00002780 if (outlen > PY_SSIZE_T_MAX / 2) {
2781 Py_DECREF(item);
2782 return NULL;
2783 }
2784
2785 if (need<2*outlen) {
Walter Dörwald903f1e02003-02-04 16:28:00 +00002786 need = 2*outlen;
Gregory P. Smith9d534572008-06-11 07:41:16 +00002787 }
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002788 if (_PyString_Resize(&result, need)) {
Walter Dörwald903f1e02003-02-04 16:28:00 +00002789 Py_DECREF(item);
2790 return NULL;
2791 }
2792 outlen = need;
2793 }
2794 memcpy(
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002795 PyString_AS_STRING(result) + j,
2796 PyString_AS_STRING(item),
Walter Dörwald903f1e02003-02-04 16:28:00 +00002797 reslen
2798 );
2799 j += reslen;
2800 }
2801 }
Tim Peters388ed082001-04-07 20:34:48 +00002802 Py_DECREF(item);
Guido van Rossum12d12c51993-10-26 17:58:25 +00002803 }
2804
Walter Dörwald903f1e02003-02-04 16:28:00 +00002805 if (j < outlen)
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002806 _PyString_Resize(&result, j);
Guido van Rossum12d12c51993-10-26 17:58:25 +00002807
Guido van Rossum12d12c51993-10-26 17:58:25 +00002808 return result;
2809
Guido van Rossum12d12c51993-10-26 17:58:25 +00002810Fail_1:
Guido van Rossum79f25d91997-04-29 20:08:16 +00002811 Py_DECREF(result);
Guido van Rossum12d12c51993-10-26 17:58:25 +00002812 return NULL;
2813}
Martin v. Löwis8afd7572003-01-25 22:46:11 +00002814
2815#ifdef Py_USING_UNICODE
2816/* Helper for filter(): filter a Unicode object through a function */
2817
2818static PyObject *
2819filterunicode(PyObject *func, PyObject *strobj)
2820{
2821 PyObject *result;
Martin v. Löwis725507b2006-03-07 12:08:51 +00002822 register Py_ssize_t i, j;
Martin v. Löwisd96ee902006-02-16 14:37:16 +00002823 Py_ssize_t len = PyUnicode_GetSize(strobj);
2824 Py_ssize_t outlen = len;
Martin v. Löwis8afd7572003-01-25 22:46:11 +00002825
2826 if (func == Py_None) {
Walter Dörwald1918f772003-02-10 13:19:13 +00002827 /* If it's a real string we can return the original,
2828 * as no character is ever false and __getitem__
2829 * does return this character. If it's a subclass
2830 * we must go through the __getitem__ loop */
2831 if (PyUnicode_CheckExact(strobj)) {
Walter Dörwaldc3da83f2003-02-04 20:24:45 +00002832 Py_INCREF(strobj);
Walter Dörwald1918f772003-02-10 13:19:13 +00002833 return strobj;
2834 }
Martin v. Löwis8afd7572003-01-25 22:46:11 +00002835 }
2836 if ((result = PyUnicode_FromUnicode(NULL, len)) == NULL)
2837 return NULL;
2838
2839 for (i = j = 0; i < len; ++i) {
2840 PyObject *item, *arg, *good;
2841 int ok;
2842
2843 item = (*strobj->ob_type->tp_as_sequence->sq_item)(strobj, i);
2844 if (item == NULL)
2845 goto Fail_1;
Walter Dörwald1918f772003-02-10 13:19:13 +00002846 if (func == Py_None) {
2847 ok = 1;
2848 } else {
Raymond Hettinger8ae46892003-10-12 19:09:37 +00002849 arg = PyTuple_Pack(1, item);
Walter Dörwald1918f772003-02-10 13:19:13 +00002850 if (arg == NULL) {
2851 Py_DECREF(item);
2852 goto Fail_1;
2853 }
2854 good = PyEval_CallObject(func, arg);
2855 Py_DECREF(arg);
2856 if (good == NULL) {
2857 Py_DECREF(item);
2858 goto Fail_1;
2859 }
2860 ok = PyObject_IsTrue(good);
2861 Py_DECREF(good);
Martin v. Löwis8afd7572003-01-25 22:46:11 +00002862 }
Walter Dörwald903f1e02003-02-04 16:28:00 +00002863 if (ok) {
Martin v. Löwisd96ee902006-02-16 14:37:16 +00002864 Py_ssize_t reslen;
Walter Dörwald903f1e02003-02-04 16:28:00 +00002865 if (!PyUnicode_Check(item)) {
Georg Brandl99d7e4e2005-08-31 22:21:15 +00002866 PyErr_SetString(PyExc_TypeError,
Jeremy Hylton1aad9c72003-09-16 03:10:59 +00002867 "can't filter unicode to unicode:"
2868 " __getitem__ returned different type");
Walter Dörwald903f1e02003-02-04 16:28:00 +00002869 Py_DECREF(item);
2870 goto Fail_1;
2871 }
2872 reslen = PyUnicode_GET_SIZE(item);
Georg Brandl99d7e4e2005-08-31 22:21:15 +00002873 if (reslen == 1)
Walter Dörwald903f1e02003-02-04 16:28:00 +00002874 PyUnicode_AS_UNICODE(result)[j++] =
2875 PyUnicode_AS_UNICODE(item)[0];
Jeremy Hylton1aad9c72003-09-16 03:10:59 +00002876 else {
Walter Dörwald903f1e02003-02-04 16:28:00 +00002877 /* do we need more space? */
Martin v. Löwisd96ee902006-02-16 14:37:16 +00002878 Py_ssize_t need = j + reslen + len - i - 1;
Gregory P. Smith9d534572008-06-11 07:41:16 +00002879
2880 /* check that didnt overflow */
2881 if ((j > PY_SSIZE_T_MAX - reslen) ||
2882 ((j + reslen) > PY_SSIZE_T_MAX - len) ||
2883 ((j + reslen + len) < i) ||
2884 ((j + reslen + len - i) <= 0)) {
2885 Py_DECREF(item);
2886 return NULL;
2887 }
2888
2889 assert(need >= 0);
2890 assert(outlen >= 0);
2891
Walter Dörwald903f1e02003-02-04 16:28:00 +00002892 if (need > outlen) {
Georg Brandl99d7e4e2005-08-31 22:21:15 +00002893 /* overallocate,
Jeremy Hylton1aad9c72003-09-16 03:10:59 +00002894 to avoid reallocations */
Gregory P. Smith9d534572008-06-11 07:41:16 +00002895 if (need < 2 * outlen) {
2896 if (outlen > PY_SSIZE_T_MAX / 2) {
2897 Py_DECREF(item);
2898 return NULL;
2899 } else {
2900 need = 2 * outlen;
2901 }
2902 }
2903
Jeremy Hylton364f6be2003-09-16 03:17:16 +00002904 if (PyUnicode_Resize(
2905 &result, need) < 0) {
Walter Dörwald903f1e02003-02-04 16:28:00 +00002906 Py_DECREF(item);
Walter Dörwald531e0002003-02-04 16:57:49 +00002907 goto Fail_1;
Walter Dörwald903f1e02003-02-04 16:28:00 +00002908 }
2909 outlen = need;
2910 }
Jeremy Hylton1aad9c72003-09-16 03:10:59 +00002911 memcpy(PyUnicode_AS_UNICODE(result) + j,
2912 PyUnicode_AS_UNICODE(item),
2913 reslen*sizeof(Py_UNICODE));
Walter Dörwald903f1e02003-02-04 16:28:00 +00002914 j += reslen;
2915 }
2916 }
Martin v. Löwis8afd7572003-01-25 22:46:11 +00002917 Py_DECREF(item);
2918 }
2919
Walter Dörwald903f1e02003-02-04 16:28:00 +00002920 if (j < outlen)
Martin v. Löwis8afd7572003-01-25 22:46:11 +00002921 PyUnicode_Resize(&result, j);
2922
2923 return result;
2924
2925Fail_1:
2926 Py_DECREF(result);
2927 return NULL;
2928}
2929#endif