blob: 1c36fe58d814488bad889f14ba16463020418a48 [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,
Georg Brandld5b635f2008-03-25 08:29:14 +0000169 "apply() not supported in 3.x; "
170 "use func(*args, **kwargs)") < 0)
Neal Norwitz8b2bfbc2007-05-23 06:35:32 +0000171 return NULL;
172
Raymond Hettingerea3fdf42002-12-29 16:33:45 +0000173 if (!PyArg_UnpackTuple(args, "apply", 1, 3, &func, &alist, &kwdict))
Guido van Rossumc02e15c1991-12-16 13:03:00 +0000174 return NULL;
Barry Warsaw968f8cb1998-10-01 15:33:12 +0000175 if (alist != NULL) {
176 if (!PyTuple_Check(alist)) {
177 if (!PySequence_Check(alist)) {
Jeremy Hyltonc862cf42001-01-19 03:25:05 +0000178 PyErr_Format(PyExc_TypeError,
Alex Martellia9b9c9f2003-04-23 13:34:35 +0000179 "apply() arg 2 expected sequence, found %s",
Jeremy Hyltonc862cf42001-01-19 03:25:05 +0000180 alist->ob_type->tp_name);
Barry Warsaw968f8cb1998-10-01 15:33:12 +0000181 return NULL;
182 }
183 t = PySequence_Tuple(alist);
184 if (t == NULL)
185 return NULL;
186 alist = t;
187 }
Guido van Rossum2d951851994-08-29 12:52:16 +0000188 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000189 if (kwdict != NULL && !PyDict_Check(kwdict)) {
Jeremy Hyltonc862cf42001-01-19 03:25:05 +0000190 PyErr_Format(PyExc_TypeError,
191 "apply() arg 3 expected dictionary, found %s",
192 kwdict->ob_type->tp_name);
Barry Warsaw968f8cb1998-10-01 15:33:12 +0000193 goto finally;
Guido van Rossum681d79a1995-07-18 14:51:37 +0000194 }
Barry Warsaw968f8cb1998-10-01 15:33:12 +0000195 retval = PyEval_CallObjectWithKeywords(func, alist, kwdict);
196 finally:
197 Py_XDECREF(t);
198 return retval;
Guido van Rossumc02e15c1991-12-16 13:03:00 +0000199}
200
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000201PyDoc_STRVAR(apply_doc,
Fred Drakef1fbc622001-01-12 17:05:05 +0000202"apply(object[, args[, kwargs]]) -> value\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000203\n\
Fred Drake7b912121999-12-23 14:16:55 +0000204Call a callable object with positional arguments taken from the tuple args,\n\
205and keyword arguments taken from the optional dictionary kwargs.\n\
Raymond Hettingerff41c482003-04-06 09:01:11 +0000206Note that classes are callable, as are instances with a __call__() method.\n\
207\n\
208Deprecated since release 2.3. Instead, use the extended call syntax:\n\
209 function(*args, **keywords).");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000210
211
Guido van Rossum79f25d91997-04-29 20:08:16 +0000212static PyObject *
Eric Smith3cd81942008-02-22 16:30:22 +0000213builtin_bin(PyObject *self, PyObject *v)
214{
215 return PyNumber_ToBase(v, 2);
216}
217
218PyDoc_STRVAR(bin_doc,
219"bin(number) -> string\n\
220\n\
221Return the binary representation of an integer or long integer.");
222
223
224static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000225builtin_callable(PyObject *self, PyObject *v)
Guido van Rossum2d951851994-08-29 12:52:16 +0000226{
Neal Norwitzdf25efe2007-05-23 06:58:36 +0000227 if (Py_Py3kWarningFlag &&
228 PyErr_Warn(PyExc_DeprecationWarning,
Georg Brandld5b635f2008-03-25 08:29:14 +0000229 "callable() not supported in 3.x; "
230 "use hasattr(o, '__call__')") < 0)
Neal Norwitzdf25efe2007-05-23 06:58:36 +0000231 return NULL;
Guido van Rossum77f6a652002-04-03 22:41:51 +0000232 return PyBool_FromLong((long)PyCallable_Check(v));
Guido van Rossum2d951851994-08-29 12:52:16 +0000233}
234
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000235PyDoc_STRVAR(callable_doc,
Guido van Rossum77f6a652002-04-03 22:41:51 +0000236"callable(object) -> bool\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000237\n\
238Return whether the object is callable (i.e., some kind of function).\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000239Note that classes are callable, as are instances with a __call__() method.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000240
241
Guido van Rossum79f25d91997-04-29 20:08:16 +0000242static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000243builtin_filter(PyObject *self, PyObject *args)
Guido van Rossum12d12c51993-10-26 17:58:25 +0000244{
Guido van Rossumc7903a12002-08-16 07:04:56 +0000245 PyObject *func, *seq, *result, *it, *arg;
Martin v. Löwisd96ee902006-02-16 14:37:16 +0000246 Py_ssize_t len; /* guess for result list size */
247 register Py_ssize_t j;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000248
Raymond Hettingerea3fdf42002-12-29 16:33:45 +0000249 if (!PyArg_UnpackTuple(args, "filter", 2, 2, &func, &seq))
Guido van Rossum12d12c51993-10-26 17:58:25 +0000250 return NULL;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000251
Tim Peters0e57abf2001-05-02 07:39:38 +0000252 /* Strings and tuples return a result of the same type. */
253 if (PyString_Check(seq))
254 return filterstring(func, seq);
Martin v. Löwis8afd7572003-01-25 22:46:11 +0000255#ifdef Py_USING_UNICODE
256 if (PyUnicode_Check(seq))
257 return filterunicode(func, seq);
258#endif
Tim Peters0e57abf2001-05-02 07:39:38 +0000259 if (PyTuple_Check(seq))
260 return filtertuple(func, seq);
261
Georg Brandle35b6572005-07-19 22:20:20 +0000262 /* Pre-allocate argument list tuple. */
263 arg = PyTuple_New(1);
264 if (arg == NULL)
265 return NULL;
266
Tim Peters0e57abf2001-05-02 07:39:38 +0000267 /* Get iterator. */
268 it = PyObject_GetIter(seq);
269 if (it == NULL)
Georg Brandle35b6572005-07-19 22:20:20 +0000270 goto Fail_arg;
Tim Peters0e57abf2001-05-02 07:39:38 +0000271
272 /* Guess a result list size. */
Raymond Hettinger4e2f7142007-12-06 00:56:53 +0000273 len = _PyObject_LengthHint(seq, 8);
Guido van Rossum12d12c51993-10-26 17:58:25 +0000274
Tim Peters0e57abf2001-05-02 07:39:38 +0000275 /* Get a result list. */
Guido van Rossum79f25d91997-04-29 20:08:16 +0000276 if (PyList_Check(seq) && seq->ob_refcnt == 1) {
Tim Peters0e57abf2001-05-02 07:39:38 +0000277 /* Eww - can modify the list in-place. */
Guido van Rossum79f25d91997-04-29 20:08:16 +0000278 Py_INCREF(seq);
Guido van Rossum12d12c51993-10-26 17:58:25 +0000279 result = seq;
280 }
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000281 else {
Tim Peters0e57abf2001-05-02 07:39:38 +0000282 result = PyList_New(len);
283 if (result == NULL)
284 goto Fail_it;
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000285 }
Guido van Rossum12d12c51993-10-26 17:58:25 +0000286
Tim Peters0e57abf2001-05-02 07:39:38 +0000287 /* Build the result list. */
Tim Petersf4848da2001-05-05 00:14:56 +0000288 j = 0;
289 for (;;) {
Guido van Rossumc7903a12002-08-16 07:04:56 +0000290 PyObject *item;
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000291 int ok;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000292
Tim Peters0e57abf2001-05-02 07:39:38 +0000293 item = PyIter_Next(it);
294 if (item == NULL) {
Tim Petersf4848da2001-05-05 00:14:56 +0000295 if (PyErr_Occurred())
296 goto Fail_result_it;
Tim Peters0e57abf2001-05-02 07:39:38 +0000297 break;
Guido van Rossum2d951851994-08-29 12:52:16 +0000298 }
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000299
Neil Schemenauer68973552003-08-14 20:37:34 +0000300 if (func == (PyObject *)&PyBool_Type || func == Py_None) {
Guido van Rossumc7903a12002-08-16 07:04:56 +0000301 ok = PyObject_IsTrue(item);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000302 }
303 else {
Guido van Rossumc7903a12002-08-16 07:04:56 +0000304 PyObject *good;
305 PyTuple_SET_ITEM(arg, 0, item);
306 good = PyObject_Call(func, arg, NULL);
307 PyTuple_SET_ITEM(arg, 0, NULL);
Guido van Rossum58b68731995-01-10 17:40:55 +0000308 if (good == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000309 Py_DECREF(item);
Tim Peters0e57abf2001-05-02 07:39:38 +0000310 goto Fail_result_it;
Guido van Rossum58b68731995-01-10 17:40:55 +0000311 }
Guido van Rossumc7903a12002-08-16 07:04:56 +0000312 ok = PyObject_IsTrue(good);
313 Py_DECREF(good);
Guido van Rossum12d12c51993-10-26 17:58:25 +0000314 }
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000315 if (ok) {
Tim Peters0e57abf2001-05-02 07:39:38 +0000316 if (j < len)
317 PyList_SET_ITEM(result, j, item);
Guido van Rossum2d951851994-08-29 12:52:16 +0000318 else {
Barry Warsawfa77e091999-01-28 18:49:12 +0000319 int status = PyList_Append(result, item);
Barry Warsawfa77e091999-01-28 18:49:12 +0000320 Py_DECREF(item);
321 if (status < 0)
Tim Peters0e57abf2001-05-02 07:39:38 +0000322 goto Fail_result_it;
Guido van Rossum2d951851994-08-29 12:52:16 +0000323 }
Tim Peters0e57abf2001-05-02 07:39:38 +0000324 ++j;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000325 }
Tim Peters0e57abf2001-05-02 07:39:38 +0000326 else
327 Py_DECREF(item);
Guido van Rossum12d12c51993-10-26 17:58:25 +0000328 }
329
Guido van Rossum12d12c51993-10-26 17:58:25 +0000330
Tim Peters0e57abf2001-05-02 07:39:38 +0000331 /* Cut back result list if len is too big. */
Guido van Rossum79f25d91997-04-29 20:08:16 +0000332 if (j < len && PyList_SetSlice(result, j, len, NULL) < 0)
Tim Peters0e57abf2001-05-02 07:39:38 +0000333 goto Fail_result_it;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000334
Tim Peters3c6b1482001-05-21 08:07:05 +0000335 Py_DECREF(it);
Guido van Rossumc7903a12002-08-16 07:04:56 +0000336 Py_DECREF(arg);
Guido van Rossum12d12c51993-10-26 17:58:25 +0000337 return result;
338
Tim Peters0e57abf2001-05-02 07:39:38 +0000339Fail_result_it:
Guido van Rossum79f25d91997-04-29 20:08:16 +0000340 Py_DECREF(result);
Tim Peters0e57abf2001-05-02 07:39:38 +0000341Fail_it:
342 Py_DECREF(it);
Guido van Rossumc7903a12002-08-16 07:04:56 +0000343Fail_arg:
344 Py_DECREF(arg);
Guido van Rossum12d12c51993-10-26 17:58:25 +0000345 return NULL;
346}
347
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000348PyDoc_STRVAR(filter_doc,
Tim Petersd50e5442002-03-09 00:06:26 +0000349"filter(function or None, sequence) -> list, tuple, or string\n"
350"\n"
351"Return those items of sequence for which function(item) is true. If\n"
352"function is None, return the items that are true. If sequence is a tuple\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000353"or string, return the same type, else return a list.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000354
Guido van Rossum79f25d91997-04-29 20:08:16 +0000355static PyObject *
Eric Smitha9f7d622008-02-17 19:46:49 +0000356builtin_format(PyObject *self, PyObject *args)
357{
358 PyObject *value;
359 PyObject *format_spec = NULL;
360
361 if (!PyArg_ParseTuple(args, "O|O:format", &value, &format_spec))
362 return NULL;
363
364 return PyObject_Format(value, format_spec);
365}
366
367PyDoc_STRVAR(format_doc,
368"format(value[, format_spec]) -> string\n\
369\n\
370Returns value.__format__(format_spec)\n\
371format_spec defaults to \"\"");
372
373static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000374builtin_chr(PyObject *self, PyObject *args)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000375{
376 long x;
377 char s[1];
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000378
Guido van Rossum79f25d91997-04-29 20:08:16 +0000379 if (!PyArg_ParseTuple(args, "l:chr", &x))
Guido van Rossum3f5da241990-12-20 15:06:42 +0000380 return NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000381 if (x < 0 || x >= 256) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000382 PyErr_SetString(PyExc_ValueError,
383 "chr() arg not in range(256)");
Guido van Rossum3f5da241990-12-20 15:06:42 +0000384 return NULL;
385 }
Guido van Rossum6bf62da1997-04-11 20:37:35 +0000386 s[0] = (char)x;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000387 return PyString_FromStringAndSize(s, 1);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000388}
389
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000390PyDoc_STRVAR(chr_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000391"chr(i) -> character\n\
392\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000393Return a string of one character with ordinal i; 0 <= i < 256.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000394
395
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000396#ifdef Py_USING_UNICODE
Guido van Rossum79f25d91997-04-29 20:08:16 +0000397static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000398builtin_unichr(PyObject *self, PyObject *args)
Guido van Rossum09095f32000-03-10 23:00:52 +0000399{
400 long x;
Guido van Rossum09095f32000-03-10 23:00:52 +0000401
402 if (!PyArg_ParseTuple(args, "l:unichr", &x))
403 return NULL;
Fredrik Lundh0dcf67e2001-06-26 20:01:56 +0000404
Marc-André Lemburgcc8764c2002-08-11 12:23:04 +0000405 return PyUnicode_FromOrdinal(x);
Guido van Rossum09095f32000-03-10 23:00:52 +0000406}
407
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000408PyDoc_STRVAR(unichr_doc,
Fred Drake078b24f2000-04-13 02:42:50 +0000409"unichr(i) -> Unicode character\n\
Guido van Rossum09095f32000-03-10 23:00:52 +0000410\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000411Return a Unicode string of one character with ordinal i; 0 <= i <= 0x10ffff.");
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000412#endif
Guido van Rossum09095f32000-03-10 23:00:52 +0000413
414
415static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000416builtin_cmp(PyObject *self, PyObject *args)
Guido van Rossuma9e7dc11992-10-18 18:53:57 +0000417{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000418 PyObject *a, *b;
Guido van Rossum09df08a1998-05-22 00:51:39 +0000419 int c;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000420
Raymond Hettingerea3fdf42002-12-29 16:33:45 +0000421 if (!PyArg_UnpackTuple(args, "cmp", 2, 2, &a, &b))
Guido van Rossuma9e7dc11992-10-18 18:53:57 +0000422 return NULL;
Guido van Rossum09df08a1998-05-22 00:51:39 +0000423 if (PyObject_Cmp(a, b, &c) < 0)
Guido van Rossumc8b6df91997-05-23 00:06:51 +0000424 return NULL;
Guido van Rossum09df08a1998-05-22 00:51:39 +0000425 return PyInt_FromLong((long)c);
Guido van Rossuma9e7dc11992-10-18 18:53:57 +0000426}
427
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000428PyDoc_STRVAR(cmp_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000429"cmp(x, y) -> integer\n\
430\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000431Return negative if x<y, zero if x==y, positive if x>y.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000432
433
Guido van Rossum79f25d91997-04-29 20:08:16 +0000434static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000435builtin_coerce(PyObject *self, PyObject *args)
Guido van Rossum6a00cd81995-01-07 12:39:01 +0000436{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000437 PyObject *v, *w;
438 PyObject *res;
Guido van Rossum5524a591995-01-10 15:26:20 +0000439
Neal Norwitzdf25efe2007-05-23 06:58:36 +0000440 if (Py_Py3kWarningFlag &&
441 PyErr_Warn(PyExc_DeprecationWarning,
442 "coerce() not supported in 3.x") < 0)
443 return NULL;
444
Raymond Hettingerea3fdf42002-12-29 16:33:45 +0000445 if (!PyArg_UnpackTuple(args, "coerce", 2, 2, &v, &w))
Guido van Rossum5524a591995-01-10 15:26:20 +0000446 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000447 if (PyNumber_Coerce(&v, &w) < 0)
Guido van Rossum04691fc1992-08-12 15:35:34 +0000448 return NULL;
Raymond Hettinger8ae46892003-10-12 19:09:37 +0000449 res = PyTuple_Pack(2, v, w);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000450 Py_DECREF(v);
451 Py_DECREF(w);
Guido van Rossum04691fc1992-08-12 15:35:34 +0000452 return res;
453}
454
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000455PyDoc_STRVAR(coerce_doc,
Martin v. Löwis8d494f32004-08-25 10:42:41 +0000456"coerce(x, y) -> (x1, y1)\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000457\n\
Martin v. Löwis8d494f32004-08-25 10:42:41 +0000458Return a tuple consisting of the two numeric arguments converted to\n\
459a common type, using the same rules as used by arithmetic operations.\n\
460If coercion is not possible, raise TypeError.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000461
Guido van Rossum79f25d91997-04-29 20:08:16 +0000462static PyObject *
Georg Brandl5240d742007-03-13 20:46:32 +0000463builtin_compile(PyObject *self, PyObject *args, PyObject *kwds)
Guido van Rossum5b722181993-03-30 17:46:03 +0000464{
465 char *str;
466 char *filename;
467 char *startstr;
468 int start;
Tim Peters6cd6a822001-08-17 22:11:27 +0000469 int dont_inherit = 0;
470 int supplied_flags = 0;
Tim Peters5ba58662001-07-16 02:29:45 +0000471 PyCompilerFlags cf;
Neal Norwitz3a9a3e72005-11-27 20:38:31 +0000472 PyObject *result = NULL, *cmd, *tmp = NULL;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000473 Py_ssize_t length;
Georg Brandl5240d742007-03-13 20:46:32 +0000474 static char *kwlist[] = {"source", "filename", "mode", "flags",
475 "dont_inherit", NULL};
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000476
Georg Brandl5240d742007-03-13 20:46:32 +0000477 if (!PyArg_ParseTupleAndKeywords(args, kwds, "Oss|ii:compile",
478 kwlist, &cmd, &filename, &startstr,
479 &supplied_flags, &dont_inherit))
Guido van Rossum5b722181993-03-30 17:46:03 +0000480 return NULL;
Tim Peters6cd6a822001-08-17 22:11:27 +0000481
Just van Rossum3aaf42c2003-02-10 08:21:10 +0000482 cf.cf_flags = supplied_flags;
483
484#ifdef Py_USING_UNICODE
485 if (PyUnicode_Check(cmd)) {
486 tmp = PyUnicode_AsUTF8String(cmd);
487 if (tmp == NULL)
488 return NULL;
489 cmd = tmp;
490 cf.cf_flags |= PyCF_SOURCE_IS_UTF8;
491 }
492#endif
Just van Rossumb9b8e9c2003-02-10 09:22:01 +0000493 if (PyObject_AsReadBuffer(cmd, (const void **)&str, &length))
494 return NULL;
Tim Peters2c646c92003-02-10 14:48:29 +0000495 if ((size_t)length != strlen(str)) {
Just van Rossum3aaf42c2003-02-10 08:21:10 +0000496 PyErr_SetString(PyExc_TypeError,
Alex Martellia9b9c9f2003-04-23 13:34:35 +0000497 "compile() expected string without null bytes");
Neal Norwitz3a9a3e72005-11-27 20:38:31 +0000498 goto cleanup;
Just van Rossum3aaf42c2003-02-10 08:21:10 +0000499 }
500
Guido van Rossum5b722181993-03-30 17:46:03 +0000501 if (strcmp(startstr, "exec") == 0)
Guido van Rossumb05a5c71997-05-07 17:46:13 +0000502 start = Py_file_input;
Guido van Rossum5b722181993-03-30 17:46:03 +0000503 else if (strcmp(startstr, "eval") == 0)
Guido van Rossumb05a5c71997-05-07 17:46:13 +0000504 start = Py_eval_input;
Guido van Rossum872537c1995-07-07 22:43:42 +0000505 else if (strcmp(startstr, "single") == 0)
Guido van Rossumb05a5c71997-05-07 17:46:13 +0000506 start = Py_single_input;
Guido van Rossum5b722181993-03-30 17:46:03 +0000507 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000508 PyErr_SetString(PyExc_ValueError,
Fred Drake661ea262000-10-24 19:57:45 +0000509 "compile() arg 3 must be 'exec' or 'eval' or 'single'");
Neal Norwitz3a9a3e72005-11-27 20:38:31 +0000510 goto cleanup;
Guido van Rossum5b722181993-03-30 17:46:03 +0000511 }
Tim Peters6cd6a822001-08-17 22:11:27 +0000512
Guido van Rossum4b499dd32003-02-13 22:07:59 +0000513 if (supplied_flags &
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000514 ~(PyCF_MASK | PyCF_MASK_OBSOLETE | PyCF_DONT_IMPLY_DEDENT | PyCF_ONLY_AST))
Guido van Rossum4b499dd32003-02-13 22:07:59 +0000515 {
Tim Peters6cd6a822001-08-17 22:11:27 +0000516 PyErr_SetString(PyExc_ValueError,
517 "compile(): unrecognised flags");
Neal Norwitz3a9a3e72005-11-27 20:38:31 +0000518 goto cleanup;
Tim Peters6cd6a822001-08-17 22:11:27 +0000519 }
520 /* XXX Warn if (supplied_flags & PyCF_MASK_OBSOLETE) != 0? */
521
Tim Peters6cd6a822001-08-17 22:11:27 +0000522 if (!dont_inherit) {
523 PyEval_MergeCompilerFlags(&cf);
524 }
Just van Rossum3aaf42c2003-02-10 08:21:10 +0000525 result = Py_CompileStringFlags(str, filename, start, &cf);
Neal Norwitz3a9a3e72005-11-27 20:38:31 +0000526cleanup:
Just van Rossum3aaf42c2003-02-10 08:21:10 +0000527 Py_XDECREF(tmp);
528 return result;
Guido van Rossum5b722181993-03-30 17:46:03 +0000529}
530
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000531PyDoc_STRVAR(compile_doc,
Tim Peters6cd6a822001-08-17 22:11:27 +0000532"compile(source, filename, mode[, flags[, dont_inherit]]) -> code object\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000533\n\
534Compile the source string (a Python module, statement or expression)\n\
535into a code object that can be executed by the exec statement or eval().\n\
536The filename will be used for run-time error messages.\n\
537The mode must be 'exec' to compile a module, 'single' to compile a\n\
Tim Peters6cd6a822001-08-17 22:11:27 +0000538single (interactive) statement, or 'eval' to compile an expression.\n\
539The flags argument, if present, controls which future statements influence\n\
540the compilation of the code.\n\
541The dont_inherit argument, if non-zero, stops the compilation inheriting\n\
542the effects of any future statements in effect in the code calling\n\
543compile; if absent or zero these statements do influence the compilation,\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000544in addition to any features explicitly specified.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000545
Guido van Rossum79f25d91997-04-29 20:08:16 +0000546static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000547builtin_dir(PyObject *self, PyObject *args)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000548{
Tim Peters5d2b77c2001-09-03 05:47:38 +0000549 PyObject *arg = NULL;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000550
Raymond Hettingerea3fdf42002-12-29 16:33:45 +0000551 if (!PyArg_UnpackTuple(args, "dir", 0, 1, &arg))
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000552 return NULL;
Tim Peters7eea37e2001-09-04 22:08:56 +0000553 return PyObject_Dir(arg);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000554}
555
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000556PyDoc_STRVAR(dir_doc,
Tim Peters5d2b77c2001-09-03 05:47:38 +0000557"dir([object]) -> list of strings\n"
558"\n"
Georg Brandl871f1bc2007-03-12 13:17:36 +0000559"If called without an argument, return the names in the current scope.\n"
560"Else, return an alphabetized list of names comprising (some of) the attributes\n"
561"of the given object, and of attributes reachable from it.\n"
562"If the object supplies a method named __dir__, it will be used; otherwise\n"
563"the default dir() logic is used and returns:\n"
564" for a module object: the module's attributes.\n"
565" for a class object: its attributes, and recursively the attributes\n"
566" of its bases.\n"
Georg Brandl3bb15672007-03-13 07:23:16 +0000567" for any other object: its attributes, its class's attributes, and\n"
Georg Brandl871f1bc2007-03-12 13:17:36 +0000568" recursively the attributes of its class's base classes.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000569
Guido van Rossum79f25d91997-04-29 20:08:16 +0000570static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000571builtin_divmod(PyObject *self, PyObject *args)
Guido van Rossum6a00cd81995-01-07 12:39:01 +0000572{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000573 PyObject *v, *w;
Guido van Rossum6a00cd81995-01-07 12:39:01 +0000574
Raymond Hettingerea3fdf42002-12-29 16:33:45 +0000575 if (!PyArg_UnpackTuple(args, "divmod", 2, 2, &v, &w))
Guido van Rossum6a00cd81995-01-07 12:39:01 +0000576 return NULL;
Guido van Rossum09df08a1998-05-22 00:51:39 +0000577 return PyNumber_Divmod(v, w);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000578}
579
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000580PyDoc_STRVAR(divmod_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000581"divmod(x, y) -> (div, mod)\n\
582\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000583Return the tuple ((x-x%y)/y, x%y). Invariant: div*y + mod == x.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000584
585
Guido van Rossum79f25d91997-04-29 20:08:16 +0000586static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000587builtin_eval(PyObject *self, PyObject *args)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000588{
Just van Rossum3aaf42c2003-02-10 08:21:10 +0000589 PyObject *cmd, *result, *tmp = NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000590 PyObject *globals = Py_None, *locals = Py_None;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000591 char *str;
Tim Peters9fa96be2001-08-17 23:04:59 +0000592 PyCompilerFlags cf;
Guido van Rossum590baa41993-11-30 13:40:46 +0000593
Raymond Hettinger214b1c32004-07-02 06:41:07 +0000594 if (!PyArg_UnpackTuple(args, "eval", 1, 3, &cmd, &globals, &locals))
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000595 return NULL;
Raymond Hettinger214b1c32004-07-02 06:41:07 +0000596 if (locals != Py_None && !PyMapping_Check(locals)) {
597 PyErr_SetString(PyExc_TypeError, "locals must be a mapping");
Raymond Hettinger513ffe82004-07-06 13:44:41 +0000598 return NULL;
Raymond Hettinger214b1c32004-07-02 06:41:07 +0000599 }
600 if (globals != Py_None && !PyDict_Check(globals)) {
Georg Brandl99d7e4e2005-08-31 22:21:15 +0000601 PyErr_SetString(PyExc_TypeError, PyMapping_Check(globals) ?
Raymond Hettinger214b1c32004-07-02 06:41:07 +0000602 "globals must be a real dict; try eval(expr, {}, mapping)"
603 : "globals must be a dict");
Raymond Hettinger513ffe82004-07-06 13:44:41 +0000604 return NULL;
Raymond Hettinger214b1c32004-07-02 06:41:07 +0000605 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000606 if (globals == Py_None) {
607 globals = PyEval_GetGlobals();
608 if (locals == Py_None)
609 locals = PyEval_GetLocals();
Guido van Rossum6135a871995-01-09 17:53:26 +0000610 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000611 else if (locals == Py_None)
Guido van Rossum6135a871995-01-09 17:53:26 +0000612 locals = globals;
Tim Peters9fa96be2001-08-17 23:04:59 +0000613
Georg Brandl77c85e62005-09-15 10:46:13 +0000614 if (globals == NULL || locals == NULL) {
615 PyErr_SetString(PyExc_TypeError,
616 "eval must be given globals and locals "
617 "when called without a frame");
618 return NULL;
619 }
620
Guido van Rossum79f25d91997-04-29 20:08:16 +0000621 if (PyDict_GetItemString(globals, "__builtins__") == NULL) {
622 if (PyDict_SetItemString(globals, "__builtins__",
623 PyEval_GetBuiltins()) != 0)
Guido van Rossum6135a871995-01-09 17:53:26 +0000624 return NULL;
625 }
Tim Peters9fa96be2001-08-17 23:04:59 +0000626
Jeremy Hylton15c1c4f2001-07-30 21:50:55 +0000627 if (PyCode_Check(cmd)) {
Jeremy Hylton733c8932001-12-13 19:51:56 +0000628 if (PyCode_GetNumFree((PyCodeObject *)cmd) > 0) {
Jeremy Hylton15c1c4f2001-07-30 21:50:55 +0000629 PyErr_SetString(PyExc_TypeError,
630 "code object passed to eval() may not contain free variables");
631 return NULL;
632 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000633 return PyEval_EvalCode((PyCodeObject *) cmd, globals, locals);
Jeremy Hylton15c1c4f2001-07-30 21:50:55 +0000634 }
Tim Peters9fa96be2001-08-17 23:04:59 +0000635
Marc-André Lemburgd1ba4432000-09-19 21:04:18 +0000636 if (!PyString_Check(cmd) &&
637 !PyUnicode_Check(cmd)) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000638 PyErr_SetString(PyExc_TypeError,
Fred Drake661ea262000-10-24 19:57:45 +0000639 "eval() arg 1 must be a string or code object");
Guido van Rossum94390a41992-08-14 15:14:30 +0000640 return NULL;
641 }
Just van Rossum3aaf42c2003-02-10 08:21:10 +0000642 cf.cf_flags = 0;
643
644#ifdef Py_USING_UNICODE
645 if (PyUnicode_Check(cmd)) {
646 tmp = PyUnicode_AsUTF8String(cmd);
647 if (tmp == NULL)
648 return NULL;
649 cmd = tmp;
650 cf.cf_flags |= PyCF_SOURCE_IS_UTF8;
651 }
652#endif
Neal Norwitz3a9a3e72005-11-27 20:38:31 +0000653 if (PyString_AsStringAndSize(cmd, &str, NULL)) {
654 Py_XDECREF(tmp);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000655 return NULL;
Neal Norwitz3a9a3e72005-11-27 20:38:31 +0000656 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000657 while (*str == ' ' || *str == '\t')
658 str++;
Tim Peters9fa96be2001-08-17 23:04:59 +0000659
Tim Peters9fa96be2001-08-17 23:04:59 +0000660 (void)PyEval_MergeCompilerFlags(&cf);
Just van Rossum3aaf42c2003-02-10 08:21:10 +0000661 result = PyRun_StringFlags(str, Py_eval_input, globals, locals, &cf);
662 Py_XDECREF(tmp);
663 return result;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000664}
665
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000666PyDoc_STRVAR(eval_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000667"eval(source[, globals[, locals]]) -> value\n\
668\n\
669Evaluate the source in the context of globals and locals.\n\
670The source may be a string representing a Python expression\n\
671or a code object as returned by compile().\n\
Neal Norwitz477ca1c2006-09-05 02:25:41 +0000672The globals must be a dictionary and locals can be any mapping,\n\
Raymond Hettinger214b1c32004-07-02 06:41:07 +0000673defaulting to the current globals and locals.\n\
674If only globals is given, locals defaults to it.\n");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000675
676
Guido van Rossum79f25d91997-04-29 20:08:16 +0000677static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000678builtin_execfile(PyObject *self, PyObject *args)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000679{
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000680 char *filename;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000681 PyObject *globals = Py_None, *locals = Py_None;
682 PyObject *res;
Guido van Rossumb3a639e2001-09-05 13:37:47 +0000683 FILE* fp = NULL;
Tim Peters5ba58662001-07-16 02:29:45 +0000684 PyCompilerFlags cf;
Martin v. Löwis6b3a2c42001-08-08 05:30:36 +0000685 int exists;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000686
Neal Norwitzdf25efe2007-05-23 06:58:36 +0000687 if (Py_Py3kWarningFlag &&
688 PyErr_Warn(PyExc_DeprecationWarning,
Georg Brandld5b635f2008-03-25 08:29:14 +0000689 "execfile() not supported in 3.x; use exec()") < 0)
Neal Norwitzdf25efe2007-05-23 06:58:36 +0000690 return NULL;
691
Raymond Hettinger66bd2332004-08-02 08:30:07 +0000692 if (!PyArg_ParseTuple(args, "s|O!O:execfile",
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000693 &filename,
Guido van Rossum79f25d91997-04-29 20:08:16 +0000694 &PyDict_Type, &globals,
Raymond Hettinger66bd2332004-08-02 08:30:07 +0000695 &locals))
Guido van Rossum0f61f8a1992-02-25 18:55:05 +0000696 return NULL;
Raymond Hettinger66bd2332004-08-02 08:30:07 +0000697 if (locals != Py_None && !PyMapping_Check(locals)) {
698 PyErr_SetString(PyExc_TypeError, "locals must be a mapping");
699 return NULL;
700 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000701 if (globals == Py_None) {
702 globals = PyEval_GetGlobals();
703 if (locals == Py_None)
704 locals = PyEval_GetLocals();
Guido van Rossum6135a871995-01-09 17:53:26 +0000705 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000706 else if (locals == Py_None)
Guido van Rossum6135a871995-01-09 17:53:26 +0000707 locals = globals;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000708 if (PyDict_GetItemString(globals, "__builtins__") == NULL) {
709 if (PyDict_SetItemString(globals, "__builtins__",
710 PyEval_GetBuiltins()) != 0)
Guido van Rossum6135a871995-01-09 17:53:26 +0000711 return NULL;
712 }
Martin v. Löwis6b3a2c42001-08-08 05:30:36 +0000713
714 exists = 0;
715 /* Test for existence or directory. */
Martin v. Löwis3484a182002-03-09 12:07:51 +0000716#if defined(PLAN9)
717 {
718 Dir *d;
719
720 if ((d = dirstat(filename))!=nil) {
721 if(d->mode & DMDIR)
722 werrstr("is a directory");
723 else
724 exists = 1;
725 free(d);
726 }
Martin v. Löwis6b3a2c42001-08-08 05:30:36 +0000727 }
Martin v. Löwis3484a182002-03-09 12:07:51 +0000728#elif defined(RISCOS)
Guido van Rossume2ae77b2001-10-24 20:42:55 +0000729 if (object_exists(filename)) {
730 if (isdir(filename))
731 errno = EISDIR;
732 else
733 exists = 1;
734 }
Martin v. Löwis3484a182002-03-09 12:07:51 +0000735#else /* standard Posix */
736 {
737 struct stat s;
738 if (stat(filename, &s) == 0) {
739 if (S_ISDIR(s.st_mode))
Andrew MacIntyreda4d6cb2004-03-29 11:53:38 +0000740# if defined(PYOS_OS2) && defined(PYCC_VACPP)
Martin v. Löwis3484a182002-03-09 12:07:51 +0000741 errno = EOS2ERR;
742# else
743 errno = EISDIR;
744# endif
745 else
746 exists = 1;
747 }
748 }
749#endif
Martin v. Löwis6b3a2c42001-08-08 05:30:36 +0000750
751 if (exists) {
752 Py_BEGIN_ALLOW_THREADS
Jack Jansen7b8c7542002-04-14 20:12:41 +0000753 fp = fopen(filename, "r" PY_STDIOTEXTMODE);
Martin v. Löwis6b3a2c42001-08-08 05:30:36 +0000754 Py_END_ALLOW_THREADS
755
756 if (fp == NULL) {
757 exists = 0;
758 }
759 }
760
761 if (!exists) {
Peter Schneider-Kamp4c013422002-08-27 16:58:00 +0000762 PyErr_SetFromErrnoWithFilename(PyExc_IOError, filename);
Guido van Rossum0f61f8a1992-02-25 18:55:05 +0000763 return NULL;
764 }
Tim Peters5ba58662001-07-16 02:29:45 +0000765 cf.cf_flags = 0;
766 if (PyEval_MergeCompilerFlags(&cf))
Tim Peters748b8bb2001-04-28 08:20:22 +0000767 res = PyRun_FileExFlags(fp, filename, Py_file_input, globals,
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000768 locals, 1, &cf);
Tim Peters5ba58662001-07-16 02:29:45 +0000769 else
Tim Peters748b8bb2001-04-28 08:20:22 +0000770 res = PyRun_FileEx(fp, filename, Py_file_input, globals,
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000771 locals, 1);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000772 return res;
Guido van Rossum0f61f8a1992-02-25 18:55:05 +0000773}
774
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000775PyDoc_STRVAR(execfile_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000776"execfile(filename[, globals[, locals]])\n\
777\n\
778Read and execute a Python script from a file.\n\
779The globals and locals are dictionaries, defaulting to the current\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000780globals and locals. If only globals is given, locals defaults to it.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000781
782
Guido van Rossum79f25d91997-04-29 20:08:16 +0000783static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000784builtin_getattr(PyObject *self, PyObject *args)
Guido van Rossum33894be1992-01-27 16:53:09 +0000785{
Guido van Rossum950ff291998-06-29 13:38:57 +0000786 PyObject *v, *result, *dflt = NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000787 PyObject *name;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000788
Raymond Hettingerea3fdf42002-12-29 16:33:45 +0000789 if (!PyArg_UnpackTuple(args, "getattr", 2, 3, &v, &name, &dflt))
Guido van Rossum33894be1992-01-27 16:53:09 +0000790 return NULL;
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000791#ifdef Py_USING_UNICODE
Jeremy Hylton0eb11152001-07-30 22:39:31 +0000792 if (PyUnicode_Check(name)) {
793 name = _PyUnicode_AsDefaultEncodedString(name, NULL);
794 if (name == NULL)
795 return NULL;
796 }
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000797#endif
Jeremy Hylton0eb11152001-07-30 22:39:31 +0000798
799 if (!PyString_Check(name)) {
800 PyErr_SetString(PyExc_TypeError,
Alex Martellia9b9c9f2003-04-23 13:34:35 +0000801 "getattr(): attribute name must be string");
Jeremy Hylton0eb11152001-07-30 22:39:31 +0000802 return NULL;
803 }
Guido van Rossum950ff291998-06-29 13:38:57 +0000804 result = PyObject_GetAttr(v, name);
Guido van Rossumd8923572001-10-16 21:31:32 +0000805 if (result == NULL && dflt != NULL &&
806 PyErr_ExceptionMatches(PyExc_AttributeError))
807 {
Guido van Rossum950ff291998-06-29 13:38:57 +0000808 PyErr_Clear();
809 Py_INCREF(dflt);
810 result = dflt;
811 }
812 return result;
Guido van Rossum9bfef441993-03-29 10:43:31 +0000813}
814
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000815PyDoc_STRVAR(getattr_doc,
Guido van Rossum950ff291998-06-29 13:38:57 +0000816"getattr(object, name[, default]) -> value\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000817\n\
Guido van Rossum950ff291998-06-29 13:38:57 +0000818Get a named attribute from an object; getattr(x, 'y') is equivalent to x.y.\n\
819When a default argument is given, it is returned when the attribute doesn't\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000820exist; without it, an exception is raised in that case.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000821
822
Guido van Rossum79f25d91997-04-29 20:08:16 +0000823static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000824builtin_globals(PyObject *self)
Guido van Rossum872537c1995-07-07 22:43:42 +0000825{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000826 PyObject *d;
Guido van Rossum872537c1995-07-07 22:43:42 +0000827
Guido van Rossum79f25d91997-04-29 20:08:16 +0000828 d = PyEval_GetGlobals();
Neal Norwitz43bd4db2006-08-12 01:46:42 +0000829 Py_XINCREF(d);
Guido van Rossum872537c1995-07-07 22:43:42 +0000830 return d;
831}
832
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000833PyDoc_STRVAR(globals_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000834"globals() -> dictionary\n\
835\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000836Return the dictionary containing the current scope's global variables.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000837
838
Guido van Rossum79f25d91997-04-29 20:08:16 +0000839static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000840builtin_hasattr(PyObject *self, PyObject *args)
Guido van Rossum9bfef441993-03-29 10:43:31 +0000841{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000842 PyObject *v;
843 PyObject *name;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000844
Raymond Hettingerea3fdf42002-12-29 16:33:45 +0000845 if (!PyArg_UnpackTuple(args, "hasattr", 2, 2, &v, &name))
Guido van Rossum9bfef441993-03-29 10:43:31 +0000846 return NULL;
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000847#ifdef Py_USING_UNICODE
Jeremy Hylton302b54a2001-07-30 22:45:19 +0000848 if (PyUnicode_Check(name)) {
849 name = _PyUnicode_AsDefaultEncodedString(name, NULL);
850 if (name == NULL)
851 return NULL;
852 }
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000853#endif
Jeremy Hylton302b54a2001-07-30 22:45:19 +0000854
855 if (!PyString_Check(name)) {
856 PyErr_SetString(PyExc_TypeError,
Alex Martellia9b9c9f2003-04-23 13:34:35 +0000857 "hasattr(): attribute name must be string");
Jeremy Hylton302b54a2001-07-30 22:45:19 +0000858 return NULL;
859 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000860 v = PyObject_GetAttr(v, name);
Guido van Rossum9bfef441993-03-29 10:43:31 +0000861 if (v == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000862 PyErr_Clear();
Guido van Rossum09df08a1998-05-22 00:51:39 +0000863 Py_INCREF(Py_False);
864 return Py_False;
Guido van Rossum9bfef441993-03-29 10:43:31 +0000865 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000866 Py_DECREF(v);
Guido van Rossum09df08a1998-05-22 00:51:39 +0000867 Py_INCREF(Py_True);
868 return Py_True;
Guido van Rossum33894be1992-01-27 16:53:09 +0000869}
870
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000871PyDoc_STRVAR(hasattr_doc,
Guido van Rossum77f6a652002-04-03 22:41:51 +0000872"hasattr(object, name) -> bool\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000873\n\
874Return whether the object has an attribute with the given name.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000875(This is done by calling getattr(object, name) and catching exceptions.)");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000876
877
Guido van Rossum79f25d91997-04-29 20:08:16 +0000878static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000879builtin_id(PyObject *self, PyObject *v)
Guido van Rossum5b722181993-03-30 17:46:03 +0000880{
Guido van Rossum106f2da2000-06-28 21:12:25 +0000881 return PyLong_FromVoidPtr(v);
Guido van Rossum5b722181993-03-30 17:46:03 +0000882}
883
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000884PyDoc_STRVAR(id_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000885"id(object) -> integer\n\
886\n\
887Return the identity of an object. This is guaranteed to be unique among\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000888simultaneously existing objects. (Hint: it's the object's memory address.)");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000889
890
Guido van Rossum79f25d91997-04-29 20:08:16 +0000891static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000892builtin_map(PyObject *self, PyObject *args)
Guido van Rossum12d12c51993-10-26 17:58:25 +0000893{
894 typedef struct {
Tim Peters4e9afdc2001-05-03 23:54:49 +0000895 PyObject *it; /* the iterator object */
896 int saw_StopIteration; /* bool: did the iterator end? */
Guido van Rossum12d12c51993-10-26 17:58:25 +0000897 } sequence;
898
Guido van Rossum79f25d91997-04-29 20:08:16 +0000899 PyObject *func, *result;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000900 sequence *seqs = NULL, *sqp;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000901 Py_ssize_t n, len;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000902 register int i, j;
903
Guido van Rossum79f25d91997-04-29 20:08:16 +0000904 n = PyTuple_Size(args);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000905 if (n < 2) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000906 PyErr_SetString(PyExc_TypeError,
907 "map() requires at least two args");
Guido van Rossum12d12c51993-10-26 17:58:25 +0000908 return NULL;
909 }
910
Guido van Rossum79f25d91997-04-29 20:08:16 +0000911 func = PyTuple_GetItem(args, 0);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000912 n--;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000913
Neal Norwitz53152a12008-02-24 02:20:25 +0000914 if (func == Py_None) {
915 if (Py_Py3kWarningFlag &&
916 PyErr_Warn(PyExc_DeprecationWarning,
Georg Brandld5b635f2008-03-25 08:29:14 +0000917 "map(None, ...) not supported in 3.x; "
918 "use list(...)") < 0)
Neal Norwitz53152a12008-02-24 02:20:25 +0000919 return NULL;
920 if (n == 1) {
921 /* map(None, S) is the same as list(S). */
922 return PySequence_List(PyTuple_GetItem(args, 1));
923 }
Guido van Rossumfa4ac711998-07-10 17:37:30 +0000924 }
925
Tim Peters4e9afdc2001-05-03 23:54:49 +0000926 /* Get space for sequence descriptors. Must NULL out the iterator
927 * pointers so that jumping to Fail_2 later doesn't see trash.
928 */
Guido van Rossum79f25d91997-04-29 20:08:16 +0000929 if ((seqs = PyMem_NEW(sequence, n)) == NULL) {
930 PyErr_NoMemory();
Tim Peters4e9afdc2001-05-03 23:54:49 +0000931 return NULL;
932 }
933 for (i = 0; i < n; ++i) {
934 seqs[i].it = (PyObject*)NULL;
935 seqs[i].saw_StopIteration = 0;
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000936 }
Guido van Rossum12d12c51993-10-26 17:58:25 +0000937
Tim Peters4e9afdc2001-05-03 23:54:49 +0000938 /* Do a first pass to obtain iterators for the arguments, and set len
939 * to the largest of their lengths.
Tim Peters748b8bb2001-04-28 08:20:22 +0000940 */
Tim Peters4e9afdc2001-05-03 23:54:49 +0000941 len = 0;
942 for (i = 0, sqp = seqs; i < n; ++i, ++sqp) {
943 PyObject *curseq;
Martin v. Löwisd96ee902006-02-16 14:37:16 +0000944 Py_ssize_t curlen;
Guido van Rossumad991772001-01-12 16:03:05 +0000945
Tim Peters4e9afdc2001-05-03 23:54:49 +0000946 /* Get iterator. */
947 curseq = PyTuple_GetItem(args, i+1);
948 sqp->it = PyObject_GetIter(curseq);
949 if (sqp->it == NULL) {
Guido van Rossum12d12c51993-10-26 17:58:25 +0000950 static char errmsg[] =
Tim Peters4e9afdc2001-05-03 23:54:49 +0000951 "argument %d to map() must support iteration";
Guido van Rossum15e33a41997-04-30 19:00:27 +0000952 char errbuf[sizeof(errmsg) + 25];
Jeremy Hylton518ab1c2001-11-28 20:42:20 +0000953 PyOS_snprintf(errbuf, sizeof(errbuf), errmsg, i+2);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000954 PyErr_SetString(PyExc_TypeError, errbuf);
Guido van Rossum12d12c51993-10-26 17:58:25 +0000955 goto Fail_2;
956 }
957
Tim Peters4e9afdc2001-05-03 23:54:49 +0000958 /* Update len. */
Raymond Hettinger4e2f7142007-12-06 00:56:53 +0000959 curlen = _PyObject_LengthHint(curseq, 8);
Raymond Hettinger77f3c872004-01-04 08:54:44 +0000960 if (curlen > len)
961 len = curlen;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000962 }
963
Tim Peters4e9afdc2001-05-03 23:54:49 +0000964 /* Get space for the result list. */
Guido van Rossum79f25d91997-04-29 20:08:16 +0000965 if ((result = (PyObject *) PyList_New(len)) == NULL)
Guido van Rossum12d12c51993-10-26 17:58:25 +0000966 goto Fail_2;
967
Tim Peters4e9afdc2001-05-03 23:54:49 +0000968 /* Iterate over the sequences until all have stopped. */
Guido van Rossum2d951851994-08-29 12:52:16 +0000969 for (i = 0; ; ++i) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000970 PyObject *alist, *item=NULL, *value;
Tim Peters4e9afdc2001-05-03 23:54:49 +0000971 int numactive = 0;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000972
Guido van Rossum79f25d91997-04-29 20:08:16 +0000973 if (func == Py_None && n == 1)
Guido van Rossum32120311995-07-10 13:52:21 +0000974 alist = NULL;
Tim Peters4e9afdc2001-05-03 23:54:49 +0000975 else if ((alist = PyTuple_New(n)) == NULL)
976 goto Fail_1;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000977
978 for (j = 0, sqp = seqs; j < n; ++j, ++sqp) {
Tim Peters4e9afdc2001-05-03 23:54:49 +0000979 if (sqp->saw_StopIteration) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000980 Py_INCREF(Py_None);
981 item = Py_None;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000982 }
Guido van Rossum12d12c51993-10-26 17:58:25 +0000983 else {
Tim Peters4e9afdc2001-05-03 23:54:49 +0000984 item = PyIter_Next(sqp->it);
985 if (item)
986 ++numactive;
987 else {
Tim Peters4e9afdc2001-05-03 23:54:49 +0000988 if (PyErr_Occurred()) {
Tim Petersf4848da2001-05-05 00:14:56 +0000989 Py_XDECREF(alist);
990 goto Fail_1;
Guido van Rossum2d951851994-08-29 12:52:16 +0000991 }
Tim Peters4e9afdc2001-05-03 23:54:49 +0000992 Py_INCREF(Py_None);
993 item = Py_None;
994 sqp->saw_StopIteration = 1;
Guido van Rossum2d951851994-08-29 12:52:16 +0000995 }
Guido van Rossum12d12c51993-10-26 17:58:25 +0000996 }
Tim Peters4e9afdc2001-05-03 23:54:49 +0000997 if (alist)
998 PyTuple_SET_ITEM(alist, j, item);
999 else
Guido van Rossum2d951851994-08-29 12:52:16 +00001000 break;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001001 }
1002
Guido van Rossum32120311995-07-10 13:52:21 +00001003 if (!alist)
1004 alist = item;
Guido van Rossum2d951851994-08-29 12:52:16 +00001005
Tim Peters4e9afdc2001-05-03 23:54:49 +00001006 if (numactive == 0) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001007 Py_DECREF(alist);
Guido van Rossum2d951851994-08-29 12:52:16 +00001008 break;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001009 }
Guido van Rossum2d951851994-08-29 12:52:16 +00001010
Guido van Rossum79f25d91997-04-29 20:08:16 +00001011 if (func == Py_None)
Guido van Rossum32120311995-07-10 13:52:21 +00001012 value = alist;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001013 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001014 value = PyEval_CallObject(func, alist);
1015 Py_DECREF(alist);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00001016 if (value == NULL)
1017 goto Fail_1;
Guido van Rossum2d951851994-08-29 12:52:16 +00001018 }
1019 if (i >= len) {
Barry Warsawfa77e091999-01-28 18:49:12 +00001020 int status = PyList_Append(result, value);
Barry Warsaw21332871999-01-28 04:21:35 +00001021 Py_DECREF(value);
Barry Warsawfa77e091999-01-28 18:49:12 +00001022 if (status < 0)
1023 goto Fail_1;
Guido van Rossum2d951851994-08-29 12:52:16 +00001024 }
Tim Peters4e9afdc2001-05-03 23:54:49 +00001025 else if (PyList_SetItem(result, i, value) < 0)
1026 goto Fail_1;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001027 }
1028
Guido van Rossumfa4ac711998-07-10 17:37:30 +00001029 if (i < len && PyList_SetSlice(result, i, len, NULL) < 0)
1030 goto Fail_1;
1031
Tim Peters4e9afdc2001-05-03 23:54:49 +00001032 goto Succeed;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001033
Guido van Rossum12d12c51993-10-26 17:58:25 +00001034Fail_1:
Guido van Rossum79f25d91997-04-29 20:08:16 +00001035 Py_DECREF(result);
Guido van Rossum12d12c51993-10-26 17:58:25 +00001036Fail_2:
Tim Peters4e9afdc2001-05-03 23:54:49 +00001037 result = NULL;
1038Succeed:
1039 assert(seqs);
1040 for (i = 0; i < n; ++i)
1041 Py_XDECREF(seqs[i].it);
1042 PyMem_DEL(seqs);
1043 return result;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001044}
1045
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001046PyDoc_STRVAR(map_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001047"map(function, sequence[, sequence, ...]) -> list\n\
1048\n\
1049Return a list of the results of applying the function to the items of\n\
1050the argument sequence(s). If more than one sequence is given, the\n\
1051function is called with an argument list consisting of the corresponding\n\
1052item of each sequence, substituting None for missing values when not all\n\
1053sequences have the same length. If the function is None, return a list of\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001054the items of the sequence (or a list of tuples if more than one sequence).");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001055
1056
Guido van Rossum79f25d91997-04-29 20:08:16 +00001057static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001058builtin_setattr(PyObject *self, PyObject *args)
Guido van Rossum33894be1992-01-27 16:53:09 +00001059{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001060 PyObject *v;
1061 PyObject *name;
1062 PyObject *value;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001063
Raymond Hettingerea3fdf42002-12-29 16:33:45 +00001064 if (!PyArg_UnpackTuple(args, "setattr", 3, 3, &v, &name, &value))
Guido van Rossum33894be1992-01-27 16:53:09 +00001065 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001066 if (PyObject_SetAttr(v, name, value) != 0)
Guido van Rossum33894be1992-01-27 16:53:09 +00001067 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001068 Py_INCREF(Py_None);
1069 return Py_None;
Guido van Rossum33894be1992-01-27 16:53:09 +00001070}
1071
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001072PyDoc_STRVAR(setattr_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001073"setattr(object, name, value)\n\
1074\n\
1075Set a named attribute on an object; setattr(x, 'y', v) is equivalent to\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001076``x.y = v''.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001077
1078
Guido van Rossum79f25d91997-04-29 20:08:16 +00001079static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001080builtin_delattr(PyObject *self, PyObject *args)
Guido van Rossum14144fc1994-08-29 12:53:40 +00001081{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001082 PyObject *v;
1083 PyObject *name;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001084
Raymond Hettingerea3fdf42002-12-29 16:33:45 +00001085 if (!PyArg_UnpackTuple(args, "delattr", 2, 2, &v, &name))
Guido van Rossum14144fc1994-08-29 12:53:40 +00001086 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001087 if (PyObject_SetAttr(v, name, (PyObject *)NULL) != 0)
Guido van Rossum14144fc1994-08-29 12:53:40 +00001088 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001089 Py_INCREF(Py_None);
1090 return Py_None;
Guido van Rossum14144fc1994-08-29 12:53:40 +00001091}
1092
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001093PyDoc_STRVAR(delattr_doc,
Guido van Rossumdf12a591998-11-23 22:13:04 +00001094"delattr(object, name)\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001095\n\
1096Delete a named attribute on an object; delattr(x, 'y') is equivalent to\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001097``del x.y''.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001098
1099
Guido van Rossum79f25d91997-04-29 20:08:16 +00001100static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001101builtin_hash(PyObject *self, PyObject *v)
Guido van Rossum9bfef441993-03-29 10:43:31 +00001102{
Guido van Rossum9bfef441993-03-29 10:43:31 +00001103 long x;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001104
Guido van Rossum79f25d91997-04-29 20:08:16 +00001105 x = PyObject_Hash(v);
Guido van Rossum9bfef441993-03-29 10:43:31 +00001106 if (x == -1)
1107 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001108 return PyInt_FromLong(x);
Guido van Rossum9bfef441993-03-29 10:43:31 +00001109}
1110
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001111PyDoc_STRVAR(hash_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001112"hash(object) -> integer\n\
1113\n\
1114Return a hash value for the object. Two objects with the same value have\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001115the same hash value. The reverse is not necessarily true, but likely.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001116
1117
Guido van Rossum79f25d91997-04-29 20:08:16 +00001118static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001119builtin_hex(PyObject *self, PyObject *v)
Guido van Rossum006bcd41991-10-24 14:54:44 +00001120{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001121 PyNumberMethods *nb;
Neil Schemenauer3a313e32004-07-19 16:29:17 +00001122 PyObject *res;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001123
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001124 if ((nb = v->ob_type->tp_as_number) == NULL ||
Guido van Rossum1899c2e1992-09-12 11:09:23 +00001125 nb->nb_hex == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001126 PyErr_SetString(PyExc_TypeError,
Guido van Rossum1899c2e1992-09-12 11:09:23 +00001127 "hex() argument can't be converted to hex");
1128 return NULL;
Guido van Rossum006bcd41991-10-24 14:54:44 +00001129 }
Neil Schemenauer3a313e32004-07-19 16:29:17 +00001130 res = (*nb->nb_hex)(v);
1131 if (res && !PyString_Check(res)) {
1132 PyErr_Format(PyExc_TypeError,
1133 "__hex__ returned non-string (type %.200s)",
1134 res->ob_type->tp_name);
1135 Py_DECREF(res);
1136 return NULL;
1137 }
1138 return res;
Guido van Rossum006bcd41991-10-24 14:54:44 +00001139}
1140
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001141PyDoc_STRVAR(hex_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001142"hex(number) -> string\n\
1143\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001144Return the hexadecimal representation of an integer or long integer.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001145
1146
Tim Petersdbd9ba62000-07-09 03:09:57 +00001147static PyObject *builtin_raw_input(PyObject *, PyObject *);
Guido van Rossum3165fe61992-09-25 21:59:05 +00001148
Guido van Rossum79f25d91997-04-29 20:08:16 +00001149static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001150builtin_input(PyObject *self, PyObject *args)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001151{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001152 PyObject *line;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001153 char *str;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001154 PyObject *res;
1155 PyObject *globals, *locals;
Hye-Shik Changff83c2b2004-02-02 13:39:01 +00001156 PyCompilerFlags cf;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001157
1158 line = builtin_raw_input(self, args);
Guido van Rossum3165fe61992-09-25 21:59:05 +00001159 if (line == NULL)
1160 return line;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001161 if (!PyArg_Parse(line, "s;embedded '\\0' in input line", &str))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001162 return NULL;
1163 while (*str == ' ' || *str == '\t')
1164 str++;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001165 globals = PyEval_GetGlobals();
1166 locals = PyEval_GetLocals();
1167 if (PyDict_GetItemString(globals, "__builtins__") == NULL) {
1168 if (PyDict_SetItemString(globals, "__builtins__",
1169 PyEval_GetBuiltins()) != 0)
Guido van Rossum6135a871995-01-09 17:53:26 +00001170 return NULL;
1171 }
Hye-Shik Changff83c2b2004-02-02 13:39:01 +00001172 cf.cf_flags = 0;
1173 PyEval_MergeCompilerFlags(&cf);
1174 res = PyRun_StringFlags(str, Py_eval_input, globals, locals, &cf);
Guido van Rossum79f25d91997-04-29 20:08:16 +00001175 Py_DECREF(line);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001176 return res;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001177}
1178
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001179PyDoc_STRVAR(input_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001180"input([prompt]) -> value\n\
1181\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001182Equivalent to eval(raw_input(prompt)).");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001183
1184
Guido van Rossume8811f81997-02-14 15:48:05 +00001185static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001186builtin_intern(PyObject *self, PyObject *args)
Guido van Rossume8811f81997-02-14 15:48:05 +00001187{
1188 PyObject *s;
Guido van Rossum43713e52000-02-29 13:59:29 +00001189 if (!PyArg_ParseTuple(args, "S:intern", &s))
Guido van Rossume8811f81997-02-14 15:48:05 +00001190 return NULL;
Jeremy Hylton4c989dd2004-08-07 19:20:05 +00001191 if (!PyString_CheckExact(s)) {
1192 PyErr_SetString(PyExc_TypeError,
1193 "can't intern subclass of string");
1194 return NULL;
1195 }
Guido van Rossume8811f81997-02-14 15:48:05 +00001196 Py_INCREF(s);
1197 PyString_InternInPlace(&s);
1198 return s;
1199}
1200
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001201PyDoc_STRVAR(intern_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001202"intern(string) -> string\n\
1203\n\
1204``Intern'' the given string. This enters the string in the (global)\n\
1205table of interned strings whose purpose is to speed up dictionary lookups.\n\
1206Return the string itself or the previously interned string object with the\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001207same value.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001208
1209
Guido van Rossum79f25d91997-04-29 20:08:16 +00001210static PyObject *
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001211builtin_iter(PyObject *self, PyObject *args)
1212{
1213 PyObject *v, *w = NULL;
1214
Raymond Hettingerea3fdf42002-12-29 16:33:45 +00001215 if (!PyArg_UnpackTuple(args, "iter", 1, 2, &v, &w))
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001216 return NULL;
1217 if (w == NULL)
1218 return PyObject_GetIter(v);
1219 if (!PyCallable_Check(v)) {
1220 PyErr_SetString(PyExc_TypeError,
1221 "iter(v, w): v must be callable");
1222 return NULL;
1223 }
1224 return PyCallIter_New(v, w);
1225}
1226
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001227PyDoc_STRVAR(iter_doc,
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001228"iter(collection) -> iterator\n\
1229iter(callable, sentinel) -> iterator\n\
1230\n\
1231Get an iterator from an object. In the first form, the argument must\n\
1232supply its own iterator, or be a sequence.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001233In the second form, the callable is called until it returns the sentinel.");
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001234
1235
1236static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001237builtin_len(PyObject *self, PyObject *v)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001238{
Martin v. Löwis18e16552006-02-15 17:27:45 +00001239 Py_ssize_t res;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001240
Jeremy Hylton03657cf2000-07-12 13:05:33 +00001241 res = PyObject_Size(v);
Guido van Rossum8ea9f4d1998-06-29 22:26:50 +00001242 if (res < 0 && PyErr_Occurred())
1243 return NULL;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001244 return PyInt_FromSsize_t(res);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001245}
1246
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001247PyDoc_STRVAR(len_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001248"len(object) -> integer\n\
1249\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001250Return the number of items of a sequence or mapping.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001251
1252
Guido van Rossum79f25d91997-04-29 20:08:16 +00001253static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001254builtin_locals(PyObject *self)
Guido van Rossum872537c1995-07-07 22:43:42 +00001255{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001256 PyObject *d;
Guido van Rossum872537c1995-07-07 22:43:42 +00001257
Guido van Rossum79f25d91997-04-29 20:08:16 +00001258 d = PyEval_GetLocals();
Neal Norwitz43bd4db2006-08-12 01:46:42 +00001259 Py_XINCREF(d);
Guido van Rossum872537c1995-07-07 22:43:42 +00001260 return d;
1261}
1262
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001263PyDoc_STRVAR(locals_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001264"locals() -> dictionary\n\
1265\n\
Raymond Hettinger69bf8f32003-01-04 02:16:22 +00001266Update and return a dictionary containing the current scope's local variables.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001267
1268
Guido van Rossum79f25d91997-04-29 20:08:16 +00001269static PyObject *
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001270min_max(PyObject *args, PyObject *kwds, int op)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001271{
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001272 PyObject *v, *it, *item, *val, *maxitem, *maxval, *keyfunc=NULL;
Martin v. Löwisfd39ad42004-08-12 14:42:37 +00001273 const char *name = op == Py_LT ? "min" : "max";
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001274
Guido van Rossum79f25d91997-04-29 20:08:16 +00001275 if (PyTuple_Size(args) > 1)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001276 v = args;
Martin v. Löwisfd39ad42004-08-12 14:42:37 +00001277 else if (!PyArg_UnpackTuple(args, (char *)name, 1, 1, &v))
Guido van Rossum3f5da241990-12-20 15:06:42 +00001278 return NULL;
Tim Peters67d687a2002-04-29 21:27:32 +00001279
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001280 if (kwds != NULL && PyDict_Check(kwds) && PyDict_Size(kwds)) {
1281 keyfunc = PyDict_GetItemString(kwds, "key");
1282 if (PyDict_Size(kwds)!=1 || keyfunc == NULL) {
Georg Brandl99d7e4e2005-08-31 22:21:15 +00001283 PyErr_Format(PyExc_TypeError,
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001284 "%s() got an unexpected keyword argument", name);
1285 return NULL;
1286 }
Guido van Rossum1d9a9ea2008-01-23 20:19:01 +00001287 Py_INCREF(keyfunc);
Georg Brandl99d7e4e2005-08-31 22:21:15 +00001288 }
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001289
Tim Petersc3074532001-05-03 07:00:32 +00001290 it = PyObject_GetIter(v);
Guido van Rossum1d9a9ea2008-01-23 20:19:01 +00001291 if (it == NULL) {
1292 Py_XDECREF(keyfunc);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001293 return NULL;
Guido van Rossum1d9a9ea2008-01-23 20:19:01 +00001294 }
Tim Petersc3074532001-05-03 07:00:32 +00001295
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001296 maxitem = NULL; /* the result */
1297 maxval = NULL; /* the value associated with the result */
Brett Cannon9e635cf2004-12-07 00:25:35 +00001298 while (( item = PyIter_Next(it) )) {
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001299 /* get the value from the key function */
1300 if (keyfunc != NULL) {
1301 val = PyObject_CallFunctionObjArgs(keyfunc, item, NULL);
1302 if (val == NULL)
1303 goto Fail_it_item;
1304 }
1305 /* no key function; the value is the item */
1306 else {
1307 val = item;
1308 Py_INCREF(val);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001309 }
Tim Petersc3074532001-05-03 07:00:32 +00001310
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001311 /* maximum value and item are unset; set them */
1312 if (maxval == NULL) {
1313 maxitem = item;
1314 maxval = val;
1315 }
1316 /* maximum value and item are set; update them as necessary */
Guido van Rossum2d951851994-08-29 12:52:16 +00001317 else {
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001318 int cmp = PyObject_RichCompareBool(val, maxval, op);
1319 if (cmp < 0)
1320 goto Fail_it_item_and_val;
1321 else if (cmp > 0) {
1322 Py_DECREF(maxval);
1323 Py_DECREF(maxitem);
1324 maxval = val;
1325 maxitem = item;
Guido van Rossum53451b32001-01-17 15:47:24 +00001326 }
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001327 else {
1328 Py_DECREF(item);
1329 Py_DECREF(val);
Guido van Rossumc8b6df91997-05-23 00:06:51 +00001330 }
Guido van Rossum2d951851994-08-29 12:52:16 +00001331 }
Guido van Rossum3f5da241990-12-20 15:06:42 +00001332 }
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001333 if (PyErr_Occurred())
1334 goto Fail_it;
1335 if (maxval == NULL) {
Martin v. Löwisfd39ad42004-08-12 14:42:37 +00001336 PyErr_Format(PyExc_ValueError,
1337 "%s() arg is an empty sequence", name);
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001338 assert(maxitem == NULL);
1339 }
1340 else
1341 Py_DECREF(maxval);
Tim Petersc3074532001-05-03 07:00:32 +00001342 Py_DECREF(it);
Guido van Rossum1d9a9ea2008-01-23 20:19:01 +00001343 Py_XDECREF(keyfunc);
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001344 return maxitem;
1345
1346Fail_it_item_and_val:
1347 Py_DECREF(val);
1348Fail_it_item:
1349 Py_DECREF(item);
1350Fail_it:
1351 Py_XDECREF(maxval);
1352 Py_XDECREF(maxitem);
1353 Py_DECREF(it);
Guido van Rossum1d9a9ea2008-01-23 20:19:01 +00001354 Py_XDECREF(keyfunc);
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001355 return NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001356}
1357
Guido van Rossum79f25d91997-04-29 20:08:16 +00001358static PyObject *
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001359builtin_min(PyObject *self, PyObject *args, PyObject *kwds)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001360{
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001361 return min_max(args, kwds, Py_LT);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001362}
1363
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001364PyDoc_STRVAR(min_doc,
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001365"min(iterable[, key=func]) -> value\n\
1366min(a, b, c, ...[, key=func]) -> value\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001367\n\
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001368With a single iterable argument, return its smallest item.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001369With two or more arguments, return the smallest argument.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001370
1371
Guido van Rossum79f25d91997-04-29 20:08:16 +00001372static PyObject *
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001373builtin_max(PyObject *self, PyObject *args, PyObject *kwds)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001374{
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001375 return min_max(args, kwds, Py_GT);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001376}
1377
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001378PyDoc_STRVAR(max_doc,
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001379"max(iterable[, key=func]) -> value\n\
1380max(a, b, c, ...[, key=func]) -> value\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001381\n\
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001382With a single iterable argument, return its largest item.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001383With two or more arguments, return the largest argument.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001384
1385
Guido van Rossum79f25d91997-04-29 20:08:16 +00001386static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001387builtin_oct(PyObject *self, PyObject *v)
Guido van Rossum006bcd41991-10-24 14:54:44 +00001388{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001389 PyNumberMethods *nb;
Neil Schemenauer3a313e32004-07-19 16:29:17 +00001390 PyObject *res;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001391
Guido van Rossum1899c2e1992-09-12 11:09:23 +00001392 if (v == NULL || (nb = v->ob_type->tp_as_number) == NULL ||
1393 nb->nb_oct == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001394 PyErr_SetString(PyExc_TypeError,
Guido van Rossum1899c2e1992-09-12 11:09:23 +00001395 "oct() argument can't be converted to oct");
1396 return NULL;
Guido van Rossum006bcd41991-10-24 14:54:44 +00001397 }
Neil Schemenauer3a313e32004-07-19 16:29:17 +00001398 res = (*nb->nb_oct)(v);
1399 if (res && !PyString_Check(res)) {
1400 PyErr_Format(PyExc_TypeError,
1401 "__oct__ returned non-string (type %.200s)",
1402 res->ob_type->tp_name);
1403 Py_DECREF(res);
1404 return NULL;
1405 }
1406 return res;
Guido van Rossum006bcd41991-10-24 14:54:44 +00001407}
1408
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001409PyDoc_STRVAR(oct_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001410"oct(number) -> string\n\
1411\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001412Return the octal representation of an integer or long integer.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001413
1414
Guido van Rossum79f25d91997-04-29 20:08:16 +00001415static PyObject *
Neal Norwitzc4edb0e2006-05-02 04:43:14 +00001416builtin_open(PyObject *self, PyObject *args, PyObject *kwds)
1417{
1418 return PyObject_Call((PyObject*)&PyFile_Type, args, kwds);
1419}
1420
1421PyDoc_STRVAR(open_doc,
1422"open(name[, mode[, buffering]]) -> file object\n\
1423\n\
Skip Montanaro4e3ebe02007-12-08 14:37:43 +00001424Open a file using the file() type, returns a file object. This is the\n\
1425preferred way to open a file.");
Neal Norwitzc4edb0e2006-05-02 04:43:14 +00001426
1427
1428static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001429builtin_ord(PyObject *self, PyObject* obj)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001430{
Guido van Rossum09095f32000-03-10 23:00:52 +00001431 long ord;
Martin v. Löwisd96ee902006-02-16 14:37:16 +00001432 Py_ssize_t size;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001433
Jeremy Hylton394b54d2000-04-12 21:19:47 +00001434 if (PyString_Check(obj)) {
1435 size = PyString_GET_SIZE(obj);
Andrew M. Kuchling34c20cf2000-12-20 14:36:56 +00001436 if (size == 1) {
Jeremy Hylton394b54d2000-04-12 21:19:47 +00001437 ord = (long)((unsigned char)*PyString_AS_STRING(obj));
Andrew M. Kuchling34c20cf2000-12-20 14:36:56 +00001438 return PyInt_FromLong(ord);
1439 }
Christian Heimes1a6387e2008-03-26 12:49:49 +00001440 } else if (PyBytes_Check(obj)) {
1441 size = PyBytes_GET_SIZE(obj);
1442 if (size == 1) {
1443 ord = (long)((unsigned char)*PyBytes_AS_STRING(obj));
1444 return PyInt_FromLong(ord);
1445 }
1446
Martin v. Löwis339d0f72001-08-17 18:39:25 +00001447#ifdef Py_USING_UNICODE
Jeremy Hylton394b54d2000-04-12 21:19:47 +00001448 } else if (PyUnicode_Check(obj)) {
1449 size = PyUnicode_GET_SIZE(obj);
Andrew M. Kuchling34c20cf2000-12-20 14:36:56 +00001450 if (size == 1) {
Jeremy Hylton394b54d2000-04-12 21:19:47 +00001451 ord = (long)*PyUnicode_AS_UNICODE(obj);
Andrew M. Kuchling34c20cf2000-12-20 14:36:56 +00001452 return PyInt_FromLong(ord);
1453 }
Martin v. Löwis339d0f72001-08-17 18:39:25 +00001454#endif
Jeremy Hylton394b54d2000-04-12 21:19:47 +00001455 } else {
1456 PyErr_Format(PyExc_TypeError,
Andrew M. Kuchlingf07aad12000-12-23 14:11:28 +00001457 "ord() expected string of length 1, but " \
Jeremy Hylton394b54d2000-04-12 21:19:47 +00001458 "%.200s found", obj->ob_type->tp_name);
Guido van Rossum09095f32000-03-10 23:00:52 +00001459 return NULL;
1460 }
1461
Guido van Rossumad991772001-01-12 16:03:05 +00001462 PyErr_Format(PyExc_TypeError,
Andrew M. Kuchlingf07aad12000-12-23 14:11:28 +00001463 "ord() expected a character, "
Martin v. Löwisd96ee902006-02-16 14:37:16 +00001464 "but string of length %zd found",
Jeremy Hylton394b54d2000-04-12 21:19:47 +00001465 size);
1466 return NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001467}
1468
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001469PyDoc_STRVAR(ord_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001470"ord(c) -> integer\n\
1471\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001472Return the integer ordinal of a one-character string.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001473
1474
Guido van Rossum79f25d91997-04-29 20:08:16 +00001475static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001476builtin_pow(PyObject *self, PyObject *args)
Guido van Rossum6a00cd81995-01-07 12:39:01 +00001477{
Guido van Rossum09df08a1998-05-22 00:51:39 +00001478 PyObject *v, *w, *z = Py_None;
Guido van Rossum6a00cd81995-01-07 12:39:01 +00001479
Raymond Hettingerea3fdf42002-12-29 16:33:45 +00001480 if (!PyArg_UnpackTuple(args, "pow", 2, 3, &v, &w, &z))
Guido van Rossum6a00cd81995-01-07 12:39:01 +00001481 return NULL;
Guido van Rossum09df08a1998-05-22 00:51:39 +00001482 return PyNumber_Power(v, w, z);
Guido van Rossumd4905451991-05-05 20:00:36 +00001483}
1484
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001485PyDoc_STRVAR(pow_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001486"pow(x, y[, z]) -> number\n\
1487\n\
1488With two arguments, equivalent to x**y. With three arguments,\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001489equivalent to (x**y) % z, but may be more efficient (e.g. for longs).");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001490
1491
Eric Smith7c478942008-03-18 23:45:49 +00001492static PyObject *
1493builtin_print(PyObject *self, PyObject *args, PyObject *kwds)
1494{
1495 static char *kwlist[] = {"sep", "end", "file", 0};
1496 static PyObject *dummy_args;
1497 PyObject *sep = NULL, *end = NULL, *file = NULL;
1498 int i, err;
1499
1500 if (dummy_args == NULL) {
1501 if (!(dummy_args = PyTuple_New(0)))
1502 return NULL;
1503 }
1504 if (!PyArg_ParseTupleAndKeywords(dummy_args, kwds, "|OOO:print",
1505 kwlist, &sep, &end, &file))
1506 return NULL;
1507 if (file == NULL || file == Py_None) {
1508 file = PySys_GetObject("stdout");
1509 /* sys.stdout may be None when FILE* stdout isn't connected */
1510 if (file == Py_None)
1511 Py_RETURN_NONE;
1512 }
1513
1514 if (sep && sep != Py_None && !PyString_Check(sep) &&
1515 !PyUnicode_Check(sep)) {
1516 PyErr_Format(PyExc_TypeError,
1517 "sep must be None, str or unicode, not %.200s",
1518 sep->ob_type->tp_name);
1519 return NULL;
1520 }
1521 if (end && end != Py_None && !PyString_Check(end) &&
1522 !PyUnicode_Check(end)) {
1523 PyErr_Format(PyExc_TypeError,
1524 "end must be None, str or unicode, not %.200s",
1525 end->ob_type->tp_name);
1526 return NULL;
1527 }
1528
1529 for (i = 0; i < PyTuple_Size(args); i++) {
1530 if (i > 0) {
1531 if (sep == NULL || sep == Py_None)
1532 err = PyFile_WriteString(" ", file);
1533 else
1534 err = PyFile_WriteObject(sep, file,
1535 Py_PRINT_RAW);
1536 if (err)
1537 return NULL;
1538 }
1539 err = PyFile_WriteObject(PyTuple_GetItem(args, i), file,
1540 Py_PRINT_RAW);
1541 if (err)
1542 return NULL;
1543 }
1544
1545 if (end == NULL || end == Py_None)
1546 err = PyFile_WriteString("\n", file);
1547 else
1548 err = PyFile_WriteObject(end, file, Py_PRINT_RAW);
1549 if (err)
1550 return NULL;
1551
1552 Py_RETURN_NONE;
1553}
1554
1555PyDoc_STRVAR(print_doc,
1556"print(value, ..., sep=' ', end='\\n', file=sys.stdout)\n\
1557\n\
1558Prints the values to a stream, or to sys.stdout by default.\n\
1559Optional keyword arguments:\n\
1560file: a file-like object (stream); defaults to the current sys.stdout.\n\
1561sep: string inserted between values, default a space.\n\
1562end: string appended after the last value, default a newline.");
1563
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001564
1565/* Return number of items in range (lo, hi, step), when arguments are
1566 * PyInt or PyLong objects. step > 0 required. Return a value < 0 if
1567 * & only if the true value is too large to fit in a signed long.
1568 * Arguments MUST return 1 with either PyInt_Check() or
1569 * PyLong_Check(). Return -1 when there is an error.
1570 */
1571static long
1572get_len_of_range_longs(PyObject *lo, PyObject *hi, PyObject *step)
1573{
1574 /* -------------------------------------------------------------
1575 Algorithm is equal to that of get_len_of_range(), but it operates
1576 on PyObjects (which are assumed to be PyLong or PyInt objects).
1577 ---------------------------------------------------------------*/
1578 long n;
1579 PyObject *diff = NULL;
1580 PyObject *one = NULL;
1581 PyObject *tmp1 = NULL, *tmp2 = NULL, *tmp3 = NULL;
1582 /* holds sub-expression evaluations */
1583
1584 /* if (lo >= hi), return length of 0. */
1585 if (PyObject_Compare(lo, hi) >= 0)
1586 return 0;
1587
1588 if ((one = PyLong_FromLong(1L)) == NULL)
1589 goto Fail;
1590
1591 if ((tmp1 = PyNumber_Subtract(hi, lo)) == NULL)
1592 goto Fail;
1593
1594 if ((diff = PyNumber_Subtract(tmp1, one)) == NULL)
1595 goto Fail;
1596
1597 if ((tmp2 = PyNumber_FloorDivide(diff, step)) == NULL)
1598 goto Fail;
1599
1600 if ((tmp3 = PyNumber_Add(tmp2, one)) == NULL)
1601 goto Fail;
1602
1603 n = PyLong_AsLong(tmp3);
1604 if (PyErr_Occurred()) { /* Check for Overflow */
1605 PyErr_Clear();
1606 goto Fail;
1607 }
1608
1609 Py_DECREF(tmp3);
1610 Py_DECREF(tmp2);
1611 Py_DECREF(diff);
1612 Py_DECREF(tmp1);
1613 Py_DECREF(one);
1614 return n;
1615
1616 Fail:
1617 Py_XDECREF(tmp3);
1618 Py_XDECREF(tmp2);
1619 Py_XDECREF(diff);
1620 Py_XDECREF(tmp1);
1621 Py_XDECREF(one);
1622 return -1;
1623}
1624
1625/* An extension of builtin_range() that handles the case when PyLong
1626 * arguments are given. */
1627static PyObject *
1628handle_range_longs(PyObject *self, PyObject *args)
1629{
1630 PyObject *ilow;
Tim Peters874e1f72003-04-13 22:13:08 +00001631 PyObject *ihigh = NULL;
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001632 PyObject *istep = NULL;
Tim Peters874e1f72003-04-13 22:13:08 +00001633
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001634 PyObject *curnum = NULL;
1635 PyObject *v = NULL;
1636 long bign;
1637 int i, n;
1638 int cmp_result;
1639
Tim Peters874e1f72003-04-13 22:13:08 +00001640 PyObject *zero = PyLong_FromLong(0);
1641
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001642 if (zero == NULL)
1643 return NULL;
1644
Tim Peters874e1f72003-04-13 22:13:08 +00001645 if (!PyArg_UnpackTuple(args, "range", 1, 3, &ilow, &ihigh, &istep)) {
1646 Py_DECREF(zero);
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001647 return NULL;
1648 }
1649
Tim Peters874e1f72003-04-13 22:13:08 +00001650 /* Figure out which way we were called, supply defaults, and be
1651 * sure to incref everything so that the decrefs at the end
1652 * are correct.
1653 */
1654 assert(ilow != NULL);
1655 if (ihigh == NULL) {
1656 /* only 1 arg -- it's the upper limit */
1657 ihigh = ilow;
1658 ilow = NULL;
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001659 }
Tim Peters874e1f72003-04-13 22:13:08 +00001660 assert(ihigh != NULL);
1661 Py_INCREF(ihigh);
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001662
Tim Peters874e1f72003-04-13 22:13:08 +00001663 /* ihigh correct now; do ilow */
1664 if (ilow == NULL)
1665 ilow = zero;
1666 Py_INCREF(ilow);
1667
1668 /* ilow and ihigh correct now; do istep */
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001669 if (istep == NULL) {
1670 istep = PyLong_FromLong(1L);
1671 if (istep == NULL)
1672 goto Fail;
1673 }
1674 else {
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001675 Py_INCREF(istep);
1676 }
1677
Tim Peters874e1f72003-04-13 22:13:08 +00001678 if (!PyInt_Check(ilow) && !PyLong_Check(ilow)) {
Guido van Rossum28e83e32003-04-15 12:43:26 +00001679 PyErr_Format(PyExc_TypeError,
Alex Martellif471d472003-04-23 13:00:44 +00001680 "range() integer start argument expected, got %s.",
Guido van Rossum817d6c92003-04-14 18:25:04 +00001681 ilow->ob_type->tp_name);
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001682 goto Fail;
1683 }
1684
Tim Peters874e1f72003-04-13 22:13:08 +00001685 if (!PyInt_Check(ihigh) && !PyLong_Check(ihigh)) {
Guido van Rossum28e83e32003-04-15 12:43:26 +00001686 PyErr_Format(PyExc_TypeError,
Alex Martellif471d472003-04-23 13:00:44 +00001687 "range() integer end argument expected, got %s.",
Guido van Rossum817d6c92003-04-14 18:25:04 +00001688 ihigh->ob_type->tp_name);
Tim Peters874e1f72003-04-13 22:13:08 +00001689 goto Fail;
1690 }
1691
1692 if (!PyInt_Check(istep) && !PyLong_Check(istep)) {
Guido van Rossum28e83e32003-04-15 12:43:26 +00001693 PyErr_Format(PyExc_TypeError,
Alex Martellif471d472003-04-23 13:00:44 +00001694 "range() integer step argument expected, got %s.",
Guido van Rossum817d6c92003-04-14 18:25:04 +00001695 istep->ob_type->tp_name);
Tim Peters874e1f72003-04-13 22:13:08 +00001696 goto Fail;
1697 }
1698
1699 if (PyObject_Cmp(istep, zero, &cmp_result) == -1)
1700 goto Fail;
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001701 if (cmp_result == 0) {
1702 PyErr_SetString(PyExc_ValueError,
Alex Martellif471d472003-04-23 13:00:44 +00001703 "range() step argument must not be zero");
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001704 goto Fail;
1705 }
1706
1707 if (cmp_result > 0)
1708 bign = get_len_of_range_longs(ilow, ihigh, istep);
1709 else {
1710 PyObject *neg_istep = PyNumber_Negative(istep);
1711 if (neg_istep == NULL)
1712 goto Fail;
1713 bign = get_len_of_range_longs(ihigh, ilow, neg_istep);
1714 Py_DECREF(neg_istep);
1715 }
1716
1717 n = (int)bign;
1718 if (bign < 0 || (long)n != bign) {
1719 PyErr_SetString(PyExc_OverflowError,
1720 "range() result has too many items");
1721 goto Fail;
1722 }
1723
1724 v = PyList_New(n);
1725 if (v == NULL)
1726 goto Fail;
1727
1728 curnum = ilow;
1729 Py_INCREF(curnum);
1730
1731 for (i = 0; i < n; i++) {
1732 PyObject *w = PyNumber_Long(curnum);
1733 PyObject *tmp_num;
1734 if (w == NULL)
1735 goto Fail;
1736
1737 PyList_SET_ITEM(v, i, w);
1738
1739 tmp_num = PyNumber_Add(curnum, istep);
1740 if (tmp_num == NULL)
1741 goto Fail;
1742
1743 Py_DECREF(curnum);
1744 curnum = tmp_num;
1745 }
Tim Peters874e1f72003-04-13 22:13:08 +00001746 Py_DECREF(ilow);
1747 Py_DECREF(ihigh);
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001748 Py_DECREF(istep);
1749 Py_DECREF(zero);
Tim Peters874e1f72003-04-13 22:13:08 +00001750 Py_DECREF(curnum);
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001751 return v;
1752
1753 Fail:
Tim Peters874e1f72003-04-13 22:13:08 +00001754 Py_DECREF(ilow);
1755 Py_DECREF(ihigh);
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001756 Py_XDECREF(istep);
Tim Peters874e1f72003-04-13 22:13:08 +00001757 Py_DECREF(zero);
1758 Py_XDECREF(curnum);
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001759 Py_XDECREF(v);
1760 return NULL;
1761}
1762
Guido van Rossum124eff01999-02-23 16:11:01 +00001763/* Return number of items in range/xrange (lo, hi, step). step > 0
1764 * required. Return a value < 0 if & only if the true value is too
1765 * large to fit in a signed long.
1766 */
1767static long
Thomas Wouterse28c2962000-07-23 22:21:32 +00001768get_len_of_range(long lo, long hi, long step)
Guido van Rossum124eff01999-02-23 16:11:01 +00001769{
1770 /* -------------------------------------------------------------
1771 If lo >= hi, the range is empty.
1772 Else if n values are in the range, the last one is
1773 lo + (n-1)*step, which must be <= hi-1. Rearranging,
1774 n <= (hi - lo - 1)/step + 1, so taking the floor of the RHS gives
1775 the proper value. Since lo < hi in this case, hi-lo-1 >= 0, so
1776 the RHS is non-negative and so truncation is the same as the
1777 floor. Letting M be the largest positive long, the worst case
1778 for the RHS numerator is hi=M, lo=-M-1, and then
1779 hi-lo-1 = M-(-M-1)-1 = 2*M. Therefore unsigned long has enough
1780 precision to compute the RHS exactly.
1781 ---------------------------------------------------------------*/
1782 long n = 0;
1783 if (lo < hi) {
1784 unsigned long uhi = (unsigned long)hi;
1785 unsigned long ulo = (unsigned long)lo;
1786 unsigned long diff = uhi - ulo - 1;
1787 n = (long)(diff / (unsigned long)step + 1);
1788 }
1789 return n;
1790}
1791
Guido van Rossum79f25d91997-04-29 20:08:16 +00001792static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001793builtin_range(PyObject *self, PyObject *args)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001794{
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001795 long ilow = 0, ihigh = 0, istep = 1;
Guido van Rossum124eff01999-02-23 16:11:01 +00001796 long bign;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001797 int i, n;
Guido van Rossum124eff01999-02-23 16:11:01 +00001798
Guido van Rossum79f25d91997-04-29 20:08:16 +00001799 PyObject *v;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001800
Guido van Rossum79f25d91997-04-29 20:08:16 +00001801 if (PyTuple_Size(args) <= 1) {
1802 if (!PyArg_ParseTuple(args,
Guido van Rossum0865dd91995-01-17 16:30:22 +00001803 "l;range() requires 1-3 int arguments",
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001804 &ihigh)) {
1805 PyErr_Clear();
1806 return handle_range_longs(self, args);
1807 }
Guido van Rossum3f5da241990-12-20 15:06:42 +00001808 }
1809 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001810 if (!PyArg_ParseTuple(args,
Guido van Rossum0865dd91995-01-17 16:30:22 +00001811 "ll|l;range() requires 1-3 int arguments",
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001812 &ilow, &ihigh, &istep)) {
1813 PyErr_Clear();
1814 return handle_range_longs(self, args);
1815 }
Guido van Rossum3f5da241990-12-20 15:06:42 +00001816 }
1817 if (istep == 0) {
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001818 PyErr_SetString(PyExc_ValueError,
Alex Martellif471d472003-04-23 13:00:44 +00001819 "range() step argument must not be zero");
Guido van Rossum3f5da241990-12-20 15:06:42 +00001820 return NULL;
1821 }
Guido van Rossum124eff01999-02-23 16:11:01 +00001822 if (istep > 0)
1823 bign = get_len_of_range(ilow, ihigh, istep);
1824 else
1825 bign = get_len_of_range(ihigh, ilow, -istep);
1826 n = (int)bign;
1827 if (bign < 0 || (long)n != bign) {
Guido van Rossume23cde21999-01-12 05:07:47 +00001828 PyErr_SetString(PyExc_OverflowError,
Fred Drake661ea262000-10-24 19:57:45 +00001829 "range() result has too many items");
Guido van Rossume23cde21999-01-12 05:07:47 +00001830 return NULL;
1831 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00001832 v = PyList_New(n);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001833 if (v == NULL)
1834 return NULL;
1835 for (i = 0; i < n; i++) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001836 PyObject *w = PyInt_FromLong(ilow);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001837 if (w == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001838 Py_DECREF(v);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001839 return NULL;
1840 }
Guido van Rossuma937d141998-04-24 18:22:02 +00001841 PyList_SET_ITEM(v, i, w);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001842 ilow += istep;
1843 }
1844 return v;
1845}
1846
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001847PyDoc_STRVAR(range_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001848"range([start,] stop[, step]) -> list of integers\n\
1849\n\
1850Return a list containing an arithmetic progression of integers.\n\
1851range(i, j) returns [i, i+1, i+2, ..., j-1]; start (!) defaults to 0.\n\
1852When step is given, it specifies the increment (or decrement).\n\
1853For example, range(4) returns [0, 1, 2, 3]. The end point is omitted!\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001854These are exactly the valid indices for a list of 4 elements.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001855
1856
Guido van Rossum79f25d91997-04-29 20:08:16 +00001857static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001858builtin_raw_input(PyObject *self, PyObject *args)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001859{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001860 PyObject *v = NULL;
Martin v. Löwis31d2df52002-08-14 15:46:02 +00001861 PyObject *fin = PySys_GetObject("stdin");
1862 PyObject *fout = PySys_GetObject("stdout");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001863
Raymond Hettingerea3fdf42002-12-29 16:33:45 +00001864 if (!PyArg_UnpackTuple(args, "[raw_]input", 0, 1, &v))
Guido van Rossum3165fe61992-09-25 21:59:05 +00001865 return NULL;
Martin v. Löwis31d2df52002-08-14 15:46:02 +00001866
1867 if (fin == NULL) {
Alex Martellia9b9c9f2003-04-23 13:34:35 +00001868 PyErr_SetString(PyExc_RuntimeError, "[raw_]input: lost sys.stdin");
Martin v. Löwis31d2df52002-08-14 15:46:02 +00001869 return NULL;
1870 }
1871 if (fout == NULL) {
Alex Martellia9b9c9f2003-04-23 13:34:35 +00001872 PyErr_SetString(PyExc_RuntimeError, "[raw_]input: lost sys.stdout");
Martin v. Löwis31d2df52002-08-14 15:46:02 +00001873 return NULL;
1874 }
1875 if (PyFile_SoftSpace(fout, 0)) {
1876 if (PyFile_WriteString(" ", fout) != 0)
1877 return NULL;
1878 }
Georg Brandl7e3ba2a2006-08-06 08:23:54 +00001879 if (PyFile_AsFile(fin) && PyFile_AsFile(fout)
Martin v. Löwis566f6af2002-10-26 14:39:10 +00001880 && isatty(fileno(PyFile_AsFile(fin)))
1881 && isatty(fileno(PyFile_AsFile(fout)))) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001882 PyObject *po;
Guido van Rossum872537c1995-07-07 22:43:42 +00001883 char *prompt;
1884 char *s;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001885 PyObject *result;
Guido van Rossum872537c1995-07-07 22:43:42 +00001886 if (v != NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001887 po = PyObject_Str(v);
Guido van Rossum872537c1995-07-07 22:43:42 +00001888 if (po == NULL)
1889 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001890 prompt = PyString_AsString(po);
Guido van Rossumd9b52081998-06-26 18:25:38 +00001891 if (prompt == NULL)
1892 return NULL;
Guido van Rossum872537c1995-07-07 22:43:42 +00001893 }
1894 else {
1895 po = NULL;
1896 prompt = "";
1897 }
Michael W. Hudson52db5192004-08-02 13:21:09 +00001898 s = PyOS_Readline(PyFile_AsFile(fin), PyFile_AsFile(fout),
Martin v. Löwis566f6af2002-10-26 14:39:10 +00001899 prompt);
Guido van Rossum79f25d91997-04-29 20:08:16 +00001900 Py_XDECREF(po);
Guido van Rossum872537c1995-07-07 22:43:42 +00001901 if (s == NULL) {
Michael W. Hudson30ea2f22004-07-07 17:44:12 +00001902 if (!PyErr_Occurred())
1903 PyErr_SetNone(PyExc_KeyboardInterrupt);
Guido van Rossum872537c1995-07-07 22:43:42 +00001904 return NULL;
1905 }
1906 if (*s == '\0') {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001907 PyErr_SetNone(PyExc_EOFError);
Guido van Rossum872537c1995-07-07 22:43:42 +00001908 result = NULL;
1909 }
1910 else { /* strip trailing '\n' */
Guido van Rossum106f2da2000-06-28 21:12:25 +00001911 size_t len = strlen(s);
Martin v. Löwisb1ed7fa2006-04-13 07:52:27 +00001912 if (len > PY_SSIZE_T_MAX) {
Martin v. Löwis31d2df52002-08-14 15:46:02 +00001913 PyErr_SetString(PyExc_OverflowError,
Alex Martellia9b9c9f2003-04-23 13:34:35 +00001914 "[raw_]input: input too long");
Guido van Rossum106f2da2000-06-28 21:12:25 +00001915 result = NULL;
1916 }
1917 else {
Martin v. Löwisb1ed7fa2006-04-13 07:52:27 +00001918 result = PyString_FromStringAndSize(s, len-1);
Guido van Rossum106f2da2000-06-28 21:12:25 +00001919 }
Guido van Rossum872537c1995-07-07 22:43:42 +00001920 }
Guido van Rossumb18618d2000-05-03 23:44:39 +00001921 PyMem_FREE(s);
Guido van Rossum872537c1995-07-07 22:43:42 +00001922 return result;
1923 }
Guido van Rossum90933611991-06-07 16:10:43 +00001924 if (v != NULL) {
Martin v. Löwis31d2df52002-08-14 15:46:02 +00001925 if (PyFile_WriteObject(v, fout, Py_PRINT_RAW) != 0)
Guido van Rossum90933611991-06-07 16:10:43 +00001926 return NULL;
1927 }
Martin v. Löwis31d2df52002-08-14 15:46:02 +00001928 return PyFile_GetLine(fin, -1);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001929}
1930
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001931PyDoc_STRVAR(raw_input_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001932"raw_input([prompt]) -> string\n\
1933\n\
1934Read a string from standard input. The trailing newline is stripped.\n\
1935If the user hits EOF (Unix: Ctl-D, Windows: Ctl-Z+Return), raise EOFError.\n\
1936On Unix, GNU readline is used if enabled. The prompt string, if given,\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001937is printed without a trailing newline before reading.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001938
1939
Guido van Rossum79f25d91997-04-29 20:08:16 +00001940static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001941builtin_reduce(PyObject *self, PyObject *args)
Guido van Rossum12d12c51993-10-26 17:58:25 +00001942{
Tim Peters15d81ef2001-05-04 04:39:21 +00001943 PyObject *seq, *func, *result = NULL, *it;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001944
Neal Norwitzdf25efe2007-05-23 06:58:36 +00001945 if (Py_Py3kWarningFlag &&
1946 PyErr_Warn(PyExc_DeprecationWarning,
Georg Brandld5b635f2008-03-25 08:29:14 +00001947 "reduce() not supported in 3.x; "
1948 "use functools.reduce()") < 0)
Neal Norwitzdf25efe2007-05-23 06:58:36 +00001949 return NULL;
1950
Raymond Hettingerea3fdf42002-12-29 16:33:45 +00001951 if (!PyArg_UnpackTuple(args, "reduce", 2, 3, &func, &seq, &result))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001952 return NULL;
1953 if (result != NULL)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001954 Py_INCREF(result);
Guido van Rossum12d12c51993-10-26 17:58:25 +00001955
Tim Peters15d81ef2001-05-04 04:39:21 +00001956 it = PyObject_GetIter(seq);
1957 if (it == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001958 PyErr_SetString(PyExc_TypeError,
Tim Peters15d81ef2001-05-04 04:39:21 +00001959 "reduce() arg 2 must support iteration");
1960 Py_XDECREF(result);
Guido van Rossum12d12c51993-10-26 17:58:25 +00001961 return NULL;
1962 }
1963
Guido van Rossum79f25d91997-04-29 20:08:16 +00001964 if ((args = PyTuple_New(2)) == NULL)
Guido van Rossum2d951851994-08-29 12:52:16 +00001965 goto Fail;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001966
Tim Peters15d81ef2001-05-04 04:39:21 +00001967 for (;;) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001968 PyObject *op2;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001969
1970 if (args->ob_refcnt > 1) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001971 Py_DECREF(args);
1972 if ((args = PyTuple_New(2)) == NULL)
Guido van Rossum2d951851994-08-29 12:52:16 +00001973 goto Fail;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001974 }
1975
Tim Peters15d81ef2001-05-04 04:39:21 +00001976 op2 = PyIter_Next(it);
1977 if (op2 == NULL) {
Tim Petersf4848da2001-05-05 00:14:56 +00001978 if (PyErr_Occurred())
1979 goto Fail;
1980 break;
Guido van Rossum2d951851994-08-29 12:52:16 +00001981 }
Guido van Rossum12d12c51993-10-26 17:58:25 +00001982
Guido van Rossum2d951851994-08-29 12:52:16 +00001983 if (result == NULL)
1984 result = op2;
1985 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001986 PyTuple_SetItem(args, 0, result);
1987 PyTuple_SetItem(args, 1, op2);
1988 if ((result = PyEval_CallObject(func, args)) == NULL)
Guido van Rossum2d951851994-08-29 12:52:16 +00001989 goto Fail;
1990 }
Guido van Rossum12d12c51993-10-26 17:58:25 +00001991 }
1992
Guido van Rossum79f25d91997-04-29 20:08:16 +00001993 Py_DECREF(args);
Guido van Rossum12d12c51993-10-26 17:58:25 +00001994
Guido van Rossum2d951851994-08-29 12:52:16 +00001995 if (result == NULL)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001996 PyErr_SetString(PyExc_TypeError,
Fred Drake661ea262000-10-24 19:57:45 +00001997 "reduce() of empty sequence with no initial value");
Guido van Rossum2d951851994-08-29 12:52:16 +00001998
Tim Peters15d81ef2001-05-04 04:39:21 +00001999 Py_DECREF(it);
Guido van Rossum12d12c51993-10-26 17:58:25 +00002000 return result;
2001
Guido van Rossum2d951851994-08-29 12:52:16 +00002002Fail:
Guido van Rossum79f25d91997-04-29 20:08:16 +00002003 Py_XDECREF(args);
2004 Py_XDECREF(result);
Tim Peters15d81ef2001-05-04 04:39:21 +00002005 Py_DECREF(it);
Guido van Rossum12d12c51993-10-26 17:58:25 +00002006 return NULL;
2007}
2008
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002009PyDoc_STRVAR(reduce_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002010"reduce(function, sequence[, initial]) -> value\n\
2011\n\
2012Apply a function of two arguments cumulatively to the items of a sequence,\n\
2013from left to right, so as to reduce the sequence to a single value.\n\
2014For example, reduce(lambda x, y: x+y, [1, 2, 3, 4, 5]) calculates\n\
2015((((1+2)+3)+4)+5). If initial is present, it is placed before the items\n\
2016of the sequence in the calculation, and serves as a default when the\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002017sequence is empty.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002018
2019
Guido van Rossum79f25d91997-04-29 20:08:16 +00002020static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002021builtin_reload(PyObject *self, PyObject *v)
Guido van Rossum3f5da241990-12-20 15:06:42 +00002022{
Neal Norwitzdf25efe2007-05-23 06:58:36 +00002023 if (Py_Py3kWarningFlag &&
2024 PyErr_Warn(PyExc_DeprecationWarning,
Georg Brandld5b635f2008-03-25 08:29:14 +00002025 "reload() not supported in 3.x; use imp.reload()") < 0)
Neal Norwitzdf25efe2007-05-23 06:58:36 +00002026 return NULL;
2027
Guido van Rossum79f25d91997-04-29 20:08:16 +00002028 return PyImport_ReloadModule(v);
Guido van Rossum3f5da241990-12-20 15:06:42 +00002029}
2030
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002031PyDoc_STRVAR(reload_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002032"reload(module) -> module\n\
2033\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002034Reload the module. The module must have been successfully imported before.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002035
2036
Guido van Rossum79f25d91997-04-29 20:08:16 +00002037static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002038builtin_repr(PyObject *self, PyObject *v)
Guido van Rossumc89705d1992-11-26 08:54:07 +00002039{
Guido van Rossum79f25d91997-04-29 20:08:16 +00002040 return PyObject_Repr(v);
Guido van Rossumc89705d1992-11-26 08:54:07 +00002041}
2042
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002043PyDoc_STRVAR(repr_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002044"repr(object) -> string\n\
2045\n\
2046Return the canonical string representation of the object.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002047For most object types, eval(repr(object)) == object.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002048
2049
Guido van Rossum79f25d91997-04-29 20:08:16 +00002050static PyObject *
Georg Brandlccadf842006-03-31 18:54:53 +00002051builtin_round(PyObject *self, PyObject *args, PyObject *kwds)
Guido van Rossum9e51f9b1993-02-12 16:29:05 +00002052{
Jeffrey Yasskin9871d8f2008-01-05 08:47:13 +00002053 double number;
2054 double f;
2055 int ndigits = 0;
2056 int i;
Georg Brandlccadf842006-03-31 18:54:53 +00002057 static char *kwlist[] = {"number", "ndigits", 0};
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002058
Jeffrey Yasskin9871d8f2008-01-05 08:47:13 +00002059 if (!PyArg_ParseTupleAndKeywords(args, kwds, "d|i:round",
2060 kwlist, &number, &ndigits))
2061 return NULL;
2062 f = 1.0;
2063 i = abs(ndigits);
2064 while (--i >= 0)
2065 f = f*10.0;
2066 if (ndigits < 0)
2067 number /= f;
2068 else
2069 number *= f;
2070 if (number >= 0.0)
2071 number = floor(number + 0.5);
2072 else
2073 number = ceil(number - 0.5);
2074 if (ndigits < 0)
2075 number *= f;
2076 else
2077 number /= f;
2078 return PyFloat_FromDouble(number);
Guido van Rossum9e51f9b1993-02-12 16:29:05 +00002079}
2080
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002081PyDoc_STRVAR(round_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002082"round(number[, ndigits]) -> floating point number\n\
2083\n\
2084Round a number to a given precision in decimal digits (default 0 digits).\n\
Jeffrey Yasskin9871d8f2008-01-05 08:47:13 +00002085This always returns a floating point number. Precision may be negative.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002086
Raymond Hettinger64958a12003-12-17 20:43:33 +00002087static PyObject *
2088builtin_sorted(PyObject *self, PyObject *args, PyObject *kwds)
2089{
2090 PyObject *newlist, *v, *seq, *compare=NULL, *keyfunc=NULL, *newargs;
2091 PyObject *callable;
Martin v. Löwis15e62742006-02-27 16:46:16 +00002092 static char *kwlist[] = {"iterable", "cmp", "key", "reverse", 0};
Neal Norwitz6f0d4792005-12-15 06:40:36 +00002093 int reverse;
Raymond Hettinger64958a12003-12-17 20:43:33 +00002094
Neal Norwitz6f0d4792005-12-15 06:40:36 +00002095 /* args 1-4 should match listsort in Objects/listobject.c */
Armin Rigo71d7e702005-09-20 18:13:03 +00002096 if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|OOi:sorted",
2097 kwlist, &seq, &compare, &keyfunc, &reverse))
2098 return NULL;
Raymond Hettinger64958a12003-12-17 20:43:33 +00002099
2100 newlist = PySequence_List(seq);
2101 if (newlist == NULL)
2102 return NULL;
2103
2104 callable = PyObject_GetAttrString(newlist, "sort");
2105 if (callable == NULL) {
2106 Py_DECREF(newlist);
2107 return NULL;
2108 }
Georg Brandl99d7e4e2005-08-31 22:21:15 +00002109
Raymond Hettinger64958a12003-12-17 20:43:33 +00002110 newargs = PyTuple_GetSlice(args, 1, 4);
2111 if (newargs == NULL) {
2112 Py_DECREF(newlist);
2113 Py_DECREF(callable);
2114 return NULL;
2115 }
2116
2117 v = PyObject_Call(callable, newargs, kwds);
2118 Py_DECREF(newargs);
2119 Py_DECREF(callable);
2120 if (v == NULL) {
2121 Py_DECREF(newlist);
2122 return NULL;
2123 }
2124 Py_DECREF(v);
2125 return newlist;
2126}
2127
2128PyDoc_STRVAR(sorted_doc,
2129"sorted(iterable, cmp=None, key=None, reverse=False) --> new sorted list");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002130
Guido van Rossum79f25d91997-04-29 20:08:16 +00002131static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002132builtin_vars(PyObject *self, PyObject *args)
Guido van Rossum2d951851994-08-29 12:52:16 +00002133{
Guido van Rossum79f25d91997-04-29 20:08:16 +00002134 PyObject *v = NULL;
2135 PyObject *d;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002136
Raymond Hettingerea3fdf42002-12-29 16:33:45 +00002137 if (!PyArg_UnpackTuple(args, "vars", 0, 1, &v))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002138 return NULL;
Guido van Rossum2d951851994-08-29 12:52:16 +00002139 if (v == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002140 d = PyEval_GetLocals();
Guido van Rossum53bb7ff1995-07-26 16:26:31 +00002141 if (d == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002142 if (!PyErr_Occurred())
2143 PyErr_SetString(PyExc_SystemError,
Alex Martellia9b9c9f2003-04-23 13:34:35 +00002144 "vars(): no locals!?");
Guido van Rossum53bb7ff1995-07-26 16:26:31 +00002145 }
2146 else
Guido van Rossum79f25d91997-04-29 20:08:16 +00002147 Py_INCREF(d);
Guido van Rossum2d951851994-08-29 12:52:16 +00002148 }
2149 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002150 d = PyObject_GetAttrString(v, "__dict__");
Guido van Rossum2d951851994-08-29 12:52:16 +00002151 if (d == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002152 PyErr_SetString(PyExc_TypeError,
Guido van Rossum2d951851994-08-29 12:52:16 +00002153 "vars() argument must have __dict__ attribute");
2154 return NULL;
2155 }
2156 }
2157 return d;
2158}
2159
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002160PyDoc_STRVAR(vars_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002161"vars([object]) -> dictionary\n\
2162\n\
2163Without arguments, equivalent to locals().\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002164With an argument, equivalent to object.__dict__.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002165
Alex Martellia70b1912003-04-22 08:12:33 +00002166
2167static PyObject*
2168builtin_sum(PyObject *self, PyObject *args)
2169{
2170 PyObject *seq;
2171 PyObject *result = NULL;
2172 PyObject *temp, *item, *iter;
2173
Raymond Hettinger5cf63942003-10-25 06:41:37 +00002174 if (!PyArg_UnpackTuple(args, "sum", 1, 2, &seq, &result))
Alex Martellia70b1912003-04-22 08:12:33 +00002175 return NULL;
2176
2177 iter = PyObject_GetIter(seq);
2178 if (iter == NULL)
2179 return NULL;
2180
2181 if (result == NULL) {
2182 result = PyInt_FromLong(0);
2183 if (result == NULL) {
2184 Py_DECREF(iter);
2185 return NULL;
2186 }
2187 } else {
2188 /* reject string values for 'start' parameter */
2189 if (PyObject_TypeCheck(result, &PyBaseString_Type)) {
2190 PyErr_SetString(PyExc_TypeError,
Alex Martellia9b9c9f2003-04-23 13:34:35 +00002191 "sum() can't sum strings [use ''.join(seq) instead]");
Alex Martellia70b1912003-04-22 08:12:33 +00002192 Py_DECREF(iter);
2193 return NULL;
2194 }
Alex Martelli41c9f882003-04-22 09:24:48 +00002195 Py_INCREF(result);
Alex Martellia70b1912003-04-22 08:12:33 +00002196 }
2197
Raymond Hettinger3f8caa32007-10-24 01:28:33 +00002198#ifndef SLOW_SUM
2199 /* Fast addition by keeping temporary sums in C instead of new Python objects.
2200 Assumes all inputs are the same type. If the assumption fails, default
2201 to the more general routine.
2202 */
2203 if (PyInt_CheckExact(result)) {
2204 long i_result = PyInt_AS_LONG(result);
2205 Py_DECREF(result);
2206 result = NULL;
2207 while(result == NULL) {
2208 item = PyIter_Next(iter);
2209 if (item == NULL) {
2210 Py_DECREF(iter);
2211 if (PyErr_Occurred())
2212 return NULL;
2213 return PyInt_FromLong(i_result);
2214 }
2215 if (PyInt_CheckExact(item)) {
2216 long b = PyInt_AS_LONG(item);
2217 long x = i_result + b;
2218 if ((x^i_result) >= 0 || (x^b) >= 0) {
2219 i_result = x;
2220 Py_DECREF(item);
2221 continue;
2222 }
2223 }
2224 /* Either overflowed or is not an int. Restore real objects and process normally */
2225 result = PyInt_FromLong(i_result);
2226 temp = PyNumber_Add(result, item);
2227 Py_DECREF(result);
2228 Py_DECREF(item);
2229 result = temp;
2230 if (result == NULL) {
2231 Py_DECREF(iter);
2232 return NULL;
2233 }
2234 }
2235 }
2236
2237 if (PyFloat_CheckExact(result)) {
2238 double f_result = PyFloat_AS_DOUBLE(result);
2239 Py_DECREF(result);
2240 result = NULL;
2241 while(result == NULL) {
2242 item = PyIter_Next(iter);
2243 if (item == NULL) {
2244 Py_DECREF(iter);
2245 if (PyErr_Occurred())
2246 return NULL;
2247 return PyFloat_FromDouble(f_result);
2248 }
2249 if (PyFloat_CheckExact(item)) {
2250 PyFPE_START_PROTECT("add", return 0)
2251 f_result += PyFloat_AS_DOUBLE(item);
Raymond Hettinger3a8daf52007-10-24 02:05:51 +00002252 PyFPE_END_PROTECT(f_result)
Raymond Hettingera45c4872007-10-25 02:26:58 +00002253 Py_DECREF(item);
Raymond Hettinger3a8daf52007-10-24 02:05:51 +00002254 continue;
2255 }
2256 if (PyInt_CheckExact(item)) {
2257 PyFPE_START_PROTECT("add", return 0)
2258 f_result += (double)PyInt_AS_LONG(item);
2259 PyFPE_END_PROTECT(f_result)
Raymond Hettingera45c4872007-10-25 02:26:58 +00002260 Py_DECREF(item);
Raymond Hettinger3f8caa32007-10-24 01:28:33 +00002261 continue;
2262 }
2263 result = PyFloat_FromDouble(f_result);
2264 temp = PyNumber_Add(result, item);
2265 Py_DECREF(result);
2266 Py_DECREF(item);
2267 result = temp;
2268 if (result == NULL) {
2269 Py_DECREF(iter);
2270 return NULL;
2271 }
2272 }
2273 }
2274#endif
2275
Alex Martellia70b1912003-04-22 08:12:33 +00002276 for(;;) {
2277 item = PyIter_Next(iter);
2278 if (item == NULL) {
2279 /* error, or end-of-sequence */
2280 if (PyErr_Occurred()) {
2281 Py_DECREF(result);
2282 result = NULL;
2283 }
2284 break;
2285 }
Alex Martellia253e182003-10-25 23:24:14 +00002286 temp = PyNumber_Add(result, item);
Alex Martellia70b1912003-04-22 08:12:33 +00002287 Py_DECREF(result);
2288 Py_DECREF(item);
2289 result = temp;
2290 if (result == NULL)
2291 break;
2292 }
2293 Py_DECREF(iter);
2294 return result;
2295}
2296
2297PyDoc_STRVAR(sum_doc,
Georg Brandl8134d062006-10-12 12:33:07 +00002298"sum(sequence[, start]) -> value\n\
Alex Martellia70b1912003-04-22 08:12:33 +00002299\n\
2300Returns the sum of a sequence of numbers (NOT strings) plus the value\n\
Georg Brandl8134d062006-10-12 12:33:07 +00002301of parameter 'start' (which defaults to 0). When the sequence is\n\
2302empty, returns start.");
Alex Martellia70b1912003-04-22 08:12:33 +00002303
2304
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002305static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002306builtin_isinstance(PyObject *self, PyObject *args)
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002307{
2308 PyObject *inst;
2309 PyObject *cls;
Guido van Rossum823649d2001-03-21 18:40:58 +00002310 int retval;
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002311
Raymond Hettingerea3fdf42002-12-29 16:33:45 +00002312 if (!PyArg_UnpackTuple(args, "isinstance", 2, 2, &inst, &cls))
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002313 return NULL;
Guido van Rossumf5dd9141997-12-02 19:11:45 +00002314
Guido van Rossum823649d2001-03-21 18:40:58 +00002315 retval = PyObject_IsInstance(inst, cls);
2316 if (retval < 0)
Guido van Rossumad991772001-01-12 16:03:05 +00002317 return NULL;
Guido van Rossum77f6a652002-04-03 22:41:51 +00002318 return PyBool_FromLong(retval);
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002319}
2320
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002321PyDoc_STRVAR(isinstance_doc,
Guido van Rossum77f6a652002-04-03 22:41:51 +00002322"isinstance(object, class-or-type-or-tuple) -> bool\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002323\n\
2324Return whether an object is an instance of a class or of a subclass thereof.\n\
Guido van Rossum03290ec2001-10-07 20:54:12 +00002325With a type as second argument, return whether that is the object's type.\n\
2326The form using a tuple, isinstance(x, (A, B, ...)), is a shortcut for\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002327isinstance(x, A) or isinstance(x, B) or ... (etc.).");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002328
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002329
2330static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002331builtin_issubclass(PyObject *self, PyObject *args)
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002332{
2333 PyObject *derived;
2334 PyObject *cls;
2335 int retval;
2336
Raymond Hettingerea3fdf42002-12-29 16:33:45 +00002337 if (!PyArg_UnpackTuple(args, "issubclass", 2, 2, &derived, &cls))
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002338 return NULL;
Guido van Rossum668213d1999-06-16 17:28:37 +00002339
Guido van Rossum823649d2001-03-21 18:40:58 +00002340 retval = PyObject_IsSubclass(derived, cls);
2341 if (retval < 0)
2342 return NULL;
Guido van Rossum77f6a652002-04-03 22:41:51 +00002343 return PyBool_FromLong(retval);
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002344}
2345
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002346PyDoc_STRVAR(issubclass_doc,
Guido van Rossum77f6a652002-04-03 22:41:51 +00002347"issubclass(C, B) -> bool\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002348\n\
Walter Dörwaldd9a6ad32002-12-12 16:41:44 +00002349Return whether class C is a subclass (i.e., a derived class) of class B.\n\
2350When using a tuple as the second argument issubclass(X, (A, B, ...)),\n\
2351is a shortcut for issubclass(X, A) or issubclass(X, B) or ... (etc.).");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002352
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002353
Barry Warsawbd599b52000-08-03 15:45:29 +00002354static PyObject*
2355builtin_zip(PyObject *self, PyObject *args)
2356{
2357 PyObject *ret;
Martin v. Löwisd96ee902006-02-16 14:37:16 +00002358 const Py_ssize_t itemsize = PySequence_Length(args);
2359 Py_ssize_t i;
Tim Peters8572b4f2001-05-06 01:05:02 +00002360 PyObject *itlist; /* tuple of iterators */
Martin v. Löwisd96ee902006-02-16 14:37:16 +00002361 Py_ssize_t len; /* guess at result length */
Barry Warsawbd599b52000-08-03 15:45:29 +00002362
Raymond Hettingereaef6152003-08-02 07:42:57 +00002363 if (itemsize == 0)
2364 return PyList_New(0);
2365
Barry Warsawbd599b52000-08-03 15:45:29 +00002366 /* args must be a tuple */
2367 assert(PyTuple_Check(args));
2368
Tim Peters39a86c22002-05-12 07:19:38 +00002369 /* Guess at result length: the shortest of the input lengths.
2370 If some argument refuses to say, we refuse to guess too, lest
2371 an argument like xrange(sys.maxint) lead us astray.*/
Tim Peters67d687a2002-04-29 21:27:32 +00002372 len = -1; /* unknown */
2373 for (i = 0; i < itemsize; ++i) {
2374 PyObject *item = PyTuple_GET_ITEM(args, i);
Raymond Hettinger4e2f7142007-12-06 00:56:53 +00002375 Py_ssize_t thislen = _PyObject_LengthHint(item, -1);
Tim Peters39a86c22002-05-12 07:19:38 +00002376 if (thislen < 0) {
Tim Peters39a86c22002-05-12 07:19:38 +00002377 len = -1;
2378 break;
2379 }
Tim Peters67d687a2002-04-29 21:27:32 +00002380 else if (len < 0 || thislen < len)
2381 len = thislen;
2382 }
2383
Tim Peters8572b4f2001-05-06 01:05:02 +00002384 /* allocate result list */
Tim Peters67d687a2002-04-29 21:27:32 +00002385 if (len < 0)
2386 len = 10; /* arbitrary */
2387 if ((ret = PyList_New(len)) == NULL)
Barry Warsawbd599b52000-08-03 15:45:29 +00002388 return NULL;
2389
Tim Peters8572b4f2001-05-06 01:05:02 +00002390 /* obtain iterators */
2391 itlist = PyTuple_New(itemsize);
2392 if (itlist == NULL)
2393 goto Fail_ret;
2394 for (i = 0; i < itemsize; ++i) {
2395 PyObject *item = PyTuple_GET_ITEM(args, i);
2396 PyObject *it = PyObject_GetIter(item);
2397 if (it == NULL) {
2398 if (PyErr_ExceptionMatches(PyExc_TypeError))
2399 PyErr_Format(PyExc_TypeError,
Neal Norwitza361bd82006-02-19 19:31:50 +00002400 "zip argument #%zd must support iteration",
Tim Peters8572b4f2001-05-06 01:05:02 +00002401 i+1);
2402 goto Fail_ret_itlist;
Barry Warsawbd599b52000-08-03 15:45:29 +00002403 }
Tim Peters8572b4f2001-05-06 01:05:02 +00002404 PyTuple_SET_ITEM(itlist, i, it);
2405 }
Barry Warsawbd599b52000-08-03 15:45:29 +00002406
Tim Peters8572b4f2001-05-06 01:05:02 +00002407 /* build result into ret list */
Tim Peters67d687a2002-04-29 21:27:32 +00002408 for (i = 0; ; ++i) {
2409 int j;
Tim Peters8572b4f2001-05-06 01:05:02 +00002410 PyObject *next = PyTuple_New(itemsize);
2411 if (!next)
2412 goto Fail_ret_itlist;
2413
Tim Peters67d687a2002-04-29 21:27:32 +00002414 for (j = 0; j < itemsize; j++) {
2415 PyObject *it = PyTuple_GET_ITEM(itlist, j);
Tim Peters8572b4f2001-05-06 01:05:02 +00002416 PyObject *item = PyIter_Next(it);
Barry Warsawbd599b52000-08-03 15:45:29 +00002417 if (!item) {
Tim Peters8572b4f2001-05-06 01:05:02 +00002418 if (PyErr_Occurred()) {
2419 Py_DECREF(ret);
2420 ret = NULL;
Barry Warsawbd599b52000-08-03 15:45:29 +00002421 }
2422 Py_DECREF(next);
Tim Peters8572b4f2001-05-06 01:05:02 +00002423 Py_DECREF(itlist);
Tim Peters67d687a2002-04-29 21:27:32 +00002424 goto Done;
Barry Warsawbd599b52000-08-03 15:45:29 +00002425 }
Tim Peters67d687a2002-04-29 21:27:32 +00002426 PyTuple_SET_ITEM(next, j, item);
Barry Warsawbd599b52000-08-03 15:45:29 +00002427 }
Tim Peters8572b4f2001-05-06 01:05:02 +00002428
Tim Peters67d687a2002-04-29 21:27:32 +00002429 if (i < len)
2430 PyList_SET_ITEM(ret, i, next);
2431 else {
2432 int status = PyList_Append(ret, next);
2433 Py_DECREF(next);
2434 ++len;
2435 if (status < 0)
2436 goto Fail_ret_itlist;
2437 }
Barry Warsawbd599b52000-08-03 15:45:29 +00002438 }
Tim Peters8572b4f2001-05-06 01:05:02 +00002439
Tim Peters67d687a2002-04-29 21:27:32 +00002440Done:
2441 if (ret != NULL && i < len) {
2442 /* The list is too big. */
2443 if (PyList_SetSlice(ret, i, len, NULL) < 0)
2444 return NULL;
2445 }
2446 return ret;
2447
Tim Peters8572b4f2001-05-06 01:05:02 +00002448Fail_ret_itlist:
2449 Py_DECREF(itlist);
2450Fail_ret:
2451 Py_DECREF(ret);
2452 return NULL;
Barry Warsawbd599b52000-08-03 15:45:29 +00002453}
2454
2455
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002456PyDoc_STRVAR(zip_doc,
Barry Warsawbd599b52000-08-03 15:45:29 +00002457"zip(seq1 [, seq2 [...]]) -> [(seq1[0], seq2[0] ...), (...)]\n\
2458\n\
2459Return a list of tuples, where each tuple contains the i-th element\n\
2460from each of the argument sequences. The returned list is truncated\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002461in length to the length of the shortest argument sequence.");
Barry Warsawbd599b52000-08-03 15:45:29 +00002462
2463
Guido van Rossum79f25d91997-04-29 20:08:16 +00002464static PyMethodDef builtin_methods[] = {
Neal Norwitz92e212f2006-04-03 04:48:37 +00002465 {"__import__", (PyCFunction)builtin___import__, METH_VARARGS | METH_KEYWORDS, import_doc},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002466 {"abs", builtin_abs, METH_O, abs_doc},
Raymond Hettinger96229b12005-03-11 06:49:40 +00002467 {"all", builtin_all, METH_O, all_doc},
2468 {"any", builtin_any, METH_O, any_doc},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002469 {"apply", builtin_apply, METH_VARARGS, apply_doc},
Eric Smith3cd81942008-02-22 16:30:22 +00002470 {"bin", builtin_bin, METH_O, bin_doc},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002471 {"callable", builtin_callable, METH_O, callable_doc},
2472 {"chr", builtin_chr, METH_VARARGS, chr_doc},
2473 {"cmp", builtin_cmp, METH_VARARGS, cmp_doc},
2474 {"coerce", builtin_coerce, METH_VARARGS, coerce_doc},
Georg Brandl5240d742007-03-13 20:46:32 +00002475 {"compile", (PyCFunction)builtin_compile, METH_VARARGS | METH_KEYWORDS, compile_doc},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002476 {"delattr", builtin_delattr, METH_VARARGS, delattr_doc},
2477 {"dir", builtin_dir, METH_VARARGS, dir_doc},
2478 {"divmod", builtin_divmod, METH_VARARGS, divmod_doc},
2479 {"eval", builtin_eval, METH_VARARGS, eval_doc},
2480 {"execfile", builtin_execfile, METH_VARARGS, execfile_doc},
2481 {"filter", builtin_filter, METH_VARARGS, filter_doc},
Eric Smitha9f7d622008-02-17 19:46:49 +00002482 {"format", builtin_format, METH_VARARGS, format_doc},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002483 {"getattr", builtin_getattr, METH_VARARGS, getattr_doc},
2484 {"globals", (PyCFunction)builtin_globals, METH_NOARGS, globals_doc},
2485 {"hasattr", builtin_hasattr, METH_VARARGS, hasattr_doc},
2486 {"hash", builtin_hash, METH_O, hash_doc},
2487 {"hex", builtin_hex, METH_O, hex_doc},
2488 {"id", builtin_id, METH_O, id_doc},
2489 {"input", builtin_input, METH_VARARGS, input_doc},
2490 {"intern", builtin_intern, METH_VARARGS, intern_doc},
2491 {"isinstance", builtin_isinstance, METH_VARARGS, isinstance_doc},
2492 {"issubclass", builtin_issubclass, METH_VARARGS, issubclass_doc},
2493 {"iter", builtin_iter, METH_VARARGS, iter_doc},
2494 {"len", builtin_len, METH_O, len_doc},
2495 {"locals", (PyCFunction)builtin_locals, METH_NOARGS, locals_doc},
2496 {"map", builtin_map, METH_VARARGS, map_doc},
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00002497 {"max", (PyCFunction)builtin_max, METH_VARARGS | METH_KEYWORDS, max_doc},
2498 {"min", (PyCFunction)builtin_min, METH_VARARGS | METH_KEYWORDS, min_doc},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002499 {"oct", builtin_oct, METH_O, oct_doc},
Neal Norwitzc4edb0e2006-05-02 04:43:14 +00002500 {"open", (PyCFunction)builtin_open, METH_VARARGS | METH_KEYWORDS, open_doc},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002501 {"ord", builtin_ord, METH_O, ord_doc},
2502 {"pow", builtin_pow, METH_VARARGS, pow_doc},
Eric Smith7c478942008-03-18 23:45:49 +00002503 {"print", (PyCFunction)builtin_print, METH_VARARGS | METH_KEYWORDS, print_doc},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002504 {"range", builtin_range, METH_VARARGS, range_doc},
2505 {"raw_input", builtin_raw_input, METH_VARARGS, raw_input_doc},
2506 {"reduce", builtin_reduce, METH_VARARGS, reduce_doc},
2507 {"reload", builtin_reload, METH_O, reload_doc},
2508 {"repr", builtin_repr, METH_O, repr_doc},
Georg Brandlccadf842006-03-31 18:54:53 +00002509 {"round", (PyCFunction)builtin_round, METH_VARARGS | METH_KEYWORDS, round_doc},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002510 {"setattr", builtin_setattr, METH_VARARGS, setattr_doc},
Raymond Hettinger64958a12003-12-17 20:43:33 +00002511 {"sorted", (PyCFunction)builtin_sorted, METH_VARARGS | METH_KEYWORDS, sorted_doc},
Alex Martellia70b1912003-04-22 08:12:33 +00002512 {"sum", builtin_sum, METH_VARARGS, sum_doc},
Martin v. Löwis339d0f72001-08-17 18:39:25 +00002513#ifdef Py_USING_UNICODE
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002514 {"unichr", builtin_unichr, METH_VARARGS, unichr_doc},
Martin v. Löwis339d0f72001-08-17 18:39:25 +00002515#endif
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002516 {"vars", builtin_vars, METH_VARARGS, vars_doc},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002517 {"zip", builtin_zip, METH_VARARGS, zip_doc},
Guido van Rossumc02e15c1991-12-16 13:03:00 +00002518 {NULL, NULL},
Guido van Rossum3f5da241990-12-20 15:06:42 +00002519};
2520
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002521PyDoc_STRVAR(builtin_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002522"Built-in functions, exceptions, and other objects.\n\
2523\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002524Noteworthy: None is the `nil' object; Ellipsis represents `...' in slices.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002525
Guido van Rossum25ce5661997-08-02 03:10:38 +00002526PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002527_PyBuiltin_Init(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +00002528{
Fred Drake5550de32000-06-20 04:54:19 +00002529 PyObject *mod, *dict, *debug;
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002530 mod = Py_InitModule4("__builtin__", builtin_methods,
2531 builtin_doc, (PyObject *)NULL,
2532 PYTHON_API_VERSION);
Guido van Rossum25ce5661997-08-02 03:10:38 +00002533 if (mod == NULL)
2534 return NULL;
2535 dict = PyModule_GetDict(mod);
Tim Peters4b7625e2001-09-13 21:37:17 +00002536
Tim Peters7571a0f2003-03-23 17:52:28 +00002537#ifdef Py_TRACE_REFS
2538 /* __builtin__ exposes a number of statically allocated objects
2539 * that, before this code was added in 2.3, never showed up in
2540 * the list of "all objects" maintained by Py_TRACE_REFS. As a
2541 * result, programs leaking references to None and False (etc)
2542 * couldn't be diagnosed by examining sys.getobjects(0).
2543 */
2544#define ADD_TO_ALL(OBJECT) _Py_AddToAllObjects((PyObject *)(OBJECT), 0)
2545#else
2546#define ADD_TO_ALL(OBJECT) (void)0
2547#endif
2548
Tim Peters4b7625e2001-09-13 21:37:17 +00002549#define SETBUILTIN(NAME, OBJECT) \
Tim Peters7571a0f2003-03-23 17:52:28 +00002550 if (PyDict_SetItemString(dict, NAME, (PyObject *)OBJECT) < 0) \
2551 return NULL; \
2552 ADD_TO_ALL(OBJECT)
Tim Peters4b7625e2001-09-13 21:37:17 +00002553
2554 SETBUILTIN("None", Py_None);
2555 SETBUILTIN("Ellipsis", Py_Ellipsis);
2556 SETBUILTIN("NotImplemented", Py_NotImplemented);
Guido van Rossum77f6a652002-04-03 22:41:51 +00002557 SETBUILTIN("False", Py_False);
2558 SETBUILTIN("True", Py_True);
Neal Norwitz32a7e7f2002-05-31 19:58:02 +00002559 SETBUILTIN("basestring", &PyBaseString_Type);
Guido van Rossum77f6a652002-04-03 22:41:51 +00002560 SETBUILTIN("bool", &PyBool_Type);
Travis E. Oliphant3781aef2008-03-18 04:44:57 +00002561 /* SETBUILTIN("memoryview", &PyMemoryView_Type); */
Christian Heimes1a6387e2008-03-26 12:49:49 +00002562 SETBUILTIN("bytearray", &PyBytes_Type);
Christian Heimes288e89a2008-01-18 18:24:07 +00002563 SETBUILTIN("bytes", &PyString_Type);
Guido van Rossumbea18cc2002-06-14 20:41:17 +00002564 SETBUILTIN("buffer", &PyBuffer_Type);
Tim Peters4b7625e2001-09-13 21:37:17 +00002565 SETBUILTIN("classmethod", &PyClassMethod_Type);
Tim Peters6d6c1a32001-08-02 04:15:00 +00002566#ifndef WITHOUT_COMPLEX
Tim Peters4b7625e2001-09-13 21:37:17 +00002567 SETBUILTIN("complex", &PyComplex_Type);
Tim Peters6d6c1a32001-08-02 04:15:00 +00002568#endif
Tim Petersa427a2b2001-10-29 22:25:45 +00002569 SETBUILTIN("dict", &PyDict_Type);
Tim Peters67d687a2002-04-29 21:27:32 +00002570 SETBUILTIN("enumerate", &PyEnum_Type);
Neal Norwitzc4edb0e2006-05-02 04:43:14 +00002571 SETBUILTIN("file", &PyFile_Type);
Tim Peters4b7625e2001-09-13 21:37:17 +00002572 SETBUILTIN("float", &PyFloat_Type);
Raymond Hettingera690a992003-11-16 16:17:49 +00002573 SETBUILTIN("frozenset", &PyFrozenSet_Type);
Tim Peters4b7625e2001-09-13 21:37:17 +00002574 SETBUILTIN("property", &PyProperty_Type);
2575 SETBUILTIN("int", &PyInt_Type);
2576 SETBUILTIN("list", &PyList_Type);
2577 SETBUILTIN("long", &PyLong_Type);
2578 SETBUILTIN("object", &PyBaseObject_Type);
Raymond Hettinger85c20a42003-11-06 14:06:48 +00002579 SETBUILTIN("reversed", &PyReversed_Type);
Raymond Hettingera690a992003-11-16 16:17:49 +00002580 SETBUILTIN("set", &PySet_Type);
Guido van Rossumbea18cc2002-06-14 20:41:17 +00002581 SETBUILTIN("slice", &PySlice_Type);
Tim Peters4b7625e2001-09-13 21:37:17 +00002582 SETBUILTIN("staticmethod", &PyStaticMethod_Type);
2583 SETBUILTIN("str", &PyString_Type);
2584 SETBUILTIN("super", &PySuper_Type);
2585 SETBUILTIN("tuple", &PyTuple_Type);
2586 SETBUILTIN("type", &PyType_Type);
Raymond Hettingerc4c453f2002-06-05 23:12:45 +00002587 SETBUILTIN("xrange", &PyRange_Type);
Martin v. Löwis339d0f72001-08-17 18:39:25 +00002588#ifdef Py_USING_UNICODE
Tim Peters4b7625e2001-09-13 21:37:17 +00002589 SETBUILTIN("unicode", &PyUnicode_Type);
Martin v. Löwis339d0f72001-08-17 18:39:25 +00002590#endif
Guido van Rossum77f6a652002-04-03 22:41:51 +00002591 debug = PyBool_FromLong(Py_OptimizeFlag == 0);
Fred Drake5550de32000-06-20 04:54:19 +00002592 if (PyDict_SetItemString(dict, "__debug__", debug) < 0) {
2593 Py_XDECREF(debug);
Guido van Rossum25ce5661997-08-02 03:10:38 +00002594 return NULL;
Fred Drake5550de32000-06-20 04:54:19 +00002595 }
2596 Py_XDECREF(debug);
Barry Warsaw757af0e1997-08-29 22:13:51 +00002597
Guido van Rossum25ce5661997-08-02 03:10:38 +00002598 return mod;
Tim Peters7571a0f2003-03-23 17:52:28 +00002599#undef ADD_TO_ALL
Tim Peters4b7625e2001-09-13 21:37:17 +00002600#undef SETBUILTIN
Guido van Rossum3f5da241990-12-20 15:06:42 +00002601}
2602
Guido van Rossume77a7571993-11-03 15:01:26 +00002603/* Helper for filter(): filter a tuple through a function */
Guido van Rossum12d12c51993-10-26 17:58:25 +00002604
Guido van Rossum79f25d91997-04-29 20:08:16 +00002605static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002606filtertuple(PyObject *func, PyObject *tuple)
Guido van Rossum12d12c51993-10-26 17:58:25 +00002607{
Guido van Rossum79f25d91997-04-29 20:08:16 +00002608 PyObject *result;
Martin v. Löwis18e16552006-02-15 17:27:45 +00002609 Py_ssize_t i, j;
2610 Py_ssize_t len = PyTuple_Size(tuple);
Guido van Rossum12d12c51993-10-26 17:58:25 +00002611
Guido van Rossumb7b45621995-08-04 04:07:45 +00002612 if (len == 0) {
Walter Dörwaldc3da83f2003-02-04 20:24:45 +00002613 if (PyTuple_CheckExact(tuple))
2614 Py_INCREF(tuple);
2615 else
2616 tuple = PyTuple_New(0);
Guido van Rossumb7b45621995-08-04 04:07:45 +00002617 return tuple;
2618 }
2619
Guido van Rossum79f25d91997-04-29 20:08:16 +00002620 if ((result = PyTuple_New(len)) == NULL)
Guido van Rossum2586bf01993-11-01 16:21:44 +00002621 return NULL;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002622
Guido van Rossum12d12c51993-10-26 17:58:25 +00002623 for (i = j = 0; i < len; ++i) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002624 PyObject *item, *good;
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002625 int ok;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002626
Walter Dörwald8dd19322003-02-10 17:36:40 +00002627 if (tuple->ob_type->tp_as_sequence &&
2628 tuple->ob_type->tp_as_sequence->sq_item) {
2629 item = tuple->ob_type->tp_as_sequence->sq_item(tuple, i);
Walter Dörwaldc58a3a12003-08-18 18:28:45 +00002630 if (item == NULL)
2631 goto Fail_1;
Walter Dörwald8dd19322003-02-10 17:36:40 +00002632 } else {
Alex Martellia9b9c9f2003-04-23 13:34:35 +00002633 PyErr_SetString(PyExc_TypeError, "filter(): unsubscriptable tuple");
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002634 goto Fail_1;
Walter Dörwald8dd19322003-02-10 17:36:40 +00002635 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002636 if (func == Py_None) {
2637 Py_INCREF(item);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002638 good = item;
2639 }
2640 else {
Raymond Hettinger8ae46892003-10-12 19:09:37 +00002641 PyObject *arg = PyTuple_Pack(1, item);
Walter Dörwaldc58a3a12003-08-18 18:28:45 +00002642 if (arg == NULL) {
2643 Py_DECREF(item);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002644 goto Fail_1;
Walter Dörwaldc58a3a12003-08-18 18:28:45 +00002645 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002646 good = PyEval_CallObject(func, arg);
2647 Py_DECREF(arg);
Walter Dörwaldc58a3a12003-08-18 18:28:45 +00002648 if (good == NULL) {
2649 Py_DECREF(item);
Guido van Rossum12d12c51993-10-26 17:58:25 +00002650 goto Fail_1;
Walter Dörwaldc58a3a12003-08-18 18:28:45 +00002651 }
Guido van Rossum12d12c51993-10-26 17:58:25 +00002652 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002653 ok = PyObject_IsTrue(good);
2654 Py_DECREF(good);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002655 if (ok) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002656 if (PyTuple_SetItem(result, j++, item) < 0)
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002657 goto Fail_1;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002658 }
Walter Dörwaldc58a3a12003-08-18 18:28:45 +00002659 else
2660 Py_DECREF(item);
Guido van Rossum12d12c51993-10-26 17:58:25 +00002661 }
2662
Tim Peters4324aa32001-05-28 22:30:08 +00002663 if (_PyTuple_Resize(&result, j) < 0)
Guido van Rossum12d12c51993-10-26 17:58:25 +00002664 return NULL;
2665
Guido van Rossum12d12c51993-10-26 17:58:25 +00002666 return result;
2667
Guido van Rossum12d12c51993-10-26 17:58:25 +00002668Fail_1:
Guido van Rossum79f25d91997-04-29 20:08:16 +00002669 Py_DECREF(result);
Guido van Rossum12d12c51993-10-26 17:58:25 +00002670 return NULL;
2671}
2672
2673
Guido van Rossume77a7571993-11-03 15:01:26 +00002674/* Helper for filter(): filter a string through a function */
Guido van Rossum12d12c51993-10-26 17:58:25 +00002675
Guido van Rossum79f25d91997-04-29 20:08:16 +00002676static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002677filterstring(PyObject *func, PyObject *strobj)
Guido van Rossum12d12c51993-10-26 17:58:25 +00002678{
Guido van Rossum79f25d91997-04-29 20:08:16 +00002679 PyObject *result;
Martin v. Löwis18e16552006-02-15 17:27:45 +00002680 Py_ssize_t i, j;
2681 Py_ssize_t len = PyString_Size(strobj);
2682 Py_ssize_t outlen = len;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002683
Guido van Rossum79f25d91997-04-29 20:08:16 +00002684 if (func == Py_None) {
Walter Dörwald1918f772003-02-10 13:19:13 +00002685 /* If it's a real string we can return the original,
2686 * as no character is ever false and __getitem__
2687 * does return this character. If it's a subclass
2688 * we must go through the __getitem__ loop */
2689 if (PyString_CheckExact(strobj)) {
Walter Dörwaldc3da83f2003-02-04 20:24:45 +00002690 Py_INCREF(strobj);
Walter Dörwald1918f772003-02-10 13:19:13 +00002691 return strobj;
2692 }
Guido van Rossum12d12c51993-10-26 17:58:25 +00002693 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002694 if ((result = PyString_FromStringAndSize(NULL, len)) == NULL)
Guido van Rossum2586bf01993-11-01 16:21:44 +00002695 return NULL;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002696
Guido van Rossum12d12c51993-10-26 17:58:25 +00002697 for (i = j = 0; i < len; ++i) {
Walter Dörwald1918f772003-02-10 13:19:13 +00002698 PyObject *item;
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002699 int ok;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002700
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002701 item = (*strobj->ob_type->tp_as_sequence->sq_item)(strobj, i);
2702 if (item == NULL)
2703 goto Fail_1;
Walter Dörwald1918f772003-02-10 13:19:13 +00002704 if (func==Py_None) {
2705 ok = 1;
2706 } else {
2707 PyObject *arg, *good;
Raymond Hettinger8ae46892003-10-12 19:09:37 +00002708 arg = PyTuple_Pack(1, item);
Walter Dörwald1918f772003-02-10 13:19:13 +00002709 if (arg == NULL) {
2710 Py_DECREF(item);
2711 goto Fail_1;
2712 }
2713 good = PyEval_CallObject(func, arg);
2714 Py_DECREF(arg);
2715 if (good == NULL) {
2716 Py_DECREF(item);
2717 goto Fail_1;
2718 }
2719 ok = PyObject_IsTrue(good);
2720 Py_DECREF(good);
Tim Peters388ed082001-04-07 20:34:48 +00002721 }
Walter Dörwald903f1e02003-02-04 16:28:00 +00002722 if (ok) {
Martin v. Löwisd96ee902006-02-16 14:37:16 +00002723 Py_ssize_t reslen;
Walter Dörwald903f1e02003-02-04 16:28:00 +00002724 if (!PyString_Check(item)) {
2725 PyErr_SetString(PyExc_TypeError, "can't filter str to str:"
2726 " __getitem__ returned different type");
2727 Py_DECREF(item);
2728 goto Fail_1;
2729 }
2730 reslen = PyString_GET_SIZE(item);
2731 if (reslen == 1) {
2732 PyString_AS_STRING(result)[j++] =
2733 PyString_AS_STRING(item)[0];
2734 } else {
2735 /* do we need more space? */
Martin v. Löwisd96ee902006-02-16 14:37:16 +00002736 Py_ssize_t need = j + reslen + len-i-1;
Walter Dörwald903f1e02003-02-04 16:28:00 +00002737 if (need > outlen) {
2738 /* overallocate, to avoid reallocations */
2739 if (need<2*outlen)
2740 need = 2*outlen;
2741 if (_PyString_Resize(&result, need)) {
2742 Py_DECREF(item);
2743 return NULL;
2744 }
2745 outlen = need;
2746 }
2747 memcpy(
2748 PyString_AS_STRING(result) + j,
2749 PyString_AS_STRING(item),
2750 reslen
2751 );
2752 j += reslen;
2753 }
2754 }
Tim Peters388ed082001-04-07 20:34:48 +00002755 Py_DECREF(item);
Guido van Rossum12d12c51993-10-26 17:58:25 +00002756 }
2757
Walter Dörwald903f1e02003-02-04 16:28:00 +00002758 if (j < outlen)
Tim Peters5de98422002-04-27 18:44:32 +00002759 _PyString_Resize(&result, j);
Guido van Rossum12d12c51993-10-26 17:58:25 +00002760
Guido van Rossum12d12c51993-10-26 17:58:25 +00002761 return result;
2762
Guido van Rossum12d12c51993-10-26 17:58:25 +00002763Fail_1:
Guido van Rossum79f25d91997-04-29 20:08:16 +00002764 Py_DECREF(result);
Guido van Rossum12d12c51993-10-26 17:58:25 +00002765 return NULL;
2766}
Martin v. Löwis8afd7572003-01-25 22:46:11 +00002767
2768#ifdef Py_USING_UNICODE
2769/* Helper for filter(): filter a Unicode object through a function */
2770
2771static PyObject *
2772filterunicode(PyObject *func, PyObject *strobj)
2773{
2774 PyObject *result;
Martin v. Löwis725507b2006-03-07 12:08:51 +00002775 register Py_ssize_t i, j;
Martin v. Löwisd96ee902006-02-16 14:37:16 +00002776 Py_ssize_t len = PyUnicode_GetSize(strobj);
2777 Py_ssize_t outlen = len;
Martin v. Löwis8afd7572003-01-25 22:46:11 +00002778
2779 if (func == Py_None) {
Walter Dörwald1918f772003-02-10 13:19:13 +00002780 /* If it's a real string we can return the original,
2781 * as no character is ever false and __getitem__
2782 * does return this character. If it's a subclass
2783 * we must go through the __getitem__ loop */
2784 if (PyUnicode_CheckExact(strobj)) {
Walter Dörwaldc3da83f2003-02-04 20:24:45 +00002785 Py_INCREF(strobj);
Walter Dörwald1918f772003-02-10 13:19:13 +00002786 return strobj;
2787 }
Martin v. Löwis8afd7572003-01-25 22:46:11 +00002788 }
2789 if ((result = PyUnicode_FromUnicode(NULL, len)) == NULL)
2790 return NULL;
2791
2792 for (i = j = 0; i < len; ++i) {
2793 PyObject *item, *arg, *good;
2794 int ok;
2795
2796 item = (*strobj->ob_type->tp_as_sequence->sq_item)(strobj, i);
2797 if (item == NULL)
2798 goto Fail_1;
Walter Dörwald1918f772003-02-10 13:19:13 +00002799 if (func == Py_None) {
2800 ok = 1;
2801 } else {
Raymond Hettinger8ae46892003-10-12 19:09:37 +00002802 arg = PyTuple_Pack(1, item);
Walter Dörwald1918f772003-02-10 13:19:13 +00002803 if (arg == NULL) {
2804 Py_DECREF(item);
2805 goto Fail_1;
2806 }
2807 good = PyEval_CallObject(func, arg);
2808 Py_DECREF(arg);
2809 if (good == NULL) {
2810 Py_DECREF(item);
2811 goto Fail_1;
2812 }
2813 ok = PyObject_IsTrue(good);
2814 Py_DECREF(good);
Martin v. Löwis8afd7572003-01-25 22:46:11 +00002815 }
Walter Dörwald903f1e02003-02-04 16:28:00 +00002816 if (ok) {
Martin v. Löwisd96ee902006-02-16 14:37:16 +00002817 Py_ssize_t reslen;
Walter Dörwald903f1e02003-02-04 16:28:00 +00002818 if (!PyUnicode_Check(item)) {
Georg Brandl99d7e4e2005-08-31 22:21:15 +00002819 PyErr_SetString(PyExc_TypeError,
Jeremy Hylton1aad9c72003-09-16 03:10:59 +00002820 "can't filter unicode to unicode:"
2821 " __getitem__ returned different type");
Walter Dörwald903f1e02003-02-04 16:28:00 +00002822 Py_DECREF(item);
2823 goto Fail_1;
2824 }
2825 reslen = PyUnicode_GET_SIZE(item);
Georg Brandl99d7e4e2005-08-31 22:21:15 +00002826 if (reslen == 1)
Walter Dörwald903f1e02003-02-04 16:28:00 +00002827 PyUnicode_AS_UNICODE(result)[j++] =
2828 PyUnicode_AS_UNICODE(item)[0];
Jeremy Hylton1aad9c72003-09-16 03:10:59 +00002829 else {
Walter Dörwald903f1e02003-02-04 16:28:00 +00002830 /* do we need more space? */
Martin v. Löwisd96ee902006-02-16 14:37:16 +00002831 Py_ssize_t need = j + reslen + len - i - 1;
Walter Dörwald903f1e02003-02-04 16:28:00 +00002832 if (need > outlen) {
Georg Brandl99d7e4e2005-08-31 22:21:15 +00002833 /* overallocate,
Jeremy Hylton1aad9c72003-09-16 03:10:59 +00002834 to avoid reallocations */
2835 if (need < 2 * outlen)
2836 need = 2 * outlen;
Jeremy Hylton364f6be2003-09-16 03:17:16 +00002837 if (PyUnicode_Resize(
2838 &result, need) < 0) {
Walter Dörwald903f1e02003-02-04 16:28:00 +00002839 Py_DECREF(item);
Walter Dörwald531e0002003-02-04 16:57:49 +00002840 goto Fail_1;
Walter Dörwald903f1e02003-02-04 16:28:00 +00002841 }
2842 outlen = need;
2843 }
Jeremy Hylton1aad9c72003-09-16 03:10:59 +00002844 memcpy(PyUnicode_AS_UNICODE(result) + j,
2845 PyUnicode_AS_UNICODE(item),
2846 reslen*sizeof(Py_UNICODE));
Walter Dörwald903f1e02003-02-04 16:28:00 +00002847 j += reslen;
2848 }
2849 }
Martin v. Löwis8afd7572003-01-25 22:46:11 +00002850 Py_DECREF(item);
2851 }
2852
Walter Dörwald903f1e02003-02-04 16:28:00 +00002853 if (j < outlen)
Martin v. Löwis8afd7572003-01-25 22:46:11 +00002854 PyUnicode_Resize(&result, j);
2855
2856 return result;
2857
2858Fail_1:
2859 Py_DECREF(result);
2860 return NULL;
2861}
2862#endif