blob: 1667d37742afee3d2e4717edf4d3b83fcdde7424 [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 Hammond26cffde42001-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 Hammond26cffde42001-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 Hammond26cffde42001-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;
81
82 it = PyObject_GetIter(v);
83 if (it == NULL)
84 return NULL;
85
86 while ((item = PyIter_Next(it)) != NULL) {
87 int cmp = PyObject_IsTrue(item);
88 Py_DECREF(item);
89 if (cmp < 0) {
90 Py_DECREF(it);
91 return NULL;
92 }
93 if (cmp == 0) {
94 Py_DECREF(it);
95 Py_RETURN_FALSE;
96 }
97 }
98 Py_DECREF(it);
99 if (PyErr_Occurred())
100 return NULL;
101 Py_RETURN_TRUE;
102}
103
104PyDoc_STRVAR(all_doc,
105"all(iterable) -> bool\n\
106\n\
107Return True if bool(x) is True for all values x in the iterable.");
108
109static PyObject *
110builtin_any(PyObject *self, PyObject *v)
111{
112 PyObject *it, *item;
113
114 it = PyObject_GetIter(v);
115 if (it == NULL)
116 return NULL;
117
118 while ((item = PyIter_Next(it)) != NULL) {
119 int cmp = PyObject_IsTrue(item);
120 Py_DECREF(item);
121 if (cmp < 0) {
122 Py_DECREF(it);
123 return NULL;
124 }
125 if (cmp == 1) {
126 Py_DECREF(it);
127 Py_RETURN_TRUE;
128 }
129 }
130 Py_DECREF(it);
131 if (PyErr_Occurred())
132 return NULL;
133 Py_RETURN_FALSE;
134}
135
136PyDoc_STRVAR(any_doc,
137"any(iterable) -> bool\n\
138\n\
139Return True if bool(x) is True for any x in the iterable.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000140
Guido van Rossum79f25d91997-04-29 20:08:16 +0000141static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000142builtin_apply(PyObject *self, PyObject *args)
Guido van Rossumc02e15c1991-12-16 13:03:00 +0000143{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000144 PyObject *func, *alist = NULL, *kwdict = NULL;
Barry Warsaw968f8cb1998-10-01 15:33:12 +0000145 PyObject *t = NULL, *retval = NULL;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000146
Neal Norwitz8b2bfbc2007-05-23 06:35:32 +0000147 if (Py_Py3kWarningFlag &&
148 PyErr_Warn(PyExc_DeprecationWarning,
149 "apply() not supported in 3.x") < 0)
150 return NULL;
151
Raymond Hettingerea3fdf42002-12-29 16:33:45 +0000152 if (!PyArg_UnpackTuple(args, "apply", 1, 3, &func, &alist, &kwdict))
Guido van Rossumc02e15c1991-12-16 13:03:00 +0000153 return NULL;
Barry Warsaw968f8cb1998-10-01 15:33:12 +0000154 if (alist != NULL) {
155 if (!PyTuple_Check(alist)) {
156 if (!PySequence_Check(alist)) {
Jeremy Hyltonc862cf42001-01-19 03:25:05 +0000157 PyErr_Format(PyExc_TypeError,
Alex Martellia9b9c9f2003-04-23 13:34:35 +0000158 "apply() arg 2 expected sequence, found %s",
Jeremy Hyltonc862cf42001-01-19 03:25:05 +0000159 alist->ob_type->tp_name);
Barry Warsaw968f8cb1998-10-01 15:33:12 +0000160 return NULL;
161 }
162 t = PySequence_Tuple(alist);
163 if (t == NULL)
164 return NULL;
165 alist = t;
166 }
Guido van Rossum2d951851994-08-29 12:52:16 +0000167 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000168 if (kwdict != NULL && !PyDict_Check(kwdict)) {
Jeremy Hyltonc862cf42001-01-19 03:25:05 +0000169 PyErr_Format(PyExc_TypeError,
170 "apply() arg 3 expected dictionary, found %s",
171 kwdict->ob_type->tp_name);
Barry Warsaw968f8cb1998-10-01 15:33:12 +0000172 goto finally;
Guido van Rossum681d79a1995-07-18 14:51:37 +0000173 }
Barry Warsaw968f8cb1998-10-01 15:33:12 +0000174 retval = PyEval_CallObjectWithKeywords(func, alist, kwdict);
175 finally:
176 Py_XDECREF(t);
177 return retval;
Guido van Rossumc02e15c1991-12-16 13:03:00 +0000178}
179
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000180PyDoc_STRVAR(apply_doc,
Fred Drakef1fbc622001-01-12 17:05:05 +0000181"apply(object[, args[, kwargs]]) -> value\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000182\n\
Fred Drake7b912121999-12-23 14:16:55 +0000183Call a callable object with positional arguments taken from the tuple args,\n\
184and keyword arguments taken from the optional dictionary kwargs.\n\
Raymond Hettingerff41c482003-04-06 09:01:11 +0000185Note that classes are callable, as are instances with a __call__() method.\n\
186\n\
187Deprecated since release 2.3. Instead, use the extended call syntax:\n\
188 function(*args, **keywords).");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000189
190
Guido van Rossum79f25d91997-04-29 20:08:16 +0000191static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000192builtin_callable(PyObject *self, PyObject *v)
Guido van Rossum2d951851994-08-29 12:52:16 +0000193{
Neal Norwitzdf25efe2007-05-23 06:58:36 +0000194 if (Py_Py3kWarningFlag &&
195 PyErr_Warn(PyExc_DeprecationWarning,
196 "callable() not supported in 3.x") < 0)
197 return NULL;
Guido van Rossum77f6a652002-04-03 22:41:51 +0000198 return PyBool_FromLong((long)PyCallable_Check(v));
Guido van Rossum2d951851994-08-29 12:52:16 +0000199}
200
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000201PyDoc_STRVAR(callable_doc,
Guido van Rossum77f6a652002-04-03 22:41:51 +0000202"callable(object) -> bool\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000203\n\
204Return whether the object is callable (i.e., some kind of function).\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000205Note that classes are callable, as are instances with a __call__() method.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000206
207
Guido van Rossum79f25d91997-04-29 20:08:16 +0000208static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000209builtin_filter(PyObject *self, PyObject *args)
Guido van Rossum12d12c51993-10-26 17:58:25 +0000210{
Guido van Rossumc7903a12002-08-16 07:04:56 +0000211 PyObject *func, *seq, *result, *it, *arg;
Martin v. Löwisd96ee902006-02-16 14:37:16 +0000212 Py_ssize_t len; /* guess for result list size */
213 register Py_ssize_t j;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000214
Raymond Hettingerea3fdf42002-12-29 16:33:45 +0000215 if (!PyArg_UnpackTuple(args, "filter", 2, 2, &func, &seq))
Guido van Rossum12d12c51993-10-26 17:58:25 +0000216 return NULL;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000217
Tim Peters0e57abf2001-05-02 07:39:38 +0000218 /* Strings and tuples return a result of the same type. */
219 if (PyString_Check(seq))
220 return filterstring(func, seq);
Martin v. Löwis8afd7572003-01-25 22:46:11 +0000221#ifdef Py_USING_UNICODE
222 if (PyUnicode_Check(seq))
223 return filterunicode(func, seq);
224#endif
Tim Peters0e57abf2001-05-02 07:39:38 +0000225 if (PyTuple_Check(seq))
226 return filtertuple(func, seq);
227
Georg Brandle35b6572005-07-19 22:20:20 +0000228 /* Pre-allocate argument list tuple. */
229 arg = PyTuple_New(1);
230 if (arg == NULL)
231 return NULL;
232
Tim Peters0e57abf2001-05-02 07:39:38 +0000233 /* Get iterator. */
234 it = PyObject_GetIter(seq);
235 if (it == NULL)
Georg Brandle35b6572005-07-19 22:20:20 +0000236 goto Fail_arg;
Tim Peters0e57abf2001-05-02 07:39:38 +0000237
238 /* Guess a result list size. */
Armin Rigof5b3e362006-02-11 21:32:43 +0000239 len = _PyObject_LengthHint(seq);
Raymond Hettingerb86269d2004-01-04 11:00:08 +0000240 if (len < 0) {
Raymond Hettingera710b332005-08-21 11:03:59 +0000241 if (!PyErr_ExceptionMatches(PyExc_TypeError) &&
242 !PyErr_ExceptionMatches(PyExc_AttributeError)) {
243 goto Fail_it;
244 }
Raymond Hettingerb86269d2004-01-04 11:00:08 +0000245 PyErr_Clear();
246 len = 8; /* arbitrary */
Guido van Rossum12d12c51993-10-26 17:58:25 +0000247 }
248
Tim Peters0e57abf2001-05-02 07:39:38 +0000249 /* Get a result list. */
Guido van Rossum79f25d91997-04-29 20:08:16 +0000250 if (PyList_Check(seq) && seq->ob_refcnt == 1) {
Tim Peters0e57abf2001-05-02 07:39:38 +0000251 /* Eww - can modify the list in-place. */
Guido van Rossum79f25d91997-04-29 20:08:16 +0000252 Py_INCREF(seq);
Guido van Rossum12d12c51993-10-26 17:58:25 +0000253 result = seq;
254 }
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000255 else {
Tim Peters0e57abf2001-05-02 07:39:38 +0000256 result = PyList_New(len);
257 if (result == NULL)
258 goto Fail_it;
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000259 }
Guido van Rossum12d12c51993-10-26 17:58:25 +0000260
Tim Peters0e57abf2001-05-02 07:39:38 +0000261 /* Build the result list. */
Tim Petersf4848da2001-05-05 00:14:56 +0000262 j = 0;
263 for (;;) {
Guido van Rossumc7903a12002-08-16 07:04:56 +0000264 PyObject *item;
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000265 int ok;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000266
Tim Peters0e57abf2001-05-02 07:39:38 +0000267 item = PyIter_Next(it);
268 if (item == NULL) {
Tim Petersf4848da2001-05-05 00:14:56 +0000269 if (PyErr_Occurred())
270 goto Fail_result_it;
Tim Peters0e57abf2001-05-02 07:39:38 +0000271 break;
Guido van Rossum2d951851994-08-29 12:52:16 +0000272 }
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000273
Neil Schemenauer68973552003-08-14 20:37:34 +0000274 if (func == (PyObject *)&PyBool_Type || func == Py_None) {
Guido van Rossumc7903a12002-08-16 07:04:56 +0000275 ok = PyObject_IsTrue(item);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000276 }
277 else {
Guido van Rossumc7903a12002-08-16 07:04:56 +0000278 PyObject *good;
279 PyTuple_SET_ITEM(arg, 0, item);
280 good = PyObject_Call(func, arg, NULL);
281 PyTuple_SET_ITEM(arg, 0, NULL);
Guido van Rossum58b68731995-01-10 17:40:55 +0000282 if (good == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000283 Py_DECREF(item);
Tim Peters0e57abf2001-05-02 07:39:38 +0000284 goto Fail_result_it;
Guido van Rossum58b68731995-01-10 17:40:55 +0000285 }
Guido van Rossumc7903a12002-08-16 07:04:56 +0000286 ok = PyObject_IsTrue(good);
287 Py_DECREF(good);
Guido van Rossum12d12c51993-10-26 17:58:25 +0000288 }
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000289 if (ok) {
Tim Peters0e57abf2001-05-02 07:39:38 +0000290 if (j < len)
291 PyList_SET_ITEM(result, j, item);
Guido van Rossum2d951851994-08-29 12:52:16 +0000292 else {
Barry Warsawfa77e091999-01-28 18:49:12 +0000293 int status = PyList_Append(result, item);
Barry Warsawfa77e091999-01-28 18:49:12 +0000294 Py_DECREF(item);
295 if (status < 0)
Tim Peters0e57abf2001-05-02 07:39:38 +0000296 goto Fail_result_it;
Guido van Rossum2d951851994-08-29 12:52:16 +0000297 }
Tim Peters0e57abf2001-05-02 07:39:38 +0000298 ++j;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000299 }
Tim Peters0e57abf2001-05-02 07:39:38 +0000300 else
301 Py_DECREF(item);
Guido van Rossum12d12c51993-10-26 17:58:25 +0000302 }
303
Guido van Rossum12d12c51993-10-26 17:58:25 +0000304
Tim Peters0e57abf2001-05-02 07:39:38 +0000305 /* Cut back result list if len is too big. */
Guido van Rossum79f25d91997-04-29 20:08:16 +0000306 if (j < len && PyList_SetSlice(result, j, len, NULL) < 0)
Tim Peters0e57abf2001-05-02 07:39:38 +0000307 goto Fail_result_it;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000308
Tim Peters3c6b1482001-05-21 08:07:05 +0000309 Py_DECREF(it);
Guido van Rossumc7903a12002-08-16 07:04:56 +0000310 Py_DECREF(arg);
Guido van Rossum12d12c51993-10-26 17:58:25 +0000311 return result;
312
Tim Peters0e57abf2001-05-02 07:39:38 +0000313Fail_result_it:
Guido van Rossum79f25d91997-04-29 20:08:16 +0000314 Py_DECREF(result);
Tim Peters0e57abf2001-05-02 07:39:38 +0000315Fail_it:
316 Py_DECREF(it);
Guido van Rossumc7903a12002-08-16 07:04:56 +0000317Fail_arg:
318 Py_DECREF(arg);
Guido van Rossum12d12c51993-10-26 17:58:25 +0000319 return NULL;
320}
321
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000322PyDoc_STRVAR(filter_doc,
Tim Petersd50e5442002-03-09 00:06:26 +0000323"filter(function or None, sequence) -> list, tuple, or string\n"
324"\n"
325"Return those items of sequence for which function(item) is true. If\n"
326"function is None, return the items that are true. If sequence is a tuple\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000327"or string, return the same type, else return a list.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000328
Guido van Rossum79f25d91997-04-29 20:08:16 +0000329static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000330builtin_chr(PyObject *self, PyObject *args)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000331{
332 long x;
333 char s[1];
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000334
Guido van Rossum79f25d91997-04-29 20:08:16 +0000335 if (!PyArg_ParseTuple(args, "l:chr", &x))
Guido van Rossum3f5da241990-12-20 15:06:42 +0000336 return NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000337 if (x < 0 || x >= 256) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000338 PyErr_SetString(PyExc_ValueError,
339 "chr() arg not in range(256)");
Guido van Rossum3f5da241990-12-20 15:06:42 +0000340 return NULL;
341 }
Guido van Rossum6bf62da1997-04-11 20:37:35 +0000342 s[0] = (char)x;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000343 return PyString_FromStringAndSize(s, 1);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000344}
345
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000346PyDoc_STRVAR(chr_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000347"chr(i) -> character\n\
348\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000349Return a string of one character with ordinal i; 0 <= i < 256.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000350
351
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000352#ifdef Py_USING_UNICODE
Guido van Rossum79f25d91997-04-29 20:08:16 +0000353static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000354builtin_unichr(PyObject *self, PyObject *args)
Guido van Rossum09095f32000-03-10 23:00:52 +0000355{
356 long x;
Guido van Rossum09095f32000-03-10 23:00:52 +0000357
358 if (!PyArg_ParseTuple(args, "l:unichr", &x))
359 return NULL;
Fredrik Lundh0dcf67e2001-06-26 20:01:56 +0000360
Marc-André Lemburgcc8764c2002-08-11 12:23:04 +0000361 return PyUnicode_FromOrdinal(x);
Guido van Rossum09095f32000-03-10 23:00:52 +0000362}
363
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000364PyDoc_STRVAR(unichr_doc,
Fred Drake078b24f2000-04-13 02:42:50 +0000365"unichr(i) -> Unicode character\n\
Guido van Rossum09095f32000-03-10 23:00:52 +0000366\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000367Return a Unicode string of one character with ordinal i; 0 <= i <= 0x10ffff.");
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000368#endif
Guido van Rossum09095f32000-03-10 23:00:52 +0000369
370
371static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000372builtin_cmp(PyObject *self, PyObject *args)
Guido van Rossuma9e7dc11992-10-18 18:53:57 +0000373{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000374 PyObject *a, *b;
Guido van Rossum09df08a1998-05-22 00:51:39 +0000375 int c;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000376
Raymond Hettingerea3fdf42002-12-29 16:33:45 +0000377 if (!PyArg_UnpackTuple(args, "cmp", 2, 2, &a, &b))
Guido van Rossuma9e7dc11992-10-18 18:53:57 +0000378 return NULL;
Guido van Rossum09df08a1998-05-22 00:51:39 +0000379 if (PyObject_Cmp(a, b, &c) < 0)
Guido van Rossumc8b6df91997-05-23 00:06:51 +0000380 return NULL;
Guido van Rossum09df08a1998-05-22 00:51:39 +0000381 return PyInt_FromLong((long)c);
Guido van Rossuma9e7dc11992-10-18 18:53:57 +0000382}
383
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000384PyDoc_STRVAR(cmp_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000385"cmp(x, y) -> integer\n\
386\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000387Return negative if x<y, zero if x==y, positive if x>y.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000388
389
Guido van Rossum79f25d91997-04-29 20:08:16 +0000390static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000391builtin_coerce(PyObject *self, PyObject *args)
Guido van Rossum6a00cd81995-01-07 12:39:01 +0000392{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000393 PyObject *v, *w;
394 PyObject *res;
Guido van Rossum5524a591995-01-10 15:26:20 +0000395
Neal Norwitzdf25efe2007-05-23 06:58:36 +0000396 if (Py_Py3kWarningFlag &&
397 PyErr_Warn(PyExc_DeprecationWarning,
398 "coerce() not supported in 3.x") < 0)
399 return NULL;
400
Raymond Hettingerea3fdf42002-12-29 16:33:45 +0000401 if (!PyArg_UnpackTuple(args, "coerce", 2, 2, &v, &w))
Guido van Rossum5524a591995-01-10 15:26:20 +0000402 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000403 if (PyNumber_Coerce(&v, &w) < 0)
Guido van Rossum04691fc1992-08-12 15:35:34 +0000404 return NULL;
Raymond Hettinger8ae46892003-10-12 19:09:37 +0000405 res = PyTuple_Pack(2, v, w);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000406 Py_DECREF(v);
407 Py_DECREF(w);
Guido van Rossum04691fc1992-08-12 15:35:34 +0000408 return res;
409}
410
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000411PyDoc_STRVAR(coerce_doc,
Martin v. Löwis8d494f32004-08-25 10:42:41 +0000412"coerce(x, y) -> (x1, y1)\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000413\n\
Martin v. Löwis8d494f32004-08-25 10:42:41 +0000414Return a tuple consisting of the two numeric arguments converted to\n\
415a common type, using the same rules as used by arithmetic operations.\n\
416If coercion is not possible, raise TypeError.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000417
Guido van Rossum79f25d91997-04-29 20:08:16 +0000418static PyObject *
Georg Brandl5240d742007-03-13 20:46:32 +0000419builtin_compile(PyObject *self, PyObject *args, PyObject *kwds)
Guido van Rossum5b722181993-03-30 17:46:03 +0000420{
421 char *str;
422 char *filename;
423 char *startstr;
424 int start;
Tim Peters6cd6a822001-08-17 22:11:27 +0000425 int dont_inherit = 0;
426 int supplied_flags = 0;
Tim Peters5ba58662001-07-16 02:29:45 +0000427 PyCompilerFlags cf;
Neal Norwitz3a9a3e72005-11-27 20:38:31 +0000428 PyObject *result = NULL, *cmd, *tmp = NULL;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000429 Py_ssize_t length;
Georg Brandl5240d742007-03-13 20:46:32 +0000430 static char *kwlist[] = {"source", "filename", "mode", "flags",
431 "dont_inherit", NULL};
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000432
Georg Brandl5240d742007-03-13 20:46:32 +0000433 if (!PyArg_ParseTupleAndKeywords(args, kwds, "Oss|ii:compile",
434 kwlist, &cmd, &filename, &startstr,
435 &supplied_flags, &dont_inherit))
Guido van Rossum5b722181993-03-30 17:46:03 +0000436 return NULL;
Tim Peters6cd6a822001-08-17 22:11:27 +0000437
Just van Rossum3aaf42c2003-02-10 08:21:10 +0000438 cf.cf_flags = supplied_flags;
439
440#ifdef Py_USING_UNICODE
441 if (PyUnicode_Check(cmd)) {
442 tmp = PyUnicode_AsUTF8String(cmd);
443 if (tmp == NULL)
444 return NULL;
445 cmd = tmp;
446 cf.cf_flags |= PyCF_SOURCE_IS_UTF8;
447 }
448#endif
Just van Rossumb9b8e9c2003-02-10 09:22:01 +0000449 if (PyObject_AsReadBuffer(cmd, (const void **)&str, &length))
450 return NULL;
Tim Peters2c646c92003-02-10 14:48:29 +0000451 if ((size_t)length != strlen(str)) {
Just van Rossum3aaf42c2003-02-10 08:21:10 +0000452 PyErr_SetString(PyExc_TypeError,
Alex Martellia9b9c9f2003-04-23 13:34:35 +0000453 "compile() expected string without null bytes");
Neal Norwitz3a9a3e72005-11-27 20:38:31 +0000454 goto cleanup;
Just van Rossum3aaf42c2003-02-10 08:21:10 +0000455 }
456
Guido van Rossum5b722181993-03-30 17:46:03 +0000457 if (strcmp(startstr, "exec") == 0)
Guido van Rossumb05a5c71997-05-07 17:46:13 +0000458 start = Py_file_input;
Guido van Rossum5b722181993-03-30 17:46:03 +0000459 else if (strcmp(startstr, "eval") == 0)
Guido van Rossumb05a5c71997-05-07 17:46:13 +0000460 start = Py_eval_input;
Guido van Rossum872537c1995-07-07 22:43:42 +0000461 else if (strcmp(startstr, "single") == 0)
Guido van Rossumb05a5c71997-05-07 17:46:13 +0000462 start = Py_single_input;
Guido van Rossum5b722181993-03-30 17:46:03 +0000463 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000464 PyErr_SetString(PyExc_ValueError,
Fred Drake661ea262000-10-24 19:57:45 +0000465 "compile() arg 3 must be 'exec' or 'eval' or 'single'");
Neal Norwitz3a9a3e72005-11-27 20:38:31 +0000466 goto cleanup;
Guido van Rossum5b722181993-03-30 17:46:03 +0000467 }
Tim Peters6cd6a822001-08-17 22:11:27 +0000468
Guido van Rossum4b499dd32003-02-13 22:07:59 +0000469 if (supplied_flags &
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000470 ~(PyCF_MASK | PyCF_MASK_OBSOLETE | PyCF_DONT_IMPLY_DEDENT | PyCF_ONLY_AST))
Guido van Rossum4b499dd32003-02-13 22:07:59 +0000471 {
Tim Peters6cd6a822001-08-17 22:11:27 +0000472 PyErr_SetString(PyExc_ValueError,
473 "compile(): unrecognised flags");
Neal Norwitz3a9a3e72005-11-27 20:38:31 +0000474 goto cleanup;
Tim Peters6cd6a822001-08-17 22:11:27 +0000475 }
476 /* XXX Warn if (supplied_flags & PyCF_MASK_OBSOLETE) != 0? */
477
Tim Peters6cd6a822001-08-17 22:11:27 +0000478 if (!dont_inherit) {
479 PyEval_MergeCompilerFlags(&cf);
480 }
Just van Rossum3aaf42c2003-02-10 08:21:10 +0000481 result = Py_CompileStringFlags(str, filename, start, &cf);
Neal Norwitz3a9a3e72005-11-27 20:38:31 +0000482cleanup:
Just van Rossum3aaf42c2003-02-10 08:21:10 +0000483 Py_XDECREF(tmp);
484 return result;
Guido van Rossum5b722181993-03-30 17:46:03 +0000485}
486
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000487PyDoc_STRVAR(compile_doc,
Tim Peters6cd6a822001-08-17 22:11:27 +0000488"compile(source, filename, mode[, flags[, dont_inherit]]) -> code object\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000489\n\
490Compile the source string (a Python module, statement or expression)\n\
491into a code object that can be executed by the exec statement or eval().\n\
492The filename will be used for run-time error messages.\n\
493The mode must be 'exec' to compile a module, 'single' to compile a\n\
Tim Peters6cd6a822001-08-17 22:11:27 +0000494single (interactive) statement, or 'eval' to compile an expression.\n\
495The flags argument, if present, controls which future statements influence\n\
496the compilation of the code.\n\
497The dont_inherit argument, if non-zero, stops the compilation inheriting\n\
498the effects of any future statements in effect in the code calling\n\
499compile; if absent or zero these statements do influence the compilation,\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000500in addition to any features explicitly specified.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000501
Guido van Rossum79f25d91997-04-29 20:08:16 +0000502static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000503builtin_dir(PyObject *self, PyObject *args)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000504{
Tim Peters5d2b77c2001-09-03 05:47:38 +0000505 PyObject *arg = NULL;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000506
Raymond Hettingerea3fdf42002-12-29 16:33:45 +0000507 if (!PyArg_UnpackTuple(args, "dir", 0, 1, &arg))
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000508 return NULL;
Tim Peters7eea37e2001-09-04 22:08:56 +0000509 return PyObject_Dir(arg);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000510}
511
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000512PyDoc_STRVAR(dir_doc,
Tim Peters5d2b77c2001-09-03 05:47:38 +0000513"dir([object]) -> list of strings\n"
514"\n"
Georg Brandl871f1bc2007-03-12 13:17:36 +0000515"If called without an argument, return the names in the current scope.\n"
516"Else, return an alphabetized list of names comprising (some of) the attributes\n"
517"of the given object, and of attributes reachable from it.\n"
518"If the object supplies a method named __dir__, it will be used; otherwise\n"
519"the default dir() logic is used and returns:\n"
520" for a module object: the module's attributes.\n"
521" for a class object: its attributes, and recursively the attributes\n"
522" of its bases.\n"
Georg Brandl3bb15672007-03-13 07:23:16 +0000523" for any other object: its attributes, its class's attributes, and\n"
Georg Brandl871f1bc2007-03-12 13:17:36 +0000524" recursively the attributes of its class's base classes.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000525
Guido van Rossum79f25d91997-04-29 20:08:16 +0000526static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000527builtin_divmod(PyObject *self, PyObject *args)
Guido van Rossum6a00cd81995-01-07 12:39:01 +0000528{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000529 PyObject *v, *w;
Guido van Rossum6a00cd81995-01-07 12:39:01 +0000530
Raymond Hettingerea3fdf42002-12-29 16:33:45 +0000531 if (!PyArg_UnpackTuple(args, "divmod", 2, 2, &v, &w))
Guido van Rossum6a00cd81995-01-07 12:39:01 +0000532 return NULL;
Guido van Rossum09df08a1998-05-22 00:51:39 +0000533 return PyNumber_Divmod(v, w);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000534}
535
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000536PyDoc_STRVAR(divmod_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000537"divmod(x, y) -> (div, mod)\n\
538\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000539Return the tuple ((x-x%y)/y, x%y). Invariant: div*y + mod == x.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000540
541
Guido van Rossum79f25d91997-04-29 20:08:16 +0000542static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000543builtin_eval(PyObject *self, PyObject *args)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000544{
Just van Rossum3aaf42c2003-02-10 08:21:10 +0000545 PyObject *cmd, *result, *tmp = NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000546 PyObject *globals = Py_None, *locals = Py_None;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000547 char *str;
Tim Peters9fa96be2001-08-17 23:04:59 +0000548 PyCompilerFlags cf;
Guido van Rossum590baa41993-11-30 13:40:46 +0000549
Raymond Hettinger214b1c32004-07-02 06:41:07 +0000550 if (!PyArg_UnpackTuple(args, "eval", 1, 3, &cmd, &globals, &locals))
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000551 return NULL;
Raymond Hettinger214b1c32004-07-02 06:41:07 +0000552 if (locals != Py_None && !PyMapping_Check(locals)) {
553 PyErr_SetString(PyExc_TypeError, "locals must be a mapping");
Raymond Hettinger513ffe82004-07-06 13:44:41 +0000554 return NULL;
Raymond Hettinger214b1c32004-07-02 06:41:07 +0000555 }
556 if (globals != Py_None && !PyDict_Check(globals)) {
Georg Brandl99d7e4e2005-08-31 22:21:15 +0000557 PyErr_SetString(PyExc_TypeError, PyMapping_Check(globals) ?
Raymond Hettinger214b1c32004-07-02 06:41:07 +0000558 "globals must be a real dict; try eval(expr, {}, mapping)"
559 : "globals must be a dict");
Raymond Hettinger513ffe82004-07-06 13:44:41 +0000560 return NULL;
Raymond Hettinger214b1c32004-07-02 06:41:07 +0000561 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000562 if (globals == Py_None) {
563 globals = PyEval_GetGlobals();
564 if (locals == Py_None)
565 locals = PyEval_GetLocals();
Guido van Rossum6135a871995-01-09 17:53:26 +0000566 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000567 else if (locals == Py_None)
Guido van Rossum6135a871995-01-09 17:53:26 +0000568 locals = globals;
Tim Peters9fa96be2001-08-17 23:04:59 +0000569
Georg Brandl77c85e62005-09-15 10:46:13 +0000570 if (globals == NULL || locals == NULL) {
571 PyErr_SetString(PyExc_TypeError,
572 "eval must be given globals and locals "
573 "when called without a frame");
574 return NULL;
575 }
576
Guido van Rossum79f25d91997-04-29 20:08:16 +0000577 if (PyDict_GetItemString(globals, "__builtins__") == NULL) {
578 if (PyDict_SetItemString(globals, "__builtins__",
579 PyEval_GetBuiltins()) != 0)
Guido van Rossum6135a871995-01-09 17:53:26 +0000580 return NULL;
581 }
Tim Peters9fa96be2001-08-17 23:04:59 +0000582
Jeremy Hylton15c1c4f2001-07-30 21:50:55 +0000583 if (PyCode_Check(cmd)) {
Jeremy Hylton733c8932001-12-13 19:51:56 +0000584 if (PyCode_GetNumFree((PyCodeObject *)cmd) > 0) {
Jeremy Hylton15c1c4f2001-07-30 21:50:55 +0000585 PyErr_SetString(PyExc_TypeError,
586 "code object passed to eval() may not contain free variables");
587 return NULL;
588 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000589 return PyEval_EvalCode((PyCodeObject *) cmd, globals, locals);
Jeremy Hylton15c1c4f2001-07-30 21:50:55 +0000590 }
Tim Peters9fa96be2001-08-17 23:04:59 +0000591
Marc-André Lemburgd1ba4432000-09-19 21:04:18 +0000592 if (!PyString_Check(cmd) &&
593 !PyUnicode_Check(cmd)) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000594 PyErr_SetString(PyExc_TypeError,
Fred Drake661ea262000-10-24 19:57:45 +0000595 "eval() arg 1 must be a string or code object");
Guido van Rossum94390a41992-08-14 15:14:30 +0000596 return NULL;
597 }
Just van Rossum3aaf42c2003-02-10 08:21:10 +0000598 cf.cf_flags = 0;
599
600#ifdef Py_USING_UNICODE
601 if (PyUnicode_Check(cmd)) {
602 tmp = PyUnicode_AsUTF8String(cmd);
603 if (tmp == NULL)
604 return NULL;
605 cmd = tmp;
606 cf.cf_flags |= PyCF_SOURCE_IS_UTF8;
607 }
608#endif
Neal Norwitz3a9a3e72005-11-27 20:38:31 +0000609 if (PyString_AsStringAndSize(cmd, &str, NULL)) {
610 Py_XDECREF(tmp);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000611 return NULL;
Neal Norwitz3a9a3e72005-11-27 20:38:31 +0000612 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000613 while (*str == ' ' || *str == '\t')
614 str++;
Tim Peters9fa96be2001-08-17 23:04:59 +0000615
Tim Peters9fa96be2001-08-17 23:04:59 +0000616 (void)PyEval_MergeCompilerFlags(&cf);
Just van Rossum3aaf42c2003-02-10 08:21:10 +0000617 result = PyRun_StringFlags(str, Py_eval_input, globals, locals, &cf);
618 Py_XDECREF(tmp);
619 return result;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000620}
621
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000622PyDoc_STRVAR(eval_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000623"eval(source[, globals[, locals]]) -> value\n\
624\n\
625Evaluate the source in the context of globals and locals.\n\
626The source may be a string representing a Python expression\n\
627or a code object as returned by compile().\n\
Neal Norwitz477ca1c2006-09-05 02:25:41 +0000628The globals must be a dictionary and locals can be any mapping,\n\
Raymond Hettinger214b1c32004-07-02 06:41:07 +0000629defaulting to the current globals and locals.\n\
630If only globals is given, locals defaults to it.\n");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000631
632
Guido van Rossum79f25d91997-04-29 20:08:16 +0000633static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000634builtin_execfile(PyObject *self, PyObject *args)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000635{
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000636 char *filename;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000637 PyObject *globals = Py_None, *locals = Py_None;
638 PyObject *res;
Guido van Rossumb3a639e2001-09-05 13:37:47 +0000639 FILE* fp = NULL;
Tim Peters5ba58662001-07-16 02:29:45 +0000640 PyCompilerFlags cf;
Martin v. Löwis6b3a2c42001-08-08 05:30:36 +0000641 int exists;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000642
Neal Norwitzdf25efe2007-05-23 06:58:36 +0000643 if (Py_Py3kWarningFlag &&
644 PyErr_Warn(PyExc_DeprecationWarning,
645 "execfile() not supported in 3.x") < 0)
646 return NULL;
647
Raymond Hettinger66bd2332004-08-02 08:30:07 +0000648 if (!PyArg_ParseTuple(args, "s|O!O:execfile",
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000649 &filename,
Guido van Rossum79f25d91997-04-29 20:08:16 +0000650 &PyDict_Type, &globals,
Raymond Hettinger66bd2332004-08-02 08:30:07 +0000651 &locals))
Guido van Rossum0f61f8a1992-02-25 18:55:05 +0000652 return NULL;
Raymond Hettinger66bd2332004-08-02 08:30:07 +0000653 if (locals != Py_None && !PyMapping_Check(locals)) {
654 PyErr_SetString(PyExc_TypeError, "locals must be a mapping");
655 return NULL;
656 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000657 if (globals == Py_None) {
658 globals = PyEval_GetGlobals();
659 if (locals == Py_None)
660 locals = PyEval_GetLocals();
Guido van Rossum6135a871995-01-09 17:53:26 +0000661 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000662 else if (locals == Py_None)
Guido van Rossum6135a871995-01-09 17:53:26 +0000663 locals = globals;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000664 if (PyDict_GetItemString(globals, "__builtins__") == NULL) {
665 if (PyDict_SetItemString(globals, "__builtins__",
666 PyEval_GetBuiltins()) != 0)
Guido van Rossum6135a871995-01-09 17:53:26 +0000667 return NULL;
668 }
Martin v. Löwis6b3a2c42001-08-08 05:30:36 +0000669
670 exists = 0;
671 /* Test for existence or directory. */
Martin v. Löwis3484a182002-03-09 12:07:51 +0000672#if defined(PLAN9)
673 {
674 Dir *d;
675
676 if ((d = dirstat(filename))!=nil) {
677 if(d->mode & DMDIR)
678 werrstr("is a directory");
679 else
680 exists = 1;
681 free(d);
682 }
Martin v. Löwis6b3a2c42001-08-08 05:30:36 +0000683 }
Martin v. Löwis3484a182002-03-09 12:07:51 +0000684#elif defined(RISCOS)
Guido van Rossume2ae77b2001-10-24 20:42:55 +0000685 if (object_exists(filename)) {
686 if (isdir(filename))
687 errno = EISDIR;
688 else
689 exists = 1;
690 }
Martin v. Löwis3484a182002-03-09 12:07:51 +0000691#else /* standard Posix */
692 {
693 struct stat s;
694 if (stat(filename, &s) == 0) {
695 if (S_ISDIR(s.st_mode))
Andrew MacIntyreda4d6cb2004-03-29 11:53:38 +0000696# if defined(PYOS_OS2) && defined(PYCC_VACPP)
Martin v. Löwis3484a182002-03-09 12:07:51 +0000697 errno = EOS2ERR;
698# else
699 errno = EISDIR;
700# endif
701 else
702 exists = 1;
703 }
704 }
705#endif
Martin v. Löwis6b3a2c42001-08-08 05:30:36 +0000706
707 if (exists) {
708 Py_BEGIN_ALLOW_THREADS
Jack Jansen7b8c7542002-04-14 20:12:41 +0000709 fp = fopen(filename, "r" PY_STDIOTEXTMODE);
Martin v. Löwis6b3a2c42001-08-08 05:30:36 +0000710 Py_END_ALLOW_THREADS
711
712 if (fp == NULL) {
713 exists = 0;
714 }
715 }
716
717 if (!exists) {
Peter Schneider-Kamp4c013422002-08-27 16:58:00 +0000718 PyErr_SetFromErrnoWithFilename(PyExc_IOError, filename);
Guido van Rossum0f61f8a1992-02-25 18:55:05 +0000719 return NULL;
720 }
Tim Peters5ba58662001-07-16 02:29:45 +0000721 cf.cf_flags = 0;
722 if (PyEval_MergeCompilerFlags(&cf))
Tim Peters748b8bb2001-04-28 08:20:22 +0000723 res = PyRun_FileExFlags(fp, filename, Py_file_input, globals,
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000724 locals, 1, &cf);
Tim Peters5ba58662001-07-16 02:29:45 +0000725 else
Tim Peters748b8bb2001-04-28 08:20:22 +0000726 res = PyRun_FileEx(fp, filename, Py_file_input, globals,
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000727 locals, 1);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000728 return res;
Guido van Rossum0f61f8a1992-02-25 18:55:05 +0000729}
730
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000731PyDoc_STRVAR(execfile_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000732"execfile(filename[, globals[, locals]])\n\
733\n\
734Read and execute a Python script from a file.\n\
735The globals and locals are dictionaries, defaulting to the current\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000736globals and locals. If only globals is given, locals defaults to it.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000737
738
Guido van Rossum79f25d91997-04-29 20:08:16 +0000739static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000740builtin_getattr(PyObject *self, PyObject *args)
Guido van Rossum33894be1992-01-27 16:53:09 +0000741{
Guido van Rossum950ff291998-06-29 13:38:57 +0000742 PyObject *v, *result, *dflt = NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000743 PyObject *name;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000744
Raymond Hettingerea3fdf42002-12-29 16:33:45 +0000745 if (!PyArg_UnpackTuple(args, "getattr", 2, 3, &v, &name, &dflt))
Guido van Rossum33894be1992-01-27 16:53:09 +0000746 return NULL;
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000747#ifdef Py_USING_UNICODE
Jeremy Hylton0eb11152001-07-30 22:39:31 +0000748 if (PyUnicode_Check(name)) {
749 name = _PyUnicode_AsDefaultEncodedString(name, NULL);
750 if (name == NULL)
751 return NULL;
752 }
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000753#endif
Jeremy Hylton0eb11152001-07-30 22:39:31 +0000754
755 if (!PyString_Check(name)) {
756 PyErr_SetString(PyExc_TypeError,
Alex Martellia9b9c9f2003-04-23 13:34:35 +0000757 "getattr(): attribute name must be string");
Jeremy Hylton0eb11152001-07-30 22:39:31 +0000758 return NULL;
759 }
Guido van Rossum950ff291998-06-29 13:38:57 +0000760 result = PyObject_GetAttr(v, name);
Guido van Rossumd8923572001-10-16 21:31:32 +0000761 if (result == NULL && dflt != NULL &&
762 PyErr_ExceptionMatches(PyExc_AttributeError))
763 {
Guido van Rossum950ff291998-06-29 13:38:57 +0000764 PyErr_Clear();
765 Py_INCREF(dflt);
766 result = dflt;
767 }
768 return result;
Guido van Rossum9bfef441993-03-29 10:43:31 +0000769}
770
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000771PyDoc_STRVAR(getattr_doc,
Guido van Rossum950ff291998-06-29 13:38:57 +0000772"getattr(object, name[, default]) -> value\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000773\n\
Guido van Rossum950ff291998-06-29 13:38:57 +0000774Get a named attribute from an object; getattr(x, 'y') is equivalent to x.y.\n\
775When a default argument is given, it is returned when the attribute doesn't\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000776exist; without it, an exception is raised in that case.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000777
778
Guido van Rossum79f25d91997-04-29 20:08:16 +0000779static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000780builtin_globals(PyObject *self)
Guido van Rossum872537c1995-07-07 22:43:42 +0000781{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000782 PyObject *d;
Guido van Rossum872537c1995-07-07 22:43:42 +0000783
Guido van Rossum79f25d91997-04-29 20:08:16 +0000784 d = PyEval_GetGlobals();
Neal Norwitz43bd4db2006-08-12 01:46:42 +0000785 Py_XINCREF(d);
Guido van Rossum872537c1995-07-07 22:43:42 +0000786 return d;
787}
788
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000789PyDoc_STRVAR(globals_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000790"globals() -> dictionary\n\
791\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000792Return the dictionary containing the current scope's global variables.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000793
794
Guido van Rossum79f25d91997-04-29 20:08:16 +0000795static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000796builtin_hasattr(PyObject *self, PyObject *args)
Guido van Rossum9bfef441993-03-29 10:43:31 +0000797{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000798 PyObject *v;
799 PyObject *name;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000800
Raymond Hettingerea3fdf42002-12-29 16:33:45 +0000801 if (!PyArg_UnpackTuple(args, "hasattr", 2, 2, &v, &name))
Guido van Rossum9bfef441993-03-29 10:43:31 +0000802 return NULL;
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000803#ifdef Py_USING_UNICODE
Jeremy Hylton302b54a2001-07-30 22:45:19 +0000804 if (PyUnicode_Check(name)) {
805 name = _PyUnicode_AsDefaultEncodedString(name, NULL);
806 if (name == NULL)
807 return NULL;
808 }
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000809#endif
Jeremy Hylton302b54a2001-07-30 22:45:19 +0000810
811 if (!PyString_Check(name)) {
812 PyErr_SetString(PyExc_TypeError,
Alex Martellia9b9c9f2003-04-23 13:34:35 +0000813 "hasattr(): attribute name must be string");
Jeremy Hylton302b54a2001-07-30 22:45:19 +0000814 return NULL;
815 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000816 v = PyObject_GetAttr(v, name);
Guido van Rossum9bfef441993-03-29 10:43:31 +0000817 if (v == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000818 PyErr_Clear();
Guido van Rossum09df08a1998-05-22 00:51:39 +0000819 Py_INCREF(Py_False);
820 return Py_False;
Guido van Rossum9bfef441993-03-29 10:43:31 +0000821 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000822 Py_DECREF(v);
Guido van Rossum09df08a1998-05-22 00:51:39 +0000823 Py_INCREF(Py_True);
824 return Py_True;
Guido van Rossum33894be1992-01-27 16:53:09 +0000825}
826
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000827PyDoc_STRVAR(hasattr_doc,
Guido van Rossum77f6a652002-04-03 22:41:51 +0000828"hasattr(object, name) -> bool\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000829\n\
830Return whether the object has an attribute with the given name.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000831(This is done by calling getattr(object, name) and catching exceptions.)");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000832
833
Guido van Rossum79f25d91997-04-29 20:08:16 +0000834static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000835builtin_id(PyObject *self, PyObject *v)
Guido van Rossum5b722181993-03-30 17:46:03 +0000836{
Guido van Rossum106f2da2000-06-28 21:12:25 +0000837 return PyLong_FromVoidPtr(v);
Guido van Rossum5b722181993-03-30 17:46:03 +0000838}
839
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000840PyDoc_STRVAR(id_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000841"id(object) -> integer\n\
842\n\
843Return the identity of an object. This is guaranteed to be unique among\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000844simultaneously existing objects. (Hint: it's the object's memory address.)");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000845
846
Guido van Rossum79f25d91997-04-29 20:08:16 +0000847static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000848builtin_map(PyObject *self, PyObject *args)
Guido van Rossum12d12c51993-10-26 17:58:25 +0000849{
850 typedef struct {
Tim Peters4e9afdc2001-05-03 23:54:49 +0000851 PyObject *it; /* the iterator object */
852 int saw_StopIteration; /* bool: did the iterator end? */
Guido van Rossum12d12c51993-10-26 17:58:25 +0000853 } sequence;
854
Guido van Rossum79f25d91997-04-29 20:08:16 +0000855 PyObject *func, *result;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000856 sequence *seqs = NULL, *sqp;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000857 Py_ssize_t n, len;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000858 register int i, j;
859
Guido van Rossum79f25d91997-04-29 20:08:16 +0000860 n = PyTuple_Size(args);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000861 if (n < 2) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000862 PyErr_SetString(PyExc_TypeError,
863 "map() requires at least two args");
Guido van Rossum12d12c51993-10-26 17:58:25 +0000864 return NULL;
865 }
866
Guido van Rossum79f25d91997-04-29 20:08:16 +0000867 func = PyTuple_GetItem(args, 0);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000868 n--;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000869
Guido van Rossumfa4ac711998-07-10 17:37:30 +0000870 if (func == Py_None && n == 1) {
871 /* map(None, S) is the same as list(S). */
872 return PySequence_List(PyTuple_GetItem(args, 1));
873 }
874
Tim Peters4e9afdc2001-05-03 23:54:49 +0000875 /* Get space for sequence descriptors. Must NULL out the iterator
876 * pointers so that jumping to Fail_2 later doesn't see trash.
877 */
Guido van Rossum79f25d91997-04-29 20:08:16 +0000878 if ((seqs = PyMem_NEW(sequence, n)) == NULL) {
879 PyErr_NoMemory();
Tim Peters4e9afdc2001-05-03 23:54:49 +0000880 return NULL;
881 }
882 for (i = 0; i < n; ++i) {
883 seqs[i].it = (PyObject*)NULL;
884 seqs[i].saw_StopIteration = 0;
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000885 }
Guido van Rossum12d12c51993-10-26 17:58:25 +0000886
Tim Peters4e9afdc2001-05-03 23:54:49 +0000887 /* Do a first pass to obtain iterators for the arguments, and set len
888 * to the largest of their lengths.
Tim Peters748b8bb2001-04-28 08:20:22 +0000889 */
Tim Peters4e9afdc2001-05-03 23:54:49 +0000890 len = 0;
891 for (i = 0, sqp = seqs; i < n; ++i, ++sqp) {
892 PyObject *curseq;
Martin v. Löwisd96ee902006-02-16 14:37:16 +0000893 Py_ssize_t curlen;
Guido van Rossumad991772001-01-12 16:03:05 +0000894
Tim Peters4e9afdc2001-05-03 23:54:49 +0000895 /* Get iterator. */
896 curseq = PyTuple_GetItem(args, i+1);
897 sqp->it = PyObject_GetIter(curseq);
898 if (sqp->it == NULL) {
Guido van Rossum12d12c51993-10-26 17:58:25 +0000899 static char errmsg[] =
Tim Peters4e9afdc2001-05-03 23:54:49 +0000900 "argument %d to map() must support iteration";
Guido van Rossum15e33a41997-04-30 19:00:27 +0000901 char errbuf[sizeof(errmsg) + 25];
Jeremy Hylton518ab1c2001-11-28 20:42:20 +0000902 PyOS_snprintf(errbuf, sizeof(errbuf), errmsg, i+2);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000903 PyErr_SetString(PyExc_TypeError, errbuf);
Guido van Rossum12d12c51993-10-26 17:58:25 +0000904 goto Fail_2;
905 }
906
Tim Peters4e9afdc2001-05-03 23:54:49 +0000907 /* Update len. */
Armin Rigof5b3e362006-02-11 21:32:43 +0000908 curlen = _PyObject_LengthHint(curseq);
Raymond Hettinger77f3c872004-01-04 08:54:44 +0000909 if (curlen < 0) {
Raymond Hettingera710b332005-08-21 11:03:59 +0000910 if (!PyErr_ExceptionMatches(PyExc_TypeError) &&
911 !PyErr_ExceptionMatches(PyExc_AttributeError)) {
912 goto Fail_2;
913 }
Raymond Hettinger77f3c872004-01-04 08:54:44 +0000914 PyErr_Clear();
Tim Peters4e9afdc2001-05-03 23:54:49 +0000915 curlen = 8; /* arbitrary */
Raymond Hettinger77f3c872004-01-04 08:54:44 +0000916 }
917 if (curlen > len)
918 len = curlen;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000919 }
920
Tim Peters4e9afdc2001-05-03 23:54:49 +0000921 /* Get space for the result list. */
Guido van Rossum79f25d91997-04-29 20:08:16 +0000922 if ((result = (PyObject *) PyList_New(len)) == NULL)
Guido van Rossum12d12c51993-10-26 17:58:25 +0000923 goto Fail_2;
924
Tim Peters4e9afdc2001-05-03 23:54:49 +0000925 /* Iterate over the sequences until all have stopped. */
Guido van Rossum2d951851994-08-29 12:52:16 +0000926 for (i = 0; ; ++i) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000927 PyObject *alist, *item=NULL, *value;
Tim Peters4e9afdc2001-05-03 23:54:49 +0000928 int numactive = 0;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000929
Guido van Rossum79f25d91997-04-29 20:08:16 +0000930 if (func == Py_None && n == 1)
Guido van Rossum32120311995-07-10 13:52:21 +0000931 alist = NULL;
Tim Peters4e9afdc2001-05-03 23:54:49 +0000932 else if ((alist = PyTuple_New(n)) == NULL)
933 goto Fail_1;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000934
935 for (j = 0, sqp = seqs; j < n; ++j, ++sqp) {
Tim Peters4e9afdc2001-05-03 23:54:49 +0000936 if (sqp->saw_StopIteration) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000937 Py_INCREF(Py_None);
938 item = Py_None;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000939 }
Guido van Rossum12d12c51993-10-26 17:58:25 +0000940 else {
Tim Peters4e9afdc2001-05-03 23:54:49 +0000941 item = PyIter_Next(sqp->it);
942 if (item)
943 ++numactive;
944 else {
Tim Peters4e9afdc2001-05-03 23:54:49 +0000945 if (PyErr_Occurred()) {
Tim Petersf4848da2001-05-05 00:14:56 +0000946 Py_XDECREF(alist);
947 goto Fail_1;
Guido van Rossum2d951851994-08-29 12:52:16 +0000948 }
Tim Peters4e9afdc2001-05-03 23:54:49 +0000949 Py_INCREF(Py_None);
950 item = Py_None;
951 sqp->saw_StopIteration = 1;
Guido van Rossum2d951851994-08-29 12:52:16 +0000952 }
Guido van Rossum12d12c51993-10-26 17:58:25 +0000953 }
Tim Peters4e9afdc2001-05-03 23:54:49 +0000954 if (alist)
955 PyTuple_SET_ITEM(alist, j, item);
956 else
Guido van Rossum2d951851994-08-29 12:52:16 +0000957 break;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000958 }
959
Guido van Rossum32120311995-07-10 13:52:21 +0000960 if (!alist)
961 alist = item;
Guido van Rossum2d951851994-08-29 12:52:16 +0000962
Tim Peters4e9afdc2001-05-03 23:54:49 +0000963 if (numactive == 0) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000964 Py_DECREF(alist);
Guido van Rossum2d951851994-08-29 12:52:16 +0000965 break;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000966 }
Guido van Rossum2d951851994-08-29 12:52:16 +0000967
Guido van Rossum79f25d91997-04-29 20:08:16 +0000968 if (func == Py_None)
Guido van Rossum32120311995-07-10 13:52:21 +0000969 value = alist;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000970 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000971 value = PyEval_CallObject(func, alist);
972 Py_DECREF(alist);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000973 if (value == NULL)
974 goto Fail_1;
Guido van Rossum2d951851994-08-29 12:52:16 +0000975 }
976 if (i >= len) {
Barry Warsawfa77e091999-01-28 18:49:12 +0000977 int status = PyList_Append(result, value);
Barry Warsaw21332871999-01-28 04:21:35 +0000978 Py_DECREF(value);
Barry Warsawfa77e091999-01-28 18:49:12 +0000979 if (status < 0)
980 goto Fail_1;
Guido van Rossum2d951851994-08-29 12:52:16 +0000981 }
Tim Peters4e9afdc2001-05-03 23:54:49 +0000982 else if (PyList_SetItem(result, i, value) < 0)
983 goto Fail_1;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000984 }
985
Guido van Rossumfa4ac711998-07-10 17:37:30 +0000986 if (i < len && PyList_SetSlice(result, i, len, NULL) < 0)
987 goto Fail_1;
988
Tim Peters4e9afdc2001-05-03 23:54:49 +0000989 goto Succeed;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000990
Guido van Rossum12d12c51993-10-26 17:58:25 +0000991Fail_1:
Guido van Rossum79f25d91997-04-29 20:08:16 +0000992 Py_DECREF(result);
Guido van Rossum12d12c51993-10-26 17:58:25 +0000993Fail_2:
Tim Peters4e9afdc2001-05-03 23:54:49 +0000994 result = NULL;
995Succeed:
996 assert(seqs);
997 for (i = 0; i < n; ++i)
998 Py_XDECREF(seqs[i].it);
999 PyMem_DEL(seqs);
1000 return result;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001001}
1002
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001003PyDoc_STRVAR(map_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001004"map(function, sequence[, sequence, ...]) -> list\n\
1005\n\
1006Return a list of the results of applying the function to the items of\n\
1007the argument sequence(s). If more than one sequence is given, the\n\
1008function is called with an argument list consisting of the corresponding\n\
1009item of each sequence, substituting None for missing values when not all\n\
1010sequences have the same length. If the function is None, return a list of\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001011the items of the sequence (or a list of tuples if more than one sequence).");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001012
1013
Guido van Rossum79f25d91997-04-29 20:08:16 +00001014static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001015builtin_setattr(PyObject *self, PyObject *args)
Guido van Rossum33894be1992-01-27 16:53:09 +00001016{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001017 PyObject *v;
1018 PyObject *name;
1019 PyObject *value;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001020
Raymond Hettingerea3fdf42002-12-29 16:33:45 +00001021 if (!PyArg_UnpackTuple(args, "setattr", 3, 3, &v, &name, &value))
Guido van Rossum33894be1992-01-27 16:53:09 +00001022 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001023 if (PyObject_SetAttr(v, name, value) != 0)
Guido van Rossum33894be1992-01-27 16:53:09 +00001024 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001025 Py_INCREF(Py_None);
1026 return Py_None;
Guido van Rossum33894be1992-01-27 16:53:09 +00001027}
1028
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001029PyDoc_STRVAR(setattr_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001030"setattr(object, name, value)\n\
1031\n\
1032Set a named attribute on an object; setattr(x, 'y', v) is equivalent to\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001033``x.y = v''.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001034
1035
Guido van Rossum79f25d91997-04-29 20:08:16 +00001036static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001037builtin_delattr(PyObject *self, PyObject *args)
Guido van Rossum14144fc1994-08-29 12:53:40 +00001038{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001039 PyObject *v;
1040 PyObject *name;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001041
Raymond Hettingerea3fdf42002-12-29 16:33:45 +00001042 if (!PyArg_UnpackTuple(args, "delattr", 2, 2, &v, &name))
Guido van Rossum14144fc1994-08-29 12:53:40 +00001043 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001044 if (PyObject_SetAttr(v, name, (PyObject *)NULL) != 0)
Guido van Rossum14144fc1994-08-29 12:53:40 +00001045 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001046 Py_INCREF(Py_None);
1047 return Py_None;
Guido van Rossum14144fc1994-08-29 12:53:40 +00001048}
1049
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001050PyDoc_STRVAR(delattr_doc,
Guido van Rossumdf12a591998-11-23 22:13:04 +00001051"delattr(object, name)\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001052\n\
1053Delete a named attribute on an object; delattr(x, 'y') is equivalent to\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001054``del x.y''.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001055
1056
Guido van Rossum79f25d91997-04-29 20:08:16 +00001057static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001058builtin_hash(PyObject *self, PyObject *v)
Guido van Rossum9bfef441993-03-29 10:43:31 +00001059{
Guido van Rossum9bfef441993-03-29 10:43:31 +00001060 long x;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001061
Guido van Rossum79f25d91997-04-29 20:08:16 +00001062 x = PyObject_Hash(v);
Guido van Rossum9bfef441993-03-29 10:43:31 +00001063 if (x == -1)
1064 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001065 return PyInt_FromLong(x);
Guido van Rossum9bfef441993-03-29 10:43:31 +00001066}
1067
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001068PyDoc_STRVAR(hash_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001069"hash(object) -> integer\n\
1070\n\
1071Return a hash value for the object. Two objects with the same value have\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001072the same hash value. The reverse is not necessarily true, but likely.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001073
1074
Guido van Rossum79f25d91997-04-29 20:08:16 +00001075static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001076builtin_hex(PyObject *self, PyObject *v)
Guido van Rossum006bcd41991-10-24 14:54:44 +00001077{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001078 PyNumberMethods *nb;
Neil Schemenauer3a313e32004-07-19 16:29:17 +00001079 PyObject *res;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001080
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001081 if ((nb = v->ob_type->tp_as_number) == NULL ||
Guido van Rossum1899c2e1992-09-12 11:09:23 +00001082 nb->nb_hex == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001083 PyErr_SetString(PyExc_TypeError,
Guido van Rossum1899c2e1992-09-12 11:09:23 +00001084 "hex() argument can't be converted to hex");
1085 return NULL;
Guido van Rossum006bcd41991-10-24 14:54:44 +00001086 }
Neil Schemenauer3a313e32004-07-19 16:29:17 +00001087 res = (*nb->nb_hex)(v);
1088 if (res && !PyString_Check(res)) {
1089 PyErr_Format(PyExc_TypeError,
1090 "__hex__ returned non-string (type %.200s)",
1091 res->ob_type->tp_name);
1092 Py_DECREF(res);
1093 return NULL;
1094 }
1095 return res;
Guido van Rossum006bcd41991-10-24 14:54:44 +00001096}
1097
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001098PyDoc_STRVAR(hex_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001099"hex(number) -> string\n\
1100\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001101Return the hexadecimal representation of an integer or long integer.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001102
1103
Tim Petersdbd9ba62000-07-09 03:09:57 +00001104static PyObject *builtin_raw_input(PyObject *, PyObject *);
Guido van Rossum3165fe61992-09-25 21:59:05 +00001105
Guido van Rossum79f25d91997-04-29 20:08:16 +00001106static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001107builtin_input(PyObject *self, PyObject *args)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001108{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001109 PyObject *line;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001110 char *str;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001111 PyObject *res;
1112 PyObject *globals, *locals;
Hye-Shik Changff83c2b2004-02-02 13:39:01 +00001113 PyCompilerFlags cf;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001114
1115 line = builtin_raw_input(self, args);
Guido van Rossum3165fe61992-09-25 21:59:05 +00001116 if (line == NULL)
1117 return line;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001118 if (!PyArg_Parse(line, "s;embedded '\\0' in input line", &str))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001119 return NULL;
1120 while (*str == ' ' || *str == '\t')
1121 str++;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001122 globals = PyEval_GetGlobals();
1123 locals = PyEval_GetLocals();
1124 if (PyDict_GetItemString(globals, "__builtins__") == NULL) {
1125 if (PyDict_SetItemString(globals, "__builtins__",
1126 PyEval_GetBuiltins()) != 0)
Guido van Rossum6135a871995-01-09 17:53:26 +00001127 return NULL;
1128 }
Hye-Shik Changff83c2b2004-02-02 13:39:01 +00001129 cf.cf_flags = 0;
1130 PyEval_MergeCompilerFlags(&cf);
1131 res = PyRun_StringFlags(str, Py_eval_input, globals, locals, &cf);
Guido van Rossum79f25d91997-04-29 20:08:16 +00001132 Py_DECREF(line);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001133 return res;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001134}
1135
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001136PyDoc_STRVAR(input_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001137"input([prompt]) -> value\n\
1138\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001139Equivalent to eval(raw_input(prompt)).");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001140
1141
Guido van Rossume8811f81997-02-14 15:48:05 +00001142static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001143builtin_intern(PyObject *self, PyObject *args)
Guido van Rossume8811f81997-02-14 15:48:05 +00001144{
1145 PyObject *s;
Guido van Rossum43713e52000-02-29 13:59:29 +00001146 if (!PyArg_ParseTuple(args, "S:intern", &s))
Guido van Rossume8811f81997-02-14 15:48:05 +00001147 return NULL;
Jeremy Hylton4c989dd2004-08-07 19:20:05 +00001148 if (!PyString_CheckExact(s)) {
1149 PyErr_SetString(PyExc_TypeError,
1150 "can't intern subclass of string");
1151 return NULL;
1152 }
Guido van Rossume8811f81997-02-14 15:48:05 +00001153 Py_INCREF(s);
1154 PyString_InternInPlace(&s);
1155 return s;
1156}
1157
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001158PyDoc_STRVAR(intern_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001159"intern(string) -> string\n\
1160\n\
1161``Intern'' the given string. This enters the string in the (global)\n\
1162table of interned strings whose purpose is to speed up dictionary lookups.\n\
1163Return the string itself or the previously interned string object with the\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001164same value.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001165
1166
Guido van Rossum79f25d91997-04-29 20:08:16 +00001167static PyObject *
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001168builtin_iter(PyObject *self, PyObject *args)
1169{
1170 PyObject *v, *w = NULL;
1171
Raymond Hettingerea3fdf42002-12-29 16:33:45 +00001172 if (!PyArg_UnpackTuple(args, "iter", 1, 2, &v, &w))
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001173 return NULL;
1174 if (w == NULL)
1175 return PyObject_GetIter(v);
1176 if (!PyCallable_Check(v)) {
1177 PyErr_SetString(PyExc_TypeError,
1178 "iter(v, w): v must be callable");
1179 return NULL;
1180 }
1181 return PyCallIter_New(v, w);
1182}
1183
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001184PyDoc_STRVAR(iter_doc,
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001185"iter(collection) -> iterator\n\
1186iter(callable, sentinel) -> iterator\n\
1187\n\
1188Get an iterator from an object. In the first form, the argument must\n\
1189supply its own iterator, or be a sequence.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001190In the second form, the callable is called until it returns the sentinel.");
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001191
1192
1193static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001194builtin_len(PyObject *self, PyObject *v)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001195{
Martin v. Löwis18e16552006-02-15 17:27:45 +00001196 Py_ssize_t res;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001197
Jeremy Hylton03657cf2000-07-12 13:05:33 +00001198 res = PyObject_Size(v);
Guido van Rossum8ea9f4d1998-06-29 22:26:50 +00001199 if (res < 0 && PyErr_Occurred())
1200 return NULL;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001201 return PyInt_FromSsize_t(res);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001202}
1203
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001204PyDoc_STRVAR(len_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001205"len(object) -> integer\n\
1206\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001207Return the number of items of a sequence or mapping.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001208
1209
Guido van Rossum79f25d91997-04-29 20:08:16 +00001210static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001211builtin_locals(PyObject *self)
Guido van Rossum872537c1995-07-07 22:43:42 +00001212{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001213 PyObject *d;
Guido van Rossum872537c1995-07-07 22:43:42 +00001214
Guido van Rossum79f25d91997-04-29 20:08:16 +00001215 d = PyEval_GetLocals();
Neal Norwitz43bd4db2006-08-12 01:46:42 +00001216 Py_XINCREF(d);
Guido van Rossum872537c1995-07-07 22:43:42 +00001217 return d;
1218}
1219
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001220PyDoc_STRVAR(locals_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001221"locals() -> dictionary\n\
1222\n\
Raymond Hettinger69bf8f32003-01-04 02:16:22 +00001223Update and return a dictionary containing the current scope's local variables.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001224
1225
Guido van Rossum79f25d91997-04-29 20:08:16 +00001226static PyObject *
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001227min_max(PyObject *args, PyObject *kwds, int op)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001228{
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001229 PyObject *v, *it, *item, *val, *maxitem, *maxval, *keyfunc=NULL;
Martin v. Löwisfd39ad42004-08-12 14:42:37 +00001230 const char *name = op == Py_LT ? "min" : "max";
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001231
Guido van Rossum79f25d91997-04-29 20:08:16 +00001232 if (PyTuple_Size(args) > 1)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001233 v = args;
Martin v. Löwisfd39ad42004-08-12 14:42:37 +00001234 else if (!PyArg_UnpackTuple(args, (char *)name, 1, 1, &v))
Guido van Rossum3f5da241990-12-20 15:06:42 +00001235 return NULL;
Tim Peters67d687a2002-04-29 21:27:32 +00001236
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001237 if (kwds != NULL && PyDict_Check(kwds) && PyDict_Size(kwds)) {
1238 keyfunc = PyDict_GetItemString(kwds, "key");
1239 if (PyDict_Size(kwds)!=1 || keyfunc == NULL) {
Georg Brandl99d7e4e2005-08-31 22:21:15 +00001240 PyErr_Format(PyExc_TypeError,
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001241 "%s() got an unexpected keyword argument", name);
1242 return NULL;
1243 }
Georg Brandl99d7e4e2005-08-31 22:21:15 +00001244 }
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001245
Tim Petersc3074532001-05-03 07:00:32 +00001246 it = PyObject_GetIter(v);
1247 if (it == NULL)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001248 return NULL;
Tim Petersc3074532001-05-03 07:00:32 +00001249
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001250 maxitem = NULL; /* the result */
1251 maxval = NULL; /* the value associated with the result */
Brett Cannon9e635cf2004-12-07 00:25:35 +00001252 while (( item = PyIter_Next(it) )) {
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001253 /* get the value from the key function */
1254 if (keyfunc != NULL) {
1255 val = PyObject_CallFunctionObjArgs(keyfunc, item, NULL);
1256 if (val == NULL)
1257 goto Fail_it_item;
1258 }
1259 /* no key function; the value is the item */
1260 else {
1261 val = item;
1262 Py_INCREF(val);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001263 }
Tim Petersc3074532001-05-03 07:00:32 +00001264
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001265 /* maximum value and item are unset; set them */
1266 if (maxval == NULL) {
1267 maxitem = item;
1268 maxval = val;
1269 }
1270 /* maximum value and item are set; update them as necessary */
Guido van Rossum2d951851994-08-29 12:52:16 +00001271 else {
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001272 int cmp = PyObject_RichCompareBool(val, maxval, op);
1273 if (cmp < 0)
1274 goto Fail_it_item_and_val;
1275 else if (cmp > 0) {
1276 Py_DECREF(maxval);
1277 Py_DECREF(maxitem);
1278 maxval = val;
1279 maxitem = item;
Guido van Rossum53451b32001-01-17 15:47:24 +00001280 }
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001281 else {
1282 Py_DECREF(item);
1283 Py_DECREF(val);
Guido van Rossumc8b6df91997-05-23 00:06:51 +00001284 }
Guido van Rossum2d951851994-08-29 12:52:16 +00001285 }
Guido van Rossum3f5da241990-12-20 15:06:42 +00001286 }
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001287 if (PyErr_Occurred())
1288 goto Fail_it;
1289 if (maxval == NULL) {
Martin v. Löwisfd39ad42004-08-12 14:42:37 +00001290 PyErr_Format(PyExc_ValueError,
1291 "%s() arg is an empty sequence", name);
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001292 assert(maxitem == NULL);
1293 }
1294 else
1295 Py_DECREF(maxval);
Tim Petersc3074532001-05-03 07:00:32 +00001296 Py_DECREF(it);
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001297 return maxitem;
1298
1299Fail_it_item_and_val:
1300 Py_DECREF(val);
1301Fail_it_item:
1302 Py_DECREF(item);
1303Fail_it:
1304 Py_XDECREF(maxval);
1305 Py_XDECREF(maxitem);
1306 Py_DECREF(it);
1307 return NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001308}
1309
Guido van Rossum79f25d91997-04-29 20:08:16 +00001310static PyObject *
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001311builtin_min(PyObject *self, PyObject *args, PyObject *kwds)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001312{
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001313 return min_max(args, kwds, Py_LT);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001314}
1315
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001316PyDoc_STRVAR(min_doc,
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001317"min(iterable[, key=func]) -> value\n\
1318min(a, b, c, ...[, key=func]) -> value\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001319\n\
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001320With a single iterable argument, return its smallest item.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001321With two or more arguments, return the smallest argument.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001322
1323
Guido van Rossum79f25d91997-04-29 20:08:16 +00001324static PyObject *
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001325builtin_max(PyObject *self, PyObject *args, PyObject *kwds)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001326{
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001327 return min_max(args, kwds, Py_GT);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001328}
1329
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001330PyDoc_STRVAR(max_doc,
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001331"max(iterable[, key=func]) -> value\n\
1332max(a, b, c, ...[, key=func]) -> value\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001333\n\
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001334With a single iterable argument, return its largest item.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001335With two or more arguments, return the largest argument.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001336
1337
Guido van Rossum79f25d91997-04-29 20:08:16 +00001338static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001339builtin_oct(PyObject *self, PyObject *v)
Guido van Rossum006bcd41991-10-24 14:54:44 +00001340{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001341 PyNumberMethods *nb;
Neil Schemenauer3a313e32004-07-19 16:29:17 +00001342 PyObject *res;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001343
Guido van Rossum1899c2e1992-09-12 11:09:23 +00001344 if (v == NULL || (nb = v->ob_type->tp_as_number) == NULL ||
1345 nb->nb_oct == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001346 PyErr_SetString(PyExc_TypeError,
Guido van Rossum1899c2e1992-09-12 11:09:23 +00001347 "oct() argument can't be converted to oct");
1348 return NULL;
Guido van Rossum006bcd41991-10-24 14:54:44 +00001349 }
Neil Schemenauer3a313e32004-07-19 16:29:17 +00001350 res = (*nb->nb_oct)(v);
1351 if (res && !PyString_Check(res)) {
1352 PyErr_Format(PyExc_TypeError,
1353 "__oct__ returned non-string (type %.200s)",
1354 res->ob_type->tp_name);
1355 Py_DECREF(res);
1356 return NULL;
1357 }
1358 return res;
Guido van Rossum006bcd41991-10-24 14:54:44 +00001359}
1360
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001361PyDoc_STRVAR(oct_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001362"oct(number) -> string\n\
1363\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001364Return the octal representation of an integer or long integer.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001365
1366
Guido van Rossum79f25d91997-04-29 20:08:16 +00001367static PyObject *
Neal Norwitzc4edb0e2006-05-02 04:43:14 +00001368builtin_open(PyObject *self, PyObject *args, PyObject *kwds)
1369{
1370 return PyObject_Call((PyObject*)&PyFile_Type, args, kwds);
1371}
1372
1373PyDoc_STRVAR(open_doc,
1374"open(name[, mode[, buffering]]) -> file object\n\
1375\n\
1376Open a file using the file() type, returns a file object.");
1377
1378
1379static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001380builtin_ord(PyObject *self, PyObject* obj)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001381{
Guido van Rossum09095f32000-03-10 23:00:52 +00001382 long ord;
Martin v. Löwisd96ee902006-02-16 14:37:16 +00001383 Py_ssize_t size;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001384
Jeremy Hylton394b54d2000-04-12 21:19:47 +00001385 if (PyString_Check(obj)) {
1386 size = PyString_GET_SIZE(obj);
Andrew M. Kuchling34c20cf2000-12-20 14:36:56 +00001387 if (size == 1) {
Jeremy Hylton394b54d2000-04-12 21:19:47 +00001388 ord = (long)((unsigned char)*PyString_AS_STRING(obj));
Andrew M. Kuchling34c20cf2000-12-20 14:36:56 +00001389 return PyInt_FromLong(ord);
1390 }
Martin v. Löwis339d0f72001-08-17 18:39:25 +00001391#ifdef Py_USING_UNICODE
Jeremy Hylton394b54d2000-04-12 21:19:47 +00001392 } else if (PyUnicode_Check(obj)) {
1393 size = PyUnicode_GET_SIZE(obj);
Andrew M. Kuchling34c20cf2000-12-20 14:36:56 +00001394 if (size == 1) {
Jeremy Hylton394b54d2000-04-12 21:19:47 +00001395 ord = (long)*PyUnicode_AS_UNICODE(obj);
Andrew M. Kuchling34c20cf2000-12-20 14:36:56 +00001396 return PyInt_FromLong(ord);
1397 }
Martin v. Löwis339d0f72001-08-17 18:39:25 +00001398#endif
Jeremy Hylton394b54d2000-04-12 21:19:47 +00001399 } else {
1400 PyErr_Format(PyExc_TypeError,
Andrew M. Kuchlingf07aad12000-12-23 14:11:28 +00001401 "ord() expected string of length 1, but " \
Jeremy Hylton394b54d2000-04-12 21:19:47 +00001402 "%.200s found", obj->ob_type->tp_name);
Guido van Rossum09095f32000-03-10 23:00:52 +00001403 return NULL;
1404 }
1405
Guido van Rossumad991772001-01-12 16:03:05 +00001406 PyErr_Format(PyExc_TypeError,
Andrew M. Kuchlingf07aad12000-12-23 14:11:28 +00001407 "ord() expected a character, "
Martin v. Löwisd96ee902006-02-16 14:37:16 +00001408 "but string of length %zd found",
Jeremy Hylton394b54d2000-04-12 21:19:47 +00001409 size);
1410 return NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001411}
1412
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001413PyDoc_STRVAR(ord_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001414"ord(c) -> integer\n\
1415\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001416Return the integer ordinal of a one-character string.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001417
1418
Guido van Rossum79f25d91997-04-29 20:08:16 +00001419static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001420builtin_pow(PyObject *self, PyObject *args)
Guido van Rossum6a00cd81995-01-07 12:39:01 +00001421{
Guido van Rossum09df08a1998-05-22 00:51:39 +00001422 PyObject *v, *w, *z = Py_None;
Guido van Rossum6a00cd81995-01-07 12:39:01 +00001423
Raymond Hettingerea3fdf42002-12-29 16:33:45 +00001424 if (!PyArg_UnpackTuple(args, "pow", 2, 3, &v, &w, &z))
Guido van Rossum6a00cd81995-01-07 12:39:01 +00001425 return NULL;
Guido van Rossum09df08a1998-05-22 00:51:39 +00001426 return PyNumber_Power(v, w, z);
Guido van Rossumd4905451991-05-05 20:00:36 +00001427}
1428
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001429PyDoc_STRVAR(pow_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001430"pow(x, y[, z]) -> number\n\
1431\n\
1432With two arguments, equivalent to x**y. With three arguments,\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001433equivalent to (x**y) % z, but may be more efficient (e.g. for longs).");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001434
1435
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001436
1437/* Return number of items in range (lo, hi, step), when arguments are
1438 * PyInt or PyLong objects. step > 0 required. Return a value < 0 if
1439 * & only if the true value is too large to fit in a signed long.
1440 * Arguments MUST return 1 with either PyInt_Check() or
1441 * PyLong_Check(). Return -1 when there is an error.
1442 */
1443static long
1444get_len_of_range_longs(PyObject *lo, PyObject *hi, PyObject *step)
1445{
1446 /* -------------------------------------------------------------
1447 Algorithm is equal to that of get_len_of_range(), but it operates
1448 on PyObjects (which are assumed to be PyLong or PyInt objects).
1449 ---------------------------------------------------------------*/
1450 long n;
1451 PyObject *diff = NULL;
1452 PyObject *one = NULL;
1453 PyObject *tmp1 = NULL, *tmp2 = NULL, *tmp3 = NULL;
1454 /* holds sub-expression evaluations */
1455
1456 /* if (lo >= hi), return length of 0. */
1457 if (PyObject_Compare(lo, hi) >= 0)
1458 return 0;
1459
1460 if ((one = PyLong_FromLong(1L)) == NULL)
1461 goto Fail;
1462
1463 if ((tmp1 = PyNumber_Subtract(hi, lo)) == NULL)
1464 goto Fail;
1465
1466 if ((diff = PyNumber_Subtract(tmp1, one)) == NULL)
1467 goto Fail;
1468
1469 if ((tmp2 = PyNumber_FloorDivide(diff, step)) == NULL)
1470 goto Fail;
1471
1472 if ((tmp3 = PyNumber_Add(tmp2, one)) == NULL)
1473 goto Fail;
1474
1475 n = PyLong_AsLong(tmp3);
1476 if (PyErr_Occurred()) { /* Check for Overflow */
1477 PyErr_Clear();
1478 goto Fail;
1479 }
1480
1481 Py_DECREF(tmp3);
1482 Py_DECREF(tmp2);
1483 Py_DECREF(diff);
1484 Py_DECREF(tmp1);
1485 Py_DECREF(one);
1486 return n;
1487
1488 Fail:
1489 Py_XDECREF(tmp3);
1490 Py_XDECREF(tmp2);
1491 Py_XDECREF(diff);
1492 Py_XDECREF(tmp1);
1493 Py_XDECREF(one);
1494 return -1;
1495}
1496
1497/* An extension of builtin_range() that handles the case when PyLong
1498 * arguments are given. */
1499static PyObject *
1500handle_range_longs(PyObject *self, PyObject *args)
1501{
1502 PyObject *ilow;
Tim Peters874e1f72003-04-13 22:13:08 +00001503 PyObject *ihigh = NULL;
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001504 PyObject *istep = NULL;
Tim Peters874e1f72003-04-13 22:13:08 +00001505
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001506 PyObject *curnum = NULL;
1507 PyObject *v = NULL;
1508 long bign;
1509 int i, n;
1510 int cmp_result;
1511
Tim Peters874e1f72003-04-13 22:13:08 +00001512 PyObject *zero = PyLong_FromLong(0);
1513
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001514 if (zero == NULL)
1515 return NULL;
1516
Tim Peters874e1f72003-04-13 22:13:08 +00001517 if (!PyArg_UnpackTuple(args, "range", 1, 3, &ilow, &ihigh, &istep)) {
1518 Py_DECREF(zero);
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001519 return NULL;
1520 }
1521
Tim Peters874e1f72003-04-13 22:13:08 +00001522 /* Figure out which way we were called, supply defaults, and be
1523 * sure to incref everything so that the decrefs at the end
1524 * are correct.
1525 */
1526 assert(ilow != NULL);
1527 if (ihigh == NULL) {
1528 /* only 1 arg -- it's the upper limit */
1529 ihigh = ilow;
1530 ilow = NULL;
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001531 }
Tim Peters874e1f72003-04-13 22:13:08 +00001532 assert(ihigh != NULL);
1533 Py_INCREF(ihigh);
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001534
Tim Peters874e1f72003-04-13 22:13:08 +00001535 /* ihigh correct now; do ilow */
1536 if (ilow == NULL)
1537 ilow = zero;
1538 Py_INCREF(ilow);
1539
1540 /* ilow and ihigh correct now; do istep */
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001541 if (istep == NULL) {
1542 istep = PyLong_FromLong(1L);
1543 if (istep == NULL)
1544 goto Fail;
1545 }
1546 else {
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001547 Py_INCREF(istep);
1548 }
1549
Tim Peters874e1f72003-04-13 22:13:08 +00001550 if (!PyInt_Check(ilow) && !PyLong_Check(ilow)) {
Guido van Rossum28e83e32003-04-15 12:43:26 +00001551 PyErr_Format(PyExc_TypeError,
Alex Martellif471d472003-04-23 13:00:44 +00001552 "range() integer start argument expected, got %s.",
Guido van Rossum817d6c92003-04-14 18:25:04 +00001553 ilow->ob_type->tp_name);
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001554 goto Fail;
1555 }
1556
Tim Peters874e1f72003-04-13 22:13:08 +00001557 if (!PyInt_Check(ihigh) && !PyLong_Check(ihigh)) {
Guido van Rossum28e83e32003-04-15 12:43:26 +00001558 PyErr_Format(PyExc_TypeError,
Alex Martellif471d472003-04-23 13:00:44 +00001559 "range() integer end argument expected, got %s.",
Guido van Rossum817d6c92003-04-14 18:25:04 +00001560 ihigh->ob_type->tp_name);
Tim Peters874e1f72003-04-13 22:13:08 +00001561 goto Fail;
1562 }
1563
1564 if (!PyInt_Check(istep) && !PyLong_Check(istep)) {
Guido van Rossum28e83e32003-04-15 12:43:26 +00001565 PyErr_Format(PyExc_TypeError,
Alex Martellif471d472003-04-23 13:00:44 +00001566 "range() integer step argument expected, got %s.",
Guido van Rossum817d6c92003-04-14 18:25:04 +00001567 istep->ob_type->tp_name);
Tim Peters874e1f72003-04-13 22:13:08 +00001568 goto Fail;
1569 }
1570
1571 if (PyObject_Cmp(istep, zero, &cmp_result) == -1)
1572 goto Fail;
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001573 if (cmp_result == 0) {
1574 PyErr_SetString(PyExc_ValueError,
Alex Martellif471d472003-04-23 13:00:44 +00001575 "range() step argument must not be zero");
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001576 goto Fail;
1577 }
1578
1579 if (cmp_result > 0)
1580 bign = get_len_of_range_longs(ilow, ihigh, istep);
1581 else {
1582 PyObject *neg_istep = PyNumber_Negative(istep);
1583 if (neg_istep == NULL)
1584 goto Fail;
1585 bign = get_len_of_range_longs(ihigh, ilow, neg_istep);
1586 Py_DECREF(neg_istep);
1587 }
1588
1589 n = (int)bign;
1590 if (bign < 0 || (long)n != bign) {
1591 PyErr_SetString(PyExc_OverflowError,
1592 "range() result has too many items");
1593 goto Fail;
1594 }
1595
1596 v = PyList_New(n);
1597 if (v == NULL)
1598 goto Fail;
1599
1600 curnum = ilow;
1601 Py_INCREF(curnum);
1602
1603 for (i = 0; i < n; i++) {
1604 PyObject *w = PyNumber_Long(curnum);
1605 PyObject *tmp_num;
1606 if (w == NULL)
1607 goto Fail;
1608
1609 PyList_SET_ITEM(v, i, w);
1610
1611 tmp_num = PyNumber_Add(curnum, istep);
1612 if (tmp_num == NULL)
1613 goto Fail;
1614
1615 Py_DECREF(curnum);
1616 curnum = tmp_num;
1617 }
Tim Peters874e1f72003-04-13 22:13:08 +00001618 Py_DECREF(ilow);
1619 Py_DECREF(ihigh);
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001620 Py_DECREF(istep);
1621 Py_DECREF(zero);
Tim Peters874e1f72003-04-13 22:13:08 +00001622 Py_DECREF(curnum);
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001623 return v;
1624
1625 Fail:
Tim Peters874e1f72003-04-13 22:13:08 +00001626 Py_DECREF(ilow);
1627 Py_DECREF(ihigh);
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001628 Py_XDECREF(istep);
Tim Peters874e1f72003-04-13 22:13:08 +00001629 Py_DECREF(zero);
1630 Py_XDECREF(curnum);
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001631 Py_XDECREF(v);
1632 return NULL;
1633}
1634
Guido van Rossum124eff01999-02-23 16:11:01 +00001635/* Return number of items in range/xrange (lo, hi, step). step > 0
1636 * required. Return a value < 0 if & only if the true value is too
1637 * large to fit in a signed long.
1638 */
1639static long
Thomas Wouterse28c2962000-07-23 22:21:32 +00001640get_len_of_range(long lo, long hi, long step)
Guido van Rossum124eff01999-02-23 16:11:01 +00001641{
1642 /* -------------------------------------------------------------
1643 If lo >= hi, the range is empty.
1644 Else if n values are in the range, the last one is
1645 lo + (n-1)*step, which must be <= hi-1. Rearranging,
1646 n <= (hi - lo - 1)/step + 1, so taking the floor of the RHS gives
1647 the proper value. Since lo < hi in this case, hi-lo-1 >= 0, so
1648 the RHS is non-negative and so truncation is the same as the
1649 floor. Letting M be the largest positive long, the worst case
1650 for the RHS numerator is hi=M, lo=-M-1, and then
1651 hi-lo-1 = M-(-M-1)-1 = 2*M. Therefore unsigned long has enough
1652 precision to compute the RHS exactly.
1653 ---------------------------------------------------------------*/
1654 long n = 0;
1655 if (lo < hi) {
1656 unsigned long uhi = (unsigned long)hi;
1657 unsigned long ulo = (unsigned long)lo;
1658 unsigned long diff = uhi - ulo - 1;
1659 n = (long)(diff / (unsigned long)step + 1);
1660 }
1661 return n;
1662}
1663
Guido van Rossum79f25d91997-04-29 20:08:16 +00001664static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001665builtin_range(PyObject *self, PyObject *args)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001666{
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001667 long ilow = 0, ihigh = 0, istep = 1;
Guido van Rossum124eff01999-02-23 16:11:01 +00001668 long bign;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001669 int i, n;
Guido van Rossum124eff01999-02-23 16:11:01 +00001670
Guido van Rossum79f25d91997-04-29 20:08:16 +00001671 PyObject *v;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001672
Guido van Rossum79f25d91997-04-29 20:08:16 +00001673 if (PyTuple_Size(args) <= 1) {
1674 if (!PyArg_ParseTuple(args,
Guido van Rossum0865dd91995-01-17 16:30:22 +00001675 "l;range() requires 1-3 int arguments",
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001676 &ihigh)) {
1677 PyErr_Clear();
1678 return handle_range_longs(self, args);
1679 }
Guido van Rossum3f5da241990-12-20 15:06:42 +00001680 }
1681 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001682 if (!PyArg_ParseTuple(args,
Guido van Rossum0865dd91995-01-17 16:30:22 +00001683 "ll|l;range() requires 1-3 int arguments",
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001684 &ilow, &ihigh, &istep)) {
1685 PyErr_Clear();
1686 return handle_range_longs(self, args);
1687 }
Guido van Rossum3f5da241990-12-20 15:06:42 +00001688 }
1689 if (istep == 0) {
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001690 PyErr_SetString(PyExc_ValueError,
Alex Martellif471d472003-04-23 13:00:44 +00001691 "range() step argument must not be zero");
Guido van Rossum3f5da241990-12-20 15:06:42 +00001692 return NULL;
1693 }
Guido van Rossum124eff01999-02-23 16:11:01 +00001694 if (istep > 0)
1695 bign = get_len_of_range(ilow, ihigh, istep);
1696 else
1697 bign = get_len_of_range(ihigh, ilow, -istep);
1698 n = (int)bign;
1699 if (bign < 0 || (long)n != bign) {
Guido van Rossume23cde21999-01-12 05:07:47 +00001700 PyErr_SetString(PyExc_OverflowError,
Fred Drake661ea262000-10-24 19:57:45 +00001701 "range() result has too many items");
Guido van Rossume23cde21999-01-12 05:07:47 +00001702 return NULL;
1703 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00001704 v = PyList_New(n);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001705 if (v == NULL)
1706 return NULL;
1707 for (i = 0; i < n; i++) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001708 PyObject *w = PyInt_FromLong(ilow);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001709 if (w == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001710 Py_DECREF(v);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001711 return NULL;
1712 }
Guido van Rossuma937d141998-04-24 18:22:02 +00001713 PyList_SET_ITEM(v, i, w);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001714 ilow += istep;
1715 }
1716 return v;
1717}
1718
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001719PyDoc_STRVAR(range_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001720"range([start,] stop[, step]) -> list of integers\n\
1721\n\
1722Return a list containing an arithmetic progression of integers.\n\
1723range(i, j) returns [i, i+1, i+2, ..., j-1]; start (!) defaults to 0.\n\
1724When step is given, it specifies the increment (or decrement).\n\
1725For example, range(4) returns [0, 1, 2, 3]. The end point is omitted!\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001726These are exactly the valid indices for a list of 4 elements.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001727
1728
Guido van Rossum79f25d91997-04-29 20:08:16 +00001729static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001730builtin_raw_input(PyObject *self, PyObject *args)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001731{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001732 PyObject *v = NULL;
Martin v. Löwis31d2df52002-08-14 15:46:02 +00001733 PyObject *fin = PySys_GetObject("stdin");
1734 PyObject *fout = PySys_GetObject("stdout");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001735
Raymond Hettingerea3fdf42002-12-29 16:33:45 +00001736 if (!PyArg_UnpackTuple(args, "[raw_]input", 0, 1, &v))
Guido van Rossum3165fe61992-09-25 21:59:05 +00001737 return NULL;
Martin v. Löwis31d2df52002-08-14 15:46:02 +00001738
1739 if (fin == NULL) {
Alex Martellia9b9c9f2003-04-23 13:34:35 +00001740 PyErr_SetString(PyExc_RuntimeError, "[raw_]input: lost sys.stdin");
Martin v. Löwis31d2df52002-08-14 15:46:02 +00001741 return NULL;
1742 }
1743 if (fout == NULL) {
Alex Martellia9b9c9f2003-04-23 13:34:35 +00001744 PyErr_SetString(PyExc_RuntimeError, "[raw_]input: lost sys.stdout");
Martin v. Löwis31d2df52002-08-14 15:46:02 +00001745 return NULL;
1746 }
1747 if (PyFile_SoftSpace(fout, 0)) {
1748 if (PyFile_WriteString(" ", fout) != 0)
1749 return NULL;
1750 }
Georg Brandl7e3ba2a2006-08-06 08:23:54 +00001751 if (PyFile_AsFile(fin) && PyFile_AsFile(fout)
Martin v. Löwis566f6af2002-10-26 14:39:10 +00001752 && isatty(fileno(PyFile_AsFile(fin)))
1753 && isatty(fileno(PyFile_AsFile(fout)))) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001754 PyObject *po;
Guido van Rossum872537c1995-07-07 22:43:42 +00001755 char *prompt;
1756 char *s;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001757 PyObject *result;
Guido van Rossum872537c1995-07-07 22:43:42 +00001758 if (v != NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001759 po = PyObject_Str(v);
Guido van Rossum872537c1995-07-07 22:43:42 +00001760 if (po == NULL)
1761 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001762 prompt = PyString_AsString(po);
Guido van Rossumd9b52081998-06-26 18:25:38 +00001763 if (prompt == NULL)
1764 return NULL;
Guido van Rossum872537c1995-07-07 22:43:42 +00001765 }
1766 else {
1767 po = NULL;
1768 prompt = "";
1769 }
Michael W. Hudson52db5192004-08-02 13:21:09 +00001770 s = PyOS_Readline(PyFile_AsFile(fin), PyFile_AsFile(fout),
Martin v. Löwis566f6af2002-10-26 14:39:10 +00001771 prompt);
Guido van Rossum79f25d91997-04-29 20:08:16 +00001772 Py_XDECREF(po);
Guido van Rossum872537c1995-07-07 22:43:42 +00001773 if (s == NULL) {
Michael W. Hudson30ea2f22004-07-07 17:44:12 +00001774 if (!PyErr_Occurred())
1775 PyErr_SetNone(PyExc_KeyboardInterrupt);
Guido van Rossum872537c1995-07-07 22:43:42 +00001776 return NULL;
1777 }
1778 if (*s == '\0') {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001779 PyErr_SetNone(PyExc_EOFError);
Guido van Rossum872537c1995-07-07 22:43:42 +00001780 result = NULL;
1781 }
1782 else { /* strip trailing '\n' */
Guido van Rossum106f2da2000-06-28 21:12:25 +00001783 size_t len = strlen(s);
Martin v. Löwisb1ed7fa2006-04-13 07:52:27 +00001784 if (len > PY_SSIZE_T_MAX) {
Martin v. Löwis31d2df52002-08-14 15:46:02 +00001785 PyErr_SetString(PyExc_OverflowError,
Alex Martellia9b9c9f2003-04-23 13:34:35 +00001786 "[raw_]input: input too long");
Guido van Rossum106f2da2000-06-28 21:12:25 +00001787 result = NULL;
1788 }
1789 else {
Martin v. Löwisb1ed7fa2006-04-13 07:52:27 +00001790 result = PyString_FromStringAndSize(s, len-1);
Guido van Rossum106f2da2000-06-28 21:12:25 +00001791 }
Guido van Rossum872537c1995-07-07 22:43:42 +00001792 }
Guido van Rossumb18618d2000-05-03 23:44:39 +00001793 PyMem_FREE(s);
Guido van Rossum872537c1995-07-07 22:43:42 +00001794 return result;
1795 }
Guido van Rossum90933611991-06-07 16:10:43 +00001796 if (v != NULL) {
Martin v. Löwis31d2df52002-08-14 15:46:02 +00001797 if (PyFile_WriteObject(v, fout, Py_PRINT_RAW) != 0)
Guido van Rossum90933611991-06-07 16:10:43 +00001798 return NULL;
1799 }
Martin v. Löwis31d2df52002-08-14 15:46:02 +00001800 return PyFile_GetLine(fin, -1);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001801}
1802
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001803PyDoc_STRVAR(raw_input_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001804"raw_input([prompt]) -> string\n\
1805\n\
1806Read a string from standard input. The trailing newline is stripped.\n\
1807If the user hits EOF (Unix: Ctl-D, Windows: Ctl-Z+Return), raise EOFError.\n\
1808On Unix, GNU readline is used if enabled. The prompt string, if given,\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001809is printed without a trailing newline before reading.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001810
1811
Guido van Rossum79f25d91997-04-29 20:08:16 +00001812static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001813builtin_reduce(PyObject *self, PyObject *args)
Guido van Rossum12d12c51993-10-26 17:58:25 +00001814{
Tim Peters15d81ef2001-05-04 04:39:21 +00001815 PyObject *seq, *func, *result = NULL, *it;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001816
Neal Norwitzdf25efe2007-05-23 06:58:36 +00001817 if (Py_Py3kWarningFlag &&
1818 PyErr_Warn(PyExc_DeprecationWarning,
1819 "reduce() not supported in 3.x") < 0)
1820 return NULL;
1821
Raymond Hettingerea3fdf42002-12-29 16:33:45 +00001822 if (!PyArg_UnpackTuple(args, "reduce", 2, 3, &func, &seq, &result))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001823 return NULL;
1824 if (result != NULL)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001825 Py_INCREF(result);
Guido van Rossum12d12c51993-10-26 17:58:25 +00001826
Tim Peters15d81ef2001-05-04 04:39:21 +00001827 it = PyObject_GetIter(seq);
1828 if (it == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001829 PyErr_SetString(PyExc_TypeError,
Tim Peters15d81ef2001-05-04 04:39:21 +00001830 "reduce() arg 2 must support iteration");
1831 Py_XDECREF(result);
Guido van Rossum12d12c51993-10-26 17:58:25 +00001832 return NULL;
1833 }
1834
Guido van Rossum79f25d91997-04-29 20:08:16 +00001835 if ((args = PyTuple_New(2)) == NULL)
Guido van Rossum2d951851994-08-29 12:52:16 +00001836 goto Fail;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001837
Tim Peters15d81ef2001-05-04 04:39:21 +00001838 for (;;) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001839 PyObject *op2;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001840
1841 if (args->ob_refcnt > 1) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001842 Py_DECREF(args);
1843 if ((args = PyTuple_New(2)) == NULL)
Guido van Rossum2d951851994-08-29 12:52:16 +00001844 goto Fail;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001845 }
1846
Tim Peters15d81ef2001-05-04 04:39:21 +00001847 op2 = PyIter_Next(it);
1848 if (op2 == NULL) {
Tim Petersf4848da2001-05-05 00:14:56 +00001849 if (PyErr_Occurred())
1850 goto Fail;
1851 break;
Guido van Rossum2d951851994-08-29 12:52:16 +00001852 }
Guido van Rossum12d12c51993-10-26 17:58:25 +00001853
Guido van Rossum2d951851994-08-29 12:52:16 +00001854 if (result == NULL)
1855 result = op2;
1856 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001857 PyTuple_SetItem(args, 0, result);
1858 PyTuple_SetItem(args, 1, op2);
1859 if ((result = PyEval_CallObject(func, args)) == NULL)
Guido van Rossum2d951851994-08-29 12:52:16 +00001860 goto Fail;
1861 }
Guido van Rossum12d12c51993-10-26 17:58:25 +00001862 }
1863
Guido van Rossum79f25d91997-04-29 20:08:16 +00001864 Py_DECREF(args);
Guido van Rossum12d12c51993-10-26 17:58:25 +00001865
Guido van Rossum2d951851994-08-29 12:52:16 +00001866 if (result == NULL)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001867 PyErr_SetString(PyExc_TypeError,
Fred Drake661ea262000-10-24 19:57:45 +00001868 "reduce() of empty sequence with no initial value");
Guido van Rossum2d951851994-08-29 12:52:16 +00001869
Tim Peters15d81ef2001-05-04 04:39:21 +00001870 Py_DECREF(it);
Guido van Rossum12d12c51993-10-26 17:58:25 +00001871 return result;
1872
Guido van Rossum2d951851994-08-29 12:52:16 +00001873Fail:
Guido van Rossum79f25d91997-04-29 20:08:16 +00001874 Py_XDECREF(args);
1875 Py_XDECREF(result);
Tim Peters15d81ef2001-05-04 04:39:21 +00001876 Py_DECREF(it);
Guido van Rossum12d12c51993-10-26 17:58:25 +00001877 return NULL;
1878}
1879
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001880PyDoc_STRVAR(reduce_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001881"reduce(function, sequence[, initial]) -> value\n\
1882\n\
1883Apply a function of two arguments cumulatively to the items of a sequence,\n\
1884from left to right, so as to reduce the sequence to a single value.\n\
1885For example, reduce(lambda x, y: x+y, [1, 2, 3, 4, 5]) calculates\n\
1886((((1+2)+3)+4)+5). If initial is present, it is placed before the items\n\
1887of the sequence in the calculation, and serves as a default when the\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001888sequence is empty.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001889
1890
Guido van Rossum79f25d91997-04-29 20:08:16 +00001891static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001892builtin_reload(PyObject *self, PyObject *v)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001893{
Neal Norwitzdf25efe2007-05-23 06:58:36 +00001894 if (Py_Py3kWarningFlag &&
1895 PyErr_Warn(PyExc_DeprecationWarning,
1896 "reload() not supported in 3.x") < 0)
1897 return NULL;
1898
Guido van Rossum79f25d91997-04-29 20:08:16 +00001899 return PyImport_ReloadModule(v);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001900}
1901
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001902PyDoc_STRVAR(reload_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001903"reload(module) -> module\n\
1904\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001905Reload the module. The module must have been successfully imported before.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001906
1907
Guido van Rossum79f25d91997-04-29 20:08:16 +00001908static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001909builtin_repr(PyObject *self, PyObject *v)
Guido van Rossumc89705d1992-11-26 08:54:07 +00001910{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001911 return PyObject_Repr(v);
Guido van Rossumc89705d1992-11-26 08:54:07 +00001912}
1913
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001914PyDoc_STRVAR(repr_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001915"repr(object) -> string\n\
1916\n\
1917Return the canonical string representation of the object.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001918For most object types, eval(repr(object)) == object.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001919
1920
Guido van Rossum79f25d91997-04-29 20:08:16 +00001921static PyObject *
Georg Brandlccadf842006-03-31 18:54:53 +00001922builtin_round(PyObject *self, PyObject *args, PyObject *kwds)
Guido van Rossum9e51f9b1993-02-12 16:29:05 +00001923{
Georg Brandlccadf842006-03-31 18:54:53 +00001924 double number;
Guido van Rossum9e51f9b1993-02-12 16:29:05 +00001925 double f;
1926 int ndigits = 0;
Guido van Rossum9e51f9b1993-02-12 16:29:05 +00001927 int i;
Georg Brandlccadf842006-03-31 18:54:53 +00001928 static char *kwlist[] = {"number", "ndigits", 0};
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001929
Georg Brandlccadf842006-03-31 18:54:53 +00001930 if (!PyArg_ParseTupleAndKeywords(args, kwds, "d|i:round",
1931 kwlist, &number, &ndigits))
1932 return NULL;
Guido van Rossum9e51f9b1993-02-12 16:29:05 +00001933 f = 1.0;
Guido van Rossum1e162d31998-05-09 14:42:25 +00001934 i = abs(ndigits);
1935 while (--i >= 0)
Guido van Rossum9e51f9b1993-02-12 16:29:05 +00001936 f = f*10.0;
Guido van Rossum1e162d31998-05-09 14:42:25 +00001937 if (ndigits < 0)
Georg Brandlccadf842006-03-31 18:54:53 +00001938 number /= f;
Guido van Rossum9e51f9b1993-02-12 16:29:05 +00001939 else
Georg Brandlccadf842006-03-31 18:54:53 +00001940 number *= f;
1941 if (number >= 0.0)
1942 number = floor(number + 0.5);
Guido van Rossum1e162d31998-05-09 14:42:25 +00001943 else
Georg Brandlccadf842006-03-31 18:54:53 +00001944 number = ceil(number - 0.5);
Guido van Rossum1e162d31998-05-09 14:42:25 +00001945 if (ndigits < 0)
Georg Brandlccadf842006-03-31 18:54:53 +00001946 number *= f;
Guido van Rossum1e162d31998-05-09 14:42:25 +00001947 else
Georg Brandlccadf842006-03-31 18:54:53 +00001948 number /= f;
1949 return PyFloat_FromDouble(number);
Guido van Rossum9e51f9b1993-02-12 16:29:05 +00001950}
1951
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001952PyDoc_STRVAR(round_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001953"round(number[, ndigits]) -> floating point number\n\
1954\n\
1955Round a number to a given precision in decimal digits (default 0 digits).\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001956This always returns a floating point number. Precision may be negative.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001957
Raymond Hettinger64958a12003-12-17 20:43:33 +00001958static PyObject *
1959builtin_sorted(PyObject *self, PyObject *args, PyObject *kwds)
1960{
1961 PyObject *newlist, *v, *seq, *compare=NULL, *keyfunc=NULL, *newargs;
1962 PyObject *callable;
Martin v. Löwis15e62742006-02-27 16:46:16 +00001963 static char *kwlist[] = {"iterable", "cmp", "key", "reverse", 0};
Neal Norwitz6f0d4792005-12-15 06:40:36 +00001964 int reverse;
Raymond Hettinger64958a12003-12-17 20:43:33 +00001965
Neal Norwitz6f0d4792005-12-15 06:40:36 +00001966 /* args 1-4 should match listsort in Objects/listobject.c */
Armin Rigo71d7e702005-09-20 18:13:03 +00001967 if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|OOi:sorted",
1968 kwlist, &seq, &compare, &keyfunc, &reverse))
1969 return NULL;
Raymond Hettinger64958a12003-12-17 20:43:33 +00001970
1971 newlist = PySequence_List(seq);
1972 if (newlist == NULL)
1973 return NULL;
1974
1975 callable = PyObject_GetAttrString(newlist, "sort");
1976 if (callable == NULL) {
1977 Py_DECREF(newlist);
1978 return NULL;
1979 }
Georg Brandl99d7e4e2005-08-31 22:21:15 +00001980
Raymond Hettinger64958a12003-12-17 20:43:33 +00001981 newargs = PyTuple_GetSlice(args, 1, 4);
1982 if (newargs == NULL) {
1983 Py_DECREF(newlist);
1984 Py_DECREF(callable);
1985 return NULL;
1986 }
1987
1988 v = PyObject_Call(callable, newargs, kwds);
1989 Py_DECREF(newargs);
1990 Py_DECREF(callable);
1991 if (v == NULL) {
1992 Py_DECREF(newlist);
1993 return NULL;
1994 }
1995 Py_DECREF(v);
1996 return newlist;
1997}
1998
1999PyDoc_STRVAR(sorted_doc,
2000"sorted(iterable, cmp=None, key=None, reverse=False) --> new sorted list");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002001
Guido van Rossum79f25d91997-04-29 20:08:16 +00002002static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002003builtin_vars(PyObject *self, PyObject *args)
Guido van Rossum2d951851994-08-29 12:52:16 +00002004{
Guido van Rossum79f25d91997-04-29 20:08:16 +00002005 PyObject *v = NULL;
2006 PyObject *d;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002007
Raymond Hettingerea3fdf42002-12-29 16:33:45 +00002008 if (!PyArg_UnpackTuple(args, "vars", 0, 1, &v))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002009 return NULL;
Guido van Rossum2d951851994-08-29 12:52:16 +00002010 if (v == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002011 d = PyEval_GetLocals();
Guido van Rossum53bb7ff1995-07-26 16:26:31 +00002012 if (d == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002013 if (!PyErr_Occurred())
2014 PyErr_SetString(PyExc_SystemError,
Alex Martellia9b9c9f2003-04-23 13:34:35 +00002015 "vars(): no locals!?");
Guido van Rossum53bb7ff1995-07-26 16:26:31 +00002016 }
2017 else
Guido van Rossum79f25d91997-04-29 20:08:16 +00002018 Py_INCREF(d);
Guido van Rossum2d951851994-08-29 12:52:16 +00002019 }
2020 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002021 d = PyObject_GetAttrString(v, "__dict__");
Guido van Rossum2d951851994-08-29 12:52:16 +00002022 if (d == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002023 PyErr_SetString(PyExc_TypeError,
Guido van Rossum2d951851994-08-29 12:52:16 +00002024 "vars() argument must have __dict__ attribute");
2025 return NULL;
2026 }
2027 }
2028 return d;
2029}
2030
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002031PyDoc_STRVAR(vars_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002032"vars([object]) -> dictionary\n\
2033\n\
2034Without arguments, equivalent to locals().\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002035With an argument, equivalent to object.__dict__.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002036
Alex Martellia70b1912003-04-22 08:12:33 +00002037
2038static PyObject*
2039builtin_sum(PyObject *self, PyObject *args)
2040{
2041 PyObject *seq;
2042 PyObject *result = NULL;
2043 PyObject *temp, *item, *iter;
2044
Raymond Hettinger5cf63942003-10-25 06:41:37 +00002045 if (!PyArg_UnpackTuple(args, "sum", 1, 2, &seq, &result))
Alex Martellia70b1912003-04-22 08:12:33 +00002046 return NULL;
2047
2048 iter = PyObject_GetIter(seq);
2049 if (iter == NULL)
2050 return NULL;
2051
2052 if (result == NULL) {
2053 result = PyInt_FromLong(0);
2054 if (result == NULL) {
2055 Py_DECREF(iter);
2056 return NULL;
2057 }
2058 } else {
2059 /* reject string values for 'start' parameter */
2060 if (PyObject_TypeCheck(result, &PyBaseString_Type)) {
2061 PyErr_SetString(PyExc_TypeError,
Alex Martellia9b9c9f2003-04-23 13:34:35 +00002062 "sum() can't sum strings [use ''.join(seq) instead]");
Alex Martellia70b1912003-04-22 08:12:33 +00002063 Py_DECREF(iter);
2064 return NULL;
2065 }
Alex Martelli41c9f882003-04-22 09:24:48 +00002066 Py_INCREF(result);
Alex Martellia70b1912003-04-22 08:12:33 +00002067 }
2068
Raymond Hettinger3f8caa32007-10-24 01:28:33 +00002069#ifndef SLOW_SUM
2070 /* Fast addition by keeping temporary sums in C instead of new Python objects.
2071 Assumes all inputs are the same type. If the assumption fails, default
2072 to the more general routine.
2073 */
2074 if (PyInt_CheckExact(result)) {
2075 long i_result = PyInt_AS_LONG(result);
2076 Py_DECREF(result);
2077 result = NULL;
2078 while(result == NULL) {
2079 item = PyIter_Next(iter);
2080 if (item == NULL) {
2081 Py_DECREF(iter);
2082 if (PyErr_Occurred())
2083 return NULL;
2084 return PyInt_FromLong(i_result);
2085 }
2086 if (PyInt_CheckExact(item)) {
2087 long b = PyInt_AS_LONG(item);
2088 long x = i_result + b;
2089 if ((x^i_result) >= 0 || (x^b) >= 0) {
2090 i_result = x;
2091 Py_DECREF(item);
2092 continue;
2093 }
2094 }
2095 /* Either overflowed or is not an int. Restore real objects and process normally */
2096 result = PyInt_FromLong(i_result);
2097 temp = PyNumber_Add(result, item);
2098 Py_DECREF(result);
2099 Py_DECREF(item);
2100 result = temp;
2101 if (result == NULL) {
2102 Py_DECREF(iter);
2103 return NULL;
2104 }
2105 }
2106 }
2107
2108 if (PyFloat_CheckExact(result)) {
2109 double f_result = PyFloat_AS_DOUBLE(result);
2110 Py_DECREF(result);
2111 result = NULL;
2112 while(result == NULL) {
2113 item = PyIter_Next(iter);
2114 if (item == NULL) {
2115 Py_DECREF(iter);
2116 if (PyErr_Occurred())
2117 return NULL;
2118 return PyFloat_FromDouble(f_result);
2119 }
2120 if (PyFloat_CheckExact(item)) {
2121 PyFPE_START_PROTECT("add", return 0)
2122 f_result += PyFloat_AS_DOUBLE(item);
Raymond Hettinger3a8daf52007-10-24 02:05:51 +00002123 PyFPE_END_PROTECT(f_result)
Raymond Hettingera45c4872007-10-25 02:26:58 +00002124 Py_DECREF(item);
Raymond Hettinger3a8daf52007-10-24 02:05:51 +00002125 continue;
2126 }
2127 if (PyInt_CheckExact(item)) {
2128 PyFPE_START_PROTECT("add", return 0)
2129 f_result += (double)PyInt_AS_LONG(item);
2130 PyFPE_END_PROTECT(f_result)
Raymond Hettingera45c4872007-10-25 02:26:58 +00002131 Py_DECREF(item);
Raymond Hettinger3f8caa32007-10-24 01:28:33 +00002132 continue;
2133 }
2134 result = PyFloat_FromDouble(f_result);
2135 temp = PyNumber_Add(result, item);
2136 Py_DECREF(result);
2137 Py_DECREF(item);
2138 result = temp;
2139 if (result == NULL) {
2140 Py_DECREF(iter);
2141 return NULL;
2142 }
2143 }
2144 }
2145#endif
2146
Alex Martellia70b1912003-04-22 08:12:33 +00002147 for(;;) {
2148 item = PyIter_Next(iter);
2149 if (item == NULL) {
2150 /* error, or end-of-sequence */
2151 if (PyErr_Occurred()) {
2152 Py_DECREF(result);
2153 result = NULL;
2154 }
2155 break;
2156 }
Alex Martellia253e182003-10-25 23:24:14 +00002157 temp = PyNumber_Add(result, item);
Alex Martellia70b1912003-04-22 08:12:33 +00002158 Py_DECREF(result);
2159 Py_DECREF(item);
2160 result = temp;
2161 if (result == NULL)
2162 break;
2163 }
2164 Py_DECREF(iter);
2165 return result;
2166}
2167
2168PyDoc_STRVAR(sum_doc,
Georg Brandl8134d062006-10-12 12:33:07 +00002169"sum(sequence[, start]) -> value\n\
Alex Martellia70b1912003-04-22 08:12:33 +00002170\n\
2171Returns the sum of a sequence of numbers (NOT strings) plus the value\n\
Georg Brandl8134d062006-10-12 12:33:07 +00002172of parameter 'start' (which defaults to 0). When the sequence is\n\
2173empty, returns start.");
Alex Martellia70b1912003-04-22 08:12:33 +00002174
2175
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002176static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002177builtin_isinstance(PyObject *self, PyObject *args)
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002178{
2179 PyObject *inst;
2180 PyObject *cls;
Guido van Rossum823649d2001-03-21 18:40:58 +00002181 int retval;
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002182
Raymond Hettingerea3fdf42002-12-29 16:33:45 +00002183 if (!PyArg_UnpackTuple(args, "isinstance", 2, 2, &inst, &cls))
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002184 return NULL;
Guido van Rossumf5dd9141997-12-02 19:11:45 +00002185
Guido van Rossum823649d2001-03-21 18:40:58 +00002186 retval = PyObject_IsInstance(inst, cls);
2187 if (retval < 0)
Guido van Rossumad991772001-01-12 16:03:05 +00002188 return NULL;
Guido van Rossum77f6a652002-04-03 22:41:51 +00002189 return PyBool_FromLong(retval);
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002190}
2191
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002192PyDoc_STRVAR(isinstance_doc,
Guido van Rossum77f6a652002-04-03 22:41:51 +00002193"isinstance(object, class-or-type-or-tuple) -> bool\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002194\n\
2195Return whether an object is an instance of a class or of a subclass thereof.\n\
Guido van Rossum03290ec2001-10-07 20:54:12 +00002196With a type as second argument, return whether that is the object's type.\n\
2197The form using a tuple, isinstance(x, (A, B, ...)), is a shortcut for\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002198isinstance(x, A) or isinstance(x, B) or ... (etc.).");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002199
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002200
2201static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002202builtin_issubclass(PyObject *self, PyObject *args)
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002203{
2204 PyObject *derived;
2205 PyObject *cls;
2206 int retval;
2207
Raymond Hettingerea3fdf42002-12-29 16:33:45 +00002208 if (!PyArg_UnpackTuple(args, "issubclass", 2, 2, &derived, &cls))
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002209 return NULL;
Guido van Rossum668213d1999-06-16 17:28:37 +00002210
Guido van Rossum823649d2001-03-21 18:40:58 +00002211 retval = PyObject_IsSubclass(derived, cls);
2212 if (retval < 0)
2213 return NULL;
Guido van Rossum77f6a652002-04-03 22:41:51 +00002214 return PyBool_FromLong(retval);
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002215}
2216
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002217PyDoc_STRVAR(issubclass_doc,
Guido van Rossum77f6a652002-04-03 22:41:51 +00002218"issubclass(C, B) -> bool\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002219\n\
Walter Dörwaldd9a6ad32002-12-12 16:41:44 +00002220Return whether class C is a subclass (i.e., a derived class) of class B.\n\
2221When using a tuple as the second argument issubclass(X, (A, B, ...)),\n\
2222is a shortcut for issubclass(X, A) or issubclass(X, B) or ... (etc.).");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002223
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002224
Barry Warsawbd599b52000-08-03 15:45:29 +00002225static PyObject*
2226builtin_zip(PyObject *self, PyObject *args)
2227{
2228 PyObject *ret;
Martin v. Löwisd96ee902006-02-16 14:37:16 +00002229 const Py_ssize_t itemsize = PySequence_Length(args);
2230 Py_ssize_t i;
Tim Peters8572b4f2001-05-06 01:05:02 +00002231 PyObject *itlist; /* tuple of iterators */
Martin v. Löwisd96ee902006-02-16 14:37:16 +00002232 Py_ssize_t len; /* guess at result length */
Barry Warsawbd599b52000-08-03 15:45:29 +00002233
Raymond Hettingereaef6152003-08-02 07:42:57 +00002234 if (itemsize == 0)
2235 return PyList_New(0);
2236
Barry Warsawbd599b52000-08-03 15:45:29 +00002237 /* args must be a tuple */
2238 assert(PyTuple_Check(args));
2239
Tim Peters39a86c22002-05-12 07:19:38 +00002240 /* Guess at result length: the shortest of the input lengths.
2241 If some argument refuses to say, we refuse to guess too, lest
2242 an argument like xrange(sys.maxint) lead us astray.*/
Tim Peters67d687a2002-04-29 21:27:32 +00002243 len = -1; /* unknown */
2244 for (i = 0; i < itemsize; ++i) {
2245 PyObject *item = PyTuple_GET_ITEM(args, i);
Martin v. Löwisd96ee902006-02-16 14:37:16 +00002246 Py_ssize_t thislen = _PyObject_LengthHint(item);
Tim Peters39a86c22002-05-12 07:19:38 +00002247 if (thislen < 0) {
Raymond Hettingera710b332005-08-21 11:03:59 +00002248 if (!PyErr_ExceptionMatches(PyExc_TypeError) &&
2249 !PyErr_ExceptionMatches(PyExc_AttributeError)) {
2250 return NULL;
2251 }
Tim Peters67d687a2002-04-29 21:27:32 +00002252 PyErr_Clear();
Tim Peters39a86c22002-05-12 07:19:38 +00002253 len = -1;
2254 break;
2255 }
Tim Peters67d687a2002-04-29 21:27:32 +00002256 else if (len < 0 || thislen < len)
2257 len = thislen;
2258 }
2259
Tim Peters8572b4f2001-05-06 01:05:02 +00002260 /* allocate result list */
Tim Peters67d687a2002-04-29 21:27:32 +00002261 if (len < 0)
2262 len = 10; /* arbitrary */
2263 if ((ret = PyList_New(len)) == NULL)
Barry Warsawbd599b52000-08-03 15:45:29 +00002264 return NULL;
2265
Tim Peters8572b4f2001-05-06 01:05:02 +00002266 /* obtain iterators */
2267 itlist = PyTuple_New(itemsize);
2268 if (itlist == NULL)
2269 goto Fail_ret;
2270 for (i = 0; i < itemsize; ++i) {
2271 PyObject *item = PyTuple_GET_ITEM(args, i);
2272 PyObject *it = PyObject_GetIter(item);
2273 if (it == NULL) {
2274 if (PyErr_ExceptionMatches(PyExc_TypeError))
2275 PyErr_Format(PyExc_TypeError,
Neal Norwitza361bd82006-02-19 19:31:50 +00002276 "zip argument #%zd must support iteration",
Tim Peters8572b4f2001-05-06 01:05:02 +00002277 i+1);
2278 goto Fail_ret_itlist;
Barry Warsawbd599b52000-08-03 15:45:29 +00002279 }
Tim Peters8572b4f2001-05-06 01:05:02 +00002280 PyTuple_SET_ITEM(itlist, i, it);
2281 }
Barry Warsawbd599b52000-08-03 15:45:29 +00002282
Tim Peters8572b4f2001-05-06 01:05:02 +00002283 /* build result into ret list */
Tim Peters67d687a2002-04-29 21:27:32 +00002284 for (i = 0; ; ++i) {
2285 int j;
Tim Peters8572b4f2001-05-06 01:05:02 +00002286 PyObject *next = PyTuple_New(itemsize);
2287 if (!next)
2288 goto Fail_ret_itlist;
2289
Tim Peters67d687a2002-04-29 21:27:32 +00002290 for (j = 0; j < itemsize; j++) {
2291 PyObject *it = PyTuple_GET_ITEM(itlist, j);
Tim Peters8572b4f2001-05-06 01:05:02 +00002292 PyObject *item = PyIter_Next(it);
Barry Warsawbd599b52000-08-03 15:45:29 +00002293 if (!item) {
Tim Peters8572b4f2001-05-06 01:05:02 +00002294 if (PyErr_Occurred()) {
2295 Py_DECREF(ret);
2296 ret = NULL;
Barry Warsawbd599b52000-08-03 15:45:29 +00002297 }
2298 Py_DECREF(next);
Tim Peters8572b4f2001-05-06 01:05:02 +00002299 Py_DECREF(itlist);
Tim Peters67d687a2002-04-29 21:27:32 +00002300 goto Done;
Barry Warsawbd599b52000-08-03 15:45:29 +00002301 }
Tim Peters67d687a2002-04-29 21:27:32 +00002302 PyTuple_SET_ITEM(next, j, item);
Barry Warsawbd599b52000-08-03 15:45:29 +00002303 }
Tim Peters8572b4f2001-05-06 01:05:02 +00002304
Tim Peters67d687a2002-04-29 21:27:32 +00002305 if (i < len)
2306 PyList_SET_ITEM(ret, i, next);
2307 else {
2308 int status = PyList_Append(ret, next);
2309 Py_DECREF(next);
2310 ++len;
2311 if (status < 0)
2312 goto Fail_ret_itlist;
2313 }
Barry Warsawbd599b52000-08-03 15:45:29 +00002314 }
Tim Peters8572b4f2001-05-06 01:05:02 +00002315
Tim Peters67d687a2002-04-29 21:27:32 +00002316Done:
2317 if (ret != NULL && i < len) {
2318 /* The list is too big. */
2319 if (PyList_SetSlice(ret, i, len, NULL) < 0)
2320 return NULL;
2321 }
2322 return ret;
2323
Tim Peters8572b4f2001-05-06 01:05:02 +00002324Fail_ret_itlist:
2325 Py_DECREF(itlist);
2326Fail_ret:
2327 Py_DECREF(ret);
2328 return NULL;
Barry Warsawbd599b52000-08-03 15:45:29 +00002329}
2330
2331
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002332PyDoc_STRVAR(zip_doc,
Barry Warsawbd599b52000-08-03 15:45:29 +00002333"zip(seq1 [, seq2 [...]]) -> [(seq1[0], seq2[0] ...), (...)]\n\
2334\n\
2335Return a list of tuples, where each tuple contains the i-th element\n\
2336from each of the argument sequences. The returned list is truncated\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002337in length to the length of the shortest argument sequence.");
Barry Warsawbd599b52000-08-03 15:45:29 +00002338
2339
Guido van Rossum79f25d91997-04-29 20:08:16 +00002340static PyMethodDef builtin_methods[] = {
Neal Norwitz92e212f2006-04-03 04:48:37 +00002341 {"__import__", (PyCFunction)builtin___import__, METH_VARARGS | METH_KEYWORDS, import_doc},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002342 {"abs", builtin_abs, METH_O, abs_doc},
Raymond Hettinger96229b12005-03-11 06:49:40 +00002343 {"all", builtin_all, METH_O, all_doc},
2344 {"any", builtin_any, METH_O, any_doc},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002345 {"apply", builtin_apply, METH_VARARGS, apply_doc},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002346 {"callable", builtin_callable, METH_O, callable_doc},
2347 {"chr", builtin_chr, METH_VARARGS, chr_doc},
2348 {"cmp", builtin_cmp, METH_VARARGS, cmp_doc},
2349 {"coerce", builtin_coerce, METH_VARARGS, coerce_doc},
Georg Brandl5240d742007-03-13 20:46:32 +00002350 {"compile", (PyCFunction)builtin_compile, METH_VARARGS | METH_KEYWORDS, compile_doc},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002351 {"delattr", builtin_delattr, METH_VARARGS, delattr_doc},
2352 {"dir", builtin_dir, METH_VARARGS, dir_doc},
2353 {"divmod", builtin_divmod, METH_VARARGS, divmod_doc},
2354 {"eval", builtin_eval, METH_VARARGS, eval_doc},
2355 {"execfile", builtin_execfile, METH_VARARGS, execfile_doc},
2356 {"filter", builtin_filter, METH_VARARGS, filter_doc},
2357 {"getattr", builtin_getattr, METH_VARARGS, getattr_doc},
2358 {"globals", (PyCFunction)builtin_globals, METH_NOARGS, globals_doc},
2359 {"hasattr", builtin_hasattr, METH_VARARGS, hasattr_doc},
2360 {"hash", builtin_hash, METH_O, hash_doc},
2361 {"hex", builtin_hex, METH_O, hex_doc},
2362 {"id", builtin_id, METH_O, id_doc},
2363 {"input", builtin_input, METH_VARARGS, input_doc},
2364 {"intern", builtin_intern, METH_VARARGS, intern_doc},
2365 {"isinstance", builtin_isinstance, METH_VARARGS, isinstance_doc},
2366 {"issubclass", builtin_issubclass, METH_VARARGS, issubclass_doc},
2367 {"iter", builtin_iter, METH_VARARGS, iter_doc},
2368 {"len", builtin_len, METH_O, len_doc},
2369 {"locals", (PyCFunction)builtin_locals, METH_NOARGS, locals_doc},
2370 {"map", builtin_map, METH_VARARGS, map_doc},
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00002371 {"max", (PyCFunction)builtin_max, METH_VARARGS | METH_KEYWORDS, max_doc},
2372 {"min", (PyCFunction)builtin_min, METH_VARARGS | METH_KEYWORDS, min_doc},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002373 {"oct", builtin_oct, METH_O, oct_doc},
Neal Norwitzc4edb0e2006-05-02 04:43:14 +00002374 {"open", (PyCFunction)builtin_open, METH_VARARGS | METH_KEYWORDS, open_doc},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002375 {"ord", builtin_ord, METH_O, ord_doc},
2376 {"pow", builtin_pow, METH_VARARGS, pow_doc},
2377 {"range", builtin_range, METH_VARARGS, range_doc},
2378 {"raw_input", builtin_raw_input, METH_VARARGS, raw_input_doc},
2379 {"reduce", builtin_reduce, METH_VARARGS, reduce_doc},
2380 {"reload", builtin_reload, METH_O, reload_doc},
2381 {"repr", builtin_repr, METH_O, repr_doc},
Georg Brandlccadf842006-03-31 18:54:53 +00002382 {"round", (PyCFunction)builtin_round, METH_VARARGS | METH_KEYWORDS, round_doc},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002383 {"setattr", builtin_setattr, METH_VARARGS, setattr_doc},
Raymond Hettinger64958a12003-12-17 20:43:33 +00002384 {"sorted", (PyCFunction)builtin_sorted, METH_VARARGS | METH_KEYWORDS, sorted_doc},
Alex Martellia70b1912003-04-22 08:12:33 +00002385 {"sum", builtin_sum, METH_VARARGS, sum_doc},
Martin v. Löwis339d0f72001-08-17 18:39:25 +00002386#ifdef Py_USING_UNICODE
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002387 {"unichr", builtin_unichr, METH_VARARGS, unichr_doc},
Martin v. Löwis339d0f72001-08-17 18:39:25 +00002388#endif
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002389 {"vars", builtin_vars, METH_VARARGS, vars_doc},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002390 {"zip", builtin_zip, METH_VARARGS, zip_doc},
Guido van Rossumc02e15c1991-12-16 13:03:00 +00002391 {NULL, NULL},
Guido van Rossum3f5da241990-12-20 15:06:42 +00002392};
2393
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002394PyDoc_STRVAR(builtin_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002395"Built-in functions, exceptions, and other objects.\n\
2396\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002397Noteworthy: None is the `nil' object; Ellipsis represents `...' in slices.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002398
Guido van Rossum25ce5661997-08-02 03:10:38 +00002399PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002400_PyBuiltin_Init(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +00002401{
Fred Drake5550de32000-06-20 04:54:19 +00002402 PyObject *mod, *dict, *debug;
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002403 mod = Py_InitModule4("__builtin__", builtin_methods,
2404 builtin_doc, (PyObject *)NULL,
2405 PYTHON_API_VERSION);
Guido van Rossum25ce5661997-08-02 03:10:38 +00002406 if (mod == NULL)
2407 return NULL;
2408 dict = PyModule_GetDict(mod);
Tim Peters4b7625e2001-09-13 21:37:17 +00002409
Tim Peters7571a0f2003-03-23 17:52:28 +00002410#ifdef Py_TRACE_REFS
2411 /* __builtin__ exposes a number of statically allocated objects
2412 * that, before this code was added in 2.3, never showed up in
2413 * the list of "all objects" maintained by Py_TRACE_REFS. As a
2414 * result, programs leaking references to None and False (etc)
2415 * couldn't be diagnosed by examining sys.getobjects(0).
2416 */
2417#define ADD_TO_ALL(OBJECT) _Py_AddToAllObjects((PyObject *)(OBJECT), 0)
2418#else
2419#define ADD_TO_ALL(OBJECT) (void)0
2420#endif
2421
Tim Peters4b7625e2001-09-13 21:37:17 +00002422#define SETBUILTIN(NAME, OBJECT) \
Tim Peters7571a0f2003-03-23 17:52:28 +00002423 if (PyDict_SetItemString(dict, NAME, (PyObject *)OBJECT) < 0) \
2424 return NULL; \
2425 ADD_TO_ALL(OBJECT)
Tim Peters4b7625e2001-09-13 21:37:17 +00002426
2427 SETBUILTIN("None", Py_None);
2428 SETBUILTIN("Ellipsis", Py_Ellipsis);
2429 SETBUILTIN("NotImplemented", Py_NotImplemented);
Guido van Rossum77f6a652002-04-03 22:41:51 +00002430 SETBUILTIN("False", Py_False);
2431 SETBUILTIN("True", Py_True);
Neal Norwitz32a7e7f2002-05-31 19:58:02 +00002432 SETBUILTIN("basestring", &PyBaseString_Type);
Guido van Rossum77f6a652002-04-03 22:41:51 +00002433 SETBUILTIN("bool", &PyBool_Type);
Guido van Rossumbea18cc2002-06-14 20:41:17 +00002434 SETBUILTIN("buffer", &PyBuffer_Type);
Tim Peters4b7625e2001-09-13 21:37:17 +00002435 SETBUILTIN("classmethod", &PyClassMethod_Type);
Tim Peters6d6c1a32001-08-02 04:15:00 +00002436#ifndef WITHOUT_COMPLEX
Tim Peters4b7625e2001-09-13 21:37:17 +00002437 SETBUILTIN("complex", &PyComplex_Type);
Tim Peters6d6c1a32001-08-02 04:15:00 +00002438#endif
Tim Petersa427a2b2001-10-29 22:25:45 +00002439 SETBUILTIN("dict", &PyDict_Type);
Tim Peters67d687a2002-04-29 21:27:32 +00002440 SETBUILTIN("enumerate", &PyEnum_Type);
Neal Norwitzc4edb0e2006-05-02 04:43:14 +00002441 SETBUILTIN("file", &PyFile_Type);
Tim Peters4b7625e2001-09-13 21:37:17 +00002442 SETBUILTIN("float", &PyFloat_Type);
Raymond Hettingera690a992003-11-16 16:17:49 +00002443 SETBUILTIN("frozenset", &PyFrozenSet_Type);
Tim Peters4b7625e2001-09-13 21:37:17 +00002444 SETBUILTIN("property", &PyProperty_Type);
2445 SETBUILTIN("int", &PyInt_Type);
2446 SETBUILTIN("list", &PyList_Type);
2447 SETBUILTIN("long", &PyLong_Type);
2448 SETBUILTIN("object", &PyBaseObject_Type);
Raymond Hettinger85c20a42003-11-06 14:06:48 +00002449 SETBUILTIN("reversed", &PyReversed_Type);
Raymond Hettingera690a992003-11-16 16:17:49 +00002450 SETBUILTIN("set", &PySet_Type);
Guido van Rossumbea18cc2002-06-14 20:41:17 +00002451 SETBUILTIN("slice", &PySlice_Type);
Tim Peters4b7625e2001-09-13 21:37:17 +00002452 SETBUILTIN("staticmethod", &PyStaticMethod_Type);
2453 SETBUILTIN("str", &PyString_Type);
2454 SETBUILTIN("super", &PySuper_Type);
2455 SETBUILTIN("tuple", &PyTuple_Type);
2456 SETBUILTIN("type", &PyType_Type);
Raymond Hettingerc4c453f2002-06-05 23:12:45 +00002457 SETBUILTIN("xrange", &PyRange_Type);
Martin v. Löwis339d0f72001-08-17 18:39:25 +00002458#ifdef Py_USING_UNICODE
Tim Peters4b7625e2001-09-13 21:37:17 +00002459 SETBUILTIN("unicode", &PyUnicode_Type);
Martin v. Löwis339d0f72001-08-17 18:39:25 +00002460#endif
Guido van Rossum77f6a652002-04-03 22:41:51 +00002461 debug = PyBool_FromLong(Py_OptimizeFlag == 0);
Fred Drake5550de32000-06-20 04:54:19 +00002462 if (PyDict_SetItemString(dict, "__debug__", debug) < 0) {
2463 Py_XDECREF(debug);
Guido van Rossum25ce5661997-08-02 03:10:38 +00002464 return NULL;
Fred Drake5550de32000-06-20 04:54:19 +00002465 }
2466 Py_XDECREF(debug);
Barry Warsaw757af0e1997-08-29 22:13:51 +00002467
Guido van Rossum25ce5661997-08-02 03:10:38 +00002468 return mod;
Tim Peters7571a0f2003-03-23 17:52:28 +00002469#undef ADD_TO_ALL
Tim Peters4b7625e2001-09-13 21:37:17 +00002470#undef SETBUILTIN
Guido van Rossum3f5da241990-12-20 15:06:42 +00002471}
2472
Guido van Rossume77a7571993-11-03 15:01:26 +00002473/* Helper for filter(): filter a tuple through a function */
Guido van Rossum12d12c51993-10-26 17:58:25 +00002474
Guido van Rossum79f25d91997-04-29 20:08:16 +00002475static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002476filtertuple(PyObject *func, PyObject *tuple)
Guido van Rossum12d12c51993-10-26 17:58:25 +00002477{
Guido van Rossum79f25d91997-04-29 20:08:16 +00002478 PyObject *result;
Martin v. Löwis18e16552006-02-15 17:27:45 +00002479 Py_ssize_t i, j;
2480 Py_ssize_t len = PyTuple_Size(tuple);
Guido van Rossum12d12c51993-10-26 17:58:25 +00002481
Guido van Rossumb7b45621995-08-04 04:07:45 +00002482 if (len == 0) {
Walter Dörwaldc3da83f2003-02-04 20:24:45 +00002483 if (PyTuple_CheckExact(tuple))
2484 Py_INCREF(tuple);
2485 else
2486 tuple = PyTuple_New(0);
Guido van Rossumb7b45621995-08-04 04:07:45 +00002487 return tuple;
2488 }
2489
Guido van Rossum79f25d91997-04-29 20:08:16 +00002490 if ((result = PyTuple_New(len)) == NULL)
Guido van Rossum2586bf01993-11-01 16:21:44 +00002491 return NULL;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002492
Guido van Rossum12d12c51993-10-26 17:58:25 +00002493 for (i = j = 0; i < len; ++i) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002494 PyObject *item, *good;
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002495 int ok;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002496
Walter Dörwald8dd19322003-02-10 17:36:40 +00002497 if (tuple->ob_type->tp_as_sequence &&
2498 tuple->ob_type->tp_as_sequence->sq_item) {
2499 item = tuple->ob_type->tp_as_sequence->sq_item(tuple, i);
Walter Dörwaldc58a3a12003-08-18 18:28:45 +00002500 if (item == NULL)
2501 goto Fail_1;
Walter Dörwald8dd19322003-02-10 17:36:40 +00002502 } else {
Alex Martellia9b9c9f2003-04-23 13:34:35 +00002503 PyErr_SetString(PyExc_TypeError, "filter(): unsubscriptable tuple");
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002504 goto Fail_1;
Walter Dörwald8dd19322003-02-10 17:36:40 +00002505 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002506 if (func == Py_None) {
2507 Py_INCREF(item);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002508 good = item;
2509 }
2510 else {
Raymond Hettinger8ae46892003-10-12 19:09:37 +00002511 PyObject *arg = PyTuple_Pack(1, item);
Walter Dörwaldc58a3a12003-08-18 18:28:45 +00002512 if (arg == NULL) {
2513 Py_DECREF(item);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002514 goto Fail_1;
Walter Dörwaldc58a3a12003-08-18 18:28:45 +00002515 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002516 good = PyEval_CallObject(func, arg);
2517 Py_DECREF(arg);
Walter Dörwaldc58a3a12003-08-18 18:28:45 +00002518 if (good == NULL) {
2519 Py_DECREF(item);
Guido van Rossum12d12c51993-10-26 17:58:25 +00002520 goto Fail_1;
Walter Dörwaldc58a3a12003-08-18 18:28:45 +00002521 }
Guido van Rossum12d12c51993-10-26 17:58:25 +00002522 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002523 ok = PyObject_IsTrue(good);
2524 Py_DECREF(good);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002525 if (ok) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002526 if (PyTuple_SetItem(result, j++, item) < 0)
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002527 goto Fail_1;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002528 }
Walter Dörwaldc58a3a12003-08-18 18:28:45 +00002529 else
2530 Py_DECREF(item);
Guido van Rossum12d12c51993-10-26 17:58:25 +00002531 }
2532
Tim Peters4324aa32001-05-28 22:30:08 +00002533 if (_PyTuple_Resize(&result, j) < 0)
Guido van Rossum12d12c51993-10-26 17:58:25 +00002534 return NULL;
2535
Guido van Rossum12d12c51993-10-26 17:58:25 +00002536 return result;
2537
Guido van Rossum12d12c51993-10-26 17:58:25 +00002538Fail_1:
Guido van Rossum79f25d91997-04-29 20:08:16 +00002539 Py_DECREF(result);
Guido van Rossum12d12c51993-10-26 17:58:25 +00002540 return NULL;
2541}
2542
2543
Guido van Rossume77a7571993-11-03 15:01:26 +00002544/* Helper for filter(): filter a string through a function */
Guido van Rossum12d12c51993-10-26 17:58:25 +00002545
Guido van Rossum79f25d91997-04-29 20:08:16 +00002546static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002547filterstring(PyObject *func, PyObject *strobj)
Guido van Rossum12d12c51993-10-26 17:58:25 +00002548{
Guido van Rossum79f25d91997-04-29 20:08:16 +00002549 PyObject *result;
Martin v. Löwis18e16552006-02-15 17:27:45 +00002550 Py_ssize_t i, j;
2551 Py_ssize_t len = PyString_Size(strobj);
2552 Py_ssize_t outlen = len;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002553
Guido van Rossum79f25d91997-04-29 20:08:16 +00002554 if (func == Py_None) {
Walter Dörwald1918f772003-02-10 13:19:13 +00002555 /* If it's a real string we can return the original,
2556 * as no character is ever false and __getitem__
2557 * does return this character. If it's a subclass
2558 * we must go through the __getitem__ loop */
2559 if (PyString_CheckExact(strobj)) {
Walter Dörwaldc3da83f2003-02-04 20:24:45 +00002560 Py_INCREF(strobj);
Walter Dörwald1918f772003-02-10 13:19:13 +00002561 return strobj;
2562 }
Guido van Rossum12d12c51993-10-26 17:58:25 +00002563 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002564 if ((result = PyString_FromStringAndSize(NULL, len)) == NULL)
Guido van Rossum2586bf01993-11-01 16:21:44 +00002565 return NULL;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002566
Guido van Rossum12d12c51993-10-26 17:58:25 +00002567 for (i = j = 0; i < len; ++i) {
Walter Dörwald1918f772003-02-10 13:19:13 +00002568 PyObject *item;
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002569 int ok;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002570
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002571 item = (*strobj->ob_type->tp_as_sequence->sq_item)(strobj, i);
2572 if (item == NULL)
2573 goto Fail_1;
Walter Dörwald1918f772003-02-10 13:19:13 +00002574 if (func==Py_None) {
2575 ok = 1;
2576 } else {
2577 PyObject *arg, *good;
Raymond Hettinger8ae46892003-10-12 19:09:37 +00002578 arg = PyTuple_Pack(1, item);
Walter Dörwald1918f772003-02-10 13:19:13 +00002579 if (arg == NULL) {
2580 Py_DECREF(item);
2581 goto Fail_1;
2582 }
2583 good = PyEval_CallObject(func, arg);
2584 Py_DECREF(arg);
2585 if (good == NULL) {
2586 Py_DECREF(item);
2587 goto Fail_1;
2588 }
2589 ok = PyObject_IsTrue(good);
2590 Py_DECREF(good);
Tim Peters388ed082001-04-07 20:34:48 +00002591 }
Walter Dörwald903f1e02003-02-04 16:28:00 +00002592 if (ok) {
Martin v. Löwisd96ee902006-02-16 14:37:16 +00002593 Py_ssize_t reslen;
Walter Dörwald903f1e02003-02-04 16:28:00 +00002594 if (!PyString_Check(item)) {
2595 PyErr_SetString(PyExc_TypeError, "can't filter str to str:"
2596 " __getitem__ returned different type");
2597 Py_DECREF(item);
2598 goto Fail_1;
2599 }
2600 reslen = PyString_GET_SIZE(item);
2601 if (reslen == 1) {
2602 PyString_AS_STRING(result)[j++] =
2603 PyString_AS_STRING(item)[0];
2604 } else {
2605 /* do we need more space? */
Martin v. Löwisd96ee902006-02-16 14:37:16 +00002606 Py_ssize_t need = j + reslen + len-i-1;
Walter Dörwald903f1e02003-02-04 16:28:00 +00002607 if (need > outlen) {
2608 /* overallocate, to avoid reallocations */
2609 if (need<2*outlen)
2610 need = 2*outlen;
2611 if (_PyString_Resize(&result, need)) {
2612 Py_DECREF(item);
2613 return NULL;
2614 }
2615 outlen = need;
2616 }
2617 memcpy(
2618 PyString_AS_STRING(result) + j,
2619 PyString_AS_STRING(item),
2620 reslen
2621 );
2622 j += reslen;
2623 }
2624 }
Tim Peters388ed082001-04-07 20:34:48 +00002625 Py_DECREF(item);
Guido van Rossum12d12c51993-10-26 17:58:25 +00002626 }
2627
Walter Dörwald903f1e02003-02-04 16:28:00 +00002628 if (j < outlen)
Tim Peters5de98422002-04-27 18:44:32 +00002629 _PyString_Resize(&result, j);
Guido van Rossum12d12c51993-10-26 17:58:25 +00002630
Guido van Rossum12d12c51993-10-26 17:58:25 +00002631 return result;
2632
Guido van Rossum12d12c51993-10-26 17:58:25 +00002633Fail_1:
Guido van Rossum79f25d91997-04-29 20:08:16 +00002634 Py_DECREF(result);
Guido van Rossum12d12c51993-10-26 17:58:25 +00002635 return NULL;
2636}
Martin v. Löwis8afd7572003-01-25 22:46:11 +00002637
2638#ifdef Py_USING_UNICODE
2639/* Helper for filter(): filter a Unicode object through a function */
2640
2641static PyObject *
2642filterunicode(PyObject *func, PyObject *strobj)
2643{
2644 PyObject *result;
Martin v. Löwis725507b2006-03-07 12:08:51 +00002645 register Py_ssize_t i, j;
Martin v. Löwisd96ee902006-02-16 14:37:16 +00002646 Py_ssize_t len = PyUnicode_GetSize(strobj);
2647 Py_ssize_t outlen = len;
Martin v. Löwis8afd7572003-01-25 22:46:11 +00002648
2649 if (func == Py_None) {
Walter Dörwald1918f772003-02-10 13:19:13 +00002650 /* If it's a real string we can return the original,
2651 * as no character is ever false and __getitem__
2652 * does return this character. If it's a subclass
2653 * we must go through the __getitem__ loop */
2654 if (PyUnicode_CheckExact(strobj)) {
Walter Dörwaldc3da83f2003-02-04 20:24:45 +00002655 Py_INCREF(strobj);
Walter Dörwald1918f772003-02-10 13:19:13 +00002656 return strobj;
2657 }
Martin v. Löwis8afd7572003-01-25 22:46:11 +00002658 }
2659 if ((result = PyUnicode_FromUnicode(NULL, len)) == NULL)
2660 return NULL;
2661
2662 for (i = j = 0; i < len; ++i) {
2663 PyObject *item, *arg, *good;
2664 int ok;
2665
2666 item = (*strobj->ob_type->tp_as_sequence->sq_item)(strobj, i);
2667 if (item == NULL)
2668 goto Fail_1;
Walter Dörwald1918f772003-02-10 13:19:13 +00002669 if (func == Py_None) {
2670 ok = 1;
2671 } else {
Raymond Hettinger8ae46892003-10-12 19:09:37 +00002672 arg = PyTuple_Pack(1, item);
Walter Dörwald1918f772003-02-10 13:19:13 +00002673 if (arg == NULL) {
2674 Py_DECREF(item);
2675 goto Fail_1;
2676 }
2677 good = PyEval_CallObject(func, arg);
2678 Py_DECREF(arg);
2679 if (good == NULL) {
2680 Py_DECREF(item);
2681 goto Fail_1;
2682 }
2683 ok = PyObject_IsTrue(good);
2684 Py_DECREF(good);
Martin v. Löwis8afd7572003-01-25 22:46:11 +00002685 }
Walter Dörwald903f1e02003-02-04 16:28:00 +00002686 if (ok) {
Martin v. Löwisd96ee902006-02-16 14:37:16 +00002687 Py_ssize_t reslen;
Walter Dörwald903f1e02003-02-04 16:28:00 +00002688 if (!PyUnicode_Check(item)) {
Georg Brandl99d7e4e2005-08-31 22:21:15 +00002689 PyErr_SetString(PyExc_TypeError,
Jeremy Hylton1aad9c72003-09-16 03:10:59 +00002690 "can't filter unicode to unicode:"
2691 " __getitem__ returned different type");
Walter Dörwald903f1e02003-02-04 16:28:00 +00002692 Py_DECREF(item);
2693 goto Fail_1;
2694 }
2695 reslen = PyUnicode_GET_SIZE(item);
Georg Brandl99d7e4e2005-08-31 22:21:15 +00002696 if (reslen == 1)
Walter Dörwald903f1e02003-02-04 16:28:00 +00002697 PyUnicode_AS_UNICODE(result)[j++] =
2698 PyUnicode_AS_UNICODE(item)[0];
Jeremy Hylton1aad9c72003-09-16 03:10:59 +00002699 else {
Walter Dörwald903f1e02003-02-04 16:28:00 +00002700 /* do we need more space? */
Martin v. Löwisd96ee902006-02-16 14:37:16 +00002701 Py_ssize_t need = j + reslen + len - i - 1;
Walter Dörwald903f1e02003-02-04 16:28:00 +00002702 if (need > outlen) {
Georg Brandl99d7e4e2005-08-31 22:21:15 +00002703 /* overallocate,
Jeremy Hylton1aad9c72003-09-16 03:10:59 +00002704 to avoid reallocations */
2705 if (need < 2 * outlen)
2706 need = 2 * outlen;
Jeremy Hylton364f6be2003-09-16 03:17:16 +00002707 if (PyUnicode_Resize(
2708 &result, need) < 0) {
Walter Dörwald903f1e02003-02-04 16:28:00 +00002709 Py_DECREF(item);
Walter Dörwald531e0002003-02-04 16:57:49 +00002710 goto Fail_1;
Walter Dörwald903f1e02003-02-04 16:28:00 +00002711 }
2712 outlen = need;
2713 }
Jeremy Hylton1aad9c72003-09-16 03:10:59 +00002714 memcpy(PyUnicode_AS_UNICODE(result) + j,
2715 PyUnicode_AS_UNICODE(item),
2716 reslen*sizeof(Py_UNICODE));
Walter Dörwald903f1e02003-02-04 16:28:00 +00002717 j += reslen;
2718 }
2719 }
Martin v. Löwis8afd7572003-01-25 22:46:11 +00002720 Py_DECREF(item);
2721 }
2722
Walter Dörwald903f1e02003-02-04 16:28:00 +00002723 if (j < outlen)
Martin v. Löwis8afd7572003-01-25 22:46:11 +00002724 PyUnicode_Resize(&result, j);
2725
2726 return result;
2727
2728Fail_1:
2729 Py_DECREF(result);
2730 return NULL;
2731}
2732#endif