blob: 228bb2d01b3af927b62ec642e02519672d012627 [file] [log] [blame]
Guido van Rossum3f5da241990-12-20 15:06:42 +00001/* Built-in functions */
2
Guido van Rossum79f25d91997-04-29 20:08:16 +00003#include "Python.h"
Guido van Rossum3f5da241990-12-20 15:06:42 +00004
5#include "node.h"
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006#include "code.h"
Guido van Rossum5b722181993-03-30 17:46:03 +00007#include "eval.h"
Guido van Rossum3f5da241990-12-20 15:06:42 +00008
Guido van Rossum6bf62da1997-04-11 20:37:35 +00009#include <ctype.h>
10
Guido van Rossume2ae77b2001-10-24 20:42:55 +000011#ifdef RISCOS
12#include "unixstuff.h"
13#endif
14
Mark Hammond26cffde2001-05-14 12:17:34 +000015/* The default encoding used by the platform file system APIs
16 Can remain NULL for all platforms that don't have such a concept
17*/
Martin v. Löwis6238d2b2002-06-30 15:26:10 +000018#if defined(MS_WINDOWS) && defined(HAVE_USABLE_WCHAR_T)
Mark Hammond26cffde2001-05-14 12:17:34 +000019const char *Py_FileSystemDefaultEncoding = "mbcs";
Just van Rossumb9b8e9c2003-02-10 09:22:01 +000020#elif defined(__APPLE__)
21const char *Py_FileSystemDefaultEncoding = "utf-8";
Mark Hammond26cffde2001-05-14 12:17:34 +000022#else
23const char *Py_FileSystemDefaultEncoding = NULL; /* use default */
24#endif
Mark Hammondef8b6542001-05-13 08:04:26 +000025
Guido van Rossum12d12c51993-10-26 17:58:25 +000026/* Forward */
Tim Petersdbd9ba62000-07-09 03:09:57 +000027static PyObject *filterstring(PyObject *, PyObject *);
Martin v. Löwis8afd7572003-01-25 22:46:11 +000028#ifdef Py_USING_UNICODE
29static PyObject *filterunicode(PyObject *, PyObject *);
30#endif
Tim Petersdbd9ba62000-07-09 03:09:57 +000031static PyObject *filtertuple (PyObject *, PyObject *);
Guido van Rossum12d12c51993-10-26 17:58:25 +000032
Guido van Rossum79f25d91997-04-29 20:08:16 +000033static PyObject *
Neal Norwitz92e212f2006-04-03 04:48:37 +000034builtin___import__(PyObject *self, PyObject *args, PyObject *kwds)
Guido van Rossum3f5da241990-12-20 15:06:42 +000035{
Neal Norwitz92e212f2006-04-03 04:48:37 +000036 static char *kwlist[] = {"name", "globals", "locals", "fromlist",
37 "level", 0};
Guido van Rossum1ae940a1995-01-02 19:04:15 +000038 char *name;
Guido van Rossum79f25d91997-04-29 20:08:16 +000039 PyObject *globals = NULL;
40 PyObject *locals = NULL;
41 PyObject *fromlist = NULL;
Thomas Woutersf7f438b2006-02-28 16:09:29 +000042 int level = -1;
Guido van Rossum1ae940a1995-01-02 19:04:15 +000043
Neal Norwitz92e212f2006-04-03 04:48:37 +000044 if (!PyArg_ParseTupleAndKeywords(args, kwds, "s|OOOi:__import__",
45 kwlist, &name, &globals, &locals, &fromlist, &level))
Guido van Rossum1ae940a1995-01-02 19:04:15 +000046 return NULL;
Thomas Woutersf7f438b2006-02-28 16:09:29 +000047 return PyImport_ImportModuleLevel(name, globals, locals,
48 fromlist, level);
Guido van Rossum1ae940a1995-01-02 19:04:15 +000049}
50
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000051PyDoc_STRVAR(import_doc,
Neal Norwitz92e212f2006-04-03 04:48:37 +000052"__import__(name, globals={}, locals={}, fromlist=[], level=-1) -> module\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +000053\n\
54Import a module. The globals are only used to determine the context;\n\
55they are not modified. The locals are currently unused. The fromlist\n\
56should be a list of names to emulate ``from name import ...'', or an\n\
57empty list to emulate ``import name''.\n\
58When importing a module from a package, note that __import__('A.B', ...)\n\
59returns package A when fromlist is empty, but its submodule B when\n\
Neal Norwitz92e212f2006-04-03 04:48:37 +000060fromlist is not empty. Level is used to determine whether to perform \n\
61absolute or relative imports. -1 is the original strategy of attempting\n\
62both absolute and relative imports, 0 is absolute, a positive number\n\
63is the number of parent directories to search relative to the current module.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +000064
Guido van Rossum1ae940a1995-01-02 19:04:15 +000065
Guido van Rossum79f25d91997-04-29 20:08:16 +000066static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +000067builtin_abs(PyObject *self, PyObject *v)
Guido van Rossum1ae940a1995-01-02 19:04:15 +000068{
Guido van Rossum09df08a1998-05-22 00:51:39 +000069 return PyNumber_Absolute(v);
Guido van Rossum3f5da241990-12-20 15:06:42 +000070}
71
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000072PyDoc_STRVAR(abs_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +000073"abs(number) -> number\n\
74\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000075Return the absolute value of the argument.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +000076
Raymond Hettinger96229b12005-03-11 06:49:40 +000077static PyObject *
78builtin_all(PyObject *self, PyObject *v)
79{
80 PyObject *it, *item;
Guido van Rossum01dbc102007-12-20 23:48:28 +000081 PyObject *(*iternext)(PyObject *);
82 int cmp;
Raymond Hettinger96229b12005-03-11 06:49:40 +000083
84 it = PyObject_GetIter(v);
85 if (it == NULL)
86 return NULL;
Guido van Rossum01dbc102007-12-20 23:48:28 +000087 iternext = *Py_TYPE(it)->tp_iternext;
Raymond Hettinger96229b12005-03-11 06:49:40 +000088
Guido van Rossum01dbc102007-12-20 23:48:28 +000089 for (;;) {
90 item = iternext(it);
91 if (item == NULL)
92 break;
93 cmp = PyObject_IsTrue(item);
Raymond Hettinger96229b12005-03-11 06:49:40 +000094 Py_DECREF(item);
95 if (cmp < 0) {
96 Py_DECREF(it);
97 return NULL;
98 }
99 if (cmp == 0) {
100 Py_DECREF(it);
101 Py_RETURN_FALSE;
102 }
103 }
104 Py_DECREF(it);
Guido van Rossum01dbc102007-12-20 23:48:28 +0000105 if (PyErr_Occurred()) {
106 if (PyErr_ExceptionMatches(PyExc_StopIteration))
107 PyErr_Clear();
108 else
109 return NULL;
110 }
Raymond Hettinger96229b12005-03-11 06:49:40 +0000111 Py_RETURN_TRUE;
112}
113
114PyDoc_STRVAR(all_doc,
115"all(iterable) -> bool\n\
116\n\
117Return True if bool(x) is True for all values x in the iterable.");
118
119static PyObject *
120builtin_any(PyObject *self, PyObject *v)
121{
122 PyObject *it, *item;
Guido van Rossum01dbc102007-12-20 23:48:28 +0000123 PyObject *(*iternext)(PyObject *);
124 int cmp;
Raymond Hettinger96229b12005-03-11 06:49:40 +0000125
126 it = PyObject_GetIter(v);
127 if (it == NULL)
128 return NULL;
Guido van Rossum01dbc102007-12-20 23:48:28 +0000129 iternext = *Py_TYPE(it)->tp_iternext;
Raymond Hettinger96229b12005-03-11 06:49:40 +0000130
Guido van Rossum01dbc102007-12-20 23:48:28 +0000131 for (;;) {
132 item = iternext(it);
133 if (item == NULL)
134 break;
135 cmp = PyObject_IsTrue(item);
Raymond Hettinger96229b12005-03-11 06:49:40 +0000136 Py_DECREF(item);
137 if (cmp < 0) {
138 Py_DECREF(it);
139 return NULL;
140 }
141 if (cmp == 1) {
142 Py_DECREF(it);
143 Py_RETURN_TRUE;
144 }
145 }
146 Py_DECREF(it);
Guido van Rossum01dbc102007-12-20 23:48:28 +0000147 if (PyErr_Occurred()) {
148 if (PyErr_ExceptionMatches(PyExc_StopIteration))
149 PyErr_Clear();
150 else
151 return NULL;
152 }
Raymond Hettinger96229b12005-03-11 06:49:40 +0000153 Py_RETURN_FALSE;
154}
155
156PyDoc_STRVAR(any_doc,
157"any(iterable) -> bool\n\
158\n\
159Return True if bool(x) is True for any x in the iterable.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000160
Guido van Rossum79f25d91997-04-29 20:08:16 +0000161static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000162builtin_apply(PyObject *self, PyObject *args)
Guido van Rossumc02e15c1991-12-16 13:03:00 +0000163{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000164 PyObject *func, *alist = NULL, *kwdict = NULL;
Barry Warsaw968f8cb1998-10-01 15:33:12 +0000165 PyObject *t = NULL, *retval = NULL;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000166
Neal Norwitz8b2bfbc2007-05-23 06:35:32 +0000167 if (Py_Py3kWarningFlag &&
168 PyErr_Warn(PyExc_DeprecationWarning,
Neal Norwitzb93e7d12008-02-24 02:40:58 +0000169 "apply() not supported in 3.x. Use func(*args, **kwargs).") < 0)
Neal Norwitz8b2bfbc2007-05-23 06:35:32 +0000170 return NULL;
171
Raymond Hettingerea3fdf42002-12-29 16:33:45 +0000172 if (!PyArg_UnpackTuple(args, "apply", 1, 3, &func, &alist, &kwdict))
Guido van Rossumc02e15c1991-12-16 13:03:00 +0000173 return NULL;
Barry Warsaw968f8cb1998-10-01 15:33:12 +0000174 if (alist != NULL) {
175 if (!PyTuple_Check(alist)) {
176 if (!PySequence_Check(alist)) {
Jeremy Hyltonc862cf42001-01-19 03:25:05 +0000177 PyErr_Format(PyExc_TypeError,
Alex Martellia9b9c9f2003-04-23 13:34:35 +0000178 "apply() arg 2 expected sequence, found %s",
Jeremy Hyltonc862cf42001-01-19 03:25:05 +0000179 alist->ob_type->tp_name);
Barry Warsaw968f8cb1998-10-01 15:33:12 +0000180 return NULL;
181 }
182 t = PySequence_Tuple(alist);
183 if (t == NULL)
184 return NULL;
185 alist = t;
186 }
Guido van Rossum2d951851994-08-29 12:52:16 +0000187 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000188 if (kwdict != NULL && !PyDict_Check(kwdict)) {
Jeremy Hyltonc862cf42001-01-19 03:25:05 +0000189 PyErr_Format(PyExc_TypeError,
190 "apply() arg 3 expected dictionary, found %s",
191 kwdict->ob_type->tp_name);
Barry Warsaw968f8cb1998-10-01 15:33:12 +0000192 goto finally;
Guido van Rossum681d79a1995-07-18 14:51:37 +0000193 }
Barry Warsaw968f8cb1998-10-01 15:33:12 +0000194 retval = PyEval_CallObjectWithKeywords(func, alist, kwdict);
195 finally:
196 Py_XDECREF(t);
197 return retval;
Guido van Rossumc02e15c1991-12-16 13:03:00 +0000198}
199
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000200PyDoc_STRVAR(apply_doc,
Fred Drakef1fbc622001-01-12 17:05:05 +0000201"apply(object[, args[, kwargs]]) -> value\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000202\n\
Fred Drake7b912121999-12-23 14:16:55 +0000203Call a callable object with positional arguments taken from the tuple args,\n\
204and keyword arguments taken from the optional dictionary kwargs.\n\
Raymond Hettingerff41c482003-04-06 09:01:11 +0000205Note that classes are callable, as are instances with a __call__() method.\n\
206\n\
207Deprecated since release 2.3. Instead, use the extended call syntax:\n\
208 function(*args, **keywords).");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000209
210
Guido van Rossum79f25d91997-04-29 20:08:16 +0000211static PyObject *
Eric Smith3cd81942008-02-22 16:30:22 +0000212builtin_bin(PyObject *self, PyObject *v)
213{
214 return PyNumber_ToBase(v, 2);
215}
216
217PyDoc_STRVAR(bin_doc,
218"bin(number) -> string\n\
219\n\
220Return the binary representation of an integer or long integer.");
221
222
223static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000224builtin_callable(PyObject *self, PyObject *v)
Guido van Rossum2d951851994-08-29 12:52:16 +0000225{
Neal Norwitzdf25efe2007-05-23 06:58:36 +0000226 if (Py_Py3kWarningFlag &&
227 PyErr_Warn(PyExc_DeprecationWarning,
Neal Norwitzb93e7d12008-02-24 02:40:58 +0000228 "callable() not supported in 3.x. Use hasattr(o, '__call__').") < 0)
Neal Norwitzdf25efe2007-05-23 06:58:36 +0000229 return NULL;
Guido van Rossum77f6a652002-04-03 22:41:51 +0000230 return PyBool_FromLong((long)PyCallable_Check(v));
Guido van Rossum2d951851994-08-29 12:52:16 +0000231}
232
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000233PyDoc_STRVAR(callable_doc,
Guido van Rossum77f6a652002-04-03 22:41:51 +0000234"callable(object) -> bool\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000235\n\
236Return whether the object is callable (i.e., some kind of function).\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000237Note that classes are callable, as are instances with a __call__() method.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000238
239
Guido van Rossum79f25d91997-04-29 20:08:16 +0000240static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000241builtin_filter(PyObject *self, PyObject *args)
Guido van Rossum12d12c51993-10-26 17:58:25 +0000242{
Guido van Rossumc7903a12002-08-16 07:04:56 +0000243 PyObject *func, *seq, *result, *it, *arg;
Martin v. Löwisd96ee902006-02-16 14:37:16 +0000244 Py_ssize_t len; /* guess for result list size */
245 register Py_ssize_t j;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000246
Raymond Hettingerea3fdf42002-12-29 16:33:45 +0000247 if (!PyArg_UnpackTuple(args, "filter", 2, 2, &func, &seq))
Guido van Rossum12d12c51993-10-26 17:58:25 +0000248 return NULL;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000249
Tim Peters0e57abf2001-05-02 07:39:38 +0000250 /* Strings and tuples return a result of the same type. */
251 if (PyString_Check(seq))
252 return filterstring(func, seq);
Martin v. Löwis8afd7572003-01-25 22:46:11 +0000253#ifdef Py_USING_UNICODE
254 if (PyUnicode_Check(seq))
255 return filterunicode(func, seq);
256#endif
Tim Peters0e57abf2001-05-02 07:39:38 +0000257 if (PyTuple_Check(seq))
258 return filtertuple(func, seq);
259
Georg Brandle35b6572005-07-19 22:20:20 +0000260 /* Pre-allocate argument list tuple. */
261 arg = PyTuple_New(1);
262 if (arg == NULL)
263 return NULL;
264
Tim Peters0e57abf2001-05-02 07:39:38 +0000265 /* Get iterator. */
266 it = PyObject_GetIter(seq);
267 if (it == NULL)
Georg Brandle35b6572005-07-19 22:20:20 +0000268 goto Fail_arg;
Tim Peters0e57abf2001-05-02 07:39:38 +0000269
270 /* Guess a result list size. */
Raymond Hettinger4e2f7142007-12-06 00:56:53 +0000271 len = _PyObject_LengthHint(seq, 8);
Guido van Rossum12d12c51993-10-26 17:58:25 +0000272
Tim Peters0e57abf2001-05-02 07:39:38 +0000273 /* Get a result list. */
Guido van Rossum79f25d91997-04-29 20:08:16 +0000274 if (PyList_Check(seq) && seq->ob_refcnt == 1) {
Tim Peters0e57abf2001-05-02 07:39:38 +0000275 /* Eww - can modify the list in-place. */
Guido van Rossum79f25d91997-04-29 20:08:16 +0000276 Py_INCREF(seq);
Guido van Rossum12d12c51993-10-26 17:58:25 +0000277 result = seq;
278 }
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000279 else {
Tim Peters0e57abf2001-05-02 07:39:38 +0000280 result = PyList_New(len);
281 if (result == NULL)
282 goto Fail_it;
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000283 }
Guido van Rossum12d12c51993-10-26 17:58:25 +0000284
Tim Peters0e57abf2001-05-02 07:39:38 +0000285 /* Build the result list. */
Tim Petersf4848da2001-05-05 00:14:56 +0000286 j = 0;
287 for (;;) {
Guido van Rossumc7903a12002-08-16 07:04:56 +0000288 PyObject *item;
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000289 int ok;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000290
Tim Peters0e57abf2001-05-02 07:39:38 +0000291 item = PyIter_Next(it);
292 if (item == NULL) {
Tim Petersf4848da2001-05-05 00:14:56 +0000293 if (PyErr_Occurred())
294 goto Fail_result_it;
Tim Peters0e57abf2001-05-02 07:39:38 +0000295 break;
Guido van Rossum2d951851994-08-29 12:52:16 +0000296 }
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000297
Neil Schemenauer68973552003-08-14 20:37:34 +0000298 if (func == (PyObject *)&PyBool_Type || func == Py_None) {
David Wolever8e6ec2f2008-03-18 21:20:25 +0000299 if (Py_Py3kWarningFlag &&
300 PyErr_Warn(PyExc_DeprecationWarning,
301 "filter with None as a first argument "
302 "is not supported in 3.x. Use a list "
303 "comprehension instead.") < 0)
304 return NULL;
305
Guido van Rossumc7903a12002-08-16 07:04:56 +0000306 ok = PyObject_IsTrue(item);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000307 }
308 else {
Guido van Rossumc7903a12002-08-16 07:04:56 +0000309 PyObject *good;
310 PyTuple_SET_ITEM(arg, 0, item);
311 good = PyObject_Call(func, arg, NULL);
312 PyTuple_SET_ITEM(arg, 0, NULL);
Guido van Rossum58b68731995-01-10 17:40:55 +0000313 if (good == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000314 Py_DECREF(item);
Tim Peters0e57abf2001-05-02 07:39:38 +0000315 goto Fail_result_it;
Guido van Rossum58b68731995-01-10 17:40:55 +0000316 }
Guido van Rossumc7903a12002-08-16 07:04:56 +0000317 ok = PyObject_IsTrue(good);
318 Py_DECREF(good);
Guido van Rossum12d12c51993-10-26 17:58:25 +0000319 }
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000320 if (ok) {
Tim Peters0e57abf2001-05-02 07:39:38 +0000321 if (j < len)
322 PyList_SET_ITEM(result, j, item);
Guido van Rossum2d951851994-08-29 12:52:16 +0000323 else {
Barry Warsawfa77e091999-01-28 18:49:12 +0000324 int status = PyList_Append(result, item);
Barry Warsawfa77e091999-01-28 18:49:12 +0000325 Py_DECREF(item);
326 if (status < 0)
Tim Peters0e57abf2001-05-02 07:39:38 +0000327 goto Fail_result_it;
Guido van Rossum2d951851994-08-29 12:52:16 +0000328 }
Tim Peters0e57abf2001-05-02 07:39:38 +0000329 ++j;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000330 }
Tim Peters0e57abf2001-05-02 07:39:38 +0000331 else
332 Py_DECREF(item);
Guido van Rossum12d12c51993-10-26 17:58:25 +0000333 }
334
Guido van Rossum12d12c51993-10-26 17:58:25 +0000335
Tim Peters0e57abf2001-05-02 07:39:38 +0000336 /* Cut back result list if len is too big. */
Guido van Rossum79f25d91997-04-29 20:08:16 +0000337 if (j < len && PyList_SetSlice(result, j, len, NULL) < 0)
Tim Peters0e57abf2001-05-02 07:39:38 +0000338 goto Fail_result_it;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000339
Tim Peters3c6b1482001-05-21 08:07:05 +0000340 Py_DECREF(it);
Guido van Rossumc7903a12002-08-16 07:04:56 +0000341 Py_DECREF(arg);
Guido van Rossum12d12c51993-10-26 17:58:25 +0000342 return result;
343
Tim Peters0e57abf2001-05-02 07:39:38 +0000344Fail_result_it:
Guido van Rossum79f25d91997-04-29 20:08:16 +0000345 Py_DECREF(result);
Tim Peters0e57abf2001-05-02 07:39:38 +0000346Fail_it:
347 Py_DECREF(it);
Guido van Rossumc7903a12002-08-16 07:04:56 +0000348Fail_arg:
349 Py_DECREF(arg);
Guido van Rossum12d12c51993-10-26 17:58:25 +0000350 return NULL;
351}
352
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000353PyDoc_STRVAR(filter_doc,
Tim Petersd50e5442002-03-09 00:06:26 +0000354"filter(function or None, sequence) -> list, tuple, or string\n"
355"\n"
356"Return those items of sequence for which function(item) is true. If\n"
357"function is None, return the items that are true. If sequence is a tuple\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000358"or string, return the same type, else return a list.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000359
Guido van Rossum79f25d91997-04-29 20:08:16 +0000360static PyObject *
Eric Smitha9f7d622008-02-17 19:46:49 +0000361builtin_format(PyObject *self, PyObject *args)
362{
363 PyObject *value;
364 PyObject *format_spec = NULL;
365
366 if (!PyArg_ParseTuple(args, "O|O:format", &value, &format_spec))
367 return NULL;
368
369 return PyObject_Format(value, format_spec);
370}
371
372PyDoc_STRVAR(format_doc,
373"format(value[, format_spec]) -> string\n\
374\n\
375Returns value.__format__(format_spec)\n\
376format_spec defaults to \"\"");
377
378static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000379builtin_chr(PyObject *self, PyObject *args)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000380{
381 long x;
382 char s[1];
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000383
Guido van Rossum79f25d91997-04-29 20:08:16 +0000384 if (!PyArg_ParseTuple(args, "l:chr", &x))
Guido van Rossum3f5da241990-12-20 15:06:42 +0000385 return NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000386 if (x < 0 || x >= 256) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000387 PyErr_SetString(PyExc_ValueError,
388 "chr() arg not in range(256)");
Guido van Rossum3f5da241990-12-20 15:06:42 +0000389 return NULL;
390 }
Guido van Rossum6bf62da1997-04-11 20:37:35 +0000391 s[0] = (char)x;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000392 return PyString_FromStringAndSize(s, 1);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000393}
394
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000395PyDoc_STRVAR(chr_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000396"chr(i) -> character\n\
397\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000398Return a string of one character with ordinal i; 0 <= i < 256.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000399
400
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000401#ifdef Py_USING_UNICODE
Guido van Rossum79f25d91997-04-29 20:08:16 +0000402static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000403builtin_unichr(PyObject *self, PyObject *args)
Guido van Rossum09095f32000-03-10 23:00:52 +0000404{
405 long x;
Guido van Rossum09095f32000-03-10 23:00:52 +0000406
407 if (!PyArg_ParseTuple(args, "l:unichr", &x))
408 return NULL;
Fredrik Lundh0dcf67e2001-06-26 20:01:56 +0000409
Marc-André Lemburgcc8764c2002-08-11 12:23:04 +0000410 return PyUnicode_FromOrdinal(x);
Guido van Rossum09095f32000-03-10 23:00:52 +0000411}
412
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000413PyDoc_STRVAR(unichr_doc,
Fred Drake078b24f2000-04-13 02:42:50 +0000414"unichr(i) -> Unicode character\n\
Guido van Rossum09095f32000-03-10 23:00:52 +0000415\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000416Return a Unicode string of one character with ordinal i; 0 <= i <= 0x10ffff.");
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000417#endif
Guido van Rossum09095f32000-03-10 23:00:52 +0000418
419
420static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000421builtin_cmp(PyObject *self, PyObject *args)
Guido van Rossuma9e7dc11992-10-18 18:53:57 +0000422{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000423 PyObject *a, *b;
Guido van Rossum09df08a1998-05-22 00:51:39 +0000424 int c;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000425
Raymond Hettingerea3fdf42002-12-29 16:33:45 +0000426 if (!PyArg_UnpackTuple(args, "cmp", 2, 2, &a, &b))
Guido van Rossuma9e7dc11992-10-18 18:53:57 +0000427 return NULL;
Guido van Rossum09df08a1998-05-22 00:51:39 +0000428 if (PyObject_Cmp(a, b, &c) < 0)
Guido van Rossumc8b6df91997-05-23 00:06:51 +0000429 return NULL;
Guido van Rossum09df08a1998-05-22 00:51:39 +0000430 return PyInt_FromLong((long)c);
Guido van Rossuma9e7dc11992-10-18 18:53:57 +0000431}
432
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000433PyDoc_STRVAR(cmp_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000434"cmp(x, y) -> integer\n\
435\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000436Return negative if x<y, zero if x==y, positive if x>y.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000437
438
Guido van Rossum79f25d91997-04-29 20:08:16 +0000439static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000440builtin_coerce(PyObject *self, PyObject *args)
Guido van Rossum6a00cd81995-01-07 12:39:01 +0000441{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000442 PyObject *v, *w;
443 PyObject *res;
Guido van Rossum5524a591995-01-10 15:26:20 +0000444
Neal Norwitzdf25efe2007-05-23 06:58:36 +0000445 if (Py_Py3kWarningFlag &&
446 PyErr_Warn(PyExc_DeprecationWarning,
447 "coerce() not supported in 3.x") < 0)
448 return NULL;
449
Raymond Hettingerea3fdf42002-12-29 16:33:45 +0000450 if (!PyArg_UnpackTuple(args, "coerce", 2, 2, &v, &w))
Guido van Rossum5524a591995-01-10 15:26:20 +0000451 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000452 if (PyNumber_Coerce(&v, &w) < 0)
Guido van Rossum04691fc1992-08-12 15:35:34 +0000453 return NULL;
Raymond Hettinger8ae46892003-10-12 19:09:37 +0000454 res = PyTuple_Pack(2, v, w);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000455 Py_DECREF(v);
456 Py_DECREF(w);
Guido van Rossum04691fc1992-08-12 15:35:34 +0000457 return res;
458}
459
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000460PyDoc_STRVAR(coerce_doc,
Martin v. Löwis8d494f32004-08-25 10:42:41 +0000461"coerce(x, y) -> (x1, y1)\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000462\n\
Martin v. Löwis8d494f32004-08-25 10:42:41 +0000463Return a tuple consisting of the two numeric arguments converted to\n\
464a common type, using the same rules as used by arithmetic operations.\n\
465If coercion is not possible, raise TypeError.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000466
Guido van Rossum79f25d91997-04-29 20:08:16 +0000467static PyObject *
Georg Brandl5240d742007-03-13 20:46:32 +0000468builtin_compile(PyObject *self, PyObject *args, PyObject *kwds)
Guido van Rossum5b722181993-03-30 17:46:03 +0000469{
470 char *str;
471 char *filename;
472 char *startstr;
473 int start;
Tim Peters6cd6a822001-08-17 22:11:27 +0000474 int dont_inherit = 0;
475 int supplied_flags = 0;
Tim Peters5ba58662001-07-16 02:29:45 +0000476 PyCompilerFlags cf;
Neal Norwitz3a9a3e72005-11-27 20:38:31 +0000477 PyObject *result = NULL, *cmd, *tmp = NULL;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000478 Py_ssize_t length;
Georg Brandl5240d742007-03-13 20:46:32 +0000479 static char *kwlist[] = {"source", "filename", "mode", "flags",
480 "dont_inherit", NULL};
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000481
Georg Brandl5240d742007-03-13 20:46:32 +0000482 if (!PyArg_ParseTupleAndKeywords(args, kwds, "Oss|ii:compile",
483 kwlist, &cmd, &filename, &startstr,
484 &supplied_flags, &dont_inherit))
Guido van Rossum5b722181993-03-30 17:46:03 +0000485 return NULL;
Tim Peters6cd6a822001-08-17 22:11:27 +0000486
Just van Rossum3aaf42c2003-02-10 08:21:10 +0000487 cf.cf_flags = supplied_flags;
488
489#ifdef Py_USING_UNICODE
490 if (PyUnicode_Check(cmd)) {
491 tmp = PyUnicode_AsUTF8String(cmd);
492 if (tmp == NULL)
493 return NULL;
494 cmd = tmp;
495 cf.cf_flags |= PyCF_SOURCE_IS_UTF8;
496 }
497#endif
Just van Rossumb9b8e9c2003-02-10 09:22:01 +0000498 if (PyObject_AsReadBuffer(cmd, (const void **)&str, &length))
499 return NULL;
Tim Peters2c646c92003-02-10 14:48:29 +0000500 if ((size_t)length != strlen(str)) {
Just van Rossum3aaf42c2003-02-10 08:21:10 +0000501 PyErr_SetString(PyExc_TypeError,
Alex Martellia9b9c9f2003-04-23 13:34:35 +0000502 "compile() expected string without null bytes");
Neal Norwitz3a9a3e72005-11-27 20:38:31 +0000503 goto cleanup;
Just van Rossum3aaf42c2003-02-10 08:21:10 +0000504 }
505
Guido van Rossum5b722181993-03-30 17:46:03 +0000506 if (strcmp(startstr, "exec") == 0)
Guido van Rossumb05a5c71997-05-07 17:46:13 +0000507 start = Py_file_input;
Guido van Rossum5b722181993-03-30 17:46:03 +0000508 else if (strcmp(startstr, "eval") == 0)
Guido van Rossumb05a5c71997-05-07 17:46:13 +0000509 start = Py_eval_input;
Guido van Rossum872537c1995-07-07 22:43:42 +0000510 else if (strcmp(startstr, "single") == 0)
Guido van Rossumb05a5c71997-05-07 17:46:13 +0000511 start = Py_single_input;
Guido van Rossum5b722181993-03-30 17:46:03 +0000512 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000513 PyErr_SetString(PyExc_ValueError,
Fred Drake661ea262000-10-24 19:57:45 +0000514 "compile() arg 3 must be 'exec' or 'eval' or 'single'");
Neal Norwitz3a9a3e72005-11-27 20:38:31 +0000515 goto cleanup;
Guido van Rossum5b722181993-03-30 17:46:03 +0000516 }
Tim Peters6cd6a822001-08-17 22:11:27 +0000517
Guido van Rossum4b499dd32003-02-13 22:07:59 +0000518 if (supplied_flags &
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000519 ~(PyCF_MASK | PyCF_MASK_OBSOLETE | PyCF_DONT_IMPLY_DEDENT | PyCF_ONLY_AST))
Guido van Rossum4b499dd32003-02-13 22:07:59 +0000520 {
Tim Peters6cd6a822001-08-17 22:11:27 +0000521 PyErr_SetString(PyExc_ValueError,
522 "compile(): unrecognised flags");
Neal Norwitz3a9a3e72005-11-27 20:38:31 +0000523 goto cleanup;
Tim Peters6cd6a822001-08-17 22:11:27 +0000524 }
525 /* XXX Warn if (supplied_flags & PyCF_MASK_OBSOLETE) != 0? */
526
Tim Peters6cd6a822001-08-17 22:11:27 +0000527 if (!dont_inherit) {
528 PyEval_MergeCompilerFlags(&cf);
529 }
Just van Rossum3aaf42c2003-02-10 08:21:10 +0000530 result = Py_CompileStringFlags(str, filename, start, &cf);
Neal Norwitz3a9a3e72005-11-27 20:38:31 +0000531cleanup:
Just van Rossum3aaf42c2003-02-10 08:21:10 +0000532 Py_XDECREF(tmp);
533 return result;
Guido van Rossum5b722181993-03-30 17:46:03 +0000534}
535
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000536PyDoc_STRVAR(compile_doc,
Tim Peters6cd6a822001-08-17 22:11:27 +0000537"compile(source, filename, mode[, flags[, dont_inherit]]) -> code object\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000538\n\
539Compile the source string (a Python module, statement or expression)\n\
540into a code object that can be executed by the exec statement or eval().\n\
541The filename will be used for run-time error messages.\n\
542The mode must be 'exec' to compile a module, 'single' to compile a\n\
Tim Peters6cd6a822001-08-17 22:11:27 +0000543single (interactive) statement, or 'eval' to compile an expression.\n\
544The flags argument, if present, controls which future statements influence\n\
545the compilation of the code.\n\
546The dont_inherit argument, if non-zero, stops the compilation inheriting\n\
547the effects of any future statements in effect in the code calling\n\
548compile; if absent or zero these statements do influence the compilation,\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000549in addition to any features explicitly specified.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000550
Guido van Rossum79f25d91997-04-29 20:08:16 +0000551static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000552builtin_dir(PyObject *self, PyObject *args)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000553{
Tim Peters5d2b77c2001-09-03 05:47:38 +0000554 PyObject *arg = NULL;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000555
Raymond Hettingerea3fdf42002-12-29 16:33:45 +0000556 if (!PyArg_UnpackTuple(args, "dir", 0, 1, &arg))
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000557 return NULL;
Tim Peters7eea37e2001-09-04 22:08:56 +0000558 return PyObject_Dir(arg);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000559}
560
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000561PyDoc_STRVAR(dir_doc,
Tim Peters5d2b77c2001-09-03 05:47:38 +0000562"dir([object]) -> list of strings\n"
563"\n"
Georg Brandl871f1bc2007-03-12 13:17:36 +0000564"If called without an argument, return the names in the current scope.\n"
565"Else, return an alphabetized list of names comprising (some of) the attributes\n"
566"of the given object, and of attributes reachable from it.\n"
567"If the object supplies a method named __dir__, it will be used; otherwise\n"
568"the default dir() logic is used and returns:\n"
569" for a module object: the module's attributes.\n"
570" for a class object: its attributes, and recursively the attributes\n"
571" of its bases.\n"
Georg Brandl3bb15672007-03-13 07:23:16 +0000572" for any other object: its attributes, its class's attributes, and\n"
Georg Brandl871f1bc2007-03-12 13:17:36 +0000573" recursively the attributes of its class's base classes.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000574
Guido van Rossum79f25d91997-04-29 20:08:16 +0000575static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000576builtin_divmod(PyObject *self, PyObject *args)
Guido van Rossum6a00cd81995-01-07 12:39:01 +0000577{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000578 PyObject *v, *w;
Guido van Rossum6a00cd81995-01-07 12:39:01 +0000579
Raymond Hettingerea3fdf42002-12-29 16:33:45 +0000580 if (!PyArg_UnpackTuple(args, "divmod", 2, 2, &v, &w))
Guido van Rossum6a00cd81995-01-07 12:39:01 +0000581 return NULL;
Guido van Rossum09df08a1998-05-22 00:51:39 +0000582 return PyNumber_Divmod(v, w);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000583}
584
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000585PyDoc_STRVAR(divmod_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000586"divmod(x, y) -> (div, mod)\n\
587\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000588Return the tuple ((x-x%y)/y, x%y). Invariant: div*y + mod == x.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000589
590
Guido van Rossum79f25d91997-04-29 20:08:16 +0000591static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000592builtin_eval(PyObject *self, PyObject *args)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000593{
Just van Rossum3aaf42c2003-02-10 08:21:10 +0000594 PyObject *cmd, *result, *tmp = NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000595 PyObject *globals = Py_None, *locals = Py_None;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000596 char *str;
Tim Peters9fa96be2001-08-17 23:04:59 +0000597 PyCompilerFlags cf;
Guido van Rossum590baa41993-11-30 13:40:46 +0000598
Raymond Hettinger214b1c32004-07-02 06:41:07 +0000599 if (!PyArg_UnpackTuple(args, "eval", 1, 3, &cmd, &globals, &locals))
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000600 return NULL;
Raymond Hettinger214b1c32004-07-02 06:41:07 +0000601 if (locals != Py_None && !PyMapping_Check(locals)) {
602 PyErr_SetString(PyExc_TypeError, "locals must be a mapping");
Raymond Hettinger513ffe82004-07-06 13:44:41 +0000603 return NULL;
Raymond Hettinger214b1c32004-07-02 06:41:07 +0000604 }
605 if (globals != Py_None && !PyDict_Check(globals)) {
Georg Brandl99d7e4e2005-08-31 22:21:15 +0000606 PyErr_SetString(PyExc_TypeError, PyMapping_Check(globals) ?
Raymond Hettinger214b1c32004-07-02 06:41:07 +0000607 "globals must be a real dict; try eval(expr, {}, mapping)"
608 : "globals must be a dict");
Raymond Hettinger513ffe82004-07-06 13:44:41 +0000609 return NULL;
Raymond Hettinger214b1c32004-07-02 06:41:07 +0000610 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000611 if (globals == Py_None) {
612 globals = PyEval_GetGlobals();
613 if (locals == Py_None)
614 locals = PyEval_GetLocals();
Guido van Rossum6135a871995-01-09 17:53:26 +0000615 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000616 else if (locals == Py_None)
Guido van Rossum6135a871995-01-09 17:53:26 +0000617 locals = globals;
Tim Peters9fa96be2001-08-17 23:04:59 +0000618
Georg Brandl77c85e62005-09-15 10:46:13 +0000619 if (globals == NULL || locals == NULL) {
620 PyErr_SetString(PyExc_TypeError,
621 "eval must be given globals and locals "
622 "when called without a frame");
623 return NULL;
624 }
625
Guido van Rossum79f25d91997-04-29 20:08:16 +0000626 if (PyDict_GetItemString(globals, "__builtins__") == NULL) {
627 if (PyDict_SetItemString(globals, "__builtins__",
628 PyEval_GetBuiltins()) != 0)
Guido van Rossum6135a871995-01-09 17:53:26 +0000629 return NULL;
630 }
Tim Peters9fa96be2001-08-17 23:04:59 +0000631
Jeremy Hylton15c1c4f2001-07-30 21:50:55 +0000632 if (PyCode_Check(cmd)) {
Jeremy Hylton733c8932001-12-13 19:51:56 +0000633 if (PyCode_GetNumFree((PyCodeObject *)cmd) > 0) {
Jeremy Hylton15c1c4f2001-07-30 21:50:55 +0000634 PyErr_SetString(PyExc_TypeError,
635 "code object passed to eval() may not contain free variables");
636 return NULL;
637 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000638 return PyEval_EvalCode((PyCodeObject *) cmd, globals, locals);
Jeremy Hylton15c1c4f2001-07-30 21:50:55 +0000639 }
Tim Peters9fa96be2001-08-17 23:04:59 +0000640
Marc-André Lemburgd1ba4432000-09-19 21:04:18 +0000641 if (!PyString_Check(cmd) &&
642 !PyUnicode_Check(cmd)) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000643 PyErr_SetString(PyExc_TypeError,
Fred Drake661ea262000-10-24 19:57:45 +0000644 "eval() arg 1 must be a string or code object");
Guido van Rossum94390a41992-08-14 15:14:30 +0000645 return NULL;
646 }
Just van Rossum3aaf42c2003-02-10 08:21:10 +0000647 cf.cf_flags = 0;
648
649#ifdef Py_USING_UNICODE
650 if (PyUnicode_Check(cmd)) {
651 tmp = PyUnicode_AsUTF8String(cmd);
652 if (tmp == NULL)
653 return NULL;
654 cmd = tmp;
655 cf.cf_flags |= PyCF_SOURCE_IS_UTF8;
656 }
657#endif
Neal Norwitz3a9a3e72005-11-27 20:38:31 +0000658 if (PyString_AsStringAndSize(cmd, &str, NULL)) {
659 Py_XDECREF(tmp);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000660 return NULL;
Neal Norwitz3a9a3e72005-11-27 20:38:31 +0000661 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000662 while (*str == ' ' || *str == '\t')
663 str++;
Tim Peters9fa96be2001-08-17 23:04:59 +0000664
Tim Peters9fa96be2001-08-17 23:04:59 +0000665 (void)PyEval_MergeCompilerFlags(&cf);
Just van Rossum3aaf42c2003-02-10 08:21:10 +0000666 result = PyRun_StringFlags(str, Py_eval_input, globals, locals, &cf);
667 Py_XDECREF(tmp);
668 return result;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000669}
670
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000671PyDoc_STRVAR(eval_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000672"eval(source[, globals[, locals]]) -> value\n\
673\n\
674Evaluate the source in the context of globals and locals.\n\
675The source may be a string representing a Python expression\n\
676or a code object as returned by compile().\n\
Neal Norwitz477ca1c2006-09-05 02:25:41 +0000677The globals must be a dictionary and locals can be any mapping,\n\
Raymond Hettinger214b1c32004-07-02 06:41:07 +0000678defaulting to the current globals and locals.\n\
679If only globals is given, locals defaults to it.\n");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000680
681
Guido van Rossum79f25d91997-04-29 20:08:16 +0000682static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000683builtin_execfile(PyObject *self, PyObject *args)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000684{
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000685 char *filename;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000686 PyObject *globals = Py_None, *locals = Py_None;
687 PyObject *res;
Guido van Rossumb3a639e2001-09-05 13:37:47 +0000688 FILE* fp = NULL;
Tim Peters5ba58662001-07-16 02:29:45 +0000689 PyCompilerFlags cf;
Martin v. Löwis6b3a2c42001-08-08 05:30:36 +0000690 int exists;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000691
Neal Norwitzdf25efe2007-05-23 06:58:36 +0000692 if (Py_Py3kWarningFlag &&
693 PyErr_Warn(PyExc_DeprecationWarning,
Neal Norwitzb93e7d12008-02-24 02:40:58 +0000694 "execfile() not supported in 3.x. Use exec().") < 0)
Neal Norwitzdf25efe2007-05-23 06:58:36 +0000695 return NULL;
696
Raymond Hettinger66bd2332004-08-02 08:30:07 +0000697 if (!PyArg_ParseTuple(args, "s|O!O:execfile",
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000698 &filename,
Guido van Rossum79f25d91997-04-29 20:08:16 +0000699 &PyDict_Type, &globals,
Raymond Hettinger66bd2332004-08-02 08:30:07 +0000700 &locals))
Guido van Rossum0f61f8a1992-02-25 18:55:05 +0000701 return NULL;
Raymond Hettinger66bd2332004-08-02 08:30:07 +0000702 if (locals != Py_None && !PyMapping_Check(locals)) {
703 PyErr_SetString(PyExc_TypeError, "locals must be a mapping");
704 return NULL;
705 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000706 if (globals == Py_None) {
707 globals = PyEval_GetGlobals();
708 if (locals == Py_None)
709 locals = PyEval_GetLocals();
Guido van Rossum6135a871995-01-09 17:53:26 +0000710 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000711 else if (locals == Py_None)
Guido van Rossum6135a871995-01-09 17:53:26 +0000712 locals = globals;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000713 if (PyDict_GetItemString(globals, "__builtins__") == NULL) {
714 if (PyDict_SetItemString(globals, "__builtins__",
715 PyEval_GetBuiltins()) != 0)
Guido van Rossum6135a871995-01-09 17:53:26 +0000716 return NULL;
717 }
Martin v. Löwis6b3a2c42001-08-08 05:30:36 +0000718
719 exists = 0;
720 /* Test for existence or directory. */
Martin v. Löwis3484a182002-03-09 12:07:51 +0000721#if defined(PLAN9)
722 {
723 Dir *d;
724
725 if ((d = dirstat(filename))!=nil) {
726 if(d->mode & DMDIR)
727 werrstr("is a directory");
728 else
729 exists = 1;
730 free(d);
731 }
Martin v. Löwis6b3a2c42001-08-08 05:30:36 +0000732 }
Martin v. Löwis3484a182002-03-09 12:07:51 +0000733#elif defined(RISCOS)
Guido van Rossume2ae77b2001-10-24 20:42:55 +0000734 if (object_exists(filename)) {
735 if (isdir(filename))
736 errno = EISDIR;
737 else
738 exists = 1;
739 }
Martin v. Löwis3484a182002-03-09 12:07:51 +0000740#else /* standard Posix */
741 {
742 struct stat s;
743 if (stat(filename, &s) == 0) {
744 if (S_ISDIR(s.st_mode))
Andrew MacIntyreda4d6cb2004-03-29 11:53:38 +0000745# if defined(PYOS_OS2) && defined(PYCC_VACPP)
Martin v. Löwis3484a182002-03-09 12:07:51 +0000746 errno = EOS2ERR;
747# else
748 errno = EISDIR;
749# endif
750 else
751 exists = 1;
752 }
753 }
754#endif
Martin v. Löwis6b3a2c42001-08-08 05:30:36 +0000755
756 if (exists) {
757 Py_BEGIN_ALLOW_THREADS
Jack Jansen7b8c7542002-04-14 20:12:41 +0000758 fp = fopen(filename, "r" PY_STDIOTEXTMODE);
Martin v. Löwis6b3a2c42001-08-08 05:30:36 +0000759 Py_END_ALLOW_THREADS
760
761 if (fp == NULL) {
762 exists = 0;
763 }
764 }
765
766 if (!exists) {
Peter Schneider-Kamp4c013422002-08-27 16:58:00 +0000767 PyErr_SetFromErrnoWithFilename(PyExc_IOError, filename);
Guido van Rossum0f61f8a1992-02-25 18:55:05 +0000768 return NULL;
769 }
Tim Peters5ba58662001-07-16 02:29:45 +0000770 cf.cf_flags = 0;
771 if (PyEval_MergeCompilerFlags(&cf))
Tim Peters748b8bb2001-04-28 08:20:22 +0000772 res = PyRun_FileExFlags(fp, filename, Py_file_input, globals,
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000773 locals, 1, &cf);
Tim Peters5ba58662001-07-16 02:29:45 +0000774 else
Tim Peters748b8bb2001-04-28 08:20:22 +0000775 res = PyRun_FileEx(fp, filename, Py_file_input, globals,
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000776 locals, 1);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000777 return res;
Guido van Rossum0f61f8a1992-02-25 18:55:05 +0000778}
779
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000780PyDoc_STRVAR(execfile_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000781"execfile(filename[, globals[, locals]])\n\
782\n\
783Read and execute a Python script from a file.\n\
784The globals and locals are dictionaries, defaulting to the current\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000785globals and locals. If only globals is given, locals defaults to it.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000786
787
Guido van Rossum79f25d91997-04-29 20:08:16 +0000788static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000789builtin_getattr(PyObject *self, PyObject *args)
Guido van Rossum33894be1992-01-27 16:53:09 +0000790{
Guido van Rossum950ff291998-06-29 13:38:57 +0000791 PyObject *v, *result, *dflt = NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000792 PyObject *name;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000793
Raymond Hettingerea3fdf42002-12-29 16:33:45 +0000794 if (!PyArg_UnpackTuple(args, "getattr", 2, 3, &v, &name, &dflt))
Guido van Rossum33894be1992-01-27 16:53:09 +0000795 return NULL;
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000796#ifdef Py_USING_UNICODE
Jeremy Hylton0eb11152001-07-30 22:39:31 +0000797 if (PyUnicode_Check(name)) {
798 name = _PyUnicode_AsDefaultEncodedString(name, NULL);
799 if (name == NULL)
800 return NULL;
801 }
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000802#endif
Jeremy Hylton0eb11152001-07-30 22:39:31 +0000803
804 if (!PyString_Check(name)) {
805 PyErr_SetString(PyExc_TypeError,
Alex Martellia9b9c9f2003-04-23 13:34:35 +0000806 "getattr(): attribute name must be string");
Jeremy Hylton0eb11152001-07-30 22:39:31 +0000807 return NULL;
808 }
Guido van Rossum950ff291998-06-29 13:38:57 +0000809 result = PyObject_GetAttr(v, name);
Guido van Rossumd8923572001-10-16 21:31:32 +0000810 if (result == NULL && dflt != NULL &&
811 PyErr_ExceptionMatches(PyExc_AttributeError))
812 {
Guido van Rossum950ff291998-06-29 13:38:57 +0000813 PyErr_Clear();
814 Py_INCREF(dflt);
815 result = dflt;
816 }
817 return result;
Guido van Rossum9bfef441993-03-29 10:43:31 +0000818}
819
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000820PyDoc_STRVAR(getattr_doc,
Guido van Rossum950ff291998-06-29 13:38:57 +0000821"getattr(object, name[, default]) -> value\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000822\n\
Guido van Rossum950ff291998-06-29 13:38:57 +0000823Get a named attribute from an object; getattr(x, 'y') is equivalent to x.y.\n\
824When a default argument is given, it is returned when the attribute doesn't\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000825exist; without it, an exception is raised in that case.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000826
827
Guido van Rossum79f25d91997-04-29 20:08:16 +0000828static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000829builtin_globals(PyObject *self)
Guido van Rossum872537c1995-07-07 22:43:42 +0000830{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000831 PyObject *d;
Guido van Rossum872537c1995-07-07 22:43:42 +0000832
Guido van Rossum79f25d91997-04-29 20:08:16 +0000833 d = PyEval_GetGlobals();
Neal Norwitz43bd4db2006-08-12 01:46:42 +0000834 Py_XINCREF(d);
Guido van Rossum872537c1995-07-07 22:43:42 +0000835 return d;
836}
837
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000838PyDoc_STRVAR(globals_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000839"globals() -> dictionary\n\
840\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000841Return the dictionary containing the current scope's global variables.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000842
843
Guido van Rossum79f25d91997-04-29 20:08:16 +0000844static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000845builtin_hasattr(PyObject *self, PyObject *args)
Guido van Rossum9bfef441993-03-29 10:43:31 +0000846{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000847 PyObject *v;
848 PyObject *name;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000849
Raymond Hettingerea3fdf42002-12-29 16:33:45 +0000850 if (!PyArg_UnpackTuple(args, "hasattr", 2, 2, &v, &name))
Guido van Rossum9bfef441993-03-29 10:43:31 +0000851 return NULL;
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000852#ifdef Py_USING_UNICODE
Jeremy Hylton302b54a2001-07-30 22:45:19 +0000853 if (PyUnicode_Check(name)) {
854 name = _PyUnicode_AsDefaultEncodedString(name, NULL);
855 if (name == NULL)
856 return NULL;
857 }
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000858#endif
Jeremy Hylton302b54a2001-07-30 22:45:19 +0000859
860 if (!PyString_Check(name)) {
861 PyErr_SetString(PyExc_TypeError,
Alex Martellia9b9c9f2003-04-23 13:34:35 +0000862 "hasattr(): attribute name must be string");
Jeremy Hylton302b54a2001-07-30 22:45:19 +0000863 return NULL;
864 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000865 v = PyObject_GetAttr(v, name);
Guido van Rossum9bfef441993-03-29 10:43:31 +0000866 if (v == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000867 PyErr_Clear();
Guido van Rossum09df08a1998-05-22 00:51:39 +0000868 Py_INCREF(Py_False);
869 return Py_False;
Guido van Rossum9bfef441993-03-29 10:43:31 +0000870 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000871 Py_DECREF(v);
Guido van Rossum09df08a1998-05-22 00:51:39 +0000872 Py_INCREF(Py_True);
873 return Py_True;
Guido van Rossum33894be1992-01-27 16:53:09 +0000874}
875
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000876PyDoc_STRVAR(hasattr_doc,
Guido van Rossum77f6a652002-04-03 22:41:51 +0000877"hasattr(object, name) -> bool\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000878\n\
879Return whether the object has an attribute with the given name.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000880(This is done by calling getattr(object, name) and catching exceptions.)");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000881
882
Guido van Rossum79f25d91997-04-29 20:08:16 +0000883static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000884builtin_id(PyObject *self, PyObject *v)
Guido van Rossum5b722181993-03-30 17:46:03 +0000885{
Guido van Rossum106f2da2000-06-28 21:12:25 +0000886 return PyLong_FromVoidPtr(v);
Guido van Rossum5b722181993-03-30 17:46:03 +0000887}
888
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000889PyDoc_STRVAR(id_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000890"id(object) -> integer\n\
891\n\
892Return the identity of an object. This is guaranteed to be unique among\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000893simultaneously existing objects. (Hint: it's the object's memory address.)");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000894
895
Guido van Rossum79f25d91997-04-29 20:08:16 +0000896static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000897builtin_map(PyObject *self, PyObject *args)
Guido van Rossum12d12c51993-10-26 17:58:25 +0000898{
899 typedef struct {
Tim Peters4e9afdc2001-05-03 23:54:49 +0000900 PyObject *it; /* the iterator object */
901 int saw_StopIteration; /* bool: did the iterator end? */
Guido van Rossum12d12c51993-10-26 17:58:25 +0000902 } sequence;
903
Guido van Rossum79f25d91997-04-29 20:08:16 +0000904 PyObject *func, *result;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000905 sequence *seqs = NULL, *sqp;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000906 Py_ssize_t n, len;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000907 register int i, j;
908
Guido van Rossum79f25d91997-04-29 20:08:16 +0000909 n = PyTuple_Size(args);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000910 if (n < 2) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000911 PyErr_SetString(PyExc_TypeError,
912 "map() requires at least two args");
Guido van Rossum12d12c51993-10-26 17:58:25 +0000913 return NULL;
914 }
915
Guido van Rossum79f25d91997-04-29 20:08:16 +0000916 func = PyTuple_GetItem(args, 0);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000917 n--;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000918
Neal Norwitz53152a12008-02-24 02:20:25 +0000919 if (func == Py_None) {
920 if (Py_Py3kWarningFlag &&
921 PyErr_Warn(PyExc_DeprecationWarning,
Neal Norwitzb93e7d12008-02-24 02:40:58 +0000922 "map(None, ...) not supported in 3.x. Use list(...).") < 0)
Neal Norwitz53152a12008-02-24 02:20:25 +0000923 return NULL;
924 if (n == 1) {
925 /* map(None, S) is the same as list(S). */
926 return PySequence_List(PyTuple_GetItem(args, 1));
927 }
Guido van Rossumfa4ac711998-07-10 17:37:30 +0000928 }
929
Tim Peters4e9afdc2001-05-03 23:54:49 +0000930 /* Get space for sequence descriptors. Must NULL out the iterator
931 * pointers so that jumping to Fail_2 later doesn't see trash.
932 */
Guido van Rossum79f25d91997-04-29 20:08:16 +0000933 if ((seqs = PyMem_NEW(sequence, n)) == NULL) {
934 PyErr_NoMemory();
Tim Peters4e9afdc2001-05-03 23:54:49 +0000935 return NULL;
936 }
937 for (i = 0; i < n; ++i) {
938 seqs[i].it = (PyObject*)NULL;
939 seqs[i].saw_StopIteration = 0;
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000940 }
Guido van Rossum12d12c51993-10-26 17:58:25 +0000941
Tim Peters4e9afdc2001-05-03 23:54:49 +0000942 /* Do a first pass to obtain iterators for the arguments, and set len
943 * to the largest of their lengths.
Tim Peters748b8bb2001-04-28 08:20:22 +0000944 */
Tim Peters4e9afdc2001-05-03 23:54:49 +0000945 len = 0;
946 for (i = 0, sqp = seqs; i < n; ++i, ++sqp) {
947 PyObject *curseq;
Martin v. Löwisd96ee902006-02-16 14:37:16 +0000948 Py_ssize_t curlen;
Guido van Rossumad991772001-01-12 16:03:05 +0000949
Tim Peters4e9afdc2001-05-03 23:54:49 +0000950 /* Get iterator. */
951 curseq = PyTuple_GetItem(args, i+1);
952 sqp->it = PyObject_GetIter(curseq);
953 if (sqp->it == NULL) {
Guido van Rossum12d12c51993-10-26 17:58:25 +0000954 static char errmsg[] =
Tim Peters4e9afdc2001-05-03 23:54:49 +0000955 "argument %d to map() must support iteration";
Guido van Rossum15e33a41997-04-30 19:00:27 +0000956 char errbuf[sizeof(errmsg) + 25];
Jeremy Hylton518ab1c2001-11-28 20:42:20 +0000957 PyOS_snprintf(errbuf, sizeof(errbuf), errmsg, i+2);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000958 PyErr_SetString(PyExc_TypeError, errbuf);
Guido van Rossum12d12c51993-10-26 17:58:25 +0000959 goto Fail_2;
960 }
961
Tim Peters4e9afdc2001-05-03 23:54:49 +0000962 /* Update len. */
Raymond Hettinger4e2f7142007-12-06 00:56:53 +0000963 curlen = _PyObject_LengthHint(curseq, 8);
Raymond Hettinger77f3c872004-01-04 08:54:44 +0000964 if (curlen > len)
965 len = curlen;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000966 }
967
Tim Peters4e9afdc2001-05-03 23:54:49 +0000968 /* Get space for the result list. */
Guido van Rossum79f25d91997-04-29 20:08:16 +0000969 if ((result = (PyObject *) PyList_New(len)) == NULL)
Guido van Rossum12d12c51993-10-26 17:58:25 +0000970 goto Fail_2;
971
Tim Peters4e9afdc2001-05-03 23:54:49 +0000972 /* Iterate over the sequences until all have stopped. */
Guido van Rossum2d951851994-08-29 12:52:16 +0000973 for (i = 0; ; ++i) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000974 PyObject *alist, *item=NULL, *value;
Tim Peters4e9afdc2001-05-03 23:54:49 +0000975 int numactive = 0;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000976
Guido van Rossum79f25d91997-04-29 20:08:16 +0000977 if (func == Py_None && n == 1)
Guido van Rossum32120311995-07-10 13:52:21 +0000978 alist = NULL;
Tim Peters4e9afdc2001-05-03 23:54:49 +0000979 else if ((alist = PyTuple_New(n)) == NULL)
980 goto Fail_1;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000981
982 for (j = 0, sqp = seqs; j < n; ++j, ++sqp) {
Tim Peters4e9afdc2001-05-03 23:54:49 +0000983 if (sqp->saw_StopIteration) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000984 Py_INCREF(Py_None);
985 item = Py_None;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000986 }
Guido van Rossum12d12c51993-10-26 17:58:25 +0000987 else {
Tim Peters4e9afdc2001-05-03 23:54:49 +0000988 item = PyIter_Next(sqp->it);
989 if (item)
990 ++numactive;
991 else {
Tim Peters4e9afdc2001-05-03 23:54:49 +0000992 if (PyErr_Occurred()) {
Tim Petersf4848da2001-05-05 00:14:56 +0000993 Py_XDECREF(alist);
994 goto Fail_1;
Guido van Rossum2d951851994-08-29 12:52:16 +0000995 }
Tim Peters4e9afdc2001-05-03 23:54:49 +0000996 Py_INCREF(Py_None);
997 item = Py_None;
998 sqp->saw_StopIteration = 1;
Guido van Rossum2d951851994-08-29 12:52:16 +0000999 }
Guido van Rossum12d12c51993-10-26 17:58:25 +00001000 }
Tim Peters4e9afdc2001-05-03 23:54:49 +00001001 if (alist)
1002 PyTuple_SET_ITEM(alist, j, item);
1003 else
Guido van Rossum2d951851994-08-29 12:52:16 +00001004 break;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001005 }
1006
Guido van Rossum32120311995-07-10 13:52:21 +00001007 if (!alist)
1008 alist = item;
Guido van Rossum2d951851994-08-29 12:52:16 +00001009
Tim Peters4e9afdc2001-05-03 23:54:49 +00001010 if (numactive == 0) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001011 Py_DECREF(alist);
Guido van Rossum2d951851994-08-29 12:52:16 +00001012 break;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001013 }
Guido van Rossum2d951851994-08-29 12:52:16 +00001014
Guido van Rossum79f25d91997-04-29 20:08:16 +00001015 if (func == Py_None)
Guido van Rossum32120311995-07-10 13:52:21 +00001016 value = alist;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001017 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001018 value = PyEval_CallObject(func, alist);
1019 Py_DECREF(alist);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00001020 if (value == NULL)
1021 goto Fail_1;
Guido van Rossum2d951851994-08-29 12:52:16 +00001022 }
1023 if (i >= len) {
Barry Warsawfa77e091999-01-28 18:49:12 +00001024 int status = PyList_Append(result, value);
Barry Warsaw21332871999-01-28 04:21:35 +00001025 Py_DECREF(value);
Barry Warsawfa77e091999-01-28 18:49:12 +00001026 if (status < 0)
1027 goto Fail_1;
Guido van Rossum2d951851994-08-29 12:52:16 +00001028 }
Tim Peters4e9afdc2001-05-03 23:54:49 +00001029 else if (PyList_SetItem(result, i, value) < 0)
1030 goto Fail_1;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001031 }
1032
Guido van Rossumfa4ac711998-07-10 17:37:30 +00001033 if (i < len && PyList_SetSlice(result, i, len, NULL) < 0)
1034 goto Fail_1;
1035
Tim Peters4e9afdc2001-05-03 23:54:49 +00001036 goto Succeed;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001037
Guido van Rossum12d12c51993-10-26 17:58:25 +00001038Fail_1:
Guido van Rossum79f25d91997-04-29 20:08:16 +00001039 Py_DECREF(result);
Guido van Rossum12d12c51993-10-26 17:58:25 +00001040Fail_2:
Tim Peters4e9afdc2001-05-03 23:54:49 +00001041 result = NULL;
1042Succeed:
1043 assert(seqs);
1044 for (i = 0; i < n; ++i)
1045 Py_XDECREF(seqs[i].it);
1046 PyMem_DEL(seqs);
1047 return result;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001048}
1049
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001050PyDoc_STRVAR(map_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001051"map(function, sequence[, sequence, ...]) -> list\n\
1052\n\
1053Return a list of the results of applying the function to the items of\n\
1054the argument sequence(s). If more than one sequence is given, the\n\
1055function is called with an argument list consisting of the corresponding\n\
1056item of each sequence, substituting None for missing values when not all\n\
1057sequences have the same length. If the function is None, return a list of\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001058the items of the sequence (or a list of tuples if more than one sequence).");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001059
1060
Guido van Rossum79f25d91997-04-29 20:08:16 +00001061static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001062builtin_setattr(PyObject *self, PyObject *args)
Guido van Rossum33894be1992-01-27 16:53:09 +00001063{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001064 PyObject *v;
1065 PyObject *name;
1066 PyObject *value;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001067
Raymond Hettingerea3fdf42002-12-29 16:33:45 +00001068 if (!PyArg_UnpackTuple(args, "setattr", 3, 3, &v, &name, &value))
Guido van Rossum33894be1992-01-27 16:53:09 +00001069 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001070 if (PyObject_SetAttr(v, name, value) != 0)
Guido van Rossum33894be1992-01-27 16:53:09 +00001071 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001072 Py_INCREF(Py_None);
1073 return Py_None;
Guido van Rossum33894be1992-01-27 16:53:09 +00001074}
1075
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001076PyDoc_STRVAR(setattr_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001077"setattr(object, name, value)\n\
1078\n\
1079Set a named attribute on an object; setattr(x, 'y', v) is equivalent to\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001080``x.y = v''.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001081
1082
Guido van Rossum79f25d91997-04-29 20:08:16 +00001083static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001084builtin_delattr(PyObject *self, PyObject *args)
Guido van Rossum14144fc1994-08-29 12:53:40 +00001085{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001086 PyObject *v;
1087 PyObject *name;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001088
Raymond Hettingerea3fdf42002-12-29 16:33:45 +00001089 if (!PyArg_UnpackTuple(args, "delattr", 2, 2, &v, &name))
Guido van Rossum14144fc1994-08-29 12:53:40 +00001090 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001091 if (PyObject_SetAttr(v, name, (PyObject *)NULL) != 0)
Guido van Rossum14144fc1994-08-29 12:53:40 +00001092 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001093 Py_INCREF(Py_None);
1094 return Py_None;
Guido van Rossum14144fc1994-08-29 12:53:40 +00001095}
1096
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001097PyDoc_STRVAR(delattr_doc,
Guido van Rossumdf12a591998-11-23 22:13:04 +00001098"delattr(object, name)\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001099\n\
1100Delete a named attribute on an object; delattr(x, 'y') is equivalent to\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001101``del x.y''.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001102
1103
Guido van Rossum79f25d91997-04-29 20:08:16 +00001104static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001105builtin_hash(PyObject *self, PyObject *v)
Guido van Rossum9bfef441993-03-29 10:43:31 +00001106{
Guido van Rossum9bfef441993-03-29 10:43:31 +00001107 long x;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001108
Guido van Rossum79f25d91997-04-29 20:08:16 +00001109 x = PyObject_Hash(v);
Guido van Rossum9bfef441993-03-29 10:43:31 +00001110 if (x == -1)
1111 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001112 return PyInt_FromLong(x);
Guido van Rossum9bfef441993-03-29 10:43:31 +00001113}
1114
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001115PyDoc_STRVAR(hash_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001116"hash(object) -> integer\n\
1117\n\
1118Return a hash value for the object. Two objects with the same value have\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001119the same hash value. The reverse is not necessarily true, but likely.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001120
1121
Guido van Rossum79f25d91997-04-29 20:08:16 +00001122static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001123builtin_hex(PyObject *self, PyObject *v)
Guido van Rossum006bcd41991-10-24 14:54:44 +00001124{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001125 PyNumberMethods *nb;
Neil Schemenauer3a313e32004-07-19 16:29:17 +00001126 PyObject *res;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001127
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001128 if ((nb = v->ob_type->tp_as_number) == NULL ||
Guido van Rossum1899c2e1992-09-12 11:09:23 +00001129 nb->nb_hex == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001130 PyErr_SetString(PyExc_TypeError,
Guido van Rossum1899c2e1992-09-12 11:09:23 +00001131 "hex() argument can't be converted to hex");
1132 return NULL;
Guido van Rossum006bcd41991-10-24 14:54:44 +00001133 }
Neil Schemenauer3a313e32004-07-19 16:29:17 +00001134 res = (*nb->nb_hex)(v);
1135 if (res && !PyString_Check(res)) {
1136 PyErr_Format(PyExc_TypeError,
1137 "__hex__ returned non-string (type %.200s)",
1138 res->ob_type->tp_name);
1139 Py_DECREF(res);
1140 return NULL;
1141 }
1142 return res;
Guido van Rossum006bcd41991-10-24 14:54:44 +00001143}
1144
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001145PyDoc_STRVAR(hex_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001146"hex(number) -> string\n\
1147\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001148Return the hexadecimal representation of an integer or long integer.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001149
1150
Tim Petersdbd9ba62000-07-09 03:09:57 +00001151static PyObject *builtin_raw_input(PyObject *, PyObject *);
Guido van Rossum3165fe61992-09-25 21:59:05 +00001152
Guido van Rossum79f25d91997-04-29 20:08:16 +00001153static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001154builtin_input(PyObject *self, PyObject *args)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001155{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001156 PyObject *line;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001157 char *str;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001158 PyObject *res;
1159 PyObject *globals, *locals;
Hye-Shik Changff83c2b2004-02-02 13:39:01 +00001160 PyCompilerFlags cf;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001161
1162 line = builtin_raw_input(self, args);
Guido van Rossum3165fe61992-09-25 21:59:05 +00001163 if (line == NULL)
1164 return line;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001165 if (!PyArg_Parse(line, "s;embedded '\\0' in input line", &str))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001166 return NULL;
1167 while (*str == ' ' || *str == '\t')
1168 str++;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001169 globals = PyEval_GetGlobals();
1170 locals = PyEval_GetLocals();
1171 if (PyDict_GetItemString(globals, "__builtins__") == NULL) {
1172 if (PyDict_SetItemString(globals, "__builtins__",
1173 PyEval_GetBuiltins()) != 0)
Guido van Rossum6135a871995-01-09 17:53:26 +00001174 return NULL;
1175 }
Hye-Shik Changff83c2b2004-02-02 13:39:01 +00001176 cf.cf_flags = 0;
1177 PyEval_MergeCompilerFlags(&cf);
1178 res = PyRun_StringFlags(str, Py_eval_input, globals, locals, &cf);
Guido van Rossum79f25d91997-04-29 20:08:16 +00001179 Py_DECREF(line);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001180 return res;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001181}
1182
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001183PyDoc_STRVAR(input_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001184"input([prompt]) -> value\n\
1185\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001186Equivalent to eval(raw_input(prompt)).");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001187
1188
Guido van Rossume8811f81997-02-14 15:48:05 +00001189static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001190builtin_intern(PyObject *self, PyObject *args)
Guido van Rossume8811f81997-02-14 15:48:05 +00001191{
1192 PyObject *s;
Guido van Rossum43713e52000-02-29 13:59:29 +00001193 if (!PyArg_ParseTuple(args, "S:intern", &s))
Guido van Rossume8811f81997-02-14 15:48:05 +00001194 return NULL;
Jeremy Hylton4c989dd2004-08-07 19:20:05 +00001195 if (!PyString_CheckExact(s)) {
1196 PyErr_SetString(PyExc_TypeError,
1197 "can't intern subclass of string");
1198 return NULL;
1199 }
Guido van Rossume8811f81997-02-14 15:48:05 +00001200 Py_INCREF(s);
1201 PyString_InternInPlace(&s);
1202 return s;
1203}
1204
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001205PyDoc_STRVAR(intern_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001206"intern(string) -> string\n\
1207\n\
1208``Intern'' the given string. This enters the string in the (global)\n\
1209table of interned strings whose purpose is to speed up dictionary lookups.\n\
1210Return the string itself or the previously interned string object with the\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001211same value.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001212
1213
Guido van Rossum79f25d91997-04-29 20:08:16 +00001214static PyObject *
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001215builtin_iter(PyObject *self, PyObject *args)
1216{
1217 PyObject *v, *w = NULL;
1218
Raymond Hettingerea3fdf42002-12-29 16:33:45 +00001219 if (!PyArg_UnpackTuple(args, "iter", 1, 2, &v, &w))
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001220 return NULL;
1221 if (w == NULL)
1222 return PyObject_GetIter(v);
1223 if (!PyCallable_Check(v)) {
1224 PyErr_SetString(PyExc_TypeError,
1225 "iter(v, w): v must be callable");
1226 return NULL;
1227 }
1228 return PyCallIter_New(v, w);
1229}
1230
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001231PyDoc_STRVAR(iter_doc,
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001232"iter(collection) -> iterator\n\
1233iter(callable, sentinel) -> iterator\n\
1234\n\
1235Get an iterator from an object. In the first form, the argument must\n\
1236supply its own iterator, or be a sequence.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001237In the second form, the callable is called until it returns the sentinel.");
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001238
1239
1240static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001241builtin_len(PyObject *self, PyObject *v)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001242{
Martin v. Löwis18e16552006-02-15 17:27:45 +00001243 Py_ssize_t res;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001244
Jeremy Hylton03657cf2000-07-12 13:05:33 +00001245 res = PyObject_Size(v);
Guido van Rossum8ea9f4d1998-06-29 22:26:50 +00001246 if (res < 0 && PyErr_Occurred())
1247 return NULL;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001248 return PyInt_FromSsize_t(res);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001249}
1250
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001251PyDoc_STRVAR(len_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001252"len(object) -> integer\n\
1253\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001254Return the number of items of a sequence or mapping.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001255
1256
Guido van Rossum79f25d91997-04-29 20:08:16 +00001257static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001258builtin_locals(PyObject *self)
Guido van Rossum872537c1995-07-07 22:43:42 +00001259{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001260 PyObject *d;
Guido van Rossum872537c1995-07-07 22:43:42 +00001261
Guido van Rossum79f25d91997-04-29 20:08:16 +00001262 d = PyEval_GetLocals();
Neal Norwitz43bd4db2006-08-12 01:46:42 +00001263 Py_XINCREF(d);
Guido van Rossum872537c1995-07-07 22:43:42 +00001264 return d;
1265}
1266
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001267PyDoc_STRVAR(locals_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001268"locals() -> dictionary\n\
1269\n\
Raymond Hettinger69bf8f32003-01-04 02:16:22 +00001270Update and return a dictionary containing the current scope's local variables.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001271
1272
Guido van Rossum79f25d91997-04-29 20:08:16 +00001273static PyObject *
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001274min_max(PyObject *args, PyObject *kwds, int op)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001275{
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001276 PyObject *v, *it, *item, *val, *maxitem, *maxval, *keyfunc=NULL;
Martin v. Löwisfd39ad42004-08-12 14:42:37 +00001277 const char *name = op == Py_LT ? "min" : "max";
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001278
Guido van Rossum79f25d91997-04-29 20:08:16 +00001279 if (PyTuple_Size(args) > 1)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001280 v = args;
Martin v. Löwisfd39ad42004-08-12 14:42:37 +00001281 else if (!PyArg_UnpackTuple(args, (char *)name, 1, 1, &v))
Guido van Rossum3f5da241990-12-20 15:06:42 +00001282 return NULL;
Tim Peters67d687a2002-04-29 21:27:32 +00001283
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001284 if (kwds != NULL && PyDict_Check(kwds) && PyDict_Size(kwds)) {
1285 keyfunc = PyDict_GetItemString(kwds, "key");
1286 if (PyDict_Size(kwds)!=1 || keyfunc == NULL) {
Georg Brandl99d7e4e2005-08-31 22:21:15 +00001287 PyErr_Format(PyExc_TypeError,
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001288 "%s() got an unexpected keyword argument", name);
1289 return NULL;
1290 }
Guido van Rossum1d9a9ea2008-01-23 20:19:01 +00001291 Py_INCREF(keyfunc);
Georg Brandl99d7e4e2005-08-31 22:21:15 +00001292 }
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001293
Tim Petersc3074532001-05-03 07:00:32 +00001294 it = PyObject_GetIter(v);
Guido van Rossum1d9a9ea2008-01-23 20:19:01 +00001295 if (it == NULL) {
1296 Py_XDECREF(keyfunc);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001297 return NULL;
Guido van Rossum1d9a9ea2008-01-23 20:19:01 +00001298 }
Tim Petersc3074532001-05-03 07:00:32 +00001299
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001300 maxitem = NULL; /* the result */
1301 maxval = NULL; /* the value associated with the result */
Brett Cannon9e635cf2004-12-07 00:25:35 +00001302 while (( item = PyIter_Next(it) )) {
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001303 /* get the value from the key function */
1304 if (keyfunc != NULL) {
1305 val = PyObject_CallFunctionObjArgs(keyfunc, item, NULL);
1306 if (val == NULL)
1307 goto Fail_it_item;
1308 }
1309 /* no key function; the value is the item */
1310 else {
1311 val = item;
1312 Py_INCREF(val);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001313 }
Tim Petersc3074532001-05-03 07:00:32 +00001314
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001315 /* maximum value and item are unset; set them */
1316 if (maxval == NULL) {
1317 maxitem = item;
1318 maxval = val;
1319 }
1320 /* maximum value and item are set; update them as necessary */
Guido van Rossum2d951851994-08-29 12:52:16 +00001321 else {
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001322 int cmp = PyObject_RichCompareBool(val, maxval, op);
1323 if (cmp < 0)
1324 goto Fail_it_item_and_val;
1325 else if (cmp > 0) {
1326 Py_DECREF(maxval);
1327 Py_DECREF(maxitem);
1328 maxval = val;
1329 maxitem = item;
Guido van Rossum53451b32001-01-17 15:47:24 +00001330 }
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001331 else {
1332 Py_DECREF(item);
1333 Py_DECREF(val);
Guido van Rossumc8b6df91997-05-23 00:06:51 +00001334 }
Guido van Rossum2d951851994-08-29 12:52:16 +00001335 }
Guido van Rossum3f5da241990-12-20 15:06:42 +00001336 }
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001337 if (PyErr_Occurred())
1338 goto Fail_it;
1339 if (maxval == NULL) {
Martin v. Löwisfd39ad42004-08-12 14:42:37 +00001340 PyErr_Format(PyExc_ValueError,
1341 "%s() arg is an empty sequence", name);
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001342 assert(maxitem == NULL);
1343 }
1344 else
1345 Py_DECREF(maxval);
Tim Petersc3074532001-05-03 07:00:32 +00001346 Py_DECREF(it);
Guido van Rossum1d9a9ea2008-01-23 20:19:01 +00001347 Py_XDECREF(keyfunc);
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001348 return maxitem;
1349
1350Fail_it_item_and_val:
1351 Py_DECREF(val);
1352Fail_it_item:
1353 Py_DECREF(item);
1354Fail_it:
1355 Py_XDECREF(maxval);
1356 Py_XDECREF(maxitem);
1357 Py_DECREF(it);
Guido van Rossum1d9a9ea2008-01-23 20:19:01 +00001358 Py_XDECREF(keyfunc);
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001359 return NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001360}
1361
Guido van Rossum79f25d91997-04-29 20:08:16 +00001362static PyObject *
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001363builtin_min(PyObject *self, PyObject *args, PyObject *kwds)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001364{
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001365 return min_max(args, kwds, Py_LT);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001366}
1367
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001368PyDoc_STRVAR(min_doc,
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001369"min(iterable[, key=func]) -> value\n\
1370min(a, b, c, ...[, key=func]) -> value\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001371\n\
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001372With a single iterable argument, return its smallest item.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001373With two or more arguments, return the smallest argument.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001374
1375
Guido van Rossum79f25d91997-04-29 20:08:16 +00001376static PyObject *
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001377builtin_max(PyObject *self, PyObject *args, PyObject *kwds)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001378{
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001379 return min_max(args, kwds, Py_GT);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001380}
1381
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001382PyDoc_STRVAR(max_doc,
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001383"max(iterable[, key=func]) -> value\n\
1384max(a, b, c, ...[, key=func]) -> value\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001385\n\
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001386With a single iterable argument, return its largest item.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001387With two or more arguments, return the largest argument.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001388
1389
Guido van Rossum79f25d91997-04-29 20:08:16 +00001390static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001391builtin_oct(PyObject *self, PyObject *v)
Guido van Rossum006bcd41991-10-24 14:54:44 +00001392{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001393 PyNumberMethods *nb;
Neil Schemenauer3a313e32004-07-19 16:29:17 +00001394 PyObject *res;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001395
Guido van Rossum1899c2e1992-09-12 11:09:23 +00001396 if (v == NULL || (nb = v->ob_type->tp_as_number) == NULL ||
1397 nb->nb_oct == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001398 PyErr_SetString(PyExc_TypeError,
Guido van Rossum1899c2e1992-09-12 11:09:23 +00001399 "oct() argument can't be converted to oct");
1400 return NULL;
Guido van Rossum006bcd41991-10-24 14:54:44 +00001401 }
Neil Schemenauer3a313e32004-07-19 16:29:17 +00001402 res = (*nb->nb_oct)(v);
1403 if (res && !PyString_Check(res)) {
1404 PyErr_Format(PyExc_TypeError,
1405 "__oct__ returned non-string (type %.200s)",
1406 res->ob_type->tp_name);
1407 Py_DECREF(res);
1408 return NULL;
1409 }
1410 return res;
Guido van Rossum006bcd41991-10-24 14:54:44 +00001411}
1412
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001413PyDoc_STRVAR(oct_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001414"oct(number) -> string\n\
1415\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001416Return the octal representation of an integer or long integer.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001417
1418
Guido van Rossum79f25d91997-04-29 20:08:16 +00001419static PyObject *
Neal Norwitzc4edb0e2006-05-02 04:43:14 +00001420builtin_open(PyObject *self, PyObject *args, PyObject *kwds)
1421{
1422 return PyObject_Call((PyObject*)&PyFile_Type, args, kwds);
1423}
1424
1425PyDoc_STRVAR(open_doc,
1426"open(name[, mode[, buffering]]) -> file object\n\
1427\n\
Skip Montanaro4e3ebe02007-12-08 14:37:43 +00001428Open a file using the file() type, returns a file object. This is the\n\
1429preferred way to open a file.");
Neal Norwitzc4edb0e2006-05-02 04:43:14 +00001430
1431
1432static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001433builtin_ord(PyObject *self, PyObject* obj)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001434{
Guido van Rossum09095f32000-03-10 23:00:52 +00001435 long ord;
Martin v. Löwisd96ee902006-02-16 14:37:16 +00001436 Py_ssize_t size;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001437
Jeremy Hylton394b54d2000-04-12 21:19:47 +00001438 if (PyString_Check(obj)) {
1439 size = PyString_GET_SIZE(obj);
Andrew M. Kuchling34c20cf2000-12-20 14:36:56 +00001440 if (size == 1) {
Jeremy Hylton394b54d2000-04-12 21:19:47 +00001441 ord = (long)((unsigned char)*PyString_AS_STRING(obj));
Andrew M. Kuchling34c20cf2000-12-20 14:36:56 +00001442 return PyInt_FromLong(ord);
1443 }
Martin v. Löwis339d0f72001-08-17 18:39:25 +00001444#ifdef Py_USING_UNICODE
Jeremy Hylton394b54d2000-04-12 21:19:47 +00001445 } else if (PyUnicode_Check(obj)) {
1446 size = PyUnicode_GET_SIZE(obj);
Andrew M. Kuchling34c20cf2000-12-20 14:36:56 +00001447 if (size == 1) {
Jeremy Hylton394b54d2000-04-12 21:19:47 +00001448 ord = (long)*PyUnicode_AS_UNICODE(obj);
Andrew M. Kuchling34c20cf2000-12-20 14:36:56 +00001449 return PyInt_FromLong(ord);
1450 }
Martin v. Löwis339d0f72001-08-17 18:39:25 +00001451#endif
Jeremy Hylton394b54d2000-04-12 21:19:47 +00001452 } else {
1453 PyErr_Format(PyExc_TypeError,
Andrew M. Kuchlingf07aad12000-12-23 14:11:28 +00001454 "ord() expected string of length 1, but " \
Jeremy Hylton394b54d2000-04-12 21:19:47 +00001455 "%.200s found", obj->ob_type->tp_name);
Guido van Rossum09095f32000-03-10 23:00:52 +00001456 return NULL;
1457 }
1458
Guido van Rossumad991772001-01-12 16:03:05 +00001459 PyErr_Format(PyExc_TypeError,
Andrew M. Kuchlingf07aad12000-12-23 14:11:28 +00001460 "ord() expected a character, "
Martin v. Löwisd96ee902006-02-16 14:37:16 +00001461 "but string of length %zd found",
Jeremy Hylton394b54d2000-04-12 21:19:47 +00001462 size);
1463 return NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001464}
1465
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001466PyDoc_STRVAR(ord_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001467"ord(c) -> integer\n\
1468\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001469Return the integer ordinal of a one-character string.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001470
1471
Guido van Rossum79f25d91997-04-29 20:08:16 +00001472static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001473builtin_pow(PyObject *self, PyObject *args)
Guido van Rossum6a00cd81995-01-07 12:39:01 +00001474{
Guido van Rossum09df08a1998-05-22 00:51:39 +00001475 PyObject *v, *w, *z = Py_None;
Guido van Rossum6a00cd81995-01-07 12:39:01 +00001476
Raymond Hettingerea3fdf42002-12-29 16:33:45 +00001477 if (!PyArg_UnpackTuple(args, "pow", 2, 3, &v, &w, &z))
Guido van Rossum6a00cd81995-01-07 12:39:01 +00001478 return NULL;
Guido van Rossum09df08a1998-05-22 00:51:39 +00001479 return PyNumber_Power(v, w, z);
Guido van Rossumd4905451991-05-05 20:00:36 +00001480}
1481
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001482PyDoc_STRVAR(pow_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001483"pow(x, y[, z]) -> number\n\
1484\n\
1485With two arguments, equivalent to x**y. With three arguments,\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001486equivalent to (x**y) % z, but may be more efficient (e.g. for longs).");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001487
1488
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001489
1490/* Return number of items in range (lo, hi, step), when arguments are
1491 * PyInt or PyLong objects. step > 0 required. Return a value < 0 if
1492 * & only if the true value is too large to fit in a signed long.
1493 * Arguments MUST return 1 with either PyInt_Check() or
1494 * PyLong_Check(). Return -1 when there is an error.
1495 */
1496static long
1497get_len_of_range_longs(PyObject *lo, PyObject *hi, PyObject *step)
1498{
1499 /* -------------------------------------------------------------
1500 Algorithm is equal to that of get_len_of_range(), but it operates
1501 on PyObjects (which are assumed to be PyLong or PyInt objects).
1502 ---------------------------------------------------------------*/
1503 long n;
1504 PyObject *diff = NULL;
1505 PyObject *one = NULL;
1506 PyObject *tmp1 = NULL, *tmp2 = NULL, *tmp3 = NULL;
1507 /* holds sub-expression evaluations */
1508
1509 /* if (lo >= hi), return length of 0. */
1510 if (PyObject_Compare(lo, hi) >= 0)
1511 return 0;
1512
1513 if ((one = PyLong_FromLong(1L)) == NULL)
1514 goto Fail;
1515
1516 if ((tmp1 = PyNumber_Subtract(hi, lo)) == NULL)
1517 goto Fail;
1518
1519 if ((diff = PyNumber_Subtract(tmp1, one)) == NULL)
1520 goto Fail;
1521
1522 if ((tmp2 = PyNumber_FloorDivide(diff, step)) == NULL)
1523 goto Fail;
1524
1525 if ((tmp3 = PyNumber_Add(tmp2, one)) == NULL)
1526 goto Fail;
1527
1528 n = PyLong_AsLong(tmp3);
1529 if (PyErr_Occurred()) { /* Check for Overflow */
1530 PyErr_Clear();
1531 goto Fail;
1532 }
1533
1534 Py_DECREF(tmp3);
1535 Py_DECREF(tmp2);
1536 Py_DECREF(diff);
1537 Py_DECREF(tmp1);
1538 Py_DECREF(one);
1539 return n;
1540
1541 Fail:
1542 Py_XDECREF(tmp3);
1543 Py_XDECREF(tmp2);
1544 Py_XDECREF(diff);
1545 Py_XDECREF(tmp1);
1546 Py_XDECREF(one);
1547 return -1;
1548}
1549
1550/* An extension of builtin_range() that handles the case when PyLong
1551 * arguments are given. */
1552static PyObject *
1553handle_range_longs(PyObject *self, PyObject *args)
1554{
1555 PyObject *ilow;
Tim Peters874e1f72003-04-13 22:13:08 +00001556 PyObject *ihigh = NULL;
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001557 PyObject *istep = NULL;
Tim Peters874e1f72003-04-13 22:13:08 +00001558
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001559 PyObject *curnum = NULL;
1560 PyObject *v = NULL;
1561 long bign;
1562 int i, n;
1563 int cmp_result;
1564
Tim Peters874e1f72003-04-13 22:13:08 +00001565 PyObject *zero = PyLong_FromLong(0);
1566
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001567 if (zero == NULL)
1568 return NULL;
1569
Tim Peters874e1f72003-04-13 22:13:08 +00001570 if (!PyArg_UnpackTuple(args, "range", 1, 3, &ilow, &ihigh, &istep)) {
1571 Py_DECREF(zero);
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001572 return NULL;
1573 }
1574
Tim Peters874e1f72003-04-13 22:13:08 +00001575 /* Figure out which way we were called, supply defaults, and be
1576 * sure to incref everything so that the decrefs at the end
1577 * are correct.
1578 */
1579 assert(ilow != NULL);
1580 if (ihigh == NULL) {
1581 /* only 1 arg -- it's the upper limit */
1582 ihigh = ilow;
1583 ilow = NULL;
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001584 }
Tim Peters874e1f72003-04-13 22:13:08 +00001585 assert(ihigh != NULL);
1586 Py_INCREF(ihigh);
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001587
Tim Peters874e1f72003-04-13 22:13:08 +00001588 /* ihigh correct now; do ilow */
1589 if (ilow == NULL)
1590 ilow = zero;
1591 Py_INCREF(ilow);
1592
1593 /* ilow and ihigh correct now; do istep */
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001594 if (istep == NULL) {
1595 istep = PyLong_FromLong(1L);
1596 if (istep == NULL)
1597 goto Fail;
1598 }
1599 else {
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001600 Py_INCREF(istep);
1601 }
1602
Tim Peters874e1f72003-04-13 22:13:08 +00001603 if (!PyInt_Check(ilow) && !PyLong_Check(ilow)) {
Guido van Rossum28e83e32003-04-15 12:43:26 +00001604 PyErr_Format(PyExc_TypeError,
Alex Martellif471d472003-04-23 13:00:44 +00001605 "range() integer start argument expected, got %s.",
Guido van Rossum817d6c92003-04-14 18:25:04 +00001606 ilow->ob_type->tp_name);
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001607 goto Fail;
1608 }
1609
Tim Peters874e1f72003-04-13 22:13:08 +00001610 if (!PyInt_Check(ihigh) && !PyLong_Check(ihigh)) {
Guido van Rossum28e83e32003-04-15 12:43:26 +00001611 PyErr_Format(PyExc_TypeError,
Alex Martellif471d472003-04-23 13:00:44 +00001612 "range() integer end argument expected, got %s.",
Guido van Rossum817d6c92003-04-14 18:25:04 +00001613 ihigh->ob_type->tp_name);
Tim Peters874e1f72003-04-13 22:13:08 +00001614 goto Fail;
1615 }
1616
1617 if (!PyInt_Check(istep) && !PyLong_Check(istep)) {
Guido van Rossum28e83e32003-04-15 12:43:26 +00001618 PyErr_Format(PyExc_TypeError,
Alex Martellif471d472003-04-23 13:00:44 +00001619 "range() integer step argument expected, got %s.",
Guido van Rossum817d6c92003-04-14 18:25:04 +00001620 istep->ob_type->tp_name);
Tim Peters874e1f72003-04-13 22:13:08 +00001621 goto Fail;
1622 }
1623
1624 if (PyObject_Cmp(istep, zero, &cmp_result) == -1)
1625 goto Fail;
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001626 if (cmp_result == 0) {
1627 PyErr_SetString(PyExc_ValueError,
Alex Martellif471d472003-04-23 13:00:44 +00001628 "range() step argument must not be zero");
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001629 goto Fail;
1630 }
1631
1632 if (cmp_result > 0)
1633 bign = get_len_of_range_longs(ilow, ihigh, istep);
1634 else {
1635 PyObject *neg_istep = PyNumber_Negative(istep);
1636 if (neg_istep == NULL)
1637 goto Fail;
1638 bign = get_len_of_range_longs(ihigh, ilow, neg_istep);
1639 Py_DECREF(neg_istep);
1640 }
1641
1642 n = (int)bign;
1643 if (bign < 0 || (long)n != bign) {
1644 PyErr_SetString(PyExc_OverflowError,
1645 "range() result has too many items");
1646 goto Fail;
1647 }
1648
1649 v = PyList_New(n);
1650 if (v == NULL)
1651 goto Fail;
1652
1653 curnum = ilow;
1654 Py_INCREF(curnum);
1655
1656 for (i = 0; i < n; i++) {
1657 PyObject *w = PyNumber_Long(curnum);
1658 PyObject *tmp_num;
1659 if (w == NULL)
1660 goto Fail;
1661
1662 PyList_SET_ITEM(v, i, w);
1663
1664 tmp_num = PyNumber_Add(curnum, istep);
1665 if (tmp_num == NULL)
1666 goto Fail;
1667
1668 Py_DECREF(curnum);
1669 curnum = tmp_num;
1670 }
Tim Peters874e1f72003-04-13 22:13:08 +00001671 Py_DECREF(ilow);
1672 Py_DECREF(ihigh);
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001673 Py_DECREF(istep);
1674 Py_DECREF(zero);
Tim Peters874e1f72003-04-13 22:13:08 +00001675 Py_DECREF(curnum);
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001676 return v;
1677
1678 Fail:
Tim Peters874e1f72003-04-13 22:13:08 +00001679 Py_DECREF(ilow);
1680 Py_DECREF(ihigh);
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001681 Py_XDECREF(istep);
Tim Peters874e1f72003-04-13 22:13:08 +00001682 Py_DECREF(zero);
1683 Py_XDECREF(curnum);
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001684 Py_XDECREF(v);
1685 return NULL;
1686}
1687
Guido van Rossum124eff01999-02-23 16:11:01 +00001688/* Return number of items in range/xrange (lo, hi, step). step > 0
1689 * required. Return a value < 0 if & only if the true value is too
1690 * large to fit in a signed long.
1691 */
1692static long
Thomas Wouterse28c2962000-07-23 22:21:32 +00001693get_len_of_range(long lo, long hi, long step)
Guido van Rossum124eff01999-02-23 16:11:01 +00001694{
1695 /* -------------------------------------------------------------
1696 If lo >= hi, the range is empty.
1697 Else if n values are in the range, the last one is
1698 lo + (n-1)*step, which must be <= hi-1. Rearranging,
1699 n <= (hi - lo - 1)/step + 1, so taking the floor of the RHS gives
1700 the proper value. Since lo < hi in this case, hi-lo-1 >= 0, so
1701 the RHS is non-negative and so truncation is the same as the
1702 floor. Letting M be the largest positive long, the worst case
1703 for the RHS numerator is hi=M, lo=-M-1, and then
1704 hi-lo-1 = M-(-M-1)-1 = 2*M. Therefore unsigned long has enough
1705 precision to compute the RHS exactly.
1706 ---------------------------------------------------------------*/
1707 long n = 0;
1708 if (lo < hi) {
1709 unsigned long uhi = (unsigned long)hi;
1710 unsigned long ulo = (unsigned long)lo;
1711 unsigned long diff = uhi - ulo - 1;
1712 n = (long)(diff / (unsigned long)step + 1);
1713 }
1714 return n;
1715}
1716
Guido van Rossum79f25d91997-04-29 20:08:16 +00001717static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001718builtin_range(PyObject *self, PyObject *args)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001719{
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001720 long ilow = 0, ihigh = 0, istep = 1;
Guido van Rossum124eff01999-02-23 16:11:01 +00001721 long bign;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001722 int i, n;
Guido van Rossum124eff01999-02-23 16:11:01 +00001723
Guido van Rossum79f25d91997-04-29 20:08:16 +00001724 PyObject *v;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001725
Guido van Rossum79f25d91997-04-29 20:08:16 +00001726 if (PyTuple_Size(args) <= 1) {
1727 if (!PyArg_ParseTuple(args,
Guido van Rossum0865dd91995-01-17 16:30:22 +00001728 "l;range() requires 1-3 int arguments",
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001729 &ihigh)) {
1730 PyErr_Clear();
1731 return handle_range_longs(self, args);
1732 }
Guido van Rossum3f5da241990-12-20 15:06:42 +00001733 }
1734 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001735 if (!PyArg_ParseTuple(args,
Guido van Rossum0865dd91995-01-17 16:30:22 +00001736 "ll|l;range() requires 1-3 int arguments",
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001737 &ilow, &ihigh, &istep)) {
1738 PyErr_Clear();
1739 return handle_range_longs(self, args);
1740 }
Guido van Rossum3f5da241990-12-20 15:06:42 +00001741 }
1742 if (istep == 0) {
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001743 PyErr_SetString(PyExc_ValueError,
Alex Martellif471d472003-04-23 13:00:44 +00001744 "range() step argument must not be zero");
Guido van Rossum3f5da241990-12-20 15:06:42 +00001745 return NULL;
1746 }
Guido van Rossum124eff01999-02-23 16:11:01 +00001747 if (istep > 0)
1748 bign = get_len_of_range(ilow, ihigh, istep);
1749 else
1750 bign = get_len_of_range(ihigh, ilow, -istep);
1751 n = (int)bign;
1752 if (bign < 0 || (long)n != bign) {
Guido van Rossume23cde21999-01-12 05:07:47 +00001753 PyErr_SetString(PyExc_OverflowError,
Fred Drake661ea262000-10-24 19:57:45 +00001754 "range() result has too many items");
Guido van Rossume23cde21999-01-12 05:07:47 +00001755 return NULL;
1756 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00001757 v = PyList_New(n);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001758 if (v == NULL)
1759 return NULL;
1760 for (i = 0; i < n; i++) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001761 PyObject *w = PyInt_FromLong(ilow);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001762 if (w == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001763 Py_DECREF(v);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001764 return NULL;
1765 }
Guido van Rossuma937d141998-04-24 18:22:02 +00001766 PyList_SET_ITEM(v, i, w);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001767 ilow += istep;
1768 }
1769 return v;
1770}
1771
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001772PyDoc_STRVAR(range_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001773"range([start,] stop[, step]) -> list of integers\n\
1774\n\
1775Return a list containing an arithmetic progression of integers.\n\
1776range(i, j) returns [i, i+1, i+2, ..., j-1]; start (!) defaults to 0.\n\
1777When step is given, it specifies the increment (or decrement).\n\
1778For example, range(4) returns [0, 1, 2, 3]. The end point is omitted!\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001779These are exactly the valid indices for a list of 4 elements.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001780
1781
Guido van Rossum79f25d91997-04-29 20:08:16 +00001782static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001783builtin_raw_input(PyObject *self, PyObject *args)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001784{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001785 PyObject *v = NULL;
Martin v. Löwis31d2df52002-08-14 15:46:02 +00001786 PyObject *fin = PySys_GetObject("stdin");
1787 PyObject *fout = PySys_GetObject("stdout");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001788
Raymond Hettingerea3fdf42002-12-29 16:33:45 +00001789 if (!PyArg_UnpackTuple(args, "[raw_]input", 0, 1, &v))
Guido van Rossum3165fe61992-09-25 21:59:05 +00001790 return NULL;
Martin v. Löwis31d2df52002-08-14 15:46:02 +00001791
1792 if (fin == NULL) {
Alex Martellia9b9c9f2003-04-23 13:34:35 +00001793 PyErr_SetString(PyExc_RuntimeError, "[raw_]input: lost sys.stdin");
Martin v. Löwis31d2df52002-08-14 15:46:02 +00001794 return NULL;
1795 }
1796 if (fout == NULL) {
Alex Martellia9b9c9f2003-04-23 13:34:35 +00001797 PyErr_SetString(PyExc_RuntimeError, "[raw_]input: lost sys.stdout");
Martin v. Löwis31d2df52002-08-14 15:46:02 +00001798 return NULL;
1799 }
1800 if (PyFile_SoftSpace(fout, 0)) {
1801 if (PyFile_WriteString(" ", fout) != 0)
1802 return NULL;
1803 }
Georg Brandl7e3ba2a2006-08-06 08:23:54 +00001804 if (PyFile_AsFile(fin) && PyFile_AsFile(fout)
Martin v. Löwis566f6af2002-10-26 14:39:10 +00001805 && isatty(fileno(PyFile_AsFile(fin)))
1806 && isatty(fileno(PyFile_AsFile(fout)))) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001807 PyObject *po;
Guido van Rossum872537c1995-07-07 22:43:42 +00001808 char *prompt;
1809 char *s;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001810 PyObject *result;
Guido van Rossum872537c1995-07-07 22:43:42 +00001811 if (v != NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001812 po = PyObject_Str(v);
Guido van Rossum872537c1995-07-07 22:43:42 +00001813 if (po == NULL)
1814 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001815 prompt = PyString_AsString(po);
Guido van Rossumd9b52081998-06-26 18:25:38 +00001816 if (prompt == NULL)
1817 return NULL;
Guido van Rossum872537c1995-07-07 22:43:42 +00001818 }
1819 else {
1820 po = NULL;
1821 prompt = "";
1822 }
Michael W. Hudson52db5192004-08-02 13:21:09 +00001823 s = PyOS_Readline(PyFile_AsFile(fin), PyFile_AsFile(fout),
Martin v. Löwis566f6af2002-10-26 14:39:10 +00001824 prompt);
Guido van Rossum79f25d91997-04-29 20:08:16 +00001825 Py_XDECREF(po);
Guido van Rossum872537c1995-07-07 22:43:42 +00001826 if (s == NULL) {
Michael W. Hudson30ea2f22004-07-07 17:44:12 +00001827 if (!PyErr_Occurred())
1828 PyErr_SetNone(PyExc_KeyboardInterrupt);
Guido van Rossum872537c1995-07-07 22:43:42 +00001829 return NULL;
1830 }
1831 if (*s == '\0') {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001832 PyErr_SetNone(PyExc_EOFError);
Guido van Rossum872537c1995-07-07 22:43:42 +00001833 result = NULL;
1834 }
1835 else { /* strip trailing '\n' */
Guido van Rossum106f2da2000-06-28 21:12:25 +00001836 size_t len = strlen(s);
Martin v. Löwisb1ed7fa2006-04-13 07:52:27 +00001837 if (len > PY_SSIZE_T_MAX) {
Martin v. Löwis31d2df52002-08-14 15:46:02 +00001838 PyErr_SetString(PyExc_OverflowError,
Alex Martellia9b9c9f2003-04-23 13:34:35 +00001839 "[raw_]input: input too long");
Guido van Rossum106f2da2000-06-28 21:12:25 +00001840 result = NULL;
1841 }
1842 else {
Martin v. Löwisb1ed7fa2006-04-13 07:52:27 +00001843 result = PyString_FromStringAndSize(s, len-1);
Guido van Rossum106f2da2000-06-28 21:12:25 +00001844 }
Guido van Rossum872537c1995-07-07 22:43:42 +00001845 }
Guido van Rossumb18618d2000-05-03 23:44:39 +00001846 PyMem_FREE(s);
Guido van Rossum872537c1995-07-07 22:43:42 +00001847 return result;
1848 }
Guido van Rossum90933611991-06-07 16:10:43 +00001849 if (v != NULL) {
Martin v. Löwis31d2df52002-08-14 15:46:02 +00001850 if (PyFile_WriteObject(v, fout, Py_PRINT_RAW) != 0)
Guido van Rossum90933611991-06-07 16:10:43 +00001851 return NULL;
1852 }
Martin v. Löwis31d2df52002-08-14 15:46:02 +00001853 return PyFile_GetLine(fin, -1);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001854}
1855
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001856PyDoc_STRVAR(raw_input_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001857"raw_input([prompt]) -> string\n\
1858\n\
1859Read a string from standard input. The trailing newline is stripped.\n\
1860If the user hits EOF (Unix: Ctl-D, Windows: Ctl-Z+Return), raise EOFError.\n\
1861On Unix, GNU readline is used if enabled. The prompt string, if given,\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001862is printed without a trailing newline before reading.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001863
1864
Guido van Rossum79f25d91997-04-29 20:08:16 +00001865static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001866builtin_reduce(PyObject *self, PyObject *args)
Guido van Rossum12d12c51993-10-26 17:58:25 +00001867{
Tim Peters15d81ef2001-05-04 04:39:21 +00001868 PyObject *seq, *func, *result = NULL, *it;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001869
Neal Norwitzdf25efe2007-05-23 06:58:36 +00001870 if (Py_Py3kWarningFlag &&
1871 PyErr_Warn(PyExc_DeprecationWarning,
1872 "reduce() not supported in 3.x") < 0)
1873 return NULL;
1874
Raymond Hettingerea3fdf42002-12-29 16:33:45 +00001875 if (!PyArg_UnpackTuple(args, "reduce", 2, 3, &func, &seq, &result))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001876 return NULL;
1877 if (result != NULL)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001878 Py_INCREF(result);
Guido van Rossum12d12c51993-10-26 17:58:25 +00001879
Tim Peters15d81ef2001-05-04 04:39:21 +00001880 it = PyObject_GetIter(seq);
1881 if (it == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001882 PyErr_SetString(PyExc_TypeError,
Tim Peters15d81ef2001-05-04 04:39:21 +00001883 "reduce() arg 2 must support iteration");
1884 Py_XDECREF(result);
Guido van Rossum12d12c51993-10-26 17:58:25 +00001885 return NULL;
1886 }
1887
Guido van Rossum79f25d91997-04-29 20:08:16 +00001888 if ((args = PyTuple_New(2)) == NULL)
Guido van Rossum2d951851994-08-29 12:52:16 +00001889 goto Fail;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001890
Tim Peters15d81ef2001-05-04 04:39:21 +00001891 for (;;) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001892 PyObject *op2;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001893
1894 if (args->ob_refcnt > 1) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001895 Py_DECREF(args);
1896 if ((args = PyTuple_New(2)) == NULL)
Guido van Rossum2d951851994-08-29 12:52:16 +00001897 goto Fail;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001898 }
1899
Tim Peters15d81ef2001-05-04 04:39:21 +00001900 op2 = PyIter_Next(it);
1901 if (op2 == NULL) {
Tim Petersf4848da2001-05-05 00:14:56 +00001902 if (PyErr_Occurred())
1903 goto Fail;
1904 break;
Guido van Rossum2d951851994-08-29 12:52:16 +00001905 }
Guido van Rossum12d12c51993-10-26 17:58:25 +00001906
Guido van Rossum2d951851994-08-29 12:52:16 +00001907 if (result == NULL)
1908 result = op2;
1909 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001910 PyTuple_SetItem(args, 0, result);
1911 PyTuple_SetItem(args, 1, op2);
1912 if ((result = PyEval_CallObject(func, args)) == NULL)
Guido van Rossum2d951851994-08-29 12:52:16 +00001913 goto Fail;
1914 }
Guido van Rossum12d12c51993-10-26 17:58:25 +00001915 }
1916
Guido van Rossum79f25d91997-04-29 20:08:16 +00001917 Py_DECREF(args);
Guido van Rossum12d12c51993-10-26 17:58:25 +00001918
Guido van Rossum2d951851994-08-29 12:52:16 +00001919 if (result == NULL)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001920 PyErr_SetString(PyExc_TypeError,
Fred Drake661ea262000-10-24 19:57:45 +00001921 "reduce() of empty sequence with no initial value");
Guido van Rossum2d951851994-08-29 12:52:16 +00001922
Tim Peters15d81ef2001-05-04 04:39:21 +00001923 Py_DECREF(it);
Guido van Rossum12d12c51993-10-26 17:58:25 +00001924 return result;
1925
Guido van Rossum2d951851994-08-29 12:52:16 +00001926Fail:
Guido van Rossum79f25d91997-04-29 20:08:16 +00001927 Py_XDECREF(args);
1928 Py_XDECREF(result);
Tim Peters15d81ef2001-05-04 04:39:21 +00001929 Py_DECREF(it);
Guido van Rossum12d12c51993-10-26 17:58:25 +00001930 return NULL;
1931}
1932
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001933PyDoc_STRVAR(reduce_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001934"reduce(function, sequence[, initial]) -> value\n\
1935\n\
1936Apply a function of two arguments cumulatively to the items of a sequence,\n\
1937from left to right, so as to reduce the sequence to a single value.\n\
1938For example, reduce(lambda x, y: x+y, [1, 2, 3, 4, 5]) calculates\n\
1939((((1+2)+3)+4)+5). If initial is present, it is placed before the items\n\
1940of the sequence in the calculation, and serves as a default when the\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001941sequence is empty.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001942
1943
Guido van Rossum79f25d91997-04-29 20:08:16 +00001944static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001945builtin_reload(PyObject *self, PyObject *v)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001946{
Neal Norwitzdf25efe2007-05-23 06:58:36 +00001947 if (Py_Py3kWarningFlag &&
1948 PyErr_Warn(PyExc_DeprecationWarning,
1949 "reload() not supported in 3.x") < 0)
1950 return NULL;
1951
Guido van Rossum79f25d91997-04-29 20:08:16 +00001952 return PyImport_ReloadModule(v);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001953}
1954
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001955PyDoc_STRVAR(reload_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001956"reload(module) -> module\n\
1957\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001958Reload the module. The module must have been successfully imported before.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001959
1960
Guido van Rossum79f25d91997-04-29 20:08:16 +00001961static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001962builtin_repr(PyObject *self, PyObject *v)
Guido van Rossumc89705d1992-11-26 08:54:07 +00001963{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001964 return PyObject_Repr(v);
Guido van Rossumc89705d1992-11-26 08:54:07 +00001965}
1966
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001967PyDoc_STRVAR(repr_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001968"repr(object) -> string\n\
1969\n\
1970Return the canonical string representation of the object.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001971For most object types, eval(repr(object)) == object.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001972
1973
Guido van Rossum79f25d91997-04-29 20:08:16 +00001974static PyObject *
Georg Brandlccadf842006-03-31 18:54:53 +00001975builtin_round(PyObject *self, PyObject *args, PyObject *kwds)
Guido van Rossum9e51f9b1993-02-12 16:29:05 +00001976{
Jeffrey Yasskin9871d8f2008-01-05 08:47:13 +00001977 double number;
1978 double f;
1979 int ndigits = 0;
1980 int i;
Georg Brandlccadf842006-03-31 18:54:53 +00001981 static char *kwlist[] = {"number", "ndigits", 0};
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001982
Jeffrey Yasskin9871d8f2008-01-05 08:47:13 +00001983 if (!PyArg_ParseTupleAndKeywords(args, kwds, "d|i:round",
1984 kwlist, &number, &ndigits))
1985 return NULL;
1986 f = 1.0;
1987 i = abs(ndigits);
1988 while (--i >= 0)
1989 f = f*10.0;
1990 if (ndigits < 0)
1991 number /= f;
1992 else
1993 number *= f;
1994 if (number >= 0.0)
1995 number = floor(number + 0.5);
1996 else
1997 number = ceil(number - 0.5);
1998 if (ndigits < 0)
1999 number *= f;
2000 else
2001 number /= f;
2002 return PyFloat_FromDouble(number);
Guido van Rossum9e51f9b1993-02-12 16:29:05 +00002003}
2004
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002005PyDoc_STRVAR(round_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002006"round(number[, ndigits]) -> floating point number\n\
2007\n\
2008Round a number to a given precision in decimal digits (default 0 digits).\n\
Jeffrey Yasskin9871d8f2008-01-05 08:47:13 +00002009This always returns a floating point number. Precision may be negative.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002010
Raymond Hettinger64958a12003-12-17 20:43:33 +00002011static PyObject *
2012builtin_sorted(PyObject *self, PyObject *args, PyObject *kwds)
2013{
2014 PyObject *newlist, *v, *seq, *compare=NULL, *keyfunc=NULL, *newargs;
2015 PyObject *callable;
Martin v. Löwis15e62742006-02-27 16:46:16 +00002016 static char *kwlist[] = {"iterable", "cmp", "key", "reverse", 0};
Neal Norwitz6f0d4792005-12-15 06:40:36 +00002017 int reverse;
Raymond Hettinger64958a12003-12-17 20:43:33 +00002018
Neal Norwitz6f0d4792005-12-15 06:40:36 +00002019 /* args 1-4 should match listsort in Objects/listobject.c */
Armin Rigo71d7e702005-09-20 18:13:03 +00002020 if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|OOi:sorted",
2021 kwlist, &seq, &compare, &keyfunc, &reverse))
2022 return NULL;
Raymond Hettinger64958a12003-12-17 20:43:33 +00002023
2024 newlist = PySequence_List(seq);
2025 if (newlist == NULL)
2026 return NULL;
2027
2028 callable = PyObject_GetAttrString(newlist, "sort");
2029 if (callable == NULL) {
2030 Py_DECREF(newlist);
2031 return NULL;
2032 }
Georg Brandl99d7e4e2005-08-31 22:21:15 +00002033
Raymond Hettinger64958a12003-12-17 20:43:33 +00002034 newargs = PyTuple_GetSlice(args, 1, 4);
2035 if (newargs == NULL) {
2036 Py_DECREF(newlist);
2037 Py_DECREF(callable);
2038 return NULL;
2039 }
2040
2041 v = PyObject_Call(callable, newargs, kwds);
2042 Py_DECREF(newargs);
2043 Py_DECREF(callable);
2044 if (v == NULL) {
2045 Py_DECREF(newlist);
2046 return NULL;
2047 }
2048 Py_DECREF(v);
2049 return newlist;
2050}
2051
2052PyDoc_STRVAR(sorted_doc,
2053"sorted(iterable, cmp=None, key=None, reverse=False) --> new sorted list");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002054
Guido van Rossum79f25d91997-04-29 20:08:16 +00002055static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002056builtin_vars(PyObject *self, PyObject *args)
Guido van Rossum2d951851994-08-29 12:52:16 +00002057{
Guido van Rossum79f25d91997-04-29 20:08:16 +00002058 PyObject *v = NULL;
2059 PyObject *d;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002060
Raymond Hettingerea3fdf42002-12-29 16:33:45 +00002061 if (!PyArg_UnpackTuple(args, "vars", 0, 1, &v))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002062 return NULL;
Guido van Rossum2d951851994-08-29 12:52:16 +00002063 if (v == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002064 d = PyEval_GetLocals();
Guido van Rossum53bb7ff1995-07-26 16:26:31 +00002065 if (d == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002066 if (!PyErr_Occurred())
2067 PyErr_SetString(PyExc_SystemError,
Alex Martellia9b9c9f2003-04-23 13:34:35 +00002068 "vars(): no locals!?");
Guido van Rossum53bb7ff1995-07-26 16:26:31 +00002069 }
2070 else
Guido van Rossum79f25d91997-04-29 20:08:16 +00002071 Py_INCREF(d);
Guido van Rossum2d951851994-08-29 12:52:16 +00002072 }
2073 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002074 d = PyObject_GetAttrString(v, "__dict__");
Guido van Rossum2d951851994-08-29 12:52:16 +00002075 if (d == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002076 PyErr_SetString(PyExc_TypeError,
Guido van Rossum2d951851994-08-29 12:52:16 +00002077 "vars() argument must have __dict__ attribute");
2078 return NULL;
2079 }
2080 }
2081 return d;
2082}
2083
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002084PyDoc_STRVAR(vars_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002085"vars([object]) -> dictionary\n\
2086\n\
2087Without arguments, equivalent to locals().\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002088With an argument, equivalent to object.__dict__.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002089
Alex Martellia70b1912003-04-22 08:12:33 +00002090
2091static PyObject*
2092builtin_sum(PyObject *self, PyObject *args)
2093{
2094 PyObject *seq;
2095 PyObject *result = NULL;
2096 PyObject *temp, *item, *iter;
2097
Raymond Hettinger5cf63942003-10-25 06:41:37 +00002098 if (!PyArg_UnpackTuple(args, "sum", 1, 2, &seq, &result))
Alex Martellia70b1912003-04-22 08:12:33 +00002099 return NULL;
2100
2101 iter = PyObject_GetIter(seq);
2102 if (iter == NULL)
2103 return NULL;
2104
2105 if (result == NULL) {
2106 result = PyInt_FromLong(0);
2107 if (result == NULL) {
2108 Py_DECREF(iter);
2109 return NULL;
2110 }
2111 } else {
2112 /* reject string values for 'start' parameter */
2113 if (PyObject_TypeCheck(result, &PyBaseString_Type)) {
2114 PyErr_SetString(PyExc_TypeError,
Alex Martellia9b9c9f2003-04-23 13:34:35 +00002115 "sum() can't sum strings [use ''.join(seq) instead]");
Alex Martellia70b1912003-04-22 08:12:33 +00002116 Py_DECREF(iter);
2117 return NULL;
2118 }
Alex Martelli41c9f882003-04-22 09:24:48 +00002119 Py_INCREF(result);
Alex Martellia70b1912003-04-22 08:12:33 +00002120 }
2121
Raymond Hettinger3f8caa32007-10-24 01:28:33 +00002122#ifndef SLOW_SUM
2123 /* Fast addition by keeping temporary sums in C instead of new Python objects.
2124 Assumes all inputs are the same type. If the assumption fails, default
2125 to the more general routine.
2126 */
2127 if (PyInt_CheckExact(result)) {
2128 long i_result = PyInt_AS_LONG(result);
2129 Py_DECREF(result);
2130 result = NULL;
2131 while(result == NULL) {
2132 item = PyIter_Next(iter);
2133 if (item == NULL) {
2134 Py_DECREF(iter);
2135 if (PyErr_Occurred())
2136 return NULL;
2137 return PyInt_FromLong(i_result);
2138 }
2139 if (PyInt_CheckExact(item)) {
2140 long b = PyInt_AS_LONG(item);
2141 long x = i_result + b;
2142 if ((x^i_result) >= 0 || (x^b) >= 0) {
2143 i_result = x;
2144 Py_DECREF(item);
2145 continue;
2146 }
2147 }
2148 /* Either overflowed or is not an int. Restore real objects and process normally */
2149 result = PyInt_FromLong(i_result);
2150 temp = PyNumber_Add(result, item);
2151 Py_DECREF(result);
2152 Py_DECREF(item);
2153 result = temp;
2154 if (result == NULL) {
2155 Py_DECREF(iter);
2156 return NULL;
2157 }
2158 }
2159 }
2160
2161 if (PyFloat_CheckExact(result)) {
2162 double f_result = PyFloat_AS_DOUBLE(result);
2163 Py_DECREF(result);
2164 result = NULL;
2165 while(result == NULL) {
2166 item = PyIter_Next(iter);
2167 if (item == NULL) {
2168 Py_DECREF(iter);
2169 if (PyErr_Occurred())
2170 return NULL;
2171 return PyFloat_FromDouble(f_result);
2172 }
2173 if (PyFloat_CheckExact(item)) {
2174 PyFPE_START_PROTECT("add", return 0)
2175 f_result += PyFloat_AS_DOUBLE(item);
Raymond Hettinger3a8daf52007-10-24 02:05:51 +00002176 PyFPE_END_PROTECT(f_result)
Raymond Hettingera45c4872007-10-25 02:26:58 +00002177 Py_DECREF(item);
Raymond Hettinger3a8daf52007-10-24 02:05:51 +00002178 continue;
2179 }
2180 if (PyInt_CheckExact(item)) {
2181 PyFPE_START_PROTECT("add", return 0)
2182 f_result += (double)PyInt_AS_LONG(item);
2183 PyFPE_END_PROTECT(f_result)
Raymond Hettingera45c4872007-10-25 02:26:58 +00002184 Py_DECREF(item);
Raymond Hettinger3f8caa32007-10-24 01:28:33 +00002185 continue;
2186 }
2187 result = PyFloat_FromDouble(f_result);
2188 temp = PyNumber_Add(result, item);
2189 Py_DECREF(result);
2190 Py_DECREF(item);
2191 result = temp;
2192 if (result == NULL) {
2193 Py_DECREF(iter);
2194 return NULL;
2195 }
2196 }
2197 }
2198#endif
2199
Alex Martellia70b1912003-04-22 08:12:33 +00002200 for(;;) {
2201 item = PyIter_Next(iter);
2202 if (item == NULL) {
2203 /* error, or end-of-sequence */
2204 if (PyErr_Occurred()) {
2205 Py_DECREF(result);
2206 result = NULL;
2207 }
2208 break;
2209 }
Alex Martellia253e182003-10-25 23:24:14 +00002210 temp = PyNumber_Add(result, item);
Alex Martellia70b1912003-04-22 08:12:33 +00002211 Py_DECREF(result);
2212 Py_DECREF(item);
2213 result = temp;
2214 if (result == NULL)
2215 break;
2216 }
2217 Py_DECREF(iter);
2218 return result;
2219}
2220
2221PyDoc_STRVAR(sum_doc,
Georg Brandl8134d062006-10-12 12:33:07 +00002222"sum(sequence[, start]) -> value\n\
Alex Martellia70b1912003-04-22 08:12:33 +00002223\n\
2224Returns the sum of a sequence of numbers (NOT strings) plus the value\n\
Georg Brandl8134d062006-10-12 12:33:07 +00002225of parameter 'start' (which defaults to 0). When the sequence is\n\
2226empty, returns start.");
Alex Martellia70b1912003-04-22 08:12:33 +00002227
2228
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002229static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002230builtin_isinstance(PyObject *self, PyObject *args)
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002231{
2232 PyObject *inst;
2233 PyObject *cls;
Guido van Rossum823649d2001-03-21 18:40:58 +00002234 int retval;
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002235
Raymond Hettingerea3fdf42002-12-29 16:33:45 +00002236 if (!PyArg_UnpackTuple(args, "isinstance", 2, 2, &inst, &cls))
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002237 return NULL;
Guido van Rossumf5dd9141997-12-02 19:11:45 +00002238
Guido van Rossum823649d2001-03-21 18:40:58 +00002239 retval = PyObject_IsInstance(inst, cls);
2240 if (retval < 0)
Guido van Rossumad991772001-01-12 16:03:05 +00002241 return NULL;
Guido van Rossum77f6a652002-04-03 22:41:51 +00002242 return PyBool_FromLong(retval);
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002243}
2244
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002245PyDoc_STRVAR(isinstance_doc,
Guido van Rossum77f6a652002-04-03 22:41:51 +00002246"isinstance(object, class-or-type-or-tuple) -> bool\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002247\n\
2248Return whether an object is an instance of a class or of a subclass thereof.\n\
Guido van Rossum03290ec2001-10-07 20:54:12 +00002249With a type as second argument, return whether that is the object's type.\n\
2250The form using a tuple, isinstance(x, (A, B, ...)), is a shortcut for\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002251isinstance(x, A) or isinstance(x, B) or ... (etc.).");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002252
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002253
2254static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002255builtin_issubclass(PyObject *self, PyObject *args)
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002256{
2257 PyObject *derived;
2258 PyObject *cls;
2259 int retval;
2260
Raymond Hettingerea3fdf42002-12-29 16:33:45 +00002261 if (!PyArg_UnpackTuple(args, "issubclass", 2, 2, &derived, &cls))
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002262 return NULL;
Guido van Rossum668213d1999-06-16 17:28:37 +00002263
Guido van Rossum823649d2001-03-21 18:40:58 +00002264 retval = PyObject_IsSubclass(derived, cls);
2265 if (retval < 0)
2266 return NULL;
Guido van Rossum77f6a652002-04-03 22:41:51 +00002267 return PyBool_FromLong(retval);
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002268}
2269
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002270PyDoc_STRVAR(issubclass_doc,
Guido van Rossum77f6a652002-04-03 22:41:51 +00002271"issubclass(C, B) -> bool\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002272\n\
Walter Dörwaldd9a6ad32002-12-12 16:41:44 +00002273Return whether class C is a subclass (i.e., a derived class) of class B.\n\
2274When using a tuple as the second argument issubclass(X, (A, B, ...)),\n\
2275is a shortcut for issubclass(X, A) or issubclass(X, B) or ... (etc.).");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002276
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002277
Barry Warsawbd599b52000-08-03 15:45:29 +00002278static PyObject*
2279builtin_zip(PyObject *self, PyObject *args)
2280{
2281 PyObject *ret;
Martin v. Löwisd96ee902006-02-16 14:37:16 +00002282 const Py_ssize_t itemsize = PySequence_Length(args);
2283 Py_ssize_t i;
Tim Peters8572b4f2001-05-06 01:05:02 +00002284 PyObject *itlist; /* tuple of iterators */
Martin v. Löwisd96ee902006-02-16 14:37:16 +00002285 Py_ssize_t len; /* guess at result length */
Barry Warsawbd599b52000-08-03 15:45:29 +00002286
Raymond Hettingereaef6152003-08-02 07:42:57 +00002287 if (itemsize == 0)
2288 return PyList_New(0);
2289
Barry Warsawbd599b52000-08-03 15:45:29 +00002290 /* args must be a tuple */
2291 assert(PyTuple_Check(args));
2292
Tim Peters39a86c22002-05-12 07:19:38 +00002293 /* Guess at result length: the shortest of the input lengths.
2294 If some argument refuses to say, we refuse to guess too, lest
2295 an argument like xrange(sys.maxint) lead us astray.*/
Tim Peters67d687a2002-04-29 21:27:32 +00002296 len = -1; /* unknown */
2297 for (i = 0; i < itemsize; ++i) {
2298 PyObject *item = PyTuple_GET_ITEM(args, i);
Raymond Hettinger4e2f7142007-12-06 00:56:53 +00002299 Py_ssize_t thislen = _PyObject_LengthHint(item, -1);
Tim Peters39a86c22002-05-12 07:19:38 +00002300 if (thislen < 0) {
Tim Peters39a86c22002-05-12 07:19:38 +00002301 len = -1;
2302 break;
2303 }
Tim Peters67d687a2002-04-29 21:27:32 +00002304 else if (len < 0 || thislen < len)
2305 len = thislen;
2306 }
2307
Tim Peters8572b4f2001-05-06 01:05:02 +00002308 /* allocate result list */
Tim Peters67d687a2002-04-29 21:27:32 +00002309 if (len < 0)
2310 len = 10; /* arbitrary */
2311 if ((ret = PyList_New(len)) == NULL)
Barry Warsawbd599b52000-08-03 15:45:29 +00002312 return NULL;
2313
Tim Peters8572b4f2001-05-06 01:05:02 +00002314 /* obtain iterators */
2315 itlist = PyTuple_New(itemsize);
2316 if (itlist == NULL)
2317 goto Fail_ret;
2318 for (i = 0; i < itemsize; ++i) {
2319 PyObject *item = PyTuple_GET_ITEM(args, i);
2320 PyObject *it = PyObject_GetIter(item);
2321 if (it == NULL) {
2322 if (PyErr_ExceptionMatches(PyExc_TypeError))
2323 PyErr_Format(PyExc_TypeError,
Neal Norwitza361bd82006-02-19 19:31:50 +00002324 "zip argument #%zd must support iteration",
Tim Peters8572b4f2001-05-06 01:05:02 +00002325 i+1);
2326 goto Fail_ret_itlist;
Barry Warsawbd599b52000-08-03 15:45:29 +00002327 }
Tim Peters8572b4f2001-05-06 01:05:02 +00002328 PyTuple_SET_ITEM(itlist, i, it);
2329 }
Barry Warsawbd599b52000-08-03 15:45:29 +00002330
Tim Peters8572b4f2001-05-06 01:05:02 +00002331 /* build result into ret list */
Tim Peters67d687a2002-04-29 21:27:32 +00002332 for (i = 0; ; ++i) {
2333 int j;
Tim Peters8572b4f2001-05-06 01:05:02 +00002334 PyObject *next = PyTuple_New(itemsize);
2335 if (!next)
2336 goto Fail_ret_itlist;
2337
Tim Peters67d687a2002-04-29 21:27:32 +00002338 for (j = 0; j < itemsize; j++) {
2339 PyObject *it = PyTuple_GET_ITEM(itlist, j);
Tim Peters8572b4f2001-05-06 01:05:02 +00002340 PyObject *item = PyIter_Next(it);
Barry Warsawbd599b52000-08-03 15:45:29 +00002341 if (!item) {
Tim Peters8572b4f2001-05-06 01:05:02 +00002342 if (PyErr_Occurred()) {
2343 Py_DECREF(ret);
2344 ret = NULL;
Barry Warsawbd599b52000-08-03 15:45:29 +00002345 }
2346 Py_DECREF(next);
Tim Peters8572b4f2001-05-06 01:05:02 +00002347 Py_DECREF(itlist);
Tim Peters67d687a2002-04-29 21:27:32 +00002348 goto Done;
Barry Warsawbd599b52000-08-03 15:45:29 +00002349 }
Tim Peters67d687a2002-04-29 21:27:32 +00002350 PyTuple_SET_ITEM(next, j, item);
Barry Warsawbd599b52000-08-03 15:45:29 +00002351 }
Tim Peters8572b4f2001-05-06 01:05:02 +00002352
Tim Peters67d687a2002-04-29 21:27:32 +00002353 if (i < len)
2354 PyList_SET_ITEM(ret, i, next);
2355 else {
2356 int status = PyList_Append(ret, next);
2357 Py_DECREF(next);
2358 ++len;
2359 if (status < 0)
2360 goto Fail_ret_itlist;
2361 }
Barry Warsawbd599b52000-08-03 15:45:29 +00002362 }
Tim Peters8572b4f2001-05-06 01:05:02 +00002363
Tim Peters67d687a2002-04-29 21:27:32 +00002364Done:
2365 if (ret != NULL && i < len) {
2366 /* The list is too big. */
2367 if (PyList_SetSlice(ret, i, len, NULL) < 0)
2368 return NULL;
2369 }
2370 return ret;
2371
Tim Peters8572b4f2001-05-06 01:05:02 +00002372Fail_ret_itlist:
2373 Py_DECREF(itlist);
2374Fail_ret:
2375 Py_DECREF(ret);
2376 return NULL;
Barry Warsawbd599b52000-08-03 15:45:29 +00002377}
2378
2379
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002380PyDoc_STRVAR(zip_doc,
Barry Warsawbd599b52000-08-03 15:45:29 +00002381"zip(seq1 [, seq2 [...]]) -> [(seq1[0], seq2[0] ...), (...)]\n\
2382\n\
2383Return a list of tuples, where each tuple contains the i-th element\n\
2384from each of the argument sequences. The returned list is truncated\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002385in length to the length of the shortest argument sequence.");
Barry Warsawbd599b52000-08-03 15:45:29 +00002386
2387
Guido van Rossum79f25d91997-04-29 20:08:16 +00002388static PyMethodDef builtin_methods[] = {
Neal Norwitz92e212f2006-04-03 04:48:37 +00002389 {"__import__", (PyCFunction)builtin___import__, METH_VARARGS | METH_KEYWORDS, import_doc},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002390 {"abs", builtin_abs, METH_O, abs_doc},
Raymond Hettinger96229b12005-03-11 06:49:40 +00002391 {"all", builtin_all, METH_O, all_doc},
2392 {"any", builtin_any, METH_O, any_doc},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002393 {"apply", builtin_apply, METH_VARARGS, apply_doc},
Eric Smith3cd81942008-02-22 16:30:22 +00002394 {"bin", builtin_bin, METH_O, bin_doc},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002395 {"callable", builtin_callable, METH_O, callable_doc},
2396 {"chr", builtin_chr, METH_VARARGS, chr_doc},
2397 {"cmp", builtin_cmp, METH_VARARGS, cmp_doc},
2398 {"coerce", builtin_coerce, METH_VARARGS, coerce_doc},
Georg Brandl5240d742007-03-13 20:46:32 +00002399 {"compile", (PyCFunction)builtin_compile, METH_VARARGS | METH_KEYWORDS, compile_doc},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002400 {"delattr", builtin_delattr, METH_VARARGS, delattr_doc},
2401 {"dir", builtin_dir, METH_VARARGS, dir_doc},
2402 {"divmod", builtin_divmod, METH_VARARGS, divmod_doc},
2403 {"eval", builtin_eval, METH_VARARGS, eval_doc},
2404 {"execfile", builtin_execfile, METH_VARARGS, execfile_doc},
2405 {"filter", builtin_filter, METH_VARARGS, filter_doc},
Eric Smitha9f7d622008-02-17 19:46:49 +00002406 {"format", builtin_format, METH_VARARGS, format_doc},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002407 {"getattr", builtin_getattr, METH_VARARGS, getattr_doc},
2408 {"globals", (PyCFunction)builtin_globals, METH_NOARGS, globals_doc},
2409 {"hasattr", builtin_hasattr, METH_VARARGS, hasattr_doc},
2410 {"hash", builtin_hash, METH_O, hash_doc},
2411 {"hex", builtin_hex, METH_O, hex_doc},
2412 {"id", builtin_id, METH_O, id_doc},
2413 {"input", builtin_input, METH_VARARGS, input_doc},
2414 {"intern", builtin_intern, METH_VARARGS, intern_doc},
2415 {"isinstance", builtin_isinstance, METH_VARARGS, isinstance_doc},
2416 {"issubclass", builtin_issubclass, METH_VARARGS, issubclass_doc},
2417 {"iter", builtin_iter, METH_VARARGS, iter_doc},
2418 {"len", builtin_len, METH_O, len_doc},
2419 {"locals", (PyCFunction)builtin_locals, METH_NOARGS, locals_doc},
2420 {"map", builtin_map, METH_VARARGS, map_doc},
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00002421 {"max", (PyCFunction)builtin_max, METH_VARARGS | METH_KEYWORDS, max_doc},
2422 {"min", (PyCFunction)builtin_min, METH_VARARGS | METH_KEYWORDS, min_doc},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002423 {"oct", builtin_oct, METH_O, oct_doc},
Neal Norwitzc4edb0e2006-05-02 04:43:14 +00002424 {"open", (PyCFunction)builtin_open, METH_VARARGS | METH_KEYWORDS, open_doc},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002425 {"ord", builtin_ord, METH_O, ord_doc},
2426 {"pow", builtin_pow, METH_VARARGS, pow_doc},
2427 {"range", builtin_range, METH_VARARGS, range_doc},
2428 {"raw_input", builtin_raw_input, METH_VARARGS, raw_input_doc},
2429 {"reduce", builtin_reduce, METH_VARARGS, reduce_doc},
2430 {"reload", builtin_reload, METH_O, reload_doc},
2431 {"repr", builtin_repr, METH_O, repr_doc},
Georg Brandlccadf842006-03-31 18:54:53 +00002432 {"round", (PyCFunction)builtin_round, METH_VARARGS | METH_KEYWORDS, round_doc},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002433 {"setattr", builtin_setattr, METH_VARARGS, setattr_doc},
Raymond Hettinger64958a12003-12-17 20:43:33 +00002434 {"sorted", (PyCFunction)builtin_sorted, METH_VARARGS | METH_KEYWORDS, sorted_doc},
Alex Martellia70b1912003-04-22 08:12:33 +00002435 {"sum", builtin_sum, METH_VARARGS, sum_doc},
Martin v. Löwis339d0f72001-08-17 18:39:25 +00002436#ifdef Py_USING_UNICODE
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002437 {"unichr", builtin_unichr, METH_VARARGS, unichr_doc},
Martin v. Löwis339d0f72001-08-17 18:39:25 +00002438#endif
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002439 {"vars", builtin_vars, METH_VARARGS, vars_doc},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002440 {"zip", builtin_zip, METH_VARARGS, zip_doc},
Guido van Rossumc02e15c1991-12-16 13:03:00 +00002441 {NULL, NULL},
Guido van Rossum3f5da241990-12-20 15:06:42 +00002442};
2443
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002444PyDoc_STRVAR(builtin_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002445"Built-in functions, exceptions, and other objects.\n\
2446\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002447Noteworthy: None is the `nil' object; Ellipsis represents `...' in slices.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002448
Guido van Rossum25ce5661997-08-02 03:10:38 +00002449PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002450_PyBuiltin_Init(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +00002451{
Fred Drake5550de32000-06-20 04:54:19 +00002452 PyObject *mod, *dict, *debug;
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002453 mod = Py_InitModule4("__builtin__", builtin_methods,
2454 builtin_doc, (PyObject *)NULL,
2455 PYTHON_API_VERSION);
Guido van Rossum25ce5661997-08-02 03:10:38 +00002456 if (mod == NULL)
2457 return NULL;
2458 dict = PyModule_GetDict(mod);
Tim Peters4b7625e2001-09-13 21:37:17 +00002459
Tim Peters7571a0f2003-03-23 17:52:28 +00002460#ifdef Py_TRACE_REFS
2461 /* __builtin__ exposes a number of statically allocated objects
2462 * that, before this code was added in 2.3, never showed up in
2463 * the list of "all objects" maintained by Py_TRACE_REFS. As a
2464 * result, programs leaking references to None and False (etc)
2465 * couldn't be diagnosed by examining sys.getobjects(0).
2466 */
2467#define ADD_TO_ALL(OBJECT) _Py_AddToAllObjects((PyObject *)(OBJECT), 0)
2468#else
2469#define ADD_TO_ALL(OBJECT) (void)0
2470#endif
2471
Tim Peters4b7625e2001-09-13 21:37:17 +00002472#define SETBUILTIN(NAME, OBJECT) \
Tim Peters7571a0f2003-03-23 17:52:28 +00002473 if (PyDict_SetItemString(dict, NAME, (PyObject *)OBJECT) < 0) \
2474 return NULL; \
2475 ADD_TO_ALL(OBJECT)
Tim Peters4b7625e2001-09-13 21:37:17 +00002476
2477 SETBUILTIN("None", Py_None);
2478 SETBUILTIN("Ellipsis", Py_Ellipsis);
2479 SETBUILTIN("NotImplemented", Py_NotImplemented);
Guido van Rossum77f6a652002-04-03 22:41:51 +00002480 SETBUILTIN("False", Py_False);
2481 SETBUILTIN("True", Py_True);
Neal Norwitz32a7e7f2002-05-31 19:58:02 +00002482 SETBUILTIN("basestring", &PyBaseString_Type);
Guido van Rossum77f6a652002-04-03 22:41:51 +00002483 SETBUILTIN("bool", &PyBool_Type);
Travis E. Oliphant3781aef2008-03-18 04:44:57 +00002484 /* SETBUILTIN("memoryview", &PyMemoryView_Type); */
Christian Heimes288e89a2008-01-18 18:24:07 +00002485 SETBUILTIN("bytes", &PyString_Type);
Guido van Rossumbea18cc2002-06-14 20:41:17 +00002486 SETBUILTIN("buffer", &PyBuffer_Type);
Tim Peters4b7625e2001-09-13 21:37:17 +00002487 SETBUILTIN("classmethod", &PyClassMethod_Type);
Tim Peters6d6c1a32001-08-02 04:15:00 +00002488#ifndef WITHOUT_COMPLEX
Tim Peters4b7625e2001-09-13 21:37:17 +00002489 SETBUILTIN("complex", &PyComplex_Type);
Tim Peters6d6c1a32001-08-02 04:15:00 +00002490#endif
Tim Petersa427a2b2001-10-29 22:25:45 +00002491 SETBUILTIN("dict", &PyDict_Type);
Tim Peters67d687a2002-04-29 21:27:32 +00002492 SETBUILTIN("enumerate", &PyEnum_Type);
Neal Norwitzc4edb0e2006-05-02 04:43:14 +00002493 SETBUILTIN("file", &PyFile_Type);
Tim Peters4b7625e2001-09-13 21:37:17 +00002494 SETBUILTIN("float", &PyFloat_Type);
Raymond Hettingera690a992003-11-16 16:17:49 +00002495 SETBUILTIN("frozenset", &PyFrozenSet_Type);
Tim Peters4b7625e2001-09-13 21:37:17 +00002496 SETBUILTIN("property", &PyProperty_Type);
2497 SETBUILTIN("int", &PyInt_Type);
2498 SETBUILTIN("list", &PyList_Type);
2499 SETBUILTIN("long", &PyLong_Type);
2500 SETBUILTIN("object", &PyBaseObject_Type);
Raymond Hettinger85c20a42003-11-06 14:06:48 +00002501 SETBUILTIN("reversed", &PyReversed_Type);
Raymond Hettingera690a992003-11-16 16:17:49 +00002502 SETBUILTIN("set", &PySet_Type);
Guido van Rossumbea18cc2002-06-14 20:41:17 +00002503 SETBUILTIN("slice", &PySlice_Type);
Tim Peters4b7625e2001-09-13 21:37:17 +00002504 SETBUILTIN("staticmethod", &PyStaticMethod_Type);
2505 SETBUILTIN("str", &PyString_Type);
2506 SETBUILTIN("super", &PySuper_Type);
2507 SETBUILTIN("tuple", &PyTuple_Type);
2508 SETBUILTIN("type", &PyType_Type);
Raymond Hettingerc4c453f2002-06-05 23:12:45 +00002509 SETBUILTIN("xrange", &PyRange_Type);
Martin v. Löwis339d0f72001-08-17 18:39:25 +00002510#ifdef Py_USING_UNICODE
Tim Peters4b7625e2001-09-13 21:37:17 +00002511 SETBUILTIN("unicode", &PyUnicode_Type);
Martin v. Löwis339d0f72001-08-17 18:39:25 +00002512#endif
Guido van Rossum77f6a652002-04-03 22:41:51 +00002513 debug = PyBool_FromLong(Py_OptimizeFlag == 0);
Fred Drake5550de32000-06-20 04:54:19 +00002514 if (PyDict_SetItemString(dict, "__debug__", debug) < 0) {
2515 Py_XDECREF(debug);
Guido van Rossum25ce5661997-08-02 03:10:38 +00002516 return NULL;
Fred Drake5550de32000-06-20 04:54:19 +00002517 }
2518 Py_XDECREF(debug);
Barry Warsaw757af0e1997-08-29 22:13:51 +00002519
Guido van Rossum25ce5661997-08-02 03:10:38 +00002520 return mod;
Tim Peters7571a0f2003-03-23 17:52:28 +00002521#undef ADD_TO_ALL
Tim Peters4b7625e2001-09-13 21:37:17 +00002522#undef SETBUILTIN
Guido van Rossum3f5da241990-12-20 15:06:42 +00002523}
2524
Guido van Rossume77a7571993-11-03 15:01:26 +00002525/* Helper for filter(): filter a tuple through a function */
Guido van Rossum12d12c51993-10-26 17:58:25 +00002526
Guido van Rossum79f25d91997-04-29 20:08:16 +00002527static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002528filtertuple(PyObject *func, PyObject *tuple)
Guido van Rossum12d12c51993-10-26 17:58:25 +00002529{
Guido van Rossum79f25d91997-04-29 20:08:16 +00002530 PyObject *result;
Martin v. Löwis18e16552006-02-15 17:27:45 +00002531 Py_ssize_t i, j;
2532 Py_ssize_t len = PyTuple_Size(tuple);
Guido van Rossum12d12c51993-10-26 17:58:25 +00002533
Guido van Rossumb7b45621995-08-04 04:07:45 +00002534 if (len == 0) {
Walter Dörwaldc3da83f2003-02-04 20:24:45 +00002535 if (PyTuple_CheckExact(tuple))
2536 Py_INCREF(tuple);
2537 else
2538 tuple = PyTuple_New(0);
Guido van Rossumb7b45621995-08-04 04:07:45 +00002539 return tuple;
2540 }
2541
Guido van Rossum79f25d91997-04-29 20:08:16 +00002542 if ((result = PyTuple_New(len)) == NULL)
Guido van Rossum2586bf01993-11-01 16:21:44 +00002543 return NULL;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002544
Guido van Rossum12d12c51993-10-26 17:58:25 +00002545 for (i = j = 0; i < len; ++i) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002546 PyObject *item, *good;
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002547 int ok;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002548
Walter Dörwald8dd19322003-02-10 17:36:40 +00002549 if (tuple->ob_type->tp_as_sequence &&
2550 tuple->ob_type->tp_as_sequence->sq_item) {
2551 item = tuple->ob_type->tp_as_sequence->sq_item(tuple, i);
Walter Dörwaldc58a3a12003-08-18 18:28:45 +00002552 if (item == NULL)
2553 goto Fail_1;
Walter Dörwald8dd19322003-02-10 17:36:40 +00002554 } else {
Alex Martellia9b9c9f2003-04-23 13:34:35 +00002555 PyErr_SetString(PyExc_TypeError, "filter(): unsubscriptable tuple");
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002556 goto Fail_1;
Walter Dörwald8dd19322003-02-10 17:36:40 +00002557 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002558 if (func == Py_None) {
2559 Py_INCREF(item);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002560 good = item;
2561 }
2562 else {
Raymond Hettinger8ae46892003-10-12 19:09:37 +00002563 PyObject *arg = PyTuple_Pack(1, item);
Walter Dörwaldc58a3a12003-08-18 18:28:45 +00002564 if (arg == NULL) {
2565 Py_DECREF(item);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002566 goto Fail_1;
Walter Dörwaldc58a3a12003-08-18 18:28:45 +00002567 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002568 good = PyEval_CallObject(func, arg);
2569 Py_DECREF(arg);
Walter Dörwaldc58a3a12003-08-18 18:28:45 +00002570 if (good == NULL) {
2571 Py_DECREF(item);
Guido van Rossum12d12c51993-10-26 17:58:25 +00002572 goto Fail_1;
Walter Dörwaldc58a3a12003-08-18 18:28:45 +00002573 }
Guido van Rossum12d12c51993-10-26 17:58:25 +00002574 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002575 ok = PyObject_IsTrue(good);
2576 Py_DECREF(good);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002577 if (ok) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002578 if (PyTuple_SetItem(result, j++, item) < 0)
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002579 goto Fail_1;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002580 }
Walter Dörwaldc58a3a12003-08-18 18:28:45 +00002581 else
2582 Py_DECREF(item);
Guido van Rossum12d12c51993-10-26 17:58:25 +00002583 }
2584
Tim Peters4324aa32001-05-28 22:30:08 +00002585 if (_PyTuple_Resize(&result, j) < 0)
Guido van Rossum12d12c51993-10-26 17:58:25 +00002586 return NULL;
2587
Guido van Rossum12d12c51993-10-26 17:58:25 +00002588 return result;
2589
Guido van Rossum12d12c51993-10-26 17:58:25 +00002590Fail_1:
Guido van Rossum79f25d91997-04-29 20:08:16 +00002591 Py_DECREF(result);
Guido van Rossum12d12c51993-10-26 17:58:25 +00002592 return NULL;
2593}
2594
2595
Guido van Rossume77a7571993-11-03 15:01:26 +00002596/* Helper for filter(): filter a string through a function */
Guido van Rossum12d12c51993-10-26 17:58:25 +00002597
Guido van Rossum79f25d91997-04-29 20:08:16 +00002598static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002599filterstring(PyObject *func, PyObject *strobj)
Guido van Rossum12d12c51993-10-26 17:58:25 +00002600{
Guido van Rossum79f25d91997-04-29 20:08:16 +00002601 PyObject *result;
Martin v. Löwis18e16552006-02-15 17:27:45 +00002602 Py_ssize_t i, j;
2603 Py_ssize_t len = PyString_Size(strobj);
2604 Py_ssize_t outlen = len;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002605
Guido van Rossum79f25d91997-04-29 20:08:16 +00002606 if (func == Py_None) {
Walter Dörwald1918f772003-02-10 13:19:13 +00002607 /* If it's a real string we can return the original,
2608 * as no character is ever false and __getitem__
2609 * does return this character. If it's a subclass
2610 * we must go through the __getitem__ loop */
2611 if (PyString_CheckExact(strobj)) {
Walter Dörwaldc3da83f2003-02-04 20:24:45 +00002612 Py_INCREF(strobj);
Walter Dörwald1918f772003-02-10 13:19:13 +00002613 return strobj;
2614 }
Guido van Rossum12d12c51993-10-26 17:58:25 +00002615 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002616 if ((result = PyString_FromStringAndSize(NULL, len)) == NULL)
Guido van Rossum2586bf01993-11-01 16:21:44 +00002617 return NULL;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002618
Guido van Rossum12d12c51993-10-26 17:58:25 +00002619 for (i = j = 0; i < len; ++i) {
Walter Dörwald1918f772003-02-10 13:19:13 +00002620 PyObject *item;
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002621 int ok;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002622
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002623 item = (*strobj->ob_type->tp_as_sequence->sq_item)(strobj, i);
2624 if (item == NULL)
2625 goto Fail_1;
Walter Dörwald1918f772003-02-10 13:19:13 +00002626 if (func==Py_None) {
2627 ok = 1;
2628 } else {
2629 PyObject *arg, *good;
Raymond Hettinger8ae46892003-10-12 19:09:37 +00002630 arg = PyTuple_Pack(1, item);
Walter Dörwald1918f772003-02-10 13:19:13 +00002631 if (arg == NULL) {
2632 Py_DECREF(item);
2633 goto Fail_1;
2634 }
2635 good = PyEval_CallObject(func, arg);
2636 Py_DECREF(arg);
2637 if (good == NULL) {
2638 Py_DECREF(item);
2639 goto Fail_1;
2640 }
2641 ok = PyObject_IsTrue(good);
2642 Py_DECREF(good);
Tim Peters388ed082001-04-07 20:34:48 +00002643 }
Walter Dörwald903f1e02003-02-04 16:28:00 +00002644 if (ok) {
Martin v. Löwisd96ee902006-02-16 14:37:16 +00002645 Py_ssize_t reslen;
Walter Dörwald903f1e02003-02-04 16:28:00 +00002646 if (!PyString_Check(item)) {
2647 PyErr_SetString(PyExc_TypeError, "can't filter str to str:"
2648 " __getitem__ returned different type");
2649 Py_DECREF(item);
2650 goto Fail_1;
2651 }
2652 reslen = PyString_GET_SIZE(item);
2653 if (reslen == 1) {
2654 PyString_AS_STRING(result)[j++] =
2655 PyString_AS_STRING(item)[0];
2656 } else {
2657 /* do we need more space? */
Martin v. Löwisd96ee902006-02-16 14:37:16 +00002658 Py_ssize_t need = j + reslen + len-i-1;
Walter Dörwald903f1e02003-02-04 16:28:00 +00002659 if (need > outlen) {
2660 /* overallocate, to avoid reallocations */
2661 if (need<2*outlen)
2662 need = 2*outlen;
2663 if (_PyString_Resize(&result, need)) {
2664 Py_DECREF(item);
2665 return NULL;
2666 }
2667 outlen = need;
2668 }
2669 memcpy(
2670 PyString_AS_STRING(result) + j,
2671 PyString_AS_STRING(item),
2672 reslen
2673 );
2674 j += reslen;
2675 }
2676 }
Tim Peters388ed082001-04-07 20:34:48 +00002677 Py_DECREF(item);
Guido van Rossum12d12c51993-10-26 17:58:25 +00002678 }
2679
Walter Dörwald903f1e02003-02-04 16:28:00 +00002680 if (j < outlen)
Tim Peters5de98422002-04-27 18:44:32 +00002681 _PyString_Resize(&result, j);
Guido van Rossum12d12c51993-10-26 17:58:25 +00002682
Guido van Rossum12d12c51993-10-26 17:58:25 +00002683 return result;
2684
Guido van Rossum12d12c51993-10-26 17:58:25 +00002685Fail_1:
Guido van Rossum79f25d91997-04-29 20:08:16 +00002686 Py_DECREF(result);
Guido van Rossum12d12c51993-10-26 17:58:25 +00002687 return NULL;
2688}
Martin v. Löwis8afd7572003-01-25 22:46:11 +00002689
2690#ifdef Py_USING_UNICODE
2691/* Helper for filter(): filter a Unicode object through a function */
2692
2693static PyObject *
2694filterunicode(PyObject *func, PyObject *strobj)
2695{
2696 PyObject *result;
Martin v. Löwis725507b2006-03-07 12:08:51 +00002697 register Py_ssize_t i, j;
Martin v. Löwisd96ee902006-02-16 14:37:16 +00002698 Py_ssize_t len = PyUnicode_GetSize(strobj);
2699 Py_ssize_t outlen = len;
Martin v. Löwis8afd7572003-01-25 22:46:11 +00002700
2701 if (func == Py_None) {
Walter Dörwald1918f772003-02-10 13:19:13 +00002702 /* If it's a real string we can return the original,
2703 * as no character is ever false and __getitem__
2704 * does return this character. If it's a subclass
2705 * we must go through the __getitem__ loop */
2706 if (PyUnicode_CheckExact(strobj)) {
Walter Dörwaldc3da83f2003-02-04 20:24:45 +00002707 Py_INCREF(strobj);
Walter Dörwald1918f772003-02-10 13:19:13 +00002708 return strobj;
2709 }
Martin v. Löwis8afd7572003-01-25 22:46:11 +00002710 }
2711 if ((result = PyUnicode_FromUnicode(NULL, len)) == NULL)
2712 return NULL;
2713
2714 for (i = j = 0; i < len; ++i) {
2715 PyObject *item, *arg, *good;
2716 int ok;
2717
2718 item = (*strobj->ob_type->tp_as_sequence->sq_item)(strobj, i);
2719 if (item == NULL)
2720 goto Fail_1;
Walter Dörwald1918f772003-02-10 13:19:13 +00002721 if (func == Py_None) {
2722 ok = 1;
2723 } else {
Raymond Hettinger8ae46892003-10-12 19:09:37 +00002724 arg = PyTuple_Pack(1, item);
Walter Dörwald1918f772003-02-10 13:19:13 +00002725 if (arg == NULL) {
2726 Py_DECREF(item);
2727 goto Fail_1;
2728 }
2729 good = PyEval_CallObject(func, arg);
2730 Py_DECREF(arg);
2731 if (good == NULL) {
2732 Py_DECREF(item);
2733 goto Fail_1;
2734 }
2735 ok = PyObject_IsTrue(good);
2736 Py_DECREF(good);
Martin v. Löwis8afd7572003-01-25 22:46:11 +00002737 }
Walter Dörwald903f1e02003-02-04 16:28:00 +00002738 if (ok) {
Martin v. Löwisd96ee902006-02-16 14:37:16 +00002739 Py_ssize_t reslen;
Walter Dörwald903f1e02003-02-04 16:28:00 +00002740 if (!PyUnicode_Check(item)) {
Georg Brandl99d7e4e2005-08-31 22:21:15 +00002741 PyErr_SetString(PyExc_TypeError,
Jeremy Hylton1aad9c72003-09-16 03:10:59 +00002742 "can't filter unicode to unicode:"
2743 " __getitem__ returned different type");
Walter Dörwald903f1e02003-02-04 16:28:00 +00002744 Py_DECREF(item);
2745 goto Fail_1;
2746 }
2747 reslen = PyUnicode_GET_SIZE(item);
Georg Brandl99d7e4e2005-08-31 22:21:15 +00002748 if (reslen == 1)
Walter Dörwald903f1e02003-02-04 16:28:00 +00002749 PyUnicode_AS_UNICODE(result)[j++] =
2750 PyUnicode_AS_UNICODE(item)[0];
Jeremy Hylton1aad9c72003-09-16 03:10:59 +00002751 else {
Walter Dörwald903f1e02003-02-04 16:28:00 +00002752 /* do we need more space? */
Martin v. Löwisd96ee902006-02-16 14:37:16 +00002753 Py_ssize_t need = j + reslen + len - i - 1;
Walter Dörwald903f1e02003-02-04 16:28:00 +00002754 if (need > outlen) {
Georg Brandl99d7e4e2005-08-31 22:21:15 +00002755 /* overallocate,
Jeremy Hylton1aad9c72003-09-16 03:10:59 +00002756 to avoid reallocations */
2757 if (need < 2 * outlen)
2758 need = 2 * outlen;
Jeremy Hylton364f6be2003-09-16 03:17:16 +00002759 if (PyUnicode_Resize(
2760 &result, need) < 0) {
Walter Dörwald903f1e02003-02-04 16:28:00 +00002761 Py_DECREF(item);
Walter Dörwald531e0002003-02-04 16:57:49 +00002762 goto Fail_1;
Walter Dörwald903f1e02003-02-04 16:28:00 +00002763 }
2764 outlen = need;
2765 }
Jeremy Hylton1aad9c72003-09-16 03:10:59 +00002766 memcpy(PyUnicode_AS_UNICODE(result) + j,
2767 PyUnicode_AS_UNICODE(item),
2768 reslen*sizeof(Py_UNICODE));
Walter Dörwald903f1e02003-02-04 16:28:00 +00002769 j += reslen;
2770 }
2771 }
Martin v. Löwis8afd7572003-01-25 22:46:11 +00002772 Py_DECREF(item);
2773 }
2774
Walter Dörwald903f1e02003-02-04 16:28:00 +00002775 if (j < outlen)
Martin v. Löwis8afd7572003-01-25 22:46:11 +00002776 PyUnicode_Resize(&result, j);
2777
2778 return result;
2779
2780Fail_1:
2781 Py_DECREF(result);
2782 return NULL;
2783}
2784#endif