blob: 5e74929e4ad9e81b2b9f3a5730fefc28701351d2 [file] [log] [blame]
Andrew M. Kuchling9bcc68c2000-12-20 15:07:34 +00001
Guido van Rossum3f5da241990-12-20 15:06:42 +00002/* Built-in functions */
3
Guido van Rossum79f25d91997-04-29 20:08:16 +00004#include "Python.h"
Guido van Rossum3f5da241990-12-20 15:06:42 +00005
6#include "node.h"
Guido van Rossum5b722181993-03-30 17:46:03 +00007#include "compile.h"
8#include "eval.h"
Guido van Rossum3f5da241990-12-20 15:06:42 +00009
Guido van Rossum6bf62da1997-04-11 20:37:35 +000010#include <ctype.h>
11
Guido van Rossume2ae77b2001-10-24 20:42:55 +000012#ifdef RISCOS
13#include "unixstuff.h"
14#endif
15
Mark Hammond26cffde42001-05-14 12:17:34 +000016/* The default encoding used by the platform file system APIs
17 Can remain NULL for all platforms that don't have such a concept
18*/
Martin v. Löwis6238d2b2002-06-30 15:26:10 +000019#if defined(MS_WINDOWS) && defined(HAVE_USABLE_WCHAR_T)
Mark Hammond26cffde42001-05-14 12:17:34 +000020const char *Py_FileSystemDefaultEncoding = "mbcs";
Just van Rossumb9b8e9c2003-02-10 09:22:01 +000021#elif defined(__APPLE__)
22const char *Py_FileSystemDefaultEncoding = "utf-8";
Mark Hammond26cffde42001-05-14 12:17:34 +000023#else
24const char *Py_FileSystemDefaultEncoding = NULL; /* use default */
25#endif
Mark Hammondef8b6542001-05-13 08:04:26 +000026
Guido van Rossum12d12c51993-10-26 17:58:25 +000027/* Forward */
Tim Petersdbd9ba62000-07-09 03:09:57 +000028static PyObject *filterstring(PyObject *, PyObject *);
Martin v. Löwis8afd7572003-01-25 22:46:11 +000029#ifdef Py_USING_UNICODE
30static PyObject *filterunicode(PyObject *, PyObject *);
31#endif
Tim Petersdbd9ba62000-07-09 03:09:57 +000032static PyObject *filtertuple (PyObject *, PyObject *);
Guido van Rossum12d12c51993-10-26 17:58:25 +000033
Guido van Rossum79f25d91997-04-29 20:08:16 +000034static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +000035builtin___import__(PyObject *self, PyObject *args)
Guido van Rossum3f5da241990-12-20 15:06:42 +000036{
Guido van Rossum1ae940a1995-01-02 19:04:15 +000037 char *name;
Guido van Rossum79f25d91997-04-29 20:08:16 +000038 PyObject *globals = NULL;
39 PyObject *locals = NULL;
40 PyObject *fromlist = NULL;
Guido van Rossum1ae940a1995-01-02 19:04:15 +000041
Guido van Rossum79f25d91997-04-29 20:08:16 +000042 if (!PyArg_ParseTuple(args, "s|OOO:__import__",
Guido van Rossum24c13741995-02-14 09:42:43 +000043 &name, &globals, &locals, &fromlist))
Guido van Rossum1ae940a1995-01-02 19:04:15 +000044 return NULL;
Guido van Rossumaee0bad1997-09-05 07:33:22 +000045 return PyImport_ImportModuleEx(name, globals, locals, fromlist);
Guido van Rossum1ae940a1995-01-02 19:04:15 +000046}
47
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000048PyDoc_STRVAR(import_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +000049"__import__(name, globals, locals, fromlist) -> module\n\
50\n\
51Import a module. The globals are only used to determine the context;\n\
52they are not modified. The locals are currently unused. The fromlist\n\
53should be a list of names to emulate ``from name import ...'', or an\n\
54empty list to emulate ``import name''.\n\
55When importing a module from a package, note that __import__('A.B', ...)\n\
56returns package A when fromlist is empty, but its submodule B when\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000057fromlist is not empty.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +000058
Guido van Rossum1ae940a1995-01-02 19:04:15 +000059
Guido van Rossum79f25d91997-04-29 20:08:16 +000060static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +000061builtin_abs(PyObject *self, PyObject *v)
Guido van Rossum1ae940a1995-01-02 19:04:15 +000062{
Guido van Rossum09df08a1998-05-22 00:51:39 +000063 return PyNumber_Absolute(v);
Guido van Rossum3f5da241990-12-20 15:06:42 +000064}
65
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000066PyDoc_STRVAR(abs_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +000067"abs(number) -> number\n\
68\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000069Return the absolute value of the argument.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +000070
71
Guido van Rossum79f25d91997-04-29 20:08:16 +000072static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +000073builtin_apply(PyObject *self, PyObject *args)
Guido van Rossumc02e15c1991-12-16 13:03:00 +000074{
Guido van Rossum79f25d91997-04-29 20:08:16 +000075 PyObject *func, *alist = NULL, *kwdict = NULL;
Barry Warsaw968f8cb1998-10-01 15:33:12 +000076 PyObject *t = NULL, *retval = NULL;
Guido van Rossum1ae940a1995-01-02 19:04:15 +000077
Neal Norwitz3e590762003-02-23 21:45:43 +000078 if (PyErr_Warn(PyExc_PendingDeprecationWarning,
79 "use func(*args, **kwargs) instead of "
80 "apply(func, args, kwargs)") < 0)
81 return NULL;
Raymond Hettingerea3fdf42002-12-29 16:33:45 +000082 if (!PyArg_UnpackTuple(args, "apply", 1, 3, &func, &alist, &kwdict))
Guido van Rossumc02e15c1991-12-16 13:03:00 +000083 return NULL;
Barry Warsaw968f8cb1998-10-01 15:33:12 +000084 if (alist != NULL) {
85 if (!PyTuple_Check(alist)) {
86 if (!PySequence_Check(alist)) {
Jeremy Hyltonc862cf42001-01-19 03:25:05 +000087 PyErr_Format(PyExc_TypeError,
Alex Martellia9b9c9f2003-04-23 13:34:35 +000088 "apply() arg 2 expected sequence, found %s",
Jeremy Hyltonc862cf42001-01-19 03:25:05 +000089 alist->ob_type->tp_name);
Barry Warsaw968f8cb1998-10-01 15:33:12 +000090 return NULL;
91 }
92 t = PySequence_Tuple(alist);
93 if (t == NULL)
94 return NULL;
95 alist = t;
96 }
Guido van Rossum2d951851994-08-29 12:52:16 +000097 }
Guido van Rossum79f25d91997-04-29 20:08:16 +000098 if (kwdict != NULL && !PyDict_Check(kwdict)) {
Jeremy Hyltonc862cf42001-01-19 03:25:05 +000099 PyErr_Format(PyExc_TypeError,
100 "apply() arg 3 expected dictionary, found %s",
101 kwdict->ob_type->tp_name);
Barry Warsaw968f8cb1998-10-01 15:33:12 +0000102 goto finally;
Guido van Rossum681d79a1995-07-18 14:51:37 +0000103 }
Barry Warsaw968f8cb1998-10-01 15:33:12 +0000104 retval = PyEval_CallObjectWithKeywords(func, alist, kwdict);
105 finally:
106 Py_XDECREF(t);
107 return retval;
Guido van Rossumc02e15c1991-12-16 13:03:00 +0000108}
109
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000110PyDoc_STRVAR(apply_doc,
Fred Drakef1fbc622001-01-12 17:05:05 +0000111"apply(object[, args[, kwargs]]) -> value\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000112\n\
Fred Drake7b912121999-12-23 14:16:55 +0000113Call a callable object with positional arguments taken from the tuple args,\n\
114and keyword arguments taken from the optional dictionary kwargs.\n\
Raymond Hettingerff41c482003-04-06 09:01:11 +0000115Note that classes are callable, as are instances with a __call__() method.\n\
116\n\
117Deprecated since release 2.3. Instead, use the extended call syntax:\n\
118 function(*args, **keywords).");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000119
120
Guido van Rossum79f25d91997-04-29 20:08:16 +0000121static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000122builtin_callable(PyObject *self, PyObject *v)
Guido van Rossum2d951851994-08-29 12:52:16 +0000123{
Guido van Rossum77f6a652002-04-03 22:41:51 +0000124 return PyBool_FromLong((long)PyCallable_Check(v));
Guido van Rossum2d951851994-08-29 12:52:16 +0000125}
126
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000127PyDoc_STRVAR(callable_doc,
Guido van Rossum77f6a652002-04-03 22:41:51 +0000128"callable(object) -> bool\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000129\n\
130Return whether the object is callable (i.e., some kind of function).\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000131Note that classes are callable, as are instances with a __call__() method.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000132
133
Guido van Rossum79f25d91997-04-29 20:08:16 +0000134static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000135builtin_filter(PyObject *self, PyObject *args)
Guido van Rossum12d12c51993-10-26 17:58:25 +0000136{
Guido van Rossumc7903a12002-08-16 07:04:56 +0000137 PyObject *func, *seq, *result, *it, *arg;
Tim Peters0e57abf2001-05-02 07:39:38 +0000138 int len; /* guess for result list size */
Tim Petersf4848da2001-05-05 00:14:56 +0000139 register int j;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000140
Raymond Hettingerea3fdf42002-12-29 16:33:45 +0000141 if (!PyArg_UnpackTuple(args, "filter", 2, 2, &func, &seq))
Guido van Rossum12d12c51993-10-26 17:58:25 +0000142 return NULL;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000143
Tim Peters0e57abf2001-05-02 07:39:38 +0000144 /* Strings and tuples return a result of the same type. */
145 if (PyString_Check(seq))
146 return filterstring(func, seq);
Martin v. Löwis8afd7572003-01-25 22:46:11 +0000147#ifdef Py_USING_UNICODE
148 if (PyUnicode_Check(seq))
149 return filterunicode(func, seq);
150#endif
Tim Peters0e57abf2001-05-02 07:39:38 +0000151 if (PyTuple_Check(seq))
152 return filtertuple(func, seq);
153
154 /* Get iterator. */
155 it = PyObject_GetIter(seq);
156 if (it == NULL)
157 return NULL;
158
159 /* Guess a result list size. */
160 len = -1; /* unknown */
161 if (PySequence_Check(seq) &&
162 seq->ob_type->tp_as_sequence->sq_length) {
163 len = PySequence_Size(seq);
164 if (len < 0)
165 PyErr_Clear();
Guido van Rossum12d12c51993-10-26 17:58:25 +0000166 }
Tim Peters0e57abf2001-05-02 07:39:38 +0000167 if (len < 0)
168 len = 8; /* arbitrary */
Guido van Rossum12d12c51993-10-26 17:58:25 +0000169
Guido van Rossumc7903a12002-08-16 07:04:56 +0000170 /* Pre-allocate argument list tuple. */
171 arg = PyTuple_New(1);
172 if (arg == NULL)
173 goto Fail_arg;
174
Tim Peters0e57abf2001-05-02 07:39:38 +0000175 /* Get a result list. */
Guido van Rossum79f25d91997-04-29 20:08:16 +0000176 if (PyList_Check(seq) && seq->ob_refcnt == 1) {
Tim Peters0e57abf2001-05-02 07:39:38 +0000177 /* Eww - can modify the list in-place. */
Guido van Rossum79f25d91997-04-29 20:08:16 +0000178 Py_INCREF(seq);
Guido van Rossum12d12c51993-10-26 17:58:25 +0000179 result = seq;
180 }
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000181 else {
Tim Peters0e57abf2001-05-02 07:39:38 +0000182 result = PyList_New(len);
183 if (result == NULL)
184 goto Fail_it;
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000185 }
Guido van Rossum12d12c51993-10-26 17:58:25 +0000186
Tim Peters0e57abf2001-05-02 07:39:38 +0000187 /* Build the result list. */
Tim Petersf4848da2001-05-05 00:14:56 +0000188 j = 0;
189 for (;;) {
Guido van Rossumc7903a12002-08-16 07:04:56 +0000190 PyObject *item;
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000191 int ok;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000192
Tim Peters0e57abf2001-05-02 07:39:38 +0000193 item = PyIter_Next(it);
194 if (item == NULL) {
Tim Petersf4848da2001-05-05 00:14:56 +0000195 if (PyErr_Occurred())
196 goto Fail_result_it;
Tim Peters0e57abf2001-05-02 07:39:38 +0000197 break;
Guido van Rossum2d951851994-08-29 12:52:16 +0000198 }
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000199
Neil Schemenauer68973552003-08-14 20:37:34 +0000200 if (func == (PyObject *)&PyBool_Type || func == Py_None) {
Guido van Rossumc7903a12002-08-16 07:04:56 +0000201 ok = PyObject_IsTrue(item);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000202 }
203 else {
Guido van Rossumc7903a12002-08-16 07:04:56 +0000204 PyObject *good;
205 PyTuple_SET_ITEM(arg, 0, item);
206 good = PyObject_Call(func, arg, NULL);
207 PyTuple_SET_ITEM(arg, 0, NULL);
Guido van Rossum58b68731995-01-10 17:40:55 +0000208 if (good == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000209 Py_DECREF(item);
Tim Peters0e57abf2001-05-02 07:39:38 +0000210 goto Fail_result_it;
Guido van Rossum58b68731995-01-10 17:40:55 +0000211 }
Guido van Rossumc7903a12002-08-16 07:04:56 +0000212 ok = PyObject_IsTrue(good);
213 Py_DECREF(good);
Guido van Rossum12d12c51993-10-26 17:58:25 +0000214 }
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000215 if (ok) {
Tim Peters0e57abf2001-05-02 07:39:38 +0000216 if (j < len)
217 PyList_SET_ITEM(result, j, item);
Guido van Rossum2d951851994-08-29 12:52:16 +0000218 else {
Barry Warsawfa77e091999-01-28 18:49:12 +0000219 int status = PyList_Append(result, item);
Barry Warsawfa77e091999-01-28 18:49:12 +0000220 Py_DECREF(item);
221 if (status < 0)
Tim Peters0e57abf2001-05-02 07:39:38 +0000222 goto Fail_result_it;
Guido van Rossum2d951851994-08-29 12:52:16 +0000223 }
Tim Peters0e57abf2001-05-02 07:39:38 +0000224 ++j;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000225 }
Tim Peters0e57abf2001-05-02 07:39:38 +0000226 else
227 Py_DECREF(item);
Guido van Rossum12d12c51993-10-26 17:58:25 +0000228 }
229
Guido van Rossum12d12c51993-10-26 17:58:25 +0000230
Tim Peters0e57abf2001-05-02 07:39:38 +0000231 /* Cut back result list if len is too big. */
Guido van Rossum79f25d91997-04-29 20:08:16 +0000232 if (j < len && PyList_SetSlice(result, j, len, NULL) < 0)
Tim Peters0e57abf2001-05-02 07:39:38 +0000233 goto Fail_result_it;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000234
Tim Peters3c6b1482001-05-21 08:07:05 +0000235 Py_DECREF(it);
Guido van Rossumc7903a12002-08-16 07:04:56 +0000236 Py_DECREF(arg);
Guido van Rossum12d12c51993-10-26 17:58:25 +0000237 return result;
238
Tim Peters0e57abf2001-05-02 07:39:38 +0000239Fail_result_it:
Guido van Rossum79f25d91997-04-29 20:08:16 +0000240 Py_DECREF(result);
Tim Peters0e57abf2001-05-02 07:39:38 +0000241Fail_it:
242 Py_DECREF(it);
Guido van Rossumc7903a12002-08-16 07:04:56 +0000243Fail_arg:
244 Py_DECREF(arg);
Guido van Rossum12d12c51993-10-26 17:58:25 +0000245 return NULL;
246}
247
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000248PyDoc_STRVAR(filter_doc,
Tim Petersd50e5442002-03-09 00:06:26 +0000249"filter(function or None, sequence) -> list, tuple, or string\n"
250"\n"
251"Return those items of sequence for which function(item) is true. If\n"
252"function is None, return the items that are true. If sequence is a tuple\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000253"or string, return the same type, else return a list.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000254
Guido van Rossum79f25d91997-04-29 20:08:16 +0000255static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000256builtin_chr(PyObject *self, PyObject *args)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000257{
258 long x;
259 char s[1];
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000260
Guido van Rossum79f25d91997-04-29 20:08:16 +0000261 if (!PyArg_ParseTuple(args, "l:chr", &x))
Guido van Rossum3f5da241990-12-20 15:06:42 +0000262 return NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000263 if (x < 0 || x >= 256) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000264 PyErr_SetString(PyExc_ValueError,
265 "chr() arg not in range(256)");
Guido van Rossum3f5da241990-12-20 15:06:42 +0000266 return NULL;
267 }
Guido van Rossum6bf62da1997-04-11 20:37:35 +0000268 s[0] = (char)x;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000269 return PyString_FromStringAndSize(s, 1);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000270}
271
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000272PyDoc_STRVAR(chr_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000273"chr(i) -> character\n\
274\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000275Return a string of one character with ordinal i; 0 <= i < 256.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000276
277
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000278#ifdef Py_USING_UNICODE
Guido van Rossum79f25d91997-04-29 20:08:16 +0000279static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000280builtin_unichr(PyObject *self, PyObject *args)
Guido van Rossum09095f32000-03-10 23:00:52 +0000281{
282 long x;
Guido van Rossum09095f32000-03-10 23:00:52 +0000283
284 if (!PyArg_ParseTuple(args, "l:unichr", &x))
285 return NULL;
Fredrik Lundh0dcf67e2001-06-26 20:01:56 +0000286
Marc-André Lemburgcc8764c2002-08-11 12:23:04 +0000287 return PyUnicode_FromOrdinal(x);
Guido van Rossum09095f32000-03-10 23:00:52 +0000288}
289
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000290PyDoc_STRVAR(unichr_doc,
Fred Drake078b24f2000-04-13 02:42:50 +0000291"unichr(i) -> Unicode character\n\
Guido van Rossum09095f32000-03-10 23:00:52 +0000292\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000293Return a Unicode string of one character with ordinal i; 0 <= i <= 0x10ffff.");
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000294#endif
Guido van Rossum09095f32000-03-10 23:00:52 +0000295
296
297static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000298builtin_cmp(PyObject *self, PyObject *args)
Guido van Rossuma9e7dc11992-10-18 18:53:57 +0000299{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000300 PyObject *a, *b;
Guido van Rossum09df08a1998-05-22 00:51:39 +0000301 int c;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000302
Raymond Hettingerea3fdf42002-12-29 16:33:45 +0000303 if (!PyArg_UnpackTuple(args, "cmp", 2, 2, &a, &b))
Guido van Rossuma9e7dc11992-10-18 18:53:57 +0000304 return NULL;
Guido van Rossum09df08a1998-05-22 00:51:39 +0000305 if (PyObject_Cmp(a, b, &c) < 0)
Guido van Rossumc8b6df91997-05-23 00:06:51 +0000306 return NULL;
Guido van Rossum09df08a1998-05-22 00:51:39 +0000307 return PyInt_FromLong((long)c);
Guido van Rossuma9e7dc11992-10-18 18:53:57 +0000308}
309
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000310PyDoc_STRVAR(cmp_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000311"cmp(x, y) -> integer\n\
312\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000313Return negative if x<y, zero if x==y, positive if x>y.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000314
315
Guido van Rossum79f25d91997-04-29 20:08:16 +0000316static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000317builtin_coerce(PyObject *self, PyObject *args)
Guido van Rossum6a00cd81995-01-07 12:39:01 +0000318{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000319 PyObject *v, *w;
320 PyObject *res;
Guido van Rossum5524a591995-01-10 15:26:20 +0000321
Raymond Hettingerea3fdf42002-12-29 16:33:45 +0000322 if (!PyArg_UnpackTuple(args, "coerce", 2, 2, &v, &w))
Guido van Rossum5524a591995-01-10 15:26:20 +0000323 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000324 if (PyNumber_Coerce(&v, &w) < 0)
Guido van Rossum04691fc1992-08-12 15:35:34 +0000325 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000326 res = Py_BuildValue("(OO)", v, w);
327 Py_DECREF(v);
328 Py_DECREF(w);
Guido van Rossum04691fc1992-08-12 15:35:34 +0000329 return res;
330}
331
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000332PyDoc_STRVAR(coerce_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000333"coerce(x, y) -> None or (x1, y1)\n\
334\n\
335When x and y can be coerced to values of the same type, return a tuple\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000336containing the coerced values. When they can't be coerced, return None.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000337
338
Guido van Rossum79f25d91997-04-29 20:08:16 +0000339static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000340builtin_compile(PyObject *self, PyObject *args)
Guido van Rossum5b722181993-03-30 17:46:03 +0000341{
342 char *str;
343 char *filename;
344 char *startstr;
345 int start;
Tim Peters6cd6a822001-08-17 22:11:27 +0000346 int dont_inherit = 0;
347 int supplied_flags = 0;
Tim Peters5ba58662001-07-16 02:29:45 +0000348 PyCompilerFlags cf;
Just van Rossum3aaf42c2003-02-10 08:21:10 +0000349 PyObject *result, *cmd, *tmp = NULL;
Just van Rossumb9b8e9c2003-02-10 09:22:01 +0000350 int length;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000351
Just van Rossum3aaf42c2003-02-10 08:21:10 +0000352 if (!PyArg_ParseTuple(args, "Oss|ii:compile", &cmd, &filename,
Tim Peters6cd6a822001-08-17 22:11:27 +0000353 &startstr, &supplied_flags, &dont_inherit))
Guido van Rossum5b722181993-03-30 17:46:03 +0000354 return NULL;
Tim Peters6cd6a822001-08-17 22:11:27 +0000355
Just van Rossum3aaf42c2003-02-10 08:21:10 +0000356 cf.cf_flags = supplied_flags;
357
358#ifdef Py_USING_UNICODE
359 if (PyUnicode_Check(cmd)) {
360 tmp = PyUnicode_AsUTF8String(cmd);
361 if (tmp == NULL)
362 return NULL;
363 cmd = tmp;
364 cf.cf_flags |= PyCF_SOURCE_IS_UTF8;
365 }
366#endif
Just van Rossumb9b8e9c2003-02-10 09:22:01 +0000367 if (PyObject_AsReadBuffer(cmd, (const void **)&str, &length))
368 return NULL;
Tim Peters2c646c92003-02-10 14:48:29 +0000369 if ((size_t)length != strlen(str)) {
Just van Rossum3aaf42c2003-02-10 08:21:10 +0000370 PyErr_SetString(PyExc_TypeError,
Alex Martellia9b9c9f2003-04-23 13:34:35 +0000371 "compile() expected string without null bytes");
Just van Rossum3aaf42c2003-02-10 08:21:10 +0000372 return NULL;
373 }
374
Guido van Rossum5b722181993-03-30 17:46:03 +0000375 if (strcmp(startstr, "exec") == 0)
Guido van Rossumb05a5c71997-05-07 17:46:13 +0000376 start = Py_file_input;
Guido van Rossum5b722181993-03-30 17:46:03 +0000377 else if (strcmp(startstr, "eval") == 0)
Guido van Rossumb05a5c71997-05-07 17:46:13 +0000378 start = Py_eval_input;
Guido van Rossum872537c1995-07-07 22:43:42 +0000379 else if (strcmp(startstr, "single") == 0)
Guido van Rossumb05a5c71997-05-07 17:46:13 +0000380 start = Py_single_input;
Guido van Rossum5b722181993-03-30 17:46:03 +0000381 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000382 PyErr_SetString(PyExc_ValueError,
Fred Drake661ea262000-10-24 19:57:45 +0000383 "compile() arg 3 must be 'exec' or 'eval' or 'single'");
Guido van Rossum5b722181993-03-30 17:46:03 +0000384 return NULL;
385 }
Tim Peters6cd6a822001-08-17 22:11:27 +0000386
Guido van Rossum4b499dd32003-02-13 22:07:59 +0000387 if (supplied_flags &
388 ~(PyCF_MASK | PyCF_MASK_OBSOLETE | PyCF_DONT_IMPLY_DEDENT))
389 {
Tim Peters6cd6a822001-08-17 22:11:27 +0000390 PyErr_SetString(PyExc_ValueError,
391 "compile(): unrecognised flags");
392 return NULL;
393 }
394 /* XXX Warn if (supplied_flags & PyCF_MASK_OBSOLETE) != 0? */
395
Tim Peters6cd6a822001-08-17 22:11:27 +0000396 if (!dont_inherit) {
397 PyEval_MergeCompilerFlags(&cf);
398 }
Just van Rossum3aaf42c2003-02-10 08:21:10 +0000399 result = Py_CompileStringFlags(str, filename, start, &cf);
400 Py_XDECREF(tmp);
401 return result;
Guido van Rossum5b722181993-03-30 17:46:03 +0000402}
403
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000404PyDoc_STRVAR(compile_doc,
Tim Peters6cd6a822001-08-17 22:11:27 +0000405"compile(source, filename, mode[, flags[, dont_inherit]]) -> code object\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000406\n\
407Compile the source string (a Python module, statement or expression)\n\
408into a code object that can be executed by the exec statement or eval().\n\
409The filename will be used for run-time error messages.\n\
410The mode must be 'exec' to compile a module, 'single' to compile a\n\
Tim Peters6cd6a822001-08-17 22:11:27 +0000411single (interactive) statement, or 'eval' to compile an expression.\n\
412The flags argument, if present, controls which future statements influence\n\
413the compilation of the code.\n\
414The dont_inherit argument, if non-zero, stops the compilation inheriting\n\
415the effects of any future statements in effect in the code calling\n\
416compile; if absent or zero these statements do influence the compilation,\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000417in addition to any features explicitly specified.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000418
Guido van Rossum79f25d91997-04-29 20:08:16 +0000419static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000420builtin_dir(PyObject *self, PyObject *args)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000421{
Tim Peters5d2b77c2001-09-03 05:47:38 +0000422 PyObject *arg = NULL;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000423
Raymond Hettingerea3fdf42002-12-29 16:33:45 +0000424 if (!PyArg_UnpackTuple(args, "dir", 0, 1, &arg))
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000425 return NULL;
Tim Peters7eea37e2001-09-04 22:08:56 +0000426 return PyObject_Dir(arg);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000427}
428
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000429PyDoc_STRVAR(dir_doc,
Tim Peters5d2b77c2001-09-03 05:47:38 +0000430"dir([object]) -> list of strings\n"
431"\n"
432"Return an alphabetized list of names comprising (some of) the attributes\n"
433"of the given object, and of attributes reachable from it:\n"
434"\n"
435"No argument: the names in the current scope.\n"
436"Module object: the module attributes.\n"
Tim Peters37a309d2001-09-04 01:20:04 +0000437"Type or class object: its attributes, and recursively the attributes of\n"
438" its bases.\n"
Tim Peters5d2b77c2001-09-03 05:47:38 +0000439"Otherwise: its attributes, its class's attributes, and recursively the\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000440" attributes of its class's base classes.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000441
Guido van Rossum79f25d91997-04-29 20:08:16 +0000442static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000443builtin_divmod(PyObject *self, PyObject *args)
Guido van Rossum6a00cd81995-01-07 12:39:01 +0000444{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000445 PyObject *v, *w;
Guido van Rossum6a00cd81995-01-07 12:39:01 +0000446
Raymond Hettingerea3fdf42002-12-29 16:33:45 +0000447 if (!PyArg_UnpackTuple(args, "divmod", 2, 2, &v, &w))
Guido van Rossum6a00cd81995-01-07 12:39:01 +0000448 return NULL;
Guido van Rossum09df08a1998-05-22 00:51:39 +0000449 return PyNumber_Divmod(v, w);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000450}
451
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000452PyDoc_STRVAR(divmod_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000453"divmod(x, y) -> (div, mod)\n\
454\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000455Return the tuple ((x-x%y)/y, x%y). Invariant: div*y + mod == x.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000456
457
Guido van Rossum79f25d91997-04-29 20:08:16 +0000458static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000459builtin_eval(PyObject *self, PyObject *args)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000460{
Just van Rossum3aaf42c2003-02-10 08:21:10 +0000461 PyObject *cmd, *result, *tmp = NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000462 PyObject *globals = Py_None, *locals = Py_None;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000463 char *str;
Tim Peters9fa96be2001-08-17 23:04:59 +0000464 PyCompilerFlags cf;
Guido van Rossum590baa41993-11-30 13:40:46 +0000465
Guido van Rossum79f25d91997-04-29 20:08:16 +0000466 if (!PyArg_ParseTuple(args, "O|O!O!:eval",
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000467 &cmd,
Guido van Rossum79f25d91997-04-29 20:08:16 +0000468 &PyDict_Type, &globals,
469 &PyDict_Type, &locals))
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000470 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000471 if (globals == Py_None) {
472 globals = PyEval_GetGlobals();
473 if (locals == Py_None)
474 locals = PyEval_GetLocals();
Guido van Rossum6135a871995-01-09 17:53:26 +0000475 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000476 else if (locals == Py_None)
Guido van Rossum6135a871995-01-09 17:53:26 +0000477 locals = globals;
Tim Peters9fa96be2001-08-17 23:04:59 +0000478
Guido van Rossum79f25d91997-04-29 20:08:16 +0000479 if (PyDict_GetItemString(globals, "__builtins__") == NULL) {
480 if (PyDict_SetItemString(globals, "__builtins__",
481 PyEval_GetBuiltins()) != 0)
Guido van Rossum6135a871995-01-09 17:53:26 +0000482 return NULL;
483 }
Tim Peters9fa96be2001-08-17 23:04:59 +0000484
Jeremy Hylton15c1c4f2001-07-30 21:50:55 +0000485 if (PyCode_Check(cmd)) {
Jeremy Hylton733c8932001-12-13 19:51:56 +0000486 if (PyCode_GetNumFree((PyCodeObject *)cmd) > 0) {
Jeremy Hylton15c1c4f2001-07-30 21:50:55 +0000487 PyErr_SetString(PyExc_TypeError,
488 "code object passed to eval() may not contain free variables");
489 return NULL;
490 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000491 return PyEval_EvalCode((PyCodeObject *) cmd, globals, locals);
Jeremy Hylton15c1c4f2001-07-30 21:50:55 +0000492 }
Tim Peters9fa96be2001-08-17 23:04:59 +0000493
Marc-André Lemburgd1ba4432000-09-19 21:04:18 +0000494 if (!PyString_Check(cmd) &&
495 !PyUnicode_Check(cmd)) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000496 PyErr_SetString(PyExc_TypeError,
Fred Drake661ea262000-10-24 19:57:45 +0000497 "eval() arg 1 must be a string or code object");
Guido van Rossum94390a41992-08-14 15:14:30 +0000498 return NULL;
499 }
Just van Rossum3aaf42c2003-02-10 08:21:10 +0000500 cf.cf_flags = 0;
501
502#ifdef Py_USING_UNICODE
503 if (PyUnicode_Check(cmd)) {
504 tmp = PyUnicode_AsUTF8String(cmd);
505 if (tmp == NULL)
506 return NULL;
507 cmd = tmp;
508 cf.cf_flags |= PyCF_SOURCE_IS_UTF8;
509 }
510#endif
Marc-André Lemburgd1ba4432000-09-19 21:04:18 +0000511 if (PyString_AsStringAndSize(cmd, &str, NULL))
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000512 return NULL;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000513 while (*str == ' ' || *str == '\t')
514 str++;
Tim Peters9fa96be2001-08-17 23:04:59 +0000515
Tim Peters9fa96be2001-08-17 23:04:59 +0000516 (void)PyEval_MergeCompilerFlags(&cf);
Just van Rossum3aaf42c2003-02-10 08:21:10 +0000517 result = PyRun_StringFlags(str, Py_eval_input, globals, locals, &cf);
518 Py_XDECREF(tmp);
519 return result;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000520}
521
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000522PyDoc_STRVAR(eval_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000523"eval(source[, globals[, locals]]) -> value\n\
524\n\
525Evaluate the source in the context of globals and locals.\n\
526The source may be a string representing a Python expression\n\
527or a code object as returned by compile().\n\
528The globals and locals are dictionaries, defaulting to the current\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000529globals and locals. If only globals is given, locals defaults to it.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000530
531
Guido van Rossum79f25d91997-04-29 20:08:16 +0000532static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000533builtin_execfile(PyObject *self, PyObject *args)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000534{
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000535 char *filename;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000536 PyObject *globals = Py_None, *locals = Py_None;
537 PyObject *res;
Guido van Rossumb3a639e2001-09-05 13:37:47 +0000538 FILE* fp = NULL;
Tim Peters5ba58662001-07-16 02:29:45 +0000539 PyCompilerFlags cf;
Martin v. Löwis6b3a2c42001-08-08 05:30:36 +0000540 int exists;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000541
Guido van Rossum79f25d91997-04-29 20:08:16 +0000542 if (!PyArg_ParseTuple(args, "s|O!O!:execfile",
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000543 &filename,
Guido van Rossum79f25d91997-04-29 20:08:16 +0000544 &PyDict_Type, &globals,
545 &PyDict_Type, &locals))
Guido van Rossum0f61f8a1992-02-25 18:55:05 +0000546 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000547 if (globals == Py_None) {
548 globals = PyEval_GetGlobals();
549 if (locals == Py_None)
550 locals = PyEval_GetLocals();
Guido van Rossum6135a871995-01-09 17:53:26 +0000551 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000552 else if (locals == Py_None)
Guido van Rossum6135a871995-01-09 17:53:26 +0000553 locals = globals;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000554 if (PyDict_GetItemString(globals, "__builtins__") == NULL) {
555 if (PyDict_SetItemString(globals, "__builtins__",
556 PyEval_GetBuiltins()) != 0)
Guido van Rossum6135a871995-01-09 17:53:26 +0000557 return NULL;
558 }
Martin v. Löwis6b3a2c42001-08-08 05:30:36 +0000559
560 exists = 0;
561 /* Test for existence or directory. */
Martin v. Löwis3484a182002-03-09 12:07:51 +0000562#if defined(PLAN9)
563 {
564 Dir *d;
565
566 if ((d = dirstat(filename))!=nil) {
567 if(d->mode & DMDIR)
568 werrstr("is a directory");
569 else
570 exists = 1;
571 free(d);
572 }
Martin v. Löwis6b3a2c42001-08-08 05:30:36 +0000573 }
Martin v. Löwis3484a182002-03-09 12:07:51 +0000574#elif defined(RISCOS)
Guido van Rossume2ae77b2001-10-24 20:42:55 +0000575 if (object_exists(filename)) {
576 if (isdir(filename))
577 errno = EISDIR;
578 else
579 exists = 1;
580 }
Martin v. Löwis3484a182002-03-09 12:07:51 +0000581#else /* standard Posix */
582 {
583 struct stat s;
584 if (stat(filename, &s) == 0) {
585 if (S_ISDIR(s.st_mode))
586# if defined(PY_OS2) && defined(PYCC_VACPP)
587 errno = EOS2ERR;
588# else
589 errno = EISDIR;
590# endif
591 else
592 exists = 1;
593 }
594 }
595#endif
Martin v. Löwis6b3a2c42001-08-08 05:30:36 +0000596
597 if (exists) {
598 Py_BEGIN_ALLOW_THREADS
Jack Jansen7b8c7542002-04-14 20:12:41 +0000599 fp = fopen(filename, "r" PY_STDIOTEXTMODE);
Martin v. Löwis6b3a2c42001-08-08 05:30:36 +0000600 Py_END_ALLOW_THREADS
601
602 if (fp == NULL) {
603 exists = 0;
604 }
605 }
606
607 if (!exists) {
Peter Schneider-Kamp4c013422002-08-27 16:58:00 +0000608 PyErr_SetFromErrnoWithFilename(PyExc_IOError, filename);
Guido van Rossum0f61f8a1992-02-25 18:55:05 +0000609 return NULL;
610 }
Tim Peters5ba58662001-07-16 02:29:45 +0000611 cf.cf_flags = 0;
612 if (PyEval_MergeCompilerFlags(&cf))
Tim Peters748b8bb2001-04-28 08:20:22 +0000613 res = PyRun_FileExFlags(fp, filename, Py_file_input, globals,
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000614 locals, 1, &cf);
Tim Peters5ba58662001-07-16 02:29:45 +0000615 else
Tim Peters748b8bb2001-04-28 08:20:22 +0000616 res = PyRun_FileEx(fp, filename, Py_file_input, globals,
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000617 locals, 1);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000618 return res;
Guido van Rossum0f61f8a1992-02-25 18:55:05 +0000619}
620
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000621PyDoc_STRVAR(execfile_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000622"execfile(filename[, globals[, locals]])\n\
623\n\
624Read and execute a Python script from a file.\n\
625The globals and locals are dictionaries, defaulting to the current\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000626globals and locals. If only globals is given, locals defaults to it.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000627
628
Guido van Rossum79f25d91997-04-29 20:08:16 +0000629static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000630builtin_getattr(PyObject *self, PyObject *args)
Guido van Rossum33894be1992-01-27 16:53:09 +0000631{
Guido van Rossum950ff291998-06-29 13:38:57 +0000632 PyObject *v, *result, *dflt = NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000633 PyObject *name;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000634
Raymond Hettingerea3fdf42002-12-29 16:33:45 +0000635 if (!PyArg_UnpackTuple(args, "getattr", 2, 3, &v, &name, &dflt))
Guido van Rossum33894be1992-01-27 16:53:09 +0000636 return NULL;
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000637#ifdef Py_USING_UNICODE
Jeremy Hylton0eb11152001-07-30 22:39:31 +0000638 if (PyUnicode_Check(name)) {
639 name = _PyUnicode_AsDefaultEncodedString(name, NULL);
640 if (name == NULL)
641 return NULL;
642 }
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000643#endif
Jeremy Hylton0eb11152001-07-30 22:39:31 +0000644
645 if (!PyString_Check(name)) {
646 PyErr_SetString(PyExc_TypeError,
Alex Martellia9b9c9f2003-04-23 13:34:35 +0000647 "getattr(): attribute name must be string");
Jeremy Hylton0eb11152001-07-30 22:39:31 +0000648 return NULL;
649 }
Guido van Rossum950ff291998-06-29 13:38:57 +0000650 result = PyObject_GetAttr(v, name);
Guido van Rossumd8923572001-10-16 21:31:32 +0000651 if (result == NULL && dflt != NULL &&
652 PyErr_ExceptionMatches(PyExc_AttributeError))
653 {
Guido van Rossum950ff291998-06-29 13:38:57 +0000654 PyErr_Clear();
655 Py_INCREF(dflt);
656 result = dflt;
657 }
658 return result;
Guido van Rossum9bfef441993-03-29 10:43:31 +0000659}
660
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000661PyDoc_STRVAR(getattr_doc,
Guido van Rossum950ff291998-06-29 13:38:57 +0000662"getattr(object, name[, default]) -> value\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000663\n\
Guido van Rossum950ff291998-06-29 13:38:57 +0000664Get a named attribute from an object; getattr(x, 'y') is equivalent to x.y.\n\
665When a default argument is given, it is returned when the attribute doesn't\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000666exist; without it, an exception is raised in that case.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000667
668
Guido van Rossum79f25d91997-04-29 20:08:16 +0000669static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000670builtin_globals(PyObject *self)
Guido van Rossum872537c1995-07-07 22:43:42 +0000671{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000672 PyObject *d;
Guido van Rossum872537c1995-07-07 22:43:42 +0000673
Guido van Rossum79f25d91997-04-29 20:08:16 +0000674 d = PyEval_GetGlobals();
675 Py_INCREF(d);
Guido van Rossum872537c1995-07-07 22:43:42 +0000676 return d;
677}
678
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000679PyDoc_STRVAR(globals_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000680"globals() -> dictionary\n\
681\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000682Return the dictionary containing the current scope's global variables.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000683
684
Guido van Rossum79f25d91997-04-29 20:08:16 +0000685static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000686builtin_hasattr(PyObject *self, PyObject *args)
Guido van Rossum9bfef441993-03-29 10:43:31 +0000687{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000688 PyObject *v;
689 PyObject *name;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000690
Raymond Hettingerea3fdf42002-12-29 16:33:45 +0000691 if (!PyArg_UnpackTuple(args, "hasattr", 2, 2, &v, &name))
Guido van Rossum9bfef441993-03-29 10:43:31 +0000692 return NULL;
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000693#ifdef Py_USING_UNICODE
Jeremy Hylton302b54a2001-07-30 22:45:19 +0000694 if (PyUnicode_Check(name)) {
695 name = _PyUnicode_AsDefaultEncodedString(name, NULL);
696 if (name == NULL)
697 return NULL;
698 }
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000699#endif
Jeremy Hylton302b54a2001-07-30 22:45:19 +0000700
701 if (!PyString_Check(name)) {
702 PyErr_SetString(PyExc_TypeError,
Alex Martellia9b9c9f2003-04-23 13:34:35 +0000703 "hasattr(): attribute name must be string");
Jeremy Hylton302b54a2001-07-30 22:45:19 +0000704 return NULL;
705 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000706 v = PyObject_GetAttr(v, name);
Guido van Rossum9bfef441993-03-29 10:43:31 +0000707 if (v == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000708 PyErr_Clear();
Guido van Rossum09df08a1998-05-22 00:51:39 +0000709 Py_INCREF(Py_False);
710 return Py_False;
Guido van Rossum9bfef441993-03-29 10:43:31 +0000711 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000712 Py_DECREF(v);
Guido van Rossum09df08a1998-05-22 00:51:39 +0000713 Py_INCREF(Py_True);
714 return Py_True;
Guido van Rossum33894be1992-01-27 16:53:09 +0000715}
716
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000717PyDoc_STRVAR(hasattr_doc,
Guido van Rossum77f6a652002-04-03 22:41:51 +0000718"hasattr(object, name) -> bool\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000719\n\
720Return whether the object has an attribute with the given name.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000721(This is done by calling getattr(object, name) and catching exceptions.)");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000722
723
Guido van Rossum79f25d91997-04-29 20:08:16 +0000724static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000725builtin_id(PyObject *self, PyObject *v)
Guido van Rossum5b722181993-03-30 17:46:03 +0000726{
Guido van Rossum106f2da2000-06-28 21:12:25 +0000727 return PyLong_FromVoidPtr(v);
Guido van Rossum5b722181993-03-30 17:46:03 +0000728}
729
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000730PyDoc_STRVAR(id_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000731"id(object) -> integer\n\
732\n\
733Return the identity of an object. This is guaranteed to be unique among\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000734simultaneously existing objects. (Hint: it's the object's memory address.)");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000735
736
Guido van Rossum79f25d91997-04-29 20:08:16 +0000737static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000738builtin_map(PyObject *self, PyObject *args)
Guido van Rossum12d12c51993-10-26 17:58:25 +0000739{
740 typedef struct {
Tim Peters4e9afdc2001-05-03 23:54:49 +0000741 PyObject *it; /* the iterator object */
742 int saw_StopIteration; /* bool: did the iterator end? */
Guido van Rossum12d12c51993-10-26 17:58:25 +0000743 } sequence;
744
Guido van Rossum79f25d91997-04-29 20:08:16 +0000745 PyObject *func, *result;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000746 sequence *seqs = NULL, *sqp;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000747 int n, len;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000748 register int i, j;
749
Guido van Rossum79f25d91997-04-29 20:08:16 +0000750 n = PyTuple_Size(args);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000751 if (n < 2) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000752 PyErr_SetString(PyExc_TypeError,
753 "map() requires at least two args");
Guido van Rossum12d12c51993-10-26 17:58:25 +0000754 return NULL;
755 }
756
Guido van Rossum79f25d91997-04-29 20:08:16 +0000757 func = PyTuple_GetItem(args, 0);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000758 n--;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000759
Guido van Rossumfa4ac711998-07-10 17:37:30 +0000760 if (func == Py_None && n == 1) {
761 /* map(None, S) is the same as list(S). */
762 return PySequence_List(PyTuple_GetItem(args, 1));
763 }
764
Tim Peters4e9afdc2001-05-03 23:54:49 +0000765 /* Get space for sequence descriptors. Must NULL out the iterator
766 * pointers so that jumping to Fail_2 later doesn't see trash.
767 */
Guido van Rossum79f25d91997-04-29 20:08:16 +0000768 if ((seqs = PyMem_NEW(sequence, n)) == NULL) {
769 PyErr_NoMemory();
Tim Peters4e9afdc2001-05-03 23:54:49 +0000770 return NULL;
771 }
772 for (i = 0; i < n; ++i) {
773 seqs[i].it = (PyObject*)NULL;
774 seqs[i].saw_StopIteration = 0;
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000775 }
Guido van Rossum12d12c51993-10-26 17:58:25 +0000776
Tim Peters4e9afdc2001-05-03 23:54:49 +0000777 /* Do a first pass to obtain iterators for the arguments, and set len
778 * to the largest of their lengths.
Tim Peters748b8bb2001-04-28 08:20:22 +0000779 */
Tim Peters4e9afdc2001-05-03 23:54:49 +0000780 len = 0;
781 for (i = 0, sqp = seqs; i < n; ++i, ++sqp) {
782 PyObject *curseq;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000783 int curlen;
Guido van Rossumad991772001-01-12 16:03:05 +0000784
Tim Peters4e9afdc2001-05-03 23:54:49 +0000785 /* Get iterator. */
786 curseq = PyTuple_GetItem(args, i+1);
787 sqp->it = PyObject_GetIter(curseq);
788 if (sqp->it == NULL) {
Guido van Rossum12d12c51993-10-26 17:58:25 +0000789 static char errmsg[] =
Tim Peters4e9afdc2001-05-03 23:54:49 +0000790 "argument %d to map() must support iteration";
Guido van Rossum15e33a41997-04-30 19:00:27 +0000791 char errbuf[sizeof(errmsg) + 25];
Jeremy Hylton518ab1c2001-11-28 20:42:20 +0000792 PyOS_snprintf(errbuf, sizeof(errbuf), errmsg, i+2);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000793 PyErr_SetString(PyExc_TypeError, errbuf);
Guido van Rossum12d12c51993-10-26 17:58:25 +0000794 goto Fail_2;
795 }
796
Tim Peters4e9afdc2001-05-03 23:54:49 +0000797 /* Update len. */
798 curlen = -1; /* unknown */
799 if (PySequence_Check(curseq) &&
800 curseq->ob_type->tp_as_sequence->sq_length) {
801 curlen = PySequence_Size(curseq);
802 if (curlen < 0)
803 PyErr_Clear();
804 }
Tim Peters748b8bb2001-04-28 08:20:22 +0000805 if (curlen < 0)
Tim Peters4e9afdc2001-05-03 23:54:49 +0000806 curlen = 8; /* arbitrary */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000807 if (curlen > len)
808 len = curlen;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000809 }
810
Tim Peters4e9afdc2001-05-03 23:54:49 +0000811 /* Get space for the result list. */
Guido van Rossum79f25d91997-04-29 20:08:16 +0000812 if ((result = (PyObject *) PyList_New(len)) == NULL)
Guido van Rossum12d12c51993-10-26 17:58:25 +0000813 goto Fail_2;
814
Tim Peters4e9afdc2001-05-03 23:54:49 +0000815 /* Iterate over the sequences until all have stopped. */
Guido van Rossum2d951851994-08-29 12:52:16 +0000816 for (i = 0; ; ++i) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000817 PyObject *alist, *item=NULL, *value;
Tim Peters4e9afdc2001-05-03 23:54:49 +0000818 int numactive = 0;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000819
Guido van Rossum79f25d91997-04-29 20:08:16 +0000820 if (func == Py_None && n == 1)
Guido van Rossum32120311995-07-10 13:52:21 +0000821 alist = NULL;
Tim Peters4e9afdc2001-05-03 23:54:49 +0000822 else if ((alist = PyTuple_New(n)) == NULL)
823 goto Fail_1;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000824
825 for (j = 0, sqp = seqs; j < n; ++j, ++sqp) {
Tim Peters4e9afdc2001-05-03 23:54:49 +0000826 if (sqp->saw_StopIteration) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000827 Py_INCREF(Py_None);
828 item = Py_None;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000829 }
Guido van Rossum12d12c51993-10-26 17:58:25 +0000830 else {
Tim Peters4e9afdc2001-05-03 23:54:49 +0000831 item = PyIter_Next(sqp->it);
832 if (item)
833 ++numactive;
834 else {
Tim Peters4e9afdc2001-05-03 23:54:49 +0000835 if (PyErr_Occurred()) {
Tim Petersf4848da2001-05-05 00:14:56 +0000836 Py_XDECREF(alist);
837 goto Fail_1;
Guido van Rossum2d951851994-08-29 12:52:16 +0000838 }
Tim Peters4e9afdc2001-05-03 23:54:49 +0000839 Py_INCREF(Py_None);
840 item = Py_None;
841 sqp->saw_StopIteration = 1;
Guido van Rossum2d951851994-08-29 12:52:16 +0000842 }
Guido van Rossum12d12c51993-10-26 17:58:25 +0000843 }
Tim Peters4e9afdc2001-05-03 23:54:49 +0000844 if (alist)
845 PyTuple_SET_ITEM(alist, j, item);
846 else
Guido van Rossum2d951851994-08-29 12:52:16 +0000847 break;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000848 }
849
Guido van Rossum32120311995-07-10 13:52:21 +0000850 if (!alist)
851 alist = item;
Guido van Rossum2d951851994-08-29 12:52:16 +0000852
Tim Peters4e9afdc2001-05-03 23:54:49 +0000853 if (numactive == 0) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000854 Py_DECREF(alist);
Guido van Rossum2d951851994-08-29 12:52:16 +0000855 break;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000856 }
Guido van Rossum2d951851994-08-29 12:52:16 +0000857
Guido van Rossum79f25d91997-04-29 20:08:16 +0000858 if (func == Py_None)
Guido van Rossum32120311995-07-10 13:52:21 +0000859 value = alist;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000860 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000861 value = PyEval_CallObject(func, alist);
862 Py_DECREF(alist);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000863 if (value == NULL)
864 goto Fail_1;
Guido van Rossum2d951851994-08-29 12:52:16 +0000865 }
866 if (i >= len) {
Barry Warsawfa77e091999-01-28 18:49:12 +0000867 int status = PyList_Append(result, value);
Barry Warsaw21332871999-01-28 04:21:35 +0000868 Py_DECREF(value);
Barry Warsawfa77e091999-01-28 18:49:12 +0000869 if (status < 0)
870 goto Fail_1;
Guido van Rossum2d951851994-08-29 12:52:16 +0000871 }
Tim Peters4e9afdc2001-05-03 23:54:49 +0000872 else if (PyList_SetItem(result, i, value) < 0)
873 goto Fail_1;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000874 }
875
Guido van Rossumfa4ac711998-07-10 17:37:30 +0000876 if (i < len && PyList_SetSlice(result, i, len, NULL) < 0)
877 goto Fail_1;
878
Tim Peters4e9afdc2001-05-03 23:54:49 +0000879 goto Succeed;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000880
Guido van Rossum12d12c51993-10-26 17:58:25 +0000881Fail_1:
Guido van Rossum79f25d91997-04-29 20:08:16 +0000882 Py_DECREF(result);
Guido van Rossum12d12c51993-10-26 17:58:25 +0000883Fail_2:
Tim Peters4e9afdc2001-05-03 23:54:49 +0000884 result = NULL;
885Succeed:
886 assert(seqs);
887 for (i = 0; i < n; ++i)
888 Py_XDECREF(seqs[i].it);
889 PyMem_DEL(seqs);
890 return result;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000891}
892
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000893PyDoc_STRVAR(map_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000894"map(function, sequence[, sequence, ...]) -> list\n\
895\n\
896Return a list of the results of applying the function to the items of\n\
897the argument sequence(s). If more than one sequence is given, the\n\
898function is called with an argument list consisting of the corresponding\n\
899item of each sequence, substituting None for missing values when not all\n\
900sequences have the same length. If the function is None, return a list of\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000901the items of the sequence (or a list of tuples if more than one sequence).");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000902
903
Guido van Rossum79f25d91997-04-29 20:08:16 +0000904static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000905builtin_setattr(PyObject *self, PyObject *args)
Guido van Rossum33894be1992-01-27 16:53:09 +0000906{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000907 PyObject *v;
908 PyObject *name;
909 PyObject *value;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000910
Raymond Hettingerea3fdf42002-12-29 16:33:45 +0000911 if (!PyArg_UnpackTuple(args, "setattr", 3, 3, &v, &name, &value))
Guido van Rossum33894be1992-01-27 16:53:09 +0000912 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000913 if (PyObject_SetAttr(v, name, value) != 0)
Guido van Rossum33894be1992-01-27 16:53:09 +0000914 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000915 Py_INCREF(Py_None);
916 return Py_None;
Guido van Rossum33894be1992-01-27 16:53:09 +0000917}
918
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000919PyDoc_STRVAR(setattr_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000920"setattr(object, name, value)\n\
921\n\
922Set a named attribute on an object; setattr(x, 'y', v) is equivalent to\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000923``x.y = v''.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000924
925
Guido van Rossum79f25d91997-04-29 20:08:16 +0000926static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000927builtin_delattr(PyObject *self, PyObject *args)
Guido van Rossum14144fc1994-08-29 12:53:40 +0000928{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000929 PyObject *v;
930 PyObject *name;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000931
Raymond Hettingerea3fdf42002-12-29 16:33:45 +0000932 if (!PyArg_UnpackTuple(args, "delattr", 2, 2, &v, &name))
Guido van Rossum14144fc1994-08-29 12:53:40 +0000933 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000934 if (PyObject_SetAttr(v, name, (PyObject *)NULL) != 0)
Guido van Rossum14144fc1994-08-29 12:53:40 +0000935 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000936 Py_INCREF(Py_None);
937 return Py_None;
Guido van Rossum14144fc1994-08-29 12:53:40 +0000938}
939
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000940PyDoc_STRVAR(delattr_doc,
Guido van Rossumdf12a591998-11-23 22:13:04 +0000941"delattr(object, name)\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000942\n\
943Delete a named attribute on an object; delattr(x, 'y') is equivalent to\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000944``del x.y''.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000945
946
Guido van Rossum79f25d91997-04-29 20:08:16 +0000947static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000948builtin_hash(PyObject *self, PyObject *v)
Guido van Rossum9bfef441993-03-29 10:43:31 +0000949{
Guido van Rossum9bfef441993-03-29 10:43:31 +0000950 long x;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000951
Guido van Rossum79f25d91997-04-29 20:08:16 +0000952 x = PyObject_Hash(v);
Guido van Rossum9bfef441993-03-29 10:43:31 +0000953 if (x == -1)
954 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000955 return PyInt_FromLong(x);
Guido van Rossum9bfef441993-03-29 10:43:31 +0000956}
957
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000958PyDoc_STRVAR(hash_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000959"hash(object) -> integer\n\
960\n\
961Return a hash value for the object. Two objects with the same value have\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000962the same hash value. The reverse is not necessarily true, but likely.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000963
964
Guido van Rossum79f25d91997-04-29 20:08:16 +0000965static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000966builtin_hex(PyObject *self, PyObject *v)
Guido van Rossum006bcd41991-10-24 14:54:44 +0000967{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000968 PyNumberMethods *nb;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000969
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000970 if ((nb = v->ob_type->tp_as_number) == NULL ||
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000971 nb->nb_hex == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000972 PyErr_SetString(PyExc_TypeError,
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000973 "hex() argument can't be converted to hex");
974 return NULL;
Guido van Rossum006bcd41991-10-24 14:54:44 +0000975 }
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000976 return (*nb->nb_hex)(v);
Guido van Rossum006bcd41991-10-24 14:54:44 +0000977}
978
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000979PyDoc_STRVAR(hex_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000980"hex(number) -> string\n\
981\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000982Return the hexadecimal representation of an integer or long integer.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000983
984
Tim Petersdbd9ba62000-07-09 03:09:57 +0000985static PyObject *builtin_raw_input(PyObject *, PyObject *);
Guido van Rossum3165fe61992-09-25 21:59:05 +0000986
Guido van Rossum79f25d91997-04-29 20:08:16 +0000987static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000988builtin_input(PyObject *self, PyObject *args)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000989{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000990 PyObject *line;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000991 char *str;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000992 PyObject *res;
993 PyObject *globals, *locals;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000994
995 line = builtin_raw_input(self, args);
Guido van Rossum3165fe61992-09-25 21:59:05 +0000996 if (line == NULL)
997 return line;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000998 if (!PyArg_Parse(line, "s;embedded '\\0' in input line", &str))
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000999 return NULL;
1000 while (*str == ' ' || *str == '\t')
1001 str++;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001002 globals = PyEval_GetGlobals();
1003 locals = PyEval_GetLocals();
1004 if (PyDict_GetItemString(globals, "__builtins__") == NULL) {
1005 if (PyDict_SetItemString(globals, "__builtins__",
1006 PyEval_GetBuiltins()) != 0)
Guido van Rossum6135a871995-01-09 17:53:26 +00001007 return NULL;
1008 }
Guido van Rossumb05a5c71997-05-07 17:46:13 +00001009 res = PyRun_String(str, Py_eval_input, globals, locals);
Guido van Rossum79f25d91997-04-29 20:08:16 +00001010 Py_DECREF(line);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001011 return res;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001012}
1013
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001014PyDoc_STRVAR(input_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001015"input([prompt]) -> value\n\
1016\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001017Equivalent to eval(raw_input(prompt)).");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001018
1019
Guido van Rossume8811f81997-02-14 15:48:05 +00001020static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001021builtin_intern(PyObject *self, PyObject *args)
Guido van Rossume8811f81997-02-14 15:48:05 +00001022{
1023 PyObject *s;
Guido van Rossum43713e52000-02-29 13:59:29 +00001024 if (!PyArg_ParseTuple(args, "S:intern", &s))
Guido van Rossume8811f81997-02-14 15:48:05 +00001025 return NULL;
1026 Py_INCREF(s);
1027 PyString_InternInPlace(&s);
1028 return s;
1029}
1030
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001031PyDoc_STRVAR(intern_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001032"intern(string) -> string\n\
1033\n\
1034``Intern'' the given string. This enters the string in the (global)\n\
1035table of interned strings whose purpose is to speed up dictionary lookups.\n\
1036Return the string itself or the previously interned string object with the\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001037same value.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001038
1039
Guido van Rossum79f25d91997-04-29 20:08:16 +00001040static PyObject *
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001041builtin_iter(PyObject *self, PyObject *args)
1042{
1043 PyObject *v, *w = NULL;
1044
Raymond Hettingerea3fdf42002-12-29 16:33:45 +00001045 if (!PyArg_UnpackTuple(args, "iter", 1, 2, &v, &w))
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001046 return NULL;
1047 if (w == NULL)
1048 return PyObject_GetIter(v);
1049 if (!PyCallable_Check(v)) {
1050 PyErr_SetString(PyExc_TypeError,
1051 "iter(v, w): v must be callable");
1052 return NULL;
1053 }
1054 return PyCallIter_New(v, w);
1055}
1056
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001057PyDoc_STRVAR(iter_doc,
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001058"iter(collection) -> iterator\n\
1059iter(callable, sentinel) -> iterator\n\
1060\n\
1061Get an iterator from an object. In the first form, the argument must\n\
1062supply its own iterator, or be a sequence.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001063In the second form, the callable is called until it returns the sentinel.");
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001064
1065
1066static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001067builtin_len(PyObject *self, PyObject *v)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001068{
Guido van Rossum8ea9f4d1998-06-29 22:26:50 +00001069 long res;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001070
Jeremy Hylton03657cf2000-07-12 13:05:33 +00001071 res = PyObject_Size(v);
Guido van Rossum8ea9f4d1998-06-29 22:26:50 +00001072 if (res < 0 && PyErr_Occurred())
1073 return NULL;
1074 return PyInt_FromLong(res);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001075}
1076
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001077PyDoc_STRVAR(len_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001078"len(object) -> integer\n\
1079\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001080Return the number of items of a sequence or mapping.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001081
1082
Guido van Rossum79f25d91997-04-29 20:08:16 +00001083static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001084builtin_locals(PyObject *self)
Guido van Rossum872537c1995-07-07 22:43:42 +00001085{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001086 PyObject *d;
Guido van Rossum872537c1995-07-07 22:43:42 +00001087
Guido van Rossum79f25d91997-04-29 20:08:16 +00001088 d = PyEval_GetLocals();
1089 Py_INCREF(d);
Guido van Rossum872537c1995-07-07 22:43:42 +00001090 return d;
1091}
1092
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001093PyDoc_STRVAR(locals_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001094"locals() -> dictionary\n\
1095\n\
Raymond Hettinger69bf8f32003-01-04 02:16:22 +00001096Update and return a dictionary containing the current scope's local variables.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001097
1098
Guido van Rossum79f25d91997-04-29 20:08:16 +00001099static PyObject *
Guido van Rossum53451b32001-01-17 15:47:24 +00001100min_max(PyObject *args, int op)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001101{
Tim Petersc3074532001-05-03 07:00:32 +00001102 PyObject *v, *w, *x, *it;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001103
Guido van Rossum79f25d91997-04-29 20:08:16 +00001104 if (PyTuple_Size(args) > 1)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001105 v = args;
Raymond Hettingerbbfb9102002-12-29 18:31:19 +00001106 else if (!PyArg_UnpackTuple(args, (op==Py_LT) ? "min" : "max", 1, 1, &v))
Guido van Rossum3f5da241990-12-20 15:06:42 +00001107 return NULL;
Tim Peters67d687a2002-04-29 21:27:32 +00001108
Tim Petersc3074532001-05-03 07:00:32 +00001109 it = PyObject_GetIter(v);
1110 if (it == NULL)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001111 return NULL;
Tim Petersc3074532001-05-03 07:00:32 +00001112
1113 w = NULL; /* the result */
Tim Petersf4848da2001-05-05 00:14:56 +00001114 for (;;) {
Tim Petersc3074532001-05-03 07:00:32 +00001115 x = PyIter_Next(it);
Guido van Rossum2d951851994-08-29 12:52:16 +00001116 if (x == NULL) {
Tim Petersc3074532001-05-03 07:00:32 +00001117 if (PyErr_Occurred()) {
Tim Petersf4848da2001-05-05 00:14:56 +00001118 Py_XDECREF(w);
1119 Py_DECREF(it);
1120 return NULL;
Guido van Rossum2d951851994-08-29 12:52:16 +00001121 }
Tim Petersc3074532001-05-03 07:00:32 +00001122 break;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001123 }
Tim Petersc3074532001-05-03 07:00:32 +00001124
Guido van Rossum2d951851994-08-29 12:52:16 +00001125 if (w == NULL)
1126 w = x;
1127 else {
Guido van Rossum53451b32001-01-17 15:47:24 +00001128 int cmp = PyObject_RichCompareBool(x, w, op);
1129 if (cmp > 0) {
1130 Py_DECREF(w);
1131 w = x;
1132 }
1133 else if (cmp < 0) {
Guido van Rossumc8b6df91997-05-23 00:06:51 +00001134 Py_DECREF(x);
Tim Petersc3074532001-05-03 07:00:32 +00001135 Py_DECREF(w);
1136 Py_DECREF(it);
Guido van Rossumc8b6df91997-05-23 00:06:51 +00001137 return NULL;
1138 }
Guido van Rossum2d951851994-08-29 12:52:16 +00001139 else
Guido van Rossum79f25d91997-04-29 20:08:16 +00001140 Py_DECREF(x);
Guido van Rossum2d951851994-08-29 12:52:16 +00001141 }
Guido van Rossum3f5da241990-12-20 15:06:42 +00001142 }
Guido van Rossum2d951851994-08-29 12:52:16 +00001143 if (w == NULL)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001144 PyErr_SetString(PyExc_ValueError,
Fred Drake661ea262000-10-24 19:57:45 +00001145 "min() or max() arg is an empty sequence");
Tim Petersc3074532001-05-03 07:00:32 +00001146 Py_DECREF(it);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001147 return w;
1148}
1149
Guido van Rossum79f25d91997-04-29 20:08:16 +00001150static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001151builtin_min(PyObject *self, PyObject *v)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001152{
Guido van Rossum53451b32001-01-17 15:47:24 +00001153 return min_max(v, Py_LT);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001154}
1155
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001156PyDoc_STRVAR(min_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001157"min(sequence) -> value\n\
1158min(a, b, c, ...) -> value\n\
1159\n\
1160With a single sequence argument, return its smallest item.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001161With two or more arguments, return the smallest argument.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001162
1163
Guido van Rossum79f25d91997-04-29 20:08:16 +00001164static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001165builtin_max(PyObject *self, PyObject *v)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001166{
Guido van Rossum53451b32001-01-17 15:47:24 +00001167 return min_max(v, Py_GT);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001168}
1169
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001170PyDoc_STRVAR(max_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001171"max(sequence) -> value\n\
1172max(a, b, c, ...) -> value\n\
1173\n\
1174With a single sequence argument, return its largest item.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001175With two or more arguments, return the largest argument.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001176
1177
Guido van Rossum79f25d91997-04-29 20:08:16 +00001178static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001179builtin_oct(PyObject *self, PyObject *v)
Guido van Rossum006bcd41991-10-24 14:54:44 +00001180{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001181 PyNumberMethods *nb;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001182
Guido van Rossum1899c2e1992-09-12 11:09:23 +00001183 if (v == NULL || (nb = v->ob_type->tp_as_number) == NULL ||
1184 nb->nb_oct == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001185 PyErr_SetString(PyExc_TypeError,
Guido van Rossum1899c2e1992-09-12 11:09:23 +00001186 "oct() argument can't be converted to oct");
1187 return NULL;
Guido van Rossum006bcd41991-10-24 14:54:44 +00001188 }
Guido van Rossum1899c2e1992-09-12 11:09:23 +00001189 return (*nb->nb_oct)(v);
Guido van Rossum006bcd41991-10-24 14:54:44 +00001190}
1191
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001192PyDoc_STRVAR(oct_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001193"oct(number) -> string\n\
1194\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001195Return the octal representation of an integer or long integer.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001196
1197
Guido van Rossum79f25d91997-04-29 20:08:16 +00001198static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001199builtin_ord(PyObject *self, PyObject* obj)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001200{
Guido van Rossum09095f32000-03-10 23:00:52 +00001201 long ord;
Jeremy Hylton394b54d2000-04-12 21:19:47 +00001202 int size;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001203
Jeremy Hylton394b54d2000-04-12 21:19:47 +00001204 if (PyString_Check(obj)) {
1205 size = PyString_GET_SIZE(obj);
Andrew M. Kuchling34c20cf2000-12-20 14:36:56 +00001206 if (size == 1) {
Jeremy Hylton394b54d2000-04-12 21:19:47 +00001207 ord = (long)((unsigned char)*PyString_AS_STRING(obj));
Andrew M. Kuchling34c20cf2000-12-20 14:36:56 +00001208 return PyInt_FromLong(ord);
1209 }
Martin v. Löwis339d0f72001-08-17 18:39:25 +00001210#ifdef Py_USING_UNICODE
Jeremy Hylton394b54d2000-04-12 21:19:47 +00001211 } else if (PyUnicode_Check(obj)) {
1212 size = PyUnicode_GET_SIZE(obj);
Andrew M. Kuchling34c20cf2000-12-20 14:36:56 +00001213 if (size == 1) {
Jeremy Hylton394b54d2000-04-12 21:19:47 +00001214 ord = (long)*PyUnicode_AS_UNICODE(obj);
Andrew M. Kuchling34c20cf2000-12-20 14:36:56 +00001215 return PyInt_FromLong(ord);
1216 }
Martin v. Löwis339d0f72001-08-17 18:39:25 +00001217#endif
Jeremy Hylton394b54d2000-04-12 21:19:47 +00001218 } else {
1219 PyErr_Format(PyExc_TypeError,
Andrew M. Kuchlingf07aad12000-12-23 14:11:28 +00001220 "ord() expected string of length 1, but " \
Jeremy Hylton394b54d2000-04-12 21:19:47 +00001221 "%.200s found", obj->ob_type->tp_name);
Guido van Rossum09095f32000-03-10 23:00:52 +00001222 return NULL;
1223 }
1224
Guido van Rossumad991772001-01-12 16:03:05 +00001225 PyErr_Format(PyExc_TypeError,
Andrew M. Kuchlingf07aad12000-12-23 14:11:28 +00001226 "ord() expected a character, "
1227 "but string of length %d found",
Jeremy Hylton394b54d2000-04-12 21:19:47 +00001228 size);
1229 return NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001230}
1231
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001232PyDoc_STRVAR(ord_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001233"ord(c) -> integer\n\
1234\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001235Return the integer ordinal of a one-character string.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001236
1237
Guido van Rossum79f25d91997-04-29 20:08:16 +00001238static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001239builtin_pow(PyObject *self, PyObject *args)
Guido van Rossum6a00cd81995-01-07 12:39:01 +00001240{
Guido van Rossum09df08a1998-05-22 00:51:39 +00001241 PyObject *v, *w, *z = Py_None;
Guido van Rossum6a00cd81995-01-07 12:39:01 +00001242
Raymond Hettingerea3fdf42002-12-29 16:33:45 +00001243 if (!PyArg_UnpackTuple(args, "pow", 2, 3, &v, &w, &z))
Guido van Rossum6a00cd81995-01-07 12:39:01 +00001244 return NULL;
Guido van Rossum09df08a1998-05-22 00:51:39 +00001245 return PyNumber_Power(v, w, z);
Guido van Rossumd4905451991-05-05 20:00:36 +00001246}
1247
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001248PyDoc_STRVAR(pow_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001249"pow(x, y[, z]) -> number\n\
1250\n\
1251With two arguments, equivalent to x**y. With three arguments,\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001252equivalent to (x**y) % z, but may be more efficient (e.g. for longs).");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001253
1254
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001255
1256/* Return number of items in range (lo, hi, step), when arguments are
1257 * PyInt or PyLong objects. step > 0 required. Return a value < 0 if
1258 * & only if the true value is too large to fit in a signed long.
1259 * Arguments MUST return 1 with either PyInt_Check() or
1260 * PyLong_Check(). Return -1 when there is an error.
1261 */
1262static long
1263get_len_of_range_longs(PyObject *lo, PyObject *hi, PyObject *step)
1264{
1265 /* -------------------------------------------------------------
1266 Algorithm is equal to that of get_len_of_range(), but it operates
1267 on PyObjects (which are assumed to be PyLong or PyInt objects).
1268 ---------------------------------------------------------------*/
1269 long n;
1270 PyObject *diff = NULL;
1271 PyObject *one = NULL;
1272 PyObject *tmp1 = NULL, *tmp2 = NULL, *tmp3 = NULL;
1273 /* holds sub-expression evaluations */
1274
1275 /* if (lo >= hi), return length of 0. */
1276 if (PyObject_Compare(lo, hi) >= 0)
1277 return 0;
1278
1279 if ((one = PyLong_FromLong(1L)) == NULL)
1280 goto Fail;
1281
1282 if ((tmp1 = PyNumber_Subtract(hi, lo)) == NULL)
1283 goto Fail;
1284
1285 if ((diff = PyNumber_Subtract(tmp1, one)) == NULL)
1286 goto Fail;
1287
1288 if ((tmp2 = PyNumber_FloorDivide(diff, step)) == NULL)
1289 goto Fail;
1290
1291 if ((tmp3 = PyNumber_Add(tmp2, one)) == NULL)
1292 goto Fail;
1293
1294 n = PyLong_AsLong(tmp3);
1295 if (PyErr_Occurred()) { /* Check for Overflow */
1296 PyErr_Clear();
1297 goto Fail;
1298 }
1299
1300 Py_DECREF(tmp3);
1301 Py_DECREF(tmp2);
1302 Py_DECREF(diff);
1303 Py_DECREF(tmp1);
1304 Py_DECREF(one);
1305 return n;
1306
1307 Fail:
1308 Py_XDECREF(tmp3);
1309 Py_XDECREF(tmp2);
1310 Py_XDECREF(diff);
1311 Py_XDECREF(tmp1);
1312 Py_XDECREF(one);
1313 return -1;
1314}
1315
1316/* An extension of builtin_range() that handles the case when PyLong
1317 * arguments are given. */
1318static PyObject *
1319handle_range_longs(PyObject *self, PyObject *args)
1320{
1321 PyObject *ilow;
Tim Peters874e1f72003-04-13 22:13:08 +00001322 PyObject *ihigh = NULL;
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001323 PyObject *istep = NULL;
Tim Peters874e1f72003-04-13 22:13:08 +00001324
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001325 PyObject *curnum = NULL;
1326 PyObject *v = NULL;
1327 long bign;
1328 int i, n;
1329 int cmp_result;
1330
Tim Peters874e1f72003-04-13 22:13:08 +00001331 PyObject *zero = PyLong_FromLong(0);
1332
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001333 if (zero == NULL)
1334 return NULL;
1335
Tim Peters874e1f72003-04-13 22:13:08 +00001336 if (!PyArg_UnpackTuple(args, "range", 1, 3, &ilow, &ihigh, &istep)) {
1337 Py_DECREF(zero);
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001338 return NULL;
1339 }
1340
Tim Peters874e1f72003-04-13 22:13:08 +00001341 /* Figure out which way we were called, supply defaults, and be
1342 * sure to incref everything so that the decrefs at the end
1343 * are correct.
1344 */
1345 assert(ilow != NULL);
1346 if (ihigh == NULL) {
1347 /* only 1 arg -- it's the upper limit */
1348 ihigh = ilow;
1349 ilow = NULL;
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001350 }
Tim Peters874e1f72003-04-13 22:13:08 +00001351 assert(ihigh != NULL);
1352 Py_INCREF(ihigh);
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001353
Tim Peters874e1f72003-04-13 22:13:08 +00001354 /* ihigh correct now; do ilow */
1355 if (ilow == NULL)
1356 ilow = zero;
1357 Py_INCREF(ilow);
1358
1359 /* ilow and ihigh correct now; do istep */
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001360 if (istep == NULL) {
1361 istep = PyLong_FromLong(1L);
1362 if (istep == NULL)
1363 goto Fail;
1364 }
1365 else {
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001366 Py_INCREF(istep);
1367 }
1368
Tim Peters874e1f72003-04-13 22:13:08 +00001369 if (!PyInt_Check(ilow) && !PyLong_Check(ilow)) {
Guido van Rossum28e83e32003-04-15 12:43:26 +00001370 PyErr_Format(PyExc_TypeError,
Alex Martellif471d472003-04-23 13:00:44 +00001371 "range() integer start argument expected, got %s.",
Guido van Rossum817d6c92003-04-14 18:25:04 +00001372 ilow->ob_type->tp_name);
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001373 goto Fail;
1374 }
1375
Tim Peters874e1f72003-04-13 22:13:08 +00001376 if (!PyInt_Check(ihigh) && !PyLong_Check(ihigh)) {
Guido van Rossum28e83e32003-04-15 12:43:26 +00001377 PyErr_Format(PyExc_TypeError,
Alex Martellif471d472003-04-23 13:00:44 +00001378 "range() integer end argument expected, got %s.",
Guido van Rossum817d6c92003-04-14 18:25:04 +00001379 ihigh->ob_type->tp_name);
Tim Peters874e1f72003-04-13 22:13:08 +00001380 goto Fail;
1381 }
1382
1383 if (!PyInt_Check(istep) && !PyLong_Check(istep)) {
Guido van Rossum28e83e32003-04-15 12:43:26 +00001384 PyErr_Format(PyExc_TypeError,
Alex Martellif471d472003-04-23 13:00:44 +00001385 "range() integer step argument expected, got %s.",
Guido van Rossum817d6c92003-04-14 18:25:04 +00001386 istep->ob_type->tp_name);
Tim Peters874e1f72003-04-13 22:13:08 +00001387 goto Fail;
1388 }
1389
1390 if (PyObject_Cmp(istep, zero, &cmp_result) == -1)
1391 goto Fail;
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001392 if (cmp_result == 0) {
1393 PyErr_SetString(PyExc_ValueError,
Alex Martellif471d472003-04-23 13:00:44 +00001394 "range() step argument must not be zero");
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001395 goto Fail;
1396 }
1397
1398 if (cmp_result > 0)
1399 bign = get_len_of_range_longs(ilow, ihigh, istep);
1400 else {
1401 PyObject *neg_istep = PyNumber_Negative(istep);
1402 if (neg_istep == NULL)
1403 goto Fail;
1404 bign = get_len_of_range_longs(ihigh, ilow, neg_istep);
1405 Py_DECREF(neg_istep);
1406 }
1407
1408 n = (int)bign;
1409 if (bign < 0 || (long)n != bign) {
1410 PyErr_SetString(PyExc_OverflowError,
1411 "range() result has too many items");
1412 goto Fail;
1413 }
1414
1415 v = PyList_New(n);
1416 if (v == NULL)
1417 goto Fail;
1418
1419 curnum = ilow;
1420 Py_INCREF(curnum);
1421
1422 for (i = 0; i < n; i++) {
1423 PyObject *w = PyNumber_Long(curnum);
1424 PyObject *tmp_num;
1425 if (w == NULL)
1426 goto Fail;
1427
1428 PyList_SET_ITEM(v, i, w);
1429
1430 tmp_num = PyNumber_Add(curnum, istep);
1431 if (tmp_num == NULL)
1432 goto Fail;
1433
1434 Py_DECREF(curnum);
1435 curnum = tmp_num;
1436 }
Tim Peters874e1f72003-04-13 22:13:08 +00001437 Py_DECREF(ilow);
1438 Py_DECREF(ihigh);
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001439 Py_DECREF(istep);
1440 Py_DECREF(zero);
Tim Peters874e1f72003-04-13 22:13:08 +00001441 Py_DECREF(curnum);
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001442 return v;
1443
1444 Fail:
Tim Peters874e1f72003-04-13 22:13:08 +00001445 Py_DECREF(ilow);
1446 Py_DECREF(ihigh);
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001447 Py_XDECREF(istep);
Tim Peters874e1f72003-04-13 22:13:08 +00001448 Py_DECREF(zero);
1449 Py_XDECREF(curnum);
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001450 Py_XDECREF(v);
1451 return NULL;
1452}
1453
Guido van Rossum124eff01999-02-23 16:11:01 +00001454/* Return number of items in range/xrange (lo, hi, step). step > 0
1455 * required. Return a value < 0 if & only if the true value is too
1456 * large to fit in a signed long.
1457 */
1458static long
Thomas Wouterse28c2962000-07-23 22:21:32 +00001459get_len_of_range(long lo, long hi, long step)
Guido van Rossum124eff01999-02-23 16:11:01 +00001460{
1461 /* -------------------------------------------------------------
1462 If lo >= hi, the range is empty.
1463 Else if n values are in the range, the last one is
1464 lo + (n-1)*step, which must be <= hi-1. Rearranging,
1465 n <= (hi - lo - 1)/step + 1, so taking the floor of the RHS gives
1466 the proper value. Since lo < hi in this case, hi-lo-1 >= 0, so
1467 the RHS is non-negative and so truncation is the same as the
1468 floor. Letting M be the largest positive long, the worst case
1469 for the RHS numerator is hi=M, lo=-M-1, and then
1470 hi-lo-1 = M-(-M-1)-1 = 2*M. Therefore unsigned long has enough
1471 precision to compute the RHS exactly.
1472 ---------------------------------------------------------------*/
1473 long n = 0;
1474 if (lo < hi) {
1475 unsigned long uhi = (unsigned long)hi;
1476 unsigned long ulo = (unsigned long)lo;
1477 unsigned long diff = uhi - ulo - 1;
1478 n = (long)(diff / (unsigned long)step + 1);
1479 }
1480 return n;
1481}
1482
Guido van Rossum79f25d91997-04-29 20:08:16 +00001483static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001484builtin_range(PyObject *self, PyObject *args)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001485{
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001486 long ilow = 0, ihigh = 0, istep = 1;
Guido van Rossum124eff01999-02-23 16:11:01 +00001487 long bign;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001488 int i, n;
Guido van Rossum124eff01999-02-23 16:11:01 +00001489
Guido van Rossum79f25d91997-04-29 20:08:16 +00001490 PyObject *v;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001491
Guido van Rossum79f25d91997-04-29 20:08:16 +00001492 if (PyTuple_Size(args) <= 1) {
1493 if (!PyArg_ParseTuple(args,
Guido van Rossum0865dd91995-01-17 16:30:22 +00001494 "l;range() requires 1-3 int arguments",
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001495 &ihigh)) {
1496 PyErr_Clear();
1497 return handle_range_longs(self, args);
1498 }
Guido van Rossum3f5da241990-12-20 15:06:42 +00001499 }
1500 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001501 if (!PyArg_ParseTuple(args,
Guido van Rossum0865dd91995-01-17 16:30:22 +00001502 "ll|l;range() requires 1-3 int arguments",
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001503 &ilow, &ihigh, &istep)) {
1504 PyErr_Clear();
1505 return handle_range_longs(self, args);
1506 }
Guido van Rossum3f5da241990-12-20 15:06:42 +00001507 }
1508 if (istep == 0) {
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001509 PyErr_SetString(PyExc_ValueError,
Alex Martellif471d472003-04-23 13:00:44 +00001510 "range() step argument must not be zero");
Guido van Rossum3f5da241990-12-20 15:06:42 +00001511 return NULL;
1512 }
Guido van Rossum124eff01999-02-23 16:11:01 +00001513 if (istep > 0)
1514 bign = get_len_of_range(ilow, ihigh, istep);
1515 else
1516 bign = get_len_of_range(ihigh, ilow, -istep);
1517 n = (int)bign;
1518 if (bign < 0 || (long)n != bign) {
Guido van Rossume23cde21999-01-12 05:07:47 +00001519 PyErr_SetString(PyExc_OverflowError,
Fred Drake661ea262000-10-24 19:57:45 +00001520 "range() result has too many items");
Guido van Rossume23cde21999-01-12 05:07:47 +00001521 return NULL;
1522 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00001523 v = PyList_New(n);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001524 if (v == NULL)
1525 return NULL;
1526 for (i = 0; i < n; i++) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001527 PyObject *w = PyInt_FromLong(ilow);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001528 if (w == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001529 Py_DECREF(v);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001530 return NULL;
1531 }
Guido van Rossuma937d141998-04-24 18:22:02 +00001532 PyList_SET_ITEM(v, i, w);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001533 ilow += istep;
1534 }
1535 return v;
1536}
1537
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001538PyDoc_STRVAR(range_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001539"range([start,] stop[, step]) -> list of integers\n\
1540\n\
1541Return a list containing an arithmetic progression of integers.\n\
1542range(i, j) returns [i, i+1, i+2, ..., j-1]; start (!) defaults to 0.\n\
1543When step is given, it specifies the increment (or decrement).\n\
1544For example, range(4) returns [0, 1, 2, 3]. The end point is omitted!\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001545These are exactly the valid indices for a list of 4 elements.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001546
1547
Guido van Rossum79f25d91997-04-29 20:08:16 +00001548static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001549builtin_raw_input(PyObject *self, PyObject *args)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001550{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001551 PyObject *v = NULL;
Martin v. Löwis31d2df52002-08-14 15:46:02 +00001552 PyObject *fin = PySys_GetObject("stdin");
1553 PyObject *fout = PySys_GetObject("stdout");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001554
Raymond Hettingerea3fdf42002-12-29 16:33:45 +00001555 if (!PyArg_UnpackTuple(args, "[raw_]input", 0, 1, &v))
Guido van Rossum3165fe61992-09-25 21:59:05 +00001556 return NULL;
Martin v. Löwis31d2df52002-08-14 15:46:02 +00001557
1558 if (fin == NULL) {
Alex Martellia9b9c9f2003-04-23 13:34:35 +00001559 PyErr_SetString(PyExc_RuntimeError, "[raw_]input: lost sys.stdin");
Martin v. Löwis31d2df52002-08-14 15:46:02 +00001560 return NULL;
1561 }
1562 if (fout == NULL) {
Alex Martellia9b9c9f2003-04-23 13:34:35 +00001563 PyErr_SetString(PyExc_RuntimeError, "[raw_]input: lost sys.stdout");
Martin v. Löwis31d2df52002-08-14 15:46:02 +00001564 return NULL;
1565 }
1566 if (PyFile_SoftSpace(fout, 0)) {
1567 if (PyFile_WriteString(" ", fout) != 0)
1568 return NULL;
1569 }
Martin v. Löwis566f6af2002-10-26 14:39:10 +00001570 if (PyFile_Check (fin) && PyFile_Check (fout)
1571 && isatty(fileno(PyFile_AsFile(fin)))
1572 && isatty(fileno(PyFile_AsFile(fout)))) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001573 PyObject *po;
Guido van Rossum872537c1995-07-07 22:43:42 +00001574 char *prompt;
1575 char *s;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001576 PyObject *result;
Guido van Rossum872537c1995-07-07 22:43:42 +00001577 if (v != NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001578 po = PyObject_Str(v);
Guido van Rossum872537c1995-07-07 22:43:42 +00001579 if (po == NULL)
1580 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001581 prompt = PyString_AsString(po);
Guido van Rossumd9b52081998-06-26 18:25:38 +00001582 if (prompt == NULL)
1583 return NULL;
Guido van Rossum872537c1995-07-07 22:43:42 +00001584 }
1585 else {
1586 po = NULL;
1587 prompt = "";
1588 }
Martin v. Löwis566f6af2002-10-26 14:39:10 +00001589 s = PyOS_Readline(PyFile_AsFile (fin), PyFile_AsFile (fout),
1590 prompt);
Guido van Rossum79f25d91997-04-29 20:08:16 +00001591 Py_XDECREF(po);
Guido van Rossum872537c1995-07-07 22:43:42 +00001592 if (s == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001593 PyErr_SetNone(PyExc_KeyboardInterrupt);
Guido van Rossum872537c1995-07-07 22:43:42 +00001594 return NULL;
1595 }
1596 if (*s == '\0') {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001597 PyErr_SetNone(PyExc_EOFError);
Guido van Rossum872537c1995-07-07 22:43:42 +00001598 result = NULL;
1599 }
1600 else { /* strip trailing '\n' */
Guido van Rossum106f2da2000-06-28 21:12:25 +00001601 size_t len = strlen(s);
1602 if (len > INT_MAX) {
Martin v. Löwis31d2df52002-08-14 15:46:02 +00001603 PyErr_SetString(PyExc_OverflowError,
Alex Martellia9b9c9f2003-04-23 13:34:35 +00001604 "[raw_]input: input too long");
Guido van Rossum106f2da2000-06-28 21:12:25 +00001605 result = NULL;
1606 }
1607 else {
Martin v. Löwis31d2df52002-08-14 15:46:02 +00001608 result = PyString_FromStringAndSize(s,
1609 (int)(len-1));
Guido van Rossum106f2da2000-06-28 21:12:25 +00001610 }
Guido van Rossum872537c1995-07-07 22:43:42 +00001611 }
Guido van Rossumb18618d2000-05-03 23:44:39 +00001612 PyMem_FREE(s);
Guido van Rossum872537c1995-07-07 22:43:42 +00001613 return result;
1614 }
Guido van Rossum90933611991-06-07 16:10:43 +00001615 if (v != NULL) {
Martin v. Löwis31d2df52002-08-14 15:46:02 +00001616 if (PyFile_WriteObject(v, fout, Py_PRINT_RAW) != 0)
Guido van Rossum90933611991-06-07 16:10:43 +00001617 return NULL;
1618 }
Martin v. Löwis31d2df52002-08-14 15:46:02 +00001619 return PyFile_GetLine(fin, -1);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001620}
1621
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001622PyDoc_STRVAR(raw_input_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001623"raw_input([prompt]) -> string\n\
1624\n\
1625Read a string from standard input. The trailing newline is stripped.\n\
1626If the user hits EOF (Unix: Ctl-D, Windows: Ctl-Z+Return), raise EOFError.\n\
1627On Unix, GNU readline is used if enabled. The prompt string, if given,\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001628is printed without a trailing newline before reading.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001629
1630
Guido van Rossum79f25d91997-04-29 20:08:16 +00001631static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001632builtin_reduce(PyObject *self, PyObject *args)
Guido van Rossum12d12c51993-10-26 17:58:25 +00001633{
Tim Peters15d81ef2001-05-04 04:39:21 +00001634 PyObject *seq, *func, *result = NULL, *it;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001635
Raymond Hettingerea3fdf42002-12-29 16:33:45 +00001636 if (!PyArg_UnpackTuple(args, "reduce", 2, 3, &func, &seq, &result))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001637 return NULL;
1638 if (result != NULL)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001639 Py_INCREF(result);
Guido van Rossum12d12c51993-10-26 17:58:25 +00001640
Tim Peters15d81ef2001-05-04 04:39:21 +00001641 it = PyObject_GetIter(seq);
1642 if (it == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001643 PyErr_SetString(PyExc_TypeError,
Tim Peters15d81ef2001-05-04 04:39:21 +00001644 "reduce() arg 2 must support iteration");
1645 Py_XDECREF(result);
Guido van Rossum12d12c51993-10-26 17:58:25 +00001646 return NULL;
1647 }
1648
Guido van Rossum79f25d91997-04-29 20:08:16 +00001649 if ((args = PyTuple_New(2)) == NULL)
Guido van Rossum2d951851994-08-29 12:52:16 +00001650 goto Fail;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001651
Tim Peters15d81ef2001-05-04 04:39:21 +00001652 for (;;) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001653 PyObject *op2;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001654
1655 if (args->ob_refcnt > 1) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001656 Py_DECREF(args);
1657 if ((args = PyTuple_New(2)) == NULL)
Guido van Rossum2d951851994-08-29 12:52:16 +00001658 goto Fail;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001659 }
1660
Tim Peters15d81ef2001-05-04 04:39:21 +00001661 op2 = PyIter_Next(it);
1662 if (op2 == NULL) {
Tim Petersf4848da2001-05-05 00:14:56 +00001663 if (PyErr_Occurred())
1664 goto Fail;
1665 break;
Guido van Rossum2d951851994-08-29 12:52:16 +00001666 }
Guido van Rossum12d12c51993-10-26 17:58:25 +00001667
Guido van Rossum2d951851994-08-29 12:52:16 +00001668 if (result == NULL)
1669 result = op2;
1670 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001671 PyTuple_SetItem(args, 0, result);
1672 PyTuple_SetItem(args, 1, op2);
1673 if ((result = PyEval_CallObject(func, args)) == NULL)
Guido van Rossum2d951851994-08-29 12:52:16 +00001674 goto Fail;
1675 }
Guido van Rossum12d12c51993-10-26 17:58:25 +00001676 }
1677
Guido van Rossum79f25d91997-04-29 20:08:16 +00001678 Py_DECREF(args);
Guido van Rossum12d12c51993-10-26 17:58:25 +00001679
Guido van Rossum2d951851994-08-29 12:52:16 +00001680 if (result == NULL)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001681 PyErr_SetString(PyExc_TypeError,
Fred Drake661ea262000-10-24 19:57:45 +00001682 "reduce() of empty sequence with no initial value");
Guido van Rossum2d951851994-08-29 12:52:16 +00001683
Tim Peters15d81ef2001-05-04 04:39:21 +00001684 Py_DECREF(it);
Guido van Rossum12d12c51993-10-26 17:58:25 +00001685 return result;
1686
Guido van Rossum2d951851994-08-29 12:52:16 +00001687Fail:
Guido van Rossum79f25d91997-04-29 20:08:16 +00001688 Py_XDECREF(args);
1689 Py_XDECREF(result);
Tim Peters15d81ef2001-05-04 04:39:21 +00001690 Py_DECREF(it);
Guido van Rossum12d12c51993-10-26 17:58:25 +00001691 return NULL;
1692}
1693
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001694PyDoc_STRVAR(reduce_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001695"reduce(function, sequence[, initial]) -> value\n\
1696\n\
1697Apply a function of two arguments cumulatively to the items of a sequence,\n\
1698from left to right, so as to reduce the sequence to a single value.\n\
1699For example, reduce(lambda x, y: x+y, [1, 2, 3, 4, 5]) calculates\n\
1700((((1+2)+3)+4)+5). If initial is present, it is placed before the items\n\
1701of the sequence in the calculation, and serves as a default when the\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001702sequence is empty.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001703
1704
Guido van Rossum79f25d91997-04-29 20:08:16 +00001705static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001706builtin_reload(PyObject *self, PyObject *v)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001707{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001708 return PyImport_ReloadModule(v);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001709}
1710
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001711PyDoc_STRVAR(reload_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001712"reload(module) -> module\n\
1713\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001714Reload the module. The module must have been successfully imported before.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001715
1716
Guido van Rossum79f25d91997-04-29 20:08:16 +00001717static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001718builtin_repr(PyObject *self, PyObject *v)
Guido van Rossumc89705d1992-11-26 08:54:07 +00001719{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001720 return PyObject_Repr(v);
Guido van Rossumc89705d1992-11-26 08:54:07 +00001721}
1722
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001723PyDoc_STRVAR(repr_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001724"repr(object) -> string\n\
1725\n\
1726Return the canonical string representation of the object.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001727For most object types, eval(repr(object)) == object.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001728
1729
Guido van Rossum79f25d91997-04-29 20:08:16 +00001730static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001731builtin_round(PyObject *self, PyObject *args)
Guido van Rossum9e51f9b1993-02-12 16:29:05 +00001732{
Guido van Rossum9e51f9b1993-02-12 16:29:05 +00001733 double x;
1734 double f;
1735 int ndigits = 0;
Guido van Rossum9e51f9b1993-02-12 16:29:05 +00001736 int i;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001737
Guido van Rossum79f25d91997-04-29 20:08:16 +00001738 if (!PyArg_ParseTuple(args, "d|i:round", &x, &ndigits))
Guido van Rossum9e51f9b1993-02-12 16:29:05 +00001739 return NULL;
Guido van Rossum9e51f9b1993-02-12 16:29:05 +00001740 f = 1.0;
Guido van Rossum1e162d31998-05-09 14:42:25 +00001741 i = abs(ndigits);
1742 while (--i >= 0)
Guido van Rossum9e51f9b1993-02-12 16:29:05 +00001743 f = f*10.0;
Guido van Rossum1e162d31998-05-09 14:42:25 +00001744 if (ndigits < 0)
1745 x /= f;
Guido van Rossum9e51f9b1993-02-12 16:29:05 +00001746 else
Guido van Rossum1e162d31998-05-09 14:42:25 +00001747 x *= f;
1748 if (x >= 0.0)
1749 x = floor(x + 0.5);
1750 else
1751 x = ceil(x - 0.5);
1752 if (ndigits < 0)
1753 x *= f;
1754 else
1755 x /= f;
1756 return PyFloat_FromDouble(x);
Guido van Rossum9e51f9b1993-02-12 16:29:05 +00001757}
1758
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001759PyDoc_STRVAR(round_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001760"round(number[, ndigits]) -> floating point number\n\
1761\n\
1762Round a number to a given precision in decimal digits (default 0 digits).\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001763This always returns a floating point number. Precision may be negative.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001764
1765
Guido van Rossum79f25d91997-04-29 20:08:16 +00001766static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001767builtin_vars(PyObject *self, PyObject *args)
Guido van Rossum2d951851994-08-29 12:52:16 +00001768{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001769 PyObject *v = NULL;
1770 PyObject *d;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001771
Raymond Hettingerea3fdf42002-12-29 16:33:45 +00001772 if (!PyArg_UnpackTuple(args, "vars", 0, 1, &v))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001773 return NULL;
Guido van Rossum2d951851994-08-29 12:52:16 +00001774 if (v == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001775 d = PyEval_GetLocals();
Guido van Rossum53bb7ff1995-07-26 16:26:31 +00001776 if (d == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001777 if (!PyErr_Occurred())
1778 PyErr_SetString(PyExc_SystemError,
Alex Martellia9b9c9f2003-04-23 13:34:35 +00001779 "vars(): no locals!?");
Guido van Rossum53bb7ff1995-07-26 16:26:31 +00001780 }
1781 else
Guido van Rossum79f25d91997-04-29 20:08:16 +00001782 Py_INCREF(d);
Guido van Rossum2d951851994-08-29 12:52:16 +00001783 }
1784 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001785 d = PyObject_GetAttrString(v, "__dict__");
Guido van Rossum2d951851994-08-29 12:52:16 +00001786 if (d == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001787 PyErr_SetString(PyExc_TypeError,
Guido van Rossum2d951851994-08-29 12:52:16 +00001788 "vars() argument must have __dict__ attribute");
1789 return NULL;
1790 }
1791 }
1792 return d;
1793}
1794
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001795PyDoc_STRVAR(vars_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001796"vars([object]) -> dictionary\n\
1797\n\
1798Without arguments, equivalent to locals().\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001799With an argument, equivalent to object.__dict__.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001800
Alex Martellia70b1912003-04-22 08:12:33 +00001801
1802static PyObject*
1803builtin_sum(PyObject *self, PyObject *args)
1804{
1805 PyObject *seq;
1806 PyObject *result = NULL;
1807 PyObject *temp, *item, *iter;
1808
1809 if (!PyArg_ParseTuple(args, "O|O:sum", &seq, &result))
1810 return NULL;
1811
1812 iter = PyObject_GetIter(seq);
1813 if (iter == NULL)
1814 return NULL;
1815
1816 if (result == NULL) {
1817 result = PyInt_FromLong(0);
1818 if (result == NULL) {
1819 Py_DECREF(iter);
1820 return NULL;
1821 }
1822 } else {
1823 /* reject string values for 'start' parameter */
1824 if (PyObject_TypeCheck(result, &PyBaseString_Type)) {
1825 PyErr_SetString(PyExc_TypeError,
Alex Martellia9b9c9f2003-04-23 13:34:35 +00001826 "sum() can't sum strings [use ''.join(seq) instead]");
Alex Martellia70b1912003-04-22 08:12:33 +00001827 Py_DECREF(iter);
1828 return NULL;
1829 }
Alex Martelli41c9f882003-04-22 09:24:48 +00001830 Py_INCREF(result);
Alex Martellia70b1912003-04-22 08:12:33 +00001831 }
1832
1833 for(;;) {
1834 item = PyIter_Next(iter);
1835 if (item == NULL) {
1836 /* error, or end-of-sequence */
1837 if (PyErr_Occurred()) {
1838 Py_DECREF(result);
1839 result = NULL;
1840 }
1841 break;
1842 }
1843 temp = PyNumber_Add(result, item);
1844 Py_DECREF(result);
1845 Py_DECREF(item);
1846 result = temp;
1847 if (result == NULL)
1848 break;
1849 }
1850 Py_DECREF(iter);
1851 return result;
1852}
1853
1854PyDoc_STRVAR(sum_doc,
1855"sum(sequence, start=0) -> value\n\
1856\n\
1857Returns the sum of a sequence of numbers (NOT strings) plus the value\n\
1858of parameter 'start'. When the sequence is empty, returns start.");
1859
1860
Barry Warsawcde8b1b1997-08-22 21:14:38 +00001861static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001862builtin_isinstance(PyObject *self, PyObject *args)
Barry Warsawcde8b1b1997-08-22 21:14:38 +00001863{
1864 PyObject *inst;
1865 PyObject *cls;
Guido van Rossum823649d2001-03-21 18:40:58 +00001866 int retval;
Barry Warsawcde8b1b1997-08-22 21:14:38 +00001867
Raymond Hettingerea3fdf42002-12-29 16:33:45 +00001868 if (!PyArg_UnpackTuple(args, "isinstance", 2, 2, &inst, &cls))
Barry Warsawcde8b1b1997-08-22 21:14:38 +00001869 return NULL;
Guido van Rossumf5dd9141997-12-02 19:11:45 +00001870
Guido van Rossum823649d2001-03-21 18:40:58 +00001871 retval = PyObject_IsInstance(inst, cls);
1872 if (retval < 0)
Guido van Rossumad991772001-01-12 16:03:05 +00001873 return NULL;
Guido van Rossum77f6a652002-04-03 22:41:51 +00001874 return PyBool_FromLong(retval);
Barry Warsawcde8b1b1997-08-22 21:14:38 +00001875}
1876
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001877PyDoc_STRVAR(isinstance_doc,
Guido van Rossum77f6a652002-04-03 22:41:51 +00001878"isinstance(object, class-or-type-or-tuple) -> bool\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001879\n\
1880Return whether an object is an instance of a class or of a subclass thereof.\n\
Guido van Rossum03290ec2001-10-07 20:54:12 +00001881With a type as second argument, return whether that is the object's type.\n\
1882The form using a tuple, isinstance(x, (A, B, ...)), is a shortcut for\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001883isinstance(x, A) or isinstance(x, B) or ... (etc.).");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001884
Barry Warsawcde8b1b1997-08-22 21:14:38 +00001885
1886static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001887builtin_issubclass(PyObject *self, PyObject *args)
Barry Warsawcde8b1b1997-08-22 21:14:38 +00001888{
1889 PyObject *derived;
1890 PyObject *cls;
1891 int retval;
1892
Raymond Hettingerea3fdf42002-12-29 16:33:45 +00001893 if (!PyArg_UnpackTuple(args, "issubclass", 2, 2, &derived, &cls))
Barry Warsawcde8b1b1997-08-22 21:14:38 +00001894 return NULL;
Guido van Rossum668213d1999-06-16 17:28:37 +00001895
Guido van Rossum823649d2001-03-21 18:40:58 +00001896 retval = PyObject_IsSubclass(derived, cls);
1897 if (retval < 0)
1898 return NULL;
Guido van Rossum77f6a652002-04-03 22:41:51 +00001899 return PyBool_FromLong(retval);
Barry Warsawcde8b1b1997-08-22 21:14:38 +00001900}
1901
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001902PyDoc_STRVAR(issubclass_doc,
Guido van Rossum77f6a652002-04-03 22:41:51 +00001903"issubclass(C, B) -> bool\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001904\n\
Walter Dörwaldd9a6ad32002-12-12 16:41:44 +00001905Return whether class C is a subclass (i.e., a derived class) of class B.\n\
1906When using a tuple as the second argument issubclass(X, (A, B, ...)),\n\
1907is a shortcut for issubclass(X, A) or issubclass(X, B) or ... (etc.).");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001908
Barry Warsawcde8b1b1997-08-22 21:14:38 +00001909
Barry Warsawbd599b52000-08-03 15:45:29 +00001910static PyObject*
1911builtin_zip(PyObject *self, PyObject *args)
1912{
1913 PyObject *ret;
Tim Peters67d687a2002-04-29 21:27:32 +00001914 const int itemsize = PySequence_Length(args);
Tim Peters8572b4f2001-05-06 01:05:02 +00001915 int i;
1916 PyObject *itlist; /* tuple of iterators */
Tim Peters67d687a2002-04-29 21:27:32 +00001917 int len; /* guess at result length */
Barry Warsawbd599b52000-08-03 15:45:29 +00001918
Raymond Hettingereaef6152003-08-02 07:42:57 +00001919 if (itemsize == 0)
1920 return PyList_New(0);
1921
Barry Warsawbd599b52000-08-03 15:45:29 +00001922 /* args must be a tuple */
1923 assert(PyTuple_Check(args));
1924
Tim Peters39a86c22002-05-12 07:19:38 +00001925 /* Guess at result length: the shortest of the input lengths.
1926 If some argument refuses to say, we refuse to guess too, lest
1927 an argument like xrange(sys.maxint) lead us astray.*/
Tim Peters67d687a2002-04-29 21:27:32 +00001928 len = -1; /* unknown */
1929 for (i = 0; i < itemsize; ++i) {
1930 PyObject *item = PyTuple_GET_ITEM(args, i);
1931 int thislen = PySequence_Length(item);
Tim Peters39a86c22002-05-12 07:19:38 +00001932 if (thislen < 0) {
Tim Peters67d687a2002-04-29 21:27:32 +00001933 PyErr_Clear();
Tim Peters39a86c22002-05-12 07:19:38 +00001934 len = -1;
1935 break;
1936 }
Tim Peters67d687a2002-04-29 21:27:32 +00001937 else if (len < 0 || thislen < len)
1938 len = thislen;
1939 }
1940
Tim Peters8572b4f2001-05-06 01:05:02 +00001941 /* allocate result list */
Tim Peters67d687a2002-04-29 21:27:32 +00001942 if (len < 0)
1943 len = 10; /* arbitrary */
1944 if ((ret = PyList_New(len)) == NULL)
Barry Warsawbd599b52000-08-03 15:45:29 +00001945 return NULL;
1946
Tim Peters8572b4f2001-05-06 01:05:02 +00001947 /* obtain iterators */
1948 itlist = PyTuple_New(itemsize);
1949 if (itlist == NULL)
1950 goto Fail_ret;
1951 for (i = 0; i < itemsize; ++i) {
1952 PyObject *item = PyTuple_GET_ITEM(args, i);
1953 PyObject *it = PyObject_GetIter(item);
1954 if (it == NULL) {
1955 if (PyErr_ExceptionMatches(PyExc_TypeError))
1956 PyErr_Format(PyExc_TypeError,
1957 "zip argument #%d must support iteration",
1958 i+1);
1959 goto Fail_ret_itlist;
Barry Warsawbd599b52000-08-03 15:45:29 +00001960 }
Tim Peters8572b4f2001-05-06 01:05:02 +00001961 PyTuple_SET_ITEM(itlist, i, it);
1962 }
Barry Warsawbd599b52000-08-03 15:45:29 +00001963
Tim Peters8572b4f2001-05-06 01:05:02 +00001964 /* build result into ret list */
Tim Peters67d687a2002-04-29 21:27:32 +00001965 for (i = 0; ; ++i) {
1966 int j;
Tim Peters8572b4f2001-05-06 01:05:02 +00001967 PyObject *next = PyTuple_New(itemsize);
1968 if (!next)
1969 goto Fail_ret_itlist;
1970
Tim Peters67d687a2002-04-29 21:27:32 +00001971 for (j = 0; j < itemsize; j++) {
1972 PyObject *it = PyTuple_GET_ITEM(itlist, j);
Tim Peters8572b4f2001-05-06 01:05:02 +00001973 PyObject *item = PyIter_Next(it);
Barry Warsawbd599b52000-08-03 15:45:29 +00001974 if (!item) {
Tim Peters8572b4f2001-05-06 01:05:02 +00001975 if (PyErr_Occurred()) {
1976 Py_DECREF(ret);
1977 ret = NULL;
Barry Warsawbd599b52000-08-03 15:45:29 +00001978 }
1979 Py_DECREF(next);
Tim Peters8572b4f2001-05-06 01:05:02 +00001980 Py_DECREF(itlist);
Tim Peters67d687a2002-04-29 21:27:32 +00001981 goto Done;
Barry Warsawbd599b52000-08-03 15:45:29 +00001982 }
Tim Peters67d687a2002-04-29 21:27:32 +00001983 PyTuple_SET_ITEM(next, j, item);
Barry Warsawbd599b52000-08-03 15:45:29 +00001984 }
Tim Peters8572b4f2001-05-06 01:05:02 +00001985
Tim Peters67d687a2002-04-29 21:27:32 +00001986 if (i < len)
1987 PyList_SET_ITEM(ret, i, next);
1988 else {
1989 int status = PyList_Append(ret, next);
1990 Py_DECREF(next);
1991 ++len;
1992 if (status < 0)
1993 goto Fail_ret_itlist;
1994 }
Barry Warsawbd599b52000-08-03 15:45:29 +00001995 }
Tim Peters8572b4f2001-05-06 01:05:02 +00001996
Tim Peters67d687a2002-04-29 21:27:32 +00001997Done:
1998 if (ret != NULL && i < len) {
1999 /* The list is too big. */
2000 if (PyList_SetSlice(ret, i, len, NULL) < 0)
2001 return NULL;
2002 }
2003 return ret;
2004
Tim Peters8572b4f2001-05-06 01:05:02 +00002005Fail_ret_itlist:
2006 Py_DECREF(itlist);
2007Fail_ret:
2008 Py_DECREF(ret);
2009 return NULL;
Barry Warsawbd599b52000-08-03 15:45:29 +00002010}
2011
2012
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002013PyDoc_STRVAR(zip_doc,
Barry Warsawbd599b52000-08-03 15:45:29 +00002014"zip(seq1 [, seq2 [...]]) -> [(seq1[0], seq2[0] ...), (...)]\n\
2015\n\
2016Return a list of tuples, where each tuple contains the i-th element\n\
2017from each of the argument sequences. The returned list is truncated\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002018in length to the length of the shortest argument sequence.");
Barry Warsawbd599b52000-08-03 15:45:29 +00002019
2020
Guido van Rossum79f25d91997-04-29 20:08:16 +00002021static PyMethodDef builtin_methods[] = {
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002022 {"__import__", builtin___import__, METH_VARARGS, import_doc},
2023 {"abs", builtin_abs, METH_O, abs_doc},
2024 {"apply", builtin_apply, METH_VARARGS, apply_doc},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002025 {"callable", builtin_callable, METH_O, callable_doc},
2026 {"chr", builtin_chr, METH_VARARGS, chr_doc},
2027 {"cmp", builtin_cmp, METH_VARARGS, cmp_doc},
2028 {"coerce", builtin_coerce, METH_VARARGS, coerce_doc},
2029 {"compile", builtin_compile, METH_VARARGS, compile_doc},
2030 {"delattr", builtin_delattr, METH_VARARGS, delattr_doc},
2031 {"dir", builtin_dir, METH_VARARGS, dir_doc},
2032 {"divmod", builtin_divmod, METH_VARARGS, divmod_doc},
2033 {"eval", builtin_eval, METH_VARARGS, eval_doc},
2034 {"execfile", builtin_execfile, METH_VARARGS, execfile_doc},
2035 {"filter", builtin_filter, METH_VARARGS, filter_doc},
2036 {"getattr", builtin_getattr, METH_VARARGS, getattr_doc},
2037 {"globals", (PyCFunction)builtin_globals, METH_NOARGS, globals_doc},
2038 {"hasattr", builtin_hasattr, METH_VARARGS, hasattr_doc},
2039 {"hash", builtin_hash, METH_O, hash_doc},
2040 {"hex", builtin_hex, METH_O, hex_doc},
2041 {"id", builtin_id, METH_O, id_doc},
2042 {"input", builtin_input, METH_VARARGS, input_doc},
2043 {"intern", builtin_intern, METH_VARARGS, intern_doc},
2044 {"isinstance", builtin_isinstance, METH_VARARGS, isinstance_doc},
2045 {"issubclass", builtin_issubclass, METH_VARARGS, issubclass_doc},
2046 {"iter", builtin_iter, METH_VARARGS, iter_doc},
2047 {"len", builtin_len, METH_O, len_doc},
2048 {"locals", (PyCFunction)builtin_locals, METH_NOARGS, locals_doc},
2049 {"map", builtin_map, METH_VARARGS, map_doc},
2050 {"max", builtin_max, METH_VARARGS, max_doc},
2051 {"min", builtin_min, METH_VARARGS, min_doc},
2052 {"oct", builtin_oct, METH_O, oct_doc},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002053 {"ord", builtin_ord, METH_O, ord_doc},
2054 {"pow", builtin_pow, METH_VARARGS, pow_doc},
2055 {"range", builtin_range, METH_VARARGS, range_doc},
2056 {"raw_input", builtin_raw_input, METH_VARARGS, raw_input_doc},
2057 {"reduce", builtin_reduce, METH_VARARGS, reduce_doc},
2058 {"reload", builtin_reload, METH_O, reload_doc},
2059 {"repr", builtin_repr, METH_O, repr_doc},
2060 {"round", builtin_round, METH_VARARGS, round_doc},
2061 {"setattr", builtin_setattr, METH_VARARGS, setattr_doc},
Alex Martellia70b1912003-04-22 08:12:33 +00002062 {"sum", builtin_sum, METH_VARARGS, sum_doc},
Martin v. Löwis339d0f72001-08-17 18:39:25 +00002063#ifdef Py_USING_UNICODE
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002064 {"unichr", builtin_unichr, METH_VARARGS, unichr_doc},
Martin v. Löwis339d0f72001-08-17 18:39:25 +00002065#endif
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002066 {"vars", builtin_vars, METH_VARARGS, vars_doc},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002067 {"zip", builtin_zip, METH_VARARGS, zip_doc},
Guido van Rossumc02e15c1991-12-16 13:03:00 +00002068 {NULL, NULL},
Guido van Rossum3f5da241990-12-20 15:06:42 +00002069};
2070
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002071PyDoc_STRVAR(builtin_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002072"Built-in functions, exceptions, and other objects.\n\
2073\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002074Noteworthy: None is the `nil' object; Ellipsis represents `...' in slices.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002075
Guido van Rossum25ce5661997-08-02 03:10:38 +00002076PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002077_PyBuiltin_Init(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +00002078{
Fred Drake5550de32000-06-20 04:54:19 +00002079 PyObject *mod, *dict, *debug;
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002080 mod = Py_InitModule4("__builtin__", builtin_methods,
2081 builtin_doc, (PyObject *)NULL,
2082 PYTHON_API_VERSION);
Guido van Rossum25ce5661997-08-02 03:10:38 +00002083 if (mod == NULL)
2084 return NULL;
2085 dict = PyModule_GetDict(mod);
Tim Peters4b7625e2001-09-13 21:37:17 +00002086
Tim Peters7571a0f2003-03-23 17:52:28 +00002087#ifdef Py_TRACE_REFS
2088 /* __builtin__ exposes a number of statically allocated objects
2089 * that, before this code was added in 2.3, never showed up in
2090 * the list of "all objects" maintained by Py_TRACE_REFS. As a
2091 * result, programs leaking references to None and False (etc)
2092 * couldn't be diagnosed by examining sys.getobjects(0).
2093 */
2094#define ADD_TO_ALL(OBJECT) _Py_AddToAllObjects((PyObject *)(OBJECT), 0)
2095#else
2096#define ADD_TO_ALL(OBJECT) (void)0
2097#endif
2098
Tim Peters4b7625e2001-09-13 21:37:17 +00002099#define SETBUILTIN(NAME, OBJECT) \
Tim Peters7571a0f2003-03-23 17:52:28 +00002100 if (PyDict_SetItemString(dict, NAME, (PyObject *)OBJECT) < 0) \
2101 return NULL; \
2102 ADD_TO_ALL(OBJECT)
Tim Peters4b7625e2001-09-13 21:37:17 +00002103
2104 SETBUILTIN("None", Py_None);
2105 SETBUILTIN("Ellipsis", Py_Ellipsis);
2106 SETBUILTIN("NotImplemented", Py_NotImplemented);
Guido van Rossum77f6a652002-04-03 22:41:51 +00002107 SETBUILTIN("False", Py_False);
2108 SETBUILTIN("True", Py_True);
Neal Norwitz32a7e7f2002-05-31 19:58:02 +00002109 SETBUILTIN("basestring", &PyBaseString_Type);
Guido van Rossum77f6a652002-04-03 22:41:51 +00002110 SETBUILTIN("bool", &PyBool_Type);
Guido van Rossumbea18cc2002-06-14 20:41:17 +00002111 SETBUILTIN("buffer", &PyBuffer_Type);
Tim Peters4b7625e2001-09-13 21:37:17 +00002112 SETBUILTIN("classmethod", &PyClassMethod_Type);
Tim Peters6d6c1a32001-08-02 04:15:00 +00002113#ifndef WITHOUT_COMPLEX
Tim Peters4b7625e2001-09-13 21:37:17 +00002114 SETBUILTIN("complex", &PyComplex_Type);
Tim Peters6d6c1a32001-08-02 04:15:00 +00002115#endif
Tim Petersa427a2b2001-10-29 22:25:45 +00002116 SETBUILTIN("dict", &PyDict_Type);
Tim Peters67d687a2002-04-29 21:27:32 +00002117 SETBUILTIN("enumerate", &PyEnum_Type);
Tim Peters4b7625e2001-09-13 21:37:17 +00002118 SETBUILTIN("float", &PyFloat_Type);
2119 SETBUILTIN("property", &PyProperty_Type);
2120 SETBUILTIN("int", &PyInt_Type);
2121 SETBUILTIN("list", &PyList_Type);
2122 SETBUILTIN("long", &PyLong_Type);
2123 SETBUILTIN("object", &PyBaseObject_Type);
Guido van Rossumbea18cc2002-06-14 20:41:17 +00002124 SETBUILTIN("slice", &PySlice_Type);
Tim Peters4b7625e2001-09-13 21:37:17 +00002125 SETBUILTIN("staticmethod", &PyStaticMethod_Type);
2126 SETBUILTIN("str", &PyString_Type);
2127 SETBUILTIN("super", &PySuper_Type);
2128 SETBUILTIN("tuple", &PyTuple_Type);
2129 SETBUILTIN("type", &PyType_Type);
Raymond Hettingerc4c453f2002-06-05 23:12:45 +00002130 SETBUILTIN("xrange", &PyRange_Type);
Tim Peters742dfd62001-09-13 21:49:44 +00002131
2132 /* Note that open() is just an alias of file(). */
2133 SETBUILTIN("open", &PyFile_Type);
Tim Peters4b7625e2001-09-13 21:37:17 +00002134 SETBUILTIN("file", &PyFile_Type);
Martin v. Löwis339d0f72001-08-17 18:39:25 +00002135#ifdef Py_USING_UNICODE
Tim Peters4b7625e2001-09-13 21:37:17 +00002136 SETBUILTIN("unicode", &PyUnicode_Type);
Martin v. Löwis339d0f72001-08-17 18:39:25 +00002137#endif
Guido van Rossum77f6a652002-04-03 22:41:51 +00002138 debug = PyBool_FromLong(Py_OptimizeFlag == 0);
Fred Drake5550de32000-06-20 04:54:19 +00002139 if (PyDict_SetItemString(dict, "__debug__", debug) < 0) {
2140 Py_XDECREF(debug);
Guido van Rossum25ce5661997-08-02 03:10:38 +00002141 return NULL;
Fred Drake5550de32000-06-20 04:54:19 +00002142 }
2143 Py_XDECREF(debug);
Barry Warsaw757af0e1997-08-29 22:13:51 +00002144
Guido van Rossum25ce5661997-08-02 03:10:38 +00002145 return mod;
Tim Peters7571a0f2003-03-23 17:52:28 +00002146#undef ADD_TO_ALL
Tim Peters4b7625e2001-09-13 21:37:17 +00002147#undef SETBUILTIN
Guido van Rossum3f5da241990-12-20 15:06:42 +00002148}
2149
Guido van Rossume77a7571993-11-03 15:01:26 +00002150/* Helper for filter(): filter a tuple through a function */
Guido van Rossum12d12c51993-10-26 17:58:25 +00002151
Guido van Rossum79f25d91997-04-29 20:08:16 +00002152static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002153filtertuple(PyObject *func, PyObject *tuple)
Guido van Rossum12d12c51993-10-26 17:58:25 +00002154{
Guido van Rossum79f25d91997-04-29 20:08:16 +00002155 PyObject *result;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002156 register int i, j;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002157 int len = PyTuple_Size(tuple);
Guido van Rossum12d12c51993-10-26 17:58:25 +00002158
Guido van Rossumb7b45621995-08-04 04:07:45 +00002159 if (len == 0) {
Walter Dörwaldc3da83f2003-02-04 20:24:45 +00002160 if (PyTuple_CheckExact(tuple))
2161 Py_INCREF(tuple);
2162 else
2163 tuple = PyTuple_New(0);
Guido van Rossumb7b45621995-08-04 04:07:45 +00002164 return tuple;
2165 }
2166
Guido van Rossum79f25d91997-04-29 20:08:16 +00002167 if ((result = PyTuple_New(len)) == NULL)
Guido van Rossum2586bf01993-11-01 16:21:44 +00002168 return NULL;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002169
Guido van Rossum12d12c51993-10-26 17:58:25 +00002170 for (i = j = 0; i < len; ++i) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002171 PyObject *item, *good;
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002172 int ok;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002173
Walter Dörwald8dd19322003-02-10 17:36:40 +00002174 if (tuple->ob_type->tp_as_sequence &&
2175 tuple->ob_type->tp_as_sequence->sq_item) {
2176 item = tuple->ob_type->tp_as_sequence->sq_item(tuple, i);
Walter Dörwaldc58a3a12003-08-18 18:28:45 +00002177 if (item == NULL)
2178 goto Fail_1;
Walter Dörwald8dd19322003-02-10 17:36:40 +00002179 } else {
Alex Martellia9b9c9f2003-04-23 13:34:35 +00002180 PyErr_SetString(PyExc_TypeError, "filter(): unsubscriptable tuple");
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002181 goto Fail_1;
Walter Dörwald8dd19322003-02-10 17:36:40 +00002182 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002183 if (func == Py_None) {
2184 Py_INCREF(item);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002185 good = item;
2186 }
2187 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002188 PyObject *arg = Py_BuildValue("(O)", item);
Walter Dörwaldc58a3a12003-08-18 18:28:45 +00002189 if (arg == NULL) {
2190 Py_DECREF(item);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002191 goto Fail_1;
Walter Dörwaldc58a3a12003-08-18 18:28:45 +00002192 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002193 good = PyEval_CallObject(func, arg);
2194 Py_DECREF(arg);
Walter Dörwaldc58a3a12003-08-18 18:28:45 +00002195 if (good == NULL) {
2196 Py_DECREF(item);
Guido van Rossum12d12c51993-10-26 17:58:25 +00002197 goto Fail_1;
Walter Dörwaldc58a3a12003-08-18 18:28:45 +00002198 }
Guido van Rossum12d12c51993-10-26 17:58:25 +00002199 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002200 ok = PyObject_IsTrue(good);
2201 Py_DECREF(good);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002202 if (ok) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002203 if (PyTuple_SetItem(result, j++, item) < 0)
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002204 goto Fail_1;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002205 }
Walter Dörwaldc58a3a12003-08-18 18:28:45 +00002206 else
2207 Py_DECREF(item);
Guido van Rossum12d12c51993-10-26 17:58:25 +00002208 }
2209
Tim Peters4324aa32001-05-28 22:30:08 +00002210 if (_PyTuple_Resize(&result, j) < 0)
Guido van Rossum12d12c51993-10-26 17:58:25 +00002211 return NULL;
2212
Guido van Rossum12d12c51993-10-26 17:58:25 +00002213 return result;
2214
Guido van Rossum12d12c51993-10-26 17:58:25 +00002215Fail_1:
Guido van Rossum79f25d91997-04-29 20:08:16 +00002216 Py_DECREF(result);
Guido van Rossum12d12c51993-10-26 17:58:25 +00002217 return NULL;
2218}
2219
2220
Guido van Rossume77a7571993-11-03 15:01:26 +00002221/* Helper for filter(): filter a string through a function */
Guido van Rossum12d12c51993-10-26 17:58:25 +00002222
Guido van Rossum79f25d91997-04-29 20:08:16 +00002223static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002224filterstring(PyObject *func, PyObject *strobj)
Guido van Rossum12d12c51993-10-26 17:58:25 +00002225{
Guido van Rossum79f25d91997-04-29 20:08:16 +00002226 PyObject *result;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002227 register int i, j;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002228 int len = PyString_Size(strobj);
Walter Dörwald903f1e02003-02-04 16:28:00 +00002229 int outlen = len;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002230
Guido van Rossum79f25d91997-04-29 20:08:16 +00002231 if (func == Py_None) {
Walter Dörwald1918f772003-02-10 13:19:13 +00002232 /* If it's a real string we can return the original,
2233 * as no character is ever false and __getitem__
2234 * does return this character. If it's a subclass
2235 * we must go through the __getitem__ loop */
2236 if (PyString_CheckExact(strobj)) {
Walter Dörwaldc3da83f2003-02-04 20:24:45 +00002237 Py_INCREF(strobj);
Walter Dörwald1918f772003-02-10 13:19:13 +00002238 return strobj;
2239 }
Guido van Rossum12d12c51993-10-26 17:58:25 +00002240 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002241 if ((result = PyString_FromStringAndSize(NULL, len)) == NULL)
Guido van Rossum2586bf01993-11-01 16:21:44 +00002242 return NULL;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002243
Guido van Rossum12d12c51993-10-26 17:58:25 +00002244 for (i = j = 0; i < len; ++i) {
Walter Dörwald1918f772003-02-10 13:19:13 +00002245 PyObject *item;
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002246 int ok;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002247
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002248 item = (*strobj->ob_type->tp_as_sequence->sq_item)(strobj, i);
2249 if (item == NULL)
2250 goto Fail_1;
Walter Dörwald1918f772003-02-10 13:19:13 +00002251 if (func==Py_None) {
2252 ok = 1;
2253 } else {
2254 PyObject *arg, *good;
2255 arg = Py_BuildValue("(O)", item);
2256 if (arg == NULL) {
2257 Py_DECREF(item);
2258 goto Fail_1;
2259 }
2260 good = PyEval_CallObject(func, arg);
2261 Py_DECREF(arg);
2262 if (good == NULL) {
2263 Py_DECREF(item);
2264 goto Fail_1;
2265 }
2266 ok = PyObject_IsTrue(good);
2267 Py_DECREF(good);
Tim Peters388ed082001-04-07 20:34:48 +00002268 }
Walter Dörwald903f1e02003-02-04 16:28:00 +00002269 if (ok) {
2270 int reslen;
2271 if (!PyString_Check(item)) {
2272 PyErr_SetString(PyExc_TypeError, "can't filter str to str:"
2273 " __getitem__ returned different type");
2274 Py_DECREF(item);
2275 goto Fail_1;
2276 }
2277 reslen = PyString_GET_SIZE(item);
2278 if (reslen == 1) {
2279 PyString_AS_STRING(result)[j++] =
2280 PyString_AS_STRING(item)[0];
2281 } else {
2282 /* do we need more space? */
2283 int need = j + reslen + len-i-1;
2284 if (need > outlen) {
2285 /* overallocate, to avoid reallocations */
2286 if (need<2*outlen)
2287 need = 2*outlen;
2288 if (_PyString_Resize(&result, need)) {
2289 Py_DECREF(item);
2290 return NULL;
2291 }
2292 outlen = need;
2293 }
2294 memcpy(
2295 PyString_AS_STRING(result) + j,
2296 PyString_AS_STRING(item),
2297 reslen
2298 );
2299 j += reslen;
2300 }
2301 }
Tim Peters388ed082001-04-07 20:34:48 +00002302 Py_DECREF(item);
Guido van Rossum12d12c51993-10-26 17:58:25 +00002303 }
2304
Walter Dörwald903f1e02003-02-04 16:28:00 +00002305 if (j < outlen)
Tim Peters5de98422002-04-27 18:44:32 +00002306 _PyString_Resize(&result, j);
Guido van Rossum12d12c51993-10-26 17:58:25 +00002307
Guido van Rossum12d12c51993-10-26 17:58:25 +00002308 return result;
2309
Guido van Rossum12d12c51993-10-26 17:58:25 +00002310Fail_1:
Guido van Rossum79f25d91997-04-29 20:08:16 +00002311 Py_DECREF(result);
Guido van Rossum12d12c51993-10-26 17:58:25 +00002312 return NULL;
2313}
Martin v. Löwis8afd7572003-01-25 22:46:11 +00002314
2315#ifdef Py_USING_UNICODE
2316/* Helper for filter(): filter a Unicode object through a function */
2317
2318static PyObject *
2319filterunicode(PyObject *func, PyObject *strobj)
2320{
2321 PyObject *result;
2322 register int i, j;
2323 int len = PyUnicode_GetSize(strobj);
Walter Dörwald903f1e02003-02-04 16:28:00 +00002324 int outlen = len;
Martin v. Löwis8afd7572003-01-25 22:46:11 +00002325
2326 if (func == Py_None) {
Walter Dörwald1918f772003-02-10 13:19:13 +00002327 /* If it's a real string we can return the original,
2328 * as no character is ever false and __getitem__
2329 * does return this character. If it's a subclass
2330 * we must go through the __getitem__ loop */
2331 if (PyUnicode_CheckExact(strobj)) {
Walter Dörwaldc3da83f2003-02-04 20:24:45 +00002332 Py_INCREF(strobj);
Walter Dörwald1918f772003-02-10 13:19:13 +00002333 return strobj;
2334 }
Martin v. Löwis8afd7572003-01-25 22:46:11 +00002335 }
2336 if ((result = PyUnicode_FromUnicode(NULL, len)) == NULL)
2337 return NULL;
2338
2339 for (i = j = 0; i < len; ++i) {
2340 PyObject *item, *arg, *good;
2341 int ok;
2342
2343 item = (*strobj->ob_type->tp_as_sequence->sq_item)(strobj, i);
2344 if (item == NULL)
2345 goto Fail_1;
Walter Dörwald1918f772003-02-10 13:19:13 +00002346 if (func == Py_None) {
2347 ok = 1;
2348 } else {
2349 arg = Py_BuildValue("(O)", item);
2350 if (arg == NULL) {
2351 Py_DECREF(item);
2352 goto Fail_1;
2353 }
2354 good = PyEval_CallObject(func, arg);
2355 Py_DECREF(arg);
2356 if (good == NULL) {
2357 Py_DECREF(item);
2358 goto Fail_1;
2359 }
2360 ok = PyObject_IsTrue(good);
2361 Py_DECREF(good);
Martin v. Löwis8afd7572003-01-25 22:46:11 +00002362 }
Walter Dörwald903f1e02003-02-04 16:28:00 +00002363 if (ok) {
2364 int reslen;
2365 if (!PyUnicode_Check(item)) {
Jeremy Hylton1aad9c72003-09-16 03:10:59 +00002366 PyErr_SetString(PyExc_TypeError,
2367 "can't filter unicode to unicode:"
2368 " __getitem__ returned different type");
Walter Dörwald903f1e02003-02-04 16:28:00 +00002369 Py_DECREF(item);
2370 goto Fail_1;
2371 }
2372 reslen = PyUnicode_GET_SIZE(item);
Jeremy Hylton1aad9c72003-09-16 03:10:59 +00002373 if (reslen == 1)
Walter Dörwald903f1e02003-02-04 16:28:00 +00002374 PyUnicode_AS_UNICODE(result)[j++] =
2375 PyUnicode_AS_UNICODE(item)[0];
Jeremy Hylton1aad9c72003-09-16 03:10:59 +00002376 else {
Walter Dörwald903f1e02003-02-04 16:28:00 +00002377 /* do we need more space? */
Jeremy Hylton1aad9c72003-09-16 03:10:59 +00002378 int need = j + reslen + len - i - 1;
Walter Dörwald903f1e02003-02-04 16:28:00 +00002379 if (need > outlen) {
Jeremy Hylton1aad9c72003-09-16 03:10:59 +00002380 /* overallocate,
2381 to avoid reallocations */
2382 if (need < 2 * outlen)
2383 need = 2 * outlen;
Jeremy Hylton364f6be2003-09-16 03:17:16 +00002384 if (PyUnicode_Resize(
2385 &result, need) < 0) {
Walter Dörwald903f1e02003-02-04 16:28:00 +00002386 Py_DECREF(item);
Walter Dörwald531e0002003-02-04 16:57:49 +00002387 goto Fail_1;
Walter Dörwald903f1e02003-02-04 16:28:00 +00002388 }
2389 outlen = need;
2390 }
Jeremy Hylton1aad9c72003-09-16 03:10:59 +00002391 memcpy(PyUnicode_AS_UNICODE(result) + j,
2392 PyUnicode_AS_UNICODE(item),
2393 reslen*sizeof(Py_UNICODE));
Walter Dörwald903f1e02003-02-04 16:28:00 +00002394 j += reslen;
2395 }
2396 }
Martin v. Löwis8afd7572003-01-25 22:46:11 +00002397 Py_DECREF(item);
2398 }
2399
Walter Dörwald903f1e02003-02-04 16:28:00 +00002400 if (j < outlen)
Martin v. Löwis8afd7572003-01-25 22:46:11 +00002401 PyUnicode_Resize(&result, j);
2402
2403 return result;
2404
2405Fail_1:
2406 Py_DECREF(result);
2407 return NULL;
2408}
2409#endif