blob: 0c3d6e2c01f9f542f259e5d4e241449495c96673 [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"
Guido van Rossum3f5da241990-12-20 15:06:42 +00004
5#include "node.h"
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006#include "code.h"
Guido van Rossum5b722181993-03-30 17:46:03 +00007#include "eval.h"
Guido van Rossum3f5da241990-12-20 15:06:42 +00008
Guido van Rossum6bf62da1997-04-11 20:37:35 +00009#include <ctype.h>
10
Guido van Rossume2ae77b2001-10-24 20:42:55 +000011#ifdef RISCOS
12#include "unixstuff.h"
13#endif
14
Mark Hammond26cffde2001-05-14 12:17:34 +000015/* The default encoding used by the platform file system APIs
16 Can remain NULL for all platforms that don't have such a concept
17*/
Martin v. Löwis6238d2b2002-06-30 15:26:10 +000018#if defined(MS_WINDOWS) && defined(HAVE_USABLE_WCHAR_T)
Mark Hammond26cffde2001-05-14 12:17:34 +000019const char *Py_FileSystemDefaultEncoding = "mbcs";
Just van Rossumb9b8e9c2003-02-10 09:22:01 +000020#elif defined(__APPLE__)
21const char *Py_FileSystemDefaultEncoding = "utf-8";
Mark Hammond26cffde2001-05-14 12:17:34 +000022#else
23const char *Py_FileSystemDefaultEncoding = NULL; /* use default */
24#endif
Mark Hammondef8b6542001-05-13 08:04:26 +000025
Guido van Rossum12d12c51993-10-26 17:58:25 +000026/* Forward */
Tim Petersdbd9ba62000-07-09 03:09:57 +000027static PyObject *filterstring(PyObject *, PyObject *);
Martin v. Löwis8afd7572003-01-25 22:46:11 +000028#ifdef Py_USING_UNICODE
29static PyObject *filterunicode(PyObject *, PyObject *);
30#endif
Tim Petersdbd9ba62000-07-09 03:09:57 +000031static PyObject *filtertuple (PyObject *, PyObject *);
Guido van Rossum12d12c51993-10-26 17:58:25 +000032
Guido van Rossum79f25d91997-04-29 20:08:16 +000033static PyObject *
Neal Norwitz92e212f2006-04-03 04:48:37 +000034builtin___import__(PyObject *self, PyObject *args, PyObject *kwds)
Guido van Rossum3f5da241990-12-20 15:06:42 +000035{
Neal Norwitz92e212f2006-04-03 04:48:37 +000036 static char *kwlist[] = {"name", "globals", "locals", "fromlist",
37 "level", 0};
Guido van Rossum1ae940a1995-01-02 19:04:15 +000038 char *name;
Guido van Rossum79f25d91997-04-29 20:08:16 +000039 PyObject *globals = NULL;
40 PyObject *locals = NULL;
41 PyObject *fromlist = NULL;
Thomas Woutersf7f438b2006-02-28 16:09:29 +000042 int level = -1;
Guido van Rossum1ae940a1995-01-02 19:04:15 +000043
Neal Norwitz92e212f2006-04-03 04:48:37 +000044 if (!PyArg_ParseTupleAndKeywords(args, kwds, "s|OOOi:__import__",
45 kwlist, &name, &globals, &locals, &fromlist, &level))
Guido van Rossum1ae940a1995-01-02 19:04:15 +000046 return NULL;
Thomas Woutersf7f438b2006-02-28 16:09:29 +000047 return PyImport_ImportModuleLevel(name, globals, locals,
48 fromlist, level);
Guido van Rossum1ae940a1995-01-02 19:04:15 +000049}
50
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000051PyDoc_STRVAR(import_doc,
Neal Norwitz92e212f2006-04-03 04:48:37 +000052"__import__(name, globals={}, locals={}, fromlist=[], level=-1) -> module\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +000053\n\
54Import a module. The globals are only used to determine the context;\n\
55they are not modified. The locals are currently unused. The fromlist\n\
56should be a list of names to emulate ``from name import ...'', or an\n\
57empty list to emulate ``import name''.\n\
58When importing a module from a package, note that __import__('A.B', ...)\n\
59returns package A when fromlist is empty, but its submodule B when\n\
Neal Norwitz92e212f2006-04-03 04:48:37 +000060fromlist is not empty. Level is used to determine whether to perform \n\
61absolute or relative imports. -1 is the original strategy of attempting\n\
62both absolute and relative imports, 0 is absolute, a positive number\n\
63is the number of parent directories to search relative to the current module.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +000064
Guido van Rossum1ae940a1995-01-02 19:04:15 +000065
Guido van Rossum79f25d91997-04-29 20:08:16 +000066static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +000067builtin_abs(PyObject *self, PyObject *v)
Guido van Rossum1ae940a1995-01-02 19:04:15 +000068{
Guido van Rossum09df08a1998-05-22 00:51:39 +000069 return PyNumber_Absolute(v);
Guido van Rossum3f5da241990-12-20 15:06:42 +000070}
71
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000072PyDoc_STRVAR(abs_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +000073"abs(number) -> number\n\
74\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000075Return the absolute value of the argument.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +000076
Raymond Hettinger96229b12005-03-11 06:49:40 +000077static PyObject *
78builtin_all(PyObject *self, PyObject *v)
79{
80 PyObject *it, *item;
Guido van Rossum01dbc102007-12-20 23:48:28 +000081 PyObject *(*iternext)(PyObject *);
82 int cmp;
Raymond Hettinger96229b12005-03-11 06:49:40 +000083
84 it = PyObject_GetIter(v);
85 if (it == NULL)
86 return NULL;
Guido van Rossum01dbc102007-12-20 23:48:28 +000087 iternext = *Py_TYPE(it)->tp_iternext;
Raymond Hettinger96229b12005-03-11 06:49:40 +000088
Guido van Rossum01dbc102007-12-20 23:48:28 +000089 for (;;) {
90 item = iternext(it);
91 if (item == NULL)
92 break;
93 cmp = PyObject_IsTrue(item);
Raymond Hettinger96229b12005-03-11 06:49:40 +000094 Py_DECREF(item);
95 if (cmp < 0) {
96 Py_DECREF(it);
97 return NULL;
98 }
99 if (cmp == 0) {
100 Py_DECREF(it);
101 Py_RETURN_FALSE;
102 }
103 }
104 Py_DECREF(it);
Guido van Rossum01dbc102007-12-20 23:48:28 +0000105 if (PyErr_Occurred()) {
106 if (PyErr_ExceptionMatches(PyExc_StopIteration))
107 PyErr_Clear();
108 else
109 return NULL;
110 }
Raymond Hettinger96229b12005-03-11 06:49:40 +0000111 Py_RETURN_TRUE;
112}
113
114PyDoc_STRVAR(all_doc,
115"all(iterable) -> bool\n\
116\n\
117Return True if bool(x) is True for all values x in the iterable.");
118
119static PyObject *
120builtin_any(PyObject *self, PyObject *v)
121{
122 PyObject *it, *item;
Guido van Rossum01dbc102007-12-20 23:48:28 +0000123 PyObject *(*iternext)(PyObject *);
124 int cmp;
Raymond Hettinger96229b12005-03-11 06:49:40 +0000125
126 it = PyObject_GetIter(v);
127 if (it == NULL)
128 return NULL;
Guido van Rossum01dbc102007-12-20 23:48:28 +0000129 iternext = *Py_TYPE(it)->tp_iternext;
Raymond Hettinger96229b12005-03-11 06:49:40 +0000130
Guido van Rossum01dbc102007-12-20 23:48:28 +0000131 for (;;) {
132 item = iternext(it);
133 if (item == NULL)
134 break;
135 cmp = PyObject_IsTrue(item);
Raymond Hettinger96229b12005-03-11 06:49:40 +0000136 Py_DECREF(item);
137 if (cmp < 0) {
138 Py_DECREF(it);
139 return NULL;
140 }
141 if (cmp == 1) {
142 Py_DECREF(it);
143 Py_RETURN_TRUE;
144 }
145 }
146 Py_DECREF(it);
Guido van Rossum01dbc102007-12-20 23:48:28 +0000147 if (PyErr_Occurred()) {
148 if (PyErr_ExceptionMatches(PyExc_StopIteration))
149 PyErr_Clear();
150 else
151 return NULL;
152 }
Raymond Hettinger96229b12005-03-11 06:49:40 +0000153 Py_RETURN_FALSE;
154}
155
156PyDoc_STRVAR(any_doc,
157"any(iterable) -> bool\n\
158\n\
159Return True if bool(x) is True for any x in the iterable.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000160
Guido van Rossum79f25d91997-04-29 20:08:16 +0000161static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000162builtin_apply(PyObject *self, PyObject *args)
Guido van Rossumc02e15c1991-12-16 13:03:00 +0000163{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000164 PyObject *func, *alist = NULL, *kwdict = NULL;
Barry Warsaw968f8cb1998-10-01 15:33:12 +0000165 PyObject *t = NULL, *retval = NULL;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000166
Neal Norwitz8b2bfbc2007-05-23 06:35:32 +0000167 if (Py_Py3kWarningFlag &&
168 PyErr_Warn(PyExc_DeprecationWarning,
Neal Norwitzb93e7d12008-02-24 02:40:58 +0000169 "apply() not supported in 3.x. Use func(*args, **kwargs).") < 0)
Neal Norwitz8b2bfbc2007-05-23 06:35:32 +0000170 return NULL;
171
Raymond Hettingerea3fdf42002-12-29 16:33:45 +0000172 if (!PyArg_UnpackTuple(args, "apply", 1, 3, &func, &alist, &kwdict))
Guido van Rossumc02e15c1991-12-16 13:03:00 +0000173 return NULL;
Barry Warsaw968f8cb1998-10-01 15:33:12 +0000174 if (alist != NULL) {
175 if (!PyTuple_Check(alist)) {
176 if (!PySequence_Check(alist)) {
Jeremy Hyltonc862cf42001-01-19 03:25:05 +0000177 PyErr_Format(PyExc_TypeError,
Alex Martellia9b9c9f2003-04-23 13:34:35 +0000178 "apply() arg 2 expected sequence, found %s",
Jeremy Hyltonc862cf42001-01-19 03:25:05 +0000179 alist->ob_type->tp_name);
Barry Warsaw968f8cb1998-10-01 15:33:12 +0000180 return NULL;
181 }
182 t = PySequence_Tuple(alist);
183 if (t == NULL)
184 return NULL;
185 alist = t;
186 }
Guido van Rossum2d951851994-08-29 12:52:16 +0000187 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000188 if (kwdict != NULL && !PyDict_Check(kwdict)) {
Jeremy Hyltonc862cf42001-01-19 03:25:05 +0000189 PyErr_Format(PyExc_TypeError,
190 "apply() arg 3 expected dictionary, found %s",
191 kwdict->ob_type->tp_name);
Barry Warsaw968f8cb1998-10-01 15:33:12 +0000192 goto finally;
Guido van Rossum681d79a1995-07-18 14:51:37 +0000193 }
Barry Warsaw968f8cb1998-10-01 15:33:12 +0000194 retval = PyEval_CallObjectWithKeywords(func, alist, kwdict);
195 finally:
196 Py_XDECREF(t);
197 return retval;
Guido van Rossumc02e15c1991-12-16 13:03:00 +0000198}
199
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000200PyDoc_STRVAR(apply_doc,
Fred Drakef1fbc622001-01-12 17:05:05 +0000201"apply(object[, args[, kwargs]]) -> value\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000202\n\
Fred Drake7b912121999-12-23 14:16:55 +0000203Call a callable object with positional arguments taken from the tuple args,\n\
204and keyword arguments taken from the optional dictionary kwargs.\n\
Raymond Hettingerff41c482003-04-06 09:01:11 +0000205Note that classes are callable, as are instances with a __call__() method.\n\
206\n\
207Deprecated since release 2.3. Instead, use the extended call syntax:\n\
208 function(*args, **keywords).");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000209
210
Guido van Rossum79f25d91997-04-29 20:08:16 +0000211static PyObject *
Eric Smith3cd81942008-02-22 16:30:22 +0000212builtin_bin(PyObject *self, PyObject *v)
213{
214 return PyNumber_ToBase(v, 2);
215}
216
217PyDoc_STRVAR(bin_doc,
218"bin(number) -> string\n\
219\n\
220Return the binary representation of an integer or long integer.");
221
222
223static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000224builtin_callable(PyObject *self, PyObject *v)
Guido van Rossum2d951851994-08-29 12:52:16 +0000225{
Neal Norwitzdf25efe2007-05-23 06:58:36 +0000226 if (Py_Py3kWarningFlag &&
227 PyErr_Warn(PyExc_DeprecationWarning,
Neal Norwitzb93e7d12008-02-24 02:40:58 +0000228 "callable() not supported in 3.x. Use hasattr(o, '__call__').") < 0)
Neal Norwitzdf25efe2007-05-23 06:58:36 +0000229 return NULL;
Guido van Rossum77f6a652002-04-03 22:41:51 +0000230 return PyBool_FromLong((long)PyCallable_Check(v));
Guido van Rossum2d951851994-08-29 12:52:16 +0000231}
232
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000233PyDoc_STRVAR(callable_doc,
Guido van Rossum77f6a652002-04-03 22:41:51 +0000234"callable(object) -> bool\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000235\n\
236Return whether the object is callable (i.e., some kind of function).\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000237Note that classes are callable, as are instances with a __call__() method.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000238
239
Guido van Rossum79f25d91997-04-29 20:08:16 +0000240static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000241builtin_filter(PyObject *self, PyObject *args)
Guido van Rossum12d12c51993-10-26 17:58:25 +0000242{
Guido van Rossumc7903a12002-08-16 07:04:56 +0000243 PyObject *func, *seq, *result, *it, *arg;
Martin v. Löwisd96ee902006-02-16 14:37:16 +0000244 Py_ssize_t len; /* guess for result list size */
245 register Py_ssize_t j;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000246
Raymond Hettingerea3fdf42002-12-29 16:33:45 +0000247 if (!PyArg_UnpackTuple(args, "filter", 2, 2, &func, &seq))
Guido van Rossum12d12c51993-10-26 17:58:25 +0000248 return NULL;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000249
Tim Peters0e57abf2001-05-02 07:39:38 +0000250 /* Strings and tuples return a result of the same type. */
251 if (PyString_Check(seq))
252 return filterstring(func, seq);
Martin v. Löwis8afd7572003-01-25 22:46:11 +0000253#ifdef Py_USING_UNICODE
254 if (PyUnicode_Check(seq))
255 return filterunicode(func, seq);
256#endif
Tim Peters0e57abf2001-05-02 07:39:38 +0000257 if (PyTuple_Check(seq))
258 return filtertuple(func, seq);
259
Georg Brandle35b6572005-07-19 22:20:20 +0000260 /* Pre-allocate argument list tuple. */
261 arg = PyTuple_New(1);
262 if (arg == NULL)
263 return NULL;
264
Tim Peters0e57abf2001-05-02 07:39:38 +0000265 /* Get iterator. */
266 it = PyObject_GetIter(seq);
267 if (it == NULL)
Georg Brandle35b6572005-07-19 22:20:20 +0000268 goto Fail_arg;
Tim Peters0e57abf2001-05-02 07:39:38 +0000269
270 /* Guess a result list size. */
Raymond Hettinger4e2f7142007-12-06 00:56:53 +0000271 len = _PyObject_LengthHint(seq, 8);
Guido van Rossum12d12c51993-10-26 17:58:25 +0000272
Tim Peters0e57abf2001-05-02 07:39:38 +0000273 /* Get a result list. */
Guido van Rossum79f25d91997-04-29 20:08:16 +0000274 if (PyList_Check(seq) && seq->ob_refcnt == 1) {
Tim Peters0e57abf2001-05-02 07:39:38 +0000275 /* Eww - can modify the list in-place. */
Guido van Rossum79f25d91997-04-29 20:08:16 +0000276 Py_INCREF(seq);
Guido van Rossum12d12c51993-10-26 17:58:25 +0000277 result = seq;
278 }
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000279 else {
Tim Peters0e57abf2001-05-02 07:39:38 +0000280 result = PyList_New(len);
281 if (result == NULL)
282 goto Fail_it;
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000283 }
Guido van Rossum12d12c51993-10-26 17:58:25 +0000284
Tim Peters0e57abf2001-05-02 07:39:38 +0000285 /* Build the result list. */
Tim Petersf4848da2001-05-05 00:14:56 +0000286 j = 0;
287 for (;;) {
Guido van Rossumc7903a12002-08-16 07:04:56 +0000288 PyObject *item;
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000289 int ok;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000290
Tim Peters0e57abf2001-05-02 07:39:38 +0000291 item = PyIter_Next(it);
292 if (item == NULL) {
Tim Petersf4848da2001-05-05 00:14:56 +0000293 if (PyErr_Occurred())
294 goto Fail_result_it;
Tim Peters0e57abf2001-05-02 07:39:38 +0000295 break;
Guido van Rossum2d951851994-08-29 12:52:16 +0000296 }
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000297
Neil Schemenauer68973552003-08-14 20:37:34 +0000298 if (func == (PyObject *)&PyBool_Type || func == Py_None) {
David Wolever8e6ec2f2008-03-18 21:20:25 +0000299 if (Py_Py3kWarningFlag &&
300 PyErr_Warn(PyExc_DeprecationWarning,
301 "filter with None as a first argument "
302 "is not supported in 3.x. Use a list "
303 "comprehension instead.") < 0)
304 return NULL;
305
Guido van Rossumc7903a12002-08-16 07:04:56 +0000306 ok = PyObject_IsTrue(item);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000307 }
308 else {
Guido van Rossumc7903a12002-08-16 07:04:56 +0000309 PyObject *good;
310 PyTuple_SET_ITEM(arg, 0, item);
311 good = PyObject_Call(func, arg, NULL);
312 PyTuple_SET_ITEM(arg, 0, NULL);
Guido van Rossum58b68731995-01-10 17:40:55 +0000313 if (good == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000314 Py_DECREF(item);
Tim Peters0e57abf2001-05-02 07:39:38 +0000315 goto Fail_result_it;
Guido van Rossum58b68731995-01-10 17:40:55 +0000316 }
Guido van Rossumc7903a12002-08-16 07:04:56 +0000317 ok = PyObject_IsTrue(good);
318 Py_DECREF(good);
Guido van Rossum12d12c51993-10-26 17:58:25 +0000319 }
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000320 if (ok) {
Tim Peters0e57abf2001-05-02 07:39:38 +0000321 if (j < len)
322 PyList_SET_ITEM(result, j, item);
Guido van Rossum2d951851994-08-29 12:52:16 +0000323 else {
Barry Warsawfa77e091999-01-28 18:49:12 +0000324 int status = PyList_Append(result, item);
Barry Warsawfa77e091999-01-28 18:49:12 +0000325 Py_DECREF(item);
326 if (status < 0)
Tim Peters0e57abf2001-05-02 07:39:38 +0000327 goto Fail_result_it;
Guido van Rossum2d951851994-08-29 12:52:16 +0000328 }
Tim Peters0e57abf2001-05-02 07:39:38 +0000329 ++j;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000330 }
Tim Peters0e57abf2001-05-02 07:39:38 +0000331 else
332 Py_DECREF(item);
Guido van Rossum12d12c51993-10-26 17:58:25 +0000333 }
334
Guido van Rossum12d12c51993-10-26 17:58:25 +0000335
Tim Peters0e57abf2001-05-02 07:39:38 +0000336 /* Cut back result list if len is too big. */
Guido van Rossum79f25d91997-04-29 20:08:16 +0000337 if (j < len && PyList_SetSlice(result, j, len, NULL) < 0)
Tim Peters0e57abf2001-05-02 07:39:38 +0000338 goto Fail_result_it;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000339
Tim Peters3c6b1482001-05-21 08:07:05 +0000340 Py_DECREF(it);
Guido van Rossumc7903a12002-08-16 07:04:56 +0000341 Py_DECREF(arg);
Guido van Rossum12d12c51993-10-26 17:58:25 +0000342 return result;
343
Tim Peters0e57abf2001-05-02 07:39:38 +0000344Fail_result_it:
Guido van Rossum79f25d91997-04-29 20:08:16 +0000345 Py_DECREF(result);
Tim Peters0e57abf2001-05-02 07:39:38 +0000346Fail_it:
347 Py_DECREF(it);
Guido van Rossumc7903a12002-08-16 07:04:56 +0000348Fail_arg:
349 Py_DECREF(arg);
Guido van Rossum12d12c51993-10-26 17:58:25 +0000350 return NULL;
351}
352
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000353PyDoc_STRVAR(filter_doc,
Tim Petersd50e5442002-03-09 00:06:26 +0000354"filter(function or None, sequence) -> list, tuple, or string\n"
355"\n"
356"Return those items of sequence for which function(item) is true. If\n"
357"function is None, return the items that are true. If sequence is a tuple\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000358"or string, return the same type, else return a list.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000359
Guido van Rossum79f25d91997-04-29 20:08:16 +0000360static PyObject *
Eric Smitha9f7d622008-02-17 19:46:49 +0000361builtin_format(PyObject *self, PyObject *args)
362{
363 PyObject *value;
364 PyObject *format_spec = NULL;
365
366 if (!PyArg_ParseTuple(args, "O|O:format", &value, &format_spec))
367 return NULL;
368
369 return PyObject_Format(value, format_spec);
370}
371
372PyDoc_STRVAR(format_doc,
373"format(value[, format_spec]) -> string\n\
374\n\
375Returns value.__format__(format_spec)\n\
376format_spec defaults to \"\"");
377
378static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000379builtin_chr(PyObject *self, PyObject *args)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000380{
381 long x;
382 char s[1];
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000383
Guido van Rossum79f25d91997-04-29 20:08:16 +0000384 if (!PyArg_ParseTuple(args, "l:chr", &x))
Guido van Rossum3f5da241990-12-20 15:06:42 +0000385 return NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000386 if (x < 0 || x >= 256) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000387 PyErr_SetString(PyExc_ValueError,
388 "chr() arg not in range(256)");
Guido van Rossum3f5da241990-12-20 15:06:42 +0000389 return NULL;
390 }
Guido van Rossum6bf62da1997-04-11 20:37:35 +0000391 s[0] = (char)x;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000392 return PyString_FromStringAndSize(s, 1);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000393}
394
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000395PyDoc_STRVAR(chr_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000396"chr(i) -> character\n\
397\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000398Return a string of one character with ordinal i; 0 <= i < 256.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000399
400
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000401#ifdef Py_USING_UNICODE
Guido van Rossum79f25d91997-04-29 20:08:16 +0000402static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000403builtin_unichr(PyObject *self, PyObject *args)
Guido van Rossum09095f32000-03-10 23:00:52 +0000404{
405 long x;
Guido van Rossum09095f32000-03-10 23:00:52 +0000406
407 if (!PyArg_ParseTuple(args, "l:unichr", &x))
408 return NULL;
Fredrik Lundh0dcf67e2001-06-26 20:01:56 +0000409
Marc-André Lemburgcc8764c2002-08-11 12:23:04 +0000410 return PyUnicode_FromOrdinal(x);
Guido van Rossum09095f32000-03-10 23:00:52 +0000411}
412
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000413PyDoc_STRVAR(unichr_doc,
Fred Drake078b24f2000-04-13 02:42:50 +0000414"unichr(i) -> Unicode character\n\
Guido van Rossum09095f32000-03-10 23:00:52 +0000415\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000416Return a Unicode string of one character with ordinal i; 0 <= i <= 0x10ffff.");
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000417#endif
Guido van Rossum09095f32000-03-10 23:00:52 +0000418
419
420static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000421builtin_cmp(PyObject *self, PyObject *args)
Guido van Rossuma9e7dc11992-10-18 18:53:57 +0000422{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000423 PyObject *a, *b;
Guido van Rossum09df08a1998-05-22 00:51:39 +0000424 int c;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000425
Raymond Hettingerea3fdf42002-12-29 16:33:45 +0000426 if (!PyArg_UnpackTuple(args, "cmp", 2, 2, &a, &b))
Guido van Rossuma9e7dc11992-10-18 18:53:57 +0000427 return NULL;
Guido van Rossum09df08a1998-05-22 00:51:39 +0000428 if (PyObject_Cmp(a, b, &c) < 0)
Guido van Rossumc8b6df91997-05-23 00:06:51 +0000429 return NULL;
Guido van Rossum09df08a1998-05-22 00:51:39 +0000430 return PyInt_FromLong((long)c);
Guido van Rossuma9e7dc11992-10-18 18:53:57 +0000431}
432
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000433PyDoc_STRVAR(cmp_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000434"cmp(x, y) -> integer\n\
435\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000436Return negative if x<y, zero if x==y, positive if x>y.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000437
438
Guido van Rossum79f25d91997-04-29 20:08:16 +0000439static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000440builtin_coerce(PyObject *self, PyObject *args)
Guido van Rossum6a00cd81995-01-07 12:39:01 +0000441{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000442 PyObject *v, *w;
443 PyObject *res;
Guido van Rossum5524a591995-01-10 15:26:20 +0000444
Neal Norwitzdf25efe2007-05-23 06:58:36 +0000445 if (Py_Py3kWarningFlag &&
446 PyErr_Warn(PyExc_DeprecationWarning,
447 "coerce() not supported in 3.x") < 0)
448 return NULL;
449
Raymond Hettingerea3fdf42002-12-29 16:33:45 +0000450 if (!PyArg_UnpackTuple(args, "coerce", 2, 2, &v, &w))
Guido van Rossum5524a591995-01-10 15:26:20 +0000451 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000452 if (PyNumber_Coerce(&v, &w) < 0)
Guido van Rossum04691fc1992-08-12 15:35:34 +0000453 return NULL;
Raymond Hettinger8ae46892003-10-12 19:09:37 +0000454 res = PyTuple_Pack(2, v, w);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000455 Py_DECREF(v);
456 Py_DECREF(w);
Guido van Rossum04691fc1992-08-12 15:35:34 +0000457 return res;
458}
459
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000460PyDoc_STRVAR(coerce_doc,
Martin v. Löwis8d494f32004-08-25 10:42:41 +0000461"coerce(x, y) -> (x1, y1)\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000462\n\
Martin v. Löwis8d494f32004-08-25 10:42:41 +0000463Return a tuple consisting of the two numeric arguments converted to\n\
464a common type, using the same rules as used by arithmetic operations.\n\
465If coercion is not possible, raise TypeError.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000466
Guido van Rossum79f25d91997-04-29 20:08:16 +0000467static PyObject *
Georg Brandl5240d742007-03-13 20:46:32 +0000468builtin_compile(PyObject *self, PyObject *args, PyObject *kwds)
Guido van Rossum5b722181993-03-30 17:46:03 +0000469{
470 char *str;
471 char *filename;
472 char *startstr;
473 int start;
Tim Peters6cd6a822001-08-17 22:11:27 +0000474 int dont_inherit = 0;
475 int supplied_flags = 0;
Tim Peters5ba58662001-07-16 02:29:45 +0000476 PyCompilerFlags cf;
Neal Norwitz3a9a3e72005-11-27 20:38:31 +0000477 PyObject *result = NULL, *cmd, *tmp = NULL;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000478 Py_ssize_t length;
Georg Brandl5240d742007-03-13 20:46:32 +0000479 static char *kwlist[] = {"source", "filename", "mode", "flags",
480 "dont_inherit", NULL};
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000481
Georg Brandl5240d742007-03-13 20:46:32 +0000482 if (!PyArg_ParseTupleAndKeywords(args, kwds, "Oss|ii:compile",
483 kwlist, &cmd, &filename, &startstr,
484 &supplied_flags, &dont_inherit))
Guido van Rossum5b722181993-03-30 17:46:03 +0000485 return NULL;
Tim Peters6cd6a822001-08-17 22:11:27 +0000486
Just van Rossum3aaf42c2003-02-10 08:21:10 +0000487 cf.cf_flags = supplied_flags;
488
489#ifdef Py_USING_UNICODE
490 if (PyUnicode_Check(cmd)) {
491 tmp = PyUnicode_AsUTF8String(cmd);
492 if (tmp == NULL)
493 return NULL;
494 cmd = tmp;
495 cf.cf_flags |= PyCF_SOURCE_IS_UTF8;
496 }
497#endif
Just van Rossumb9b8e9c2003-02-10 09:22:01 +0000498 if (PyObject_AsReadBuffer(cmd, (const void **)&str, &length))
499 return NULL;
Tim Peters2c646c92003-02-10 14:48:29 +0000500 if ((size_t)length != strlen(str)) {
Just van Rossum3aaf42c2003-02-10 08:21:10 +0000501 PyErr_SetString(PyExc_TypeError,
Alex Martellia9b9c9f2003-04-23 13:34:35 +0000502 "compile() expected string without null bytes");
Neal Norwitz3a9a3e72005-11-27 20:38:31 +0000503 goto cleanup;
Just van Rossum3aaf42c2003-02-10 08:21:10 +0000504 }
505
Guido van Rossum5b722181993-03-30 17:46:03 +0000506 if (strcmp(startstr, "exec") == 0)
Guido van Rossumb05a5c71997-05-07 17:46:13 +0000507 start = Py_file_input;
Guido van Rossum5b722181993-03-30 17:46:03 +0000508 else if (strcmp(startstr, "eval") == 0)
Guido van Rossumb05a5c71997-05-07 17:46:13 +0000509 start = Py_eval_input;
Guido van Rossum872537c1995-07-07 22:43:42 +0000510 else if (strcmp(startstr, "single") == 0)
Guido van Rossumb05a5c71997-05-07 17:46:13 +0000511 start = Py_single_input;
Guido van Rossum5b722181993-03-30 17:46:03 +0000512 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000513 PyErr_SetString(PyExc_ValueError,
Fred Drake661ea262000-10-24 19:57:45 +0000514 "compile() arg 3 must be 'exec' or 'eval' or 'single'");
Neal Norwitz3a9a3e72005-11-27 20:38:31 +0000515 goto cleanup;
Guido van Rossum5b722181993-03-30 17:46:03 +0000516 }
Tim Peters6cd6a822001-08-17 22:11:27 +0000517
Guido van Rossum4b499dd32003-02-13 22:07:59 +0000518 if (supplied_flags &
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000519 ~(PyCF_MASK | PyCF_MASK_OBSOLETE | PyCF_DONT_IMPLY_DEDENT | PyCF_ONLY_AST))
Guido van Rossum4b499dd32003-02-13 22:07:59 +0000520 {
Tim Peters6cd6a822001-08-17 22:11:27 +0000521 PyErr_SetString(PyExc_ValueError,
522 "compile(): unrecognised flags");
Neal Norwitz3a9a3e72005-11-27 20:38:31 +0000523 goto cleanup;
Tim Peters6cd6a822001-08-17 22:11:27 +0000524 }
525 /* XXX Warn if (supplied_flags & PyCF_MASK_OBSOLETE) != 0? */
526
Tim Peters6cd6a822001-08-17 22:11:27 +0000527 if (!dont_inherit) {
528 PyEval_MergeCompilerFlags(&cf);
529 }
Just van Rossum3aaf42c2003-02-10 08:21:10 +0000530 result = Py_CompileStringFlags(str, filename, start, &cf);
Neal Norwitz3a9a3e72005-11-27 20:38:31 +0000531cleanup:
Just van Rossum3aaf42c2003-02-10 08:21:10 +0000532 Py_XDECREF(tmp);
533 return result;
Guido van Rossum5b722181993-03-30 17:46:03 +0000534}
535
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000536PyDoc_STRVAR(compile_doc,
Tim Peters6cd6a822001-08-17 22:11:27 +0000537"compile(source, filename, mode[, flags[, dont_inherit]]) -> code object\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000538\n\
539Compile the source string (a Python module, statement or expression)\n\
540into a code object that can be executed by the exec statement or eval().\n\
541The filename will be used for run-time error messages.\n\
542The mode must be 'exec' to compile a module, 'single' to compile a\n\
Tim Peters6cd6a822001-08-17 22:11:27 +0000543single (interactive) statement, or 'eval' to compile an expression.\n\
544The flags argument, if present, controls which future statements influence\n\
545the compilation of the code.\n\
546The dont_inherit argument, if non-zero, stops the compilation inheriting\n\
547the effects of any future statements in effect in the code calling\n\
548compile; if absent or zero these statements do influence the compilation,\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000549in addition to any features explicitly specified.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000550
Guido van Rossum79f25d91997-04-29 20:08:16 +0000551static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000552builtin_dir(PyObject *self, PyObject *args)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000553{
Tim Peters5d2b77c2001-09-03 05:47:38 +0000554 PyObject *arg = NULL;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000555
Raymond Hettingerea3fdf42002-12-29 16:33:45 +0000556 if (!PyArg_UnpackTuple(args, "dir", 0, 1, &arg))
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000557 return NULL;
Tim Peters7eea37e2001-09-04 22:08:56 +0000558 return PyObject_Dir(arg);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000559}
560
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000561PyDoc_STRVAR(dir_doc,
Tim Peters5d2b77c2001-09-03 05:47:38 +0000562"dir([object]) -> list of strings\n"
563"\n"
Georg Brandl871f1bc2007-03-12 13:17:36 +0000564"If called without an argument, return the names in the current scope.\n"
565"Else, return an alphabetized list of names comprising (some of) the attributes\n"
566"of the given object, and of attributes reachable from it.\n"
567"If the object supplies a method named __dir__, it will be used; otherwise\n"
568"the default dir() logic is used and returns:\n"
569" for a module object: the module's attributes.\n"
570" for a class object: its attributes, and recursively the attributes\n"
571" of its bases.\n"
Georg Brandl3bb15672007-03-13 07:23:16 +0000572" for any other object: its attributes, its class's attributes, and\n"
Georg Brandl871f1bc2007-03-12 13:17:36 +0000573" recursively the attributes of its class's base classes.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000574
Guido van Rossum79f25d91997-04-29 20:08:16 +0000575static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000576builtin_divmod(PyObject *self, PyObject *args)
Guido van Rossum6a00cd81995-01-07 12:39:01 +0000577{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000578 PyObject *v, *w;
Guido van Rossum6a00cd81995-01-07 12:39:01 +0000579
Raymond Hettingerea3fdf42002-12-29 16:33:45 +0000580 if (!PyArg_UnpackTuple(args, "divmod", 2, 2, &v, &w))
Guido van Rossum6a00cd81995-01-07 12:39:01 +0000581 return NULL;
Guido van Rossum09df08a1998-05-22 00:51:39 +0000582 return PyNumber_Divmod(v, w);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000583}
584
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000585PyDoc_STRVAR(divmod_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000586"divmod(x, y) -> (div, mod)\n\
587\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000588Return the tuple ((x-x%y)/y, x%y). Invariant: div*y + mod == x.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000589
590
Guido van Rossum79f25d91997-04-29 20:08:16 +0000591static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000592builtin_eval(PyObject *self, PyObject *args)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000593{
Just van Rossum3aaf42c2003-02-10 08:21:10 +0000594 PyObject *cmd, *result, *tmp = NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000595 PyObject *globals = Py_None, *locals = Py_None;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000596 char *str;
Tim Peters9fa96be2001-08-17 23:04:59 +0000597 PyCompilerFlags cf;
Guido van Rossum590baa41993-11-30 13:40:46 +0000598
Raymond Hettinger214b1c32004-07-02 06:41:07 +0000599 if (!PyArg_UnpackTuple(args, "eval", 1, 3, &cmd, &globals, &locals))
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000600 return NULL;
Raymond Hettinger214b1c32004-07-02 06:41:07 +0000601 if (locals != Py_None && !PyMapping_Check(locals)) {
602 PyErr_SetString(PyExc_TypeError, "locals must be a mapping");
Raymond Hettinger513ffe82004-07-06 13:44:41 +0000603 return NULL;
Raymond Hettinger214b1c32004-07-02 06:41:07 +0000604 }
605 if (globals != Py_None && !PyDict_Check(globals)) {
Georg Brandl99d7e4e2005-08-31 22:21:15 +0000606 PyErr_SetString(PyExc_TypeError, PyMapping_Check(globals) ?
Raymond Hettinger214b1c32004-07-02 06:41:07 +0000607 "globals must be a real dict; try eval(expr, {}, mapping)"
608 : "globals must be a dict");
Raymond Hettinger513ffe82004-07-06 13:44:41 +0000609 return NULL;
Raymond Hettinger214b1c32004-07-02 06:41:07 +0000610 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000611 if (globals == Py_None) {
612 globals = PyEval_GetGlobals();
613 if (locals == Py_None)
614 locals = PyEval_GetLocals();
Guido van Rossum6135a871995-01-09 17:53:26 +0000615 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000616 else if (locals == Py_None)
Guido van Rossum6135a871995-01-09 17:53:26 +0000617 locals = globals;
Tim Peters9fa96be2001-08-17 23:04:59 +0000618
Georg Brandl77c85e62005-09-15 10:46:13 +0000619 if (globals == NULL || locals == NULL) {
620 PyErr_SetString(PyExc_TypeError,
621 "eval must be given globals and locals "
622 "when called without a frame");
623 return NULL;
624 }
625
Guido van Rossum79f25d91997-04-29 20:08:16 +0000626 if (PyDict_GetItemString(globals, "__builtins__") == NULL) {
627 if (PyDict_SetItemString(globals, "__builtins__",
628 PyEval_GetBuiltins()) != 0)
Guido van Rossum6135a871995-01-09 17:53:26 +0000629 return NULL;
630 }
Tim Peters9fa96be2001-08-17 23:04:59 +0000631
Jeremy Hylton15c1c4f2001-07-30 21:50:55 +0000632 if (PyCode_Check(cmd)) {
Jeremy Hylton733c8932001-12-13 19:51:56 +0000633 if (PyCode_GetNumFree((PyCodeObject *)cmd) > 0) {
Jeremy Hylton15c1c4f2001-07-30 21:50:55 +0000634 PyErr_SetString(PyExc_TypeError,
635 "code object passed to eval() may not contain free variables");
636 return NULL;
637 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000638 return PyEval_EvalCode((PyCodeObject *) cmd, globals, locals);
Jeremy Hylton15c1c4f2001-07-30 21:50:55 +0000639 }
Tim Peters9fa96be2001-08-17 23:04:59 +0000640
Marc-André Lemburgd1ba4432000-09-19 21:04:18 +0000641 if (!PyString_Check(cmd) &&
642 !PyUnicode_Check(cmd)) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000643 PyErr_SetString(PyExc_TypeError,
Fred Drake661ea262000-10-24 19:57:45 +0000644 "eval() arg 1 must be a string or code object");
Guido van Rossum94390a41992-08-14 15:14:30 +0000645 return NULL;
646 }
Just van Rossum3aaf42c2003-02-10 08:21:10 +0000647 cf.cf_flags = 0;
648
649#ifdef Py_USING_UNICODE
650 if (PyUnicode_Check(cmd)) {
651 tmp = PyUnicode_AsUTF8String(cmd);
652 if (tmp == NULL)
653 return NULL;
654 cmd = tmp;
655 cf.cf_flags |= PyCF_SOURCE_IS_UTF8;
656 }
657#endif
Neal Norwitz3a9a3e72005-11-27 20:38:31 +0000658 if (PyString_AsStringAndSize(cmd, &str, NULL)) {
659 Py_XDECREF(tmp);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000660 return NULL;
Neal Norwitz3a9a3e72005-11-27 20:38:31 +0000661 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000662 while (*str == ' ' || *str == '\t')
663 str++;
Tim Peters9fa96be2001-08-17 23:04:59 +0000664
Tim Peters9fa96be2001-08-17 23:04:59 +0000665 (void)PyEval_MergeCompilerFlags(&cf);
Just van Rossum3aaf42c2003-02-10 08:21:10 +0000666 result = PyRun_StringFlags(str, Py_eval_input, globals, locals, &cf);
667 Py_XDECREF(tmp);
668 return result;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000669}
670
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000671PyDoc_STRVAR(eval_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000672"eval(source[, globals[, locals]]) -> value\n\
673\n\
674Evaluate the source in the context of globals and locals.\n\
675The source may be a string representing a Python expression\n\
676or a code object as returned by compile().\n\
Neal Norwitz477ca1c2006-09-05 02:25:41 +0000677The globals must be a dictionary and locals can be any mapping,\n\
Raymond Hettinger214b1c32004-07-02 06:41:07 +0000678defaulting to the current globals and locals.\n\
679If only globals is given, locals defaults to it.\n");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000680
681
Guido van Rossum79f25d91997-04-29 20:08:16 +0000682static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000683builtin_execfile(PyObject *self, PyObject *args)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000684{
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000685 char *filename;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000686 PyObject *globals = Py_None, *locals = Py_None;
687 PyObject *res;
Guido van Rossumb3a639e2001-09-05 13:37:47 +0000688 FILE* fp = NULL;
Tim Peters5ba58662001-07-16 02:29:45 +0000689 PyCompilerFlags cf;
Martin v. Löwis6b3a2c42001-08-08 05:30:36 +0000690 int exists;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000691
Neal Norwitzdf25efe2007-05-23 06:58:36 +0000692 if (Py_Py3kWarningFlag &&
693 PyErr_Warn(PyExc_DeprecationWarning,
Neal Norwitzb93e7d12008-02-24 02:40:58 +0000694 "execfile() not supported in 3.x. Use exec().") < 0)
Neal Norwitzdf25efe2007-05-23 06:58:36 +0000695 return NULL;
696
Raymond Hettinger66bd2332004-08-02 08:30:07 +0000697 if (!PyArg_ParseTuple(args, "s|O!O:execfile",
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000698 &filename,
Guido van Rossum79f25d91997-04-29 20:08:16 +0000699 &PyDict_Type, &globals,
Raymond Hettinger66bd2332004-08-02 08:30:07 +0000700 &locals))
Guido van Rossum0f61f8a1992-02-25 18:55:05 +0000701 return NULL;
Raymond Hettinger66bd2332004-08-02 08:30:07 +0000702 if (locals != Py_None && !PyMapping_Check(locals)) {
703 PyErr_SetString(PyExc_TypeError, "locals must be a mapping");
704 return NULL;
705 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000706 if (globals == Py_None) {
707 globals = PyEval_GetGlobals();
708 if (locals == Py_None)
709 locals = PyEval_GetLocals();
Guido van Rossum6135a871995-01-09 17:53:26 +0000710 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000711 else if (locals == Py_None)
Guido van Rossum6135a871995-01-09 17:53:26 +0000712 locals = globals;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000713 if (PyDict_GetItemString(globals, "__builtins__") == NULL) {
714 if (PyDict_SetItemString(globals, "__builtins__",
715 PyEval_GetBuiltins()) != 0)
Guido van Rossum6135a871995-01-09 17:53:26 +0000716 return NULL;
717 }
Martin v. Löwis6b3a2c42001-08-08 05:30:36 +0000718
719 exists = 0;
720 /* Test for existence or directory. */
Martin v. Löwis3484a182002-03-09 12:07:51 +0000721#if defined(PLAN9)
722 {
723 Dir *d;
724
725 if ((d = dirstat(filename))!=nil) {
726 if(d->mode & DMDIR)
727 werrstr("is a directory");
728 else
729 exists = 1;
730 free(d);
731 }
Martin v. Löwis6b3a2c42001-08-08 05:30:36 +0000732 }
Martin v. Löwis3484a182002-03-09 12:07:51 +0000733#elif defined(RISCOS)
Guido van Rossume2ae77b2001-10-24 20:42:55 +0000734 if (object_exists(filename)) {
735 if (isdir(filename))
736 errno = EISDIR;
737 else
738 exists = 1;
739 }
Martin v. Löwis3484a182002-03-09 12:07:51 +0000740#else /* standard Posix */
741 {
742 struct stat s;
743 if (stat(filename, &s) == 0) {
744 if (S_ISDIR(s.st_mode))
Andrew MacIntyreda4d6cb2004-03-29 11:53:38 +0000745# if defined(PYOS_OS2) && defined(PYCC_VACPP)
Martin v. Löwis3484a182002-03-09 12:07:51 +0000746 errno = EOS2ERR;
747# else
748 errno = EISDIR;
749# endif
750 else
751 exists = 1;
752 }
753 }
754#endif
Martin v. Löwis6b3a2c42001-08-08 05:30:36 +0000755
756 if (exists) {
757 Py_BEGIN_ALLOW_THREADS
Jack Jansen7b8c7542002-04-14 20:12:41 +0000758 fp = fopen(filename, "r" PY_STDIOTEXTMODE);
Martin v. Löwis6b3a2c42001-08-08 05:30:36 +0000759 Py_END_ALLOW_THREADS
760
761 if (fp == NULL) {
762 exists = 0;
763 }
764 }
765
766 if (!exists) {
Peter Schneider-Kamp4c013422002-08-27 16:58:00 +0000767 PyErr_SetFromErrnoWithFilename(PyExc_IOError, filename);
Guido van Rossum0f61f8a1992-02-25 18:55:05 +0000768 return NULL;
769 }
Tim Peters5ba58662001-07-16 02:29:45 +0000770 cf.cf_flags = 0;
771 if (PyEval_MergeCompilerFlags(&cf))
Tim Peters748b8bb2001-04-28 08:20:22 +0000772 res = PyRun_FileExFlags(fp, filename, Py_file_input, globals,
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000773 locals, 1, &cf);
Tim Peters5ba58662001-07-16 02:29:45 +0000774 else
Tim Peters748b8bb2001-04-28 08:20:22 +0000775 res = PyRun_FileEx(fp, filename, Py_file_input, globals,
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000776 locals, 1);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000777 return res;
Guido van Rossum0f61f8a1992-02-25 18:55:05 +0000778}
779
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000780PyDoc_STRVAR(execfile_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000781"execfile(filename[, globals[, locals]])\n\
782\n\
783Read and execute a Python script from a file.\n\
784The globals and locals are dictionaries, defaulting to the current\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000785globals and locals. If only globals is given, locals defaults to it.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000786
787
Guido van Rossum79f25d91997-04-29 20:08:16 +0000788static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000789builtin_getattr(PyObject *self, PyObject *args)
Guido van Rossum33894be1992-01-27 16:53:09 +0000790{
Guido van Rossum950ff291998-06-29 13:38:57 +0000791 PyObject *v, *result, *dflt = NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000792 PyObject *name;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000793
Raymond Hettingerea3fdf42002-12-29 16:33:45 +0000794 if (!PyArg_UnpackTuple(args, "getattr", 2, 3, &v, &name, &dflt))
Guido van Rossum33894be1992-01-27 16:53:09 +0000795 return NULL;
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000796#ifdef Py_USING_UNICODE
Jeremy Hylton0eb11152001-07-30 22:39:31 +0000797 if (PyUnicode_Check(name)) {
798 name = _PyUnicode_AsDefaultEncodedString(name, NULL);
799 if (name == NULL)
800 return NULL;
801 }
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000802#endif
Jeremy Hylton0eb11152001-07-30 22:39:31 +0000803
804 if (!PyString_Check(name)) {
805 PyErr_SetString(PyExc_TypeError,
Alex Martellia9b9c9f2003-04-23 13:34:35 +0000806 "getattr(): attribute name must be string");
Jeremy Hylton0eb11152001-07-30 22:39:31 +0000807 return NULL;
808 }
Guido van Rossum950ff291998-06-29 13:38:57 +0000809 result = PyObject_GetAttr(v, name);
Guido van Rossumd8923572001-10-16 21:31:32 +0000810 if (result == NULL && dflt != NULL &&
811 PyErr_ExceptionMatches(PyExc_AttributeError))
812 {
Guido van Rossum950ff291998-06-29 13:38:57 +0000813 PyErr_Clear();
814 Py_INCREF(dflt);
815 result = dflt;
816 }
817 return result;
Guido van Rossum9bfef441993-03-29 10:43:31 +0000818}
819
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000820PyDoc_STRVAR(getattr_doc,
Guido van Rossum950ff291998-06-29 13:38:57 +0000821"getattr(object, name[, default]) -> value\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000822\n\
Guido van Rossum950ff291998-06-29 13:38:57 +0000823Get a named attribute from an object; getattr(x, 'y') is equivalent to x.y.\n\
824When a default argument is given, it is returned when the attribute doesn't\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000825exist; without it, an exception is raised in that case.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000826
827
Guido van Rossum79f25d91997-04-29 20:08:16 +0000828static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000829builtin_globals(PyObject *self)
Guido van Rossum872537c1995-07-07 22:43:42 +0000830{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000831 PyObject *d;
Guido van Rossum872537c1995-07-07 22:43:42 +0000832
Guido van Rossum79f25d91997-04-29 20:08:16 +0000833 d = PyEval_GetGlobals();
Neal Norwitz43bd4db2006-08-12 01:46:42 +0000834 Py_XINCREF(d);
Guido van Rossum872537c1995-07-07 22:43:42 +0000835 return d;
836}
837
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000838PyDoc_STRVAR(globals_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000839"globals() -> dictionary\n\
840\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000841Return the dictionary containing the current scope's global variables.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000842
843
Guido van Rossum79f25d91997-04-29 20:08:16 +0000844static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000845builtin_hasattr(PyObject *self, PyObject *args)
Guido van Rossum9bfef441993-03-29 10:43:31 +0000846{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000847 PyObject *v;
848 PyObject *name;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000849
Raymond Hettingerea3fdf42002-12-29 16:33:45 +0000850 if (!PyArg_UnpackTuple(args, "hasattr", 2, 2, &v, &name))
Guido van Rossum9bfef441993-03-29 10:43:31 +0000851 return NULL;
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000852#ifdef Py_USING_UNICODE
Jeremy Hylton302b54a2001-07-30 22:45:19 +0000853 if (PyUnicode_Check(name)) {
854 name = _PyUnicode_AsDefaultEncodedString(name, NULL);
855 if (name == NULL)
856 return NULL;
857 }
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000858#endif
Jeremy Hylton302b54a2001-07-30 22:45:19 +0000859
860 if (!PyString_Check(name)) {
861 PyErr_SetString(PyExc_TypeError,
Alex Martellia9b9c9f2003-04-23 13:34:35 +0000862 "hasattr(): attribute name must be string");
Jeremy Hylton302b54a2001-07-30 22:45:19 +0000863 return NULL;
864 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000865 v = PyObject_GetAttr(v, name);
Guido van Rossum9bfef441993-03-29 10:43:31 +0000866 if (v == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000867 PyErr_Clear();
Guido van Rossum09df08a1998-05-22 00:51:39 +0000868 Py_INCREF(Py_False);
869 return Py_False;
Guido van Rossum9bfef441993-03-29 10:43:31 +0000870 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000871 Py_DECREF(v);
Guido van Rossum09df08a1998-05-22 00:51:39 +0000872 Py_INCREF(Py_True);
873 return Py_True;
Guido van Rossum33894be1992-01-27 16:53:09 +0000874}
875
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000876PyDoc_STRVAR(hasattr_doc,
Guido van Rossum77f6a652002-04-03 22:41:51 +0000877"hasattr(object, name) -> bool\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000878\n\
879Return whether the object has an attribute with the given name.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000880(This is done by calling getattr(object, name) and catching exceptions.)");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000881
882
Guido van Rossum79f25d91997-04-29 20:08:16 +0000883static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000884builtin_id(PyObject *self, PyObject *v)
Guido van Rossum5b722181993-03-30 17:46:03 +0000885{
Guido van Rossum106f2da2000-06-28 21:12:25 +0000886 return PyLong_FromVoidPtr(v);
Guido van Rossum5b722181993-03-30 17:46:03 +0000887}
888
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000889PyDoc_STRVAR(id_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000890"id(object) -> integer\n\
891\n\
892Return the identity of an object. This is guaranteed to be unique among\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000893simultaneously existing objects. (Hint: it's the object's memory address.)");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000894
895
Guido van Rossum79f25d91997-04-29 20:08:16 +0000896static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000897builtin_map(PyObject *self, PyObject *args)
Guido van Rossum12d12c51993-10-26 17:58:25 +0000898{
899 typedef struct {
Tim Peters4e9afdc2001-05-03 23:54:49 +0000900 PyObject *it; /* the iterator object */
901 int saw_StopIteration; /* bool: did the iterator end? */
Guido van Rossum12d12c51993-10-26 17:58:25 +0000902 } sequence;
903
Guido van Rossum79f25d91997-04-29 20:08:16 +0000904 PyObject *func, *result;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000905 sequence *seqs = NULL, *sqp;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000906 Py_ssize_t n, len;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000907 register int i, j;
908
Guido van Rossum79f25d91997-04-29 20:08:16 +0000909 n = PyTuple_Size(args);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000910 if (n < 2) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000911 PyErr_SetString(PyExc_TypeError,
912 "map() requires at least two args");
Guido van Rossum12d12c51993-10-26 17:58:25 +0000913 return NULL;
914 }
915
Guido van Rossum79f25d91997-04-29 20:08:16 +0000916 func = PyTuple_GetItem(args, 0);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000917 n--;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000918
Neal Norwitz53152a12008-02-24 02:20:25 +0000919 if (func == Py_None) {
920 if (Py_Py3kWarningFlag &&
921 PyErr_Warn(PyExc_DeprecationWarning,
Neal Norwitzb93e7d12008-02-24 02:40:58 +0000922 "map(None, ...) not supported in 3.x. Use list(...).") < 0)
Neal Norwitz53152a12008-02-24 02:20:25 +0000923 return NULL;
924 if (n == 1) {
925 /* map(None, S) is the same as list(S). */
926 return PySequence_List(PyTuple_GetItem(args, 1));
927 }
Guido van Rossumfa4ac711998-07-10 17:37:30 +0000928 }
929
Tim Peters4e9afdc2001-05-03 23:54:49 +0000930 /* Get space for sequence descriptors. Must NULL out the iterator
931 * pointers so that jumping to Fail_2 later doesn't see trash.
932 */
Guido van Rossum79f25d91997-04-29 20:08:16 +0000933 if ((seqs = PyMem_NEW(sequence, n)) == NULL) {
934 PyErr_NoMemory();
Tim Peters4e9afdc2001-05-03 23:54:49 +0000935 return NULL;
936 }
937 for (i = 0; i < n; ++i) {
938 seqs[i].it = (PyObject*)NULL;
939 seqs[i].saw_StopIteration = 0;
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000940 }
Guido van Rossum12d12c51993-10-26 17:58:25 +0000941
Tim Peters4e9afdc2001-05-03 23:54:49 +0000942 /* Do a first pass to obtain iterators for the arguments, and set len
943 * to the largest of their lengths.
Tim Peters748b8bb2001-04-28 08:20:22 +0000944 */
Tim Peters4e9afdc2001-05-03 23:54:49 +0000945 len = 0;
946 for (i = 0, sqp = seqs; i < n; ++i, ++sqp) {
947 PyObject *curseq;
Martin v. Löwisd96ee902006-02-16 14:37:16 +0000948 Py_ssize_t curlen;
Guido van Rossumad991772001-01-12 16:03:05 +0000949
Tim Peters4e9afdc2001-05-03 23:54:49 +0000950 /* Get iterator. */
951 curseq = PyTuple_GetItem(args, i+1);
952 sqp->it = PyObject_GetIter(curseq);
953 if (sqp->it == NULL) {
Guido van Rossum12d12c51993-10-26 17:58:25 +0000954 static char errmsg[] =
Tim Peters4e9afdc2001-05-03 23:54:49 +0000955 "argument %d to map() must support iteration";
Guido van Rossum15e33a41997-04-30 19:00:27 +0000956 char errbuf[sizeof(errmsg) + 25];
Jeremy Hylton518ab1c2001-11-28 20:42:20 +0000957 PyOS_snprintf(errbuf, sizeof(errbuf), errmsg, i+2);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000958 PyErr_SetString(PyExc_TypeError, errbuf);
Guido van Rossum12d12c51993-10-26 17:58:25 +0000959 goto Fail_2;
960 }
961
Tim Peters4e9afdc2001-05-03 23:54:49 +0000962 /* Update len. */
Raymond Hettinger4e2f7142007-12-06 00:56:53 +0000963 curlen = _PyObject_LengthHint(curseq, 8);
Raymond Hettinger77f3c872004-01-04 08:54:44 +0000964 if (curlen > len)
965 len = curlen;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000966 }
967
Tim Peters4e9afdc2001-05-03 23:54:49 +0000968 /* Get space for the result list. */
Guido van Rossum79f25d91997-04-29 20:08:16 +0000969 if ((result = (PyObject *) PyList_New(len)) == NULL)
Guido van Rossum12d12c51993-10-26 17:58:25 +0000970 goto Fail_2;
971
Tim Peters4e9afdc2001-05-03 23:54:49 +0000972 /* Iterate over the sequences until all have stopped. */
Guido van Rossum2d951851994-08-29 12:52:16 +0000973 for (i = 0; ; ++i) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000974 PyObject *alist, *item=NULL, *value;
Tim Peters4e9afdc2001-05-03 23:54:49 +0000975 int numactive = 0;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000976
Guido van Rossum79f25d91997-04-29 20:08:16 +0000977 if (func == Py_None && n == 1)
Guido van Rossum32120311995-07-10 13:52:21 +0000978 alist = NULL;
Tim Peters4e9afdc2001-05-03 23:54:49 +0000979 else if ((alist = PyTuple_New(n)) == NULL)
980 goto Fail_1;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000981
982 for (j = 0, sqp = seqs; j < n; ++j, ++sqp) {
Tim Peters4e9afdc2001-05-03 23:54:49 +0000983 if (sqp->saw_StopIteration) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000984 Py_INCREF(Py_None);
985 item = Py_None;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000986 }
Guido van Rossum12d12c51993-10-26 17:58:25 +0000987 else {
Tim Peters4e9afdc2001-05-03 23:54:49 +0000988 item = PyIter_Next(sqp->it);
989 if (item)
990 ++numactive;
991 else {
Tim Peters4e9afdc2001-05-03 23:54:49 +0000992 if (PyErr_Occurred()) {
Tim Petersf4848da2001-05-05 00:14:56 +0000993 Py_XDECREF(alist);
994 goto Fail_1;
Guido van Rossum2d951851994-08-29 12:52:16 +0000995 }
Tim Peters4e9afdc2001-05-03 23:54:49 +0000996 Py_INCREF(Py_None);
997 item = Py_None;
998 sqp->saw_StopIteration = 1;
Guido van Rossum2d951851994-08-29 12:52:16 +0000999 }
Guido van Rossum12d12c51993-10-26 17:58:25 +00001000 }
Tim Peters4e9afdc2001-05-03 23:54:49 +00001001 if (alist)
1002 PyTuple_SET_ITEM(alist, j, item);
1003 else
Guido van Rossum2d951851994-08-29 12:52:16 +00001004 break;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001005 }
1006
Guido van Rossum32120311995-07-10 13:52:21 +00001007 if (!alist)
1008 alist = item;
Guido van Rossum2d951851994-08-29 12:52:16 +00001009
Tim Peters4e9afdc2001-05-03 23:54:49 +00001010 if (numactive == 0) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001011 Py_DECREF(alist);
Guido van Rossum2d951851994-08-29 12:52:16 +00001012 break;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001013 }
Guido van Rossum2d951851994-08-29 12:52:16 +00001014
Guido van Rossum79f25d91997-04-29 20:08:16 +00001015 if (func == Py_None)
Guido van Rossum32120311995-07-10 13:52:21 +00001016 value = alist;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001017 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001018 value = PyEval_CallObject(func, alist);
1019 Py_DECREF(alist);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00001020 if (value == NULL)
1021 goto Fail_1;
Guido van Rossum2d951851994-08-29 12:52:16 +00001022 }
1023 if (i >= len) {
Barry Warsawfa77e091999-01-28 18:49:12 +00001024 int status = PyList_Append(result, value);
Barry Warsaw21332871999-01-28 04:21:35 +00001025 Py_DECREF(value);
Barry Warsawfa77e091999-01-28 18:49:12 +00001026 if (status < 0)
1027 goto Fail_1;
Guido van Rossum2d951851994-08-29 12:52:16 +00001028 }
Tim Peters4e9afdc2001-05-03 23:54:49 +00001029 else if (PyList_SetItem(result, i, value) < 0)
1030 goto Fail_1;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001031 }
1032
Guido van Rossumfa4ac711998-07-10 17:37:30 +00001033 if (i < len && PyList_SetSlice(result, i, len, NULL) < 0)
1034 goto Fail_1;
1035
Tim Peters4e9afdc2001-05-03 23:54:49 +00001036 goto Succeed;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001037
Guido van Rossum12d12c51993-10-26 17:58:25 +00001038Fail_1:
Guido van Rossum79f25d91997-04-29 20:08:16 +00001039 Py_DECREF(result);
Guido van Rossum12d12c51993-10-26 17:58:25 +00001040Fail_2:
Tim Peters4e9afdc2001-05-03 23:54:49 +00001041 result = NULL;
1042Succeed:
1043 assert(seqs);
1044 for (i = 0; i < n; ++i)
1045 Py_XDECREF(seqs[i].it);
1046 PyMem_DEL(seqs);
1047 return result;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001048}
1049
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001050PyDoc_STRVAR(map_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001051"map(function, sequence[, sequence, ...]) -> list\n\
1052\n\
1053Return a list of the results of applying the function to the items of\n\
1054the argument sequence(s). If more than one sequence is given, the\n\
1055function is called with an argument list consisting of the corresponding\n\
1056item of each sequence, substituting None for missing values when not all\n\
1057sequences have the same length. If the function is None, return a list of\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001058the items of the sequence (or a list of tuples if more than one sequence).");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001059
1060
Guido van Rossum79f25d91997-04-29 20:08:16 +00001061static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001062builtin_setattr(PyObject *self, PyObject *args)
Guido van Rossum33894be1992-01-27 16:53:09 +00001063{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001064 PyObject *v;
1065 PyObject *name;
1066 PyObject *value;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001067
Raymond Hettingerea3fdf42002-12-29 16:33:45 +00001068 if (!PyArg_UnpackTuple(args, "setattr", 3, 3, &v, &name, &value))
Guido van Rossum33894be1992-01-27 16:53:09 +00001069 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001070 if (PyObject_SetAttr(v, name, value) != 0)
Guido van Rossum33894be1992-01-27 16:53:09 +00001071 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001072 Py_INCREF(Py_None);
1073 return Py_None;
Guido van Rossum33894be1992-01-27 16:53:09 +00001074}
1075
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001076PyDoc_STRVAR(setattr_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001077"setattr(object, name, value)\n\
1078\n\
1079Set a named attribute on an object; setattr(x, 'y', v) is equivalent to\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001080``x.y = v''.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001081
1082
Guido van Rossum79f25d91997-04-29 20:08:16 +00001083static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001084builtin_delattr(PyObject *self, PyObject *args)
Guido van Rossum14144fc1994-08-29 12:53:40 +00001085{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001086 PyObject *v;
1087 PyObject *name;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001088
Raymond Hettingerea3fdf42002-12-29 16:33:45 +00001089 if (!PyArg_UnpackTuple(args, "delattr", 2, 2, &v, &name))
Guido van Rossum14144fc1994-08-29 12:53:40 +00001090 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001091 if (PyObject_SetAttr(v, name, (PyObject *)NULL) != 0)
Guido van Rossum14144fc1994-08-29 12:53:40 +00001092 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001093 Py_INCREF(Py_None);
1094 return Py_None;
Guido van Rossum14144fc1994-08-29 12:53:40 +00001095}
1096
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001097PyDoc_STRVAR(delattr_doc,
Guido van Rossumdf12a591998-11-23 22:13:04 +00001098"delattr(object, name)\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001099\n\
1100Delete a named attribute on an object; delattr(x, 'y') is equivalent to\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001101``del x.y''.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001102
1103
Guido van Rossum79f25d91997-04-29 20:08:16 +00001104static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001105builtin_hash(PyObject *self, PyObject *v)
Guido van Rossum9bfef441993-03-29 10:43:31 +00001106{
Guido van Rossum9bfef441993-03-29 10:43:31 +00001107 long x;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001108
Guido van Rossum79f25d91997-04-29 20:08:16 +00001109 x = PyObject_Hash(v);
Guido van Rossum9bfef441993-03-29 10:43:31 +00001110 if (x == -1)
1111 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001112 return PyInt_FromLong(x);
Guido van Rossum9bfef441993-03-29 10:43:31 +00001113}
1114
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001115PyDoc_STRVAR(hash_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001116"hash(object) -> integer\n\
1117\n\
1118Return a hash value for the object. Two objects with the same value have\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001119the same hash value. The reverse is not necessarily true, but likely.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001120
1121
Guido van Rossum79f25d91997-04-29 20:08:16 +00001122static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001123builtin_hex(PyObject *self, PyObject *v)
Guido van Rossum006bcd41991-10-24 14:54:44 +00001124{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001125 PyNumberMethods *nb;
Neil Schemenauer3a313e32004-07-19 16:29:17 +00001126 PyObject *res;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001127
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001128 if ((nb = v->ob_type->tp_as_number) == NULL ||
Guido van Rossum1899c2e1992-09-12 11:09:23 +00001129 nb->nb_hex == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001130 PyErr_SetString(PyExc_TypeError,
Guido van Rossum1899c2e1992-09-12 11:09:23 +00001131 "hex() argument can't be converted to hex");
1132 return NULL;
Guido van Rossum006bcd41991-10-24 14:54:44 +00001133 }
Neil Schemenauer3a313e32004-07-19 16:29:17 +00001134 res = (*nb->nb_hex)(v);
1135 if (res && !PyString_Check(res)) {
1136 PyErr_Format(PyExc_TypeError,
1137 "__hex__ returned non-string (type %.200s)",
1138 res->ob_type->tp_name);
1139 Py_DECREF(res);
1140 return NULL;
1141 }
1142 return res;
Guido van Rossum006bcd41991-10-24 14:54:44 +00001143}
1144
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001145PyDoc_STRVAR(hex_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001146"hex(number) -> string\n\
1147\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001148Return the hexadecimal representation of an integer or long integer.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001149
1150
Tim Petersdbd9ba62000-07-09 03:09:57 +00001151static PyObject *builtin_raw_input(PyObject *, PyObject *);
Guido van Rossum3165fe61992-09-25 21:59:05 +00001152
Guido van Rossum79f25d91997-04-29 20:08:16 +00001153static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001154builtin_input(PyObject *self, PyObject *args)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001155{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001156 PyObject *line;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001157 char *str;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001158 PyObject *res;
1159 PyObject *globals, *locals;
Hye-Shik Changff83c2b2004-02-02 13:39:01 +00001160 PyCompilerFlags cf;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001161
1162 line = builtin_raw_input(self, args);
Guido van Rossum3165fe61992-09-25 21:59:05 +00001163 if (line == NULL)
1164 return line;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001165 if (!PyArg_Parse(line, "s;embedded '\\0' in input line", &str))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001166 return NULL;
1167 while (*str == ' ' || *str == '\t')
1168 str++;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001169 globals = PyEval_GetGlobals();
1170 locals = PyEval_GetLocals();
1171 if (PyDict_GetItemString(globals, "__builtins__") == NULL) {
1172 if (PyDict_SetItemString(globals, "__builtins__",
1173 PyEval_GetBuiltins()) != 0)
Guido van Rossum6135a871995-01-09 17:53:26 +00001174 return NULL;
1175 }
Hye-Shik Changff83c2b2004-02-02 13:39:01 +00001176 cf.cf_flags = 0;
1177 PyEval_MergeCompilerFlags(&cf);
1178 res = PyRun_StringFlags(str, Py_eval_input, globals, locals, &cf);
Guido van Rossum79f25d91997-04-29 20:08:16 +00001179 Py_DECREF(line);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001180 return res;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001181}
1182
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001183PyDoc_STRVAR(input_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001184"input([prompt]) -> value\n\
1185\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001186Equivalent to eval(raw_input(prompt)).");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001187
1188
Guido van Rossume8811f81997-02-14 15:48:05 +00001189static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001190builtin_intern(PyObject *self, PyObject *args)
Guido van Rossume8811f81997-02-14 15:48:05 +00001191{
1192 PyObject *s;
Guido van Rossum43713e52000-02-29 13:59:29 +00001193 if (!PyArg_ParseTuple(args, "S:intern", &s))
Guido van Rossume8811f81997-02-14 15:48:05 +00001194 return NULL;
Jeremy Hylton4c989dd2004-08-07 19:20:05 +00001195 if (!PyString_CheckExact(s)) {
1196 PyErr_SetString(PyExc_TypeError,
1197 "can't intern subclass of string");
1198 return NULL;
1199 }
Guido van Rossume8811f81997-02-14 15:48:05 +00001200 Py_INCREF(s);
1201 PyString_InternInPlace(&s);
1202 return s;
1203}
1204
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001205PyDoc_STRVAR(intern_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001206"intern(string) -> string\n\
1207\n\
1208``Intern'' the given string. This enters the string in the (global)\n\
1209table of interned strings whose purpose is to speed up dictionary lookups.\n\
1210Return the string itself or the previously interned string object with the\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001211same value.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001212
1213
Guido van Rossum79f25d91997-04-29 20:08:16 +00001214static PyObject *
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001215builtin_iter(PyObject *self, PyObject *args)
1216{
1217 PyObject *v, *w = NULL;
1218
Raymond Hettingerea3fdf42002-12-29 16:33:45 +00001219 if (!PyArg_UnpackTuple(args, "iter", 1, 2, &v, &w))
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001220 return NULL;
1221 if (w == NULL)
1222 return PyObject_GetIter(v);
1223 if (!PyCallable_Check(v)) {
1224 PyErr_SetString(PyExc_TypeError,
1225 "iter(v, w): v must be callable");
1226 return NULL;
1227 }
1228 return PyCallIter_New(v, w);
1229}
1230
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001231PyDoc_STRVAR(iter_doc,
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001232"iter(collection) -> iterator\n\
1233iter(callable, sentinel) -> iterator\n\
1234\n\
1235Get an iterator from an object. In the first form, the argument must\n\
1236supply its own iterator, or be a sequence.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001237In the second form, the callable is called until it returns the sentinel.");
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001238
1239
1240static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001241builtin_len(PyObject *self, PyObject *v)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001242{
Martin v. Löwis18e16552006-02-15 17:27:45 +00001243 Py_ssize_t res;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001244
Jeremy Hylton03657cf2000-07-12 13:05:33 +00001245 res = PyObject_Size(v);
Guido van Rossum8ea9f4d1998-06-29 22:26:50 +00001246 if (res < 0 && PyErr_Occurred())
1247 return NULL;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001248 return PyInt_FromSsize_t(res);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001249}
1250
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001251PyDoc_STRVAR(len_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001252"len(object) -> integer\n\
1253\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001254Return the number of items of a sequence or mapping.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001255
1256
Guido van Rossum79f25d91997-04-29 20:08:16 +00001257static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001258builtin_locals(PyObject *self)
Guido van Rossum872537c1995-07-07 22:43:42 +00001259{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001260 PyObject *d;
Guido van Rossum872537c1995-07-07 22:43:42 +00001261
Guido van Rossum79f25d91997-04-29 20:08:16 +00001262 d = PyEval_GetLocals();
Neal Norwitz43bd4db2006-08-12 01:46:42 +00001263 Py_XINCREF(d);
Guido van Rossum872537c1995-07-07 22:43:42 +00001264 return d;
1265}
1266
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001267PyDoc_STRVAR(locals_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001268"locals() -> dictionary\n\
1269\n\
Raymond Hettinger69bf8f32003-01-04 02:16:22 +00001270Update and return a dictionary containing the current scope's local variables.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001271
1272
Guido van Rossum79f25d91997-04-29 20:08:16 +00001273static PyObject *
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001274min_max(PyObject *args, PyObject *kwds, int op)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001275{
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001276 PyObject *v, *it, *item, *val, *maxitem, *maxval, *keyfunc=NULL;
Martin v. Löwisfd39ad42004-08-12 14:42:37 +00001277 const char *name = op == Py_LT ? "min" : "max";
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001278
Guido van Rossum79f25d91997-04-29 20:08:16 +00001279 if (PyTuple_Size(args) > 1)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001280 v = args;
Martin v. Löwisfd39ad42004-08-12 14:42:37 +00001281 else if (!PyArg_UnpackTuple(args, (char *)name, 1, 1, &v))
Guido van Rossum3f5da241990-12-20 15:06:42 +00001282 return NULL;
Tim Peters67d687a2002-04-29 21:27:32 +00001283
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001284 if (kwds != NULL && PyDict_Check(kwds) && PyDict_Size(kwds)) {
1285 keyfunc = PyDict_GetItemString(kwds, "key");
1286 if (PyDict_Size(kwds)!=1 || keyfunc == NULL) {
Georg Brandl99d7e4e2005-08-31 22:21:15 +00001287 PyErr_Format(PyExc_TypeError,
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001288 "%s() got an unexpected keyword argument", name);
1289 return NULL;
1290 }
Guido van Rossum1d9a9ea2008-01-23 20:19:01 +00001291 Py_INCREF(keyfunc);
Georg Brandl99d7e4e2005-08-31 22:21:15 +00001292 }
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001293
Tim Petersc3074532001-05-03 07:00:32 +00001294 it = PyObject_GetIter(v);
Guido van Rossum1d9a9ea2008-01-23 20:19:01 +00001295 if (it == NULL) {
1296 Py_XDECREF(keyfunc);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001297 return NULL;
Guido van Rossum1d9a9ea2008-01-23 20:19:01 +00001298 }
Tim Petersc3074532001-05-03 07:00:32 +00001299
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001300 maxitem = NULL; /* the result */
1301 maxval = NULL; /* the value associated with the result */
Brett Cannon9e635cf2004-12-07 00:25:35 +00001302 while (( item = PyIter_Next(it) )) {
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001303 /* get the value from the key function */
1304 if (keyfunc != NULL) {
1305 val = PyObject_CallFunctionObjArgs(keyfunc, item, NULL);
1306 if (val == NULL)
1307 goto Fail_it_item;
1308 }
1309 /* no key function; the value is the item */
1310 else {
1311 val = item;
1312 Py_INCREF(val);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001313 }
Tim Petersc3074532001-05-03 07:00:32 +00001314
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001315 /* maximum value and item are unset; set them */
1316 if (maxval == NULL) {
1317 maxitem = item;
1318 maxval = val;
1319 }
1320 /* maximum value and item are set; update them as necessary */
Guido van Rossum2d951851994-08-29 12:52:16 +00001321 else {
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001322 int cmp = PyObject_RichCompareBool(val, maxval, op);
1323 if (cmp < 0)
1324 goto Fail_it_item_and_val;
1325 else if (cmp > 0) {
1326 Py_DECREF(maxval);
1327 Py_DECREF(maxitem);
1328 maxval = val;
1329 maxitem = item;
Guido van Rossum53451b32001-01-17 15:47:24 +00001330 }
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001331 else {
1332 Py_DECREF(item);
1333 Py_DECREF(val);
Guido van Rossumc8b6df91997-05-23 00:06:51 +00001334 }
Guido van Rossum2d951851994-08-29 12:52:16 +00001335 }
Guido van Rossum3f5da241990-12-20 15:06:42 +00001336 }
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001337 if (PyErr_Occurred())
1338 goto Fail_it;
1339 if (maxval == NULL) {
Martin v. Löwisfd39ad42004-08-12 14:42:37 +00001340 PyErr_Format(PyExc_ValueError,
1341 "%s() arg is an empty sequence", name);
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001342 assert(maxitem == NULL);
1343 }
1344 else
1345 Py_DECREF(maxval);
Tim Petersc3074532001-05-03 07:00:32 +00001346 Py_DECREF(it);
Guido van Rossum1d9a9ea2008-01-23 20:19:01 +00001347 Py_XDECREF(keyfunc);
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001348 return maxitem;
1349
1350Fail_it_item_and_val:
1351 Py_DECREF(val);
1352Fail_it_item:
1353 Py_DECREF(item);
1354Fail_it:
1355 Py_XDECREF(maxval);
1356 Py_XDECREF(maxitem);
1357 Py_DECREF(it);
Guido van Rossum1d9a9ea2008-01-23 20:19:01 +00001358 Py_XDECREF(keyfunc);
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001359 return NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001360}
1361
Guido van Rossum79f25d91997-04-29 20:08:16 +00001362static PyObject *
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001363builtin_min(PyObject *self, PyObject *args, PyObject *kwds)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001364{
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001365 return min_max(args, kwds, Py_LT);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001366}
1367
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001368PyDoc_STRVAR(min_doc,
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001369"min(iterable[, key=func]) -> value\n\
1370min(a, b, c, ...[, key=func]) -> value\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001371\n\
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001372With a single iterable argument, return its smallest item.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001373With two or more arguments, return the smallest argument.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001374
1375
Guido van Rossum79f25d91997-04-29 20:08:16 +00001376static PyObject *
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001377builtin_max(PyObject *self, PyObject *args, PyObject *kwds)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001378{
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001379 return min_max(args, kwds, Py_GT);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001380}
1381
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001382PyDoc_STRVAR(max_doc,
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001383"max(iterable[, key=func]) -> value\n\
1384max(a, b, c, ...[, key=func]) -> value\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001385\n\
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001386With a single iterable argument, return its largest item.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001387With two or more arguments, return the largest argument.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001388
1389
Guido van Rossum79f25d91997-04-29 20:08:16 +00001390static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001391builtin_oct(PyObject *self, PyObject *v)
Guido van Rossum006bcd41991-10-24 14:54:44 +00001392{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001393 PyNumberMethods *nb;
Neil Schemenauer3a313e32004-07-19 16:29:17 +00001394 PyObject *res;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001395
Guido van Rossum1899c2e1992-09-12 11:09:23 +00001396 if (v == NULL || (nb = v->ob_type->tp_as_number) == NULL ||
1397 nb->nb_oct == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001398 PyErr_SetString(PyExc_TypeError,
Guido van Rossum1899c2e1992-09-12 11:09:23 +00001399 "oct() argument can't be converted to oct");
1400 return NULL;
Guido van Rossum006bcd41991-10-24 14:54:44 +00001401 }
Neil Schemenauer3a313e32004-07-19 16:29:17 +00001402 res = (*nb->nb_oct)(v);
1403 if (res && !PyString_Check(res)) {
1404 PyErr_Format(PyExc_TypeError,
1405 "__oct__ returned non-string (type %.200s)",
1406 res->ob_type->tp_name);
1407 Py_DECREF(res);
1408 return NULL;
1409 }
1410 return res;
Guido van Rossum006bcd41991-10-24 14:54:44 +00001411}
1412
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001413PyDoc_STRVAR(oct_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001414"oct(number) -> string\n\
1415\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001416Return the octal representation of an integer or long integer.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001417
1418
Guido van Rossum79f25d91997-04-29 20:08:16 +00001419static PyObject *
Neal Norwitzc4edb0e2006-05-02 04:43:14 +00001420builtin_open(PyObject *self, PyObject *args, PyObject *kwds)
1421{
1422 return PyObject_Call((PyObject*)&PyFile_Type, args, kwds);
1423}
1424
1425PyDoc_STRVAR(open_doc,
1426"open(name[, mode[, buffering]]) -> file object\n\
1427\n\
Skip Montanaro4e3ebe02007-12-08 14:37:43 +00001428Open a file using the file() type, returns a file object. This is the\n\
1429preferred way to open a file.");
Neal Norwitzc4edb0e2006-05-02 04:43:14 +00001430
1431
1432static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001433builtin_ord(PyObject *self, PyObject* obj)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001434{
Guido van Rossum09095f32000-03-10 23:00:52 +00001435 long ord;
Martin v. Löwisd96ee902006-02-16 14:37:16 +00001436 Py_ssize_t size;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001437
Jeremy Hylton394b54d2000-04-12 21:19:47 +00001438 if (PyString_Check(obj)) {
1439 size = PyString_GET_SIZE(obj);
Andrew M. Kuchling34c20cf2000-12-20 14:36:56 +00001440 if (size == 1) {
Jeremy Hylton394b54d2000-04-12 21:19:47 +00001441 ord = (long)((unsigned char)*PyString_AS_STRING(obj));
Andrew M. Kuchling34c20cf2000-12-20 14:36:56 +00001442 return PyInt_FromLong(ord);
1443 }
Martin v. Löwis339d0f72001-08-17 18:39:25 +00001444#ifdef Py_USING_UNICODE
Jeremy Hylton394b54d2000-04-12 21:19:47 +00001445 } else if (PyUnicode_Check(obj)) {
1446 size = PyUnicode_GET_SIZE(obj);
Andrew M. Kuchling34c20cf2000-12-20 14:36:56 +00001447 if (size == 1) {
Jeremy Hylton394b54d2000-04-12 21:19:47 +00001448 ord = (long)*PyUnicode_AS_UNICODE(obj);
Andrew M. Kuchling34c20cf2000-12-20 14:36:56 +00001449 return PyInt_FromLong(ord);
1450 }
Martin v. Löwis339d0f72001-08-17 18:39:25 +00001451#endif
Jeremy Hylton394b54d2000-04-12 21:19:47 +00001452 } else {
1453 PyErr_Format(PyExc_TypeError,
Andrew M. Kuchlingf07aad12000-12-23 14:11:28 +00001454 "ord() expected string of length 1, but " \
Jeremy Hylton394b54d2000-04-12 21:19:47 +00001455 "%.200s found", obj->ob_type->tp_name);
Guido van Rossum09095f32000-03-10 23:00:52 +00001456 return NULL;
1457 }
1458
Guido van Rossumad991772001-01-12 16:03:05 +00001459 PyErr_Format(PyExc_TypeError,
Andrew M. Kuchlingf07aad12000-12-23 14:11:28 +00001460 "ord() expected a character, "
Martin v. Löwisd96ee902006-02-16 14:37:16 +00001461 "but string of length %zd found",
Jeremy Hylton394b54d2000-04-12 21:19:47 +00001462 size);
1463 return NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001464}
1465
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001466PyDoc_STRVAR(ord_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001467"ord(c) -> integer\n\
1468\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001469Return the integer ordinal of a one-character string.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001470
1471
Guido van Rossum79f25d91997-04-29 20:08:16 +00001472static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001473builtin_pow(PyObject *self, PyObject *args)
Guido van Rossum6a00cd81995-01-07 12:39:01 +00001474{
Guido van Rossum09df08a1998-05-22 00:51:39 +00001475 PyObject *v, *w, *z = Py_None;
Guido van Rossum6a00cd81995-01-07 12:39:01 +00001476
Raymond Hettingerea3fdf42002-12-29 16:33:45 +00001477 if (!PyArg_UnpackTuple(args, "pow", 2, 3, &v, &w, &z))
Guido van Rossum6a00cd81995-01-07 12:39:01 +00001478 return NULL;
Guido van Rossum09df08a1998-05-22 00:51:39 +00001479 return PyNumber_Power(v, w, z);
Guido van Rossumd4905451991-05-05 20:00:36 +00001480}
1481
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001482PyDoc_STRVAR(pow_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001483"pow(x, y[, z]) -> number\n\
1484\n\
1485With two arguments, equivalent to x**y. With three arguments,\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001486equivalent to (x**y) % z, but may be more efficient (e.g. for longs).");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001487
1488
Eric Smith7c478942008-03-18 23:45:49 +00001489static PyObject *
1490builtin_print(PyObject *self, PyObject *args, PyObject *kwds)
1491{
1492 static char *kwlist[] = {"sep", "end", "file", 0};
1493 static PyObject *dummy_args;
1494 PyObject *sep = NULL, *end = NULL, *file = NULL;
1495 int i, err;
1496
1497 if (dummy_args == NULL) {
1498 if (!(dummy_args = PyTuple_New(0)))
1499 return NULL;
1500 }
1501 if (!PyArg_ParseTupleAndKeywords(dummy_args, kwds, "|OOO:print",
1502 kwlist, &sep, &end, &file))
1503 return NULL;
1504 if (file == NULL || file == Py_None) {
1505 file = PySys_GetObject("stdout");
1506 /* sys.stdout may be None when FILE* stdout isn't connected */
1507 if (file == Py_None)
1508 Py_RETURN_NONE;
1509 }
1510
1511 if (sep && sep != Py_None && !PyString_Check(sep) &&
1512 !PyUnicode_Check(sep)) {
1513 PyErr_Format(PyExc_TypeError,
1514 "sep must be None, str or unicode, not %.200s",
1515 sep->ob_type->tp_name);
1516 return NULL;
1517 }
1518 if (end && end != Py_None && !PyString_Check(end) &&
1519 !PyUnicode_Check(end)) {
1520 PyErr_Format(PyExc_TypeError,
1521 "end must be None, str or unicode, not %.200s",
1522 end->ob_type->tp_name);
1523 return NULL;
1524 }
1525
1526 for (i = 0; i < PyTuple_Size(args); i++) {
1527 if (i > 0) {
1528 if (sep == NULL || sep == Py_None)
1529 err = PyFile_WriteString(" ", file);
1530 else
1531 err = PyFile_WriteObject(sep, file,
1532 Py_PRINT_RAW);
1533 if (err)
1534 return NULL;
1535 }
1536 err = PyFile_WriteObject(PyTuple_GetItem(args, i), file,
1537 Py_PRINT_RAW);
1538 if (err)
1539 return NULL;
1540 }
1541
1542 if (end == NULL || end == Py_None)
1543 err = PyFile_WriteString("\n", file);
1544 else
1545 err = PyFile_WriteObject(end, file, Py_PRINT_RAW);
1546 if (err)
1547 return NULL;
1548
1549 Py_RETURN_NONE;
1550}
1551
1552PyDoc_STRVAR(print_doc,
1553"print(value, ..., sep=' ', end='\\n', file=sys.stdout)\n\
1554\n\
1555Prints the values to a stream, or to sys.stdout by default.\n\
1556Optional keyword arguments:\n\
1557file: a file-like object (stream); defaults to the current sys.stdout.\n\
1558sep: string inserted between values, default a space.\n\
1559end: string appended after the last value, default a newline.");
1560
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001561
1562/* Return number of items in range (lo, hi, step), when arguments are
1563 * PyInt or PyLong objects. step > 0 required. Return a value < 0 if
1564 * & only if the true value is too large to fit in a signed long.
1565 * Arguments MUST return 1 with either PyInt_Check() or
1566 * PyLong_Check(). Return -1 when there is an error.
1567 */
1568static long
1569get_len_of_range_longs(PyObject *lo, PyObject *hi, PyObject *step)
1570{
1571 /* -------------------------------------------------------------
1572 Algorithm is equal to that of get_len_of_range(), but it operates
1573 on PyObjects (which are assumed to be PyLong or PyInt objects).
1574 ---------------------------------------------------------------*/
1575 long n;
1576 PyObject *diff = NULL;
1577 PyObject *one = NULL;
1578 PyObject *tmp1 = NULL, *tmp2 = NULL, *tmp3 = NULL;
1579 /* holds sub-expression evaluations */
1580
1581 /* if (lo >= hi), return length of 0. */
1582 if (PyObject_Compare(lo, hi) >= 0)
1583 return 0;
1584
1585 if ((one = PyLong_FromLong(1L)) == NULL)
1586 goto Fail;
1587
1588 if ((tmp1 = PyNumber_Subtract(hi, lo)) == NULL)
1589 goto Fail;
1590
1591 if ((diff = PyNumber_Subtract(tmp1, one)) == NULL)
1592 goto Fail;
1593
1594 if ((tmp2 = PyNumber_FloorDivide(diff, step)) == NULL)
1595 goto Fail;
1596
1597 if ((tmp3 = PyNumber_Add(tmp2, one)) == NULL)
1598 goto Fail;
1599
1600 n = PyLong_AsLong(tmp3);
1601 if (PyErr_Occurred()) { /* Check for Overflow */
1602 PyErr_Clear();
1603 goto Fail;
1604 }
1605
1606 Py_DECREF(tmp3);
1607 Py_DECREF(tmp2);
1608 Py_DECREF(diff);
1609 Py_DECREF(tmp1);
1610 Py_DECREF(one);
1611 return n;
1612
1613 Fail:
1614 Py_XDECREF(tmp3);
1615 Py_XDECREF(tmp2);
1616 Py_XDECREF(diff);
1617 Py_XDECREF(tmp1);
1618 Py_XDECREF(one);
1619 return -1;
1620}
1621
1622/* An extension of builtin_range() that handles the case when PyLong
1623 * arguments are given. */
1624static PyObject *
1625handle_range_longs(PyObject *self, PyObject *args)
1626{
1627 PyObject *ilow;
Tim Peters874e1f72003-04-13 22:13:08 +00001628 PyObject *ihigh = NULL;
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001629 PyObject *istep = NULL;
Tim Peters874e1f72003-04-13 22:13:08 +00001630
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001631 PyObject *curnum = NULL;
1632 PyObject *v = NULL;
1633 long bign;
1634 int i, n;
1635 int cmp_result;
1636
Tim Peters874e1f72003-04-13 22:13:08 +00001637 PyObject *zero = PyLong_FromLong(0);
1638
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001639 if (zero == NULL)
1640 return NULL;
1641
Tim Peters874e1f72003-04-13 22:13:08 +00001642 if (!PyArg_UnpackTuple(args, "range", 1, 3, &ilow, &ihigh, &istep)) {
1643 Py_DECREF(zero);
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001644 return NULL;
1645 }
1646
Tim Peters874e1f72003-04-13 22:13:08 +00001647 /* Figure out which way we were called, supply defaults, and be
1648 * sure to incref everything so that the decrefs at the end
1649 * are correct.
1650 */
1651 assert(ilow != NULL);
1652 if (ihigh == NULL) {
1653 /* only 1 arg -- it's the upper limit */
1654 ihigh = ilow;
1655 ilow = NULL;
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001656 }
Tim Peters874e1f72003-04-13 22:13:08 +00001657 assert(ihigh != NULL);
1658 Py_INCREF(ihigh);
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001659
Tim Peters874e1f72003-04-13 22:13:08 +00001660 /* ihigh correct now; do ilow */
1661 if (ilow == NULL)
1662 ilow = zero;
1663 Py_INCREF(ilow);
1664
1665 /* ilow and ihigh correct now; do istep */
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001666 if (istep == NULL) {
1667 istep = PyLong_FromLong(1L);
1668 if (istep == NULL)
1669 goto Fail;
1670 }
1671 else {
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001672 Py_INCREF(istep);
1673 }
1674
Tim Peters874e1f72003-04-13 22:13:08 +00001675 if (!PyInt_Check(ilow) && !PyLong_Check(ilow)) {
Guido van Rossum28e83e32003-04-15 12:43:26 +00001676 PyErr_Format(PyExc_TypeError,
Alex Martellif471d472003-04-23 13:00:44 +00001677 "range() integer start argument expected, got %s.",
Guido van Rossum817d6c92003-04-14 18:25:04 +00001678 ilow->ob_type->tp_name);
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001679 goto Fail;
1680 }
1681
Tim Peters874e1f72003-04-13 22:13:08 +00001682 if (!PyInt_Check(ihigh) && !PyLong_Check(ihigh)) {
Guido van Rossum28e83e32003-04-15 12:43:26 +00001683 PyErr_Format(PyExc_TypeError,
Alex Martellif471d472003-04-23 13:00:44 +00001684 "range() integer end argument expected, got %s.",
Guido van Rossum817d6c92003-04-14 18:25:04 +00001685 ihigh->ob_type->tp_name);
Tim Peters874e1f72003-04-13 22:13:08 +00001686 goto Fail;
1687 }
1688
1689 if (!PyInt_Check(istep) && !PyLong_Check(istep)) {
Guido van Rossum28e83e32003-04-15 12:43:26 +00001690 PyErr_Format(PyExc_TypeError,
Alex Martellif471d472003-04-23 13:00:44 +00001691 "range() integer step argument expected, got %s.",
Guido van Rossum817d6c92003-04-14 18:25:04 +00001692 istep->ob_type->tp_name);
Tim Peters874e1f72003-04-13 22:13:08 +00001693 goto Fail;
1694 }
1695
1696 if (PyObject_Cmp(istep, zero, &cmp_result) == -1)
1697 goto Fail;
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001698 if (cmp_result == 0) {
1699 PyErr_SetString(PyExc_ValueError,
Alex Martellif471d472003-04-23 13:00:44 +00001700 "range() step argument must not be zero");
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001701 goto Fail;
1702 }
1703
1704 if (cmp_result > 0)
1705 bign = get_len_of_range_longs(ilow, ihigh, istep);
1706 else {
1707 PyObject *neg_istep = PyNumber_Negative(istep);
1708 if (neg_istep == NULL)
1709 goto Fail;
1710 bign = get_len_of_range_longs(ihigh, ilow, neg_istep);
1711 Py_DECREF(neg_istep);
1712 }
1713
1714 n = (int)bign;
1715 if (bign < 0 || (long)n != bign) {
1716 PyErr_SetString(PyExc_OverflowError,
1717 "range() result has too many items");
1718 goto Fail;
1719 }
1720
1721 v = PyList_New(n);
1722 if (v == NULL)
1723 goto Fail;
1724
1725 curnum = ilow;
1726 Py_INCREF(curnum);
1727
1728 for (i = 0; i < n; i++) {
1729 PyObject *w = PyNumber_Long(curnum);
1730 PyObject *tmp_num;
1731 if (w == NULL)
1732 goto Fail;
1733
1734 PyList_SET_ITEM(v, i, w);
1735
1736 tmp_num = PyNumber_Add(curnum, istep);
1737 if (tmp_num == NULL)
1738 goto Fail;
1739
1740 Py_DECREF(curnum);
1741 curnum = tmp_num;
1742 }
Tim Peters874e1f72003-04-13 22:13:08 +00001743 Py_DECREF(ilow);
1744 Py_DECREF(ihigh);
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001745 Py_DECREF(istep);
1746 Py_DECREF(zero);
Tim Peters874e1f72003-04-13 22:13:08 +00001747 Py_DECREF(curnum);
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001748 return v;
1749
1750 Fail:
Tim Peters874e1f72003-04-13 22:13:08 +00001751 Py_DECREF(ilow);
1752 Py_DECREF(ihigh);
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001753 Py_XDECREF(istep);
Tim Peters874e1f72003-04-13 22:13:08 +00001754 Py_DECREF(zero);
1755 Py_XDECREF(curnum);
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001756 Py_XDECREF(v);
1757 return NULL;
1758}
1759
Guido van Rossum124eff01999-02-23 16:11:01 +00001760/* Return number of items in range/xrange (lo, hi, step). step > 0
1761 * required. Return a value < 0 if & only if the true value is too
1762 * large to fit in a signed long.
1763 */
1764static long
Thomas Wouterse28c2962000-07-23 22:21:32 +00001765get_len_of_range(long lo, long hi, long step)
Guido van Rossum124eff01999-02-23 16:11:01 +00001766{
1767 /* -------------------------------------------------------------
1768 If lo >= hi, the range is empty.
1769 Else if n values are in the range, the last one is
1770 lo + (n-1)*step, which must be <= hi-1. Rearranging,
1771 n <= (hi - lo - 1)/step + 1, so taking the floor of the RHS gives
1772 the proper value. Since lo < hi in this case, hi-lo-1 >= 0, so
1773 the RHS is non-negative and so truncation is the same as the
1774 floor. Letting M be the largest positive long, the worst case
1775 for the RHS numerator is hi=M, lo=-M-1, and then
1776 hi-lo-1 = M-(-M-1)-1 = 2*M. Therefore unsigned long has enough
1777 precision to compute the RHS exactly.
1778 ---------------------------------------------------------------*/
1779 long n = 0;
1780 if (lo < hi) {
1781 unsigned long uhi = (unsigned long)hi;
1782 unsigned long ulo = (unsigned long)lo;
1783 unsigned long diff = uhi - ulo - 1;
1784 n = (long)(diff / (unsigned long)step + 1);
1785 }
1786 return n;
1787}
1788
Guido van Rossum79f25d91997-04-29 20:08:16 +00001789static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001790builtin_range(PyObject *self, PyObject *args)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001791{
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001792 long ilow = 0, ihigh = 0, istep = 1;
Guido van Rossum124eff01999-02-23 16:11:01 +00001793 long bign;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001794 int i, n;
Guido van Rossum124eff01999-02-23 16:11:01 +00001795
Guido van Rossum79f25d91997-04-29 20:08:16 +00001796 PyObject *v;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001797
Guido van Rossum79f25d91997-04-29 20:08:16 +00001798 if (PyTuple_Size(args) <= 1) {
1799 if (!PyArg_ParseTuple(args,
Guido van Rossum0865dd91995-01-17 16:30:22 +00001800 "l;range() requires 1-3 int arguments",
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001801 &ihigh)) {
1802 PyErr_Clear();
1803 return handle_range_longs(self, args);
1804 }
Guido van Rossum3f5da241990-12-20 15:06:42 +00001805 }
1806 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001807 if (!PyArg_ParseTuple(args,
Guido van Rossum0865dd91995-01-17 16:30:22 +00001808 "ll|l;range() requires 1-3 int arguments",
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001809 &ilow, &ihigh, &istep)) {
1810 PyErr_Clear();
1811 return handle_range_longs(self, args);
1812 }
Guido van Rossum3f5da241990-12-20 15:06:42 +00001813 }
1814 if (istep == 0) {
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001815 PyErr_SetString(PyExc_ValueError,
Alex Martellif471d472003-04-23 13:00:44 +00001816 "range() step argument must not be zero");
Guido van Rossum3f5da241990-12-20 15:06:42 +00001817 return NULL;
1818 }
Guido van Rossum124eff01999-02-23 16:11:01 +00001819 if (istep > 0)
1820 bign = get_len_of_range(ilow, ihigh, istep);
1821 else
1822 bign = get_len_of_range(ihigh, ilow, -istep);
1823 n = (int)bign;
1824 if (bign < 0 || (long)n != bign) {
Guido van Rossume23cde21999-01-12 05:07:47 +00001825 PyErr_SetString(PyExc_OverflowError,
Fred Drake661ea262000-10-24 19:57:45 +00001826 "range() result has too many items");
Guido van Rossume23cde21999-01-12 05:07:47 +00001827 return NULL;
1828 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00001829 v = PyList_New(n);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001830 if (v == NULL)
1831 return NULL;
1832 for (i = 0; i < n; i++) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001833 PyObject *w = PyInt_FromLong(ilow);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001834 if (w == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001835 Py_DECREF(v);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001836 return NULL;
1837 }
Guido van Rossuma937d141998-04-24 18:22:02 +00001838 PyList_SET_ITEM(v, i, w);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001839 ilow += istep;
1840 }
1841 return v;
1842}
1843
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001844PyDoc_STRVAR(range_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001845"range([start,] stop[, step]) -> list of integers\n\
1846\n\
1847Return a list containing an arithmetic progression of integers.\n\
1848range(i, j) returns [i, i+1, i+2, ..., j-1]; start (!) defaults to 0.\n\
1849When step is given, it specifies the increment (or decrement).\n\
1850For example, range(4) returns [0, 1, 2, 3]. The end point is omitted!\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001851These are exactly the valid indices for a list of 4 elements.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001852
1853
Guido van Rossum79f25d91997-04-29 20:08:16 +00001854static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001855builtin_raw_input(PyObject *self, PyObject *args)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001856{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001857 PyObject *v = NULL;
Martin v. Löwis31d2df52002-08-14 15:46:02 +00001858 PyObject *fin = PySys_GetObject("stdin");
1859 PyObject *fout = PySys_GetObject("stdout");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001860
Raymond Hettingerea3fdf42002-12-29 16:33:45 +00001861 if (!PyArg_UnpackTuple(args, "[raw_]input", 0, 1, &v))
Guido van Rossum3165fe61992-09-25 21:59:05 +00001862 return NULL;
Martin v. Löwis31d2df52002-08-14 15:46:02 +00001863
1864 if (fin == NULL) {
Alex Martellia9b9c9f2003-04-23 13:34:35 +00001865 PyErr_SetString(PyExc_RuntimeError, "[raw_]input: lost sys.stdin");
Martin v. Löwis31d2df52002-08-14 15:46:02 +00001866 return NULL;
1867 }
1868 if (fout == NULL) {
Alex Martellia9b9c9f2003-04-23 13:34:35 +00001869 PyErr_SetString(PyExc_RuntimeError, "[raw_]input: lost sys.stdout");
Martin v. Löwis31d2df52002-08-14 15:46:02 +00001870 return NULL;
1871 }
1872 if (PyFile_SoftSpace(fout, 0)) {
1873 if (PyFile_WriteString(" ", fout) != 0)
1874 return NULL;
1875 }
Georg Brandl7e3ba2a2006-08-06 08:23:54 +00001876 if (PyFile_AsFile(fin) && PyFile_AsFile(fout)
Martin v. Löwis566f6af2002-10-26 14:39:10 +00001877 && isatty(fileno(PyFile_AsFile(fin)))
1878 && isatty(fileno(PyFile_AsFile(fout)))) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001879 PyObject *po;
Guido van Rossum872537c1995-07-07 22:43:42 +00001880 char *prompt;
1881 char *s;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001882 PyObject *result;
Guido van Rossum872537c1995-07-07 22:43:42 +00001883 if (v != NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001884 po = PyObject_Str(v);
Guido van Rossum872537c1995-07-07 22:43:42 +00001885 if (po == NULL)
1886 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001887 prompt = PyString_AsString(po);
Guido van Rossumd9b52081998-06-26 18:25:38 +00001888 if (prompt == NULL)
1889 return NULL;
Guido van Rossum872537c1995-07-07 22:43:42 +00001890 }
1891 else {
1892 po = NULL;
1893 prompt = "";
1894 }
Michael W. Hudson52db5192004-08-02 13:21:09 +00001895 s = PyOS_Readline(PyFile_AsFile(fin), PyFile_AsFile(fout),
Martin v. Löwis566f6af2002-10-26 14:39:10 +00001896 prompt);
Guido van Rossum79f25d91997-04-29 20:08:16 +00001897 Py_XDECREF(po);
Guido van Rossum872537c1995-07-07 22:43:42 +00001898 if (s == NULL) {
Michael W. Hudson30ea2f22004-07-07 17:44:12 +00001899 if (!PyErr_Occurred())
1900 PyErr_SetNone(PyExc_KeyboardInterrupt);
Guido van Rossum872537c1995-07-07 22:43:42 +00001901 return NULL;
1902 }
1903 if (*s == '\0') {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001904 PyErr_SetNone(PyExc_EOFError);
Guido van Rossum872537c1995-07-07 22:43:42 +00001905 result = NULL;
1906 }
1907 else { /* strip trailing '\n' */
Guido van Rossum106f2da2000-06-28 21:12:25 +00001908 size_t len = strlen(s);
Martin v. Löwisb1ed7fa2006-04-13 07:52:27 +00001909 if (len > PY_SSIZE_T_MAX) {
Martin v. Löwis31d2df52002-08-14 15:46:02 +00001910 PyErr_SetString(PyExc_OverflowError,
Alex Martellia9b9c9f2003-04-23 13:34:35 +00001911 "[raw_]input: input too long");
Guido van Rossum106f2da2000-06-28 21:12:25 +00001912 result = NULL;
1913 }
1914 else {
Martin v. Löwisb1ed7fa2006-04-13 07:52:27 +00001915 result = PyString_FromStringAndSize(s, len-1);
Guido van Rossum106f2da2000-06-28 21:12:25 +00001916 }
Guido van Rossum872537c1995-07-07 22:43:42 +00001917 }
Guido van Rossumb18618d2000-05-03 23:44:39 +00001918 PyMem_FREE(s);
Guido van Rossum872537c1995-07-07 22:43:42 +00001919 return result;
1920 }
Guido van Rossum90933611991-06-07 16:10:43 +00001921 if (v != NULL) {
Martin v. Löwis31d2df52002-08-14 15:46:02 +00001922 if (PyFile_WriteObject(v, fout, Py_PRINT_RAW) != 0)
Guido van Rossum90933611991-06-07 16:10:43 +00001923 return NULL;
1924 }
Martin v. Löwis31d2df52002-08-14 15:46:02 +00001925 return PyFile_GetLine(fin, -1);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001926}
1927
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001928PyDoc_STRVAR(raw_input_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001929"raw_input([prompt]) -> string\n\
1930\n\
1931Read a string from standard input. The trailing newline is stripped.\n\
1932If the user hits EOF (Unix: Ctl-D, Windows: Ctl-Z+Return), raise EOFError.\n\
1933On Unix, GNU readline is used if enabled. The prompt string, if given,\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001934is printed without a trailing newline before reading.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001935
1936
Guido van Rossum79f25d91997-04-29 20:08:16 +00001937static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001938builtin_reduce(PyObject *self, PyObject *args)
Guido van Rossum12d12c51993-10-26 17:58:25 +00001939{
Tim Peters15d81ef2001-05-04 04:39:21 +00001940 PyObject *seq, *func, *result = NULL, *it;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001941
Neal Norwitzdf25efe2007-05-23 06:58:36 +00001942 if (Py_Py3kWarningFlag &&
1943 PyErr_Warn(PyExc_DeprecationWarning,
1944 "reduce() not supported in 3.x") < 0)
1945 return NULL;
1946
Raymond Hettingerea3fdf42002-12-29 16:33:45 +00001947 if (!PyArg_UnpackTuple(args, "reduce", 2, 3, &func, &seq, &result))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001948 return NULL;
1949 if (result != NULL)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001950 Py_INCREF(result);
Guido van Rossum12d12c51993-10-26 17:58:25 +00001951
Tim Peters15d81ef2001-05-04 04:39:21 +00001952 it = PyObject_GetIter(seq);
1953 if (it == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001954 PyErr_SetString(PyExc_TypeError,
Tim Peters15d81ef2001-05-04 04:39:21 +00001955 "reduce() arg 2 must support iteration");
1956 Py_XDECREF(result);
Guido van Rossum12d12c51993-10-26 17:58:25 +00001957 return NULL;
1958 }
1959
Guido van Rossum79f25d91997-04-29 20:08:16 +00001960 if ((args = PyTuple_New(2)) == NULL)
Guido van Rossum2d951851994-08-29 12:52:16 +00001961 goto Fail;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001962
Tim Peters15d81ef2001-05-04 04:39:21 +00001963 for (;;) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001964 PyObject *op2;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001965
1966 if (args->ob_refcnt > 1) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001967 Py_DECREF(args);
1968 if ((args = PyTuple_New(2)) == NULL)
Guido van Rossum2d951851994-08-29 12:52:16 +00001969 goto Fail;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001970 }
1971
Tim Peters15d81ef2001-05-04 04:39:21 +00001972 op2 = PyIter_Next(it);
1973 if (op2 == NULL) {
Tim Petersf4848da2001-05-05 00:14:56 +00001974 if (PyErr_Occurred())
1975 goto Fail;
1976 break;
Guido van Rossum2d951851994-08-29 12:52:16 +00001977 }
Guido van Rossum12d12c51993-10-26 17:58:25 +00001978
Guido van Rossum2d951851994-08-29 12:52:16 +00001979 if (result == NULL)
1980 result = op2;
1981 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001982 PyTuple_SetItem(args, 0, result);
1983 PyTuple_SetItem(args, 1, op2);
1984 if ((result = PyEval_CallObject(func, args)) == NULL)
Guido van Rossum2d951851994-08-29 12:52:16 +00001985 goto Fail;
1986 }
Guido van Rossum12d12c51993-10-26 17:58:25 +00001987 }
1988
Guido van Rossum79f25d91997-04-29 20:08:16 +00001989 Py_DECREF(args);
Guido van Rossum12d12c51993-10-26 17:58:25 +00001990
Guido van Rossum2d951851994-08-29 12:52:16 +00001991 if (result == NULL)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001992 PyErr_SetString(PyExc_TypeError,
Fred Drake661ea262000-10-24 19:57:45 +00001993 "reduce() of empty sequence with no initial value");
Guido van Rossum2d951851994-08-29 12:52:16 +00001994
Tim Peters15d81ef2001-05-04 04:39:21 +00001995 Py_DECREF(it);
Guido van Rossum12d12c51993-10-26 17:58:25 +00001996 return result;
1997
Guido van Rossum2d951851994-08-29 12:52:16 +00001998Fail:
Guido van Rossum79f25d91997-04-29 20:08:16 +00001999 Py_XDECREF(args);
2000 Py_XDECREF(result);
Tim Peters15d81ef2001-05-04 04:39:21 +00002001 Py_DECREF(it);
Guido van Rossum12d12c51993-10-26 17:58:25 +00002002 return NULL;
2003}
2004
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002005PyDoc_STRVAR(reduce_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002006"reduce(function, sequence[, initial]) -> value\n\
2007\n\
2008Apply a function of two arguments cumulatively to the items of a sequence,\n\
2009from left to right, so as to reduce the sequence to a single value.\n\
2010For example, reduce(lambda x, y: x+y, [1, 2, 3, 4, 5]) calculates\n\
2011((((1+2)+3)+4)+5). If initial is present, it is placed before the items\n\
2012of the sequence in the calculation, and serves as a default when the\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002013sequence is empty.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002014
2015
Guido van Rossum79f25d91997-04-29 20:08:16 +00002016static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002017builtin_reload(PyObject *self, PyObject *v)
Guido van Rossum3f5da241990-12-20 15:06:42 +00002018{
Neal Norwitzdf25efe2007-05-23 06:58:36 +00002019 if (Py_Py3kWarningFlag &&
2020 PyErr_Warn(PyExc_DeprecationWarning,
2021 "reload() not supported in 3.x") < 0)
2022 return NULL;
2023
Guido van Rossum79f25d91997-04-29 20:08:16 +00002024 return PyImport_ReloadModule(v);
Guido van Rossum3f5da241990-12-20 15:06:42 +00002025}
2026
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002027PyDoc_STRVAR(reload_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002028"reload(module) -> module\n\
2029\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002030Reload the module. The module must have been successfully imported before.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002031
2032
Guido van Rossum79f25d91997-04-29 20:08:16 +00002033static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002034builtin_repr(PyObject *self, PyObject *v)
Guido van Rossumc89705d1992-11-26 08:54:07 +00002035{
Guido van Rossum79f25d91997-04-29 20:08:16 +00002036 return PyObject_Repr(v);
Guido van Rossumc89705d1992-11-26 08:54:07 +00002037}
2038
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002039PyDoc_STRVAR(repr_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002040"repr(object) -> string\n\
2041\n\
2042Return the canonical string representation of the object.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002043For most object types, eval(repr(object)) == object.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002044
2045
Guido van Rossum79f25d91997-04-29 20:08:16 +00002046static PyObject *
Georg Brandlccadf842006-03-31 18:54:53 +00002047builtin_round(PyObject *self, PyObject *args, PyObject *kwds)
Guido van Rossum9e51f9b1993-02-12 16:29:05 +00002048{
Jeffrey Yasskin9871d8f2008-01-05 08:47:13 +00002049 double number;
2050 double f;
2051 int ndigits = 0;
2052 int i;
Georg Brandlccadf842006-03-31 18:54:53 +00002053 static char *kwlist[] = {"number", "ndigits", 0};
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002054
Jeffrey Yasskin9871d8f2008-01-05 08:47:13 +00002055 if (!PyArg_ParseTupleAndKeywords(args, kwds, "d|i:round",
2056 kwlist, &number, &ndigits))
2057 return NULL;
2058 f = 1.0;
2059 i = abs(ndigits);
2060 while (--i >= 0)
2061 f = f*10.0;
2062 if (ndigits < 0)
2063 number /= f;
2064 else
2065 number *= f;
2066 if (number >= 0.0)
2067 number = floor(number + 0.5);
2068 else
2069 number = ceil(number - 0.5);
2070 if (ndigits < 0)
2071 number *= f;
2072 else
2073 number /= f;
2074 return PyFloat_FromDouble(number);
Guido van Rossum9e51f9b1993-02-12 16:29:05 +00002075}
2076
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002077PyDoc_STRVAR(round_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002078"round(number[, ndigits]) -> floating point number\n\
2079\n\
2080Round a number to a given precision in decimal digits (default 0 digits).\n\
Jeffrey Yasskin9871d8f2008-01-05 08:47:13 +00002081This always returns a floating point number. Precision may be negative.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002082
Raymond Hettinger64958a12003-12-17 20:43:33 +00002083static PyObject *
2084builtin_sorted(PyObject *self, PyObject *args, PyObject *kwds)
2085{
2086 PyObject *newlist, *v, *seq, *compare=NULL, *keyfunc=NULL, *newargs;
2087 PyObject *callable;
Martin v. Löwis15e62742006-02-27 16:46:16 +00002088 static char *kwlist[] = {"iterable", "cmp", "key", "reverse", 0};
Neal Norwitz6f0d4792005-12-15 06:40:36 +00002089 int reverse;
Raymond Hettinger64958a12003-12-17 20:43:33 +00002090
Neal Norwitz6f0d4792005-12-15 06:40:36 +00002091 /* args 1-4 should match listsort in Objects/listobject.c */
Armin Rigo71d7e702005-09-20 18:13:03 +00002092 if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|OOi:sorted",
2093 kwlist, &seq, &compare, &keyfunc, &reverse))
2094 return NULL;
Raymond Hettinger64958a12003-12-17 20:43:33 +00002095
2096 newlist = PySequence_List(seq);
2097 if (newlist == NULL)
2098 return NULL;
2099
2100 callable = PyObject_GetAttrString(newlist, "sort");
2101 if (callable == NULL) {
2102 Py_DECREF(newlist);
2103 return NULL;
2104 }
Georg Brandl99d7e4e2005-08-31 22:21:15 +00002105
Raymond Hettinger64958a12003-12-17 20:43:33 +00002106 newargs = PyTuple_GetSlice(args, 1, 4);
2107 if (newargs == NULL) {
2108 Py_DECREF(newlist);
2109 Py_DECREF(callable);
2110 return NULL;
2111 }
2112
2113 v = PyObject_Call(callable, newargs, kwds);
2114 Py_DECREF(newargs);
2115 Py_DECREF(callable);
2116 if (v == NULL) {
2117 Py_DECREF(newlist);
2118 return NULL;
2119 }
2120 Py_DECREF(v);
2121 return newlist;
2122}
2123
2124PyDoc_STRVAR(sorted_doc,
2125"sorted(iterable, cmp=None, key=None, reverse=False) --> new sorted list");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002126
Guido van Rossum79f25d91997-04-29 20:08:16 +00002127static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002128builtin_vars(PyObject *self, PyObject *args)
Guido van Rossum2d951851994-08-29 12:52:16 +00002129{
Guido van Rossum79f25d91997-04-29 20:08:16 +00002130 PyObject *v = NULL;
2131 PyObject *d;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002132
Raymond Hettingerea3fdf42002-12-29 16:33:45 +00002133 if (!PyArg_UnpackTuple(args, "vars", 0, 1, &v))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002134 return NULL;
Guido van Rossum2d951851994-08-29 12:52:16 +00002135 if (v == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002136 d = PyEval_GetLocals();
Guido van Rossum53bb7ff1995-07-26 16:26:31 +00002137 if (d == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002138 if (!PyErr_Occurred())
2139 PyErr_SetString(PyExc_SystemError,
Alex Martellia9b9c9f2003-04-23 13:34:35 +00002140 "vars(): no locals!?");
Guido van Rossum53bb7ff1995-07-26 16:26:31 +00002141 }
2142 else
Guido van Rossum79f25d91997-04-29 20:08:16 +00002143 Py_INCREF(d);
Guido van Rossum2d951851994-08-29 12:52:16 +00002144 }
2145 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002146 d = PyObject_GetAttrString(v, "__dict__");
Guido van Rossum2d951851994-08-29 12:52:16 +00002147 if (d == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002148 PyErr_SetString(PyExc_TypeError,
Guido van Rossum2d951851994-08-29 12:52:16 +00002149 "vars() argument must have __dict__ attribute");
2150 return NULL;
2151 }
2152 }
2153 return d;
2154}
2155
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002156PyDoc_STRVAR(vars_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002157"vars([object]) -> dictionary\n\
2158\n\
2159Without arguments, equivalent to locals().\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002160With an argument, equivalent to object.__dict__.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002161
Alex Martellia70b1912003-04-22 08:12:33 +00002162
2163static PyObject*
2164builtin_sum(PyObject *self, PyObject *args)
2165{
2166 PyObject *seq;
2167 PyObject *result = NULL;
2168 PyObject *temp, *item, *iter;
2169
Raymond Hettinger5cf63942003-10-25 06:41:37 +00002170 if (!PyArg_UnpackTuple(args, "sum", 1, 2, &seq, &result))
Alex Martellia70b1912003-04-22 08:12:33 +00002171 return NULL;
2172
2173 iter = PyObject_GetIter(seq);
2174 if (iter == NULL)
2175 return NULL;
2176
2177 if (result == NULL) {
2178 result = PyInt_FromLong(0);
2179 if (result == NULL) {
2180 Py_DECREF(iter);
2181 return NULL;
2182 }
2183 } else {
2184 /* reject string values for 'start' parameter */
2185 if (PyObject_TypeCheck(result, &PyBaseString_Type)) {
2186 PyErr_SetString(PyExc_TypeError,
Alex Martellia9b9c9f2003-04-23 13:34:35 +00002187 "sum() can't sum strings [use ''.join(seq) instead]");
Alex Martellia70b1912003-04-22 08:12:33 +00002188 Py_DECREF(iter);
2189 return NULL;
2190 }
Alex Martelli41c9f882003-04-22 09:24:48 +00002191 Py_INCREF(result);
Alex Martellia70b1912003-04-22 08:12:33 +00002192 }
2193
Raymond Hettinger3f8caa32007-10-24 01:28:33 +00002194#ifndef SLOW_SUM
2195 /* Fast addition by keeping temporary sums in C instead of new Python objects.
2196 Assumes all inputs are the same type. If the assumption fails, default
2197 to the more general routine.
2198 */
2199 if (PyInt_CheckExact(result)) {
2200 long i_result = PyInt_AS_LONG(result);
2201 Py_DECREF(result);
2202 result = NULL;
2203 while(result == NULL) {
2204 item = PyIter_Next(iter);
2205 if (item == NULL) {
2206 Py_DECREF(iter);
2207 if (PyErr_Occurred())
2208 return NULL;
2209 return PyInt_FromLong(i_result);
2210 }
2211 if (PyInt_CheckExact(item)) {
2212 long b = PyInt_AS_LONG(item);
2213 long x = i_result + b;
2214 if ((x^i_result) >= 0 || (x^b) >= 0) {
2215 i_result = x;
2216 Py_DECREF(item);
2217 continue;
2218 }
2219 }
2220 /* Either overflowed or is not an int. Restore real objects and process normally */
2221 result = PyInt_FromLong(i_result);
2222 temp = PyNumber_Add(result, item);
2223 Py_DECREF(result);
2224 Py_DECREF(item);
2225 result = temp;
2226 if (result == NULL) {
2227 Py_DECREF(iter);
2228 return NULL;
2229 }
2230 }
2231 }
2232
2233 if (PyFloat_CheckExact(result)) {
2234 double f_result = PyFloat_AS_DOUBLE(result);
2235 Py_DECREF(result);
2236 result = NULL;
2237 while(result == NULL) {
2238 item = PyIter_Next(iter);
2239 if (item == NULL) {
2240 Py_DECREF(iter);
2241 if (PyErr_Occurred())
2242 return NULL;
2243 return PyFloat_FromDouble(f_result);
2244 }
2245 if (PyFloat_CheckExact(item)) {
2246 PyFPE_START_PROTECT("add", return 0)
2247 f_result += PyFloat_AS_DOUBLE(item);
Raymond Hettinger3a8daf52007-10-24 02:05:51 +00002248 PyFPE_END_PROTECT(f_result)
Raymond Hettingera45c4872007-10-25 02:26:58 +00002249 Py_DECREF(item);
Raymond Hettinger3a8daf52007-10-24 02:05:51 +00002250 continue;
2251 }
2252 if (PyInt_CheckExact(item)) {
2253 PyFPE_START_PROTECT("add", return 0)
2254 f_result += (double)PyInt_AS_LONG(item);
2255 PyFPE_END_PROTECT(f_result)
Raymond Hettingera45c4872007-10-25 02:26:58 +00002256 Py_DECREF(item);
Raymond Hettinger3f8caa32007-10-24 01:28:33 +00002257 continue;
2258 }
2259 result = PyFloat_FromDouble(f_result);
2260 temp = PyNumber_Add(result, item);
2261 Py_DECREF(result);
2262 Py_DECREF(item);
2263 result = temp;
2264 if (result == NULL) {
2265 Py_DECREF(iter);
2266 return NULL;
2267 }
2268 }
2269 }
2270#endif
2271
Alex Martellia70b1912003-04-22 08:12:33 +00002272 for(;;) {
2273 item = PyIter_Next(iter);
2274 if (item == NULL) {
2275 /* error, or end-of-sequence */
2276 if (PyErr_Occurred()) {
2277 Py_DECREF(result);
2278 result = NULL;
2279 }
2280 break;
2281 }
Alex Martellia253e182003-10-25 23:24:14 +00002282 temp = PyNumber_Add(result, item);
Alex Martellia70b1912003-04-22 08:12:33 +00002283 Py_DECREF(result);
2284 Py_DECREF(item);
2285 result = temp;
2286 if (result == NULL)
2287 break;
2288 }
2289 Py_DECREF(iter);
2290 return result;
2291}
2292
2293PyDoc_STRVAR(sum_doc,
Georg Brandl8134d062006-10-12 12:33:07 +00002294"sum(sequence[, start]) -> value\n\
Alex Martellia70b1912003-04-22 08:12:33 +00002295\n\
2296Returns the sum of a sequence of numbers (NOT strings) plus the value\n\
Georg Brandl8134d062006-10-12 12:33:07 +00002297of parameter 'start' (which defaults to 0). When the sequence is\n\
2298empty, returns start.");
Alex Martellia70b1912003-04-22 08:12:33 +00002299
2300
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002301static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002302builtin_isinstance(PyObject *self, PyObject *args)
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002303{
2304 PyObject *inst;
2305 PyObject *cls;
Guido van Rossum823649d2001-03-21 18:40:58 +00002306 int retval;
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002307
Raymond Hettingerea3fdf42002-12-29 16:33:45 +00002308 if (!PyArg_UnpackTuple(args, "isinstance", 2, 2, &inst, &cls))
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002309 return NULL;
Guido van Rossumf5dd9141997-12-02 19:11:45 +00002310
Guido van Rossum823649d2001-03-21 18:40:58 +00002311 retval = PyObject_IsInstance(inst, cls);
2312 if (retval < 0)
Guido van Rossumad991772001-01-12 16:03:05 +00002313 return NULL;
Guido van Rossum77f6a652002-04-03 22:41:51 +00002314 return PyBool_FromLong(retval);
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002315}
2316
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002317PyDoc_STRVAR(isinstance_doc,
Guido van Rossum77f6a652002-04-03 22:41:51 +00002318"isinstance(object, class-or-type-or-tuple) -> bool\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002319\n\
2320Return whether an object is an instance of a class or of a subclass thereof.\n\
Guido van Rossum03290ec2001-10-07 20:54:12 +00002321With a type as second argument, return whether that is the object's type.\n\
2322The form using a tuple, isinstance(x, (A, B, ...)), is a shortcut for\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002323isinstance(x, A) or isinstance(x, B) or ... (etc.).");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002324
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002325
2326static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002327builtin_issubclass(PyObject *self, PyObject *args)
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002328{
2329 PyObject *derived;
2330 PyObject *cls;
2331 int retval;
2332
Raymond Hettingerea3fdf42002-12-29 16:33:45 +00002333 if (!PyArg_UnpackTuple(args, "issubclass", 2, 2, &derived, &cls))
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002334 return NULL;
Guido van Rossum668213d1999-06-16 17:28:37 +00002335
Guido van Rossum823649d2001-03-21 18:40:58 +00002336 retval = PyObject_IsSubclass(derived, cls);
2337 if (retval < 0)
2338 return NULL;
Guido van Rossum77f6a652002-04-03 22:41:51 +00002339 return PyBool_FromLong(retval);
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002340}
2341
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002342PyDoc_STRVAR(issubclass_doc,
Guido van Rossum77f6a652002-04-03 22:41:51 +00002343"issubclass(C, B) -> bool\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002344\n\
Walter Dörwaldd9a6ad32002-12-12 16:41:44 +00002345Return whether class C is a subclass (i.e., a derived class) of class B.\n\
2346When using a tuple as the second argument issubclass(X, (A, B, ...)),\n\
2347is a shortcut for issubclass(X, A) or issubclass(X, B) or ... (etc.).");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002348
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002349
Barry Warsawbd599b52000-08-03 15:45:29 +00002350static PyObject*
2351builtin_zip(PyObject *self, PyObject *args)
2352{
2353 PyObject *ret;
Martin v. Löwisd96ee902006-02-16 14:37:16 +00002354 const Py_ssize_t itemsize = PySequence_Length(args);
2355 Py_ssize_t i;
Tim Peters8572b4f2001-05-06 01:05:02 +00002356 PyObject *itlist; /* tuple of iterators */
Martin v. Löwisd96ee902006-02-16 14:37:16 +00002357 Py_ssize_t len; /* guess at result length */
Barry Warsawbd599b52000-08-03 15:45:29 +00002358
Raymond Hettingereaef6152003-08-02 07:42:57 +00002359 if (itemsize == 0)
2360 return PyList_New(0);
2361
Barry Warsawbd599b52000-08-03 15:45:29 +00002362 /* args must be a tuple */
2363 assert(PyTuple_Check(args));
2364
Tim Peters39a86c22002-05-12 07:19:38 +00002365 /* Guess at result length: the shortest of the input lengths.
2366 If some argument refuses to say, we refuse to guess too, lest
2367 an argument like xrange(sys.maxint) lead us astray.*/
Tim Peters67d687a2002-04-29 21:27:32 +00002368 len = -1; /* unknown */
2369 for (i = 0; i < itemsize; ++i) {
2370 PyObject *item = PyTuple_GET_ITEM(args, i);
Raymond Hettinger4e2f7142007-12-06 00:56:53 +00002371 Py_ssize_t thislen = _PyObject_LengthHint(item, -1);
Tim Peters39a86c22002-05-12 07:19:38 +00002372 if (thislen < 0) {
Tim Peters39a86c22002-05-12 07:19:38 +00002373 len = -1;
2374 break;
2375 }
Tim Peters67d687a2002-04-29 21:27:32 +00002376 else if (len < 0 || thislen < len)
2377 len = thislen;
2378 }
2379
Tim Peters8572b4f2001-05-06 01:05:02 +00002380 /* allocate result list */
Tim Peters67d687a2002-04-29 21:27:32 +00002381 if (len < 0)
2382 len = 10; /* arbitrary */
2383 if ((ret = PyList_New(len)) == NULL)
Barry Warsawbd599b52000-08-03 15:45:29 +00002384 return NULL;
2385
Tim Peters8572b4f2001-05-06 01:05:02 +00002386 /* obtain iterators */
2387 itlist = PyTuple_New(itemsize);
2388 if (itlist == NULL)
2389 goto Fail_ret;
2390 for (i = 0; i < itemsize; ++i) {
2391 PyObject *item = PyTuple_GET_ITEM(args, i);
2392 PyObject *it = PyObject_GetIter(item);
2393 if (it == NULL) {
2394 if (PyErr_ExceptionMatches(PyExc_TypeError))
2395 PyErr_Format(PyExc_TypeError,
Neal Norwitza361bd82006-02-19 19:31:50 +00002396 "zip argument #%zd must support iteration",
Tim Peters8572b4f2001-05-06 01:05:02 +00002397 i+1);
2398 goto Fail_ret_itlist;
Barry Warsawbd599b52000-08-03 15:45:29 +00002399 }
Tim Peters8572b4f2001-05-06 01:05:02 +00002400 PyTuple_SET_ITEM(itlist, i, it);
2401 }
Barry Warsawbd599b52000-08-03 15:45:29 +00002402
Tim Peters8572b4f2001-05-06 01:05:02 +00002403 /* build result into ret list */
Tim Peters67d687a2002-04-29 21:27:32 +00002404 for (i = 0; ; ++i) {
2405 int j;
Tim Peters8572b4f2001-05-06 01:05:02 +00002406 PyObject *next = PyTuple_New(itemsize);
2407 if (!next)
2408 goto Fail_ret_itlist;
2409
Tim Peters67d687a2002-04-29 21:27:32 +00002410 for (j = 0; j < itemsize; j++) {
2411 PyObject *it = PyTuple_GET_ITEM(itlist, j);
Tim Peters8572b4f2001-05-06 01:05:02 +00002412 PyObject *item = PyIter_Next(it);
Barry Warsawbd599b52000-08-03 15:45:29 +00002413 if (!item) {
Tim Peters8572b4f2001-05-06 01:05:02 +00002414 if (PyErr_Occurred()) {
2415 Py_DECREF(ret);
2416 ret = NULL;
Barry Warsawbd599b52000-08-03 15:45:29 +00002417 }
2418 Py_DECREF(next);
Tim Peters8572b4f2001-05-06 01:05:02 +00002419 Py_DECREF(itlist);
Tim Peters67d687a2002-04-29 21:27:32 +00002420 goto Done;
Barry Warsawbd599b52000-08-03 15:45:29 +00002421 }
Tim Peters67d687a2002-04-29 21:27:32 +00002422 PyTuple_SET_ITEM(next, j, item);
Barry Warsawbd599b52000-08-03 15:45:29 +00002423 }
Tim Peters8572b4f2001-05-06 01:05:02 +00002424
Tim Peters67d687a2002-04-29 21:27:32 +00002425 if (i < len)
2426 PyList_SET_ITEM(ret, i, next);
2427 else {
2428 int status = PyList_Append(ret, next);
2429 Py_DECREF(next);
2430 ++len;
2431 if (status < 0)
2432 goto Fail_ret_itlist;
2433 }
Barry Warsawbd599b52000-08-03 15:45:29 +00002434 }
Tim Peters8572b4f2001-05-06 01:05:02 +00002435
Tim Peters67d687a2002-04-29 21:27:32 +00002436Done:
2437 if (ret != NULL && i < len) {
2438 /* The list is too big. */
2439 if (PyList_SetSlice(ret, i, len, NULL) < 0)
2440 return NULL;
2441 }
2442 return ret;
2443
Tim Peters8572b4f2001-05-06 01:05:02 +00002444Fail_ret_itlist:
2445 Py_DECREF(itlist);
2446Fail_ret:
2447 Py_DECREF(ret);
2448 return NULL;
Barry Warsawbd599b52000-08-03 15:45:29 +00002449}
2450
2451
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002452PyDoc_STRVAR(zip_doc,
Barry Warsawbd599b52000-08-03 15:45:29 +00002453"zip(seq1 [, seq2 [...]]) -> [(seq1[0], seq2[0] ...), (...)]\n\
2454\n\
2455Return a list of tuples, where each tuple contains the i-th element\n\
2456from each of the argument sequences. The returned list is truncated\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002457in length to the length of the shortest argument sequence.");
Barry Warsawbd599b52000-08-03 15:45:29 +00002458
2459
Guido van Rossum79f25d91997-04-29 20:08:16 +00002460static PyMethodDef builtin_methods[] = {
Neal Norwitz92e212f2006-04-03 04:48:37 +00002461 {"__import__", (PyCFunction)builtin___import__, METH_VARARGS | METH_KEYWORDS, import_doc},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002462 {"abs", builtin_abs, METH_O, abs_doc},
Raymond Hettinger96229b12005-03-11 06:49:40 +00002463 {"all", builtin_all, METH_O, all_doc},
2464 {"any", builtin_any, METH_O, any_doc},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002465 {"apply", builtin_apply, METH_VARARGS, apply_doc},
Eric Smith3cd81942008-02-22 16:30:22 +00002466 {"bin", builtin_bin, METH_O, bin_doc},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002467 {"callable", builtin_callable, METH_O, callable_doc},
2468 {"chr", builtin_chr, METH_VARARGS, chr_doc},
2469 {"cmp", builtin_cmp, METH_VARARGS, cmp_doc},
2470 {"coerce", builtin_coerce, METH_VARARGS, coerce_doc},
Georg Brandl5240d742007-03-13 20:46:32 +00002471 {"compile", (PyCFunction)builtin_compile, METH_VARARGS | METH_KEYWORDS, compile_doc},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002472 {"delattr", builtin_delattr, METH_VARARGS, delattr_doc},
2473 {"dir", builtin_dir, METH_VARARGS, dir_doc},
2474 {"divmod", builtin_divmod, METH_VARARGS, divmod_doc},
2475 {"eval", builtin_eval, METH_VARARGS, eval_doc},
2476 {"execfile", builtin_execfile, METH_VARARGS, execfile_doc},
2477 {"filter", builtin_filter, METH_VARARGS, filter_doc},
Eric Smitha9f7d622008-02-17 19:46:49 +00002478 {"format", builtin_format, METH_VARARGS, format_doc},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002479 {"getattr", builtin_getattr, METH_VARARGS, getattr_doc},
2480 {"globals", (PyCFunction)builtin_globals, METH_NOARGS, globals_doc},
2481 {"hasattr", builtin_hasattr, METH_VARARGS, hasattr_doc},
2482 {"hash", builtin_hash, METH_O, hash_doc},
2483 {"hex", builtin_hex, METH_O, hex_doc},
2484 {"id", builtin_id, METH_O, id_doc},
2485 {"input", builtin_input, METH_VARARGS, input_doc},
2486 {"intern", builtin_intern, METH_VARARGS, intern_doc},
2487 {"isinstance", builtin_isinstance, METH_VARARGS, isinstance_doc},
2488 {"issubclass", builtin_issubclass, METH_VARARGS, issubclass_doc},
2489 {"iter", builtin_iter, METH_VARARGS, iter_doc},
2490 {"len", builtin_len, METH_O, len_doc},
2491 {"locals", (PyCFunction)builtin_locals, METH_NOARGS, locals_doc},
2492 {"map", builtin_map, METH_VARARGS, map_doc},
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00002493 {"max", (PyCFunction)builtin_max, METH_VARARGS | METH_KEYWORDS, max_doc},
2494 {"min", (PyCFunction)builtin_min, METH_VARARGS | METH_KEYWORDS, min_doc},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002495 {"oct", builtin_oct, METH_O, oct_doc},
Neal Norwitzc4edb0e2006-05-02 04:43:14 +00002496 {"open", (PyCFunction)builtin_open, METH_VARARGS | METH_KEYWORDS, open_doc},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002497 {"ord", builtin_ord, METH_O, ord_doc},
2498 {"pow", builtin_pow, METH_VARARGS, pow_doc},
Eric Smith7c478942008-03-18 23:45:49 +00002499 {"print", (PyCFunction)builtin_print, METH_VARARGS | METH_KEYWORDS, print_doc},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002500 {"range", builtin_range, METH_VARARGS, range_doc},
2501 {"raw_input", builtin_raw_input, METH_VARARGS, raw_input_doc},
2502 {"reduce", builtin_reduce, METH_VARARGS, reduce_doc},
2503 {"reload", builtin_reload, METH_O, reload_doc},
2504 {"repr", builtin_repr, METH_O, repr_doc},
Georg Brandlccadf842006-03-31 18:54:53 +00002505 {"round", (PyCFunction)builtin_round, METH_VARARGS | METH_KEYWORDS, round_doc},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002506 {"setattr", builtin_setattr, METH_VARARGS, setattr_doc},
Raymond Hettinger64958a12003-12-17 20:43:33 +00002507 {"sorted", (PyCFunction)builtin_sorted, METH_VARARGS | METH_KEYWORDS, sorted_doc},
Alex Martellia70b1912003-04-22 08:12:33 +00002508 {"sum", builtin_sum, METH_VARARGS, sum_doc},
Martin v. Löwis339d0f72001-08-17 18:39:25 +00002509#ifdef Py_USING_UNICODE
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002510 {"unichr", builtin_unichr, METH_VARARGS, unichr_doc},
Martin v. Löwis339d0f72001-08-17 18:39:25 +00002511#endif
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002512 {"vars", builtin_vars, METH_VARARGS, vars_doc},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002513 {"zip", builtin_zip, METH_VARARGS, zip_doc},
Guido van Rossumc02e15c1991-12-16 13:03:00 +00002514 {NULL, NULL},
Guido van Rossum3f5da241990-12-20 15:06:42 +00002515};
2516
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002517PyDoc_STRVAR(builtin_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002518"Built-in functions, exceptions, and other objects.\n\
2519\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002520Noteworthy: None is the `nil' object; Ellipsis represents `...' in slices.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002521
Guido van Rossum25ce5661997-08-02 03:10:38 +00002522PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002523_PyBuiltin_Init(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +00002524{
Fred Drake5550de32000-06-20 04:54:19 +00002525 PyObject *mod, *dict, *debug;
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002526 mod = Py_InitModule4("__builtin__", builtin_methods,
2527 builtin_doc, (PyObject *)NULL,
2528 PYTHON_API_VERSION);
Guido van Rossum25ce5661997-08-02 03:10:38 +00002529 if (mod == NULL)
2530 return NULL;
2531 dict = PyModule_GetDict(mod);
Tim Peters4b7625e2001-09-13 21:37:17 +00002532
Tim Peters7571a0f2003-03-23 17:52:28 +00002533#ifdef Py_TRACE_REFS
2534 /* __builtin__ exposes a number of statically allocated objects
2535 * that, before this code was added in 2.3, never showed up in
2536 * the list of "all objects" maintained by Py_TRACE_REFS. As a
2537 * result, programs leaking references to None and False (etc)
2538 * couldn't be diagnosed by examining sys.getobjects(0).
2539 */
2540#define ADD_TO_ALL(OBJECT) _Py_AddToAllObjects((PyObject *)(OBJECT), 0)
2541#else
2542#define ADD_TO_ALL(OBJECT) (void)0
2543#endif
2544
Tim Peters4b7625e2001-09-13 21:37:17 +00002545#define SETBUILTIN(NAME, OBJECT) \
Tim Peters7571a0f2003-03-23 17:52:28 +00002546 if (PyDict_SetItemString(dict, NAME, (PyObject *)OBJECT) < 0) \
2547 return NULL; \
2548 ADD_TO_ALL(OBJECT)
Tim Peters4b7625e2001-09-13 21:37:17 +00002549
2550 SETBUILTIN("None", Py_None);
2551 SETBUILTIN("Ellipsis", Py_Ellipsis);
2552 SETBUILTIN("NotImplemented", Py_NotImplemented);
Guido van Rossum77f6a652002-04-03 22:41:51 +00002553 SETBUILTIN("False", Py_False);
2554 SETBUILTIN("True", Py_True);
Neal Norwitz32a7e7f2002-05-31 19:58:02 +00002555 SETBUILTIN("basestring", &PyBaseString_Type);
Guido van Rossum77f6a652002-04-03 22:41:51 +00002556 SETBUILTIN("bool", &PyBool_Type);
Travis E. Oliphant3781aef2008-03-18 04:44:57 +00002557 /* SETBUILTIN("memoryview", &PyMemoryView_Type); */
Christian Heimes288e89a2008-01-18 18:24:07 +00002558 SETBUILTIN("bytes", &PyString_Type);
Guido van Rossumbea18cc2002-06-14 20:41:17 +00002559 SETBUILTIN("buffer", &PyBuffer_Type);
Tim Peters4b7625e2001-09-13 21:37:17 +00002560 SETBUILTIN("classmethod", &PyClassMethod_Type);
Tim Peters6d6c1a32001-08-02 04:15:00 +00002561#ifndef WITHOUT_COMPLEX
Tim Peters4b7625e2001-09-13 21:37:17 +00002562 SETBUILTIN("complex", &PyComplex_Type);
Tim Peters6d6c1a32001-08-02 04:15:00 +00002563#endif
Tim Petersa427a2b2001-10-29 22:25:45 +00002564 SETBUILTIN("dict", &PyDict_Type);
Tim Peters67d687a2002-04-29 21:27:32 +00002565 SETBUILTIN("enumerate", &PyEnum_Type);
Neal Norwitzc4edb0e2006-05-02 04:43:14 +00002566 SETBUILTIN("file", &PyFile_Type);
Tim Peters4b7625e2001-09-13 21:37:17 +00002567 SETBUILTIN("float", &PyFloat_Type);
Raymond Hettingera690a992003-11-16 16:17:49 +00002568 SETBUILTIN("frozenset", &PyFrozenSet_Type);
Tim Peters4b7625e2001-09-13 21:37:17 +00002569 SETBUILTIN("property", &PyProperty_Type);
2570 SETBUILTIN("int", &PyInt_Type);
2571 SETBUILTIN("list", &PyList_Type);
2572 SETBUILTIN("long", &PyLong_Type);
2573 SETBUILTIN("object", &PyBaseObject_Type);
Raymond Hettinger85c20a42003-11-06 14:06:48 +00002574 SETBUILTIN("reversed", &PyReversed_Type);
Raymond Hettingera690a992003-11-16 16:17:49 +00002575 SETBUILTIN("set", &PySet_Type);
Guido van Rossumbea18cc2002-06-14 20:41:17 +00002576 SETBUILTIN("slice", &PySlice_Type);
Tim Peters4b7625e2001-09-13 21:37:17 +00002577 SETBUILTIN("staticmethod", &PyStaticMethod_Type);
2578 SETBUILTIN("str", &PyString_Type);
2579 SETBUILTIN("super", &PySuper_Type);
2580 SETBUILTIN("tuple", &PyTuple_Type);
2581 SETBUILTIN("type", &PyType_Type);
Raymond Hettingerc4c453f2002-06-05 23:12:45 +00002582 SETBUILTIN("xrange", &PyRange_Type);
Martin v. Löwis339d0f72001-08-17 18:39:25 +00002583#ifdef Py_USING_UNICODE
Tim Peters4b7625e2001-09-13 21:37:17 +00002584 SETBUILTIN("unicode", &PyUnicode_Type);
Martin v. Löwis339d0f72001-08-17 18:39:25 +00002585#endif
Guido van Rossum77f6a652002-04-03 22:41:51 +00002586 debug = PyBool_FromLong(Py_OptimizeFlag == 0);
Fred Drake5550de32000-06-20 04:54:19 +00002587 if (PyDict_SetItemString(dict, "__debug__", debug) < 0) {
2588 Py_XDECREF(debug);
Guido van Rossum25ce5661997-08-02 03:10:38 +00002589 return NULL;
Fred Drake5550de32000-06-20 04:54:19 +00002590 }
2591 Py_XDECREF(debug);
Barry Warsaw757af0e1997-08-29 22:13:51 +00002592
Guido van Rossum25ce5661997-08-02 03:10:38 +00002593 return mod;
Tim Peters7571a0f2003-03-23 17:52:28 +00002594#undef ADD_TO_ALL
Tim Peters4b7625e2001-09-13 21:37:17 +00002595#undef SETBUILTIN
Guido van Rossum3f5da241990-12-20 15:06:42 +00002596}
2597
Guido van Rossume77a7571993-11-03 15:01:26 +00002598/* Helper for filter(): filter a tuple through a function */
Guido van Rossum12d12c51993-10-26 17:58:25 +00002599
Guido van Rossum79f25d91997-04-29 20:08:16 +00002600static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002601filtertuple(PyObject *func, PyObject *tuple)
Guido van Rossum12d12c51993-10-26 17:58:25 +00002602{
Guido van Rossum79f25d91997-04-29 20:08:16 +00002603 PyObject *result;
Martin v. Löwis18e16552006-02-15 17:27:45 +00002604 Py_ssize_t i, j;
2605 Py_ssize_t len = PyTuple_Size(tuple);
Guido van Rossum12d12c51993-10-26 17:58:25 +00002606
Guido van Rossumb7b45621995-08-04 04:07:45 +00002607 if (len == 0) {
Walter Dörwaldc3da83f2003-02-04 20:24:45 +00002608 if (PyTuple_CheckExact(tuple))
2609 Py_INCREF(tuple);
2610 else
2611 tuple = PyTuple_New(0);
Guido van Rossumb7b45621995-08-04 04:07:45 +00002612 return tuple;
2613 }
2614
Guido van Rossum79f25d91997-04-29 20:08:16 +00002615 if ((result = PyTuple_New(len)) == NULL)
Guido van Rossum2586bf01993-11-01 16:21:44 +00002616 return NULL;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002617
Guido van Rossum12d12c51993-10-26 17:58:25 +00002618 for (i = j = 0; i < len; ++i) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002619 PyObject *item, *good;
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002620 int ok;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002621
Walter Dörwald8dd19322003-02-10 17:36:40 +00002622 if (tuple->ob_type->tp_as_sequence &&
2623 tuple->ob_type->tp_as_sequence->sq_item) {
2624 item = tuple->ob_type->tp_as_sequence->sq_item(tuple, i);
Walter Dörwaldc58a3a12003-08-18 18:28:45 +00002625 if (item == NULL)
2626 goto Fail_1;
Walter Dörwald8dd19322003-02-10 17:36:40 +00002627 } else {
Alex Martellia9b9c9f2003-04-23 13:34:35 +00002628 PyErr_SetString(PyExc_TypeError, "filter(): unsubscriptable tuple");
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002629 goto Fail_1;
Walter Dörwald8dd19322003-02-10 17:36:40 +00002630 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002631 if (func == Py_None) {
2632 Py_INCREF(item);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002633 good = item;
2634 }
2635 else {
Raymond Hettinger8ae46892003-10-12 19:09:37 +00002636 PyObject *arg = PyTuple_Pack(1, item);
Walter Dörwaldc58a3a12003-08-18 18:28:45 +00002637 if (arg == NULL) {
2638 Py_DECREF(item);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002639 goto Fail_1;
Walter Dörwaldc58a3a12003-08-18 18:28:45 +00002640 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002641 good = PyEval_CallObject(func, arg);
2642 Py_DECREF(arg);
Walter Dörwaldc58a3a12003-08-18 18:28:45 +00002643 if (good == NULL) {
2644 Py_DECREF(item);
Guido van Rossum12d12c51993-10-26 17:58:25 +00002645 goto Fail_1;
Walter Dörwaldc58a3a12003-08-18 18:28:45 +00002646 }
Guido van Rossum12d12c51993-10-26 17:58:25 +00002647 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002648 ok = PyObject_IsTrue(good);
2649 Py_DECREF(good);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002650 if (ok) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002651 if (PyTuple_SetItem(result, j++, item) < 0)
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002652 goto Fail_1;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002653 }
Walter Dörwaldc58a3a12003-08-18 18:28:45 +00002654 else
2655 Py_DECREF(item);
Guido van Rossum12d12c51993-10-26 17:58:25 +00002656 }
2657
Tim Peters4324aa32001-05-28 22:30:08 +00002658 if (_PyTuple_Resize(&result, j) < 0)
Guido van Rossum12d12c51993-10-26 17:58:25 +00002659 return NULL;
2660
Guido van Rossum12d12c51993-10-26 17:58:25 +00002661 return result;
2662
Guido van Rossum12d12c51993-10-26 17:58:25 +00002663Fail_1:
Guido van Rossum79f25d91997-04-29 20:08:16 +00002664 Py_DECREF(result);
Guido van Rossum12d12c51993-10-26 17:58:25 +00002665 return NULL;
2666}
2667
2668
Guido van Rossume77a7571993-11-03 15:01:26 +00002669/* Helper for filter(): filter a string through a function */
Guido van Rossum12d12c51993-10-26 17:58:25 +00002670
Guido van Rossum79f25d91997-04-29 20:08:16 +00002671static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002672filterstring(PyObject *func, PyObject *strobj)
Guido van Rossum12d12c51993-10-26 17:58:25 +00002673{
Guido van Rossum79f25d91997-04-29 20:08:16 +00002674 PyObject *result;
Martin v. Löwis18e16552006-02-15 17:27:45 +00002675 Py_ssize_t i, j;
2676 Py_ssize_t len = PyString_Size(strobj);
2677 Py_ssize_t outlen = len;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002678
Guido van Rossum79f25d91997-04-29 20:08:16 +00002679 if (func == Py_None) {
Walter Dörwald1918f772003-02-10 13:19:13 +00002680 /* If it's a real string we can return the original,
2681 * as no character is ever false and __getitem__
2682 * does return this character. If it's a subclass
2683 * we must go through the __getitem__ loop */
2684 if (PyString_CheckExact(strobj)) {
Walter Dörwaldc3da83f2003-02-04 20:24:45 +00002685 Py_INCREF(strobj);
Walter Dörwald1918f772003-02-10 13:19:13 +00002686 return strobj;
2687 }
Guido van Rossum12d12c51993-10-26 17:58:25 +00002688 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002689 if ((result = PyString_FromStringAndSize(NULL, len)) == NULL)
Guido van Rossum2586bf01993-11-01 16:21:44 +00002690 return NULL;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002691
Guido van Rossum12d12c51993-10-26 17:58:25 +00002692 for (i = j = 0; i < len; ++i) {
Walter Dörwald1918f772003-02-10 13:19:13 +00002693 PyObject *item;
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002694 int ok;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002695
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002696 item = (*strobj->ob_type->tp_as_sequence->sq_item)(strobj, i);
2697 if (item == NULL)
2698 goto Fail_1;
Walter Dörwald1918f772003-02-10 13:19:13 +00002699 if (func==Py_None) {
2700 ok = 1;
2701 } else {
2702 PyObject *arg, *good;
Raymond Hettinger8ae46892003-10-12 19:09:37 +00002703 arg = PyTuple_Pack(1, item);
Walter Dörwald1918f772003-02-10 13:19:13 +00002704 if (arg == NULL) {
2705 Py_DECREF(item);
2706 goto Fail_1;
2707 }
2708 good = PyEval_CallObject(func, arg);
2709 Py_DECREF(arg);
2710 if (good == NULL) {
2711 Py_DECREF(item);
2712 goto Fail_1;
2713 }
2714 ok = PyObject_IsTrue(good);
2715 Py_DECREF(good);
Tim Peters388ed082001-04-07 20:34:48 +00002716 }
Walter Dörwald903f1e02003-02-04 16:28:00 +00002717 if (ok) {
Martin v. Löwisd96ee902006-02-16 14:37:16 +00002718 Py_ssize_t reslen;
Walter Dörwald903f1e02003-02-04 16:28:00 +00002719 if (!PyString_Check(item)) {
2720 PyErr_SetString(PyExc_TypeError, "can't filter str to str:"
2721 " __getitem__ returned different type");
2722 Py_DECREF(item);
2723 goto Fail_1;
2724 }
2725 reslen = PyString_GET_SIZE(item);
2726 if (reslen == 1) {
2727 PyString_AS_STRING(result)[j++] =
2728 PyString_AS_STRING(item)[0];
2729 } else {
2730 /* do we need more space? */
Martin v. Löwisd96ee902006-02-16 14:37:16 +00002731 Py_ssize_t need = j + reslen + len-i-1;
Walter Dörwald903f1e02003-02-04 16:28:00 +00002732 if (need > outlen) {
2733 /* overallocate, to avoid reallocations */
2734 if (need<2*outlen)
2735 need = 2*outlen;
2736 if (_PyString_Resize(&result, need)) {
2737 Py_DECREF(item);
2738 return NULL;
2739 }
2740 outlen = need;
2741 }
2742 memcpy(
2743 PyString_AS_STRING(result) + j,
2744 PyString_AS_STRING(item),
2745 reslen
2746 );
2747 j += reslen;
2748 }
2749 }
Tim Peters388ed082001-04-07 20:34:48 +00002750 Py_DECREF(item);
Guido van Rossum12d12c51993-10-26 17:58:25 +00002751 }
2752
Walter Dörwald903f1e02003-02-04 16:28:00 +00002753 if (j < outlen)
Tim Peters5de98422002-04-27 18:44:32 +00002754 _PyString_Resize(&result, j);
Guido van Rossum12d12c51993-10-26 17:58:25 +00002755
Guido van Rossum12d12c51993-10-26 17:58:25 +00002756 return result;
2757
Guido van Rossum12d12c51993-10-26 17:58:25 +00002758Fail_1:
Guido van Rossum79f25d91997-04-29 20:08:16 +00002759 Py_DECREF(result);
Guido van Rossum12d12c51993-10-26 17:58:25 +00002760 return NULL;
2761}
Martin v. Löwis8afd7572003-01-25 22:46:11 +00002762
2763#ifdef Py_USING_UNICODE
2764/* Helper for filter(): filter a Unicode object through a function */
2765
2766static PyObject *
2767filterunicode(PyObject *func, PyObject *strobj)
2768{
2769 PyObject *result;
Martin v. Löwis725507b2006-03-07 12:08:51 +00002770 register Py_ssize_t i, j;
Martin v. Löwisd96ee902006-02-16 14:37:16 +00002771 Py_ssize_t len = PyUnicode_GetSize(strobj);
2772 Py_ssize_t outlen = len;
Martin v. Löwis8afd7572003-01-25 22:46:11 +00002773
2774 if (func == Py_None) {
Walter Dörwald1918f772003-02-10 13:19:13 +00002775 /* If it's a real string we can return the original,
2776 * as no character is ever false and __getitem__
2777 * does return this character. If it's a subclass
2778 * we must go through the __getitem__ loop */
2779 if (PyUnicode_CheckExact(strobj)) {
Walter Dörwaldc3da83f2003-02-04 20:24:45 +00002780 Py_INCREF(strobj);
Walter Dörwald1918f772003-02-10 13:19:13 +00002781 return strobj;
2782 }
Martin v. Löwis8afd7572003-01-25 22:46:11 +00002783 }
2784 if ((result = PyUnicode_FromUnicode(NULL, len)) == NULL)
2785 return NULL;
2786
2787 for (i = j = 0; i < len; ++i) {
2788 PyObject *item, *arg, *good;
2789 int ok;
2790
2791 item = (*strobj->ob_type->tp_as_sequence->sq_item)(strobj, i);
2792 if (item == NULL)
2793 goto Fail_1;
Walter Dörwald1918f772003-02-10 13:19:13 +00002794 if (func == Py_None) {
2795 ok = 1;
2796 } else {
Raymond Hettinger8ae46892003-10-12 19:09:37 +00002797 arg = PyTuple_Pack(1, item);
Walter Dörwald1918f772003-02-10 13:19:13 +00002798 if (arg == NULL) {
2799 Py_DECREF(item);
2800 goto Fail_1;
2801 }
2802 good = PyEval_CallObject(func, arg);
2803 Py_DECREF(arg);
2804 if (good == NULL) {
2805 Py_DECREF(item);
2806 goto Fail_1;
2807 }
2808 ok = PyObject_IsTrue(good);
2809 Py_DECREF(good);
Martin v. Löwis8afd7572003-01-25 22:46:11 +00002810 }
Walter Dörwald903f1e02003-02-04 16:28:00 +00002811 if (ok) {
Martin v. Löwisd96ee902006-02-16 14:37:16 +00002812 Py_ssize_t reslen;
Walter Dörwald903f1e02003-02-04 16:28:00 +00002813 if (!PyUnicode_Check(item)) {
Georg Brandl99d7e4e2005-08-31 22:21:15 +00002814 PyErr_SetString(PyExc_TypeError,
Jeremy Hylton1aad9c72003-09-16 03:10:59 +00002815 "can't filter unicode to unicode:"
2816 " __getitem__ returned different type");
Walter Dörwald903f1e02003-02-04 16:28:00 +00002817 Py_DECREF(item);
2818 goto Fail_1;
2819 }
2820 reslen = PyUnicode_GET_SIZE(item);
Georg Brandl99d7e4e2005-08-31 22:21:15 +00002821 if (reslen == 1)
Walter Dörwald903f1e02003-02-04 16:28:00 +00002822 PyUnicode_AS_UNICODE(result)[j++] =
2823 PyUnicode_AS_UNICODE(item)[0];
Jeremy Hylton1aad9c72003-09-16 03:10:59 +00002824 else {
Walter Dörwald903f1e02003-02-04 16:28:00 +00002825 /* do we need more space? */
Martin v. Löwisd96ee902006-02-16 14:37:16 +00002826 Py_ssize_t need = j + reslen + len - i - 1;
Walter Dörwald903f1e02003-02-04 16:28:00 +00002827 if (need > outlen) {
Georg Brandl99d7e4e2005-08-31 22:21:15 +00002828 /* overallocate,
Jeremy Hylton1aad9c72003-09-16 03:10:59 +00002829 to avoid reallocations */
2830 if (need < 2 * outlen)
2831 need = 2 * outlen;
Jeremy Hylton364f6be2003-09-16 03:17:16 +00002832 if (PyUnicode_Resize(
2833 &result, need) < 0) {
Walter Dörwald903f1e02003-02-04 16:28:00 +00002834 Py_DECREF(item);
Walter Dörwald531e0002003-02-04 16:57:49 +00002835 goto Fail_1;
Walter Dörwald903f1e02003-02-04 16:28:00 +00002836 }
2837 outlen = need;
2838 }
Jeremy Hylton1aad9c72003-09-16 03:10:59 +00002839 memcpy(PyUnicode_AS_UNICODE(result) + j,
2840 PyUnicode_AS_UNICODE(item),
2841 reslen*sizeof(Py_UNICODE));
Walter Dörwald903f1e02003-02-04 16:28:00 +00002842 j += reslen;
2843 }
2844 }
Martin v. Löwis8afd7572003-01-25 22:46:11 +00002845 Py_DECREF(item);
2846 }
2847
Walter Dörwald903f1e02003-02-04 16:28:00 +00002848 if (j < outlen)
Martin v. Löwis8afd7572003-01-25 22:46:11 +00002849 PyUnicode_Resize(&result, j);
2850
2851 return result;
2852
2853Fail_1:
2854 Py_DECREF(result);
2855 return NULL;
2856}
2857#endif