blob: fe5cb42cca6ca2add2858b6a6fb5b706af3376f0 [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) {
Guido van Rossumc7903a12002-08-16 07:04:56 +0000299 ok = PyObject_IsTrue(item);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000300 }
301 else {
Guido van Rossumc7903a12002-08-16 07:04:56 +0000302 PyObject *good;
303 PyTuple_SET_ITEM(arg, 0, item);
304 good = PyObject_Call(func, arg, NULL);
305 PyTuple_SET_ITEM(arg, 0, NULL);
Guido van Rossum58b68731995-01-10 17:40:55 +0000306 if (good == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000307 Py_DECREF(item);
Tim Peters0e57abf2001-05-02 07:39:38 +0000308 goto Fail_result_it;
Guido van Rossum58b68731995-01-10 17:40:55 +0000309 }
Guido van Rossumc7903a12002-08-16 07:04:56 +0000310 ok = PyObject_IsTrue(good);
311 Py_DECREF(good);
Guido van Rossum12d12c51993-10-26 17:58:25 +0000312 }
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000313 if (ok) {
Tim Peters0e57abf2001-05-02 07:39:38 +0000314 if (j < len)
315 PyList_SET_ITEM(result, j, item);
Guido van Rossum2d951851994-08-29 12:52:16 +0000316 else {
Barry Warsawfa77e091999-01-28 18:49:12 +0000317 int status = PyList_Append(result, item);
Barry Warsawfa77e091999-01-28 18:49:12 +0000318 Py_DECREF(item);
319 if (status < 0)
Tim Peters0e57abf2001-05-02 07:39:38 +0000320 goto Fail_result_it;
Guido van Rossum2d951851994-08-29 12:52:16 +0000321 }
Tim Peters0e57abf2001-05-02 07:39:38 +0000322 ++j;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000323 }
Tim Peters0e57abf2001-05-02 07:39:38 +0000324 else
325 Py_DECREF(item);
Guido van Rossum12d12c51993-10-26 17:58:25 +0000326 }
327
Guido van Rossum12d12c51993-10-26 17:58:25 +0000328
Tim Peters0e57abf2001-05-02 07:39:38 +0000329 /* Cut back result list if len is too big. */
Guido van Rossum79f25d91997-04-29 20:08:16 +0000330 if (j < len && PyList_SetSlice(result, j, len, NULL) < 0)
Tim Peters0e57abf2001-05-02 07:39:38 +0000331 goto Fail_result_it;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000332
Tim Peters3c6b1482001-05-21 08:07:05 +0000333 Py_DECREF(it);
Guido van Rossumc7903a12002-08-16 07:04:56 +0000334 Py_DECREF(arg);
Guido van Rossum12d12c51993-10-26 17:58:25 +0000335 return result;
336
Tim Peters0e57abf2001-05-02 07:39:38 +0000337Fail_result_it:
Guido van Rossum79f25d91997-04-29 20:08:16 +0000338 Py_DECREF(result);
Tim Peters0e57abf2001-05-02 07:39:38 +0000339Fail_it:
340 Py_DECREF(it);
Guido van Rossumc7903a12002-08-16 07:04:56 +0000341Fail_arg:
342 Py_DECREF(arg);
Guido van Rossum12d12c51993-10-26 17:58:25 +0000343 return NULL;
344}
345
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000346PyDoc_STRVAR(filter_doc,
Tim Petersd50e5442002-03-09 00:06:26 +0000347"filter(function or None, sequence) -> list, tuple, or string\n"
348"\n"
349"Return those items of sequence for which function(item) is true. If\n"
350"function is None, return the items that are true. If sequence is a tuple\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000351"or string, return the same type, else return a list.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000352
Guido van Rossum79f25d91997-04-29 20:08:16 +0000353static PyObject *
Eric Smitha9f7d622008-02-17 19:46:49 +0000354builtin_format(PyObject *self, PyObject *args)
355{
356 PyObject *value;
357 PyObject *format_spec = NULL;
358
359 if (!PyArg_ParseTuple(args, "O|O:format", &value, &format_spec))
360 return NULL;
361
362 return PyObject_Format(value, format_spec);
363}
364
365PyDoc_STRVAR(format_doc,
366"format(value[, format_spec]) -> string\n\
367\n\
368Returns value.__format__(format_spec)\n\
369format_spec defaults to \"\"");
370
371static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000372builtin_chr(PyObject *self, PyObject *args)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000373{
374 long x;
375 char s[1];
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000376
Guido van Rossum79f25d91997-04-29 20:08:16 +0000377 if (!PyArg_ParseTuple(args, "l:chr", &x))
Guido van Rossum3f5da241990-12-20 15:06:42 +0000378 return NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000379 if (x < 0 || x >= 256) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000380 PyErr_SetString(PyExc_ValueError,
381 "chr() arg not in range(256)");
Guido van Rossum3f5da241990-12-20 15:06:42 +0000382 return NULL;
383 }
Guido van Rossum6bf62da1997-04-11 20:37:35 +0000384 s[0] = (char)x;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000385 return PyString_FromStringAndSize(s, 1);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000386}
387
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000388PyDoc_STRVAR(chr_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000389"chr(i) -> character\n\
390\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000391Return a string of one character with ordinal i; 0 <= i < 256.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000392
393
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000394#ifdef Py_USING_UNICODE
Guido van Rossum79f25d91997-04-29 20:08:16 +0000395static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000396builtin_unichr(PyObject *self, PyObject *args)
Guido van Rossum09095f32000-03-10 23:00:52 +0000397{
398 long x;
Guido van Rossum09095f32000-03-10 23:00:52 +0000399
400 if (!PyArg_ParseTuple(args, "l:unichr", &x))
401 return NULL;
Fredrik Lundh0dcf67e2001-06-26 20:01:56 +0000402
Marc-André Lemburgcc8764c2002-08-11 12:23:04 +0000403 return PyUnicode_FromOrdinal(x);
Guido van Rossum09095f32000-03-10 23:00:52 +0000404}
405
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000406PyDoc_STRVAR(unichr_doc,
Fred Drake078b24f2000-04-13 02:42:50 +0000407"unichr(i) -> Unicode character\n\
Guido van Rossum09095f32000-03-10 23:00:52 +0000408\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000409Return a Unicode string of one character with ordinal i; 0 <= i <= 0x10ffff.");
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000410#endif
Guido van Rossum09095f32000-03-10 23:00:52 +0000411
412
413static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000414builtin_cmp(PyObject *self, PyObject *args)
Guido van Rossuma9e7dc11992-10-18 18:53:57 +0000415{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000416 PyObject *a, *b;
Guido van Rossum09df08a1998-05-22 00:51:39 +0000417 int c;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000418
Raymond Hettingerea3fdf42002-12-29 16:33:45 +0000419 if (!PyArg_UnpackTuple(args, "cmp", 2, 2, &a, &b))
Guido van Rossuma9e7dc11992-10-18 18:53:57 +0000420 return NULL;
Guido van Rossum09df08a1998-05-22 00:51:39 +0000421 if (PyObject_Cmp(a, b, &c) < 0)
Guido van Rossumc8b6df91997-05-23 00:06:51 +0000422 return NULL;
Guido van Rossum09df08a1998-05-22 00:51:39 +0000423 return PyInt_FromLong((long)c);
Guido van Rossuma9e7dc11992-10-18 18:53:57 +0000424}
425
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000426PyDoc_STRVAR(cmp_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000427"cmp(x, y) -> integer\n\
428\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000429Return negative if x<y, zero if x==y, positive if x>y.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000430
431
Guido van Rossum79f25d91997-04-29 20:08:16 +0000432static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000433builtin_coerce(PyObject *self, PyObject *args)
Guido van Rossum6a00cd81995-01-07 12:39:01 +0000434{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000435 PyObject *v, *w;
436 PyObject *res;
Guido van Rossum5524a591995-01-10 15:26:20 +0000437
Neal Norwitzdf25efe2007-05-23 06:58:36 +0000438 if (Py_Py3kWarningFlag &&
439 PyErr_Warn(PyExc_DeprecationWarning,
440 "coerce() not supported in 3.x") < 0)
441 return NULL;
442
Raymond Hettingerea3fdf42002-12-29 16:33:45 +0000443 if (!PyArg_UnpackTuple(args, "coerce", 2, 2, &v, &w))
Guido van Rossum5524a591995-01-10 15:26:20 +0000444 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000445 if (PyNumber_Coerce(&v, &w) < 0)
Guido van Rossum04691fc1992-08-12 15:35:34 +0000446 return NULL;
Raymond Hettinger8ae46892003-10-12 19:09:37 +0000447 res = PyTuple_Pack(2, v, w);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000448 Py_DECREF(v);
449 Py_DECREF(w);
Guido van Rossum04691fc1992-08-12 15:35:34 +0000450 return res;
451}
452
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000453PyDoc_STRVAR(coerce_doc,
Martin v. Löwis8d494f32004-08-25 10:42:41 +0000454"coerce(x, y) -> (x1, y1)\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000455\n\
Martin v. Löwis8d494f32004-08-25 10:42:41 +0000456Return a tuple consisting of the two numeric arguments converted to\n\
457a common type, using the same rules as used by arithmetic operations.\n\
458If coercion is not possible, raise TypeError.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000459
Guido van Rossum79f25d91997-04-29 20:08:16 +0000460static PyObject *
Georg Brandl5240d742007-03-13 20:46:32 +0000461builtin_compile(PyObject *self, PyObject *args, PyObject *kwds)
Guido van Rossum5b722181993-03-30 17:46:03 +0000462{
463 char *str;
464 char *filename;
465 char *startstr;
466 int start;
Tim Peters6cd6a822001-08-17 22:11:27 +0000467 int dont_inherit = 0;
468 int supplied_flags = 0;
Tim Peters5ba58662001-07-16 02:29:45 +0000469 PyCompilerFlags cf;
Neal Norwitz3a9a3e72005-11-27 20:38:31 +0000470 PyObject *result = NULL, *cmd, *tmp = NULL;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000471 Py_ssize_t length;
Georg Brandl5240d742007-03-13 20:46:32 +0000472 static char *kwlist[] = {"source", "filename", "mode", "flags",
473 "dont_inherit", NULL};
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000474
Georg Brandl5240d742007-03-13 20:46:32 +0000475 if (!PyArg_ParseTupleAndKeywords(args, kwds, "Oss|ii:compile",
476 kwlist, &cmd, &filename, &startstr,
477 &supplied_flags, &dont_inherit))
Guido van Rossum5b722181993-03-30 17:46:03 +0000478 return NULL;
Tim Peters6cd6a822001-08-17 22:11:27 +0000479
Just van Rossum3aaf42c2003-02-10 08:21:10 +0000480 cf.cf_flags = supplied_flags;
481
482#ifdef Py_USING_UNICODE
483 if (PyUnicode_Check(cmd)) {
484 tmp = PyUnicode_AsUTF8String(cmd);
485 if (tmp == NULL)
486 return NULL;
487 cmd = tmp;
488 cf.cf_flags |= PyCF_SOURCE_IS_UTF8;
489 }
490#endif
Just van Rossumb9b8e9c2003-02-10 09:22:01 +0000491 if (PyObject_AsReadBuffer(cmd, (const void **)&str, &length))
492 return NULL;
Tim Peters2c646c92003-02-10 14:48:29 +0000493 if ((size_t)length != strlen(str)) {
Just van Rossum3aaf42c2003-02-10 08:21:10 +0000494 PyErr_SetString(PyExc_TypeError,
Alex Martellia9b9c9f2003-04-23 13:34:35 +0000495 "compile() expected string without null bytes");
Neal Norwitz3a9a3e72005-11-27 20:38:31 +0000496 goto cleanup;
Just van Rossum3aaf42c2003-02-10 08:21:10 +0000497 }
498
Guido van Rossum5b722181993-03-30 17:46:03 +0000499 if (strcmp(startstr, "exec") == 0)
Guido van Rossumb05a5c71997-05-07 17:46:13 +0000500 start = Py_file_input;
Guido van Rossum5b722181993-03-30 17:46:03 +0000501 else if (strcmp(startstr, "eval") == 0)
Guido van Rossumb05a5c71997-05-07 17:46:13 +0000502 start = Py_eval_input;
Guido van Rossum872537c1995-07-07 22:43:42 +0000503 else if (strcmp(startstr, "single") == 0)
Guido van Rossumb05a5c71997-05-07 17:46:13 +0000504 start = Py_single_input;
Guido van Rossum5b722181993-03-30 17:46:03 +0000505 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000506 PyErr_SetString(PyExc_ValueError,
Fred Drake661ea262000-10-24 19:57:45 +0000507 "compile() arg 3 must be 'exec' or 'eval' or 'single'");
Neal Norwitz3a9a3e72005-11-27 20:38:31 +0000508 goto cleanup;
Guido van Rossum5b722181993-03-30 17:46:03 +0000509 }
Tim Peters6cd6a822001-08-17 22:11:27 +0000510
Guido van Rossum4b499dd32003-02-13 22:07:59 +0000511 if (supplied_flags &
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000512 ~(PyCF_MASK | PyCF_MASK_OBSOLETE | PyCF_DONT_IMPLY_DEDENT | PyCF_ONLY_AST))
Guido van Rossum4b499dd32003-02-13 22:07:59 +0000513 {
Tim Peters6cd6a822001-08-17 22:11:27 +0000514 PyErr_SetString(PyExc_ValueError,
515 "compile(): unrecognised flags");
Neal Norwitz3a9a3e72005-11-27 20:38:31 +0000516 goto cleanup;
Tim Peters6cd6a822001-08-17 22:11:27 +0000517 }
518 /* XXX Warn if (supplied_flags & PyCF_MASK_OBSOLETE) != 0? */
519
Tim Peters6cd6a822001-08-17 22:11:27 +0000520 if (!dont_inherit) {
521 PyEval_MergeCompilerFlags(&cf);
522 }
Just van Rossum3aaf42c2003-02-10 08:21:10 +0000523 result = Py_CompileStringFlags(str, filename, start, &cf);
Neal Norwitz3a9a3e72005-11-27 20:38:31 +0000524cleanup:
Just van Rossum3aaf42c2003-02-10 08:21:10 +0000525 Py_XDECREF(tmp);
526 return result;
Guido van Rossum5b722181993-03-30 17:46:03 +0000527}
528
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000529PyDoc_STRVAR(compile_doc,
Tim Peters6cd6a822001-08-17 22:11:27 +0000530"compile(source, filename, mode[, flags[, dont_inherit]]) -> code object\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000531\n\
532Compile the source string (a Python module, statement or expression)\n\
533into a code object that can be executed by the exec statement or eval().\n\
534The filename will be used for run-time error messages.\n\
535The mode must be 'exec' to compile a module, 'single' to compile a\n\
Tim Peters6cd6a822001-08-17 22:11:27 +0000536single (interactive) statement, or 'eval' to compile an expression.\n\
537The flags argument, if present, controls which future statements influence\n\
538the compilation of the code.\n\
539The dont_inherit argument, if non-zero, stops the compilation inheriting\n\
540the effects of any future statements in effect in the code calling\n\
541compile; if absent or zero these statements do influence the compilation,\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000542in addition to any features explicitly specified.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000543
Guido van Rossum79f25d91997-04-29 20:08:16 +0000544static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000545builtin_dir(PyObject *self, PyObject *args)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000546{
Tim Peters5d2b77c2001-09-03 05:47:38 +0000547 PyObject *arg = NULL;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000548
Raymond Hettingerea3fdf42002-12-29 16:33:45 +0000549 if (!PyArg_UnpackTuple(args, "dir", 0, 1, &arg))
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000550 return NULL;
Tim Peters7eea37e2001-09-04 22:08:56 +0000551 return PyObject_Dir(arg);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000552}
553
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000554PyDoc_STRVAR(dir_doc,
Tim Peters5d2b77c2001-09-03 05:47:38 +0000555"dir([object]) -> list of strings\n"
556"\n"
Georg Brandl871f1bc2007-03-12 13:17:36 +0000557"If called without an argument, return the names in the current scope.\n"
558"Else, return an alphabetized list of names comprising (some of) the attributes\n"
559"of the given object, and of attributes reachable from it.\n"
560"If the object supplies a method named __dir__, it will be used; otherwise\n"
561"the default dir() logic is used and returns:\n"
562" for a module object: the module's attributes.\n"
563" for a class object: its attributes, and recursively the attributes\n"
564" of its bases.\n"
Georg Brandl3bb15672007-03-13 07:23:16 +0000565" for any other object: its attributes, its class's attributes, and\n"
Georg Brandl871f1bc2007-03-12 13:17:36 +0000566" recursively the attributes of its class's base classes.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000567
Guido van Rossum79f25d91997-04-29 20:08:16 +0000568static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000569builtin_divmod(PyObject *self, PyObject *args)
Guido van Rossum6a00cd81995-01-07 12:39:01 +0000570{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000571 PyObject *v, *w;
Guido van Rossum6a00cd81995-01-07 12:39:01 +0000572
Raymond Hettingerea3fdf42002-12-29 16:33:45 +0000573 if (!PyArg_UnpackTuple(args, "divmod", 2, 2, &v, &w))
Guido van Rossum6a00cd81995-01-07 12:39:01 +0000574 return NULL;
Guido van Rossum09df08a1998-05-22 00:51:39 +0000575 return PyNumber_Divmod(v, w);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000576}
577
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000578PyDoc_STRVAR(divmod_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000579"divmod(x, y) -> (div, mod)\n\
580\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000581Return the tuple ((x-x%y)/y, x%y). Invariant: div*y + mod == x.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000582
583
Guido van Rossum79f25d91997-04-29 20:08:16 +0000584static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000585builtin_eval(PyObject *self, PyObject *args)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000586{
Just van Rossum3aaf42c2003-02-10 08:21:10 +0000587 PyObject *cmd, *result, *tmp = NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000588 PyObject *globals = Py_None, *locals = Py_None;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000589 char *str;
Tim Peters9fa96be2001-08-17 23:04:59 +0000590 PyCompilerFlags cf;
Guido van Rossum590baa41993-11-30 13:40:46 +0000591
Raymond Hettinger214b1c32004-07-02 06:41:07 +0000592 if (!PyArg_UnpackTuple(args, "eval", 1, 3, &cmd, &globals, &locals))
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000593 return NULL;
Raymond Hettinger214b1c32004-07-02 06:41:07 +0000594 if (locals != Py_None && !PyMapping_Check(locals)) {
595 PyErr_SetString(PyExc_TypeError, "locals must be a mapping");
Raymond Hettinger513ffe82004-07-06 13:44:41 +0000596 return NULL;
Raymond Hettinger214b1c32004-07-02 06:41:07 +0000597 }
598 if (globals != Py_None && !PyDict_Check(globals)) {
Georg Brandl99d7e4e2005-08-31 22:21:15 +0000599 PyErr_SetString(PyExc_TypeError, PyMapping_Check(globals) ?
Raymond Hettinger214b1c32004-07-02 06:41:07 +0000600 "globals must be a real dict; try eval(expr, {}, mapping)"
601 : "globals must be a dict");
Raymond Hettinger513ffe82004-07-06 13:44:41 +0000602 return NULL;
Raymond Hettinger214b1c32004-07-02 06:41:07 +0000603 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000604 if (globals == Py_None) {
605 globals = PyEval_GetGlobals();
606 if (locals == Py_None)
607 locals = PyEval_GetLocals();
Guido van Rossum6135a871995-01-09 17:53:26 +0000608 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000609 else if (locals == Py_None)
Guido van Rossum6135a871995-01-09 17:53:26 +0000610 locals = globals;
Tim Peters9fa96be2001-08-17 23:04:59 +0000611
Georg Brandl77c85e62005-09-15 10:46:13 +0000612 if (globals == NULL || locals == NULL) {
613 PyErr_SetString(PyExc_TypeError,
614 "eval must be given globals and locals "
615 "when called without a frame");
616 return NULL;
617 }
618
Guido van Rossum79f25d91997-04-29 20:08:16 +0000619 if (PyDict_GetItemString(globals, "__builtins__") == NULL) {
620 if (PyDict_SetItemString(globals, "__builtins__",
621 PyEval_GetBuiltins()) != 0)
Guido van Rossum6135a871995-01-09 17:53:26 +0000622 return NULL;
623 }
Tim Peters9fa96be2001-08-17 23:04:59 +0000624
Jeremy Hylton15c1c4f2001-07-30 21:50:55 +0000625 if (PyCode_Check(cmd)) {
Jeremy Hylton733c8932001-12-13 19:51:56 +0000626 if (PyCode_GetNumFree((PyCodeObject *)cmd) > 0) {
Jeremy Hylton15c1c4f2001-07-30 21:50:55 +0000627 PyErr_SetString(PyExc_TypeError,
628 "code object passed to eval() may not contain free variables");
629 return NULL;
630 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000631 return PyEval_EvalCode((PyCodeObject *) cmd, globals, locals);
Jeremy Hylton15c1c4f2001-07-30 21:50:55 +0000632 }
Tim Peters9fa96be2001-08-17 23:04:59 +0000633
Marc-André Lemburgd1ba4432000-09-19 21:04:18 +0000634 if (!PyString_Check(cmd) &&
635 !PyUnicode_Check(cmd)) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000636 PyErr_SetString(PyExc_TypeError,
Fred Drake661ea262000-10-24 19:57:45 +0000637 "eval() arg 1 must be a string or code object");
Guido van Rossum94390a41992-08-14 15:14:30 +0000638 return NULL;
639 }
Just van Rossum3aaf42c2003-02-10 08:21:10 +0000640 cf.cf_flags = 0;
641
642#ifdef Py_USING_UNICODE
643 if (PyUnicode_Check(cmd)) {
644 tmp = PyUnicode_AsUTF8String(cmd);
645 if (tmp == NULL)
646 return NULL;
647 cmd = tmp;
648 cf.cf_flags |= PyCF_SOURCE_IS_UTF8;
649 }
650#endif
Neal Norwitz3a9a3e72005-11-27 20:38:31 +0000651 if (PyString_AsStringAndSize(cmd, &str, NULL)) {
652 Py_XDECREF(tmp);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000653 return NULL;
Neal Norwitz3a9a3e72005-11-27 20:38:31 +0000654 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000655 while (*str == ' ' || *str == '\t')
656 str++;
Tim Peters9fa96be2001-08-17 23:04:59 +0000657
Tim Peters9fa96be2001-08-17 23:04:59 +0000658 (void)PyEval_MergeCompilerFlags(&cf);
Just van Rossum3aaf42c2003-02-10 08:21:10 +0000659 result = PyRun_StringFlags(str, Py_eval_input, globals, locals, &cf);
660 Py_XDECREF(tmp);
661 return result;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000662}
663
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000664PyDoc_STRVAR(eval_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000665"eval(source[, globals[, locals]]) -> value\n\
666\n\
667Evaluate the source in the context of globals and locals.\n\
668The source may be a string representing a Python expression\n\
669or a code object as returned by compile().\n\
Neal Norwitz477ca1c2006-09-05 02:25:41 +0000670The globals must be a dictionary and locals can be any mapping,\n\
Raymond Hettinger214b1c32004-07-02 06:41:07 +0000671defaulting to the current globals and locals.\n\
672If only globals is given, locals defaults to it.\n");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000673
674
Guido van Rossum79f25d91997-04-29 20:08:16 +0000675static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000676builtin_execfile(PyObject *self, PyObject *args)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000677{
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000678 char *filename;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000679 PyObject *globals = Py_None, *locals = Py_None;
680 PyObject *res;
Guido van Rossumb3a639e2001-09-05 13:37:47 +0000681 FILE* fp = NULL;
Tim Peters5ba58662001-07-16 02:29:45 +0000682 PyCompilerFlags cf;
Martin v. Löwis6b3a2c42001-08-08 05:30:36 +0000683 int exists;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000684
Neal Norwitzdf25efe2007-05-23 06:58:36 +0000685 if (Py_Py3kWarningFlag &&
686 PyErr_Warn(PyExc_DeprecationWarning,
Neal Norwitzb93e7d12008-02-24 02:40:58 +0000687 "execfile() not supported in 3.x. Use exec().") < 0)
Neal Norwitzdf25efe2007-05-23 06:58:36 +0000688 return NULL;
689
Raymond Hettinger66bd2332004-08-02 08:30:07 +0000690 if (!PyArg_ParseTuple(args, "s|O!O:execfile",
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000691 &filename,
Guido van Rossum79f25d91997-04-29 20:08:16 +0000692 &PyDict_Type, &globals,
Raymond Hettinger66bd2332004-08-02 08:30:07 +0000693 &locals))
Guido van Rossum0f61f8a1992-02-25 18:55:05 +0000694 return NULL;
Raymond Hettinger66bd2332004-08-02 08:30:07 +0000695 if (locals != Py_None && !PyMapping_Check(locals)) {
696 PyErr_SetString(PyExc_TypeError, "locals must be a mapping");
697 return NULL;
698 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000699 if (globals == Py_None) {
700 globals = PyEval_GetGlobals();
701 if (locals == Py_None)
702 locals = PyEval_GetLocals();
Guido van Rossum6135a871995-01-09 17:53:26 +0000703 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000704 else if (locals == Py_None)
Guido van Rossum6135a871995-01-09 17:53:26 +0000705 locals = globals;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000706 if (PyDict_GetItemString(globals, "__builtins__") == NULL) {
707 if (PyDict_SetItemString(globals, "__builtins__",
708 PyEval_GetBuiltins()) != 0)
Guido van Rossum6135a871995-01-09 17:53:26 +0000709 return NULL;
710 }
Martin v. Löwis6b3a2c42001-08-08 05:30:36 +0000711
712 exists = 0;
713 /* Test for existence or directory. */
Martin v. Löwis3484a182002-03-09 12:07:51 +0000714#if defined(PLAN9)
715 {
716 Dir *d;
717
718 if ((d = dirstat(filename))!=nil) {
719 if(d->mode & DMDIR)
720 werrstr("is a directory");
721 else
722 exists = 1;
723 free(d);
724 }
Martin v. Löwis6b3a2c42001-08-08 05:30:36 +0000725 }
Martin v. Löwis3484a182002-03-09 12:07:51 +0000726#elif defined(RISCOS)
Guido van Rossume2ae77b2001-10-24 20:42:55 +0000727 if (object_exists(filename)) {
728 if (isdir(filename))
729 errno = EISDIR;
730 else
731 exists = 1;
732 }
Martin v. Löwis3484a182002-03-09 12:07:51 +0000733#else /* standard Posix */
734 {
735 struct stat s;
736 if (stat(filename, &s) == 0) {
737 if (S_ISDIR(s.st_mode))
Andrew MacIntyreda4d6cb2004-03-29 11:53:38 +0000738# if defined(PYOS_OS2) && defined(PYCC_VACPP)
Martin v. Löwis3484a182002-03-09 12:07:51 +0000739 errno = EOS2ERR;
740# else
741 errno = EISDIR;
742# endif
743 else
744 exists = 1;
745 }
746 }
747#endif
Martin v. Löwis6b3a2c42001-08-08 05:30:36 +0000748
749 if (exists) {
750 Py_BEGIN_ALLOW_THREADS
Jack Jansen7b8c7542002-04-14 20:12:41 +0000751 fp = fopen(filename, "r" PY_STDIOTEXTMODE);
Martin v. Löwis6b3a2c42001-08-08 05:30:36 +0000752 Py_END_ALLOW_THREADS
753
754 if (fp == NULL) {
755 exists = 0;
756 }
757 }
758
759 if (!exists) {
Peter Schneider-Kamp4c013422002-08-27 16:58:00 +0000760 PyErr_SetFromErrnoWithFilename(PyExc_IOError, filename);
Guido van Rossum0f61f8a1992-02-25 18:55:05 +0000761 return NULL;
762 }
Tim Peters5ba58662001-07-16 02:29:45 +0000763 cf.cf_flags = 0;
764 if (PyEval_MergeCompilerFlags(&cf))
Tim Peters748b8bb2001-04-28 08:20:22 +0000765 res = PyRun_FileExFlags(fp, filename, Py_file_input, globals,
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000766 locals, 1, &cf);
Tim Peters5ba58662001-07-16 02:29:45 +0000767 else
Tim Peters748b8bb2001-04-28 08:20:22 +0000768 res = PyRun_FileEx(fp, filename, Py_file_input, globals,
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000769 locals, 1);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000770 return res;
Guido van Rossum0f61f8a1992-02-25 18:55:05 +0000771}
772
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000773PyDoc_STRVAR(execfile_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000774"execfile(filename[, globals[, locals]])\n\
775\n\
776Read and execute a Python script from a file.\n\
777The globals and locals are dictionaries, defaulting to the current\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000778globals and locals. If only globals is given, locals defaults to it.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000779
780
Guido van Rossum79f25d91997-04-29 20:08:16 +0000781static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000782builtin_getattr(PyObject *self, PyObject *args)
Guido van Rossum33894be1992-01-27 16:53:09 +0000783{
Guido van Rossum950ff291998-06-29 13:38:57 +0000784 PyObject *v, *result, *dflt = NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000785 PyObject *name;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000786
Raymond Hettingerea3fdf42002-12-29 16:33:45 +0000787 if (!PyArg_UnpackTuple(args, "getattr", 2, 3, &v, &name, &dflt))
Guido van Rossum33894be1992-01-27 16:53:09 +0000788 return NULL;
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000789#ifdef Py_USING_UNICODE
Jeremy Hylton0eb11152001-07-30 22:39:31 +0000790 if (PyUnicode_Check(name)) {
791 name = _PyUnicode_AsDefaultEncodedString(name, NULL);
792 if (name == NULL)
793 return NULL;
794 }
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000795#endif
Jeremy Hylton0eb11152001-07-30 22:39:31 +0000796
797 if (!PyString_Check(name)) {
798 PyErr_SetString(PyExc_TypeError,
Alex Martellia9b9c9f2003-04-23 13:34:35 +0000799 "getattr(): attribute name must be string");
Jeremy Hylton0eb11152001-07-30 22:39:31 +0000800 return NULL;
801 }
Guido van Rossum950ff291998-06-29 13:38:57 +0000802 result = PyObject_GetAttr(v, name);
Guido van Rossumd8923572001-10-16 21:31:32 +0000803 if (result == NULL && dflt != NULL &&
804 PyErr_ExceptionMatches(PyExc_AttributeError))
805 {
Guido van Rossum950ff291998-06-29 13:38:57 +0000806 PyErr_Clear();
807 Py_INCREF(dflt);
808 result = dflt;
809 }
810 return result;
Guido van Rossum9bfef441993-03-29 10:43:31 +0000811}
812
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000813PyDoc_STRVAR(getattr_doc,
Guido van Rossum950ff291998-06-29 13:38:57 +0000814"getattr(object, name[, default]) -> value\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000815\n\
Guido van Rossum950ff291998-06-29 13:38:57 +0000816Get a named attribute from an object; getattr(x, 'y') is equivalent to x.y.\n\
817When a default argument is given, it is returned when the attribute doesn't\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000818exist; without it, an exception is raised in that case.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000819
820
Guido van Rossum79f25d91997-04-29 20:08:16 +0000821static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000822builtin_globals(PyObject *self)
Guido van Rossum872537c1995-07-07 22:43:42 +0000823{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000824 PyObject *d;
Guido van Rossum872537c1995-07-07 22:43:42 +0000825
Guido van Rossum79f25d91997-04-29 20:08:16 +0000826 d = PyEval_GetGlobals();
Neal Norwitz43bd4db2006-08-12 01:46:42 +0000827 Py_XINCREF(d);
Guido van Rossum872537c1995-07-07 22:43:42 +0000828 return d;
829}
830
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000831PyDoc_STRVAR(globals_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000832"globals() -> dictionary\n\
833\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000834Return the dictionary containing the current scope's global variables.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000835
836
Guido van Rossum79f25d91997-04-29 20:08:16 +0000837static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000838builtin_hasattr(PyObject *self, PyObject *args)
Guido van Rossum9bfef441993-03-29 10:43:31 +0000839{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000840 PyObject *v;
841 PyObject *name;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000842
Raymond Hettingerea3fdf42002-12-29 16:33:45 +0000843 if (!PyArg_UnpackTuple(args, "hasattr", 2, 2, &v, &name))
Guido van Rossum9bfef441993-03-29 10:43:31 +0000844 return NULL;
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000845#ifdef Py_USING_UNICODE
Jeremy Hylton302b54a2001-07-30 22:45:19 +0000846 if (PyUnicode_Check(name)) {
847 name = _PyUnicode_AsDefaultEncodedString(name, NULL);
848 if (name == NULL)
849 return NULL;
850 }
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000851#endif
Jeremy Hylton302b54a2001-07-30 22:45:19 +0000852
853 if (!PyString_Check(name)) {
854 PyErr_SetString(PyExc_TypeError,
Alex Martellia9b9c9f2003-04-23 13:34:35 +0000855 "hasattr(): attribute name must be string");
Jeremy Hylton302b54a2001-07-30 22:45:19 +0000856 return NULL;
857 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000858 v = PyObject_GetAttr(v, name);
Guido van Rossum9bfef441993-03-29 10:43:31 +0000859 if (v == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000860 PyErr_Clear();
Guido van Rossum09df08a1998-05-22 00:51:39 +0000861 Py_INCREF(Py_False);
862 return Py_False;
Guido van Rossum9bfef441993-03-29 10:43:31 +0000863 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000864 Py_DECREF(v);
Guido van Rossum09df08a1998-05-22 00:51:39 +0000865 Py_INCREF(Py_True);
866 return Py_True;
Guido van Rossum33894be1992-01-27 16:53:09 +0000867}
868
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000869PyDoc_STRVAR(hasattr_doc,
Guido van Rossum77f6a652002-04-03 22:41:51 +0000870"hasattr(object, name) -> bool\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000871\n\
872Return whether the object has an attribute with the given name.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000873(This is done by calling getattr(object, name) and catching exceptions.)");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000874
875
Guido van Rossum79f25d91997-04-29 20:08:16 +0000876static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000877builtin_id(PyObject *self, PyObject *v)
Guido van Rossum5b722181993-03-30 17:46:03 +0000878{
Guido van Rossum106f2da2000-06-28 21:12:25 +0000879 return PyLong_FromVoidPtr(v);
Guido van Rossum5b722181993-03-30 17:46:03 +0000880}
881
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000882PyDoc_STRVAR(id_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000883"id(object) -> integer\n\
884\n\
885Return the identity of an object. This is guaranteed to be unique among\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000886simultaneously existing objects. (Hint: it's the object's memory address.)");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000887
888
Guido van Rossum79f25d91997-04-29 20:08:16 +0000889static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000890builtin_map(PyObject *self, PyObject *args)
Guido van Rossum12d12c51993-10-26 17:58:25 +0000891{
892 typedef struct {
Tim Peters4e9afdc2001-05-03 23:54:49 +0000893 PyObject *it; /* the iterator object */
894 int saw_StopIteration; /* bool: did the iterator end? */
Guido van Rossum12d12c51993-10-26 17:58:25 +0000895 } sequence;
896
Guido van Rossum79f25d91997-04-29 20:08:16 +0000897 PyObject *func, *result;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000898 sequence *seqs = NULL, *sqp;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000899 Py_ssize_t n, len;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000900 register int i, j;
901
Guido van Rossum79f25d91997-04-29 20:08:16 +0000902 n = PyTuple_Size(args);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000903 if (n < 2) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000904 PyErr_SetString(PyExc_TypeError,
905 "map() requires at least two args");
Guido van Rossum12d12c51993-10-26 17:58:25 +0000906 return NULL;
907 }
908
Guido van Rossum79f25d91997-04-29 20:08:16 +0000909 func = PyTuple_GetItem(args, 0);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000910 n--;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000911
Neal Norwitz53152a12008-02-24 02:20:25 +0000912 if (func == Py_None) {
913 if (Py_Py3kWarningFlag &&
914 PyErr_Warn(PyExc_DeprecationWarning,
Neal Norwitzb93e7d12008-02-24 02:40:58 +0000915 "map(None, ...) not supported in 3.x. Use list(...).") < 0)
Neal Norwitz53152a12008-02-24 02:20:25 +0000916 return NULL;
917 if (n == 1) {
918 /* map(None, S) is the same as list(S). */
919 return PySequence_List(PyTuple_GetItem(args, 1));
920 }
Guido van Rossumfa4ac711998-07-10 17:37:30 +0000921 }
922
Tim Peters4e9afdc2001-05-03 23:54:49 +0000923 /* Get space for sequence descriptors. Must NULL out the iterator
924 * pointers so that jumping to Fail_2 later doesn't see trash.
925 */
Guido van Rossum79f25d91997-04-29 20:08:16 +0000926 if ((seqs = PyMem_NEW(sequence, n)) == NULL) {
927 PyErr_NoMemory();
Tim Peters4e9afdc2001-05-03 23:54:49 +0000928 return NULL;
929 }
930 for (i = 0; i < n; ++i) {
931 seqs[i].it = (PyObject*)NULL;
932 seqs[i].saw_StopIteration = 0;
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000933 }
Guido van Rossum12d12c51993-10-26 17:58:25 +0000934
Tim Peters4e9afdc2001-05-03 23:54:49 +0000935 /* Do a first pass to obtain iterators for the arguments, and set len
936 * to the largest of their lengths.
Tim Peters748b8bb2001-04-28 08:20:22 +0000937 */
Tim Peters4e9afdc2001-05-03 23:54:49 +0000938 len = 0;
939 for (i = 0, sqp = seqs; i < n; ++i, ++sqp) {
940 PyObject *curseq;
Martin v. Löwisd96ee902006-02-16 14:37:16 +0000941 Py_ssize_t curlen;
Guido van Rossumad991772001-01-12 16:03:05 +0000942
Tim Peters4e9afdc2001-05-03 23:54:49 +0000943 /* Get iterator. */
944 curseq = PyTuple_GetItem(args, i+1);
945 sqp->it = PyObject_GetIter(curseq);
946 if (sqp->it == NULL) {
Guido van Rossum12d12c51993-10-26 17:58:25 +0000947 static char errmsg[] =
Tim Peters4e9afdc2001-05-03 23:54:49 +0000948 "argument %d to map() must support iteration";
Guido van Rossum15e33a41997-04-30 19:00:27 +0000949 char errbuf[sizeof(errmsg) + 25];
Jeremy Hylton518ab1c2001-11-28 20:42:20 +0000950 PyOS_snprintf(errbuf, sizeof(errbuf), errmsg, i+2);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000951 PyErr_SetString(PyExc_TypeError, errbuf);
Guido van Rossum12d12c51993-10-26 17:58:25 +0000952 goto Fail_2;
953 }
954
Tim Peters4e9afdc2001-05-03 23:54:49 +0000955 /* Update len. */
Raymond Hettinger4e2f7142007-12-06 00:56:53 +0000956 curlen = _PyObject_LengthHint(curseq, 8);
Raymond Hettinger77f3c872004-01-04 08:54:44 +0000957 if (curlen > len)
958 len = curlen;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000959 }
960
Tim Peters4e9afdc2001-05-03 23:54:49 +0000961 /* Get space for the result list. */
Guido van Rossum79f25d91997-04-29 20:08:16 +0000962 if ((result = (PyObject *) PyList_New(len)) == NULL)
Guido van Rossum12d12c51993-10-26 17:58:25 +0000963 goto Fail_2;
964
Tim Peters4e9afdc2001-05-03 23:54:49 +0000965 /* Iterate over the sequences until all have stopped. */
Guido van Rossum2d951851994-08-29 12:52:16 +0000966 for (i = 0; ; ++i) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000967 PyObject *alist, *item=NULL, *value;
Tim Peters4e9afdc2001-05-03 23:54:49 +0000968 int numactive = 0;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000969
Guido van Rossum79f25d91997-04-29 20:08:16 +0000970 if (func == Py_None && n == 1)
Guido van Rossum32120311995-07-10 13:52:21 +0000971 alist = NULL;
Tim Peters4e9afdc2001-05-03 23:54:49 +0000972 else if ((alist = PyTuple_New(n)) == NULL)
973 goto Fail_1;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000974
975 for (j = 0, sqp = seqs; j < n; ++j, ++sqp) {
Tim Peters4e9afdc2001-05-03 23:54:49 +0000976 if (sqp->saw_StopIteration) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000977 Py_INCREF(Py_None);
978 item = Py_None;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000979 }
Guido van Rossum12d12c51993-10-26 17:58:25 +0000980 else {
Tim Peters4e9afdc2001-05-03 23:54:49 +0000981 item = PyIter_Next(sqp->it);
982 if (item)
983 ++numactive;
984 else {
Tim Peters4e9afdc2001-05-03 23:54:49 +0000985 if (PyErr_Occurred()) {
Tim Petersf4848da2001-05-05 00:14:56 +0000986 Py_XDECREF(alist);
987 goto Fail_1;
Guido van Rossum2d951851994-08-29 12:52:16 +0000988 }
Tim Peters4e9afdc2001-05-03 23:54:49 +0000989 Py_INCREF(Py_None);
990 item = Py_None;
991 sqp->saw_StopIteration = 1;
Guido van Rossum2d951851994-08-29 12:52:16 +0000992 }
Guido van Rossum12d12c51993-10-26 17:58:25 +0000993 }
Tim Peters4e9afdc2001-05-03 23:54:49 +0000994 if (alist)
995 PyTuple_SET_ITEM(alist, j, item);
996 else
Guido van Rossum2d951851994-08-29 12:52:16 +0000997 break;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000998 }
999
Guido van Rossum32120311995-07-10 13:52:21 +00001000 if (!alist)
1001 alist = item;
Guido van Rossum2d951851994-08-29 12:52:16 +00001002
Tim Peters4e9afdc2001-05-03 23:54:49 +00001003 if (numactive == 0) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001004 Py_DECREF(alist);
Guido van Rossum2d951851994-08-29 12:52:16 +00001005 break;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001006 }
Guido van Rossum2d951851994-08-29 12:52:16 +00001007
Guido van Rossum79f25d91997-04-29 20:08:16 +00001008 if (func == Py_None)
Guido van Rossum32120311995-07-10 13:52:21 +00001009 value = alist;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001010 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001011 value = PyEval_CallObject(func, alist);
1012 Py_DECREF(alist);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00001013 if (value == NULL)
1014 goto Fail_1;
Guido van Rossum2d951851994-08-29 12:52:16 +00001015 }
1016 if (i >= len) {
Barry Warsawfa77e091999-01-28 18:49:12 +00001017 int status = PyList_Append(result, value);
Barry Warsaw21332871999-01-28 04:21:35 +00001018 Py_DECREF(value);
Barry Warsawfa77e091999-01-28 18:49:12 +00001019 if (status < 0)
1020 goto Fail_1;
Guido van Rossum2d951851994-08-29 12:52:16 +00001021 }
Tim Peters4e9afdc2001-05-03 23:54:49 +00001022 else if (PyList_SetItem(result, i, value) < 0)
1023 goto Fail_1;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001024 }
1025
Guido van Rossumfa4ac711998-07-10 17:37:30 +00001026 if (i < len && PyList_SetSlice(result, i, len, NULL) < 0)
1027 goto Fail_1;
1028
Tim Peters4e9afdc2001-05-03 23:54:49 +00001029 goto Succeed;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001030
Guido van Rossum12d12c51993-10-26 17:58:25 +00001031Fail_1:
Guido van Rossum79f25d91997-04-29 20:08:16 +00001032 Py_DECREF(result);
Guido van Rossum12d12c51993-10-26 17:58:25 +00001033Fail_2:
Tim Peters4e9afdc2001-05-03 23:54:49 +00001034 result = NULL;
1035Succeed:
1036 assert(seqs);
1037 for (i = 0; i < n; ++i)
1038 Py_XDECREF(seqs[i].it);
1039 PyMem_DEL(seqs);
1040 return result;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001041}
1042
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001043PyDoc_STRVAR(map_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001044"map(function, sequence[, sequence, ...]) -> list\n\
1045\n\
1046Return a list of the results of applying the function to the items of\n\
1047the argument sequence(s). If more than one sequence is given, the\n\
1048function is called with an argument list consisting of the corresponding\n\
1049item of each sequence, substituting None for missing values when not all\n\
1050sequences have the same length. If the function is None, return a list of\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001051the items of the sequence (or a list of tuples if more than one sequence).");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001052
1053
Guido van Rossum79f25d91997-04-29 20:08:16 +00001054static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001055builtin_setattr(PyObject *self, PyObject *args)
Guido van Rossum33894be1992-01-27 16:53:09 +00001056{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001057 PyObject *v;
1058 PyObject *name;
1059 PyObject *value;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001060
Raymond Hettingerea3fdf42002-12-29 16:33:45 +00001061 if (!PyArg_UnpackTuple(args, "setattr", 3, 3, &v, &name, &value))
Guido van Rossum33894be1992-01-27 16:53:09 +00001062 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001063 if (PyObject_SetAttr(v, name, value) != 0)
Guido van Rossum33894be1992-01-27 16:53:09 +00001064 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001065 Py_INCREF(Py_None);
1066 return Py_None;
Guido van Rossum33894be1992-01-27 16:53:09 +00001067}
1068
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001069PyDoc_STRVAR(setattr_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001070"setattr(object, name, value)\n\
1071\n\
1072Set a named attribute on an object; setattr(x, 'y', v) is equivalent to\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001073``x.y = v''.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001074
1075
Guido van Rossum79f25d91997-04-29 20:08:16 +00001076static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001077builtin_delattr(PyObject *self, PyObject *args)
Guido van Rossum14144fc1994-08-29 12:53:40 +00001078{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001079 PyObject *v;
1080 PyObject *name;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001081
Raymond Hettingerea3fdf42002-12-29 16:33:45 +00001082 if (!PyArg_UnpackTuple(args, "delattr", 2, 2, &v, &name))
Guido van Rossum14144fc1994-08-29 12:53:40 +00001083 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001084 if (PyObject_SetAttr(v, name, (PyObject *)NULL) != 0)
Guido van Rossum14144fc1994-08-29 12:53:40 +00001085 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001086 Py_INCREF(Py_None);
1087 return Py_None;
Guido van Rossum14144fc1994-08-29 12:53:40 +00001088}
1089
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001090PyDoc_STRVAR(delattr_doc,
Guido van Rossumdf12a591998-11-23 22:13:04 +00001091"delattr(object, name)\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001092\n\
1093Delete a named attribute on an object; delattr(x, 'y') is equivalent to\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001094``del x.y''.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001095
1096
Guido van Rossum79f25d91997-04-29 20:08:16 +00001097static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001098builtin_hash(PyObject *self, PyObject *v)
Guido van Rossum9bfef441993-03-29 10:43:31 +00001099{
Guido van Rossum9bfef441993-03-29 10:43:31 +00001100 long x;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001101
Guido van Rossum79f25d91997-04-29 20:08:16 +00001102 x = PyObject_Hash(v);
Guido van Rossum9bfef441993-03-29 10:43:31 +00001103 if (x == -1)
1104 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001105 return PyInt_FromLong(x);
Guido van Rossum9bfef441993-03-29 10:43:31 +00001106}
1107
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001108PyDoc_STRVAR(hash_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001109"hash(object) -> integer\n\
1110\n\
1111Return a hash value for the object. Two objects with the same value have\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001112the same hash value. The reverse is not necessarily true, but likely.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001113
1114
Guido van Rossum79f25d91997-04-29 20:08:16 +00001115static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001116builtin_hex(PyObject *self, PyObject *v)
Guido van Rossum006bcd41991-10-24 14:54:44 +00001117{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001118 PyNumberMethods *nb;
Neil Schemenauer3a313e32004-07-19 16:29:17 +00001119 PyObject *res;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001120
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001121 if ((nb = v->ob_type->tp_as_number) == NULL ||
Guido van Rossum1899c2e1992-09-12 11:09:23 +00001122 nb->nb_hex == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001123 PyErr_SetString(PyExc_TypeError,
Guido van Rossum1899c2e1992-09-12 11:09:23 +00001124 "hex() argument can't be converted to hex");
1125 return NULL;
Guido van Rossum006bcd41991-10-24 14:54:44 +00001126 }
Neil Schemenauer3a313e32004-07-19 16:29:17 +00001127 res = (*nb->nb_hex)(v);
1128 if (res && !PyString_Check(res)) {
1129 PyErr_Format(PyExc_TypeError,
1130 "__hex__ returned non-string (type %.200s)",
1131 res->ob_type->tp_name);
1132 Py_DECREF(res);
1133 return NULL;
1134 }
1135 return res;
Guido van Rossum006bcd41991-10-24 14:54:44 +00001136}
1137
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001138PyDoc_STRVAR(hex_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001139"hex(number) -> string\n\
1140\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001141Return the hexadecimal representation of an integer or long integer.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001142
1143
Tim Petersdbd9ba62000-07-09 03:09:57 +00001144static PyObject *builtin_raw_input(PyObject *, PyObject *);
Guido van Rossum3165fe61992-09-25 21:59:05 +00001145
Guido van Rossum79f25d91997-04-29 20:08:16 +00001146static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001147builtin_input(PyObject *self, PyObject *args)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001148{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001149 PyObject *line;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001150 char *str;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001151 PyObject *res;
1152 PyObject *globals, *locals;
Hye-Shik Changff83c2b2004-02-02 13:39:01 +00001153 PyCompilerFlags cf;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001154
1155 line = builtin_raw_input(self, args);
Guido van Rossum3165fe61992-09-25 21:59:05 +00001156 if (line == NULL)
1157 return line;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001158 if (!PyArg_Parse(line, "s;embedded '\\0' in input line", &str))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001159 return NULL;
1160 while (*str == ' ' || *str == '\t')
1161 str++;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001162 globals = PyEval_GetGlobals();
1163 locals = PyEval_GetLocals();
1164 if (PyDict_GetItemString(globals, "__builtins__") == NULL) {
1165 if (PyDict_SetItemString(globals, "__builtins__",
1166 PyEval_GetBuiltins()) != 0)
Guido van Rossum6135a871995-01-09 17:53:26 +00001167 return NULL;
1168 }
Hye-Shik Changff83c2b2004-02-02 13:39:01 +00001169 cf.cf_flags = 0;
1170 PyEval_MergeCompilerFlags(&cf);
1171 res = PyRun_StringFlags(str, Py_eval_input, globals, locals, &cf);
Guido van Rossum79f25d91997-04-29 20:08:16 +00001172 Py_DECREF(line);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001173 return res;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001174}
1175
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001176PyDoc_STRVAR(input_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001177"input([prompt]) -> value\n\
1178\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001179Equivalent to eval(raw_input(prompt)).");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001180
1181
Guido van Rossume8811f81997-02-14 15:48:05 +00001182static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001183builtin_intern(PyObject *self, PyObject *args)
Guido van Rossume8811f81997-02-14 15:48:05 +00001184{
1185 PyObject *s;
Guido van Rossum43713e52000-02-29 13:59:29 +00001186 if (!PyArg_ParseTuple(args, "S:intern", &s))
Guido van Rossume8811f81997-02-14 15:48:05 +00001187 return NULL;
Jeremy Hylton4c989dd2004-08-07 19:20:05 +00001188 if (!PyString_CheckExact(s)) {
1189 PyErr_SetString(PyExc_TypeError,
1190 "can't intern subclass of string");
1191 return NULL;
1192 }
Guido van Rossume8811f81997-02-14 15:48:05 +00001193 Py_INCREF(s);
1194 PyString_InternInPlace(&s);
1195 return s;
1196}
1197
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001198PyDoc_STRVAR(intern_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001199"intern(string) -> string\n\
1200\n\
1201``Intern'' the given string. This enters the string in the (global)\n\
1202table of interned strings whose purpose is to speed up dictionary lookups.\n\
1203Return the string itself or the previously interned string object with the\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001204same value.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001205
1206
Guido van Rossum79f25d91997-04-29 20:08:16 +00001207static PyObject *
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001208builtin_iter(PyObject *self, PyObject *args)
1209{
1210 PyObject *v, *w = NULL;
1211
Raymond Hettingerea3fdf42002-12-29 16:33:45 +00001212 if (!PyArg_UnpackTuple(args, "iter", 1, 2, &v, &w))
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001213 return NULL;
1214 if (w == NULL)
1215 return PyObject_GetIter(v);
1216 if (!PyCallable_Check(v)) {
1217 PyErr_SetString(PyExc_TypeError,
1218 "iter(v, w): v must be callable");
1219 return NULL;
1220 }
1221 return PyCallIter_New(v, w);
1222}
1223
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001224PyDoc_STRVAR(iter_doc,
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001225"iter(collection) -> iterator\n\
1226iter(callable, sentinel) -> iterator\n\
1227\n\
1228Get an iterator from an object. In the first form, the argument must\n\
1229supply its own iterator, or be a sequence.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001230In the second form, the callable is called until it returns the sentinel.");
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001231
1232
1233static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001234builtin_len(PyObject *self, PyObject *v)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001235{
Martin v. Löwis18e16552006-02-15 17:27:45 +00001236 Py_ssize_t res;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001237
Jeremy Hylton03657cf2000-07-12 13:05:33 +00001238 res = PyObject_Size(v);
Guido van Rossum8ea9f4d1998-06-29 22:26:50 +00001239 if (res < 0 && PyErr_Occurred())
1240 return NULL;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001241 return PyInt_FromSsize_t(res);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001242}
1243
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001244PyDoc_STRVAR(len_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001245"len(object) -> integer\n\
1246\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001247Return the number of items of a sequence or mapping.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001248
1249
Guido van Rossum79f25d91997-04-29 20:08:16 +00001250static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001251builtin_locals(PyObject *self)
Guido van Rossum872537c1995-07-07 22:43:42 +00001252{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001253 PyObject *d;
Guido van Rossum872537c1995-07-07 22:43:42 +00001254
Guido van Rossum79f25d91997-04-29 20:08:16 +00001255 d = PyEval_GetLocals();
Neal Norwitz43bd4db2006-08-12 01:46:42 +00001256 Py_XINCREF(d);
Guido van Rossum872537c1995-07-07 22:43:42 +00001257 return d;
1258}
1259
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001260PyDoc_STRVAR(locals_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001261"locals() -> dictionary\n\
1262\n\
Raymond Hettinger69bf8f32003-01-04 02:16:22 +00001263Update and return a dictionary containing the current scope's local variables.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001264
1265
Guido van Rossum79f25d91997-04-29 20:08:16 +00001266static PyObject *
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001267min_max(PyObject *args, PyObject *kwds, int op)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001268{
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001269 PyObject *v, *it, *item, *val, *maxitem, *maxval, *keyfunc=NULL;
Martin v. Löwisfd39ad42004-08-12 14:42:37 +00001270 const char *name = op == Py_LT ? "min" : "max";
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001271
Guido van Rossum79f25d91997-04-29 20:08:16 +00001272 if (PyTuple_Size(args) > 1)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001273 v = args;
Martin v. Löwisfd39ad42004-08-12 14:42:37 +00001274 else if (!PyArg_UnpackTuple(args, (char *)name, 1, 1, &v))
Guido van Rossum3f5da241990-12-20 15:06:42 +00001275 return NULL;
Tim Peters67d687a2002-04-29 21:27:32 +00001276
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001277 if (kwds != NULL && PyDict_Check(kwds) && PyDict_Size(kwds)) {
1278 keyfunc = PyDict_GetItemString(kwds, "key");
1279 if (PyDict_Size(kwds)!=1 || keyfunc == NULL) {
Georg Brandl99d7e4e2005-08-31 22:21:15 +00001280 PyErr_Format(PyExc_TypeError,
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001281 "%s() got an unexpected keyword argument", name);
1282 return NULL;
1283 }
Guido van Rossum1d9a9ea2008-01-23 20:19:01 +00001284 Py_INCREF(keyfunc);
Georg Brandl99d7e4e2005-08-31 22:21:15 +00001285 }
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001286
Tim Petersc3074532001-05-03 07:00:32 +00001287 it = PyObject_GetIter(v);
Guido van Rossum1d9a9ea2008-01-23 20:19:01 +00001288 if (it == NULL) {
1289 Py_XDECREF(keyfunc);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001290 return NULL;
Guido van Rossum1d9a9ea2008-01-23 20:19:01 +00001291 }
Tim Petersc3074532001-05-03 07:00:32 +00001292
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001293 maxitem = NULL; /* the result */
1294 maxval = NULL; /* the value associated with the result */
Brett Cannon9e635cf2004-12-07 00:25:35 +00001295 while (( item = PyIter_Next(it) )) {
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001296 /* get the value from the key function */
1297 if (keyfunc != NULL) {
1298 val = PyObject_CallFunctionObjArgs(keyfunc, item, NULL);
1299 if (val == NULL)
1300 goto Fail_it_item;
1301 }
1302 /* no key function; the value is the item */
1303 else {
1304 val = item;
1305 Py_INCREF(val);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001306 }
Tim Petersc3074532001-05-03 07:00:32 +00001307
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001308 /* maximum value and item are unset; set them */
1309 if (maxval == NULL) {
1310 maxitem = item;
1311 maxval = val;
1312 }
1313 /* maximum value and item are set; update them as necessary */
Guido van Rossum2d951851994-08-29 12:52:16 +00001314 else {
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001315 int cmp = PyObject_RichCompareBool(val, maxval, op);
1316 if (cmp < 0)
1317 goto Fail_it_item_and_val;
1318 else if (cmp > 0) {
1319 Py_DECREF(maxval);
1320 Py_DECREF(maxitem);
1321 maxval = val;
1322 maxitem = item;
Guido van Rossum53451b32001-01-17 15:47:24 +00001323 }
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001324 else {
1325 Py_DECREF(item);
1326 Py_DECREF(val);
Guido van Rossumc8b6df91997-05-23 00:06:51 +00001327 }
Guido van Rossum2d951851994-08-29 12:52:16 +00001328 }
Guido van Rossum3f5da241990-12-20 15:06:42 +00001329 }
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001330 if (PyErr_Occurred())
1331 goto Fail_it;
1332 if (maxval == NULL) {
Martin v. Löwisfd39ad42004-08-12 14:42:37 +00001333 PyErr_Format(PyExc_ValueError,
1334 "%s() arg is an empty sequence", name);
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001335 assert(maxitem == NULL);
1336 }
1337 else
1338 Py_DECREF(maxval);
Tim Petersc3074532001-05-03 07:00:32 +00001339 Py_DECREF(it);
Guido van Rossum1d9a9ea2008-01-23 20:19:01 +00001340 Py_XDECREF(keyfunc);
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001341 return maxitem;
1342
1343Fail_it_item_and_val:
1344 Py_DECREF(val);
1345Fail_it_item:
1346 Py_DECREF(item);
1347Fail_it:
1348 Py_XDECREF(maxval);
1349 Py_XDECREF(maxitem);
1350 Py_DECREF(it);
Guido van Rossum1d9a9ea2008-01-23 20:19:01 +00001351 Py_XDECREF(keyfunc);
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001352 return NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001353}
1354
Guido van Rossum79f25d91997-04-29 20:08:16 +00001355static PyObject *
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001356builtin_min(PyObject *self, PyObject *args, PyObject *kwds)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001357{
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001358 return min_max(args, kwds, Py_LT);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001359}
1360
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001361PyDoc_STRVAR(min_doc,
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001362"min(iterable[, key=func]) -> value\n\
1363min(a, b, c, ...[, key=func]) -> value\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001364\n\
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001365With a single iterable argument, return its smallest item.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001366With two or more arguments, return the smallest argument.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001367
1368
Guido van Rossum79f25d91997-04-29 20:08:16 +00001369static PyObject *
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001370builtin_max(PyObject *self, PyObject *args, PyObject *kwds)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001371{
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001372 return min_max(args, kwds, Py_GT);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001373}
1374
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001375PyDoc_STRVAR(max_doc,
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001376"max(iterable[, key=func]) -> value\n\
1377max(a, b, c, ...[, key=func]) -> value\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001378\n\
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001379With a single iterable argument, return its largest item.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001380With two or more arguments, return the largest argument.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001381
1382
Guido van Rossum79f25d91997-04-29 20:08:16 +00001383static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001384builtin_oct(PyObject *self, PyObject *v)
Guido van Rossum006bcd41991-10-24 14:54:44 +00001385{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001386 PyNumberMethods *nb;
Neil Schemenauer3a313e32004-07-19 16:29:17 +00001387 PyObject *res;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001388
Guido van Rossum1899c2e1992-09-12 11:09:23 +00001389 if (v == NULL || (nb = v->ob_type->tp_as_number) == NULL ||
1390 nb->nb_oct == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001391 PyErr_SetString(PyExc_TypeError,
Guido van Rossum1899c2e1992-09-12 11:09:23 +00001392 "oct() argument can't be converted to oct");
1393 return NULL;
Guido van Rossum006bcd41991-10-24 14:54:44 +00001394 }
Neil Schemenauer3a313e32004-07-19 16:29:17 +00001395 res = (*nb->nb_oct)(v);
1396 if (res && !PyString_Check(res)) {
1397 PyErr_Format(PyExc_TypeError,
1398 "__oct__ returned non-string (type %.200s)",
1399 res->ob_type->tp_name);
1400 Py_DECREF(res);
1401 return NULL;
1402 }
1403 return res;
Guido van Rossum006bcd41991-10-24 14:54:44 +00001404}
1405
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001406PyDoc_STRVAR(oct_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001407"oct(number) -> string\n\
1408\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001409Return the octal representation of an integer or long integer.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001410
1411
Guido van Rossum79f25d91997-04-29 20:08:16 +00001412static PyObject *
Neal Norwitzc4edb0e2006-05-02 04:43:14 +00001413builtin_open(PyObject *self, PyObject *args, PyObject *kwds)
1414{
1415 return PyObject_Call((PyObject*)&PyFile_Type, args, kwds);
1416}
1417
1418PyDoc_STRVAR(open_doc,
1419"open(name[, mode[, buffering]]) -> file object\n\
1420\n\
Skip Montanaro4e3ebe02007-12-08 14:37:43 +00001421Open a file using the file() type, returns a file object. This is the\n\
1422preferred way to open a file.");
Neal Norwitzc4edb0e2006-05-02 04:43:14 +00001423
1424
1425static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001426builtin_ord(PyObject *self, PyObject* obj)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001427{
Guido van Rossum09095f32000-03-10 23:00:52 +00001428 long ord;
Martin v. Löwisd96ee902006-02-16 14:37:16 +00001429 Py_ssize_t size;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001430
Jeremy Hylton394b54d2000-04-12 21:19:47 +00001431 if (PyString_Check(obj)) {
1432 size = PyString_GET_SIZE(obj);
Andrew M. Kuchling34c20cf2000-12-20 14:36:56 +00001433 if (size == 1) {
Jeremy Hylton394b54d2000-04-12 21:19:47 +00001434 ord = (long)((unsigned char)*PyString_AS_STRING(obj));
Andrew M. Kuchling34c20cf2000-12-20 14:36:56 +00001435 return PyInt_FromLong(ord);
1436 }
Martin v. Löwis339d0f72001-08-17 18:39:25 +00001437#ifdef Py_USING_UNICODE
Jeremy Hylton394b54d2000-04-12 21:19:47 +00001438 } else if (PyUnicode_Check(obj)) {
1439 size = PyUnicode_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)*PyUnicode_AS_UNICODE(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#endif
Jeremy Hylton394b54d2000-04-12 21:19:47 +00001445 } else {
1446 PyErr_Format(PyExc_TypeError,
Andrew M. Kuchlingf07aad12000-12-23 14:11:28 +00001447 "ord() expected string of length 1, but " \
Jeremy Hylton394b54d2000-04-12 21:19:47 +00001448 "%.200s found", obj->ob_type->tp_name);
Guido van Rossum09095f32000-03-10 23:00:52 +00001449 return NULL;
1450 }
1451
Guido van Rossumad991772001-01-12 16:03:05 +00001452 PyErr_Format(PyExc_TypeError,
Andrew M. Kuchlingf07aad12000-12-23 14:11:28 +00001453 "ord() expected a character, "
Martin v. Löwisd96ee902006-02-16 14:37:16 +00001454 "but string of length %zd found",
Jeremy Hylton394b54d2000-04-12 21:19:47 +00001455 size);
1456 return NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001457}
1458
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001459PyDoc_STRVAR(ord_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001460"ord(c) -> integer\n\
1461\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001462Return the integer ordinal of a one-character string.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001463
1464
Guido van Rossum79f25d91997-04-29 20:08:16 +00001465static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001466builtin_pow(PyObject *self, PyObject *args)
Guido van Rossum6a00cd81995-01-07 12:39:01 +00001467{
Guido van Rossum09df08a1998-05-22 00:51:39 +00001468 PyObject *v, *w, *z = Py_None;
Guido van Rossum6a00cd81995-01-07 12:39:01 +00001469
Raymond Hettingerea3fdf42002-12-29 16:33:45 +00001470 if (!PyArg_UnpackTuple(args, "pow", 2, 3, &v, &w, &z))
Guido van Rossum6a00cd81995-01-07 12:39:01 +00001471 return NULL;
Guido van Rossum09df08a1998-05-22 00:51:39 +00001472 return PyNumber_Power(v, w, z);
Guido van Rossumd4905451991-05-05 20:00:36 +00001473}
1474
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001475PyDoc_STRVAR(pow_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001476"pow(x, y[, z]) -> number\n\
1477\n\
1478With two arguments, equivalent to x**y. With three arguments,\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001479equivalent to (x**y) % z, but may be more efficient (e.g. for longs).");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001480
1481
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001482
1483/* Return number of items in range (lo, hi, step), when arguments are
1484 * PyInt or PyLong objects. step > 0 required. Return a value < 0 if
1485 * & only if the true value is too large to fit in a signed long.
1486 * Arguments MUST return 1 with either PyInt_Check() or
1487 * PyLong_Check(). Return -1 when there is an error.
1488 */
1489static long
1490get_len_of_range_longs(PyObject *lo, PyObject *hi, PyObject *step)
1491{
1492 /* -------------------------------------------------------------
1493 Algorithm is equal to that of get_len_of_range(), but it operates
1494 on PyObjects (which are assumed to be PyLong or PyInt objects).
1495 ---------------------------------------------------------------*/
1496 long n;
1497 PyObject *diff = NULL;
1498 PyObject *one = NULL;
1499 PyObject *tmp1 = NULL, *tmp2 = NULL, *tmp3 = NULL;
1500 /* holds sub-expression evaluations */
1501
1502 /* if (lo >= hi), return length of 0. */
1503 if (PyObject_Compare(lo, hi) >= 0)
1504 return 0;
1505
1506 if ((one = PyLong_FromLong(1L)) == NULL)
1507 goto Fail;
1508
1509 if ((tmp1 = PyNumber_Subtract(hi, lo)) == NULL)
1510 goto Fail;
1511
1512 if ((diff = PyNumber_Subtract(tmp1, one)) == NULL)
1513 goto Fail;
1514
1515 if ((tmp2 = PyNumber_FloorDivide(diff, step)) == NULL)
1516 goto Fail;
1517
1518 if ((tmp3 = PyNumber_Add(tmp2, one)) == NULL)
1519 goto Fail;
1520
1521 n = PyLong_AsLong(tmp3);
1522 if (PyErr_Occurred()) { /* Check for Overflow */
1523 PyErr_Clear();
1524 goto Fail;
1525 }
1526
1527 Py_DECREF(tmp3);
1528 Py_DECREF(tmp2);
1529 Py_DECREF(diff);
1530 Py_DECREF(tmp1);
1531 Py_DECREF(one);
1532 return n;
1533
1534 Fail:
1535 Py_XDECREF(tmp3);
1536 Py_XDECREF(tmp2);
1537 Py_XDECREF(diff);
1538 Py_XDECREF(tmp1);
1539 Py_XDECREF(one);
1540 return -1;
1541}
1542
1543/* An extension of builtin_range() that handles the case when PyLong
1544 * arguments are given. */
1545static PyObject *
1546handle_range_longs(PyObject *self, PyObject *args)
1547{
1548 PyObject *ilow;
Tim Peters874e1f72003-04-13 22:13:08 +00001549 PyObject *ihigh = NULL;
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001550 PyObject *istep = NULL;
Tim Peters874e1f72003-04-13 22:13:08 +00001551
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001552 PyObject *curnum = NULL;
1553 PyObject *v = NULL;
1554 long bign;
1555 int i, n;
1556 int cmp_result;
1557
Tim Peters874e1f72003-04-13 22:13:08 +00001558 PyObject *zero = PyLong_FromLong(0);
1559
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001560 if (zero == NULL)
1561 return NULL;
1562
Tim Peters874e1f72003-04-13 22:13:08 +00001563 if (!PyArg_UnpackTuple(args, "range", 1, 3, &ilow, &ihigh, &istep)) {
1564 Py_DECREF(zero);
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001565 return NULL;
1566 }
1567
Tim Peters874e1f72003-04-13 22:13:08 +00001568 /* Figure out which way we were called, supply defaults, and be
1569 * sure to incref everything so that the decrefs at the end
1570 * are correct.
1571 */
1572 assert(ilow != NULL);
1573 if (ihigh == NULL) {
1574 /* only 1 arg -- it's the upper limit */
1575 ihigh = ilow;
1576 ilow = NULL;
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001577 }
Tim Peters874e1f72003-04-13 22:13:08 +00001578 assert(ihigh != NULL);
1579 Py_INCREF(ihigh);
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001580
Tim Peters874e1f72003-04-13 22:13:08 +00001581 /* ihigh correct now; do ilow */
1582 if (ilow == NULL)
1583 ilow = zero;
1584 Py_INCREF(ilow);
1585
1586 /* ilow and ihigh correct now; do istep */
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001587 if (istep == NULL) {
1588 istep = PyLong_FromLong(1L);
1589 if (istep == NULL)
1590 goto Fail;
1591 }
1592 else {
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001593 Py_INCREF(istep);
1594 }
1595
Tim Peters874e1f72003-04-13 22:13:08 +00001596 if (!PyInt_Check(ilow) && !PyLong_Check(ilow)) {
Guido van Rossum28e83e32003-04-15 12:43:26 +00001597 PyErr_Format(PyExc_TypeError,
Alex Martellif471d472003-04-23 13:00:44 +00001598 "range() integer start argument expected, got %s.",
Guido van Rossum817d6c92003-04-14 18:25:04 +00001599 ilow->ob_type->tp_name);
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001600 goto Fail;
1601 }
1602
Tim Peters874e1f72003-04-13 22:13:08 +00001603 if (!PyInt_Check(ihigh) && !PyLong_Check(ihigh)) {
Guido van Rossum28e83e32003-04-15 12:43:26 +00001604 PyErr_Format(PyExc_TypeError,
Alex Martellif471d472003-04-23 13:00:44 +00001605 "range() integer end argument expected, got %s.",
Guido van Rossum817d6c92003-04-14 18:25:04 +00001606 ihigh->ob_type->tp_name);
Tim Peters874e1f72003-04-13 22:13:08 +00001607 goto Fail;
1608 }
1609
1610 if (!PyInt_Check(istep) && !PyLong_Check(istep)) {
Guido van Rossum28e83e32003-04-15 12:43:26 +00001611 PyErr_Format(PyExc_TypeError,
Alex Martellif471d472003-04-23 13:00:44 +00001612 "range() integer step argument expected, got %s.",
Guido van Rossum817d6c92003-04-14 18:25:04 +00001613 istep->ob_type->tp_name);
Tim Peters874e1f72003-04-13 22:13:08 +00001614 goto Fail;
1615 }
1616
1617 if (PyObject_Cmp(istep, zero, &cmp_result) == -1)
1618 goto Fail;
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001619 if (cmp_result == 0) {
1620 PyErr_SetString(PyExc_ValueError,
Alex Martellif471d472003-04-23 13:00:44 +00001621 "range() step argument must not be zero");
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001622 goto Fail;
1623 }
1624
1625 if (cmp_result > 0)
1626 bign = get_len_of_range_longs(ilow, ihigh, istep);
1627 else {
1628 PyObject *neg_istep = PyNumber_Negative(istep);
1629 if (neg_istep == NULL)
1630 goto Fail;
1631 bign = get_len_of_range_longs(ihigh, ilow, neg_istep);
1632 Py_DECREF(neg_istep);
1633 }
1634
1635 n = (int)bign;
1636 if (bign < 0 || (long)n != bign) {
1637 PyErr_SetString(PyExc_OverflowError,
1638 "range() result has too many items");
1639 goto Fail;
1640 }
1641
1642 v = PyList_New(n);
1643 if (v == NULL)
1644 goto Fail;
1645
1646 curnum = ilow;
1647 Py_INCREF(curnum);
1648
1649 for (i = 0; i < n; i++) {
1650 PyObject *w = PyNumber_Long(curnum);
1651 PyObject *tmp_num;
1652 if (w == NULL)
1653 goto Fail;
1654
1655 PyList_SET_ITEM(v, i, w);
1656
1657 tmp_num = PyNumber_Add(curnum, istep);
1658 if (tmp_num == NULL)
1659 goto Fail;
1660
1661 Py_DECREF(curnum);
1662 curnum = tmp_num;
1663 }
Tim Peters874e1f72003-04-13 22:13:08 +00001664 Py_DECREF(ilow);
1665 Py_DECREF(ihigh);
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001666 Py_DECREF(istep);
1667 Py_DECREF(zero);
Tim Peters874e1f72003-04-13 22:13:08 +00001668 Py_DECREF(curnum);
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001669 return v;
1670
1671 Fail:
Tim Peters874e1f72003-04-13 22:13:08 +00001672 Py_DECREF(ilow);
1673 Py_DECREF(ihigh);
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001674 Py_XDECREF(istep);
Tim Peters874e1f72003-04-13 22:13:08 +00001675 Py_DECREF(zero);
1676 Py_XDECREF(curnum);
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001677 Py_XDECREF(v);
1678 return NULL;
1679}
1680
Guido van Rossum124eff01999-02-23 16:11:01 +00001681/* Return number of items in range/xrange (lo, hi, step). step > 0
1682 * required. Return a value < 0 if & only if the true value is too
1683 * large to fit in a signed long.
1684 */
1685static long
Thomas Wouterse28c2962000-07-23 22:21:32 +00001686get_len_of_range(long lo, long hi, long step)
Guido van Rossum124eff01999-02-23 16:11:01 +00001687{
1688 /* -------------------------------------------------------------
1689 If lo >= hi, the range is empty.
1690 Else if n values are in the range, the last one is
1691 lo + (n-1)*step, which must be <= hi-1. Rearranging,
1692 n <= (hi - lo - 1)/step + 1, so taking the floor of the RHS gives
1693 the proper value. Since lo < hi in this case, hi-lo-1 >= 0, so
1694 the RHS is non-negative and so truncation is the same as the
1695 floor. Letting M be the largest positive long, the worst case
1696 for the RHS numerator is hi=M, lo=-M-1, and then
1697 hi-lo-1 = M-(-M-1)-1 = 2*M. Therefore unsigned long has enough
1698 precision to compute the RHS exactly.
1699 ---------------------------------------------------------------*/
1700 long n = 0;
1701 if (lo < hi) {
1702 unsigned long uhi = (unsigned long)hi;
1703 unsigned long ulo = (unsigned long)lo;
1704 unsigned long diff = uhi - ulo - 1;
1705 n = (long)(diff / (unsigned long)step + 1);
1706 }
1707 return n;
1708}
1709
Guido van Rossum79f25d91997-04-29 20:08:16 +00001710static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001711builtin_range(PyObject *self, PyObject *args)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001712{
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001713 long ilow = 0, ihigh = 0, istep = 1;
Guido van Rossum124eff01999-02-23 16:11:01 +00001714 long bign;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001715 int i, n;
Guido van Rossum124eff01999-02-23 16:11:01 +00001716
Guido van Rossum79f25d91997-04-29 20:08:16 +00001717 PyObject *v;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001718
Guido van Rossum79f25d91997-04-29 20:08:16 +00001719 if (PyTuple_Size(args) <= 1) {
1720 if (!PyArg_ParseTuple(args,
Guido van Rossum0865dd91995-01-17 16:30:22 +00001721 "l;range() requires 1-3 int arguments",
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001722 &ihigh)) {
1723 PyErr_Clear();
1724 return handle_range_longs(self, args);
1725 }
Guido van Rossum3f5da241990-12-20 15:06:42 +00001726 }
1727 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001728 if (!PyArg_ParseTuple(args,
Guido van Rossum0865dd91995-01-17 16:30:22 +00001729 "ll|l;range() requires 1-3 int arguments",
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001730 &ilow, &ihigh, &istep)) {
1731 PyErr_Clear();
1732 return handle_range_longs(self, args);
1733 }
Guido van Rossum3f5da241990-12-20 15:06:42 +00001734 }
1735 if (istep == 0) {
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001736 PyErr_SetString(PyExc_ValueError,
Alex Martellif471d472003-04-23 13:00:44 +00001737 "range() step argument must not be zero");
Guido van Rossum3f5da241990-12-20 15:06:42 +00001738 return NULL;
1739 }
Guido van Rossum124eff01999-02-23 16:11:01 +00001740 if (istep > 0)
1741 bign = get_len_of_range(ilow, ihigh, istep);
1742 else
1743 bign = get_len_of_range(ihigh, ilow, -istep);
1744 n = (int)bign;
1745 if (bign < 0 || (long)n != bign) {
Guido van Rossume23cde21999-01-12 05:07:47 +00001746 PyErr_SetString(PyExc_OverflowError,
Fred Drake661ea262000-10-24 19:57:45 +00001747 "range() result has too many items");
Guido van Rossume23cde21999-01-12 05:07:47 +00001748 return NULL;
1749 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00001750 v = PyList_New(n);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001751 if (v == NULL)
1752 return NULL;
1753 for (i = 0; i < n; i++) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001754 PyObject *w = PyInt_FromLong(ilow);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001755 if (w == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001756 Py_DECREF(v);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001757 return NULL;
1758 }
Guido van Rossuma937d141998-04-24 18:22:02 +00001759 PyList_SET_ITEM(v, i, w);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001760 ilow += istep;
1761 }
1762 return v;
1763}
1764
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001765PyDoc_STRVAR(range_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001766"range([start,] stop[, step]) -> list of integers\n\
1767\n\
1768Return a list containing an arithmetic progression of integers.\n\
1769range(i, j) returns [i, i+1, i+2, ..., j-1]; start (!) defaults to 0.\n\
1770When step is given, it specifies the increment (or decrement).\n\
1771For example, range(4) returns [0, 1, 2, 3]. The end point is omitted!\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001772These are exactly the valid indices for a list of 4 elements.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001773
1774
Guido van Rossum79f25d91997-04-29 20:08:16 +00001775static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001776builtin_raw_input(PyObject *self, PyObject *args)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001777{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001778 PyObject *v = NULL;
Martin v. Löwis31d2df52002-08-14 15:46:02 +00001779 PyObject *fin = PySys_GetObject("stdin");
1780 PyObject *fout = PySys_GetObject("stdout");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001781
Raymond Hettingerea3fdf42002-12-29 16:33:45 +00001782 if (!PyArg_UnpackTuple(args, "[raw_]input", 0, 1, &v))
Guido van Rossum3165fe61992-09-25 21:59:05 +00001783 return NULL;
Martin v. Löwis31d2df52002-08-14 15:46:02 +00001784
1785 if (fin == NULL) {
Alex Martellia9b9c9f2003-04-23 13:34:35 +00001786 PyErr_SetString(PyExc_RuntimeError, "[raw_]input: lost sys.stdin");
Martin v. Löwis31d2df52002-08-14 15:46:02 +00001787 return NULL;
1788 }
1789 if (fout == NULL) {
Alex Martellia9b9c9f2003-04-23 13:34:35 +00001790 PyErr_SetString(PyExc_RuntimeError, "[raw_]input: lost sys.stdout");
Martin v. Löwis31d2df52002-08-14 15:46:02 +00001791 return NULL;
1792 }
1793 if (PyFile_SoftSpace(fout, 0)) {
1794 if (PyFile_WriteString(" ", fout) != 0)
1795 return NULL;
1796 }
Georg Brandl7e3ba2a2006-08-06 08:23:54 +00001797 if (PyFile_AsFile(fin) && PyFile_AsFile(fout)
Martin v. Löwis566f6af2002-10-26 14:39:10 +00001798 && isatty(fileno(PyFile_AsFile(fin)))
1799 && isatty(fileno(PyFile_AsFile(fout)))) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001800 PyObject *po;
Guido van Rossum872537c1995-07-07 22:43:42 +00001801 char *prompt;
1802 char *s;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001803 PyObject *result;
Guido van Rossum872537c1995-07-07 22:43:42 +00001804 if (v != NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001805 po = PyObject_Str(v);
Guido van Rossum872537c1995-07-07 22:43:42 +00001806 if (po == NULL)
1807 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001808 prompt = PyString_AsString(po);
Guido van Rossumd9b52081998-06-26 18:25:38 +00001809 if (prompt == NULL)
1810 return NULL;
Guido van Rossum872537c1995-07-07 22:43:42 +00001811 }
1812 else {
1813 po = NULL;
1814 prompt = "";
1815 }
Michael W. Hudson52db5192004-08-02 13:21:09 +00001816 s = PyOS_Readline(PyFile_AsFile(fin), PyFile_AsFile(fout),
Martin v. Löwis566f6af2002-10-26 14:39:10 +00001817 prompt);
Guido van Rossum79f25d91997-04-29 20:08:16 +00001818 Py_XDECREF(po);
Guido van Rossum872537c1995-07-07 22:43:42 +00001819 if (s == NULL) {
Michael W. Hudson30ea2f22004-07-07 17:44:12 +00001820 if (!PyErr_Occurred())
1821 PyErr_SetNone(PyExc_KeyboardInterrupt);
Guido van Rossum872537c1995-07-07 22:43:42 +00001822 return NULL;
1823 }
1824 if (*s == '\0') {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001825 PyErr_SetNone(PyExc_EOFError);
Guido van Rossum872537c1995-07-07 22:43:42 +00001826 result = NULL;
1827 }
1828 else { /* strip trailing '\n' */
Guido van Rossum106f2da2000-06-28 21:12:25 +00001829 size_t len = strlen(s);
Martin v. Löwisb1ed7fa2006-04-13 07:52:27 +00001830 if (len > PY_SSIZE_T_MAX) {
Martin v. Löwis31d2df52002-08-14 15:46:02 +00001831 PyErr_SetString(PyExc_OverflowError,
Alex Martellia9b9c9f2003-04-23 13:34:35 +00001832 "[raw_]input: input too long");
Guido van Rossum106f2da2000-06-28 21:12:25 +00001833 result = NULL;
1834 }
1835 else {
Martin v. Löwisb1ed7fa2006-04-13 07:52:27 +00001836 result = PyString_FromStringAndSize(s, len-1);
Guido van Rossum106f2da2000-06-28 21:12:25 +00001837 }
Guido van Rossum872537c1995-07-07 22:43:42 +00001838 }
Guido van Rossumb18618d2000-05-03 23:44:39 +00001839 PyMem_FREE(s);
Guido van Rossum872537c1995-07-07 22:43:42 +00001840 return result;
1841 }
Guido van Rossum90933611991-06-07 16:10:43 +00001842 if (v != NULL) {
Martin v. Löwis31d2df52002-08-14 15:46:02 +00001843 if (PyFile_WriteObject(v, fout, Py_PRINT_RAW) != 0)
Guido van Rossum90933611991-06-07 16:10:43 +00001844 return NULL;
1845 }
Martin v. Löwis31d2df52002-08-14 15:46:02 +00001846 return PyFile_GetLine(fin, -1);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001847}
1848
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001849PyDoc_STRVAR(raw_input_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001850"raw_input([prompt]) -> string\n\
1851\n\
1852Read a string from standard input. The trailing newline is stripped.\n\
1853If the user hits EOF (Unix: Ctl-D, Windows: Ctl-Z+Return), raise EOFError.\n\
1854On Unix, GNU readline is used if enabled. The prompt string, if given,\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001855is printed without a trailing newline before reading.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001856
1857
Guido van Rossum79f25d91997-04-29 20:08:16 +00001858static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001859builtin_reduce(PyObject *self, PyObject *args)
Guido van Rossum12d12c51993-10-26 17:58:25 +00001860{
Tim Peters15d81ef2001-05-04 04:39:21 +00001861 PyObject *seq, *func, *result = NULL, *it;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001862
Neal Norwitzdf25efe2007-05-23 06:58:36 +00001863 if (Py_Py3kWarningFlag &&
1864 PyErr_Warn(PyExc_DeprecationWarning,
1865 "reduce() not supported in 3.x") < 0)
1866 return NULL;
1867
Raymond Hettingerea3fdf42002-12-29 16:33:45 +00001868 if (!PyArg_UnpackTuple(args, "reduce", 2, 3, &func, &seq, &result))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001869 return NULL;
1870 if (result != NULL)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001871 Py_INCREF(result);
Guido van Rossum12d12c51993-10-26 17:58:25 +00001872
Tim Peters15d81ef2001-05-04 04:39:21 +00001873 it = PyObject_GetIter(seq);
1874 if (it == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001875 PyErr_SetString(PyExc_TypeError,
Tim Peters15d81ef2001-05-04 04:39:21 +00001876 "reduce() arg 2 must support iteration");
1877 Py_XDECREF(result);
Guido van Rossum12d12c51993-10-26 17:58:25 +00001878 return NULL;
1879 }
1880
Guido van Rossum79f25d91997-04-29 20:08:16 +00001881 if ((args = PyTuple_New(2)) == NULL)
Guido van Rossum2d951851994-08-29 12:52:16 +00001882 goto Fail;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001883
Tim Peters15d81ef2001-05-04 04:39:21 +00001884 for (;;) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001885 PyObject *op2;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001886
1887 if (args->ob_refcnt > 1) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001888 Py_DECREF(args);
1889 if ((args = PyTuple_New(2)) == NULL)
Guido van Rossum2d951851994-08-29 12:52:16 +00001890 goto Fail;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001891 }
1892
Tim Peters15d81ef2001-05-04 04:39:21 +00001893 op2 = PyIter_Next(it);
1894 if (op2 == NULL) {
Tim Petersf4848da2001-05-05 00:14:56 +00001895 if (PyErr_Occurred())
1896 goto Fail;
1897 break;
Guido van Rossum2d951851994-08-29 12:52:16 +00001898 }
Guido van Rossum12d12c51993-10-26 17:58:25 +00001899
Guido van Rossum2d951851994-08-29 12:52:16 +00001900 if (result == NULL)
1901 result = op2;
1902 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001903 PyTuple_SetItem(args, 0, result);
1904 PyTuple_SetItem(args, 1, op2);
1905 if ((result = PyEval_CallObject(func, args)) == NULL)
Guido van Rossum2d951851994-08-29 12:52:16 +00001906 goto Fail;
1907 }
Guido van Rossum12d12c51993-10-26 17:58:25 +00001908 }
1909
Guido van Rossum79f25d91997-04-29 20:08:16 +00001910 Py_DECREF(args);
Guido van Rossum12d12c51993-10-26 17:58:25 +00001911
Guido van Rossum2d951851994-08-29 12:52:16 +00001912 if (result == NULL)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001913 PyErr_SetString(PyExc_TypeError,
Fred Drake661ea262000-10-24 19:57:45 +00001914 "reduce() of empty sequence with no initial value");
Guido van Rossum2d951851994-08-29 12:52:16 +00001915
Tim Peters15d81ef2001-05-04 04:39:21 +00001916 Py_DECREF(it);
Guido van Rossum12d12c51993-10-26 17:58:25 +00001917 return result;
1918
Guido van Rossum2d951851994-08-29 12:52:16 +00001919Fail:
Guido van Rossum79f25d91997-04-29 20:08:16 +00001920 Py_XDECREF(args);
1921 Py_XDECREF(result);
Tim Peters15d81ef2001-05-04 04:39:21 +00001922 Py_DECREF(it);
Guido van Rossum12d12c51993-10-26 17:58:25 +00001923 return NULL;
1924}
1925
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001926PyDoc_STRVAR(reduce_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001927"reduce(function, sequence[, initial]) -> value\n\
1928\n\
1929Apply a function of two arguments cumulatively to the items of a sequence,\n\
1930from left to right, so as to reduce the sequence to a single value.\n\
1931For example, reduce(lambda x, y: x+y, [1, 2, 3, 4, 5]) calculates\n\
1932((((1+2)+3)+4)+5). If initial is present, it is placed before the items\n\
1933of the sequence in the calculation, and serves as a default when the\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001934sequence is empty.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001935
1936
Guido van Rossum79f25d91997-04-29 20:08:16 +00001937static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001938builtin_reload(PyObject *self, PyObject *v)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001939{
Neal Norwitzdf25efe2007-05-23 06:58:36 +00001940 if (Py_Py3kWarningFlag &&
1941 PyErr_Warn(PyExc_DeprecationWarning,
1942 "reload() not supported in 3.x") < 0)
1943 return NULL;
1944
Guido van Rossum79f25d91997-04-29 20:08:16 +00001945 return PyImport_ReloadModule(v);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001946}
1947
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001948PyDoc_STRVAR(reload_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001949"reload(module) -> module\n\
1950\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001951Reload the module. The module must have been successfully imported before.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001952
1953
Guido van Rossum79f25d91997-04-29 20:08:16 +00001954static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001955builtin_repr(PyObject *self, PyObject *v)
Guido van Rossumc89705d1992-11-26 08:54:07 +00001956{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001957 return PyObject_Repr(v);
Guido van Rossumc89705d1992-11-26 08:54:07 +00001958}
1959
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001960PyDoc_STRVAR(repr_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001961"repr(object) -> string\n\
1962\n\
1963Return the canonical string representation of the object.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001964For most object types, eval(repr(object)) == object.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001965
1966
Guido van Rossum79f25d91997-04-29 20:08:16 +00001967static PyObject *
Georg Brandlccadf842006-03-31 18:54:53 +00001968builtin_round(PyObject *self, PyObject *args, PyObject *kwds)
Guido van Rossum9e51f9b1993-02-12 16:29:05 +00001969{
Jeffrey Yasskin9871d8f2008-01-05 08:47:13 +00001970 double number;
1971 double f;
1972 int ndigits = 0;
1973 int i;
Georg Brandlccadf842006-03-31 18:54:53 +00001974 static char *kwlist[] = {"number", "ndigits", 0};
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001975
Jeffrey Yasskin9871d8f2008-01-05 08:47:13 +00001976 if (!PyArg_ParseTupleAndKeywords(args, kwds, "d|i:round",
1977 kwlist, &number, &ndigits))
1978 return NULL;
1979 f = 1.0;
1980 i = abs(ndigits);
1981 while (--i >= 0)
1982 f = f*10.0;
1983 if (ndigits < 0)
1984 number /= f;
1985 else
1986 number *= f;
1987 if (number >= 0.0)
1988 number = floor(number + 0.5);
1989 else
1990 number = ceil(number - 0.5);
1991 if (ndigits < 0)
1992 number *= f;
1993 else
1994 number /= f;
1995 return PyFloat_FromDouble(number);
Guido van Rossum9e51f9b1993-02-12 16:29:05 +00001996}
1997
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001998PyDoc_STRVAR(round_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001999"round(number[, ndigits]) -> floating point number\n\
2000\n\
2001Round a number to a given precision in decimal digits (default 0 digits).\n\
Jeffrey Yasskin9871d8f2008-01-05 08:47:13 +00002002This always returns a floating point number. Precision may be negative.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002003
Raymond Hettinger64958a12003-12-17 20:43:33 +00002004static PyObject *
2005builtin_sorted(PyObject *self, PyObject *args, PyObject *kwds)
2006{
2007 PyObject *newlist, *v, *seq, *compare=NULL, *keyfunc=NULL, *newargs;
2008 PyObject *callable;
Martin v. Löwis15e62742006-02-27 16:46:16 +00002009 static char *kwlist[] = {"iterable", "cmp", "key", "reverse", 0};
Neal Norwitz6f0d4792005-12-15 06:40:36 +00002010 int reverse;
Raymond Hettinger64958a12003-12-17 20:43:33 +00002011
Neal Norwitz6f0d4792005-12-15 06:40:36 +00002012 /* args 1-4 should match listsort in Objects/listobject.c */
Armin Rigo71d7e702005-09-20 18:13:03 +00002013 if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|OOi:sorted",
2014 kwlist, &seq, &compare, &keyfunc, &reverse))
2015 return NULL;
Raymond Hettinger64958a12003-12-17 20:43:33 +00002016
2017 newlist = PySequence_List(seq);
2018 if (newlist == NULL)
2019 return NULL;
2020
2021 callable = PyObject_GetAttrString(newlist, "sort");
2022 if (callable == NULL) {
2023 Py_DECREF(newlist);
2024 return NULL;
2025 }
Georg Brandl99d7e4e2005-08-31 22:21:15 +00002026
Raymond Hettinger64958a12003-12-17 20:43:33 +00002027 newargs = PyTuple_GetSlice(args, 1, 4);
2028 if (newargs == NULL) {
2029 Py_DECREF(newlist);
2030 Py_DECREF(callable);
2031 return NULL;
2032 }
2033
2034 v = PyObject_Call(callable, newargs, kwds);
2035 Py_DECREF(newargs);
2036 Py_DECREF(callable);
2037 if (v == NULL) {
2038 Py_DECREF(newlist);
2039 return NULL;
2040 }
2041 Py_DECREF(v);
2042 return newlist;
2043}
2044
2045PyDoc_STRVAR(sorted_doc,
2046"sorted(iterable, cmp=None, key=None, reverse=False) --> new sorted list");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002047
Guido van Rossum79f25d91997-04-29 20:08:16 +00002048static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002049builtin_vars(PyObject *self, PyObject *args)
Guido van Rossum2d951851994-08-29 12:52:16 +00002050{
Guido van Rossum79f25d91997-04-29 20:08:16 +00002051 PyObject *v = NULL;
2052 PyObject *d;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002053
Raymond Hettingerea3fdf42002-12-29 16:33:45 +00002054 if (!PyArg_UnpackTuple(args, "vars", 0, 1, &v))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002055 return NULL;
Guido van Rossum2d951851994-08-29 12:52:16 +00002056 if (v == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002057 d = PyEval_GetLocals();
Guido van Rossum53bb7ff1995-07-26 16:26:31 +00002058 if (d == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002059 if (!PyErr_Occurred())
2060 PyErr_SetString(PyExc_SystemError,
Alex Martellia9b9c9f2003-04-23 13:34:35 +00002061 "vars(): no locals!?");
Guido van Rossum53bb7ff1995-07-26 16:26:31 +00002062 }
2063 else
Guido van Rossum79f25d91997-04-29 20:08:16 +00002064 Py_INCREF(d);
Guido van Rossum2d951851994-08-29 12:52:16 +00002065 }
2066 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002067 d = PyObject_GetAttrString(v, "__dict__");
Guido van Rossum2d951851994-08-29 12:52:16 +00002068 if (d == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002069 PyErr_SetString(PyExc_TypeError,
Guido van Rossum2d951851994-08-29 12:52:16 +00002070 "vars() argument must have __dict__ attribute");
2071 return NULL;
2072 }
2073 }
2074 return d;
2075}
2076
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002077PyDoc_STRVAR(vars_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002078"vars([object]) -> dictionary\n\
2079\n\
2080Without arguments, equivalent to locals().\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002081With an argument, equivalent to object.__dict__.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002082
Alex Martellia70b1912003-04-22 08:12:33 +00002083
2084static PyObject*
2085builtin_sum(PyObject *self, PyObject *args)
2086{
2087 PyObject *seq;
2088 PyObject *result = NULL;
2089 PyObject *temp, *item, *iter;
2090
Raymond Hettinger5cf63942003-10-25 06:41:37 +00002091 if (!PyArg_UnpackTuple(args, "sum", 1, 2, &seq, &result))
Alex Martellia70b1912003-04-22 08:12:33 +00002092 return NULL;
2093
2094 iter = PyObject_GetIter(seq);
2095 if (iter == NULL)
2096 return NULL;
2097
2098 if (result == NULL) {
2099 result = PyInt_FromLong(0);
2100 if (result == NULL) {
2101 Py_DECREF(iter);
2102 return NULL;
2103 }
2104 } else {
2105 /* reject string values for 'start' parameter */
2106 if (PyObject_TypeCheck(result, &PyBaseString_Type)) {
2107 PyErr_SetString(PyExc_TypeError,
Alex Martellia9b9c9f2003-04-23 13:34:35 +00002108 "sum() can't sum strings [use ''.join(seq) instead]");
Alex Martellia70b1912003-04-22 08:12:33 +00002109 Py_DECREF(iter);
2110 return NULL;
2111 }
Alex Martelli41c9f882003-04-22 09:24:48 +00002112 Py_INCREF(result);
Alex Martellia70b1912003-04-22 08:12:33 +00002113 }
2114
Raymond Hettinger3f8caa32007-10-24 01:28:33 +00002115#ifndef SLOW_SUM
2116 /* Fast addition by keeping temporary sums in C instead of new Python objects.
2117 Assumes all inputs are the same type. If the assumption fails, default
2118 to the more general routine.
2119 */
2120 if (PyInt_CheckExact(result)) {
2121 long i_result = PyInt_AS_LONG(result);
2122 Py_DECREF(result);
2123 result = NULL;
2124 while(result == NULL) {
2125 item = PyIter_Next(iter);
2126 if (item == NULL) {
2127 Py_DECREF(iter);
2128 if (PyErr_Occurred())
2129 return NULL;
2130 return PyInt_FromLong(i_result);
2131 }
2132 if (PyInt_CheckExact(item)) {
2133 long b = PyInt_AS_LONG(item);
2134 long x = i_result + b;
2135 if ((x^i_result) >= 0 || (x^b) >= 0) {
2136 i_result = x;
2137 Py_DECREF(item);
2138 continue;
2139 }
2140 }
2141 /* Either overflowed or is not an int. Restore real objects and process normally */
2142 result = PyInt_FromLong(i_result);
2143 temp = PyNumber_Add(result, item);
2144 Py_DECREF(result);
2145 Py_DECREF(item);
2146 result = temp;
2147 if (result == NULL) {
2148 Py_DECREF(iter);
2149 return NULL;
2150 }
2151 }
2152 }
2153
2154 if (PyFloat_CheckExact(result)) {
2155 double f_result = PyFloat_AS_DOUBLE(result);
2156 Py_DECREF(result);
2157 result = NULL;
2158 while(result == NULL) {
2159 item = PyIter_Next(iter);
2160 if (item == NULL) {
2161 Py_DECREF(iter);
2162 if (PyErr_Occurred())
2163 return NULL;
2164 return PyFloat_FromDouble(f_result);
2165 }
2166 if (PyFloat_CheckExact(item)) {
2167 PyFPE_START_PROTECT("add", return 0)
2168 f_result += PyFloat_AS_DOUBLE(item);
Raymond Hettinger3a8daf52007-10-24 02:05:51 +00002169 PyFPE_END_PROTECT(f_result)
Raymond Hettingera45c4872007-10-25 02:26:58 +00002170 Py_DECREF(item);
Raymond Hettinger3a8daf52007-10-24 02:05:51 +00002171 continue;
2172 }
2173 if (PyInt_CheckExact(item)) {
2174 PyFPE_START_PROTECT("add", return 0)
2175 f_result += (double)PyInt_AS_LONG(item);
2176 PyFPE_END_PROTECT(f_result)
Raymond Hettingera45c4872007-10-25 02:26:58 +00002177 Py_DECREF(item);
Raymond Hettinger3f8caa32007-10-24 01:28:33 +00002178 continue;
2179 }
2180 result = PyFloat_FromDouble(f_result);
2181 temp = PyNumber_Add(result, item);
2182 Py_DECREF(result);
2183 Py_DECREF(item);
2184 result = temp;
2185 if (result == NULL) {
2186 Py_DECREF(iter);
2187 return NULL;
2188 }
2189 }
2190 }
2191#endif
2192
Alex Martellia70b1912003-04-22 08:12:33 +00002193 for(;;) {
2194 item = PyIter_Next(iter);
2195 if (item == NULL) {
2196 /* error, or end-of-sequence */
2197 if (PyErr_Occurred()) {
2198 Py_DECREF(result);
2199 result = NULL;
2200 }
2201 break;
2202 }
Alex Martellia253e182003-10-25 23:24:14 +00002203 temp = PyNumber_Add(result, item);
Alex Martellia70b1912003-04-22 08:12:33 +00002204 Py_DECREF(result);
2205 Py_DECREF(item);
2206 result = temp;
2207 if (result == NULL)
2208 break;
2209 }
2210 Py_DECREF(iter);
2211 return result;
2212}
2213
2214PyDoc_STRVAR(sum_doc,
Georg Brandl8134d062006-10-12 12:33:07 +00002215"sum(sequence[, start]) -> value\n\
Alex Martellia70b1912003-04-22 08:12:33 +00002216\n\
2217Returns the sum of a sequence of numbers (NOT strings) plus the value\n\
Georg Brandl8134d062006-10-12 12:33:07 +00002218of parameter 'start' (which defaults to 0). When the sequence is\n\
2219empty, returns start.");
Alex Martellia70b1912003-04-22 08:12:33 +00002220
2221
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002222static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002223builtin_isinstance(PyObject *self, PyObject *args)
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002224{
2225 PyObject *inst;
2226 PyObject *cls;
Guido van Rossum823649d2001-03-21 18:40:58 +00002227 int retval;
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002228
Raymond Hettingerea3fdf42002-12-29 16:33:45 +00002229 if (!PyArg_UnpackTuple(args, "isinstance", 2, 2, &inst, &cls))
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002230 return NULL;
Guido van Rossumf5dd9141997-12-02 19:11:45 +00002231
Guido van Rossum823649d2001-03-21 18:40:58 +00002232 retval = PyObject_IsInstance(inst, cls);
2233 if (retval < 0)
Guido van Rossumad991772001-01-12 16:03:05 +00002234 return NULL;
Guido van Rossum77f6a652002-04-03 22:41:51 +00002235 return PyBool_FromLong(retval);
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002236}
2237
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002238PyDoc_STRVAR(isinstance_doc,
Guido van Rossum77f6a652002-04-03 22:41:51 +00002239"isinstance(object, class-or-type-or-tuple) -> bool\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002240\n\
2241Return whether an object is an instance of a class or of a subclass thereof.\n\
Guido van Rossum03290ec2001-10-07 20:54:12 +00002242With a type as second argument, return whether that is the object's type.\n\
2243The form using a tuple, isinstance(x, (A, B, ...)), is a shortcut for\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002244isinstance(x, A) or isinstance(x, B) or ... (etc.).");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002245
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002246
2247static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002248builtin_issubclass(PyObject *self, PyObject *args)
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002249{
2250 PyObject *derived;
2251 PyObject *cls;
2252 int retval;
2253
Raymond Hettingerea3fdf42002-12-29 16:33:45 +00002254 if (!PyArg_UnpackTuple(args, "issubclass", 2, 2, &derived, &cls))
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002255 return NULL;
Guido van Rossum668213d1999-06-16 17:28:37 +00002256
Guido van Rossum823649d2001-03-21 18:40:58 +00002257 retval = PyObject_IsSubclass(derived, cls);
2258 if (retval < 0)
2259 return NULL;
Guido van Rossum77f6a652002-04-03 22:41:51 +00002260 return PyBool_FromLong(retval);
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002261}
2262
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002263PyDoc_STRVAR(issubclass_doc,
Guido van Rossum77f6a652002-04-03 22:41:51 +00002264"issubclass(C, B) -> bool\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002265\n\
Walter Dörwaldd9a6ad32002-12-12 16:41:44 +00002266Return whether class C is a subclass (i.e., a derived class) of class B.\n\
2267When using a tuple as the second argument issubclass(X, (A, B, ...)),\n\
2268is a shortcut for issubclass(X, A) or issubclass(X, B) or ... (etc.).");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002269
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002270
Barry Warsawbd599b52000-08-03 15:45:29 +00002271static PyObject*
2272builtin_zip(PyObject *self, PyObject *args)
2273{
2274 PyObject *ret;
Martin v. Löwisd96ee902006-02-16 14:37:16 +00002275 const Py_ssize_t itemsize = PySequence_Length(args);
2276 Py_ssize_t i;
Tim Peters8572b4f2001-05-06 01:05:02 +00002277 PyObject *itlist; /* tuple of iterators */
Martin v. Löwisd96ee902006-02-16 14:37:16 +00002278 Py_ssize_t len; /* guess at result length */
Barry Warsawbd599b52000-08-03 15:45:29 +00002279
Raymond Hettingereaef6152003-08-02 07:42:57 +00002280 if (itemsize == 0)
2281 return PyList_New(0);
2282
Barry Warsawbd599b52000-08-03 15:45:29 +00002283 /* args must be a tuple */
2284 assert(PyTuple_Check(args));
2285
Tim Peters39a86c22002-05-12 07:19:38 +00002286 /* Guess at result length: the shortest of the input lengths.
2287 If some argument refuses to say, we refuse to guess too, lest
2288 an argument like xrange(sys.maxint) lead us astray.*/
Tim Peters67d687a2002-04-29 21:27:32 +00002289 len = -1; /* unknown */
2290 for (i = 0; i < itemsize; ++i) {
2291 PyObject *item = PyTuple_GET_ITEM(args, i);
Raymond Hettinger4e2f7142007-12-06 00:56:53 +00002292 Py_ssize_t thislen = _PyObject_LengthHint(item, -1);
Tim Peters39a86c22002-05-12 07:19:38 +00002293 if (thislen < 0) {
Tim Peters39a86c22002-05-12 07:19:38 +00002294 len = -1;
2295 break;
2296 }
Tim Peters67d687a2002-04-29 21:27:32 +00002297 else if (len < 0 || thislen < len)
2298 len = thislen;
2299 }
2300
Tim Peters8572b4f2001-05-06 01:05:02 +00002301 /* allocate result list */
Tim Peters67d687a2002-04-29 21:27:32 +00002302 if (len < 0)
2303 len = 10; /* arbitrary */
2304 if ((ret = PyList_New(len)) == NULL)
Barry Warsawbd599b52000-08-03 15:45:29 +00002305 return NULL;
2306
Tim Peters8572b4f2001-05-06 01:05:02 +00002307 /* obtain iterators */
2308 itlist = PyTuple_New(itemsize);
2309 if (itlist == NULL)
2310 goto Fail_ret;
2311 for (i = 0; i < itemsize; ++i) {
2312 PyObject *item = PyTuple_GET_ITEM(args, i);
2313 PyObject *it = PyObject_GetIter(item);
2314 if (it == NULL) {
2315 if (PyErr_ExceptionMatches(PyExc_TypeError))
2316 PyErr_Format(PyExc_TypeError,
Neal Norwitza361bd82006-02-19 19:31:50 +00002317 "zip argument #%zd must support iteration",
Tim Peters8572b4f2001-05-06 01:05:02 +00002318 i+1);
2319 goto Fail_ret_itlist;
Barry Warsawbd599b52000-08-03 15:45:29 +00002320 }
Tim Peters8572b4f2001-05-06 01:05:02 +00002321 PyTuple_SET_ITEM(itlist, i, it);
2322 }
Barry Warsawbd599b52000-08-03 15:45:29 +00002323
Tim Peters8572b4f2001-05-06 01:05:02 +00002324 /* build result into ret list */
Tim Peters67d687a2002-04-29 21:27:32 +00002325 for (i = 0; ; ++i) {
2326 int j;
Tim Peters8572b4f2001-05-06 01:05:02 +00002327 PyObject *next = PyTuple_New(itemsize);
2328 if (!next)
2329 goto Fail_ret_itlist;
2330
Tim Peters67d687a2002-04-29 21:27:32 +00002331 for (j = 0; j < itemsize; j++) {
2332 PyObject *it = PyTuple_GET_ITEM(itlist, j);
Tim Peters8572b4f2001-05-06 01:05:02 +00002333 PyObject *item = PyIter_Next(it);
Barry Warsawbd599b52000-08-03 15:45:29 +00002334 if (!item) {
Tim Peters8572b4f2001-05-06 01:05:02 +00002335 if (PyErr_Occurred()) {
2336 Py_DECREF(ret);
2337 ret = NULL;
Barry Warsawbd599b52000-08-03 15:45:29 +00002338 }
2339 Py_DECREF(next);
Tim Peters8572b4f2001-05-06 01:05:02 +00002340 Py_DECREF(itlist);
Tim Peters67d687a2002-04-29 21:27:32 +00002341 goto Done;
Barry Warsawbd599b52000-08-03 15:45:29 +00002342 }
Tim Peters67d687a2002-04-29 21:27:32 +00002343 PyTuple_SET_ITEM(next, j, item);
Barry Warsawbd599b52000-08-03 15:45:29 +00002344 }
Tim Peters8572b4f2001-05-06 01:05:02 +00002345
Tim Peters67d687a2002-04-29 21:27:32 +00002346 if (i < len)
2347 PyList_SET_ITEM(ret, i, next);
2348 else {
2349 int status = PyList_Append(ret, next);
2350 Py_DECREF(next);
2351 ++len;
2352 if (status < 0)
2353 goto Fail_ret_itlist;
2354 }
Barry Warsawbd599b52000-08-03 15:45:29 +00002355 }
Tim Peters8572b4f2001-05-06 01:05:02 +00002356
Tim Peters67d687a2002-04-29 21:27:32 +00002357Done:
2358 if (ret != NULL && i < len) {
2359 /* The list is too big. */
2360 if (PyList_SetSlice(ret, i, len, NULL) < 0)
2361 return NULL;
2362 }
2363 return ret;
2364
Tim Peters8572b4f2001-05-06 01:05:02 +00002365Fail_ret_itlist:
2366 Py_DECREF(itlist);
2367Fail_ret:
2368 Py_DECREF(ret);
2369 return NULL;
Barry Warsawbd599b52000-08-03 15:45:29 +00002370}
2371
2372
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002373PyDoc_STRVAR(zip_doc,
Barry Warsawbd599b52000-08-03 15:45:29 +00002374"zip(seq1 [, seq2 [...]]) -> [(seq1[0], seq2[0] ...), (...)]\n\
2375\n\
2376Return a list of tuples, where each tuple contains the i-th element\n\
2377from each of the argument sequences. The returned list is truncated\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002378in length to the length of the shortest argument sequence.");
Barry Warsawbd599b52000-08-03 15:45:29 +00002379
2380
Guido van Rossum79f25d91997-04-29 20:08:16 +00002381static PyMethodDef builtin_methods[] = {
Neal Norwitz92e212f2006-04-03 04:48:37 +00002382 {"__import__", (PyCFunction)builtin___import__, METH_VARARGS | METH_KEYWORDS, import_doc},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002383 {"abs", builtin_abs, METH_O, abs_doc},
Raymond Hettinger96229b12005-03-11 06:49:40 +00002384 {"all", builtin_all, METH_O, all_doc},
2385 {"any", builtin_any, METH_O, any_doc},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002386 {"apply", builtin_apply, METH_VARARGS, apply_doc},
Eric Smith3cd81942008-02-22 16:30:22 +00002387 {"bin", builtin_bin, METH_O, bin_doc},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002388 {"callable", builtin_callable, METH_O, callable_doc},
2389 {"chr", builtin_chr, METH_VARARGS, chr_doc},
2390 {"cmp", builtin_cmp, METH_VARARGS, cmp_doc},
2391 {"coerce", builtin_coerce, METH_VARARGS, coerce_doc},
Georg Brandl5240d742007-03-13 20:46:32 +00002392 {"compile", (PyCFunction)builtin_compile, METH_VARARGS | METH_KEYWORDS, compile_doc},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002393 {"delattr", builtin_delattr, METH_VARARGS, delattr_doc},
2394 {"dir", builtin_dir, METH_VARARGS, dir_doc},
2395 {"divmod", builtin_divmod, METH_VARARGS, divmod_doc},
2396 {"eval", builtin_eval, METH_VARARGS, eval_doc},
2397 {"execfile", builtin_execfile, METH_VARARGS, execfile_doc},
2398 {"filter", builtin_filter, METH_VARARGS, filter_doc},
Eric Smitha9f7d622008-02-17 19:46:49 +00002399 {"format", builtin_format, METH_VARARGS, format_doc},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002400 {"getattr", builtin_getattr, METH_VARARGS, getattr_doc},
2401 {"globals", (PyCFunction)builtin_globals, METH_NOARGS, globals_doc},
2402 {"hasattr", builtin_hasattr, METH_VARARGS, hasattr_doc},
2403 {"hash", builtin_hash, METH_O, hash_doc},
2404 {"hex", builtin_hex, METH_O, hex_doc},
2405 {"id", builtin_id, METH_O, id_doc},
2406 {"input", builtin_input, METH_VARARGS, input_doc},
2407 {"intern", builtin_intern, METH_VARARGS, intern_doc},
2408 {"isinstance", builtin_isinstance, METH_VARARGS, isinstance_doc},
2409 {"issubclass", builtin_issubclass, METH_VARARGS, issubclass_doc},
2410 {"iter", builtin_iter, METH_VARARGS, iter_doc},
2411 {"len", builtin_len, METH_O, len_doc},
2412 {"locals", (PyCFunction)builtin_locals, METH_NOARGS, locals_doc},
2413 {"map", builtin_map, METH_VARARGS, map_doc},
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00002414 {"max", (PyCFunction)builtin_max, METH_VARARGS | METH_KEYWORDS, max_doc},
2415 {"min", (PyCFunction)builtin_min, METH_VARARGS | METH_KEYWORDS, min_doc},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002416 {"oct", builtin_oct, METH_O, oct_doc},
Neal Norwitzc4edb0e2006-05-02 04:43:14 +00002417 {"open", (PyCFunction)builtin_open, METH_VARARGS | METH_KEYWORDS, open_doc},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002418 {"ord", builtin_ord, METH_O, ord_doc},
2419 {"pow", builtin_pow, METH_VARARGS, pow_doc},
2420 {"range", builtin_range, METH_VARARGS, range_doc},
2421 {"raw_input", builtin_raw_input, METH_VARARGS, raw_input_doc},
2422 {"reduce", builtin_reduce, METH_VARARGS, reduce_doc},
2423 {"reload", builtin_reload, METH_O, reload_doc},
2424 {"repr", builtin_repr, METH_O, repr_doc},
Georg Brandlccadf842006-03-31 18:54:53 +00002425 {"round", (PyCFunction)builtin_round, METH_VARARGS | METH_KEYWORDS, round_doc},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002426 {"setattr", builtin_setattr, METH_VARARGS, setattr_doc},
Raymond Hettinger64958a12003-12-17 20:43:33 +00002427 {"sorted", (PyCFunction)builtin_sorted, METH_VARARGS | METH_KEYWORDS, sorted_doc},
Alex Martellia70b1912003-04-22 08:12:33 +00002428 {"sum", builtin_sum, METH_VARARGS, sum_doc},
Martin v. Löwis339d0f72001-08-17 18:39:25 +00002429#ifdef Py_USING_UNICODE
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002430 {"unichr", builtin_unichr, METH_VARARGS, unichr_doc},
Martin v. Löwis339d0f72001-08-17 18:39:25 +00002431#endif
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002432 {"vars", builtin_vars, METH_VARARGS, vars_doc},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002433 {"zip", builtin_zip, METH_VARARGS, zip_doc},
Guido van Rossumc02e15c1991-12-16 13:03:00 +00002434 {NULL, NULL},
Guido van Rossum3f5da241990-12-20 15:06:42 +00002435};
2436
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002437PyDoc_STRVAR(builtin_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002438"Built-in functions, exceptions, and other objects.\n\
2439\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002440Noteworthy: None is the `nil' object; Ellipsis represents `...' in slices.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002441
Guido van Rossum25ce5661997-08-02 03:10:38 +00002442PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002443_PyBuiltin_Init(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +00002444{
Fred Drake5550de32000-06-20 04:54:19 +00002445 PyObject *mod, *dict, *debug;
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002446 mod = Py_InitModule4("__builtin__", builtin_methods,
2447 builtin_doc, (PyObject *)NULL,
2448 PYTHON_API_VERSION);
Guido van Rossum25ce5661997-08-02 03:10:38 +00002449 if (mod == NULL)
2450 return NULL;
2451 dict = PyModule_GetDict(mod);
Tim Peters4b7625e2001-09-13 21:37:17 +00002452
Tim Peters7571a0f2003-03-23 17:52:28 +00002453#ifdef Py_TRACE_REFS
2454 /* __builtin__ exposes a number of statically allocated objects
2455 * that, before this code was added in 2.3, never showed up in
2456 * the list of "all objects" maintained by Py_TRACE_REFS. As a
2457 * result, programs leaking references to None and False (etc)
2458 * couldn't be diagnosed by examining sys.getobjects(0).
2459 */
2460#define ADD_TO_ALL(OBJECT) _Py_AddToAllObjects((PyObject *)(OBJECT), 0)
2461#else
2462#define ADD_TO_ALL(OBJECT) (void)0
2463#endif
2464
Tim Peters4b7625e2001-09-13 21:37:17 +00002465#define SETBUILTIN(NAME, OBJECT) \
Tim Peters7571a0f2003-03-23 17:52:28 +00002466 if (PyDict_SetItemString(dict, NAME, (PyObject *)OBJECT) < 0) \
2467 return NULL; \
2468 ADD_TO_ALL(OBJECT)
Tim Peters4b7625e2001-09-13 21:37:17 +00002469
2470 SETBUILTIN("None", Py_None);
2471 SETBUILTIN("Ellipsis", Py_Ellipsis);
2472 SETBUILTIN("NotImplemented", Py_NotImplemented);
Guido van Rossum77f6a652002-04-03 22:41:51 +00002473 SETBUILTIN("False", Py_False);
2474 SETBUILTIN("True", Py_True);
Neal Norwitz32a7e7f2002-05-31 19:58:02 +00002475 SETBUILTIN("basestring", &PyBaseString_Type);
Guido van Rossum77f6a652002-04-03 22:41:51 +00002476 SETBUILTIN("bool", &PyBool_Type);
Christian Heimes288e89a2008-01-18 18:24:07 +00002477 SETBUILTIN("bytes", &PyString_Type);
Guido van Rossumbea18cc2002-06-14 20:41:17 +00002478 SETBUILTIN("buffer", &PyBuffer_Type);
Tim Peters4b7625e2001-09-13 21:37:17 +00002479 SETBUILTIN("classmethod", &PyClassMethod_Type);
Tim Peters6d6c1a32001-08-02 04:15:00 +00002480#ifndef WITHOUT_COMPLEX
Tim Peters4b7625e2001-09-13 21:37:17 +00002481 SETBUILTIN("complex", &PyComplex_Type);
Tim Peters6d6c1a32001-08-02 04:15:00 +00002482#endif
Tim Petersa427a2b2001-10-29 22:25:45 +00002483 SETBUILTIN("dict", &PyDict_Type);
Tim Peters67d687a2002-04-29 21:27:32 +00002484 SETBUILTIN("enumerate", &PyEnum_Type);
Neal Norwitzc4edb0e2006-05-02 04:43:14 +00002485 SETBUILTIN("file", &PyFile_Type);
Tim Peters4b7625e2001-09-13 21:37:17 +00002486 SETBUILTIN("float", &PyFloat_Type);
Raymond Hettingera690a992003-11-16 16:17:49 +00002487 SETBUILTIN("frozenset", &PyFrozenSet_Type);
Tim Peters4b7625e2001-09-13 21:37:17 +00002488 SETBUILTIN("property", &PyProperty_Type);
2489 SETBUILTIN("int", &PyInt_Type);
2490 SETBUILTIN("list", &PyList_Type);
2491 SETBUILTIN("long", &PyLong_Type);
2492 SETBUILTIN("object", &PyBaseObject_Type);
Raymond Hettinger85c20a42003-11-06 14:06:48 +00002493 SETBUILTIN("reversed", &PyReversed_Type);
Raymond Hettingera690a992003-11-16 16:17:49 +00002494 SETBUILTIN("set", &PySet_Type);
Guido van Rossumbea18cc2002-06-14 20:41:17 +00002495 SETBUILTIN("slice", &PySlice_Type);
Tim Peters4b7625e2001-09-13 21:37:17 +00002496 SETBUILTIN("staticmethod", &PyStaticMethod_Type);
2497 SETBUILTIN("str", &PyString_Type);
2498 SETBUILTIN("super", &PySuper_Type);
2499 SETBUILTIN("tuple", &PyTuple_Type);
2500 SETBUILTIN("type", &PyType_Type);
Raymond Hettingerc4c453f2002-06-05 23:12:45 +00002501 SETBUILTIN("xrange", &PyRange_Type);
Martin v. Löwis339d0f72001-08-17 18:39:25 +00002502#ifdef Py_USING_UNICODE
Tim Peters4b7625e2001-09-13 21:37:17 +00002503 SETBUILTIN("unicode", &PyUnicode_Type);
Martin v. Löwis339d0f72001-08-17 18:39:25 +00002504#endif
Guido van Rossum77f6a652002-04-03 22:41:51 +00002505 debug = PyBool_FromLong(Py_OptimizeFlag == 0);
Fred Drake5550de32000-06-20 04:54:19 +00002506 if (PyDict_SetItemString(dict, "__debug__", debug) < 0) {
2507 Py_XDECREF(debug);
Guido van Rossum25ce5661997-08-02 03:10:38 +00002508 return NULL;
Fred Drake5550de32000-06-20 04:54:19 +00002509 }
2510 Py_XDECREF(debug);
Barry Warsaw757af0e1997-08-29 22:13:51 +00002511
Guido van Rossum25ce5661997-08-02 03:10:38 +00002512 return mod;
Tim Peters7571a0f2003-03-23 17:52:28 +00002513#undef ADD_TO_ALL
Tim Peters4b7625e2001-09-13 21:37:17 +00002514#undef SETBUILTIN
Guido van Rossum3f5da241990-12-20 15:06:42 +00002515}
2516
Guido van Rossume77a7571993-11-03 15:01:26 +00002517/* Helper for filter(): filter a tuple through a function */
Guido van Rossum12d12c51993-10-26 17:58:25 +00002518
Guido van Rossum79f25d91997-04-29 20:08:16 +00002519static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002520filtertuple(PyObject *func, PyObject *tuple)
Guido van Rossum12d12c51993-10-26 17:58:25 +00002521{
Guido van Rossum79f25d91997-04-29 20:08:16 +00002522 PyObject *result;
Martin v. Löwis18e16552006-02-15 17:27:45 +00002523 Py_ssize_t i, j;
2524 Py_ssize_t len = PyTuple_Size(tuple);
Guido van Rossum12d12c51993-10-26 17:58:25 +00002525
Guido van Rossumb7b45621995-08-04 04:07:45 +00002526 if (len == 0) {
Walter Dörwaldc3da83f2003-02-04 20:24:45 +00002527 if (PyTuple_CheckExact(tuple))
2528 Py_INCREF(tuple);
2529 else
2530 tuple = PyTuple_New(0);
Guido van Rossumb7b45621995-08-04 04:07:45 +00002531 return tuple;
2532 }
2533
Guido van Rossum79f25d91997-04-29 20:08:16 +00002534 if ((result = PyTuple_New(len)) == NULL)
Guido van Rossum2586bf01993-11-01 16:21:44 +00002535 return NULL;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002536
Guido van Rossum12d12c51993-10-26 17:58:25 +00002537 for (i = j = 0; i < len; ++i) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002538 PyObject *item, *good;
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002539 int ok;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002540
Walter Dörwald8dd19322003-02-10 17:36:40 +00002541 if (tuple->ob_type->tp_as_sequence &&
2542 tuple->ob_type->tp_as_sequence->sq_item) {
2543 item = tuple->ob_type->tp_as_sequence->sq_item(tuple, i);
Walter Dörwaldc58a3a12003-08-18 18:28:45 +00002544 if (item == NULL)
2545 goto Fail_1;
Walter Dörwald8dd19322003-02-10 17:36:40 +00002546 } else {
Alex Martellia9b9c9f2003-04-23 13:34:35 +00002547 PyErr_SetString(PyExc_TypeError, "filter(): unsubscriptable tuple");
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002548 goto Fail_1;
Walter Dörwald8dd19322003-02-10 17:36:40 +00002549 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002550 if (func == Py_None) {
2551 Py_INCREF(item);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002552 good = item;
2553 }
2554 else {
Raymond Hettinger8ae46892003-10-12 19:09:37 +00002555 PyObject *arg = PyTuple_Pack(1, item);
Walter Dörwaldc58a3a12003-08-18 18:28:45 +00002556 if (arg == NULL) {
2557 Py_DECREF(item);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002558 goto Fail_1;
Walter Dörwaldc58a3a12003-08-18 18:28:45 +00002559 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002560 good = PyEval_CallObject(func, arg);
2561 Py_DECREF(arg);
Walter Dörwaldc58a3a12003-08-18 18:28:45 +00002562 if (good == NULL) {
2563 Py_DECREF(item);
Guido van Rossum12d12c51993-10-26 17:58:25 +00002564 goto Fail_1;
Walter Dörwaldc58a3a12003-08-18 18:28:45 +00002565 }
Guido van Rossum12d12c51993-10-26 17:58:25 +00002566 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002567 ok = PyObject_IsTrue(good);
2568 Py_DECREF(good);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002569 if (ok) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002570 if (PyTuple_SetItem(result, j++, item) < 0)
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002571 goto Fail_1;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002572 }
Walter Dörwaldc58a3a12003-08-18 18:28:45 +00002573 else
2574 Py_DECREF(item);
Guido van Rossum12d12c51993-10-26 17:58:25 +00002575 }
2576
Tim Peters4324aa32001-05-28 22:30:08 +00002577 if (_PyTuple_Resize(&result, j) < 0)
Guido van Rossum12d12c51993-10-26 17:58:25 +00002578 return NULL;
2579
Guido van Rossum12d12c51993-10-26 17:58:25 +00002580 return result;
2581
Guido van Rossum12d12c51993-10-26 17:58:25 +00002582Fail_1:
Guido van Rossum79f25d91997-04-29 20:08:16 +00002583 Py_DECREF(result);
Guido van Rossum12d12c51993-10-26 17:58:25 +00002584 return NULL;
2585}
2586
2587
Guido van Rossume77a7571993-11-03 15:01:26 +00002588/* Helper for filter(): filter a string through a function */
Guido van Rossum12d12c51993-10-26 17:58:25 +00002589
Guido van Rossum79f25d91997-04-29 20:08:16 +00002590static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002591filterstring(PyObject *func, PyObject *strobj)
Guido van Rossum12d12c51993-10-26 17:58:25 +00002592{
Guido van Rossum79f25d91997-04-29 20:08:16 +00002593 PyObject *result;
Martin v. Löwis18e16552006-02-15 17:27:45 +00002594 Py_ssize_t i, j;
2595 Py_ssize_t len = PyString_Size(strobj);
2596 Py_ssize_t outlen = len;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002597
Guido van Rossum79f25d91997-04-29 20:08:16 +00002598 if (func == Py_None) {
Walter Dörwald1918f772003-02-10 13:19:13 +00002599 /* If it's a real string we can return the original,
2600 * as no character is ever false and __getitem__
2601 * does return this character. If it's a subclass
2602 * we must go through the __getitem__ loop */
2603 if (PyString_CheckExact(strobj)) {
Walter Dörwaldc3da83f2003-02-04 20:24:45 +00002604 Py_INCREF(strobj);
Walter Dörwald1918f772003-02-10 13:19:13 +00002605 return strobj;
2606 }
Guido van Rossum12d12c51993-10-26 17:58:25 +00002607 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002608 if ((result = PyString_FromStringAndSize(NULL, len)) == NULL)
Guido van Rossum2586bf01993-11-01 16:21:44 +00002609 return NULL;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002610
Guido van Rossum12d12c51993-10-26 17:58:25 +00002611 for (i = j = 0; i < len; ++i) {
Walter Dörwald1918f772003-02-10 13:19:13 +00002612 PyObject *item;
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002613 int ok;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002614
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002615 item = (*strobj->ob_type->tp_as_sequence->sq_item)(strobj, i);
2616 if (item == NULL)
2617 goto Fail_1;
Walter Dörwald1918f772003-02-10 13:19:13 +00002618 if (func==Py_None) {
2619 ok = 1;
2620 } else {
2621 PyObject *arg, *good;
Raymond Hettinger8ae46892003-10-12 19:09:37 +00002622 arg = PyTuple_Pack(1, item);
Walter Dörwald1918f772003-02-10 13:19:13 +00002623 if (arg == NULL) {
2624 Py_DECREF(item);
2625 goto Fail_1;
2626 }
2627 good = PyEval_CallObject(func, arg);
2628 Py_DECREF(arg);
2629 if (good == NULL) {
2630 Py_DECREF(item);
2631 goto Fail_1;
2632 }
2633 ok = PyObject_IsTrue(good);
2634 Py_DECREF(good);
Tim Peters388ed082001-04-07 20:34:48 +00002635 }
Walter Dörwald903f1e02003-02-04 16:28:00 +00002636 if (ok) {
Martin v. Löwisd96ee902006-02-16 14:37:16 +00002637 Py_ssize_t reslen;
Walter Dörwald903f1e02003-02-04 16:28:00 +00002638 if (!PyString_Check(item)) {
2639 PyErr_SetString(PyExc_TypeError, "can't filter str to str:"
2640 " __getitem__ returned different type");
2641 Py_DECREF(item);
2642 goto Fail_1;
2643 }
2644 reslen = PyString_GET_SIZE(item);
2645 if (reslen == 1) {
2646 PyString_AS_STRING(result)[j++] =
2647 PyString_AS_STRING(item)[0];
2648 } else {
2649 /* do we need more space? */
Martin v. Löwisd96ee902006-02-16 14:37:16 +00002650 Py_ssize_t need = j + reslen + len-i-1;
Walter Dörwald903f1e02003-02-04 16:28:00 +00002651 if (need > outlen) {
2652 /* overallocate, to avoid reallocations */
2653 if (need<2*outlen)
2654 need = 2*outlen;
2655 if (_PyString_Resize(&result, need)) {
2656 Py_DECREF(item);
2657 return NULL;
2658 }
2659 outlen = need;
2660 }
2661 memcpy(
2662 PyString_AS_STRING(result) + j,
2663 PyString_AS_STRING(item),
2664 reslen
2665 );
2666 j += reslen;
2667 }
2668 }
Tim Peters388ed082001-04-07 20:34:48 +00002669 Py_DECREF(item);
Guido van Rossum12d12c51993-10-26 17:58:25 +00002670 }
2671
Walter Dörwald903f1e02003-02-04 16:28:00 +00002672 if (j < outlen)
Tim Peters5de98422002-04-27 18:44:32 +00002673 _PyString_Resize(&result, j);
Guido van Rossum12d12c51993-10-26 17:58:25 +00002674
Guido van Rossum12d12c51993-10-26 17:58:25 +00002675 return result;
2676
Guido van Rossum12d12c51993-10-26 17:58:25 +00002677Fail_1:
Guido van Rossum79f25d91997-04-29 20:08:16 +00002678 Py_DECREF(result);
Guido van Rossum12d12c51993-10-26 17:58:25 +00002679 return NULL;
2680}
Martin v. Löwis8afd7572003-01-25 22:46:11 +00002681
2682#ifdef Py_USING_UNICODE
2683/* Helper for filter(): filter a Unicode object through a function */
2684
2685static PyObject *
2686filterunicode(PyObject *func, PyObject *strobj)
2687{
2688 PyObject *result;
Martin v. Löwis725507b2006-03-07 12:08:51 +00002689 register Py_ssize_t i, j;
Martin v. Löwisd96ee902006-02-16 14:37:16 +00002690 Py_ssize_t len = PyUnicode_GetSize(strobj);
2691 Py_ssize_t outlen = len;
Martin v. Löwis8afd7572003-01-25 22:46:11 +00002692
2693 if (func == Py_None) {
Walter Dörwald1918f772003-02-10 13:19:13 +00002694 /* If it's a real string we can return the original,
2695 * as no character is ever false and __getitem__
2696 * does return this character. If it's a subclass
2697 * we must go through the __getitem__ loop */
2698 if (PyUnicode_CheckExact(strobj)) {
Walter Dörwaldc3da83f2003-02-04 20:24:45 +00002699 Py_INCREF(strobj);
Walter Dörwald1918f772003-02-10 13:19:13 +00002700 return strobj;
2701 }
Martin v. Löwis8afd7572003-01-25 22:46:11 +00002702 }
2703 if ((result = PyUnicode_FromUnicode(NULL, len)) == NULL)
2704 return NULL;
2705
2706 for (i = j = 0; i < len; ++i) {
2707 PyObject *item, *arg, *good;
2708 int ok;
2709
2710 item = (*strobj->ob_type->tp_as_sequence->sq_item)(strobj, i);
2711 if (item == NULL)
2712 goto Fail_1;
Walter Dörwald1918f772003-02-10 13:19:13 +00002713 if (func == Py_None) {
2714 ok = 1;
2715 } else {
Raymond Hettinger8ae46892003-10-12 19:09:37 +00002716 arg = PyTuple_Pack(1, item);
Walter Dörwald1918f772003-02-10 13:19:13 +00002717 if (arg == NULL) {
2718 Py_DECREF(item);
2719 goto Fail_1;
2720 }
2721 good = PyEval_CallObject(func, arg);
2722 Py_DECREF(arg);
2723 if (good == NULL) {
2724 Py_DECREF(item);
2725 goto Fail_1;
2726 }
2727 ok = PyObject_IsTrue(good);
2728 Py_DECREF(good);
Martin v. Löwis8afd7572003-01-25 22:46:11 +00002729 }
Walter Dörwald903f1e02003-02-04 16:28:00 +00002730 if (ok) {
Martin v. Löwisd96ee902006-02-16 14:37:16 +00002731 Py_ssize_t reslen;
Walter Dörwald903f1e02003-02-04 16:28:00 +00002732 if (!PyUnicode_Check(item)) {
Georg Brandl99d7e4e2005-08-31 22:21:15 +00002733 PyErr_SetString(PyExc_TypeError,
Jeremy Hylton1aad9c72003-09-16 03:10:59 +00002734 "can't filter unicode to unicode:"
2735 " __getitem__ returned different type");
Walter Dörwald903f1e02003-02-04 16:28:00 +00002736 Py_DECREF(item);
2737 goto Fail_1;
2738 }
2739 reslen = PyUnicode_GET_SIZE(item);
Georg Brandl99d7e4e2005-08-31 22:21:15 +00002740 if (reslen == 1)
Walter Dörwald903f1e02003-02-04 16:28:00 +00002741 PyUnicode_AS_UNICODE(result)[j++] =
2742 PyUnicode_AS_UNICODE(item)[0];
Jeremy Hylton1aad9c72003-09-16 03:10:59 +00002743 else {
Walter Dörwald903f1e02003-02-04 16:28:00 +00002744 /* do we need more space? */
Martin v. Löwisd96ee902006-02-16 14:37:16 +00002745 Py_ssize_t need = j + reslen + len - i - 1;
Walter Dörwald903f1e02003-02-04 16:28:00 +00002746 if (need > outlen) {
Georg Brandl99d7e4e2005-08-31 22:21:15 +00002747 /* overallocate,
Jeremy Hylton1aad9c72003-09-16 03:10:59 +00002748 to avoid reallocations */
2749 if (need < 2 * outlen)
2750 need = 2 * outlen;
Jeremy Hylton364f6be2003-09-16 03:17:16 +00002751 if (PyUnicode_Resize(
2752 &result, need) < 0) {
Walter Dörwald903f1e02003-02-04 16:28:00 +00002753 Py_DECREF(item);
Walter Dörwald531e0002003-02-04 16:57:49 +00002754 goto Fail_1;
Walter Dörwald903f1e02003-02-04 16:28:00 +00002755 }
2756 outlen = need;
2757 }
Jeremy Hylton1aad9c72003-09-16 03:10:59 +00002758 memcpy(PyUnicode_AS_UNICODE(result) + j,
2759 PyUnicode_AS_UNICODE(item),
2760 reslen*sizeof(Py_UNICODE));
Walter Dörwald903f1e02003-02-04 16:28:00 +00002761 j += reslen;
2762 }
2763 }
Martin v. Löwis8afd7572003-01-25 22:46:11 +00002764 Py_DECREF(item);
2765 }
2766
Walter Dörwald903f1e02003-02-04 16:28:00 +00002767 if (j < outlen)
Martin v. Löwis8afd7572003-01-25 22:46:11 +00002768 PyUnicode_Resize(&result, j);
2769
2770 return result;
2771
2772Fail_1:
2773 Py_DECREF(result);
2774 return NULL;
2775}
2776#endif