blob: 92304b3862ea97b927d94725fa4b281f09cfc88a [file] [log] [blame]
Guido van Rossum3f5da241990-12-20 15:06:42 +00001/* Built-in functions */
2
Guido van Rossum79f25d91997-04-29 20:08:16 +00003#include "Python.h"
Guido van Rossum3f5da241990-12-20 15:06:42 +00004
5#include "node.h"
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006#include "code.h"
Guido van Rossum5b722181993-03-30 17:46:03 +00007#include "eval.h"
Guido van Rossum3f5da241990-12-20 15:06:42 +00008
Guido van Rossum6bf62da1997-04-11 20:37:35 +00009#include <ctype.h>
10
Guido van Rossume2ae77b2001-10-24 20:42:55 +000011#ifdef RISCOS
12#include "unixstuff.h"
13#endif
14
Mark Hammond26cffde2001-05-14 12:17:34 +000015/* The default encoding used by the platform file system APIs
16 Can remain NULL for all platforms that don't have such a concept
17*/
Martin v. Löwis6238d2b2002-06-30 15:26:10 +000018#if defined(MS_WINDOWS) && defined(HAVE_USABLE_WCHAR_T)
Mark Hammond26cffde2001-05-14 12:17:34 +000019const char *Py_FileSystemDefaultEncoding = "mbcs";
Just van Rossumb9b8e9c2003-02-10 09:22:01 +000020#elif defined(__APPLE__)
21const char *Py_FileSystemDefaultEncoding = "utf-8";
Mark Hammond26cffde2001-05-14 12:17:34 +000022#else
23const char *Py_FileSystemDefaultEncoding = NULL; /* use default */
24#endif
Mark Hammondef8b6542001-05-13 08:04:26 +000025
Guido van Rossum12d12c51993-10-26 17:58:25 +000026/* Forward */
Tim Petersdbd9ba62000-07-09 03:09:57 +000027static PyObject *filterstring(PyObject *, PyObject *);
Martin v. Löwis8afd7572003-01-25 22:46:11 +000028#ifdef Py_USING_UNICODE
29static PyObject *filterunicode(PyObject *, PyObject *);
30#endif
Tim Petersdbd9ba62000-07-09 03:09:57 +000031static PyObject *filtertuple (PyObject *, PyObject *);
Guido van Rossum12d12c51993-10-26 17:58:25 +000032
Guido van Rossum79f25d91997-04-29 20:08:16 +000033static PyObject *
Neal Norwitz92e212f2006-04-03 04:48:37 +000034builtin___import__(PyObject *self, PyObject *args, PyObject *kwds)
Guido van Rossum3f5da241990-12-20 15:06:42 +000035{
Neal Norwitz92e212f2006-04-03 04:48:37 +000036 static char *kwlist[] = {"name", "globals", "locals", "fromlist",
37 "level", 0};
Guido van Rossum1ae940a1995-01-02 19:04:15 +000038 char *name;
Guido van Rossum79f25d91997-04-29 20:08:16 +000039 PyObject *globals = NULL;
40 PyObject *locals = NULL;
41 PyObject *fromlist = NULL;
Thomas Woutersf7f438b2006-02-28 16:09:29 +000042 int level = -1;
Guido van Rossum1ae940a1995-01-02 19:04:15 +000043
Neal Norwitz92e212f2006-04-03 04:48:37 +000044 if (!PyArg_ParseTupleAndKeywords(args, kwds, "s|OOOi:__import__",
45 kwlist, &name, &globals, &locals, &fromlist, &level))
Guido van Rossum1ae940a1995-01-02 19:04:15 +000046 return NULL;
Thomas Woutersf7f438b2006-02-28 16:09:29 +000047 return PyImport_ImportModuleLevel(name, globals, locals,
48 fromlist, level);
Guido van Rossum1ae940a1995-01-02 19:04:15 +000049}
50
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000051PyDoc_STRVAR(import_doc,
Neal Norwitz92e212f2006-04-03 04:48:37 +000052"__import__(name, globals={}, locals={}, fromlist=[], level=-1) -> module\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +000053\n\
54Import a module. The globals are only used to determine the context;\n\
55they are not modified. The locals are currently unused. The fromlist\n\
56should be a list of names to emulate ``from name import ...'', or an\n\
57empty list to emulate ``import name''.\n\
58When importing a module from a package, note that __import__('A.B', ...)\n\
59returns package A when fromlist is empty, but its submodule B when\n\
Neal Norwitz92e212f2006-04-03 04:48:37 +000060fromlist is not empty. Level is used to determine whether to perform \n\
61absolute or relative imports. -1 is the original strategy of attempting\n\
62both absolute and relative imports, 0 is absolute, a positive number\n\
63is the number of parent directories to search relative to the current module.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +000064
Guido van Rossum1ae940a1995-01-02 19:04:15 +000065
Guido van Rossum79f25d91997-04-29 20:08:16 +000066static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +000067builtin_abs(PyObject *self, PyObject *v)
Guido van Rossum1ae940a1995-01-02 19:04:15 +000068{
Guido van Rossum09df08a1998-05-22 00:51:39 +000069 return PyNumber_Absolute(v);
Guido van Rossum3f5da241990-12-20 15:06:42 +000070}
71
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000072PyDoc_STRVAR(abs_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +000073"abs(number) -> number\n\
74\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000075Return the absolute value of the argument.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +000076
Raymond Hettinger96229b12005-03-11 06:49:40 +000077static PyObject *
78builtin_all(PyObject *self, PyObject *v)
79{
80 PyObject *it, *item;
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. */
Raymond Hettinger4e2f7142007-12-06 00:56:53 +0000239 len = _PyObject_LengthHint(seq, 8);
Guido van Rossum12d12c51993-10-26 17:58:25 +0000240
Tim Peters0e57abf2001-05-02 07:39:38 +0000241 /* Get a result list. */
Guido van Rossum79f25d91997-04-29 20:08:16 +0000242 if (PyList_Check(seq) && seq->ob_refcnt == 1) {
Tim Peters0e57abf2001-05-02 07:39:38 +0000243 /* Eww - can modify the list in-place. */
Guido van Rossum79f25d91997-04-29 20:08:16 +0000244 Py_INCREF(seq);
Guido van Rossum12d12c51993-10-26 17:58:25 +0000245 result = seq;
246 }
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000247 else {
Tim Peters0e57abf2001-05-02 07:39:38 +0000248 result = PyList_New(len);
249 if (result == NULL)
250 goto Fail_it;
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000251 }
Guido van Rossum12d12c51993-10-26 17:58:25 +0000252
Tim Peters0e57abf2001-05-02 07:39:38 +0000253 /* Build the result list. */
Tim Petersf4848da2001-05-05 00:14:56 +0000254 j = 0;
255 for (;;) {
Guido van Rossumc7903a12002-08-16 07:04:56 +0000256 PyObject *item;
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000257 int ok;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000258
Tim Peters0e57abf2001-05-02 07:39:38 +0000259 item = PyIter_Next(it);
260 if (item == NULL) {
Tim Petersf4848da2001-05-05 00:14:56 +0000261 if (PyErr_Occurred())
262 goto Fail_result_it;
Tim Peters0e57abf2001-05-02 07:39:38 +0000263 break;
Guido van Rossum2d951851994-08-29 12:52:16 +0000264 }
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000265
Neil Schemenauer68973552003-08-14 20:37:34 +0000266 if (func == (PyObject *)&PyBool_Type || func == Py_None) {
Guido van Rossumc7903a12002-08-16 07:04:56 +0000267 ok = PyObject_IsTrue(item);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000268 }
269 else {
Guido van Rossumc7903a12002-08-16 07:04:56 +0000270 PyObject *good;
271 PyTuple_SET_ITEM(arg, 0, item);
272 good = PyObject_Call(func, arg, NULL);
273 PyTuple_SET_ITEM(arg, 0, NULL);
Guido van Rossum58b68731995-01-10 17:40:55 +0000274 if (good == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000275 Py_DECREF(item);
Tim Peters0e57abf2001-05-02 07:39:38 +0000276 goto Fail_result_it;
Guido van Rossum58b68731995-01-10 17:40:55 +0000277 }
Guido van Rossumc7903a12002-08-16 07:04:56 +0000278 ok = PyObject_IsTrue(good);
279 Py_DECREF(good);
Guido van Rossum12d12c51993-10-26 17:58:25 +0000280 }
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000281 if (ok) {
Tim Peters0e57abf2001-05-02 07:39:38 +0000282 if (j < len)
283 PyList_SET_ITEM(result, j, item);
Guido van Rossum2d951851994-08-29 12:52:16 +0000284 else {
Barry Warsawfa77e091999-01-28 18:49:12 +0000285 int status = PyList_Append(result, item);
Barry Warsawfa77e091999-01-28 18:49:12 +0000286 Py_DECREF(item);
287 if (status < 0)
Tim Peters0e57abf2001-05-02 07:39:38 +0000288 goto Fail_result_it;
Guido van Rossum2d951851994-08-29 12:52:16 +0000289 }
Tim Peters0e57abf2001-05-02 07:39:38 +0000290 ++j;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000291 }
Tim Peters0e57abf2001-05-02 07:39:38 +0000292 else
293 Py_DECREF(item);
Guido van Rossum12d12c51993-10-26 17:58:25 +0000294 }
295
Guido van Rossum12d12c51993-10-26 17:58:25 +0000296
Tim Peters0e57abf2001-05-02 07:39:38 +0000297 /* Cut back result list if len is too big. */
Guido van Rossum79f25d91997-04-29 20:08:16 +0000298 if (j < len && PyList_SetSlice(result, j, len, NULL) < 0)
Tim Peters0e57abf2001-05-02 07:39:38 +0000299 goto Fail_result_it;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000300
Tim Peters3c6b1482001-05-21 08:07:05 +0000301 Py_DECREF(it);
Guido van Rossumc7903a12002-08-16 07:04:56 +0000302 Py_DECREF(arg);
Guido van Rossum12d12c51993-10-26 17:58:25 +0000303 return result;
304
Tim Peters0e57abf2001-05-02 07:39:38 +0000305Fail_result_it:
Guido van Rossum79f25d91997-04-29 20:08:16 +0000306 Py_DECREF(result);
Tim Peters0e57abf2001-05-02 07:39:38 +0000307Fail_it:
308 Py_DECREF(it);
Guido van Rossumc7903a12002-08-16 07:04:56 +0000309Fail_arg:
310 Py_DECREF(arg);
Guido van Rossum12d12c51993-10-26 17:58:25 +0000311 return NULL;
312}
313
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000314PyDoc_STRVAR(filter_doc,
Tim Petersd50e5442002-03-09 00:06:26 +0000315"filter(function or None, sequence) -> list, tuple, or string\n"
316"\n"
317"Return those items of sequence for which function(item) is true. If\n"
318"function is None, return the items that are true. If sequence is a tuple\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000319"or string, return the same type, else return a list.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000320
Guido van Rossum79f25d91997-04-29 20:08:16 +0000321static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000322builtin_chr(PyObject *self, PyObject *args)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000323{
324 long x;
325 char s[1];
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000326
Guido van Rossum79f25d91997-04-29 20:08:16 +0000327 if (!PyArg_ParseTuple(args, "l:chr", &x))
Guido van Rossum3f5da241990-12-20 15:06:42 +0000328 return NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000329 if (x < 0 || x >= 256) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000330 PyErr_SetString(PyExc_ValueError,
331 "chr() arg not in range(256)");
Guido van Rossum3f5da241990-12-20 15:06:42 +0000332 return NULL;
333 }
Guido van Rossum6bf62da1997-04-11 20:37:35 +0000334 s[0] = (char)x;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000335 return PyString_FromStringAndSize(s, 1);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000336}
337
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000338PyDoc_STRVAR(chr_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000339"chr(i) -> character\n\
340\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000341Return a string of one character with ordinal i; 0 <= i < 256.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000342
343
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000344#ifdef Py_USING_UNICODE
Guido van Rossum79f25d91997-04-29 20:08:16 +0000345static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000346builtin_unichr(PyObject *self, PyObject *args)
Guido van Rossum09095f32000-03-10 23:00:52 +0000347{
348 long x;
Guido van Rossum09095f32000-03-10 23:00:52 +0000349
350 if (!PyArg_ParseTuple(args, "l:unichr", &x))
351 return NULL;
Fredrik Lundh0dcf67e2001-06-26 20:01:56 +0000352
Marc-André Lemburgcc8764c2002-08-11 12:23:04 +0000353 return PyUnicode_FromOrdinal(x);
Guido van Rossum09095f32000-03-10 23:00:52 +0000354}
355
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000356PyDoc_STRVAR(unichr_doc,
Fred Drake078b24f2000-04-13 02:42:50 +0000357"unichr(i) -> Unicode character\n\
Guido van Rossum09095f32000-03-10 23:00:52 +0000358\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000359Return a Unicode string of one character with ordinal i; 0 <= i <= 0x10ffff.");
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000360#endif
Guido van Rossum09095f32000-03-10 23:00:52 +0000361
362
363static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000364builtin_cmp(PyObject *self, PyObject *args)
Guido van Rossuma9e7dc11992-10-18 18:53:57 +0000365{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000366 PyObject *a, *b;
Guido van Rossum09df08a1998-05-22 00:51:39 +0000367 int c;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000368
Raymond Hettingerea3fdf42002-12-29 16:33:45 +0000369 if (!PyArg_UnpackTuple(args, "cmp", 2, 2, &a, &b))
Guido van Rossuma9e7dc11992-10-18 18:53:57 +0000370 return NULL;
Guido van Rossum09df08a1998-05-22 00:51:39 +0000371 if (PyObject_Cmp(a, b, &c) < 0)
Guido van Rossumc8b6df91997-05-23 00:06:51 +0000372 return NULL;
Guido van Rossum09df08a1998-05-22 00:51:39 +0000373 return PyInt_FromLong((long)c);
Guido van Rossuma9e7dc11992-10-18 18:53:57 +0000374}
375
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000376PyDoc_STRVAR(cmp_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000377"cmp(x, y) -> integer\n\
378\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000379Return negative if x<y, zero if x==y, positive if x>y.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000380
381
Guido van Rossum79f25d91997-04-29 20:08:16 +0000382static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000383builtin_coerce(PyObject *self, PyObject *args)
Guido van Rossum6a00cd81995-01-07 12:39:01 +0000384{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000385 PyObject *v, *w;
386 PyObject *res;
Guido van Rossum5524a591995-01-10 15:26:20 +0000387
Neal Norwitzdf25efe2007-05-23 06:58:36 +0000388 if (Py_Py3kWarningFlag &&
389 PyErr_Warn(PyExc_DeprecationWarning,
390 "coerce() not supported in 3.x") < 0)
391 return NULL;
392
Raymond Hettingerea3fdf42002-12-29 16:33:45 +0000393 if (!PyArg_UnpackTuple(args, "coerce", 2, 2, &v, &w))
Guido van Rossum5524a591995-01-10 15:26:20 +0000394 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000395 if (PyNumber_Coerce(&v, &w) < 0)
Guido van Rossum04691fc1992-08-12 15:35:34 +0000396 return NULL;
Raymond Hettinger8ae46892003-10-12 19:09:37 +0000397 res = PyTuple_Pack(2, v, w);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000398 Py_DECREF(v);
399 Py_DECREF(w);
Guido van Rossum04691fc1992-08-12 15:35:34 +0000400 return res;
401}
402
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000403PyDoc_STRVAR(coerce_doc,
Martin v. Löwis8d494f32004-08-25 10:42:41 +0000404"coerce(x, y) -> (x1, y1)\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000405\n\
Martin v. Löwis8d494f32004-08-25 10:42:41 +0000406Return a tuple consisting of the two numeric arguments converted to\n\
407a common type, using the same rules as used by arithmetic operations.\n\
408If coercion is not possible, raise TypeError.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000409
Guido van Rossum79f25d91997-04-29 20:08:16 +0000410static PyObject *
Georg Brandl5240d742007-03-13 20:46:32 +0000411builtin_compile(PyObject *self, PyObject *args, PyObject *kwds)
Guido van Rossum5b722181993-03-30 17:46:03 +0000412{
413 char *str;
414 char *filename;
415 char *startstr;
416 int start;
Tim Peters6cd6a822001-08-17 22:11:27 +0000417 int dont_inherit = 0;
418 int supplied_flags = 0;
Tim Peters5ba58662001-07-16 02:29:45 +0000419 PyCompilerFlags cf;
Neal Norwitz3a9a3e72005-11-27 20:38:31 +0000420 PyObject *result = NULL, *cmd, *tmp = NULL;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000421 Py_ssize_t length;
Georg Brandl5240d742007-03-13 20:46:32 +0000422 static char *kwlist[] = {"source", "filename", "mode", "flags",
423 "dont_inherit", NULL};
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000424
Georg Brandl5240d742007-03-13 20:46:32 +0000425 if (!PyArg_ParseTupleAndKeywords(args, kwds, "Oss|ii:compile",
426 kwlist, &cmd, &filename, &startstr,
427 &supplied_flags, &dont_inherit))
Guido van Rossum5b722181993-03-30 17:46:03 +0000428 return NULL;
Tim Peters6cd6a822001-08-17 22:11:27 +0000429
Just van Rossum3aaf42c2003-02-10 08:21:10 +0000430 cf.cf_flags = supplied_flags;
431
432#ifdef Py_USING_UNICODE
433 if (PyUnicode_Check(cmd)) {
434 tmp = PyUnicode_AsUTF8String(cmd);
435 if (tmp == NULL)
436 return NULL;
437 cmd = tmp;
438 cf.cf_flags |= PyCF_SOURCE_IS_UTF8;
439 }
440#endif
Just van Rossumb9b8e9c2003-02-10 09:22:01 +0000441 if (PyObject_AsReadBuffer(cmd, (const void **)&str, &length))
442 return NULL;
Tim Peters2c646c92003-02-10 14:48:29 +0000443 if ((size_t)length != strlen(str)) {
Just van Rossum3aaf42c2003-02-10 08:21:10 +0000444 PyErr_SetString(PyExc_TypeError,
Alex Martellia9b9c9f2003-04-23 13:34:35 +0000445 "compile() expected string without null bytes");
Neal Norwitz3a9a3e72005-11-27 20:38:31 +0000446 goto cleanup;
Just van Rossum3aaf42c2003-02-10 08:21:10 +0000447 }
448
Guido van Rossum5b722181993-03-30 17:46:03 +0000449 if (strcmp(startstr, "exec") == 0)
Guido van Rossumb05a5c71997-05-07 17:46:13 +0000450 start = Py_file_input;
Guido van Rossum5b722181993-03-30 17:46:03 +0000451 else if (strcmp(startstr, "eval") == 0)
Guido van Rossumb05a5c71997-05-07 17:46:13 +0000452 start = Py_eval_input;
Guido van Rossum872537c1995-07-07 22:43:42 +0000453 else if (strcmp(startstr, "single") == 0)
Guido van Rossumb05a5c71997-05-07 17:46:13 +0000454 start = Py_single_input;
Guido van Rossum5b722181993-03-30 17:46:03 +0000455 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000456 PyErr_SetString(PyExc_ValueError,
Fred Drake661ea262000-10-24 19:57:45 +0000457 "compile() arg 3 must be 'exec' or 'eval' or 'single'");
Neal Norwitz3a9a3e72005-11-27 20:38:31 +0000458 goto cleanup;
Guido van Rossum5b722181993-03-30 17:46:03 +0000459 }
Tim Peters6cd6a822001-08-17 22:11:27 +0000460
Guido van Rossum4b499dd32003-02-13 22:07:59 +0000461 if (supplied_flags &
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000462 ~(PyCF_MASK | PyCF_MASK_OBSOLETE | PyCF_DONT_IMPLY_DEDENT | PyCF_ONLY_AST))
Guido van Rossum4b499dd32003-02-13 22:07:59 +0000463 {
Tim Peters6cd6a822001-08-17 22:11:27 +0000464 PyErr_SetString(PyExc_ValueError,
465 "compile(): unrecognised flags");
Neal Norwitz3a9a3e72005-11-27 20:38:31 +0000466 goto cleanup;
Tim Peters6cd6a822001-08-17 22:11:27 +0000467 }
468 /* XXX Warn if (supplied_flags & PyCF_MASK_OBSOLETE) != 0? */
469
Tim Peters6cd6a822001-08-17 22:11:27 +0000470 if (!dont_inherit) {
471 PyEval_MergeCompilerFlags(&cf);
472 }
Just van Rossum3aaf42c2003-02-10 08:21:10 +0000473 result = Py_CompileStringFlags(str, filename, start, &cf);
Neal Norwitz3a9a3e72005-11-27 20:38:31 +0000474cleanup:
Just van Rossum3aaf42c2003-02-10 08:21:10 +0000475 Py_XDECREF(tmp);
476 return result;
Guido van Rossum5b722181993-03-30 17:46:03 +0000477}
478
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000479PyDoc_STRVAR(compile_doc,
Tim Peters6cd6a822001-08-17 22:11:27 +0000480"compile(source, filename, mode[, flags[, dont_inherit]]) -> code object\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000481\n\
482Compile the source string (a Python module, statement or expression)\n\
483into a code object that can be executed by the exec statement or eval().\n\
484The filename will be used for run-time error messages.\n\
485The mode must be 'exec' to compile a module, 'single' to compile a\n\
Tim Peters6cd6a822001-08-17 22:11:27 +0000486single (interactive) statement, or 'eval' to compile an expression.\n\
487The flags argument, if present, controls which future statements influence\n\
488the compilation of the code.\n\
489The dont_inherit argument, if non-zero, stops the compilation inheriting\n\
490the effects of any future statements in effect in the code calling\n\
491compile; if absent or zero these statements do influence the compilation,\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000492in addition to any features explicitly specified.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000493
Guido van Rossum79f25d91997-04-29 20:08:16 +0000494static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000495builtin_dir(PyObject *self, PyObject *args)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000496{
Tim Peters5d2b77c2001-09-03 05:47:38 +0000497 PyObject *arg = NULL;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000498
Raymond Hettingerea3fdf42002-12-29 16:33:45 +0000499 if (!PyArg_UnpackTuple(args, "dir", 0, 1, &arg))
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000500 return NULL;
Tim Peters7eea37e2001-09-04 22:08:56 +0000501 return PyObject_Dir(arg);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000502}
503
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000504PyDoc_STRVAR(dir_doc,
Tim Peters5d2b77c2001-09-03 05:47:38 +0000505"dir([object]) -> list of strings\n"
506"\n"
Georg Brandl871f1bc2007-03-12 13:17:36 +0000507"If called without an argument, return the names in the current scope.\n"
508"Else, return an alphabetized list of names comprising (some of) the attributes\n"
509"of the given object, and of attributes reachable from it.\n"
510"If the object supplies a method named __dir__, it will be used; otherwise\n"
511"the default dir() logic is used and returns:\n"
512" for a module object: the module's attributes.\n"
513" for a class object: its attributes, and recursively the attributes\n"
514" of its bases.\n"
Georg Brandl3bb15672007-03-13 07:23:16 +0000515" for any other object: its attributes, its class's attributes, and\n"
Georg Brandl871f1bc2007-03-12 13:17:36 +0000516" recursively the attributes of its class's base classes.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000517
Guido van Rossum79f25d91997-04-29 20:08:16 +0000518static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000519builtin_divmod(PyObject *self, PyObject *args)
Guido van Rossum6a00cd81995-01-07 12:39:01 +0000520{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000521 PyObject *v, *w;
Guido van Rossum6a00cd81995-01-07 12:39:01 +0000522
Raymond Hettingerea3fdf42002-12-29 16:33:45 +0000523 if (!PyArg_UnpackTuple(args, "divmod", 2, 2, &v, &w))
Guido van Rossum6a00cd81995-01-07 12:39:01 +0000524 return NULL;
Guido van Rossum09df08a1998-05-22 00:51:39 +0000525 return PyNumber_Divmod(v, w);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000526}
527
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000528PyDoc_STRVAR(divmod_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000529"divmod(x, y) -> (div, mod)\n\
530\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000531Return the tuple ((x-x%y)/y, x%y). Invariant: div*y + mod == x.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000532
533
Guido van Rossum79f25d91997-04-29 20:08:16 +0000534static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000535builtin_eval(PyObject *self, PyObject *args)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000536{
Just van Rossum3aaf42c2003-02-10 08:21:10 +0000537 PyObject *cmd, *result, *tmp = NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000538 PyObject *globals = Py_None, *locals = Py_None;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000539 char *str;
Tim Peters9fa96be2001-08-17 23:04:59 +0000540 PyCompilerFlags cf;
Guido van Rossum590baa41993-11-30 13:40:46 +0000541
Raymond Hettinger214b1c32004-07-02 06:41:07 +0000542 if (!PyArg_UnpackTuple(args, "eval", 1, 3, &cmd, &globals, &locals))
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000543 return NULL;
Raymond Hettinger214b1c32004-07-02 06:41:07 +0000544 if (locals != Py_None && !PyMapping_Check(locals)) {
545 PyErr_SetString(PyExc_TypeError, "locals must be a mapping");
Raymond Hettinger513ffe82004-07-06 13:44:41 +0000546 return NULL;
Raymond Hettinger214b1c32004-07-02 06:41:07 +0000547 }
548 if (globals != Py_None && !PyDict_Check(globals)) {
Georg Brandl99d7e4e2005-08-31 22:21:15 +0000549 PyErr_SetString(PyExc_TypeError, PyMapping_Check(globals) ?
Raymond Hettinger214b1c32004-07-02 06:41:07 +0000550 "globals must be a real dict; try eval(expr, {}, mapping)"
551 : "globals must be a dict");
Raymond Hettinger513ffe82004-07-06 13:44:41 +0000552 return NULL;
Raymond Hettinger214b1c32004-07-02 06:41:07 +0000553 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000554 if (globals == Py_None) {
555 globals = PyEval_GetGlobals();
556 if (locals == Py_None)
557 locals = PyEval_GetLocals();
Guido van Rossum6135a871995-01-09 17:53:26 +0000558 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000559 else if (locals == Py_None)
Guido van Rossum6135a871995-01-09 17:53:26 +0000560 locals = globals;
Tim Peters9fa96be2001-08-17 23:04:59 +0000561
Georg Brandl77c85e62005-09-15 10:46:13 +0000562 if (globals == NULL || locals == NULL) {
563 PyErr_SetString(PyExc_TypeError,
564 "eval must be given globals and locals "
565 "when called without a frame");
566 return NULL;
567 }
568
Guido van Rossum79f25d91997-04-29 20:08:16 +0000569 if (PyDict_GetItemString(globals, "__builtins__") == NULL) {
570 if (PyDict_SetItemString(globals, "__builtins__",
571 PyEval_GetBuiltins()) != 0)
Guido van Rossum6135a871995-01-09 17:53:26 +0000572 return NULL;
573 }
Tim Peters9fa96be2001-08-17 23:04:59 +0000574
Jeremy Hylton15c1c4f2001-07-30 21:50:55 +0000575 if (PyCode_Check(cmd)) {
Jeremy Hylton733c8932001-12-13 19:51:56 +0000576 if (PyCode_GetNumFree((PyCodeObject *)cmd) > 0) {
Jeremy Hylton15c1c4f2001-07-30 21:50:55 +0000577 PyErr_SetString(PyExc_TypeError,
578 "code object passed to eval() may not contain free variables");
579 return NULL;
580 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000581 return PyEval_EvalCode((PyCodeObject *) cmd, globals, locals);
Jeremy Hylton15c1c4f2001-07-30 21:50:55 +0000582 }
Tim Peters9fa96be2001-08-17 23:04:59 +0000583
Marc-André Lemburgd1ba4432000-09-19 21:04:18 +0000584 if (!PyString_Check(cmd) &&
585 !PyUnicode_Check(cmd)) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000586 PyErr_SetString(PyExc_TypeError,
Fred Drake661ea262000-10-24 19:57:45 +0000587 "eval() arg 1 must be a string or code object");
Guido van Rossum94390a41992-08-14 15:14:30 +0000588 return NULL;
589 }
Just van Rossum3aaf42c2003-02-10 08:21:10 +0000590 cf.cf_flags = 0;
591
592#ifdef Py_USING_UNICODE
593 if (PyUnicode_Check(cmd)) {
594 tmp = PyUnicode_AsUTF8String(cmd);
595 if (tmp == NULL)
596 return NULL;
597 cmd = tmp;
598 cf.cf_flags |= PyCF_SOURCE_IS_UTF8;
599 }
600#endif
Neal Norwitz3a9a3e72005-11-27 20:38:31 +0000601 if (PyString_AsStringAndSize(cmd, &str, NULL)) {
602 Py_XDECREF(tmp);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000603 return NULL;
Neal Norwitz3a9a3e72005-11-27 20:38:31 +0000604 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000605 while (*str == ' ' || *str == '\t')
606 str++;
Tim Peters9fa96be2001-08-17 23:04:59 +0000607
Tim Peters9fa96be2001-08-17 23:04:59 +0000608 (void)PyEval_MergeCompilerFlags(&cf);
Just van Rossum3aaf42c2003-02-10 08:21:10 +0000609 result = PyRun_StringFlags(str, Py_eval_input, globals, locals, &cf);
610 Py_XDECREF(tmp);
611 return result;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000612}
613
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000614PyDoc_STRVAR(eval_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000615"eval(source[, globals[, locals]]) -> value\n\
616\n\
617Evaluate the source in the context of globals and locals.\n\
618The source may be a string representing a Python expression\n\
619or a code object as returned by compile().\n\
Neal Norwitz477ca1c2006-09-05 02:25:41 +0000620The globals must be a dictionary and locals can be any mapping,\n\
Raymond Hettinger214b1c32004-07-02 06:41:07 +0000621defaulting to the current globals and locals.\n\
622If only globals is given, locals defaults to it.\n");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000623
624
Guido van Rossum79f25d91997-04-29 20:08:16 +0000625static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000626builtin_execfile(PyObject *self, PyObject *args)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000627{
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000628 char *filename;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000629 PyObject *globals = Py_None, *locals = Py_None;
630 PyObject *res;
Guido van Rossumb3a639e2001-09-05 13:37:47 +0000631 FILE* fp = NULL;
Tim Peters5ba58662001-07-16 02:29:45 +0000632 PyCompilerFlags cf;
Martin v. Löwis6b3a2c42001-08-08 05:30:36 +0000633 int exists;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000634
Neal Norwitzdf25efe2007-05-23 06:58:36 +0000635 if (Py_Py3kWarningFlag &&
636 PyErr_Warn(PyExc_DeprecationWarning,
637 "execfile() not supported in 3.x") < 0)
638 return NULL;
639
Raymond Hettinger66bd2332004-08-02 08:30:07 +0000640 if (!PyArg_ParseTuple(args, "s|O!O:execfile",
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000641 &filename,
Guido van Rossum79f25d91997-04-29 20:08:16 +0000642 &PyDict_Type, &globals,
Raymond Hettinger66bd2332004-08-02 08:30:07 +0000643 &locals))
Guido van Rossum0f61f8a1992-02-25 18:55:05 +0000644 return NULL;
Raymond Hettinger66bd2332004-08-02 08:30:07 +0000645 if (locals != Py_None && !PyMapping_Check(locals)) {
646 PyErr_SetString(PyExc_TypeError, "locals must be a mapping");
647 return NULL;
648 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000649 if (globals == Py_None) {
650 globals = PyEval_GetGlobals();
651 if (locals == Py_None)
652 locals = PyEval_GetLocals();
Guido van Rossum6135a871995-01-09 17:53:26 +0000653 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000654 else if (locals == Py_None)
Guido van Rossum6135a871995-01-09 17:53:26 +0000655 locals = globals;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000656 if (PyDict_GetItemString(globals, "__builtins__") == NULL) {
657 if (PyDict_SetItemString(globals, "__builtins__",
658 PyEval_GetBuiltins()) != 0)
Guido van Rossum6135a871995-01-09 17:53:26 +0000659 return NULL;
660 }
Martin v. Löwis6b3a2c42001-08-08 05:30:36 +0000661
662 exists = 0;
663 /* Test for existence or directory. */
Martin v. Löwis3484a182002-03-09 12:07:51 +0000664#if defined(PLAN9)
665 {
666 Dir *d;
667
668 if ((d = dirstat(filename))!=nil) {
669 if(d->mode & DMDIR)
670 werrstr("is a directory");
671 else
672 exists = 1;
673 free(d);
674 }
Martin v. Löwis6b3a2c42001-08-08 05:30:36 +0000675 }
Martin v. Löwis3484a182002-03-09 12:07:51 +0000676#elif defined(RISCOS)
Guido van Rossume2ae77b2001-10-24 20:42:55 +0000677 if (object_exists(filename)) {
678 if (isdir(filename))
679 errno = EISDIR;
680 else
681 exists = 1;
682 }
Martin v. Löwis3484a182002-03-09 12:07:51 +0000683#else /* standard Posix */
684 {
685 struct stat s;
686 if (stat(filename, &s) == 0) {
687 if (S_ISDIR(s.st_mode))
Andrew MacIntyreda4d6cb2004-03-29 11:53:38 +0000688# if defined(PYOS_OS2) && defined(PYCC_VACPP)
Martin v. Löwis3484a182002-03-09 12:07:51 +0000689 errno = EOS2ERR;
690# else
691 errno = EISDIR;
692# endif
693 else
694 exists = 1;
695 }
696 }
697#endif
Martin v. Löwis6b3a2c42001-08-08 05:30:36 +0000698
699 if (exists) {
700 Py_BEGIN_ALLOW_THREADS
Jack Jansen7b8c7542002-04-14 20:12:41 +0000701 fp = fopen(filename, "r" PY_STDIOTEXTMODE);
Martin v. Löwis6b3a2c42001-08-08 05:30:36 +0000702 Py_END_ALLOW_THREADS
703
704 if (fp == NULL) {
705 exists = 0;
706 }
707 }
708
709 if (!exists) {
Peter Schneider-Kamp4c013422002-08-27 16:58:00 +0000710 PyErr_SetFromErrnoWithFilename(PyExc_IOError, filename);
Guido van Rossum0f61f8a1992-02-25 18:55:05 +0000711 return NULL;
712 }
Tim Peters5ba58662001-07-16 02:29:45 +0000713 cf.cf_flags = 0;
714 if (PyEval_MergeCompilerFlags(&cf))
Tim Peters748b8bb2001-04-28 08:20:22 +0000715 res = PyRun_FileExFlags(fp, filename, Py_file_input, globals,
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000716 locals, 1, &cf);
Tim Peters5ba58662001-07-16 02:29:45 +0000717 else
Tim Peters748b8bb2001-04-28 08:20:22 +0000718 res = PyRun_FileEx(fp, filename, Py_file_input, globals,
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000719 locals, 1);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000720 return res;
Guido van Rossum0f61f8a1992-02-25 18:55:05 +0000721}
722
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000723PyDoc_STRVAR(execfile_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000724"execfile(filename[, globals[, locals]])\n\
725\n\
726Read and execute a Python script from a file.\n\
727The globals and locals are dictionaries, defaulting to the current\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000728globals and locals. If only globals is given, locals defaults to it.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000729
730
Guido van Rossum79f25d91997-04-29 20:08:16 +0000731static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000732builtin_getattr(PyObject *self, PyObject *args)
Guido van Rossum33894be1992-01-27 16:53:09 +0000733{
Guido van Rossum950ff291998-06-29 13:38:57 +0000734 PyObject *v, *result, *dflt = NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000735 PyObject *name;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000736
Raymond Hettingerea3fdf42002-12-29 16:33:45 +0000737 if (!PyArg_UnpackTuple(args, "getattr", 2, 3, &v, &name, &dflt))
Guido van Rossum33894be1992-01-27 16:53:09 +0000738 return NULL;
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000739#ifdef Py_USING_UNICODE
Jeremy Hylton0eb11152001-07-30 22:39:31 +0000740 if (PyUnicode_Check(name)) {
741 name = _PyUnicode_AsDefaultEncodedString(name, NULL);
742 if (name == NULL)
743 return NULL;
744 }
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000745#endif
Jeremy Hylton0eb11152001-07-30 22:39:31 +0000746
747 if (!PyString_Check(name)) {
748 PyErr_SetString(PyExc_TypeError,
Alex Martellia9b9c9f2003-04-23 13:34:35 +0000749 "getattr(): attribute name must be string");
Jeremy Hylton0eb11152001-07-30 22:39:31 +0000750 return NULL;
751 }
Guido van Rossum950ff291998-06-29 13:38:57 +0000752 result = PyObject_GetAttr(v, name);
Guido van Rossumd8923572001-10-16 21:31:32 +0000753 if (result == NULL && dflt != NULL &&
754 PyErr_ExceptionMatches(PyExc_AttributeError))
755 {
Guido van Rossum950ff291998-06-29 13:38:57 +0000756 PyErr_Clear();
757 Py_INCREF(dflt);
758 result = dflt;
759 }
760 return result;
Guido van Rossum9bfef441993-03-29 10:43:31 +0000761}
762
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000763PyDoc_STRVAR(getattr_doc,
Guido van Rossum950ff291998-06-29 13:38:57 +0000764"getattr(object, name[, default]) -> value\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000765\n\
Guido van Rossum950ff291998-06-29 13:38:57 +0000766Get a named attribute from an object; getattr(x, 'y') is equivalent to x.y.\n\
767When a default argument is given, it is returned when the attribute doesn't\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000768exist; without it, an exception is raised in that case.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000769
770
Guido van Rossum79f25d91997-04-29 20:08:16 +0000771static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000772builtin_globals(PyObject *self)
Guido van Rossum872537c1995-07-07 22:43:42 +0000773{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000774 PyObject *d;
Guido van Rossum872537c1995-07-07 22:43:42 +0000775
Guido van Rossum79f25d91997-04-29 20:08:16 +0000776 d = PyEval_GetGlobals();
Neal Norwitz43bd4db2006-08-12 01:46:42 +0000777 Py_XINCREF(d);
Guido van Rossum872537c1995-07-07 22:43:42 +0000778 return d;
779}
780
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000781PyDoc_STRVAR(globals_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000782"globals() -> dictionary\n\
783\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000784Return the dictionary containing the current scope's global variables.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000785
786
Guido van Rossum79f25d91997-04-29 20:08:16 +0000787static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000788builtin_hasattr(PyObject *self, PyObject *args)
Guido van Rossum9bfef441993-03-29 10:43:31 +0000789{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000790 PyObject *v;
791 PyObject *name;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000792
Raymond Hettingerea3fdf42002-12-29 16:33:45 +0000793 if (!PyArg_UnpackTuple(args, "hasattr", 2, 2, &v, &name))
Guido van Rossum9bfef441993-03-29 10:43:31 +0000794 return NULL;
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000795#ifdef Py_USING_UNICODE
Jeremy Hylton302b54a2001-07-30 22:45:19 +0000796 if (PyUnicode_Check(name)) {
797 name = _PyUnicode_AsDefaultEncodedString(name, NULL);
798 if (name == NULL)
799 return NULL;
800 }
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000801#endif
Jeremy Hylton302b54a2001-07-30 22:45:19 +0000802
803 if (!PyString_Check(name)) {
804 PyErr_SetString(PyExc_TypeError,
Alex Martellia9b9c9f2003-04-23 13:34:35 +0000805 "hasattr(): attribute name must be string");
Jeremy Hylton302b54a2001-07-30 22:45:19 +0000806 return NULL;
807 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000808 v = PyObject_GetAttr(v, name);
Guido van Rossum9bfef441993-03-29 10:43:31 +0000809 if (v == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000810 PyErr_Clear();
Guido van Rossum09df08a1998-05-22 00:51:39 +0000811 Py_INCREF(Py_False);
812 return Py_False;
Guido van Rossum9bfef441993-03-29 10:43:31 +0000813 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000814 Py_DECREF(v);
Guido van Rossum09df08a1998-05-22 00:51:39 +0000815 Py_INCREF(Py_True);
816 return Py_True;
Guido van Rossum33894be1992-01-27 16:53:09 +0000817}
818
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000819PyDoc_STRVAR(hasattr_doc,
Guido van Rossum77f6a652002-04-03 22:41:51 +0000820"hasattr(object, name) -> bool\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000821\n\
822Return whether the object has an attribute with the given name.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000823(This is done by calling getattr(object, name) and catching exceptions.)");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000824
825
Guido van Rossum79f25d91997-04-29 20:08:16 +0000826static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000827builtin_id(PyObject *self, PyObject *v)
Guido van Rossum5b722181993-03-30 17:46:03 +0000828{
Guido van Rossum106f2da2000-06-28 21:12:25 +0000829 return PyLong_FromVoidPtr(v);
Guido van Rossum5b722181993-03-30 17:46:03 +0000830}
831
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000832PyDoc_STRVAR(id_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000833"id(object) -> integer\n\
834\n\
835Return the identity of an object. This is guaranteed to be unique among\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000836simultaneously existing objects. (Hint: it's the object's memory address.)");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000837
838
Guido van Rossum79f25d91997-04-29 20:08:16 +0000839static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000840builtin_map(PyObject *self, PyObject *args)
Guido van Rossum12d12c51993-10-26 17:58:25 +0000841{
842 typedef struct {
Tim Peters4e9afdc2001-05-03 23:54:49 +0000843 PyObject *it; /* the iterator object */
844 int saw_StopIteration; /* bool: did the iterator end? */
Guido van Rossum12d12c51993-10-26 17:58:25 +0000845 } sequence;
846
Guido van Rossum79f25d91997-04-29 20:08:16 +0000847 PyObject *func, *result;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000848 sequence *seqs = NULL, *sqp;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000849 Py_ssize_t n, len;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000850 register int i, j;
851
Guido van Rossum79f25d91997-04-29 20:08:16 +0000852 n = PyTuple_Size(args);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000853 if (n < 2) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000854 PyErr_SetString(PyExc_TypeError,
855 "map() requires at least two args");
Guido van Rossum12d12c51993-10-26 17:58:25 +0000856 return NULL;
857 }
858
Guido van Rossum79f25d91997-04-29 20:08:16 +0000859 func = PyTuple_GetItem(args, 0);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000860 n--;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000861
Guido van Rossumfa4ac711998-07-10 17:37:30 +0000862 if (func == Py_None && n == 1) {
863 /* map(None, S) is the same as list(S). */
864 return PySequence_List(PyTuple_GetItem(args, 1));
865 }
866
Tim Peters4e9afdc2001-05-03 23:54:49 +0000867 /* Get space for sequence descriptors. Must NULL out the iterator
868 * pointers so that jumping to Fail_2 later doesn't see trash.
869 */
Guido van Rossum79f25d91997-04-29 20:08:16 +0000870 if ((seqs = PyMem_NEW(sequence, n)) == NULL) {
871 PyErr_NoMemory();
Tim Peters4e9afdc2001-05-03 23:54:49 +0000872 return NULL;
873 }
874 for (i = 0; i < n; ++i) {
875 seqs[i].it = (PyObject*)NULL;
876 seqs[i].saw_StopIteration = 0;
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000877 }
Guido van Rossum12d12c51993-10-26 17:58:25 +0000878
Tim Peters4e9afdc2001-05-03 23:54:49 +0000879 /* Do a first pass to obtain iterators for the arguments, and set len
880 * to the largest of their lengths.
Tim Peters748b8bb2001-04-28 08:20:22 +0000881 */
Tim Peters4e9afdc2001-05-03 23:54:49 +0000882 len = 0;
883 for (i = 0, sqp = seqs; i < n; ++i, ++sqp) {
884 PyObject *curseq;
Martin v. Löwisd96ee902006-02-16 14:37:16 +0000885 Py_ssize_t curlen;
Guido van Rossumad991772001-01-12 16:03:05 +0000886
Tim Peters4e9afdc2001-05-03 23:54:49 +0000887 /* Get iterator. */
888 curseq = PyTuple_GetItem(args, i+1);
889 sqp->it = PyObject_GetIter(curseq);
890 if (sqp->it == NULL) {
Guido van Rossum12d12c51993-10-26 17:58:25 +0000891 static char errmsg[] =
Tim Peters4e9afdc2001-05-03 23:54:49 +0000892 "argument %d to map() must support iteration";
Guido van Rossum15e33a41997-04-30 19:00:27 +0000893 char errbuf[sizeof(errmsg) + 25];
Jeremy Hylton518ab1c2001-11-28 20:42:20 +0000894 PyOS_snprintf(errbuf, sizeof(errbuf), errmsg, i+2);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000895 PyErr_SetString(PyExc_TypeError, errbuf);
Guido van Rossum12d12c51993-10-26 17:58:25 +0000896 goto Fail_2;
897 }
898
Tim Peters4e9afdc2001-05-03 23:54:49 +0000899 /* Update len. */
Raymond Hettinger4e2f7142007-12-06 00:56:53 +0000900 curlen = _PyObject_LengthHint(curseq, 8);
Raymond Hettinger77f3c872004-01-04 08:54:44 +0000901 if (curlen > len)
902 len = curlen;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000903 }
904
Tim Peters4e9afdc2001-05-03 23:54:49 +0000905 /* Get space for the result list. */
Guido van Rossum79f25d91997-04-29 20:08:16 +0000906 if ((result = (PyObject *) PyList_New(len)) == NULL)
Guido van Rossum12d12c51993-10-26 17:58:25 +0000907 goto Fail_2;
908
Tim Peters4e9afdc2001-05-03 23:54:49 +0000909 /* Iterate over the sequences until all have stopped. */
Guido van Rossum2d951851994-08-29 12:52:16 +0000910 for (i = 0; ; ++i) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000911 PyObject *alist, *item=NULL, *value;
Tim Peters4e9afdc2001-05-03 23:54:49 +0000912 int numactive = 0;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000913
Guido van Rossum79f25d91997-04-29 20:08:16 +0000914 if (func == Py_None && n == 1)
Guido van Rossum32120311995-07-10 13:52:21 +0000915 alist = NULL;
Tim Peters4e9afdc2001-05-03 23:54:49 +0000916 else if ((alist = PyTuple_New(n)) == NULL)
917 goto Fail_1;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000918
919 for (j = 0, sqp = seqs; j < n; ++j, ++sqp) {
Tim Peters4e9afdc2001-05-03 23:54:49 +0000920 if (sqp->saw_StopIteration) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000921 Py_INCREF(Py_None);
922 item = Py_None;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000923 }
Guido van Rossum12d12c51993-10-26 17:58:25 +0000924 else {
Tim Peters4e9afdc2001-05-03 23:54:49 +0000925 item = PyIter_Next(sqp->it);
926 if (item)
927 ++numactive;
928 else {
Tim Peters4e9afdc2001-05-03 23:54:49 +0000929 if (PyErr_Occurred()) {
Tim Petersf4848da2001-05-05 00:14:56 +0000930 Py_XDECREF(alist);
931 goto Fail_1;
Guido van Rossum2d951851994-08-29 12:52:16 +0000932 }
Tim Peters4e9afdc2001-05-03 23:54:49 +0000933 Py_INCREF(Py_None);
934 item = Py_None;
935 sqp->saw_StopIteration = 1;
Guido van Rossum2d951851994-08-29 12:52:16 +0000936 }
Guido van Rossum12d12c51993-10-26 17:58:25 +0000937 }
Tim Peters4e9afdc2001-05-03 23:54:49 +0000938 if (alist)
939 PyTuple_SET_ITEM(alist, j, item);
940 else
Guido van Rossum2d951851994-08-29 12:52:16 +0000941 break;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000942 }
943
Guido van Rossum32120311995-07-10 13:52:21 +0000944 if (!alist)
945 alist = item;
Guido van Rossum2d951851994-08-29 12:52:16 +0000946
Tim Peters4e9afdc2001-05-03 23:54:49 +0000947 if (numactive == 0) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000948 Py_DECREF(alist);
Guido van Rossum2d951851994-08-29 12:52:16 +0000949 break;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000950 }
Guido van Rossum2d951851994-08-29 12:52:16 +0000951
Guido van Rossum79f25d91997-04-29 20:08:16 +0000952 if (func == Py_None)
Guido van Rossum32120311995-07-10 13:52:21 +0000953 value = alist;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000954 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000955 value = PyEval_CallObject(func, alist);
956 Py_DECREF(alist);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000957 if (value == NULL)
958 goto Fail_1;
Guido van Rossum2d951851994-08-29 12:52:16 +0000959 }
960 if (i >= len) {
Barry Warsawfa77e091999-01-28 18:49:12 +0000961 int status = PyList_Append(result, value);
Barry Warsaw21332871999-01-28 04:21:35 +0000962 Py_DECREF(value);
Barry Warsawfa77e091999-01-28 18:49:12 +0000963 if (status < 0)
964 goto Fail_1;
Guido van Rossum2d951851994-08-29 12:52:16 +0000965 }
Tim Peters4e9afdc2001-05-03 23:54:49 +0000966 else if (PyList_SetItem(result, i, value) < 0)
967 goto Fail_1;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000968 }
969
Guido van Rossumfa4ac711998-07-10 17:37:30 +0000970 if (i < len && PyList_SetSlice(result, i, len, NULL) < 0)
971 goto Fail_1;
972
Tim Peters4e9afdc2001-05-03 23:54:49 +0000973 goto Succeed;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000974
Guido van Rossum12d12c51993-10-26 17:58:25 +0000975Fail_1:
Guido van Rossum79f25d91997-04-29 20:08:16 +0000976 Py_DECREF(result);
Guido van Rossum12d12c51993-10-26 17:58:25 +0000977Fail_2:
Tim Peters4e9afdc2001-05-03 23:54:49 +0000978 result = NULL;
979Succeed:
980 assert(seqs);
981 for (i = 0; i < n; ++i)
982 Py_XDECREF(seqs[i].it);
983 PyMem_DEL(seqs);
984 return result;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000985}
986
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000987PyDoc_STRVAR(map_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000988"map(function, sequence[, sequence, ...]) -> list\n\
989\n\
990Return a list of the results of applying the function to the items of\n\
991the argument sequence(s). If more than one sequence is given, the\n\
992function is called with an argument list consisting of the corresponding\n\
993item of each sequence, substituting None for missing values when not all\n\
994sequences have the same length. If the function is None, return a list of\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000995the items of the sequence (or a list of tuples if more than one sequence).");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000996
997
Guido van Rossum79f25d91997-04-29 20:08:16 +0000998static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000999builtin_setattr(PyObject *self, PyObject *args)
Guido van Rossum33894be1992-01-27 16:53:09 +00001000{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001001 PyObject *v;
1002 PyObject *name;
1003 PyObject *value;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001004
Raymond Hettingerea3fdf42002-12-29 16:33:45 +00001005 if (!PyArg_UnpackTuple(args, "setattr", 3, 3, &v, &name, &value))
Guido van Rossum33894be1992-01-27 16:53:09 +00001006 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001007 if (PyObject_SetAttr(v, name, value) != 0)
Guido van Rossum33894be1992-01-27 16:53:09 +00001008 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001009 Py_INCREF(Py_None);
1010 return Py_None;
Guido van Rossum33894be1992-01-27 16:53:09 +00001011}
1012
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001013PyDoc_STRVAR(setattr_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001014"setattr(object, name, value)\n\
1015\n\
1016Set a named attribute on an object; setattr(x, 'y', v) is equivalent to\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001017``x.y = v''.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001018
1019
Guido van Rossum79f25d91997-04-29 20:08:16 +00001020static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001021builtin_delattr(PyObject *self, PyObject *args)
Guido van Rossum14144fc1994-08-29 12:53:40 +00001022{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001023 PyObject *v;
1024 PyObject *name;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001025
Raymond Hettingerea3fdf42002-12-29 16:33:45 +00001026 if (!PyArg_UnpackTuple(args, "delattr", 2, 2, &v, &name))
Guido van Rossum14144fc1994-08-29 12:53:40 +00001027 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001028 if (PyObject_SetAttr(v, name, (PyObject *)NULL) != 0)
Guido van Rossum14144fc1994-08-29 12:53:40 +00001029 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001030 Py_INCREF(Py_None);
1031 return Py_None;
Guido van Rossum14144fc1994-08-29 12:53:40 +00001032}
1033
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001034PyDoc_STRVAR(delattr_doc,
Guido van Rossumdf12a591998-11-23 22:13:04 +00001035"delattr(object, name)\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001036\n\
1037Delete a named attribute on an object; delattr(x, 'y') is equivalent to\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001038``del x.y''.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001039
1040
Guido van Rossum79f25d91997-04-29 20:08:16 +00001041static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001042builtin_hash(PyObject *self, PyObject *v)
Guido van Rossum9bfef441993-03-29 10:43:31 +00001043{
Guido van Rossum9bfef441993-03-29 10:43:31 +00001044 long x;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001045
Guido van Rossum79f25d91997-04-29 20:08:16 +00001046 x = PyObject_Hash(v);
Guido van Rossum9bfef441993-03-29 10:43:31 +00001047 if (x == -1)
1048 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001049 return PyInt_FromLong(x);
Guido van Rossum9bfef441993-03-29 10:43:31 +00001050}
1051
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001052PyDoc_STRVAR(hash_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001053"hash(object) -> integer\n\
1054\n\
1055Return a hash value for the object. Two objects with the same value have\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001056the same hash value. The reverse is not necessarily true, but likely.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001057
1058
Guido van Rossum79f25d91997-04-29 20:08:16 +00001059static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001060builtin_hex(PyObject *self, PyObject *v)
Guido van Rossum006bcd41991-10-24 14:54:44 +00001061{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001062 PyNumberMethods *nb;
Neil Schemenauer3a313e32004-07-19 16:29:17 +00001063 PyObject *res;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001064
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001065 if ((nb = v->ob_type->tp_as_number) == NULL ||
Guido van Rossum1899c2e1992-09-12 11:09:23 +00001066 nb->nb_hex == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001067 PyErr_SetString(PyExc_TypeError,
Guido van Rossum1899c2e1992-09-12 11:09:23 +00001068 "hex() argument can't be converted to hex");
1069 return NULL;
Guido van Rossum006bcd41991-10-24 14:54:44 +00001070 }
Neil Schemenauer3a313e32004-07-19 16:29:17 +00001071 res = (*nb->nb_hex)(v);
1072 if (res && !PyString_Check(res)) {
1073 PyErr_Format(PyExc_TypeError,
1074 "__hex__ returned non-string (type %.200s)",
1075 res->ob_type->tp_name);
1076 Py_DECREF(res);
1077 return NULL;
1078 }
1079 return res;
Guido van Rossum006bcd41991-10-24 14:54:44 +00001080}
1081
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001082PyDoc_STRVAR(hex_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001083"hex(number) -> string\n\
1084\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001085Return the hexadecimal representation of an integer or long integer.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001086
1087
Tim Petersdbd9ba62000-07-09 03:09:57 +00001088static PyObject *builtin_raw_input(PyObject *, PyObject *);
Guido van Rossum3165fe61992-09-25 21:59:05 +00001089
Guido van Rossum79f25d91997-04-29 20:08:16 +00001090static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001091builtin_input(PyObject *self, PyObject *args)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001092{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001093 PyObject *line;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001094 char *str;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001095 PyObject *res;
1096 PyObject *globals, *locals;
Hye-Shik Changff83c2b2004-02-02 13:39:01 +00001097 PyCompilerFlags cf;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001098
1099 line = builtin_raw_input(self, args);
Guido van Rossum3165fe61992-09-25 21:59:05 +00001100 if (line == NULL)
1101 return line;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001102 if (!PyArg_Parse(line, "s;embedded '\\0' in input line", &str))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001103 return NULL;
1104 while (*str == ' ' || *str == '\t')
1105 str++;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001106 globals = PyEval_GetGlobals();
1107 locals = PyEval_GetLocals();
1108 if (PyDict_GetItemString(globals, "__builtins__") == NULL) {
1109 if (PyDict_SetItemString(globals, "__builtins__",
1110 PyEval_GetBuiltins()) != 0)
Guido van Rossum6135a871995-01-09 17:53:26 +00001111 return NULL;
1112 }
Hye-Shik Changff83c2b2004-02-02 13:39:01 +00001113 cf.cf_flags = 0;
1114 PyEval_MergeCompilerFlags(&cf);
1115 res = PyRun_StringFlags(str, Py_eval_input, globals, locals, &cf);
Guido van Rossum79f25d91997-04-29 20:08:16 +00001116 Py_DECREF(line);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001117 return res;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001118}
1119
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001120PyDoc_STRVAR(input_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001121"input([prompt]) -> value\n\
1122\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001123Equivalent to eval(raw_input(prompt)).");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001124
1125
Guido van Rossume8811f81997-02-14 15:48:05 +00001126static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001127builtin_intern(PyObject *self, PyObject *args)
Guido van Rossume8811f81997-02-14 15:48:05 +00001128{
1129 PyObject *s;
Guido van Rossum43713e52000-02-29 13:59:29 +00001130 if (!PyArg_ParseTuple(args, "S:intern", &s))
Guido van Rossume8811f81997-02-14 15:48:05 +00001131 return NULL;
Jeremy Hylton4c989dd2004-08-07 19:20:05 +00001132 if (!PyString_CheckExact(s)) {
1133 PyErr_SetString(PyExc_TypeError,
1134 "can't intern subclass of string");
1135 return NULL;
1136 }
Guido van Rossume8811f81997-02-14 15:48:05 +00001137 Py_INCREF(s);
1138 PyString_InternInPlace(&s);
1139 return s;
1140}
1141
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001142PyDoc_STRVAR(intern_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001143"intern(string) -> string\n\
1144\n\
1145``Intern'' the given string. This enters the string in the (global)\n\
1146table of interned strings whose purpose is to speed up dictionary lookups.\n\
1147Return the string itself or the previously interned string object with the\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001148same value.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001149
1150
Guido van Rossum79f25d91997-04-29 20:08:16 +00001151static PyObject *
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001152builtin_iter(PyObject *self, PyObject *args)
1153{
1154 PyObject *v, *w = NULL;
1155
Raymond Hettingerea3fdf42002-12-29 16:33:45 +00001156 if (!PyArg_UnpackTuple(args, "iter", 1, 2, &v, &w))
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001157 return NULL;
1158 if (w == NULL)
1159 return PyObject_GetIter(v);
1160 if (!PyCallable_Check(v)) {
1161 PyErr_SetString(PyExc_TypeError,
1162 "iter(v, w): v must be callable");
1163 return NULL;
1164 }
1165 return PyCallIter_New(v, w);
1166}
1167
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001168PyDoc_STRVAR(iter_doc,
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001169"iter(collection) -> iterator\n\
1170iter(callable, sentinel) -> iterator\n\
1171\n\
1172Get an iterator from an object. In the first form, the argument must\n\
1173supply its own iterator, or be a sequence.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001174In the second form, the callable is called until it returns the sentinel.");
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001175
1176
1177static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001178builtin_len(PyObject *self, PyObject *v)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001179{
Martin v. Löwis18e16552006-02-15 17:27:45 +00001180 Py_ssize_t res;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001181
Jeremy Hylton03657cf2000-07-12 13:05:33 +00001182 res = PyObject_Size(v);
Guido van Rossum8ea9f4d1998-06-29 22:26:50 +00001183 if (res < 0 && PyErr_Occurred())
1184 return NULL;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001185 return PyInt_FromSsize_t(res);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001186}
1187
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001188PyDoc_STRVAR(len_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001189"len(object) -> integer\n\
1190\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001191Return the number of items of a sequence or mapping.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001192
1193
Guido van Rossum79f25d91997-04-29 20:08:16 +00001194static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001195builtin_locals(PyObject *self)
Guido van Rossum872537c1995-07-07 22:43:42 +00001196{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001197 PyObject *d;
Guido van Rossum872537c1995-07-07 22:43:42 +00001198
Guido van Rossum79f25d91997-04-29 20:08:16 +00001199 d = PyEval_GetLocals();
Neal Norwitz43bd4db2006-08-12 01:46:42 +00001200 Py_XINCREF(d);
Guido van Rossum872537c1995-07-07 22:43:42 +00001201 return d;
1202}
1203
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001204PyDoc_STRVAR(locals_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001205"locals() -> dictionary\n\
1206\n\
Raymond Hettinger69bf8f32003-01-04 02:16:22 +00001207Update and return a dictionary containing the current scope's local variables.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001208
1209
Guido van Rossum79f25d91997-04-29 20:08:16 +00001210static PyObject *
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001211min_max(PyObject *args, PyObject *kwds, int op)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001212{
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001213 PyObject *v, *it, *item, *val, *maxitem, *maxval, *keyfunc=NULL;
Martin v. Löwisfd39ad42004-08-12 14:42:37 +00001214 const char *name = op == Py_LT ? "min" : "max";
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001215
Guido van Rossum79f25d91997-04-29 20:08:16 +00001216 if (PyTuple_Size(args) > 1)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001217 v = args;
Martin v. Löwisfd39ad42004-08-12 14:42:37 +00001218 else if (!PyArg_UnpackTuple(args, (char *)name, 1, 1, &v))
Guido van Rossum3f5da241990-12-20 15:06:42 +00001219 return NULL;
Tim Peters67d687a2002-04-29 21:27:32 +00001220
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001221 if (kwds != NULL && PyDict_Check(kwds) && PyDict_Size(kwds)) {
1222 keyfunc = PyDict_GetItemString(kwds, "key");
1223 if (PyDict_Size(kwds)!=1 || keyfunc == NULL) {
Georg Brandl99d7e4e2005-08-31 22:21:15 +00001224 PyErr_Format(PyExc_TypeError,
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001225 "%s() got an unexpected keyword argument", name);
1226 return NULL;
1227 }
Georg Brandl99d7e4e2005-08-31 22:21:15 +00001228 }
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001229
Tim Petersc3074532001-05-03 07:00:32 +00001230 it = PyObject_GetIter(v);
1231 if (it == NULL)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001232 return NULL;
Tim Petersc3074532001-05-03 07:00:32 +00001233
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001234 maxitem = NULL; /* the result */
1235 maxval = NULL; /* the value associated with the result */
Brett Cannon9e635cf2004-12-07 00:25:35 +00001236 while (( item = PyIter_Next(it) )) {
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001237 /* get the value from the key function */
1238 if (keyfunc != NULL) {
1239 val = PyObject_CallFunctionObjArgs(keyfunc, item, NULL);
1240 if (val == NULL)
1241 goto Fail_it_item;
1242 }
1243 /* no key function; the value is the item */
1244 else {
1245 val = item;
1246 Py_INCREF(val);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001247 }
Tim Petersc3074532001-05-03 07:00:32 +00001248
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001249 /* maximum value and item are unset; set them */
1250 if (maxval == NULL) {
1251 maxitem = item;
1252 maxval = val;
1253 }
1254 /* maximum value and item are set; update them as necessary */
Guido van Rossum2d951851994-08-29 12:52:16 +00001255 else {
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001256 int cmp = PyObject_RichCompareBool(val, maxval, op);
1257 if (cmp < 0)
1258 goto Fail_it_item_and_val;
1259 else if (cmp > 0) {
1260 Py_DECREF(maxval);
1261 Py_DECREF(maxitem);
1262 maxval = val;
1263 maxitem = item;
Guido van Rossum53451b32001-01-17 15:47:24 +00001264 }
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001265 else {
1266 Py_DECREF(item);
1267 Py_DECREF(val);
Guido van Rossumc8b6df91997-05-23 00:06:51 +00001268 }
Guido van Rossum2d951851994-08-29 12:52:16 +00001269 }
Guido van Rossum3f5da241990-12-20 15:06:42 +00001270 }
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001271 if (PyErr_Occurred())
1272 goto Fail_it;
1273 if (maxval == NULL) {
Martin v. Löwisfd39ad42004-08-12 14:42:37 +00001274 PyErr_Format(PyExc_ValueError,
1275 "%s() arg is an empty sequence", name);
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001276 assert(maxitem == NULL);
1277 }
1278 else
1279 Py_DECREF(maxval);
Tim Petersc3074532001-05-03 07:00:32 +00001280 Py_DECREF(it);
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001281 return maxitem;
1282
1283Fail_it_item_and_val:
1284 Py_DECREF(val);
1285Fail_it_item:
1286 Py_DECREF(item);
1287Fail_it:
1288 Py_XDECREF(maxval);
1289 Py_XDECREF(maxitem);
1290 Py_DECREF(it);
1291 return NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001292}
1293
Guido van Rossum79f25d91997-04-29 20:08:16 +00001294static PyObject *
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001295builtin_min(PyObject *self, PyObject *args, PyObject *kwds)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001296{
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001297 return min_max(args, kwds, Py_LT);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001298}
1299
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001300PyDoc_STRVAR(min_doc,
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001301"min(iterable[, key=func]) -> value\n\
1302min(a, b, c, ...[, key=func]) -> value\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001303\n\
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001304With a single iterable argument, return its smallest item.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001305With two or more arguments, return the smallest argument.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001306
1307
Guido van Rossum79f25d91997-04-29 20:08:16 +00001308static PyObject *
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001309builtin_max(PyObject *self, PyObject *args, PyObject *kwds)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001310{
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001311 return min_max(args, kwds, Py_GT);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001312}
1313
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001314PyDoc_STRVAR(max_doc,
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001315"max(iterable[, key=func]) -> value\n\
1316max(a, b, c, ...[, key=func]) -> value\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001317\n\
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001318With a single iterable argument, return its largest item.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001319With two or more arguments, return the largest argument.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001320
1321
Guido van Rossum79f25d91997-04-29 20:08:16 +00001322static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001323builtin_oct(PyObject *self, PyObject *v)
Guido van Rossum006bcd41991-10-24 14:54:44 +00001324{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001325 PyNumberMethods *nb;
Neil Schemenauer3a313e32004-07-19 16:29:17 +00001326 PyObject *res;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001327
Guido van Rossum1899c2e1992-09-12 11:09:23 +00001328 if (v == NULL || (nb = v->ob_type->tp_as_number) == NULL ||
1329 nb->nb_oct == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001330 PyErr_SetString(PyExc_TypeError,
Guido van Rossum1899c2e1992-09-12 11:09:23 +00001331 "oct() argument can't be converted to oct");
1332 return NULL;
Guido van Rossum006bcd41991-10-24 14:54:44 +00001333 }
Neil Schemenauer3a313e32004-07-19 16:29:17 +00001334 res = (*nb->nb_oct)(v);
1335 if (res && !PyString_Check(res)) {
1336 PyErr_Format(PyExc_TypeError,
1337 "__oct__ returned non-string (type %.200s)",
1338 res->ob_type->tp_name);
1339 Py_DECREF(res);
1340 return NULL;
1341 }
1342 return res;
Guido van Rossum006bcd41991-10-24 14:54:44 +00001343}
1344
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001345PyDoc_STRVAR(oct_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001346"oct(number) -> string\n\
1347\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001348Return the octal representation of an integer or long integer.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001349
1350
Guido van Rossum79f25d91997-04-29 20:08:16 +00001351static PyObject *
Neal Norwitzc4edb0e2006-05-02 04:43:14 +00001352builtin_open(PyObject *self, PyObject *args, PyObject *kwds)
1353{
1354 return PyObject_Call((PyObject*)&PyFile_Type, args, kwds);
1355}
1356
1357PyDoc_STRVAR(open_doc,
1358"open(name[, mode[, buffering]]) -> file object\n\
1359\n\
Skip Montanaro4e3ebe02007-12-08 14:37:43 +00001360Open a file using the file() type, returns a file object. This is the\n\
1361preferred way to open a file.");
Neal Norwitzc4edb0e2006-05-02 04:43:14 +00001362
1363
1364static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001365builtin_ord(PyObject *self, PyObject* obj)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001366{
Guido van Rossum09095f32000-03-10 23:00:52 +00001367 long ord;
Martin v. Löwisd96ee902006-02-16 14:37:16 +00001368 Py_ssize_t size;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001369
Jeremy Hylton394b54d2000-04-12 21:19:47 +00001370 if (PyString_Check(obj)) {
1371 size = PyString_GET_SIZE(obj);
Andrew M. Kuchling34c20cf2000-12-20 14:36:56 +00001372 if (size == 1) {
Jeremy Hylton394b54d2000-04-12 21:19:47 +00001373 ord = (long)((unsigned char)*PyString_AS_STRING(obj));
Andrew M. Kuchling34c20cf2000-12-20 14:36:56 +00001374 return PyInt_FromLong(ord);
1375 }
Martin v. Löwis339d0f72001-08-17 18:39:25 +00001376#ifdef Py_USING_UNICODE
Jeremy Hylton394b54d2000-04-12 21:19:47 +00001377 } else if (PyUnicode_Check(obj)) {
1378 size = PyUnicode_GET_SIZE(obj);
Andrew M. Kuchling34c20cf2000-12-20 14:36:56 +00001379 if (size == 1) {
Jeremy Hylton394b54d2000-04-12 21:19:47 +00001380 ord = (long)*PyUnicode_AS_UNICODE(obj);
Andrew M. Kuchling34c20cf2000-12-20 14:36:56 +00001381 return PyInt_FromLong(ord);
1382 }
Martin v. Löwis339d0f72001-08-17 18:39:25 +00001383#endif
Jeremy Hylton394b54d2000-04-12 21:19:47 +00001384 } else {
1385 PyErr_Format(PyExc_TypeError,
Andrew M. Kuchlingf07aad12000-12-23 14:11:28 +00001386 "ord() expected string of length 1, but " \
Jeremy Hylton394b54d2000-04-12 21:19:47 +00001387 "%.200s found", obj->ob_type->tp_name);
Guido van Rossum09095f32000-03-10 23:00:52 +00001388 return NULL;
1389 }
1390
Guido van Rossumad991772001-01-12 16:03:05 +00001391 PyErr_Format(PyExc_TypeError,
Andrew M. Kuchlingf07aad12000-12-23 14:11:28 +00001392 "ord() expected a character, "
Martin v. Löwisd96ee902006-02-16 14:37:16 +00001393 "but string of length %zd found",
Jeremy Hylton394b54d2000-04-12 21:19:47 +00001394 size);
1395 return NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001396}
1397
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001398PyDoc_STRVAR(ord_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001399"ord(c) -> integer\n\
1400\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001401Return the integer ordinal of a one-character string.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001402
1403
Guido van Rossum79f25d91997-04-29 20:08:16 +00001404static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001405builtin_pow(PyObject *self, PyObject *args)
Guido van Rossum6a00cd81995-01-07 12:39:01 +00001406{
Guido van Rossum09df08a1998-05-22 00:51:39 +00001407 PyObject *v, *w, *z = Py_None;
Guido van Rossum6a00cd81995-01-07 12:39:01 +00001408
Raymond Hettingerea3fdf42002-12-29 16:33:45 +00001409 if (!PyArg_UnpackTuple(args, "pow", 2, 3, &v, &w, &z))
Guido van Rossum6a00cd81995-01-07 12:39:01 +00001410 return NULL;
Guido van Rossum09df08a1998-05-22 00:51:39 +00001411 return PyNumber_Power(v, w, z);
Guido van Rossumd4905451991-05-05 20:00:36 +00001412}
1413
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001414PyDoc_STRVAR(pow_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001415"pow(x, y[, z]) -> number\n\
1416\n\
1417With two arguments, equivalent to x**y. With three arguments,\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001418equivalent to (x**y) % z, but may be more efficient (e.g. for longs).");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001419
1420
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001421
1422/* Return number of items in range (lo, hi, step), when arguments are
1423 * PyInt or PyLong objects. step > 0 required. Return a value < 0 if
1424 * & only if the true value is too large to fit in a signed long.
1425 * Arguments MUST return 1 with either PyInt_Check() or
1426 * PyLong_Check(). Return -1 when there is an error.
1427 */
1428static long
1429get_len_of_range_longs(PyObject *lo, PyObject *hi, PyObject *step)
1430{
1431 /* -------------------------------------------------------------
1432 Algorithm is equal to that of get_len_of_range(), but it operates
1433 on PyObjects (which are assumed to be PyLong or PyInt objects).
1434 ---------------------------------------------------------------*/
1435 long n;
1436 PyObject *diff = NULL;
1437 PyObject *one = NULL;
1438 PyObject *tmp1 = NULL, *tmp2 = NULL, *tmp3 = NULL;
1439 /* holds sub-expression evaluations */
1440
1441 /* if (lo >= hi), return length of 0. */
1442 if (PyObject_Compare(lo, hi) >= 0)
1443 return 0;
1444
1445 if ((one = PyLong_FromLong(1L)) == NULL)
1446 goto Fail;
1447
1448 if ((tmp1 = PyNumber_Subtract(hi, lo)) == NULL)
1449 goto Fail;
1450
1451 if ((diff = PyNumber_Subtract(tmp1, one)) == NULL)
1452 goto Fail;
1453
1454 if ((tmp2 = PyNumber_FloorDivide(diff, step)) == NULL)
1455 goto Fail;
1456
1457 if ((tmp3 = PyNumber_Add(tmp2, one)) == NULL)
1458 goto Fail;
1459
1460 n = PyLong_AsLong(tmp3);
1461 if (PyErr_Occurred()) { /* Check for Overflow */
1462 PyErr_Clear();
1463 goto Fail;
1464 }
1465
1466 Py_DECREF(tmp3);
1467 Py_DECREF(tmp2);
1468 Py_DECREF(diff);
1469 Py_DECREF(tmp1);
1470 Py_DECREF(one);
1471 return n;
1472
1473 Fail:
1474 Py_XDECREF(tmp3);
1475 Py_XDECREF(tmp2);
1476 Py_XDECREF(diff);
1477 Py_XDECREF(tmp1);
1478 Py_XDECREF(one);
1479 return -1;
1480}
1481
1482/* An extension of builtin_range() that handles the case when PyLong
1483 * arguments are given. */
1484static PyObject *
1485handle_range_longs(PyObject *self, PyObject *args)
1486{
1487 PyObject *ilow;
Tim Peters874e1f72003-04-13 22:13:08 +00001488 PyObject *ihigh = NULL;
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001489 PyObject *istep = NULL;
Tim Peters874e1f72003-04-13 22:13:08 +00001490
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001491 PyObject *curnum = NULL;
1492 PyObject *v = NULL;
1493 long bign;
1494 int i, n;
1495 int cmp_result;
1496
Tim Peters874e1f72003-04-13 22:13:08 +00001497 PyObject *zero = PyLong_FromLong(0);
1498
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001499 if (zero == NULL)
1500 return NULL;
1501
Tim Peters874e1f72003-04-13 22:13:08 +00001502 if (!PyArg_UnpackTuple(args, "range", 1, 3, &ilow, &ihigh, &istep)) {
1503 Py_DECREF(zero);
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001504 return NULL;
1505 }
1506
Tim Peters874e1f72003-04-13 22:13:08 +00001507 /* Figure out which way we were called, supply defaults, and be
1508 * sure to incref everything so that the decrefs at the end
1509 * are correct.
1510 */
1511 assert(ilow != NULL);
1512 if (ihigh == NULL) {
1513 /* only 1 arg -- it's the upper limit */
1514 ihigh = ilow;
1515 ilow = NULL;
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001516 }
Tim Peters874e1f72003-04-13 22:13:08 +00001517 assert(ihigh != NULL);
1518 Py_INCREF(ihigh);
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001519
Tim Peters874e1f72003-04-13 22:13:08 +00001520 /* ihigh correct now; do ilow */
1521 if (ilow == NULL)
1522 ilow = zero;
1523 Py_INCREF(ilow);
1524
1525 /* ilow and ihigh correct now; do istep */
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001526 if (istep == NULL) {
1527 istep = PyLong_FromLong(1L);
1528 if (istep == NULL)
1529 goto Fail;
1530 }
1531 else {
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001532 Py_INCREF(istep);
1533 }
1534
Tim Peters874e1f72003-04-13 22:13:08 +00001535 if (!PyInt_Check(ilow) && !PyLong_Check(ilow)) {
Guido van Rossum28e83e32003-04-15 12:43:26 +00001536 PyErr_Format(PyExc_TypeError,
Alex Martellif471d472003-04-23 13:00:44 +00001537 "range() integer start argument expected, got %s.",
Guido van Rossum817d6c92003-04-14 18:25:04 +00001538 ilow->ob_type->tp_name);
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001539 goto Fail;
1540 }
1541
Tim Peters874e1f72003-04-13 22:13:08 +00001542 if (!PyInt_Check(ihigh) && !PyLong_Check(ihigh)) {
Guido van Rossum28e83e32003-04-15 12:43:26 +00001543 PyErr_Format(PyExc_TypeError,
Alex Martellif471d472003-04-23 13:00:44 +00001544 "range() integer end argument expected, got %s.",
Guido van Rossum817d6c92003-04-14 18:25:04 +00001545 ihigh->ob_type->tp_name);
Tim Peters874e1f72003-04-13 22:13:08 +00001546 goto Fail;
1547 }
1548
1549 if (!PyInt_Check(istep) && !PyLong_Check(istep)) {
Guido van Rossum28e83e32003-04-15 12:43:26 +00001550 PyErr_Format(PyExc_TypeError,
Alex Martellif471d472003-04-23 13:00:44 +00001551 "range() integer step argument expected, got %s.",
Guido van Rossum817d6c92003-04-14 18:25:04 +00001552 istep->ob_type->tp_name);
Tim Peters874e1f72003-04-13 22:13:08 +00001553 goto Fail;
1554 }
1555
1556 if (PyObject_Cmp(istep, zero, &cmp_result) == -1)
1557 goto Fail;
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001558 if (cmp_result == 0) {
1559 PyErr_SetString(PyExc_ValueError,
Alex Martellif471d472003-04-23 13:00:44 +00001560 "range() step argument must not be zero");
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001561 goto Fail;
1562 }
1563
1564 if (cmp_result > 0)
1565 bign = get_len_of_range_longs(ilow, ihigh, istep);
1566 else {
1567 PyObject *neg_istep = PyNumber_Negative(istep);
1568 if (neg_istep == NULL)
1569 goto Fail;
1570 bign = get_len_of_range_longs(ihigh, ilow, neg_istep);
1571 Py_DECREF(neg_istep);
1572 }
1573
1574 n = (int)bign;
1575 if (bign < 0 || (long)n != bign) {
1576 PyErr_SetString(PyExc_OverflowError,
1577 "range() result has too many items");
1578 goto Fail;
1579 }
1580
1581 v = PyList_New(n);
1582 if (v == NULL)
1583 goto Fail;
1584
1585 curnum = ilow;
1586 Py_INCREF(curnum);
1587
1588 for (i = 0; i < n; i++) {
1589 PyObject *w = PyNumber_Long(curnum);
1590 PyObject *tmp_num;
1591 if (w == NULL)
1592 goto Fail;
1593
1594 PyList_SET_ITEM(v, i, w);
1595
1596 tmp_num = PyNumber_Add(curnum, istep);
1597 if (tmp_num == NULL)
1598 goto Fail;
1599
1600 Py_DECREF(curnum);
1601 curnum = tmp_num;
1602 }
Tim Peters874e1f72003-04-13 22:13:08 +00001603 Py_DECREF(ilow);
1604 Py_DECREF(ihigh);
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001605 Py_DECREF(istep);
1606 Py_DECREF(zero);
Tim Peters874e1f72003-04-13 22:13:08 +00001607 Py_DECREF(curnum);
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001608 return v;
1609
1610 Fail:
Tim Peters874e1f72003-04-13 22:13:08 +00001611 Py_DECREF(ilow);
1612 Py_DECREF(ihigh);
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001613 Py_XDECREF(istep);
Tim Peters874e1f72003-04-13 22:13:08 +00001614 Py_DECREF(zero);
1615 Py_XDECREF(curnum);
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001616 Py_XDECREF(v);
1617 return NULL;
1618}
1619
Guido van Rossum124eff01999-02-23 16:11:01 +00001620/* Return number of items in range/xrange (lo, hi, step). step > 0
1621 * required. Return a value < 0 if & only if the true value is too
1622 * large to fit in a signed long.
1623 */
1624static long
Thomas Wouterse28c2962000-07-23 22:21:32 +00001625get_len_of_range(long lo, long hi, long step)
Guido van Rossum124eff01999-02-23 16:11:01 +00001626{
1627 /* -------------------------------------------------------------
1628 If lo >= hi, the range is empty.
1629 Else if n values are in the range, the last one is
1630 lo + (n-1)*step, which must be <= hi-1. Rearranging,
1631 n <= (hi - lo - 1)/step + 1, so taking the floor of the RHS gives
1632 the proper value. Since lo < hi in this case, hi-lo-1 >= 0, so
1633 the RHS is non-negative and so truncation is the same as the
1634 floor. Letting M be the largest positive long, the worst case
1635 for the RHS numerator is hi=M, lo=-M-1, and then
1636 hi-lo-1 = M-(-M-1)-1 = 2*M. Therefore unsigned long has enough
1637 precision to compute the RHS exactly.
1638 ---------------------------------------------------------------*/
1639 long n = 0;
1640 if (lo < hi) {
1641 unsigned long uhi = (unsigned long)hi;
1642 unsigned long ulo = (unsigned long)lo;
1643 unsigned long diff = uhi - ulo - 1;
1644 n = (long)(diff / (unsigned long)step + 1);
1645 }
1646 return n;
1647}
1648
Guido van Rossum79f25d91997-04-29 20:08:16 +00001649static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001650builtin_range(PyObject *self, PyObject *args)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001651{
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001652 long ilow = 0, ihigh = 0, istep = 1;
Guido van Rossum124eff01999-02-23 16:11:01 +00001653 long bign;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001654 int i, n;
Guido van Rossum124eff01999-02-23 16:11:01 +00001655
Guido van Rossum79f25d91997-04-29 20:08:16 +00001656 PyObject *v;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001657
Guido van Rossum79f25d91997-04-29 20:08:16 +00001658 if (PyTuple_Size(args) <= 1) {
1659 if (!PyArg_ParseTuple(args,
Guido van Rossum0865dd91995-01-17 16:30:22 +00001660 "l;range() requires 1-3 int arguments",
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001661 &ihigh)) {
1662 PyErr_Clear();
1663 return handle_range_longs(self, args);
1664 }
Guido van Rossum3f5da241990-12-20 15:06:42 +00001665 }
1666 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001667 if (!PyArg_ParseTuple(args,
Guido van Rossum0865dd91995-01-17 16:30:22 +00001668 "ll|l;range() requires 1-3 int arguments",
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001669 &ilow, &ihigh, &istep)) {
1670 PyErr_Clear();
1671 return handle_range_longs(self, args);
1672 }
Guido van Rossum3f5da241990-12-20 15:06:42 +00001673 }
1674 if (istep == 0) {
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001675 PyErr_SetString(PyExc_ValueError,
Alex Martellif471d472003-04-23 13:00:44 +00001676 "range() step argument must not be zero");
Guido van Rossum3f5da241990-12-20 15:06:42 +00001677 return NULL;
1678 }
Guido van Rossum124eff01999-02-23 16:11:01 +00001679 if (istep > 0)
1680 bign = get_len_of_range(ilow, ihigh, istep);
1681 else
1682 bign = get_len_of_range(ihigh, ilow, -istep);
1683 n = (int)bign;
1684 if (bign < 0 || (long)n != bign) {
Guido van Rossume23cde21999-01-12 05:07:47 +00001685 PyErr_SetString(PyExc_OverflowError,
Fred Drake661ea262000-10-24 19:57:45 +00001686 "range() result has too many items");
Guido van Rossume23cde21999-01-12 05:07:47 +00001687 return NULL;
1688 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00001689 v = PyList_New(n);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001690 if (v == NULL)
1691 return NULL;
1692 for (i = 0; i < n; i++) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001693 PyObject *w = PyInt_FromLong(ilow);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001694 if (w == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001695 Py_DECREF(v);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001696 return NULL;
1697 }
Guido van Rossuma937d141998-04-24 18:22:02 +00001698 PyList_SET_ITEM(v, i, w);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001699 ilow += istep;
1700 }
1701 return v;
1702}
1703
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001704PyDoc_STRVAR(range_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001705"range([start,] stop[, step]) -> list of integers\n\
1706\n\
1707Return a list containing an arithmetic progression of integers.\n\
1708range(i, j) returns [i, i+1, i+2, ..., j-1]; start (!) defaults to 0.\n\
1709When step is given, it specifies the increment (or decrement).\n\
1710For example, range(4) returns [0, 1, 2, 3]. The end point is omitted!\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001711These are exactly the valid indices for a list of 4 elements.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001712
1713
Guido van Rossum79f25d91997-04-29 20:08:16 +00001714static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001715builtin_raw_input(PyObject *self, PyObject *args)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001716{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001717 PyObject *v = NULL;
Martin v. Löwis31d2df52002-08-14 15:46:02 +00001718 PyObject *fin = PySys_GetObject("stdin");
1719 PyObject *fout = PySys_GetObject("stdout");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001720
Raymond Hettingerea3fdf42002-12-29 16:33:45 +00001721 if (!PyArg_UnpackTuple(args, "[raw_]input", 0, 1, &v))
Guido van Rossum3165fe61992-09-25 21:59:05 +00001722 return NULL;
Martin v. Löwis31d2df52002-08-14 15:46:02 +00001723
1724 if (fin == NULL) {
Alex Martellia9b9c9f2003-04-23 13:34:35 +00001725 PyErr_SetString(PyExc_RuntimeError, "[raw_]input: lost sys.stdin");
Martin v. Löwis31d2df52002-08-14 15:46:02 +00001726 return NULL;
1727 }
1728 if (fout == NULL) {
Alex Martellia9b9c9f2003-04-23 13:34:35 +00001729 PyErr_SetString(PyExc_RuntimeError, "[raw_]input: lost sys.stdout");
Martin v. Löwis31d2df52002-08-14 15:46:02 +00001730 return NULL;
1731 }
1732 if (PyFile_SoftSpace(fout, 0)) {
1733 if (PyFile_WriteString(" ", fout) != 0)
1734 return NULL;
1735 }
Georg Brandl7e3ba2a2006-08-06 08:23:54 +00001736 if (PyFile_AsFile(fin) && PyFile_AsFile(fout)
Martin v. Löwis566f6af2002-10-26 14:39:10 +00001737 && isatty(fileno(PyFile_AsFile(fin)))
1738 && isatty(fileno(PyFile_AsFile(fout)))) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001739 PyObject *po;
Guido van Rossum872537c1995-07-07 22:43:42 +00001740 char *prompt;
1741 char *s;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001742 PyObject *result;
Guido van Rossum872537c1995-07-07 22:43:42 +00001743 if (v != NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001744 po = PyObject_Str(v);
Guido van Rossum872537c1995-07-07 22:43:42 +00001745 if (po == NULL)
1746 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001747 prompt = PyString_AsString(po);
Guido van Rossumd9b52081998-06-26 18:25:38 +00001748 if (prompt == NULL)
1749 return NULL;
Guido van Rossum872537c1995-07-07 22:43:42 +00001750 }
1751 else {
1752 po = NULL;
1753 prompt = "";
1754 }
Michael W. Hudson52db5192004-08-02 13:21:09 +00001755 s = PyOS_Readline(PyFile_AsFile(fin), PyFile_AsFile(fout),
Martin v. Löwis566f6af2002-10-26 14:39:10 +00001756 prompt);
Guido van Rossum79f25d91997-04-29 20:08:16 +00001757 Py_XDECREF(po);
Guido van Rossum872537c1995-07-07 22:43:42 +00001758 if (s == NULL) {
Michael W. Hudson30ea2f22004-07-07 17:44:12 +00001759 if (!PyErr_Occurred())
1760 PyErr_SetNone(PyExc_KeyboardInterrupt);
Guido van Rossum872537c1995-07-07 22:43:42 +00001761 return NULL;
1762 }
1763 if (*s == '\0') {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001764 PyErr_SetNone(PyExc_EOFError);
Guido van Rossum872537c1995-07-07 22:43:42 +00001765 result = NULL;
1766 }
1767 else { /* strip trailing '\n' */
Guido van Rossum106f2da2000-06-28 21:12:25 +00001768 size_t len = strlen(s);
Martin v. Löwisb1ed7fa2006-04-13 07:52:27 +00001769 if (len > PY_SSIZE_T_MAX) {
Martin v. Löwis31d2df52002-08-14 15:46:02 +00001770 PyErr_SetString(PyExc_OverflowError,
Alex Martellia9b9c9f2003-04-23 13:34:35 +00001771 "[raw_]input: input too long");
Guido van Rossum106f2da2000-06-28 21:12:25 +00001772 result = NULL;
1773 }
1774 else {
Martin v. Löwisb1ed7fa2006-04-13 07:52:27 +00001775 result = PyString_FromStringAndSize(s, len-1);
Guido van Rossum106f2da2000-06-28 21:12:25 +00001776 }
Guido van Rossum872537c1995-07-07 22:43:42 +00001777 }
Guido van Rossumb18618d2000-05-03 23:44:39 +00001778 PyMem_FREE(s);
Guido van Rossum872537c1995-07-07 22:43:42 +00001779 return result;
1780 }
Guido van Rossum90933611991-06-07 16:10:43 +00001781 if (v != NULL) {
Martin v. Löwis31d2df52002-08-14 15:46:02 +00001782 if (PyFile_WriteObject(v, fout, Py_PRINT_RAW) != 0)
Guido van Rossum90933611991-06-07 16:10:43 +00001783 return NULL;
1784 }
Martin v. Löwis31d2df52002-08-14 15:46:02 +00001785 return PyFile_GetLine(fin, -1);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001786}
1787
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001788PyDoc_STRVAR(raw_input_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001789"raw_input([prompt]) -> string\n\
1790\n\
1791Read a string from standard input. The trailing newline is stripped.\n\
1792If the user hits EOF (Unix: Ctl-D, Windows: Ctl-Z+Return), raise EOFError.\n\
1793On Unix, GNU readline is used if enabled. The prompt string, if given,\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001794is printed without a trailing newline before reading.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001795
1796
Guido van Rossum79f25d91997-04-29 20:08:16 +00001797static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001798builtin_reduce(PyObject *self, PyObject *args)
Guido van Rossum12d12c51993-10-26 17:58:25 +00001799{
Tim Peters15d81ef2001-05-04 04:39:21 +00001800 PyObject *seq, *func, *result = NULL, *it;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001801
Neal Norwitzdf25efe2007-05-23 06:58:36 +00001802 if (Py_Py3kWarningFlag &&
1803 PyErr_Warn(PyExc_DeprecationWarning,
1804 "reduce() not supported in 3.x") < 0)
1805 return NULL;
1806
Raymond Hettingerea3fdf42002-12-29 16:33:45 +00001807 if (!PyArg_UnpackTuple(args, "reduce", 2, 3, &func, &seq, &result))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001808 return NULL;
1809 if (result != NULL)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001810 Py_INCREF(result);
Guido van Rossum12d12c51993-10-26 17:58:25 +00001811
Tim Peters15d81ef2001-05-04 04:39:21 +00001812 it = PyObject_GetIter(seq);
1813 if (it == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001814 PyErr_SetString(PyExc_TypeError,
Tim Peters15d81ef2001-05-04 04:39:21 +00001815 "reduce() arg 2 must support iteration");
1816 Py_XDECREF(result);
Guido van Rossum12d12c51993-10-26 17:58:25 +00001817 return NULL;
1818 }
1819
Guido van Rossum79f25d91997-04-29 20:08:16 +00001820 if ((args = PyTuple_New(2)) == NULL)
Guido van Rossum2d951851994-08-29 12:52:16 +00001821 goto Fail;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001822
Tim Peters15d81ef2001-05-04 04:39:21 +00001823 for (;;) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001824 PyObject *op2;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001825
1826 if (args->ob_refcnt > 1) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001827 Py_DECREF(args);
1828 if ((args = PyTuple_New(2)) == NULL)
Guido van Rossum2d951851994-08-29 12:52:16 +00001829 goto Fail;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001830 }
1831
Tim Peters15d81ef2001-05-04 04:39:21 +00001832 op2 = PyIter_Next(it);
1833 if (op2 == NULL) {
Tim Petersf4848da2001-05-05 00:14:56 +00001834 if (PyErr_Occurred())
1835 goto Fail;
1836 break;
Guido van Rossum2d951851994-08-29 12:52:16 +00001837 }
Guido van Rossum12d12c51993-10-26 17:58:25 +00001838
Guido van Rossum2d951851994-08-29 12:52:16 +00001839 if (result == NULL)
1840 result = op2;
1841 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001842 PyTuple_SetItem(args, 0, result);
1843 PyTuple_SetItem(args, 1, op2);
1844 if ((result = PyEval_CallObject(func, args)) == NULL)
Guido van Rossum2d951851994-08-29 12:52:16 +00001845 goto Fail;
1846 }
Guido van Rossum12d12c51993-10-26 17:58:25 +00001847 }
1848
Guido van Rossum79f25d91997-04-29 20:08:16 +00001849 Py_DECREF(args);
Guido van Rossum12d12c51993-10-26 17:58:25 +00001850
Guido van Rossum2d951851994-08-29 12:52:16 +00001851 if (result == NULL)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001852 PyErr_SetString(PyExc_TypeError,
Fred Drake661ea262000-10-24 19:57:45 +00001853 "reduce() of empty sequence with no initial value");
Guido van Rossum2d951851994-08-29 12:52:16 +00001854
Tim Peters15d81ef2001-05-04 04:39:21 +00001855 Py_DECREF(it);
Guido van Rossum12d12c51993-10-26 17:58:25 +00001856 return result;
1857
Guido van Rossum2d951851994-08-29 12:52:16 +00001858Fail:
Guido van Rossum79f25d91997-04-29 20:08:16 +00001859 Py_XDECREF(args);
1860 Py_XDECREF(result);
Tim Peters15d81ef2001-05-04 04:39:21 +00001861 Py_DECREF(it);
Guido van Rossum12d12c51993-10-26 17:58:25 +00001862 return NULL;
1863}
1864
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001865PyDoc_STRVAR(reduce_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001866"reduce(function, sequence[, initial]) -> value\n\
1867\n\
1868Apply a function of two arguments cumulatively to the items of a sequence,\n\
1869from left to right, so as to reduce the sequence to a single value.\n\
1870For example, reduce(lambda x, y: x+y, [1, 2, 3, 4, 5]) calculates\n\
1871((((1+2)+3)+4)+5). If initial is present, it is placed before the items\n\
1872of the sequence in the calculation, and serves as a default when the\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001873sequence is empty.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001874
1875
Guido van Rossum79f25d91997-04-29 20:08:16 +00001876static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001877builtin_reload(PyObject *self, PyObject *v)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001878{
Neal Norwitzdf25efe2007-05-23 06:58:36 +00001879 if (Py_Py3kWarningFlag &&
1880 PyErr_Warn(PyExc_DeprecationWarning,
1881 "reload() not supported in 3.x") < 0)
1882 return NULL;
1883
Guido van Rossum79f25d91997-04-29 20:08:16 +00001884 return PyImport_ReloadModule(v);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001885}
1886
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001887PyDoc_STRVAR(reload_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001888"reload(module) -> module\n\
1889\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001890Reload the module. The module must have been successfully imported before.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001891
1892
Guido van Rossum79f25d91997-04-29 20:08:16 +00001893static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001894builtin_repr(PyObject *self, PyObject *v)
Guido van Rossumc89705d1992-11-26 08:54:07 +00001895{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001896 return PyObject_Repr(v);
Guido van Rossumc89705d1992-11-26 08:54:07 +00001897}
1898
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001899PyDoc_STRVAR(repr_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001900"repr(object) -> string\n\
1901\n\
1902Return the canonical string representation of the object.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001903For most object types, eval(repr(object)) == object.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001904
1905
Guido van Rossum79f25d91997-04-29 20:08:16 +00001906static PyObject *
Georg Brandlccadf842006-03-31 18:54:53 +00001907builtin_round(PyObject *self, PyObject *args, PyObject *kwds)
Guido van Rossum9e51f9b1993-02-12 16:29:05 +00001908{
Georg Brandlccadf842006-03-31 18:54:53 +00001909 double number;
Guido van Rossum9e51f9b1993-02-12 16:29:05 +00001910 double f;
1911 int ndigits = 0;
Guido van Rossum9e51f9b1993-02-12 16:29:05 +00001912 int i;
Georg Brandlccadf842006-03-31 18:54:53 +00001913 static char *kwlist[] = {"number", "ndigits", 0};
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001914
Georg Brandlccadf842006-03-31 18:54:53 +00001915 if (!PyArg_ParseTupleAndKeywords(args, kwds, "d|i:round",
1916 kwlist, &number, &ndigits))
1917 return NULL;
Guido van Rossum9e51f9b1993-02-12 16:29:05 +00001918 f = 1.0;
Guido van Rossum1e162d31998-05-09 14:42:25 +00001919 i = abs(ndigits);
1920 while (--i >= 0)
Guido van Rossum9e51f9b1993-02-12 16:29:05 +00001921 f = f*10.0;
Guido van Rossum1e162d31998-05-09 14:42:25 +00001922 if (ndigits < 0)
Georg Brandlccadf842006-03-31 18:54:53 +00001923 number /= f;
Guido van Rossum9e51f9b1993-02-12 16:29:05 +00001924 else
Georg Brandlccadf842006-03-31 18:54:53 +00001925 number *= f;
1926 if (number >= 0.0)
1927 number = floor(number + 0.5);
Guido van Rossum1e162d31998-05-09 14:42:25 +00001928 else
Georg Brandlccadf842006-03-31 18:54:53 +00001929 number = ceil(number - 0.5);
Guido van Rossum1e162d31998-05-09 14:42:25 +00001930 if (ndigits < 0)
Georg Brandlccadf842006-03-31 18:54:53 +00001931 number *= f;
Guido van Rossum1e162d31998-05-09 14:42:25 +00001932 else
Georg Brandlccadf842006-03-31 18:54:53 +00001933 number /= f;
1934 return PyFloat_FromDouble(number);
Guido van Rossum9e51f9b1993-02-12 16:29:05 +00001935}
1936
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001937PyDoc_STRVAR(round_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001938"round(number[, ndigits]) -> floating point number\n\
1939\n\
1940Round a number to a given precision in decimal digits (default 0 digits).\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001941This always returns a floating point number. Precision may be negative.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001942
Raymond Hettinger64958a12003-12-17 20:43:33 +00001943static PyObject *
1944builtin_sorted(PyObject *self, PyObject *args, PyObject *kwds)
1945{
1946 PyObject *newlist, *v, *seq, *compare=NULL, *keyfunc=NULL, *newargs;
1947 PyObject *callable;
Martin v. Löwis15e62742006-02-27 16:46:16 +00001948 static char *kwlist[] = {"iterable", "cmp", "key", "reverse", 0};
Neal Norwitz6f0d4792005-12-15 06:40:36 +00001949 int reverse;
Raymond Hettinger64958a12003-12-17 20:43:33 +00001950
Neal Norwitz6f0d4792005-12-15 06:40:36 +00001951 /* args 1-4 should match listsort in Objects/listobject.c */
Armin Rigo71d7e702005-09-20 18:13:03 +00001952 if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|OOi:sorted",
1953 kwlist, &seq, &compare, &keyfunc, &reverse))
1954 return NULL;
Raymond Hettinger64958a12003-12-17 20:43:33 +00001955
1956 newlist = PySequence_List(seq);
1957 if (newlist == NULL)
1958 return NULL;
1959
1960 callable = PyObject_GetAttrString(newlist, "sort");
1961 if (callable == NULL) {
1962 Py_DECREF(newlist);
1963 return NULL;
1964 }
Georg Brandl99d7e4e2005-08-31 22:21:15 +00001965
Raymond Hettinger64958a12003-12-17 20:43:33 +00001966 newargs = PyTuple_GetSlice(args, 1, 4);
1967 if (newargs == NULL) {
1968 Py_DECREF(newlist);
1969 Py_DECREF(callable);
1970 return NULL;
1971 }
1972
1973 v = PyObject_Call(callable, newargs, kwds);
1974 Py_DECREF(newargs);
1975 Py_DECREF(callable);
1976 if (v == NULL) {
1977 Py_DECREF(newlist);
1978 return NULL;
1979 }
1980 Py_DECREF(v);
1981 return newlist;
1982}
1983
1984PyDoc_STRVAR(sorted_doc,
1985"sorted(iterable, cmp=None, key=None, reverse=False) --> new sorted list");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001986
Guido van Rossum79f25d91997-04-29 20:08:16 +00001987static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001988builtin_vars(PyObject *self, PyObject *args)
Guido van Rossum2d951851994-08-29 12:52:16 +00001989{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001990 PyObject *v = NULL;
1991 PyObject *d;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001992
Raymond Hettingerea3fdf42002-12-29 16:33:45 +00001993 if (!PyArg_UnpackTuple(args, "vars", 0, 1, &v))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001994 return NULL;
Guido van Rossum2d951851994-08-29 12:52:16 +00001995 if (v == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001996 d = PyEval_GetLocals();
Guido van Rossum53bb7ff1995-07-26 16:26:31 +00001997 if (d == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001998 if (!PyErr_Occurred())
1999 PyErr_SetString(PyExc_SystemError,
Alex Martellia9b9c9f2003-04-23 13:34:35 +00002000 "vars(): no locals!?");
Guido van Rossum53bb7ff1995-07-26 16:26:31 +00002001 }
2002 else
Guido van Rossum79f25d91997-04-29 20:08:16 +00002003 Py_INCREF(d);
Guido van Rossum2d951851994-08-29 12:52:16 +00002004 }
2005 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002006 d = PyObject_GetAttrString(v, "__dict__");
Guido van Rossum2d951851994-08-29 12:52:16 +00002007 if (d == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002008 PyErr_SetString(PyExc_TypeError,
Guido van Rossum2d951851994-08-29 12:52:16 +00002009 "vars() argument must have __dict__ attribute");
2010 return NULL;
2011 }
2012 }
2013 return d;
2014}
2015
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002016PyDoc_STRVAR(vars_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002017"vars([object]) -> dictionary\n\
2018\n\
2019Without arguments, equivalent to locals().\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002020With an argument, equivalent to object.__dict__.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002021
Alex Martellia70b1912003-04-22 08:12:33 +00002022
2023static PyObject*
2024builtin_sum(PyObject *self, PyObject *args)
2025{
2026 PyObject *seq;
2027 PyObject *result = NULL;
2028 PyObject *temp, *item, *iter;
2029
Raymond Hettinger5cf63942003-10-25 06:41:37 +00002030 if (!PyArg_UnpackTuple(args, "sum", 1, 2, &seq, &result))
Alex Martellia70b1912003-04-22 08:12:33 +00002031 return NULL;
2032
2033 iter = PyObject_GetIter(seq);
2034 if (iter == NULL)
2035 return NULL;
2036
2037 if (result == NULL) {
2038 result = PyInt_FromLong(0);
2039 if (result == NULL) {
2040 Py_DECREF(iter);
2041 return NULL;
2042 }
2043 } else {
2044 /* reject string values for 'start' parameter */
2045 if (PyObject_TypeCheck(result, &PyBaseString_Type)) {
2046 PyErr_SetString(PyExc_TypeError,
Alex Martellia9b9c9f2003-04-23 13:34:35 +00002047 "sum() can't sum strings [use ''.join(seq) instead]");
Alex Martellia70b1912003-04-22 08:12:33 +00002048 Py_DECREF(iter);
2049 return NULL;
2050 }
Alex Martelli41c9f882003-04-22 09:24:48 +00002051 Py_INCREF(result);
Alex Martellia70b1912003-04-22 08:12:33 +00002052 }
2053
Raymond Hettinger3f8caa32007-10-24 01:28:33 +00002054#ifndef SLOW_SUM
2055 /* Fast addition by keeping temporary sums in C instead of new Python objects.
2056 Assumes all inputs are the same type. If the assumption fails, default
2057 to the more general routine.
2058 */
2059 if (PyInt_CheckExact(result)) {
2060 long i_result = PyInt_AS_LONG(result);
2061 Py_DECREF(result);
2062 result = NULL;
2063 while(result == NULL) {
2064 item = PyIter_Next(iter);
2065 if (item == NULL) {
2066 Py_DECREF(iter);
2067 if (PyErr_Occurred())
2068 return NULL;
2069 return PyInt_FromLong(i_result);
2070 }
2071 if (PyInt_CheckExact(item)) {
2072 long b = PyInt_AS_LONG(item);
2073 long x = i_result + b;
2074 if ((x^i_result) >= 0 || (x^b) >= 0) {
2075 i_result = x;
2076 Py_DECREF(item);
2077 continue;
2078 }
2079 }
2080 /* Either overflowed or is not an int. Restore real objects and process normally */
2081 result = PyInt_FromLong(i_result);
2082 temp = PyNumber_Add(result, item);
2083 Py_DECREF(result);
2084 Py_DECREF(item);
2085 result = temp;
2086 if (result == NULL) {
2087 Py_DECREF(iter);
2088 return NULL;
2089 }
2090 }
2091 }
2092
2093 if (PyFloat_CheckExact(result)) {
2094 double f_result = PyFloat_AS_DOUBLE(result);
2095 Py_DECREF(result);
2096 result = NULL;
2097 while(result == NULL) {
2098 item = PyIter_Next(iter);
2099 if (item == NULL) {
2100 Py_DECREF(iter);
2101 if (PyErr_Occurred())
2102 return NULL;
2103 return PyFloat_FromDouble(f_result);
2104 }
2105 if (PyFloat_CheckExact(item)) {
2106 PyFPE_START_PROTECT("add", return 0)
2107 f_result += PyFloat_AS_DOUBLE(item);
Raymond Hettinger3a8daf52007-10-24 02:05:51 +00002108 PyFPE_END_PROTECT(f_result)
Raymond Hettingera45c4872007-10-25 02:26:58 +00002109 Py_DECREF(item);
Raymond Hettinger3a8daf52007-10-24 02:05:51 +00002110 continue;
2111 }
2112 if (PyInt_CheckExact(item)) {
2113 PyFPE_START_PROTECT("add", return 0)
2114 f_result += (double)PyInt_AS_LONG(item);
2115 PyFPE_END_PROTECT(f_result)
Raymond Hettingera45c4872007-10-25 02:26:58 +00002116 Py_DECREF(item);
Raymond Hettinger3f8caa32007-10-24 01:28:33 +00002117 continue;
2118 }
2119 result = PyFloat_FromDouble(f_result);
2120 temp = PyNumber_Add(result, item);
2121 Py_DECREF(result);
2122 Py_DECREF(item);
2123 result = temp;
2124 if (result == NULL) {
2125 Py_DECREF(iter);
2126 return NULL;
2127 }
2128 }
2129 }
2130#endif
2131
Alex Martellia70b1912003-04-22 08:12:33 +00002132 for(;;) {
2133 item = PyIter_Next(iter);
2134 if (item == NULL) {
2135 /* error, or end-of-sequence */
2136 if (PyErr_Occurred()) {
2137 Py_DECREF(result);
2138 result = NULL;
2139 }
2140 break;
2141 }
Alex Martellia253e182003-10-25 23:24:14 +00002142 temp = PyNumber_Add(result, item);
Alex Martellia70b1912003-04-22 08:12:33 +00002143 Py_DECREF(result);
2144 Py_DECREF(item);
2145 result = temp;
2146 if (result == NULL)
2147 break;
2148 }
2149 Py_DECREF(iter);
2150 return result;
2151}
2152
2153PyDoc_STRVAR(sum_doc,
Georg Brandl8134d062006-10-12 12:33:07 +00002154"sum(sequence[, start]) -> value\n\
Alex Martellia70b1912003-04-22 08:12:33 +00002155\n\
2156Returns the sum of a sequence of numbers (NOT strings) plus the value\n\
Georg Brandl8134d062006-10-12 12:33:07 +00002157of parameter 'start' (which defaults to 0). When the sequence is\n\
2158empty, returns start.");
Alex Martellia70b1912003-04-22 08:12:33 +00002159
2160
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002161static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002162builtin_isinstance(PyObject *self, PyObject *args)
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002163{
2164 PyObject *inst;
2165 PyObject *cls;
Guido van Rossum823649d2001-03-21 18:40:58 +00002166 int retval;
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002167
Raymond Hettingerea3fdf42002-12-29 16:33:45 +00002168 if (!PyArg_UnpackTuple(args, "isinstance", 2, 2, &inst, &cls))
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002169 return NULL;
Guido van Rossumf5dd9141997-12-02 19:11:45 +00002170
Guido van Rossum823649d2001-03-21 18:40:58 +00002171 retval = PyObject_IsInstance(inst, cls);
2172 if (retval < 0)
Guido van Rossumad991772001-01-12 16:03:05 +00002173 return NULL;
Guido van Rossum77f6a652002-04-03 22:41:51 +00002174 return PyBool_FromLong(retval);
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002175}
2176
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002177PyDoc_STRVAR(isinstance_doc,
Guido van Rossum77f6a652002-04-03 22:41:51 +00002178"isinstance(object, class-or-type-or-tuple) -> bool\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002179\n\
2180Return whether an object is an instance of a class or of a subclass thereof.\n\
Guido van Rossum03290ec2001-10-07 20:54:12 +00002181With a type as second argument, return whether that is the object's type.\n\
2182The form using a tuple, isinstance(x, (A, B, ...)), is a shortcut for\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002183isinstance(x, A) or isinstance(x, B) or ... (etc.).");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002184
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002185
2186static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002187builtin_issubclass(PyObject *self, PyObject *args)
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002188{
2189 PyObject *derived;
2190 PyObject *cls;
2191 int retval;
2192
Raymond Hettingerea3fdf42002-12-29 16:33:45 +00002193 if (!PyArg_UnpackTuple(args, "issubclass", 2, 2, &derived, &cls))
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002194 return NULL;
Guido van Rossum668213d1999-06-16 17:28:37 +00002195
Guido van Rossum823649d2001-03-21 18:40:58 +00002196 retval = PyObject_IsSubclass(derived, cls);
2197 if (retval < 0)
2198 return NULL;
Guido van Rossum77f6a652002-04-03 22:41:51 +00002199 return PyBool_FromLong(retval);
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002200}
2201
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002202PyDoc_STRVAR(issubclass_doc,
Guido van Rossum77f6a652002-04-03 22:41:51 +00002203"issubclass(C, B) -> bool\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002204\n\
Walter Dörwaldd9a6ad32002-12-12 16:41:44 +00002205Return whether class C is a subclass (i.e., a derived class) of class B.\n\
2206When using a tuple as the second argument issubclass(X, (A, B, ...)),\n\
2207is a shortcut for issubclass(X, A) or issubclass(X, B) or ... (etc.).");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002208
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002209
Barry Warsawbd599b52000-08-03 15:45:29 +00002210static PyObject*
2211builtin_zip(PyObject *self, PyObject *args)
2212{
2213 PyObject *ret;
Martin v. Löwisd96ee902006-02-16 14:37:16 +00002214 const Py_ssize_t itemsize = PySequence_Length(args);
2215 Py_ssize_t i;
Tim Peters8572b4f2001-05-06 01:05:02 +00002216 PyObject *itlist; /* tuple of iterators */
Martin v. Löwisd96ee902006-02-16 14:37:16 +00002217 Py_ssize_t len; /* guess at result length */
Barry Warsawbd599b52000-08-03 15:45:29 +00002218
Raymond Hettingereaef6152003-08-02 07:42:57 +00002219 if (itemsize == 0)
2220 return PyList_New(0);
2221
Barry Warsawbd599b52000-08-03 15:45:29 +00002222 /* args must be a tuple */
2223 assert(PyTuple_Check(args));
2224
Tim Peters39a86c22002-05-12 07:19:38 +00002225 /* Guess at result length: the shortest of the input lengths.
2226 If some argument refuses to say, we refuse to guess too, lest
2227 an argument like xrange(sys.maxint) lead us astray.*/
Tim Peters67d687a2002-04-29 21:27:32 +00002228 len = -1; /* unknown */
2229 for (i = 0; i < itemsize; ++i) {
2230 PyObject *item = PyTuple_GET_ITEM(args, i);
Raymond Hettinger4e2f7142007-12-06 00:56:53 +00002231 Py_ssize_t thislen = _PyObject_LengthHint(item, -1);
Tim Peters39a86c22002-05-12 07:19:38 +00002232 if (thislen < 0) {
Tim Peters39a86c22002-05-12 07:19:38 +00002233 len = -1;
2234 break;
2235 }
Tim Peters67d687a2002-04-29 21:27:32 +00002236 else if (len < 0 || thislen < len)
2237 len = thislen;
2238 }
2239
Tim Peters8572b4f2001-05-06 01:05:02 +00002240 /* allocate result list */
Tim Peters67d687a2002-04-29 21:27:32 +00002241 if (len < 0)
2242 len = 10; /* arbitrary */
2243 if ((ret = PyList_New(len)) == NULL)
Barry Warsawbd599b52000-08-03 15:45:29 +00002244 return NULL;
2245
Tim Peters8572b4f2001-05-06 01:05:02 +00002246 /* obtain iterators */
2247 itlist = PyTuple_New(itemsize);
2248 if (itlist == NULL)
2249 goto Fail_ret;
2250 for (i = 0; i < itemsize; ++i) {
2251 PyObject *item = PyTuple_GET_ITEM(args, i);
2252 PyObject *it = PyObject_GetIter(item);
2253 if (it == NULL) {
2254 if (PyErr_ExceptionMatches(PyExc_TypeError))
2255 PyErr_Format(PyExc_TypeError,
Neal Norwitza361bd82006-02-19 19:31:50 +00002256 "zip argument #%zd must support iteration",
Tim Peters8572b4f2001-05-06 01:05:02 +00002257 i+1);
2258 goto Fail_ret_itlist;
Barry Warsawbd599b52000-08-03 15:45:29 +00002259 }
Tim Peters8572b4f2001-05-06 01:05:02 +00002260 PyTuple_SET_ITEM(itlist, i, it);
2261 }
Barry Warsawbd599b52000-08-03 15:45:29 +00002262
Tim Peters8572b4f2001-05-06 01:05:02 +00002263 /* build result into ret list */
Tim Peters67d687a2002-04-29 21:27:32 +00002264 for (i = 0; ; ++i) {
2265 int j;
Tim Peters8572b4f2001-05-06 01:05:02 +00002266 PyObject *next = PyTuple_New(itemsize);
2267 if (!next)
2268 goto Fail_ret_itlist;
2269
Tim Peters67d687a2002-04-29 21:27:32 +00002270 for (j = 0; j < itemsize; j++) {
2271 PyObject *it = PyTuple_GET_ITEM(itlist, j);
Tim Peters8572b4f2001-05-06 01:05:02 +00002272 PyObject *item = PyIter_Next(it);
Barry Warsawbd599b52000-08-03 15:45:29 +00002273 if (!item) {
Tim Peters8572b4f2001-05-06 01:05:02 +00002274 if (PyErr_Occurred()) {
2275 Py_DECREF(ret);
2276 ret = NULL;
Barry Warsawbd599b52000-08-03 15:45:29 +00002277 }
2278 Py_DECREF(next);
Tim Peters8572b4f2001-05-06 01:05:02 +00002279 Py_DECREF(itlist);
Tim Peters67d687a2002-04-29 21:27:32 +00002280 goto Done;
Barry Warsawbd599b52000-08-03 15:45:29 +00002281 }
Tim Peters67d687a2002-04-29 21:27:32 +00002282 PyTuple_SET_ITEM(next, j, item);
Barry Warsawbd599b52000-08-03 15:45:29 +00002283 }
Tim Peters8572b4f2001-05-06 01:05:02 +00002284
Tim Peters67d687a2002-04-29 21:27:32 +00002285 if (i < len)
2286 PyList_SET_ITEM(ret, i, next);
2287 else {
2288 int status = PyList_Append(ret, next);
2289 Py_DECREF(next);
2290 ++len;
2291 if (status < 0)
2292 goto Fail_ret_itlist;
2293 }
Barry Warsawbd599b52000-08-03 15:45:29 +00002294 }
Tim Peters8572b4f2001-05-06 01:05:02 +00002295
Tim Peters67d687a2002-04-29 21:27:32 +00002296Done:
2297 if (ret != NULL && i < len) {
2298 /* The list is too big. */
2299 if (PyList_SetSlice(ret, i, len, NULL) < 0)
2300 return NULL;
2301 }
2302 return ret;
2303
Tim Peters8572b4f2001-05-06 01:05:02 +00002304Fail_ret_itlist:
2305 Py_DECREF(itlist);
2306Fail_ret:
2307 Py_DECREF(ret);
2308 return NULL;
Barry Warsawbd599b52000-08-03 15:45:29 +00002309}
2310
2311
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002312PyDoc_STRVAR(zip_doc,
Barry Warsawbd599b52000-08-03 15:45:29 +00002313"zip(seq1 [, seq2 [...]]) -> [(seq1[0], seq2[0] ...), (...)]\n\
2314\n\
2315Return a list of tuples, where each tuple contains the i-th element\n\
2316from each of the argument sequences. The returned list is truncated\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002317in length to the length of the shortest argument sequence.");
Barry Warsawbd599b52000-08-03 15:45:29 +00002318
2319
Guido van Rossum79f25d91997-04-29 20:08:16 +00002320static PyMethodDef builtin_methods[] = {
Neal Norwitz92e212f2006-04-03 04:48:37 +00002321 {"__import__", (PyCFunction)builtin___import__, METH_VARARGS | METH_KEYWORDS, import_doc},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002322 {"abs", builtin_abs, METH_O, abs_doc},
Raymond Hettinger96229b12005-03-11 06:49:40 +00002323 {"all", builtin_all, METH_O, all_doc},
2324 {"any", builtin_any, METH_O, any_doc},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002325 {"apply", builtin_apply, METH_VARARGS, apply_doc},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002326 {"callable", builtin_callable, METH_O, callable_doc},
2327 {"chr", builtin_chr, METH_VARARGS, chr_doc},
2328 {"cmp", builtin_cmp, METH_VARARGS, cmp_doc},
2329 {"coerce", builtin_coerce, METH_VARARGS, coerce_doc},
Georg Brandl5240d742007-03-13 20:46:32 +00002330 {"compile", (PyCFunction)builtin_compile, METH_VARARGS | METH_KEYWORDS, compile_doc},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002331 {"delattr", builtin_delattr, METH_VARARGS, delattr_doc},
2332 {"dir", builtin_dir, METH_VARARGS, dir_doc},
2333 {"divmod", builtin_divmod, METH_VARARGS, divmod_doc},
2334 {"eval", builtin_eval, METH_VARARGS, eval_doc},
2335 {"execfile", builtin_execfile, METH_VARARGS, execfile_doc},
2336 {"filter", builtin_filter, METH_VARARGS, filter_doc},
2337 {"getattr", builtin_getattr, METH_VARARGS, getattr_doc},
2338 {"globals", (PyCFunction)builtin_globals, METH_NOARGS, globals_doc},
2339 {"hasattr", builtin_hasattr, METH_VARARGS, hasattr_doc},
2340 {"hash", builtin_hash, METH_O, hash_doc},
2341 {"hex", builtin_hex, METH_O, hex_doc},
2342 {"id", builtin_id, METH_O, id_doc},
2343 {"input", builtin_input, METH_VARARGS, input_doc},
2344 {"intern", builtin_intern, METH_VARARGS, intern_doc},
2345 {"isinstance", builtin_isinstance, METH_VARARGS, isinstance_doc},
2346 {"issubclass", builtin_issubclass, METH_VARARGS, issubclass_doc},
2347 {"iter", builtin_iter, METH_VARARGS, iter_doc},
2348 {"len", builtin_len, METH_O, len_doc},
2349 {"locals", (PyCFunction)builtin_locals, METH_NOARGS, locals_doc},
2350 {"map", builtin_map, METH_VARARGS, map_doc},
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00002351 {"max", (PyCFunction)builtin_max, METH_VARARGS | METH_KEYWORDS, max_doc},
2352 {"min", (PyCFunction)builtin_min, METH_VARARGS | METH_KEYWORDS, min_doc},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002353 {"oct", builtin_oct, METH_O, oct_doc},
Neal Norwitzc4edb0e2006-05-02 04:43:14 +00002354 {"open", (PyCFunction)builtin_open, METH_VARARGS | METH_KEYWORDS, open_doc},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002355 {"ord", builtin_ord, METH_O, ord_doc},
2356 {"pow", builtin_pow, METH_VARARGS, pow_doc},
2357 {"range", builtin_range, METH_VARARGS, range_doc},
2358 {"raw_input", builtin_raw_input, METH_VARARGS, raw_input_doc},
2359 {"reduce", builtin_reduce, METH_VARARGS, reduce_doc},
2360 {"reload", builtin_reload, METH_O, reload_doc},
2361 {"repr", builtin_repr, METH_O, repr_doc},
Georg Brandlccadf842006-03-31 18:54:53 +00002362 {"round", (PyCFunction)builtin_round, METH_VARARGS | METH_KEYWORDS, round_doc},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002363 {"setattr", builtin_setattr, METH_VARARGS, setattr_doc},
Raymond Hettinger64958a12003-12-17 20:43:33 +00002364 {"sorted", (PyCFunction)builtin_sorted, METH_VARARGS | METH_KEYWORDS, sorted_doc},
Alex Martellia70b1912003-04-22 08:12:33 +00002365 {"sum", builtin_sum, METH_VARARGS, sum_doc},
Martin v. Löwis339d0f72001-08-17 18:39:25 +00002366#ifdef Py_USING_UNICODE
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002367 {"unichr", builtin_unichr, METH_VARARGS, unichr_doc},
Martin v. Löwis339d0f72001-08-17 18:39:25 +00002368#endif
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002369 {"vars", builtin_vars, METH_VARARGS, vars_doc},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002370 {"zip", builtin_zip, METH_VARARGS, zip_doc},
Guido van Rossumc02e15c1991-12-16 13:03:00 +00002371 {NULL, NULL},
Guido van Rossum3f5da241990-12-20 15:06:42 +00002372};
2373
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002374PyDoc_STRVAR(builtin_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002375"Built-in functions, exceptions, and other objects.\n\
2376\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002377Noteworthy: None is the `nil' object; Ellipsis represents `...' in slices.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002378
Guido van Rossum25ce5661997-08-02 03:10:38 +00002379PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002380_PyBuiltin_Init(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +00002381{
Fred Drake5550de32000-06-20 04:54:19 +00002382 PyObject *mod, *dict, *debug;
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002383 mod = Py_InitModule4("__builtin__", builtin_methods,
2384 builtin_doc, (PyObject *)NULL,
2385 PYTHON_API_VERSION);
Guido van Rossum25ce5661997-08-02 03:10:38 +00002386 if (mod == NULL)
2387 return NULL;
2388 dict = PyModule_GetDict(mod);
Tim Peters4b7625e2001-09-13 21:37:17 +00002389
Tim Peters7571a0f2003-03-23 17:52:28 +00002390#ifdef Py_TRACE_REFS
2391 /* __builtin__ exposes a number of statically allocated objects
2392 * that, before this code was added in 2.3, never showed up in
2393 * the list of "all objects" maintained by Py_TRACE_REFS. As a
2394 * result, programs leaking references to None and False (etc)
2395 * couldn't be diagnosed by examining sys.getobjects(0).
2396 */
2397#define ADD_TO_ALL(OBJECT) _Py_AddToAllObjects((PyObject *)(OBJECT), 0)
2398#else
2399#define ADD_TO_ALL(OBJECT) (void)0
2400#endif
2401
Tim Peters4b7625e2001-09-13 21:37:17 +00002402#define SETBUILTIN(NAME, OBJECT) \
Tim Peters7571a0f2003-03-23 17:52:28 +00002403 if (PyDict_SetItemString(dict, NAME, (PyObject *)OBJECT) < 0) \
2404 return NULL; \
2405 ADD_TO_ALL(OBJECT)
Tim Peters4b7625e2001-09-13 21:37:17 +00002406
2407 SETBUILTIN("None", Py_None);
2408 SETBUILTIN("Ellipsis", Py_Ellipsis);
2409 SETBUILTIN("NotImplemented", Py_NotImplemented);
Guido van Rossum77f6a652002-04-03 22:41:51 +00002410 SETBUILTIN("False", Py_False);
2411 SETBUILTIN("True", Py_True);
Neal Norwitz32a7e7f2002-05-31 19:58:02 +00002412 SETBUILTIN("basestring", &PyBaseString_Type);
Guido van Rossum77f6a652002-04-03 22:41:51 +00002413 SETBUILTIN("bool", &PyBool_Type);
Guido van Rossumbea18cc2002-06-14 20:41:17 +00002414 SETBUILTIN("buffer", &PyBuffer_Type);
Tim Peters4b7625e2001-09-13 21:37:17 +00002415 SETBUILTIN("classmethod", &PyClassMethod_Type);
Tim Peters6d6c1a32001-08-02 04:15:00 +00002416#ifndef WITHOUT_COMPLEX
Tim Peters4b7625e2001-09-13 21:37:17 +00002417 SETBUILTIN("complex", &PyComplex_Type);
Tim Peters6d6c1a32001-08-02 04:15:00 +00002418#endif
Tim Petersa427a2b2001-10-29 22:25:45 +00002419 SETBUILTIN("dict", &PyDict_Type);
Tim Peters67d687a2002-04-29 21:27:32 +00002420 SETBUILTIN("enumerate", &PyEnum_Type);
Neal Norwitzc4edb0e2006-05-02 04:43:14 +00002421 SETBUILTIN("file", &PyFile_Type);
Tim Peters4b7625e2001-09-13 21:37:17 +00002422 SETBUILTIN("float", &PyFloat_Type);
Raymond Hettingera690a992003-11-16 16:17:49 +00002423 SETBUILTIN("frozenset", &PyFrozenSet_Type);
Tim Peters4b7625e2001-09-13 21:37:17 +00002424 SETBUILTIN("property", &PyProperty_Type);
2425 SETBUILTIN("int", &PyInt_Type);
2426 SETBUILTIN("list", &PyList_Type);
2427 SETBUILTIN("long", &PyLong_Type);
2428 SETBUILTIN("object", &PyBaseObject_Type);
Raymond Hettinger85c20a42003-11-06 14:06:48 +00002429 SETBUILTIN("reversed", &PyReversed_Type);
Raymond Hettingera690a992003-11-16 16:17:49 +00002430 SETBUILTIN("set", &PySet_Type);
Guido van Rossumbea18cc2002-06-14 20:41:17 +00002431 SETBUILTIN("slice", &PySlice_Type);
Tim Peters4b7625e2001-09-13 21:37:17 +00002432 SETBUILTIN("staticmethod", &PyStaticMethod_Type);
2433 SETBUILTIN("str", &PyString_Type);
2434 SETBUILTIN("super", &PySuper_Type);
2435 SETBUILTIN("tuple", &PyTuple_Type);
2436 SETBUILTIN("type", &PyType_Type);
Raymond Hettingerc4c453f2002-06-05 23:12:45 +00002437 SETBUILTIN("xrange", &PyRange_Type);
Martin v. Löwis339d0f72001-08-17 18:39:25 +00002438#ifdef Py_USING_UNICODE
Tim Peters4b7625e2001-09-13 21:37:17 +00002439 SETBUILTIN("unicode", &PyUnicode_Type);
Martin v. Löwis339d0f72001-08-17 18:39:25 +00002440#endif
Guido van Rossum77f6a652002-04-03 22:41:51 +00002441 debug = PyBool_FromLong(Py_OptimizeFlag == 0);
Fred Drake5550de32000-06-20 04:54:19 +00002442 if (PyDict_SetItemString(dict, "__debug__", debug) < 0) {
2443 Py_XDECREF(debug);
Guido van Rossum25ce5661997-08-02 03:10:38 +00002444 return NULL;
Fred Drake5550de32000-06-20 04:54:19 +00002445 }
2446 Py_XDECREF(debug);
Barry Warsaw757af0e1997-08-29 22:13:51 +00002447
Guido van Rossum25ce5661997-08-02 03:10:38 +00002448 return mod;
Tim Peters7571a0f2003-03-23 17:52:28 +00002449#undef ADD_TO_ALL
Tim Peters4b7625e2001-09-13 21:37:17 +00002450#undef SETBUILTIN
Guido van Rossum3f5da241990-12-20 15:06:42 +00002451}
2452
Guido van Rossume77a7571993-11-03 15:01:26 +00002453/* Helper for filter(): filter a tuple through a function */
Guido van Rossum12d12c51993-10-26 17:58:25 +00002454
Guido van Rossum79f25d91997-04-29 20:08:16 +00002455static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002456filtertuple(PyObject *func, PyObject *tuple)
Guido van Rossum12d12c51993-10-26 17:58:25 +00002457{
Guido van Rossum79f25d91997-04-29 20:08:16 +00002458 PyObject *result;
Martin v. Löwis18e16552006-02-15 17:27:45 +00002459 Py_ssize_t i, j;
2460 Py_ssize_t len = PyTuple_Size(tuple);
Guido van Rossum12d12c51993-10-26 17:58:25 +00002461
Guido van Rossumb7b45621995-08-04 04:07:45 +00002462 if (len == 0) {
Walter Dörwaldc3da83f2003-02-04 20:24:45 +00002463 if (PyTuple_CheckExact(tuple))
2464 Py_INCREF(tuple);
2465 else
2466 tuple = PyTuple_New(0);
Guido van Rossumb7b45621995-08-04 04:07:45 +00002467 return tuple;
2468 }
2469
Guido van Rossum79f25d91997-04-29 20:08:16 +00002470 if ((result = PyTuple_New(len)) == NULL)
Guido van Rossum2586bf01993-11-01 16:21:44 +00002471 return NULL;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002472
Guido van Rossum12d12c51993-10-26 17:58:25 +00002473 for (i = j = 0; i < len; ++i) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002474 PyObject *item, *good;
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002475 int ok;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002476
Walter Dörwald8dd19322003-02-10 17:36:40 +00002477 if (tuple->ob_type->tp_as_sequence &&
2478 tuple->ob_type->tp_as_sequence->sq_item) {
2479 item = tuple->ob_type->tp_as_sequence->sq_item(tuple, i);
Walter Dörwaldc58a3a12003-08-18 18:28:45 +00002480 if (item == NULL)
2481 goto Fail_1;
Walter Dörwald8dd19322003-02-10 17:36:40 +00002482 } else {
Alex Martellia9b9c9f2003-04-23 13:34:35 +00002483 PyErr_SetString(PyExc_TypeError, "filter(): unsubscriptable tuple");
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002484 goto Fail_1;
Walter Dörwald8dd19322003-02-10 17:36:40 +00002485 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002486 if (func == Py_None) {
2487 Py_INCREF(item);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002488 good = item;
2489 }
2490 else {
Raymond Hettinger8ae46892003-10-12 19:09:37 +00002491 PyObject *arg = PyTuple_Pack(1, item);
Walter Dörwaldc58a3a12003-08-18 18:28:45 +00002492 if (arg == NULL) {
2493 Py_DECREF(item);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002494 goto Fail_1;
Walter Dörwaldc58a3a12003-08-18 18:28:45 +00002495 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002496 good = PyEval_CallObject(func, arg);
2497 Py_DECREF(arg);
Walter Dörwaldc58a3a12003-08-18 18:28:45 +00002498 if (good == NULL) {
2499 Py_DECREF(item);
Guido van Rossum12d12c51993-10-26 17:58:25 +00002500 goto Fail_1;
Walter Dörwaldc58a3a12003-08-18 18:28:45 +00002501 }
Guido van Rossum12d12c51993-10-26 17:58:25 +00002502 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002503 ok = PyObject_IsTrue(good);
2504 Py_DECREF(good);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002505 if (ok) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002506 if (PyTuple_SetItem(result, j++, item) < 0)
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002507 goto Fail_1;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002508 }
Walter Dörwaldc58a3a12003-08-18 18:28:45 +00002509 else
2510 Py_DECREF(item);
Guido van Rossum12d12c51993-10-26 17:58:25 +00002511 }
2512
Tim Peters4324aa32001-05-28 22:30:08 +00002513 if (_PyTuple_Resize(&result, j) < 0)
Guido van Rossum12d12c51993-10-26 17:58:25 +00002514 return NULL;
2515
Guido van Rossum12d12c51993-10-26 17:58:25 +00002516 return result;
2517
Guido van Rossum12d12c51993-10-26 17:58:25 +00002518Fail_1:
Guido van Rossum79f25d91997-04-29 20:08:16 +00002519 Py_DECREF(result);
Guido van Rossum12d12c51993-10-26 17:58:25 +00002520 return NULL;
2521}
2522
2523
Guido van Rossume77a7571993-11-03 15:01:26 +00002524/* Helper for filter(): filter a string through a function */
Guido van Rossum12d12c51993-10-26 17:58:25 +00002525
Guido van Rossum79f25d91997-04-29 20:08:16 +00002526static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002527filterstring(PyObject *func, PyObject *strobj)
Guido van Rossum12d12c51993-10-26 17:58:25 +00002528{
Guido van Rossum79f25d91997-04-29 20:08:16 +00002529 PyObject *result;
Martin v. Löwis18e16552006-02-15 17:27:45 +00002530 Py_ssize_t i, j;
2531 Py_ssize_t len = PyString_Size(strobj);
2532 Py_ssize_t outlen = len;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002533
Guido van Rossum79f25d91997-04-29 20:08:16 +00002534 if (func == Py_None) {
Walter Dörwald1918f772003-02-10 13:19:13 +00002535 /* If it's a real string we can return the original,
2536 * as no character is ever false and __getitem__
2537 * does return this character. If it's a subclass
2538 * we must go through the __getitem__ loop */
2539 if (PyString_CheckExact(strobj)) {
Walter Dörwaldc3da83f2003-02-04 20:24:45 +00002540 Py_INCREF(strobj);
Walter Dörwald1918f772003-02-10 13:19:13 +00002541 return strobj;
2542 }
Guido van Rossum12d12c51993-10-26 17:58:25 +00002543 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002544 if ((result = PyString_FromStringAndSize(NULL, len)) == NULL)
Guido van Rossum2586bf01993-11-01 16:21:44 +00002545 return NULL;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002546
Guido van Rossum12d12c51993-10-26 17:58:25 +00002547 for (i = j = 0; i < len; ++i) {
Walter Dörwald1918f772003-02-10 13:19:13 +00002548 PyObject *item;
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002549 int ok;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002550
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002551 item = (*strobj->ob_type->tp_as_sequence->sq_item)(strobj, i);
2552 if (item == NULL)
2553 goto Fail_1;
Walter Dörwald1918f772003-02-10 13:19:13 +00002554 if (func==Py_None) {
2555 ok = 1;
2556 } else {
2557 PyObject *arg, *good;
Raymond Hettinger8ae46892003-10-12 19:09:37 +00002558 arg = PyTuple_Pack(1, item);
Walter Dörwald1918f772003-02-10 13:19:13 +00002559 if (arg == NULL) {
2560 Py_DECREF(item);
2561 goto Fail_1;
2562 }
2563 good = PyEval_CallObject(func, arg);
2564 Py_DECREF(arg);
2565 if (good == NULL) {
2566 Py_DECREF(item);
2567 goto Fail_1;
2568 }
2569 ok = PyObject_IsTrue(good);
2570 Py_DECREF(good);
Tim Peters388ed082001-04-07 20:34:48 +00002571 }
Walter Dörwald903f1e02003-02-04 16:28:00 +00002572 if (ok) {
Martin v. Löwisd96ee902006-02-16 14:37:16 +00002573 Py_ssize_t reslen;
Walter Dörwald903f1e02003-02-04 16:28:00 +00002574 if (!PyString_Check(item)) {
2575 PyErr_SetString(PyExc_TypeError, "can't filter str to str:"
2576 " __getitem__ returned different type");
2577 Py_DECREF(item);
2578 goto Fail_1;
2579 }
2580 reslen = PyString_GET_SIZE(item);
2581 if (reslen == 1) {
2582 PyString_AS_STRING(result)[j++] =
2583 PyString_AS_STRING(item)[0];
2584 } else {
2585 /* do we need more space? */
Martin v. Löwisd96ee902006-02-16 14:37:16 +00002586 Py_ssize_t need = j + reslen + len-i-1;
Walter Dörwald903f1e02003-02-04 16:28:00 +00002587 if (need > outlen) {
2588 /* overallocate, to avoid reallocations */
2589 if (need<2*outlen)
2590 need = 2*outlen;
2591 if (_PyString_Resize(&result, need)) {
2592 Py_DECREF(item);
2593 return NULL;
2594 }
2595 outlen = need;
2596 }
2597 memcpy(
2598 PyString_AS_STRING(result) + j,
2599 PyString_AS_STRING(item),
2600 reslen
2601 );
2602 j += reslen;
2603 }
2604 }
Tim Peters388ed082001-04-07 20:34:48 +00002605 Py_DECREF(item);
Guido van Rossum12d12c51993-10-26 17:58:25 +00002606 }
2607
Walter Dörwald903f1e02003-02-04 16:28:00 +00002608 if (j < outlen)
Tim Peters5de98422002-04-27 18:44:32 +00002609 _PyString_Resize(&result, j);
Guido van Rossum12d12c51993-10-26 17:58:25 +00002610
Guido van Rossum12d12c51993-10-26 17:58:25 +00002611 return result;
2612
Guido van Rossum12d12c51993-10-26 17:58:25 +00002613Fail_1:
Guido van Rossum79f25d91997-04-29 20:08:16 +00002614 Py_DECREF(result);
Guido van Rossum12d12c51993-10-26 17:58:25 +00002615 return NULL;
2616}
Martin v. Löwis8afd7572003-01-25 22:46:11 +00002617
2618#ifdef Py_USING_UNICODE
2619/* Helper for filter(): filter a Unicode object through a function */
2620
2621static PyObject *
2622filterunicode(PyObject *func, PyObject *strobj)
2623{
2624 PyObject *result;
Martin v. Löwis725507b2006-03-07 12:08:51 +00002625 register Py_ssize_t i, j;
Martin v. Löwisd96ee902006-02-16 14:37:16 +00002626 Py_ssize_t len = PyUnicode_GetSize(strobj);
2627 Py_ssize_t outlen = len;
Martin v. Löwis8afd7572003-01-25 22:46:11 +00002628
2629 if (func == Py_None) {
Walter Dörwald1918f772003-02-10 13:19:13 +00002630 /* If it's a real string we can return the original,
2631 * as no character is ever false and __getitem__
2632 * does return this character. If it's a subclass
2633 * we must go through the __getitem__ loop */
2634 if (PyUnicode_CheckExact(strobj)) {
Walter Dörwaldc3da83f2003-02-04 20:24:45 +00002635 Py_INCREF(strobj);
Walter Dörwald1918f772003-02-10 13:19:13 +00002636 return strobj;
2637 }
Martin v. Löwis8afd7572003-01-25 22:46:11 +00002638 }
2639 if ((result = PyUnicode_FromUnicode(NULL, len)) == NULL)
2640 return NULL;
2641
2642 for (i = j = 0; i < len; ++i) {
2643 PyObject *item, *arg, *good;
2644 int ok;
2645
2646 item = (*strobj->ob_type->tp_as_sequence->sq_item)(strobj, i);
2647 if (item == NULL)
2648 goto Fail_1;
Walter Dörwald1918f772003-02-10 13:19:13 +00002649 if (func == Py_None) {
2650 ok = 1;
2651 } else {
Raymond Hettinger8ae46892003-10-12 19:09:37 +00002652 arg = PyTuple_Pack(1, item);
Walter Dörwald1918f772003-02-10 13:19:13 +00002653 if (arg == NULL) {
2654 Py_DECREF(item);
2655 goto Fail_1;
2656 }
2657 good = PyEval_CallObject(func, arg);
2658 Py_DECREF(arg);
2659 if (good == NULL) {
2660 Py_DECREF(item);
2661 goto Fail_1;
2662 }
2663 ok = PyObject_IsTrue(good);
2664 Py_DECREF(good);
Martin v. Löwis8afd7572003-01-25 22:46:11 +00002665 }
Walter Dörwald903f1e02003-02-04 16:28:00 +00002666 if (ok) {
Martin v. Löwisd96ee902006-02-16 14:37:16 +00002667 Py_ssize_t reslen;
Walter Dörwald903f1e02003-02-04 16:28:00 +00002668 if (!PyUnicode_Check(item)) {
Georg Brandl99d7e4e2005-08-31 22:21:15 +00002669 PyErr_SetString(PyExc_TypeError,
Jeremy Hylton1aad9c72003-09-16 03:10:59 +00002670 "can't filter unicode to unicode:"
2671 " __getitem__ returned different type");
Walter Dörwald903f1e02003-02-04 16:28:00 +00002672 Py_DECREF(item);
2673 goto Fail_1;
2674 }
2675 reslen = PyUnicode_GET_SIZE(item);
Georg Brandl99d7e4e2005-08-31 22:21:15 +00002676 if (reslen == 1)
Walter Dörwald903f1e02003-02-04 16:28:00 +00002677 PyUnicode_AS_UNICODE(result)[j++] =
2678 PyUnicode_AS_UNICODE(item)[0];
Jeremy Hylton1aad9c72003-09-16 03:10:59 +00002679 else {
Walter Dörwald903f1e02003-02-04 16:28:00 +00002680 /* do we need more space? */
Martin v. Löwisd96ee902006-02-16 14:37:16 +00002681 Py_ssize_t need = j + reslen + len - i - 1;
Walter Dörwald903f1e02003-02-04 16:28:00 +00002682 if (need > outlen) {
Georg Brandl99d7e4e2005-08-31 22:21:15 +00002683 /* overallocate,
Jeremy Hylton1aad9c72003-09-16 03:10:59 +00002684 to avoid reallocations */
2685 if (need < 2 * outlen)
2686 need = 2 * outlen;
Jeremy Hylton364f6be2003-09-16 03:17:16 +00002687 if (PyUnicode_Resize(
2688 &result, need) < 0) {
Walter Dörwald903f1e02003-02-04 16:28:00 +00002689 Py_DECREF(item);
Walter Dörwald531e0002003-02-04 16:57:49 +00002690 goto Fail_1;
Walter Dörwald903f1e02003-02-04 16:28:00 +00002691 }
2692 outlen = need;
2693 }
Jeremy Hylton1aad9c72003-09-16 03:10:59 +00002694 memcpy(PyUnicode_AS_UNICODE(result) + j,
2695 PyUnicode_AS_UNICODE(item),
2696 reslen*sizeof(Py_UNICODE));
Walter Dörwald903f1e02003-02-04 16:28:00 +00002697 j += reslen;
2698 }
2699 }
Martin v. Löwis8afd7572003-01-25 22:46:11 +00002700 Py_DECREF(item);
2701 }
2702
Walter Dörwald903f1e02003-02-04 16:28:00 +00002703 if (j < outlen)
Martin v. Löwis8afd7572003-01-25 22:46:11 +00002704 PyUnicode_Resize(&result, j);
2705
2706 return result;
2707
2708Fail_1:
2709 Py_DECREF(result);
2710 return NULL;
2711}
2712#endif