blob: c760dcb3cbb649e6fc70387dc0fa7a34c3a030df [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 }
Martin v. Löwis339d0f72001-08-17 18:39:25 +00001440#ifdef Py_USING_UNICODE
Jeremy Hylton394b54d2000-04-12 21:19:47 +00001441 } else if (PyUnicode_Check(obj)) {
1442 size = PyUnicode_GET_SIZE(obj);
Andrew M. Kuchling34c20cf2000-12-20 14:36:56 +00001443 if (size == 1) {
Jeremy Hylton394b54d2000-04-12 21:19:47 +00001444 ord = (long)*PyUnicode_AS_UNICODE(obj);
Andrew M. Kuchling34c20cf2000-12-20 14:36:56 +00001445 return PyInt_FromLong(ord);
1446 }
Martin v. Löwis339d0f72001-08-17 18:39:25 +00001447#endif
Jeremy Hylton394b54d2000-04-12 21:19:47 +00001448 } else {
1449 PyErr_Format(PyExc_TypeError,
Andrew M. Kuchlingf07aad12000-12-23 14:11:28 +00001450 "ord() expected string of length 1, but " \
Jeremy Hylton394b54d2000-04-12 21:19:47 +00001451 "%.200s found", obj->ob_type->tp_name);
Guido van Rossum09095f32000-03-10 23:00:52 +00001452 return NULL;
1453 }
1454
Guido van Rossumad991772001-01-12 16:03:05 +00001455 PyErr_Format(PyExc_TypeError,
Andrew M. Kuchlingf07aad12000-12-23 14:11:28 +00001456 "ord() expected a character, "
Martin v. Löwisd96ee902006-02-16 14:37:16 +00001457 "but string of length %zd found",
Jeremy Hylton394b54d2000-04-12 21:19:47 +00001458 size);
1459 return NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001460}
1461
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001462PyDoc_STRVAR(ord_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001463"ord(c) -> integer\n\
1464\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001465Return the integer ordinal of a one-character string.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001466
1467
Guido van Rossum79f25d91997-04-29 20:08:16 +00001468static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001469builtin_pow(PyObject *self, PyObject *args)
Guido van Rossum6a00cd81995-01-07 12:39:01 +00001470{
Guido van Rossum09df08a1998-05-22 00:51:39 +00001471 PyObject *v, *w, *z = Py_None;
Guido van Rossum6a00cd81995-01-07 12:39:01 +00001472
Raymond Hettingerea3fdf42002-12-29 16:33:45 +00001473 if (!PyArg_UnpackTuple(args, "pow", 2, 3, &v, &w, &z))
Guido van Rossum6a00cd81995-01-07 12:39:01 +00001474 return NULL;
Guido van Rossum09df08a1998-05-22 00:51:39 +00001475 return PyNumber_Power(v, w, z);
Guido van Rossumd4905451991-05-05 20:00:36 +00001476}
1477
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001478PyDoc_STRVAR(pow_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001479"pow(x, y[, z]) -> number\n\
1480\n\
1481With two arguments, equivalent to x**y. With three arguments,\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001482equivalent to (x**y) % z, but may be more efficient (e.g. for longs).");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001483
1484
Eric Smith7c478942008-03-18 23:45:49 +00001485static PyObject *
1486builtin_print(PyObject *self, PyObject *args, PyObject *kwds)
1487{
1488 static char *kwlist[] = {"sep", "end", "file", 0};
1489 static PyObject *dummy_args;
1490 PyObject *sep = NULL, *end = NULL, *file = NULL;
1491 int i, err;
1492
1493 if (dummy_args == NULL) {
1494 if (!(dummy_args = PyTuple_New(0)))
1495 return NULL;
1496 }
1497 if (!PyArg_ParseTupleAndKeywords(dummy_args, kwds, "|OOO:print",
1498 kwlist, &sep, &end, &file))
1499 return NULL;
1500 if (file == NULL || file == Py_None) {
1501 file = PySys_GetObject("stdout");
1502 /* sys.stdout may be None when FILE* stdout isn't connected */
1503 if (file == Py_None)
1504 Py_RETURN_NONE;
1505 }
1506
1507 if (sep && sep != Py_None && !PyString_Check(sep) &&
1508 !PyUnicode_Check(sep)) {
1509 PyErr_Format(PyExc_TypeError,
1510 "sep must be None, str or unicode, not %.200s",
1511 sep->ob_type->tp_name);
1512 return NULL;
1513 }
1514 if (end && end != Py_None && !PyString_Check(end) &&
1515 !PyUnicode_Check(end)) {
1516 PyErr_Format(PyExc_TypeError,
1517 "end must be None, str or unicode, not %.200s",
1518 end->ob_type->tp_name);
1519 return NULL;
1520 }
1521
1522 for (i = 0; i < PyTuple_Size(args); i++) {
1523 if (i > 0) {
1524 if (sep == NULL || sep == Py_None)
1525 err = PyFile_WriteString(" ", file);
1526 else
1527 err = PyFile_WriteObject(sep, file,
1528 Py_PRINT_RAW);
1529 if (err)
1530 return NULL;
1531 }
1532 err = PyFile_WriteObject(PyTuple_GetItem(args, i), file,
1533 Py_PRINT_RAW);
1534 if (err)
1535 return NULL;
1536 }
1537
1538 if (end == NULL || end == Py_None)
1539 err = PyFile_WriteString("\n", file);
1540 else
1541 err = PyFile_WriteObject(end, file, Py_PRINT_RAW);
1542 if (err)
1543 return NULL;
1544
1545 Py_RETURN_NONE;
1546}
1547
1548PyDoc_STRVAR(print_doc,
1549"print(value, ..., sep=' ', end='\\n', file=sys.stdout)\n\
1550\n\
1551Prints the values to a stream, or to sys.stdout by default.\n\
1552Optional keyword arguments:\n\
1553file: a file-like object (stream); defaults to the current sys.stdout.\n\
1554sep: string inserted between values, default a space.\n\
1555end: string appended after the last value, default a newline.");
1556
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001557
1558/* Return number of items in range (lo, hi, step), when arguments are
1559 * PyInt or PyLong objects. step > 0 required. Return a value < 0 if
1560 * & only if the true value is too large to fit in a signed long.
1561 * Arguments MUST return 1 with either PyInt_Check() or
1562 * PyLong_Check(). Return -1 when there is an error.
1563 */
1564static long
1565get_len_of_range_longs(PyObject *lo, PyObject *hi, PyObject *step)
1566{
1567 /* -------------------------------------------------------------
1568 Algorithm is equal to that of get_len_of_range(), but it operates
1569 on PyObjects (which are assumed to be PyLong or PyInt objects).
1570 ---------------------------------------------------------------*/
1571 long n;
1572 PyObject *diff = NULL;
1573 PyObject *one = NULL;
1574 PyObject *tmp1 = NULL, *tmp2 = NULL, *tmp3 = NULL;
1575 /* holds sub-expression evaluations */
1576
1577 /* if (lo >= hi), return length of 0. */
1578 if (PyObject_Compare(lo, hi) >= 0)
1579 return 0;
1580
1581 if ((one = PyLong_FromLong(1L)) == NULL)
1582 goto Fail;
1583
1584 if ((tmp1 = PyNumber_Subtract(hi, lo)) == NULL)
1585 goto Fail;
1586
1587 if ((diff = PyNumber_Subtract(tmp1, one)) == NULL)
1588 goto Fail;
1589
1590 if ((tmp2 = PyNumber_FloorDivide(diff, step)) == NULL)
1591 goto Fail;
1592
1593 if ((tmp3 = PyNumber_Add(tmp2, one)) == NULL)
1594 goto Fail;
1595
1596 n = PyLong_AsLong(tmp3);
1597 if (PyErr_Occurred()) { /* Check for Overflow */
1598 PyErr_Clear();
1599 goto Fail;
1600 }
1601
1602 Py_DECREF(tmp3);
1603 Py_DECREF(tmp2);
1604 Py_DECREF(diff);
1605 Py_DECREF(tmp1);
1606 Py_DECREF(one);
1607 return n;
1608
1609 Fail:
1610 Py_XDECREF(tmp3);
1611 Py_XDECREF(tmp2);
1612 Py_XDECREF(diff);
1613 Py_XDECREF(tmp1);
1614 Py_XDECREF(one);
1615 return -1;
1616}
1617
1618/* An extension of builtin_range() that handles the case when PyLong
1619 * arguments are given. */
1620static PyObject *
1621handle_range_longs(PyObject *self, PyObject *args)
1622{
1623 PyObject *ilow;
Tim Peters874e1f72003-04-13 22:13:08 +00001624 PyObject *ihigh = NULL;
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001625 PyObject *istep = NULL;
Tim Peters874e1f72003-04-13 22:13:08 +00001626
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001627 PyObject *curnum = NULL;
1628 PyObject *v = NULL;
1629 long bign;
1630 int i, n;
1631 int cmp_result;
1632
Tim Peters874e1f72003-04-13 22:13:08 +00001633 PyObject *zero = PyLong_FromLong(0);
1634
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001635 if (zero == NULL)
1636 return NULL;
1637
Tim Peters874e1f72003-04-13 22:13:08 +00001638 if (!PyArg_UnpackTuple(args, "range", 1, 3, &ilow, &ihigh, &istep)) {
1639 Py_DECREF(zero);
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001640 return NULL;
1641 }
1642
Tim Peters874e1f72003-04-13 22:13:08 +00001643 /* Figure out which way we were called, supply defaults, and be
1644 * sure to incref everything so that the decrefs at the end
1645 * are correct.
1646 */
1647 assert(ilow != NULL);
1648 if (ihigh == NULL) {
1649 /* only 1 arg -- it's the upper limit */
1650 ihigh = ilow;
1651 ilow = NULL;
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001652 }
Tim Peters874e1f72003-04-13 22:13:08 +00001653 assert(ihigh != NULL);
1654 Py_INCREF(ihigh);
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001655
Tim Peters874e1f72003-04-13 22:13:08 +00001656 /* ihigh correct now; do ilow */
1657 if (ilow == NULL)
1658 ilow = zero;
1659 Py_INCREF(ilow);
1660
1661 /* ilow and ihigh correct now; do istep */
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001662 if (istep == NULL) {
1663 istep = PyLong_FromLong(1L);
1664 if (istep == NULL)
1665 goto Fail;
1666 }
1667 else {
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001668 Py_INCREF(istep);
1669 }
1670
Tim Peters874e1f72003-04-13 22:13:08 +00001671 if (!PyInt_Check(ilow) && !PyLong_Check(ilow)) {
Guido van Rossum28e83e32003-04-15 12:43:26 +00001672 PyErr_Format(PyExc_TypeError,
Alex Martellif471d472003-04-23 13:00:44 +00001673 "range() integer start argument expected, got %s.",
Guido van Rossum817d6c92003-04-14 18:25:04 +00001674 ilow->ob_type->tp_name);
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001675 goto Fail;
1676 }
1677
Tim Peters874e1f72003-04-13 22:13:08 +00001678 if (!PyInt_Check(ihigh) && !PyLong_Check(ihigh)) {
Guido van Rossum28e83e32003-04-15 12:43:26 +00001679 PyErr_Format(PyExc_TypeError,
Alex Martellif471d472003-04-23 13:00:44 +00001680 "range() integer end argument expected, got %s.",
Guido van Rossum817d6c92003-04-14 18:25:04 +00001681 ihigh->ob_type->tp_name);
Tim Peters874e1f72003-04-13 22:13:08 +00001682 goto Fail;
1683 }
1684
1685 if (!PyInt_Check(istep) && !PyLong_Check(istep)) {
Guido van Rossum28e83e32003-04-15 12:43:26 +00001686 PyErr_Format(PyExc_TypeError,
Alex Martellif471d472003-04-23 13:00:44 +00001687 "range() integer step argument expected, got %s.",
Guido van Rossum817d6c92003-04-14 18:25:04 +00001688 istep->ob_type->tp_name);
Tim Peters874e1f72003-04-13 22:13:08 +00001689 goto Fail;
1690 }
1691
1692 if (PyObject_Cmp(istep, zero, &cmp_result) == -1)
1693 goto Fail;
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001694 if (cmp_result == 0) {
1695 PyErr_SetString(PyExc_ValueError,
Alex Martellif471d472003-04-23 13:00:44 +00001696 "range() step argument must not be zero");
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001697 goto Fail;
1698 }
1699
1700 if (cmp_result > 0)
1701 bign = get_len_of_range_longs(ilow, ihigh, istep);
1702 else {
1703 PyObject *neg_istep = PyNumber_Negative(istep);
1704 if (neg_istep == NULL)
1705 goto Fail;
1706 bign = get_len_of_range_longs(ihigh, ilow, neg_istep);
1707 Py_DECREF(neg_istep);
1708 }
1709
1710 n = (int)bign;
1711 if (bign < 0 || (long)n != bign) {
1712 PyErr_SetString(PyExc_OverflowError,
1713 "range() result has too many items");
1714 goto Fail;
1715 }
1716
1717 v = PyList_New(n);
1718 if (v == NULL)
1719 goto Fail;
1720
1721 curnum = ilow;
1722 Py_INCREF(curnum);
1723
1724 for (i = 0; i < n; i++) {
1725 PyObject *w = PyNumber_Long(curnum);
1726 PyObject *tmp_num;
1727 if (w == NULL)
1728 goto Fail;
1729
1730 PyList_SET_ITEM(v, i, w);
1731
1732 tmp_num = PyNumber_Add(curnum, istep);
1733 if (tmp_num == NULL)
1734 goto Fail;
1735
1736 Py_DECREF(curnum);
1737 curnum = tmp_num;
1738 }
Tim Peters874e1f72003-04-13 22:13:08 +00001739 Py_DECREF(ilow);
1740 Py_DECREF(ihigh);
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001741 Py_DECREF(istep);
1742 Py_DECREF(zero);
Tim Peters874e1f72003-04-13 22:13:08 +00001743 Py_DECREF(curnum);
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001744 return v;
1745
1746 Fail:
Tim Peters874e1f72003-04-13 22:13:08 +00001747 Py_DECREF(ilow);
1748 Py_DECREF(ihigh);
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001749 Py_XDECREF(istep);
Tim Peters874e1f72003-04-13 22:13:08 +00001750 Py_DECREF(zero);
1751 Py_XDECREF(curnum);
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001752 Py_XDECREF(v);
1753 return NULL;
1754}
1755
Guido van Rossum124eff01999-02-23 16:11:01 +00001756/* Return number of items in range/xrange (lo, hi, step). step > 0
1757 * required. Return a value < 0 if & only if the true value is too
1758 * large to fit in a signed long.
1759 */
1760static long
Thomas Wouterse28c2962000-07-23 22:21:32 +00001761get_len_of_range(long lo, long hi, long step)
Guido van Rossum124eff01999-02-23 16:11:01 +00001762{
1763 /* -------------------------------------------------------------
1764 If lo >= hi, the range is empty.
1765 Else if n values are in the range, the last one is
1766 lo + (n-1)*step, which must be <= hi-1. Rearranging,
1767 n <= (hi - lo - 1)/step + 1, so taking the floor of the RHS gives
1768 the proper value. Since lo < hi in this case, hi-lo-1 >= 0, so
1769 the RHS is non-negative and so truncation is the same as the
1770 floor. Letting M be the largest positive long, the worst case
1771 for the RHS numerator is hi=M, lo=-M-1, and then
1772 hi-lo-1 = M-(-M-1)-1 = 2*M. Therefore unsigned long has enough
1773 precision to compute the RHS exactly.
1774 ---------------------------------------------------------------*/
1775 long n = 0;
1776 if (lo < hi) {
1777 unsigned long uhi = (unsigned long)hi;
1778 unsigned long ulo = (unsigned long)lo;
1779 unsigned long diff = uhi - ulo - 1;
1780 n = (long)(diff / (unsigned long)step + 1);
1781 }
1782 return n;
1783}
1784
Guido van Rossum79f25d91997-04-29 20:08:16 +00001785static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001786builtin_range(PyObject *self, PyObject *args)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001787{
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001788 long ilow = 0, ihigh = 0, istep = 1;
Guido van Rossum124eff01999-02-23 16:11:01 +00001789 long bign;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001790 int i, n;
Guido van Rossum124eff01999-02-23 16:11:01 +00001791
Guido van Rossum79f25d91997-04-29 20:08:16 +00001792 PyObject *v;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001793
Guido van Rossum79f25d91997-04-29 20:08:16 +00001794 if (PyTuple_Size(args) <= 1) {
1795 if (!PyArg_ParseTuple(args,
Guido van Rossum0865dd91995-01-17 16:30:22 +00001796 "l;range() requires 1-3 int arguments",
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001797 &ihigh)) {
1798 PyErr_Clear();
1799 return handle_range_longs(self, args);
1800 }
Guido van Rossum3f5da241990-12-20 15:06:42 +00001801 }
1802 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001803 if (!PyArg_ParseTuple(args,
Guido van Rossum0865dd91995-01-17 16:30:22 +00001804 "ll|l;range() requires 1-3 int arguments",
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001805 &ilow, &ihigh, &istep)) {
1806 PyErr_Clear();
1807 return handle_range_longs(self, args);
1808 }
Guido van Rossum3f5da241990-12-20 15:06:42 +00001809 }
1810 if (istep == 0) {
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001811 PyErr_SetString(PyExc_ValueError,
Alex Martellif471d472003-04-23 13:00:44 +00001812 "range() step argument must not be zero");
Guido van Rossum3f5da241990-12-20 15:06:42 +00001813 return NULL;
1814 }
Guido van Rossum124eff01999-02-23 16:11:01 +00001815 if (istep > 0)
1816 bign = get_len_of_range(ilow, ihigh, istep);
1817 else
1818 bign = get_len_of_range(ihigh, ilow, -istep);
1819 n = (int)bign;
1820 if (bign < 0 || (long)n != bign) {
Guido van Rossume23cde21999-01-12 05:07:47 +00001821 PyErr_SetString(PyExc_OverflowError,
Fred Drake661ea262000-10-24 19:57:45 +00001822 "range() result has too many items");
Guido van Rossume23cde21999-01-12 05:07:47 +00001823 return NULL;
1824 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00001825 v = PyList_New(n);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001826 if (v == NULL)
1827 return NULL;
1828 for (i = 0; i < n; i++) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001829 PyObject *w = PyInt_FromLong(ilow);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001830 if (w == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001831 Py_DECREF(v);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001832 return NULL;
1833 }
Guido van Rossuma937d141998-04-24 18:22:02 +00001834 PyList_SET_ITEM(v, i, w);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001835 ilow += istep;
1836 }
1837 return v;
1838}
1839
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001840PyDoc_STRVAR(range_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001841"range([start,] stop[, step]) -> list of integers\n\
1842\n\
1843Return a list containing an arithmetic progression of integers.\n\
1844range(i, j) returns [i, i+1, i+2, ..., j-1]; start (!) defaults to 0.\n\
1845When step is given, it specifies the increment (or decrement).\n\
1846For example, range(4) returns [0, 1, 2, 3]. The end point is omitted!\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001847These are exactly the valid indices for a list of 4 elements.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001848
1849
Guido van Rossum79f25d91997-04-29 20:08:16 +00001850static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001851builtin_raw_input(PyObject *self, PyObject *args)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001852{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001853 PyObject *v = NULL;
Martin v. Löwis31d2df52002-08-14 15:46:02 +00001854 PyObject *fin = PySys_GetObject("stdin");
1855 PyObject *fout = PySys_GetObject("stdout");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001856
Raymond Hettingerea3fdf42002-12-29 16:33:45 +00001857 if (!PyArg_UnpackTuple(args, "[raw_]input", 0, 1, &v))
Guido van Rossum3165fe61992-09-25 21:59:05 +00001858 return NULL;
Martin v. Löwis31d2df52002-08-14 15:46:02 +00001859
1860 if (fin == NULL) {
Alex Martellia9b9c9f2003-04-23 13:34:35 +00001861 PyErr_SetString(PyExc_RuntimeError, "[raw_]input: lost sys.stdin");
Martin v. Löwis31d2df52002-08-14 15:46:02 +00001862 return NULL;
1863 }
1864 if (fout == NULL) {
Alex Martellia9b9c9f2003-04-23 13:34:35 +00001865 PyErr_SetString(PyExc_RuntimeError, "[raw_]input: lost sys.stdout");
Martin v. Löwis31d2df52002-08-14 15:46:02 +00001866 return NULL;
1867 }
1868 if (PyFile_SoftSpace(fout, 0)) {
1869 if (PyFile_WriteString(" ", fout) != 0)
1870 return NULL;
1871 }
Georg Brandl7e3ba2a2006-08-06 08:23:54 +00001872 if (PyFile_AsFile(fin) && PyFile_AsFile(fout)
Martin v. Löwis566f6af2002-10-26 14:39:10 +00001873 && isatty(fileno(PyFile_AsFile(fin)))
1874 && isatty(fileno(PyFile_AsFile(fout)))) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001875 PyObject *po;
Guido van Rossum872537c1995-07-07 22:43:42 +00001876 char *prompt;
1877 char *s;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001878 PyObject *result;
Guido van Rossum872537c1995-07-07 22:43:42 +00001879 if (v != NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001880 po = PyObject_Str(v);
Guido van Rossum872537c1995-07-07 22:43:42 +00001881 if (po == NULL)
1882 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001883 prompt = PyString_AsString(po);
Guido van Rossumd9b52081998-06-26 18:25:38 +00001884 if (prompt == NULL)
1885 return NULL;
Guido van Rossum872537c1995-07-07 22:43:42 +00001886 }
1887 else {
1888 po = NULL;
1889 prompt = "";
1890 }
Michael W. Hudson52db5192004-08-02 13:21:09 +00001891 s = PyOS_Readline(PyFile_AsFile(fin), PyFile_AsFile(fout),
Martin v. Löwis566f6af2002-10-26 14:39:10 +00001892 prompt);
Guido van Rossum79f25d91997-04-29 20:08:16 +00001893 Py_XDECREF(po);
Guido van Rossum872537c1995-07-07 22:43:42 +00001894 if (s == NULL) {
Michael W. Hudson30ea2f22004-07-07 17:44:12 +00001895 if (!PyErr_Occurred())
1896 PyErr_SetNone(PyExc_KeyboardInterrupt);
Guido van Rossum872537c1995-07-07 22:43:42 +00001897 return NULL;
1898 }
1899 if (*s == '\0') {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001900 PyErr_SetNone(PyExc_EOFError);
Guido van Rossum872537c1995-07-07 22:43:42 +00001901 result = NULL;
1902 }
1903 else { /* strip trailing '\n' */
Guido van Rossum106f2da2000-06-28 21:12:25 +00001904 size_t len = strlen(s);
Martin v. Löwisb1ed7fa2006-04-13 07:52:27 +00001905 if (len > PY_SSIZE_T_MAX) {
Martin v. Löwis31d2df52002-08-14 15:46:02 +00001906 PyErr_SetString(PyExc_OverflowError,
Alex Martellia9b9c9f2003-04-23 13:34:35 +00001907 "[raw_]input: input too long");
Guido van Rossum106f2da2000-06-28 21:12:25 +00001908 result = NULL;
1909 }
1910 else {
Martin v. Löwisb1ed7fa2006-04-13 07:52:27 +00001911 result = PyString_FromStringAndSize(s, len-1);
Guido van Rossum106f2da2000-06-28 21:12:25 +00001912 }
Guido van Rossum872537c1995-07-07 22:43:42 +00001913 }
Guido van Rossumb18618d2000-05-03 23:44:39 +00001914 PyMem_FREE(s);
Guido van Rossum872537c1995-07-07 22:43:42 +00001915 return result;
1916 }
Guido van Rossum90933611991-06-07 16:10:43 +00001917 if (v != NULL) {
Martin v. Löwis31d2df52002-08-14 15:46:02 +00001918 if (PyFile_WriteObject(v, fout, Py_PRINT_RAW) != 0)
Guido van Rossum90933611991-06-07 16:10:43 +00001919 return NULL;
1920 }
Martin v. Löwis31d2df52002-08-14 15:46:02 +00001921 return PyFile_GetLine(fin, -1);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001922}
1923
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001924PyDoc_STRVAR(raw_input_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001925"raw_input([prompt]) -> string\n\
1926\n\
1927Read a string from standard input. The trailing newline is stripped.\n\
1928If the user hits EOF (Unix: Ctl-D, Windows: Ctl-Z+Return), raise EOFError.\n\
1929On Unix, GNU readline is used if enabled. The prompt string, if given,\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001930is printed without a trailing newline before reading.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001931
1932
Guido van Rossum79f25d91997-04-29 20:08:16 +00001933static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001934builtin_reduce(PyObject *self, PyObject *args)
Guido van Rossum12d12c51993-10-26 17:58:25 +00001935{
Tim Peters15d81ef2001-05-04 04:39:21 +00001936 PyObject *seq, *func, *result = NULL, *it;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001937
Neal Norwitzdf25efe2007-05-23 06:58:36 +00001938 if (Py_Py3kWarningFlag &&
1939 PyErr_Warn(PyExc_DeprecationWarning,
Georg Brandld5b635f2008-03-25 08:29:14 +00001940 "reduce() not supported in 3.x; "
1941 "use functools.reduce()") < 0)
Neal Norwitzdf25efe2007-05-23 06:58:36 +00001942 return NULL;
1943
Raymond Hettingerea3fdf42002-12-29 16:33:45 +00001944 if (!PyArg_UnpackTuple(args, "reduce", 2, 3, &func, &seq, &result))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001945 return NULL;
1946 if (result != NULL)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001947 Py_INCREF(result);
Guido van Rossum12d12c51993-10-26 17:58:25 +00001948
Tim Peters15d81ef2001-05-04 04:39:21 +00001949 it = PyObject_GetIter(seq);
1950 if (it == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001951 PyErr_SetString(PyExc_TypeError,
Tim Peters15d81ef2001-05-04 04:39:21 +00001952 "reduce() arg 2 must support iteration");
1953 Py_XDECREF(result);
Guido van Rossum12d12c51993-10-26 17:58:25 +00001954 return NULL;
1955 }
1956
Guido van Rossum79f25d91997-04-29 20:08:16 +00001957 if ((args = PyTuple_New(2)) == NULL)
Guido van Rossum2d951851994-08-29 12:52:16 +00001958 goto Fail;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001959
Tim Peters15d81ef2001-05-04 04:39:21 +00001960 for (;;) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001961 PyObject *op2;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001962
1963 if (args->ob_refcnt > 1) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001964 Py_DECREF(args);
1965 if ((args = PyTuple_New(2)) == NULL)
Guido van Rossum2d951851994-08-29 12:52:16 +00001966 goto Fail;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001967 }
1968
Tim Peters15d81ef2001-05-04 04:39:21 +00001969 op2 = PyIter_Next(it);
1970 if (op2 == NULL) {
Tim Petersf4848da2001-05-05 00:14:56 +00001971 if (PyErr_Occurred())
1972 goto Fail;
1973 break;
Guido van Rossum2d951851994-08-29 12:52:16 +00001974 }
Guido van Rossum12d12c51993-10-26 17:58:25 +00001975
Guido van Rossum2d951851994-08-29 12:52:16 +00001976 if (result == NULL)
1977 result = op2;
1978 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001979 PyTuple_SetItem(args, 0, result);
1980 PyTuple_SetItem(args, 1, op2);
1981 if ((result = PyEval_CallObject(func, args)) == NULL)
Guido van Rossum2d951851994-08-29 12:52:16 +00001982 goto Fail;
1983 }
Guido van Rossum12d12c51993-10-26 17:58:25 +00001984 }
1985
Guido van Rossum79f25d91997-04-29 20:08:16 +00001986 Py_DECREF(args);
Guido van Rossum12d12c51993-10-26 17:58:25 +00001987
Guido van Rossum2d951851994-08-29 12:52:16 +00001988 if (result == NULL)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001989 PyErr_SetString(PyExc_TypeError,
Fred Drake661ea262000-10-24 19:57:45 +00001990 "reduce() of empty sequence with no initial value");
Guido van Rossum2d951851994-08-29 12:52:16 +00001991
Tim Peters15d81ef2001-05-04 04:39:21 +00001992 Py_DECREF(it);
Guido van Rossum12d12c51993-10-26 17:58:25 +00001993 return result;
1994
Guido van Rossum2d951851994-08-29 12:52:16 +00001995Fail:
Guido van Rossum79f25d91997-04-29 20:08:16 +00001996 Py_XDECREF(args);
1997 Py_XDECREF(result);
Tim Peters15d81ef2001-05-04 04:39:21 +00001998 Py_DECREF(it);
Guido van Rossum12d12c51993-10-26 17:58:25 +00001999 return NULL;
2000}
2001
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002002PyDoc_STRVAR(reduce_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002003"reduce(function, sequence[, initial]) -> value\n\
2004\n\
2005Apply a function of two arguments cumulatively to the items of a sequence,\n\
2006from left to right, so as to reduce the sequence to a single value.\n\
2007For example, reduce(lambda x, y: x+y, [1, 2, 3, 4, 5]) calculates\n\
2008((((1+2)+3)+4)+5). If initial is present, it is placed before the items\n\
2009of the sequence in the calculation, and serves as a default when the\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002010sequence is empty.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002011
2012
Guido van Rossum79f25d91997-04-29 20:08:16 +00002013static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002014builtin_reload(PyObject *self, PyObject *v)
Guido van Rossum3f5da241990-12-20 15:06:42 +00002015{
Neal Norwitzdf25efe2007-05-23 06:58:36 +00002016 if (Py_Py3kWarningFlag &&
2017 PyErr_Warn(PyExc_DeprecationWarning,
Georg Brandld5b635f2008-03-25 08:29:14 +00002018 "reload() not supported in 3.x; use imp.reload()") < 0)
Neal Norwitzdf25efe2007-05-23 06:58:36 +00002019 return NULL;
2020
Guido van Rossum79f25d91997-04-29 20:08:16 +00002021 return PyImport_ReloadModule(v);
Guido van Rossum3f5da241990-12-20 15:06:42 +00002022}
2023
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002024PyDoc_STRVAR(reload_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002025"reload(module) -> module\n\
2026\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002027Reload the module. The module must have been successfully imported before.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002028
2029
Guido van Rossum79f25d91997-04-29 20:08:16 +00002030static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002031builtin_repr(PyObject *self, PyObject *v)
Guido van Rossumc89705d1992-11-26 08:54:07 +00002032{
Guido van Rossum79f25d91997-04-29 20:08:16 +00002033 return PyObject_Repr(v);
Guido van Rossumc89705d1992-11-26 08:54:07 +00002034}
2035
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002036PyDoc_STRVAR(repr_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002037"repr(object) -> string\n\
2038\n\
2039Return the canonical string representation of the object.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002040For most object types, eval(repr(object)) == object.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002041
2042
Guido van Rossum79f25d91997-04-29 20:08:16 +00002043static PyObject *
Georg Brandlccadf842006-03-31 18:54:53 +00002044builtin_round(PyObject *self, PyObject *args, PyObject *kwds)
Guido van Rossum9e51f9b1993-02-12 16:29:05 +00002045{
Jeffrey Yasskin9871d8f2008-01-05 08:47:13 +00002046 double number;
2047 double f;
2048 int ndigits = 0;
2049 int i;
Georg Brandlccadf842006-03-31 18:54:53 +00002050 static char *kwlist[] = {"number", "ndigits", 0};
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002051
Jeffrey Yasskin9871d8f2008-01-05 08:47:13 +00002052 if (!PyArg_ParseTupleAndKeywords(args, kwds, "d|i:round",
2053 kwlist, &number, &ndigits))
2054 return NULL;
2055 f = 1.0;
2056 i = abs(ndigits);
2057 while (--i >= 0)
2058 f = f*10.0;
2059 if (ndigits < 0)
2060 number /= f;
2061 else
2062 number *= f;
2063 if (number >= 0.0)
2064 number = floor(number + 0.5);
2065 else
2066 number = ceil(number - 0.5);
2067 if (ndigits < 0)
2068 number *= f;
2069 else
2070 number /= f;
2071 return PyFloat_FromDouble(number);
Guido van Rossum9e51f9b1993-02-12 16:29:05 +00002072}
2073
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002074PyDoc_STRVAR(round_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002075"round(number[, ndigits]) -> floating point number\n\
2076\n\
2077Round a number to a given precision in decimal digits (default 0 digits).\n\
Jeffrey Yasskin9871d8f2008-01-05 08:47:13 +00002078This always returns a floating point number. Precision may be negative.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002079
Raymond Hettinger64958a12003-12-17 20:43:33 +00002080static PyObject *
2081builtin_sorted(PyObject *self, PyObject *args, PyObject *kwds)
2082{
2083 PyObject *newlist, *v, *seq, *compare=NULL, *keyfunc=NULL, *newargs;
2084 PyObject *callable;
Martin v. Löwis15e62742006-02-27 16:46:16 +00002085 static char *kwlist[] = {"iterable", "cmp", "key", "reverse", 0};
Neal Norwitz6f0d4792005-12-15 06:40:36 +00002086 int reverse;
Raymond Hettinger64958a12003-12-17 20:43:33 +00002087
Neal Norwitz6f0d4792005-12-15 06:40:36 +00002088 /* args 1-4 should match listsort in Objects/listobject.c */
Armin Rigo71d7e702005-09-20 18:13:03 +00002089 if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|OOi:sorted",
2090 kwlist, &seq, &compare, &keyfunc, &reverse))
2091 return NULL;
Raymond Hettinger64958a12003-12-17 20:43:33 +00002092
2093 newlist = PySequence_List(seq);
2094 if (newlist == NULL)
2095 return NULL;
2096
2097 callable = PyObject_GetAttrString(newlist, "sort");
2098 if (callable == NULL) {
2099 Py_DECREF(newlist);
2100 return NULL;
2101 }
Georg Brandl99d7e4e2005-08-31 22:21:15 +00002102
Raymond Hettinger64958a12003-12-17 20:43:33 +00002103 newargs = PyTuple_GetSlice(args, 1, 4);
2104 if (newargs == NULL) {
2105 Py_DECREF(newlist);
2106 Py_DECREF(callable);
2107 return NULL;
2108 }
2109
2110 v = PyObject_Call(callable, newargs, kwds);
2111 Py_DECREF(newargs);
2112 Py_DECREF(callable);
2113 if (v == NULL) {
2114 Py_DECREF(newlist);
2115 return NULL;
2116 }
2117 Py_DECREF(v);
2118 return newlist;
2119}
2120
2121PyDoc_STRVAR(sorted_doc,
2122"sorted(iterable, cmp=None, key=None, reverse=False) --> new sorted list");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002123
Guido van Rossum79f25d91997-04-29 20:08:16 +00002124static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002125builtin_vars(PyObject *self, PyObject *args)
Guido van Rossum2d951851994-08-29 12:52:16 +00002126{
Guido van Rossum79f25d91997-04-29 20:08:16 +00002127 PyObject *v = NULL;
2128 PyObject *d;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002129
Raymond Hettingerea3fdf42002-12-29 16:33:45 +00002130 if (!PyArg_UnpackTuple(args, "vars", 0, 1, &v))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002131 return NULL;
Guido van Rossum2d951851994-08-29 12:52:16 +00002132 if (v == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002133 d = PyEval_GetLocals();
Guido van Rossum53bb7ff1995-07-26 16:26:31 +00002134 if (d == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002135 if (!PyErr_Occurred())
2136 PyErr_SetString(PyExc_SystemError,
Alex Martellia9b9c9f2003-04-23 13:34:35 +00002137 "vars(): no locals!?");
Guido van Rossum53bb7ff1995-07-26 16:26:31 +00002138 }
2139 else
Guido van Rossum79f25d91997-04-29 20:08:16 +00002140 Py_INCREF(d);
Guido van Rossum2d951851994-08-29 12:52:16 +00002141 }
2142 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002143 d = PyObject_GetAttrString(v, "__dict__");
Guido van Rossum2d951851994-08-29 12:52:16 +00002144 if (d == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002145 PyErr_SetString(PyExc_TypeError,
Guido van Rossum2d951851994-08-29 12:52:16 +00002146 "vars() argument must have __dict__ attribute");
2147 return NULL;
2148 }
2149 }
2150 return d;
2151}
2152
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002153PyDoc_STRVAR(vars_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002154"vars([object]) -> dictionary\n\
2155\n\
2156Without arguments, equivalent to locals().\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002157With an argument, equivalent to object.__dict__.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002158
Alex Martellia70b1912003-04-22 08:12:33 +00002159
2160static PyObject*
2161builtin_sum(PyObject *self, PyObject *args)
2162{
2163 PyObject *seq;
2164 PyObject *result = NULL;
2165 PyObject *temp, *item, *iter;
2166
Raymond Hettinger5cf63942003-10-25 06:41:37 +00002167 if (!PyArg_UnpackTuple(args, "sum", 1, 2, &seq, &result))
Alex Martellia70b1912003-04-22 08:12:33 +00002168 return NULL;
2169
2170 iter = PyObject_GetIter(seq);
2171 if (iter == NULL)
2172 return NULL;
2173
2174 if (result == NULL) {
2175 result = PyInt_FromLong(0);
2176 if (result == NULL) {
2177 Py_DECREF(iter);
2178 return NULL;
2179 }
2180 } else {
2181 /* reject string values for 'start' parameter */
2182 if (PyObject_TypeCheck(result, &PyBaseString_Type)) {
2183 PyErr_SetString(PyExc_TypeError,
Alex Martellia9b9c9f2003-04-23 13:34:35 +00002184 "sum() can't sum strings [use ''.join(seq) instead]");
Alex Martellia70b1912003-04-22 08:12:33 +00002185 Py_DECREF(iter);
2186 return NULL;
2187 }
Alex Martelli41c9f882003-04-22 09:24:48 +00002188 Py_INCREF(result);
Alex Martellia70b1912003-04-22 08:12:33 +00002189 }
2190
Raymond Hettinger3f8caa32007-10-24 01:28:33 +00002191#ifndef SLOW_SUM
2192 /* Fast addition by keeping temporary sums in C instead of new Python objects.
2193 Assumes all inputs are the same type. If the assumption fails, default
2194 to the more general routine.
2195 */
2196 if (PyInt_CheckExact(result)) {
2197 long i_result = PyInt_AS_LONG(result);
2198 Py_DECREF(result);
2199 result = NULL;
2200 while(result == NULL) {
2201 item = PyIter_Next(iter);
2202 if (item == NULL) {
2203 Py_DECREF(iter);
2204 if (PyErr_Occurred())
2205 return NULL;
2206 return PyInt_FromLong(i_result);
2207 }
2208 if (PyInt_CheckExact(item)) {
2209 long b = PyInt_AS_LONG(item);
2210 long x = i_result + b;
2211 if ((x^i_result) >= 0 || (x^b) >= 0) {
2212 i_result = x;
2213 Py_DECREF(item);
2214 continue;
2215 }
2216 }
2217 /* Either overflowed or is not an int. Restore real objects and process normally */
2218 result = PyInt_FromLong(i_result);
2219 temp = PyNumber_Add(result, item);
2220 Py_DECREF(result);
2221 Py_DECREF(item);
2222 result = temp;
2223 if (result == NULL) {
2224 Py_DECREF(iter);
2225 return NULL;
2226 }
2227 }
2228 }
2229
2230 if (PyFloat_CheckExact(result)) {
2231 double f_result = PyFloat_AS_DOUBLE(result);
2232 Py_DECREF(result);
2233 result = NULL;
2234 while(result == NULL) {
2235 item = PyIter_Next(iter);
2236 if (item == NULL) {
2237 Py_DECREF(iter);
2238 if (PyErr_Occurred())
2239 return NULL;
2240 return PyFloat_FromDouble(f_result);
2241 }
2242 if (PyFloat_CheckExact(item)) {
2243 PyFPE_START_PROTECT("add", return 0)
2244 f_result += PyFloat_AS_DOUBLE(item);
Raymond Hettinger3a8daf52007-10-24 02:05:51 +00002245 PyFPE_END_PROTECT(f_result)
Raymond Hettingera45c4872007-10-25 02:26:58 +00002246 Py_DECREF(item);
Raymond Hettinger3a8daf52007-10-24 02:05:51 +00002247 continue;
2248 }
2249 if (PyInt_CheckExact(item)) {
2250 PyFPE_START_PROTECT("add", return 0)
2251 f_result += (double)PyInt_AS_LONG(item);
2252 PyFPE_END_PROTECT(f_result)
Raymond Hettingera45c4872007-10-25 02:26:58 +00002253 Py_DECREF(item);
Raymond Hettinger3f8caa32007-10-24 01:28:33 +00002254 continue;
2255 }
2256 result = PyFloat_FromDouble(f_result);
2257 temp = PyNumber_Add(result, item);
2258 Py_DECREF(result);
2259 Py_DECREF(item);
2260 result = temp;
2261 if (result == NULL) {
2262 Py_DECREF(iter);
2263 return NULL;
2264 }
2265 }
2266 }
2267#endif
2268
Alex Martellia70b1912003-04-22 08:12:33 +00002269 for(;;) {
2270 item = PyIter_Next(iter);
2271 if (item == NULL) {
2272 /* error, or end-of-sequence */
2273 if (PyErr_Occurred()) {
2274 Py_DECREF(result);
2275 result = NULL;
2276 }
2277 break;
2278 }
Alex Martellia253e182003-10-25 23:24:14 +00002279 temp = PyNumber_Add(result, item);
Alex Martellia70b1912003-04-22 08:12:33 +00002280 Py_DECREF(result);
2281 Py_DECREF(item);
2282 result = temp;
2283 if (result == NULL)
2284 break;
2285 }
2286 Py_DECREF(iter);
2287 return result;
2288}
2289
2290PyDoc_STRVAR(sum_doc,
Georg Brandl8134d062006-10-12 12:33:07 +00002291"sum(sequence[, start]) -> value\n\
Alex Martellia70b1912003-04-22 08:12:33 +00002292\n\
2293Returns the sum of a sequence of numbers (NOT strings) plus the value\n\
Georg Brandl8134d062006-10-12 12:33:07 +00002294of parameter 'start' (which defaults to 0). When the sequence is\n\
2295empty, returns start.");
Alex Martellia70b1912003-04-22 08:12:33 +00002296
2297
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002298static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002299builtin_isinstance(PyObject *self, PyObject *args)
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002300{
2301 PyObject *inst;
2302 PyObject *cls;
Guido van Rossum823649d2001-03-21 18:40:58 +00002303 int retval;
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002304
Raymond Hettingerea3fdf42002-12-29 16:33:45 +00002305 if (!PyArg_UnpackTuple(args, "isinstance", 2, 2, &inst, &cls))
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002306 return NULL;
Guido van Rossumf5dd9141997-12-02 19:11:45 +00002307
Guido van Rossum823649d2001-03-21 18:40:58 +00002308 retval = PyObject_IsInstance(inst, cls);
2309 if (retval < 0)
Guido van Rossumad991772001-01-12 16:03:05 +00002310 return NULL;
Guido van Rossum77f6a652002-04-03 22:41:51 +00002311 return PyBool_FromLong(retval);
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002312}
2313
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002314PyDoc_STRVAR(isinstance_doc,
Guido van Rossum77f6a652002-04-03 22:41:51 +00002315"isinstance(object, class-or-type-or-tuple) -> bool\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002316\n\
2317Return whether an object is an instance of a class or of a subclass thereof.\n\
Guido van Rossum03290ec2001-10-07 20:54:12 +00002318With a type as second argument, return whether that is the object's type.\n\
2319The form using a tuple, isinstance(x, (A, B, ...)), is a shortcut for\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002320isinstance(x, A) or isinstance(x, B) or ... (etc.).");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002321
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002322
2323static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002324builtin_issubclass(PyObject *self, PyObject *args)
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002325{
2326 PyObject *derived;
2327 PyObject *cls;
2328 int retval;
2329
Raymond Hettingerea3fdf42002-12-29 16:33:45 +00002330 if (!PyArg_UnpackTuple(args, "issubclass", 2, 2, &derived, &cls))
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002331 return NULL;
Guido van Rossum668213d1999-06-16 17:28:37 +00002332
Guido van Rossum823649d2001-03-21 18:40:58 +00002333 retval = PyObject_IsSubclass(derived, cls);
2334 if (retval < 0)
2335 return NULL;
Guido van Rossum77f6a652002-04-03 22:41:51 +00002336 return PyBool_FromLong(retval);
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002337}
2338
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002339PyDoc_STRVAR(issubclass_doc,
Guido van Rossum77f6a652002-04-03 22:41:51 +00002340"issubclass(C, B) -> bool\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002341\n\
Walter Dörwaldd9a6ad32002-12-12 16:41:44 +00002342Return whether class C is a subclass (i.e., a derived class) of class B.\n\
2343When using a tuple as the second argument issubclass(X, (A, B, ...)),\n\
2344is a shortcut for issubclass(X, A) or issubclass(X, B) or ... (etc.).");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002345
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002346
Barry Warsawbd599b52000-08-03 15:45:29 +00002347static PyObject*
2348builtin_zip(PyObject *self, PyObject *args)
2349{
2350 PyObject *ret;
Martin v. Löwisd96ee902006-02-16 14:37:16 +00002351 const Py_ssize_t itemsize = PySequence_Length(args);
2352 Py_ssize_t i;
Tim Peters8572b4f2001-05-06 01:05:02 +00002353 PyObject *itlist; /* tuple of iterators */
Martin v. Löwisd96ee902006-02-16 14:37:16 +00002354 Py_ssize_t len; /* guess at result length */
Barry Warsawbd599b52000-08-03 15:45:29 +00002355
Raymond Hettingereaef6152003-08-02 07:42:57 +00002356 if (itemsize == 0)
2357 return PyList_New(0);
2358
Barry Warsawbd599b52000-08-03 15:45:29 +00002359 /* args must be a tuple */
2360 assert(PyTuple_Check(args));
2361
Tim Peters39a86c22002-05-12 07:19:38 +00002362 /* Guess at result length: the shortest of the input lengths.
2363 If some argument refuses to say, we refuse to guess too, lest
2364 an argument like xrange(sys.maxint) lead us astray.*/
Tim Peters67d687a2002-04-29 21:27:32 +00002365 len = -1; /* unknown */
2366 for (i = 0; i < itemsize; ++i) {
2367 PyObject *item = PyTuple_GET_ITEM(args, i);
Raymond Hettinger4e2f7142007-12-06 00:56:53 +00002368 Py_ssize_t thislen = _PyObject_LengthHint(item, -1);
Tim Peters39a86c22002-05-12 07:19:38 +00002369 if (thislen < 0) {
Tim Peters39a86c22002-05-12 07:19:38 +00002370 len = -1;
2371 break;
2372 }
Tim Peters67d687a2002-04-29 21:27:32 +00002373 else if (len < 0 || thislen < len)
2374 len = thislen;
2375 }
2376
Tim Peters8572b4f2001-05-06 01:05:02 +00002377 /* allocate result list */
Tim Peters67d687a2002-04-29 21:27:32 +00002378 if (len < 0)
2379 len = 10; /* arbitrary */
2380 if ((ret = PyList_New(len)) == NULL)
Barry Warsawbd599b52000-08-03 15:45:29 +00002381 return NULL;
2382
Tim Peters8572b4f2001-05-06 01:05:02 +00002383 /* obtain iterators */
2384 itlist = PyTuple_New(itemsize);
2385 if (itlist == NULL)
2386 goto Fail_ret;
2387 for (i = 0; i < itemsize; ++i) {
2388 PyObject *item = PyTuple_GET_ITEM(args, i);
2389 PyObject *it = PyObject_GetIter(item);
2390 if (it == NULL) {
2391 if (PyErr_ExceptionMatches(PyExc_TypeError))
2392 PyErr_Format(PyExc_TypeError,
Neal Norwitza361bd82006-02-19 19:31:50 +00002393 "zip argument #%zd must support iteration",
Tim Peters8572b4f2001-05-06 01:05:02 +00002394 i+1);
2395 goto Fail_ret_itlist;
Barry Warsawbd599b52000-08-03 15:45:29 +00002396 }
Tim Peters8572b4f2001-05-06 01:05:02 +00002397 PyTuple_SET_ITEM(itlist, i, it);
2398 }
Barry Warsawbd599b52000-08-03 15:45:29 +00002399
Tim Peters8572b4f2001-05-06 01:05:02 +00002400 /* build result into ret list */
Tim Peters67d687a2002-04-29 21:27:32 +00002401 for (i = 0; ; ++i) {
2402 int j;
Tim Peters8572b4f2001-05-06 01:05:02 +00002403 PyObject *next = PyTuple_New(itemsize);
2404 if (!next)
2405 goto Fail_ret_itlist;
2406
Tim Peters67d687a2002-04-29 21:27:32 +00002407 for (j = 0; j < itemsize; j++) {
2408 PyObject *it = PyTuple_GET_ITEM(itlist, j);
Tim Peters8572b4f2001-05-06 01:05:02 +00002409 PyObject *item = PyIter_Next(it);
Barry Warsawbd599b52000-08-03 15:45:29 +00002410 if (!item) {
Tim Peters8572b4f2001-05-06 01:05:02 +00002411 if (PyErr_Occurred()) {
2412 Py_DECREF(ret);
2413 ret = NULL;
Barry Warsawbd599b52000-08-03 15:45:29 +00002414 }
2415 Py_DECREF(next);
Tim Peters8572b4f2001-05-06 01:05:02 +00002416 Py_DECREF(itlist);
Tim Peters67d687a2002-04-29 21:27:32 +00002417 goto Done;
Barry Warsawbd599b52000-08-03 15:45:29 +00002418 }
Tim Peters67d687a2002-04-29 21:27:32 +00002419 PyTuple_SET_ITEM(next, j, item);
Barry Warsawbd599b52000-08-03 15:45:29 +00002420 }
Tim Peters8572b4f2001-05-06 01:05:02 +00002421
Tim Peters67d687a2002-04-29 21:27:32 +00002422 if (i < len)
2423 PyList_SET_ITEM(ret, i, next);
2424 else {
2425 int status = PyList_Append(ret, next);
2426 Py_DECREF(next);
2427 ++len;
2428 if (status < 0)
2429 goto Fail_ret_itlist;
2430 }
Barry Warsawbd599b52000-08-03 15:45:29 +00002431 }
Tim Peters8572b4f2001-05-06 01:05:02 +00002432
Tim Peters67d687a2002-04-29 21:27:32 +00002433Done:
2434 if (ret != NULL && i < len) {
2435 /* The list is too big. */
2436 if (PyList_SetSlice(ret, i, len, NULL) < 0)
2437 return NULL;
2438 }
2439 return ret;
2440
Tim Peters8572b4f2001-05-06 01:05:02 +00002441Fail_ret_itlist:
2442 Py_DECREF(itlist);
2443Fail_ret:
2444 Py_DECREF(ret);
2445 return NULL;
Barry Warsawbd599b52000-08-03 15:45:29 +00002446}
2447
2448
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002449PyDoc_STRVAR(zip_doc,
Barry Warsawbd599b52000-08-03 15:45:29 +00002450"zip(seq1 [, seq2 [...]]) -> [(seq1[0], seq2[0] ...), (...)]\n\
2451\n\
2452Return a list of tuples, where each tuple contains the i-th element\n\
2453from each of the argument sequences. The returned list is truncated\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002454in length to the length of the shortest argument sequence.");
Barry Warsawbd599b52000-08-03 15:45:29 +00002455
2456
Guido van Rossum79f25d91997-04-29 20:08:16 +00002457static PyMethodDef builtin_methods[] = {
Neal Norwitz92e212f2006-04-03 04:48:37 +00002458 {"__import__", (PyCFunction)builtin___import__, METH_VARARGS | METH_KEYWORDS, import_doc},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002459 {"abs", builtin_abs, METH_O, abs_doc},
Raymond Hettinger96229b12005-03-11 06:49:40 +00002460 {"all", builtin_all, METH_O, all_doc},
2461 {"any", builtin_any, METH_O, any_doc},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002462 {"apply", builtin_apply, METH_VARARGS, apply_doc},
Eric Smith3cd81942008-02-22 16:30:22 +00002463 {"bin", builtin_bin, METH_O, bin_doc},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002464 {"callable", builtin_callable, METH_O, callable_doc},
2465 {"chr", builtin_chr, METH_VARARGS, chr_doc},
2466 {"cmp", builtin_cmp, METH_VARARGS, cmp_doc},
2467 {"coerce", builtin_coerce, METH_VARARGS, coerce_doc},
Georg Brandl5240d742007-03-13 20:46:32 +00002468 {"compile", (PyCFunction)builtin_compile, METH_VARARGS | METH_KEYWORDS, compile_doc},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002469 {"delattr", builtin_delattr, METH_VARARGS, delattr_doc},
2470 {"dir", builtin_dir, METH_VARARGS, dir_doc},
2471 {"divmod", builtin_divmod, METH_VARARGS, divmod_doc},
2472 {"eval", builtin_eval, METH_VARARGS, eval_doc},
2473 {"execfile", builtin_execfile, METH_VARARGS, execfile_doc},
2474 {"filter", builtin_filter, METH_VARARGS, filter_doc},
Eric Smitha9f7d622008-02-17 19:46:49 +00002475 {"format", builtin_format, METH_VARARGS, format_doc},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002476 {"getattr", builtin_getattr, METH_VARARGS, getattr_doc},
2477 {"globals", (PyCFunction)builtin_globals, METH_NOARGS, globals_doc},
2478 {"hasattr", builtin_hasattr, METH_VARARGS, hasattr_doc},
2479 {"hash", builtin_hash, METH_O, hash_doc},
2480 {"hex", builtin_hex, METH_O, hex_doc},
2481 {"id", builtin_id, METH_O, id_doc},
2482 {"input", builtin_input, METH_VARARGS, input_doc},
2483 {"intern", builtin_intern, METH_VARARGS, intern_doc},
2484 {"isinstance", builtin_isinstance, METH_VARARGS, isinstance_doc},
2485 {"issubclass", builtin_issubclass, METH_VARARGS, issubclass_doc},
2486 {"iter", builtin_iter, METH_VARARGS, iter_doc},
2487 {"len", builtin_len, METH_O, len_doc},
2488 {"locals", (PyCFunction)builtin_locals, METH_NOARGS, locals_doc},
2489 {"map", builtin_map, METH_VARARGS, map_doc},
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00002490 {"max", (PyCFunction)builtin_max, METH_VARARGS | METH_KEYWORDS, max_doc},
2491 {"min", (PyCFunction)builtin_min, METH_VARARGS | METH_KEYWORDS, min_doc},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002492 {"oct", builtin_oct, METH_O, oct_doc},
Neal Norwitzc4edb0e2006-05-02 04:43:14 +00002493 {"open", (PyCFunction)builtin_open, METH_VARARGS | METH_KEYWORDS, open_doc},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002494 {"ord", builtin_ord, METH_O, ord_doc},
2495 {"pow", builtin_pow, METH_VARARGS, pow_doc},
Eric Smith7c478942008-03-18 23:45:49 +00002496 {"print", (PyCFunction)builtin_print, METH_VARARGS | METH_KEYWORDS, print_doc},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002497 {"range", builtin_range, METH_VARARGS, range_doc},
2498 {"raw_input", builtin_raw_input, METH_VARARGS, raw_input_doc},
2499 {"reduce", builtin_reduce, METH_VARARGS, reduce_doc},
2500 {"reload", builtin_reload, METH_O, reload_doc},
2501 {"repr", builtin_repr, METH_O, repr_doc},
Georg Brandlccadf842006-03-31 18:54:53 +00002502 {"round", (PyCFunction)builtin_round, METH_VARARGS | METH_KEYWORDS, round_doc},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002503 {"setattr", builtin_setattr, METH_VARARGS, setattr_doc},
Raymond Hettinger64958a12003-12-17 20:43:33 +00002504 {"sorted", (PyCFunction)builtin_sorted, METH_VARARGS | METH_KEYWORDS, sorted_doc},
Alex Martellia70b1912003-04-22 08:12:33 +00002505 {"sum", builtin_sum, METH_VARARGS, sum_doc},
Martin v. Löwis339d0f72001-08-17 18:39:25 +00002506#ifdef Py_USING_UNICODE
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002507 {"unichr", builtin_unichr, METH_VARARGS, unichr_doc},
Martin v. Löwis339d0f72001-08-17 18:39:25 +00002508#endif
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002509 {"vars", builtin_vars, METH_VARARGS, vars_doc},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002510 {"zip", builtin_zip, METH_VARARGS, zip_doc},
Guido van Rossumc02e15c1991-12-16 13:03:00 +00002511 {NULL, NULL},
Guido van Rossum3f5da241990-12-20 15:06:42 +00002512};
2513
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002514PyDoc_STRVAR(builtin_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002515"Built-in functions, exceptions, and other objects.\n\
2516\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002517Noteworthy: None is the `nil' object; Ellipsis represents `...' in slices.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002518
Guido van Rossum25ce5661997-08-02 03:10:38 +00002519PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002520_PyBuiltin_Init(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +00002521{
Fred Drake5550de32000-06-20 04:54:19 +00002522 PyObject *mod, *dict, *debug;
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002523 mod = Py_InitModule4("__builtin__", builtin_methods,
2524 builtin_doc, (PyObject *)NULL,
2525 PYTHON_API_VERSION);
Guido van Rossum25ce5661997-08-02 03:10:38 +00002526 if (mod == NULL)
2527 return NULL;
2528 dict = PyModule_GetDict(mod);
Tim Peters4b7625e2001-09-13 21:37:17 +00002529
Tim Peters7571a0f2003-03-23 17:52:28 +00002530#ifdef Py_TRACE_REFS
2531 /* __builtin__ exposes a number of statically allocated objects
2532 * that, before this code was added in 2.3, never showed up in
2533 * the list of "all objects" maintained by Py_TRACE_REFS. As a
2534 * result, programs leaking references to None and False (etc)
2535 * couldn't be diagnosed by examining sys.getobjects(0).
2536 */
2537#define ADD_TO_ALL(OBJECT) _Py_AddToAllObjects((PyObject *)(OBJECT), 0)
2538#else
2539#define ADD_TO_ALL(OBJECT) (void)0
2540#endif
2541
Tim Peters4b7625e2001-09-13 21:37:17 +00002542#define SETBUILTIN(NAME, OBJECT) \
Tim Peters7571a0f2003-03-23 17:52:28 +00002543 if (PyDict_SetItemString(dict, NAME, (PyObject *)OBJECT) < 0) \
2544 return NULL; \
2545 ADD_TO_ALL(OBJECT)
Tim Peters4b7625e2001-09-13 21:37:17 +00002546
2547 SETBUILTIN("None", Py_None);
2548 SETBUILTIN("Ellipsis", Py_Ellipsis);
2549 SETBUILTIN("NotImplemented", Py_NotImplemented);
Guido van Rossum77f6a652002-04-03 22:41:51 +00002550 SETBUILTIN("False", Py_False);
2551 SETBUILTIN("True", Py_True);
Neal Norwitz32a7e7f2002-05-31 19:58:02 +00002552 SETBUILTIN("basestring", &PyBaseString_Type);
Guido van Rossum77f6a652002-04-03 22:41:51 +00002553 SETBUILTIN("bool", &PyBool_Type);
Travis E. Oliphant3781aef2008-03-18 04:44:57 +00002554 /* SETBUILTIN("memoryview", &PyMemoryView_Type); */
Christian Heimes288e89a2008-01-18 18:24:07 +00002555 SETBUILTIN("bytes", &PyString_Type);
Guido van Rossumbea18cc2002-06-14 20:41:17 +00002556 SETBUILTIN("buffer", &PyBuffer_Type);
Tim Peters4b7625e2001-09-13 21:37:17 +00002557 SETBUILTIN("classmethod", &PyClassMethod_Type);
Tim Peters6d6c1a32001-08-02 04:15:00 +00002558#ifndef WITHOUT_COMPLEX
Tim Peters4b7625e2001-09-13 21:37:17 +00002559 SETBUILTIN("complex", &PyComplex_Type);
Tim Peters6d6c1a32001-08-02 04:15:00 +00002560#endif
Tim Petersa427a2b2001-10-29 22:25:45 +00002561 SETBUILTIN("dict", &PyDict_Type);
Tim Peters67d687a2002-04-29 21:27:32 +00002562 SETBUILTIN("enumerate", &PyEnum_Type);
Neal Norwitzc4edb0e2006-05-02 04:43:14 +00002563 SETBUILTIN("file", &PyFile_Type);
Tim Peters4b7625e2001-09-13 21:37:17 +00002564 SETBUILTIN("float", &PyFloat_Type);
Raymond Hettingera690a992003-11-16 16:17:49 +00002565 SETBUILTIN("frozenset", &PyFrozenSet_Type);
Tim Peters4b7625e2001-09-13 21:37:17 +00002566 SETBUILTIN("property", &PyProperty_Type);
2567 SETBUILTIN("int", &PyInt_Type);
2568 SETBUILTIN("list", &PyList_Type);
2569 SETBUILTIN("long", &PyLong_Type);
2570 SETBUILTIN("object", &PyBaseObject_Type);
Raymond Hettinger85c20a42003-11-06 14:06:48 +00002571 SETBUILTIN("reversed", &PyReversed_Type);
Raymond Hettingera690a992003-11-16 16:17:49 +00002572 SETBUILTIN("set", &PySet_Type);
Guido van Rossumbea18cc2002-06-14 20:41:17 +00002573 SETBUILTIN("slice", &PySlice_Type);
Tim Peters4b7625e2001-09-13 21:37:17 +00002574 SETBUILTIN("staticmethod", &PyStaticMethod_Type);
2575 SETBUILTIN("str", &PyString_Type);
2576 SETBUILTIN("super", &PySuper_Type);
2577 SETBUILTIN("tuple", &PyTuple_Type);
2578 SETBUILTIN("type", &PyType_Type);
Raymond Hettingerc4c453f2002-06-05 23:12:45 +00002579 SETBUILTIN("xrange", &PyRange_Type);
Martin v. Löwis339d0f72001-08-17 18:39:25 +00002580#ifdef Py_USING_UNICODE
Tim Peters4b7625e2001-09-13 21:37:17 +00002581 SETBUILTIN("unicode", &PyUnicode_Type);
Martin v. Löwis339d0f72001-08-17 18:39:25 +00002582#endif
Guido van Rossum77f6a652002-04-03 22:41:51 +00002583 debug = PyBool_FromLong(Py_OptimizeFlag == 0);
Fred Drake5550de32000-06-20 04:54:19 +00002584 if (PyDict_SetItemString(dict, "__debug__", debug) < 0) {
2585 Py_XDECREF(debug);
Guido van Rossum25ce5661997-08-02 03:10:38 +00002586 return NULL;
Fred Drake5550de32000-06-20 04:54:19 +00002587 }
2588 Py_XDECREF(debug);
Barry Warsaw757af0e1997-08-29 22:13:51 +00002589
Guido van Rossum25ce5661997-08-02 03:10:38 +00002590 return mod;
Tim Peters7571a0f2003-03-23 17:52:28 +00002591#undef ADD_TO_ALL
Tim Peters4b7625e2001-09-13 21:37:17 +00002592#undef SETBUILTIN
Guido van Rossum3f5da241990-12-20 15:06:42 +00002593}
2594
Guido van Rossume77a7571993-11-03 15:01:26 +00002595/* Helper for filter(): filter a tuple through a function */
Guido van Rossum12d12c51993-10-26 17:58:25 +00002596
Guido van Rossum79f25d91997-04-29 20:08:16 +00002597static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002598filtertuple(PyObject *func, PyObject *tuple)
Guido van Rossum12d12c51993-10-26 17:58:25 +00002599{
Guido van Rossum79f25d91997-04-29 20:08:16 +00002600 PyObject *result;
Martin v. Löwis18e16552006-02-15 17:27:45 +00002601 Py_ssize_t i, j;
2602 Py_ssize_t len = PyTuple_Size(tuple);
Guido van Rossum12d12c51993-10-26 17:58:25 +00002603
Guido van Rossumb7b45621995-08-04 04:07:45 +00002604 if (len == 0) {
Walter Dörwaldc3da83f2003-02-04 20:24:45 +00002605 if (PyTuple_CheckExact(tuple))
2606 Py_INCREF(tuple);
2607 else
2608 tuple = PyTuple_New(0);
Guido van Rossumb7b45621995-08-04 04:07:45 +00002609 return tuple;
2610 }
2611
Guido van Rossum79f25d91997-04-29 20:08:16 +00002612 if ((result = PyTuple_New(len)) == NULL)
Guido van Rossum2586bf01993-11-01 16:21:44 +00002613 return NULL;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002614
Guido van Rossum12d12c51993-10-26 17:58:25 +00002615 for (i = j = 0; i < len; ++i) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002616 PyObject *item, *good;
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002617 int ok;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002618
Walter Dörwald8dd19322003-02-10 17:36:40 +00002619 if (tuple->ob_type->tp_as_sequence &&
2620 tuple->ob_type->tp_as_sequence->sq_item) {
2621 item = tuple->ob_type->tp_as_sequence->sq_item(tuple, i);
Walter Dörwaldc58a3a12003-08-18 18:28:45 +00002622 if (item == NULL)
2623 goto Fail_1;
Walter Dörwald8dd19322003-02-10 17:36:40 +00002624 } else {
Alex Martellia9b9c9f2003-04-23 13:34:35 +00002625 PyErr_SetString(PyExc_TypeError, "filter(): unsubscriptable tuple");
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002626 goto Fail_1;
Walter Dörwald8dd19322003-02-10 17:36:40 +00002627 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002628 if (func == Py_None) {
2629 Py_INCREF(item);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002630 good = item;
2631 }
2632 else {
Raymond Hettinger8ae46892003-10-12 19:09:37 +00002633 PyObject *arg = PyTuple_Pack(1, item);
Walter Dörwaldc58a3a12003-08-18 18:28:45 +00002634 if (arg == NULL) {
2635 Py_DECREF(item);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002636 goto Fail_1;
Walter Dörwaldc58a3a12003-08-18 18:28:45 +00002637 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002638 good = PyEval_CallObject(func, arg);
2639 Py_DECREF(arg);
Walter Dörwaldc58a3a12003-08-18 18:28:45 +00002640 if (good == NULL) {
2641 Py_DECREF(item);
Guido van Rossum12d12c51993-10-26 17:58:25 +00002642 goto Fail_1;
Walter Dörwaldc58a3a12003-08-18 18:28:45 +00002643 }
Guido van Rossum12d12c51993-10-26 17:58:25 +00002644 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002645 ok = PyObject_IsTrue(good);
2646 Py_DECREF(good);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002647 if (ok) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002648 if (PyTuple_SetItem(result, j++, item) < 0)
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002649 goto Fail_1;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002650 }
Walter Dörwaldc58a3a12003-08-18 18:28:45 +00002651 else
2652 Py_DECREF(item);
Guido van Rossum12d12c51993-10-26 17:58:25 +00002653 }
2654
Tim Peters4324aa32001-05-28 22:30:08 +00002655 if (_PyTuple_Resize(&result, j) < 0)
Guido van Rossum12d12c51993-10-26 17:58:25 +00002656 return NULL;
2657
Guido van Rossum12d12c51993-10-26 17:58:25 +00002658 return result;
2659
Guido van Rossum12d12c51993-10-26 17:58:25 +00002660Fail_1:
Guido van Rossum79f25d91997-04-29 20:08:16 +00002661 Py_DECREF(result);
Guido van Rossum12d12c51993-10-26 17:58:25 +00002662 return NULL;
2663}
2664
2665
Guido van Rossume77a7571993-11-03 15:01:26 +00002666/* Helper for filter(): filter a string through a function */
Guido van Rossum12d12c51993-10-26 17:58:25 +00002667
Guido van Rossum79f25d91997-04-29 20:08:16 +00002668static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002669filterstring(PyObject *func, PyObject *strobj)
Guido van Rossum12d12c51993-10-26 17:58:25 +00002670{
Guido van Rossum79f25d91997-04-29 20:08:16 +00002671 PyObject *result;
Martin v. Löwis18e16552006-02-15 17:27:45 +00002672 Py_ssize_t i, j;
2673 Py_ssize_t len = PyString_Size(strobj);
2674 Py_ssize_t outlen = len;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002675
Guido van Rossum79f25d91997-04-29 20:08:16 +00002676 if (func == Py_None) {
Walter Dörwald1918f772003-02-10 13:19:13 +00002677 /* If it's a real string we can return the original,
2678 * as no character is ever false and __getitem__
2679 * does return this character. If it's a subclass
2680 * we must go through the __getitem__ loop */
2681 if (PyString_CheckExact(strobj)) {
Walter Dörwaldc3da83f2003-02-04 20:24:45 +00002682 Py_INCREF(strobj);
Walter Dörwald1918f772003-02-10 13:19:13 +00002683 return strobj;
2684 }
Guido van Rossum12d12c51993-10-26 17:58:25 +00002685 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002686 if ((result = PyString_FromStringAndSize(NULL, len)) == NULL)
Guido van Rossum2586bf01993-11-01 16:21:44 +00002687 return NULL;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002688
Guido van Rossum12d12c51993-10-26 17:58:25 +00002689 for (i = j = 0; i < len; ++i) {
Walter Dörwald1918f772003-02-10 13:19:13 +00002690 PyObject *item;
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002691 int ok;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002692
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002693 item = (*strobj->ob_type->tp_as_sequence->sq_item)(strobj, i);
2694 if (item == NULL)
2695 goto Fail_1;
Walter Dörwald1918f772003-02-10 13:19:13 +00002696 if (func==Py_None) {
2697 ok = 1;
2698 } else {
2699 PyObject *arg, *good;
Raymond Hettinger8ae46892003-10-12 19:09:37 +00002700 arg = PyTuple_Pack(1, item);
Walter Dörwald1918f772003-02-10 13:19:13 +00002701 if (arg == NULL) {
2702 Py_DECREF(item);
2703 goto Fail_1;
2704 }
2705 good = PyEval_CallObject(func, arg);
2706 Py_DECREF(arg);
2707 if (good == NULL) {
2708 Py_DECREF(item);
2709 goto Fail_1;
2710 }
2711 ok = PyObject_IsTrue(good);
2712 Py_DECREF(good);
Tim Peters388ed082001-04-07 20:34:48 +00002713 }
Walter Dörwald903f1e02003-02-04 16:28:00 +00002714 if (ok) {
Martin v. Löwisd96ee902006-02-16 14:37:16 +00002715 Py_ssize_t reslen;
Walter Dörwald903f1e02003-02-04 16:28:00 +00002716 if (!PyString_Check(item)) {
2717 PyErr_SetString(PyExc_TypeError, "can't filter str to str:"
2718 " __getitem__ returned different type");
2719 Py_DECREF(item);
2720 goto Fail_1;
2721 }
2722 reslen = PyString_GET_SIZE(item);
2723 if (reslen == 1) {
2724 PyString_AS_STRING(result)[j++] =
2725 PyString_AS_STRING(item)[0];
2726 } else {
2727 /* do we need more space? */
Martin v. Löwisd96ee902006-02-16 14:37:16 +00002728 Py_ssize_t need = j + reslen + len-i-1;
Walter Dörwald903f1e02003-02-04 16:28:00 +00002729 if (need > outlen) {
2730 /* overallocate, to avoid reallocations */
2731 if (need<2*outlen)
2732 need = 2*outlen;
2733 if (_PyString_Resize(&result, need)) {
2734 Py_DECREF(item);
2735 return NULL;
2736 }
2737 outlen = need;
2738 }
2739 memcpy(
2740 PyString_AS_STRING(result) + j,
2741 PyString_AS_STRING(item),
2742 reslen
2743 );
2744 j += reslen;
2745 }
2746 }
Tim Peters388ed082001-04-07 20:34:48 +00002747 Py_DECREF(item);
Guido van Rossum12d12c51993-10-26 17:58:25 +00002748 }
2749
Walter Dörwald903f1e02003-02-04 16:28:00 +00002750 if (j < outlen)
Tim Peters5de98422002-04-27 18:44:32 +00002751 _PyString_Resize(&result, j);
Guido van Rossum12d12c51993-10-26 17:58:25 +00002752
Guido van Rossum12d12c51993-10-26 17:58:25 +00002753 return result;
2754
Guido van Rossum12d12c51993-10-26 17:58:25 +00002755Fail_1:
Guido van Rossum79f25d91997-04-29 20:08:16 +00002756 Py_DECREF(result);
Guido van Rossum12d12c51993-10-26 17:58:25 +00002757 return NULL;
2758}
Martin v. Löwis8afd7572003-01-25 22:46:11 +00002759
2760#ifdef Py_USING_UNICODE
2761/* Helper for filter(): filter a Unicode object through a function */
2762
2763static PyObject *
2764filterunicode(PyObject *func, PyObject *strobj)
2765{
2766 PyObject *result;
Martin v. Löwis725507b2006-03-07 12:08:51 +00002767 register Py_ssize_t i, j;
Martin v. Löwisd96ee902006-02-16 14:37:16 +00002768 Py_ssize_t len = PyUnicode_GetSize(strobj);
2769 Py_ssize_t outlen = len;
Martin v. Löwis8afd7572003-01-25 22:46:11 +00002770
2771 if (func == Py_None) {
Walter Dörwald1918f772003-02-10 13:19:13 +00002772 /* If it's a real string we can return the original,
2773 * as no character is ever false and __getitem__
2774 * does return this character. If it's a subclass
2775 * we must go through the __getitem__ loop */
2776 if (PyUnicode_CheckExact(strobj)) {
Walter Dörwaldc3da83f2003-02-04 20:24:45 +00002777 Py_INCREF(strobj);
Walter Dörwald1918f772003-02-10 13:19:13 +00002778 return strobj;
2779 }
Martin v. Löwis8afd7572003-01-25 22:46:11 +00002780 }
2781 if ((result = PyUnicode_FromUnicode(NULL, len)) == NULL)
2782 return NULL;
2783
2784 for (i = j = 0; i < len; ++i) {
2785 PyObject *item, *arg, *good;
2786 int ok;
2787
2788 item = (*strobj->ob_type->tp_as_sequence->sq_item)(strobj, i);
2789 if (item == NULL)
2790 goto Fail_1;
Walter Dörwald1918f772003-02-10 13:19:13 +00002791 if (func == Py_None) {
2792 ok = 1;
2793 } else {
Raymond Hettinger8ae46892003-10-12 19:09:37 +00002794 arg = PyTuple_Pack(1, item);
Walter Dörwald1918f772003-02-10 13:19:13 +00002795 if (arg == NULL) {
2796 Py_DECREF(item);
2797 goto Fail_1;
2798 }
2799 good = PyEval_CallObject(func, arg);
2800 Py_DECREF(arg);
2801 if (good == NULL) {
2802 Py_DECREF(item);
2803 goto Fail_1;
2804 }
2805 ok = PyObject_IsTrue(good);
2806 Py_DECREF(good);
Martin v. Löwis8afd7572003-01-25 22:46:11 +00002807 }
Walter Dörwald903f1e02003-02-04 16:28:00 +00002808 if (ok) {
Martin v. Löwisd96ee902006-02-16 14:37:16 +00002809 Py_ssize_t reslen;
Walter Dörwald903f1e02003-02-04 16:28:00 +00002810 if (!PyUnicode_Check(item)) {
Georg Brandl99d7e4e2005-08-31 22:21:15 +00002811 PyErr_SetString(PyExc_TypeError,
Jeremy Hylton1aad9c72003-09-16 03:10:59 +00002812 "can't filter unicode to unicode:"
2813 " __getitem__ returned different type");
Walter Dörwald903f1e02003-02-04 16:28:00 +00002814 Py_DECREF(item);
2815 goto Fail_1;
2816 }
2817 reslen = PyUnicode_GET_SIZE(item);
Georg Brandl99d7e4e2005-08-31 22:21:15 +00002818 if (reslen == 1)
Walter Dörwald903f1e02003-02-04 16:28:00 +00002819 PyUnicode_AS_UNICODE(result)[j++] =
2820 PyUnicode_AS_UNICODE(item)[0];
Jeremy Hylton1aad9c72003-09-16 03:10:59 +00002821 else {
Walter Dörwald903f1e02003-02-04 16:28:00 +00002822 /* do we need more space? */
Martin v. Löwisd96ee902006-02-16 14:37:16 +00002823 Py_ssize_t need = j + reslen + len - i - 1;
Walter Dörwald903f1e02003-02-04 16:28:00 +00002824 if (need > outlen) {
Georg Brandl99d7e4e2005-08-31 22:21:15 +00002825 /* overallocate,
Jeremy Hylton1aad9c72003-09-16 03:10:59 +00002826 to avoid reallocations */
2827 if (need < 2 * outlen)
2828 need = 2 * outlen;
Jeremy Hylton364f6be2003-09-16 03:17:16 +00002829 if (PyUnicode_Resize(
2830 &result, need) < 0) {
Walter Dörwald903f1e02003-02-04 16:28:00 +00002831 Py_DECREF(item);
Walter Dörwald531e0002003-02-04 16:57:49 +00002832 goto Fail_1;
Walter Dörwald903f1e02003-02-04 16:28:00 +00002833 }
2834 outlen = need;
2835 }
Jeremy Hylton1aad9c72003-09-16 03:10:59 +00002836 memcpy(PyUnicode_AS_UNICODE(result) + j,
2837 PyUnicode_AS_UNICODE(item),
2838 reslen*sizeof(Py_UNICODE));
Walter Dörwald903f1e02003-02-04 16:28:00 +00002839 j += reslen;
2840 }
2841 }
Martin v. Löwis8afd7572003-01-25 22:46:11 +00002842 Py_DECREF(item);
2843 }
2844
Walter Dörwald903f1e02003-02-04 16:28:00 +00002845 if (j < outlen)
Martin v. Löwis8afd7572003-01-25 22:46:11 +00002846 PyUnicode_Resize(&result, j);
2847
2848 return result;
2849
2850Fail_1:
2851 Py_DECREF(result);
2852 return NULL;
2853}
2854#endif