blob: bcdcda6355894c8e2483723166ae0720186639e0 [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 Hammond26cffde42001-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 Hammond26cffde42001-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 Hammond26cffde42001-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
Neal Norwitz8b2bfbc2007-05-23 06:35:32 +0000168 if (Py_Py3kWarningFlag &&
169 PyErr_Warn(PyExc_DeprecationWarning,
Georg Brandld5b635f2008-03-25 08:29:14 +0000170 "apply() not supported in 3.x; "
171 "use func(*args, **kwargs)") < 0)
Neal Norwitz8b2bfbc2007-05-23 06:35:32 +0000172 return NULL;
173
Raymond Hettingerea3fdf42002-12-29 16:33:45 +0000174 if (!PyArg_UnpackTuple(args, "apply", 1, 3, &func, &alist, &kwdict))
Guido van Rossumc02e15c1991-12-16 13:03:00 +0000175 return NULL;
Barry Warsaw968f8cb1998-10-01 15:33:12 +0000176 if (alist != NULL) {
177 if (!PyTuple_Check(alist)) {
178 if (!PySequence_Check(alist)) {
Jeremy Hyltonc862cf42001-01-19 03:25:05 +0000179 PyErr_Format(PyExc_TypeError,
Alex Martellia9b9c9f2003-04-23 13:34:35 +0000180 "apply() arg 2 expected sequence, found %s",
Jeremy Hyltonc862cf42001-01-19 03:25:05 +0000181 alist->ob_type->tp_name);
Barry Warsaw968f8cb1998-10-01 15:33:12 +0000182 return NULL;
183 }
184 t = PySequence_Tuple(alist);
185 if (t == NULL)
186 return NULL;
187 alist = t;
188 }
Guido van Rossum2d951851994-08-29 12:52:16 +0000189 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000190 if (kwdict != NULL && !PyDict_Check(kwdict)) {
Jeremy Hyltonc862cf42001-01-19 03:25:05 +0000191 PyErr_Format(PyExc_TypeError,
192 "apply() arg 3 expected dictionary, found %s",
193 kwdict->ob_type->tp_name);
Barry Warsaw968f8cb1998-10-01 15:33:12 +0000194 goto finally;
Guido van Rossum681d79a1995-07-18 14:51:37 +0000195 }
Barry Warsaw968f8cb1998-10-01 15:33:12 +0000196 retval = PyEval_CallObjectWithKeywords(func, alist, kwdict);
197 finally:
198 Py_XDECREF(t);
199 return retval;
Guido van Rossumc02e15c1991-12-16 13:03:00 +0000200}
201
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000202PyDoc_STRVAR(apply_doc,
Fred Drakef1fbc622001-01-12 17:05:05 +0000203"apply(object[, args[, kwargs]]) -> value\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000204\n\
Fred Drake7b912121999-12-23 14:16:55 +0000205Call a callable object with positional arguments taken from the tuple args,\n\
206and keyword arguments taken from the optional dictionary kwargs.\n\
Raymond Hettingerff41c482003-04-06 09:01:11 +0000207Note that classes are callable, as are instances with a __call__() method.\n\
208\n\
209Deprecated since release 2.3. Instead, use the extended call syntax:\n\
210 function(*args, **keywords).");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000211
212
Guido van Rossum79f25d91997-04-29 20:08:16 +0000213static PyObject *
Eric Smith3cd81942008-02-22 16:30:22 +0000214builtin_bin(PyObject *self, PyObject *v)
215{
216 return PyNumber_ToBase(v, 2);
217}
218
219PyDoc_STRVAR(bin_doc,
220"bin(number) -> string\n\
221\n\
222Return the binary representation of an integer or long integer.");
223
224
225static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000226builtin_callable(PyObject *self, PyObject *v)
Guido van Rossum2d951851994-08-29 12:52:16 +0000227{
Neal Norwitzdf25efe2007-05-23 06:58:36 +0000228 if (Py_Py3kWarningFlag &&
229 PyErr_Warn(PyExc_DeprecationWarning,
Georg Brandld5b635f2008-03-25 08:29:14 +0000230 "callable() not supported in 3.x; "
231 "use hasattr(o, '__call__')") < 0)
Neal Norwitzdf25efe2007-05-23 06:58:36 +0000232 return NULL;
Guido van Rossum77f6a652002-04-03 22:41:51 +0000233 return PyBool_FromLong((long)PyCallable_Check(v));
Guido van Rossum2d951851994-08-29 12:52:16 +0000234}
235
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000236PyDoc_STRVAR(callable_doc,
Guido van Rossum77f6a652002-04-03 22:41:51 +0000237"callable(object) -> bool\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000238\n\
239Return whether the object is callable (i.e., some kind of function).\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000240Note that classes are callable, as are instances with a __call__() method.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000241
242
Guido van Rossum79f25d91997-04-29 20:08:16 +0000243static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000244builtin_filter(PyObject *self, PyObject *args)
Guido van Rossum12d12c51993-10-26 17:58:25 +0000245{
Guido van Rossumc7903a12002-08-16 07:04:56 +0000246 PyObject *func, *seq, *result, *it, *arg;
Martin v. Löwisd96ee902006-02-16 14:37:16 +0000247 Py_ssize_t len; /* guess for result list size */
248 register Py_ssize_t j;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000249
Raymond Hettingerea3fdf42002-12-29 16:33:45 +0000250 if (!PyArg_UnpackTuple(args, "filter", 2, 2, &func, &seq))
Guido van Rossum12d12c51993-10-26 17:58:25 +0000251 return NULL;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000252
Tim Peters0e57abf2001-05-02 07:39:38 +0000253 /* Strings and tuples return a result of the same type. */
254 if (PyString_Check(seq))
255 return filterstring(func, seq);
Martin v. Löwis8afd7572003-01-25 22:46:11 +0000256#ifdef Py_USING_UNICODE
257 if (PyUnicode_Check(seq))
258 return filterunicode(func, seq);
259#endif
Tim Peters0e57abf2001-05-02 07:39:38 +0000260 if (PyTuple_Check(seq))
261 return filtertuple(func, seq);
262
Georg Brandle35b6572005-07-19 22:20:20 +0000263 /* Pre-allocate argument list tuple. */
264 arg = PyTuple_New(1);
265 if (arg == NULL)
266 return NULL;
267
Tim Peters0e57abf2001-05-02 07:39:38 +0000268 /* Get iterator. */
269 it = PyObject_GetIter(seq);
270 if (it == NULL)
Georg Brandle35b6572005-07-19 22:20:20 +0000271 goto Fail_arg;
Tim Peters0e57abf2001-05-02 07:39:38 +0000272
273 /* Guess a result list size. */
Raymond Hettinger4e2f7142007-12-06 00:56:53 +0000274 len = _PyObject_LengthHint(seq, 8);
Guido van Rossum12d12c51993-10-26 17:58:25 +0000275
Tim Peters0e57abf2001-05-02 07:39:38 +0000276 /* Get a result list. */
Guido van Rossum79f25d91997-04-29 20:08:16 +0000277 if (PyList_Check(seq) && seq->ob_refcnt == 1) {
Tim Peters0e57abf2001-05-02 07:39:38 +0000278 /* Eww - can modify the list in-place. */
Guido van Rossum79f25d91997-04-29 20:08:16 +0000279 Py_INCREF(seq);
Guido van Rossum12d12c51993-10-26 17:58:25 +0000280 result = seq;
281 }
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000282 else {
Tim Peters0e57abf2001-05-02 07:39:38 +0000283 result = PyList_New(len);
284 if (result == NULL)
285 goto Fail_it;
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000286 }
Guido van Rossum12d12c51993-10-26 17:58:25 +0000287
Tim Peters0e57abf2001-05-02 07:39:38 +0000288 /* Build the result list. */
Tim Petersf4848da2001-05-05 00:14:56 +0000289 j = 0;
290 for (;;) {
Guido van Rossumc7903a12002-08-16 07:04:56 +0000291 PyObject *item;
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000292 int ok;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000293
Tim Peters0e57abf2001-05-02 07:39:38 +0000294 item = PyIter_Next(it);
295 if (item == NULL) {
Tim Petersf4848da2001-05-05 00:14:56 +0000296 if (PyErr_Occurred())
297 goto Fail_result_it;
Tim Peters0e57abf2001-05-02 07:39:38 +0000298 break;
Guido van Rossum2d951851994-08-29 12:52:16 +0000299 }
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000300
Neil Schemenauer68973552003-08-14 20:37:34 +0000301 if (func == (PyObject *)&PyBool_Type || func == Py_None) {
Guido van Rossumc7903a12002-08-16 07:04:56 +0000302 ok = PyObject_IsTrue(item);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000303 }
304 else {
Guido van Rossumc7903a12002-08-16 07:04:56 +0000305 PyObject *good;
306 PyTuple_SET_ITEM(arg, 0, item);
307 good = PyObject_Call(func, arg, NULL);
308 PyTuple_SET_ITEM(arg, 0, NULL);
Guido van Rossum58b68731995-01-10 17:40:55 +0000309 if (good == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000310 Py_DECREF(item);
Tim Peters0e57abf2001-05-02 07:39:38 +0000311 goto Fail_result_it;
Guido van Rossum58b68731995-01-10 17:40:55 +0000312 }
Guido van Rossumc7903a12002-08-16 07:04:56 +0000313 ok = PyObject_IsTrue(good);
314 Py_DECREF(good);
Guido van Rossum12d12c51993-10-26 17:58:25 +0000315 }
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000316 if (ok) {
Tim Peters0e57abf2001-05-02 07:39:38 +0000317 if (j < len)
318 PyList_SET_ITEM(result, j, item);
Guido van Rossum2d951851994-08-29 12:52:16 +0000319 else {
Barry Warsawfa77e091999-01-28 18:49:12 +0000320 int status = PyList_Append(result, item);
Barry Warsawfa77e091999-01-28 18:49:12 +0000321 Py_DECREF(item);
322 if (status < 0)
Tim Peters0e57abf2001-05-02 07:39:38 +0000323 goto Fail_result_it;
Guido van Rossum2d951851994-08-29 12:52:16 +0000324 }
Tim Peters0e57abf2001-05-02 07:39:38 +0000325 ++j;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000326 }
Tim Peters0e57abf2001-05-02 07:39:38 +0000327 else
328 Py_DECREF(item);
Guido van Rossum12d12c51993-10-26 17:58:25 +0000329 }
330
Guido van Rossum12d12c51993-10-26 17:58:25 +0000331
Tim Peters0e57abf2001-05-02 07:39:38 +0000332 /* Cut back result list if len is too big. */
Guido van Rossum79f25d91997-04-29 20:08:16 +0000333 if (j < len && PyList_SetSlice(result, j, len, NULL) < 0)
Tim Peters0e57abf2001-05-02 07:39:38 +0000334 goto Fail_result_it;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000335
Tim Peters3c6b1482001-05-21 08:07:05 +0000336 Py_DECREF(it);
Guido van Rossumc7903a12002-08-16 07:04:56 +0000337 Py_DECREF(arg);
Guido van Rossum12d12c51993-10-26 17:58:25 +0000338 return result;
339
Tim Peters0e57abf2001-05-02 07:39:38 +0000340Fail_result_it:
Guido van Rossum79f25d91997-04-29 20:08:16 +0000341 Py_DECREF(result);
Tim Peters0e57abf2001-05-02 07:39:38 +0000342Fail_it:
343 Py_DECREF(it);
Guido van Rossumc7903a12002-08-16 07:04:56 +0000344Fail_arg:
345 Py_DECREF(arg);
Guido van Rossum12d12c51993-10-26 17:58:25 +0000346 return NULL;
347}
348
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000349PyDoc_STRVAR(filter_doc,
Tim Petersd50e5442002-03-09 00:06:26 +0000350"filter(function or None, sequence) -> list, tuple, or string\n"
351"\n"
352"Return those items of sequence for which function(item) is true. If\n"
353"function is None, return the items that are true. If sequence is a tuple\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000354"or string, return the same type, else return a list.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000355
Guido van Rossum79f25d91997-04-29 20:08:16 +0000356static PyObject *
Eric Smitha9f7d622008-02-17 19:46:49 +0000357builtin_format(PyObject *self, PyObject *args)
358{
359 PyObject *value;
360 PyObject *format_spec = NULL;
361
362 if (!PyArg_ParseTuple(args, "O|O:format", &value, &format_spec))
363 return NULL;
364
365 return PyObject_Format(value, format_spec);
366}
367
368PyDoc_STRVAR(format_doc,
369"format(value[, format_spec]) -> string\n\
370\n\
371Returns value.__format__(format_spec)\n\
372format_spec defaults to \"\"");
373
374static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000375builtin_chr(PyObject *self, PyObject *args)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000376{
377 long x;
378 char s[1];
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000379
Guido van Rossum79f25d91997-04-29 20:08:16 +0000380 if (!PyArg_ParseTuple(args, "l:chr", &x))
Guido van Rossum3f5da241990-12-20 15:06:42 +0000381 return NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000382 if (x < 0 || x >= 256) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000383 PyErr_SetString(PyExc_ValueError,
384 "chr() arg not in range(256)");
Guido van Rossum3f5da241990-12-20 15:06:42 +0000385 return NULL;
386 }
Guido van Rossum6bf62da1997-04-11 20:37:35 +0000387 s[0] = (char)x;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000388 return PyString_FromStringAndSize(s, 1);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000389}
390
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000391PyDoc_STRVAR(chr_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000392"chr(i) -> character\n\
393\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000394Return a string of one character with ordinal i; 0 <= i < 256.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000395
396
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000397#ifdef Py_USING_UNICODE
Guido van Rossum79f25d91997-04-29 20:08:16 +0000398static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000399builtin_unichr(PyObject *self, PyObject *args)
Guido van Rossum09095f32000-03-10 23:00:52 +0000400{
401 long x;
Guido van Rossum09095f32000-03-10 23:00:52 +0000402
403 if (!PyArg_ParseTuple(args, "l:unichr", &x))
404 return NULL;
Fredrik Lundh0dcf67e2001-06-26 20:01:56 +0000405
Marc-André Lemburgcc8764c2002-08-11 12:23:04 +0000406 return PyUnicode_FromOrdinal(x);
Guido van Rossum09095f32000-03-10 23:00:52 +0000407}
408
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000409PyDoc_STRVAR(unichr_doc,
Fred Drake078b24f2000-04-13 02:42:50 +0000410"unichr(i) -> Unicode character\n\
Guido van Rossum09095f32000-03-10 23:00:52 +0000411\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000412Return a Unicode string of one character with ordinal i; 0 <= i <= 0x10ffff.");
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000413#endif
Guido van Rossum09095f32000-03-10 23:00:52 +0000414
415
416static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000417builtin_cmp(PyObject *self, PyObject *args)
Guido van Rossuma9e7dc11992-10-18 18:53:57 +0000418{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000419 PyObject *a, *b;
Guido van Rossum09df08a1998-05-22 00:51:39 +0000420 int c;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000421
Raymond Hettingerea3fdf42002-12-29 16:33:45 +0000422 if (!PyArg_UnpackTuple(args, "cmp", 2, 2, &a, &b))
Guido van Rossuma9e7dc11992-10-18 18:53:57 +0000423 return NULL;
Guido van Rossum09df08a1998-05-22 00:51:39 +0000424 if (PyObject_Cmp(a, b, &c) < 0)
Guido van Rossumc8b6df91997-05-23 00:06:51 +0000425 return NULL;
Guido van Rossum09df08a1998-05-22 00:51:39 +0000426 return PyInt_FromLong((long)c);
Guido van Rossuma9e7dc11992-10-18 18:53:57 +0000427}
428
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000429PyDoc_STRVAR(cmp_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000430"cmp(x, y) -> integer\n\
431\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000432Return negative if x<y, zero if x==y, positive if x>y.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000433
434
Guido van Rossum79f25d91997-04-29 20:08:16 +0000435static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000436builtin_coerce(PyObject *self, PyObject *args)
Guido van Rossum6a00cd81995-01-07 12:39:01 +0000437{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000438 PyObject *v, *w;
439 PyObject *res;
Guido van Rossum5524a591995-01-10 15:26:20 +0000440
Neal Norwitzdf25efe2007-05-23 06:58:36 +0000441 if (Py_Py3kWarningFlag &&
442 PyErr_Warn(PyExc_DeprecationWarning,
443 "coerce() not supported in 3.x") < 0)
444 return NULL;
445
Raymond Hettingerea3fdf42002-12-29 16:33:45 +0000446 if (!PyArg_UnpackTuple(args, "coerce", 2, 2, &v, &w))
Guido van Rossum5524a591995-01-10 15:26:20 +0000447 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000448 if (PyNumber_Coerce(&v, &w) < 0)
Guido van Rossum04691fc1992-08-12 15:35:34 +0000449 return NULL;
Raymond Hettinger8ae46892003-10-12 19:09:37 +0000450 res = PyTuple_Pack(2, v, w);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000451 Py_DECREF(v);
452 Py_DECREF(w);
Guido van Rossum04691fc1992-08-12 15:35:34 +0000453 return res;
454}
455
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000456PyDoc_STRVAR(coerce_doc,
Martin v. Löwis8d494f32004-08-25 10:42:41 +0000457"coerce(x, y) -> (x1, y1)\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000458\n\
Martin v. Löwis8d494f32004-08-25 10:42:41 +0000459Return a tuple consisting of the two numeric arguments converted to\n\
460a common type, using the same rules as used by arithmetic operations.\n\
461If coercion is not possible, raise TypeError.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000462
Guido van Rossum79f25d91997-04-29 20:08:16 +0000463static PyObject *
Georg Brandl5240d742007-03-13 20:46:32 +0000464builtin_compile(PyObject *self, PyObject *args, PyObject *kwds)
Guido van Rossum5b722181993-03-30 17:46:03 +0000465{
466 char *str;
467 char *filename;
468 char *startstr;
Georg Brandlf2bfd542008-03-29 13:24:23 +0000469 int mode = -1;
Tim Peters6cd6a822001-08-17 22:11:27 +0000470 int dont_inherit = 0;
471 int supplied_flags = 0;
Tim Peters5ba58662001-07-16 02:29:45 +0000472 PyCompilerFlags cf;
Neal Norwitz3a9a3e72005-11-27 20:38:31 +0000473 PyObject *result = NULL, *cmd, *tmp = NULL;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000474 Py_ssize_t length;
Georg Brandl5240d742007-03-13 20:46:32 +0000475 static char *kwlist[] = {"source", "filename", "mode", "flags",
476 "dont_inherit", NULL};
Georg Brandlf2bfd542008-03-29 13:24:23 +0000477 int start[] = {Py_file_input, Py_eval_input, Py_single_input};
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000478
Georg Brandl5240d742007-03-13 20:46:32 +0000479 if (!PyArg_ParseTupleAndKeywords(args, kwds, "Oss|ii:compile",
480 kwlist, &cmd, &filename, &startstr,
481 &supplied_flags, &dont_inherit))
Guido van Rossum5b722181993-03-30 17:46:03 +0000482 return NULL;
Tim Peters6cd6a822001-08-17 22:11:27 +0000483
Just van Rossum3aaf42c2003-02-10 08:21:10 +0000484 cf.cf_flags = supplied_flags;
485
Georg Brandlfc8eef32008-03-28 12:11:56 +0000486 if (supplied_flags &
487 ~(PyCF_MASK | PyCF_MASK_OBSOLETE | PyCF_DONT_IMPLY_DEDENT | PyCF_ONLY_AST))
488 {
489 PyErr_SetString(PyExc_ValueError,
490 "compile(): unrecognised flags");
491 return NULL;
492 }
493 /* XXX Warn if (supplied_flags & PyCF_MASK_OBSOLETE) != 0? */
494
495 if (!dont_inherit) {
496 PyEval_MergeCompilerFlags(&cf);
497 }
498
Georg Brandlf2bfd542008-03-29 13:24:23 +0000499 if (strcmp(startstr, "exec") == 0)
500 mode = 0;
501 else if (strcmp(startstr, "eval") == 0)
502 mode = 1;
503 else if (strcmp(startstr, "single") == 0)
504 mode = 2;
505 else {
506 PyErr_SetString(PyExc_ValueError,
507 "compile() arg 3 must be 'exec', 'eval' or 'single'");
508 return NULL;
509 }
510
Georg Brandlfc8eef32008-03-28 12:11:56 +0000511 if (PyAST_Check(cmd)) {
512 if (supplied_flags & PyCF_ONLY_AST) {
513 Py_INCREF(cmd);
514 result = cmd;
515 }
516 else {
517 PyArena *arena;
518 mod_ty mod;
519
520 arena = PyArena_New();
Georg Brandlf2bfd542008-03-29 13:24:23 +0000521 mod = PyAST_obj2mod(cmd, arena, mode);
Georg Brandlfc8eef32008-03-28 12:11:56 +0000522 if (mod == NULL) {
523 PyArena_Free(arena);
524 return NULL;
525 }
526 result = (PyObject*)PyAST_Compile(mod, filename,
527 &cf, arena);
528 PyArena_Free(arena);
529 }
530 return result;
531 }
532
Just van Rossum3aaf42c2003-02-10 08:21:10 +0000533#ifdef Py_USING_UNICODE
534 if (PyUnicode_Check(cmd)) {
535 tmp = PyUnicode_AsUTF8String(cmd);
536 if (tmp == NULL)
537 return NULL;
538 cmd = tmp;
539 cf.cf_flags |= PyCF_SOURCE_IS_UTF8;
540 }
541#endif
Tim Peters6cd6a822001-08-17 22:11:27 +0000542
Georg Brandlfc8eef32008-03-28 12:11:56 +0000543 if (PyObject_AsReadBuffer(cmd, (const void **)&str, &length))
Neal Norwitz3a9a3e72005-11-27 20:38:31 +0000544 goto cleanup;
Georg Brandlfc8eef32008-03-28 12:11:56 +0000545 if ((size_t)length != strlen(str)) {
546 PyErr_SetString(PyExc_TypeError,
547 "compile() expected string without null bytes");
548 goto cleanup;
Tim Peters6cd6a822001-08-17 22:11:27 +0000549 }
Georg Brandlf2bfd542008-03-29 13:24:23 +0000550 result = Py_CompileStringFlags(str, filename, start[mode], &cf);
Neal Norwitz3a9a3e72005-11-27 20:38:31 +0000551cleanup:
Just van Rossum3aaf42c2003-02-10 08:21:10 +0000552 Py_XDECREF(tmp);
553 return result;
Guido van Rossum5b722181993-03-30 17:46:03 +0000554}
555
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000556PyDoc_STRVAR(compile_doc,
Tim Peters6cd6a822001-08-17 22:11:27 +0000557"compile(source, filename, mode[, flags[, dont_inherit]]) -> code object\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000558\n\
559Compile the source string (a Python module, statement or expression)\n\
560into a code object that can be executed by the exec statement or eval().\n\
561The filename will be used for run-time error messages.\n\
562The mode must be 'exec' to compile a module, 'single' to compile a\n\
Tim Peters6cd6a822001-08-17 22:11:27 +0000563single (interactive) statement, or 'eval' to compile an expression.\n\
564The flags argument, if present, controls which future statements influence\n\
565the compilation of the code.\n\
566The dont_inherit argument, if non-zero, stops the compilation inheriting\n\
567the effects of any future statements in effect in the code calling\n\
568compile; if absent or zero these statements do influence the compilation,\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000569in addition to any features explicitly specified.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000570
Guido van Rossum79f25d91997-04-29 20:08:16 +0000571static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000572builtin_dir(PyObject *self, PyObject *args)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000573{
Tim Peters5d2b77c2001-09-03 05:47:38 +0000574 PyObject *arg = NULL;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000575
Raymond Hettingerea3fdf42002-12-29 16:33:45 +0000576 if (!PyArg_UnpackTuple(args, "dir", 0, 1, &arg))
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000577 return NULL;
Tim Peters7eea37e2001-09-04 22:08:56 +0000578 return PyObject_Dir(arg);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000579}
580
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000581PyDoc_STRVAR(dir_doc,
Tim Peters5d2b77c2001-09-03 05:47:38 +0000582"dir([object]) -> list of strings\n"
583"\n"
Georg Brandl871f1bc2007-03-12 13:17:36 +0000584"If called without an argument, return the names in the current scope.\n"
585"Else, return an alphabetized list of names comprising (some of) the attributes\n"
586"of the given object, and of attributes reachable from it.\n"
587"If the object supplies a method named __dir__, it will be used; otherwise\n"
588"the default dir() logic is used and returns:\n"
589" for a module object: the module's attributes.\n"
590" for a class object: its attributes, and recursively the attributes\n"
591" of its bases.\n"
Georg Brandl3bb15672007-03-13 07:23:16 +0000592" for any other object: its attributes, its class's attributes, and\n"
Georg Brandl871f1bc2007-03-12 13:17:36 +0000593" recursively the attributes of its class's base classes.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000594
Guido van Rossum79f25d91997-04-29 20:08:16 +0000595static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000596builtin_divmod(PyObject *self, PyObject *args)
Guido van Rossum6a00cd81995-01-07 12:39:01 +0000597{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000598 PyObject *v, *w;
Guido van Rossum6a00cd81995-01-07 12:39:01 +0000599
Raymond Hettingerea3fdf42002-12-29 16:33:45 +0000600 if (!PyArg_UnpackTuple(args, "divmod", 2, 2, &v, &w))
Guido van Rossum6a00cd81995-01-07 12:39:01 +0000601 return NULL;
Guido van Rossum09df08a1998-05-22 00:51:39 +0000602 return PyNumber_Divmod(v, w);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000603}
604
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000605PyDoc_STRVAR(divmod_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000606"divmod(x, y) -> (div, mod)\n\
607\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000608Return the tuple ((x-x%y)/y, x%y). Invariant: div*y + mod == x.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000609
610
Guido van Rossum79f25d91997-04-29 20:08:16 +0000611static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000612builtin_eval(PyObject *self, PyObject *args)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000613{
Just van Rossum3aaf42c2003-02-10 08:21:10 +0000614 PyObject *cmd, *result, *tmp = NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000615 PyObject *globals = Py_None, *locals = Py_None;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000616 char *str;
Tim Peters9fa96be2001-08-17 23:04:59 +0000617 PyCompilerFlags cf;
Guido van Rossum590baa41993-11-30 13:40:46 +0000618
Raymond Hettinger214b1c32004-07-02 06:41:07 +0000619 if (!PyArg_UnpackTuple(args, "eval", 1, 3, &cmd, &globals, &locals))
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000620 return NULL;
Raymond Hettinger214b1c32004-07-02 06:41:07 +0000621 if (locals != Py_None && !PyMapping_Check(locals)) {
622 PyErr_SetString(PyExc_TypeError, "locals must be a mapping");
Raymond Hettinger513ffe82004-07-06 13:44:41 +0000623 return NULL;
Raymond Hettinger214b1c32004-07-02 06:41:07 +0000624 }
625 if (globals != Py_None && !PyDict_Check(globals)) {
Georg Brandl99d7e4e2005-08-31 22:21:15 +0000626 PyErr_SetString(PyExc_TypeError, PyMapping_Check(globals) ?
Raymond Hettinger214b1c32004-07-02 06:41:07 +0000627 "globals must be a real dict; try eval(expr, {}, mapping)"
628 : "globals must be a dict");
Raymond Hettinger513ffe82004-07-06 13:44:41 +0000629 return NULL;
Raymond Hettinger214b1c32004-07-02 06:41:07 +0000630 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000631 if (globals == Py_None) {
632 globals = PyEval_GetGlobals();
633 if (locals == Py_None)
634 locals = PyEval_GetLocals();
Guido van Rossum6135a871995-01-09 17:53:26 +0000635 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000636 else if (locals == Py_None)
Guido van Rossum6135a871995-01-09 17:53:26 +0000637 locals = globals;
Tim Peters9fa96be2001-08-17 23:04:59 +0000638
Georg Brandl77c85e62005-09-15 10:46:13 +0000639 if (globals == NULL || locals == NULL) {
640 PyErr_SetString(PyExc_TypeError,
641 "eval must be given globals and locals "
642 "when called without a frame");
643 return NULL;
644 }
645
Guido van Rossum79f25d91997-04-29 20:08:16 +0000646 if (PyDict_GetItemString(globals, "__builtins__") == NULL) {
647 if (PyDict_SetItemString(globals, "__builtins__",
648 PyEval_GetBuiltins()) != 0)
Guido van Rossum6135a871995-01-09 17:53:26 +0000649 return NULL;
650 }
Tim Peters9fa96be2001-08-17 23:04:59 +0000651
Jeremy Hylton15c1c4f2001-07-30 21:50:55 +0000652 if (PyCode_Check(cmd)) {
Jeremy Hylton733c8932001-12-13 19:51:56 +0000653 if (PyCode_GetNumFree((PyCodeObject *)cmd) > 0) {
Jeremy Hylton15c1c4f2001-07-30 21:50:55 +0000654 PyErr_SetString(PyExc_TypeError,
655 "code object passed to eval() may not contain free variables");
656 return NULL;
657 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000658 return PyEval_EvalCode((PyCodeObject *) cmd, globals, locals);
Jeremy Hylton15c1c4f2001-07-30 21:50:55 +0000659 }
Tim Peters9fa96be2001-08-17 23:04:59 +0000660
Marc-André Lemburgd1ba4432000-09-19 21:04:18 +0000661 if (!PyString_Check(cmd) &&
662 !PyUnicode_Check(cmd)) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000663 PyErr_SetString(PyExc_TypeError,
Fred Drake661ea262000-10-24 19:57:45 +0000664 "eval() arg 1 must be a string or code object");
Guido van Rossum94390a41992-08-14 15:14:30 +0000665 return NULL;
666 }
Just van Rossum3aaf42c2003-02-10 08:21:10 +0000667 cf.cf_flags = 0;
668
669#ifdef Py_USING_UNICODE
670 if (PyUnicode_Check(cmd)) {
671 tmp = PyUnicode_AsUTF8String(cmd);
672 if (tmp == NULL)
673 return NULL;
674 cmd = tmp;
675 cf.cf_flags |= PyCF_SOURCE_IS_UTF8;
676 }
677#endif
Neal Norwitz3a9a3e72005-11-27 20:38:31 +0000678 if (PyString_AsStringAndSize(cmd, &str, NULL)) {
679 Py_XDECREF(tmp);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000680 return NULL;
Neal Norwitz3a9a3e72005-11-27 20:38:31 +0000681 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000682 while (*str == ' ' || *str == '\t')
683 str++;
Tim Peters9fa96be2001-08-17 23:04:59 +0000684
Tim Peters9fa96be2001-08-17 23:04:59 +0000685 (void)PyEval_MergeCompilerFlags(&cf);
Just van Rossum3aaf42c2003-02-10 08:21:10 +0000686 result = PyRun_StringFlags(str, Py_eval_input, globals, locals, &cf);
687 Py_XDECREF(tmp);
688 return result;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000689}
690
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000691PyDoc_STRVAR(eval_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000692"eval(source[, globals[, locals]]) -> value\n\
693\n\
694Evaluate the source in the context of globals and locals.\n\
695The source may be a string representing a Python expression\n\
696or a code object as returned by compile().\n\
Neal Norwitz477ca1c2006-09-05 02:25:41 +0000697The globals must be a dictionary and locals can be any mapping,\n\
Raymond Hettinger214b1c32004-07-02 06:41:07 +0000698defaulting to the current globals and locals.\n\
699If only globals is given, locals defaults to it.\n");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000700
701
Guido van Rossum79f25d91997-04-29 20:08:16 +0000702static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000703builtin_execfile(PyObject *self, PyObject *args)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000704{
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000705 char *filename;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000706 PyObject *globals = Py_None, *locals = Py_None;
707 PyObject *res;
Guido van Rossumb3a639e2001-09-05 13:37:47 +0000708 FILE* fp = NULL;
Tim Peters5ba58662001-07-16 02:29:45 +0000709 PyCompilerFlags cf;
Martin v. Löwis6b3a2c42001-08-08 05:30:36 +0000710 int exists;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000711
Neal Norwitzdf25efe2007-05-23 06:58:36 +0000712 if (Py_Py3kWarningFlag &&
713 PyErr_Warn(PyExc_DeprecationWarning,
Georg Brandld5b635f2008-03-25 08:29:14 +0000714 "execfile() not supported in 3.x; use exec()") < 0)
Neal Norwitzdf25efe2007-05-23 06:58:36 +0000715 return NULL;
716
Raymond Hettinger66bd2332004-08-02 08:30:07 +0000717 if (!PyArg_ParseTuple(args, "s|O!O:execfile",
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000718 &filename,
Guido van Rossum79f25d91997-04-29 20:08:16 +0000719 &PyDict_Type, &globals,
Raymond Hettinger66bd2332004-08-02 08:30:07 +0000720 &locals))
Guido van Rossum0f61f8a1992-02-25 18:55:05 +0000721 return NULL;
Raymond Hettinger66bd2332004-08-02 08:30:07 +0000722 if (locals != Py_None && !PyMapping_Check(locals)) {
723 PyErr_SetString(PyExc_TypeError, "locals must be a mapping");
724 return NULL;
725 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000726 if (globals == Py_None) {
727 globals = PyEval_GetGlobals();
728 if (locals == Py_None)
729 locals = PyEval_GetLocals();
Guido van Rossum6135a871995-01-09 17:53:26 +0000730 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000731 else if (locals == Py_None)
Guido van Rossum6135a871995-01-09 17:53:26 +0000732 locals = globals;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000733 if (PyDict_GetItemString(globals, "__builtins__") == NULL) {
734 if (PyDict_SetItemString(globals, "__builtins__",
735 PyEval_GetBuiltins()) != 0)
Guido van Rossum6135a871995-01-09 17:53:26 +0000736 return NULL;
737 }
Martin v. Löwis6b3a2c42001-08-08 05:30:36 +0000738
739 exists = 0;
740 /* Test for existence or directory. */
Martin v. Löwis3484a182002-03-09 12:07:51 +0000741#if defined(PLAN9)
742 {
743 Dir *d;
744
745 if ((d = dirstat(filename))!=nil) {
746 if(d->mode & DMDIR)
747 werrstr("is a directory");
748 else
749 exists = 1;
750 free(d);
751 }
Martin v. Löwis6b3a2c42001-08-08 05:30:36 +0000752 }
Martin v. Löwis3484a182002-03-09 12:07:51 +0000753#elif defined(RISCOS)
Guido van Rossume2ae77b2001-10-24 20:42:55 +0000754 if (object_exists(filename)) {
755 if (isdir(filename))
756 errno = EISDIR;
757 else
758 exists = 1;
759 }
Martin v. Löwis3484a182002-03-09 12:07:51 +0000760#else /* standard Posix */
761 {
762 struct stat s;
763 if (stat(filename, &s) == 0) {
764 if (S_ISDIR(s.st_mode))
Andrew MacIntyreda4d6cb2004-03-29 11:53:38 +0000765# if defined(PYOS_OS2) && defined(PYCC_VACPP)
Martin v. Löwis3484a182002-03-09 12:07:51 +0000766 errno = EOS2ERR;
767# else
768 errno = EISDIR;
769# endif
770 else
771 exists = 1;
772 }
773 }
774#endif
Martin v. Löwis6b3a2c42001-08-08 05:30:36 +0000775
776 if (exists) {
777 Py_BEGIN_ALLOW_THREADS
Jack Jansen7b8c7542002-04-14 20:12:41 +0000778 fp = fopen(filename, "r" PY_STDIOTEXTMODE);
Martin v. Löwis6b3a2c42001-08-08 05:30:36 +0000779 Py_END_ALLOW_THREADS
780
781 if (fp == NULL) {
782 exists = 0;
783 }
784 }
785
786 if (!exists) {
Peter Schneider-Kamp4c013422002-08-27 16:58:00 +0000787 PyErr_SetFromErrnoWithFilename(PyExc_IOError, filename);
Guido van Rossum0f61f8a1992-02-25 18:55:05 +0000788 return NULL;
789 }
Tim Peters5ba58662001-07-16 02:29:45 +0000790 cf.cf_flags = 0;
791 if (PyEval_MergeCompilerFlags(&cf))
Tim Peters748b8bb2001-04-28 08:20:22 +0000792 res = PyRun_FileExFlags(fp, filename, Py_file_input, globals,
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000793 locals, 1, &cf);
Tim Peters5ba58662001-07-16 02:29:45 +0000794 else
Tim Peters748b8bb2001-04-28 08:20:22 +0000795 res = PyRun_FileEx(fp, filename, Py_file_input, globals,
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000796 locals, 1);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000797 return res;
Guido van Rossum0f61f8a1992-02-25 18:55:05 +0000798}
799
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000800PyDoc_STRVAR(execfile_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000801"execfile(filename[, globals[, locals]])\n\
802\n\
803Read and execute a Python script from a file.\n\
804The globals and locals are dictionaries, defaulting to the current\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000805globals and locals. If only globals is given, locals defaults to it.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000806
807
Guido van Rossum79f25d91997-04-29 20:08:16 +0000808static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000809builtin_getattr(PyObject *self, PyObject *args)
Guido van Rossum33894be1992-01-27 16:53:09 +0000810{
Guido van Rossum950ff291998-06-29 13:38:57 +0000811 PyObject *v, *result, *dflt = NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000812 PyObject *name;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000813
Raymond Hettingerea3fdf42002-12-29 16:33:45 +0000814 if (!PyArg_UnpackTuple(args, "getattr", 2, 3, &v, &name, &dflt))
Guido van Rossum33894be1992-01-27 16:53:09 +0000815 return NULL;
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000816#ifdef Py_USING_UNICODE
Jeremy Hylton0eb11152001-07-30 22:39:31 +0000817 if (PyUnicode_Check(name)) {
818 name = _PyUnicode_AsDefaultEncodedString(name, NULL);
819 if (name == NULL)
820 return NULL;
821 }
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000822#endif
Jeremy Hylton0eb11152001-07-30 22:39:31 +0000823
824 if (!PyString_Check(name)) {
825 PyErr_SetString(PyExc_TypeError,
Alex Martellia9b9c9f2003-04-23 13:34:35 +0000826 "getattr(): attribute name must be string");
Jeremy Hylton0eb11152001-07-30 22:39:31 +0000827 return NULL;
828 }
Guido van Rossum950ff291998-06-29 13:38:57 +0000829 result = PyObject_GetAttr(v, name);
Guido van Rossumd8923572001-10-16 21:31:32 +0000830 if (result == NULL && dflt != NULL &&
831 PyErr_ExceptionMatches(PyExc_AttributeError))
832 {
Guido van Rossum950ff291998-06-29 13:38:57 +0000833 PyErr_Clear();
834 Py_INCREF(dflt);
835 result = dflt;
836 }
837 return result;
Guido van Rossum9bfef441993-03-29 10:43:31 +0000838}
839
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000840PyDoc_STRVAR(getattr_doc,
Guido van Rossum950ff291998-06-29 13:38:57 +0000841"getattr(object, name[, default]) -> value\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000842\n\
Guido van Rossum950ff291998-06-29 13:38:57 +0000843Get a named attribute from an object; getattr(x, 'y') is equivalent to x.y.\n\
844When a default argument is given, it is returned when the attribute doesn't\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000845exist; without it, an exception is raised in that case.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000846
847
Guido van Rossum79f25d91997-04-29 20:08:16 +0000848static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000849builtin_globals(PyObject *self)
Guido van Rossum872537c1995-07-07 22:43:42 +0000850{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000851 PyObject *d;
Guido van Rossum872537c1995-07-07 22:43:42 +0000852
Guido van Rossum79f25d91997-04-29 20:08:16 +0000853 d = PyEval_GetGlobals();
Neal Norwitz43bd4db2006-08-12 01:46:42 +0000854 Py_XINCREF(d);
Guido van Rossum872537c1995-07-07 22:43:42 +0000855 return d;
856}
857
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000858PyDoc_STRVAR(globals_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000859"globals() -> dictionary\n\
860\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000861Return the dictionary containing the current scope's global variables.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000862
863
Guido van Rossum79f25d91997-04-29 20:08:16 +0000864static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000865builtin_hasattr(PyObject *self, PyObject *args)
Guido van Rossum9bfef441993-03-29 10:43:31 +0000866{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000867 PyObject *v;
868 PyObject *name;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000869
Raymond Hettingerea3fdf42002-12-29 16:33:45 +0000870 if (!PyArg_UnpackTuple(args, "hasattr", 2, 2, &v, &name))
Guido van Rossum9bfef441993-03-29 10:43:31 +0000871 return NULL;
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000872#ifdef Py_USING_UNICODE
Jeremy Hylton302b54a2001-07-30 22:45:19 +0000873 if (PyUnicode_Check(name)) {
874 name = _PyUnicode_AsDefaultEncodedString(name, NULL);
875 if (name == NULL)
876 return NULL;
877 }
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000878#endif
Jeremy Hylton302b54a2001-07-30 22:45:19 +0000879
880 if (!PyString_Check(name)) {
881 PyErr_SetString(PyExc_TypeError,
Alex Martellia9b9c9f2003-04-23 13:34:35 +0000882 "hasattr(): attribute name must be string");
Jeremy Hylton302b54a2001-07-30 22:45:19 +0000883 return NULL;
884 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000885 v = PyObject_GetAttr(v, name);
Guido van Rossum9bfef441993-03-29 10:43:31 +0000886 if (v == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000887 PyErr_Clear();
Guido van Rossum09df08a1998-05-22 00:51:39 +0000888 Py_INCREF(Py_False);
889 return Py_False;
Guido van Rossum9bfef441993-03-29 10:43:31 +0000890 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000891 Py_DECREF(v);
Guido van Rossum09df08a1998-05-22 00:51:39 +0000892 Py_INCREF(Py_True);
893 return Py_True;
Guido van Rossum33894be1992-01-27 16:53:09 +0000894}
895
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000896PyDoc_STRVAR(hasattr_doc,
Guido van Rossum77f6a652002-04-03 22:41:51 +0000897"hasattr(object, name) -> bool\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000898\n\
899Return whether the object has an attribute with the given name.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000900(This is done by calling getattr(object, name) and catching exceptions.)");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000901
902
Guido van Rossum79f25d91997-04-29 20:08:16 +0000903static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000904builtin_id(PyObject *self, PyObject *v)
Guido van Rossum5b722181993-03-30 17:46:03 +0000905{
Guido van Rossum106f2da2000-06-28 21:12:25 +0000906 return PyLong_FromVoidPtr(v);
Guido van Rossum5b722181993-03-30 17:46:03 +0000907}
908
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000909PyDoc_STRVAR(id_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000910"id(object) -> integer\n\
911\n\
912Return the identity of an object. This is guaranteed to be unique among\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000913simultaneously existing objects. (Hint: it's the object's memory address.)");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000914
915
Guido van Rossum79f25d91997-04-29 20:08:16 +0000916static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000917builtin_map(PyObject *self, PyObject *args)
Guido van Rossum12d12c51993-10-26 17:58:25 +0000918{
919 typedef struct {
Tim Peters4e9afdc2001-05-03 23:54:49 +0000920 PyObject *it; /* the iterator object */
921 int saw_StopIteration; /* bool: did the iterator end? */
Guido van Rossum12d12c51993-10-26 17:58:25 +0000922 } sequence;
923
Guido van Rossum79f25d91997-04-29 20:08:16 +0000924 PyObject *func, *result;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000925 sequence *seqs = NULL, *sqp;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000926 Py_ssize_t n, len;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000927 register int i, j;
928
Guido van Rossum79f25d91997-04-29 20:08:16 +0000929 n = PyTuple_Size(args);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000930 if (n < 2) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000931 PyErr_SetString(PyExc_TypeError,
932 "map() requires at least two args");
Guido van Rossum12d12c51993-10-26 17:58:25 +0000933 return NULL;
934 }
935
Guido van Rossum79f25d91997-04-29 20:08:16 +0000936 func = PyTuple_GetItem(args, 0);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000937 n--;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000938
Neal Norwitz53152a12008-02-24 02:20:25 +0000939 if (func == Py_None) {
940 if (Py_Py3kWarningFlag &&
941 PyErr_Warn(PyExc_DeprecationWarning,
Georg Brandld5b635f2008-03-25 08:29:14 +0000942 "map(None, ...) not supported in 3.x; "
943 "use list(...)") < 0)
Neal Norwitz53152a12008-02-24 02:20:25 +0000944 return NULL;
945 if (n == 1) {
946 /* map(None, S) is the same as list(S). */
947 return PySequence_List(PyTuple_GetItem(args, 1));
948 }
Guido van Rossumfa4ac711998-07-10 17:37:30 +0000949 }
950
Tim Peters4e9afdc2001-05-03 23:54:49 +0000951 /* Get space for sequence descriptors. Must NULL out the iterator
952 * pointers so that jumping to Fail_2 later doesn't see trash.
953 */
Guido van Rossum79f25d91997-04-29 20:08:16 +0000954 if ((seqs = PyMem_NEW(sequence, n)) == NULL) {
955 PyErr_NoMemory();
Tim Peters4e9afdc2001-05-03 23:54:49 +0000956 return NULL;
957 }
958 for (i = 0; i < n; ++i) {
959 seqs[i].it = (PyObject*)NULL;
960 seqs[i].saw_StopIteration = 0;
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000961 }
Guido van Rossum12d12c51993-10-26 17:58:25 +0000962
Tim Peters4e9afdc2001-05-03 23:54:49 +0000963 /* Do a first pass to obtain iterators for the arguments, and set len
964 * to the largest of their lengths.
Tim Peters748b8bb2001-04-28 08:20:22 +0000965 */
Tim Peters4e9afdc2001-05-03 23:54:49 +0000966 len = 0;
967 for (i = 0, sqp = seqs; i < n; ++i, ++sqp) {
968 PyObject *curseq;
Martin v. Löwisd96ee902006-02-16 14:37:16 +0000969 Py_ssize_t curlen;
Guido van Rossumad991772001-01-12 16:03:05 +0000970
Tim Peters4e9afdc2001-05-03 23:54:49 +0000971 /* Get iterator. */
972 curseq = PyTuple_GetItem(args, i+1);
973 sqp->it = PyObject_GetIter(curseq);
974 if (sqp->it == NULL) {
Guido van Rossum12d12c51993-10-26 17:58:25 +0000975 static char errmsg[] =
Tim Peters4e9afdc2001-05-03 23:54:49 +0000976 "argument %d to map() must support iteration";
Guido van Rossum15e33a41997-04-30 19:00:27 +0000977 char errbuf[sizeof(errmsg) + 25];
Jeremy Hylton518ab1c2001-11-28 20:42:20 +0000978 PyOS_snprintf(errbuf, sizeof(errbuf), errmsg, i+2);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000979 PyErr_SetString(PyExc_TypeError, errbuf);
Guido van Rossum12d12c51993-10-26 17:58:25 +0000980 goto Fail_2;
981 }
982
Tim Peters4e9afdc2001-05-03 23:54:49 +0000983 /* Update len. */
Raymond Hettinger4e2f7142007-12-06 00:56:53 +0000984 curlen = _PyObject_LengthHint(curseq, 8);
Raymond Hettinger77f3c872004-01-04 08:54:44 +0000985 if (curlen > len)
986 len = curlen;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000987 }
988
Tim Peters4e9afdc2001-05-03 23:54:49 +0000989 /* Get space for the result list. */
Guido van Rossum79f25d91997-04-29 20:08:16 +0000990 if ((result = (PyObject *) PyList_New(len)) == NULL)
Guido van Rossum12d12c51993-10-26 17:58:25 +0000991 goto Fail_2;
992
Tim Peters4e9afdc2001-05-03 23:54:49 +0000993 /* Iterate over the sequences until all have stopped. */
Guido van Rossum2d951851994-08-29 12:52:16 +0000994 for (i = 0; ; ++i) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000995 PyObject *alist, *item=NULL, *value;
Tim Peters4e9afdc2001-05-03 23:54:49 +0000996 int numactive = 0;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000997
Guido van Rossum79f25d91997-04-29 20:08:16 +0000998 if (func == Py_None && n == 1)
Guido van Rossum32120311995-07-10 13:52:21 +0000999 alist = NULL;
Tim Peters4e9afdc2001-05-03 23:54:49 +00001000 else if ((alist = PyTuple_New(n)) == NULL)
1001 goto Fail_1;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001002
1003 for (j = 0, sqp = seqs; j < n; ++j, ++sqp) {
Tim Peters4e9afdc2001-05-03 23:54:49 +00001004 if (sqp->saw_StopIteration) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001005 Py_INCREF(Py_None);
1006 item = Py_None;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001007 }
Guido van Rossum12d12c51993-10-26 17:58:25 +00001008 else {
Tim Peters4e9afdc2001-05-03 23:54:49 +00001009 item = PyIter_Next(sqp->it);
1010 if (item)
1011 ++numactive;
1012 else {
Tim Peters4e9afdc2001-05-03 23:54:49 +00001013 if (PyErr_Occurred()) {
Tim Petersf4848da2001-05-05 00:14:56 +00001014 Py_XDECREF(alist);
1015 goto Fail_1;
Guido van Rossum2d951851994-08-29 12:52:16 +00001016 }
Tim Peters4e9afdc2001-05-03 23:54:49 +00001017 Py_INCREF(Py_None);
1018 item = Py_None;
1019 sqp->saw_StopIteration = 1;
Guido van Rossum2d951851994-08-29 12:52:16 +00001020 }
Guido van Rossum12d12c51993-10-26 17:58:25 +00001021 }
Tim Peters4e9afdc2001-05-03 23:54:49 +00001022 if (alist)
1023 PyTuple_SET_ITEM(alist, j, item);
1024 else
Guido van Rossum2d951851994-08-29 12:52:16 +00001025 break;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001026 }
1027
Guido van Rossum32120311995-07-10 13:52:21 +00001028 if (!alist)
1029 alist = item;
Guido van Rossum2d951851994-08-29 12:52:16 +00001030
Tim Peters4e9afdc2001-05-03 23:54:49 +00001031 if (numactive == 0) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001032 Py_DECREF(alist);
Guido van Rossum2d951851994-08-29 12:52:16 +00001033 break;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001034 }
Guido van Rossum2d951851994-08-29 12:52:16 +00001035
Guido van Rossum79f25d91997-04-29 20:08:16 +00001036 if (func == Py_None)
Guido van Rossum32120311995-07-10 13:52:21 +00001037 value = alist;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001038 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001039 value = PyEval_CallObject(func, alist);
1040 Py_DECREF(alist);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00001041 if (value == NULL)
1042 goto Fail_1;
Guido van Rossum2d951851994-08-29 12:52:16 +00001043 }
1044 if (i >= len) {
Barry Warsawfa77e091999-01-28 18:49:12 +00001045 int status = PyList_Append(result, value);
Barry Warsaw21332871999-01-28 04:21:35 +00001046 Py_DECREF(value);
Barry Warsawfa77e091999-01-28 18:49:12 +00001047 if (status < 0)
1048 goto Fail_1;
Guido van Rossum2d951851994-08-29 12:52:16 +00001049 }
Tim Peters4e9afdc2001-05-03 23:54:49 +00001050 else if (PyList_SetItem(result, i, value) < 0)
1051 goto Fail_1;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001052 }
1053
Guido van Rossumfa4ac711998-07-10 17:37:30 +00001054 if (i < len && PyList_SetSlice(result, i, len, NULL) < 0)
1055 goto Fail_1;
1056
Tim Peters4e9afdc2001-05-03 23:54:49 +00001057 goto Succeed;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001058
Guido van Rossum12d12c51993-10-26 17:58:25 +00001059Fail_1:
Guido van Rossum79f25d91997-04-29 20:08:16 +00001060 Py_DECREF(result);
Guido van Rossum12d12c51993-10-26 17:58:25 +00001061Fail_2:
Tim Peters4e9afdc2001-05-03 23:54:49 +00001062 result = NULL;
1063Succeed:
1064 assert(seqs);
1065 for (i = 0; i < n; ++i)
1066 Py_XDECREF(seqs[i].it);
1067 PyMem_DEL(seqs);
1068 return result;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001069}
1070
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001071PyDoc_STRVAR(map_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001072"map(function, sequence[, sequence, ...]) -> list\n\
1073\n\
1074Return a list of the results of applying the function to the items of\n\
1075the argument sequence(s). If more than one sequence is given, the\n\
1076function is called with an argument list consisting of the corresponding\n\
1077item of each sequence, substituting None for missing values when not all\n\
1078sequences have the same length. If the function is None, return a list of\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001079the items of the sequence (or a list of tuples if more than one sequence).");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001080
1081
Guido van Rossum79f25d91997-04-29 20:08:16 +00001082static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001083builtin_setattr(PyObject *self, PyObject *args)
Guido van Rossum33894be1992-01-27 16:53:09 +00001084{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001085 PyObject *v;
1086 PyObject *name;
1087 PyObject *value;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001088
Raymond Hettingerea3fdf42002-12-29 16:33:45 +00001089 if (!PyArg_UnpackTuple(args, "setattr", 3, 3, &v, &name, &value))
Guido van Rossum33894be1992-01-27 16:53:09 +00001090 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001091 if (PyObject_SetAttr(v, name, value) != 0)
Guido van Rossum33894be1992-01-27 16:53:09 +00001092 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001093 Py_INCREF(Py_None);
1094 return Py_None;
Guido van Rossum33894be1992-01-27 16:53:09 +00001095}
1096
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001097PyDoc_STRVAR(setattr_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001098"setattr(object, name, value)\n\
1099\n\
1100Set a named attribute on an object; setattr(x, 'y', v) is equivalent to\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001101``x.y = v''.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001102
1103
Guido van Rossum79f25d91997-04-29 20:08:16 +00001104static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001105builtin_delattr(PyObject *self, PyObject *args)
Guido van Rossum14144fc1994-08-29 12:53:40 +00001106{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001107 PyObject *v;
1108 PyObject *name;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001109
Raymond Hettingerea3fdf42002-12-29 16:33:45 +00001110 if (!PyArg_UnpackTuple(args, "delattr", 2, 2, &v, &name))
Guido van Rossum14144fc1994-08-29 12:53:40 +00001111 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001112 if (PyObject_SetAttr(v, name, (PyObject *)NULL) != 0)
Guido van Rossum14144fc1994-08-29 12:53:40 +00001113 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001114 Py_INCREF(Py_None);
1115 return Py_None;
Guido van Rossum14144fc1994-08-29 12:53:40 +00001116}
1117
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001118PyDoc_STRVAR(delattr_doc,
Guido van Rossumdf12a591998-11-23 22:13:04 +00001119"delattr(object, name)\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001120\n\
1121Delete a named attribute on an object; delattr(x, 'y') is equivalent to\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001122``del x.y''.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001123
1124
Guido van Rossum79f25d91997-04-29 20:08:16 +00001125static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001126builtin_hash(PyObject *self, PyObject *v)
Guido van Rossum9bfef441993-03-29 10:43:31 +00001127{
Guido van Rossum9bfef441993-03-29 10:43:31 +00001128 long x;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001129
Guido van Rossum79f25d91997-04-29 20:08:16 +00001130 x = PyObject_Hash(v);
Guido van Rossum9bfef441993-03-29 10:43:31 +00001131 if (x == -1)
1132 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001133 return PyInt_FromLong(x);
Guido van Rossum9bfef441993-03-29 10:43:31 +00001134}
1135
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001136PyDoc_STRVAR(hash_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001137"hash(object) -> integer\n\
1138\n\
1139Return a hash value for the object. Two objects with the same value have\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001140the same hash value. The reverse is not necessarily true, but likely.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001141
1142
Guido van Rossum79f25d91997-04-29 20:08:16 +00001143static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001144builtin_hex(PyObject *self, PyObject *v)
Guido van Rossum006bcd41991-10-24 14:54:44 +00001145{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001146 PyNumberMethods *nb;
Neil Schemenauer3a313e32004-07-19 16:29:17 +00001147 PyObject *res;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001148
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001149 if ((nb = v->ob_type->tp_as_number) == NULL ||
Guido van Rossum1899c2e1992-09-12 11:09:23 +00001150 nb->nb_hex == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001151 PyErr_SetString(PyExc_TypeError,
Guido van Rossum1899c2e1992-09-12 11:09:23 +00001152 "hex() argument can't be converted to hex");
1153 return NULL;
Guido van Rossum006bcd41991-10-24 14:54:44 +00001154 }
Neil Schemenauer3a313e32004-07-19 16:29:17 +00001155 res = (*nb->nb_hex)(v);
1156 if (res && !PyString_Check(res)) {
1157 PyErr_Format(PyExc_TypeError,
1158 "__hex__ returned non-string (type %.200s)",
1159 res->ob_type->tp_name);
1160 Py_DECREF(res);
1161 return NULL;
1162 }
1163 return res;
Guido van Rossum006bcd41991-10-24 14:54:44 +00001164}
1165
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001166PyDoc_STRVAR(hex_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001167"hex(number) -> string\n\
1168\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001169Return the hexadecimal representation of an integer or long integer.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001170
1171
Tim Petersdbd9ba62000-07-09 03:09:57 +00001172static PyObject *builtin_raw_input(PyObject *, PyObject *);
Guido van Rossum3165fe61992-09-25 21:59:05 +00001173
Guido van Rossum79f25d91997-04-29 20:08:16 +00001174static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001175builtin_input(PyObject *self, PyObject *args)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001176{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001177 PyObject *line;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001178 char *str;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001179 PyObject *res;
1180 PyObject *globals, *locals;
Hye-Shik Changff83c2b2004-02-02 13:39:01 +00001181 PyCompilerFlags cf;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001182
1183 line = builtin_raw_input(self, args);
Guido van Rossum3165fe61992-09-25 21:59:05 +00001184 if (line == NULL)
1185 return line;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001186 if (!PyArg_Parse(line, "s;embedded '\\0' in input line", &str))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001187 return NULL;
1188 while (*str == ' ' || *str == '\t')
1189 str++;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001190 globals = PyEval_GetGlobals();
1191 locals = PyEval_GetLocals();
1192 if (PyDict_GetItemString(globals, "__builtins__") == NULL) {
1193 if (PyDict_SetItemString(globals, "__builtins__",
1194 PyEval_GetBuiltins()) != 0)
Guido van Rossum6135a871995-01-09 17:53:26 +00001195 return NULL;
1196 }
Hye-Shik Changff83c2b2004-02-02 13:39:01 +00001197 cf.cf_flags = 0;
1198 PyEval_MergeCompilerFlags(&cf);
1199 res = PyRun_StringFlags(str, Py_eval_input, globals, locals, &cf);
Guido van Rossum79f25d91997-04-29 20:08:16 +00001200 Py_DECREF(line);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001201 return res;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001202}
1203
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001204PyDoc_STRVAR(input_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001205"input([prompt]) -> value\n\
1206\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001207Equivalent to eval(raw_input(prompt)).");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001208
1209
Guido van Rossume8811f81997-02-14 15:48:05 +00001210static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001211builtin_intern(PyObject *self, PyObject *args)
Guido van Rossume8811f81997-02-14 15:48:05 +00001212{
1213 PyObject *s;
Guido van Rossum43713e52000-02-29 13:59:29 +00001214 if (!PyArg_ParseTuple(args, "S:intern", &s))
Guido van Rossume8811f81997-02-14 15:48:05 +00001215 return NULL;
Jeremy Hylton4c989dd2004-08-07 19:20:05 +00001216 if (!PyString_CheckExact(s)) {
1217 PyErr_SetString(PyExc_TypeError,
1218 "can't intern subclass of string");
1219 return NULL;
1220 }
Guido van Rossume8811f81997-02-14 15:48:05 +00001221 Py_INCREF(s);
1222 PyString_InternInPlace(&s);
1223 return s;
1224}
1225
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001226PyDoc_STRVAR(intern_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001227"intern(string) -> string\n\
1228\n\
1229``Intern'' the given string. This enters the string in the (global)\n\
1230table of interned strings whose purpose is to speed up dictionary lookups.\n\
1231Return the string itself or the previously interned string object with the\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001232same value.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001233
1234
Guido van Rossum79f25d91997-04-29 20:08:16 +00001235static PyObject *
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001236builtin_iter(PyObject *self, PyObject *args)
1237{
1238 PyObject *v, *w = NULL;
1239
Raymond Hettingerea3fdf42002-12-29 16:33:45 +00001240 if (!PyArg_UnpackTuple(args, "iter", 1, 2, &v, &w))
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001241 return NULL;
1242 if (w == NULL)
1243 return PyObject_GetIter(v);
1244 if (!PyCallable_Check(v)) {
1245 PyErr_SetString(PyExc_TypeError,
1246 "iter(v, w): v must be callable");
1247 return NULL;
1248 }
1249 return PyCallIter_New(v, w);
1250}
1251
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001252PyDoc_STRVAR(iter_doc,
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001253"iter(collection) -> iterator\n\
1254iter(callable, sentinel) -> iterator\n\
1255\n\
1256Get an iterator from an object. In the first form, the argument must\n\
1257supply its own iterator, or be a sequence.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001258In the second form, the callable is called until it returns the sentinel.");
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001259
1260
1261static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001262builtin_len(PyObject *self, PyObject *v)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001263{
Martin v. Löwis18e16552006-02-15 17:27:45 +00001264 Py_ssize_t res;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001265
Jeremy Hylton03657cf2000-07-12 13:05:33 +00001266 res = PyObject_Size(v);
Guido van Rossum8ea9f4d1998-06-29 22:26:50 +00001267 if (res < 0 && PyErr_Occurred())
1268 return NULL;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001269 return PyInt_FromSsize_t(res);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001270}
1271
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001272PyDoc_STRVAR(len_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001273"len(object) -> integer\n\
1274\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001275Return the number of items of a sequence or mapping.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001276
1277
Guido van Rossum79f25d91997-04-29 20:08:16 +00001278static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001279builtin_locals(PyObject *self)
Guido van Rossum872537c1995-07-07 22:43:42 +00001280{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001281 PyObject *d;
Guido van Rossum872537c1995-07-07 22:43:42 +00001282
Guido van Rossum79f25d91997-04-29 20:08:16 +00001283 d = PyEval_GetLocals();
Neal Norwitz43bd4db2006-08-12 01:46:42 +00001284 Py_XINCREF(d);
Guido van Rossum872537c1995-07-07 22:43:42 +00001285 return d;
1286}
1287
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001288PyDoc_STRVAR(locals_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001289"locals() -> dictionary\n\
1290\n\
Raymond Hettinger69bf8f32003-01-04 02:16:22 +00001291Update and return a dictionary containing the current scope's local variables.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001292
1293
Guido van Rossum79f25d91997-04-29 20:08:16 +00001294static PyObject *
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001295min_max(PyObject *args, PyObject *kwds, int op)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001296{
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001297 PyObject *v, *it, *item, *val, *maxitem, *maxval, *keyfunc=NULL;
Martin v. Löwisfd39ad42004-08-12 14:42:37 +00001298 const char *name = op == Py_LT ? "min" : "max";
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001299
Guido van Rossum79f25d91997-04-29 20:08:16 +00001300 if (PyTuple_Size(args) > 1)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001301 v = args;
Martin v. Löwisfd39ad42004-08-12 14:42:37 +00001302 else if (!PyArg_UnpackTuple(args, (char *)name, 1, 1, &v))
Guido van Rossum3f5da241990-12-20 15:06:42 +00001303 return NULL;
Tim Peters67d687a2002-04-29 21:27:32 +00001304
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001305 if (kwds != NULL && PyDict_Check(kwds) && PyDict_Size(kwds)) {
1306 keyfunc = PyDict_GetItemString(kwds, "key");
1307 if (PyDict_Size(kwds)!=1 || keyfunc == NULL) {
Georg Brandl99d7e4e2005-08-31 22:21:15 +00001308 PyErr_Format(PyExc_TypeError,
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001309 "%s() got an unexpected keyword argument", name);
1310 return NULL;
1311 }
Guido van Rossum1d9a9ea2008-01-23 20:19:01 +00001312 Py_INCREF(keyfunc);
Georg Brandl99d7e4e2005-08-31 22:21:15 +00001313 }
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001314
Tim Petersc3074532001-05-03 07:00:32 +00001315 it = PyObject_GetIter(v);
Guido van Rossum1d9a9ea2008-01-23 20:19:01 +00001316 if (it == NULL) {
1317 Py_XDECREF(keyfunc);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001318 return NULL;
Guido van Rossum1d9a9ea2008-01-23 20:19:01 +00001319 }
Tim Petersc3074532001-05-03 07:00:32 +00001320
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001321 maxitem = NULL; /* the result */
1322 maxval = NULL; /* the value associated with the result */
Brett Cannon9e635cf2004-12-07 00:25:35 +00001323 while (( item = PyIter_Next(it) )) {
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001324 /* get the value from the key function */
1325 if (keyfunc != NULL) {
1326 val = PyObject_CallFunctionObjArgs(keyfunc, item, NULL);
1327 if (val == NULL)
1328 goto Fail_it_item;
1329 }
1330 /* no key function; the value is the item */
1331 else {
1332 val = item;
1333 Py_INCREF(val);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001334 }
Tim Petersc3074532001-05-03 07:00:32 +00001335
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001336 /* maximum value and item are unset; set them */
1337 if (maxval == NULL) {
1338 maxitem = item;
1339 maxval = val;
1340 }
1341 /* maximum value and item are set; update them as necessary */
Guido van Rossum2d951851994-08-29 12:52:16 +00001342 else {
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001343 int cmp = PyObject_RichCompareBool(val, maxval, op);
1344 if (cmp < 0)
1345 goto Fail_it_item_and_val;
1346 else if (cmp > 0) {
1347 Py_DECREF(maxval);
1348 Py_DECREF(maxitem);
1349 maxval = val;
1350 maxitem = item;
Guido van Rossum53451b32001-01-17 15:47:24 +00001351 }
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001352 else {
1353 Py_DECREF(item);
1354 Py_DECREF(val);
Guido van Rossumc8b6df91997-05-23 00:06:51 +00001355 }
Guido van Rossum2d951851994-08-29 12:52:16 +00001356 }
Guido van Rossum3f5da241990-12-20 15:06:42 +00001357 }
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001358 if (PyErr_Occurred())
1359 goto Fail_it;
1360 if (maxval == NULL) {
Martin v. Löwisfd39ad42004-08-12 14:42:37 +00001361 PyErr_Format(PyExc_ValueError,
1362 "%s() arg is an empty sequence", name);
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001363 assert(maxitem == NULL);
1364 }
1365 else
1366 Py_DECREF(maxval);
Tim Petersc3074532001-05-03 07:00:32 +00001367 Py_DECREF(it);
Guido van Rossum1d9a9ea2008-01-23 20:19:01 +00001368 Py_XDECREF(keyfunc);
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001369 return maxitem;
1370
1371Fail_it_item_and_val:
1372 Py_DECREF(val);
1373Fail_it_item:
1374 Py_DECREF(item);
1375Fail_it:
1376 Py_XDECREF(maxval);
1377 Py_XDECREF(maxitem);
1378 Py_DECREF(it);
Guido van Rossum1d9a9ea2008-01-23 20:19:01 +00001379 Py_XDECREF(keyfunc);
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001380 return NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001381}
1382
Guido van Rossum79f25d91997-04-29 20:08:16 +00001383static PyObject *
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001384builtin_min(PyObject *self, PyObject *args, PyObject *kwds)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001385{
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001386 return min_max(args, kwds, Py_LT);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001387}
1388
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001389PyDoc_STRVAR(min_doc,
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001390"min(iterable[, key=func]) -> value\n\
1391min(a, b, c, ...[, key=func]) -> value\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001392\n\
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001393With a single iterable argument, return its smallest item.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001394With two or more arguments, return the smallest argument.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001395
1396
Guido van Rossum79f25d91997-04-29 20:08:16 +00001397static PyObject *
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001398builtin_max(PyObject *self, PyObject *args, PyObject *kwds)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001399{
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001400 return min_max(args, kwds, Py_GT);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001401}
1402
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001403PyDoc_STRVAR(max_doc,
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001404"max(iterable[, key=func]) -> value\n\
1405max(a, b, c, ...[, key=func]) -> value\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001406\n\
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001407With a single iterable argument, return its largest item.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001408With two or more arguments, return the largest argument.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001409
1410
Guido van Rossum79f25d91997-04-29 20:08:16 +00001411static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001412builtin_oct(PyObject *self, PyObject *v)
Guido van Rossum006bcd41991-10-24 14:54:44 +00001413{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001414 PyNumberMethods *nb;
Neil Schemenauer3a313e32004-07-19 16:29:17 +00001415 PyObject *res;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001416
Guido van Rossum1899c2e1992-09-12 11:09:23 +00001417 if (v == NULL || (nb = v->ob_type->tp_as_number) == NULL ||
1418 nb->nb_oct == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001419 PyErr_SetString(PyExc_TypeError,
Guido van Rossum1899c2e1992-09-12 11:09:23 +00001420 "oct() argument can't be converted to oct");
1421 return NULL;
Guido van Rossum006bcd41991-10-24 14:54:44 +00001422 }
Neil Schemenauer3a313e32004-07-19 16:29:17 +00001423 res = (*nb->nb_oct)(v);
1424 if (res && !PyString_Check(res)) {
1425 PyErr_Format(PyExc_TypeError,
1426 "__oct__ returned non-string (type %.200s)",
1427 res->ob_type->tp_name);
1428 Py_DECREF(res);
1429 return NULL;
1430 }
1431 return res;
Guido van Rossum006bcd41991-10-24 14:54:44 +00001432}
1433
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001434PyDoc_STRVAR(oct_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001435"oct(number) -> string\n\
1436\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001437Return the octal representation of an integer or long integer.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001438
1439
Guido van Rossum79f25d91997-04-29 20:08:16 +00001440static PyObject *
Neal Norwitzc4edb0e2006-05-02 04:43:14 +00001441builtin_open(PyObject *self, PyObject *args, PyObject *kwds)
1442{
1443 return PyObject_Call((PyObject*)&PyFile_Type, args, kwds);
1444}
1445
1446PyDoc_STRVAR(open_doc,
1447"open(name[, mode[, buffering]]) -> file object\n\
1448\n\
Skip Montanaro4e3ebe02007-12-08 14:37:43 +00001449Open a file using the file() type, returns a file object. This is the\n\
1450preferred way to open a file.");
Neal Norwitzc4edb0e2006-05-02 04:43:14 +00001451
1452
1453static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001454builtin_ord(PyObject *self, PyObject* obj)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001455{
Guido van Rossum09095f32000-03-10 23:00:52 +00001456 long ord;
Martin v. Löwisd96ee902006-02-16 14:37:16 +00001457 Py_ssize_t size;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001458
Jeremy Hylton394b54d2000-04-12 21:19:47 +00001459 if (PyString_Check(obj)) {
1460 size = PyString_GET_SIZE(obj);
Andrew M. Kuchling34c20cf2000-12-20 14:36:56 +00001461 if (size == 1) {
Jeremy Hylton394b54d2000-04-12 21:19:47 +00001462 ord = (long)((unsigned char)*PyString_AS_STRING(obj));
Andrew M. Kuchling34c20cf2000-12-20 14:36:56 +00001463 return PyInt_FromLong(ord);
1464 }
Christian Heimes1a6387e2008-03-26 12:49:49 +00001465 } else if (PyBytes_Check(obj)) {
1466 size = PyBytes_GET_SIZE(obj);
1467 if (size == 1) {
1468 ord = (long)((unsigned char)*PyBytes_AS_STRING(obj));
1469 return PyInt_FromLong(ord);
1470 }
1471
Martin v. Löwis339d0f72001-08-17 18:39:25 +00001472#ifdef Py_USING_UNICODE
Jeremy Hylton394b54d2000-04-12 21:19:47 +00001473 } else if (PyUnicode_Check(obj)) {
1474 size = PyUnicode_GET_SIZE(obj);
Andrew M. Kuchling34c20cf2000-12-20 14:36:56 +00001475 if (size == 1) {
Jeremy Hylton394b54d2000-04-12 21:19:47 +00001476 ord = (long)*PyUnicode_AS_UNICODE(obj);
Andrew M. Kuchling34c20cf2000-12-20 14:36:56 +00001477 return PyInt_FromLong(ord);
1478 }
Martin v. Löwis339d0f72001-08-17 18:39:25 +00001479#endif
Jeremy Hylton394b54d2000-04-12 21:19:47 +00001480 } else {
1481 PyErr_Format(PyExc_TypeError,
Andrew M. Kuchlingf07aad12000-12-23 14:11:28 +00001482 "ord() expected string of length 1, but " \
Jeremy Hylton394b54d2000-04-12 21:19:47 +00001483 "%.200s found", obj->ob_type->tp_name);
Guido van Rossum09095f32000-03-10 23:00:52 +00001484 return NULL;
1485 }
1486
Guido van Rossumad991772001-01-12 16:03:05 +00001487 PyErr_Format(PyExc_TypeError,
Andrew M. Kuchlingf07aad12000-12-23 14:11:28 +00001488 "ord() expected a character, "
Martin v. Löwisd96ee902006-02-16 14:37:16 +00001489 "but string of length %zd found",
Jeremy Hylton394b54d2000-04-12 21:19:47 +00001490 size);
1491 return NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001492}
1493
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001494PyDoc_STRVAR(ord_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001495"ord(c) -> integer\n\
1496\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001497Return the integer ordinal of a one-character string.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001498
1499
Guido van Rossum79f25d91997-04-29 20:08:16 +00001500static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001501builtin_pow(PyObject *self, PyObject *args)
Guido van Rossum6a00cd81995-01-07 12:39:01 +00001502{
Guido van Rossum09df08a1998-05-22 00:51:39 +00001503 PyObject *v, *w, *z = Py_None;
Guido van Rossum6a00cd81995-01-07 12:39:01 +00001504
Raymond Hettingerea3fdf42002-12-29 16:33:45 +00001505 if (!PyArg_UnpackTuple(args, "pow", 2, 3, &v, &w, &z))
Guido van Rossum6a00cd81995-01-07 12:39:01 +00001506 return NULL;
Guido van Rossum09df08a1998-05-22 00:51:39 +00001507 return PyNumber_Power(v, w, z);
Guido van Rossumd4905451991-05-05 20:00:36 +00001508}
1509
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001510PyDoc_STRVAR(pow_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001511"pow(x, y[, z]) -> number\n\
1512\n\
1513With two arguments, equivalent to x**y. With three arguments,\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001514equivalent to (x**y) % z, but may be more efficient (e.g. for longs).");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001515
1516
Eric Smith7c478942008-03-18 23:45:49 +00001517static PyObject *
1518builtin_print(PyObject *self, PyObject *args, PyObject *kwds)
1519{
1520 static char *kwlist[] = {"sep", "end", "file", 0};
1521 static PyObject *dummy_args;
1522 PyObject *sep = NULL, *end = NULL, *file = NULL;
1523 int i, err;
1524
1525 if (dummy_args == NULL) {
1526 if (!(dummy_args = PyTuple_New(0)))
1527 return NULL;
1528 }
1529 if (!PyArg_ParseTupleAndKeywords(dummy_args, kwds, "|OOO:print",
1530 kwlist, &sep, &end, &file))
1531 return NULL;
1532 if (file == NULL || file == Py_None) {
1533 file = PySys_GetObject("stdout");
1534 /* sys.stdout may be None when FILE* stdout isn't connected */
1535 if (file == Py_None)
1536 Py_RETURN_NONE;
1537 }
1538
1539 if (sep && sep != Py_None && !PyString_Check(sep) &&
1540 !PyUnicode_Check(sep)) {
1541 PyErr_Format(PyExc_TypeError,
1542 "sep must be None, str or unicode, not %.200s",
1543 sep->ob_type->tp_name);
1544 return NULL;
1545 }
1546 if (end && end != Py_None && !PyString_Check(end) &&
1547 !PyUnicode_Check(end)) {
1548 PyErr_Format(PyExc_TypeError,
1549 "end must be None, str or unicode, not %.200s",
1550 end->ob_type->tp_name);
1551 return NULL;
1552 }
1553
1554 for (i = 0; i < PyTuple_Size(args); i++) {
1555 if (i > 0) {
1556 if (sep == NULL || sep == Py_None)
1557 err = PyFile_WriteString(" ", file);
1558 else
1559 err = PyFile_WriteObject(sep, file,
1560 Py_PRINT_RAW);
1561 if (err)
1562 return NULL;
1563 }
1564 err = PyFile_WriteObject(PyTuple_GetItem(args, i), file,
1565 Py_PRINT_RAW);
1566 if (err)
1567 return NULL;
1568 }
1569
1570 if (end == NULL || end == Py_None)
1571 err = PyFile_WriteString("\n", file);
1572 else
1573 err = PyFile_WriteObject(end, file, Py_PRINT_RAW);
1574 if (err)
1575 return NULL;
1576
1577 Py_RETURN_NONE;
1578}
1579
1580PyDoc_STRVAR(print_doc,
1581"print(value, ..., sep=' ', end='\\n', file=sys.stdout)\n\
1582\n\
1583Prints the values to a stream, or to sys.stdout by default.\n\
1584Optional keyword arguments:\n\
1585file: a file-like object (stream); defaults to the current sys.stdout.\n\
1586sep: string inserted between values, default a space.\n\
1587end: string appended after the last value, default a newline.");
1588
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001589
1590/* Return number of items in range (lo, hi, step), when arguments are
1591 * PyInt or PyLong objects. step > 0 required. Return a value < 0 if
1592 * & only if the true value is too large to fit in a signed long.
1593 * Arguments MUST return 1 with either PyInt_Check() or
1594 * PyLong_Check(). Return -1 when there is an error.
1595 */
1596static long
1597get_len_of_range_longs(PyObject *lo, PyObject *hi, PyObject *step)
1598{
1599 /* -------------------------------------------------------------
1600 Algorithm is equal to that of get_len_of_range(), but it operates
1601 on PyObjects (which are assumed to be PyLong or PyInt objects).
1602 ---------------------------------------------------------------*/
1603 long n;
1604 PyObject *diff = NULL;
1605 PyObject *one = NULL;
1606 PyObject *tmp1 = NULL, *tmp2 = NULL, *tmp3 = NULL;
1607 /* holds sub-expression evaluations */
1608
1609 /* if (lo >= hi), return length of 0. */
1610 if (PyObject_Compare(lo, hi) >= 0)
1611 return 0;
1612
1613 if ((one = PyLong_FromLong(1L)) == NULL)
1614 goto Fail;
1615
1616 if ((tmp1 = PyNumber_Subtract(hi, lo)) == NULL)
1617 goto Fail;
1618
1619 if ((diff = PyNumber_Subtract(tmp1, one)) == NULL)
1620 goto Fail;
1621
1622 if ((tmp2 = PyNumber_FloorDivide(diff, step)) == NULL)
1623 goto Fail;
1624
1625 if ((tmp3 = PyNumber_Add(tmp2, one)) == NULL)
1626 goto Fail;
1627
1628 n = PyLong_AsLong(tmp3);
1629 if (PyErr_Occurred()) { /* Check for Overflow */
1630 PyErr_Clear();
1631 goto Fail;
1632 }
1633
1634 Py_DECREF(tmp3);
1635 Py_DECREF(tmp2);
1636 Py_DECREF(diff);
1637 Py_DECREF(tmp1);
1638 Py_DECREF(one);
1639 return n;
1640
1641 Fail:
1642 Py_XDECREF(tmp3);
1643 Py_XDECREF(tmp2);
1644 Py_XDECREF(diff);
1645 Py_XDECREF(tmp1);
1646 Py_XDECREF(one);
1647 return -1;
1648}
1649
1650/* An extension of builtin_range() that handles the case when PyLong
1651 * arguments are given. */
1652static PyObject *
1653handle_range_longs(PyObject *self, PyObject *args)
1654{
1655 PyObject *ilow;
Tim Peters874e1f72003-04-13 22:13:08 +00001656 PyObject *ihigh = NULL;
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001657 PyObject *istep = NULL;
Tim Peters874e1f72003-04-13 22:13:08 +00001658
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001659 PyObject *curnum = NULL;
1660 PyObject *v = NULL;
1661 long bign;
1662 int i, n;
1663 int cmp_result;
1664
Tim Peters874e1f72003-04-13 22:13:08 +00001665 PyObject *zero = PyLong_FromLong(0);
1666
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001667 if (zero == NULL)
1668 return NULL;
1669
Tim Peters874e1f72003-04-13 22:13:08 +00001670 if (!PyArg_UnpackTuple(args, "range", 1, 3, &ilow, &ihigh, &istep)) {
1671 Py_DECREF(zero);
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001672 return NULL;
1673 }
1674
Tim Peters874e1f72003-04-13 22:13:08 +00001675 /* Figure out which way we were called, supply defaults, and be
1676 * sure to incref everything so that the decrefs at the end
1677 * are correct.
1678 */
1679 assert(ilow != NULL);
1680 if (ihigh == NULL) {
1681 /* only 1 arg -- it's the upper limit */
1682 ihigh = ilow;
1683 ilow = NULL;
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001684 }
Tim Peters874e1f72003-04-13 22:13:08 +00001685 assert(ihigh != NULL);
1686 Py_INCREF(ihigh);
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001687
Tim Peters874e1f72003-04-13 22:13:08 +00001688 /* ihigh correct now; do ilow */
1689 if (ilow == NULL)
1690 ilow = zero;
1691 Py_INCREF(ilow);
1692
1693 /* ilow and ihigh correct now; do istep */
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001694 if (istep == NULL) {
1695 istep = PyLong_FromLong(1L);
1696 if (istep == NULL)
1697 goto Fail;
1698 }
1699 else {
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001700 Py_INCREF(istep);
1701 }
1702
Tim Peters874e1f72003-04-13 22:13:08 +00001703 if (!PyInt_Check(ilow) && !PyLong_Check(ilow)) {
Guido van Rossum28e83e32003-04-15 12:43:26 +00001704 PyErr_Format(PyExc_TypeError,
Alex Martellif471d472003-04-23 13:00:44 +00001705 "range() integer start argument expected, got %s.",
Guido van Rossum817d6c92003-04-14 18:25:04 +00001706 ilow->ob_type->tp_name);
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001707 goto Fail;
1708 }
1709
Tim Peters874e1f72003-04-13 22:13:08 +00001710 if (!PyInt_Check(ihigh) && !PyLong_Check(ihigh)) {
Guido van Rossum28e83e32003-04-15 12:43:26 +00001711 PyErr_Format(PyExc_TypeError,
Alex Martellif471d472003-04-23 13:00:44 +00001712 "range() integer end argument expected, got %s.",
Guido van Rossum817d6c92003-04-14 18:25:04 +00001713 ihigh->ob_type->tp_name);
Tim Peters874e1f72003-04-13 22:13:08 +00001714 goto Fail;
1715 }
1716
1717 if (!PyInt_Check(istep) && !PyLong_Check(istep)) {
Guido van Rossum28e83e32003-04-15 12:43:26 +00001718 PyErr_Format(PyExc_TypeError,
Alex Martellif471d472003-04-23 13:00:44 +00001719 "range() integer step argument expected, got %s.",
Guido van Rossum817d6c92003-04-14 18:25:04 +00001720 istep->ob_type->tp_name);
Tim Peters874e1f72003-04-13 22:13:08 +00001721 goto Fail;
1722 }
1723
1724 if (PyObject_Cmp(istep, zero, &cmp_result) == -1)
1725 goto Fail;
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001726 if (cmp_result == 0) {
1727 PyErr_SetString(PyExc_ValueError,
Alex Martellif471d472003-04-23 13:00:44 +00001728 "range() step argument must not be zero");
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001729 goto Fail;
1730 }
1731
1732 if (cmp_result > 0)
1733 bign = get_len_of_range_longs(ilow, ihigh, istep);
1734 else {
1735 PyObject *neg_istep = PyNumber_Negative(istep);
1736 if (neg_istep == NULL)
1737 goto Fail;
1738 bign = get_len_of_range_longs(ihigh, ilow, neg_istep);
1739 Py_DECREF(neg_istep);
1740 }
1741
1742 n = (int)bign;
1743 if (bign < 0 || (long)n != bign) {
1744 PyErr_SetString(PyExc_OverflowError,
1745 "range() result has too many items");
1746 goto Fail;
1747 }
1748
1749 v = PyList_New(n);
1750 if (v == NULL)
1751 goto Fail;
1752
1753 curnum = ilow;
1754 Py_INCREF(curnum);
1755
1756 for (i = 0; i < n; i++) {
1757 PyObject *w = PyNumber_Long(curnum);
1758 PyObject *tmp_num;
1759 if (w == NULL)
1760 goto Fail;
1761
1762 PyList_SET_ITEM(v, i, w);
1763
1764 tmp_num = PyNumber_Add(curnum, istep);
1765 if (tmp_num == NULL)
1766 goto Fail;
1767
1768 Py_DECREF(curnum);
1769 curnum = tmp_num;
1770 }
Tim Peters874e1f72003-04-13 22:13:08 +00001771 Py_DECREF(ilow);
1772 Py_DECREF(ihigh);
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001773 Py_DECREF(istep);
1774 Py_DECREF(zero);
Tim Peters874e1f72003-04-13 22:13:08 +00001775 Py_DECREF(curnum);
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001776 return v;
1777
1778 Fail:
Tim Peters874e1f72003-04-13 22:13:08 +00001779 Py_DECREF(ilow);
1780 Py_DECREF(ihigh);
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001781 Py_XDECREF(istep);
Tim Peters874e1f72003-04-13 22:13:08 +00001782 Py_DECREF(zero);
1783 Py_XDECREF(curnum);
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001784 Py_XDECREF(v);
1785 return NULL;
1786}
1787
Guido van Rossum124eff01999-02-23 16:11:01 +00001788/* Return number of items in range/xrange (lo, hi, step). step > 0
1789 * required. Return a value < 0 if & only if the true value is too
1790 * large to fit in a signed long.
1791 */
1792static long
Thomas Wouterse28c2962000-07-23 22:21:32 +00001793get_len_of_range(long lo, long hi, long step)
Guido van Rossum124eff01999-02-23 16:11:01 +00001794{
1795 /* -------------------------------------------------------------
1796 If lo >= hi, the range is empty.
1797 Else if n values are in the range, the last one is
1798 lo + (n-1)*step, which must be <= hi-1. Rearranging,
1799 n <= (hi - lo - 1)/step + 1, so taking the floor of the RHS gives
1800 the proper value. Since lo < hi in this case, hi-lo-1 >= 0, so
1801 the RHS is non-negative and so truncation is the same as the
1802 floor. Letting M be the largest positive long, the worst case
1803 for the RHS numerator is hi=M, lo=-M-1, and then
1804 hi-lo-1 = M-(-M-1)-1 = 2*M. Therefore unsigned long has enough
1805 precision to compute the RHS exactly.
1806 ---------------------------------------------------------------*/
1807 long n = 0;
1808 if (lo < hi) {
1809 unsigned long uhi = (unsigned long)hi;
1810 unsigned long ulo = (unsigned long)lo;
1811 unsigned long diff = uhi - ulo - 1;
1812 n = (long)(diff / (unsigned long)step + 1);
1813 }
1814 return n;
1815}
1816
Guido van Rossum79f25d91997-04-29 20:08:16 +00001817static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001818builtin_range(PyObject *self, PyObject *args)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001819{
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001820 long ilow = 0, ihigh = 0, istep = 1;
Guido van Rossum124eff01999-02-23 16:11:01 +00001821 long bign;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001822 int i, n;
Guido van Rossum124eff01999-02-23 16:11:01 +00001823
Guido van Rossum79f25d91997-04-29 20:08:16 +00001824 PyObject *v;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001825
Guido van Rossum79f25d91997-04-29 20:08:16 +00001826 if (PyTuple_Size(args) <= 1) {
1827 if (!PyArg_ParseTuple(args,
Guido van Rossum0865dd91995-01-17 16:30:22 +00001828 "l;range() requires 1-3 int arguments",
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001829 &ihigh)) {
1830 PyErr_Clear();
1831 return handle_range_longs(self, args);
1832 }
Guido van Rossum3f5da241990-12-20 15:06:42 +00001833 }
1834 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001835 if (!PyArg_ParseTuple(args,
Guido van Rossum0865dd91995-01-17 16:30:22 +00001836 "ll|l;range() requires 1-3 int arguments",
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001837 &ilow, &ihigh, &istep)) {
1838 PyErr_Clear();
1839 return handle_range_longs(self, args);
1840 }
Guido van Rossum3f5da241990-12-20 15:06:42 +00001841 }
1842 if (istep == 0) {
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001843 PyErr_SetString(PyExc_ValueError,
Alex Martellif471d472003-04-23 13:00:44 +00001844 "range() step argument must not be zero");
Guido van Rossum3f5da241990-12-20 15:06:42 +00001845 return NULL;
1846 }
Guido van Rossum124eff01999-02-23 16:11:01 +00001847 if (istep > 0)
1848 bign = get_len_of_range(ilow, ihigh, istep);
1849 else
1850 bign = get_len_of_range(ihigh, ilow, -istep);
1851 n = (int)bign;
1852 if (bign < 0 || (long)n != bign) {
Guido van Rossume23cde21999-01-12 05:07:47 +00001853 PyErr_SetString(PyExc_OverflowError,
Fred Drake661ea262000-10-24 19:57:45 +00001854 "range() result has too many items");
Guido van Rossume23cde21999-01-12 05:07:47 +00001855 return NULL;
1856 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00001857 v = PyList_New(n);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001858 if (v == NULL)
1859 return NULL;
1860 for (i = 0; i < n; i++) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001861 PyObject *w = PyInt_FromLong(ilow);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001862 if (w == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001863 Py_DECREF(v);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001864 return NULL;
1865 }
Guido van Rossuma937d141998-04-24 18:22:02 +00001866 PyList_SET_ITEM(v, i, w);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001867 ilow += istep;
1868 }
1869 return v;
1870}
1871
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001872PyDoc_STRVAR(range_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001873"range([start,] stop[, step]) -> list of integers\n\
1874\n\
1875Return a list containing an arithmetic progression of integers.\n\
1876range(i, j) returns [i, i+1, i+2, ..., j-1]; start (!) defaults to 0.\n\
1877When step is given, it specifies the increment (or decrement).\n\
1878For example, range(4) returns [0, 1, 2, 3]. The end point is omitted!\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001879These are exactly the valid indices for a list of 4 elements.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001880
1881
Guido van Rossum79f25d91997-04-29 20:08:16 +00001882static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001883builtin_raw_input(PyObject *self, PyObject *args)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001884{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001885 PyObject *v = NULL;
Martin v. Löwis31d2df52002-08-14 15:46:02 +00001886 PyObject *fin = PySys_GetObject("stdin");
1887 PyObject *fout = PySys_GetObject("stdout");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001888
Raymond Hettingerea3fdf42002-12-29 16:33:45 +00001889 if (!PyArg_UnpackTuple(args, "[raw_]input", 0, 1, &v))
Guido van Rossum3165fe61992-09-25 21:59:05 +00001890 return NULL;
Martin v. Löwis31d2df52002-08-14 15:46:02 +00001891
1892 if (fin == NULL) {
Alex Martellia9b9c9f2003-04-23 13:34:35 +00001893 PyErr_SetString(PyExc_RuntimeError, "[raw_]input: lost sys.stdin");
Martin v. Löwis31d2df52002-08-14 15:46:02 +00001894 return NULL;
1895 }
1896 if (fout == NULL) {
Alex Martellia9b9c9f2003-04-23 13:34:35 +00001897 PyErr_SetString(PyExc_RuntimeError, "[raw_]input: lost sys.stdout");
Martin v. Löwis31d2df52002-08-14 15:46:02 +00001898 return NULL;
1899 }
1900 if (PyFile_SoftSpace(fout, 0)) {
1901 if (PyFile_WriteString(" ", fout) != 0)
1902 return NULL;
1903 }
Georg Brandl7e3ba2a2006-08-06 08:23:54 +00001904 if (PyFile_AsFile(fin) && PyFile_AsFile(fout)
Martin v. Löwis566f6af2002-10-26 14:39:10 +00001905 && isatty(fileno(PyFile_AsFile(fin)))
1906 && isatty(fileno(PyFile_AsFile(fout)))) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001907 PyObject *po;
Guido van Rossum872537c1995-07-07 22:43:42 +00001908 char *prompt;
1909 char *s;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001910 PyObject *result;
Guido van Rossum872537c1995-07-07 22:43:42 +00001911 if (v != NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001912 po = PyObject_Str(v);
Guido van Rossum872537c1995-07-07 22:43:42 +00001913 if (po == NULL)
1914 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001915 prompt = PyString_AsString(po);
Guido van Rossumd9b52081998-06-26 18:25:38 +00001916 if (prompt == NULL)
1917 return NULL;
Guido van Rossum872537c1995-07-07 22:43:42 +00001918 }
1919 else {
1920 po = NULL;
1921 prompt = "";
1922 }
Michael W. Hudson52db5192004-08-02 13:21:09 +00001923 s = PyOS_Readline(PyFile_AsFile(fin), PyFile_AsFile(fout),
Martin v. Löwis566f6af2002-10-26 14:39:10 +00001924 prompt);
Guido van Rossum79f25d91997-04-29 20:08:16 +00001925 Py_XDECREF(po);
Guido van Rossum872537c1995-07-07 22:43:42 +00001926 if (s == NULL) {
Michael W. Hudson30ea2f22004-07-07 17:44:12 +00001927 if (!PyErr_Occurred())
1928 PyErr_SetNone(PyExc_KeyboardInterrupt);
Guido van Rossum872537c1995-07-07 22:43:42 +00001929 return NULL;
1930 }
1931 if (*s == '\0') {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001932 PyErr_SetNone(PyExc_EOFError);
Guido van Rossum872537c1995-07-07 22:43:42 +00001933 result = NULL;
1934 }
1935 else { /* strip trailing '\n' */
Guido van Rossum106f2da2000-06-28 21:12:25 +00001936 size_t len = strlen(s);
Martin v. Löwisb1ed7fa2006-04-13 07:52:27 +00001937 if (len > PY_SSIZE_T_MAX) {
Martin v. Löwis31d2df52002-08-14 15:46:02 +00001938 PyErr_SetString(PyExc_OverflowError,
Alex Martellia9b9c9f2003-04-23 13:34:35 +00001939 "[raw_]input: input too long");
Guido van Rossum106f2da2000-06-28 21:12:25 +00001940 result = NULL;
1941 }
1942 else {
Martin v. Löwisb1ed7fa2006-04-13 07:52:27 +00001943 result = PyString_FromStringAndSize(s, len-1);
Guido van Rossum106f2da2000-06-28 21:12:25 +00001944 }
Guido van Rossum872537c1995-07-07 22:43:42 +00001945 }
Guido van Rossumb18618d2000-05-03 23:44:39 +00001946 PyMem_FREE(s);
Guido van Rossum872537c1995-07-07 22:43:42 +00001947 return result;
1948 }
Guido van Rossum90933611991-06-07 16:10:43 +00001949 if (v != NULL) {
Martin v. Löwis31d2df52002-08-14 15:46:02 +00001950 if (PyFile_WriteObject(v, fout, Py_PRINT_RAW) != 0)
Guido van Rossum90933611991-06-07 16:10:43 +00001951 return NULL;
1952 }
Martin v. Löwis31d2df52002-08-14 15:46:02 +00001953 return PyFile_GetLine(fin, -1);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001954}
1955
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001956PyDoc_STRVAR(raw_input_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001957"raw_input([prompt]) -> string\n\
1958\n\
1959Read a string from standard input. The trailing newline is stripped.\n\
1960If the user hits EOF (Unix: Ctl-D, Windows: Ctl-Z+Return), raise EOFError.\n\
1961On Unix, GNU readline is used if enabled. The prompt string, if given,\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001962is printed without a trailing newline before reading.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001963
1964
Guido van Rossum79f25d91997-04-29 20:08:16 +00001965static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001966builtin_reduce(PyObject *self, PyObject *args)
Guido van Rossum12d12c51993-10-26 17:58:25 +00001967{
Tim Peters15d81ef2001-05-04 04:39:21 +00001968 PyObject *seq, *func, *result = NULL, *it;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001969
Neal Norwitzdf25efe2007-05-23 06:58:36 +00001970 if (Py_Py3kWarningFlag &&
1971 PyErr_Warn(PyExc_DeprecationWarning,
Georg Brandld5b635f2008-03-25 08:29:14 +00001972 "reduce() not supported in 3.x; "
1973 "use functools.reduce()") < 0)
Neal Norwitzdf25efe2007-05-23 06:58:36 +00001974 return NULL;
1975
Raymond Hettingerea3fdf42002-12-29 16:33:45 +00001976 if (!PyArg_UnpackTuple(args, "reduce", 2, 3, &func, &seq, &result))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001977 return NULL;
1978 if (result != NULL)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001979 Py_INCREF(result);
Guido van Rossum12d12c51993-10-26 17:58:25 +00001980
Tim Peters15d81ef2001-05-04 04:39:21 +00001981 it = PyObject_GetIter(seq);
1982 if (it == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001983 PyErr_SetString(PyExc_TypeError,
Tim Peters15d81ef2001-05-04 04:39:21 +00001984 "reduce() arg 2 must support iteration");
1985 Py_XDECREF(result);
Guido van Rossum12d12c51993-10-26 17:58:25 +00001986 return NULL;
1987 }
1988
Guido van Rossum79f25d91997-04-29 20:08:16 +00001989 if ((args = PyTuple_New(2)) == NULL)
Guido van Rossum2d951851994-08-29 12:52:16 +00001990 goto Fail;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001991
Tim Peters15d81ef2001-05-04 04:39:21 +00001992 for (;;) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001993 PyObject *op2;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001994
1995 if (args->ob_refcnt > 1) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001996 Py_DECREF(args);
1997 if ((args = PyTuple_New(2)) == NULL)
Guido van Rossum2d951851994-08-29 12:52:16 +00001998 goto Fail;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001999 }
2000
Tim Peters15d81ef2001-05-04 04:39:21 +00002001 op2 = PyIter_Next(it);
2002 if (op2 == NULL) {
Tim Petersf4848da2001-05-05 00:14:56 +00002003 if (PyErr_Occurred())
2004 goto Fail;
2005 break;
Guido van Rossum2d951851994-08-29 12:52:16 +00002006 }
Guido van Rossum12d12c51993-10-26 17:58:25 +00002007
Guido van Rossum2d951851994-08-29 12:52:16 +00002008 if (result == NULL)
2009 result = op2;
2010 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002011 PyTuple_SetItem(args, 0, result);
2012 PyTuple_SetItem(args, 1, op2);
2013 if ((result = PyEval_CallObject(func, args)) == NULL)
Guido van Rossum2d951851994-08-29 12:52:16 +00002014 goto Fail;
2015 }
Guido van Rossum12d12c51993-10-26 17:58:25 +00002016 }
2017
Guido van Rossum79f25d91997-04-29 20:08:16 +00002018 Py_DECREF(args);
Guido van Rossum12d12c51993-10-26 17:58:25 +00002019
Guido van Rossum2d951851994-08-29 12:52:16 +00002020 if (result == NULL)
Guido van Rossum79f25d91997-04-29 20:08:16 +00002021 PyErr_SetString(PyExc_TypeError,
Fred Drake661ea262000-10-24 19:57:45 +00002022 "reduce() of empty sequence with no initial value");
Guido van Rossum2d951851994-08-29 12:52:16 +00002023
Tim Peters15d81ef2001-05-04 04:39:21 +00002024 Py_DECREF(it);
Guido van Rossum12d12c51993-10-26 17:58:25 +00002025 return result;
2026
Guido van Rossum2d951851994-08-29 12:52:16 +00002027Fail:
Guido van Rossum79f25d91997-04-29 20:08:16 +00002028 Py_XDECREF(args);
2029 Py_XDECREF(result);
Tim Peters15d81ef2001-05-04 04:39:21 +00002030 Py_DECREF(it);
Guido van Rossum12d12c51993-10-26 17:58:25 +00002031 return NULL;
2032}
2033
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002034PyDoc_STRVAR(reduce_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002035"reduce(function, sequence[, initial]) -> value\n\
2036\n\
2037Apply a function of two arguments cumulatively to the items of a sequence,\n\
2038from left to right, so as to reduce the sequence to a single value.\n\
2039For example, reduce(lambda x, y: x+y, [1, 2, 3, 4, 5]) calculates\n\
2040((((1+2)+3)+4)+5). If initial is present, it is placed before the items\n\
2041of the sequence in the calculation, and serves as a default when the\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002042sequence is empty.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002043
2044
Guido van Rossum79f25d91997-04-29 20:08:16 +00002045static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002046builtin_reload(PyObject *self, PyObject *v)
Guido van Rossum3f5da241990-12-20 15:06:42 +00002047{
Neal Norwitzdf25efe2007-05-23 06:58:36 +00002048 if (Py_Py3kWarningFlag &&
2049 PyErr_Warn(PyExc_DeprecationWarning,
Georg Brandld5b635f2008-03-25 08:29:14 +00002050 "reload() not supported in 3.x; use imp.reload()") < 0)
Neal Norwitzdf25efe2007-05-23 06:58:36 +00002051 return NULL;
2052
Guido van Rossum79f25d91997-04-29 20:08:16 +00002053 return PyImport_ReloadModule(v);
Guido van Rossum3f5da241990-12-20 15:06:42 +00002054}
2055
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002056PyDoc_STRVAR(reload_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002057"reload(module) -> module\n\
2058\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002059Reload the module. The module must have been successfully imported before.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002060
2061
Guido van Rossum79f25d91997-04-29 20:08:16 +00002062static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002063builtin_repr(PyObject *self, PyObject *v)
Guido van Rossumc89705d1992-11-26 08:54:07 +00002064{
Guido van Rossum79f25d91997-04-29 20:08:16 +00002065 return PyObject_Repr(v);
Guido van Rossumc89705d1992-11-26 08:54:07 +00002066}
2067
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002068PyDoc_STRVAR(repr_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002069"repr(object) -> string\n\
2070\n\
2071Return the canonical string representation of the object.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002072For most object types, eval(repr(object)) == object.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002073
2074
Guido van Rossum79f25d91997-04-29 20:08:16 +00002075static PyObject *
Georg Brandlccadf842006-03-31 18:54:53 +00002076builtin_round(PyObject *self, PyObject *args, PyObject *kwds)
Guido van Rossum9e51f9b1993-02-12 16:29:05 +00002077{
Jeffrey Yasskin9871d8f2008-01-05 08:47:13 +00002078 double number;
2079 double f;
2080 int ndigits = 0;
2081 int i;
Georg Brandlccadf842006-03-31 18:54:53 +00002082 static char *kwlist[] = {"number", "ndigits", 0};
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002083
Jeffrey Yasskin9871d8f2008-01-05 08:47:13 +00002084 if (!PyArg_ParseTupleAndKeywords(args, kwds, "d|i:round",
2085 kwlist, &number, &ndigits))
2086 return NULL;
2087 f = 1.0;
2088 i = abs(ndigits);
2089 while (--i >= 0)
2090 f = f*10.0;
2091 if (ndigits < 0)
2092 number /= f;
2093 else
2094 number *= f;
2095 if (number >= 0.0)
2096 number = floor(number + 0.5);
2097 else
2098 number = ceil(number - 0.5);
2099 if (ndigits < 0)
2100 number *= f;
2101 else
2102 number /= f;
2103 return PyFloat_FromDouble(number);
Guido van Rossum9e51f9b1993-02-12 16:29:05 +00002104}
2105
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002106PyDoc_STRVAR(round_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002107"round(number[, ndigits]) -> floating point number\n\
2108\n\
2109Round a number to a given precision in decimal digits (default 0 digits).\n\
Jeffrey Yasskin9871d8f2008-01-05 08:47:13 +00002110This always returns a floating point number. Precision may be negative.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002111
Raymond Hettinger64958a12003-12-17 20:43:33 +00002112static PyObject *
2113builtin_sorted(PyObject *self, PyObject *args, PyObject *kwds)
2114{
2115 PyObject *newlist, *v, *seq, *compare=NULL, *keyfunc=NULL, *newargs;
2116 PyObject *callable;
Martin v. Löwis15e62742006-02-27 16:46:16 +00002117 static char *kwlist[] = {"iterable", "cmp", "key", "reverse", 0};
Neal Norwitz6f0d4792005-12-15 06:40:36 +00002118 int reverse;
Raymond Hettinger64958a12003-12-17 20:43:33 +00002119
Neal Norwitz6f0d4792005-12-15 06:40:36 +00002120 /* args 1-4 should match listsort in Objects/listobject.c */
Armin Rigo71d7e702005-09-20 18:13:03 +00002121 if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|OOi:sorted",
2122 kwlist, &seq, &compare, &keyfunc, &reverse))
2123 return NULL;
Raymond Hettinger64958a12003-12-17 20:43:33 +00002124
2125 newlist = PySequence_List(seq);
2126 if (newlist == NULL)
2127 return NULL;
2128
2129 callable = PyObject_GetAttrString(newlist, "sort");
2130 if (callable == NULL) {
2131 Py_DECREF(newlist);
2132 return NULL;
2133 }
Georg Brandl99d7e4e2005-08-31 22:21:15 +00002134
Raymond Hettinger64958a12003-12-17 20:43:33 +00002135 newargs = PyTuple_GetSlice(args, 1, 4);
2136 if (newargs == NULL) {
2137 Py_DECREF(newlist);
2138 Py_DECREF(callable);
2139 return NULL;
2140 }
2141
2142 v = PyObject_Call(callable, newargs, kwds);
2143 Py_DECREF(newargs);
2144 Py_DECREF(callable);
2145 if (v == NULL) {
2146 Py_DECREF(newlist);
2147 return NULL;
2148 }
2149 Py_DECREF(v);
2150 return newlist;
2151}
2152
2153PyDoc_STRVAR(sorted_doc,
2154"sorted(iterable, cmp=None, key=None, reverse=False) --> new sorted list");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002155
Guido van Rossum79f25d91997-04-29 20:08:16 +00002156static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002157builtin_vars(PyObject *self, PyObject *args)
Guido van Rossum2d951851994-08-29 12:52:16 +00002158{
Guido van Rossum79f25d91997-04-29 20:08:16 +00002159 PyObject *v = NULL;
2160 PyObject *d;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002161
Raymond Hettingerea3fdf42002-12-29 16:33:45 +00002162 if (!PyArg_UnpackTuple(args, "vars", 0, 1, &v))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002163 return NULL;
Guido van Rossum2d951851994-08-29 12:52:16 +00002164 if (v == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002165 d = PyEval_GetLocals();
Guido van Rossum53bb7ff1995-07-26 16:26:31 +00002166 if (d == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002167 if (!PyErr_Occurred())
2168 PyErr_SetString(PyExc_SystemError,
Alex Martellia9b9c9f2003-04-23 13:34:35 +00002169 "vars(): no locals!?");
Guido van Rossum53bb7ff1995-07-26 16:26:31 +00002170 }
2171 else
Guido van Rossum79f25d91997-04-29 20:08:16 +00002172 Py_INCREF(d);
Guido van Rossum2d951851994-08-29 12:52:16 +00002173 }
2174 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002175 d = PyObject_GetAttrString(v, "__dict__");
Guido van Rossum2d951851994-08-29 12:52:16 +00002176 if (d == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002177 PyErr_SetString(PyExc_TypeError,
Guido van Rossum2d951851994-08-29 12:52:16 +00002178 "vars() argument must have __dict__ attribute");
2179 return NULL;
2180 }
2181 }
2182 return d;
2183}
2184
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002185PyDoc_STRVAR(vars_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002186"vars([object]) -> dictionary\n\
2187\n\
2188Without arguments, equivalent to locals().\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002189With an argument, equivalent to object.__dict__.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002190
Alex Martellia70b1912003-04-22 08:12:33 +00002191
2192static PyObject*
2193builtin_sum(PyObject *self, PyObject *args)
2194{
2195 PyObject *seq;
2196 PyObject *result = NULL;
2197 PyObject *temp, *item, *iter;
2198
Raymond Hettinger5cf63942003-10-25 06:41:37 +00002199 if (!PyArg_UnpackTuple(args, "sum", 1, 2, &seq, &result))
Alex Martellia70b1912003-04-22 08:12:33 +00002200 return NULL;
2201
2202 iter = PyObject_GetIter(seq);
2203 if (iter == NULL)
2204 return NULL;
2205
2206 if (result == NULL) {
2207 result = PyInt_FromLong(0);
2208 if (result == NULL) {
2209 Py_DECREF(iter);
2210 return NULL;
2211 }
2212 } else {
2213 /* reject string values for 'start' parameter */
2214 if (PyObject_TypeCheck(result, &PyBaseString_Type)) {
2215 PyErr_SetString(PyExc_TypeError,
Alex Martellia9b9c9f2003-04-23 13:34:35 +00002216 "sum() can't sum strings [use ''.join(seq) instead]");
Alex Martellia70b1912003-04-22 08:12:33 +00002217 Py_DECREF(iter);
2218 return NULL;
2219 }
Alex Martelli41c9f882003-04-22 09:24:48 +00002220 Py_INCREF(result);
Alex Martellia70b1912003-04-22 08:12:33 +00002221 }
2222
Raymond Hettinger3f8caa32007-10-24 01:28:33 +00002223#ifndef SLOW_SUM
2224 /* Fast addition by keeping temporary sums in C instead of new Python objects.
2225 Assumes all inputs are the same type. If the assumption fails, default
2226 to the more general routine.
2227 */
2228 if (PyInt_CheckExact(result)) {
2229 long i_result = PyInt_AS_LONG(result);
2230 Py_DECREF(result);
2231 result = NULL;
2232 while(result == NULL) {
2233 item = PyIter_Next(iter);
2234 if (item == NULL) {
2235 Py_DECREF(iter);
2236 if (PyErr_Occurred())
2237 return NULL;
2238 return PyInt_FromLong(i_result);
2239 }
2240 if (PyInt_CheckExact(item)) {
2241 long b = PyInt_AS_LONG(item);
2242 long x = i_result + b;
2243 if ((x^i_result) >= 0 || (x^b) >= 0) {
2244 i_result = x;
2245 Py_DECREF(item);
2246 continue;
2247 }
2248 }
2249 /* Either overflowed or is not an int. Restore real objects and process normally */
2250 result = PyInt_FromLong(i_result);
2251 temp = PyNumber_Add(result, item);
2252 Py_DECREF(result);
2253 Py_DECREF(item);
2254 result = temp;
2255 if (result == NULL) {
2256 Py_DECREF(iter);
2257 return NULL;
2258 }
2259 }
2260 }
2261
2262 if (PyFloat_CheckExact(result)) {
2263 double f_result = PyFloat_AS_DOUBLE(result);
2264 Py_DECREF(result);
2265 result = NULL;
2266 while(result == NULL) {
2267 item = PyIter_Next(iter);
2268 if (item == NULL) {
2269 Py_DECREF(iter);
2270 if (PyErr_Occurred())
2271 return NULL;
2272 return PyFloat_FromDouble(f_result);
2273 }
2274 if (PyFloat_CheckExact(item)) {
2275 PyFPE_START_PROTECT("add", return 0)
2276 f_result += PyFloat_AS_DOUBLE(item);
Raymond Hettinger3a8daf52007-10-24 02:05:51 +00002277 PyFPE_END_PROTECT(f_result)
Raymond Hettingera45c4872007-10-25 02:26:58 +00002278 Py_DECREF(item);
Raymond Hettinger3a8daf52007-10-24 02:05:51 +00002279 continue;
2280 }
2281 if (PyInt_CheckExact(item)) {
2282 PyFPE_START_PROTECT("add", return 0)
2283 f_result += (double)PyInt_AS_LONG(item);
2284 PyFPE_END_PROTECT(f_result)
Raymond Hettingera45c4872007-10-25 02:26:58 +00002285 Py_DECREF(item);
Raymond Hettinger3f8caa32007-10-24 01:28:33 +00002286 continue;
2287 }
2288 result = PyFloat_FromDouble(f_result);
2289 temp = PyNumber_Add(result, item);
2290 Py_DECREF(result);
2291 Py_DECREF(item);
2292 result = temp;
2293 if (result == NULL) {
2294 Py_DECREF(iter);
2295 return NULL;
2296 }
2297 }
2298 }
2299#endif
2300
Alex Martellia70b1912003-04-22 08:12:33 +00002301 for(;;) {
2302 item = PyIter_Next(iter);
2303 if (item == NULL) {
2304 /* error, or end-of-sequence */
2305 if (PyErr_Occurred()) {
2306 Py_DECREF(result);
2307 result = NULL;
2308 }
2309 break;
2310 }
Alex Martellia253e182003-10-25 23:24:14 +00002311 temp = PyNumber_Add(result, item);
Alex Martellia70b1912003-04-22 08:12:33 +00002312 Py_DECREF(result);
2313 Py_DECREF(item);
2314 result = temp;
2315 if (result == NULL)
2316 break;
2317 }
2318 Py_DECREF(iter);
2319 return result;
2320}
2321
2322PyDoc_STRVAR(sum_doc,
Georg Brandl8134d062006-10-12 12:33:07 +00002323"sum(sequence[, start]) -> value\n\
Alex Martellia70b1912003-04-22 08:12:33 +00002324\n\
2325Returns the sum of a sequence of numbers (NOT strings) plus the value\n\
Georg Brandl8134d062006-10-12 12:33:07 +00002326of parameter 'start' (which defaults to 0). When the sequence is\n\
2327empty, returns start.");
Alex Martellia70b1912003-04-22 08:12:33 +00002328
2329
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002330static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002331builtin_isinstance(PyObject *self, PyObject *args)
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002332{
2333 PyObject *inst;
2334 PyObject *cls;
Guido van Rossum823649d2001-03-21 18:40:58 +00002335 int retval;
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002336
Raymond Hettingerea3fdf42002-12-29 16:33:45 +00002337 if (!PyArg_UnpackTuple(args, "isinstance", 2, 2, &inst, &cls))
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002338 return NULL;
Guido van Rossumf5dd9141997-12-02 19:11:45 +00002339
Guido van Rossum823649d2001-03-21 18:40:58 +00002340 retval = PyObject_IsInstance(inst, cls);
2341 if (retval < 0)
Guido van Rossumad991772001-01-12 16:03:05 +00002342 return NULL;
Guido van Rossum77f6a652002-04-03 22:41:51 +00002343 return PyBool_FromLong(retval);
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002344}
2345
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002346PyDoc_STRVAR(isinstance_doc,
Guido van Rossum77f6a652002-04-03 22:41:51 +00002347"isinstance(object, class-or-type-or-tuple) -> bool\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002348\n\
2349Return whether an object is an instance of a class or of a subclass thereof.\n\
Guido van Rossum03290ec2001-10-07 20:54:12 +00002350With a type as second argument, return whether that is the object's type.\n\
2351The form using a tuple, isinstance(x, (A, B, ...)), is a shortcut for\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002352isinstance(x, A) or isinstance(x, B) or ... (etc.).");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002353
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002354
2355static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002356builtin_issubclass(PyObject *self, PyObject *args)
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002357{
2358 PyObject *derived;
2359 PyObject *cls;
2360 int retval;
2361
Raymond Hettingerea3fdf42002-12-29 16:33:45 +00002362 if (!PyArg_UnpackTuple(args, "issubclass", 2, 2, &derived, &cls))
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002363 return NULL;
Guido van Rossum668213d1999-06-16 17:28:37 +00002364
Guido van Rossum823649d2001-03-21 18:40:58 +00002365 retval = PyObject_IsSubclass(derived, cls);
2366 if (retval < 0)
2367 return NULL;
Guido van Rossum77f6a652002-04-03 22:41:51 +00002368 return PyBool_FromLong(retval);
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002369}
2370
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002371PyDoc_STRVAR(issubclass_doc,
Guido van Rossum77f6a652002-04-03 22:41:51 +00002372"issubclass(C, B) -> bool\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002373\n\
Walter Dörwaldd9a6ad32002-12-12 16:41:44 +00002374Return whether class C is a subclass (i.e., a derived class) of class B.\n\
2375When using a tuple as the second argument issubclass(X, (A, B, ...)),\n\
2376is a shortcut for issubclass(X, A) or issubclass(X, B) or ... (etc.).");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002377
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002378
Barry Warsawbd599b52000-08-03 15:45:29 +00002379static PyObject*
2380builtin_zip(PyObject *self, PyObject *args)
2381{
2382 PyObject *ret;
Martin v. Löwisd96ee902006-02-16 14:37:16 +00002383 const Py_ssize_t itemsize = PySequence_Length(args);
2384 Py_ssize_t i;
Tim Peters8572b4f2001-05-06 01:05:02 +00002385 PyObject *itlist; /* tuple of iterators */
Martin v. Löwisd96ee902006-02-16 14:37:16 +00002386 Py_ssize_t len; /* guess at result length */
Barry Warsawbd599b52000-08-03 15:45:29 +00002387
Raymond Hettingereaef6152003-08-02 07:42:57 +00002388 if (itemsize == 0)
2389 return PyList_New(0);
2390
Barry Warsawbd599b52000-08-03 15:45:29 +00002391 /* args must be a tuple */
2392 assert(PyTuple_Check(args));
2393
Tim Peters39a86c22002-05-12 07:19:38 +00002394 /* Guess at result length: the shortest of the input lengths.
2395 If some argument refuses to say, we refuse to guess too, lest
2396 an argument like xrange(sys.maxint) lead us astray.*/
Tim Peters67d687a2002-04-29 21:27:32 +00002397 len = -1; /* unknown */
2398 for (i = 0; i < itemsize; ++i) {
2399 PyObject *item = PyTuple_GET_ITEM(args, i);
Raymond Hettinger4e2f7142007-12-06 00:56:53 +00002400 Py_ssize_t thislen = _PyObject_LengthHint(item, -1);
Tim Peters39a86c22002-05-12 07:19:38 +00002401 if (thislen < 0) {
Tim Peters39a86c22002-05-12 07:19:38 +00002402 len = -1;
2403 break;
2404 }
Tim Peters67d687a2002-04-29 21:27:32 +00002405 else if (len < 0 || thislen < len)
2406 len = thislen;
2407 }
2408
Tim Peters8572b4f2001-05-06 01:05:02 +00002409 /* allocate result list */
Tim Peters67d687a2002-04-29 21:27:32 +00002410 if (len < 0)
2411 len = 10; /* arbitrary */
2412 if ((ret = PyList_New(len)) == NULL)
Barry Warsawbd599b52000-08-03 15:45:29 +00002413 return NULL;
2414
Tim Peters8572b4f2001-05-06 01:05:02 +00002415 /* obtain iterators */
2416 itlist = PyTuple_New(itemsize);
2417 if (itlist == NULL)
2418 goto Fail_ret;
2419 for (i = 0; i < itemsize; ++i) {
2420 PyObject *item = PyTuple_GET_ITEM(args, i);
2421 PyObject *it = PyObject_GetIter(item);
2422 if (it == NULL) {
2423 if (PyErr_ExceptionMatches(PyExc_TypeError))
2424 PyErr_Format(PyExc_TypeError,
Neal Norwitza361bd82006-02-19 19:31:50 +00002425 "zip argument #%zd must support iteration",
Tim Peters8572b4f2001-05-06 01:05:02 +00002426 i+1);
2427 goto Fail_ret_itlist;
Barry Warsawbd599b52000-08-03 15:45:29 +00002428 }
Tim Peters8572b4f2001-05-06 01:05:02 +00002429 PyTuple_SET_ITEM(itlist, i, it);
2430 }
Barry Warsawbd599b52000-08-03 15:45:29 +00002431
Tim Peters8572b4f2001-05-06 01:05:02 +00002432 /* build result into ret list */
Tim Peters67d687a2002-04-29 21:27:32 +00002433 for (i = 0; ; ++i) {
2434 int j;
Tim Peters8572b4f2001-05-06 01:05:02 +00002435 PyObject *next = PyTuple_New(itemsize);
2436 if (!next)
2437 goto Fail_ret_itlist;
2438
Tim Peters67d687a2002-04-29 21:27:32 +00002439 for (j = 0; j < itemsize; j++) {
2440 PyObject *it = PyTuple_GET_ITEM(itlist, j);
Tim Peters8572b4f2001-05-06 01:05:02 +00002441 PyObject *item = PyIter_Next(it);
Barry Warsawbd599b52000-08-03 15:45:29 +00002442 if (!item) {
Tim Peters8572b4f2001-05-06 01:05:02 +00002443 if (PyErr_Occurred()) {
2444 Py_DECREF(ret);
2445 ret = NULL;
Barry Warsawbd599b52000-08-03 15:45:29 +00002446 }
2447 Py_DECREF(next);
Tim Peters8572b4f2001-05-06 01:05:02 +00002448 Py_DECREF(itlist);
Tim Peters67d687a2002-04-29 21:27:32 +00002449 goto Done;
Barry Warsawbd599b52000-08-03 15:45:29 +00002450 }
Tim Peters67d687a2002-04-29 21:27:32 +00002451 PyTuple_SET_ITEM(next, j, item);
Barry Warsawbd599b52000-08-03 15:45:29 +00002452 }
Tim Peters8572b4f2001-05-06 01:05:02 +00002453
Tim Peters67d687a2002-04-29 21:27:32 +00002454 if (i < len)
2455 PyList_SET_ITEM(ret, i, next);
2456 else {
2457 int status = PyList_Append(ret, next);
2458 Py_DECREF(next);
2459 ++len;
2460 if (status < 0)
2461 goto Fail_ret_itlist;
2462 }
Barry Warsawbd599b52000-08-03 15:45:29 +00002463 }
Tim Peters8572b4f2001-05-06 01:05:02 +00002464
Tim Peters67d687a2002-04-29 21:27:32 +00002465Done:
2466 if (ret != NULL && i < len) {
2467 /* The list is too big. */
2468 if (PyList_SetSlice(ret, i, len, NULL) < 0)
2469 return NULL;
2470 }
2471 return ret;
2472
Tim Peters8572b4f2001-05-06 01:05:02 +00002473Fail_ret_itlist:
2474 Py_DECREF(itlist);
2475Fail_ret:
2476 Py_DECREF(ret);
2477 return NULL;
Barry Warsawbd599b52000-08-03 15:45:29 +00002478}
2479
2480
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002481PyDoc_STRVAR(zip_doc,
Barry Warsawbd599b52000-08-03 15:45:29 +00002482"zip(seq1 [, seq2 [...]]) -> [(seq1[0], seq2[0] ...), (...)]\n\
2483\n\
2484Return a list of tuples, where each tuple contains the i-th element\n\
2485from each of the argument sequences. The returned list is truncated\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002486in length to the length of the shortest argument sequence.");
Barry Warsawbd599b52000-08-03 15:45:29 +00002487
2488
Guido van Rossum79f25d91997-04-29 20:08:16 +00002489static PyMethodDef builtin_methods[] = {
Neal Norwitz92e212f2006-04-03 04:48:37 +00002490 {"__import__", (PyCFunction)builtin___import__, METH_VARARGS | METH_KEYWORDS, import_doc},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002491 {"abs", builtin_abs, METH_O, abs_doc},
Raymond Hettinger96229b12005-03-11 06:49:40 +00002492 {"all", builtin_all, METH_O, all_doc},
2493 {"any", builtin_any, METH_O, any_doc},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002494 {"apply", builtin_apply, METH_VARARGS, apply_doc},
Eric Smith3cd81942008-02-22 16:30:22 +00002495 {"bin", builtin_bin, METH_O, bin_doc},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002496 {"callable", builtin_callable, METH_O, callable_doc},
2497 {"chr", builtin_chr, METH_VARARGS, chr_doc},
2498 {"cmp", builtin_cmp, METH_VARARGS, cmp_doc},
2499 {"coerce", builtin_coerce, METH_VARARGS, coerce_doc},
Georg Brandl5240d742007-03-13 20:46:32 +00002500 {"compile", (PyCFunction)builtin_compile, METH_VARARGS | METH_KEYWORDS, compile_doc},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002501 {"delattr", builtin_delattr, METH_VARARGS, delattr_doc},
2502 {"dir", builtin_dir, METH_VARARGS, dir_doc},
2503 {"divmod", builtin_divmod, METH_VARARGS, divmod_doc},
2504 {"eval", builtin_eval, METH_VARARGS, eval_doc},
2505 {"execfile", builtin_execfile, METH_VARARGS, execfile_doc},
2506 {"filter", builtin_filter, METH_VARARGS, filter_doc},
Eric Smitha9f7d622008-02-17 19:46:49 +00002507 {"format", builtin_format, METH_VARARGS, format_doc},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002508 {"getattr", builtin_getattr, METH_VARARGS, getattr_doc},
2509 {"globals", (PyCFunction)builtin_globals, METH_NOARGS, globals_doc},
2510 {"hasattr", builtin_hasattr, METH_VARARGS, hasattr_doc},
2511 {"hash", builtin_hash, METH_O, hash_doc},
2512 {"hex", builtin_hex, METH_O, hex_doc},
2513 {"id", builtin_id, METH_O, id_doc},
2514 {"input", builtin_input, METH_VARARGS, input_doc},
2515 {"intern", builtin_intern, METH_VARARGS, intern_doc},
2516 {"isinstance", builtin_isinstance, METH_VARARGS, isinstance_doc},
2517 {"issubclass", builtin_issubclass, METH_VARARGS, issubclass_doc},
2518 {"iter", builtin_iter, METH_VARARGS, iter_doc},
2519 {"len", builtin_len, METH_O, len_doc},
2520 {"locals", (PyCFunction)builtin_locals, METH_NOARGS, locals_doc},
2521 {"map", builtin_map, METH_VARARGS, map_doc},
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00002522 {"max", (PyCFunction)builtin_max, METH_VARARGS | METH_KEYWORDS, max_doc},
2523 {"min", (PyCFunction)builtin_min, METH_VARARGS | METH_KEYWORDS, min_doc},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002524 {"oct", builtin_oct, METH_O, oct_doc},
Neal Norwitzc4edb0e2006-05-02 04:43:14 +00002525 {"open", (PyCFunction)builtin_open, METH_VARARGS | METH_KEYWORDS, open_doc},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002526 {"ord", builtin_ord, METH_O, ord_doc},
2527 {"pow", builtin_pow, METH_VARARGS, pow_doc},
Eric Smith7c478942008-03-18 23:45:49 +00002528 {"print", (PyCFunction)builtin_print, METH_VARARGS | METH_KEYWORDS, print_doc},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002529 {"range", builtin_range, METH_VARARGS, range_doc},
2530 {"raw_input", builtin_raw_input, METH_VARARGS, raw_input_doc},
2531 {"reduce", builtin_reduce, METH_VARARGS, reduce_doc},
2532 {"reload", builtin_reload, METH_O, reload_doc},
2533 {"repr", builtin_repr, METH_O, repr_doc},
Georg Brandlccadf842006-03-31 18:54:53 +00002534 {"round", (PyCFunction)builtin_round, METH_VARARGS | METH_KEYWORDS, round_doc},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002535 {"setattr", builtin_setattr, METH_VARARGS, setattr_doc},
Raymond Hettinger64958a12003-12-17 20:43:33 +00002536 {"sorted", (PyCFunction)builtin_sorted, METH_VARARGS | METH_KEYWORDS, sorted_doc},
Alex Martellia70b1912003-04-22 08:12:33 +00002537 {"sum", builtin_sum, METH_VARARGS, sum_doc},
Martin v. Löwis339d0f72001-08-17 18:39:25 +00002538#ifdef Py_USING_UNICODE
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002539 {"unichr", builtin_unichr, METH_VARARGS, unichr_doc},
Martin v. Löwis339d0f72001-08-17 18:39:25 +00002540#endif
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002541 {"vars", builtin_vars, METH_VARARGS, vars_doc},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002542 {"zip", builtin_zip, METH_VARARGS, zip_doc},
Guido van Rossumc02e15c1991-12-16 13:03:00 +00002543 {NULL, NULL},
Guido van Rossum3f5da241990-12-20 15:06:42 +00002544};
2545
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002546PyDoc_STRVAR(builtin_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002547"Built-in functions, exceptions, and other objects.\n\
2548\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002549Noteworthy: None is the `nil' object; Ellipsis represents `...' in slices.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002550
Guido van Rossum25ce5661997-08-02 03:10:38 +00002551PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002552_PyBuiltin_Init(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +00002553{
Fred Drake5550de32000-06-20 04:54:19 +00002554 PyObject *mod, *dict, *debug;
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002555 mod = Py_InitModule4("__builtin__", builtin_methods,
2556 builtin_doc, (PyObject *)NULL,
2557 PYTHON_API_VERSION);
Guido van Rossum25ce5661997-08-02 03:10:38 +00002558 if (mod == NULL)
2559 return NULL;
2560 dict = PyModule_GetDict(mod);
Tim Peters4b7625e2001-09-13 21:37:17 +00002561
Tim Peters7571a0f2003-03-23 17:52:28 +00002562#ifdef Py_TRACE_REFS
2563 /* __builtin__ exposes a number of statically allocated objects
2564 * that, before this code was added in 2.3, never showed up in
2565 * the list of "all objects" maintained by Py_TRACE_REFS. As a
2566 * result, programs leaking references to None and False (etc)
2567 * couldn't be diagnosed by examining sys.getobjects(0).
2568 */
2569#define ADD_TO_ALL(OBJECT) _Py_AddToAllObjects((PyObject *)(OBJECT), 0)
2570#else
2571#define ADD_TO_ALL(OBJECT) (void)0
2572#endif
2573
Tim Peters4b7625e2001-09-13 21:37:17 +00002574#define SETBUILTIN(NAME, OBJECT) \
Tim Peters7571a0f2003-03-23 17:52:28 +00002575 if (PyDict_SetItemString(dict, NAME, (PyObject *)OBJECT) < 0) \
2576 return NULL; \
2577 ADD_TO_ALL(OBJECT)
Tim Peters4b7625e2001-09-13 21:37:17 +00002578
2579 SETBUILTIN("None", Py_None);
2580 SETBUILTIN("Ellipsis", Py_Ellipsis);
2581 SETBUILTIN("NotImplemented", Py_NotImplemented);
Guido van Rossum77f6a652002-04-03 22:41:51 +00002582 SETBUILTIN("False", Py_False);
2583 SETBUILTIN("True", Py_True);
Neal Norwitz32a7e7f2002-05-31 19:58:02 +00002584 SETBUILTIN("basestring", &PyBaseString_Type);
Guido van Rossum77f6a652002-04-03 22:41:51 +00002585 SETBUILTIN("bool", &PyBool_Type);
Travis E. Oliphant3781aef2008-03-18 04:44:57 +00002586 /* SETBUILTIN("memoryview", &PyMemoryView_Type); */
Christian Heimes1a6387e2008-03-26 12:49:49 +00002587 SETBUILTIN("bytearray", &PyBytes_Type);
Christian Heimes288e89a2008-01-18 18:24:07 +00002588 SETBUILTIN("bytes", &PyString_Type);
Guido van Rossumbea18cc2002-06-14 20:41:17 +00002589 SETBUILTIN("buffer", &PyBuffer_Type);
Tim Peters4b7625e2001-09-13 21:37:17 +00002590 SETBUILTIN("classmethod", &PyClassMethod_Type);
Tim Peters6d6c1a32001-08-02 04:15:00 +00002591#ifndef WITHOUT_COMPLEX
Tim Peters4b7625e2001-09-13 21:37:17 +00002592 SETBUILTIN("complex", &PyComplex_Type);
Tim Peters6d6c1a32001-08-02 04:15:00 +00002593#endif
Tim Petersa427a2b2001-10-29 22:25:45 +00002594 SETBUILTIN("dict", &PyDict_Type);
Tim Peters67d687a2002-04-29 21:27:32 +00002595 SETBUILTIN("enumerate", &PyEnum_Type);
Neal Norwitzc4edb0e2006-05-02 04:43:14 +00002596 SETBUILTIN("file", &PyFile_Type);
Tim Peters4b7625e2001-09-13 21:37:17 +00002597 SETBUILTIN("float", &PyFloat_Type);
Raymond Hettingera690a992003-11-16 16:17:49 +00002598 SETBUILTIN("frozenset", &PyFrozenSet_Type);
Tim Peters4b7625e2001-09-13 21:37:17 +00002599 SETBUILTIN("property", &PyProperty_Type);
2600 SETBUILTIN("int", &PyInt_Type);
2601 SETBUILTIN("list", &PyList_Type);
2602 SETBUILTIN("long", &PyLong_Type);
2603 SETBUILTIN("object", &PyBaseObject_Type);
Raymond Hettinger85c20a42003-11-06 14:06:48 +00002604 SETBUILTIN("reversed", &PyReversed_Type);
Raymond Hettingera690a992003-11-16 16:17:49 +00002605 SETBUILTIN("set", &PySet_Type);
Guido van Rossumbea18cc2002-06-14 20:41:17 +00002606 SETBUILTIN("slice", &PySlice_Type);
Tim Peters4b7625e2001-09-13 21:37:17 +00002607 SETBUILTIN("staticmethod", &PyStaticMethod_Type);
2608 SETBUILTIN("str", &PyString_Type);
2609 SETBUILTIN("super", &PySuper_Type);
2610 SETBUILTIN("tuple", &PyTuple_Type);
2611 SETBUILTIN("type", &PyType_Type);
Raymond Hettingerc4c453f2002-06-05 23:12:45 +00002612 SETBUILTIN("xrange", &PyRange_Type);
Martin v. Löwis339d0f72001-08-17 18:39:25 +00002613#ifdef Py_USING_UNICODE
Tim Peters4b7625e2001-09-13 21:37:17 +00002614 SETBUILTIN("unicode", &PyUnicode_Type);
Martin v. Löwis339d0f72001-08-17 18:39:25 +00002615#endif
Guido van Rossum77f6a652002-04-03 22:41:51 +00002616 debug = PyBool_FromLong(Py_OptimizeFlag == 0);
Fred Drake5550de32000-06-20 04:54:19 +00002617 if (PyDict_SetItemString(dict, "__debug__", debug) < 0) {
2618 Py_XDECREF(debug);
Guido van Rossum25ce5661997-08-02 03:10:38 +00002619 return NULL;
Fred Drake5550de32000-06-20 04:54:19 +00002620 }
2621 Py_XDECREF(debug);
Barry Warsaw757af0e1997-08-29 22:13:51 +00002622
Guido van Rossum25ce5661997-08-02 03:10:38 +00002623 return mod;
Tim Peters7571a0f2003-03-23 17:52:28 +00002624#undef ADD_TO_ALL
Tim Peters4b7625e2001-09-13 21:37:17 +00002625#undef SETBUILTIN
Guido van Rossum3f5da241990-12-20 15:06:42 +00002626}
2627
Guido van Rossume77a7571993-11-03 15:01:26 +00002628/* Helper for filter(): filter a tuple through a function */
Guido van Rossum12d12c51993-10-26 17:58:25 +00002629
Guido van Rossum79f25d91997-04-29 20:08:16 +00002630static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002631filtertuple(PyObject *func, PyObject *tuple)
Guido van Rossum12d12c51993-10-26 17:58:25 +00002632{
Guido van Rossum79f25d91997-04-29 20:08:16 +00002633 PyObject *result;
Martin v. Löwis18e16552006-02-15 17:27:45 +00002634 Py_ssize_t i, j;
2635 Py_ssize_t len = PyTuple_Size(tuple);
Guido van Rossum12d12c51993-10-26 17:58:25 +00002636
Guido van Rossumb7b45621995-08-04 04:07:45 +00002637 if (len == 0) {
Walter Dörwaldc3da83f2003-02-04 20:24:45 +00002638 if (PyTuple_CheckExact(tuple))
2639 Py_INCREF(tuple);
2640 else
2641 tuple = PyTuple_New(0);
Guido van Rossumb7b45621995-08-04 04:07:45 +00002642 return tuple;
2643 }
2644
Guido van Rossum79f25d91997-04-29 20:08:16 +00002645 if ((result = PyTuple_New(len)) == NULL)
Guido van Rossum2586bf01993-11-01 16:21:44 +00002646 return NULL;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002647
Guido van Rossum12d12c51993-10-26 17:58:25 +00002648 for (i = j = 0; i < len; ++i) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002649 PyObject *item, *good;
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002650 int ok;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002651
Walter Dörwald8dd19322003-02-10 17:36:40 +00002652 if (tuple->ob_type->tp_as_sequence &&
2653 tuple->ob_type->tp_as_sequence->sq_item) {
2654 item = tuple->ob_type->tp_as_sequence->sq_item(tuple, i);
Walter Dörwaldc58a3a12003-08-18 18:28:45 +00002655 if (item == NULL)
2656 goto Fail_1;
Walter Dörwald8dd19322003-02-10 17:36:40 +00002657 } else {
Alex Martellia9b9c9f2003-04-23 13:34:35 +00002658 PyErr_SetString(PyExc_TypeError, "filter(): unsubscriptable tuple");
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002659 goto Fail_1;
Walter Dörwald8dd19322003-02-10 17:36:40 +00002660 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002661 if (func == Py_None) {
2662 Py_INCREF(item);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002663 good = item;
2664 }
2665 else {
Raymond Hettinger8ae46892003-10-12 19:09:37 +00002666 PyObject *arg = PyTuple_Pack(1, item);
Walter Dörwaldc58a3a12003-08-18 18:28:45 +00002667 if (arg == NULL) {
2668 Py_DECREF(item);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002669 goto Fail_1;
Walter Dörwaldc58a3a12003-08-18 18:28:45 +00002670 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002671 good = PyEval_CallObject(func, arg);
2672 Py_DECREF(arg);
Walter Dörwaldc58a3a12003-08-18 18:28:45 +00002673 if (good == NULL) {
2674 Py_DECREF(item);
Guido van Rossum12d12c51993-10-26 17:58:25 +00002675 goto Fail_1;
Walter Dörwaldc58a3a12003-08-18 18:28:45 +00002676 }
Guido van Rossum12d12c51993-10-26 17:58:25 +00002677 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002678 ok = PyObject_IsTrue(good);
2679 Py_DECREF(good);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002680 if (ok) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002681 if (PyTuple_SetItem(result, j++, item) < 0)
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002682 goto Fail_1;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002683 }
Walter Dörwaldc58a3a12003-08-18 18:28:45 +00002684 else
2685 Py_DECREF(item);
Guido van Rossum12d12c51993-10-26 17:58:25 +00002686 }
2687
Tim Peters4324aa32001-05-28 22:30:08 +00002688 if (_PyTuple_Resize(&result, j) < 0)
Guido van Rossum12d12c51993-10-26 17:58:25 +00002689 return NULL;
2690
Guido van Rossum12d12c51993-10-26 17:58:25 +00002691 return result;
2692
Guido van Rossum12d12c51993-10-26 17:58:25 +00002693Fail_1:
Guido van Rossum79f25d91997-04-29 20:08:16 +00002694 Py_DECREF(result);
Guido van Rossum12d12c51993-10-26 17:58:25 +00002695 return NULL;
2696}
2697
2698
Guido van Rossume77a7571993-11-03 15:01:26 +00002699/* Helper for filter(): filter a string through a function */
Guido van Rossum12d12c51993-10-26 17:58:25 +00002700
Guido van Rossum79f25d91997-04-29 20:08:16 +00002701static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002702filterstring(PyObject *func, PyObject *strobj)
Guido van Rossum12d12c51993-10-26 17:58:25 +00002703{
Guido van Rossum79f25d91997-04-29 20:08:16 +00002704 PyObject *result;
Martin v. Löwis18e16552006-02-15 17:27:45 +00002705 Py_ssize_t i, j;
2706 Py_ssize_t len = PyString_Size(strobj);
2707 Py_ssize_t outlen = len;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002708
Guido van Rossum79f25d91997-04-29 20:08:16 +00002709 if (func == Py_None) {
Walter Dörwald1918f772003-02-10 13:19:13 +00002710 /* If it's a real string we can return the original,
2711 * as no character is ever false and __getitem__
2712 * does return this character. If it's a subclass
2713 * we must go through the __getitem__ loop */
2714 if (PyString_CheckExact(strobj)) {
Walter Dörwaldc3da83f2003-02-04 20:24:45 +00002715 Py_INCREF(strobj);
Walter Dörwald1918f772003-02-10 13:19:13 +00002716 return strobj;
2717 }
Guido van Rossum12d12c51993-10-26 17:58:25 +00002718 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002719 if ((result = PyString_FromStringAndSize(NULL, len)) == NULL)
Guido van Rossum2586bf01993-11-01 16:21:44 +00002720 return NULL;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002721
Guido van Rossum12d12c51993-10-26 17:58:25 +00002722 for (i = j = 0; i < len; ++i) {
Walter Dörwald1918f772003-02-10 13:19:13 +00002723 PyObject *item;
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002724 int ok;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002725
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002726 item = (*strobj->ob_type->tp_as_sequence->sq_item)(strobj, i);
2727 if (item == NULL)
2728 goto Fail_1;
Walter Dörwald1918f772003-02-10 13:19:13 +00002729 if (func==Py_None) {
2730 ok = 1;
2731 } else {
2732 PyObject *arg, *good;
Raymond Hettinger8ae46892003-10-12 19:09:37 +00002733 arg = PyTuple_Pack(1, item);
Walter Dörwald1918f772003-02-10 13:19:13 +00002734 if (arg == NULL) {
2735 Py_DECREF(item);
2736 goto Fail_1;
2737 }
2738 good = PyEval_CallObject(func, arg);
2739 Py_DECREF(arg);
2740 if (good == NULL) {
2741 Py_DECREF(item);
2742 goto Fail_1;
2743 }
2744 ok = PyObject_IsTrue(good);
2745 Py_DECREF(good);
Tim Peters388ed082001-04-07 20:34:48 +00002746 }
Walter Dörwald903f1e02003-02-04 16:28:00 +00002747 if (ok) {
Martin v. Löwisd96ee902006-02-16 14:37:16 +00002748 Py_ssize_t reslen;
Walter Dörwald903f1e02003-02-04 16:28:00 +00002749 if (!PyString_Check(item)) {
2750 PyErr_SetString(PyExc_TypeError, "can't filter str to str:"
2751 " __getitem__ returned different type");
2752 Py_DECREF(item);
2753 goto Fail_1;
2754 }
2755 reslen = PyString_GET_SIZE(item);
2756 if (reslen == 1) {
2757 PyString_AS_STRING(result)[j++] =
2758 PyString_AS_STRING(item)[0];
2759 } else {
2760 /* do we need more space? */
Martin v. Löwisd96ee902006-02-16 14:37:16 +00002761 Py_ssize_t need = j + reslen + len-i-1;
Walter Dörwald903f1e02003-02-04 16:28:00 +00002762 if (need > outlen) {
2763 /* overallocate, to avoid reallocations */
2764 if (need<2*outlen)
2765 need = 2*outlen;
2766 if (_PyString_Resize(&result, need)) {
2767 Py_DECREF(item);
2768 return NULL;
2769 }
2770 outlen = need;
2771 }
2772 memcpy(
2773 PyString_AS_STRING(result) + j,
2774 PyString_AS_STRING(item),
2775 reslen
2776 );
2777 j += reslen;
2778 }
2779 }
Tim Peters388ed082001-04-07 20:34:48 +00002780 Py_DECREF(item);
Guido van Rossum12d12c51993-10-26 17:58:25 +00002781 }
2782
Walter Dörwald903f1e02003-02-04 16:28:00 +00002783 if (j < outlen)
Tim Peters5de98422002-04-27 18:44:32 +00002784 _PyString_Resize(&result, j);
Guido van Rossum12d12c51993-10-26 17:58:25 +00002785
Guido van Rossum12d12c51993-10-26 17:58:25 +00002786 return result;
2787
Guido van Rossum12d12c51993-10-26 17:58:25 +00002788Fail_1:
Guido van Rossum79f25d91997-04-29 20:08:16 +00002789 Py_DECREF(result);
Guido van Rossum12d12c51993-10-26 17:58:25 +00002790 return NULL;
2791}
Martin v. Löwis8afd7572003-01-25 22:46:11 +00002792
2793#ifdef Py_USING_UNICODE
2794/* Helper for filter(): filter a Unicode object through a function */
2795
2796static PyObject *
2797filterunicode(PyObject *func, PyObject *strobj)
2798{
2799 PyObject *result;
Martin v. Löwis725507b2006-03-07 12:08:51 +00002800 register Py_ssize_t i, j;
Martin v. Löwisd96ee902006-02-16 14:37:16 +00002801 Py_ssize_t len = PyUnicode_GetSize(strobj);
2802 Py_ssize_t outlen = len;
Martin v. Löwis8afd7572003-01-25 22:46:11 +00002803
2804 if (func == Py_None) {
Walter Dörwald1918f772003-02-10 13:19:13 +00002805 /* If it's a real string we can return the original,
2806 * as no character is ever false and __getitem__
2807 * does return this character. If it's a subclass
2808 * we must go through the __getitem__ loop */
2809 if (PyUnicode_CheckExact(strobj)) {
Walter Dörwaldc3da83f2003-02-04 20:24:45 +00002810 Py_INCREF(strobj);
Walter Dörwald1918f772003-02-10 13:19:13 +00002811 return strobj;
2812 }
Martin v. Löwis8afd7572003-01-25 22:46:11 +00002813 }
2814 if ((result = PyUnicode_FromUnicode(NULL, len)) == NULL)
2815 return NULL;
2816
2817 for (i = j = 0; i < len; ++i) {
2818 PyObject *item, *arg, *good;
2819 int ok;
2820
2821 item = (*strobj->ob_type->tp_as_sequence->sq_item)(strobj, i);
2822 if (item == NULL)
2823 goto Fail_1;
Walter Dörwald1918f772003-02-10 13:19:13 +00002824 if (func == Py_None) {
2825 ok = 1;
2826 } else {
Raymond Hettinger8ae46892003-10-12 19:09:37 +00002827 arg = PyTuple_Pack(1, item);
Walter Dörwald1918f772003-02-10 13:19:13 +00002828 if (arg == NULL) {
2829 Py_DECREF(item);
2830 goto Fail_1;
2831 }
2832 good = PyEval_CallObject(func, arg);
2833 Py_DECREF(arg);
2834 if (good == NULL) {
2835 Py_DECREF(item);
2836 goto Fail_1;
2837 }
2838 ok = PyObject_IsTrue(good);
2839 Py_DECREF(good);
Martin v. Löwis8afd7572003-01-25 22:46:11 +00002840 }
Walter Dörwald903f1e02003-02-04 16:28:00 +00002841 if (ok) {
Martin v. Löwisd96ee902006-02-16 14:37:16 +00002842 Py_ssize_t reslen;
Walter Dörwald903f1e02003-02-04 16:28:00 +00002843 if (!PyUnicode_Check(item)) {
Georg Brandl99d7e4e2005-08-31 22:21:15 +00002844 PyErr_SetString(PyExc_TypeError,
Jeremy Hylton1aad9c72003-09-16 03:10:59 +00002845 "can't filter unicode to unicode:"
2846 " __getitem__ returned different type");
Walter Dörwald903f1e02003-02-04 16:28:00 +00002847 Py_DECREF(item);
2848 goto Fail_1;
2849 }
2850 reslen = PyUnicode_GET_SIZE(item);
Georg Brandl99d7e4e2005-08-31 22:21:15 +00002851 if (reslen == 1)
Walter Dörwald903f1e02003-02-04 16:28:00 +00002852 PyUnicode_AS_UNICODE(result)[j++] =
2853 PyUnicode_AS_UNICODE(item)[0];
Jeremy Hylton1aad9c72003-09-16 03:10:59 +00002854 else {
Walter Dörwald903f1e02003-02-04 16:28:00 +00002855 /* do we need more space? */
Martin v. Löwisd96ee902006-02-16 14:37:16 +00002856 Py_ssize_t need = j + reslen + len - i - 1;
Walter Dörwald903f1e02003-02-04 16:28:00 +00002857 if (need > outlen) {
Georg Brandl99d7e4e2005-08-31 22:21:15 +00002858 /* overallocate,
Jeremy Hylton1aad9c72003-09-16 03:10:59 +00002859 to avoid reallocations */
2860 if (need < 2 * outlen)
2861 need = 2 * outlen;
Jeremy Hylton364f6be2003-09-16 03:17:16 +00002862 if (PyUnicode_Resize(
2863 &result, need) < 0) {
Walter Dörwald903f1e02003-02-04 16:28:00 +00002864 Py_DECREF(item);
Walter Dörwald531e0002003-02-04 16:57:49 +00002865 goto Fail_1;
Walter Dörwald903f1e02003-02-04 16:28:00 +00002866 }
2867 outlen = need;
2868 }
Jeremy Hylton1aad9c72003-09-16 03:10:59 +00002869 memcpy(PyUnicode_AS_UNICODE(result) + j,
2870 PyUnicode_AS_UNICODE(item),
2871 reslen*sizeof(Py_UNICODE));
Walter Dörwald903f1e02003-02-04 16:28:00 +00002872 j += reslen;
2873 }
2874 }
Martin v. Löwis8afd7572003-01-25 22:46:11 +00002875 Py_DECREF(item);
2876 }
2877
Walter Dörwald903f1e02003-02-04 16:28:00 +00002878 if (j < outlen)
Martin v. Löwis8afd7572003-01-25 22:46:11 +00002879 PyUnicode_Resize(&result, j);
2880
2881 return result;
2882
2883Fail_1:
2884 Py_DECREF(result);
2885 return NULL;
2886}
2887#endif