blob: 5d191a63e01fd60fef806f9d3c01a45f50bdd753 [file] [log] [blame]
Guido van Rossum3f5da241990-12-20 15:06:42 +00001/* Built-in functions */
2
Guido van Rossum79f25d91997-04-29 20:08:16 +00003#include "Python.h"
Georg Brandlfc8eef32008-03-28 12:11:56 +00004#include "Python-ast.h"
Guido van Rossum3f5da241990-12-20 15:06:42 +00005
6#include "node.h"
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00007#include "code.h"
Guido van Rossum5b722181993-03-30 17:46:03 +00008#include "eval.h"
Guido van Rossum3f5da241990-12-20 15:06:42 +00009
Guido van Rossum6bf62da1997-04-11 20:37:35 +000010#include <ctype.h>
11
Guido van Rossume2ae77b2001-10-24 20:42:55 +000012#ifdef RISCOS
13#include "unixstuff.h"
14#endif
15
Mark Hammond26cffde2001-05-14 12:17:34 +000016/* The default encoding used by the platform file system APIs
17 Can remain NULL for all platforms that don't have such a concept
18*/
Martin v. Löwis6238d2b2002-06-30 15:26:10 +000019#if defined(MS_WINDOWS) && defined(HAVE_USABLE_WCHAR_T)
Mark Hammond26cffde2001-05-14 12:17:34 +000020const char *Py_FileSystemDefaultEncoding = "mbcs";
Just van Rossumb9b8e9c2003-02-10 09:22:01 +000021#elif defined(__APPLE__)
22const char *Py_FileSystemDefaultEncoding = "utf-8";
Mark Hammond26cffde2001-05-14 12:17:34 +000023#else
24const char *Py_FileSystemDefaultEncoding = NULL; /* use default */
25#endif
Mark Hammondef8b6542001-05-13 08:04:26 +000026
Guido van Rossum12d12c51993-10-26 17:58:25 +000027/* Forward */
Tim Petersdbd9ba62000-07-09 03:09:57 +000028static PyObject *filterstring(PyObject *, PyObject *);
Martin v. Löwis8afd7572003-01-25 22:46:11 +000029#ifdef Py_USING_UNICODE
30static PyObject *filterunicode(PyObject *, PyObject *);
31#endif
Tim Petersdbd9ba62000-07-09 03:09:57 +000032static PyObject *filtertuple (PyObject *, PyObject *);
Guido van Rossum12d12c51993-10-26 17:58:25 +000033
Guido van Rossum79f25d91997-04-29 20:08:16 +000034static PyObject *
Neal Norwitz92e212f2006-04-03 04:48:37 +000035builtin___import__(PyObject *self, PyObject *args, PyObject *kwds)
Guido van Rossum3f5da241990-12-20 15:06:42 +000036{
Neal Norwitz92e212f2006-04-03 04:48:37 +000037 static char *kwlist[] = {"name", "globals", "locals", "fromlist",
38 "level", 0};
Guido van Rossum1ae940a1995-01-02 19:04:15 +000039 char *name;
Guido van Rossum79f25d91997-04-29 20:08:16 +000040 PyObject *globals = NULL;
41 PyObject *locals = NULL;
42 PyObject *fromlist = NULL;
Thomas Woutersf7f438b2006-02-28 16:09:29 +000043 int level = -1;
Guido van Rossum1ae940a1995-01-02 19:04:15 +000044
Neal Norwitz92e212f2006-04-03 04:48:37 +000045 if (!PyArg_ParseTupleAndKeywords(args, kwds, "s|OOOi:__import__",
46 kwlist, &name, &globals, &locals, &fromlist, &level))
Guido van Rossum1ae940a1995-01-02 19:04:15 +000047 return NULL;
Thomas Woutersf7f438b2006-02-28 16:09:29 +000048 return PyImport_ImportModuleLevel(name, globals, locals,
49 fromlist, level);
Guido van Rossum1ae940a1995-01-02 19:04:15 +000050}
51
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000052PyDoc_STRVAR(import_doc,
Neal Norwitz92e212f2006-04-03 04:48:37 +000053"__import__(name, globals={}, locals={}, fromlist=[], level=-1) -> module\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +000054\n\
55Import a module. The globals are only used to determine the context;\n\
56they are not modified. The locals are currently unused. The fromlist\n\
57should be a list of names to emulate ``from name import ...'', or an\n\
58empty list to emulate ``import name''.\n\
59When importing a module from a package, note that __import__('A.B', ...)\n\
60returns package A when fromlist is empty, but its submodule B when\n\
Neal Norwitz92e212f2006-04-03 04:48:37 +000061fromlist is not empty. Level is used to determine whether to perform \n\
62absolute or relative imports. -1 is the original strategy of attempting\n\
63both absolute and relative imports, 0 is absolute, a positive number\n\
64is the number of parent directories to search relative to the current module.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +000065
Guido van Rossum1ae940a1995-01-02 19:04:15 +000066
Guido van Rossum79f25d91997-04-29 20:08:16 +000067static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +000068builtin_abs(PyObject *self, PyObject *v)
Guido van Rossum1ae940a1995-01-02 19:04:15 +000069{
Guido van Rossum09df08a1998-05-22 00:51:39 +000070 return PyNumber_Absolute(v);
Guido van Rossum3f5da241990-12-20 15:06:42 +000071}
72
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000073PyDoc_STRVAR(abs_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +000074"abs(number) -> number\n\
75\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000076Return the absolute value of the argument.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +000077
Raymond Hettinger96229b12005-03-11 06:49:40 +000078static PyObject *
79builtin_all(PyObject *self, PyObject *v)
80{
81 PyObject *it, *item;
Guido van Rossum01dbc102007-12-20 23:48:28 +000082 PyObject *(*iternext)(PyObject *);
83 int cmp;
Raymond Hettinger96229b12005-03-11 06:49:40 +000084
85 it = PyObject_GetIter(v);
86 if (it == NULL)
87 return NULL;
Guido van Rossum01dbc102007-12-20 23:48:28 +000088 iternext = *Py_TYPE(it)->tp_iternext;
Raymond Hettinger96229b12005-03-11 06:49:40 +000089
Guido van Rossum01dbc102007-12-20 23:48:28 +000090 for (;;) {
91 item = iternext(it);
92 if (item == NULL)
93 break;
94 cmp = PyObject_IsTrue(item);
Raymond Hettinger96229b12005-03-11 06:49:40 +000095 Py_DECREF(item);
96 if (cmp < 0) {
97 Py_DECREF(it);
98 return NULL;
99 }
100 if (cmp == 0) {
101 Py_DECREF(it);
102 Py_RETURN_FALSE;
103 }
104 }
105 Py_DECREF(it);
Guido van Rossum01dbc102007-12-20 23:48:28 +0000106 if (PyErr_Occurred()) {
107 if (PyErr_ExceptionMatches(PyExc_StopIteration))
108 PyErr_Clear();
109 else
110 return NULL;
111 }
Raymond Hettinger96229b12005-03-11 06:49:40 +0000112 Py_RETURN_TRUE;
113}
114
115PyDoc_STRVAR(all_doc,
116"all(iterable) -> bool\n\
117\n\
118Return True if bool(x) is True for all values x in the iterable.");
119
120static PyObject *
121builtin_any(PyObject *self, PyObject *v)
122{
123 PyObject *it, *item;
Guido van Rossum01dbc102007-12-20 23:48:28 +0000124 PyObject *(*iternext)(PyObject *);
125 int cmp;
Raymond Hettinger96229b12005-03-11 06:49:40 +0000126
127 it = PyObject_GetIter(v);
128 if (it == NULL)
129 return NULL;
Guido van Rossum01dbc102007-12-20 23:48:28 +0000130 iternext = *Py_TYPE(it)->tp_iternext;
Raymond Hettinger96229b12005-03-11 06:49:40 +0000131
Guido van Rossum01dbc102007-12-20 23:48:28 +0000132 for (;;) {
133 item = iternext(it);
134 if (item == NULL)
135 break;
136 cmp = PyObject_IsTrue(item);
Raymond Hettinger96229b12005-03-11 06:49:40 +0000137 Py_DECREF(item);
138 if (cmp < 0) {
139 Py_DECREF(it);
140 return NULL;
141 }
142 if (cmp == 1) {
143 Py_DECREF(it);
144 Py_RETURN_TRUE;
145 }
146 }
147 Py_DECREF(it);
Guido van Rossum01dbc102007-12-20 23:48:28 +0000148 if (PyErr_Occurred()) {
149 if (PyErr_ExceptionMatches(PyExc_StopIteration))
150 PyErr_Clear();
151 else
152 return NULL;
153 }
Raymond Hettinger96229b12005-03-11 06:49:40 +0000154 Py_RETURN_FALSE;
155}
156
157PyDoc_STRVAR(any_doc,
158"any(iterable) -> bool\n\
159\n\
160Return True if bool(x) is True for any x in the iterable.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000161
Guido van Rossum79f25d91997-04-29 20:08:16 +0000162static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000163builtin_apply(PyObject *self, PyObject *args)
Guido van Rossumc02e15c1991-12-16 13:03:00 +0000164{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000165 PyObject *func, *alist = NULL, *kwdict = NULL;
Barry Warsaw968f8cb1998-10-01 15:33:12 +0000166 PyObject *t = NULL, *retval = NULL;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000167
Benjamin Peterson9f4f4812008-04-27 03:01:45 +0000168 if (PyErr_WarnPy3k("apply() not supported in 3.x; "
169 "use func(*args, **kwargs)", 1) < 0)
Neal Norwitz8b2bfbc2007-05-23 06:35:32 +0000170 return NULL;
171
Raymond Hettingerea3fdf42002-12-29 16:33:45 +0000172 if (!PyArg_UnpackTuple(args, "apply", 1, 3, &func, &alist, &kwdict))
Guido van Rossumc02e15c1991-12-16 13:03:00 +0000173 return NULL;
Barry Warsaw968f8cb1998-10-01 15:33:12 +0000174 if (alist != NULL) {
175 if (!PyTuple_Check(alist)) {
176 if (!PySequence_Check(alist)) {
Jeremy Hyltonc862cf42001-01-19 03:25:05 +0000177 PyErr_Format(PyExc_TypeError,
Alex Martellia9b9c9f2003-04-23 13:34:35 +0000178 "apply() arg 2 expected sequence, found %s",
Jeremy Hyltonc862cf42001-01-19 03:25:05 +0000179 alist->ob_type->tp_name);
Barry Warsaw968f8cb1998-10-01 15:33:12 +0000180 return NULL;
181 }
182 t = PySequence_Tuple(alist);
183 if (t == NULL)
184 return NULL;
185 alist = t;
186 }
Guido van Rossum2d951851994-08-29 12:52:16 +0000187 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000188 if (kwdict != NULL && !PyDict_Check(kwdict)) {
Jeremy Hyltonc862cf42001-01-19 03:25:05 +0000189 PyErr_Format(PyExc_TypeError,
190 "apply() arg 3 expected dictionary, found %s",
191 kwdict->ob_type->tp_name);
Barry Warsaw968f8cb1998-10-01 15:33:12 +0000192 goto finally;
Guido van Rossum681d79a1995-07-18 14:51:37 +0000193 }
Barry Warsaw968f8cb1998-10-01 15:33:12 +0000194 retval = PyEval_CallObjectWithKeywords(func, alist, kwdict);
195 finally:
196 Py_XDECREF(t);
197 return retval;
Guido van Rossumc02e15c1991-12-16 13:03:00 +0000198}
199
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000200PyDoc_STRVAR(apply_doc,
Fred Drakef1fbc622001-01-12 17:05:05 +0000201"apply(object[, args[, kwargs]]) -> value\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000202\n\
Fred Drake7b912121999-12-23 14:16:55 +0000203Call a callable object with positional arguments taken from the tuple args,\n\
204and keyword arguments taken from the optional dictionary kwargs.\n\
Raymond Hettingerff41c482003-04-06 09:01:11 +0000205Note that classes are callable, as are instances with a __call__() method.\n\
206\n\
207Deprecated since release 2.3. Instead, use the extended call syntax:\n\
208 function(*args, **keywords).");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000209
210
Guido van Rossum79f25d91997-04-29 20:08:16 +0000211static PyObject *
Eric Smith3cd81942008-02-22 16:30:22 +0000212builtin_bin(PyObject *self, PyObject *v)
213{
214 return PyNumber_ToBase(v, 2);
215}
216
217PyDoc_STRVAR(bin_doc,
218"bin(number) -> string\n\
219\n\
220Return the binary representation of an integer or long integer.");
221
222
223static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000224builtin_callable(PyObject *self, PyObject *v)
Guido van Rossum2d951851994-08-29 12:52:16 +0000225{
Benjamin Peterson9f4f4812008-04-27 03:01:45 +0000226 if (PyErr_WarnPy3k("callable() not supported in 3.x; "
227 "use hasattr(o, '__call__')", 1) < 0)
Neal Norwitzdf25efe2007-05-23 06:58:36 +0000228 return NULL;
Guido van Rossum77f6a652002-04-03 22:41:51 +0000229 return PyBool_FromLong((long)PyCallable_Check(v));
Guido van Rossum2d951851994-08-29 12:52:16 +0000230}
231
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000232PyDoc_STRVAR(callable_doc,
Guido van Rossum77f6a652002-04-03 22:41:51 +0000233"callable(object) -> bool\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000234\n\
235Return whether the object is callable (i.e., some kind of function).\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000236Note that classes are callable, as are instances with a __call__() method.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000237
238
Guido van Rossum79f25d91997-04-29 20:08:16 +0000239static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000240builtin_filter(PyObject *self, PyObject *args)
Guido van Rossum12d12c51993-10-26 17:58:25 +0000241{
Guido van Rossumc7903a12002-08-16 07:04:56 +0000242 PyObject *func, *seq, *result, *it, *arg;
Martin v. Löwisd96ee902006-02-16 14:37:16 +0000243 Py_ssize_t len; /* guess for result list size */
244 register Py_ssize_t j;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000245
Raymond Hettingerea3fdf42002-12-29 16:33:45 +0000246 if (!PyArg_UnpackTuple(args, "filter", 2, 2, &func, &seq))
Guido van Rossum12d12c51993-10-26 17:58:25 +0000247 return NULL;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000248
Tim Peters0e57abf2001-05-02 07:39:38 +0000249 /* Strings and tuples return a result of the same type. */
250 if (PyString_Check(seq))
251 return filterstring(func, seq);
Martin v. Löwis8afd7572003-01-25 22:46:11 +0000252#ifdef Py_USING_UNICODE
253 if (PyUnicode_Check(seq))
254 return filterunicode(func, seq);
255#endif
Tim Peters0e57abf2001-05-02 07:39:38 +0000256 if (PyTuple_Check(seq))
257 return filtertuple(func, seq);
258
Georg Brandle35b6572005-07-19 22:20:20 +0000259 /* Pre-allocate argument list tuple. */
260 arg = PyTuple_New(1);
261 if (arg == NULL)
262 return NULL;
263
Tim Peters0e57abf2001-05-02 07:39:38 +0000264 /* Get iterator. */
265 it = PyObject_GetIter(seq);
266 if (it == NULL)
Georg Brandle35b6572005-07-19 22:20:20 +0000267 goto Fail_arg;
Tim Peters0e57abf2001-05-02 07:39:38 +0000268
269 /* Guess a result list size. */
Raymond Hettinger4e2f7142007-12-06 00:56:53 +0000270 len = _PyObject_LengthHint(seq, 8);
Guido van Rossum12d12c51993-10-26 17:58:25 +0000271
Tim Peters0e57abf2001-05-02 07:39:38 +0000272 /* Get a result list. */
Guido van Rossum79f25d91997-04-29 20:08:16 +0000273 if (PyList_Check(seq) && seq->ob_refcnt == 1) {
Tim Peters0e57abf2001-05-02 07:39:38 +0000274 /* Eww - can modify the list in-place. */
Guido van Rossum79f25d91997-04-29 20:08:16 +0000275 Py_INCREF(seq);
Guido van Rossum12d12c51993-10-26 17:58:25 +0000276 result = seq;
277 }
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000278 else {
Tim Peters0e57abf2001-05-02 07:39:38 +0000279 result = PyList_New(len);
280 if (result == NULL)
281 goto Fail_it;
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000282 }
Guido van Rossum12d12c51993-10-26 17:58:25 +0000283
Tim Peters0e57abf2001-05-02 07:39:38 +0000284 /* Build the result list. */
Tim Petersf4848da2001-05-05 00:14:56 +0000285 j = 0;
286 for (;;) {
Guido van Rossumc7903a12002-08-16 07:04:56 +0000287 PyObject *item;
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000288 int ok;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000289
Tim Peters0e57abf2001-05-02 07:39:38 +0000290 item = PyIter_Next(it);
291 if (item == NULL) {
Tim Petersf4848da2001-05-05 00:14:56 +0000292 if (PyErr_Occurred())
293 goto Fail_result_it;
Tim Peters0e57abf2001-05-02 07:39:38 +0000294 break;
Guido van Rossum2d951851994-08-29 12:52:16 +0000295 }
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000296
Neil Schemenauer68973552003-08-14 20:37:34 +0000297 if (func == (PyObject *)&PyBool_Type || func == Py_None) {
Guido van Rossumc7903a12002-08-16 07:04:56 +0000298 ok = PyObject_IsTrue(item);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000299 }
300 else {
Guido van Rossumc7903a12002-08-16 07:04:56 +0000301 PyObject *good;
302 PyTuple_SET_ITEM(arg, 0, item);
303 good = PyObject_Call(func, arg, NULL);
304 PyTuple_SET_ITEM(arg, 0, NULL);
Guido van Rossum58b68731995-01-10 17:40:55 +0000305 if (good == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000306 Py_DECREF(item);
Tim Peters0e57abf2001-05-02 07:39:38 +0000307 goto Fail_result_it;
Guido van Rossum58b68731995-01-10 17:40:55 +0000308 }
Guido van Rossumc7903a12002-08-16 07:04:56 +0000309 ok = PyObject_IsTrue(good);
310 Py_DECREF(good);
Guido van Rossum12d12c51993-10-26 17:58:25 +0000311 }
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000312 if (ok) {
Tim Peters0e57abf2001-05-02 07:39:38 +0000313 if (j < len)
314 PyList_SET_ITEM(result, j, item);
Guido van Rossum2d951851994-08-29 12:52:16 +0000315 else {
Barry Warsawfa77e091999-01-28 18:49:12 +0000316 int status = PyList_Append(result, item);
Barry Warsawfa77e091999-01-28 18:49:12 +0000317 Py_DECREF(item);
318 if (status < 0)
Tim Peters0e57abf2001-05-02 07:39:38 +0000319 goto Fail_result_it;
Guido van Rossum2d951851994-08-29 12:52:16 +0000320 }
Tim Peters0e57abf2001-05-02 07:39:38 +0000321 ++j;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000322 }
Tim Peters0e57abf2001-05-02 07:39:38 +0000323 else
324 Py_DECREF(item);
Guido van Rossum12d12c51993-10-26 17:58:25 +0000325 }
326
Guido van Rossum12d12c51993-10-26 17:58:25 +0000327
Tim Peters0e57abf2001-05-02 07:39:38 +0000328 /* Cut back result list if len is too big. */
Guido van Rossum79f25d91997-04-29 20:08:16 +0000329 if (j < len && PyList_SetSlice(result, j, len, NULL) < 0)
Tim Peters0e57abf2001-05-02 07:39:38 +0000330 goto Fail_result_it;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000331
Tim Peters3c6b1482001-05-21 08:07:05 +0000332 Py_DECREF(it);
Guido van Rossumc7903a12002-08-16 07:04:56 +0000333 Py_DECREF(arg);
Guido van Rossum12d12c51993-10-26 17:58:25 +0000334 return result;
335
Tim Peters0e57abf2001-05-02 07:39:38 +0000336Fail_result_it:
Guido van Rossum79f25d91997-04-29 20:08:16 +0000337 Py_DECREF(result);
Tim Peters0e57abf2001-05-02 07:39:38 +0000338Fail_it:
339 Py_DECREF(it);
Guido van Rossumc7903a12002-08-16 07:04:56 +0000340Fail_arg:
341 Py_DECREF(arg);
Guido van Rossum12d12c51993-10-26 17:58:25 +0000342 return NULL;
343}
344
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000345PyDoc_STRVAR(filter_doc,
Tim Petersd50e5442002-03-09 00:06:26 +0000346"filter(function or None, sequence) -> list, tuple, or string\n"
347"\n"
348"Return those items of sequence for which function(item) is true. If\n"
349"function is None, return the items that are true. If sequence is a tuple\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000350"or string, return the same type, else return a list.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000351
Guido van Rossum79f25d91997-04-29 20:08:16 +0000352static PyObject *
Eric Smitha9f7d622008-02-17 19:46:49 +0000353builtin_format(PyObject *self, PyObject *args)
354{
355 PyObject *value;
356 PyObject *format_spec = NULL;
357
358 if (!PyArg_ParseTuple(args, "O|O:format", &value, &format_spec))
359 return NULL;
360
361 return PyObject_Format(value, format_spec);
362}
363
364PyDoc_STRVAR(format_doc,
365"format(value[, format_spec]) -> string\n\
366\n\
367Returns value.__format__(format_spec)\n\
368format_spec defaults to \"\"");
369
370static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000371builtin_chr(PyObject *self, PyObject *args)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000372{
373 long x;
374 char s[1];
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000375
Guido van Rossum79f25d91997-04-29 20:08:16 +0000376 if (!PyArg_ParseTuple(args, "l:chr", &x))
Guido van Rossum3f5da241990-12-20 15:06:42 +0000377 return NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000378 if (x < 0 || x >= 256) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000379 PyErr_SetString(PyExc_ValueError,
380 "chr() arg not in range(256)");
Guido van Rossum3f5da241990-12-20 15:06:42 +0000381 return NULL;
382 }
Guido van Rossum6bf62da1997-04-11 20:37:35 +0000383 s[0] = (char)x;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000384 return PyString_FromStringAndSize(s, 1);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000385}
386
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000387PyDoc_STRVAR(chr_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000388"chr(i) -> character\n\
389\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000390Return a string of one character with ordinal i; 0 <= i < 256.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000391
392
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000393#ifdef Py_USING_UNICODE
Guido van Rossum79f25d91997-04-29 20:08:16 +0000394static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000395builtin_unichr(PyObject *self, PyObject *args)
Guido van Rossum09095f32000-03-10 23:00:52 +0000396{
397 long x;
Guido van Rossum09095f32000-03-10 23:00:52 +0000398
399 if (!PyArg_ParseTuple(args, "l:unichr", &x))
400 return NULL;
Fredrik Lundh0dcf67e2001-06-26 20:01:56 +0000401
Marc-André Lemburgcc8764c2002-08-11 12:23:04 +0000402 return PyUnicode_FromOrdinal(x);
Guido van Rossum09095f32000-03-10 23:00:52 +0000403}
404
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000405PyDoc_STRVAR(unichr_doc,
Fred Drake078b24f2000-04-13 02:42:50 +0000406"unichr(i) -> Unicode character\n\
Guido van Rossum09095f32000-03-10 23:00:52 +0000407\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000408Return a Unicode string of one character with ordinal i; 0 <= i <= 0x10ffff.");
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000409#endif
Guido van Rossum09095f32000-03-10 23:00:52 +0000410
411
412static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000413builtin_cmp(PyObject *self, PyObject *args)
Guido van Rossuma9e7dc11992-10-18 18:53:57 +0000414{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000415 PyObject *a, *b;
Guido van Rossum09df08a1998-05-22 00:51:39 +0000416 int c;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000417
Raymond Hettingerea3fdf42002-12-29 16:33:45 +0000418 if (!PyArg_UnpackTuple(args, "cmp", 2, 2, &a, &b))
Guido van Rossuma9e7dc11992-10-18 18:53:57 +0000419 return NULL;
Guido van Rossum09df08a1998-05-22 00:51:39 +0000420 if (PyObject_Cmp(a, b, &c) < 0)
Guido van Rossumc8b6df91997-05-23 00:06:51 +0000421 return NULL;
Guido van Rossum09df08a1998-05-22 00:51:39 +0000422 return PyInt_FromLong((long)c);
Guido van Rossuma9e7dc11992-10-18 18:53:57 +0000423}
424
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000425PyDoc_STRVAR(cmp_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000426"cmp(x, y) -> integer\n\
427\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000428Return negative if x<y, zero if x==y, positive if x>y.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000429
430
Guido van Rossum79f25d91997-04-29 20:08:16 +0000431static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000432builtin_coerce(PyObject *self, PyObject *args)
Guido van Rossum6a00cd81995-01-07 12:39:01 +0000433{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000434 PyObject *v, *w;
435 PyObject *res;
Guido van Rossum5524a591995-01-10 15:26:20 +0000436
Benjamin Peterson9f4f4812008-04-27 03:01:45 +0000437 if (PyErr_WarnPy3k("coerce() not supported in 3.x", 1) < 0)
Neal Norwitzdf25efe2007-05-23 06:58:36 +0000438 return NULL;
439
Raymond Hettingerea3fdf42002-12-29 16:33:45 +0000440 if (!PyArg_UnpackTuple(args, "coerce", 2, 2, &v, &w))
Guido van Rossum5524a591995-01-10 15:26:20 +0000441 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000442 if (PyNumber_Coerce(&v, &w) < 0)
Guido van Rossum04691fc1992-08-12 15:35:34 +0000443 return NULL;
Raymond Hettinger8ae46892003-10-12 19:09:37 +0000444 res = PyTuple_Pack(2, v, w);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000445 Py_DECREF(v);
446 Py_DECREF(w);
Guido van Rossum04691fc1992-08-12 15:35:34 +0000447 return res;
448}
449
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000450PyDoc_STRVAR(coerce_doc,
Martin v. Löwis8d494f32004-08-25 10:42:41 +0000451"coerce(x, y) -> (x1, y1)\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000452\n\
Martin v. Löwis8d494f32004-08-25 10:42:41 +0000453Return a tuple consisting of the two numeric arguments converted to\n\
454a common type, using the same rules as used by arithmetic operations.\n\
455If coercion is not possible, raise TypeError.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000456
Guido van Rossum79f25d91997-04-29 20:08:16 +0000457static PyObject *
Georg Brandl5240d742007-03-13 20:46:32 +0000458builtin_compile(PyObject *self, PyObject *args, PyObject *kwds)
Guido van Rossum5b722181993-03-30 17:46:03 +0000459{
460 char *str;
461 char *filename;
462 char *startstr;
Georg Brandlf2bfd542008-03-29 13:24:23 +0000463 int mode = -1;
Tim Peters6cd6a822001-08-17 22:11:27 +0000464 int dont_inherit = 0;
465 int supplied_flags = 0;
Tim Peters5ba58662001-07-16 02:29:45 +0000466 PyCompilerFlags cf;
Neal Norwitz3a9a3e72005-11-27 20:38:31 +0000467 PyObject *result = NULL, *cmd, *tmp = NULL;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000468 Py_ssize_t length;
Georg Brandl5240d742007-03-13 20:46:32 +0000469 static char *kwlist[] = {"source", "filename", "mode", "flags",
470 "dont_inherit", NULL};
Georg Brandlf2bfd542008-03-29 13:24:23 +0000471 int start[] = {Py_file_input, Py_eval_input, Py_single_input};
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000472
Georg Brandl5240d742007-03-13 20:46:32 +0000473 if (!PyArg_ParseTupleAndKeywords(args, kwds, "Oss|ii:compile",
474 kwlist, &cmd, &filename, &startstr,
475 &supplied_flags, &dont_inherit))
Guido van Rossum5b722181993-03-30 17:46:03 +0000476 return NULL;
Tim Peters6cd6a822001-08-17 22:11:27 +0000477
Just van Rossum3aaf42c2003-02-10 08:21:10 +0000478 cf.cf_flags = supplied_flags;
479
Georg Brandlfc8eef32008-03-28 12:11:56 +0000480 if (supplied_flags &
481 ~(PyCF_MASK | PyCF_MASK_OBSOLETE | PyCF_DONT_IMPLY_DEDENT | PyCF_ONLY_AST))
482 {
483 PyErr_SetString(PyExc_ValueError,
484 "compile(): unrecognised flags");
485 return NULL;
486 }
487 /* XXX Warn if (supplied_flags & PyCF_MASK_OBSOLETE) != 0? */
488
489 if (!dont_inherit) {
490 PyEval_MergeCompilerFlags(&cf);
491 }
492
Georg Brandlf2bfd542008-03-29 13:24:23 +0000493 if (strcmp(startstr, "exec") == 0)
494 mode = 0;
495 else if (strcmp(startstr, "eval") == 0)
496 mode = 1;
497 else if (strcmp(startstr, "single") == 0)
498 mode = 2;
499 else {
500 PyErr_SetString(PyExc_ValueError,
501 "compile() arg 3 must be 'exec', 'eval' or 'single'");
502 return NULL;
503 }
504
Georg Brandlfc8eef32008-03-28 12:11:56 +0000505 if (PyAST_Check(cmd)) {
506 if (supplied_flags & PyCF_ONLY_AST) {
507 Py_INCREF(cmd);
508 result = cmd;
509 }
510 else {
511 PyArena *arena;
512 mod_ty mod;
513
514 arena = PyArena_New();
Georg Brandlf2bfd542008-03-29 13:24:23 +0000515 mod = PyAST_obj2mod(cmd, arena, mode);
Georg Brandlfc8eef32008-03-28 12:11:56 +0000516 if (mod == NULL) {
517 PyArena_Free(arena);
518 return NULL;
519 }
520 result = (PyObject*)PyAST_Compile(mod, filename,
521 &cf, arena);
522 PyArena_Free(arena);
523 }
524 return result;
525 }
526
Just van Rossum3aaf42c2003-02-10 08:21:10 +0000527#ifdef Py_USING_UNICODE
528 if (PyUnicode_Check(cmd)) {
529 tmp = PyUnicode_AsUTF8String(cmd);
530 if (tmp == NULL)
531 return NULL;
532 cmd = tmp;
533 cf.cf_flags |= PyCF_SOURCE_IS_UTF8;
534 }
535#endif
Tim Peters6cd6a822001-08-17 22:11:27 +0000536
Georg Brandlfc8eef32008-03-28 12:11:56 +0000537 if (PyObject_AsReadBuffer(cmd, (const void **)&str, &length))
Neal Norwitz3a9a3e72005-11-27 20:38:31 +0000538 goto cleanup;
Georg Brandlfc8eef32008-03-28 12:11:56 +0000539 if ((size_t)length != strlen(str)) {
540 PyErr_SetString(PyExc_TypeError,
541 "compile() expected string without null bytes");
542 goto cleanup;
Tim Peters6cd6a822001-08-17 22:11:27 +0000543 }
Georg Brandlf2bfd542008-03-29 13:24:23 +0000544 result = Py_CompileStringFlags(str, filename, start[mode], &cf);
Neal Norwitz3a9a3e72005-11-27 20:38:31 +0000545cleanup:
Just van Rossum3aaf42c2003-02-10 08:21:10 +0000546 Py_XDECREF(tmp);
547 return result;
Guido van Rossum5b722181993-03-30 17:46:03 +0000548}
549
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000550PyDoc_STRVAR(compile_doc,
Tim Peters6cd6a822001-08-17 22:11:27 +0000551"compile(source, filename, mode[, flags[, dont_inherit]]) -> code object\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000552\n\
553Compile the source string (a Python module, statement or expression)\n\
554into a code object that can be executed by the exec statement or eval().\n\
555The filename will be used for run-time error messages.\n\
556The mode must be 'exec' to compile a module, 'single' to compile a\n\
Tim Peters6cd6a822001-08-17 22:11:27 +0000557single (interactive) statement, or 'eval' to compile an expression.\n\
558The flags argument, if present, controls which future statements influence\n\
559the compilation of the code.\n\
560The dont_inherit argument, if non-zero, stops the compilation inheriting\n\
561the effects of any future statements in effect in the code calling\n\
562compile; if absent or zero these statements do influence the compilation,\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000563in addition to any features explicitly specified.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000564
Guido van Rossum79f25d91997-04-29 20:08:16 +0000565static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000566builtin_dir(PyObject *self, PyObject *args)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000567{
Tim Peters5d2b77c2001-09-03 05:47:38 +0000568 PyObject *arg = NULL;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000569
Raymond Hettingerea3fdf42002-12-29 16:33:45 +0000570 if (!PyArg_UnpackTuple(args, "dir", 0, 1, &arg))
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000571 return NULL;
Tim Peters7eea37e2001-09-04 22:08:56 +0000572 return PyObject_Dir(arg);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000573}
574
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000575PyDoc_STRVAR(dir_doc,
Tim Peters5d2b77c2001-09-03 05:47:38 +0000576"dir([object]) -> list of strings\n"
577"\n"
Georg Brandl871f1bc2007-03-12 13:17:36 +0000578"If called without an argument, return the names in the current scope.\n"
579"Else, return an alphabetized list of names comprising (some of) the attributes\n"
580"of the given object, and of attributes reachable from it.\n"
581"If the object supplies a method named __dir__, it will be used; otherwise\n"
582"the default dir() logic is used and returns:\n"
583" for a module object: the module's attributes.\n"
584" for a class object: its attributes, and recursively the attributes\n"
585" of its bases.\n"
Georg Brandl3bb15672007-03-13 07:23:16 +0000586" for any other object: its attributes, its class's attributes, and\n"
Georg Brandl871f1bc2007-03-12 13:17:36 +0000587" recursively the attributes of its class's base classes.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000588
Guido van Rossum79f25d91997-04-29 20:08:16 +0000589static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000590builtin_divmod(PyObject *self, PyObject *args)
Guido van Rossum6a00cd81995-01-07 12:39:01 +0000591{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000592 PyObject *v, *w;
Guido van Rossum6a00cd81995-01-07 12:39:01 +0000593
Raymond Hettingerea3fdf42002-12-29 16:33:45 +0000594 if (!PyArg_UnpackTuple(args, "divmod", 2, 2, &v, &w))
Guido van Rossum6a00cd81995-01-07 12:39:01 +0000595 return NULL;
Guido van Rossum09df08a1998-05-22 00:51:39 +0000596 return PyNumber_Divmod(v, w);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000597}
598
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000599PyDoc_STRVAR(divmod_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000600"divmod(x, y) -> (div, mod)\n\
601\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000602Return the tuple ((x-x%y)/y, x%y). Invariant: div*y + mod == x.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000603
604
Guido van Rossum79f25d91997-04-29 20:08:16 +0000605static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000606builtin_eval(PyObject *self, PyObject *args)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000607{
Just van Rossum3aaf42c2003-02-10 08:21:10 +0000608 PyObject *cmd, *result, *tmp = NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000609 PyObject *globals = Py_None, *locals = Py_None;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000610 char *str;
Tim Peters9fa96be2001-08-17 23:04:59 +0000611 PyCompilerFlags cf;
Guido van Rossum590baa41993-11-30 13:40:46 +0000612
Raymond Hettinger214b1c32004-07-02 06:41:07 +0000613 if (!PyArg_UnpackTuple(args, "eval", 1, 3, &cmd, &globals, &locals))
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000614 return NULL;
Raymond Hettinger214b1c32004-07-02 06:41:07 +0000615 if (locals != Py_None && !PyMapping_Check(locals)) {
616 PyErr_SetString(PyExc_TypeError, "locals must be a mapping");
Raymond Hettinger513ffe82004-07-06 13:44:41 +0000617 return NULL;
Raymond Hettinger214b1c32004-07-02 06:41:07 +0000618 }
619 if (globals != Py_None && !PyDict_Check(globals)) {
Georg Brandl99d7e4e2005-08-31 22:21:15 +0000620 PyErr_SetString(PyExc_TypeError, PyMapping_Check(globals) ?
Raymond Hettinger214b1c32004-07-02 06:41:07 +0000621 "globals must be a real dict; try eval(expr, {}, mapping)"
622 : "globals must be a dict");
Raymond Hettinger513ffe82004-07-06 13:44:41 +0000623 return NULL;
Raymond Hettinger214b1c32004-07-02 06:41:07 +0000624 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000625 if (globals == Py_None) {
626 globals = PyEval_GetGlobals();
627 if (locals == Py_None)
628 locals = PyEval_GetLocals();
Guido van Rossum6135a871995-01-09 17:53:26 +0000629 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000630 else if (locals == Py_None)
Guido van Rossum6135a871995-01-09 17:53:26 +0000631 locals = globals;
Tim Peters9fa96be2001-08-17 23:04:59 +0000632
Georg Brandl77c85e62005-09-15 10:46:13 +0000633 if (globals == NULL || locals == NULL) {
634 PyErr_SetString(PyExc_TypeError,
635 "eval must be given globals and locals "
636 "when called without a frame");
637 return NULL;
638 }
639
Guido van Rossum79f25d91997-04-29 20:08:16 +0000640 if (PyDict_GetItemString(globals, "__builtins__") == NULL) {
641 if (PyDict_SetItemString(globals, "__builtins__",
642 PyEval_GetBuiltins()) != 0)
Guido van Rossum6135a871995-01-09 17:53:26 +0000643 return NULL;
644 }
Tim Peters9fa96be2001-08-17 23:04:59 +0000645
Jeremy Hylton15c1c4f2001-07-30 21:50:55 +0000646 if (PyCode_Check(cmd)) {
Jeremy Hylton733c8932001-12-13 19:51:56 +0000647 if (PyCode_GetNumFree((PyCodeObject *)cmd) > 0) {
Jeremy Hylton15c1c4f2001-07-30 21:50:55 +0000648 PyErr_SetString(PyExc_TypeError,
649 "code object passed to eval() may not contain free variables");
650 return NULL;
651 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000652 return PyEval_EvalCode((PyCodeObject *) cmd, globals, locals);
Jeremy Hylton15c1c4f2001-07-30 21:50:55 +0000653 }
Tim Peters9fa96be2001-08-17 23:04:59 +0000654
Marc-André Lemburgd1ba4432000-09-19 21:04:18 +0000655 if (!PyString_Check(cmd) &&
656 !PyUnicode_Check(cmd)) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000657 PyErr_SetString(PyExc_TypeError,
Fred Drake661ea262000-10-24 19:57:45 +0000658 "eval() arg 1 must be a string or code object");
Guido van Rossum94390a41992-08-14 15:14:30 +0000659 return NULL;
660 }
Just van Rossum3aaf42c2003-02-10 08:21:10 +0000661 cf.cf_flags = 0;
662
663#ifdef Py_USING_UNICODE
664 if (PyUnicode_Check(cmd)) {
665 tmp = PyUnicode_AsUTF8String(cmd);
666 if (tmp == NULL)
667 return NULL;
668 cmd = tmp;
669 cf.cf_flags |= PyCF_SOURCE_IS_UTF8;
670 }
671#endif
Neal Norwitz3a9a3e72005-11-27 20:38:31 +0000672 if (PyString_AsStringAndSize(cmd, &str, NULL)) {
673 Py_XDECREF(tmp);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000674 return NULL;
Neal Norwitz3a9a3e72005-11-27 20:38:31 +0000675 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000676 while (*str == ' ' || *str == '\t')
677 str++;
Tim Peters9fa96be2001-08-17 23:04:59 +0000678
Tim Peters9fa96be2001-08-17 23:04:59 +0000679 (void)PyEval_MergeCompilerFlags(&cf);
Just van Rossum3aaf42c2003-02-10 08:21:10 +0000680 result = PyRun_StringFlags(str, Py_eval_input, globals, locals, &cf);
681 Py_XDECREF(tmp);
682 return result;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000683}
684
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000685PyDoc_STRVAR(eval_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000686"eval(source[, globals[, locals]]) -> value\n\
687\n\
688Evaluate the source in the context of globals and locals.\n\
689The source may be a string representing a Python expression\n\
690or a code object as returned by compile().\n\
Neal Norwitz477ca1c2006-09-05 02:25:41 +0000691The globals must be a dictionary and locals can be any mapping,\n\
Raymond Hettinger214b1c32004-07-02 06:41:07 +0000692defaulting to the current globals and locals.\n\
693If only globals is given, locals defaults to it.\n");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000694
695
Guido van Rossum79f25d91997-04-29 20:08:16 +0000696static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000697builtin_execfile(PyObject *self, PyObject *args)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000698{
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000699 char *filename;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000700 PyObject *globals = Py_None, *locals = Py_None;
701 PyObject *res;
Guido van Rossumb3a639e2001-09-05 13:37:47 +0000702 FILE* fp = NULL;
Tim Peters5ba58662001-07-16 02:29:45 +0000703 PyCompilerFlags cf;
Martin v. Löwis6b3a2c42001-08-08 05:30:36 +0000704 int exists;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000705
Benjamin Peterson9f4f4812008-04-27 03:01:45 +0000706 if (PyErr_WarnPy3k("execfile() not supported in 3.x; use exec()",
707 1) < 0)
Neal Norwitzdf25efe2007-05-23 06:58:36 +0000708 return NULL;
709
Raymond Hettinger66bd2332004-08-02 08:30:07 +0000710 if (!PyArg_ParseTuple(args, "s|O!O:execfile",
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000711 &filename,
Guido van Rossum79f25d91997-04-29 20:08:16 +0000712 &PyDict_Type, &globals,
Raymond Hettinger66bd2332004-08-02 08:30:07 +0000713 &locals))
Guido van Rossum0f61f8a1992-02-25 18:55:05 +0000714 return NULL;
Raymond Hettinger66bd2332004-08-02 08:30:07 +0000715 if (locals != Py_None && !PyMapping_Check(locals)) {
716 PyErr_SetString(PyExc_TypeError, "locals must be a mapping");
717 return NULL;
718 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000719 if (globals == Py_None) {
720 globals = PyEval_GetGlobals();
721 if (locals == Py_None)
722 locals = PyEval_GetLocals();
Guido van Rossum6135a871995-01-09 17:53:26 +0000723 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000724 else if (locals == Py_None)
Guido van Rossum6135a871995-01-09 17:53:26 +0000725 locals = globals;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000726 if (PyDict_GetItemString(globals, "__builtins__") == NULL) {
727 if (PyDict_SetItemString(globals, "__builtins__",
728 PyEval_GetBuiltins()) != 0)
Guido van Rossum6135a871995-01-09 17:53:26 +0000729 return NULL;
730 }
Martin v. Löwis6b3a2c42001-08-08 05:30:36 +0000731
732 exists = 0;
733 /* Test for existence or directory. */
Martin v. Löwis3484a182002-03-09 12:07:51 +0000734#if defined(PLAN9)
735 {
736 Dir *d;
737
738 if ((d = dirstat(filename))!=nil) {
739 if(d->mode & DMDIR)
740 werrstr("is a directory");
741 else
742 exists = 1;
743 free(d);
744 }
Martin v. Löwis6b3a2c42001-08-08 05:30:36 +0000745 }
Martin v. Löwis3484a182002-03-09 12:07:51 +0000746#elif defined(RISCOS)
Guido van Rossume2ae77b2001-10-24 20:42:55 +0000747 if (object_exists(filename)) {
748 if (isdir(filename))
749 errno = EISDIR;
750 else
751 exists = 1;
752 }
Martin v. Löwis3484a182002-03-09 12:07:51 +0000753#else /* standard Posix */
754 {
755 struct stat s;
756 if (stat(filename, &s) == 0) {
757 if (S_ISDIR(s.st_mode))
Andrew MacIntyreda4d6cb2004-03-29 11:53:38 +0000758# if defined(PYOS_OS2) && defined(PYCC_VACPP)
Martin v. Löwis3484a182002-03-09 12:07:51 +0000759 errno = EOS2ERR;
760# else
761 errno = EISDIR;
762# endif
763 else
764 exists = 1;
765 }
766 }
767#endif
Martin v. Löwis6b3a2c42001-08-08 05:30:36 +0000768
769 if (exists) {
770 Py_BEGIN_ALLOW_THREADS
Jack Jansen7b8c7542002-04-14 20:12:41 +0000771 fp = fopen(filename, "r" PY_STDIOTEXTMODE);
Martin v. Löwis6b3a2c42001-08-08 05:30:36 +0000772 Py_END_ALLOW_THREADS
773
774 if (fp == NULL) {
775 exists = 0;
776 }
777 }
778
779 if (!exists) {
Peter Schneider-Kamp4c013422002-08-27 16:58:00 +0000780 PyErr_SetFromErrnoWithFilename(PyExc_IOError, filename);
Guido van Rossum0f61f8a1992-02-25 18:55:05 +0000781 return NULL;
782 }
Tim Peters5ba58662001-07-16 02:29:45 +0000783 cf.cf_flags = 0;
784 if (PyEval_MergeCompilerFlags(&cf))
Tim Peters748b8bb2001-04-28 08:20:22 +0000785 res = PyRun_FileExFlags(fp, filename, Py_file_input, globals,
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000786 locals, 1, &cf);
Tim Peters5ba58662001-07-16 02:29:45 +0000787 else
Tim Peters748b8bb2001-04-28 08:20:22 +0000788 res = PyRun_FileEx(fp, filename, Py_file_input, globals,
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000789 locals, 1);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000790 return res;
Guido van Rossum0f61f8a1992-02-25 18:55:05 +0000791}
792
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000793PyDoc_STRVAR(execfile_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000794"execfile(filename[, globals[, locals]])\n\
795\n\
796Read and execute a Python script from a file.\n\
797The globals and locals are dictionaries, defaulting to the current\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000798globals and locals. If only globals is given, locals defaults to it.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000799
800
Guido van Rossum79f25d91997-04-29 20:08:16 +0000801static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000802builtin_getattr(PyObject *self, PyObject *args)
Guido van Rossum33894be1992-01-27 16:53:09 +0000803{
Guido van Rossum950ff291998-06-29 13:38:57 +0000804 PyObject *v, *result, *dflt = NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000805 PyObject *name;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000806
Raymond Hettingerea3fdf42002-12-29 16:33:45 +0000807 if (!PyArg_UnpackTuple(args, "getattr", 2, 3, &v, &name, &dflt))
Guido van Rossum33894be1992-01-27 16:53:09 +0000808 return NULL;
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000809#ifdef Py_USING_UNICODE
Jeremy Hylton0eb11152001-07-30 22:39:31 +0000810 if (PyUnicode_Check(name)) {
811 name = _PyUnicode_AsDefaultEncodedString(name, NULL);
812 if (name == NULL)
813 return NULL;
814 }
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000815#endif
Jeremy Hylton0eb11152001-07-30 22:39:31 +0000816
817 if (!PyString_Check(name)) {
818 PyErr_SetString(PyExc_TypeError,
Alex Martellia9b9c9f2003-04-23 13:34:35 +0000819 "getattr(): attribute name must be string");
Jeremy Hylton0eb11152001-07-30 22:39:31 +0000820 return NULL;
821 }
Guido van Rossum950ff291998-06-29 13:38:57 +0000822 result = PyObject_GetAttr(v, name);
Guido van Rossumd8923572001-10-16 21:31:32 +0000823 if (result == NULL && dflt != NULL &&
824 PyErr_ExceptionMatches(PyExc_AttributeError))
825 {
Guido van Rossum950ff291998-06-29 13:38:57 +0000826 PyErr_Clear();
827 Py_INCREF(dflt);
828 result = dflt;
829 }
830 return result;
Guido van Rossum9bfef441993-03-29 10:43:31 +0000831}
832
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000833PyDoc_STRVAR(getattr_doc,
Guido van Rossum950ff291998-06-29 13:38:57 +0000834"getattr(object, name[, default]) -> value\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000835\n\
Guido van Rossum950ff291998-06-29 13:38:57 +0000836Get a named attribute from an object; getattr(x, 'y') is equivalent to x.y.\n\
837When a default argument is given, it is returned when the attribute doesn't\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000838exist; without it, an exception is raised in that case.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000839
840
Guido van Rossum79f25d91997-04-29 20:08:16 +0000841static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000842builtin_globals(PyObject *self)
Guido van Rossum872537c1995-07-07 22:43:42 +0000843{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000844 PyObject *d;
Guido van Rossum872537c1995-07-07 22:43:42 +0000845
Guido van Rossum79f25d91997-04-29 20:08:16 +0000846 d = PyEval_GetGlobals();
Neal Norwitz43bd4db2006-08-12 01:46:42 +0000847 Py_XINCREF(d);
Guido van Rossum872537c1995-07-07 22:43:42 +0000848 return d;
849}
850
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000851PyDoc_STRVAR(globals_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000852"globals() -> dictionary\n\
853\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000854Return the dictionary containing the current scope's global variables.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000855
856
Guido van Rossum79f25d91997-04-29 20:08:16 +0000857static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000858builtin_hasattr(PyObject *self, PyObject *args)
Guido van Rossum9bfef441993-03-29 10:43:31 +0000859{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000860 PyObject *v;
861 PyObject *name;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000862
Raymond Hettingerea3fdf42002-12-29 16:33:45 +0000863 if (!PyArg_UnpackTuple(args, "hasattr", 2, 2, &v, &name))
Guido van Rossum9bfef441993-03-29 10:43:31 +0000864 return NULL;
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000865#ifdef Py_USING_UNICODE
Jeremy Hylton302b54a2001-07-30 22:45:19 +0000866 if (PyUnicode_Check(name)) {
867 name = _PyUnicode_AsDefaultEncodedString(name, NULL);
868 if (name == NULL)
869 return NULL;
870 }
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000871#endif
Jeremy Hylton302b54a2001-07-30 22:45:19 +0000872
873 if (!PyString_Check(name)) {
874 PyErr_SetString(PyExc_TypeError,
Alex Martellia9b9c9f2003-04-23 13:34:35 +0000875 "hasattr(): attribute name must be string");
Jeremy Hylton302b54a2001-07-30 22:45:19 +0000876 return NULL;
877 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000878 v = PyObject_GetAttr(v, name);
Guido van Rossum9bfef441993-03-29 10:43:31 +0000879 if (v == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000880 PyErr_Clear();
Guido van Rossum09df08a1998-05-22 00:51:39 +0000881 Py_INCREF(Py_False);
882 return Py_False;
Guido van Rossum9bfef441993-03-29 10:43:31 +0000883 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000884 Py_DECREF(v);
Guido van Rossum09df08a1998-05-22 00:51:39 +0000885 Py_INCREF(Py_True);
886 return Py_True;
Guido van Rossum33894be1992-01-27 16:53:09 +0000887}
888
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000889PyDoc_STRVAR(hasattr_doc,
Guido van Rossum77f6a652002-04-03 22:41:51 +0000890"hasattr(object, name) -> bool\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000891\n\
892Return whether the object has an attribute with the given name.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000893(This is done by calling getattr(object, name) and catching exceptions.)");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000894
895
Guido van Rossum79f25d91997-04-29 20:08:16 +0000896static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000897builtin_id(PyObject *self, PyObject *v)
Guido van Rossum5b722181993-03-30 17:46:03 +0000898{
Guido van Rossum106f2da2000-06-28 21:12:25 +0000899 return PyLong_FromVoidPtr(v);
Guido van Rossum5b722181993-03-30 17:46:03 +0000900}
901
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000902PyDoc_STRVAR(id_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000903"id(object) -> integer\n\
904\n\
905Return the identity of an object. This is guaranteed to be unique among\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000906simultaneously existing objects. (Hint: it's the object's memory address.)");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000907
908
Guido van Rossum79f25d91997-04-29 20:08:16 +0000909static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000910builtin_map(PyObject *self, PyObject *args)
Guido van Rossum12d12c51993-10-26 17:58:25 +0000911{
912 typedef struct {
Tim Peters4e9afdc2001-05-03 23:54:49 +0000913 PyObject *it; /* the iterator object */
914 int saw_StopIteration; /* bool: did the iterator end? */
Guido van Rossum12d12c51993-10-26 17:58:25 +0000915 } sequence;
916
Guido van Rossum79f25d91997-04-29 20:08:16 +0000917 PyObject *func, *result;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000918 sequence *seqs = NULL, *sqp;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000919 Py_ssize_t n, len;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000920 register int i, j;
921
Guido van Rossum79f25d91997-04-29 20:08:16 +0000922 n = PyTuple_Size(args);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000923 if (n < 2) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000924 PyErr_SetString(PyExc_TypeError,
925 "map() requires at least two args");
Guido van Rossum12d12c51993-10-26 17:58:25 +0000926 return NULL;
927 }
928
Guido van Rossum79f25d91997-04-29 20:08:16 +0000929 func = PyTuple_GetItem(args, 0);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000930 n--;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000931
Neal Norwitz53152a12008-02-24 02:20:25 +0000932 if (func == Py_None) {
Benjamin Peterson9f4f4812008-04-27 03:01:45 +0000933 if (PyErr_WarnPy3k("map(None, ...) not supported in 3.x; "
934 "use list(...)", 1) < 0)
Neal Norwitz53152a12008-02-24 02:20:25 +0000935 return NULL;
936 if (n == 1) {
937 /* map(None, S) is the same as list(S). */
938 return PySequence_List(PyTuple_GetItem(args, 1));
939 }
Guido van Rossumfa4ac711998-07-10 17:37:30 +0000940 }
941
Tim Peters4e9afdc2001-05-03 23:54:49 +0000942 /* Get space for sequence descriptors. Must NULL out the iterator
943 * pointers so that jumping to Fail_2 later doesn't see trash.
944 */
Guido van Rossum79f25d91997-04-29 20:08:16 +0000945 if ((seqs = PyMem_NEW(sequence, n)) == NULL) {
946 PyErr_NoMemory();
Tim Peters4e9afdc2001-05-03 23:54:49 +0000947 return NULL;
948 }
949 for (i = 0; i < n; ++i) {
950 seqs[i].it = (PyObject*)NULL;
951 seqs[i].saw_StopIteration = 0;
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000952 }
Guido van Rossum12d12c51993-10-26 17:58:25 +0000953
Tim Peters4e9afdc2001-05-03 23:54:49 +0000954 /* Do a first pass to obtain iterators for the arguments, and set len
955 * to the largest of their lengths.
Tim Peters748b8bb2001-04-28 08:20:22 +0000956 */
Tim Peters4e9afdc2001-05-03 23:54:49 +0000957 len = 0;
958 for (i = 0, sqp = seqs; i < n; ++i, ++sqp) {
959 PyObject *curseq;
Martin v. Löwisd96ee902006-02-16 14:37:16 +0000960 Py_ssize_t curlen;
Guido van Rossumad991772001-01-12 16:03:05 +0000961
Tim Peters4e9afdc2001-05-03 23:54:49 +0000962 /* Get iterator. */
963 curseq = PyTuple_GetItem(args, i+1);
964 sqp->it = PyObject_GetIter(curseq);
965 if (sqp->it == NULL) {
Guido van Rossum12d12c51993-10-26 17:58:25 +0000966 static char errmsg[] =
Tim Peters4e9afdc2001-05-03 23:54:49 +0000967 "argument %d to map() must support iteration";
Guido van Rossum15e33a41997-04-30 19:00:27 +0000968 char errbuf[sizeof(errmsg) + 25];
Jeremy Hylton518ab1c2001-11-28 20:42:20 +0000969 PyOS_snprintf(errbuf, sizeof(errbuf), errmsg, i+2);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000970 PyErr_SetString(PyExc_TypeError, errbuf);
Guido van Rossum12d12c51993-10-26 17:58:25 +0000971 goto Fail_2;
972 }
973
Tim Peters4e9afdc2001-05-03 23:54:49 +0000974 /* Update len. */
Raymond Hettinger4e2f7142007-12-06 00:56:53 +0000975 curlen = _PyObject_LengthHint(curseq, 8);
Raymond Hettinger77f3c872004-01-04 08:54:44 +0000976 if (curlen > len)
977 len = curlen;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000978 }
979
Tim Peters4e9afdc2001-05-03 23:54:49 +0000980 /* Get space for the result list. */
Guido van Rossum79f25d91997-04-29 20:08:16 +0000981 if ((result = (PyObject *) PyList_New(len)) == NULL)
Guido van Rossum12d12c51993-10-26 17:58:25 +0000982 goto Fail_2;
983
Tim Peters4e9afdc2001-05-03 23:54:49 +0000984 /* Iterate over the sequences until all have stopped. */
Guido van Rossum2d951851994-08-29 12:52:16 +0000985 for (i = 0; ; ++i) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000986 PyObject *alist, *item=NULL, *value;
Tim Peters4e9afdc2001-05-03 23:54:49 +0000987 int numactive = 0;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000988
Guido van Rossum79f25d91997-04-29 20:08:16 +0000989 if (func == Py_None && n == 1)
Guido van Rossum32120311995-07-10 13:52:21 +0000990 alist = NULL;
Tim Peters4e9afdc2001-05-03 23:54:49 +0000991 else if ((alist = PyTuple_New(n)) == NULL)
992 goto Fail_1;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000993
994 for (j = 0, sqp = seqs; j < n; ++j, ++sqp) {
Tim Peters4e9afdc2001-05-03 23:54:49 +0000995 if (sqp->saw_StopIteration) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000996 Py_INCREF(Py_None);
997 item = Py_None;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000998 }
Guido van Rossum12d12c51993-10-26 17:58:25 +0000999 else {
Tim Peters4e9afdc2001-05-03 23:54:49 +00001000 item = PyIter_Next(sqp->it);
1001 if (item)
1002 ++numactive;
1003 else {
Tim Peters4e9afdc2001-05-03 23:54:49 +00001004 if (PyErr_Occurred()) {
Tim Petersf4848da2001-05-05 00:14:56 +00001005 Py_XDECREF(alist);
1006 goto Fail_1;
Guido van Rossum2d951851994-08-29 12:52:16 +00001007 }
Tim Peters4e9afdc2001-05-03 23:54:49 +00001008 Py_INCREF(Py_None);
1009 item = Py_None;
1010 sqp->saw_StopIteration = 1;
Guido van Rossum2d951851994-08-29 12:52:16 +00001011 }
Guido van Rossum12d12c51993-10-26 17:58:25 +00001012 }
Tim Peters4e9afdc2001-05-03 23:54:49 +00001013 if (alist)
1014 PyTuple_SET_ITEM(alist, j, item);
1015 else
Guido van Rossum2d951851994-08-29 12:52:16 +00001016 break;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001017 }
1018
Guido van Rossum32120311995-07-10 13:52:21 +00001019 if (!alist)
1020 alist = item;
Guido van Rossum2d951851994-08-29 12:52:16 +00001021
Tim Peters4e9afdc2001-05-03 23:54:49 +00001022 if (numactive == 0) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001023 Py_DECREF(alist);
Guido van Rossum2d951851994-08-29 12:52:16 +00001024 break;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001025 }
Guido van Rossum2d951851994-08-29 12:52:16 +00001026
Guido van Rossum79f25d91997-04-29 20:08:16 +00001027 if (func == Py_None)
Guido van Rossum32120311995-07-10 13:52:21 +00001028 value = alist;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001029 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001030 value = PyEval_CallObject(func, alist);
1031 Py_DECREF(alist);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00001032 if (value == NULL)
1033 goto Fail_1;
Guido van Rossum2d951851994-08-29 12:52:16 +00001034 }
1035 if (i >= len) {
Barry Warsawfa77e091999-01-28 18:49:12 +00001036 int status = PyList_Append(result, value);
Barry Warsaw21332871999-01-28 04:21:35 +00001037 Py_DECREF(value);
Barry Warsawfa77e091999-01-28 18:49:12 +00001038 if (status < 0)
1039 goto Fail_1;
Guido van Rossum2d951851994-08-29 12:52:16 +00001040 }
Tim Peters4e9afdc2001-05-03 23:54:49 +00001041 else if (PyList_SetItem(result, i, value) < 0)
1042 goto Fail_1;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001043 }
1044
Guido van Rossumfa4ac711998-07-10 17:37:30 +00001045 if (i < len && PyList_SetSlice(result, i, len, NULL) < 0)
1046 goto Fail_1;
1047
Tim Peters4e9afdc2001-05-03 23:54:49 +00001048 goto Succeed;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001049
Guido van Rossum12d12c51993-10-26 17:58:25 +00001050Fail_1:
Guido van Rossum79f25d91997-04-29 20:08:16 +00001051 Py_DECREF(result);
Guido van Rossum12d12c51993-10-26 17:58:25 +00001052Fail_2:
Tim Peters4e9afdc2001-05-03 23:54:49 +00001053 result = NULL;
1054Succeed:
1055 assert(seqs);
1056 for (i = 0; i < n; ++i)
1057 Py_XDECREF(seqs[i].it);
1058 PyMem_DEL(seqs);
1059 return result;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001060}
1061
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001062PyDoc_STRVAR(map_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001063"map(function, sequence[, sequence, ...]) -> list\n\
1064\n\
1065Return a list of the results of applying the function to the items of\n\
1066the argument sequence(s). If more than one sequence is given, the\n\
1067function is called with an argument list consisting of the corresponding\n\
1068item of each sequence, substituting None for missing values when not all\n\
1069sequences have the same length. If the function is None, return a list of\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001070the items of the sequence (or a list of tuples if more than one sequence).");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001071
1072
Guido van Rossum79f25d91997-04-29 20:08:16 +00001073static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001074builtin_setattr(PyObject *self, PyObject *args)
Guido van Rossum33894be1992-01-27 16:53:09 +00001075{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001076 PyObject *v;
1077 PyObject *name;
1078 PyObject *value;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001079
Raymond Hettingerea3fdf42002-12-29 16:33:45 +00001080 if (!PyArg_UnpackTuple(args, "setattr", 3, 3, &v, &name, &value))
Guido van Rossum33894be1992-01-27 16:53:09 +00001081 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001082 if (PyObject_SetAttr(v, name, value) != 0)
Guido van Rossum33894be1992-01-27 16:53:09 +00001083 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001084 Py_INCREF(Py_None);
1085 return Py_None;
Guido van Rossum33894be1992-01-27 16:53:09 +00001086}
1087
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001088PyDoc_STRVAR(setattr_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001089"setattr(object, name, value)\n\
1090\n\
1091Set a named attribute on an object; setattr(x, 'y', v) is equivalent to\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001092``x.y = v''.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001093
1094
Guido van Rossum79f25d91997-04-29 20:08:16 +00001095static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001096builtin_delattr(PyObject *self, PyObject *args)
Guido van Rossum14144fc1994-08-29 12:53:40 +00001097{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001098 PyObject *v;
1099 PyObject *name;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001100
Raymond Hettingerea3fdf42002-12-29 16:33:45 +00001101 if (!PyArg_UnpackTuple(args, "delattr", 2, 2, &v, &name))
Guido van Rossum14144fc1994-08-29 12:53:40 +00001102 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001103 if (PyObject_SetAttr(v, name, (PyObject *)NULL) != 0)
Guido van Rossum14144fc1994-08-29 12:53:40 +00001104 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001105 Py_INCREF(Py_None);
1106 return Py_None;
Guido van Rossum14144fc1994-08-29 12:53:40 +00001107}
1108
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001109PyDoc_STRVAR(delattr_doc,
Guido van Rossumdf12a591998-11-23 22:13:04 +00001110"delattr(object, name)\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001111\n\
1112Delete a named attribute on an object; delattr(x, 'y') is equivalent to\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001113``del x.y''.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001114
1115
Guido van Rossum79f25d91997-04-29 20:08:16 +00001116static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001117builtin_hash(PyObject *self, PyObject *v)
Guido van Rossum9bfef441993-03-29 10:43:31 +00001118{
Guido van Rossum9bfef441993-03-29 10:43:31 +00001119 long x;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001120
Guido van Rossum79f25d91997-04-29 20:08:16 +00001121 x = PyObject_Hash(v);
Guido van Rossum9bfef441993-03-29 10:43:31 +00001122 if (x == -1)
1123 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001124 return PyInt_FromLong(x);
Guido van Rossum9bfef441993-03-29 10:43:31 +00001125}
1126
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001127PyDoc_STRVAR(hash_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001128"hash(object) -> integer\n\
1129\n\
1130Return a hash value for the object. Two objects with the same value have\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001131the same hash value. The reverse is not necessarily true, but likely.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001132
1133
Guido van Rossum79f25d91997-04-29 20:08:16 +00001134static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001135builtin_hex(PyObject *self, PyObject *v)
Guido van Rossum006bcd41991-10-24 14:54:44 +00001136{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001137 PyNumberMethods *nb;
Neil Schemenauer3a313e32004-07-19 16:29:17 +00001138 PyObject *res;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001139
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001140 if ((nb = v->ob_type->tp_as_number) == NULL ||
Guido van Rossum1899c2e1992-09-12 11:09:23 +00001141 nb->nb_hex == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001142 PyErr_SetString(PyExc_TypeError,
Guido van Rossum1899c2e1992-09-12 11:09:23 +00001143 "hex() argument can't be converted to hex");
1144 return NULL;
Guido van Rossum006bcd41991-10-24 14:54:44 +00001145 }
Neil Schemenauer3a313e32004-07-19 16:29:17 +00001146 res = (*nb->nb_hex)(v);
1147 if (res && !PyString_Check(res)) {
1148 PyErr_Format(PyExc_TypeError,
1149 "__hex__ returned non-string (type %.200s)",
1150 res->ob_type->tp_name);
1151 Py_DECREF(res);
1152 return NULL;
1153 }
1154 return res;
Guido van Rossum006bcd41991-10-24 14:54:44 +00001155}
1156
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001157PyDoc_STRVAR(hex_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001158"hex(number) -> string\n\
1159\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001160Return the hexadecimal representation of an integer or long integer.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001161
1162
Tim Petersdbd9ba62000-07-09 03:09:57 +00001163static PyObject *builtin_raw_input(PyObject *, PyObject *);
Guido van Rossum3165fe61992-09-25 21:59:05 +00001164
Guido van Rossum79f25d91997-04-29 20:08:16 +00001165static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001166builtin_input(PyObject *self, PyObject *args)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001167{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001168 PyObject *line;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001169 char *str;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001170 PyObject *res;
1171 PyObject *globals, *locals;
Hye-Shik Changff83c2b2004-02-02 13:39:01 +00001172 PyCompilerFlags cf;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001173
1174 line = builtin_raw_input(self, args);
Guido van Rossum3165fe61992-09-25 21:59:05 +00001175 if (line == NULL)
1176 return line;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001177 if (!PyArg_Parse(line, "s;embedded '\\0' in input line", &str))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001178 return NULL;
1179 while (*str == ' ' || *str == '\t')
1180 str++;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001181 globals = PyEval_GetGlobals();
1182 locals = PyEval_GetLocals();
1183 if (PyDict_GetItemString(globals, "__builtins__") == NULL) {
1184 if (PyDict_SetItemString(globals, "__builtins__",
1185 PyEval_GetBuiltins()) != 0)
Guido van Rossum6135a871995-01-09 17:53:26 +00001186 return NULL;
1187 }
Hye-Shik Changff83c2b2004-02-02 13:39:01 +00001188 cf.cf_flags = 0;
1189 PyEval_MergeCompilerFlags(&cf);
1190 res = PyRun_StringFlags(str, Py_eval_input, globals, locals, &cf);
Guido van Rossum79f25d91997-04-29 20:08:16 +00001191 Py_DECREF(line);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001192 return res;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001193}
1194
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001195PyDoc_STRVAR(input_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001196"input([prompt]) -> value\n\
1197\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001198Equivalent to eval(raw_input(prompt)).");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001199
1200
Guido van Rossume8811f81997-02-14 15:48:05 +00001201static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001202builtin_intern(PyObject *self, PyObject *args)
Guido van Rossume8811f81997-02-14 15:48:05 +00001203{
1204 PyObject *s;
Guido van Rossum43713e52000-02-29 13:59:29 +00001205 if (!PyArg_ParseTuple(args, "S:intern", &s))
Guido van Rossume8811f81997-02-14 15:48:05 +00001206 return NULL;
Jeremy Hylton4c989dd2004-08-07 19:20:05 +00001207 if (!PyString_CheckExact(s)) {
1208 PyErr_SetString(PyExc_TypeError,
1209 "can't intern subclass of string");
1210 return NULL;
1211 }
Guido van Rossume8811f81997-02-14 15:48:05 +00001212 Py_INCREF(s);
1213 PyString_InternInPlace(&s);
1214 return s;
1215}
1216
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001217PyDoc_STRVAR(intern_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001218"intern(string) -> string\n\
1219\n\
1220``Intern'' the given string. This enters the string in the (global)\n\
1221table of interned strings whose purpose is to speed up dictionary lookups.\n\
1222Return the string itself or the previously interned string object with the\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001223same value.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001224
1225
Guido van Rossum79f25d91997-04-29 20:08:16 +00001226static PyObject *
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001227builtin_iter(PyObject *self, PyObject *args)
1228{
1229 PyObject *v, *w = NULL;
1230
Raymond Hettingerea3fdf42002-12-29 16:33:45 +00001231 if (!PyArg_UnpackTuple(args, "iter", 1, 2, &v, &w))
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001232 return NULL;
1233 if (w == NULL)
1234 return PyObject_GetIter(v);
1235 if (!PyCallable_Check(v)) {
1236 PyErr_SetString(PyExc_TypeError,
1237 "iter(v, w): v must be callable");
1238 return NULL;
1239 }
1240 return PyCallIter_New(v, w);
1241}
1242
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001243PyDoc_STRVAR(iter_doc,
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001244"iter(collection) -> iterator\n\
1245iter(callable, sentinel) -> iterator\n\
1246\n\
1247Get an iterator from an object. In the first form, the argument must\n\
1248supply its own iterator, or be a sequence.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001249In the second form, the callable is called until it returns the sentinel.");
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001250
1251
1252static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001253builtin_len(PyObject *self, PyObject *v)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001254{
Martin v. Löwis18e16552006-02-15 17:27:45 +00001255 Py_ssize_t res;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001256
Jeremy Hylton03657cf2000-07-12 13:05:33 +00001257 res = PyObject_Size(v);
Guido van Rossum8ea9f4d1998-06-29 22:26:50 +00001258 if (res < 0 && PyErr_Occurred())
1259 return NULL;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001260 return PyInt_FromSsize_t(res);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001261}
1262
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001263PyDoc_STRVAR(len_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001264"len(object) -> integer\n\
1265\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001266Return the number of items of a sequence or mapping.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001267
1268
Guido van Rossum79f25d91997-04-29 20:08:16 +00001269static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001270builtin_locals(PyObject *self)
Guido van Rossum872537c1995-07-07 22:43:42 +00001271{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001272 PyObject *d;
Guido van Rossum872537c1995-07-07 22:43:42 +00001273
Guido van Rossum79f25d91997-04-29 20:08:16 +00001274 d = PyEval_GetLocals();
Neal Norwitz43bd4db2006-08-12 01:46:42 +00001275 Py_XINCREF(d);
Guido van Rossum872537c1995-07-07 22:43:42 +00001276 return d;
1277}
1278
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001279PyDoc_STRVAR(locals_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001280"locals() -> dictionary\n\
1281\n\
Raymond Hettinger69bf8f32003-01-04 02:16:22 +00001282Update and return a dictionary containing the current scope's local variables.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001283
1284
Guido van Rossum79f25d91997-04-29 20:08:16 +00001285static PyObject *
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001286min_max(PyObject *args, PyObject *kwds, int op)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001287{
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001288 PyObject *v, *it, *item, *val, *maxitem, *maxval, *keyfunc=NULL;
Martin v. Löwisfd39ad42004-08-12 14:42:37 +00001289 const char *name = op == Py_LT ? "min" : "max";
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001290
Guido van Rossum79f25d91997-04-29 20:08:16 +00001291 if (PyTuple_Size(args) > 1)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001292 v = args;
Martin v. Löwisfd39ad42004-08-12 14:42:37 +00001293 else if (!PyArg_UnpackTuple(args, (char *)name, 1, 1, &v))
Guido van Rossum3f5da241990-12-20 15:06:42 +00001294 return NULL;
Tim Peters67d687a2002-04-29 21:27:32 +00001295
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001296 if (kwds != NULL && PyDict_Check(kwds) && PyDict_Size(kwds)) {
1297 keyfunc = PyDict_GetItemString(kwds, "key");
1298 if (PyDict_Size(kwds)!=1 || keyfunc == NULL) {
Georg Brandl99d7e4e2005-08-31 22:21:15 +00001299 PyErr_Format(PyExc_TypeError,
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001300 "%s() got an unexpected keyword argument", name);
1301 return NULL;
1302 }
Guido van Rossum1d9a9ea2008-01-23 20:19:01 +00001303 Py_INCREF(keyfunc);
Georg Brandl99d7e4e2005-08-31 22:21:15 +00001304 }
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001305
Tim Petersc3074532001-05-03 07:00:32 +00001306 it = PyObject_GetIter(v);
Guido van Rossum1d9a9ea2008-01-23 20:19:01 +00001307 if (it == NULL) {
1308 Py_XDECREF(keyfunc);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001309 return NULL;
Guido van Rossum1d9a9ea2008-01-23 20:19:01 +00001310 }
Tim Petersc3074532001-05-03 07:00:32 +00001311
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001312 maxitem = NULL; /* the result */
1313 maxval = NULL; /* the value associated with the result */
Brett Cannon9e635cf2004-12-07 00:25:35 +00001314 while (( item = PyIter_Next(it) )) {
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001315 /* get the value from the key function */
1316 if (keyfunc != NULL) {
1317 val = PyObject_CallFunctionObjArgs(keyfunc, item, NULL);
1318 if (val == NULL)
1319 goto Fail_it_item;
1320 }
1321 /* no key function; the value is the item */
1322 else {
1323 val = item;
1324 Py_INCREF(val);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001325 }
Tim Petersc3074532001-05-03 07:00:32 +00001326
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001327 /* maximum value and item are unset; set them */
1328 if (maxval == NULL) {
1329 maxitem = item;
1330 maxval = val;
1331 }
1332 /* maximum value and item are set; update them as necessary */
Guido van Rossum2d951851994-08-29 12:52:16 +00001333 else {
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001334 int cmp = PyObject_RichCompareBool(val, maxval, op);
1335 if (cmp < 0)
1336 goto Fail_it_item_and_val;
1337 else if (cmp > 0) {
1338 Py_DECREF(maxval);
1339 Py_DECREF(maxitem);
1340 maxval = val;
1341 maxitem = item;
Guido van Rossum53451b32001-01-17 15:47:24 +00001342 }
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001343 else {
1344 Py_DECREF(item);
1345 Py_DECREF(val);
Guido van Rossumc8b6df91997-05-23 00:06:51 +00001346 }
Guido van Rossum2d951851994-08-29 12:52:16 +00001347 }
Guido van Rossum3f5da241990-12-20 15:06:42 +00001348 }
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001349 if (PyErr_Occurred())
1350 goto Fail_it;
1351 if (maxval == NULL) {
Martin v. Löwisfd39ad42004-08-12 14:42:37 +00001352 PyErr_Format(PyExc_ValueError,
1353 "%s() arg is an empty sequence", name);
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001354 assert(maxitem == NULL);
1355 }
1356 else
1357 Py_DECREF(maxval);
Tim Petersc3074532001-05-03 07:00:32 +00001358 Py_DECREF(it);
Guido van Rossum1d9a9ea2008-01-23 20:19:01 +00001359 Py_XDECREF(keyfunc);
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001360 return maxitem;
1361
1362Fail_it_item_and_val:
1363 Py_DECREF(val);
1364Fail_it_item:
1365 Py_DECREF(item);
1366Fail_it:
1367 Py_XDECREF(maxval);
1368 Py_XDECREF(maxitem);
1369 Py_DECREF(it);
Guido van Rossum1d9a9ea2008-01-23 20:19:01 +00001370 Py_XDECREF(keyfunc);
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001371 return NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001372}
1373
Guido van Rossum79f25d91997-04-29 20:08:16 +00001374static PyObject *
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001375builtin_min(PyObject *self, PyObject *args, PyObject *kwds)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001376{
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001377 return min_max(args, kwds, Py_LT);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001378}
1379
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001380PyDoc_STRVAR(min_doc,
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001381"min(iterable[, key=func]) -> value\n\
1382min(a, b, c, ...[, key=func]) -> value\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001383\n\
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001384With a single iterable argument, return its smallest item.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001385With two or more arguments, return the smallest argument.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001386
1387
Guido van Rossum79f25d91997-04-29 20:08:16 +00001388static PyObject *
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001389builtin_max(PyObject *self, PyObject *args, PyObject *kwds)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001390{
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001391 return min_max(args, kwds, Py_GT);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001392}
1393
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001394PyDoc_STRVAR(max_doc,
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001395"max(iterable[, key=func]) -> value\n\
1396max(a, b, c, ...[, key=func]) -> value\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001397\n\
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001398With a single iterable argument, return its largest item.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001399With two or more arguments, return the largest argument.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001400
1401
Guido van Rossum79f25d91997-04-29 20:08:16 +00001402static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001403builtin_oct(PyObject *self, PyObject *v)
Guido van Rossum006bcd41991-10-24 14:54:44 +00001404{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001405 PyNumberMethods *nb;
Neil Schemenauer3a313e32004-07-19 16:29:17 +00001406 PyObject *res;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001407
Guido van Rossum1899c2e1992-09-12 11:09:23 +00001408 if (v == NULL || (nb = v->ob_type->tp_as_number) == NULL ||
1409 nb->nb_oct == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001410 PyErr_SetString(PyExc_TypeError,
Guido van Rossum1899c2e1992-09-12 11:09:23 +00001411 "oct() argument can't be converted to oct");
1412 return NULL;
Guido van Rossum006bcd41991-10-24 14:54:44 +00001413 }
Neil Schemenauer3a313e32004-07-19 16:29:17 +00001414 res = (*nb->nb_oct)(v);
1415 if (res && !PyString_Check(res)) {
1416 PyErr_Format(PyExc_TypeError,
1417 "__oct__ returned non-string (type %.200s)",
1418 res->ob_type->tp_name);
1419 Py_DECREF(res);
1420 return NULL;
1421 }
1422 return res;
Guido van Rossum006bcd41991-10-24 14:54:44 +00001423}
1424
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001425PyDoc_STRVAR(oct_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001426"oct(number) -> string\n\
1427\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001428Return the octal representation of an integer or long integer.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001429
1430
Guido van Rossum79f25d91997-04-29 20:08:16 +00001431static PyObject *
Neal Norwitzc4edb0e2006-05-02 04:43:14 +00001432builtin_open(PyObject *self, PyObject *args, PyObject *kwds)
1433{
1434 return PyObject_Call((PyObject*)&PyFile_Type, args, kwds);
1435}
1436
1437PyDoc_STRVAR(open_doc,
1438"open(name[, mode[, buffering]]) -> file object\n\
1439\n\
Skip Montanaro4e3ebe02007-12-08 14:37:43 +00001440Open a file using the file() type, returns a file object. This is the\n\
1441preferred way to open a file.");
Neal Norwitzc4edb0e2006-05-02 04:43:14 +00001442
1443
1444static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001445builtin_ord(PyObject *self, PyObject* obj)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001446{
Guido van Rossum09095f32000-03-10 23:00:52 +00001447 long ord;
Martin v. Löwisd96ee902006-02-16 14:37:16 +00001448 Py_ssize_t size;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001449
Jeremy Hylton394b54d2000-04-12 21:19:47 +00001450 if (PyString_Check(obj)) {
1451 size = PyString_GET_SIZE(obj);
Andrew M. Kuchling34c20cf2000-12-20 14:36:56 +00001452 if (size == 1) {
Jeremy Hylton394b54d2000-04-12 21:19:47 +00001453 ord = (long)((unsigned char)*PyString_AS_STRING(obj));
Andrew M. Kuchling34c20cf2000-12-20 14:36:56 +00001454 return PyInt_FromLong(ord);
1455 }
Christian Heimes1a6387e2008-03-26 12:49:49 +00001456 } else if (PyBytes_Check(obj)) {
1457 size = PyBytes_GET_SIZE(obj);
1458 if (size == 1) {
1459 ord = (long)((unsigned char)*PyBytes_AS_STRING(obj));
1460 return PyInt_FromLong(ord);
1461 }
1462
Martin v. Löwis339d0f72001-08-17 18:39:25 +00001463#ifdef Py_USING_UNICODE
Jeremy Hylton394b54d2000-04-12 21:19:47 +00001464 } else if (PyUnicode_Check(obj)) {
1465 size = PyUnicode_GET_SIZE(obj);
Andrew M. Kuchling34c20cf2000-12-20 14:36:56 +00001466 if (size == 1) {
Jeremy Hylton394b54d2000-04-12 21:19:47 +00001467 ord = (long)*PyUnicode_AS_UNICODE(obj);
Andrew M. Kuchling34c20cf2000-12-20 14:36:56 +00001468 return PyInt_FromLong(ord);
1469 }
Martin v. Löwis339d0f72001-08-17 18:39:25 +00001470#endif
Jeremy Hylton394b54d2000-04-12 21:19:47 +00001471 } else {
1472 PyErr_Format(PyExc_TypeError,
Andrew M. Kuchlingf07aad12000-12-23 14:11:28 +00001473 "ord() expected string of length 1, but " \
Jeremy Hylton394b54d2000-04-12 21:19:47 +00001474 "%.200s found", obj->ob_type->tp_name);
Guido van Rossum09095f32000-03-10 23:00:52 +00001475 return NULL;
1476 }
1477
Guido van Rossumad991772001-01-12 16:03:05 +00001478 PyErr_Format(PyExc_TypeError,
Andrew M. Kuchlingf07aad12000-12-23 14:11:28 +00001479 "ord() expected a character, "
Martin v. Löwisd96ee902006-02-16 14:37:16 +00001480 "but string of length %zd found",
Jeremy Hylton394b54d2000-04-12 21:19:47 +00001481 size);
1482 return NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001483}
1484
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001485PyDoc_STRVAR(ord_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001486"ord(c) -> integer\n\
1487\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001488Return the integer ordinal of a one-character string.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001489
1490
Guido van Rossum79f25d91997-04-29 20:08:16 +00001491static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001492builtin_pow(PyObject *self, PyObject *args)
Guido van Rossum6a00cd81995-01-07 12:39:01 +00001493{
Guido van Rossum09df08a1998-05-22 00:51:39 +00001494 PyObject *v, *w, *z = Py_None;
Guido van Rossum6a00cd81995-01-07 12:39:01 +00001495
Raymond Hettingerea3fdf42002-12-29 16:33:45 +00001496 if (!PyArg_UnpackTuple(args, "pow", 2, 3, &v, &w, &z))
Guido van Rossum6a00cd81995-01-07 12:39:01 +00001497 return NULL;
Guido van Rossum09df08a1998-05-22 00:51:39 +00001498 return PyNumber_Power(v, w, z);
Guido van Rossumd4905451991-05-05 20:00:36 +00001499}
1500
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001501PyDoc_STRVAR(pow_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001502"pow(x, y[, z]) -> number\n\
1503\n\
1504With two arguments, equivalent to x**y. With three arguments,\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001505equivalent to (x**y) % z, but may be more efficient (e.g. for longs).");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001506
1507
Eric Smith7c478942008-03-18 23:45:49 +00001508static PyObject *
1509builtin_print(PyObject *self, PyObject *args, PyObject *kwds)
1510{
1511 static char *kwlist[] = {"sep", "end", "file", 0};
1512 static PyObject *dummy_args;
1513 PyObject *sep = NULL, *end = NULL, *file = NULL;
1514 int i, err;
1515
1516 if (dummy_args == NULL) {
1517 if (!(dummy_args = PyTuple_New(0)))
1518 return NULL;
1519 }
1520 if (!PyArg_ParseTupleAndKeywords(dummy_args, kwds, "|OOO:print",
1521 kwlist, &sep, &end, &file))
1522 return NULL;
1523 if (file == NULL || file == Py_None) {
1524 file = PySys_GetObject("stdout");
1525 /* sys.stdout may be None when FILE* stdout isn't connected */
1526 if (file == Py_None)
1527 Py_RETURN_NONE;
1528 }
1529
1530 if (sep && sep != Py_None && !PyString_Check(sep) &&
1531 !PyUnicode_Check(sep)) {
1532 PyErr_Format(PyExc_TypeError,
1533 "sep must be None, str or unicode, not %.200s",
1534 sep->ob_type->tp_name);
1535 return NULL;
1536 }
1537 if (end && end != Py_None && !PyString_Check(end) &&
1538 !PyUnicode_Check(end)) {
1539 PyErr_Format(PyExc_TypeError,
1540 "end must be None, str or unicode, not %.200s",
1541 end->ob_type->tp_name);
1542 return NULL;
1543 }
1544
1545 for (i = 0; i < PyTuple_Size(args); i++) {
1546 if (i > 0) {
1547 if (sep == NULL || sep == Py_None)
1548 err = PyFile_WriteString(" ", file);
1549 else
1550 err = PyFile_WriteObject(sep, file,
1551 Py_PRINT_RAW);
1552 if (err)
1553 return NULL;
1554 }
1555 err = PyFile_WriteObject(PyTuple_GetItem(args, i), file,
1556 Py_PRINT_RAW);
1557 if (err)
1558 return NULL;
1559 }
1560
1561 if (end == NULL || end == Py_None)
1562 err = PyFile_WriteString("\n", file);
1563 else
1564 err = PyFile_WriteObject(end, file, Py_PRINT_RAW);
1565 if (err)
1566 return NULL;
1567
1568 Py_RETURN_NONE;
1569}
1570
1571PyDoc_STRVAR(print_doc,
1572"print(value, ..., sep=' ', end='\\n', file=sys.stdout)\n\
1573\n\
1574Prints the values to a stream, or to sys.stdout by default.\n\
1575Optional keyword arguments:\n\
1576file: a file-like object (stream); defaults to the current sys.stdout.\n\
1577sep: string inserted between values, default a space.\n\
1578end: string appended after the last value, default a newline.");
1579
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001580
1581/* Return number of items in range (lo, hi, step), when arguments are
1582 * PyInt or PyLong objects. step > 0 required. Return a value < 0 if
1583 * & only if the true value is too large to fit in a signed long.
1584 * Arguments MUST return 1 with either PyInt_Check() or
1585 * PyLong_Check(). Return -1 when there is an error.
1586 */
1587static long
1588get_len_of_range_longs(PyObject *lo, PyObject *hi, PyObject *step)
1589{
1590 /* -------------------------------------------------------------
1591 Algorithm is equal to that of get_len_of_range(), but it operates
1592 on PyObjects (which are assumed to be PyLong or PyInt objects).
1593 ---------------------------------------------------------------*/
1594 long n;
1595 PyObject *diff = NULL;
1596 PyObject *one = NULL;
1597 PyObject *tmp1 = NULL, *tmp2 = NULL, *tmp3 = NULL;
1598 /* holds sub-expression evaluations */
1599
1600 /* if (lo >= hi), return length of 0. */
1601 if (PyObject_Compare(lo, hi) >= 0)
1602 return 0;
1603
1604 if ((one = PyLong_FromLong(1L)) == NULL)
1605 goto Fail;
1606
1607 if ((tmp1 = PyNumber_Subtract(hi, lo)) == NULL)
1608 goto Fail;
1609
1610 if ((diff = PyNumber_Subtract(tmp1, one)) == NULL)
1611 goto Fail;
1612
1613 if ((tmp2 = PyNumber_FloorDivide(diff, step)) == NULL)
1614 goto Fail;
1615
1616 if ((tmp3 = PyNumber_Add(tmp2, one)) == NULL)
1617 goto Fail;
1618
1619 n = PyLong_AsLong(tmp3);
1620 if (PyErr_Occurred()) { /* Check for Overflow */
1621 PyErr_Clear();
1622 goto Fail;
1623 }
1624
1625 Py_DECREF(tmp3);
1626 Py_DECREF(tmp2);
1627 Py_DECREF(diff);
1628 Py_DECREF(tmp1);
1629 Py_DECREF(one);
1630 return n;
1631
1632 Fail:
1633 Py_XDECREF(tmp3);
1634 Py_XDECREF(tmp2);
1635 Py_XDECREF(diff);
1636 Py_XDECREF(tmp1);
1637 Py_XDECREF(one);
1638 return -1;
1639}
1640
1641/* An extension of builtin_range() that handles the case when PyLong
1642 * arguments are given. */
1643static PyObject *
1644handle_range_longs(PyObject *self, PyObject *args)
1645{
1646 PyObject *ilow;
Tim Peters874e1f72003-04-13 22:13:08 +00001647 PyObject *ihigh = NULL;
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001648 PyObject *istep = NULL;
Tim Peters874e1f72003-04-13 22:13:08 +00001649
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001650 PyObject *curnum = NULL;
1651 PyObject *v = NULL;
1652 long bign;
1653 int i, n;
1654 int cmp_result;
1655
Tim Peters874e1f72003-04-13 22:13:08 +00001656 PyObject *zero = PyLong_FromLong(0);
1657
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001658 if (zero == NULL)
1659 return NULL;
1660
Tim Peters874e1f72003-04-13 22:13:08 +00001661 if (!PyArg_UnpackTuple(args, "range", 1, 3, &ilow, &ihigh, &istep)) {
1662 Py_DECREF(zero);
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001663 return NULL;
1664 }
1665
Tim Peters874e1f72003-04-13 22:13:08 +00001666 /* Figure out which way we were called, supply defaults, and be
1667 * sure to incref everything so that the decrefs at the end
1668 * are correct.
1669 */
1670 assert(ilow != NULL);
1671 if (ihigh == NULL) {
1672 /* only 1 arg -- it's the upper limit */
1673 ihigh = ilow;
1674 ilow = NULL;
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001675 }
Tim Peters874e1f72003-04-13 22:13:08 +00001676 assert(ihigh != NULL);
1677 Py_INCREF(ihigh);
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001678
Tim Peters874e1f72003-04-13 22:13:08 +00001679 /* ihigh correct now; do ilow */
1680 if (ilow == NULL)
1681 ilow = zero;
1682 Py_INCREF(ilow);
1683
1684 /* ilow and ihigh correct now; do istep */
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001685 if (istep == NULL) {
1686 istep = PyLong_FromLong(1L);
1687 if (istep == NULL)
1688 goto Fail;
1689 }
1690 else {
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001691 Py_INCREF(istep);
1692 }
1693
Tim Peters874e1f72003-04-13 22:13:08 +00001694 if (!PyInt_Check(ilow) && !PyLong_Check(ilow)) {
Guido van Rossum28e83e32003-04-15 12:43:26 +00001695 PyErr_Format(PyExc_TypeError,
Alex Martellif471d472003-04-23 13:00:44 +00001696 "range() integer start argument expected, got %s.",
Guido van Rossum817d6c92003-04-14 18:25:04 +00001697 ilow->ob_type->tp_name);
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001698 goto Fail;
1699 }
1700
Tim Peters874e1f72003-04-13 22:13:08 +00001701 if (!PyInt_Check(ihigh) && !PyLong_Check(ihigh)) {
Guido van Rossum28e83e32003-04-15 12:43:26 +00001702 PyErr_Format(PyExc_TypeError,
Alex Martellif471d472003-04-23 13:00:44 +00001703 "range() integer end argument expected, got %s.",
Guido van Rossum817d6c92003-04-14 18:25:04 +00001704 ihigh->ob_type->tp_name);
Tim Peters874e1f72003-04-13 22:13:08 +00001705 goto Fail;
1706 }
1707
1708 if (!PyInt_Check(istep) && !PyLong_Check(istep)) {
Guido van Rossum28e83e32003-04-15 12:43:26 +00001709 PyErr_Format(PyExc_TypeError,
Alex Martellif471d472003-04-23 13:00:44 +00001710 "range() integer step argument expected, got %s.",
Guido van Rossum817d6c92003-04-14 18:25:04 +00001711 istep->ob_type->tp_name);
Tim Peters874e1f72003-04-13 22:13:08 +00001712 goto Fail;
1713 }
1714
1715 if (PyObject_Cmp(istep, zero, &cmp_result) == -1)
1716 goto Fail;
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001717 if (cmp_result == 0) {
1718 PyErr_SetString(PyExc_ValueError,
Alex Martellif471d472003-04-23 13:00:44 +00001719 "range() step argument must not be zero");
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001720 goto Fail;
1721 }
1722
1723 if (cmp_result > 0)
1724 bign = get_len_of_range_longs(ilow, ihigh, istep);
1725 else {
1726 PyObject *neg_istep = PyNumber_Negative(istep);
1727 if (neg_istep == NULL)
1728 goto Fail;
1729 bign = get_len_of_range_longs(ihigh, ilow, neg_istep);
1730 Py_DECREF(neg_istep);
1731 }
1732
1733 n = (int)bign;
1734 if (bign < 0 || (long)n != bign) {
1735 PyErr_SetString(PyExc_OverflowError,
1736 "range() result has too many items");
1737 goto Fail;
1738 }
1739
1740 v = PyList_New(n);
1741 if (v == NULL)
1742 goto Fail;
1743
1744 curnum = ilow;
1745 Py_INCREF(curnum);
1746
1747 for (i = 0; i < n; i++) {
1748 PyObject *w = PyNumber_Long(curnum);
1749 PyObject *tmp_num;
1750 if (w == NULL)
1751 goto Fail;
1752
1753 PyList_SET_ITEM(v, i, w);
1754
1755 tmp_num = PyNumber_Add(curnum, istep);
1756 if (tmp_num == NULL)
1757 goto Fail;
1758
1759 Py_DECREF(curnum);
1760 curnum = tmp_num;
1761 }
Tim Peters874e1f72003-04-13 22:13:08 +00001762 Py_DECREF(ilow);
1763 Py_DECREF(ihigh);
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001764 Py_DECREF(istep);
1765 Py_DECREF(zero);
Tim Peters874e1f72003-04-13 22:13:08 +00001766 Py_DECREF(curnum);
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001767 return v;
1768
1769 Fail:
Tim Peters874e1f72003-04-13 22:13:08 +00001770 Py_DECREF(ilow);
1771 Py_DECREF(ihigh);
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001772 Py_XDECREF(istep);
Tim Peters874e1f72003-04-13 22:13:08 +00001773 Py_DECREF(zero);
1774 Py_XDECREF(curnum);
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001775 Py_XDECREF(v);
1776 return NULL;
1777}
1778
Guido van Rossum124eff01999-02-23 16:11:01 +00001779/* Return number of items in range/xrange (lo, hi, step). step > 0
1780 * required. Return a value < 0 if & only if the true value is too
1781 * large to fit in a signed long.
1782 */
1783static long
Thomas Wouterse28c2962000-07-23 22:21:32 +00001784get_len_of_range(long lo, long hi, long step)
Guido van Rossum124eff01999-02-23 16:11:01 +00001785{
1786 /* -------------------------------------------------------------
1787 If lo >= hi, the range is empty.
1788 Else if n values are in the range, the last one is
1789 lo + (n-1)*step, which must be <= hi-1. Rearranging,
1790 n <= (hi - lo - 1)/step + 1, so taking the floor of the RHS gives
1791 the proper value. Since lo < hi in this case, hi-lo-1 >= 0, so
1792 the RHS is non-negative and so truncation is the same as the
1793 floor. Letting M be the largest positive long, the worst case
1794 for the RHS numerator is hi=M, lo=-M-1, and then
1795 hi-lo-1 = M-(-M-1)-1 = 2*M. Therefore unsigned long has enough
1796 precision to compute the RHS exactly.
1797 ---------------------------------------------------------------*/
1798 long n = 0;
1799 if (lo < hi) {
1800 unsigned long uhi = (unsigned long)hi;
1801 unsigned long ulo = (unsigned long)lo;
1802 unsigned long diff = uhi - ulo - 1;
1803 n = (long)(diff / (unsigned long)step + 1);
1804 }
1805 return n;
1806}
1807
Guido van Rossum79f25d91997-04-29 20:08:16 +00001808static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001809builtin_range(PyObject *self, PyObject *args)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001810{
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001811 long ilow = 0, ihigh = 0, istep = 1;
Guido van Rossum124eff01999-02-23 16:11:01 +00001812 long bign;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001813 int i, n;
Guido van Rossum124eff01999-02-23 16:11:01 +00001814
Guido van Rossum79f25d91997-04-29 20:08:16 +00001815 PyObject *v;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001816
Guido van Rossum79f25d91997-04-29 20:08:16 +00001817 if (PyTuple_Size(args) <= 1) {
1818 if (!PyArg_ParseTuple(args,
Guido van Rossum0865dd91995-01-17 16:30:22 +00001819 "l;range() requires 1-3 int arguments",
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001820 &ihigh)) {
1821 PyErr_Clear();
1822 return handle_range_longs(self, args);
1823 }
Guido van Rossum3f5da241990-12-20 15:06:42 +00001824 }
1825 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001826 if (!PyArg_ParseTuple(args,
Guido van Rossum0865dd91995-01-17 16:30:22 +00001827 "ll|l;range() requires 1-3 int arguments",
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001828 &ilow, &ihigh, &istep)) {
1829 PyErr_Clear();
1830 return handle_range_longs(self, args);
1831 }
Guido van Rossum3f5da241990-12-20 15:06:42 +00001832 }
1833 if (istep == 0) {
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001834 PyErr_SetString(PyExc_ValueError,
Alex Martellif471d472003-04-23 13:00:44 +00001835 "range() step argument must not be zero");
Guido van Rossum3f5da241990-12-20 15:06:42 +00001836 return NULL;
1837 }
Guido van Rossum124eff01999-02-23 16:11:01 +00001838 if (istep > 0)
1839 bign = get_len_of_range(ilow, ihigh, istep);
1840 else
1841 bign = get_len_of_range(ihigh, ilow, -istep);
1842 n = (int)bign;
1843 if (bign < 0 || (long)n != bign) {
Guido van Rossume23cde21999-01-12 05:07:47 +00001844 PyErr_SetString(PyExc_OverflowError,
Fred Drake661ea262000-10-24 19:57:45 +00001845 "range() result has too many items");
Guido van Rossume23cde21999-01-12 05:07:47 +00001846 return NULL;
1847 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00001848 v = PyList_New(n);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001849 if (v == NULL)
1850 return NULL;
1851 for (i = 0; i < n; i++) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001852 PyObject *w = PyInt_FromLong(ilow);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001853 if (w == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001854 Py_DECREF(v);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001855 return NULL;
1856 }
Guido van Rossuma937d141998-04-24 18:22:02 +00001857 PyList_SET_ITEM(v, i, w);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001858 ilow += istep;
1859 }
1860 return v;
1861}
1862
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001863PyDoc_STRVAR(range_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001864"range([start,] stop[, step]) -> list of integers\n\
1865\n\
1866Return a list containing an arithmetic progression of integers.\n\
1867range(i, j) returns [i, i+1, i+2, ..., j-1]; start (!) defaults to 0.\n\
1868When step is given, it specifies the increment (or decrement).\n\
1869For example, range(4) returns [0, 1, 2, 3]. The end point is omitted!\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001870These are exactly the valid indices for a list of 4 elements.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001871
1872
Guido van Rossum79f25d91997-04-29 20:08:16 +00001873static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001874builtin_raw_input(PyObject *self, PyObject *args)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001875{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001876 PyObject *v = NULL;
Martin v. Löwis31d2df52002-08-14 15:46:02 +00001877 PyObject *fin = PySys_GetObject("stdin");
1878 PyObject *fout = PySys_GetObject("stdout");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001879
Raymond Hettingerea3fdf42002-12-29 16:33:45 +00001880 if (!PyArg_UnpackTuple(args, "[raw_]input", 0, 1, &v))
Guido van Rossum3165fe61992-09-25 21:59:05 +00001881 return NULL;
Martin v. Löwis31d2df52002-08-14 15:46:02 +00001882
1883 if (fin == NULL) {
Alex Martellia9b9c9f2003-04-23 13:34:35 +00001884 PyErr_SetString(PyExc_RuntimeError, "[raw_]input: lost sys.stdin");
Martin v. Löwis31d2df52002-08-14 15:46:02 +00001885 return NULL;
1886 }
1887 if (fout == NULL) {
Alex Martellia9b9c9f2003-04-23 13:34:35 +00001888 PyErr_SetString(PyExc_RuntimeError, "[raw_]input: lost sys.stdout");
Martin v. Löwis31d2df52002-08-14 15:46:02 +00001889 return NULL;
1890 }
1891 if (PyFile_SoftSpace(fout, 0)) {
1892 if (PyFile_WriteString(" ", fout) != 0)
1893 return NULL;
1894 }
Georg Brandl7e3ba2a2006-08-06 08:23:54 +00001895 if (PyFile_AsFile(fin) && PyFile_AsFile(fout)
Martin v. Löwis566f6af2002-10-26 14:39:10 +00001896 && isatty(fileno(PyFile_AsFile(fin)))
1897 && isatty(fileno(PyFile_AsFile(fout)))) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001898 PyObject *po;
Guido van Rossum872537c1995-07-07 22:43:42 +00001899 char *prompt;
1900 char *s;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001901 PyObject *result;
Guido van Rossum872537c1995-07-07 22:43:42 +00001902 if (v != NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001903 po = PyObject_Str(v);
Guido van Rossum872537c1995-07-07 22:43:42 +00001904 if (po == NULL)
1905 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001906 prompt = PyString_AsString(po);
Guido van Rossumd9b52081998-06-26 18:25:38 +00001907 if (prompt == NULL)
1908 return NULL;
Guido van Rossum872537c1995-07-07 22:43:42 +00001909 }
1910 else {
1911 po = NULL;
1912 prompt = "";
1913 }
Michael W. Hudson52db5192004-08-02 13:21:09 +00001914 s = PyOS_Readline(PyFile_AsFile(fin), PyFile_AsFile(fout),
Martin v. Löwis566f6af2002-10-26 14:39:10 +00001915 prompt);
Guido van Rossum79f25d91997-04-29 20:08:16 +00001916 Py_XDECREF(po);
Guido van Rossum872537c1995-07-07 22:43:42 +00001917 if (s == NULL) {
Michael W. Hudson30ea2f22004-07-07 17:44:12 +00001918 if (!PyErr_Occurred())
1919 PyErr_SetNone(PyExc_KeyboardInterrupt);
Guido van Rossum872537c1995-07-07 22:43:42 +00001920 return NULL;
1921 }
1922 if (*s == '\0') {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001923 PyErr_SetNone(PyExc_EOFError);
Guido van Rossum872537c1995-07-07 22:43:42 +00001924 result = NULL;
1925 }
1926 else { /* strip trailing '\n' */
Guido van Rossum106f2da2000-06-28 21:12:25 +00001927 size_t len = strlen(s);
Martin v. Löwisb1ed7fa2006-04-13 07:52:27 +00001928 if (len > PY_SSIZE_T_MAX) {
Martin v. Löwis31d2df52002-08-14 15:46:02 +00001929 PyErr_SetString(PyExc_OverflowError,
Alex Martellia9b9c9f2003-04-23 13:34:35 +00001930 "[raw_]input: input too long");
Guido van Rossum106f2da2000-06-28 21:12:25 +00001931 result = NULL;
1932 }
1933 else {
Martin v. Löwisb1ed7fa2006-04-13 07:52:27 +00001934 result = PyString_FromStringAndSize(s, len-1);
Guido van Rossum106f2da2000-06-28 21:12:25 +00001935 }
Guido van Rossum872537c1995-07-07 22:43:42 +00001936 }
Guido van Rossumb18618d2000-05-03 23:44:39 +00001937 PyMem_FREE(s);
Guido van Rossum872537c1995-07-07 22:43:42 +00001938 return result;
1939 }
Guido van Rossum90933611991-06-07 16:10:43 +00001940 if (v != NULL) {
Martin v. Löwis31d2df52002-08-14 15:46:02 +00001941 if (PyFile_WriteObject(v, fout, Py_PRINT_RAW) != 0)
Guido van Rossum90933611991-06-07 16:10:43 +00001942 return NULL;
1943 }
Martin v. Löwis31d2df52002-08-14 15:46:02 +00001944 return PyFile_GetLine(fin, -1);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001945}
1946
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001947PyDoc_STRVAR(raw_input_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001948"raw_input([prompt]) -> string\n\
1949\n\
1950Read a string from standard input. The trailing newline is stripped.\n\
1951If the user hits EOF (Unix: Ctl-D, Windows: Ctl-Z+Return), raise EOFError.\n\
1952On Unix, GNU readline is used if enabled. The prompt string, if given,\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001953is printed without a trailing newline before reading.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001954
1955
Guido van Rossum79f25d91997-04-29 20:08:16 +00001956static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001957builtin_reduce(PyObject *self, PyObject *args)
Guido van Rossum12d12c51993-10-26 17:58:25 +00001958{
Tim Peters15d81ef2001-05-04 04:39:21 +00001959 PyObject *seq, *func, *result = NULL, *it;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001960
Benjamin Peterson9f4f4812008-04-27 03:01:45 +00001961 if (PyErr_WarnPy3k("reduce() not supported in 3.x; "
1962 "use functools.reduce()", 1) < 0)
Neal Norwitzdf25efe2007-05-23 06:58:36 +00001963 return NULL;
1964
Raymond Hettingerea3fdf42002-12-29 16:33:45 +00001965 if (!PyArg_UnpackTuple(args, "reduce", 2, 3, &func, &seq, &result))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001966 return NULL;
1967 if (result != NULL)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001968 Py_INCREF(result);
Guido van Rossum12d12c51993-10-26 17:58:25 +00001969
Tim Peters15d81ef2001-05-04 04:39:21 +00001970 it = PyObject_GetIter(seq);
1971 if (it == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001972 PyErr_SetString(PyExc_TypeError,
Tim Peters15d81ef2001-05-04 04:39:21 +00001973 "reduce() arg 2 must support iteration");
1974 Py_XDECREF(result);
Guido van Rossum12d12c51993-10-26 17:58:25 +00001975 return NULL;
1976 }
1977
Guido van Rossum79f25d91997-04-29 20:08:16 +00001978 if ((args = PyTuple_New(2)) == NULL)
Guido van Rossum2d951851994-08-29 12:52:16 +00001979 goto Fail;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001980
Tim Peters15d81ef2001-05-04 04:39:21 +00001981 for (;;) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001982 PyObject *op2;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001983
1984 if (args->ob_refcnt > 1) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001985 Py_DECREF(args);
1986 if ((args = PyTuple_New(2)) == NULL)
Guido van Rossum2d951851994-08-29 12:52:16 +00001987 goto Fail;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001988 }
1989
Tim Peters15d81ef2001-05-04 04:39:21 +00001990 op2 = PyIter_Next(it);
1991 if (op2 == NULL) {
Tim Petersf4848da2001-05-05 00:14:56 +00001992 if (PyErr_Occurred())
1993 goto Fail;
1994 break;
Guido van Rossum2d951851994-08-29 12:52:16 +00001995 }
Guido van Rossum12d12c51993-10-26 17:58:25 +00001996
Guido van Rossum2d951851994-08-29 12:52:16 +00001997 if (result == NULL)
1998 result = op2;
1999 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002000 PyTuple_SetItem(args, 0, result);
2001 PyTuple_SetItem(args, 1, op2);
2002 if ((result = PyEval_CallObject(func, args)) == NULL)
Guido van Rossum2d951851994-08-29 12:52:16 +00002003 goto Fail;
2004 }
Guido van Rossum12d12c51993-10-26 17:58:25 +00002005 }
2006
Guido van Rossum79f25d91997-04-29 20:08:16 +00002007 Py_DECREF(args);
Guido van Rossum12d12c51993-10-26 17:58:25 +00002008
Guido van Rossum2d951851994-08-29 12:52:16 +00002009 if (result == NULL)
Guido van Rossum79f25d91997-04-29 20:08:16 +00002010 PyErr_SetString(PyExc_TypeError,
Fred Drake661ea262000-10-24 19:57:45 +00002011 "reduce() of empty sequence with no initial value");
Guido van Rossum2d951851994-08-29 12:52:16 +00002012
Tim Peters15d81ef2001-05-04 04:39:21 +00002013 Py_DECREF(it);
Guido van Rossum12d12c51993-10-26 17:58:25 +00002014 return result;
2015
Guido van Rossum2d951851994-08-29 12:52:16 +00002016Fail:
Guido van Rossum79f25d91997-04-29 20:08:16 +00002017 Py_XDECREF(args);
2018 Py_XDECREF(result);
Tim Peters15d81ef2001-05-04 04:39:21 +00002019 Py_DECREF(it);
Guido van Rossum12d12c51993-10-26 17:58:25 +00002020 return NULL;
2021}
2022
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002023PyDoc_STRVAR(reduce_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002024"reduce(function, sequence[, initial]) -> value\n\
2025\n\
2026Apply a function of two arguments cumulatively to the items of a sequence,\n\
2027from left to right, so as to reduce the sequence to a single value.\n\
2028For example, reduce(lambda x, y: x+y, [1, 2, 3, 4, 5]) calculates\n\
2029((((1+2)+3)+4)+5). If initial is present, it is placed before the items\n\
2030of the sequence in the calculation, and serves as a default when the\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002031sequence is empty.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002032
2033
Guido van Rossum79f25d91997-04-29 20:08:16 +00002034static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002035builtin_reload(PyObject *self, PyObject *v)
Guido van Rossum3f5da241990-12-20 15:06:42 +00002036{
Benjamin Peterson9f4f4812008-04-27 03:01:45 +00002037 if (PyErr_WarnPy3k("In 3.x, reload() is renamed to imp.reload()",
2038 1) < 0)
Neal Norwitzdf25efe2007-05-23 06:58:36 +00002039 return NULL;
2040
Guido van Rossum79f25d91997-04-29 20:08:16 +00002041 return PyImport_ReloadModule(v);
Guido van Rossum3f5da241990-12-20 15:06:42 +00002042}
2043
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002044PyDoc_STRVAR(reload_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002045"reload(module) -> module\n\
2046\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002047Reload the module. The module must have been successfully imported before.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002048
2049
Guido van Rossum79f25d91997-04-29 20:08:16 +00002050static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002051builtin_repr(PyObject *self, PyObject *v)
Guido van Rossumc89705d1992-11-26 08:54:07 +00002052{
Guido van Rossum79f25d91997-04-29 20:08:16 +00002053 return PyObject_Repr(v);
Guido van Rossumc89705d1992-11-26 08:54:07 +00002054}
2055
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002056PyDoc_STRVAR(repr_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002057"repr(object) -> string\n\
2058\n\
2059Return the canonical string representation of the object.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002060For most object types, eval(repr(object)) == object.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002061
2062
Guido van Rossum79f25d91997-04-29 20:08:16 +00002063static PyObject *
Georg Brandlccadf842006-03-31 18:54:53 +00002064builtin_round(PyObject *self, PyObject *args, PyObject *kwds)
Guido van Rossum9e51f9b1993-02-12 16:29:05 +00002065{
Jeffrey Yasskin9871d8f2008-01-05 08:47:13 +00002066 double number;
2067 double f;
2068 int ndigits = 0;
2069 int i;
Georg Brandlccadf842006-03-31 18:54:53 +00002070 static char *kwlist[] = {"number", "ndigits", 0};
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002071
Jeffrey Yasskin9871d8f2008-01-05 08:47:13 +00002072 if (!PyArg_ParseTupleAndKeywords(args, kwds, "d|i:round",
2073 kwlist, &number, &ndigits))
2074 return NULL;
2075 f = 1.0;
2076 i = abs(ndigits);
2077 while (--i >= 0)
2078 f = f*10.0;
2079 if (ndigits < 0)
2080 number /= f;
2081 else
2082 number *= f;
2083 if (number >= 0.0)
2084 number = floor(number + 0.5);
2085 else
2086 number = ceil(number - 0.5);
2087 if (ndigits < 0)
2088 number *= f;
2089 else
2090 number /= f;
2091 return PyFloat_FromDouble(number);
Guido van Rossum9e51f9b1993-02-12 16:29:05 +00002092}
2093
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002094PyDoc_STRVAR(round_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002095"round(number[, ndigits]) -> floating point number\n\
2096\n\
2097Round a number to a given precision in decimal digits (default 0 digits).\n\
Jeffrey Yasskin9871d8f2008-01-05 08:47:13 +00002098This always returns a floating point number. Precision may be negative.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002099
Raymond Hettinger64958a12003-12-17 20:43:33 +00002100static PyObject *
2101builtin_sorted(PyObject *self, PyObject *args, PyObject *kwds)
2102{
2103 PyObject *newlist, *v, *seq, *compare=NULL, *keyfunc=NULL, *newargs;
2104 PyObject *callable;
Martin v. Löwis15e62742006-02-27 16:46:16 +00002105 static char *kwlist[] = {"iterable", "cmp", "key", "reverse", 0};
Neal Norwitz6f0d4792005-12-15 06:40:36 +00002106 int reverse;
Raymond Hettinger64958a12003-12-17 20:43:33 +00002107
Neal Norwitz6f0d4792005-12-15 06:40:36 +00002108 /* args 1-4 should match listsort in Objects/listobject.c */
Armin Rigo71d7e702005-09-20 18:13:03 +00002109 if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|OOi:sorted",
2110 kwlist, &seq, &compare, &keyfunc, &reverse))
2111 return NULL;
Raymond Hettinger64958a12003-12-17 20:43:33 +00002112
2113 newlist = PySequence_List(seq);
2114 if (newlist == NULL)
2115 return NULL;
2116
2117 callable = PyObject_GetAttrString(newlist, "sort");
2118 if (callable == NULL) {
2119 Py_DECREF(newlist);
2120 return NULL;
2121 }
Georg Brandl99d7e4e2005-08-31 22:21:15 +00002122
Raymond Hettinger64958a12003-12-17 20:43:33 +00002123 newargs = PyTuple_GetSlice(args, 1, 4);
2124 if (newargs == NULL) {
2125 Py_DECREF(newlist);
2126 Py_DECREF(callable);
2127 return NULL;
2128 }
2129
2130 v = PyObject_Call(callable, newargs, kwds);
2131 Py_DECREF(newargs);
2132 Py_DECREF(callable);
2133 if (v == NULL) {
2134 Py_DECREF(newlist);
2135 return NULL;
2136 }
2137 Py_DECREF(v);
2138 return newlist;
2139}
2140
2141PyDoc_STRVAR(sorted_doc,
2142"sorted(iterable, cmp=None, key=None, reverse=False) --> new sorted list");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002143
Guido van Rossum79f25d91997-04-29 20:08:16 +00002144static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002145builtin_vars(PyObject *self, PyObject *args)
Guido van Rossum2d951851994-08-29 12:52:16 +00002146{
Guido van Rossum79f25d91997-04-29 20:08:16 +00002147 PyObject *v = NULL;
2148 PyObject *d;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002149
Raymond Hettingerea3fdf42002-12-29 16:33:45 +00002150 if (!PyArg_UnpackTuple(args, "vars", 0, 1, &v))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002151 return NULL;
Guido van Rossum2d951851994-08-29 12:52:16 +00002152 if (v == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002153 d = PyEval_GetLocals();
Guido van Rossum53bb7ff1995-07-26 16:26:31 +00002154 if (d == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002155 if (!PyErr_Occurred())
2156 PyErr_SetString(PyExc_SystemError,
Alex Martellia9b9c9f2003-04-23 13:34:35 +00002157 "vars(): no locals!?");
Guido van Rossum53bb7ff1995-07-26 16:26:31 +00002158 }
2159 else
Guido van Rossum79f25d91997-04-29 20:08:16 +00002160 Py_INCREF(d);
Guido van Rossum2d951851994-08-29 12:52:16 +00002161 }
2162 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002163 d = PyObject_GetAttrString(v, "__dict__");
Guido van Rossum2d951851994-08-29 12:52:16 +00002164 if (d == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002165 PyErr_SetString(PyExc_TypeError,
Guido van Rossum2d951851994-08-29 12:52:16 +00002166 "vars() argument must have __dict__ attribute");
2167 return NULL;
2168 }
2169 }
2170 return d;
2171}
2172
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002173PyDoc_STRVAR(vars_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002174"vars([object]) -> dictionary\n\
2175\n\
2176Without arguments, equivalent to locals().\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002177With an argument, equivalent to object.__dict__.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002178
Alex Martellia70b1912003-04-22 08:12:33 +00002179
2180static PyObject*
2181builtin_sum(PyObject *self, PyObject *args)
2182{
2183 PyObject *seq;
2184 PyObject *result = NULL;
2185 PyObject *temp, *item, *iter;
2186
Raymond Hettinger5cf63942003-10-25 06:41:37 +00002187 if (!PyArg_UnpackTuple(args, "sum", 1, 2, &seq, &result))
Alex Martellia70b1912003-04-22 08:12:33 +00002188 return NULL;
2189
2190 iter = PyObject_GetIter(seq);
2191 if (iter == NULL)
2192 return NULL;
2193
2194 if (result == NULL) {
2195 result = PyInt_FromLong(0);
2196 if (result == NULL) {
2197 Py_DECREF(iter);
2198 return NULL;
2199 }
2200 } else {
2201 /* reject string values for 'start' parameter */
2202 if (PyObject_TypeCheck(result, &PyBaseString_Type)) {
2203 PyErr_SetString(PyExc_TypeError,
Alex Martellia9b9c9f2003-04-23 13:34:35 +00002204 "sum() can't sum strings [use ''.join(seq) instead]");
Alex Martellia70b1912003-04-22 08:12:33 +00002205 Py_DECREF(iter);
2206 return NULL;
2207 }
Alex Martelli41c9f882003-04-22 09:24:48 +00002208 Py_INCREF(result);
Alex Martellia70b1912003-04-22 08:12:33 +00002209 }
2210
Raymond Hettinger3f8caa32007-10-24 01:28:33 +00002211#ifndef SLOW_SUM
2212 /* Fast addition by keeping temporary sums in C instead of new Python objects.
2213 Assumes all inputs are the same type. If the assumption fails, default
2214 to the more general routine.
2215 */
2216 if (PyInt_CheckExact(result)) {
2217 long i_result = PyInt_AS_LONG(result);
2218 Py_DECREF(result);
2219 result = NULL;
2220 while(result == NULL) {
2221 item = PyIter_Next(iter);
2222 if (item == NULL) {
2223 Py_DECREF(iter);
2224 if (PyErr_Occurred())
2225 return NULL;
2226 return PyInt_FromLong(i_result);
2227 }
2228 if (PyInt_CheckExact(item)) {
2229 long b = PyInt_AS_LONG(item);
2230 long x = i_result + b;
2231 if ((x^i_result) >= 0 || (x^b) >= 0) {
2232 i_result = x;
2233 Py_DECREF(item);
2234 continue;
2235 }
2236 }
2237 /* Either overflowed or is not an int. Restore real objects and process normally */
2238 result = PyInt_FromLong(i_result);
2239 temp = PyNumber_Add(result, item);
2240 Py_DECREF(result);
2241 Py_DECREF(item);
2242 result = temp;
2243 if (result == NULL) {
2244 Py_DECREF(iter);
2245 return NULL;
2246 }
2247 }
2248 }
2249
2250 if (PyFloat_CheckExact(result)) {
2251 double f_result = PyFloat_AS_DOUBLE(result);
2252 Py_DECREF(result);
2253 result = NULL;
2254 while(result == NULL) {
2255 item = PyIter_Next(iter);
2256 if (item == NULL) {
2257 Py_DECREF(iter);
2258 if (PyErr_Occurred())
2259 return NULL;
2260 return PyFloat_FromDouble(f_result);
2261 }
2262 if (PyFloat_CheckExact(item)) {
2263 PyFPE_START_PROTECT("add", return 0)
2264 f_result += PyFloat_AS_DOUBLE(item);
Raymond Hettinger3a8daf52007-10-24 02:05:51 +00002265 PyFPE_END_PROTECT(f_result)
Raymond Hettingera45c4872007-10-25 02:26:58 +00002266 Py_DECREF(item);
Raymond Hettinger3a8daf52007-10-24 02:05:51 +00002267 continue;
2268 }
2269 if (PyInt_CheckExact(item)) {
2270 PyFPE_START_PROTECT("add", return 0)
2271 f_result += (double)PyInt_AS_LONG(item);
2272 PyFPE_END_PROTECT(f_result)
Raymond Hettingera45c4872007-10-25 02:26:58 +00002273 Py_DECREF(item);
Raymond Hettinger3f8caa32007-10-24 01:28:33 +00002274 continue;
2275 }
2276 result = PyFloat_FromDouble(f_result);
2277 temp = PyNumber_Add(result, item);
2278 Py_DECREF(result);
2279 Py_DECREF(item);
2280 result = temp;
2281 if (result == NULL) {
2282 Py_DECREF(iter);
2283 return NULL;
2284 }
2285 }
2286 }
2287#endif
2288
Alex Martellia70b1912003-04-22 08:12:33 +00002289 for(;;) {
2290 item = PyIter_Next(iter);
2291 if (item == NULL) {
2292 /* error, or end-of-sequence */
2293 if (PyErr_Occurred()) {
2294 Py_DECREF(result);
2295 result = NULL;
2296 }
2297 break;
2298 }
Alex Martellia253e182003-10-25 23:24:14 +00002299 temp = PyNumber_Add(result, item);
Alex Martellia70b1912003-04-22 08:12:33 +00002300 Py_DECREF(result);
2301 Py_DECREF(item);
2302 result = temp;
2303 if (result == NULL)
2304 break;
2305 }
2306 Py_DECREF(iter);
2307 return result;
2308}
2309
2310PyDoc_STRVAR(sum_doc,
Georg Brandl8134d062006-10-12 12:33:07 +00002311"sum(sequence[, start]) -> value\n\
Alex Martellia70b1912003-04-22 08:12:33 +00002312\n\
2313Returns the sum of a sequence of numbers (NOT strings) plus the value\n\
Georg Brandl8134d062006-10-12 12:33:07 +00002314of parameter 'start' (which defaults to 0). When the sequence is\n\
2315empty, returns start.");
Alex Martellia70b1912003-04-22 08:12:33 +00002316
2317
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002318static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002319builtin_isinstance(PyObject *self, PyObject *args)
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002320{
2321 PyObject *inst;
2322 PyObject *cls;
Guido van Rossum823649d2001-03-21 18:40:58 +00002323 int retval;
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002324
Raymond Hettingerea3fdf42002-12-29 16:33:45 +00002325 if (!PyArg_UnpackTuple(args, "isinstance", 2, 2, &inst, &cls))
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002326 return NULL;
Guido van Rossumf5dd9141997-12-02 19:11:45 +00002327
Guido van Rossum823649d2001-03-21 18:40:58 +00002328 retval = PyObject_IsInstance(inst, cls);
2329 if (retval < 0)
Guido van Rossumad991772001-01-12 16:03:05 +00002330 return NULL;
Guido van Rossum77f6a652002-04-03 22:41:51 +00002331 return PyBool_FromLong(retval);
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002332}
2333
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002334PyDoc_STRVAR(isinstance_doc,
Guido van Rossum77f6a652002-04-03 22:41:51 +00002335"isinstance(object, class-or-type-or-tuple) -> bool\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002336\n\
2337Return whether an object is an instance of a class or of a subclass thereof.\n\
Guido van Rossum03290ec2001-10-07 20:54:12 +00002338With a type as second argument, return whether that is the object's type.\n\
2339The form using a tuple, isinstance(x, (A, B, ...)), is a shortcut for\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002340isinstance(x, A) or isinstance(x, B) or ... (etc.).");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002341
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002342
2343static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002344builtin_issubclass(PyObject *self, PyObject *args)
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002345{
2346 PyObject *derived;
2347 PyObject *cls;
2348 int retval;
2349
Raymond Hettingerea3fdf42002-12-29 16:33:45 +00002350 if (!PyArg_UnpackTuple(args, "issubclass", 2, 2, &derived, &cls))
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002351 return NULL;
Guido van Rossum668213d1999-06-16 17:28:37 +00002352
Guido van Rossum823649d2001-03-21 18:40:58 +00002353 retval = PyObject_IsSubclass(derived, cls);
2354 if (retval < 0)
2355 return NULL;
Guido van Rossum77f6a652002-04-03 22:41:51 +00002356 return PyBool_FromLong(retval);
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002357}
2358
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002359PyDoc_STRVAR(issubclass_doc,
Guido van Rossum77f6a652002-04-03 22:41:51 +00002360"issubclass(C, B) -> bool\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002361\n\
Walter Dörwaldd9a6ad32002-12-12 16:41:44 +00002362Return whether class C is a subclass (i.e., a derived class) of class B.\n\
2363When using a tuple as the second argument issubclass(X, (A, B, ...)),\n\
2364is a shortcut for issubclass(X, A) or issubclass(X, B) or ... (etc.).");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002365
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002366
Barry Warsawbd599b52000-08-03 15:45:29 +00002367static PyObject*
2368builtin_zip(PyObject *self, PyObject *args)
2369{
2370 PyObject *ret;
Martin v. Löwisd96ee902006-02-16 14:37:16 +00002371 const Py_ssize_t itemsize = PySequence_Length(args);
2372 Py_ssize_t i;
Tim Peters8572b4f2001-05-06 01:05:02 +00002373 PyObject *itlist; /* tuple of iterators */
Martin v. Löwisd96ee902006-02-16 14:37:16 +00002374 Py_ssize_t len; /* guess at result length */
Barry Warsawbd599b52000-08-03 15:45:29 +00002375
Raymond Hettingereaef6152003-08-02 07:42:57 +00002376 if (itemsize == 0)
2377 return PyList_New(0);
2378
Barry Warsawbd599b52000-08-03 15:45:29 +00002379 /* args must be a tuple */
2380 assert(PyTuple_Check(args));
2381
Tim Peters39a86c22002-05-12 07:19:38 +00002382 /* Guess at result length: the shortest of the input lengths.
2383 If some argument refuses to say, we refuse to guess too, lest
2384 an argument like xrange(sys.maxint) lead us astray.*/
Tim Peters67d687a2002-04-29 21:27:32 +00002385 len = -1; /* unknown */
2386 for (i = 0; i < itemsize; ++i) {
2387 PyObject *item = PyTuple_GET_ITEM(args, i);
Raymond Hettinger4e2f7142007-12-06 00:56:53 +00002388 Py_ssize_t thislen = _PyObject_LengthHint(item, -1);
Tim Peters39a86c22002-05-12 07:19:38 +00002389 if (thislen < 0) {
Tim Peters39a86c22002-05-12 07:19:38 +00002390 len = -1;
2391 break;
2392 }
Tim Peters67d687a2002-04-29 21:27:32 +00002393 else if (len < 0 || thislen < len)
2394 len = thislen;
2395 }
2396
Tim Peters8572b4f2001-05-06 01:05:02 +00002397 /* allocate result list */
Tim Peters67d687a2002-04-29 21:27:32 +00002398 if (len < 0)
2399 len = 10; /* arbitrary */
2400 if ((ret = PyList_New(len)) == NULL)
Barry Warsawbd599b52000-08-03 15:45:29 +00002401 return NULL;
2402
Tim Peters8572b4f2001-05-06 01:05:02 +00002403 /* obtain iterators */
2404 itlist = PyTuple_New(itemsize);
2405 if (itlist == NULL)
2406 goto Fail_ret;
2407 for (i = 0; i < itemsize; ++i) {
2408 PyObject *item = PyTuple_GET_ITEM(args, i);
2409 PyObject *it = PyObject_GetIter(item);
2410 if (it == NULL) {
2411 if (PyErr_ExceptionMatches(PyExc_TypeError))
2412 PyErr_Format(PyExc_TypeError,
Neal Norwitza361bd82006-02-19 19:31:50 +00002413 "zip argument #%zd must support iteration",
Tim Peters8572b4f2001-05-06 01:05:02 +00002414 i+1);
2415 goto Fail_ret_itlist;
Barry Warsawbd599b52000-08-03 15:45:29 +00002416 }
Tim Peters8572b4f2001-05-06 01:05:02 +00002417 PyTuple_SET_ITEM(itlist, i, it);
2418 }
Barry Warsawbd599b52000-08-03 15:45:29 +00002419
Tim Peters8572b4f2001-05-06 01:05:02 +00002420 /* build result into ret list */
Tim Peters67d687a2002-04-29 21:27:32 +00002421 for (i = 0; ; ++i) {
2422 int j;
Tim Peters8572b4f2001-05-06 01:05:02 +00002423 PyObject *next = PyTuple_New(itemsize);
2424 if (!next)
2425 goto Fail_ret_itlist;
2426
Tim Peters67d687a2002-04-29 21:27:32 +00002427 for (j = 0; j < itemsize; j++) {
2428 PyObject *it = PyTuple_GET_ITEM(itlist, j);
Tim Peters8572b4f2001-05-06 01:05:02 +00002429 PyObject *item = PyIter_Next(it);
Barry Warsawbd599b52000-08-03 15:45:29 +00002430 if (!item) {
Tim Peters8572b4f2001-05-06 01:05:02 +00002431 if (PyErr_Occurred()) {
2432 Py_DECREF(ret);
2433 ret = NULL;
Barry Warsawbd599b52000-08-03 15:45:29 +00002434 }
2435 Py_DECREF(next);
Tim Peters8572b4f2001-05-06 01:05:02 +00002436 Py_DECREF(itlist);
Tim Peters67d687a2002-04-29 21:27:32 +00002437 goto Done;
Barry Warsawbd599b52000-08-03 15:45:29 +00002438 }
Tim Peters67d687a2002-04-29 21:27:32 +00002439 PyTuple_SET_ITEM(next, j, item);
Barry Warsawbd599b52000-08-03 15:45:29 +00002440 }
Tim Peters8572b4f2001-05-06 01:05:02 +00002441
Tim Peters67d687a2002-04-29 21:27:32 +00002442 if (i < len)
2443 PyList_SET_ITEM(ret, i, next);
2444 else {
2445 int status = PyList_Append(ret, next);
2446 Py_DECREF(next);
2447 ++len;
2448 if (status < 0)
2449 goto Fail_ret_itlist;
2450 }
Barry Warsawbd599b52000-08-03 15:45:29 +00002451 }
Tim Peters8572b4f2001-05-06 01:05:02 +00002452
Tim Peters67d687a2002-04-29 21:27:32 +00002453Done:
2454 if (ret != NULL && i < len) {
2455 /* The list is too big. */
2456 if (PyList_SetSlice(ret, i, len, NULL) < 0)
2457 return NULL;
2458 }
2459 return ret;
2460
Tim Peters8572b4f2001-05-06 01:05:02 +00002461Fail_ret_itlist:
2462 Py_DECREF(itlist);
2463Fail_ret:
2464 Py_DECREF(ret);
2465 return NULL;
Barry Warsawbd599b52000-08-03 15:45:29 +00002466}
2467
2468
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002469PyDoc_STRVAR(zip_doc,
Barry Warsawbd599b52000-08-03 15:45:29 +00002470"zip(seq1 [, seq2 [...]]) -> [(seq1[0], seq2[0] ...), (...)]\n\
2471\n\
2472Return a list of tuples, where each tuple contains the i-th element\n\
2473from each of the argument sequences. The returned list is truncated\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002474in length to the length of the shortest argument sequence.");
Barry Warsawbd599b52000-08-03 15:45:29 +00002475
2476
Guido van Rossum79f25d91997-04-29 20:08:16 +00002477static PyMethodDef builtin_methods[] = {
Neal Norwitz92e212f2006-04-03 04:48:37 +00002478 {"__import__", (PyCFunction)builtin___import__, METH_VARARGS | METH_KEYWORDS, import_doc},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002479 {"abs", builtin_abs, METH_O, abs_doc},
Raymond Hettinger96229b12005-03-11 06:49:40 +00002480 {"all", builtin_all, METH_O, all_doc},
2481 {"any", builtin_any, METH_O, any_doc},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002482 {"apply", builtin_apply, METH_VARARGS, apply_doc},
Eric Smith3cd81942008-02-22 16:30:22 +00002483 {"bin", builtin_bin, METH_O, bin_doc},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002484 {"callable", builtin_callable, METH_O, callable_doc},
2485 {"chr", builtin_chr, METH_VARARGS, chr_doc},
2486 {"cmp", builtin_cmp, METH_VARARGS, cmp_doc},
2487 {"coerce", builtin_coerce, METH_VARARGS, coerce_doc},
Georg Brandl5240d742007-03-13 20:46:32 +00002488 {"compile", (PyCFunction)builtin_compile, METH_VARARGS | METH_KEYWORDS, compile_doc},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002489 {"delattr", builtin_delattr, METH_VARARGS, delattr_doc},
2490 {"dir", builtin_dir, METH_VARARGS, dir_doc},
2491 {"divmod", builtin_divmod, METH_VARARGS, divmod_doc},
2492 {"eval", builtin_eval, METH_VARARGS, eval_doc},
2493 {"execfile", builtin_execfile, METH_VARARGS, execfile_doc},
2494 {"filter", builtin_filter, METH_VARARGS, filter_doc},
Eric Smitha9f7d622008-02-17 19:46:49 +00002495 {"format", builtin_format, METH_VARARGS, format_doc},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002496 {"getattr", builtin_getattr, METH_VARARGS, getattr_doc},
2497 {"globals", (PyCFunction)builtin_globals, METH_NOARGS, globals_doc},
2498 {"hasattr", builtin_hasattr, METH_VARARGS, hasattr_doc},
2499 {"hash", builtin_hash, METH_O, hash_doc},
2500 {"hex", builtin_hex, METH_O, hex_doc},
2501 {"id", builtin_id, METH_O, id_doc},
2502 {"input", builtin_input, METH_VARARGS, input_doc},
2503 {"intern", builtin_intern, METH_VARARGS, intern_doc},
2504 {"isinstance", builtin_isinstance, METH_VARARGS, isinstance_doc},
2505 {"issubclass", builtin_issubclass, METH_VARARGS, issubclass_doc},
2506 {"iter", builtin_iter, METH_VARARGS, iter_doc},
2507 {"len", builtin_len, METH_O, len_doc},
2508 {"locals", (PyCFunction)builtin_locals, METH_NOARGS, locals_doc},
2509 {"map", builtin_map, METH_VARARGS, map_doc},
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00002510 {"max", (PyCFunction)builtin_max, METH_VARARGS | METH_KEYWORDS, max_doc},
2511 {"min", (PyCFunction)builtin_min, METH_VARARGS | METH_KEYWORDS, min_doc},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002512 {"oct", builtin_oct, METH_O, oct_doc},
Neal Norwitzc4edb0e2006-05-02 04:43:14 +00002513 {"open", (PyCFunction)builtin_open, METH_VARARGS | METH_KEYWORDS, open_doc},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002514 {"ord", builtin_ord, METH_O, ord_doc},
2515 {"pow", builtin_pow, METH_VARARGS, pow_doc},
Eric Smith7c478942008-03-18 23:45:49 +00002516 {"print", (PyCFunction)builtin_print, METH_VARARGS | METH_KEYWORDS, print_doc},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002517 {"range", builtin_range, METH_VARARGS, range_doc},
2518 {"raw_input", builtin_raw_input, METH_VARARGS, raw_input_doc},
2519 {"reduce", builtin_reduce, METH_VARARGS, reduce_doc},
2520 {"reload", builtin_reload, METH_O, reload_doc},
2521 {"repr", builtin_repr, METH_O, repr_doc},
Georg Brandlccadf842006-03-31 18:54:53 +00002522 {"round", (PyCFunction)builtin_round, METH_VARARGS | METH_KEYWORDS, round_doc},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002523 {"setattr", builtin_setattr, METH_VARARGS, setattr_doc},
Raymond Hettinger64958a12003-12-17 20:43:33 +00002524 {"sorted", (PyCFunction)builtin_sorted, METH_VARARGS | METH_KEYWORDS, sorted_doc},
Alex Martellia70b1912003-04-22 08:12:33 +00002525 {"sum", builtin_sum, METH_VARARGS, sum_doc},
Martin v. Löwis339d0f72001-08-17 18:39:25 +00002526#ifdef Py_USING_UNICODE
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002527 {"unichr", builtin_unichr, METH_VARARGS, unichr_doc},
Martin v. Löwis339d0f72001-08-17 18:39:25 +00002528#endif
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002529 {"vars", builtin_vars, METH_VARARGS, vars_doc},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002530 {"zip", builtin_zip, METH_VARARGS, zip_doc},
Guido van Rossumc02e15c1991-12-16 13:03:00 +00002531 {NULL, NULL},
Guido van Rossum3f5da241990-12-20 15:06:42 +00002532};
2533
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002534PyDoc_STRVAR(builtin_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002535"Built-in functions, exceptions, and other objects.\n\
2536\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002537Noteworthy: None is the `nil' object; Ellipsis represents `...' in slices.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002538
Guido van Rossum25ce5661997-08-02 03:10:38 +00002539PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002540_PyBuiltin_Init(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +00002541{
Fred Drake5550de32000-06-20 04:54:19 +00002542 PyObject *mod, *dict, *debug;
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002543 mod = Py_InitModule4("__builtin__", builtin_methods,
2544 builtin_doc, (PyObject *)NULL,
2545 PYTHON_API_VERSION);
Guido van Rossum25ce5661997-08-02 03:10:38 +00002546 if (mod == NULL)
2547 return NULL;
2548 dict = PyModule_GetDict(mod);
Tim Peters4b7625e2001-09-13 21:37:17 +00002549
Tim Peters7571a0f2003-03-23 17:52:28 +00002550#ifdef Py_TRACE_REFS
2551 /* __builtin__ exposes a number of statically allocated objects
2552 * that, before this code was added in 2.3, never showed up in
2553 * the list of "all objects" maintained by Py_TRACE_REFS. As a
2554 * result, programs leaking references to None and False (etc)
2555 * couldn't be diagnosed by examining sys.getobjects(0).
2556 */
2557#define ADD_TO_ALL(OBJECT) _Py_AddToAllObjects((PyObject *)(OBJECT), 0)
2558#else
2559#define ADD_TO_ALL(OBJECT) (void)0
2560#endif
2561
Tim Peters4b7625e2001-09-13 21:37:17 +00002562#define SETBUILTIN(NAME, OBJECT) \
Tim Peters7571a0f2003-03-23 17:52:28 +00002563 if (PyDict_SetItemString(dict, NAME, (PyObject *)OBJECT) < 0) \
2564 return NULL; \
2565 ADD_TO_ALL(OBJECT)
Tim Peters4b7625e2001-09-13 21:37:17 +00002566
2567 SETBUILTIN("None", Py_None);
2568 SETBUILTIN("Ellipsis", Py_Ellipsis);
2569 SETBUILTIN("NotImplemented", Py_NotImplemented);
Guido van Rossum77f6a652002-04-03 22:41:51 +00002570 SETBUILTIN("False", Py_False);
2571 SETBUILTIN("True", Py_True);
Neal Norwitz32a7e7f2002-05-31 19:58:02 +00002572 SETBUILTIN("basestring", &PyBaseString_Type);
Guido van Rossum77f6a652002-04-03 22:41:51 +00002573 SETBUILTIN("bool", &PyBool_Type);
Travis E. Oliphant3781aef2008-03-18 04:44:57 +00002574 /* SETBUILTIN("memoryview", &PyMemoryView_Type); */
Christian Heimes1a6387e2008-03-26 12:49:49 +00002575 SETBUILTIN("bytearray", &PyBytes_Type);
Christian Heimes288e89a2008-01-18 18:24:07 +00002576 SETBUILTIN("bytes", &PyString_Type);
Guido van Rossumbea18cc2002-06-14 20:41:17 +00002577 SETBUILTIN("buffer", &PyBuffer_Type);
Tim Peters4b7625e2001-09-13 21:37:17 +00002578 SETBUILTIN("classmethod", &PyClassMethod_Type);
Tim Peters6d6c1a32001-08-02 04:15:00 +00002579#ifndef WITHOUT_COMPLEX
Tim Peters4b7625e2001-09-13 21:37:17 +00002580 SETBUILTIN("complex", &PyComplex_Type);
Tim Peters6d6c1a32001-08-02 04:15:00 +00002581#endif
Tim Petersa427a2b2001-10-29 22:25:45 +00002582 SETBUILTIN("dict", &PyDict_Type);
Tim Peters67d687a2002-04-29 21:27:32 +00002583 SETBUILTIN("enumerate", &PyEnum_Type);
Neal Norwitzc4edb0e2006-05-02 04:43:14 +00002584 SETBUILTIN("file", &PyFile_Type);
Tim Peters4b7625e2001-09-13 21:37:17 +00002585 SETBUILTIN("float", &PyFloat_Type);
Raymond Hettingera690a992003-11-16 16:17:49 +00002586 SETBUILTIN("frozenset", &PyFrozenSet_Type);
Tim Peters4b7625e2001-09-13 21:37:17 +00002587 SETBUILTIN("property", &PyProperty_Type);
2588 SETBUILTIN("int", &PyInt_Type);
2589 SETBUILTIN("list", &PyList_Type);
2590 SETBUILTIN("long", &PyLong_Type);
2591 SETBUILTIN("object", &PyBaseObject_Type);
Raymond Hettinger85c20a42003-11-06 14:06:48 +00002592 SETBUILTIN("reversed", &PyReversed_Type);
Raymond Hettingera690a992003-11-16 16:17:49 +00002593 SETBUILTIN("set", &PySet_Type);
Guido van Rossumbea18cc2002-06-14 20:41:17 +00002594 SETBUILTIN("slice", &PySlice_Type);
Tim Peters4b7625e2001-09-13 21:37:17 +00002595 SETBUILTIN("staticmethod", &PyStaticMethod_Type);
2596 SETBUILTIN("str", &PyString_Type);
2597 SETBUILTIN("super", &PySuper_Type);
2598 SETBUILTIN("tuple", &PyTuple_Type);
2599 SETBUILTIN("type", &PyType_Type);
Raymond Hettingerc4c453f2002-06-05 23:12:45 +00002600 SETBUILTIN("xrange", &PyRange_Type);
Martin v. Löwis339d0f72001-08-17 18:39:25 +00002601#ifdef Py_USING_UNICODE
Tim Peters4b7625e2001-09-13 21:37:17 +00002602 SETBUILTIN("unicode", &PyUnicode_Type);
Martin v. Löwis339d0f72001-08-17 18:39:25 +00002603#endif
Guido van Rossum77f6a652002-04-03 22:41:51 +00002604 debug = PyBool_FromLong(Py_OptimizeFlag == 0);
Fred Drake5550de32000-06-20 04:54:19 +00002605 if (PyDict_SetItemString(dict, "__debug__", debug) < 0) {
2606 Py_XDECREF(debug);
Guido van Rossum25ce5661997-08-02 03:10:38 +00002607 return NULL;
Fred Drake5550de32000-06-20 04:54:19 +00002608 }
2609 Py_XDECREF(debug);
Barry Warsaw757af0e1997-08-29 22:13:51 +00002610
Guido van Rossum25ce5661997-08-02 03:10:38 +00002611 return mod;
Tim Peters7571a0f2003-03-23 17:52:28 +00002612#undef ADD_TO_ALL
Tim Peters4b7625e2001-09-13 21:37:17 +00002613#undef SETBUILTIN
Guido van Rossum3f5da241990-12-20 15:06:42 +00002614}
2615
Guido van Rossume77a7571993-11-03 15:01:26 +00002616/* Helper for filter(): filter a tuple through a function */
Guido van Rossum12d12c51993-10-26 17:58:25 +00002617
Guido van Rossum79f25d91997-04-29 20:08:16 +00002618static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002619filtertuple(PyObject *func, PyObject *tuple)
Guido van Rossum12d12c51993-10-26 17:58:25 +00002620{
Guido van Rossum79f25d91997-04-29 20:08:16 +00002621 PyObject *result;
Martin v. Löwis18e16552006-02-15 17:27:45 +00002622 Py_ssize_t i, j;
2623 Py_ssize_t len = PyTuple_Size(tuple);
Guido van Rossum12d12c51993-10-26 17:58:25 +00002624
Guido van Rossumb7b45621995-08-04 04:07:45 +00002625 if (len == 0) {
Walter Dörwaldc3da83f2003-02-04 20:24:45 +00002626 if (PyTuple_CheckExact(tuple))
2627 Py_INCREF(tuple);
2628 else
2629 tuple = PyTuple_New(0);
Guido van Rossumb7b45621995-08-04 04:07:45 +00002630 return tuple;
2631 }
2632
Guido van Rossum79f25d91997-04-29 20:08:16 +00002633 if ((result = PyTuple_New(len)) == NULL)
Guido van Rossum2586bf01993-11-01 16:21:44 +00002634 return NULL;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002635
Guido van Rossum12d12c51993-10-26 17:58:25 +00002636 for (i = j = 0; i < len; ++i) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002637 PyObject *item, *good;
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002638 int ok;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002639
Walter Dörwald8dd19322003-02-10 17:36:40 +00002640 if (tuple->ob_type->tp_as_sequence &&
2641 tuple->ob_type->tp_as_sequence->sq_item) {
2642 item = tuple->ob_type->tp_as_sequence->sq_item(tuple, i);
Walter Dörwaldc58a3a12003-08-18 18:28:45 +00002643 if (item == NULL)
2644 goto Fail_1;
Walter Dörwald8dd19322003-02-10 17:36:40 +00002645 } else {
Alex Martellia9b9c9f2003-04-23 13:34:35 +00002646 PyErr_SetString(PyExc_TypeError, "filter(): unsubscriptable tuple");
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002647 goto Fail_1;
Walter Dörwald8dd19322003-02-10 17:36:40 +00002648 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002649 if (func == Py_None) {
2650 Py_INCREF(item);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002651 good = item;
2652 }
2653 else {
Raymond Hettinger8ae46892003-10-12 19:09:37 +00002654 PyObject *arg = PyTuple_Pack(1, item);
Walter Dörwaldc58a3a12003-08-18 18:28:45 +00002655 if (arg == NULL) {
2656 Py_DECREF(item);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002657 goto Fail_1;
Walter Dörwaldc58a3a12003-08-18 18:28:45 +00002658 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002659 good = PyEval_CallObject(func, arg);
2660 Py_DECREF(arg);
Walter Dörwaldc58a3a12003-08-18 18:28:45 +00002661 if (good == NULL) {
2662 Py_DECREF(item);
Guido van Rossum12d12c51993-10-26 17:58:25 +00002663 goto Fail_1;
Walter Dörwaldc58a3a12003-08-18 18:28:45 +00002664 }
Guido van Rossum12d12c51993-10-26 17:58:25 +00002665 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002666 ok = PyObject_IsTrue(good);
2667 Py_DECREF(good);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002668 if (ok) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002669 if (PyTuple_SetItem(result, j++, item) < 0)
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002670 goto Fail_1;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002671 }
Walter Dörwaldc58a3a12003-08-18 18:28:45 +00002672 else
2673 Py_DECREF(item);
Guido van Rossum12d12c51993-10-26 17:58:25 +00002674 }
2675
Tim Peters4324aa32001-05-28 22:30:08 +00002676 if (_PyTuple_Resize(&result, j) < 0)
Guido van Rossum12d12c51993-10-26 17:58:25 +00002677 return NULL;
2678
Guido van Rossum12d12c51993-10-26 17:58:25 +00002679 return result;
2680
Guido van Rossum12d12c51993-10-26 17:58:25 +00002681Fail_1:
Guido van Rossum79f25d91997-04-29 20:08:16 +00002682 Py_DECREF(result);
Guido van Rossum12d12c51993-10-26 17:58:25 +00002683 return NULL;
2684}
2685
2686
Guido van Rossume77a7571993-11-03 15:01:26 +00002687/* Helper for filter(): filter a string through a function */
Guido van Rossum12d12c51993-10-26 17:58:25 +00002688
Guido van Rossum79f25d91997-04-29 20:08:16 +00002689static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002690filterstring(PyObject *func, PyObject *strobj)
Guido van Rossum12d12c51993-10-26 17:58:25 +00002691{
Guido van Rossum79f25d91997-04-29 20:08:16 +00002692 PyObject *result;
Martin v. Löwis18e16552006-02-15 17:27:45 +00002693 Py_ssize_t i, j;
2694 Py_ssize_t len = PyString_Size(strobj);
2695 Py_ssize_t outlen = len;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002696
Guido van Rossum79f25d91997-04-29 20:08:16 +00002697 if (func == Py_None) {
Walter Dörwald1918f772003-02-10 13:19:13 +00002698 /* If it's a real string we can return the original,
2699 * as no character is ever false and __getitem__
2700 * does return this character. If it's a subclass
2701 * we must go through the __getitem__ loop */
2702 if (PyString_CheckExact(strobj)) {
Walter Dörwaldc3da83f2003-02-04 20:24:45 +00002703 Py_INCREF(strobj);
Walter Dörwald1918f772003-02-10 13:19:13 +00002704 return strobj;
2705 }
Guido van Rossum12d12c51993-10-26 17:58:25 +00002706 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002707 if ((result = PyString_FromStringAndSize(NULL, len)) == NULL)
Guido van Rossum2586bf01993-11-01 16:21:44 +00002708 return NULL;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002709
Guido van Rossum12d12c51993-10-26 17:58:25 +00002710 for (i = j = 0; i < len; ++i) {
Walter Dörwald1918f772003-02-10 13:19:13 +00002711 PyObject *item;
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002712 int ok;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002713
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002714 item = (*strobj->ob_type->tp_as_sequence->sq_item)(strobj, i);
2715 if (item == NULL)
2716 goto Fail_1;
Walter Dörwald1918f772003-02-10 13:19:13 +00002717 if (func==Py_None) {
2718 ok = 1;
2719 } else {
2720 PyObject *arg, *good;
Raymond Hettinger8ae46892003-10-12 19:09:37 +00002721 arg = PyTuple_Pack(1, item);
Walter Dörwald1918f772003-02-10 13:19:13 +00002722 if (arg == NULL) {
2723 Py_DECREF(item);
2724 goto Fail_1;
2725 }
2726 good = PyEval_CallObject(func, arg);
2727 Py_DECREF(arg);
2728 if (good == NULL) {
2729 Py_DECREF(item);
2730 goto Fail_1;
2731 }
2732 ok = PyObject_IsTrue(good);
2733 Py_DECREF(good);
Tim Peters388ed082001-04-07 20:34:48 +00002734 }
Walter Dörwald903f1e02003-02-04 16:28:00 +00002735 if (ok) {
Martin v. Löwisd96ee902006-02-16 14:37:16 +00002736 Py_ssize_t reslen;
Walter Dörwald903f1e02003-02-04 16:28:00 +00002737 if (!PyString_Check(item)) {
2738 PyErr_SetString(PyExc_TypeError, "can't filter str to str:"
2739 " __getitem__ returned different type");
2740 Py_DECREF(item);
2741 goto Fail_1;
2742 }
2743 reslen = PyString_GET_SIZE(item);
2744 if (reslen == 1) {
2745 PyString_AS_STRING(result)[j++] =
2746 PyString_AS_STRING(item)[0];
2747 } else {
2748 /* do we need more space? */
Martin v. Löwisd96ee902006-02-16 14:37:16 +00002749 Py_ssize_t need = j + reslen + len-i-1;
Walter Dörwald903f1e02003-02-04 16:28:00 +00002750 if (need > outlen) {
2751 /* overallocate, to avoid reallocations */
2752 if (need<2*outlen)
2753 need = 2*outlen;
2754 if (_PyString_Resize(&result, need)) {
2755 Py_DECREF(item);
2756 return NULL;
2757 }
2758 outlen = need;
2759 }
2760 memcpy(
2761 PyString_AS_STRING(result) + j,
2762 PyString_AS_STRING(item),
2763 reslen
2764 );
2765 j += reslen;
2766 }
2767 }
Tim Peters388ed082001-04-07 20:34:48 +00002768 Py_DECREF(item);
Guido van Rossum12d12c51993-10-26 17:58:25 +00002769 }
2770
Walter Dörwald903f1e02003-02-04 16:28:00 +00002771 if (j < outlen)
Tim Peters5de98422002-04-27 18:44:32 +00002772 _PyString_Resize(&result, j);
Guido van Rossum12d12c51993-10-26 17:58:25 +00002773
Guido van Rossum12d12c51993-10-26 17:58:25 +00002774 return result;
2775
Guido van Rossum12d12c51993-10-26 17:58:25 +00002776Fail_1:
Guido van Rossum79f25d91997-04-29 20:08:16 +00002777 Py_DECREF(result);
Guido van Rossum12d12c51993-10-26 17:58:25 +00002778 return NULL;
2779}
Martin v. Löwis8afd7572003-01-25 22:46:11 +00002780
2781#ifdef Py_USING_UNICODE
2782/* Helper for filter(): filter a Unicode object through a function */
2783
2784static PyObject *
2785filterunicode(PyObject *func, PyObject *strobj)
2786{
2787 PyObject *result;
Martin v. Löwis725507b2006-03-07 12:08:51 +00002788 register Py_ssize_t i, j;
Martin v. Löwisd96ee902006-02-16 14:37:16 +00002789 Py_ssize_t len = PyUnicode_GetSize(strobj);
2790 Py_ssize_t outlen = len;
Martin v. Löwis8afd7572003-01-25 22:46:11 +00002791
2792 if (func == Py_None) {
Walter Dörwald1918f772003-02-10 13:19:13 +00002793 /* If it's a real string we can return the original,
2794 * as no character is ever false and __getitem__
2795 * does return this character. If it's a subclass
2796 * we must go through the __getitem__ loop */
2797 if (PyUnicode_CheckExact(strobj)) {
Walter Dörwaldc3da83f2003-02-04 20:24:45 +00002798 Py_INCREF(strobj);
Walter Dörwald1918f772003-02-10 13:19:13 +00002799 return strobj;
2800 }
Martin v. Löwis8afd7572003-01-25 22:46:11 +00002801 }
2802 if ((result = PyUnicode_FromUnicode(NULL, len)) == NULL)
2803 return NULL;
2804
2805 for (i = j = 0; i < len; ++i) {
2806 PyObject *item, *arg, *good;
2807 int ok;
2808
2809 item = (*strobj->ob_type->tp_as_sequence->sq_item)(strobj, i);
2810 if (item == NULL)
2811 goto Fail_1;
Walter Dörwald1918f772003-02-10 13:19:13 +00002812 if (func == Py_None) {
2813 ok = 1;
2814 } else {
Raymond Hettinger8ae46892003-10-12 19:09:37 +00002815 arg = PyTuple_Pack(1, item);
Walter Dörwald1918f772003-02-10 13:19:13 +00002816 if (arg == NULL) {
2817 Py_DECREF(item);
2818 goto Fail_1;
2819 }
2820 good = PyEval_CallObject(func, arg);
2821 Py_DECREF(arg);
2822 if (good == NULL) {
2823 Py_DECREF(item);
2824 goto Fail_1;
2825 }
2826 ok = PyObject_IsTrue(good);
2827 Py_DECREF(good);
Martin v. Löwis8afd7572003-01-25 22:46:11 +00002828 }
Walter Dörwald903f1e02003-02-04 16:28:00 +00002829 if (ok) {
Martin v. Löwisd96ee902006-02-16 14:37:16 +00002830 Py_ssize_t reslen;
Walter Dörwald903f1e02003-02-04 16:28:00 +00002831 if (!PyUnicode_Check(item)) {
Georg Brandl99d7e4e2005-08-31 22:21:15 +00002832 PyErr_SetString(PyExc_TypeError,
Jeremy Hylton1aad9c72003-09-16 03:10:59 +00002833 "can't filter unicode to unicode:"
2834 " __getitem__ returned different type");
Walter Dörwald903f1e02003-02-04 16:28:00 +00002835 Py_DECREF(item);
2836 goto Fail_1;
2837 }
2838 reslen = PyUnicode_GET_SIZE(item);
Georg Brandl99d7e4e2005-08-31 22:21:15 +00002839 if (reslen == 1)
Walter Dörwald903f1e02003-02-04 16:28:00 +00002840 PyUnicode_AS_UNICODE(result)[j++] =
2841 PyUnicode_AS_UNICODE(item)[0];
Jeremy Hylton1aad9c72003-09-16 03:10:59 +00002842 else {
Walter Dörwald903f1e02003-02-04 16:28:00 +00002843 /* do we need more space? */
Martin v. Löwisd96ee902006-02-16 14:37:16 +00002844 Py_ssize_t need = j + reslen + len - i - 1;
Walter Dörwald903f1e02003-02-04 16:28:00 +00002845 if (need > outlen) {
Georg Brandl99d7e4e2005-08-31 22:21:15 +00002846 /* overallocate,
Jeremy Hylton1aad9c72003-09-16 03:10:59 +00002847 to avoid reallocations */
2848 if (need < 2 * outlen)
2849 need = 2 * outlen;
Jeremy Hylton364f6be2003-09-16 03:17:16 +00002850 if (PyUnicode_Resize(
2851 &result, need) < 0) {
Walter Dörwald903f1e02003-02-04 16:28:00 +00002852 Py_DECREF(item);
Walter Dörwald531e0002003-02-04 16:57:49 +00002853 goto Fail_1;
Walter Dörwald903f1e02003-02-04 16:28:00 +00002854 }
2855 outlen = need;
2856 }
Jeremy Hylton1aad9c72003-09-16 03:10:59 +00002857 memcpy(PyUnicode_AS_UNICODE(result) + j,
2858 PyUnicode_AS_UNICODE(item),
2859 reslen*sizeof(Py_UNICODE));
Walter Dörwald903f1e02003-02-04 16:28:00 +00002860 j += reslen;
2861 }
2862 }
Martin v. Löwis8afd7572003-01-25 22:46:11 +00002863 Py_DECREF(item);
2864 }
2865
Walter Dörwald903f1e02003-02-04 16:28:00 +00002866 if (j < outlen)
Martin v. Löwis8afd7572003-01-25 22:46:11 +00002867 PyUnicode_Resize(&result, j);
2868
2869 return result;
2870
2871Fail_1:
2872 Py_DECREF(result);
2873 return NULL;
2874}
2875#endif