blob: 02a2faae3cdaa8bafd7a582f752a9edbc3b273ee [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{
214 return PyNumber_ToBase(v, 2);
215}
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. */
250 if (PyString_Check(seq))
251 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;
Guido van Rossum79f25d91997-04-29 20:08:16 +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{
397 long x;
Guido van Rossum09095f32000-03-10 23:00:52 +0000398
399 if (!PyArg_ParseTuple(args, "l:unichr", &x))
400 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
Marc-André Lemburgd1ba4432000-09-19 21:04:18 +0000655 if (!PyString_Check(cmd) &&
656 !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
Neal Norwitz3a9a3e72005-11-27 20:38:31 +0000672 if (PyString_AsStringAndSize(cmd, &str, NULL)) {
673 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
817 if (!PyString_Check(name)) {
818 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
873 if (!PyString_Check(name)) {
874 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) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000880 PyErr_Clear();
Guido van Rossum09df08a1998-05-22 00:51:39 +0000881 Py_INCREF(Py_False);
882 return Py_False;
Guido van Rossum9bfef441993-03-29 10:43:31 +0000883 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000884 Py_DECREF(v);
Guido van Rossum09df08a1998-05-22 00:51:39 +0000885 Py_INCREF(Py_True);
886 return Py_True;
Guido van Rossum33894be1992-01-27 16:53:09 +0000887}
888
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000889PyDoc_STRVAR(hasattr_doc,
Guido van Rossum77f6a652002-04-03 22:41:51 +0000890"hasattr(object, name) -> bool\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000891\n\
892Return whether the object has an attribute with the given name.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000893(This is done by calling getattr(object, name) and catching exceptions.)");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000894
895
Guido van Rossum79f25d91997-04-29 20:08:16 +0000896static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000897builtin_id(PyObject *self, PyObject *v)
Guido van Rossum5b722181993-03-30 17:46:03 +0000898{
Guido van Rossum106f2da2000-06-28 21:12:25 +0000899 return PyLong_FromVoidPtr(v);
Guido van Rossum5b722181993-03-30 17:46:03 +0000900}
901
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000902PyDoc_STRVAR(id_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000903"id(object) -> integer\n\
904\n\
905Return the identity of an object. This is guaranteed to be unique among\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000906simultaneously existing objects. (Hint: it's the object's memory address.)");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000907
908
Guido van Rossum79f25d91997-04-29 20:08:16 +0000909static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000910builtin_map(PyObject *self, PyObject *args)
Guido van Rossum12d12c51993-10-26 17:58:25 +0000911{
912 typedef struct {
Tim Peters4e9afdc2001-05-03 23:54:49 +0000913 PyObject *it; /* the iterator object */
914 int saw_StopIteration; /* bool: did the iterator end? */
Guido van Rossum12d12c51993-10-26 17:58:25 +0000915 } sequence;
916
Guido van Rossum79f25d91997-04-29 20:08:16 +0000917 PyObject *func, *result;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000918 sequence *seqs = NULL, *sqp;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000919 Py_ssize_t n, len;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000920 register int i, j;
921
Guido van Rossum79f25d91997-04-29 20:08:16 +0000922 n = PyTuple_Size(args);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000923 if (n < 2) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000924 PyErr_SetString(PyExc_TypeError,
925 "map() requires at least two args");
Guido van Rossum12d12c51993-10-26 17:58:25 +0000926 return NULL;
927 }
928
Guido van Rossum79f25d91997-04-29 20:08:16 +0000929 func = PyTuple_GetItem(args, 0);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000930 n--;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000931
Neal Norwitz53152a12008-02-24 02:20:25 +0000932 if (func == Py_None) {
Benjamin Peterson9f4f4812008-04-27 03:01:45 +0000933 if (PyErr_WarnPy3k("map(None, ...) not supported in 3.x; "
Benjamin Petersonf19a7b92008-04-27 18:40:21 +0000934 "use list(...)", 1) < 0)
Neal Norwitz53152a12008-02-24 02:20:25 +0000935 return NULL;
936 if (n == 1) {
937 /* map(None, S) is the same as list(S). */
938 return PySequence_List(PyTuple_GetItem(args, 1));
939 }
Guido van Rossumfa4ac711998-07-10 17:37:30 +0000940 }
941
Tim Peters4e9afdc2001-05-03 23:54:49 +0000942 /* Get space for sequence descriptors. Must NULL out the iterator
943 * pointers so that jumping to Fail_2 later doesn't see trash.
944 */
Guido van Rossum79f25d91997-04-29 20:08:16 +0000945 if ((seqs = PyMem_NEW(sequence, n)) == NULL) {
946 PyErr_NoMemory();
Tim Peters4e9afdc2001-05-03 23:54:49 +0000947 return NULL;
948 }
949 for (i = 0; i < n; ++i) {
950 seqs[i].it = (PyObject*)NULL;
951 seqs[i].saw_StopIteration = 0;
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000952 }
Guido van Rossum12d12c51993-10-26 17:58:25 +0000953
Tim Peters4e9afdc2001-05-03 23:54:49 +0000954 /* Do a first pass to obtain iterators for the arguments, and set len
955 * to the largest of their lengths.
Tim Peters748b8bb2001-04-28 08:20:22 +0000956 */
Tim Peters4e9afdc2001-05-03 23:54:49 +0000957 len = 0;
958 for (i = 0, sqp = seqs; i < n; ++i, ++sqp) {
959 PyObject *curseq;
Martin v. Löwisd96ee902006-02-16 14:37:16 +0000960 Py_ssize_t curlen;
Guido van Rossumad991772001-01-12 16:03:05 +0000961
Tim Peters4e9afdc2001-05-03 23:54:49 +0000962 /* Get iterator. */
963 curseq = PyTuple_GetItem(args, i+1);
964 sqp->it = PyObject_GetIter(curseq);
965 if (sqp->it == NULL) {
Guido van Rossum12d12c51993-10-26 17:58:25 +0000966 static char errmsg[] =
Tim Peters4e9afdc2001-05-03 23:54:49 +0000967 "argument %d to map() must support iteration";
Guido van Rossum15e33a41997-04-30 19:00:27 +0000968 char errbuf[sizeof(errmsg) + 25];
Jeremy Hylton518ab1c2001-11-28 20:42:20 +0000969 PyOS_snprintf(errbuf, sizeof(errbuf), errmsg, i+2);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000970 PyErr_SetString(PyExc_TypeError, errbuf);
Guido van Rossum12d12c51993-10-26 17:58:25 +0000971 goto Fail_2;
972 }
973
Tim Peters4e9afdc2001-05-03 23:54:49 +0000974 /* Update len. */
Raymond Hettinger4e2f7142007-12-06 00:56:53 +0000975 curlen = _PyObject_LengthHint(curseq, 8);
Raymond Hettinger77f3c872004-01-04 08:54:44 +0000976 if (curlen > len)
977 len = curlen;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000978 }
979
Tim Peters4e9afdc2001-05-03 23:54:49 +0000980 /* Get space for the result list. */
Guido van Rossum79f25d91997-04-29 20:08:16 +0000981 if ((result = (PyObject *) PyList_New(len)) == NULL)
Guido van Rossum12d12c51993-10-26 17:58:25 +0000982 goto Fail_2;
983
Tim Peters4e9afdc2001-05-03 23:54:49 +0000984 /* Iterate over the sequences until all have stopped. */
Guido van Rossum2d951851994-08-29 12:52:16 +0000985 for (i = 0; ; ++i) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000986 PyObject *alist, *item=NULL, *value;
Tim Peters4e9afdc2001-05-03 23:54:49 +0000987 int numactive = 0;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000988
Guido van Rossum79f25d91997-04-29 20:08:16 +0000989 if (func == Py_None && n == 1)
Guido van Rossum32120311995-07-10 13:52:21 +0000990 alist = NULL;
Tim Peters4e9afdc2001-05-03 23:54:49 +0000991 else if ((alist = PyTuple_New(n)) == NULL)
992 goto Fail_1;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000993
994 for (j = 0, sqp = seqs; j < n; ++j, ++sqp) {
Tim Peters4e9afdc2001-05-03 23:54:49 +0000995 if (sqp->saw_StopIteration) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000996 Py_INCREF(Py_None);
997 item = Py_None;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000998 }
Guido van Rossum12d12c51993-10-26 17:58:25 +0000999 else {
Tim Peters4e9afdc2001-05-03 23:54:49 +00001000 item = PyIter_Next(sqp->it);
1001 if (item)
1002 ++numactive;
1003 else {
Tim Peters4e9afdc2001-05-03 23:54:49 +00001004 if (PyErr_Occurred()) {
Tim Petersf4848da2001-05-05 00:14:56 +00001005 Py_XDECREF(alist);
1006 goto Fail_1;
Guido van Rossum2d951851994-08-29 12:52:16 +00001007 }
Tim Peters4e9afdc2001-05-03 23:54:49 +00001008 Py_INCREF(Py_None);
1009 item = Py_None;
1010 sqp->saw_StopIteration = 1;
Guido van Rossum2d951851994-08-29 12:52:16 +00001011 }
Guido van Rossum12d12c51993-10-26 17:58:25 +00001012 }
Tim Peters4e9afdc2001-05-03 23:54:49 +00001013 if (alist)
1014 PyTuple_SET_ITEM(alist, j, item);
1015 else
Guido van Rossum2d951851994-08-29 12:52:16 +00001016 break;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001017 }
1018
Guido van Rossum32120311995-07-10 13:52:21 +00001019 if (!alist)
1020 alist = item;
Guido van Rossum2d951851994-08-29 12:52:16 +00001021
Tim Peters4e9afdc2001-05-03 23:54:49 +00001022 if (numactive == 0) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001023 Py_DECREF(alist);
Guido van Rossum2d951851994-08-29 12:52:16 +00001024 break;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001025 }
Guido van Rossum2d951851994-08-29 12:52:16 +00001026
Guido van Rossum79f25d91997-04-29 20:08:16 +00001027 if (func == Py_None)
Guido van Rossum32120311995-07-10 13:52:21 +00001028 value = alist;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001029 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001030 value = PyEval_CallObject(func, alist);
1031 Py_DECREF(alist);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00001032 if (value == NULL)
1033 goto Fail_1;
Guido van Rossum2d951851994-08-29 12:52:16 +00001034 }
1035 if (i >= len) {
Barry Warsawfa77e091999-01-28 18:49:12 +00001036 int status = PyList_Append(result, value);
Barry Warsaw21332871999-01-28 04:21:35 +00001037 Py_DECREF(value);
Barry Warsawfa77e091999-01-28 18:49:12 +00001038 if (status < 0)
1039 goto Fail_1;
Guido van Rossum2d951851994-08-29 12:52:16 +00001040 }
Tim Peters4e9afdc2001-05-03 23:54:49 +00001041 else if (PyList_SetItem(result, i, value) < 0)
1042 goto Fail_1;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001043 }
1044
Guido van Rossumfa4ac711998-07-10 17:37:30 +00001045 if (i < len && PyList_SetSlice(result, i, len, NULL) < 0)
1046 goto Fail_1;
1047
Tim Peters4e9afdc2001-05-03 23:54:49 +00001048 goto Succeed;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001049
Guido van Rossum12d12c51993-10-26 17:58:25 +00001050Fail_1:
Guido van Rossum79f25d91997-04-29 20:08:16 +00001051 Py_DECREF(result);
Guido van Rossum12d12c51993-10-26 17:58:25 +00001052Fail_2:
Tim Peters4e9afdc2001-05-03 23:54:49 +00001053 result = NULL;
1054Succeed:
1055 assert(seqs);
1056 for (i = 0; i < n; ++i)
1057 Py_XDECREF(seqs[i].it);
1058 PyMem_DEL(seqs);
1059 return result;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001060}
1061
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001062PyDoc_STRVAR(map_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001063"map(function, sequence[, sequence, ...]) -> list\n\
1064\n\
1065Return a list of the results of applying the function to the items of\n\
1066the argument sequence(s). If more than one sequence is given, the\n\
1067function is called with an argument list consisting of the corresponding\n\
1068item of each sequence, substituting None for missing values when not all\n\
1069sequences have the same length. If the function is None, return a list of\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001070the items of the sequence (or a list of tuples if more than one sequence).");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001071
1072
Guido van Rossum79f25d91997-04-29 20:08:16 +00001073static PyObject *
Georg Brandl28e08732008-04-30 19:47:09 +00001074builtin_next(PyObject *self, PyObject *args)
1075{
1076 PyObject *it, *res;
1077 PyObject *def = NULL;
1078
1079 if (!PyArg_UnpackTuple(args, "next", 1, 2, &it, &def))
1080 return NULL;
1081 if (!PyIter_Check(it)) {
1082 PyErr_Format(PyExc_TypeError,
1083 "%.200s object is not an iterator",
1084 it->ob_type->tp_name);
1085 return NULL;
1086 }
1087
1088 res = (*it->ob_type->tp_iternext)(it);
1089 if (res != NULL) {
1090 return res;
1091 } else if (def != NULL) {
1092 if (PyErr_Occurred()) {
1093 if (!PyErr_ExceptionMatches(PyExc_StopIteration))
1094 return NULL;
1095 PyErr_Clear();
1096 }
1097 Py_INCREF(def);
1098 return def;
1099 } else if (PyErr_Occurred()) {
1100 return NULL;
1101 } else {
1102 PyErr_SetNone(PyExc_StopIteration);
1103 return NULL;
1104 }
1105}
1106
1107PyDoc_STRVAR(next_doc,
1108"next(iterator[, default])\n\
1109\n\
1110Return the next item from the iterator. If default is given and the iterator\n\
1111is exhausted, it is returned instead of raising StopIteration.");
1112
1113
1114static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001115builtin_setattr(PyObject *self, PyObject *args)
Guido van Rossum33894be1992-01-27 16:53:09 +00001116{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001117 PyObject *v;
1118 PyObject *name;
1119 PyObject *value;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001120
Raymond Hettingerea3fdf42002-12-29 16:33:45 +00001121 if (!PyArg_UnpackTuple(args, "setattr", 3, 3, &v, &name, &value))
Guido van Rossum33894be1992-01-27 16:53:09 +00001122 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001123 if (PyObject_SetAttr(v, name, value) != 0)
Guido van Rossum33894be1992-01-27 16:53:09 +00001124 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001125 Py_INCREF(Py_None);
1126 return Py_None;
Guido van Rossum33894be1992-01-27 16:53:09 +00001127}
1128
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001129PyDoc_STRVAR(setattr_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001130"setattr(object, name, value)\n\
1131\n\
1132Set a named attribute on an object; setattr(x, 'y', v) is equivalent to\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001133``x.y = v''.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001134
1135
Guido van Rossum79f25d91997-04-29 20:08:16 +00001136static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001137builtin_delattr(PyObject *self, PyObject *args)
Guido van Rossum14144fc1994-08-29 12:53:40 +00001138{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001139 PyObject *v;
1140 PyObject *name;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001141
Raymond Hettingerea3fdf42002-12-29 16:33:45 +00001142 if (!PyArg_UnpackTuple(args, "delattr", 2, 2, &v, &name))
Guido van Rossum14144fc1994-08-29 12:53:40 +00001143 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001144 if (PyObject_SetAttr(v, name, (PyObject *)NULL) != 0)
Guido van Rossum14144fc1994-08-29 12:53:40 +00001145 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001146 Py_INCREF(Py_None);
1147 return Py_None;
Guido van Rossum14144fc1994-08-29 12:53:40 +00001148}
1149
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001150PyDoc_STRVAR(delattr_doc,
Guido van Rossumdf12a591998-11-23 22:13:04 +00001151"delattr(object, name)\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001152\n\
1153Delete a named attribute on an object; delattr(x, 'y') is equivalent to\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001154``del x.y''.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001155
1156
Guido van Rossum79f25d91997-04-29 20:08:16 +00001157static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001158builtin_hash(PyObject *self, PyObject *v)
Guido van Rossum9bfef441993-03-29 10:43:31 +00001159{
Guido van Rossum9bfef441993-03-29 10:43:31 +00001160 long x;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001161
Guido van Rossum79f25d91997-04-29 20:08:16 +00001162 x = PyObject_Hash(v);
Guido van Rossum9bfef441993-03-29 10:43:31 +00001163 if (x == -1)
1164 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001165 return PyInt_FromLong(x);
Guido van Rossum9bfef441993-03-29 10:43:31 +00001166}
1167
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001168PyDoc_STRVAR(hash_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001169"hash(object) -> integer\n\
1170\n\
1171Return a hash value for the object. Two objects with the same value have\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001172the same hash value. The reverse is not necessarily true, but likely.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001173
1174
Guido van Rossum79f25d91997-04-29 20:08:16 +00001175static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001176builtin_hex(PyObject *self, PyObject *v)
Guido van Rossum006bcd41991-10-24 14:54:44 +00001177{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001178 PyNumberMethods *nb;
Neil Schemenauer3a313e32004-07-19 16:29:17 +00001179 PyObject *res;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001180
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001181 if ((nb = v->ob_type->tp_as_number) == NULL ||
Guido van Rossum1899c2e1992-09-12 11:09:23 +00001182 nb->nb_hex == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001183 PyErr_SetString(PyExc_TypeError,
Guido van Rossum1899c2e1992-09-12 11:09:23 +00001184 "hex() argument can't be converted to hex");
1185 return NULL;
Guido van Rossum006bcd41991-10-24 14:54:44 +00001186 }
Neil Schemenauer3a313e32004-07-19 16:29:17 +00001187 res = (*nb->nb_hex)(v);
1188 if (res && !PyString_Check(res)) {
1189 PyErr_Format(PyExc_TypeError,
1190 "__hex__ returned non-string (type %.200s)",
1191 res->ob_type->tp_name);
1192 Py_DECREF(res);
1193 return NULL;
1194 }
1195 return res;
Guido van Rossum006bcd41991-10-24 14:54:44 +00001196}
1197
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001198PyDoc_STRVAR(hex_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001199"hex(number) -> string\n\
1200\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001201Return the hexadecimal representation of an integer or long integer.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001202
1203
Tim Petersdbd9ba62000-07-09 03:09:57 +00001204static PyObject *builtin_raw_input(PyObject *, PyObject *);
Guido van Rossum3165fe61992-09-25 21:59:05 +00001205
Guido van Rossum79f25d91997-04-29 20:08:16 +00001206static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001207builtin_input(PyObject *self, PyObject *args)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001208{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001209 PyObject *line;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001210 char *str;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001211 PyObject *res;
1212 PyObject *globals, *locals;
Hye-Shik Changff83c2b2004-02-02 13:39:01 +00001213 PyCompilerFlags cf;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001214
1215 line = builtin_raw_input(self, args);
Guido van Rossum3165fe61992-09-25 21:59:05 +00001216 if (line == NULL)
1217 return line;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001218 if (!PyArg_Parse(line, "s;embedded '\\0' in input line", &str))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001219 return NULL;
1220 while (*str == ' ' || *str == '\t')
1221 str++;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001222 globals = PyEval_GetGlobals();
1223 locals = PyEval_GetLocals();
1224 if (PyDict_GetItemString(globals, "__builtins__") == NULL) {
1225 if (PyDict_SetItemString(globals, "__builtins__",
1226 PyEval_GetBuiltins()) != 0)
Guido van Rossum6135a871995-01-09 17:53:26 +00001227 return NULL;
1228 }
Hye-Shik Changff83c2b2004-02-02 13:39:01 +00001229 cf.cf_flags = 0;
1230 PyEval_MergeCompilerFlags(&cf);
1231 res = PyRun_StringFlags(str, Py_eval_input, globals, locals, &cf);
Guido van Rossum79f25d91997-04-29 20:08:16 +00001232 Py_DECREF(line);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001233 return res;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001234}
1235
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001236PyDoc_STRVAR(input_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001237"input([prompt]) -> value\n\
1238\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001239Equivalent to eval(raw_input(prompt)).");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001240
1241
Guido van Rossume8811f81997-02-14 15:48:05 +00001242static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001243builtin_intern(PyObject *self, PyObject *args)
Guido van Rossume8811f81997-02-14 15:48:05 +00001244{
1245 PyObject *s;
Guido van Rossum43713e52000-02-29 13:59:29 +00001246 if (!PyArg_ParseTuple(args, "S:intern", &s))
Guido van Rossume8811f81997-02-14 15:48:05 +00001247 return NULL;
Jeremy Hylton4c989dd2004-08-07 19:20:05 +00001248 if (!PyString_CheckExact(s)) {
1249 PyErr_SetString(PyExc_TypeError,
1250 "can't intern subclass of string");
1251 return NULL;
1252 }
Guido van Rossume8811f81997-02-14 15:48:05 +00001253 Py_INCREF(s);
1254 PyString_InternInPlace(&s);
1255 return s;
1256}
1257
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001258PyDoc_STRVAR(intern_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001259"intern(string) -> string\n\
1260\n\
1261``Intern'' the given string. This enters the string in the (global)\n\
1262table of interned strings whose purpose is to speed up dictionary lookups.\n\
1263Return the string itself or the previously interned string object with the\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001264same value.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001265
1266
Guido van Rossum79f25d91997-04-29 20:08:16 +00001267static PyObject *
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001268builtin_iter(PyObject *self, PyObject *args)
1269{
1270 PyObject *v, *w = NULL;
1271
Raymond Hettingerea3fdf42002-12-29 16:33:45 +00001272 if (!PyArg_UnpackTuple(args, "iter", 1, 2, &v, &w))
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001273 return NULL;
1274 if (w == NULL)
1275 return PyObject_GetIter(v);
1276 if (!PyCallable_Check(v)) {
1277 PyErr_SetString(PyExc_TypeError,
1278 "iter(v, w): v must be callable");
1279 return NULL;
1280 }
1281 return PyCallIter_New(v, w);
1282}
1283
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001284PyDoc_STRVAR(iter_doc,
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001285"iter(collection) -> iterator\n\
1286iter(callable, sentinel) -> iterator\n\
1287\n\
1288Get an iterator from an object. In the first form, the argument must\n\
1289supply its own iterator, or be a sequence.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001290In the second form, the callable is called until it returns the sentinel.");
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001291
1292
1293static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001294builtin_len(PyObject *self, PyObject *v)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001295{
Martin v. Löwis18e16552006-02-15 17:27:45 +00001296 Py_ssize_t res;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001297
Jeremy Hylton03657cf2000-07-12 13:05:33 +00001298 res = PyObject_Size(v);
Guido van Rossum8ea9f4d1998-06-29 22:26:50 +00001299 if (res < 0 && PyErr_Occurred())
1300 return NULL;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001301 return PyInt_FromSsize_t(res);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001302}
1303
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001304PyDoc_STRVAR(len_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001305"len(object) -> integer\n\
1306\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001307Return the number of items of a sequence or mapping.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001308
1309
Guido van Rossum79f25d91997-04-29 20:08:16 +00001310static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001311builtin_locals(PyObject *self)
Guido van Rossum872537c1995-07-07 22:43:42 +00001312{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001313 PyObject *d;
Guido van Rossum872537c1995-07-07 22:43:42 +00001314
Guido van Rossum79f25d91997-04-29 20:08:16 +00001315 d = PyEval_GetLocals();
Neal Norwitz43bd4db2006-08-12 01:46:42 +00001316 Py_XINCREF(d);
Guido van Rossum872537c1995-07-07 22:43:42 +00001317 return d;
1318}
1319
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001320PyDoc_STRVAR(locals_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001321"locals() -> dictionary\n\
1322\n\
Raymond Hettinger69bf8f32003-01-04 02:16:22 +00001323Update and return a dictionary containing the current scope's local variables.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001324
1325
Guido van Rossum79f25d91997-04-29 20:08:16 +00001326static PyObject *
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001327min_max(PyObject *args, PyObject *kwds, int op)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001328{
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001329 PyObject *v, *it, *item, *val, *maxitem, *maxval, *keyfunc=NULL;
Martin v. Löwisfd39ad42004-08-12 14:42:37 +00001330 const char *name = op == Py_LT ? "min" : "max";
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001331
Guido van Rossum79f25d91997-04-29 20:08:16 +00001332 if (PyTuple_Size(args) > 1)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001333 v = args;
Martin v. Löwisfd39ad42004-08-12 14:42:37 +00001334 else if (!PyArg_UnpackTuple(args, (char *)name, 1, 1, &v))
Guido van Rossum3f5da241990-12-20 15:06:42 +00001335 return NULL;
Tim Peters67d687a2002-04-29 21:27:32 +00001336
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001337 if (kwds != NULL && PyDict_Check(kwds) && PyDict_Size(kwds)) {
1338 keyfunc = PyDict_GetItemString(kwds, "key");
1339 if (PyDict_Size(kwds)!=1 || keyfunc == NULL) {
Georg Brandl99d7e4e2005-08-31 22:21:15 +00001340 PyErr_Format(PyExc_TypeError,
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001341 "%s() got an unexpected keyword argument", name);
1342 return NULL;
1343 }
Guido van Rossum1d9a9ea2008-01-23 20:19:01 +00001344 Py_INCREF(keyfunc);
Georg Brandl99d7e4e2005-08-31 22:21:15 +00001345 }
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001346
Tim Petersc3074532001-05-03 07:00:32 +00001347 it = PyObject_GetIter(v);
Guido van Rossum1d9a9ea2008-01-23 20:19:01 +00001348 if (it == NULL) {
1349 Py_XDECREF(keyfunc);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001350 return NULL;
Guido van Rossum1d9a9ea2008-01-23 20:19:01 +00001351 }
Tim Petersc3074532001-05-03 07:00:32 +00001352
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001353 maxitem = NULL; /* the result */
1354 maxval = NULL; /* the value associated with the result */
Brett Cannon9e635cf2004-12-07 00:25:35 +00001355 while (( item = PyIter_Next(it) )) {
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001356 /* get the value from the key function */
1357 if (keyfunc != NULL) {
1358 val = PyObject_CallFunctionObjArgs(keyfunc, item, NULL);
1359 if (val == NULL)
1360 goto Fail_it_item;
1361 }
1362 /* no key function; the value is the item */
1363 else {
1364 val = item;
1365 Py_INCREF(val);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001366 }
Tim Petersc3074532001-05-03 07:00:32 +00001367
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001368 /* maximum value and item are unset; set them */
1369 if (maxval == NULL) {
1370 maxitem = item;
1371 maxval = val;
1372 }
1373 /* maximum value and item are set; update them as necessary */
Guido van Rossum2d951851994-08-29 12:52:16 +00001374 else {
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001375 int cmp = PyObject_RichCompareBool(val, maxval, op);
1376 if (cmp < 0)
1377 goto Fail_it_item_and_val;
1378 else if (cmp > 0) {
1379 Py_DECREF(maxval);
1380 Py_DECREF(maxitem);
1381 maxval = val;
1382 maxitem = item;
Guido van Rossum53451b32001-01-17 15:47:24 +00001383 }
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001384 else {
1385 Py_DECREF(item);
1386 Py_DECREF(val);
Guido van Rossumc8b6df91997-05-23 00:06:51 +00001387 }
Guido van Rossum2d951851994-08-29 12:52:16 +00001388 }
Guido van Rossum3f5da241990-12-20 15:06:42 +00001389 }
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001390 if (PyErr_Occurred())
1391 goto Fail_it;
1392 if (maxval == NULL) {
Martin v. Löwisfd39ad42004-08-12 14:42:37 +00001393 PyErr_Format(PyExc_ValueError,
1394 "%s() arg is an empty sequence", name);
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001395 assert(maxitem == NULL);
1396 }
1397 else
1398 Py_DECREF(maxval);
Tim Petersc3074532001-05-03 07:00:32 +00001399 Py_DECREF(it);
Guido van Rossum1d9a9ea2008-01-23 20:19:01 +00001400 Py_XDECREF(keyfunc);
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001401 return maxitem;
1402
1403Fail_it_item_and_val:
1404 Py_DECREF(val);
1405Fail_it_item:
1406 Py_DECREF(item);
1407Fail_it:
1408 Py_XDECREF(maxval);
1409 Py_XDECREF(maxitem);
1410 Py_DECREF(it);
Guido van Rossum1d9a9ea2008-01-23 20:19:01 +00001411 Py_XDECREF(keyfunc);
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001412 return NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001413}
1414
Guido van Rossum79f25d91997-04-29 20:08:16 +00001415static PyObject *
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001416builtin_min(PyObject *self, PyObject *args, PyObject *kwds)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001417{
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001418 return min_max(args, kwds, Py_LT);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001419}
1420
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001421PyDoc_STRVAR(min_doc,
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001422"min(iterable[, key=func]) -> value\n\
1423min(a, b, c, ...[, key=func]) -> value\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001424\n\
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001425With a single iterable argument, return its smallest item.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001426With two or more arguments, return the smallest argument.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001427
1428
Guido van Rossum79f25d91997-04-29 20:08:16 +00001429static PyObject *
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001430builtin_max(PyObject *self, PyObject *args, PyObject *kwds)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001431{
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001432 return min_max(args, kwds, Py_GT);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001433}
1434
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001435PyDoc_STRVAR(max_doc,
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001436"max(iterable[, key=func]) -> value\n\
1437max(a, b, c, ...[, key=func]) -> value\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001438\n\
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001439With a single iterable argument, return its largest item.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001440With two or more arguments, return the largest argument.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001441
1442
Guido van Rossum79f25d91997-04-29 20:08:16 +00001443static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001444builtin_oct(PyObject *self, PyObject *v)
Guido van Rossum006bcd41991-10-24 14:54:44 +00001445{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001446 PyNumberMethods *nb;
Neil Schemenauer3a313e32004-07-19 16:29:17 +00001447 PyObject *res;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001448
Guido van Rossum1899c2e1992-09-12 11:09:23 +00001449 if (v == NULL || (nb = v->ob_type->tp_as_number) == NULL ||
1450 nb->nb_oct == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001451 PyErr_SetString(PyExc_TypeError,
Guido van Rossum1899c2e1992-09-12 11:09:23 +00001452 "oct() argument can't be converted to oct");
1453 return NULL;
Guido van Rossum006bcd41991-10-24 14:54:44 +00001454 }
Neil Schemenauer3a313e32004-07-19 16:29:17 +00001455 res = (*nb->nb_oct)(v);
1456 if (res && !PyString_Check(res)) {
1457 PyErr_Format(PyExc_TypeError,
1458 "__oct__ returned non-string (type %.200s)",
1459 res->ob_type->tp_name);
1460 Py_DECREF(res);
1461 return NULL;
1462 }
1463 return res;
Guido van Rossum006bcd41991-10-24 14:54:44 +00001464}
1465
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001466PyDoc_STRVAR(oct_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001467"oct(number) -> string\n\
1468\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001469Return the octal representation of an integer or long integer.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001470
1471
Guido van Rossum79f25d91997-04-29 20:08:16 +00001472static PyObject *
Neal Norwitzc4edb0e2006-05-02 04:43:14 +00001473builtin_open(PyObject *self, PyObject *args, PyObject *kwds)
1474{
1475 return PyObject_Call((PyObject*)&PyFile_Type, args, kwds);
1476}
1477
1478PyDoc_STRVAR(open_doc,
1479"open(name[, mode[, buffering]]) -> file object\n\
1480\n\
Skip Montanaro4e3ebe02007-12-08 14:37:43 +00001481Open a file using the file() type, returns a file object. This is the\n\
1482preferred way to open a file.");
Neal Norwitzc4edb0e2006-05-02 04:43:14 +00001483
1484
1485static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001486builtin_ord(PyObject *self, PyObject* obj)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001487{
Guido van Rossum09095f32000-03-10 23:00:52 +00001488 long ord;
Martin v. Löwisd96ee902006-02-16 14:37:16 +00001489 Py_ssize_t size;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001490
Jeremy Hylton394b54d2000-04-12 21:19:47 +00001491 if (PyString_Check(obj)) {
1492 size = PyString_GET_SIZE(obj);
Andrew M. Kuchling34c20cf2000-12-20 14:36:56 +00001493 if (size == 1) {
Jeremy Hylton394b54d2000-04-12 21:19:47 +00001494 ord = (long)((unsigned char)*PyString_AS_STRING(obj));
Andrew M. Kuchling34c20cf2000-12-20 14:36:56 +00001495 return PyInt_FromLong(ord);
1496 }
Christian Heimes1a6387e2008-03-26 12:49:49 +00001497 } else if (PyBytes_Check(obj)) {
1498 size = PyBytes_GET_SIZE(obj);
1499 if (size == 1) {
1500 ord = (long)((unsigned char)*PyBytes_AS_STRING(obj));
1501 return PyInt_FromLong(ord);
1502 }
1503
Martin v. Löwis339d0f72001-08-17 18:39:25 +00001504#ifdef Py_USING_UNICODE
Jeremy Hylton394b54d2000-04-12 21:19:47 +00001505 } else if (PyUnicode_Check(obj)) {
1506 size = PyUnicode_GET_SIZE(obj);
Andrew M. Kuchling34c20cf2000-12-20 14:36:56 +00001507 if (size == 1) {
Jeremy Hylton394b54d2000-04-12 21:19:47 +00001508 ord = (long)*PyUnicode_AS_UNICODE(obj);
Andrew M. Kuchling34c20cf2000-12-20 14:36:56 +00001509 return PyInt_FromLong(ord);
1510 }
Martin v. Löwis339d0f72001-08-17 18:39:25 +00001511#endif
Jeremy Hylton394b54d2000-04-12 21:19:47 +00001512 } else {
1513 PyErr_Format(PyExc_TypeError,
Andrew M. Kuchlingf07aad12000-12-23 14:11:28 +00001514 "ord() expected string of length 1, but " \
Jeremy Hylton394b54d2000-04-12 21:19:47 +00001515 "%.200s found", obj->ob_type->tp_name);
Guido van Rossum09095f32000-03-10 23:00:52 +00001516 return NULL;
1517 }
1518
Guido van Rossumad991772001-01-12 16:03:05 +00001519 PyErr_Format(PyExc_TypeError,
Andrew M. Kuchlingf07aad12000-12-23 14:11:28 +00001520 "ord() expected a character, "
Martin v. Löwisd96ee902006-02-16 14:37:16 +00001521 "but string of length %zd found",
Jeremy Hylton394b54d2000-04-12 21:19:47 +00001522 size);
1523 return NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001524}
1525
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001526PyDoc_STRVAR(ord_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001527"ord(c) -> integer\n\
1528\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001529Return the integer ordinal of a one-character string.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001530
1531
Guido van Rossum79f25d91997-04-29 20:08:16 +00001532static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001533builtin_pow(PyObject *self, PyObject *args)
Guido van Rossum6a00cd81995-01-07 12:39:01 +00001534{
Guido van Rossum09df08a1998-05-22 00:51:39 +00001535 PyObject *v, *w, *z = Py_None;
Guido van Rossum6a00cd81995-01-07 12:39:01 +00001536
Raymond Hettingerea3fdf42002-12-29 16:33:45 +00001537 if (!PyArg_UnpackTuple(args, "pow", 2, 3, &v, &w, &z))
Guido van Rossum6a00cd81995-01-07 12:39:01 +00001538 return NULL;
Guido van Rossum09df08a1998-05-22 00:51:39 +00001539 return PyNumber_Power(v, w, z);
Guido van Rossumd4905451991-05-05 20:00:36 +00001540}
1541
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001542PyDoc_STRVAR(pow_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001543"pow(x, y[, z]) -> number\n\
1544\n\
1545With two arguments, equivalent to x**y. With three arguments,\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001546equivalent to (x**y) % z, but may be more efficient (e.g. for longs).");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001547
1548
Eric Smith7c478942008-03-18 23:45:49 +00001549static PyObject *
1550builtin_print(PyObject *self, PyObject *args, PyObject *kwds)
1551{
1552 static char *kwlist[] = {"sep", "end", "file", 0};
1553 static PyObject *dummy_args;
1554 PyObject *sep = NULL, *end = NULL, *file = NULL;
1555 int i, err;
1556
1557 if (dummy_args == NULL) {
1558 if (!(dummy_args = PyTuple_New(0)))
1559 return NULL;
1560 }
1561 if (!PyArg_ParseTupleAndKeywords(dummy_args, kwds, "|OOO:print",
1562 kwlist, &sep, &end, &file))
1563 return NULL;
1564 if (file == NULL || file == Py_None) {
1565 file = PySys_GetObject("stdout");
1566 /* sys.stdout may be None when FILE* stdout isn't connected */
1567 if (file == Py_None)
1568 Py_RETURN_NONE;
1569 }
1570
1571 if (sep && sep != Py_None && !PyString_Check(sep) &&
1572 !PyUnicode_Check(sep)) {
1573 PyErr_Format(PyExc_TypeError,
1574 "sep must be None, str or unicode, not %.200s",
1575 sep->ob_type->tp_name);
1576 return NULL;
1577 }
1578 if (end && end != Py_None && !PyString_Check(end) &&
1579 !PyUnicode_Check(end)) {
1580 PyErr_Format(PyExc_TypeError,
1581 "end must be None, str or unicode, not %.200s",
1582 end->ob_type->tp_name);
1583 return NULL;
1584 }
1585
1586 for (i = 0; i < PyTuple_Size(args); i++) {
1587 if (i > 0) {
1588 if (sep == NULL || sep == Py_None)
1589 err = PyFile_WriteString(" ", file);
1590 else
1591 err = PyFile_WriteObject(sep, file,
1592 Py_PRINT_RAW);
1593 if (err)
1594 return NULL;
1595 }
1596 err = PyFile_WriteObject(PyTuple_GetItem(args, i), file,
1597 Py_PRINT_RAW);
1598 if (err)
1599 return NULL;
1600 }
1601
1602 if (end == NULL || end == Py_None)
1603 err = PyFile_WriteString("\n", file);
1604 else
1605 err = PyFile_WriteObject(end, file, Py_PRINT_RAW);
1606 if (err)
1607 return NULL;
1608
1609 Py_RETURN_NONE;
1610}
1611
1612PyDoc_STRVAR(print_doc,
1613"print(value, ..., sep=' ', end='\\n', file=sys.stdout)\n\
1614\n\
1615Prints the values to a stream, or to sys.stdout by default.\n\
1616Optional keyword arguments:\n\
1617file: a file-like object (stream); defaults to the current sys.stdout.\n\
1618sep: string inserted between values, default a space.\n\
1619end: string appended after the last value, default a newline.");
1620
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001621
1622/* Return number of items in range (lo, hi, step), when arguments are
1623 * PyInt or PyLong objects. step > 0 required. Return a value < 0 if
1624 * & only if the true value is too large to fit in a signed long.
1625 * Arguments MUST return 1 with either PyInt_Check() or
1626 * PyLong_Check(). Return -1 when there is an error.
1627 */
1628static long
1629get_len_of_range_longs(PyObject *lo, PyObject *hi, PyObject *step)
1630{
1631 /* -------------------------------------------------------------
1632 Algorithm is equal to that of get_len_of_range(), but it operates
1633 on PyObjects (which are assumed to be PyLong or PyInt objects).
1634 ---------------------------------------------------------------*/
1635 long n;
1636 PyObject *diff = NULL;
1637 PyObject *one = NULL;
1638 PyObject *tmp1 = NULL, *tmp2 = NULL, *tmp3 = NULL;
1639 /* holds sub-expression evaluations */
1640
1641 /* if (lo >= hi), return length of 0. */
1642 if (PyObject_Compare(lo, hi) >= 0)
1643 return 0;
1644
1645 if ((one = PyLong_FromLong(1L)) == NULL)
1646 goto Fail;
1647
1648 if ((tmp1 = PyNumber_Subtract(hi, lo)) == NULL)
1649 goto Fail;
1650
1651 if ((diff = PyNumber_Subtract(tmp1, one)) == NULL)
1652 goto Fail;
1653
1654 if ((tmp2 = PyNumber_FloorDivide(diff, step)) == NULL)
1655 goto Fail;
1656
1657 if ((tmp3 = PyNumber_Add(tmp2, one)) == NULL)
1658 goto Fail;
1659
1660 n = PyLong_AsLong(tmp3);
1661 if (PyErr_Occurred()) { /* Check for Overflow */
1662 PyErr_Clear();
1663 goto Fail;
1664 }
1665
1666 Py_DECREF(tmp3);
1667 Py_DECREF(tmp2);
1668 Py_DECREF(diff);
1669 Py_DECREF(tmp1);
1670 Py_DECREF(one);
1671 return n;
1672
1673 Fail:
1674 Py_XDECREF(tmp3);
1675 Py_XDECREF(tmp2);
1676 Py_XDECREF(diff);
1677 Py_XDECREF(tmp1);
1678 Py_XDECREF(one);
1679 return -1;
1680}
1681
1682/* An extension of builtin_range() that handles the case when PyLong
1683 * arguments are given. */
1684static PyObject *
1685handle_range_longs(PyObject *self, PyObject *args)
1686{
1687 PyObject *ilow;
Tim Peters874e1f72003-04-13 22:13:08 +00001688 PyObject *ihigh = NULL;
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001689 PyObject *istep = NULL;
Tim Peters874e1f72003-04-13 22:13:08 +00001690
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001691 PyObject *curnum = NULL;
1692 PyObject *v = NULL;
1693 long bign;
1694 int i, n;
1695 int cmp_result;
1696
Tim Peters874e1f72003-04-13 22:13:08 +00001697 PyObject *zero = PyLong_FromLong(0);
1698
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001699 if (zero == NULL)
1700 return NULL;
1701
Tim Peters874e1f72003-04-13 22:13:08 +00001702 if (!PyArg_UnpackTuple(args, "range", 1, 3, &ilow, &ihigh, &istep)) {
1703 Py_DECREF(zero);
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001704 return NULL;
1705 }
1706
Tim Peters874e1f72003-04-13 22:13:08 +00001707 /* Figure out which way we were called, supply defaults, and be
1708 * sure to incref everything so that the decrefs at the end
1709 * are correct.
1710 */
1711 assert(ilow != NULL);
1712 if (ihigh == NULL) {
1713 /* only 1 arg -- it's the upper limit */
1714 ihigh = ilow;
1715 ilow = NULL;
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001716 }
Tim Peters874e1f72003-04-13 22:13:08 +00001717 assert(ihigh != NULL);
1718 Py_INCREF(ihigh);
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001719
Tim Peters874e1f72003-04-13 22:13:08 +00001720 /* ihigh correct now; do ilow */
1721 if (ilow == NULL)
1722 ilow = zero;
1723 Py_INCREF(ilow);
1724
1725 /* ilow and ihigh correct now; do istep */
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001726 if (istep == NULL) {
1727 istep = PyLong_FromLong(1L);
1728 if (istep == NULL)
1729 goto Fail;
1730 }
1731 else {
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001732 Py_INCREF(istep);
1733 }
1734
Tim Peters874e1f72003-04-13 22:13:08 +00001735 if (!PyInt_Check(ilow) && !PyLong_Check(ilow)) {
Guido van Rossum28e83e32003-04-15 12:43:26 +00001736 PyErr_Format(PyExc_TypeError,
Alex Martellif471d472003-04-23 13:00:44 +00001737 "range() integer start argument expected, got %s.",
Guido van Rossum817d6c92003-04-14 18:25:04 +00001738 ilow->ob_type->tp_name);
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001739 goto Fail;
1740 }
1741
Tim Peters874e1f72003-04-13 22:13:08 +00001742 if (!PyInt_Check(ihigh) && !PyLong_Check(ihigh)) {
Guido van Rossum28e83e32003-04-15 12:43:26 +00001743 PyErr_Format(PyExc_TypeError,
Alex Martellif471d472003-04-23 13:00:44 +00001744 "range() integer end argument expected, got %s.",
Guido van Rossum817d6c92003-04-14 18:25:04 +00001745 ihigh->ob_type->tp_name);
Tim Peters874e1f72003-04-13 22:13:08 +00001746 goto Fail;
1747 }
1748
1749 if (!PyInt_Check(istep) && !PyLong_Check(istep)) {
Guido van Rossum28e83e32003-04-15 12:43:26 +00001750 PyErr_Format(PyExc_TypeError,
Alex Martellif471d472003-04-23 13:00:44 +00001751 "range() integer step argument expected, got %s.",
Guido van Rossum817d6c92003-04-14 18:25:04 +00001752 istep->ob_type->tp_name);
Tim Peters874e1f72003-04-13 22:13:08 +00001753 goto Fail;
1754 }
1755
1756 if (PyObject_Cmp(istep, zero, &cmp_result) == -1)
1757 goto Fail;
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001758 if (cmp_result == 0) {
1759 PyErr_SetString(PyExc_ValueError,
Alex Martellif471d472003-04-23 13:00:44 +00001760 "range() step argument must not be zero");
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001761 goto Fail;
1762 }
1763
1764 if (cmp_result > 0)
1765 bign = get_len_of_range_longs(ilow, ihigh, istep);
1766 else {
1767 PyObject *neg_istep = PyNumber_Negative(istep);
1768 if (neg_istep == NULL)
1769 goto Fail;
1770 bign = get_len_of_range_longs(ihigh, ilow, neg_istep);
1771 Py_DECREF(neg_istep);
1772 }
1773
1774 n = (int)bign;
1775 if (bign < 0 || (long)n != bign) {
1776 PyErr_SetString(PyExc_OverflowError,
1777 "range() result has too many items");
1778 goto Fail;
1779 }
1780
1781 v = PyList_New(n);
1782 if (v == NULL)
1783 goto Fail;
1784
1785 curnum = ilow;
1786 Py_INCREF(curnum);
1787
1788 for (i = 0; i < n; i++) {
1789 PyObject *w = PyNumber_Long(curnum);
1790 PyObject *tmp_num;
1791 if (w == NULL)
1792 goto Fail;
1793
1794 PyList_SET_ITEM(v, i, w);
1795
1796 tmp_num = PyNumber_Add(curnum, istep);
1797 if (tmp_num == NULL)
1798 goto Fail;
1799
1800 Py_DECREF(curnum);
1801 curnum = tmp_num;
1802 }
Tim Peters874e1f72003-04-13 22:13:08 +00001803 Py_DECREF(ilow);
1804 Py_DECREF(ihigh);
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001805 Py_DECREF(istep);
1806 Py_DECREF(zero);
Tim Peters874e1f72003-04-13 22:13:08 +00001807 Py_DECREF(curnum);
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001808 return v;
1809
1810 Fail:
Tim Peters874e1f72003-04-13 22:13:08 +00001811 Py_DECREF(ilow);
1812 Py_DECREF(ihigh);
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001813 Py_XDECREF(istep);
Tim Peters874e1f72003-04-13 22:13:08 +00001814 Py_DECREF(zero);
1815 Py_XDECREF(curnum);
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001816 Py_XDECREF(v);
1817 return NULL;
1818}
1819
Guido van Rossum124eff01999-02-23 16:11:01 +00001820/* Return number of items in range/xrange (lo, hi, step). step > 0
1821 * required. Return a value < 0 if & only if the true value is too
1822 * large to fit in a signed long.
1823 */
1824static long
Thomas Wouterse28c2962000-07-23 22:21:32 +00001825get_len_of_range(long lo, long hi, long step)
Guido van Rossum124eff01999-02-23 16:11:01 +00001826{
1827 /* -------------------------------------------------------------
1828 If lo >= hi, the range is empty.
1829 Else if n values are in the range, the last one is
1830 lo + (n-1)*step, which must be <= hi-1. Rearranging,
1831 n <= (hi - lo - 1)/step + 1, so taking the floor of the RHS gives
1832 the proper value. Since lo < hi in this case, hi-lo-1 >= 0, so
1833 the RHS is non-negative and so truncation is the same as the
1834 floor. Letting M be the largest positive long, the worst case
1835 for the RHS numerator is hi=M, lo=-M-1, and then
1836 hi-lo-1 = M-(-M-1)-1 = 2*M. Therefore unsigned long has enough
1837 precision to compute the RHS exactly.
1838 ---------------------------------------------------------------*/
1839 long n = 0;
1840 if (lo < hi) {
1841 unsigned long uhi = (unsigned long)hi;
1842 unsigned long ulo = (unsigned long)lo;
1843 unsigned long diff = uhi - ulo - 1;
1844 n = (long)(diff / (unsigned long)step + 1);
1845 }
1846 return n;
1847}
1848
Guido van Rossum79f25d91997-04-29 20:08:16 +00001849static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001850builtin_range(PyObject *self, PyObject *args)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001851{
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001852 long ilow = 0, ihigh = 0, istep = 1;
Guido van Rossum124eff01999-02-23 16:11:01 +00001853 long bign;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001854 int i, n;
Guido van Rossum124eff01999-02-23 16:11:01 +00001855
Guido van Rossum79f25d91997-04-29 20:08:16 +00001856 PyObject *v;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001857
Guido van Rossum79f25d91997-04-29 20:08:16 +00001858 if (PyTuple_Size(args) <= 1) {
1859 if (!PyArg_ParseTuple(args,
Guido van Rossum0865dd91995-01-17 16:30:22 +00001860 "l;range() requires 1-3 int arguments",
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001861 &ihigh)) {
1862 PyErr_Clear();
1863 return handle_range_longs(self, args);
1864 }
Guido van Rossum3f5da241990-12-20 15:06:42 +00001865 }
1866 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001867 if (!PyArg_ParseTuple(args,
Guido van Rossum0865dd91995-01-17 16:30:22 +00001868 "ll|l;range() requires 1-3 int arguments",
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001869 &ilow, &ihigh, &istep)) {
1870 PyErr_Clear();
1871 return handle_range_longs(self, args);
1872 }
Guido van Rossum3f5da241990-12-20 15:06:42 +00001873 }
1874 if (istep == 0) {
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001875 PyErr_SetString(PyExc_ValueError,
Alex Martellif471d472003-04-23 13:00:44 +00001876 "range() step argument must not be zero");
Guido van Rossum3f5da241990-12-20 15:06:42 +00001877 return NULL;
1878 }
Guido van Rossum124eff01999-02-23 16:11:01 +00001879 if (istep > 0)
1880 bign = get_len_of_range(ilow, ihigh, istep);
1881 else
1882 bign = get_len_of_range(ihigh, ilow, -istep);
1883 n = (int)bign;
1884 if (bign < 0 || (long)n != bign) {
Guido van Rossume23cde21999-01-12 05:07:47 +00001885 PyErr_SetString(PyExc_OverflowError,
Fred Drake661ea262000-10-24 19:57:45 +00001886 "range() result has too many items");
Guido van Rossume23cde21999-01-12 05:07:47 +00001887 return NULL;
1888 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00001889 v = PyList_New(n);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001890 if (v == NULL)
1891 return NULL;
1892 for (i = 0; i < n; i++) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001893 PyObject *w = PyInt_FromLong(ilow);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001894 if (w == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001895 Py_DECREF(v);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001896 return NULL;
1897 }
Guido van Rossuma937d141998-04-24 18:22:02 +00001898 PyList_SET_ITEM(v, i, w);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001899 ilow += istep;
1900 }
1901 return v;
1902}
1903
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001904PyDoc_STRVAR(range_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001905"range([start,] stop[, step]) -> list of integers\n\
1906\n\
1907Return a list containing an arithmetic progression of integers.\n\
1908range(i, j) returns [i, i+1, i+2, ..., j-1]; start (!) defaults to 0.\n\
1909When step is given, it specifies the increment (or decrement).\n\
1910For example, range(4) returns [0, 1, 2, 3]. The end point is omitted!\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001911These are exactly the valid indices for a list of 4 elements.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001912
1913
Guido van Rossum79f25d91997-04-29 20:08:16 +00001914static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001915builtin_raw_input(PyObject *self, PyObject *args)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001916{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001917 PyObject *v = NULL;
Martin v. Löwis31d2df52002-08-14 15:46:02 +00001918 PyObject *fin = PySys_GetObject("stdin");
1919 PyObject *fout = PySys_GetObject("stdout");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001920
Raymond Hettingerea3fdf42002-12-29 16:33:45 +00001921 if (!PyArg_UnpackTuple(args, "[raw_]input", 0, 1, &v))
Guido van Rossum3165fe61992-09-25 21:59:05 +00001922 return NULL;
Martin v. Löwis31d2df52002-08-14 15:46:02 +00001923
1924 if (fin == NULL) {
Alex Martellia9b9c9f2003-04-23 13:34:35 +00001925 PyErr_SetString(PyExc_RuntimeError, "[raw_]input: lost sys.stdin");
Martin v. Löwis31d2df52002-08-14 15:46:02 +00001926 return NULL;
1927 }
1928 if (fout == NULL) {
Alex Martellia9b9c9f2003-04-23 13:34:35 +00001929 PyErr_SetString(PyExc_RuntimeError, "[raw_]input: lost sys.stdout");
Martin v. Löwis31d2df52002-08-14 15:46:02 +00001930 return NULL;
1931 }
1932 if (PyFile_SoftSpace(fout, 0)) {
1933 if (PyFile_WriteString(" ", fout) != 0)
1934 return NULL;
1935 }
Georg Brandl7e3ba2a2006-08-06 08:23:54 +00001936 if (PyFile_AsFile(fin) && PyFile_AsFile(fout)
Martin v. Löwis566f6af2002-10-26 14:39:10 +00001937 && isatty(fileno(PyFile_AsFile(fin)))
1938 && isatty(fileno(PyFile_AsFile(fout)))) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001939 PyObject *po;
Guido van Rossum872537c1995-07-07 22:43:42 +00001940 char *prompt;
1941 char *s;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001942 PyObject *result;
Guido van Rossum872537c1995-07-07 22:43:42 +00001943 if (v != NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001944 po = PyObject_Str(v);
Guido van Rossum872537c1995-07-07 22:43:42 +00001945 if (po == NULL)
1946 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001947 prompt = PyString_AsString(po);
Guido van Rossumd9b52081998-06-26 18:25:38 +00001948 if (prompt == NULL)
1949 return NULL;
Guido van Rossum872537c1995-07-07 22:43:42 +00001950 }
1951 else {
1952 po = NULL;
1953 prompt = "";
1954 }
Michael W. Hudson52db5192004-08-02 13:21:09 +00001955 s = PyOS_Readline(PyFile_AsFile(fin), PyFile_AsFile(fout),
Martin v. Löwis566f6af2002-10-26 14:39:10 +00001956 prompt);
Guido van Rossum79f25d91997-04-29 20:08:16 +00001957 Py_XDECREF(po);
Guido van Rossum872537c1995-07-07 22:43:42 +00001958 if (s == NULL) {
Michael W. Hudson30ea2f22004-07-07 17:44:12 +00001959 if (!PyErr_Occurred())
1960 PyErr_SetNone(PyExc_KeyboardInterrupt);
Guido van Rossum872537c1995-07-07 22:43:42 +00001961 return NULL;
1962 }
1963 if (*s == '\0') {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001964 PyErr_SetNone(PyExc_EOFError);
Guido van Rossum872537c1995-07-07 22:43:42 +00001965 result = NULL;
1966 }
1967 else { /* strip trailing '\n' */
Guido van Rossum106f2da2000-06-28 21:12:25 +00001968 size_t len = strlen(s);
Martin v. Löwisb1ed7fa2006-04-13 07:52:27 +00001969 if (len > PY_SSIZE_T_MAX) {
Martin v. Löwis31d2df52002-08-14 15:46:02 +00001970 PyErr_SetString(PyExc_OverflowError,
Alex Martellia9b9c9f2003-04-23 13:34:35 +00001971 "[raw_]input: input too long");
Guido van Rossum106f2da2000-06-28 21:12:25 +00001972 result = NULL;
1973 }
1974 else {
Martin v. Löwisb1ed7fa2006-04-13 07:52:27 +00001975 result = PyString_FromStringAndSize(s, len-1);
Guido van Rossum106f2da2000-06-28 21:12:25 +00001976 }
Guido van Rossum872537c1995-07-07 22:43:42 +00001977 }
Guido van Rossumb18618d2000-05-03 23:44:39 +00001978 PyMem_FREE(s);
Guido van Rossum872537c1995-07-07 22:43:42 +00001979 return result;
1980 }
Guido van Rossum90933611991-06-07 16:10:43 +00001981 if (v != NULL) {
Martin v. Löwis31d2df52002-08-14 15:46:02 +00001982 if (PyFile_WriteObject(v, fout, Py_PRINT_RAW) != 0)
Guido van Rossum90933611991-06-07 16:10:43 +00001983 return NULL;
1984 }
Martin v. Löwis31d2df52002-08-14 15:46:02 +00001985 return PyFile_GetLine(fin, -1);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001986}
1987
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001988PyDoc_STRVAR(raw_input_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001989"raw_input([prompt]) -> string\n\
1990\n\
1991Read a string from standard input. The trailing newline is stripped.\n\
1992If the user hits EOF (Unix: Ctl-D, Windows: Ctl-Z+Return), raise EOFError.\n\
1993On Unix, GNU readline is used if enabled. The prompt string, if given,\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001994is printed without a trailing newline before reading.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001995
1996
Guido van Rossum79f25d91997-04-29 20:08:16 +00001997static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001998builtin_reduce(PyObject *self, PyObject *args)
Guido van Rossum12d12c51993-10-26 17:58:25 +00001999{
Tim Peters15d81ef2001-05-04 04:39:21 +00002000 PyObject *seq, *func, *result = NULL, *it;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002001
Benjamin Peterson9f4f4812008-04-27 03:01:45 +00002002 if (PyErr_WarnPy3k("reduce() not supported in 3.x; "
Benjamin Petersonf19a7b92008-04-27 18:40:21 +00002003 "use functools.reduce()", 1) < 0)
Neal Norwitzdf25efe2007-05-23 06:58:36 +00002004 return NULL;
2005
Raymond Hettingerea3fdf42002-12-29 16:33:45 +00002006 if (!PyArg_UnpackTuple(args, "reduce", 2, 3, &func, &seq, &result))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002007 return NULL;
2008 if (result != NULL)
Guido van Rossum79f25d91997-04-29 20:08:16 +00002009 Py_INCREF(result);
Guido van Rossum12d12c51993-10-26 17:58:25 +00002010
Tim Peters15d81ef2001-05-04 04:39:21 +00002011 it = PyObject_GetIter(seq);
2012 if (it == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002013 PyErr_SetString(PyExc_TypeError,
Tim Peters15d81ef2001-05-04 04:39:21 +00002014 "reduce() arg 2 must support iteration");
2015 Py_XDECREF(result);
Guido van Rossum12d12c51993-10-26 17:58:25 +00002016 return NULL;
2017 }
2018
Guido van Rossum79f25d91997-04-29 20:08:16 +00002019 if ((args = PyTuple_New(2)) == NULL)
Guido van Rossum2d951851994-08-29 12:52:16 +00002020 goto Fail;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002021
Tim Peters15d81ef2001-05-04 04:39:21 +00002022 for (;;) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002023 PyObject *op2;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002024
2025 if (args->ob_refcnt > 1) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002026 Py_DECREF(args);
2027 if ((args = PyTuple_New(2)) == NULL)
Guido van Rossum2d951851994-08-29 12:52:16 +00002028 goto Fail;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002029 }
2030
Tim Peters15d81ef2001-05-04 04:39:21 +00002031 op2 = PyIter_Next(it);
2032 if (op2 == NULL) {
Tim Petersf4848da2001-05-05 00:14:56 +00002033 if (PyErr_Occurred())
2034 goto Fail;
2035 break;
Guido van Rossum2d951851994-08-29 12:52:16 +00002036 }
Guido van Rossum12d12c51993-10-26 17:58:25 +00002037
Guido van Rossum2d951851994-08-29 12:52:16 +00002038 if (result == NULL)
2039 result = op2;
2040 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002041 PyTuple_SetItem(args, 0, result);
2042 PyTuple_SetItem(args, 1, op2);
2043 if ((result = PyEval_CallObject(func, args)) == NULL)
Guido van Rossum2d951851994-08-29 12:52:16 +00002044 goto Fail;
2045 }
Guido van Rossum12d12c51993-10-26 17:58:25 +00002046 }
2047
Guido van Rossum79f25d91997-04-29 20:08:16 +00002048 Py_DECREF(args);
Guido van Rossum12d12c51993-10-26 17:58:25 +00002049
Guido van Rossum2d951851994-08-29 12:52:16 +00002050 if (result == NULL)
Guido van Rossum79f25d91997-04-29 20:08:16 +00002051 PyErr_SetString(PyExc_TypeError,
Fred Drake661ea262000-10-24 19:57:45 +00002052 "reduce() of empty sequence with no initial value");
Guido van Rossum2d951851994-08-29 12:52:16 +00002053
Tim Peters15d81ef2001-05-04 04:39:21 +00002054 Py_DECREF(it);
Guido van Rossum12d12c51993-10-26 17:58:25 +00002055 return result;
2056
Guido van Rossum2d951851994-08-29 12:52:16 +00002057Fail:
Guido van Rossum79f25d91997-04-29 20:08:16 +00002058 Py_XDECREF(args);
2059 Py_XDECREF(result);
Tim Peters15d81ef2001-05-04 04:39:21 +00002060 Py_DECREF(it);
Guido van Rossum12d12c51993-10-26 17:58:25 +00002061 return NULL;
2062}
2063
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002064PyDoc_STRVAR(reduce_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002065"reduce(function, sequence[, initial]) -> value\n\
2066\n\
2067Apply a function of two arguments cumulatively to the items of a sequence,\n\
2068from left to right, so as to reduce the sequence to a single value.\n\
2069For example, reduce(lambda x, y: x+y, [1, 2, 3, 4, 5]) calculates\n\
2070((((1+2)+3)+4)+5). If initial is present, it is placed before the items\n\
2071of the sequence in the calculation, and serves as a default when the\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002072sequence is empty.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002073
2074
Guido van Rossum79f25d91997-04-29 20:08:16 +00002075static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002076builtin_reload(PyObject *self, PyObject *v)
Guido van Rossum3f5da241990-12-20 15:06:42 +00002077{
Benjamin Peterson9f4f4812008-04-27 03:01:45 +00002078 if (PyErr_WarnPy3k("In 3.x, reload() is renamed to imp.reload()",
Benjamin Petersonf19a7b92008-04-27 18:40:21 +00002079 1) < 0)
Neal Norwitzdf25efe2007-05-23 06:58:36 +00002080 return NULL;
2081
Guido van Rossum79f25d91997-04-29 20:08:16 +00002082 return PyImport_ReloadModule(v);
Guido van Rossum3f5da241990-12-20 15:06:42 +00002083}
2084
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002085PyDoc_STRVAR(reload_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002086"reload(module) -> module\n\
2087\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002088Reload the module. The module must have been successfully imported before.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002089
2090
Guido van Rossum79f25d91997-04-29 20:08:16 +00002091static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002092builtin_repr(PyObject *self, PyObject *v)
Guido van Rossumc89705d1992-11-26 08:54:07 +00002093{
Guido van Rossum79f25d91997-04-29 20:08:16 +00002094 return PyObject_Repr(v);
Guido van Rossumc89705d1992-11-26 08:54:07 +00002095}
2096
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002097PyDoc_STRVAR(repr_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002098"repr(object) -> string\n\
2099\n\
2100Return the canonical string representation of the object.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002101For most object types, eval(repr(object)) == object.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002102
2103
Guido van Rossum79f25d91997-04-29 20:08:16 +00002104static PyObject *
Georg Brandlccadf842006-03-31 18:54:53 +00002105builtin_round(PyObject *self, PyObject *args, PyObject *kwds)
Guido van Rossum9e51f9b1993-02-12 16:29:05 +00002106{
Jeffrey Yasskin9871d8f2008-01-05 08:47:13 +00002107 double number;
2108 double f;
2109 int ndigits = 0;
2110 int i;
Georg Brandlccadf842006-03-31 18:54:53 +00002111 static char *kwlist[] = {"number", "ndigits", 0};
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002112
Jeffrey Yasskin9871d8f2008-01-05 08:47:13 +00002113 if (!PyArg_ParseTupleAndKeywords(args, kwds, "d|i:round",
2114 kwlist, &number, &ndigits))
2115 return NULL;
2116 f = 1.0;
2117 i = abs(ndigits);
2118 while (--i >= 0)
2119 f = f*10.0;
2120 if (ndigits < 0)
2121 number /= f;
2122 else
2123 number *= f;
2124 if (number >= 0.0)
2125 number = floor(number + 0.5);
2126 else
2127 number = ceil(number - 0.5);
2128 if (ndigits < 0)
2129 number *= f;
2130 else
2131 number /= f;
2132 return PyFloat_FromDouble(number);
Guido van Rossum9e51f9b1993-02-12 16:29:05 +00002133}
2134
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002135PyDoc_STRVAR(round_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002136"round(number[, ndigits]) -> floating point number\n\
2137\n\
2138Round a number to a given precision in decimal digits (default 0 digits).\n\
Jeffrey Yasskin9871d8f2008-01-05 08:47:13 +00002139This always returns a floating point number. Precision may be negative.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002140
Raymond Hettinger64958a12003-12-17 20:43:33 +00002141static PyObject *
2142builtin_sorted(PyObject *self, PyObject *args, PyObject *kwds)
2143{
2144 PyObject *newlist, *v, *seq, *compare=NULL, *keyfunc=NULL, *newargs;
2145 PyObject *callable;
Martin v. Löwis15e62742006-02-27 16:46:16 +00002146 static char *kwlist[] = {"iterable", "cmp", "key", "reverse", 0};
Neal Norwitz6f0d4792005-12-15 06:40:36 +00002147 int reverse;
Raymond Hettinger64958a12003-12-17 20:43:33 +00002148
Neal Norwitz6f0d4792005-12-15 06:40:36 +00002149 /* args 1-4 should match listsort in Objects/listobject.c */
Armin Rigo71d7e702005-09-20 18:13:03 +00002150 if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|OOi:sorted",
2151 kwlist, &seq, &compare, &keyfunc, &reverse))
2152 return NULL;
Raymond Hettinger64958a12003-12-17 20:43:33 +00002153
2154 newlist = PySequence_List(seq);
2155 if (newlist == NULL)
2156 return NULL;
2157
2158 callable = PyObject_GetAttrString(newlist, "sort");
2159 if (callable == NULL) {
2160 Py_DECREF(newlist);
2161 return NULL;
2162 }
Georg Brandl99d7e4e2005-08-31 22:21:15 +00002163
Raymond Hettinger64958a12003-12-17 20:43:33 +00002164 newargs = PyTuple_GetSlice(args, 1, 4);
2165 if (newargs == NULL) {
2166 Py_DECREF(newlist);
2167 Py_DECREF(callable);
2168 return NULL;
2169 }
2170
2171 v = PyObject_Call(callable, newargs, kwds);
2172 Py_DECREF(newargs);
2173 Py_DECREF(callable);
2174 if (v == NULL) {
2175 Py_DECREF(newlist);
2176 return NULL;
2177 }
2178 Py_DECREF(v);
2179 return newlist;
2180}
2181
2182PyDoc_STRVAR(sorted_doc,
2183"sorted(iterable, cmp=None, key=None, reverse=False) --> new sorted list");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002184
Guido van Rossum79f25d91997-04-29 20:08:16 +00002185static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002186builtin_vars(PyObject *self, PyObject *args)
Guido van Rossum2d951851994-08-29 12:52:16 +00002187{
Guido van Rossum79f25d91997-04-29 20:08:16 +00002188 PyObject *v = NULL;
2189 PyObject *d;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002190
Raymond Hettingerea3fdf42002-12-29 16:33:45 +00002191 if (!PyArg_UnpackTuple(args, "vars", 0, 1, &v))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002192 return NULL;
Guido van Rossum2d951851994-08-29 12:52:16 +00002193 if (v == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002194 d = PyEval_GetLocals();
Guido van Rossum53bb7ff1995-07-26 16:26:31 +00002195 if (d == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002196 if (!PyErr_Occurred())
2197 PyErr_SetString(PyExc_SystemError,
Alex Martellia9b9c9f2003-04-23 13:34:35 +00002198 "vars(): no locals!?");
Guido van Rossum53bb7ff1995-07-26 16:26:31 +00002199 }
2200 else
Guido van Rossum79f25d91997-04-29 20:08:16 +00002201 Py_INCREF(d);
Guido van Rossum2d951851994-08-29 12:52:16 +00002202 }
2203 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002204 d = PyObject_GetAttrString(v, "__dict__");
Guido van Rossum2d951851994-08-29 12:52:16 +00002205 if (d == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002206 PyErr_SetString(PyExc_TypeError,
Guido van Rossum2d951851994-08-29 12:52:16 +00002207 "vars() argument must have __dict__ attribute");
2208 return NULL;
2209 }
2210 }
2211 return d;
2212}
2213
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002214PyDoc_STRVAR(vars_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002215"vars([object]) -> dictionary\n\
2216\n\
2217Without arguments, equivalent to locals().\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002218With an argument, equivalent to object.__dict__.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002219
Alex Martellia70b1912003-04-22 08:12:33 +00002220
2221static PyObject*
2222builtin_sum(PyObject *self, PyObject *args)
2223{
2224 PyObject *seq;
2225 PyObject *result = NULL;
2226 PyObject *temp, *item, *iter;
2227
Raymond Hettinger5cf63942003-10-25 06:41:37 +00002228 if (!PyArg_UnpackTuple(args, "sum", 1, 2, &seq, &result))
Alex Martellia70b1912003-04-22 08:12:33 +00002229 return NULL;
2230
2231 iter = PyObject_GetIter(seq);
2232 if (iter == NULL)
2233 return NULL;
2234
2235 if (result == NULL) {
2236 result = PyInt_FromLong(0);
2237 if (result == NULL) {
2238 Py_DECREF(iter);
2239 return NULL;
2240 }
2241 } else {
2242 /* reject string values for 'start' parameter */
2243 if (PyObject_TypeCheck(result, &PyBaseString_Type)) {
2244 PyErr_SetString(PyExc_TypeError,
Alex Martellia9b9c9f2003-04-23 13:34:35 +00002245 "sum() can't sum strings [use ''.join(seq) instead]");
Alex Martellia70b1912003-04-22 08:12:33 +00002246 Py_DECREF(iter);
2247 return NULL;
2248 }
Alex Martelli41c9f882003-04-22 09:24:48 +00002249 Py_INCREF(result);
Alex Martellia70b1912003-04-22 08:12:33 +00002250 }
2251
Raymond Hettinger3f8caa32007-10-24 01:28:33 +00002252#ifndef SLOW_SUM
2253 /* Fast addition by keeping temporary sums in C instead of new Python objects.
2254 Assumes all inputs are the same type. If the assumption fails, default
2255 to the more general routine.
2256 */
2257 if (PyInt_CheckExact(result)) {
2258 long i_result = PyInt_AS_LONG(result);
2259 Py_DECREF(result);
2260 result = NULL;
2261 while(result == NULL) {
2262 item = PyIter_Next(iter);
2263 if (item == NULL) {
2264 Py_DECREF(iter);
2265 if (PyErr_Occurred())
2266 return NULL;
2267 return PyInt_FromLong(i_result);
2268 }
2269 if (PyInt_CheckExact(item)) {
2270 long b = PyInt_AS_LONG(item);
2271 long x = i_result + b;
2272 if ((x^i_result) >= 0 || (x^b) >= 0) {
2273 i_result = x;
2274 Py_DECREF(item);
2275 continue;
2276 }
2277 }
2278 /* Either overflowed or is not an int. Restore real objects and process normally */
2279 result = PyInt_FromLong(i_result);
2280 temp = PyNumber_Add(result, item);
2281 Py_DECREF(result);
2282 Py_DECREF(item);
2283 result = temp;
2284 if (result == NULL) {
2285 Py_DECREF(iter);
2286 return NULL;
2287 }
2288 }
2289 }
2290
2291 if (PyFloat_CheckExact(result)) {
2292 double f_result = PyFloat_AS_DOUBLE(result);
2293 Py_DECREF(result);
2294 result = NULL;
2295 while(result == NULL) {
2296 item = PyIter_Next(iter);
2297 if (item == NULL) {
2298 Py_DECREF(iter);
2299 if (PyErr_Occurred())
2300 return NULL;
2301 return PyFloat_FromDouble(f_result);
2302 }
2303 if (PyFloat_CheckExact(item)) {
2304 PyFPE_START_PROTECT("add", return 0)
2305 f_result += PyFloat_AS_DOUBLE(item);
Raymond Hettinger3a8daf52007-10-24 02:05:51 +00002306 PyFPE_END_PROTECT(f_result)
Raymond Hettingera45c4872007-10-25 02:26:58 +00002307 Py_DECREF(item);
Raymond Hettinger3a8daf52007-10-24 02:05:51 +00002308 continue;
2309 }
2310 if (PyInt_CheckExact(item)) {
2311 PyFPE_START_PROTECT("add", return 0)
2312 f_result += (double)PyInt_AS_LONG(item);
2313 PyFPE_END_PROTECT(f_result)
Raymond Hettingera45c4872007-10-25 02:26:58 +00002314 Py_DECREF(item);
Raymond Hettinger3f8caa32007-10-24 01:28:33 +00002315 continue;
2316 }
2317 result = PyFloat_FromDouble(f_result);
2318 temp = PyNumber_Add(result, item);
2319 Py_DECREF(result);
2320 Py_DECREF(item);
2321 result = temp;
2322 if (result == NULL) {
2323 Py_DECREF(iter);
2324 return NULL;
2325 }
2326 }
2327 }
2328#endif
2329
Alex Martellia70b1912003-04-22 08:12:33 +00002330 for(;;) {
2331 item = PyIter_Next(iter);
2332 if (item == NULL) {
2333 /* error, or end-of-sequence */
2334 if (PyErr_Occurred()) {
2335 Py_DECREF(result);
2336 result = NULL;
2337 }
2338 break;
2339 }
Alex Martellia253e182003-10-25 23:24:14 +00002340 temp = PyNumber_Add(result, item);
Alex Martellia70b1912003-04-22 08:12:33 +00002341 Py_DECREF(result);
2342 Py_DECREF(item);
2343 result = temp;
2344 if (result == NULL)
2345 break;
2346 }
2347 Py_DECREF(iter);
2348 return result;
2349}
2350
2351PyDoc_STRVAR(sum_doc,
Georg Brandl8134d062006-10-12 12:33:07 +00002352"sum(sequence[, start]) -> value\n\
Alex Martellia70b1912003-04-22 08:12:33 +00002353\n\
2354Returns the sum of a sequence of numbers (NOT strings) plus the value\n\
Georg Brandl8134d062006-10-12 12:33:07 +00002355of parameter 'start' (which defaults to 0). When the sequence is\n\
2356empty, returns start.");
Alex Martellia70b1912003-04-22 08:12:33 +00002357
2358
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002359static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002360builtin_isinstance(PyObject *self, PyObject *args)
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002361{
2362 PyObject *inst;
2363 PyObject *cls;
Guido van Rossum823649d2001-03-21 18:40:58 +00002364 int retval;
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002365
Raymond Hettingerea3fdf42002-12-29 16:33:45 +00002366 if (!PyArg_UnpackTuple(args, "isinstance", 2, 2, &inst, &cls))
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002367 return NULL;
Guido van Rossumf5dd9141997-12-02 19:11:45 +00002368
Guido van Rossum823649d2001-03-21 18:40:58 +00002369 retval = PyObject_IsInstance(inst, cls);
2370 if (retval < 0)
Guido van Rossumad991772001-01-12 16:03:05 +00002371 return NULL;
Guido van Rossum77f6a652002-04-03 22:41:51 +00002372 return PyBool_FromLong(retval);
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002373}
2374
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002375PyDoc_STRVAR(isinstance_doc,
Guido van Rossum77f6a652002-04-03 22:41:51 +00002376"isinstance(object, class-or-type-or-tuple) -> bool\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002377\n\
2378Return whether an object is an instance of a class or of a subclass thereof.\n\
Guido van Rossum03290ec2001-10-07 20:54:12 +00002379With a type as second argument, return whether that is the object's type.\n\
2380The form using a tuple, isinstance(x, (A, B, ...)), is a shortcut for\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002381isinstance(x, A) or isinstance(x, B) or ... (etc.).");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002382
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002383
2384static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002385builtin_issubclass(PyObject *self, PyObject *args)
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002386{
2387 PyObject *derived;
2388 PyObject *cls;
2389 int retval;
2390
Raymond Hettingerea3fdf42002-12-29 16:33:45 +00002391 if (!PyArg_UnpackTuple(args, "issubclass", 2, 2, &derived, &cls))
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002392 return NULL;
Guido van Rossum668213d1999-06-16 17:28:37 +00002393
Guido van Rossum823649d2001-03-21 18:40:58 +00002394 retval = PyObject_IsSubclass(derived, cls);
2395 if (retval < 0)
2396 return NULL;
Guido van Rossum77f6a652002-04-03 22:41:51 +00002397 return PyBool_FromLong(retval);
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002398}
2399
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002400PyDoc_STRVAR(issubclass_doc,
Guido van Rossum77f6a652002-04-03 22:41:51 +00002401"issubclass(C, B) -> bool\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002402\n\
Walter Dörwaldd9a6ad32002-12-12 16:41:44 +00002403Return whether class C is a subclass (i.e., a derived class) of class B.\n\
2404When using a tuple as the second argument issubclass(X, (A, B, ...)),\n\
2405is a shortcut for issubclass(X, A) or issubclass(X, B) or ... (etc.).");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002406
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002407
Barry Warsawbd599b52000-08-03 15:45:29 +00002408static PyObject*
2409builtin_zip(PyObject *self, PyObject *args)
2410{
2411 PyObject *ret;
Martin v. Löwisd96ee902006-02-16 14:37:16 +00002412 const Py_ssize_t itemsize = PySequence_Length(args);
2413 Py_ssize_t i;
Tim Peters8572b4f2001-05-06 01:05:02 +00002414 PyObject *itlist; /* tuple of iterators */
Martin v. Löwisd96ee902006-02-16 14:37:16 +00002415 Py_ssize_t len; /* guess at result length */
Barry Warsawbd599b52000-08-03 15:45:29 +00002416
Raymond Hettingereaef6152003-08-02 07:42:57 +00002417 if (itemsize == 0)
2418 return PyList_New(0);
2419
Barry Warsawbd599b52000-08-03 15:45:29 +00002420 /* args must be a tuple */
2421 assert(PyTuple_Check(args));
2422
Tim Peters39a86c22002-05-12 07:19:38 +00002423 /* Guess at result length: the shortest of the input lengths.
2424 If some argument refuses to say, we refuse to guess too, lest
2425 an argument like xrange(sys.maxint) lead us astray.*/
Tim Peters67d687a2002-04-29 21:27:32 +00002426 len = -1; /* unknown */
2427 for (i = 0; i < itemsize; ++i) {
2428 PyObject *item = PyTuple_GET_ITEM(args, i);
Raymond Hettinger4e2f7142007-12-06 00:56:53 +00002429 Py_ssize_t thislen = _PyObject_LengthHint(item, -1);
Tim Peters39a86c22002-05-12 07:19:38 +00002430 if (thislen < 0) {
Tim Peters39a86c22002-05-12 07:19:38 +00002431 len = -1;
2432 break;
2433 }
Tim Peters67d687a2002-04-29 21:27:32 +00002434 else if (len < 0 || thislen < len)
2435 len = thislen;
2436 }
2437
Tim Peters8572b4f2001-05-06 01:05:02 +00002438 /* allocate result list */
Tim Peters67d687a2002-04-29 21:27:32 +00002439 if (len < 0)
2440 len = 10; /* arbitrary */
2441 if ((ret = PyList_New(len)) == NULL)
Barry Warsawbd599b52000-08-03 15:45:29 +00002442 return NULL;
2443
Tim Peters8572b4f2001-05-06 01:05:02 +00002444 /* obtain iterators */
2445 itlist = PyTuple_New(itemsize);
2446 if (itlist == NULL)
2447 goto Fail_ret;
2448 for (i = 0; i < itemsize; ++i) {
2449 PyObject *item = PyTuple_GET_ITEM(args, i);
2450 PyObject *it = PyObject_GetIter(item);
2451 if (it == NULL) {
2452 if (PyErr_ExceptionMatches(PyExc_TypeError))
2453 PyErr_Format(PyExc_TypeError,
Neal Norwitza361bd82006-02-19 19:31:50 +00002454 "zip argument #%zd must support iteration",
Tim Peters8572b4f2001-05-06 01:05:02 +00002455 i+1);
2456 goto Fail_ret_itlist;
Barry Warsawbd599b52000-08-03 15:45:29 +00002457 }
Tim Peters8572b4f2001-05-06 01:05:02 +00002458 PyTuple_SET_ITEM(itlist, i, it);
2459 }
Barry Warsawbd599b52000-08-03 15:45:29 +00002460
Tim Peters8572b4f2001-05-06 01:05:02 +00002461 /* build result into ret list */
Tim Peters67d687a2002-04-29 21:27:32 +00002462 for (i = 0; ; ++i) {
2463 int j;
Tim Peters8572b4f2001-05-06 01:05:02 +00002464 PyObject *next = PyTuple_New(itemsize);
2465 if (!next)
2466 goto Fail_ret_itlist;
2467
Tim Peters67d687a2002-04-29 21:27:32 +00002468 for (j = 0; j < itemsize; j++) {
2469 PyObject *it = PyTuple_GET_ITEM(itlist, j);
Tim Peters8572b4f2001-05-06 01:05:02 +00002470 PyObject *item = PyIter_Next(it);
Barry Warsawbd599b52000-08-03 15:45:29 +00002471 if (!item) {
Tim Peters8572b4f2001-05-06 01:05:02 +00002472 if (PyErr_Occurred()) {
2473 Py_DECREF(ret);
2474 ret = NULL;
Barry Warsawbd599b52000-08-03 15:45:29 +00002475 }
2476 Py_DECREF(next);
Tim Peters8572b4f2001-05-06 01:05:02 +00002477 Py_DECREF(itlist);
Tim Peters67d687a2002-04-29 21:27:32 +00002478 goto Done;
Barry Warsawbd599b52000-08-03 15:45:29 +00002479 }
Tim Peters67d687a2002-04-29 21:27:32 +00002480 PyTuple_SET_ITEM(next, j, item);
Barry Warsawbd599b52000-08-03 15:45:29 +00002481 }
Tim Peters8572b4f2001-05-06 01:05:02 +00002482
Tim Peters67d687a2002-04-29 21:27:32 +00002483 if (i < len)
2484 PyList_SET_ITEM(ret, i, next);
2485 else {
2486 int status = PyList_Append(ret, next);
2487 Py_DECREF(next);
2488 ++len;
2489 if (status < 0)
2490 goto Fail_ret_itlist;
2491 }
Barry Warsawbd599b52000-08-03 15:45:29 +00002492 }
Tim Peters8572b4f2001-05-06 01:05:02 +00002493
Tim Peters67d687a2002-04-29 21:27:32 +00002494Done:
2495 if (ret != NULL && i < len) {
2496 /* The list is too big. */
2497 if (PyList_SetSlice(ret, i, len, NULL) < 0)
2498 return NULL;
2499 }
2500 return ret;
2501
Tim Peters8572b4f2001-05-06 01:05:02 +00002502Fail_ret_itlist:
2503 Py_DECREF(itlist);
2504Fail_ret:
2505 Py_DECREF(ret);
2506 return NULL;
Barry Warsawbd599b52000-08-03 15:45:29 +00002507}
2508
2509
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002510PyDoc_STRVAR(zip_doc,
Barry Warsawbd599b52000-08-03 15:45:29 +00002511"zip(seq1 [, seq2 [...]]) -> [(seq1[0], seq2[0] ...), (...)]\n\
2512\n\
2513Return a list of tuples, where each tuple contains the i-th element\n\
2514from each of the argument sequences. The returned list is truncated\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002515in length to the length of the shortest argument sequence.");
Barry Warsawbd599b52000-08-03 15:45:29 +00002516
2517
Guido van Rossum79f25d91997-04-29 20:08:16 +00002518static PyMethodDef builtin_methods[] = {
Neal Norwitz92e212f2006-04-03 04:48:37 +00002519 {"__import__", (PyCFunction)builtin___import__, METH_VARARGS | METH_KEYWORDS, import_doc},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002520 {"abs", builtin_abs, METH_O, abs_doc},
Raymond Hettinger96229b12005-03-11 06:49:40 +00002521 {"all", builtin_all, METH_O, all_doc},
2522 {"any", builtin_any, METH_O, any_doc},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002523 {"apply", builtin_apply, METH_VARARGS, apply_doc},
Eric Smith3cd81942008-02-22 16:30:22 +00002524 {"bin", builtin_bin, METH_O, bin_doc},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002525 {"callable", builtin_callable, METH_O, callable_doc},
2526 {"chr", builtin_chr, METH_VARARGS, chr_doc},
2527 {"cmp", builtin_cmp, METH_VARARGS, cmp_doc},
2528 {"coerce", builtin_coerce, METH_VARARGS, coerce_doc},
Georg Brandl5240d742007-03-13 20:46:32 +00002529 {"compile", (PyCFunction)builtin_compile, METH_VARARGS | METH_KEYWORDS, compile_doc},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002530 {"delattr", builtin_delattr, METH_VARARGS, delattr_doc},
2531 {"dir", builtin_dir, METH_VARARGS, dir_doc},
2532 {"divmod", builtin_divmod, METH_VARARGS, divmod_doc},
2533 {"eval", builtin_eval, METH_VARARGS, eval_doc},
2534 {"execfile", builtin_execfile, METH_VARARGS, execfile_doc},
2535 {"filter", builtin_filter, METH_VARARGS, filter_doc},
Eric Smitha9f7d622008-02-17 19:46:49 +00002536 {"format", builtin_format, METH_VARARGS, format_doc},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002537 {"getattr", builtin_getattr, METH_VARARGS, getattr_doc},
2538 {"globals", (PyCFunction)builtin_globals, METH_NOARGS, globals_doc},
2539 {"hasattr", builtin_hasattr, METH_VARARGS, hasattr_doc},
2540 {"hash", builtin_hash, METH_O, hash_doc},
2541 {"hex", builtin_hex, METH_O, hex_doc},
2542 {"id", builtin_id, METH_O, id_doc},
2543 {"input", builtin_input, METH_VARARGS, input_doc},
2544 {"intern", builtin_intern, METH_VARARGS, intern_doc},
2545 {"isinstance", builtin_isinstance, METH_VARARGS, isinstance_doc},
2546 {"issubclass", builtin_issubclass, METH_VARARGS, issubclass_doc},
2547 {"iter", builtin_iter, METH_VARARGS, iter_doc},
2548 {"len", builtin_len, METH_O, len_doc},
2549 {"locals", (PyCFunction)builtin_locals, METH_NOARGS, locals_doc},
2550 {"map", builtin_map, METH_VARARGS, map_doc},
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00002551 {"max", (PyCFunction)builtin_max, METH_VARARGS | METH_KEYWORDS, max_doc},
2552 {"min", (PyCFunction)builtin_min, METH_VARARGS | METH_KEYWORDS, min_doc},
Georg Brandl28e08732008-04-30 19:47:09 +00002553 {"next", builtin_next, METH_VARARGS, next_doc},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002554 {"oct", builtin_oct, METH_O, oct_doc},
Neal Norwitzc4edb0e2006-05-02 04:43:14 +00002555 {"open", (PyCFunction)builtin_open, METH_VARARGS | METH_KEYWORDS, open_doc},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002556 {"ord", builtin_ord, METH_O, ord_doc},
2557 {"pow", builtin_pow, METH_VARARGS, pow_doc},
Eric Smith7c478942008-03-18 23:45:49 +00002558 {"print", (PyCFunction)builtin_print, METH_VARARGS | METH_KEYWORDS, print_doc},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002559 {"range", builtin_range, METH_VARARGS, range_doc},
2560 {"raw_input", builtin_raw_input, METH_VARARGS, raw_input_doc},
2561 {"reduce", builtin_reduce, METH_VARARGS, reduce_doc},
2562 {"reload", builtin_reload, METH_O, reload_doc},
2563 {"repr", builtin_repr, METH_O, repr_doc},
Georg Brandlccadf842006-03-31 18:54:53 +00002564 {"round", (PyCFunction)builtin_round, METH_VARARGS | METH_KEYWORDS, round_doc},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002565 {"setattr", builtin_setattr, METH_VARARGS, setattr_doc},
Raymond Hettinger64958a12003-12-17 20:43:33 +00002566 {"sorted", (PyCFunction)builtin_sorted, METH_VARARGS | METH_KEYWORDS, sorted_doc},
Alex Martellia70b1912003-04-22 08:12:33 +00002567 {"sum", builtin_sum, METH_VARARGS, sum_doc},
Martin v. Löwis339d0f72001-08-17 18:39:25 +00002568#ifdef Py_USING_UNICODE
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002569 {"unichr", builtin_unichr, METH_VARARGS, unichr_doc},
Martin v. Löwis339d0f72001-08-17 18:39:25 +00002570#endif
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002571 {"vars", builtin_vars, METH_VARARGS, vars_doc},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002572 {"zip", builtin_zip, METH_VARARGS, zip_doc},
Guido van Rossumc02e15c1991-12-16 13:03:00 +00002573 {NULL, NULL},
Guido van Rossum3f5da241990-12-20 15:06:42 +00002574};
2575
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002576PyDoc_STRVAR(builtin_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002577"Built-in functions, exceptions, and other objects.\n\
2578\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002579Noteworthy: None is the `nil' object; Ellipsis represents `...' in slices.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002580
Guido van Rossum25ce5661997-08-02 03:10:38 +00002581PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002582_PyBuiltin_Init(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +00002583{
Fred Drake5550de32000-06-20 04:54:19 +00002584 PyObject *mod, *dict, *debug;
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002585 mod = Py_InitModule4("__builtin__", builtin_methods,
2586 builtin_doc, (PyObject *)NULL,
2587 PYTHON_API_VERSION);
Guido van Rossum25ce5661997-08-02 03:10:38 +00002588 if (mod == NULL)
2589 return NULL;
2590 dict = PyModule_GetDict(mod);
Tim Peters4b7625e2001-09-13 21:37:17 +00002591
Tim Peters7571a0f2003-03-23 17:52:28 +00002592#ifdef Py_TRACE_REFS
2593 /* __builtin__ exposes a number of statically allocated objects
2594 * that, before this code was added in 2.3, never showed up in
2595 * the list of "all objects" maintained by Py_TRACE_REFS. As a
2596 * result, programs leaking references to None and False (etc)
2597 * couldn't be diagnosed by examining sys.getobjects(0).
2598 */
2599#define ADD_TO_ALL(OBJECT) _Py_AddToAllObjects((PyObject *)(OBJECT), 0)
2600#else
2601#define ADD_TO_ALL(OBJECT) (void)0
2602#endif
2603
Tim Peters4b7625e2001-09-13 21:37:17 +00002604#define SETBUILTIN(NAME, OBJECT) \
Tim Peters7571a0f2003-03-23 17:52:28 +00002605 if (PyDict_SetItemString(dict, NAME, (PyObject *)OBJECT) < 0) \
2606 return NULL; \
2607 ADD_TO_ALL(OBJECT)
Tim Peters4b7625e2001-09-13 21:37:17 +00002608
2609 SETBUILTIN("None", Py_None);
2610 SETBUILTIN("Ellipsis", Py_Ellipsis);
2611 SETBUILTIN("NotImplemented", Py_NotImplemented);
Guido van Rossum77f6a652002-04-03 22:41:51 +00002612 SETBUILTIN("False", Py_False);
2613 SETBUILTIN("True", Py_True);
Neal Norwitz32a7e7f2002-05-31 19:58:02 +00002614 SETBUILTIN("basestring", &PyBaseString_Type);
Guido van Rossum77f6a652002-04-03 22:41:51 +00002615 SETBUILTIN("bool", &PyBool_Type);
Travis E. Oliphant3781aef2008-03-18 04:44:57 +00002616 /* SETBUILTIN("memoryview", &PyMemoryView_Type); */
Christian Heimes1a6387e2008-03-26 12:49:49 +00002617 SETBUILTIN("bytearray", &PyBytes_Type);
Christian Heimes288e89a2008-01-18 18:24:07 +00002618 SETBUILTIN("bytes", &PyString_Type);
Guido van Rossumbea18cc2002-06-14 20:41:17 +00002619 SETBUILTIN("buffer", &PyBuffer_Type);
Tim Peters4b7625e2001-09-13 21:37:17 +00002620 SETBUILTIN("classmethod", &PyClassMethod_Type);
Tim Peters6d6c1a32001-08-02 04:15:00 +00002621#ifndef WITHOUT_COMPLEX
Tim Peters4b7625e2001-09-13 21:37:17 +00002622 SETBUILTIN("complex", &PyComplex_Type);
Tim Peters6d6c1a32001-08-02 04:15:00 +00002623#endif
Tim Petersa427a2b2001-10-29 22:25:45 +00002624 SETBUILTIN("dict", &PyDict_Type);
Tim Peters67d687a2002-04-29 21:27:32 +00002625 SETBUILTIN("enumerate", &PyEnum_Type);
Neal Norwitzc4edb0e2006-05-02 04:43:14 +00002626 SETBUILTIN("file", &PyFile_Type);
Tim Peters4b7625e2001-09-13 21:37:17 +00002627 SETBUILTIN("float", &PyFloat_Type);
Raymond Hettingera690a992003-11-16 16:17:49 +00002628 SETBUILTIN("frozenset", &PyFrozenSet_Type);
Tim Peters4b7625e2001-09-13 21:37:17 +00002629 SETBUILTIN("property", &PyProperty_Type);
2630 SETBUILTIN("int", &PyInt_Type);
2631 SETBUILTIN("list", &PyList_Type);
2632 SETBUILTIN("long", &PyLong_Type);
2633 SETBUILTIN("object", &PyBaseObject_Type);
Raymond Hettinger85c20a42003-11-06 14:06:48 +00002634 SETBUILTIN("reversed", &PyReversed_Type);
Raymond Hettingera690a992003-11-16 16:17:49 +00002635 SETBUILTIN("set", &PySet_Type);
Guido van Rossumbea18cc2002-06-14 20:41:17 +00002636 SETBUILTIN("slice", &PySlice_Type);
Tim Peters4b7625e2001-09-13 21:37:17 +00002637 SETBUILTIN("staticmethod", &PyStaticMethod_Type);
2638 SETBUILTIN("str", &PyString_Type);
2639 SETBUILTIN("super", &PySuper_Type);
2640 SETBUILTIN("tuple", &PyTuple_Type);
2641 SETBUILTIN("type", &PyType_Type);
Raymond Hettingerc4c453f2002-06-05 23:12:45 +00002642 SETBUILTIN("xrange", &PyRange_Type);
Martin v. Löwis339d0f72001-08-17 18:39:25 +00002643#ifdef Py_USING_UNICODE
Tim Peters4b7625e2001-09-13 21:37:17 +00002644 SETBUILTIN("unicode", &PyUnicode_Type);
Martin v. Löwis339d0f72001-08-17 18:39:25 +00002645#endif
Guido van Rossum77f6a652002-04-03 22:41:51 +00002646 debug = PyBool_FromLong(Py_OptimizeFlag == 0);
Fred Drake5550de32000-06-20 04:54:19 +00002647 if (PyDict_SetItemString(dict, "__debug__", debug) < 0) {
2648 Py_XDECREF(debug);
Guido van Rossum25ce5661997-08-02 03:10:38 +00002649 return NULL;
Fred Drake5550de32000-06-20 04:54:19 +00002650 }
2651 Py_XDECREF(debug);
Barry Warsaw757af0e1997-08-29 22:13:51 +00002652
Guido van Rossum25ce5661997-08-02 03:10:38 +00002653 return mod;
Tim Peters7571a0f2003-03-23 17:52:28 +00002654#undef ADD_TO_ALL
Tim Peters4b7625e2001-09-13 21:37:17 +00002655#undef SETBUILTIN
Guido van Rossum3f5da241990-12-20 15:06:42 +00002656}
2657
Guido van Rossume77a7571993-11-03 15:01:26 +00002658/* Helper for filter(): filter a tuple through a function */
Guido van Rossum12d12c51993-10-26 17:58:25 +00002659
Guido van Rossum79f25d91997-04-29 20:08:16 +00002660static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002661filtertuple(PyObject *func, PyObject *tuple)
Guido van Rossum12d12c51993-10-26 17:58:25 +00002662{
Guido van Rossum79f25d91997-04-29 20:08:16 +00002663 PyObject *result;
Martin v. Löwis18e16552006-02-15 17:27:45 +00002664 Py_ssize_t i, j;
2665 Py_ssize_t len = PyTuple_Size(tuple);
Guido van Rossum12d12c51993-10-26 17:58:25 +00002666
Guido van Rossumb7b45621995-08-04 04:07:45 +00002667 if (len == 0) {
Walter Dörwaldc3da83f2003-02-04 20:24:45 +00002668 if (PyTuple_CheckExact(tuple))
2669 Py_INCREF(tuple);
2670 else
2671 tuple = PyTuple_New(0);
Guido van Rossumb7b45621995-08-04 04:07:45 +00002672 return tuple;
2673 }
2674
Guido van Rossum79f25d91997-04-29 20:08:16 +00002675 if ((result = PyTuple_New(len)) == NULL)
Guido van Rossum2586bf01993-11-01 16:21:44 +00002676 return NULL;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002677
Guido van Rossum12d12c51993-10-26 17:58:25 +00002678 for (i = j = 0; i < len; ++i) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002679 PyObject *item, *good;
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002680 int ok;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002681
Walter Dörwald8dd19322003-02-10 17:36:40 +00002682 if (tuple->ob_type->tp_as_sequence &&
2683 tuple->ob_type->tp_as_sequence->sq_item) {
2684 item = tuple->ob_type->tp_as_sequence->sq_item(tuple, i);
Walter Dörwaldc58a3a12003-08-18 18:28:45 +00002685 if (item == NULL)
2686 goto Fail_1;
Walter Dörwald8dd19322003-02-10 17:36:40 +00002687 } else {
Alex Martellia9b9c9f2003-04-23 13:34:35 +00002688 PyErr_SetString(PyExc_TypeError, "filter(): unsubscriptable tuple");
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002689 goto Fail_1;
Walter Dörwald8dd19322003-02-10 17:36:40 +00002690 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002691 if (func == Py_None) {
2692 Py_INCREF(item);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002693 good = item;
2694 }
2695 else {
Raymond Hettinger8ae46892003-10-12 19:09:37 +00002696 PyObject *arg = PyTuple_Pack(1, item);
Walter Dörwaldc58a3a12003-08-18 18:28:45 +00002697 if (arg == NULL) {
2698 Py_DECREF(item);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002699 goto Fail_1;
Walter Dörwaldc58a3a12003-08-18 18:28:45 +00002700 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002701 good = PyEval_CallObject(func, arg);
2702 Py_DECREF(arg);
Walter Dörwaldc58a3a12003-08-18 18:28:45 +00002703 if (good == NULL) {
2704 Py_DECREF(item);
Guido van Rossum12d12c51993-10-26 17:58:25 +00002705 goto Fail_1;
Walter Dörwaldc58a3a12003-08-18 18:28:45 +00002706 }
Guido van Rossum12d12c51993-10-26 17:58:25 +00002707 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002708 ok = PyObject_IsTrue(good);
2709 Py_DECREF(good);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002710 if (ok) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002711 if (PyTuple_SetItem(result, j++, item) < 0)
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002712 goto Fail_1;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002713 }
Walter Dörwaldc58a3a12003-08-18 18:28:45 +00002714 else
2715 Py_DECREF(item);
Guido van Rossum12d12c51993-10-26 17:58:25 +00002716 }
2717
Tim Peters4324aa32001-05-28 22:30:08 +00002718 if (_PyTuple_Resize(&result, j) < 0)
Guido van Rossum12d12c51993-10-26 17:58:25 +00002719 return NULL;
2720
Guido van Rossum12d12c51993-10-26 17:58:25 +00002721 return result;
2722
Guido van Rossum12d12c51993-10-26 17:58:25 +00002723Fail_1:
Guido van Rossum79f25d91997-04-29 20:08:16 +00002724 Py_DECREF(result);
Guido van Rossum12d12c51993-10-26 17:58:25 +00002725 return NULL;
2726}
2727
2728
Guido van Rossume77a7571993-11-03 15:01:26 +00002729/* Helper for filter(): filter a string through a function */
Guido van Rossum12d12c51993-10-26 17:58:25 +00002730
Guido van Rossum79f25d91997-04-29 20:08:16 +00002731static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002732filterstring(PyObject *func, PyObject *strobj)
Guido van Rossum12d12c51993-10-26 17:58:25 +00002733{
Guido van Rossum79f25d91997-04-29 20:08:16 +00002734 PyObject *result;
Martin v. Löwis18e16552006-02-15 17:27:45 +00002735 Py_ssize_t i, j;
2736 Py_ssize_t len = PyString_Size(strobj);
2737 Py_ssize_t outlen = len;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002738
Guido van Rossum79f25d91997-04-29 20:08:16 +00002739 if (func == Py_None) {
Walter Dörwald1918f772003-02-10 13:19:13 +00002740 /* If it's a real string we can return the original,
2741 * as no character is ever false and __getitem__
2742 * does return this character. If it's a subclass
2743 * we must go through the __getitem__ loop */
2744 if (PyString_CheckExact(strobj)) {
Walter Dörwaldc3da83f2003-02-04 20:24:45 +00002745 Py_INCREF(strobj);
Walter Dörwald1918f772003-02-10 13:19:13 +00002746 return strobj;
2747 }
Guido van Rossum12d12c51993-10-26 17:58:25 +00002748 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002749 if ((result = PyString_FromStringAndSize(NULL, len)) == NULL)
Guido van Rossum2586bf01993-11-01 16:21:44 +00002750 return NULL;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002751
Guido van Rossum12d12c51993-10-26 17:58:25 +00002752 for (i = j = 0; i < len; ++i) {
Walter Dörwald1918f772003-02-10 13:19:13 +00002753 PyObject *item;
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002754 int ok;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002755
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002756 item = (*strobj->ob_type->tp_as_sequence->sq_item)(strobj, i);
2757 if (item == NULL)
2758 goto Fail_1;
Walter Dörwald1918f772003-02-10 13:19:13 +00002759 if (func==Py_None) {
2760 ok = 1;
2761 } else {
2762 PyObject *arg, *good;
Raymond Hettinger8ae46892003-10-12 19:09:37 +00002763 arg = PyTuple_Pack(1, item);
Walter Dörwald1918f772003-02-10 13:19:13 +00002764 if (arg == NULL) {
2765 Py_DECREF(item);
2766 goto Fail_1;
2767 }
2768 good = PyEval_CallObject(func, arg);
2769 Py_DECREF(arg);
2770 if (good == NULL) {
2771 Py_DECREF(item);
2772 goto Fail_1;
2773 }
2774 ok = PyObject_IsTrue(good);
2775 Py_DECREF(good);
Tim Peters388ed082001-04-07 20:34:48 +00002776 }
Walter Dörwald903f1e02003-02-04 16:28:00 +00002777 if (ok) {
Martin v. Löwisd96ee902006-02-16 14:37:16 +00002778 Py_ssize_t reslen;
Walter Dörwald903f1e02003-02-04 16:28:00 +00002779 if (!PyString_Check(item)) {
2780 PyErr_SetString(PyExc_TypeError, "can't filter str to str:"
2781 " __getitem__ returned different type");
2782 Py_DECREF(item);
2783 goto Fail_1;
2784 }
2785 reslen = PyString_GET_SIZE(item);
2786 if (reslen == 1) {
2787 PyString_AS_STRING(result)[j++] =
2788 PyString_AS_STRING(item)[0];
2789 } else {
2790 /* do we need more space? */
Martin v. Löwisd96ee902006-02-16 14:37:16 +00002791 Py_ssize_t need = j + reslen + len-i-1;
Walter Dörwald903f1e02003-02-04 16:28:00 +00002792 if (need > outlen) {
2793 /* overallocate, to avoid reallocations */
2794 if (need<2*outlen)
2795 need = 2*outlen;
2796 if (_PyString_Resize(&result, need)) {
2797 Py_DECREF(item);
2798 return NULL;
2799 }
2800 outlen = need;
2801 }
2802 memcpy(
2803 PyString_AS_STRING(result) + j,
2804 PyString_AS_STRING(item),
2805 reslen
2806 );
2807 j += reslen;
2808 }
2809 }
Tim Peters388ed082001-04-07 20:34:48 +00002810 Py_DECREF(item);
Guido van Rossum12d12c51993-10-26 17:58:25 +00002811 }
2812
Walter Dörwald903f1e02003-02-04 16:28:00 +00002813 if (j < outlen)
Tim Peters5de98422002-04-27 18:44:32 +00002814 _PyString_Resize(&result, j);
Guido van Rossum12d12c51993-10-26 17:58:25 +00002815
Guido van Rossum12d12c51993-10-26 17:58:25 +00002816 return result;
2817
Guido van Rossum12d12c51993-10-26 17:58:25 +00002818Fail_1:
Guido van Rossum79f25d91997-04-29 20:08:16 +00002819 Py_DECREF(result);
Guido van Rossum12d12c51993-10-26 17:58:25 +00002820 return NULL;
2821}
Martin v. Löwis8afd7572003-01-25 22:46:11 +00002822
2823#ifdef Py_USING_UNICODE
2824/* Helper for filter(): filter a Unicode object through a function */
2825
2826static PyObject *
2827filterunicode(PyObject *func, PyObject *strobj)
2828{
2829 PyObject *result;
Martin v. Löwis725507b2006-03-07 12:08:51 +00002830 register Py_ssize_t i, j;
Martin v. Löwisd96ee902006-02-16 14:37:16 +00002831 Py_ssize_t len = PyUnicode_GetSize(strobj);
2832 Py_ssize_t outlen = len;
Martin v. Löwis8afd7572003-01-25 22:46:11 +00002833
2834 if (func == Py_None) {
Walter Dörwald1918f772003-02-10 13:19:13 +00002835 /* If it's a real string we can return the original,
2836 * as no character is ever false and __getitem__
2837 * does return this character. If it's a subclass
2838 * we must go through the __getitem__ loop */
2839 if (PyUnicode_CheckExact(strobj)) {
Walter Dörwaldc3da83f2003-02-04 20:24:45 +00002840 Py_INCREF(strobj);
Walter Dörwald1918f772003-02-10 13:19:13 +00002841 return strobj;
2842 }
Martin v. Löwis8afd7572003-01-25 22:46:11 +00002843 }
2844 if ((result = PyUnicode_FromUnicode(NULL, len)) == NULL)
2845 return NULL;
2846
2847 for (i = j = 0; i < len; ++i) {
2848 PyObject *item, *arg, *good;
2849 int ok;
2850
2851 item = (*strobj->ob_type->tp_as_sequence->sq_item)(strobj, i);
2852 if (item == NULL)
2853 goto Fail_1;
Walter Dörwald1918f772003-02-10 13:19:13 +00002854 if (func == Py_None) {
2855 ok = 1;
2856 } else {
Raymond Hettinger8ae46892003-10-12 19:09:37 +00002857 arg = PyTuple_Pack(1, item);
Walter Dörwald1918f772003-02-10 13:19:13 +00002858 if (arg == NULL) {
2859 Py_DECREF(item);
2860 goto Fail_1;
2861 }
2862 good = PyEval_CallObject(func, arg);
2863 Py_DECREF(arg);
2864 if (good == NULL) {
2865 Py_DECREF(item);
2866 goto Fail_1;
2867 }
2868 ok = PyObject_IsTrue(good);
2869 Py_DECREF(good);
Martin v. Löwis8afd7572003-01-25 22:46:11 +00002870 }
Walter Dörwald903f1e02003-02-04 16:28:00 +00002871 if (ok) {
Martin v. Löwisd96ee902006-02-16 14:37:16 +00002872 Py_ssize_t reslen;
Walter Dörwald903f1e02003-02-04 16:28:00 +00002873 if (!PyUnicode_Check(item)) {
Georg Brandl99d7e4e2005-08-31 22:21:15 +00002874 PyErr_SetString(PyExc_TypeError,
Jeremy Hylton1aad9c72003-09-16 03:10:59 +00002875 "can't filter unicode to unicode:"
2876 " __getitem__ returned different type");
Walter Dörwald903f1e02003-02-04 16:28:00 +00002877 Py_DECREF(item);
2878 goto Fail_1;
2879 }
2880 reslen = PyUnicode_GET_SIZE(item);
Georg Brandl99d7e4e2005-08-31 22:21:15 +00002881 if (reslen == 1)
Walter Dörwald903f1e02003-02-04 16:28:00 +00002882 PyUnicode_AS_UNICODE(result)[j++] =
2883 PyUnicode_AS_UNICODE(item)[0];
Jeremy Hylton1aad9c72003-09-16 03:10:59 +00002884 else {
Walter Dörwald903f1e02003-02-04 16:28:00 +00002885 /* do we need more space? */
Martin v. Löwisd96ee902006-02-16 14:37:16 +00002886 Py_ssize_t need = j + reslen + len - i - 1;
Walter Dörwald903f1e02003-02-04 16:28:00 +00002887 if (need > outlen) {
Georg Brandl99d7e4e2005-08-31 22:21:15 +00002888 /* overallocate,
Jeremy Hylton1aad9c72003-09-16 03:10:59 +00002889 to avoid reallocations */
2890 if (need < 2 * outlen)
2891 need = 2 * outlen;
Jeremy Hylton364f6be2003-09-16 03:17:16 +00002892 if (PyUnicode_Resize(
2893 &result, need) < 0) {
Walter Dörwald903f1e02003-02-04 16:28:00 +00002894 Py_DECREF(item);
Walter Dörwald531e0002003-02-04 16:57:49 +00002895 goto Fail_1;
Walter Dörwald903f1e02003-02-04 16:28:00 +00002896 }
2897 outlen = need;
2898 }
Jeremy Hylton1aad9c72003-09-16 03:10:59 +00002899 memcpy(PyUnicode_AS_UNICODE(result) + j,
2900 PyUnicode_AS_UNICODE(item),
2901 reslen*sizeof(Py_UNICODE));
Walter Dörwald903f1e02003-02-04 16:28:00 +00002902 j += reslen;
2903 }
2904 }
Martin v. Löwis8afd7572003-01-25 22:46:11 +00002905 Py_DECREF(item);
2906 }
2907
Walter Dörwald903f1e02003-02-04 16:28:00 +00002908 if (j < outlen)
Martin v. Löwis8afd7572003-01-25 22:46:11 +00002909 PyUnicode_Resize(&result, j);
2910
2911 return result;
2912
2913Fail_1:
2914 Py_DECREF(result);
2915 return NULL;
2916}
2917#endif