blob: 4e6f901ab34a8b20ef8df6c347fe42e14d6387dc [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);
Guido van Rossum12d12c51993-10-26 17:58:25 +0000271
Tim Peters0e57abf2001-05-02 07:39:38 +0000272 /* Get a result list. */
Guido van Rossum79f25d91997-04-29 20:08:16 +0000273 if (PyList_Check(seq) && seq->ob_refcnt == 1) {
Tim Peters0e57abf2001-05-02 07:39:38 +0000274 /* Eww - can modify the list in-place. */
Guido van Rossum79f25d91997-04-29 20:08:16 +0000275 Py_INCREF(seq);
Guido van Rossum12d12c51993-10-26 17:58:25 +0000276 result = seq;
277 }
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000278 else {
Tim Peters0e57abf2001-05-02 07:39:38 +0000279 result = PyList_New(len);
280 if (result == NULL)
281 goto Fail_it;
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000282 }
Guido van Rossum12d12c51993-10-26 17:58:25 +0000283
Tim Peters0e57abf2001-05-02 07:39:38 +0000284 /* Build the result list. */
Tim Petersf4848da2001-05-05 00:14:56 +0000285 j = 0;
286 for (;;) {
Guido van Rossumc7903a12002-08-16 07:04:56 +0000287 PyObject *item;
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000288 int ok;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000289
Tim Peters0e57abf2001-05-02 07:39:38 +0000290 item = PyIter_Next(it);
291 if (item == NULL) {
Tim Petersf4848da2001-05-05 00:14:56 +0000292 if (PyErr_Occurred())
293 goto Fail_result_it;
Tim Peters0e57abf2001-05-02 07:39:38 +0000294 break;
Guido van Rossum2d951851994-08-29 12:52:16 +0000295 }
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000296
Neil Schemenauer68973552003-08-14 20:37:34 +0000297 if (func == (PyObject *)&PyBool_Type || func == Py_None) {
Guido van Rossumc7903a12002-08-16 07:04:56 +0000298 ok = PyObject_IsTrue(item);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000299 }
300 else {
Guido van Rossumc7903a12002-08-16 07:04:56 +0000301 PyObject *good;
302 PyTuple_SET_ITEM(arg, 0, item);
303 good = PyObject_Call(func, arg, NULL);
304 PyTuple_SET_ITEM(arg, 0, NULL);
Guido van Rossum58b68731995-01-10 17:40:55 +0000305 if (good == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000306 Py_DECREF(item);
Tim Peters0e57abf2001-05-02 07:39:38 +0000307 goto Fail_result_it;
Guido van Rossum58b68731995-01-10 17:40:55 +0000308 }
Guido van Rossumc7903a12002-08-16 07:04:56 +0000309 ok = PyObject_IsTrue(good);
310 Py_DECREF(good);
Guido van Rossum12d12c51993-10-26 17:58:25 +0000311 }
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000312 if (ok) {
Tim Peters0e57abf2001-05-02 07:39:38 +0000313 if (j < len)
314 PyList_SET_ITEM(result, j, item);
Guido van Rossum2d951851994-08-29 12:52:16 +0000315 else {
Barry Warsawfa77e091999-01-28 18:49:12 +0000316 int status = PyList_Append(result, item);
Barry Warsawfa77e091999-01-28 18:49:12 +0000317 Py_DECREF(item);
318 if (status < 0)
Tim Peters0e57abf2001-05-02 07:39:38 +0000319 goto Fail_result_it;
Guido van Rossum2d951851994-08-29 12:52:16 +0000320 }
Tim Peters0e57abf2001-05-02 07:39:38 +0000321 ++j;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000322 }
Tim Peters0e57abf2001-05-02 07:39:38 +0000323 else
324 Py_DECREF(item);
Guido van Rossum12d12c51993-10-26 17:58:25 +0000325 }
326
Guido van Rossum12d12c51993-10-26 17:58:25 +0000327
Tim Peters0e57abf2001-05-02 07:39:38 +0000328 /* Cut back result list if len is too big. */
Guido van Rossum79f25d91997-04-29 20:08:16 +0000329 if (j < len && PyList_SetSlice(result, j, len, NULL) < 0)
Tim Peters0e57abf2001-05-02 07:39:38 +0000330 goto Fail_result_it;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000331
Tim Peters3c6b1482001-05-21 08:07:05 +0000332 Py_DECREF(it);
Guido van Rossumc7903a12002-08-16 07:04:56 +0000333 Py_DECREF(arg);
Guido van Rossum12d12c51993-10-26 17:58:25 +0000334 return result;
335
Tim Peters0e57abf2001-05-02 07:39:38 +0000336Fail_result_it:
Guido van Rossum79f25d91997-04-29 20:08:16 +0000337 Py_DECREF(result);
Tim Peters0e57abf2001-05-02 07:39:38 +0000338Fail_it:
339 Py_DECREF(it);
Guido van Rossumc7903a12002-08-16 07:04:56 +0000340Fail_arg:
341 Py_DECREF(arg);
Guido van Rossum12d12c51993-10-26 17:58:25 +0000342 return NULL;
343}
344
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000345PyDoc_STRVAR(filter_doc,
Tim Petersd50e5442002-03-09 00:06:26 +0000346"filter(function or None, sequence) -> list, tuple, or string\n"
347"\n"
348"Return those items of sequence for which function(item) is true. If\n"
349"function is None, return the items that are true. If sequence is a tuple\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000350"or string, return the same type, else return a list.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000351
Guido van Rossum79f25d91997-04-29 20:08:16 +0000352static PyObject *
Eric Smitha9f7d622008-02-17 19:46:49 +0000353builtin_format(PyObject *self, PyObject *args)
354{
355 PyObject *value;
356 PyObject *format_spec = NULL;
357
358 if (!PyArg_ParseTuple(args, "O|O:format", &value, &format_spec))
359 return NULL;
360
361 return PyObject_Format(value, format_spec);
362}
363
364PyDoc_STRVAR(format_doc,
365"format(value[, format_spec]) -> string\n\
366\n\
367Returns value.__format__(format_spec)\n\
368format_spec defaults to \"\"");
369
370static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000371builtin_chr(PyObject *self, PyObject *args)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000372{
373 long x;
374 char s[1];
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000375
Guido van Rossum79f25d91997-04-29 20:08:16 +0000376 if (!PyArg_ParseTuple(args, "l:chr", &x))
Guido van Rossum3f5da241990-12-20 15:06:42 +0000377 return NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000378 if (x < 0 || x >= 256) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000379 PyErr_SetString(PyExc_ValueError,
380 "chr() arg not in range(256)");
Guido van Rossum3f5da241990-12-20 15:06:42 +0000381 return NULL;
382 }
Guido van Rossum6bf62da1997-04-11 20:37:35 +0000383 s[0] = (char)x;
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000384 return PyString_FromStringAndSize(s, 1);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000385}
386
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000387PyDoc_STRVAR(chr_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000388"chr(i) -> character\n\
389\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000390Return a string of one character with ordinal i; 0 <= i < 256.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000391
392
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000393#ifdef Py_USING_UNICODE
Guido van Rossum79f25d91997-04-29 20:08:16 +0000394static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000395builtin_unichr(PyObject *self, PyObject *args)
Guido van Rossum09095f32000-03-10 23:00:52 +0000396{
Amaury Forgeot d'Arc39fd6722008-07-31 21:28:03 +0000397 int x;
Guido van Rossum09095f32000-03-10 23:00:52 +0000398
Amaury Forgeot d'Arc39fd6722008-07-31 21:28:03 +0000399 if (!PyArg_ParseTuple(args, "i:unichr", &x))
Guido van Rossum09095f32000-03-10 23:00:52 +0000400 return NULL;
Fredrik Lundh0dcf67e2001-06-26 20:01:56 +0000401
Marc-André Lemburgcc8764c2002-08-11 12:23:04 +0000402 return PyUnicode_FromOrdinal(x);
Guido van Rossum09095f32000-03-10 23:00:52 +0000403}
404
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000405PyDoc_STRVAR(unichr_doc,
Fred Drake078b24f2000-04-13 02:42:50 +0000406"unichr(i) -> Unicode character\n\
Guido van Rossum09095f32000-03-10 23:00:52 +0000407\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000408Return a Unicode string of one character with ordinal i; 0 <= i <= 0x10ffff.");
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000409#endif
Guido van Rossum09095f32000-03-10 23:00:52 +0000410
411
412static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000413builtin_cmp(PyObject *self, PyObject *args)
Guido van Rossuma9e7dc11992-10-18 18:53:57 +0000414{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000415 PyObject *a, *b;
Guido van Rossum09df08a1998-05-22 00:51:39 +0000416 int c;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000417
Raymond Hettingerea3fdf42002-12-29 16:33:45 +0000418 if (!PyArg_UnpackTuple(args, "cmp", 2, 2, &a, &b))
Guido van Rossuma9e7dc11992-10-18 18:53:57 +0000419 return NULL;
Guido van Rossum09df08a1998-05-22 00:51:39 +0000420 if (PyObject_Cmp(a, b, &c) < 0)
Guido van Rossumc8b6df91997-05-23 00:06:51 +0000421 return NULL;
Guido van Rossum09df08a1998-05-22 00:51:39 +0000422 return PyInt_FromLong((long)c);
Guido van Rossuma9e7dc11992-10-18 18:53:57 +0000423}
424
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000425PyDoc_STRVAR(cmp_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000426"cmp(x, y) -> integer\n\
427\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000428Return negative if x<y, zero if x==y, positive if x>y.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000429
430
Guido van Rossum79f25d91997-04-29 20:08:16 +0000431static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000432builtin_coerce(PyObject *self, PyObject *args)
Guido van Rossum6a00cd81995-01-07 12:39:01 +0000433{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000434 PyObject *v, *w;
435 PyObject *res;
Guido van Rossum5524a591995-01-10 15:26:20 +0000436
Benjamin Peterson9f4f4812008-04-27 03:01:45 +0000437 if (PyErr_WarnPy3k("coerce() not supported in 3.x", 1) < 0)
Neal Norwitzdf25efe2007-05-23 06:58:36 +0000438 return NULL;
439
Raymond Hettingerea3fdf42002-12-29 16:33:45 +0000440 if (!PyArg_UnpackTuple(args, "coerce", 2, 2, &v, &w))
Guido van Rossum5524a591995-01-10 15:26:20 +0000441 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000442 if (PyNumber_Coerce(&v, &w) < 0)
Guido van Rossum04691fc1992-08-12 15:35:34 +0000443 return NULL;
Raymond Hettinger8ae46892003-10-12 19:09:37 +0000444 res = PyTuple_Pack(2, v, w);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000445 Py_DECREF(v);
446 Py_DECREF(w);
Guido van Rossum04691fc1992-08-12 15:35:34 +0000447 return res;
448}
449
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000450PyDoc_STRVAR(coerce_doc,
Martin v. Löwis8d494f32004-08-25 10:42:41 +0000451"coerce(x, y) -> (x1, y1)\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000452\n\
Martin v. Löwis8d494f32004-08-25 10:42:41 +0000453Return a tuple consisting of the two numeric arguments converted to\n\
454a common type, using the same rules as used by arithmetic operations.\n\
455If coercion is not possible, raise TypeError.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000456
Guido van Rossum79f25d91997-04-29 20:08:16 +0000457static PyObject *
Georg Brandl5240d742007-03-13 20:46:32 +0000458builtin_compile(PyObject *self, PyObject *args, PyObject *kwds)
Guido van Rossum5b722181993-03-30 17:46:03 +0000459{
460 char *str;
461 char *filename;
462 char *startstr;
Georg Brandlf2bfd542008-03-29 13:24:23 +0000463 int mode = -1;
Tim Peters6cd6a822001-08-17 22:11:27 +0000464 int dont_inherit = 0;
465 int supplied_flags = 0;
Tim Peters5ba58662001-07-16 02:29:45 +0000466 PyCompilerFlags cf;
Neal Norwitz3a9a3e72005-11-27 20:38:31 +0000467 PyObject *result = NULL, *cmd, *tmp = NULL;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000468 Py_ssize_t length;
Georg Brandl5240d742007-03-13 20:46:32 +0000469 static char *kwlist[] = {"source", "filename", "mode", "flags",
470 "dont_inherit", NULL};
Georg Brandlf2bfd542008-03-29 13:24:23 +0000471 int start[] = {Py_file_input, Py_eval_input, Py_single_input};
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000472
Georg Brandl5240d742007-03-13 20:46:32 +0000473 if (!PyArg_ParseTupleAndKeywords(args, kwds, "Oss|ii:compile",
474 kwlist, &cmd, &filename, &startstr,
475 &supplied_flags, &dont_inherit))
Guido van Rossum5b722181993-03-30 17:46:03 +0000476 return NULL;
Tim Peters6cd6a822001-08-17 22:11:27 +0000477
Just van Rossum3aaf42c2003-02-10 08:21:10 +0000478 cf.cf_flags = supplied_flags;
479
Georg Brandlfc8eef32008-03-28 12:11:56 +0000480 if (supplied_flags &
481 ~(PyCF_MASK | PyCF_MASK_OBSOLETE | PyCF_DONT_IMPLY_DEDENT | PyCF_ONLY_AST))
482 {
483 PyErr_SetString(PyExc_ValueError,
484 "compile(): unrecognised flags");
485 return NULL;
486 }
487 /* XXX Warn if (supplied_flags & PyCF_MASK_OBSOLETE) != 0? */
488
489 if (!dont_inherit) {
490 PyEval_MergeCompilerFlags(&cf);
491 }
492
Georg Brandlf2bfd542008-03-29 13:24:23 +0000493 if (strcmp(startstr, "exec") == 0)
494 mode = 0;
495 else if (strcmp(startstr, "eval") == 0)
496 mode = 1;
497 else if (strcmp(startstr, "single") == 0)
498 mode = 2;
499 else {
500 PyErr_SetString(PyExc_ValueError,
501 "compile() arg 3 must be 'exec', 'eval' or 'single'");
502 return NULL;
503 }
504
Georg Brandlfc8eef32008-03-28 12:11:56 +0000505 if (PyAST_Check(cmd)) {
506 if (supplied_flags & PyCF_ONLY_AST) {
507 Py_INCREF(cmd);
508 result = cmd;
509 }
510 else {
511 PyArena *arena;
512 mod_ty mod;
513
514 arena = PyArena_New();
Georg Brandlf2bfd542008-03-29 13:24:23 +0000515 mod = PyAST_obj2mod(cmd, arena, mode);
Georg Brandlfc8eef32008-03-28 12:11:56 +0000516 if (mod == NULL) {
517 PyArena_Free(arena);
518 return NULL;
519 }
520 result = (PyObject*)PyAST_Compile(mod, filename,
521 &cf, arena);
522 PyArena_Free(arena);
523 }
524 return result;
525 }
526
Just van Rossum3aaf42c2003-02-10 08:21:10 +0000527#ifdef Py_USING_UNICODE
528 if (PyUnicode_Check(cmd)) {
529 tmp = PyUnicode_AsUTF8String(cmd);
530 if (tmp == NULL)
531 return NULL;
532 cmd = tmp;
533 cf.cf_flags |= PyCF_SOURCE_IS_UTF8;
534 }
535#endif
Tim Peters6cd6a822001-08-17 22:11:27 +0000536
Georg Brandlfc8eef32008-03-28 12:11:56 +0000537 if (PyObject_AsReadBuffer(cmd, (const void **)&str, &length))
Neal Norwitz3a9a3e72005-11-27 20:38:31 +0000538 goto cleanup;
Georg Brandlfc8eef32008-03-28 12:11:56 +0000539 if ((size_t)length != strlen(str)) {
540 PyErr_SetString(PyExc_TypeError,
541 "compile() expected string without null bytes");
542 goto cleanup;
Tim Peters6cd6a822001-08-17 22:11:27 +0000543 }
Georg Brandlf2bfd542008-03-29 13:24:23 +0000544 result = Py_CompileStringFlags(str, filename, start[mode], &cf);
Neal Norwitz3a9a3e72005-11-27 20:38:31 +0000545cleanup:
Just van Rossum3aaf42c2003-02-10 08:21:10 +0000546 Py_XDECREF(tmp);
547 return result;
Guido van Rossum5b722181993-03-30 17:46:03 +0000548}
549
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000550PyDoc_STRVAR(compile_doc,
Tim Peters6cd6a822001-08-17 22:11:27 +0000551"compile(source, filename, mode[, flags[, dont_inherit]]) -> code object\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000552\n\
553Compile the source string (a Python module, statement or expression)\n\
554into a code object that can be executed by the exec statement or eval().\n\
555The filename will be used for run-time error messages.\n\
556The mode must be 'exec' to compile a module, 'single' to compile a\n\
Tim Peters6cd6a822001-08-17 22:11:27 +0000557single (interactive) statement, or 'eval' to compile an expression.\n\
558The flags argument, if present, controls which future statements influence\n\
559the compilation of the code.\n\
560The dont_inherit argument, if non-zero, stops the compilation inheriting\n\
561the effects of any future statements in effect in the code calling\n\
562compile; if absent or zero these statements do influence the compilation,\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000563in addition to any features explicitly specified.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000564
Guido van Rossum79f25d91997-04-29 20:08:16 +0000565static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000566builtin_dir(PyObject *self, PyObject *args)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000567{
Tim Peters5d2b77c2001-09-03 05:47:38 +0000568 PyObject *arg = NULL;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000569
Raymond Hettingerea3fdf42002-12-29 16:33:45 +0000570 if (!PyArg_UnpackTuple(args, "dir", 0, 1, &arg))
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000571 return NULL;
Tim Peters7eea37e2001-09-04 22:08:56 +0000572 return PyObject_Dir(arg);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000573}
574
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000575PyDoc_STRVAR(dir_doc,
Tim Peters5d2b77c2001-09-03 05:47:38 +0000576"dir([object]) -> list of strings\n"
577"\n"
Georg Brandl871f1bc2007-03-12 13:17:36 +0000578"If called without an argument, return the names in the current scope.\n"
579"Else, return an alphabetized list of names comprising (some of) the attributes\n"
580"of the given object, and of attributes reachable from it.\n"
581"If the object supplies a method named __dir__, it will be used; otherwise\n"
582"the default dir() logic is used and returns:\n"
583" for a module object: the module's attributes.\n"
584" for a class object: its attributes, and recursively the attributes\n"
585" of its bases.\n"
Georg Brandl3bb15672007-03-13 07:23:16 +0000586" for any other object: its attributes, its class's attributes, and\n"
Georg Brandl871f1bc2007-03-12 13:17:36 +0000587" recursively the attributes of its class's base classes.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000588
Guido van Rossum79f25d91997-04-29 20:08:16 +0000589static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000590builtin_divmod(PyObject *self, PyObject *args)
Guido van Rossum6a00cd81995-01-07 12:39:01 +0000591{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000592 PyObject *v, *w;
Guido van Rossum6a00cd81995-01-07 12:39:01 +0000593
Raymond Hettingerea3fdf42002-12-29 16:33:45 +0000594 if (!PyArg_UnpackTuple(args, "divmod", 2, 2, &v, &w))
Guido van Rossum6a00cd81995-01-07 12:39:01 +0000595 return NULL;
Guido van Rossum09df08a1998-05-22 00:51:39 +0000596 return PyNumber_Divmod(v, w);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000597}
598
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000599PyDoc_STRVAR(divmod_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000600"divmod(x, y) -> (div, mod)\n\
601\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000602Return the tuple ((x-x%y)/y, x%y). Invariant: div*y + mod == x.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000603
604
Guido van Rossum79f25d91997-04-29 20:08:16 +0000605static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000606builtin_eval(PyObject *self, PyObject *args)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000607{
Just van Rossum3aaf42c2003-02-10 08:21:10 +0000608 PyObject *cmd, *result, *tmp = NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000609 PyObject *globals = Py_None, *locals = Py_None;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000610 char *str;
Tim Peters9fa96be2001-08-17 23:04:59 +0000611 PyCompilerFlags cf;
Guido van Rossum590baa41993-11-30 13:40:46 +0000612
Raymond Hettinger214b1c32004-07-02 06:41:07 +0000613 if (!PyArg_UnpackTuple(args, "eval", 1, 3, &cmd, &globals, &locals))
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000614 return NULL;
Raymond Hettinger214b1c32004-07-02 06:41:07 +0000615 if (locals != Py_None && !PyMapping_Check(locals)) {
616 PyErr_SetString(PyExc_TypeError, "locals must be a mapping");
Raymond Hettinger513ffe82004-07-06 13:44:41 +0000617 return NULL;
Raymond Hettinger214b1c32004-07-02 06:41:07 +0000618 }
619 if (globals != Py_None && !PyDict_Check(globals)) {
Georg Brandl99d7e4e2005-08-31 22:21:15 +0000620 PyErr_SetString(PyExc_TypeError, PyMapping_Check(globals) ?
Raymond Hettinger214b1c32004-07-02 06:41:07 +0000621 "globals must be a real dict; try eval(expr, {}, mapping)"
622 : "globals must be a dict");
Raymond Hettinger513ffe82004-07-06 13:44:41 +0000623 return NULL;
Raymond Hettinger214b1c32004-07-02 06:41:07 +0000624 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000625 if (globals == Py_None) {
626 globals = PyEval_GetGlobals();
627 if (locals == Py_None)
628 locals = PyEval_GetLocals();
Guido van Rossum6135a871995-01-09 17:53:26 +0000629 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000630 else if (locals == Py_None)
Guido van Rossum6135a871995-01-09 17:53:26 +0000631 locals = globals;
Tim Peters9fa96be2001-08-17 23:04:59 +0000632
Georg Brandl77c85e62005-09-15 10:46:13 +0000633 if (globals == NULL || locals == NULL) {
634 PyErr_SetString(PyExc_TypeError,
635 "eval must be given globals and locals "
636 "when called without a frame");
637 return NULL;
638 }
639
Guido van Rossum79f25d91997-04-29 20:08:16 +0000640 if (PyDict_GetItemString(globals, "__builtins__") == NULL) {
641 if (PyDict_SetItemString(globals, "__builtins__",
642 PyEval_GetBuiltins()) != 0)
Guido van Rossum6135a871995-01-09 17:53:26 +0000643 return NULL;
644 }
Tim Peters9fa96be2001-08-17 23:04:59 +0000645
Jeremy Hylton15c1c4f2001-07-30 21:50:55 +0000646 if (PyCode_Check(cmd)) {
Jeremy Hylton733c8932001-12-13 19:51:56 +0000647 if (PyCode_GetNumFree((PyCodeObject *)cmd) > 0) {
Jeremy Hylton15c1c4f2001-07-30 21:50:55 +0000648 PyErr_SetString(PyExc_TypeError,
649 "code object passed to eval() may not contain free variables");
650 return NULL;
651 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000652 return PyEval_EvalCode((PyCodeObject *) cmd, globals, locals);
Jeremy Hylton15c1c4f2001-07-30 21:50:55 +0000653 }
Tim Peters9fa96be2001-08-17 23:04:59 +0000654
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000655 if (!PyString_Check(cmd) &&
Marc-André Lemburgd1ba4432000-09-19 21:04:18 +0000656 !PyUnicode_Check(cmd)) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000657 PyErr_SetString(PyExc_TypeError,
Fred Drake661ea262000-10-24 19:57:45 +0000658 "eval() arg 1 must be a string or code object");
Guido van Rossum94390a41992-08-14 15:14:30 +0000659 return NULL;
660 }
Just van Rossum3aaf42c2003-02-10 08:21:10 +0000661 cf.cf_flags = 0;
662
663#ifdef Py_USING_UNICODE
664 if (PyUnicode_Check(cmd)) {
665 tmp = PyUnicode_AsUTF8String(cmd);
666 if (tmp == NULL)
667 return NULL;
668 cmd = tmp;
669 cf.cf_flags |= PyCF_SOURCE_IS_UTF8;
670 }
671#endif
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000672 if (PyString_AsStringAndSize(cmd, &str, NULL)) {
Neal Norwitz3a9a3e72005-11-27 20:38:31 +0000673 Py_XDECREF(tmp);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000674 return NULL;
Neal Norwitz3a9a3e72005-11-27 20:38:31 +0000675 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000676 while (*str == ' ' || *str == '\t')
677 str++;
Tim Peters9fa96be2001-08-17 23:04:59 +0000678
Tim Peters9fa96be2001-08-17 23:04:59 +0000679 (void)PyEval_MergeCompilerFlags(&cf);
Just van Rossum3aaf42c2003-02-10 08:21:10 +0000680 result = PyRun_StringFlags(str, Py_eval_input, globals, locals, &cf);
681 Py_XDECREF(tmp);
682 return result;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000683}
684
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000685PyDoc_STRVAR(eval_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000686"eval(source[, globals[, locals]]) -> value\n\
687\n\
688Evaluate the source in the context of globals and locals.\n\
689The source may be a string representing a Python expression\n\
690or a code object as returned by compile().\n\
Neal Norwitz477ca1c2006-09-05 02:25:41 +0000691The globals must be a dictionary and locals can be any mapping,\n\
Raymond Hettinger214b1c32004-07-02 06:41:07 +0000692defaulting to the current globals and locals.\n\
693If only globals is given, locals defaults to it.\n");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000694
695
Guido van Rossum79f25d91997-04-29 20:08:16 +0000696static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000697builtin_execfile(PyObject *self, PyObject *args)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000698{
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000699 char *filename;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000700 PyObject *globals = Py_None, *locals = Py_None;
701 PyObject *res;
Guido van Rossumb3a639e2001-09-05 13:37:47 +0000702 FILE* fp = NULL;
Tim Peters5ba58662001-07-16 02:29:45 +0000703 PyCompilerFlags cf;
Martin v. Löwis6b3a2c42001-08-08 05:30:36 +0000704 int exists;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000705
Benjamin Peterson9f4f4812008-04-27 03:01:45 +0000706 if (PyErr_WarnPy3k("execfile() not supported in 3.x; use exec()",
Benjamin Petersonf19a7b92008-04-27 18:40:21 +0000707 1) < 0)
Neal Norwitzdf25efe2007-05-23 06:58:36 +0000708 return NULL;
709
Raymond Hettinger66bd2332004-08-02 08:30:07 +0000710 if (!PyArg_ParseTuple(args, "s|O!O:execfile",
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000711 &filename,
Guido van Rossum79f25d91997-04-29 20:08:16 +0000712 &PyDict_Type, &globals,
Raymond Hettinger66bd2332004-08-02 08:30:07 +0000713 &locals))
Guido van Rossum0f61f8a1992-02-25 18:55:05 +0000714 return NULL;
Raymond Hettinger66bd2332004-08-02 08:30:07 +0000715 if (locals != Py_None && !PyMapping_Check(locals)) {
716 PyErr_SetString(PyExc_TypeError, "locals must be a mapping");
717 return NULL;
718 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000719 if (globals == Py_None) {
720 globals = PyEval_GetGlobals();
721 if (locals == Py_None)
722 locals = PyEval_GetLocals();
Guido van Rossum6135a871995-01-09 17:53:26 +0000723 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000724 else if (locals == Py_None)
Guido van Rossum6135a871995-01-09 17:53:26 +0000725 locals = globals;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000726 if (PyDict_GetItemString(globals, "__builtins__") == NULL) {
727 if (PyDict_SetItemString(globals, "__builtins__",
728 PyEval_GetBuiltins()) != 0)
Guido van Rossum6135a871995-01-09 17:53:26 +0000729 return NULL;
730 }
Martin v. Löwis6b3a2c42001-08-08 05:30:36 +0000731
732 exists = 0;
733 /* Test for existence or directory. */
Martin v. Löwis3484a182002-03-09 12:07:51 +0000734#if defined(PLAN9)
735 {
736 Dir *d;
737
738 if ((d = dirstat(filename))!=nil) {
739 if(d->mode & DMDIR)
740 werrstr("is a directory");
741 else
742 exists = 1;
743 free(d);
744 }
Martin v. Löwis6b3a2c42001-08-08 05:30:36 +0000745 }
Martin v. Löwis3484a182002-03-09 12:07:51 +0000746#elif defined(RISCOS)
Guido van Rossume2ae77b2001-10-24 20:42:55 +0000747 if (object_exists(filename)) {
748 if (isdir(filename))
749 errno = EISDIR;
750 else
751 exists = 1;
752 }
Martin v. Löwis3484a182002-03-09 12:07:51 +0000753#else /* standard Posix */
754 {
755 struct stat s;
756 if (stat(filename, &s) == 0) {
757 if (S_ISDIR(s.st_mode))
Andrew MacIntyreda4d6cb2004-03-29 11:53:38 +0000758# if defined(PYOS_OS2) && defined(PYCC_VACPP)
Martin v. Löwis3484a182002-03-09 12:07:51 +0000759 errno = EOS2ERR;
760# else
761 errno = EISDIR;
762# endif
763 else
764 exists = 1;
765 }
766 }
767#endif
Martin v. Löwis6b3a2c42001-08-08 05:30:36 +0000768
769 if (exists) {
770 Py_BEGIN_ALLOW_THREADS
Jack Jansen7b8c7542002-04-14 20:12:41 +0000771 fp = fopen(filename, "r" PY_STDIOTEXTMODE);
Martin v. Löwis6b3a2c42001-08-08 05:30:36 +0000772 Py_END_ALLOW_THREADS
773
774 if (fp == NULL) {
775 exists = 0;
776 }
777 }
778
779 if (!exists) {
Peter Schneider-Kamp4c013422002-08-27 16:58:00 +0000780 PyErr_SetFromErrnoWithFilename(PyExc_IOError, filename);
Guido van Rossum0f61f8a1992-02-25 18:55:05 +0000781 return NULL;
782 }
Tim Peters5ba58662001-07-16 02:29:45 +0000783 cf.cf_flags = 0;
784 if (PyEval_MergeCompilerFlags(&cf))
Tim Peters748b8bb2001-04-28 08:20:22 +0000785 res = PyRun_FileExFlags(fp, filename, Py_file_input, globals,
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000786 locals, 1, &cf);
Tim Peters5ba58662001-07-16 02:29:45 +0000787 else
Tim Peters748b8bb2001-04-28 08:20:22 +0000788 res = PyRun_FileEx(fp, filename, Py_file_input, globals,
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000789 locals, 1);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000790 return res;
Guido van Rossum0f61f8a1992-02-25 18:55:05 +0000791}
792
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000793PyDoc_STRVAR(execfile_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000794"execfile(filename[, globals[, locals]])\n\
795\n\
796Read and execute a Python script from a file.\n\
797The globals and locals are dictionaries, defaulting to the current\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000798globals and locals. If only globals is given, locals defaults to it.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000799
800
Guido van Rossum79f25d91997-04-29 20:08:16 +0000801static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000802builtin_getattr(PyObject *self, PyObject *args)
Guido van Rossum33894be1992-01-27 16:53:09 +0000803{
Guido van Rossum950ff291998-06-29 13:38:57 +0000804 PyObject *v, *result, *dflt = NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000805 PyObject *name;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000806
Raymond Hettingerea3fdf42002-12-29 16:33:45 +0000807 if (!PyArg_UnpackTuple(args, "getattr", 2, 3, &v, &name, &dflt))
Guido van Rossum33894be1992-01-27 16:53:09 +0000808 return NULL;
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000809#ifdef Py_USING_UNICODE
Jeremy Hylton0eb11152001-07-30 22:39:31 +0000810 if (PyUnicode_Check(name)) {
811 name = _PyUnicode_AsDefaultEncodedString(name, NULL);
812 if (name == NULL)
813 return NULL;
814 }
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000815#endif
Jeremy Hylton0eb11152001-07-30 22:39:31 +0000816
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000817 if (!PyString_Check(name)) {
Jeremy Hylton0eb11152001-07-30 22:39:31 +0000818 PyErr_SetString(PyExc_TypeError,
Alex Martellia9b9c9f2003-04-23 13:34:35 +0000819 "getattr(): attribute name must be string");
Jeremy Hylton0eb11152001-07-30 22:39:31 +0000820 return NULL;
821 }
Guido van Rossum950ff291998-06-29 13:38:57 +0000822 result = PyObject_GetAttr(v, name);
Guido van Rossumd8923572001-10-16 21:31:32 +0000823 if (result == NULL && dflt != NULL &&
824 PyErr_ExceptionMatches(PyExc_AttributeError))
825 {
Guido van Rossum950ff291998-06-29 13:38:57 +0000826 PyErr_Clear();
827 Py_INCREF(dflt);
828 result = dflt;
829 }
830 return result;
Guido van Rossum9bfef441993-03-29 10:43:31 +0000831}
832
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000833PyDoc_STRVAR(getattr_doc,
Guido van Rossum950ff291998-06-29 13:38:57 +0000834"getattr(object, name[, default]) -> value\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000835\n\
Guido van Rossum950ff291998-06-29 13:38:57 +0000836Get a named attribute from an object; getattr(x, 'y') is equivalent to x.y.\n\
837When a default argument is given, it is returned when the attribute doesn't\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000838exist; without it, an exception is raised in that case.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000839
840
Guido van Rossum79f25d91997-04-29 20:08:16 +0000841static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000842builtin_globals(PyObject *self)
Guido van Rossum872537c1995-07-07 22:43:42 +0000843{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000844 PyObject *d;
Guido van Rossum872537c1995-07-07 22:43:42 +0000845
Guido van Rossum79f25d91997-04-29 20:08:16 +0000846 d = PyEval_GetGlobals();
Neal Norwitz43bd4db2006-08-12 01:46:42 +0000847 Py_XINCREF(d);
Guido van Rossum872537c1995-07-07 22:43:42 +0000848 return d;
849}
850
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000851PyDoc_STRVAR(globals_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000852"globals() -> dictionary\n\
853\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000854Return the dictionary containing the current scope's global variables.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000855
856
Guido van Rossum79f25d91997-04-29 20:08:16 +0000857static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000858builtin_hasattr(PyObject *self, PyObject *args)
Guido van Rossum9bfef441993-03-29 10:43:31 +0000859{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000860 PyObject *v;
861 PyObject *name;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000862
Raymond Hettingerea3fdf42002-12-29 16:33:45 +0000863 if (!PyArg_UnpackTuple(args, "hasattr", 2, 2, &v, &name))
Guido van Rossum9bfef441993-03-29 10:43:31 +0000864 return NULL;
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000865#ifdef Py_USING_UNICODE
Jeremy Hylton302b54a2001-07-30 22:45:19 +0000866 if (PyUnicode_Check(name)) {
867 name = _PyUnicode_AsDefaultEncodedString(name, NULL);
868 if (name == NULL)
869 return NULL;
870 }
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000871#endif
Jeremy Hylton302b54a2001-07-30 22:45:19 +0000872
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000873 if (!PyString_Check(name)) {
Jeremy Hylton302b54a2001-07-30 22:45:19 +0000874 PyErr_SetString(PyExc_TypeError,
Alex Martellia9b9c9f2003-04-23 13:34:35 +0000875 "hasattr(): attribute name must be string");
Jeremy Hylton302b54a2001-07-30 22:45:19 +0000876 return NULL;
877 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000878 v = PyObject_GetAttr(v, name);
Guido van Rossum9bfef441993-03-29 10:43:31 +0000879 if (v == NULL) {
Benjamin Petersonb9030f42008-05-12 00:41:23 +0000880 if (!PyErr_ExceptionMatches(PyExc_Exception))
881 return NULL;
882 else {
883 PyErr_Clear();
884 Py_INCREF(Py_False);
885 return Py_False;
886 }
Guido van Rossum9bfef441993-03-29 10:43:31 +0000887 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000888 Py_DECREF(v);
Guido van Rossum09df08a1998-05-22 00:51:39 +0000889 Py_INCREF(Py_True);
890 return Py_True;
Guido van Rossum33894be1992-01-27 16:53:09 +0000891}
892
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000893PyDoc_STRVAR(hasattr_doc,
Guido van Rossum77f6a652002-04-03 22:41:51 +0000894"hasattr(object, name) -> bool\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000895\n\
896Return whether the object has an attribute with the given name.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000897(This is done by calling getattr(object, name) and catching exceptions.)");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000898
899
Guido van Rossum79f25d91997-04-29 20:08:16 +0000900static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000901builtin_id(PyObject *self, PyObject *v)
Guido van Rossum5b722181993-03-30 17:46:03 +0000902{
Guido van Rossum106f2da2000-06-28 21:12:25 +0000903 return PyLong_FromVoidPtr(v);
Guido van Rossum5b722181993-03-30 17:46:03 +0000904}
905
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000906PyDoc_STRVAR(id_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000907"id(object) -> integer\n\
908\n\
909Return the identity of an object. This is guaranteed to be unique among\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000910simultaneously existing objects. (Hint: it's the object's memory address.)");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000911
912
Guido van Rossum79f25d91997-04-29 20:08:16 +0000913static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000914builtin_map(PyObject *self, PyObject *args)
Guido van Rossum12d12c51993-10-26 17:58:25 +0000915{
916 typedef struct {
Tim Peters4e9afdc2001-05-03 23:54:49 +0000917 PyObject *it; /* the iterator object */
918 int saw_StopIteration; /* bool: did the iterator end? */
Guido van Rossum12d12c51993-10-26 17:58:25 +0000919 } sequence;
920
Guido van Rossum79f25d91997-04-29 20:08:16 +0000921 PyObject *func, *result;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000922 sequence *seqs = NULL, *sqp;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000923 Py_ssize_t n, len;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000924 register int i, j;
925
Guido van Rossum79f25d91997-04-29 20:08:16 +0000926 n = PyTuple_Size(args);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000927 if (n < 2) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000928 PyErr_SetString(PyExc_TypeError,
929 "map() requires at least two args");
Guido van Rossum12d12c51993-10-26 17:58:25 +0000930 return NULL;
931 }
932
Guido van Rossum79f25d91997-04-29 20:08:16 +0000933 func = PyTuple_GetItem(args, 0);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000934 n--;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000935
Neal Norwitz53152a12008-02-24 02:20:25 +0000936 if (func == Py_None) {
Benjamin Peterson9f4f4812008-04-27 03:01:45 +0000937 if (PyErr_WarnPy3k("map(None, ...) not supported in 3.x; "
Benjamin Petersonf19a7b92008-04-27 18:40:21 +0000938 "use list(...)", 1) < 0)
Neal Norwitz53152a12008-02-24 02:20:25 +0000939 return NULL;
940 if (n == 1) {
941 /* map(None, S) is the same as list(S). */
942 return PySequence_List(PyTuple_GetItem(args, 1));
943 }
Guido van Rossumfa4ac711998-07-10 17:37:30 +0000944 }
945
Tim Peters4e9afdc2001-05-03 23:54:49 +0000946 /* Get space for sequence descriptors. Must NULL out the iterator
947 * pointers so that jumping to Fail_2 later doesn't see trash.
948 */
Guido van Rossum79f25d91997-04-29 20:08:16 +0000949 if ((seqs = PyMem_NEW(sequence, n)) == NULL) {
950 PyErr_NoMemory();
Tim Peters4e9afdc2001-05-03 23:54:49 +0000951 return NULL;
952 }
953 for (i = 0; i < n; ++i) {
954 seqs[i].it = (PyObject*)NULL;
955 seqs[i].saw_StopIteration = 0;
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000956 }
Guido van Rossum12d12c51993-10-26 17:58:25 +0000957
Tim Peters4e9afdc2001-05-03 23:54:49 +0000958 /* Do a first pass to obtain iterators for the arguments, and set len
959 * to the largest of their lengths.
Tim Peters748b8bb2001-04-28 08:20:22 +0000960 */
Tim Peters4e9afdc2001-05-03 23:54:49 +0000961 len = 0;
962 for (i = 0, sqp = seqs; i < n; ++i, ++sqp) {
963 PyObject *curseq;
Martin v. Löwisd96ee902006-02-16 14:37:16 +0000964 Py_ssize_t curlen;
Guido van Rossumad991772001-01-12 16:03:05 +0000965
Tim Peters4e9afdc2001-05-03 23:54:49 +0000966 /* Get iterator. */
967 curseq = PyTuple_GetItem(args, i+1);
968 sqp->it = PyObject_GetIter(curseq);
969 if (sqp->it == NULL) {
Guido van Rossum12d12c51993-10-26 17:58:25 +0000970 static char errmsg[] =
Tim Peters4e9afdc2001-05-03 23:54:49 +0000971 "argument %d to map() must support iteration";
Guido van Rossum15e33a41997-04-30 19:00:27 +0000972 char errbuf[sizeof(errmsg) + 25];
Jeremy Hylton518ab1c2001-11-28 20:42:20 +0000973 PyOS_snprintf(errbuf, sizeof(errbuf), errmsg, i+2);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000974 PyErr_SetString(PyExc_TypeError, errbuf);
Guido van Rossum12d12c51993-10-26 17:58:25 +0000975 goto Fail_2;
976 }
977
Tim Peters4e9afdc2001-05-03 23:54:49 +0000978 /* Update len. */
Raymond Hettinger4e2f7142007-12-06 00:56:53 +0000979 curlen = _PyObject_LengthHint(curseq, 8);
Raymond Hettinger77f3c872004-01-04 08:54:44 +0000980 if (curlen > len)
981 len = curlen;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000982 }
983
Tim Peters4e9afdc2001-05-03 23:54:49 +0000984 /* Get space for the result list. */
Guido van Rossum79f25d91997-04-29 20:08:16 +0000985 if ((result = (PyObject *) PyList_New(len)) == NULL)
Guido van Rossum12d12c51993-10-26 17:58:25 +0000986 goto Fail_2;
987
Tim Peters4e9afdc2001-05-03 23:54:49 +0000988 /* Iterate over the sequences until all have stopped. */
Guido van Rossum2d951851994-08-29 12:52:16 +0000989 for (i = 0; ; ++i) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000990 PyObject *alist, *item=NULL, *value;
Tim Peters4e9afdc2001-05-03 23:54:49 +0000991 int numactive = 0;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000992
Guido van Rossum79f25d91997-04-29 20:08:16 +0000993 if (func == Py_None && n == 1)
Guido van Rossum32120311995-07-10 13:52:21 +0000994 alist = NULL;
Tim Peters4e9afdc2001-05-03 23:54:49 +0000995 else if ((alist = PyTuple_New(n)) == NULL)
996 goto Fail_1;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000997
998 for (j = 0, sqp = seqs; j < n; ++j, ++sqp) {
Tim Peters4e9afdc2001-05-03 23:54:49 +0000999 if (sqp->saw_StopIteration) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001000 Py_INCREF(Py_None);
1001 item = Py_None;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001002 }
Guido van Rossum12d12c51993-10-26 17:58:25 +00001003 else {
Tim Peters4e9afdc2001-05-03 23:54:49 +00001004 item = PyIter_Next(sqp->it);
1005 if (item)
1006 ++numactive;
1007 else {
Tim Peters4e9afdc2001-05-03 23:54:49 +00001008 if (PyErr_Occurred()) {
Tim Petersf4848da2001-05-05 00:14:56 +00001009 Py_XDECREF(alist);
1010 goto Fail_1;
Guido van Rossum2d951851994-08-29 12:52:16 +00001011 }
Tim Peters4e9afdc2001-05-03 23:54:49 +00001012 Py_INCREF(Py_None);
1013 item = Py_None;
1014 sqp->saw_StopIteration = 1;
Guido van Rossum2d951851994-08-29 12:52:16 +00001015 }
Guido van Rossum12d12c51993-10-26 17:58:25 +00001016 }
Tim Peters4e9afdc2001-05-03 23:54:49 +00001017 if (alist)
1018 PyTuple_SET_ITEM(alist, j, item);
1019 else
Guido van Rossum2d951851994-08-29 12:52:16 +00001020 break;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001021 }
1022
Guido van Rossum32120311995-07-10 13:52:21 +00001023 if (!alist)
1024 alist = item;
Guido van Rossum2d951851994-08-29 12:52:16 +00001025
Tim Peters4e9afdc2001-05-03 23:54:49 +00001026 if (numactive == 0) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001027 Py_DECREF(alist);
Guido van Rossum2d951851994-08-29 12:52:16 +00001028 break;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001029 }
Guido van Rossum2d951851994-08-29 12:52:16 +00001030
Guido van Rossum79f25d91997-04-29 20:08:16 +00001031 if (func == Py_None)
Guido van Rossum32120311995-07-10 13:52:21 +00001032 value = alist;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001033 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001034 value = PyEval_CallObject(func, alist);
1035 Py_DECREF(alist);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00001036 if (value == NULL)
1037 goto Fail_1;
Guido van Rossum2d951851994-08-29 12:52:16 +00001038 }
1039 if (i >= len) {
Barry Warsawfa77e091999-01-28 18:49:12 +00001040 int status = PyList_Append(result, value);
Barry Warsaw21332871999-01-28 04:21:35 +00001041 Py_DECREF(value);
Barry Warsawfa77e091999-01-28 18:49:12 +00001042 if (status < 0)
1043 goto Fail_1;
Guido van Rossum2d951851994-08-29 12:52:16 +00001044 }
Tim Peters4e9afdc2001-05-03 23:54:49 +00001045 else if (PyList_SetItem(result, i, value) < 0)
1046 goto Fail_1;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001047 }
1048
Guido van Rossumfa4ac711998-07-10 17:37:30 +00001049 if (i < len && PyList_SetSlice(result, i, len, NULL) < 0)
1050 goto Fail_1;
1051
Tim Peters4e9afdc2001-05-03 23:54:49 +00001052 goto Succeed;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001053
Guido van Rossum12d12c51993-10-26 17:58:25 +00001054Fail_1:
Guido van Rossum79f25d91997-04-29 20:08:16 +00001055 Py_DECREF(result);
Guido van Rossum12d12c51993-10-26 17:58:25 +00001056Fail_2:
Tim Peters4e9afdc2001-05-03 23:54:49 +00001057 result = NULL;
1058Succeed:
1059 assert(seqs);
1060 for (i = 0; i < n; ++i)
1061 Py_XDECREF(seqs[i].it);
1062 PyMem_DEL(seqs);
1063 return result;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001064}
1065
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001066PyDoc_STRVAR(map_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001067"map(function, sequence[, sequence, ...]) -> list\n\
1068\n\
1069Return a list of the results of applying the function to the items of\n\
1070the argument sequence(s). If more than one sequence is given, the\n\
1071function is called with an argument list consisting of the corresponding\n\
1072item of each sequence, substituting None for missing values when not all\n\
1073sequences have the same length. If the function is None, return a list of\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001074the items of the sequence (or a list of tuples if more than one sequence).");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001075
1076
Guido van Rossum79f25d91997-04-29 20:08:16 +00001077static PyObject *
Georg Brandl28e08732008-04-30 19:47:09 +00001078builtin_next(PyObject *self, PyObject *args)
1079{
1080 PyObject *it, *res;
1081 PyObject *def = NULL;
1082
1083 if (!PyArg_UnpackTuple(args, "next", 1, 2, &it, &def))
1084 return NULL;
1085 if (!PyIter_Check(it)) {
1086 PyErr_Format(PyExc_TypeError,
1087 "%.200s object is not an iterator",
1088 it->ob_type->tp_name);
1089 return NULL;
1090 }
1091
1092 res = (*it->ob_type->tp_iternext)(it);
1093 if (res != NULL) {
1094 return res;
1095 } else if (def != NULL) {
1096 if (PyErr_Occurred()) {
1097 if (!PyErr_ExceptionMatches(PyExc_StopIteration))
1098 return NULL;
1099 PyErr_Clear();
1100 }
1101 Py_INCREF(def);
1102 return def;
1103 } else if (PyErr_Occurred()) {
1104 return NULL;
1105 } else {
1106 PyErr_SetNone(PyExc_StopIteration);
1107 return NULL;
1108 }
1109}
1110
1111PyDoc_STRVAR(next_doc,
1112"next(iterator[, default])\n\
1113\n\
1114Return the next item from the iterator. If default is given and the iterator\n\
1115is exhausted, it is returned instead of raising StopIteration.");
1116
1117
1118static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001119builtin_setattr(PyObject *self, PyObject *args)
Guido van Rossum33894be1992-01-27 16:53:09 +00001120{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001121 PyObject *v;
1122 PyObject *name;
1123 PyObject *value;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001124
Raymond Hettingerea3fdf42002-12-29 16:33:45 +00001125 if (!PyArg_UnpackTuple(args, "setattr", 3, 3, &v, &name, &value))
Guido van Rossum33894be1992-01-27 16:53:09 +00001126 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001127 if (PyObject_SetAttr(v, name, value) != 0)
Guido van Rossum33894be1992-01-27 16:53:09 +00001128 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001129 Py_INCREF(Py_None);
1130 return Py_None;
Guido van Rossum33894be1992-01-27 16:53:09 +00001131}
1132
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001133PyDoc_STRVAR(setattr_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001134"setattr(object, name, value)\n\
1135\n\
1136Set a named attribute on an object; setattr(x, 'y', v) is equivalent to\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001137``x.y = v''.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001138
1139
Guido van Rossum79f25d91997-04-29 20:08:16 +00001140static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001141builtin_delattr(PyObject *self, PyObject *args)
Guido van Rossum14144fc1994-08-29 12:53:40 +00001142{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001143 PyObject *v;
1144 PyObject *name;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001145
Raymond Hettingerea3fdf42002-12-29 16:33:45 +00001146 if (!PyArg_UnpackTuple(args, "delattr", 2, 2, &v, &name))
Guido van Rossum14144fc1994-08-29 12:53:40 +00001147 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001148 if (PyObject_SetAttr(v, name, (PyObject *)NULL) != 0)
Guido van Rossum14144fc1994-08-29 12:53:40 +00001149 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001150 Py_INCREF(Py_None);
1151 return Py_None;
Guido van Rossum14144fc1994-08-29 12:53:40 +00001152}
1153
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001154PyDoc_STRVAR(delattr_doc,
Guido van Rossumdf12a591998-11-23 22:13:04 +00001155"delattr(object, name)\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001156\n\
1157Delete a named attribute on an object; delattr(x, 'y') is equivalent to\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001158``del x.y''.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001159
1160
Guido van Rossum79f25d91997-04-29 20:08:16 +00001161static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001162builtin_hash(PyObject *self, PyObject *v)
Guido van Rossum9bfef441993-03-29 10:43:31 +00001163{
Guido van Rossum9bfef441993-03-29 10:43:31 +00001164 long x;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001165
Guido van Rossum79f25d91997-04-29 20:08:16 +00001166 x = PyObject_Hash(v);
Guido van Rossum9bfef441993-03-29 10:43:31 +00001167 if (x == -1)
1168 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001169 return PyInt_FromLong(x);
Guido van Rossum9bfef441993-03-29 10:43:31 +00001170}
1171
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001172PyDoc_STRVAR(hash_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001173"hash(object) -> integer\n\
1174\n\
1175Return a hash value for the object. Two objects with the same value have\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001176the same hash value. The reverse is not necessarily true, but likely.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001177
1178
Guido van Rossum79f25d91997-04-29 20:08:16 +00001179static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001180builtin_hex(PyObject *self, PyObject *v)
Guido van Rossum006bcd41991-10-24 14:54:44 +00001181{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001182 PyNumberMethods *nb;
Neil Schemenauer3a313e32004-07-19 16:29:17 +00001183 PyObject *res;
Benjamin Petersonc6d64ec2008-05-17 20:09:42 +00001184
1185 if ((nb = v->ob_type->tp_as_number) == NULL ||
1186 nb->nb_hex == NULL) {
1187 PyErr_SetString(PyExc_TypeError,
1188 "hex() argument can't be converted to hex");
1189 return NULL;
Guido van Rossum006bcd41991-10-24 14:54:44 +00001190 }
Benjamin Petersonc6d64ec2008-05-17 20:09:42 +00001191 res = (*nb->nb_hex)(v);
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001192 if (res && !PyString_Check(res)) {
Benjamin Petersonc6d64ec2008-05-17 20:09:42 +00001193 PyErr_Format(PyExc_TypeError,
1194 "__hex__ returned non-string (type %.200s)",
1195 res->ob_type->tp_name);
1196 Py_DECREF(res);
1197 return NULL;
1198 }
1199 return res;
Guido van Rossum006bcd41991-10-24 14:54:44 +00001200}
1201
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001202PyDoc_STRVAR(hex_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001203"hex(number) -> string\n\
1204\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001205Return the hexadecimal representation of an integer or long integer.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001206
1207
Tim Petersdbd9ba62000-07-09 03:09:57 +00001208static PyObject *builtin_raw_input(PyObject *, PyObject *);
Guido van Rossum3165fe61992-09-25 21:59:05 +00001209
Guido van Rossum79f25d91997-04-29 20:08:16 +00001210static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001211builtin_input(PyObject *self, PyObject *args)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001212{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001213 PyObject *line;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001214 char *str;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001215 PyObject *res;
1216 PyObject *globals, *locals;
Hye-Shik Changff83c2b2004-02-02 13:39:01 +00001217 PyCompilerFlags cf;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001218
1219 line = builtin_raw_input(self, args);
Guido van Rossum3165fe61992-09-25 21:59:05 +00001220 if (line == NULL)
1221 return line;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001222 if (!PyArg_Parse(line, "s;embedded '\\0' in input line", &str))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001223 return NULL;
1224 while (*str == ' ' || *str == '\t')
1225 str++;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001226 globals = PyEval_GetGlobals();
1227 locals = PyEval_GetLocals();
1228 if (PyDict_GetItemString(globals, "__builtins__") == NULL) {
1229 if (PyDict_SetItemString(globals, "__builtins__",
1230 PyEval_GetBuiltins()) != 0)
Guido van Rossum6135a871995-01-09 17:53:26 +00001231 return NULL;
1232 }
Hye-Shik Changff83c2b2004-02-02 13:39:01 +00001233 cf.cf_flags = 0;
1234 PyEval_MergeCompilerFlags(&cf);
1235 res = PyRun_StringFlags(str, Py_eval_input, globals, locals, &cf);
Guido van Rossum79f25d91997-04-29 20:08:16 +00001236 Py_DECREF(line);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001237 return res;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001238}
1239
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001240PyDoc_STRVAR(input_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001241"input([prompt]) -> value\n\
1242\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001243Equivalent to eval(raw_input(prompt)).");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001244
1245
Guido van Rossume8811f81997-02-14 15:48:05 +00001246static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001247builtin_intern(PyObject *self, PyObject *args)
Guido van Rossume8811f81997-02-14 15:48:05 +00001248{
1249 PyObject *s;
Guido van Rossum43713e52000-02-29 13:59:29 +00001250 if (!PyArg_ParseTuple(args, "S:intern", &s))
Guido van Rossume8811f81997-02-14 15:48:05 +00001251 return NULL;
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001252 if (!PyString_CheckExact(s)) {
Jeremy Hylton4c989dd2004-08-07 19:20:05 +00001253 PyErr_SetString(PyExc_TypeError,
1254 "can't intern subclass of string");
1255 return NULL;
1256 }
Guido van Rossume8811f81997-02-14 15:48:05 +00001257 Py_INCREF(s);
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001258 PyString_InternInPlace(&s);
Guido van Rossume8811f81997-02-14 15:48:05 +00001259 return s;
1260}
1261
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001262PyDoc_STRVAR(intern_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001263"intern(string) -> string\n\
1264\n\
1265``Intern'' the given string. This enters the string in the (global)\n\
1266table of interned strings whose purpose is to speed up dictionary lookups.\n\
1267Return the string itself or the previously interned string object with the\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001268same value.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001269
1270
Guido van Rossum79f25d91997-04-29 20:08:16 +00001271static PyObject *
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001272builtin_iter(PyObject *self, PyObject *args)
1273{
1274 PyObject *v, *w = NULL;
1275
Raymond Hettingerea3fdf42002-12-29 16:33:45 +00001276 if (!PyArg_UnpackTuple(args, "iter", 1, 2, &v, &w))
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001277 return NULL;
1278 if (w == NULL)
1279 return PyObject_GetIter(v);
1280 if (!PyCallable_Check(v)) {
1281 PyErr_SetString(PyExc_TypeError,
1282 "iter(v, w): v must be callable");
1283 return NULL;
1284 }
1285 return PyCallIter_New(v, w);
1286}
1287
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001288PyDoc_STRVAR(iter_doc,
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001289"iter(collection) -> iterator\n\
1290iter(callable, sentinel) -> iterator\n\
1291\n\
1292Get an iterator from an object. In the first form, the argument must\n\
1293supply its own iterator, or be a sequence.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001294In the second form, the callable is called until it returns the sentinel.");
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001295
1296
1297static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001298builtin_len(PyObject *self, PyObject *v)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001299{
Martin v. Löwis18e16552006-02-15 17:27:45 +00001300 Py_ssize_t res;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001301
Jeremy Hylton03657cf2000-07-12 13:05:33 +00001302 res = PyObject_Size(v);
Guido van Rossum8ea9f4d1998-06-29 22:26:50 +00001303 if (res < 0 && PyErr_Occurred())
1304 return NULL;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001305 return PyInt_FromSsize_t(res);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001306}
1307
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001308PyDoc_STRVAR(len_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001309"len(object) -> integer\n\
1310\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001311Return the number of items of a sequence or mapping.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001312
1313
Guido van Rossum79f25d91997-04-29 20:08:16 +00001314static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001315builtin_locals(PyObject *self)
Guido van Rossum872537c1995-07-07 22:43:42 +00001316{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001317 PyObject *d;
Guido van Rossum872537c1995-07-07 22:43:42 +00001318
Guido van Rossum79f25d91997-04-29 20:08:16 +00001319 d = PyEval_GetLocals();
Neal Norwitz43bd4db2006-08-12 01:46:42 +00001320 Py_XINCREF(d);
Guido van Rossum872537c1995-07-07 22:43:42 +00001321 return d;
1322}
1323
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001324PyDoc_STRVAR(locals_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001325"locals() -> dictionary\n\
1326\n\
Raymond Hettinger69bf8f32003-01-04 02:16:22 +00001327Update and return a dictionary containing the current scope's local variables.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001328
1329
Guido van Rossum79f25d91997-04-29 20:08:16 +00001330static PyObject *
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001331min_max(PyObject *args, PyObject *kwds, int op)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001332{
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001333 PyObject *v, *it, *item, *val, *maxitem, *maxval, *keyfunc=NULL;
Martin v. Löwisfd39ad42004-08-12 14:42:37 +00001334 const char *name = op == Py_LT ? "min" : "max";
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001335
Guido van Rossum79f25d91997-04-29 20:08:16 +00001336 if (PyTuple_Size(args) > 1)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001337 v = args;
Martin v. Löwisfd39ad42004-08-12 14:42:37 +00001338 else if (!PyArg_UnpackTuple(args, (char *)name, 1, 1, &v))
Guido van Rossum3f5da241990-12-20 15:06:42 +00001339 return NULL;
Tim Peters67d687a2002-04-29 21:27:32 +00001340
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001341 if (kwds != NULL && PyDict_Check(kwds) && PyDict_Size(kwds)) {
1342 keyfunc = PyDict_GetItemString(kwds, "key");
1343 if (PyDict_Size(kwds)!=1 || keyfunc == NULL) {
Georg Brandl99d7e4e2005-08-31 22:21:15 +00001344 PyErr_Format(PyExc_TypeError,
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001345 "%s() got an unexpected keyword argument", name);
1346 return NULL;
1347 }
Guido van Rossum1d9a9ea2008-01-23 20:19:01 +00001348 Py_INCREF(keyfunc);
Georg Brandl99d7e4e2005-08-31 22:21:15 +00001349 }
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001350
Tim Petersc3074532001-05-03 07:00:32 +00001351 it = PyObject_GetIter(v);
Guido van Rossum1d9a9ea2008-01-23 20:19:01 +00001352 if (it == NULL) {
1353 Py_XDECREF(keyfunc);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001354 return NULL;
Guido van Rossum1d9a9ea2008-01-23 20:19:01 +00001355 }
Tim Petersc3074532001-05-03 07:00:32 +00001356
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001357 maxitem = NULL; /* the result */
1358 maxval = NULL; /* the value associated with the result */
Brett Cannon9e635cf2004-12-07 00:25:35 +00001359 while (( item = PyIter_Next(it) )) {
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001360 /* get the value from the key function */
1361 if (keyfunc != NULL) {
1362 val = PyObject_CallFunctionObjArgs(keyfunc, item, NULL);
1363 if (val == NULL)
1364 goto Fail_it_item;
1365 }
1366 /* no key function; the value is the item */
1367 else {
1368 val = item;
1369 Py_INCREF(val);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001370 }
Tim Petersc3074532001-05-03 07:00:32 +00001371
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001372 /* maximum value and item are unset; set them */
1373 if (maxval == NULL) {
1374 maxitem = item;
1375 maxval = val;
1376 }
1377 /* maximum value and item are set; update them as necessary */
Guido van Rossum2d951851994-08-29 12:52:16 +00001378 else {
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001379 int cmp = PyObject_RichCompareBool(val, maxval, op);
1380 if (cmp < 0)
1381 goto Fail_it_item_and_val;
1382 else if (cmp > 0) {
1383 Py_DECREF(maxval);
1384 Py_DECREF(maxitem);
1385 maxval = val;
1386 maxitem = item;
Guido van Rossum53451b32001-01-17 15:47:24 +00001387 }
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001388 else {
1389 Py_DECREF(item);
1390 Py_DECREF(val);
Guido van Rossumc8b6df91997-05-23 00:06:51 +00001391 }
Guido van Rossum2d951851994-08-29 12:52:16 +00001392 }
Guido van Rossum3f5da241990-12-20 15:06:42 +00001393 }
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001394 if (PyErr_Occurred())
1395 goto Fail_it;
1396 if (maxval == NULL) {
Martin v. Löwisfd39ad42004-08-12 14:42:37 +00001397 PyErr_Format(PyExc_ValueError,
1398 "%s() arg is an empty sequence", name);
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001399 assert(maxitem == NULL);
1400 }
1401 else
1402 Py_DECREF(maxval);
Tim Petersc3074532001-05-03 07:00:32 +00001403 Py_DECREF(it);
Guido van Rossum1d9a9ea2008-01-23 20:19:01 +00001404 Py_XDECREF(keyfunc);
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001405 return maxitem;
1406
1407Fail_it_item_and_val:
1408 Py_DECREF(val);
1409Fail_it_item:
1410 Py_DECREF(item);
1411Fail_it:
1412 Py_XDECREF(maxval);
1413 Py_XDECREF(maxitem);
1414 Py_DECREF(it);
Guido van Rossum1d9a9ea2008-01-23 20:19:01 +00001415 Py_XDECREF(keyfunc);
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001416 return NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001417}
1418
Guido van Rossum79f25d91997-04-29 20:08:16 +00001419static PyObject *
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001420builtin_min(PyObject *self, PyObject *args, PyObject *kwds)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001421{
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001422 return min_max(args, kwds, Py_LT);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001423}
1424
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001425PyDoc_STRVAR(min_doc,
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001426"min(iterable[, key=func]) -> value\n\
1427min(a, b, c, ...[, key=func]) -> value\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001428\n\
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001429With a single iterable argument, return its smallest item.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001430With two or more arguments, return the smallest argument.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001431
1432
Guido van Rossum79f25d91997-04-29 20:08:16 +00001433static PyObject *
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001434builtin_max(PyObject *self, PyObject *args, PyObject *kwds)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001435{
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001436 return min_max(args, kwds, Py_GT);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001437}
1438
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001439PyDoc_STRVAR(max_doc,
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001440"max(iterable[, key=func]) -> value\n\
1441max(a, b, c, ...[, key=func]) -> value\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001442\n\
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001443With a single iterable argument, return its largest item.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001444With two or more arguments, return the largest argument.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001445
1446
Guido van Rossum79f25d91997-04-29 20:08:16 +00001447static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001448builtin_oct(PyObject *self, PyObject *v)
Guido van Rossum006bcd41991-10-24 14:54:44 +00001449{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001450 PyNumberMethods *nb;
Neil Schemenauer3a313e32004-07-19 16:29:17 +00001451 PyObject *res;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001452
Guido van Rossum1899c2e1992-09-12 11:09:23 +00001453 if (v == NULL || (nb = v->ob_type->tp_as_number) == NULL ||
1454 nb->nb_oct == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001455 PyErr_SetString(PyExc_TypeError,
Guido van Rossum1899c2e1992-09-12 11:09:23 +00001456 "oct() argument can't be converted to oct");
1457 return NULL;
Guido van Rossum006bcd41991-10-24 14:54:44 +00001458 }
Neil Schemenauer3a313e32004-07-19 16:29:17 +00001459 res = (*nb->nb_oct)(v);
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001460 if (res && !PyString_Check(res)) {
Neil Schemenauer3a313e32004-07-19 16:29:17 +00001461 PyErr_Format(PyExc_TypeError,
1462 "__oct__ returned non-string (type %.200s)",
1463 res->ob_type->tp_name);
1464 Py_DECREF(res);
1465 return NULL;
1466 }
1467 return res;
Guido van Rossum006bcd41991-10-24 14:54:44 +00001468}
1469
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001470PyDoc_STRVAR(oct_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001471"oct(number) -> string\n\
1472\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001473Return the octal representation of an integer or long integer.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001474
1475
Guido van Rossum79f25d91997-04-29 20:08:16 +00001476static PyObject *
Neal Norwitzc4edb0e2006-05-02 04:43:14 +00001477builtin_open(PyObject *self, PyObject *args, PyObject *kwds)
1478{
1479 return PyObject_Call((PyObject*)&PyFile_Type, args, kwds);
1480}
1481
1482PyDoc_STRVAR(open_doc,
1483"open(name[, mode[, buffering]]) -> file object\n\
1484\n\
Skip Montanaro4e3ebe02007-12-08 14:37:43 +00001485Open a file using the file() type, returns a file object. This is the\n\
1486preferred way to open a file.");
Neal Norwitzc4edb0e2006-05-02 04:43:14 +00001487
1488
1489static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001490builtin_ord(PyObject *self, PyObject* obj)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001491{
Guido van Rossum09095f32000-03-10 23:00:52 +00001492 long ord;
Martin v. Löwisd96ee902006-02-16 14:37:16 +00001493 Py_ssize_t size;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001494
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001495 if (PyString_Check(obj)) {
1496 size = PyString_GET_SIZE(obj);
Andrew M. Kuchling34c20cf2000-12-20 14:36:56 +00001497 if (size == 1) {
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001498 ord = (long)((unsigned char)*PyString_AS_STRING(obj));
Andrew M. Kuchling34c20cf2000-12-20 14:36:56 +00001499 return PyInt_FromLong(ord);
1500 }
Christian Heimes3497f942008-05-26 12:29:14 +00001501 } else if (PyByteArray_Check(obj)) {
1502 size = PyByteArray_GET_SIZE(obj);
Christian Heimes1a6387e2008-03-26 12:49:49 +00001503 if (size == 1) {
Christian Heimes3497f942008-05-26 12:29:14 +00001504 ord = (long)((unsigned char)*PyByteArray_AS_STRING(obj));
Christian Heimes1a6387e2008-03-26 12:49:49 +00001505 return PyInt_FromLong(ord);
1506 }
1507
Martin v. Löwis339d0f72001-08-17 18:39:25 +00001508#ifdef Py_USING_UNICODE
Jeremy Hylton394b54d2000-04-12 21:19:47 +00001509 } else if (PyUnicode_Check(obj)) {
1510 size = PyUnicode_GET_SIZE(obj);
Andrew M. Kuchling34c20cf2000-12-20 14:36:56 +00001511 if (size == 1) {
Jeremy Hylton394b54d2000-04-12 21:19:47 +00001512 ord = (long)*PyUnicode_AS_UNICODE(obj);
Andrew M. Kuchling34c20cf2000-12-20 14:36:56 +00001513 return PyInt_FromLong(ord);
1514 }
Martin v. Löwis339d0f72001-08-17 18:39:25 +00001515#endif
Jeremy Hylton394b54d2000-04-12 21:19:47 +00001516 } else {
1517 PyErr_Format(PyExc_TypeError,
Andrew M. Kuchlingf07aad12000-12-23 14:11:28 +00001518 "ord() expected string of length 1, but " \
Jeremy Hylton394b54d2000-04-12 21:19:47 +00001519 "%.200s found", obj->ob_type->tp_name);
Guido van Rossum09095f32000-03-10 23:00:52 +00001520 return NULL;
1521 }
1522
Guido van Rossumad991772001-01-12 16:03:05 +00001523 PyErr_Format(PyExc_TypeError,
Andrew M. Kuchlingf07aad12000-12-23 14:11:28 +00001524 "ord() expected a character, "
Martin v. Löwisd96ee902006-02-16 14:37:16 +00001525 "but string of length %zd found",
Jeremy Hylton394b54d2000-04-12 21:19:47 +00001526 size);
1527 return NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001528}
1529
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001530PyDoc_STRVAR(ord_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001531"ord(c) -> integer\n\
1532\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001533Return the integer ordinal of a one-character string.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001534
1535
Guido van Rossum79f25d91997-04-29 20:08:16 +00001536static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001537builtin_pow(PyObject *self, PyObject *args)
Guido van Rossum6a00cd81995-01-07 12:39:01 +00001538{
Guido van Rossum09df08a1998-05-22 00:51:39 +00001539 PyObject *v, *w, *z = Py_None;
Guido van Rossum6a00cd81995-01-07 12:39:01 +00001540
Raymond Hettingerea3fdf42002-12-29 16:33:45 +00001541 if (!PyArg_UnpackTuple(args, "pow", 2, 3, &v, &w, &z))
Guido van Rossum6a00cd81995-01-07 12:39:01 +00001542 return NULL;
Guido van Rossum09df08a1998-05-22 00:51:39 +00001543 return PyNumber_Power(v, w, z);
Guido van Rossumd4905451991-05-05 20:00:36 +00001544}
1545
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001546PyDoc_STRVAR(pow_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001547"pow(x, y[, z]) -> number\n\
1548\n\
1549With two arguments, equivalent to x**y. With three arguments,\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001550equivalent to (x**y) % z, but may be more efficient (e.g. for longs).");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001551
1552
Eric Smith7c478942008-03-18 23:45:49 +00001553static PyObject *
1554builtin_print(PyObject *self, PyObject *args, PyObject *kwds)
1555{
1556 static char *kwlist[] = {"sep", "end", "file", 0};
1557 static PyObject *dummy_args;
1558 PyObject *sep = NULL, *end = NULL, *file = NULL;
1559 int i, err;
1560
1561 if (dummy_args == NULL) {
1562 if (!(dummy_args = PyTuple_New(0)))
1563 return NULL;
1564 }
1565 if (!PyArg_ParseTupleAndKeywords(dummy_args, kwds, "|OOO:print",
1566 kwlist, &sep, &end, &file))
1567 return NULL;
1568 if (file == NULL || file == Py_None) {
1569 file = PySys_GetObject("stdout");
1570 /* sys.stdout may be None when FILE* stdout isn't connected */
1571 if (file == Py_None)
1572 Py_RETURN_NONE;
1573 }
1574
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001575 if (sep && sep != Py_None && !PyString_Check(sep) &&
Eric Smith7c478942008-03-18 23:45:49 +00001576 !PyUnicode_Check(sep)) {
1577 PyErr_Format(PyExc_TypeError,
1578 "sep must be None, str or unicode, not %.200s",
1579 sep->ob_type->tp_name);
1580 return NULL;
1581 }
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001582 if (end && end != Py_None && !PyString_Check(end) &&
Eric Smith7c478942008-03-18 23:45:49 +00001583 !PyUnicode_Check(end)) {
1584 PyErr_Format(PyExc_TypeError,
1585 "end must be None, str or unicode, not %.200s",
1586 end->ob_type->tp_name);
1587 return NULL;
1588 }
1589
1590 for (i = 0; i < PyTuple_Size(args); i++) {
1591 if (i > 0) {
1592 if (sep == NULL || sep == Py_None)
1593 err = PyFile_WriteString(" ", file);
1594 else
1595 err = PyFile_WriteObject(sep, file,
1596 Py_PRINT_RAW);
1597 if (err)
1598 return NULL;
1599 }
1600 err = PyFile_WriteObject(PyTuple_GetItem(args, i), file,
1601 Py_PRINT_RAW);
1602 if (err)
1603 return NULL;
1604 }
1605
1606 if (end == NULL || end == Py_None)
1607 err = PyFile_WriteString("\n", file);
1608 else
1609 err = PyFile_WriteObject(end, file, Py_PRINT_RAW);
1610 if (err)
1611 return NULL;
1612
1613 Py_RETURN_NONE;
1614}
1615
1616PyDoc_STRVAR(print_doc,
1617"print(value, ..., sep=' ', end='\\n', file=sys.stdout)\n\
1618\n\
1619Prints the values to a stream, or to sys.stdout by default.\n\
1620Optional keyword arguments:\n\
1621file: a file-like object (stream); defaults to the current sys.stdout.\n\
1622sep: string inserted between values, default a space.\n\
1623end: string appended after the last value, default a newline.");
1624
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001625
1626/* Return number of items in range (lo, hi, step), when arguments are
1627 * PyInt or PyLong objects. step > 0 required. Return a value < 0 if
1628 * & only if the true value is too large to fit in a signed long.
1629 * Arguments MUST return 1 with either PyInt_Check() or
1630 * PyLong_Check(). Return -1 when there is an error.
1631 */
1632static long
1633get_len_of_range_longs(PyObject *lo, PyObject *hi, PyObject *step)
1634{
1635 /* -------------------------------------------------------------
1636 Algorithm is equal to that of get_len_of_range(), but it operates
1637 on PyObjects (which are assumed to be PyLong or PyInt objects).
1638 ---------------------------------------------------------------*/
1639 long n;
1640 PyObject *diff = NULL;
1641 PyObject *one = NULL;
1642 PyObject *tmp1 = NULL, *tmp2 = NULL, *tmp3 = NULL;
1643 /* holds sub-expression evaluations */
1644
1645 /* if (lo >= hi), return length of 0. */
1646 if (PyObject_Compare(lo, hi) >= 0)
1647 return 0;
1648
1649 if ((one = PyLong_FromLong(1L)) == NULL)
1650 goto Fail;
1651
1652 if ((tmp1 = PyNumber_Subtract(hi, lo)) == NULL)
1653 goto Fail;
1654
1655 if ((diff = PyNumber_Subtract(tmp1, one)) == NULL)
1656 goto Fail;
1657
1658 if ((tmp2 = PyNumber_FloorDivide(diff, step)) == NULL)
1659 goto Fail;
1660
1661 if ((tmp3 = PyNumber_Add(tmp2, one)) == NULL)
1662 goto Fail;
1663
1664 n = PyLong_AsLong(tmp3);
1665 if (PyErr_Occurred()) { /* Check for Overflow */
1666 PyErr_Clear();
1667 goto Fail;
1668 }
1669
1670 Py_DECREF(tmp3);
1671 Py_DECREF(tmp2);
1672 Py_DECREF(diff);
1673 Py_DECREF(tmp1);
1674 Py_DECREF(one);
1675 return n;
1676
1677 Fail:
1678 Py_XDECREF(tmp3);
1679 Py_XDECREF(tmp2);
1680 Py_XDECREF(diff);
1681 Py_XDECREF(tmp1);
1682 Py_XDECREF(one);
1683 return -1;
1684}
1685
1686/* An extension of builtin_range() that handles the case when PyLong
1687 * arguments are given. */
1688static PyObject *
1689handle_range_longs(PyObject *self, PyObject *args)
1690{
1691 PyObject *ilow;
Tim Peters874e1f72003-04-13 22:13:08 +00001692 PyObject *ihigh = NULL;
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001693 PyObject *istep = NULL;
Tim Peters874e1f72003-04-13 22:13:08 +00001694
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001695 PyObject *curnum = NULL;
1696 PyObject *v = NULL;
1697 long bign;
1698 int i, n;
1699 int cmp_result;
1700
Tim Peters874e1f72003-04-13 22:13:08 +00001701 PyObject *zero = PyLong_FromLong(0);
1702
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001703 if (zero == NULL)
1704 return NULL;
1705
Tim Peters874e1f72003-04-13 22:13:08 +00001706 if (!PyArg_UnpackTuple(args, "range", 1, 3, &ilow, &ihigh, &istep)) {
1707 Py_DECREF(zero);
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001708 return NULL;
1709 }
1710
Tim Peters874e1f72003-04-13 22:13:08 +00001711 /* Figure out which way we were called, supply defaults, and be
1712 * sure to incref everything so that the decrefs at the end
1713 * are correct.
1714 */
1715 assert(ilow != NULL);
1716 if (ihigh == NULL) {
1717 /* only 1 arg -- it's the upper limit */
1718 ihigh = ilow;
1719 ilow = NULL;
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001720 }
Tim Peters874e1f72003-04-13 22:13:08 +00001721 assert(ihigh != NULL);
1722 Py_INCREF(ihigh);
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001723
Tim Peters874e1f72003-04-13 22:13:08 +00001724 /* ihigh correct now; do ilow */
1725 if (ilow == NULL)
1726 ilow = zero;
1727 Py_INCREF(ilow);
1728
1729 /* ilow and ihigh correct now; do istep */
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001730 if (istep == NULL) {
1731 istep = PyLong_FromLong(1L);
1732 if (istep == NULL)
1733 goto Fail;
1734 }
1735 else {
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001736 Py_INCREF(istep);
1737 }
1738
Tim Peters874e1f72003-04-13 22:13:08 +00001739 if (!PyInt_Check(ilow) && !PyLong_Check(ilow)) {
Guido van Rossum28e83e32003-04-15 12:43:26 +00001740 PyErr_Format(PyExc_TypeError,
Alex Martellif471d472003-04-23 13:00:44 +00001741 "range() integer start argument expected, got %s.",
Guido van Rossum817d6c92003-04-14 18:25:04 +00001742 ilow->ob_type->tp_name);
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001743 goto Fail;
1744 }
1745
Tim Peters874e1f72003-04-13 22:13:08 +00001746 if (!PyInt_Check(ihigh) && !PyLong_Check(ihigh)) {
Guido van Rossum28e83e32003-04-15 12:43:26 +00001747 PyErr_Format(PyExc_TypeError,
Alex Martellif471d472003-04-23 13:00:44 +00001748 "range() integer end argument expected, got %s.",
Guido van Rossum817d6c92003-04-14 18:25:04 +00001749 ihigh->ob_type->tp_name);
Tim Peters874e1f72003-04-13 22:13:08 +00001750 goto Fail;
1751 }
1752
1753 if (!PyInt_Check(istep) && !PyLong_Check(istep)) {
Guido van Rossum28e83e32003-04-15 12:43:26 +00001754 PyErr_Format(PyExc_TypeError,
Alex Martellif471d472003-04-23 13:00:44 +00001755 "range() integer step argument expected, got %s.",
Guido van Rossum817d6c92003-04-14 18:25:04 +00001756 istep->ob_type->tp_name);
Tim Peters874e1f72003-04-13 22:13:08 +00001757 goto Fail;
1758 }
1759
1760 if (PyObject_Cmp(istep, zero, &cmp_result) == -1)
1761 goto Fail;
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001762 if (cmp_result == 0) {
1763 PyErr_SetString(PyExc_ValueError,
Alex Martellif471d472003-04-23 13:00:44 +00001764 "range() step argument must not be zero");
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001765 goto Fail;
1766 }
1767
1768 if (cmp_result > 0)
1769 bign = get_len_of_range_longs(ilow, ihigh, istep);
1770 else {
1771 PyObject *neg_istep = PyNumber_Negative(istep);
1772 if (neg_istep == NULL)
1773 goto Fail;
1774 bign = get_len_of_range_longs(ihigh, ilow, neg_istep);
1775 Py_DECREF(neg_istep);
1776 }
1777
1778 n = (int)bign;
1779 if (bign < 0 || (long)n != bign) {
1780 PyErr_SetString(PyExc_OverflowError,
1781 "range() result has too many items");
1782 goto Fail;
1783 }
1784
1785 v = PyList_New(n);
1786 if (v == NULL)
1787 goto Fail;
1788
1789 curnum = ilow;
1790 Py_INCREF(curnum);
1791
1792 for (i = 0; i < n; i++) {
1793 PyObject *w = PyNumber_Long(curnum);
1794 PyObject *tmp_num;
1795 if (w == NULL)
1796 goto Fail;
1797
1798 PyList_SET_ITEM(v, i, w);
1799
1800 tmp_num = PyNumber_Add(curnum, istep);
1801 if (tmp_num == NULL)
1802 goto Fail;
1803
1804 Py_DECREF(curnum);
1805 curnum = tmp_num;
1806 }
Tim Peters874e1f72003-04-13 22:13:08 +00001807 Py_DECREF(ilow);
1808 Py_DECREF(ihigh);
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001809 Py_DECREF(istep);
1810 Py_DECREF(zero);
Tim Peters874e1f72003-04-13 22:13:08 +00001811 Py_DECREF(curnum);
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001812 return v;
1813
1814 Fail:
Tim Peters874e1f72003-04-13 22:13:08 +00001815 Py_DECREF(ilow);
1816 Py_DECREF(ihigh);
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001817 Py_XDECREF(istep);
Tim Peters874e1f72003-04-13 22:13:08 +00001818 Py_DECREF(zero);
1819 Py_XDECREF(curnum);
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001820 Py_XDECREF(v);
1821 return NULL;
1822}
1823
Guido van Rossum124eff01999-02-23 16:11:01 +00001824/* Return number of items in range/xrange (lo, hi, step). step > 0
1825 * required. Return a value < 0 if & only if the true value is too
1826 * large to fit in a signed long.
1827 */
1828static long
Thomas Wouterse28c2962000-07-23 22:21:32 +00001829get_len_of_range(long lo, long hi, long step)
Guido van Rossum124eff01999-02-23 16:11:01 +00001830{
1831 /* -------------------------------------------------------------
1832 If lo >= hi, the range is empty.
1833 Else if n values are in the range, the last one is
1834 lo + (n-1)*step, which must be <= hi-1. Rearranging,
1835 n <= (hi - lo - 1)/step + 1, so taking the floor of the RHS gives
1836 the proper value. Since lo < hi in this case, hi-lo-1 >= 0, so
1837 the RHS is non-negative and so truncation is the same as the
1838 floor. Letting M be the largest positive long, the worst case
1839 for the RHS numerator is hi=M, lo=-M-1, and then
1840 hi-lo-1 = M-(-M-1)-1 = 2*M. Therefore unsigned long has enough
1841 precision to compute the RHS exactly.
1842 ---------------------------------------------------------------*/
1843 long n = 0;
1844 if (lo < hi) {
1845 unsigned long uhi = (unsigned long)hi;
1846 unsigned long ulo = (unsigned long)lo;
1847 unsigned long diff = uhi - ulo - 1;
1848 n = (long)(diff / (unsigned long)step + 1);
1849 }
1850 return n;
1851}
1852
Guido van Rossum79f25d91997-04-29 20:08:16 +00001853static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001854builtin_range(PyObject *self, PyObject *args)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001855{
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001856 long ilow = 0, ihigh = 0, istep = 1;
Guido van Rossum124eff01999-02-23 16:11:01 +00001857 long bign;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001858 int i, n;
Guido van Rossum124eff01999-02-23 16:11:01 +00001859
Guido van Rossum79f25d91997-04-29 20:08:16 +00001860 PyObject *v;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001861
Guido van Rossum79f25d91997-04-29 20:08:16 +00001862 if (PyTuple_Size(args) <= 1) {
1863 if (!PyArg_ParseTuple(args,
Guido van Rossum0865dd91995-01-17 16:30:22 +00001864 "l;range() requires 1-3 int arguments",
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001865 &ihigh)) {
1866 PyErr_Clear();
1867 return handle_range_longs(self, args);
1868 }
Guido van Rossum3f5da241990-12-20 15:06:42 +00001869 }
1870 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001871 if (!PyArg_ParseTuple(args,
Guido van Rossum0865dd91995-01-17 16:30:22 +00001872 "ll|l;range() requires 1-3 int arguments",
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001873 &ilow, &ihigh, &istep)) {
1874 PyErr_Clear();
1875 return handle_range_longs(self, args);
1876 }
Guido van Rossum3f5da241990-12-20 15:06:42 +00001877 }
1878 if (istep == 0) {
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001879 PyErr_SetString(PyExc_ValueError,
Alex Martellif471d472003-04-23 13:00:44 +00001880 "range() step argument must not be zero");
Guido van Rossum3f5da241990-12-20 15:06:42 +00001881 return NULL;
1882 }
Guido van Rossum124eff01999-02-23 16:11:01 +00001883 if (istep > 0)
1884 bign = get_len_of_range(ilow, ihigh, istep);
1885 else
1886 bign = get_len_of_range(ihigh, ilow, -istep);
1887 n = (int)bign;
1888 if (bign < 0 || (long)n != bign) {
Guido van Rossume23cde21999-01-12 05:07:47 +00001889 PyErr_SetString(PyExc_OverflowError,
Fred Drake661ea262000-10-24 19:57:45 +00001890 "range() result has too many items");
Guido van Rossume23cde21999-01-12 05:07:47 +00001891 return NULL;
1892 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00001893 v = PyList_New(n);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001894 if (v == NULL)
1895 return NULL;
1896 for (i = 0; i < n; i++) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001897 PyObject *w = PyInt_FromLong(ilow);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001898 if (w == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001899 Py_DECREF(v);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001900 return NULL;
1901 }
Guido van Rossuma937d141998-04-24 18:22:02 +00001902 PyList_SET_ITEM(v, i, w);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001903 ilow += istep;
1904 }
1905 return v;
1906}
1907
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001908PyDoc_STRVAR(range_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001909"range([start,] stop[, step]) -> list of integers\n\
1910\n\
1911Return a list containing an arithmetic progression of integers.\n\
1912range(i, j) returns [i, i+1, i+2, ..., j-1]; start (!) defaults to 0.\n\
1913When step is given, it specifies the increment (or decrement).\n\
1914For example, range(4) returns [0, 1, 2, 3]. The end point is omitted!\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001915These are exactly the valid indices for a list of 4 elements.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001916
1917
Guido van Rossum79f25d91997-04-29 20:08:16 +00001918static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001919builtin_raw_input(PyObject *self, PyObject *args)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001920{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001921 PyObject *v = NULL;
Martin v. Löwis31d2df52002-08-14 15:46:02 +00001922 PyObject *fin = PySys_GetObject("stdin");
1923 PyObject *fout = PySys_GetObject("stdout");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001924
Raymond Hettingerea3fdf42002-12-29 16:33:45 +00001925 if (!PyArg_UnpackTuple(args, "[raw_]input", 0, 1, &v))
Guido van Rossum3165fe61992-09-25 21:59:05 +00001926 return NULL;
Martin v. Löwis31d2df52002-08-14 15:46:02 +00001927
1928 if (fin == NULL) {
Alex Martellia9b9c9f2003-04-23 13:34:35 +00001929 PyErr_SetString(PyExc_RuntimeError, "[raw_]input: lost sys.stdin");
Martin v. Löwis31d2df52002-08-14 15:46:02 +00001930 return NULL;
1931 }
1932 if (fout == NULL) {
Alex Martellia9b9c9f2003-04-23 13:34:35 +00001933 PyErr_SetString(PyExc_RuntimeError, "[raw_]input: lost sys.stdout");
Martin v. Löwis31d2df52002-08-14 15:46:02 +00001934 return NULL;
1935 }
1936 if (PyFile_SoftSpace(fout, 0)) {
1937 if (PyFile_WriteString(" ", fout) != 0)
1938 return NULL;
1939 }
Georg Brandl7e3ba2a2006-08-06 08:23:54 +00001940 if (PyFile_AsFile(fin) && PyFile_AsFile(fout)
Martin v. Löwis566f6af2002-10-26 14:39:10 +00001941 && isatty(fileno(PyFile_AsFile(fin)))
1942 && isatty(fileno(PyFile_AsFile(fout)))) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001943 PyObject *po;
Guido van Rossum872537c1995-07-07 22:43:42 +00001944 char *prompt;
1945 char *s;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001946 PyObject *result;
Guido van Rossum872537c1995-07-07 22:43:42 +00001947 if (v != NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001948 po = PyObject_Str(v);
Guido van Rossum872537c1995-07-07 22:43:42 +00001949 if (po == NULL)
1950 return NULL;
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001951 prompt = PyString_AsString(po);
Guido van Rossumd9b52081998-06-26 18:25:38 +00001952 if (prompt == NULL)
1953 return NULL;
Guido van Rossum872537c1995-07-07 22:43:42 +00001954 }
1955 else {
1956 po = NULL;
1957 prompt = "";
1958 }
Michael W. Hudson52db5192004-08-02 13:21:09 +00001959 s = PyOS_Readline(PyFile_AsFile(fin), PyFile_AsFile(fout),
Martin v. Löwis566f6af2002-10-26 14:39:10 +00001960 prompt);
Guido van Rossum79f25d91997-04-29 20:08:16 +00001961 Py_XDECREF(po);
Guido van Rossum872537c1995-07-07 22:43:42 +00001962 if (s == NULL) {
Michael W. Hudson30ea2f22004-07-07 17:44:12 +00001963 if (!PyErr_Occurred())
1964 PyErr_SetNone(PyExc_KeyboardInterrupt);
Guido van Rossum872537c1995-07-07 22:43:42 +00001965 return NULL;
1966 }
1967 if (*s == '\0') {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001968 PyErr_SetNone(PyExc_EOFError);
Guido van Rossum872537c1995-07-07 22:43:42 +00001969 result = NULL;
1970 }
1971 else { /* strip trailing '\n' */
Guido van Rossum106f2da2000-06-28 21:12:25 +00001972 size_t len = strlen(s);
Martin v. Löwisb1ed7fa2006-04-13 07:52:27 +00001973 if (len > PY_SSIZE_T_MAX) {
Martin v. Löwis31d2df52002-08-14 15:46:02 +00001974 PyErr_SetString(PyExc_OverflowError,
Alex Martellia9b9c9f2003-04-23 13:34:35 +00001975 "[raw_]input: input too long");
Guido van Rossum106f2da2000-06-28 21:12:25 +00001976 result = NULL;
1977 }
1978 else {
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001979 result = PyString_FromStringAndSize(s, len-1);
Guido van Rossum106f2da2000-06-28 21:12:25 +00001980 }
Guido van Rossum872537c1995-07-07 22:43:42 +00001981 }
Guido van Rossumb18618d2000-05-03 23:44:39 +00001982 PyMem_FREE(s);
Guido van Rossum872537c1995-07-07 22:43:42 +00001983 return result;
1984 }
Guido van Rossum90933611991-06-07 16:10:43 +00001985 if (v != NULL) {
Martin v. Löwis31d2df52002-08-14 15:46:02 +00001986 if (PyFile_WriteObject(v, fout, Py_PRINT_RAW) != 0)
Guido van Rossum90933611991-06-07 16:10:43 +00001987 return NULL;
1988 }
Martin v. Löwis31d2df52002-08-14 15:46:02 +00001989 return PyFile_GetLine(fin, -1);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001990}
1991
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001992PyDoc_STRVAR(raw_input_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001993"raw_input([prompt]) -> string\n\
1994\n\
1995Read a string from standard input. The trailing newline is stripped.\n\
1996If the user hits EOF (Unix: Ctl-D, Windows: Ctl-Z+Return), raise EOFError.\n\
1997On Unix, GNU readline is used if enabled. The prompt string, if given,\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001998is printed without a trailing newline before reading.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001999
2000
Guido van Rossum79f25d91997-04-29 20:08:16 +00002001static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002002builtin_reduce(PyObject *self, PyObject *args)
Guido van Rossum12d12c51993-10-26 17:58:25 +00002003{
Benjamin Peterson08336e32008-08-18 02:01:21 +00002004 static PyObject *functools_reduce = NULL;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002005
Benjamin Peterson9f4f4812008-04-27 03:01:45 +00002006 if (PyErr_WarnPy3k("reduce() not supported in 3.x; "
Benjamin Petersonf19a7b92008-04-27 18:40:21 +00002007 "use functools.reduce()", 1) < 0)
Neal Norwitzdf25efe2007-05-23 06:58:36 +00002008 return NULL;
2009
Benjamin Peterson08336e32008-08-18 02:01:21 +00002010 if (functools_reduce == NULL) {
2011 PyObject *functools = PyImport_ImportModule("functools");
2012 if (functools == NULL)
2013 return NULL;
2014 functools_reduce = PyObject_GetAttrString(functools, "reduce");
2015 Py_DECREF(functools);
2016 if (functools_reduce == NULL)
2017 return NULL;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002018 }
Benjamin Peterson08336e32008-08-18 02:01:21 +00002019 return PyObject_Call(functools_reduce, args, NULL);
Guido van Rossum12d12c51993-10-26 17:58:25 +00002020}
2021
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002022PyDoc_STRVAR(reduce_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002023"reduce(function, sequence[, initial]) -> value\n\
2024\n\
2025Apply a function of two arguments cumulatively to the items of a sequence,\n\
2026from left to right, so as to reduce the sequence to a single value.\n\
2027For example, reduce(lambda x, y: x+y, [1, 2, 3, 4, 5]) calculates\n\
2028((((1+2)+3)+4)+5). If initial is present, it is placed before the items\n\
2029of the sequence in the calculation, and serves as a default when the\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002030sequence is empty.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002031
2032
Guido van Rossum79f25d91997-04-29 20:08:16 +00002033static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002034builtin_reload(PyObject *self, PyObject *v)
Guido van Rossum3f5da241990-12-20 15:06:42 +00002035{
Benjamin Peterson9f4f4812008-04-27 03:01:45 +00002036 if (PyErr_WarnPy3k("In 3.x, reload() is renamed to imp.reload()",
Benjamin Petersonf19a7b92008-04-27 18:40:21 +00002037 1) < 0)
Neal Norwitzdf25efe2007-05-23 06:58:36 +00002038 return NULL;
2039
Guido van Rossum79f25d91997-04-29 20:08:16 +00002040 return PyImport_ReloadModule(v);
Guido van Rossum3f5da241990-12-20 15:06:42 +00002041}
2042
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002043PyDoc_STRVAR(reload_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002044"reload(module) -> module\n\
2045\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002046Reload the module. The module must have been successfully imported before.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002047
2048
Guido van Rossum79f25d91997-04-29 20:08:16 +00002049static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002050builtin_repr(PyObject *self, PyObject *v)
Guido van Rossumc89705d1992-11-26 08:54:07 +00002051{
Guido van Rossum79f25d91997-04-29 20:08:16 +00002052 return PyObject_Repr(v);
Guido van Rossumc89705d1992-11-26 08:54:07 +00002053}
2054
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002055PyDoc_STRVAR(repr_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002056"repr(object) -> string\n\
2057\n\
2058Return the canonical string representation of the object.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002059For most object types, eval(repr(object)) == object.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002060
2061
Guido van Rossum79f25d91997-04-29 20:08:16 +00002062static PyObject *
Georg Brandlccadf842006-03-31 18:54:53 +00002063builtin_round(PyObject *self, PyObject *args, PyObject *kwds)
Guido van Rossum9e51f9b1993-02-12 16:29:05 +00002064{
Jeffrey Yasskin9871d8f2008-01-05 08:47:13 +00002065 double number;
2066 double f;
2067 int ndigits = 0;
2068 int i;
Georg Brandlccadf842006-03-31 18:54:53 +00002069 static char *kwlist[] = {"number", "ndigits", 0};
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002070
Jeffrey Yasskin9871d8f2008-01-05 08:47:13 +00002071 if (!PyArg_ParseTupleAndKeywords(args, kwds, "d|i:round",
2072 kwlist, &number, &ndigits))
2073 return NULL;
2074 f = 1.0;
2075 i = abs(ndigits);
2076 while (--i >= 0)
2077 f = f*10.0;
2078 if (ndigits < 0)
2079 number /= f;
2080 else
2081 number *= f;
2082 if (number >= 0.0)
2083 number = floor(number + 0.5);
2084 else
2085 number = ceil(number - 0.5);
2086 if (ndigits < 0)
2087 number *= f;
2088 else
2089 number /= f;
2090 return PyFloat_FromDouble(number);
Guido van Rossum9e51f9b1993-02-12 16:29:05 +00002091}
2092
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002093PyDoc_STRVAR(round_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002094"round(number[, ndigits]) -> floating point number\n\
2095\n\
2096Round a number to a given precision in decimal digits (default 0 digits).\n\
Jeffrey Yasskin9871d8f2008-01-05 08:47:13 +00002097This always returns a floating point number. Precision may be negative.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002098
Raymond Hettinger64958a12003-12-17 20:43:33 +00002099static PyObject *
2100builtin_sorted(PyObject *self, PyObject *args, PyObject *kwds)
2101{
2102 PyObject *newlist, *v, *seq, *compare=NULL, *keyfunc=NULL, *newargs;
2103 PyObject *callable;
Martin v. Löwis15e62742006-02-27 16:46:16 +00002104 static char *kwlist[] = {"iterable", "cmp", "key", "reverse", 0};
Neal Norwitz6f0d4792005-12-15 06:40:36 +00002105 int reverse;
Raymond Hettinger64958a12003-12-17 20:43:33 +00002106
Neal Norwitz6f0d4792005-12-15 06:40:36 +00002107 /* args 1-4 should match listsort in Objects/listobject.c */
Armin Rigo71d7e702005-09-20 18:13:03 +00002108 if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|OOi:sorted",
2109 kwlist, &seq, &compare, &keyfunc, &reverse))
2110 return NULL;
Raymond Hettinger64958a12003-12-17 20:43:33 +00002111
2112 newlist = PySequence_List(seq);
2113 if (newlist == NULL)
2114 return NULL;
2115
2116 callable = PyObject_GetAttrString(newlist, "sort");
2117 if (callable == NULL) {
2118 Py_DECREF(newlist);
2119 return NULL;
2120 }
Georg Brandl99d7e4e2005-08-31 22:21:15 +00002121
Raymond Hettinger64958a12003-12-17 20:43:33 +00002122 newargs = PyTuple_GetSlice(args, 1, 4);
2123 if (newargs == NULL) {
2124 Py_DECREF(newlist);
2125 Py_DECREF(callable);
2126 return NULL;
2127 }
2128
2129 v = PyObject_Call(callable, newargs, kwds);
2130 Py_DECREF(newargs);
2131 Py_DECREF(callable);
2132 if (v == NULL) {
2133 Py_DECREF(newlist);
2134 return NULL;
2135 }
2136 Py_DECREF(v);
2137 return newlist;
2138}
2139
2140PyDoc_STRVAR(sorted_doc,
2141"sorted(iterable, cmp=None, key=None, reverse=False) --> new sorted list");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002142
Guido van Rossum79f25d91997-04-29 20:08:16 +00002143static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002144builtin_vars(PyObject *self, PyObject *args)
Guido van Rossum2d951851994-08-29 12:52:16 +00002145{
Guido van Rossum79f25d91997-04-29 20:08:16 +00002146 PyObject *v = NULL;
2147 PyObject *d;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002148
Raymond Hettingerea3fdf42002-12-29 16:33:45 +00002149 if (!PyArg_UnpackTuple(args, "vars", 0, 1, &v))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002150 return NULL;
Guido van Rossum2d951851994-08-29 12:52:16 +00002151 if (v == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002152 d = PyEval_GetLocals();
Guido van Rossum53bb7ff1995-07-26 16:26:31 +00002153 if (d == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002154 if (!PyErr_Occurred())
2155 PyErr_SetString(PyExc_SystemError,
Alex Martellia9b9c9f2003-04-23 13:34:35 +00002156 "vars(): no locals!?");
Guido van Rossum53bb7ff1995-07-26 16:26:31 +00002157 }
2158 else
Guido van Rossum79f25d91997-04-29 20:08:16 +00002159 Py_INCREF(d);
Guido van Rossum2d951851994-08-29 12:52:16 +00002160 }
2161 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002162 d = PyObject_GetAttrString(v, "__dict__");
Guido van Rossum2d951851994-08-29 12:52:16 +00002163 if (d == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002164 PyErr_SetString(PyExc_TypeError,
Guido van Rossum2d951851994-08-29 12:52:16 +00002165 "vars() argument must have __dict__ attribute");
2166 return NULL;
2167 }
2168 }
2169 return d;
2170}
2171
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002172PyDoc_STRVAR(vars_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002173"vars([object]) -> dictionary\n\
2174\n\
2175Without arguments, equivalent to locals().\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002176With an argument, equivalent to object.__dict__.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002177
Alex Martellia70b1912003-04-22 08:12:33 +00002178
2179static PyObject*
2180builtin_sum(PyObject *self, PyObject *args)
2181{
2182 PyObject *seq;
2183 PyObject *result = NULL;
2184 PyObject *temp, *item, *iter;
2185
Raymond Hettinger5cf63942003-10-25 06:41:37 +00002186 if (!PyArg_UnpackTuple(args, "sum", 1, 2, &seq, &result))
Alex Martellia70b1912003-04-22 08:12:33 +00002187 return NULL;
2188
2189 iter = PyObject_GetIter(seq);
2190 if (iter == NULL)
2191 return NULL;
2192
2193 if (result == NULL) {
2194 result = PyInt_FromLong(0);
2195 if (result == NULL) {
2196 Py_DECREF(iter);
2197 return NULL;
2198 }
2199 } else {
2200 /* reject string values for 'start' parameter */
2201 if (PyObject_TypeCheck(result, &PyBaseString_Type)) {
2202 PyErr_SetString(PyExc_TypeError,
Alex Martellia9b9c9f2003-04-23 13:34:35 +00002203 "sum() can't sum strings [use ''.join(seq) instead]");
Alex Martellia70b1912003-04-22 08:12:33 +00002204 Py_DECREF(iter);
2205 return NULL;
2206 }
Alex Martelli41c9f882003-04-22 09:24:48 +00002207 Py_INCREF(result);
Alex Martellia70b1912003-04-22 08:12:33 +00002208 }
2209
Raymond Hettinger3f8caa32007-10-24 01:28:33 +00002210#ifndef SLOW_SUM
2211 /* Fast addition by keeping temporary sums in C instead of new Python objects.
2212 Assumes all inputs are the same type. If the assumption fails, default
2213 to the more general routine.
2214 */
2215 if (PyInt_CheckExact(result)) {
2216 long i_result = PyInt_AS_LONG(result);
2217 Py_DECREF(result);
2218 result = NULL;
2219 while(result == NULL) {
2220 item = PyIter_Next(iter);
2221 if (item == NULL) {
2222 Py_DECREF(iter);
2223 if (PyErr_Occurred())
2224 return NULL;
2225 return PyInt_FromLong(i_result);
2226 }
2227 if (PyInt_CheckExact(item)) {
2228 long b = PyInt_AS_LONG(item);
2229 long x = i_result + b;
2230 if ((x^i_result) >= 0 || (x^b) >= 0) {
2231 i_result = x;
2232 Py_DECREF(item);
2233 continue;
2234 }
2235 }
2236 /* Either overflowed or is not an int. Restore real objects and process normally */
2237 result = PyInt_FromLong(i_result);
2238 temp = PyNumber_Add(result, item);
2239 Py_DECREF(result);
2240 Py_DECREF(item);
2241 result = temp;
2242 if (result == NULL) {
2243 Py_DECREF(iter);
2244 return NULL;
2245 }
2246 }
2247 }
2248
2249 if (PyFloat_CheckExact(result)) {
2250 double f_result = PyFloat_AS_DOUBLE(result);
2251 Py_DECREF(result);
2252 result = NULL;
2253 while(result == NULL) {
2254 item = PyIter_Next(iter);
2255 if (item == NULL) {
2256 Py_DECREF(iter);
2257 if (PyErr_Occurred())
2258 return NULL;
2259 return PyFloat_FromDouble(f_result);
2260 }
2261 if (PyFloat_CheckExact(item)) {
Raymond Hettinger65856602008-05-30 06:37:27 +00002262 PyFPE_START_PROTECT("add", Py_DECREF(item); Py_DECREF(iter); return 0)
Raymond Hettinger3f8caa32007-10-24 01:28:33 +00002263 f_result += PyFloat_AS_DOUBLE(item);
Raymond Hettinger3a8daf52007-10-24 02:05:51 +00002264 PyFPE_END_PROTECT(f_result)
Raymond Hettingera45c4872007-10-25 02:26:58 +00002265 Py_DECREF(item);
Raymond Hettinger3a8daf52007-10-24 02:05:51 +00002266 continue;
2267 }
2268 if (PyInt_CheckExact(item)) {
Raymond Hettinger65856602008-05-30 06:37:27 +00002269 PyFPE_START_PROTECT("add", Py_DECREF(item); Py_DECREF(iter); return 0)
Raymond Hettinger3a8daf52007-10-24 02:05:51 +00002270 f_result += (double)PyInt_AS_LONG(item);
2271 PyFPE_END_PROTECT(f_result)
Raymond Hettingera45c4872007-10-25 02:26:58 +00002272 Py_DECREF(item);
Raymond Hettinger3f8caa32007-10-24 01:28:33 +00002273 continue;
2274 }
2275 result = PyFloat_FromDouble(f_result);
2276 temp = PyNumber_Add(result, item);
2277 Py_DECREF(result);
2278 Py_DECREF(item);
2279 result = temp;
2280 if (result == NULL) {
2281 Py_DECREF(iter);
2282 return NULL;
2283 }
2284 }
2285 }
2286#endif
2287
Alex Martellia70b1912003-04-22 08:12:33 +00002288 for(;;) {
2289 item = PyIter_Next(iter);
2290 if (item == NULL) {
2291 /* error, or end-of-sequence */
2292 if (PyErr_Occurred()) {
2293 Py_DECREF(result);
2294 result = NULL;
2295 }
2296 break;
2297 }
Alex Martellia253e182003-10-25 23:24:14 +00002298 temp = PyNumber_Add(result, item);
Alex Martellia70b1912003-04-22 08:12:33 +00002299 Py_DECREF(result);
2300 Py_DECREF(item);
2301 result = temp;
2302 if (result == NULL)
2303 break;
2304 }
2305 Py_DECREF(iter);
2306 return result;
2307}
2308
2309PyDoc_STRVAR(sum_doc,
Georg Brandl8134d062006-10-12 12:33:07 +00002310"sum(sequence[, start]) -> value\n\
Alex Martellia70b1912003-04-22 08:12:33 +00002311\n\
2312Returns the sum of a sequence of numbers (NOT strings) plus the value\n\
Georg Brandl8134d062006-10-12 12:33:07 +00002313of parameter 'start' (which defaults to 0). When the sequence is\n\
2314empty, returns start.");
Alex Martellia70b1912003-04-22 08:12:33 +00002315
2316
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002317static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002318builtin_isinstance(PyObject *self, PyObject *args)
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002319{
2320 PyObject *inst;
2321 PyObject *cls;
Guido van Rossum823649d2001-03-21 18:40:58 +00002322 int retval;
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002323
Raymond Hettingerea3fdf42002-12-29 16:33:45 +00002324 if (!PyArg_UnpackTuple(args, "isinstance", 2, 2, &inst, &cls))
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002325 return NULL;
Guido van Rossumf5dd9141997-12-02 19:11:45 +00002326
Guido van Rossum823649d2001-03-21 18:40:58 +00002327 retval = PyObject_IsInstance(inst, cls);
2328 if (retval < 0)
Guido van Rossumad991772001-01-12 16:03:05 +00002329 return NULL;
Guido van Rossum77f6a652002-04-03 22:41:51 +00002330 return PyBool_FromLong(retval);
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002331}
2332
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002333PyDoc_STRVAR(isinstance_doc,
Guido van Rossum77f6a652002-04-03 22:41:51 +00002334"isinstance(object, class-or-type-or-tuple) -> bool\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002335\n\
2336Return whether an object is an instance of a class or of a subclass thereof.\n\
Guido van Rossum03290ec2001-10-07 20:54:12 +00002337With a type as second argument, return whether that is the object's type.\n\
2338The form using a tuple, isinstance(x, (A, B, ...)), is a shortcut for\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002339isinstance(x, A) or isinstance(x, B) or ... (etc.).");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002340
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002341
2342static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002343builtin_issubclass(PyObject *self, PyObject *args)
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002344{
2345 PyObject *derived;
2346 PyObject *cls;
2347 int retval;
2348
Raymond Hettingerea3fdf42002-12-29 16:33:45 +00002349 if (!PyArg_UnpackTuple(args, "issubclass", 2, 2, &derived, &cls))
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002350 return NULL;
Guido van Rossum668213d1999-06-16 17:28:37 +00002351
Guido van Rossum823649d2001-03-21 18:40:58 +00002352 retval = PyObject_IsSubclass(derived, cls);
2353 if (retval < 0)
2354 return NULL;
Guido van Rossum77f6a652002-04-03 22:41:51 +00002355 return PyBool_FromLong(retval);
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002356}
2357
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002358PyDoc_STRVAR(issubclass_doc,
Guido van Rossum77f6a652002-04-03 22:41:51 +00002359"issubclass(C, B) -> bool\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002360\n\
Walter Dörwaldd9a6ad32002-12-12 16:41:44 +00002361Return whether class C is a subclass (i.e., a derived class) of class B.\n\
2362When using a tuple as the second argument issubclass(X, (A, B, ...)),\n\
2363is a shortcut for issubclass(X, A) or issubclass(X, B) or ... (etc.).");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002364
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002365
Barry Warsawbd599b52000-08-03 15:45:29 +00002366static PyObject*
2367builtin_zip(PyObject *self, PyObject *args)
2368{
2369 PyObject *ret;
Martin v. Löwisd96ee902006-02-16 14:37:16 +00002370 const Py_ssize_t itemsize = PySequence_Length(args);
2371 Py_ssize_t i;
Tim Peters8572b4f2001-05-06 01:05:02 +00002372 PyObject *itlist; /* tuple of iterators */
Martin v. Löwisd96ee902006-02-16 14:37:16 +00002373 Py_ssize_t len; /* guess at result length */
Barry Warsawbd599b52000-08-03 15:45:29 +00002374
Raymond Hettingereaef6152003-08-02 07:42:57 +00002375 if (itemsize == 0)
2376 return PyList_New(0);
2377
Barry Warsawbd599b52000-08-03 15:45:29 +00002378 /* args must be a tuple */
2379 assert(PyTuple_Check(args));
2380
Tim Peters39a86c22002-05-12 07:19:38 +00002381 /* Guess at result length: the shortest of the input lengths.
2382 If some argument refuses to say, we refuse to guess too, lest
2383 an argument like xrange(sys.maxint) lead us astray.*/
Tim Peters67d687a2002-04-29 21:27:32 +00002384 len = -1; /* unknown */
2385 for (i = 0; i < itemsize; ++i) {
2386 PyObject *item = PyTuple_GET_ITEM(args, i);
Raymond Hettinger4e2f7142007-12-06 00:56:53 +00002387 Py_ssize_t thislen = _PyObject_LengthHint(item, -1);
Tim Peters39a86c22002-05-12 07:19:38 +00002388 if (thislen < 0) {
Tim Peters39a86c22002-05-12 07:19:38 +00002389 len = -1;
2390 break;
2391 }
Tim Peters67d687a2002-04-29 21:27:32 +00002392 else if (len < 0 || thislen < len)
2393 len = thislen;
2394 }
2395
Tim Peters8572b4f2001-05-06 01:05:02 +00002396 /* allocate result list */
Tim Peters67d687a2002-04-29 21:27:32 +00002397 if (len < 0)
2398 len = 10; /* arbitrary */
2399 if ((ret = PyList_New(len)) == NULL)
Barry Warsawbd599b52000-08-03 15:45:29 +00002400 return NULL;
2401
Tim Peters8572b4f2001-05-06 01:05:02 +00002402 /* obtain iterators */
2403 itlist = PyTuple_New(itemsize);
2404 if (itlist == NULL)
2405 goto Fail_ret;
2406 for (i = 0; i < itemsize; ++i) {
2407 PyObject *item = PyTuple_GET_ITEM(args, i);
2408 PyObject *it = PyObject_GetIter(item);
2409 if (it == NULL) {
2410 if (PyErr_ExceptionMatches(PyExc_TypeError))
2411 PyErr_Format(PyExc_TypeError,
Neal Norwitza361bd82006-02-19 19:31:50 +00002412 "zip argument #%zd must support iteration",
Tim Peters8572b4f2001-05-06 01:05:02 +00002413 i+1);
2414 goto Fail_ret_itlist;
Barry Warsawbd599b52000-08-03 15:45:29 +00002415 }
Tim Peters8572b4f2001-05-06 01:05:02 +00002416 PyTuple_SET_ITEM(itlist, i, it);
2417 }
Barry Warsawbd599b52000-08-03 15:45:29 +00002418
Tim Peters8572b4f2001-05-06 01:05:02 +00002419 /* build result into ret list */
Tim Peters67d687a2002-04-29 21:27:32 +00002420 for (i = 0; ; ++i) {
2421 int j;
Tim Peters8572b4f2001-05-06 01:05:02 +00002422 PyObject *next = PyTuple_New(itemsize);
2423 if (!next)
2424 goto Fail_ret_itlist;
2425
Tim Peters67d687a2002-04-29 21:27:32 +00002426 for (j = 0; j < itemsize; j++) {
2427 PyObject *it = PyTuple_GET_ITEM(itlist, j);
Tim Peters8572b4f2001-05-06 01:05:02 +00002428 PyObject *item = PyIter_Next(it);
Barry Warsawbd599b52000-08-03 15:45:29 +00002429 if (!item) {
Tim Peters8572b4f2001-05-06 01:05:02 +00002430 if (PyErr_Occurred()) {
2431 Py_DECREF(ret);
2432 ret = NULL;
Barry Warsawbd599b52000-08-03 15:45:29 +00002433 }
2434 Py_DECREF(next);
Tim Peters8572b4f2001-05-06 01:05:02 +00002435 Py_DECREF(itlist);
Tim Peters67d687a2002-04-29 21:27:32 +00002436 goto Done;
Barry Warsawbd599b52000-08-03 15:45:29 +00002437 }
Tim Peters67d687a2002-04-29 21:27:32 +00002438 PyTuple_SET_ITEM(next, j, item);
Barry Warsawbd599b52000-08-03 15:45:29 +00002439 }
Tim Peters8572b4f2001-05-06 01:05:02 +00002440
Tim Peters67d687a2002-04-29 21:27:32 +00002441 if (i < len)
2442 PyList_SET_ITEM(ret, i, next);
2443 else {
2444 int status = PyList_Append(ret, next);
2445 Py_DECREF(next);
2446 ++len;
2447 if (status < 0)
2448 goto Fail_ret_itlist;
2449 }
Barry Warsawbd599b52000-08-03 15:45:29 +00002450 }
Tim Peters8572b4f2001-05-06 01:05:02 +00002451
Tim Peters67d687a2002-04-29 21:27:32 +00002452Done:
2453 if (ret != NULL && i < len) {
2454 /* The list is too big. */
2455 if (PyList_SetSlice(ret, i, len, NULL) < 0)
2456 return NULL;
2457 }
2458 return ret;
2459
Tim Peters8572b4f2001-05-06 01:05:02 +00002460Fail_ret_itlist:
2461 Py_DECREF(itlist);
2462Fail_ret:
2463 Py_DECREF(ret);
2464 return NULL;
Barry Warsawbd599b52000-08-03 15:45:29 +00002465}
2466
2467
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002468PyDoc_STRVAR(zip_doc,
Barry Warsawbd599b52000-08-03 15:45:29 +00002469"zip(seq1 [, seq2 [...]]) -> [(seq1[0], seq2[0] ...), (...)]\n\
2470\n\
2471Return a list of tuples, where each tuple contains the i-th element\n\
2472from each of the argument sequences. The returned list is truncated\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002473in length to the length of the shortest argument sequence.");
Barry Warsawbd599b52000-08-03 15:45:29 +00002474
2475
Guido van Rossum79f25d91997-04-29 20:08:16 +00002476static PyMethodDef builtin_methods[] = {
Neal Norwitz92e212f2006-04-03 04:48:37 +00002477 {"__import__", (PyCFunction)builtin___import__, METH_VARARGS | METH_KEYWORDS, import_doc},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002478 {"abs", builtin_abs, METH_O, abs_doc},
Raymond Hettinger96229b12005-03-11 06:49:40 +00002479 {"all", builtin_all, METH_O, all_doc},
2480 {"any", builtin_any, METH_O, any_doc},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002481 {"apply", builtin_apply, METH_VARARGS, apply_doc},
Eric Smith3cd81942008-02-22 16:30:22 +00002482 {"bin", builtin_bin, METH_O, bin_doc},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002483 {"callable", builtin_callable, METH_O, callable_doc},
2484 {"chr", builtin_chr, METH_VARARGS, chr_doc},
2485 {"cmp", builtin_cmp, METH_VARARGS, cmp_doc},
2486 {"coerce", builtin_coerce, METH_VARARGS, coerce_doc},
Georg Brandl5240d742007-03-13 20:46:32 +00002487 {"compile", (PyCFunction)builtin_compile, METH_VARARGS | METH_KEYWORDS, compile_doc},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002488 {"delattr", builtin_delattr, METH_VARARGS, delattr_doc},
2489 {"dir", builtin_dir, METH_VARARGS, dir_doc},
2490 {"divmod", builtin_divmod, METH_VARARGS, divmod_doc},
2491 {"eval", builtin_eval, METH_VARARGS, eval_doc},
2492 {"execfile", builtin_execfile, METH_VARARGS, execfile_doc},
2493 {"filter", builtin_filter, METH_VARARGS, filter_doc},
Eric Smitha9f7d622008-02-17 19:46:49 +00002494 {"format", builtin_format, METH_VARARGS, format_doc},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002495 {"getattr", builtin_getattr, METH_VARARGS, getattr_doc},
2496 {"globals", (PyCFunction)builtin_globals, METH_NOARGS, globals_doc},
2497 {"hasattr", builtin_hasattr, METH_VARARGS, hasattr_doc},
2498 {"hash", builtin_hash, METH_O, hash_doc},
2499 {"hex", builtin_hex, METH_O, hex_doc},
2500 {"id", builtin_id, METH_O, id_doc},
2501 {"input", builtin_input, METH_VARARGS, input_doc},
2502 {"intern", builtin_intern, METH_VARARGS, intern_doc},
2503 {"isinstance", builtin_isinstance, METH_VARARGS, isinstance_doc},
2504 {"issubclass", builtin_issubclass, METH_VARARGS, issubclass_doc},
2505 {"iter", builtin_iter, METH_VARARGS, iter_doc},
2506 {"len", builtin_len, METH_O, len_doc},
2507 {"locals", (PyCFunction)builtin_locals, METH_NOARGS, locals_doc},
2508 {"map", builtin_map, METH_VARARGS, map_doc},
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00002509 {"max", (PyCFunction)builtin_max, METH_VARARGS | METH_KEYWORDS, max_doc},
2510 {"min", (PyCFunction)builtin_min, METH_VARARGS | METH_KEYWORDS, min_doc},
Georg Brandl28e08732008-04-30 19:47:09 +00002511 {"next", builtin_next, METH_VARARGS, next_doc},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002512 {"oct", builtin_oct, METH_O, oct_doc},
Neal Norwitzc4edb0e2006-05-02 04:43:14 +00002513 {"open", (PyCFunction)builtin_open, METH_VARARGS | METH_KEYWORDS, open_doc},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002514 {"ord", builtin_ord, METH_O, ord_doc},
2515 {"pow", builtin_pow, METH_VARARGS, pow_doc},
Eric Smith7c478942008-03-18 23:45:49 +00002516 {"print", (PyCFunction)builtin_print, METH_VARARGS | METH_KEYWORDS, print_doc},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002517 {"range", builtin_range, METH_VARARGS, range_doc},
2518 {"raw_input", builtin_raw_input, METH_VARARGS, raw_input_doc},
2519 {"reduce", builtin_reduce, METH_VARARGS, reduce_doc},
2520 {"reload", builtin_reload, METH_O, reload_doc},
2521 {"repr", builtin_repr, METH_O, repr_doc},
Georg Brandlccadf842006-03-31 18:54:53 +00002522 {"round", (PyCFunction)builtin_round, METH_VARARGS | METH_KEYWORDS, round_doc},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002523 {"setattr", builtin_setattr, METH_VARARGS, setattr_doc},
Raymond Hettinger64958a12003-12-17 20:43:33 +00002524 {"sorted", (PyCFunction)builtin_sorted, METH_VARARGS | METH_KEYWORDS, sorted_doc},
Alex Martellia70b1912003-04-22 08:12:33 +00002525 {"sum", builtin_sum, METH_VARARGS, sum_doc},
Martin v. Löwis339d0f72001-08-17 18:39:25 +00002526#ifdef Py_USING_UNICODE
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002527 {"unichr", builtin_unichr, METH_VARARGS, unichr_doc},
Martin v. Löwis339d0f72001-08-17 18:39:25 +00002528#endif
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002529 {"vars", builtin_vars, METH_VARARGS, vars_doc},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002530 {"zip", builtin_zip, METH_VARARGS, zip_doc},
Guido van Rossumc02e15c1991-12-16 13:03:00 +00002531 {NULL, NULL},
Guido van Rossum3f5da241990-12-20 15:06:42 +00002532};
2533
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002534PyDoc_STRVAR(builtin_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002535"Built-in functions, exceptions, and other objects.\n\
2536\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002537Noteworthy: None is the `nil' object; Ellipsis represents `...' in slices.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002538
Guido van Rossum25ce5661997-08-02 03:10:38 +00002539PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002540_PyBuiltin_Init(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +00002541{
Fred Drake5550de32000-06-20 04:54:19 +00002542 PyObject *mod, *dict, *debug;
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002543 mod = Py_InitModule4("__builtin__", builtin_methods,
2544 builtin_doc, (PyObject *)NULL,
2545 PYTHON_API_VERSION);
Guido van Rossum25ce5661997-08-02 03:10:38 +00002546 if (mod == NULL)
2547 return NULL;
2548 dict = PyModule_GetDict(mod);
Tim Peters4b7625e2001-09-13 21:37:17 +00002549
Tim Peters7571a0f2003-03-23 17:52:28 +00002550#ifdef Py_TRACE_REFS
2551 /* __builtin__ exposes a number of statically allocated objects
2552 * that, before this code was added in 2.3, never showed up in
2553 * the list of "all objects" maintained by Py_TRACE_REFS. As a
2554 * result, programs leaking references to None and False (etc)
2555 * couldn't be diagnosed by examining sys.getobjects(0).
2556 */
2557#define ADD_TO_ALL(OBJECT) _Py_AddToAllObjects((PyObject *)(OBJECT), 0)
2558#else
2559#define ADD_TO_ALL(OBJECT) (void)0
2560#endif
2561
Tim Peters4b7625e2001-09-13 21:37:17 +00002562#define SETBUILTIN(NAME, OBJECT) \
Tim Peters7571a0f2003-03-23 17:52:28 +00002563 if (PyDict_SetItemString(dict, NAME, (PyObject *)OBJECT) < 0) \
2564 return NULL; \
2565 ADD_TO_ALL(OBJECT)
Tim Peters4b7625e2001-09-13 21:37:17 +00002566
2567 SETBUILTIN("None", Py_None);
2568 SETBUILTIN("Ellipsis", Py_Ellipsis);
2569 SETBUILTIN("NotImplemented", Py_NotImplemented);
Guido van Rossum77f6a652002-04-03 22:41:51 +00002570 SETBUILTIN("False", Py_False);
2571 SETBUILTIN("True", Py_True);
Neal Norwitz32a7e7f2002-05-31 19:58:02 +00002572 SETBUILTIN("basestring", &PyBaseString_Type);
Guido van Rossum77f6a652002-04-03 22:41:51 +00002573 SETBUILTIN("bool", &PyBool_Type);
Travis E. Oliphant3781aef2008-03-18 04:44:57 +00002574 /* SETBUILTIN("memoryview", &PyMemoryView_Type); */
Christian Heimes3497f942008-05-26 12:29:14 +00002575 SETBUILTIN("bytearray", &PyByteArray_Type);
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002576 SETBUILTIN("bytes", &PyString_Type);
Guido van Rossumbea18cc2002-06-14 20:41:17 +00002577 SETBUILTIN("buffer", &PyBuffer_Type);
Tim Peters4b7625e2001-09-13 21:37:17 +00002578 SETBUILTIN("classmethod", &PyClassMethod_Type);
Tim Peters6d6c1a32001-08-02 04:15:00 +00002579#ifndef WITHOUT_COMPLEX
Tim Peters4b7625e2001-09-13 21:37:17 +00002580 SETBUILTIN("complex", &PyComplex_Type);
Tim Peters6d6c1a32001-08-02 04:15:00 +00002581#endif
Tim Petersa427a2b2001-10-29 22:25:45 +00002582 SETBUILTIN("dict", &PyDict_Type);
Tim Peters67d687a2002-04-29 21:27:32 +00002583 SETBUILTIN("enumerate", &PyEnum_Type);
Neal Norwitzc4edb0e2006-05-02 04:43:14 +00002584 SETBUILTIN("file", &PyFile_Type);
Tim Peters4b7625e2001-09-13 21:37:17 +00002585 SETBUILTIN("float", &PyFloat_Type);
Raymond Hettingera690a992003-11-16 16:17:49 +00002586 SETBUILTIN("frozenset", &PyFrozenSet_Type);
Tim Peters4b7625e2001-09-13 21:37:17 +00002587 SETBUILTIN("property", &PyProperty_Type);
2588 SETBUILTIN("int", &PyInt_Type);
2589 SETBUILTIN("list", &PyList_Type);
2590 SETBUILTIN("long", &PyLong_Type);
2591 SETBUILTIN("object", &PyBaseObject_Type);
Raymond Hettinger85c20a42003-11-06 14:06:48 +00002592 SETBUILTIN("reversed", &PyReversed_Type);
Raymond Hettingera690a992003-11-16 16:17:49 +00002593 SETBUILTIN("set", &PySet_Type);
Guido van Rossumbea18cc2002-06-14 20:41:17 +00002594 SETBUILTIN("slice", &PySlice_Type);
Tim Peters4b7625e2001-09-13 21:37:17 +00002595 SETBUILTIN("staticmethod", &PyStaticMethod_Type);
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002596 SETBUILTIN("str", &PyString_Type);
Tim Peters4b7625e2001-09-13 21:37:17 +00002597 SETBUILTIN("super", &PySuper_Type);
2598 SETBUILTIN("tuple", &PyTuple_Type);
2599 SETBUILTIN("type", &PyType_Type);
Raymond Hettingerc4c453f2002-06-05 23:12:45 +00002600 SETBUILTIN("xrange", &PyRange_Type);
Martin v. Löwis339d0f72001-08-17 18:39:25 +00002601#ifdef Py_USING_UNICODE
Tim Peters4b7625e2001-09-13 21:37:17 +00002602 SETBUILTIN("unicode", &PyUnicode_Type);
Martin v. Löwis339d0f72001-08-17 18:39:25 +00002603#endif
Guido van Rossum77f6a652002-04-03 22:41:51 +00002604 debug = PyBool_FromLong(Py_OptimizeFlag == 0);
Fred Drake5550de32000-06-20 04:54:19 +00002605 if (PyDict_SetItemString(dict, "__debug__", debug) < 0) {
2606 Py_XDECREF(debug);
Guido van Rossum25ce5661997-08-02 03:10:38 +00002607 return NULL;
Fred Drake5550de32000-06-20 04:54:19 +00002608 }
2609 Py_XDECREF(debug);
Barry Warsaw757af0e1997-08-29 22:13:51 +00002610
Guido van Rossum25ce5661997-08-02 03:10:38 +00002611 return mod;
Tim Peters7571a0f2003-03-23 17:52:28 +00002612#undef ADD_TO_ALL
Tim Peters4b7625e2001-09-13 21:37:17 +00002613#undef SETBUILTIN
Guido van Rossum3f5da241990-12-20 15:06:42 +00002614}
2615
Guido van Rossume77a7571993-11-03 15:01:26 +00002616/* Helper for filter(): filter a tuple through a function */
Guido van Rossum12d12c51993-10-26 17:58:25 +00002617
Guido van Rossum79f25d91997-04-29 20:08:16 +00002618static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002619filtertuple(PyObject *func, PyObject *tuple)
Guido van Rossum12d12c51993-10-26 17:58:25 +00002620{
Guido van Rossum79f25d91997-04-29 20:08:16 +00002621 PyObject *result;
Martin v. Löwis18e16552006-02-15 17:27:45 +00002622 Py_ssize_t i, j;
2623 Py_ssize_t len = PyTuple_Size(tuple);
Guido van Rossum12d12c51993-10-26 17:58:25 +00002624
Guido van Rossumb7b45621995-08-04 04:07:45 +00002625 if (len == 0) {
Walter Dörwaldc3da83f2003-02-04 20:24:45 +00002626 if (PyTuple_CheckExact(tuple))
2627 Py_INCREF(tuple);
2628 else
2629 tuple = PyTuple_New(0);
Guido van Rossumb7b45621995-08-04 04:07:45 +00002630 return tuple;
2631 }
2632
Guido van Rossum79f25d91997-04-29 20:08:16 +00002633 if ((result = PyTuple_New(len)) == NULL)
Guido van Rossum2586bf01993-11-01 16:21:44 +00002634 return NULL;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002635
Guido van Rossum12d12c51993-10-26 17:58:25 +00002636 for (i = j = 0; i < len; ++i) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002637 PyObject *item, *good;
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002638 int ok;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002639
Walter Dörwald8dd19322003-02-10 17:36:40 +00002640 if (tuple->ob_type->tp_as_sequence &&
2641 tuple->ob_type->tp_as_sequence->sq_item) {
2642 item = tuple->ob_type->tp_as_sequence->sq_item(tuple, i);
Walter Dörwaldc58a3a12003-08-18 18:28:45 +00002643 if (item == NULL)
2644 goto Fail_1;
Walter Dörwald8dd19322003-02-10 17:36:40 +00002645 } else {
Alex Martellia9b9c9f2003-04-23 13:34:35 +00002646 PyErr_SetString(PyExc_TypeError, "filter(): unsubscriptable tuple");
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002647 goto Fail_1;
Walter Dörwald8dd19322003-02-10 17:36:40 +00002648 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002649 if (func == Py_None) {
2650 Py_INCREF(item);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002651 good = item;
2652 }
2653 else {
Raymond Hettinger8ae46892003-10-12 19:09:37 +00002654 PyObject *arg = PyTuple_Pack(1, item);
Walter Dörwaldc58a3a12003-08-18 18:28:45 +00002655 if (arg == NULL) {
2656 Py_DECREF(item);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002657 goto Fail_1;
Walter Dörwaldc58a3a12003-08-18 18:28:45 +00002658 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002659 good = PyEval_CallObject(func, arg);
2660 Py_DECREF(arg);
Walter Dörwaldc58a3a12003-08-18 18:28:45 +00002661 if (good == NULL) {
2662 Py_DECREF(item);
Guido van Rossum12d12c51993-10-26 17:58:25 +00002663 goto Fail_1;
Walter Dörwaldc58a3a12003-08-18 18:28:45 +00002664 }
Guido van Rossum12d12c51993-10-26 17:58:25 +00002665 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002666 ok = PyObject_IsTrue(good);
2667 Py_DECREF(good);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002668 if (ok) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002669 if (PyTuple_SetItem(result, j++, item) < 0)
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002670 goto Fail_1;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002671 }
Walter Dörwaldc58a3a12003-08-18 18:28:45 +00002672 else
2673 Py_DECREF(item);
Guido van Rossum12d12c51993-10-26 17:58:25 +00002674 }
2675
Tim Peters4324aa32001-05-28 22:30:08 +00002676 if (_PyTuple_Resize(&result, j) < 0)
Guido van Rossum12d12c51993-10-26 17:58:25 +00002677 return NULL;
2678
Guido van Rossum12d12c51993-10-26 17:58:25 +00002679 return result;
2680
Guido van Rossum12d12c51993-10-26 17:58:25 +00002681Fail_1:
Guido van Rossum79f25d91997-04-29 20:08:16 +00002682 Py_DECREF(result);
Guido van Rossum12d12c51993-10-26 17:58:25 +00002683 return NULL;
2684}
2685
2686
Guido van Rossume77a7571993-11-03 15:01:26 +00002687/* Helper for filter(): filter a string through a function */
Guido van Rossum12d12c51993-10-26 17:58:25 +00002688
Guido van Rossum79f25d91997-04-29 20:08:16 +00002689static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002690filterstring(PyObject *func, PyObject *strobj)
Guido van Rossum12d12c51993-10-26 17:58:25 +00002691{
Guido van Rossum79f25d91997-04-29 20:08:16 +00002692 PyObject *result;
Martin v. Löwis18e16552006-02-15 17:27:45 +00002693 Py_ssize_t i, j;
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002694 Py_ssize_t len = PyString_Size(strobj);
Martin v. Löwis18e16552006-02-15 17:27:45 +00002695 Py_ssize_t outlen = len;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002696
Guido van Rossum79f25d91997-04-29 20:08:16 +00002697 if (func == Py_None) {
Walter Dörwald1918f772003-02-10 13:19:13 +00002698 /* If it's a real string we can return the original,
2699 * as no character is ever false and __getitem__
2700 * does return this character. If it's a subclass
2701 * we must go through the __getitem__ loop */
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002702 if (PyString_CheckExact(strobj)) {
Walter Dörwaldc3da83f2003-02-04 20:24:45 +00002703 Py_INCREF(strobj);
Walter Dörwald1918f772003-02-10 13:19:13 +00002704 return strobj;
2705 }
Guido van Rossum12d12c51993-10-26 17:58:25 +00002706 }
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002707 if ((result = PyString_FromStringAndSize(NULL, len)) == NULL)
Guido van Rossum2586bf01993-11-01 16:21:44 +00002708 return NULL;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002709
Guido van Rossum12d12c51993-10-26 17:58:25 +00002710 for (i = j = 0; i < len; ++i) {
Walter Dörwald1918f772003-02-10 13:19:13 +00002711 PyObject *item;
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002712 int ok;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002713
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002714 item = (*strobj->ob_type->tp_as_sequence->sq_item)(strobj, i);
2715 if (item == NULL)
2716 goto Fail_1;
Walter Dörwald1918f772003-02-10 13:19:13 +00002717 if (func==Py_None) {
2718 ok = 1;
2719 } else {
2720 PyObject *arg, *good;
Raymond Hettinger8ae46892003-10-12 19:09:37 +00002721 arg = PyTuple_Pack(1, item);
Walter Dörwald1918f772003-02-10 13:19:13 +00002722 if (arg == NULL) {
2723 Py_DECREF(item);
2724 goto Fail_1;
2725 }
2726 good = PyEval_CallObject(func, arg);
2727 Py_DECREF(arg);
2728 if (good == NULL) {
2729 Py_DECREF(item);
2730 goto Fail_1;
2731 }
2732 ok = PyObject_IsTrue(good);
2733 Py_DECREF(good);
Tim Peters388ed082001-04-07 20:34:48 +00002734 }
Walter Dörwald903f1e02003-02-04 16:28:00 +00002735 if (ok) {
Martin v. Löwisd96ee902006-02-16 14:37:16 +00002736 Py_ssize_t reslen;
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002737 if (!PyString_Check(item)) {
Walter Dörwald903f1e02003-02-04 16:28:00 +00002738 PyErr_SetString(PyExc_TypeError, "can't filter str to str:"
2739 " __getitem__ returned different type");
2740 Py_DECREF(item);
2741 goto Fail_1;
2742 }
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002743 reslen = PyString_GET_SIZE(item);
Walter Dörwald903f1e02003-02-04 16:28:00 +00002744 if (reslen == 1) {
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002745 PyString_AS_STRING(result)[j++] =
2746 PyString_AS_STRING(item)[0];
Walter Dörwald903f1e02003-02-04 16:28:00 +00002747 } else {
2748 /* do we need more space? */
Gregory P. Smith9d534572008-06-11 07:41:16 +00002749 Py_ssize_t need = j;
2750
2751 /* calculate space requirements while checking for overflow */
2752 if (need > PY_SSIZE_T_MAX - reslen) {
2753 Py_DECREF(item);
2754 goto Fail_1;
2755 }
2756
2757 need += reslen;
2758
2759 if (need > PY_SSIZE_T_MAX - len) {
2760 Py_DECREF(item);
2761 goto Fail_1;
2762 }
2763
2764 need += len;
2765
2766 if (need <= i) {
2767 Py_DECREF(item);
2768 goto Fail_1;
2769 }
2770
2771 need = need - i - 1;
2772
2773 assert(need >= 0);
2774 assert(outlen >= 0);
2775
Walter Dörwald903f1e02003-02-04 16:28:00 +00002776 if (need > outlen) {
2777 /* overallocate, to avoid reallocations */
Gregory P. Smith9d534572008-06-11 07:41:16 +00002778 if (outlen > PY_SSIZE_T_MAX / 2) {
2779 Py_DECREF(item);
2780 return NULL;
2781 }
2782
2783 if (need<2*outlen) {
Walter Dörwald903f1e02003-02-04 16:28:00 +00002784 need = 2*outlen;
Gregory P. Smith9d534572008-06-11 07:41:16 +00002785 }
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002786 if (_PyString_Resize(&result, need)) {
Walter Dörwald903f1e02003-02-04 16:28:00 +00002787 Py_DECREF(item);
2788 return NULL;
2789 }
2790 outlen = need;
2791 }
2792 memcpy(
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002793 PyString_AS_STRING(result) + j,
2794 PyString_AS_STRING(item),
Walter Dörwald903f1e02003-02-04 16:28:00 +00002795 reslen
2796 );
2797 j += reslen;
2798 }
2799 }
Tim Peters388ed082001-04-07 20:34:48 +00002800 Py_DECREF(item);
Guido van Rossum12d12c51993-10-26 17:58:25 +00002801 }
2802
Walter Dörwald903f1e02003-02-04 16:28:00 +00002803 if (j < outlen)
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002804 _PyString_Resize(&result, j);
Guido van Rossum12d12c51993-10-26 17:58:25 +00002805
Guido van Rossum12d12c51993-10-26 17:58:25 +00002806 return result;
2807
Guido van Rossum12d12c51993-10-26 17:58:25 +00002808Fail_1:
Guido van Rossum79f25d91997-04-29 20:08:16 +00002809 Py_DECREF(result);
Guido van Rossum12d12c51993-10-26 17:58:25 +00002810 return NULL;
2811}
Martin v. Löwis8afd7572003-01-25 22:46:11 +00002812
2813#ifdef Py_USING_UNICODE
2814/* Helper for filter(): filter a Unicode object through a function */
2815
2816static PyObject *
2817filterunicode(PyObject *func, PyObject *strobj)
2818{
2819 PyObject *result;
Martin v. Löwis725507b2006-03-07 12:08:51 +00002820 register Py_ssize_t i, j;
Martin v. Löwisd96ee902006-02-16 14:37:16 +00002821 Py_ssize_t len = PyUnicode_GetSize(strobj);
2822 Py_ssize_t outlen = len;
Martin v. Löwis8afd7572003-01-25 22:46:11 +00002823
2824 if (func == Py_None) {
Walter Dörwald1918f772003-02-10 13:19:13 +00002825 /* If it's a real string we can return the original,
2826 * as no character is ever false and __getitem__
2827 * does return this character. If it's a subclass
2828 * we must go through the __getitem__ loop */
2829 if (PyUnicode_CheckExact(strobj)) {
Walter Dörwaldc3da83f2003-02-04 20:24:45 +00002830 Py_INCREF(strobj);
Walter Dörwald1918f772003-02-10 13:19:13 +00002831 return strobj;
2832 }
Martin v. Löwis8afd7572003-01-25 22:46:11 +00002833 }
2834 if ((result = PyUnicode_FromUnicode(NULL, len)) == NULL)
2835 return NULL;
2836
2837 for (i = j = 0; i < len; ++i) {
2838 PyObject *item, *arg, *good;
2839 int ok;
2840
2841 item = (*strobj->ob_type->tp_as_sequence->sq_item)(strobj, i);
2842 if (item == NULL)
2843 goto Fail_1;
Walter Dörwald1918f772003-02-10 13:19:13 +00002844 if (func == Py_None) {
2845 ok = 1;
2846 } else {
Raymond Hettinger8ae46892003-10-12 19:09:37 +00002847 arg = PyTuple_Pack(1, item);
Walter Dörwald1918f772003-02-10 13:19:13 +00002848 if (arg == NULL) {
2849 Py_DECREF(item);
2850 goto Fail_1;
2851 }
2852 good = PyEval_CallObject(func, arg);
2853 Py_DECREF(arg);
2854 if (good == NULL) {
2855 Py_DECREF(item);
2856 goto Fail_1;
2857 }
2858 ok = PyObject_IsTrue(good);
2859 Py_DECREF(good);
Martin v. Löwis8afd7572003-01-25 22:46:11 +00002860 }
Walter Dörwald903f1e02003-02-04 16:28:00 +00002861 if (ok) {
Martin v. Löwisd96ee902006-02-16 14:37:16 +00002862 Py_ssize_t reslen;
Walter Dörwald903f1e02003-02-04 16:28:00 +00002863 if (!PyUnicode_Check(item)) {
Georg Brandl99d7e4e2005-08-31 22:21:15 +00002864 PyErr_SetString(PyExc_TypeError,
Jeremy Hylton1aad9c72003-09-16 03:10:59 +00002865 "can't filter unicode to unicode:"
2866 " __getitem__ returned different type");
Walter Dörwald903f1e02003-02-04 16:28:00 +00002867 Py_DECREF(item);
2868 goto Fail_1;
2869 }
2870 reslen = PyUnicode_GET_SIZE(item);
Georg Brandl99d7e4e2005-08-31 22:21:15 +00002871 if (reslen == 1)
Walter Dörwald903f1e02003-02-04 16:28:00 +00002872 PyUnicode_AS_UNICODE(result)[j++] =
2873 PyUnicode_AS_UNICODE(item)[0];
Jeremy Hylton1aad9c72003-09-16 03:10:59 +00002874 else {
Walter Dörwald903f1e02003-02-04 16:28:00 +00002875 /* do we need more space? */
Martin v. Löwisd96ee902006-02-16 14:37:16 +00002876 Py_ssize_t need = j + reslen + len - i - 1;
Gregory P. Smith9d534572008-06-11 07:41:16 +00002877
2878 /* check that didnt overflow */
2879 if ((j > PY_SSIZE_T_MAX - reslen) ||
2880 ((j + reslen) > PY_SSIZE_T_MAX - len) ||
2881 ((j + reslen + len) < i) ||
2882 ((j + reslen + len - i) <= 0)) {
2883 Py_DECREF(item);
2884 return NULL;
2885 }
2886
2887 assert(need >= 0);
2888 assert(outlen >= 0);
2889
Walter Dörwald903f1e02003-02-04 16:28:00 +00002890 if (need > outlen) {
Georg Brandl99d7e4e2005-08-31 22:21:15 +00002891 /* overallocate,
Jeremy Hylton1aad9c72003-09-16 03:10:59 +00002892 to avoid reallocations */
Gregory P. Smith9d534572008-06-11 07:41:16 +00002893 if (need < 2 * outlen) {
2894 if (outlen > PY_SSIZE_T_MAX / 2) {
2895 Py_DECREF(item);
2896 return NULL;
2897 } else {
2898 need = 2 * outlen;
2899 }
2900 }
2901
Jeremy Hylton364f6be2003-09-16 03:17:16 +00002902 if (PyUnicode_Resize(
2903 &result, need) < 0) {
Walter Dörwald903f1e02003-02-04 16:28:00 +00002904 Py_DECREF(item);
Walter Dörwald531e0002003-02-04 16:57:49 +00002905 goto Fail_1;
Walter Dörwald903f1e02003-02-04 16:28:00 +00002906 }
2907 outlen = need;
2908 }
Jeremy Hylton1aad9c72003-09-16 03:10:59 +00002909 memcpy(PyUnicode_AS_UNICODE(result) + j,
2910 PyUnicode_AS_UNICODE(item),
2911 reslen*sizeof(Py_UNICODE));
Walter Dörwald903f1e02003-02-04 16:28:00 +00002912 j += reslen;
2913 }
2914 }
Martin v. Löwis8afd7572003-01-25 22:46:11 +00002915 Py_DECREF(item);
2916 }
2917
Walter Dörwald903f1e02003-02-04 16:28:00 +00002918 if (j < outlen)
Martin v. Löwis8afd7572003-01-25 22:46:11 +00002919 PyUnicode_Resize(&result, j);
2920
2921 return result;
2922
2923Fail_1:
2924 Py_DECREF(result);
2925 return NULL;
2926}
2927#endif