blob: 9d6378bd78880eed858a4d4e16db8ae156c3c889 [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
Raymond Hettingerea3fdf42002-12-29 16:33:45 +000078 if (!PyArg_UnpackTuple(args, "apply", 1, 3, &func, &alist, &kwdict))
Guido van Rossumc02e15c1991-12-16 13:03:00 +000079 return NULL;
Barry Warsaw968f8cb1998-10-01 15:33:12 +000080 if (alist != NULL) {
81 if (!PyTuple_Check(alist)) {
82 if (!PySequence_Check(alist)) {
Jeremy Hyltonc862cf42001-01-19 03:25:05 +000083 PyErr_Format(PyExc_TypeError,
Alex Martellia9b9c9f2003-04-23 13:34:35 +000084 "apply() arg 2 expected sequence, found %s",
Jeremy Hyltonc862cf42001-01-19 03:25:05 +000085 alist->ob_type->tp_name);
Barry Warsaw968f8cb1998-10-01 15:33:12 +000086 return NULL;
87 }
88 t = PySequence_Tuple(alist);
89 if (t == NULL)
90 return NULL;
91 alist = t;
92 }
Guido van Rossum2d951851994-08-29 12:52:16 +000093 }
Guido van Rossum79f25d91997-04-29 20:08:16 +000094 if (kwdict != NULL && !PyDict_Check(kwdict)) {
Jeremy Hyltonc862cf42001-01-19 03:25:05 +000095 PyErr_Format(PyExc_TypeError,
96 "apply() arg 3 expected dictionary, found %s",
97 kwdict->ob_type->tp_name);
Barry Warsaw968f8cb1998-10-01 15:33:12 +000098 goto finally;
Guido van Rossum681d79a1995-07-18 14:51:37 +000099 }
Barry Warsaw968f8cb1998-10-01 15:33:12 +0000100 retval = PyEval_CallObjectWithKeywords(func, alist, kwdict);
101 finally:
102 Py_XDECREF(t);
103 return retval;
Guido van Rossumc02e15c1991-12-16 13:03:00 +0000104}
105
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000106PyDoc_STRVAR(apply_doc,
Fred Drakef1fbc622001-01-12 17:05:05 +0000107"apply(object[, args[, kwargs]]) -> value\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000108\n\
Fred Drake7b912121999-12-23 14:16:55 +0000109Call a callable object with positional arguments taken from the tuple args,\n\
110and keyword arguments taken from the optional dictionary kwargs.\n\
Raymond Hettingerff41c482003-04-06 09:01:11 +0000111Note that classes are callable, as are instances with a __call__() method.\n\
112\n\
113Deprecated since release 2.3. Instead, use the extended call syntax:\n\
114 function(*args, **keywords).");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000115
116
Guido van Rossum79f25d91997-04-29 20:08:16 +0000117static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000118builtin_callable(PyObject *self, PyObject *v)
Guido van Rossum2d951851994-08-29 12:52:16 +0000119{
Guido van Rossum77f6a652002-04-03 22:41:51 +0000120 return PyBool_FromLong((long)PyCallable_Check(v));
Guido van Rossum2d951851994-08-29 12:52:16 +0000121}
122
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000123PyDoc_STRVAR(callable_doc,
Guido van Rossum77f6a652002-04-03 22:41:51 +0000124"callable(object) -> bool\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000125\n\
126Return whether the object is callable (i.e., some kind of function).\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000127Note that classes are callable, as are instances with a __call__() method.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000128
129
Guido van Rossum79f25d91997-04-29 20:08:16 +0000130static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000131builtin_filter(PyObject *self, PyObject *args)
Guido van Rossum12d12c51993-10-26 17:58:25 +0000132{
Guido van Rossumc7903a12002-08-16 07:04:56 +0000133 PyObject *func, *seq, *result, *it, *arg;
Tim Peters0e57abf2001-05-02 07:39:38 +0000134 int len; /* guess for result list size */
Tim Petersf4848da2001-05-05 00:14:56 +0000135 register int j;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000136
Raymond Hettingerea3fdf42002-12-29 16:33:45 +0000137 if (!PyArg_UnpackTuple(args, "filter", 2, 2, &func, &seq))
Guido van Rossum12d12c51993-10-26 17:58:25 +0000138 return NULL;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000139
Tim Peters0e57abf2001-05-02 07:39:38 +0000140 /* Strings and tuples return a result of the same type. */
141 if (PyString_Check(seq))
142 return filterstring(func, seq);
Martin v. Löwis8afd7572003-01-25 22:46:11 +0000143#ifdef Py_USING_UNICODE
144 if (PyUnicode_Check(seq))
145 return filterunicode(func, seq);
146#endif
Tim Peters0e57abf2001-05-02 07:39:38 +0000147 if (PyTuple_Check(seq))
148 return filtertuple(func, seq);
149
150 /* Get iterator. */
151 it = PyObject_GetIter(seq);
152 if (it == NULL)
153 return NULL;
154
155 /* Guess a result list size. */
156 len = -1; /* unknown */
157 if (PySequence_Check(seq) &&
158 seq->ob_type->tp_as_sequence->sq_length) {
159 len = PySequence_Size(seq);
160 if (len < 0)
161 PyErr_Clear();
Guido van Rossum12d12c51993-10-26 17:58:25 +0000162 }
Tim Peters0e57abf2001-05-02 07:39:38 +0000163 if (len < 0)
164 len = 8; /* arbitrary */
Guido van Rossum12d12c51993-10-26 17:58:25 +0000165
Guido van Rossumc7903a12002-08-16 07:04:56 +0000166 /* Pre-allocate argument list tuple. */
167 arg = PyTuple_New(1);
168 if (arg == NULL)
169 goto Fail_arg;
170
Tim Peters0e57abf2001-05-02 07:39:38 +0000171 /* Get a result list. */
Guido van Rossum79f25d91997-04-29 20:08:16 +0000172 if (PyList_Check(seq) && seq->ob_refcnt == 1) {
Tim Peters0e57abf2001-05-02 07:39:38 +0000173 /* Eww - can modify the list in-place. */
Guido van Rossum79f25d91997-04-29 20:08:16 +0000174 Py_INCREF(seq);
Guido van Rossum12d12c51993-10-26 17:58:25 +0000175 result = seq;
176 }
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000177 else {
Tim Peters0e57abf2001-05-02 07:39:38 +0000178 result = PyList_New(len);
179 if (result == NULL)
180 goto Fail_it;
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000181 }
Guido van Rossum12d12c51993-10-26 17:58:25 +0000182
Tim Peters0e57abf2001-05-02 07:39:38 +0000183 /* Build the result list. */
Tim Petersf4848da2001-05-05 00:14:56 +0000184 j = 0;
185 for (;;) {
Guido van Rossumc7903a12002-08-16 07:04:56 +0000186 PyObject *item;
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000187 int ok;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000188
Tim Peters0e57abf2001-05-02 07:39:38 +0000189 item = PyIter_Next(it);
190 if (item == NULL) {
Tim Petersf4848da2001-05-05 00:14:56 +0000191 if (PyErr_Occurred())
192 goto Fail_result_it;
Tim Peters0e57abf2001-05-02 07:39:38 +0000193 break;
Guido van Rossum2d951851994-08-29 12:52:16 +0000194 }
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000195
Neil Schemenauer68973552003-08-14 20:37:34 +0000196 if (func == (PyObject *)&PyBool_Type || func == Py_None) {
Guido van Rossumc7903a12002-08-16 07:04:56 +0000197 ok = PyObject_IsTrue(item);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000198 }
199 else {
Guido van Rossumc7903a12002-08-16 07:04:56 +0000200 PyObject *good;
201 PyTuple_SET_ITEM(arg, 0, item);
202 good = PyObject_Call(func, arg, NULL);
203 PyTuple_SET_ITEM(arg, 0, NULL);
Guido van Rossum58b68731995-01-10 17:40:55 +0000204 if (good == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000205 Py_DECREF(item);
Tim Peters0e57abf2001-05-02 07:39:38 +0000206 goto Fail_result_it;
Guido van Rossum58b68731995-01-10 17:40:55 +0000207 }
Guido van Rossumc7903a12002-08-16 07:04:56 +0000208 ok = PyObject_IsTrue(good);
209 Py_DECREF(good);
Guido van Rossum12d12c51993-10-26 17:58:25 +0000210 }
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000211 if (ok) {
Tim Peters0e57abf2001-05-02 07:39:38 +0000212 if (j < len)
213 PyList_SET_ITEM(result, j, item);
Guido van Rossum2d951851994-08-29 12:52:16 +0000214 else {
Barry Warsawfa77e091999-01-28 18:49:12 +0000215 int status = PyList_Append(result, item);
Barry Warsawfa77e091999-01-28 18:49:12 +0000216 Py_DECREF(item);
217 if (status < 0)
Tim Peters0e57abf2001-05-02 07:39:38 +0000218 goto Fail_result_it;
Guido van Rossum2d951851994-08-29 12:52:16 +0000219 }
Tim Peters0e57abf2001-05-02 07:39:38 +0000220 ++j;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000221 }
Tim Peters0e57abf2001-05-02 07:39:38 +0000222 else
223 Py_DECREF(item);
Guido van Rossum12d12c51993-10-26 17:58:25 +0000224 }
225
Guido van Rossum12d12c51993-10-26 17:58:25 +0000226
Tim Peters0e57abf2001-05-02 07:39:38 +0000227 /* Cut back result list if len is too big. */
Guido van Rossum79f25d91997-04-29 20:08:16 +0000228 if (j < len && PyList_SetSlice(result, j, len, NULL) < 0)
Tim Peters0e57abf2001-05-02 07:39:38 +0000229 goto Fail_result_it;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000230
Tim Peters3c6b1482001-05-21 08:07:05 +0000231 Py_DECREF(it);
Guido van Rossumc7903a12002-08-16 07:04:56 +0000232 Py_DECREF(arg);
Guido van Rossum12d12c51993-10-26 17:58:25 +0000233 return result;
234
Tim Peters0e57abf2001-05-02 07:39:38 +0000235Fail_result_it:
Guido van Rossum79f25d91997-04-29 20:08:16 +0000236 Py_DECREF(result);
Tim Peters0e57abf2001-05-02 07:39:38 +0000237Fail_it:
238 Py_DECREF(it);
Guido van Rossumc7903a12002-08-16 07:04:56 +0000239Fail_arg:
240 Py_DECREF(arg);
Guido van Rossum12d12c51993-10-26 17:58:25 +0000241 return NULL;
242}
243
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000244PyDoc_STRVAR(filter_doc,
Tim Petersd50e5442002-03-09 00:06:26 +0000245"filter(function or None, sequence) -> list, tuple, or string\n"
246"\n"
247"Return those items of sequence for which function(item) is true. If\n"
248"function is None, return the items that are true. If sequence is a tuple\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000249"or string, return the same type, else return a list.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000250
Guido van Rossum79f25d91997-04-29 20:08:16 +0000251static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000252builtin_chr(PyObject *self, PyObject *args)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000253{
254 long x;
255 char s[1];
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000256
Guido van Rossum79f25d91997-04-29 20:08:16 +0000257 if (!PyArg_ParseTuple(args, "l:chr", &x))
Guido van Rossum3f5da241990-12-20 15:06:42 +0000258 return NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000259 if (x < 0 || x >= 256) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000260 PyErr_SetString(PyExc_ValueError,
261 "chr() arg not in range(256)");
Guido van Rossum3f5da241990-12-20 15:06:42 +0000262 return NULL;
263 }
Guido van Rossum6bf62da1997-04-11 20:37:35 +0000264 s[0] = (char)x;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000265 return PyString_FromStringAndSize(s, 1);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000266}
267
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000268PyDoc_STRVAR(chr_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000269"chr(i) -> character\n\
270\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000271Return a string of one character with ordinal i; 0 <= i < 256.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000272
273
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000274#ifdef Py_USING_UNICODE
Guido van Rossum79f25d91997-04-29 20:08:16 +0000275static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000276builtin_unichr(PyObject *self, PyObject *args)
Guido van Rossum09095f32000-03-10 23:00:52 +0000277{
278 long x;
Guido van Rossum09095f32000-03-10 23:00:52 +0000279
280 if (!PyArg_ParseTuple(args, "l:unichr", &x))
281 return NULL;
Fredrik Lundh0dcf67e2001-06-26 20:01:56 +0000282
Marc-André Lemburgcc8764c2002-08-11 12:23:04 +0000283 return PyUnicode_FromOrdinal(x);
Guido van Rossum09095f32000-03-10 23:00:52 +0000284}
285
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000286PyDoc_STRVAR(unichr_doc,
Fred Drake078b24f2000-04-13 02:42:50 +0000287"unichr(i) -> Unicode character\n\
Guido van Rossum09095f32000-03-10 23:00:52 +0000288\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000289Return a Unicode string of one character with ordinal i; 0 <= i <= 0x10ffff.");
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000290#endif
Guido van Rossum09095f32000-03-10 23:00:52 +0000291
292
293static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000294builtin_cmp(PyObject *self, PyObject *args)
Guido van Rossuma9e7dc11992-10-18 18:53:57 +0000295{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000296 PyObject *a, *b;
Guido van Rossum09df08a1998-05-22 00:51:39 +0000297 int c;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000298
Raymond Hettingerea3fdf42002-12-29 16:33:45 +0000299 if (!PyArg_UnpackTuple(args, "cmp", 2, 2, &a, &b))
Guido van Rossuma9e7dc11992-10-18 18:53:57 +0000300 return NULL;
Guido van Rossum09df08a1998-05-22 00:51:39 +0000301 if (PyObject_Cmp(a, b, &c) < 0)
Guido van Rossumc8b6df91997-05-23 00:06:51 +0000302 return NULL;
Guido van Rossum09df08a1998-05-22 00:51:39 +0000303 return PyInt_FromLong((long)c);
Guido van Rossuma9e7dc11992-10-18 18:53:57 +0000304}
305
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000306PyDoc_STRVAR(cmp_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000307"cmp(x, y) -> integer\n\
308\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000309Return negative if x<y, zero if x==y, positive if x>y.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000310
311
Guido van Rossum79f25d91997-04-29 20:08:16 +0000312static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000313builtin_coerce(PyObject *self, PyObject *args)
Guido van Rossum6a00cd81995-01-07 12:39:01 +0000314{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000315 PyObject *v, *w;
316 PyObject *res;
Guido van Rossum5524a591995-01-10 15:26:20 +0000317
Raymond Hettingerea3fdf42002-12-29 16:33:45 +0000318 if (!PyArg_UnpackTuple(args, "coerce", 2, 2, &v, &w))
Guido van Rossum5524a591995-01-10 15:26:20 +0000319 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000320 if (PyNumber_Coerce(&v, &w) < 0)
Guido van Rossum04691fc1992-08-12 15:35:34 +0000321 return NULL;
Raymond Hettinger8ae46892003-10-12 19:09:37 +0000322 res = PyTuple_Pack(2, v, w);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000323 Py_DECREF(v);
324 Py_DECREF(w);
Guido van Rossum04691fc1992-08-12 15:35:34 +0000325 return res;
326}
327
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000328PyDoc_STRVAR(coerce_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000329"coerce(x, y) -> None or (x1, y1)\n\
330\n\
331When 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 +0000332containing the coerced values. When they can't be coerced, return None.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000333
334
Guido van Rossum79f25d91997-04-29 20:08:16 +0000335static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000336builtin_compile(PyObject *self, PyObject *args)
Guido van Rossum5b722181993-03-30 17:46:03 +0000337{
338 char *str;
339 char *filename;
340 char *startstr;
341 int start;
Tim Peters6cd6a822001-08-17 22:11:27 +0000342 int dont_inherit = 0;
343 int supplied_flags = 0;
Tim Peters5ba58662001-07-16 02:29:45 +0000344 PyCompilerFlags cf;
Just van Rossum3aaf42c2003-02-10 08:21:10 +0000345 PyObject *result, *cmd, *tmp = NULL;
Just van Rossumb9b8e9c2003-02-10 09:22:01 +0000346 int length;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000347
Just van Rossum3aaf42c2003-02-10 08:21:10 +0000348 if (!PyArg_ParseTuple(args, "Oss|ii:compile", &cmd, &filename,
Tim Peters6cd6a822001-08-17 22:11:27 +0000349 &startstr, &supplied_flags, &dont_inherit))
Guido van Rossum5b722181993-03-30 17:46:03 +0000350 return NULL;
Tim Peters6cd6a822001-08-17 22:11:27 +0000351
Just van Rossum3aaf42c2003-02-10 08:21:10 +0000352 cf.cf_flags = supplied_flags;
353
354#ifdef Py_USING_UNICODE
355 if (PyUnicode_Check(cmd)) {
356 tmp = PyUnicode_AsUTF8String(cmd);
357 if (tmp == NULL)
358 return NULL;
359 cmd = tmp;
360 cf.cf_flags |= PyCF_SOURCE_IS_UTF8;
361 }
362#endif
Just van Rossumb9b8e9c2003-02-10 09:22:01 +0000363 if (PyObject_AsReadBuffer(cmd, (const void **)&str, &length))
364 return NULL;
Tim Peters2c646c92003-02-10 14:48:29 +0000365 if ((size_t)length != strlen(str)) {
Just van Rossum3aaf42c2003-02-10 08:21:10 +0000366 PyErr_SetString(PyExc_TypeError,
Alex Martellia9b9c9f2003-04-23 13:34:35 +0000367 "compile() expected string without null bytes");
Just van Rossum3aaf42c2003-02-10 08:21:10 +0000368 return NULL;
369 }
370
Guido van Rossum5b722181993-03-30 17:46:03 +0000371 if (strcmp(startstr, "exec") == 0)
Guido van Rossumb05a5c71997-05-07 17:46:13 +0000372 start = Py_file_input;
Guido van Rossum5b722181993-03-30 17:46:03 +0000373 else if (strcmp(startstr, "eval") == 0)
Guido van Rossumb05a5c71997-05-07 17:46:13 +0000374 start = Py_eval_input;
Guido van Rossum872537c1995-07-07 22:43:42 +0000375 else if (strcmp(startstr, "single") == 0)
Guido van Rossumb05a5c71997-05-07 17:46:13 +0000376 start = Py_single_input;
Guido van Rossum5b722181993-03-30 17:46:03 +0000377 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000378 PyErr_SetString(PyExc_ValueError,
Fred Drake661ea262000-10-24 19:57:45 +0000379 "compile() arg 3 must be 'exec' or 'eval' or 'single'");
Guido van Rossum5b722181993-03-30 17:46:03 +0000380 return NULL;
381 }
Tim Peters6cd6a822001-08-17 22:11:27 +0000382
Guido van Rossum4b499dd32003-02-13 22:07:59 +0000383 if (supplied_flags &
384 ~(PyCF_MASK | PyCF_MASK_OBSOLETE | PyCF_DONT_IMPLY_DEDENT))
385 {
Tim Peters6cd6a822001-08-17 22:11:27 +0000386 PyErr_SetString(PyExc_ValueError,
387 "compile(): unrecognised flags");
388 return NULL;
389 }
390 /* XXX Warn if (supplied_flags & PyCF_MASK_OBSOLETE) != 0? */
391
Tim Peters6cd6a822001-08-17 22:11:27 +0000392 if (!dont_inherit) {
393 PyEval_MergeCompilerFlags(&cf);
394 }
Just van Rossum3aaf42c2003-02-10 08:21:10 +0000395 result = Py_CompileStringFlags(str, filename, start, &cf);
396 Py_XDECREF(tmp);
397 return result;
Guido van Rossum5b722181993-03-30 17:46:03 +0000398}
399
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000400PyDoc_STRVAR(compile_doc,
Tim Peters6cd6a822001-08-17 22:11:27 +0000401"compile(source, filename, mode[, flags[, dont_inherit]]) -> code object\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000402\n\
403Compile the source string (a Python module, statement or expression)\n\
404into a code object that can be executed by the exec statement or eval().\n\
405The filename will be used for run-time error messages.\n\
406The mode must be 'exec' to compile a module, 'single' to compile a\n\
Tim Peters6cd6a822001-08-17 22:11:27 +0000407single (interactive) statement, or 'eval' to compile an expression.\n\
408The flags argument, if present, controls which future statements influence\n\
409the compilation of the code.\n\
410The dont_inherit argument, if non-zero, stops the compilation inheriting\n\
411the effects of any future statements in effect in the code calling\n\
412compile; if absent or zero these statements do influence the compilation,\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000413in addition to any features explicitly specified.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000414
Guido van Rossum79f25d91997-04-29 20:08:16 +0000415static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000416builtin_dir(PyObject *self, PyObject *args)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000417{
Tim Peters5d2b77c2001-09-03 05:47:38 +0000418 PyObject *arg = NULL;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000419
Raymond Hettingerea3fdf42002-12-29 16:33:45 +0000420 if (!PyArg_UnpackTuple(args, "dir", 0, 1, &arg))
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000421 return NULL;
Tim Peters7eea37e2001-09-04 22:08:56 +0000422 return PyObject_Dir(arg);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000423}
424
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000425PyDoc_STRVAR(dir_doc,
Tim Peters5d2b77c2001-09-03 05:47:38 +0000426"dir([object]) -> list of strings\n"
427"\n"
428"Return an alphabetized list of names comprising (some of) the attributes\n"
429"of the given object, and of attributes reachable from it:\n"
430"\n"
431"No argument: the names in the current scope.\n"
432"Module object: the module attributes.\n"
Tim Peters37a309d2001-09-04 01:20:04 +0000433"Type or class object: its attributes, and recursively the attributes of\n"
434" its bases.\n"
Tim Peters5d2b77c2001-09-03 05:47:38 +0000435"Otherwise: its attributes, its class's attributes, and recursively the\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000436" attributes of its class's base classes.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000437
Guido van Rossum79f25d91997-04-29 20:08:16 +0000438static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000439builtin_divmod(PyObject *self, PyObject *args)
Guido van Rossum6a00cd81995-01-07 12:39:01 +0000440{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000441 PyObject *v, *w;
Guido van Rossum6a00cd81995-01-07 12:39:01 +0000442
Raymond Hettingerea3fdf42002-12-29 16:33:45 +0000443 if (!PyArg_UnpackTuple(args, "divmod", 2, 2, &v, &w))
Guido van Rossum6a00cd81995-01-07 12:39:01 +0000444 return NULL;
Guido van Rossum09df08a1998-05-22 00:51:39 +0000445 return PyNumber_Divmod(v, w);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000446}
447
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000448PyDoc_STRVAR(divmod_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000449"divmod(x, y) -> (div, mod)\n\
450\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000451Return the tuple ((x-x%y)/y, x%y). Invariant: div*y + mod == x.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000452
453
Guido van Rossum79f25d91997-04-29 20:08:16 +0000454static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000455builtin_eval(PyObject *self, PyObject *args)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000456{
Just van Rossum3aaf42c2003-02-10 08:21:10 +0000457 PyObject *cmd, *result, *tmp = NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000458 PyObject *globals = Py_None, *locals = Py_None;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000459 char *str;
Tim Peters9fa96be2001-08-17 23:04:59 +0000460 PyCompilerFlags cf;
Guido van Rossum590baa41993-11-30 13:40:46 +0000461
Guido van Rossum79f25d91997-04-29 20:08:16 +0000462 if (!PyArg_ParseTuple(args, "O|O!O!:eval",
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000463 &cmd,
Guido van Rossum79f25d91997-04-29 20:08:16 +0000464 &PyDict_Type, &globals,
465 &PyDict_Type, &locals))
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000466 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000467 if (globals == Py_None) {
468 globals = PyEval_GetGlobals();
469 if (locals == Py_None)
470 locals = PyEval_GetLocals();
Guido van Rossum6135a871995-01-09 17:53:26 +0000471 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000472 else if (locals == Py_None)
Guido van Rossum6135a871995-01-09 17:53:26 +0000473 locals = globals;
Tim Peters9fa96be2001-08-17 23:04:59 +0000474
Guido van Rossum79f25d91997-04-29 20:08:16 +0000475 if (PyDict_GetItemString(globals, "__builtins__") == NULL) {
476 if (PyDict_SetItemString(globals, "__builtins__",
477 PyEval_GetBuiltins()) != 0)
Guido van Rossum6135a871995-01-09 17:53:26 +0000478 return NULL;
479 }
Tim Peters9fa96be2001-08-17 23:04:59 +0000480
Jeremy Hylton15c1c4f2001-07-30 21:50:55 +0000481 if (PyCode_Check(cmd)) {
Jeremy Hylton733c8932001-12-13 19:51:56 +0000482 if (PyCode_GetNumFree((PyCodeObject *)cmd) > 0) {
Jeremy Hylton15c1c4f2001-07-30 21:50:55 +0000483 PyErr_SetString(PyExc_TypeError,
484 "code object passed to eval() may not contain free variables");
485 return NULL;
486 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000487 return PyEval_EvalCode((PyCodeObject *) cmd, globals, locals);
Jeremy Hylton15c1c4f2001-07-30 21:50:55 +0000488 }
Tim Peters9fa96be2001-08-17 23:04:59 +0000489
Marc-André Lemburgd1ba4432000-09-19 21:04:18 +0000490 if (!PyString_Check(cmd) &&
491 !PyUnicode_Check(cmd)) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000492 PyErr_SetString(PyExc_TypeError,
Fred Drake661ea262000-10-24 19:57:45 +0000493 "eval() arg 1 must be a string or code object");
Guido van Rossum94390a41992-08-14 15:14:30 +0000494 return NULL;
495 }
Just van Rossum3aaf42c2003-02-10 08:21:10 +0000496 cf.cf_flags = 0;
497
498#ifdef Py_USING_UNICODE
499 if (PyUnicode_Check(cmd)) {
500 tmp = PyUnicode_AsUTF8String(cmd);
501 if (tmp == NULL)
502 return NULL;
503 cmd = tmp;
504 cf.cf_flags |= PyCF_SOURCE_IS_UTF8;
505 }
506#endif
Marc-André Lemburgd1ba4432000-09-19 21:04:18 +0000507 if (PyString_AsStringAndSize(cmd, &str, NULL))
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000508 return NULL;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000509 while (*str == ' ' || *str == '\t')
510 str++;
Tim Peters9fa96be2001-08-17 23:04:59 +0000511
Tim Peters9fa96be2001-08-17 23:04:59 +0000512 (void)PyEval_MergeCompilerFlags(&cf);
Just van Rossum3aaf42c2003-02-10 08:21:10 +0000513 result = PyRun_StringFlags(str, Py_eval_input, globals, locals, &cf);
514 Py_XDECREF(tmp);
515 return result;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000516}
517
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000518PyDoc_STRVAR(eval_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000519"eval(source[, globals[, locals]]) -> value\n\
520\n\
521Evaluate the source in the context of globals and locals.\n\
522The source may be a string representing a Python expression\n\
523or a code object as returned by compile().\n\
524The globals and locals are dictionaries, defaulting to the current\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000525globals and locals. If only globals is given, locals defaults to it.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000526
527
Guido van Rossum79f25d91997-04-29 20:08:16 +0000528static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000529builtin_execfile(PyObject *self, PyObject *args)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000530{
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000531 char *filename;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000532 PyObject *globals = Py_None, *locals = Py_None;
533 PyObject *res;
Guido van Rossumb3a639e2001-09-05 13:37:47 +0000534 FILE* fp = NULL;
Tim Peters5ba58662001-07-16 02:29:45 +0000535 PyCompilerFlags cf;
Martin v. Löwis6b3a2c42001-08-08 05:30:36 +0000536 int exists;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000537
Guido van Rossum79f25d91997-04-29 20:08:16 +0000538 if (!PyArg_ParseTuple(args, "s|O!O!:execfile",
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000539 &filename,
Guido van Rossum79f25d91997-04-29 20:08:16 +0000540 &PyDict_Type, &globals,
541 &PyDict_Type, &locals))
Guido van Rossum0f61f8a1992-02-25 18:55:05 +0000542 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000543 if (globals == Py_None) {
544 globals = PyEval_GetGlobals();
545 if (locals == Py_None)
546 locals = PyEval_GetLocals();
Guido van Rossum6135a871995-01-09 17:53:26 +0000547 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000548 else if (locals == Py_None)
Guido van Rossum6135a871995-01-09 17:53:26 +0000549 locals = globals;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000550 if (PyDict_GetItemString(globals, "__builtins__") == NULL) {
551 if (PyDict_SetItemString(globals, "__builtins__",
552 PyEval_GetBuiltins()) != 0)
Guido van Rossum6135a871995-01-09 17:53:26 +0000553 return NULL;
554 }
Martin v. Löwis6b3a2c42001-08-08 05:30:36 +0000555
556 exists = 0;
557 /* Test for existence or directory. */
Martin v. Löwis3484a182002-03-09 12:07:51 +0000558#if defined(PLAN9)
559 {
560 Dir *d;
561
562 if ((d = dirstat(filename))!=nil) {
563 if(d->mode & DMDIR)
564 werrstr("is a directory");
565 else
566 exists = 1;
567 free(d);
568 }
Martin v. Löwis6b3a2c42001-08-08 05:30:36 +0000569 }
Martin v. Löwis3484a182002-03-09 12:07:51 +0000570#elif defined(RISCOS)
Guido van Rossume2ae77b2001-10-24 20:42:55 +0000571 if (object_exists(filename)) {
572 if (isdir(filename))
573 errno = EISDIR;
574 else
575 exists = 1;
576 }
Martin v. Löwis3484a182002-03-09 12:07:51 +0000577#else /* standard Posix */
578 {
579 struct stat s;
580 if (stat(filename, &s) == 0) {
581 if (S_ISDIR(s.st_mode))
582# if defined(PY_OS2) && defined(PYCC_VACPP)
583 errno = EOS2ERR;
584# else
585 errno = EISDIR;
586# endif
587 else
588 exists = 1;
589 }
590 }
591#endif
Martin v. Löwis6b3a2c42001-08-08 05:30:36 +0000592
593 if (exists) {
594 Py_BEGIN_ALLOW_THREADS
Jack Jansen7b8c7542002-04-14 20:12:41 +0000595 fp = fopen(filename, "r" PY_STDIOTEXTMODE);
Martin v. Löwis6b3a2c42001-08-08 05:30:36 +0000596 Py_END_ALLOW_THREADS
597
598 if (fp == NULL) {
599 exists = 0;
600 }
601 }
602
603 if (!exists) {
Peter Schneider-Kamp4c013422002-08-27 16:58:00 +0000604 PyErr_SetFromErrnoWithFilename(PyExc_IOError, filename);
Guido van Rossum0f61f8a1992-02-25 18:55:05 +0000605 return NULL;
606 }
Tim Peters5ba58662001-07-16 02:29:45 +0000607 cf.cf_flags = 0;
608 if (PyEval_MergeCompilerFlags(&cf))
Tim Peters748b8bb2001-04-28 08:20:22 +0000609 res = PyRun_FileExFlags(fp, filename, Py_file_input, globals,
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000610 locals, 1, &cf);
Tim Peters5ba58662001-07-16 02:29:45 +0000611 else
Tim Peters748b8bb2001-04-28 08:20:22 +0000612 res = PyRun_FileEx(fp, filename, Py_file_input, globals,
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000613 locals, 1);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000614 return res;
Guido van Rossum0f61f8a1992-02-25 18:55:05 +0000615}
616
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000617PyDoc_STRVAR(execfile_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000618"execfile(filename[, globals[, locals]])\n\
619\n\
620Read and execute a Python script from a file.\n\
621The globals and locals are dictionaries, defaulting to the current\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000622globals and locals. If only globals is given, locals defaults to it.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000623
624
Guido van Rossum79f25d91997-04-29 20:08:16 +0000625static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000626builtin_getattr(PyObject *self, PyObject *args)
Guido van Rossum33894be1992-01-27 16:53:09 +0000627{
Guido van Rossum950ff291998-06-29 13:38:57 +0000628 PyObject *v, *result, *dflt = NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000629 PyObject *name;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000630
Raymond Hettingerea3fdf42002-12-29 16:33:45 +0000631 if (!PyArg_UnpackTuple(args, "getattr", 2, 3, &v, &name, &dflt))
Guido van Rossum33894be1992-01-27 16:53:09 +0000632 return NULL;
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000633#ifdef Py_USING_UNICODE
Jeremy Hylton0eb11152001-07-30 22:39:31 +0000634 if (PyUnicode_Check(name)) {
635 name = _PyUnicode_AsDefaultEncodedString(name, NULL);
636 if (name == NULL)
637 return NULL;
638 }
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000639#endif
Jeremy Hylton0eb11152001-07-30 22:39:31 +0000640
641 if (!PyString_Check(name)) {
642 PyErr_SetString(PyExc_TypeError,
Alex Martellia9b9c9f2003-04-23 13:34:35 +0000643 "getattr(): attribute name must be string");
Jeremy Hylton0eb11152001-07-30 22:39:31 +0000644 return NULL;
645 }
Guido van Rossum950ff291998-06-29 13:38:57 +0000646 result = PyObject_GetAttr(v, name);
Guido van Rossumd8923572001-10-16 21:31:32 +0000647 if (result == NULL && dflt != NULL &&
648 PyErr_ExceptionMatches(PyExc_AttributeError))
649 {
Guido van Rossum950ff291998-06-29 13:38:57 +0000650 PyErr_Clear();
651 Py_INCREF(dflt);
652 result = dflt;
653 }
654 return result;
Guido van Rossum9bfef441993-03-29 10:43:31 +0000655}
656
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000657PyDoc_STRVAR(getattr_doc,
Guido van Rossum950ff291998-06-29 13:38:57 +0000658"getattr(object, name[, default]) -> value\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000659\n\
Guido van Rossum950ff291998-06-29 13:38:57 +0000660Get a named attribute from an object; getattr(x, 'y') is equivalent to x.y.\n\
661When a default argument is given, it is returned when the attribute doesn't\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000662exist; without it, an exception is raised in that case.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000663
664
Guido van Rossum79f25d91997-04-29 20:08:16 +0000665static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000666builtin_globals(PyObject *self)
Guido van Rossum872537c1995-07-07 22:43:42 +0000667{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000668 PyObject *d;
Guido van Rossum872537c1995-07-07 22:43:42 +0000669
Guido van Rossum79f25d91997-04-29 20:08:16 +0000670 d = PyEval_GetGlobals();
671 Py_INCREF(d);
Guido van Rossum872537c1995-07-07 22:43:42 +0000672 return d;
673}
674
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000675PyDoc_STRVAR(globals_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000676"globals() -> dictionary\n\
677\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000678Return the dictionary containing the current scope's global variables.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000679
680
Guido van Rossum79f25d91997-04-29 20:08:16 +0000681static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000682builtin_hasattr(PyObject *self, PyObject *args)
Guido van Rossum9bfef441993-03-29 10:43:31 +0000683{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000684 PyObject *v;
685 PyObject *name;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000686
Raymond Hettingerea3fdf42002-12-29 16:33:45 +0000687 if (!PyArg_UnpackTuple(args, "hasattr", 2, 2, &v, &name))
Guido van Rossum9bfef441993-03-29 10:43:31 +0000688 return NULL;
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000689#ifdef Py_USING_UNICODE
Jeremy Hylton302b54a2001-07-30 22:45:19 +0000690 if (PyUnicode_Check(name)) {
691 name = _PyUnicode_AsDefaultEncodedString(name, NULL);
692 if (name == NULL)
693 return NULL;
694 }
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000695#endif
Jeremy Hylton302b54a2001-07-30 22:45:19 +0000696
697 if (!PyString_Check(name)) {
698 PyErr_SetString(PyExc_TypeError,
Alex Martellia9b9c9f2003-04-23 13:34:35 +0000699 "hasattr(): attribute name must be string");
Jeremy Hylton302b54a2001-07-30 22:45:19 +0000700 return NULL;
701 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000702 v = PyObject_GetAttr(v, name);
Guido van Rossum9bfef441993-03-29 10:43:31 +0000703 if (v == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000704 PyErr_Clear();
Guido van Rossum09df08a1998-05-22 00:51:39 +0000705 Py_INCREF(Py_False);
706 return Py_False;
Guido van Rossum9bfef441993-03-29 10:43:31 +0000707 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000708 Py_DECREF(v);
Guido van Rossum09df08a1998-05-22 00:51:39 +0000709 Py_INCREF(Py_True);
710 return Py_True;
Guido van Rossum33894be1992-01-27 16:53:09 +0000711}
712
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000713PyDoc_STRVAR(hasattr_doc,
Guido van Rossum77f6a652002-04-03 22:41:51 +0000714"hasattr(object, name) -> bool\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000715\n\
716Return whether the object has an attribute with the given name.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000717(This is done by calling getattr(object, name) and catching exceptions.)");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000718
719
Guido van Rossum79f25d91997-04-29 20:08:16 +0000720static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000721builtin_id(PyObject *self, PyObject *v)
Guido van Rossum5b722181993-03-30 17:46:03 +0000722{
Guido van Rossum106f2da2000-06-28 21:12:25 +0000723 return PyLong_FromVoidPtr(v);
Guido van Rossum5b722181993-03-30 17:46:03 +0000724}
725
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000726PyDoc_STRVAR(id_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000727"id(object) -> integer\n\
728\n\
729Return the identity of an object. This is guaranteed to be unique among\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000730simultaneously existing objects. (Hint: it's the object's memory address.)");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000731
732
Guido van Rossum79f25d91997-04-29 20:08:16 +0000733static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000734builtin_map(PyObject *self, PyObject *args)
Guido van Rossum12d12c51993-10-26 17:58:25 +0000735{
736 typedef struct {
Tim Peters4e9afdc2001-05-03 23:54:49 +0000737 PyObject *it; /* the iterator object */
738 int saw_StopIteration; /* bool: did the iterator end? */
Guido van Rossum12d12c51993-10-26 17:58:25 +0000739 } sequence;
740
Guido van Rossum79f25d91997-04-29 20:08:16 +0000741 PyObject *func, *result;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000742 sequence *seqs = NULL, *sqp;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000743 int n, len;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000744 register int i, j;
745
Guido van Rossum79f25d91997-04-29 20:08:16 +0000746 n = PyTuple_Size(args);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000747 if (n < 2) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000748 PyErr_SetString(PyExc_TypeError,
749 "map() requires at least two args");
Guido van Rossum12d12c51993-10-26 17:58:25 +0000750 return NULL;
751 }
752
Guido van Rossum79f25d91997-04-29 20:08:16 +0000753 func = PyTuple_GetItem(args, 0);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000754 n--;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000755
Guido van Rossumfa4ac711998-07-10 17:37:30 +0000756 if (func == Py_None && n == 1) {
757 /* map(None, S) is the same as list(S). */
758 return PySequence_List(PyTuple_GetItem(args, 1));
759 }
760
Tim Peters4e9afdc2001-05-03 23:54:49 +0000761 /* Get space for sequence descriptors. Must NULL out the iterator
762 * pointers so that jumping to Fail_2 later doesn't see trash.
763 */
Guido van Rossum79f25d91997-04-29 20:08:16 +0000764 if ((seqs = PyMem_NEW(sequence, n)) == NULL) {
765 PyErr_NoMemory();
Tim Peters4e9afdc2001-05-03 23:54:49 +0000766 return NULL;
767 }
768 for (i = 0; i < n; ++i) {
769 seqs[i].it = (PyObject*)NULL;
770 seqs[i].saw_StopIteration = 0;
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000771 }
Guido van Rossum12d12c51993-10-26 17:58:25 +0000772
Tim Peters4e9afdc2001-05-03 23:54:49 +0000773 /* Do a first pass to obtain iterators for the arguments, and set len
774 * to the largest of their lengths.
Tim Peters748b8bb2001-04-28 08:20:22 +0000775 */
Tim Peters4e9afdc2001-05-03 23:54:49 +0000776 len = 0;
777 for (i = 0, sqp = seqs; i < n; ++i, ++sqp) {
778 PyObject *curseq;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000779 int curlen;
Guido van Rossumad991772001-01-12 16:03:05 +0000780
Tim Peters4e9afdc2001-05-03 23:54:49 +0000781 /* Get iterator. */
782 curseq = PyTuple_GetItem(args, i+1);
783 sqp->it = PyObject_GetIter(curseq);
784 if (sqp->it == NULL) {
Guido van Rossum12d12c51993-10-26 17:58:25 +0000785 static char errmsg[] =
Tim Peters4e9afdc2001-05-03 23:54:49 +0000786 "argument %d to map() must support iteration";
Guido van Rossum15e33a41997-04-30 19:00:27 +0000787 char errbuf[sizeof(errmsg) + 25];
Jeremy Hylton518ab1c2001-11-28 20:42:20 +0000788 PyOS_snprintf(errbuf, sizeof(errbuf), errmsg, i+2);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000789 PyErr_SetString(PyExc_TypeError, errbuf);
Guido van Rossum12d12c51993-10-26 17:58:25 +0000790 goto Fail_2;
791 }
792
Tim Peters4e9afdc2001-05-03 23:54:49 +0000793 /* Update len. */
794 curlen = -1; /* unknown */
795 if (PySequence_Check(curseq) &&
796 curseq->ob_type->tp_as_sequence->sq_length) {
797 curlen = PySequence_Size(curseq);
798 if (curlen < 0)
799 PyErr_Clear();
800 }
Tim Peters748b8bb2001-04-28 08:20:22 +0000801 if (curlen < 0)
Tim Peters4e9afdc2001-05-03 23:54:49 +0000802 curlen = 8; /* arbitrary */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000803 if (curlen > len)
804 len = curlen;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000805 }
806
Tim Peters4e9afdc2001-05-03 23:54:49 +0000807 /* Get space for the result list. */
Guido van Rossum79f25d91997-04-29 20:08:16 +0000808 if ((result = (PyObject *) PyList_New(len)) == NULL)
Guido van Rossum12d12c51993-10-26 17:58:25 +0000809 goto Fail_2;
810
Tim Peters4e9afdc2001-05-03 23:54:49 +0000811 /* Iterate over the sequences until all have stopped. */
Guido van Rossum2d951851994-08-29 12:52:16 +0000812 for (i = 0; ; ++i) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000813 PyObject *alist, *item=NULL, *value;
Tim Peters4e9afdc2001-05-03 23:54:49 +0000814 int numactive = 0;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000815
Guido van Rossum79f25d91997-04-29 20:08:16 +0000816 if (func == Py_None && n == 1)
Guido van Rossum32120311995-07-10 13:52:21 +0000817 alist = NULL;
Tim Peters4e9afdc2001-05-03 23:54:49 +0000818 else if ((alist = PyTuple_New(n)) == NULL)
819 goto Fail_1;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000820
821 for (j = 0, sqp = seqs; j < n; ++j, ++sqp) {
Tim Peters4e9afdc2001-05-03 23:54:49 +0000822 if (sqp->saw_StopIteration) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000823 Py_INCREF(Py_None);
824 item = Py_None;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000825 }
Guido van Rossum12d12c51993-10-26 17:58:25 +0000826 else {
Tim Peters4e9afdc2001-05-03 23:54:49 +0000827 item = PyIter_Next(sqp->it);
828 if (item)
829 ++numactive;
830 else {
Tim Peters4e9afdc2001-05-03 23:54:49 +0000831 if (PyErr_Occurred()) {
Tim Petersf4848da2001-05-05 00:14:56 +0000832 Py_XDECREF(alist);
833 goto Fail_1;
Guido van Rossum2d951851994-08-29 12:52:16 +0000834 }
Tim Peters4e9afdc2001-05-03 23:54:49 +0000835 Py_INCREF(Py_None);
836 item = Py_None;
837 sqp->saw_StopIteration = 1;
Guido van Rossum2d951851994-08-29 12:52:16 +0000838 }
Guido van Rossum12d12c51993-10-26 17:58:25 +0000839 }
Tim Peters4e9afdc2001-05-03 23:54:49 +0000840 if (alist)
841 PyTuple_SET_ITEM(alist, j, item);
842 else
Guido van Rossum2d951851994-08-29 12:52:16 +0000843 break;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000844 }
845
Guido van Rossum32120311995-07-10 13:52:21 +0000846 if (!alist)
847 alist = item;
Guido van Rossum2d951851994-08-29 12:52:16 +0000848
Tim Peters4e9afdc2001-05-03 23:54:49 +0000849 if (numactive == 0) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000850 Py_DECREF(alist);
Guido van Rossum2d951851994-08-29 12:52:16 +0000851 break;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000852 }
Guido van Rossum2d951851994-08-29 12:52:16 +0000853
Guido van Rossum79f25d91997-04-29 20:08:16 +0000854 if (func == Py_None)
Guido van Rossum32120311995-07-10 13:52:21 +0000855 value = alist;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000856 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000857 value = PyEval_CallObject(func, alist);
858 Py_DECREF(alist);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000859 if (value == NULL)
860 goto Fail_1;
Guido van Rossum2d951851994-08-29 12:52:16 +0000861 }
862 if (i >= len) {
Barry Warsawfa77e091999-01-28 18:49:12 +0000863 int status = PyList_Append(result, value);
Barry Warsaw21332871999-01-28 04:21:35 +0000864 Py_DECREF(value);
Barry Warsawfa77e091999-01-28 18:49:12 +0000865 if (status < 0)
866 goto Fail_1;
Guido van Rossum2d951851994-08-29 12:52:16 +0000867 }
Tim Peters4e9afdc2001-05-03 23:54:49 +0000868 else if (PyList_SetItem(result, i, value) < 0)
869 goto Fail_1;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000870 }
871
Guido van Rossumfa4ac711998-07-10 17:37:30 +0000872 if (i < len && PyList_SetSlice(result, i, len, NULL) < 0)
873 goto Fail_1;
874
Tim Peters4e9afdc2001-05-03 23:54:49 +0000875 goto Succeed;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000876
Guido van Rossum12d12c51993-10-26 17:58:25 +0000877Fail_1:
Guido van Rossum79f25d91997-04-29 20:08:16 +0000878 Py_DECREF(result);
Guido van Rossum12d12c51993-10-26 17:58:25 +0000879Fail_2:
Tim Peters4e9afdc2001-05-03 23:54:49 +0000880 result = NULL;
881Succeed:
882 assert(seqs);
883 for (i = 0; i < n; ++i)
884 Py_XDECREF(seqs[i].it);
885 PyMem_DEL(seqs);
886 return result;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000887}
888
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000889PyDoc_STRVAR(map_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000890"map(function, sequence[, sequence, ...]) -> list\n\
891\n\
892Return a list of the results of applying the function to the items of\n\
893the argument sequence(s). If more than one sequence is given, the\n\
894function is called with an argument list consisting of the corresponding\n\
895item of each sequence, substituting None for missing values when not all\n\
896sequences have the same length. If the function is None, return a list of\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000897the items of the sequence (or a list of tuples if more than one sequence).");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000898
899
Guido van Rossum79f25d91997-04-29 20:08:16 +0000900static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000901builtin_setattr(PyObject *self, PyObject *args)
Guido van Rossum33894be1992-01-27 16:53:09 +0000902{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000903 PyObject *v;
904 PyObject *name;
905 PyObject *value;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000906
Raymond Hettingerea3fdf42002-12-29 16:33:45 +0000907 if (!PyArg_UnpackTuple(args, "setattr", 3, 3, &v, &name, &value))
Guido van Rossum33894be1992-01-27 16:53:09 +0000908 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000909 if (PyObject_SetAttr(v, name, value) != 0)
Guido van Rossum33894be1992-01-27 16:53:09 +0000910 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000911 Py_INCREF(Py_None);
912 return Py_None;
Guido van Rossum33894be1992-01-27 16:53:09 +0000913}
914
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000915PyDoc_STRVAR(setattr_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000916"setattr(object, name, value)\n\
917\n\
918Set a named attribute on an object; setattr(x, 'y', v) is equivalent to\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000919``x.y = v''.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000920
921
Guido van Rossum79f25d91997-04-29 20:08:16 +0000922static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000923builtin_delattr(PyObject *self, PyObject *args)
Guido van Rossum14144fc1994-08-29 12:53:40 +0000924{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000925 PyObject *v;
926 PyObject *name;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000927
Raymond Hettingerea3fdf42002-12-29 16:33:45 +0000928 if (!PyArg_UnpackTuple(args, "delattr", 2, 2, &v, &name))
Guido van Rossum14144fc1994-08-29 12:53:40 +0000929 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000930 if (PyObject_SetAttr(v, name, (PyObject *)NULL) != 0)
Guido van Rossum14144fc1994-08-29 12:53:40 +0000931 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000932 Py_INCREF(Py_None);
933 return Py_None;
Guido van Rossum14144fc1994-08-29 12:53:40 +0000934}
935
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000936PyDoc_STRVAR(delattr_doc,
Guido van Rossumdf12a591998-11-23 22:13:04 +0000937"delattr(object, name)\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000938\n\
939Delete a named attribute on an object; delattr(x, 'y') is equivalent to\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000940``del x.y''.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000941
942
Guido van Rossum79f25d91997-04-29 20:08:16 +0000943static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000944builtin_hash(PyObject *self, PyObject *v)
Guido van Rossum9bfef441993-03-29 10:43:31 +0000945{
Guido van Rossum9bfef441993-03-29 10:43:31 +0000946 long x;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000947
Guido van Rossum79f25d91997-04-29 20:08:16 +0000948 x = PyObject_Hash(v);
Guido van Rossum9bfef441993-03-29 10:43:31 +0000949 if (x == -1)
950 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000951 return PyInt_FromLong(x);
Guido van Rossum9bfef441993-03-29 10:43:31 +0000952}
953
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000954PyDoc_STRVAR(hash_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000955"hash(object) -> integer\n\
956\n\
957Return a hash value for the object. Two objects with the same value have\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000958the same hash value. The reverse is not necessarily true, but likely.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000959
960
Guido van Rossum79f25d91997-04-29 20:08:16 +0000961static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000962builtin_hex(PyObject *self, PyObject *v)
Guido van Rossum006bcd41991-10-24 14:54:44 +0000963{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000964 PyNumberMethods *nb;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000965
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000966 if ((nb = v->ob_type->tp_as_number) == NULL ||
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000967 nb->nb_hex == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000968 PyErr_SetString(PyExc_TypeError,
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000969 "hex() argument can't be converted to hex");
970 return NULL;
Guido van Rossum006bcd41991-10-24 14:54:44 +0000971 }
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000972 return (*nb->nb_hex)(v);
Guido van Rossum006bcd41991-10-24 14:54:44 +0000973}
974
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000975PyDoc_STRVAR(hex_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000976"hex(number) -> string\n\
977\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000978Return the hexadecimal representation of an integer or long integer.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000979
980
Tim Petersdbd9ba62000-07-09 03:09:57 +0000981static PyObject *builtin_raw_input(PyObject *, PyObject *);
Guido van Rossum3165fe61992-09-25 21:59:05 +0000982
Guido van Rossum79f25d91997-04-29 20:08:16 +0000983static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000984builtin_input(PyObject *self, PyObject *args)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000985{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000986 PyObject *line;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000987 char *str;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000988 PyObject *res;
989 PyObject *globals, *locals;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000990
991 line = builtin_raw_input(self, args);
Guido van Rossum3165fe61992-09-25 21:59:05 +0000992 if (line == NULL)
993 return line;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000994 if (!PyArg_Parse(line, "s;embedded '\\0' in input line", &str))
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000995 return NULL;
996 while (*str == ' ' || *str == '\t')
997 str++;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000998 globals = PyEval_GetGlobals();
999 locals = PyEval_GetLocals();
1000 if (PyDict_GetItemString(globals, "__builtins__") == NULL) {
1001 if (PyDict_SetItemString(globals, "__builtins__",
1002 PyEval_GetBuiltins()) != 0)
Guido van Rossum6135a871995-01-09 17:53:26 +00001003 return NULL;
1004 }
Guido van Rossumb05a5c71997-05-07 17:46:13 +00001005 res = PyRun_String(str, Py_eval_input, globals, locals);
Guido van Rossum79f25d91997-04-29 20:08:16 +00001006 Py_DECREF(line);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001007 return res;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001008}
1009
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001010PyDoc_STRVAR(input_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001011"input([prompt]) -> value\n\
1012\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001013Equivalent to eval(raw_input(prompt)).");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001014
1015
Guido van Rossume8811f81997-02-14 15:48:05 +00001016static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001017builtin_intern(PyObject *self, PyObject *args)
Guido van Rossume8811f81997-02-14 15:48:05 +00001018{
1019 PyObject *s;
Guido van Rossum43713e52000-02-29 13:59:29 +00001020 if (!PyArg_ParseTuple(args, "S:intern", &s))
Guido van Rossume8811f81997-02-14 15:48:05 +00001021 return NULL;
1022 Py_INCREF(s);
1023 PyString_InternInPlace(&s);
1024 return s;
1025}
1026
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001027PyDoc_STRVAR(intern_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001028"intern(string) -> string\n\
1029\n\
1030``Intern'' the given string. This enters the string in the (global)\n\
1031table of interned strings whose purpose is to speed up dictionary lookups.\n\
1032Return the string itself or the previously interned string object with the\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001033same value.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001034
1035
Guido van Rossum79f25d91997-04-29 20:08:16 +00001036static PyObject *
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001037builtin_iter(PyObject *self, PyObject *args)
1038{
1039 PyObject *v, *w = NULL;
1040
Raymond Hettingerea3fdf42002-12-29 16:33:45 +00001041 if (!PyArg_UnpackTuple(args, "iter", 1, 2, &v, &w))
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001042 return NULL;
1043 if (w == NULL)
1044 return PyObject_GetIter(v);
1045 if (!PyCallable_Check(v)) {
1046 PyErr_SetString(PyExc_TypeError,
1047 "iter(v, w): v must be callable");
1048 return NULL;
1049 }
1050 return PyCallIter_New(v, w);
1051}
1052
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001053PyDoc_STRVAR(iter_doc,
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001054"iter(collection) -> iterator\n\
1055iter(callable, sentinel) -> iterator\n\
1056\n\
1057Get an iterator from an object. In the first form, the argument must\n\
1058supply its own iterator, or be a sequence.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001059In the second form, the callable is called until it returns the sentinel.");
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001060
1061
1062static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001063builtin_len(PyObject *self, PyObject *v)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001064{
Guido van Rossum8ea9f4d1998-06-29 22:26:50 +00001065 long res;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001066
Jeremy Hylton03657cf2000-07-12 13:05:33 +00001067 res = PyObject_Size(v);
Guido van Rossum8ea9f4d1998-06-29 22:26:50 +00001068 if (res < 0 && PyErr_Occurred())
1069 return NULL;
1070 return PyInt_FromLong(res);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001071}
1072
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001073PyDoc_STRVAR(len_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001074"len(object) -> integer\n\
1075\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001076Return the number of items of a sequence or mapping.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001077
1078
Guido van Rossum79f25d91997-04-29 20:08:16 +00001079static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001080builtin_locals(PyObject *self)
Guido van Rossum872537c1995-07-07 22:43:42 +00001081{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001082 PyObject *d;
Guido van Rossum872537c1995-07-07 22:43:42 +00001083
Guido van Rossum79f25d91997-04-29 20:08:16 +00001084 d = PyEval_GetLocals();
1085 Py_INCREF(d);
Guido van Rossum872537c1995-07-07 22:43:42 +00001086 return d;
1087}
1088
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001089PyDoc_STRVAR(locals_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001090"locals() -> dictionary\n\
1091\n\
Raymond Hettinger69bf8f32003-01-04 02:16:22 +00001092Update and return a dictionary containing the current scope's local variables.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001093
1094
Guido van Rossum79f25d91997-04-29 20:08:16 +00001095static PyObject *
Guido van Rossum53451b32001-01-17 15:47:24 +00001096min_max(PyObject *args, int op)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001097{
Tim Petersc3074532001-05-03 07:00:32 +00001098 PyObject *v, *w, *x, *it;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001099
Guido van Rossum79f25d91997-04-29 20:08:16 +00001100 if (PyTuple_Size(args) > 1)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001101 v = args;
Raymond Hettingerbbfb9102002-12-29 18:31:19 +00001102 else if (!PyArg_UnpackTuple(args, (op==Py_LT) ? "min" : "max", 1, 1, &v))
Guido van Rossum3f5da241990-12-20 15:06:42 +00001103 return NULL;
Tim Peters67d687a2002-04-29 21:27:32 +00001104
Tim Petersc3074532001-05-03 07:00:32 +00001105 it = PyObject_GetIter(v);
1106 if (it == NULL)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001107 return NULL;
Tim Petersc3074532001-05-03 07:00:32 +00001108
1109 w = NULL; /* the result */
Tim Petersf4848da2001-05-05 00:14:56 +00001110 for (;;) {
Tim Petersc3074532001-05-03 07:00:32 +00001111 x = PyIter_Next(it);
Guido van Rossum2d951851994-08-29 12:52:16 +00001112 if (x == NULL) {
Tim Petersc3074532001-05-03 07:00:32 +00001113 if (PyErr_Occurred()) {
Tim Petersf4848da2001-05-05 00:14:56 +00001114 Py_XDECREF(w);
1115 Py_DECREF(it);
1116 return NULL;
Guido van Rossum2d951851994-08-29 12:52:16 +00001117 }
Tim Petersc3074532001-05-03 07:00:32 +00001118 break;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001119 }
Tim Petersc3074532001-05-03 07:00:32 +00001120
Guido van Rossum2d951851994-08-29 12:52:16 +00001121 if (w == NULL)
1122 w = x;
1123 else {
Guido van Rossum53451b32001-01-17 15:47:24 +00001124 int cmp = PyObject_RichCompareBool(x, w, op);
1125 if (cmp > 0) {
1126 Py_DECREF(w);
1127 w = x;
1128 }
1129 else if (cmp < 0) {
Guido van Rossumc8b6df91997-05-23 00:06:51 +00001130 Py_DECREF(x);
Tim Petersc3074532001-05-03 07:00:32 +00001131 Py_DECREF(w);
1132 Py_DECREF(it);
Guido van Rossumc8b6df91997-05-23 00:06:51 +00001133 return NULL;
1134 }
Guido van Rossum2d951851994-08-29 12:52:16 +00001135 else
Guido van Rossum79f25d91997-04-29 20:08:16 +00001136 Py_DECREF(x);
Guido van Rossum2d951851994-08-29 12:52:16 +00001137 }
Guido van Rossum3f5da241990-12-20 15:06:42 +00001138 }
Guido van Rossum2d951851994-08-29 12:52:16 +00001139 if (w == NULL)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001140 PyErr_SetString(PyExc_ValueError,
Fred Drake661ea262000-10-24 19:57:45 +00001141 "min() or max() arg is an empty sequence");
Tim Petersc3074532001-05-03 07:00:32 +00001142 Py_DECREF(it);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001143 return w;
1144}
1145
Guido van Rossum79f25d91997-04-29 20:08:16 +00001146static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001147builtin_min(PyObject *self, PyObject *v)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001148{
Guido van Rossum53451b32001-01-17 15:47:24 +00001149 return min_max(v, Py_LT);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001150}
1151
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001152PyDoc_STRVAR(min_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001153"min(sequence) -> value\n\
1154min(a, b, c, ...) -> value\n\
1155\n\
1156With a single sequence argument, return its smallest item.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001157With two or more arguments, return the smallest argument.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001158
1159
Guido van Rossum79f25d91997-04-29 20:08:16 +00001160static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001161builtin_max(PyObject *self, PyObject *v)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001162{
Guido van Rossum53451b32001-01-17 15:47:24 +00001163 return min_max(v, Py_GT);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001164}
1165
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001166PyDoc_STRVAR(max_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001167"max(sequence) -> value\n\
1168max(a, b, c, ...) -> value\n\
1169\n\
1170With a single sequence argument, return its largest item.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001171With two or more arguments, return the largest argument.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001172
1173
Guido van Rossum79f25d91997-04-29 20:08:16 +00001174static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001175builtin_oct(PyObject *self, PyObject *v)
Guido van Rossum006bcd41991-10-24 14:54:44 +00001176{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001177 PyNumberMethods *nb;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001178
Guido van Rossum1899c2e1992-09-12 11:09:23 +00001179 if (v == NULL || (nb = v->ob_type->tp_as_number) == NULL ||
1180 nb->nb_oct == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001181 PyErr_SetString(PyExc_TypeError,
Guido van Rossum1899c2e1992-09-12 11:09:23 +00001182 "oct() argument can't be converted to oct");
1183 return NULL;
Guido van Rossum006bcd41991-10-24 14:54:44 +00001184 }
Guido van Rossum1899c2e1992-09-12 11:09:23 +00001185 return (*nb->nb_oct)(v);
Guido van Rossum006bcd41991-10-24 14:54:44 +00001186}
1187
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001188PyDoc_STRVAR(oct_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001189"oct(number) -> string\n\
1190\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001191Return the octal representation of an integer or long integer.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001192
1193
Guido van Rossum79f25d91997-04-29 20:08:16 +00001194static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001195builtin_ord(PyObject *self, PyObject* obj)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001196{
Guido van Rossum09095f32000-03-10 23:00:52 +00001197 long ord;
Jeremy Hylton394b54d2000-04-12 21:19:47 +00001198 int size;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001199
Jeremy Hylton394b54d2000-04-12 21:19:47 +00001200 if (PyString_Check(obj)) {
1201 size = PyString_GET_SIZE(obj);
Andrew M. Kuchling34c20cf2000-12-20 14:36:56 +00001202 if (size == 1) {
Jeremy Hylton394b54d2000-04-12 21:19:47 +00001203 ord = (long)((unsigned char)*PyString_AS_STRING(obj));
Andrew M. Kuchling34c20cf2000-12-20 14:36:56 +00001204 return PyInt_FromLong(ord);
1205 }
Martin v. Löwis339d0f72001-08-17 18:39:25 +00001206#ifdef Py_USING_UNICODE
Jeremy Hylton394b54d2000-04-12 21:19:47 +00001207 } else if (PyUnicode_Check(obj)) {
1208 size = PyUnicode_GET_SIZE(obj);
Andrew M. Kuchling34c20cf2000-12-20 14:36:56 +00001209 if (size == 1) {
Jeremy Hylton394b54d2000-04-12 21:19:47 +00001210 ord = (long)*PyUnicode_AS_UNICODE(obj);
Andrew M. Kuchling34c20cf2000-12-20 14:36:56 +00001211 return PyInt_FromLong(ord);
1212 }
Martin v. Löwis339d0f72001-08-17 18:39:25 +00001213#endif
Jeremy Hylton394b54d2000-04-12 21:19:47 +00001214 } else {
1215 PyErr_Format(PyExc_TypeError,
Andrew M. Kuchlingf07aad12000-12-23 14:11:28 +00001216 "ord() expected string of length 1, but " \
Jeremy Hylton394b54d2000-04-12 21:19:47 +00001217 "%.200s found", obj->ob_type->tp_name);
Guido van Rossum09095f32000-03-10 23:00:52 +00001218 return NULL;
1219 }
1220
Guido van Rossumad991772001-01-12 16:03:05 +00001221 PyErr_Format(PyExc_TypeError,
Andrew M. Kuchlingf07aad12000-12-23 14:11:28 +00001222 "ord() expected a character, "
1223 "but string of length %d found",
Jeremy Hylton394b54d2000-04-12 21:19:47 +00001224 size);
1225 return NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001226}
1227
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001228PyDoc_STRVAR(ord_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001229"ord(c) -> integer\n\
1230\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001231Return the integer ordinal of a one-character string.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001232
1233
Guido van Rossum79f25d91997-04-29 20:08:16 +00001234static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001235builtin_pow(PyObject *self, PyObject *args)
Guido van Rossum6a00cd81995-01-07 12:39:01 +00001236{
Guido van Rossum09df08a1998-05-22 00:51:39 +00001237 PyObject *v, *w, *z = Py_None;
Guido van Rossum6a00cd81995-01-07 12:39:01 +00001238
Raymond Hettingerea3fdf42002-12-29 16:33:45 +00001239 if (!PyArg_UnpackTuple(args, "pow", 2, 3, &v, &w, &z))
Guido van Rossum6a00cd81995-01-07 12:39:01 +00001240 return NULL;
Guido van Rossum09df08a1998-05-22 00:51:39 +00001241 return PyNumber_Power(v, w, z);
Guido van Rossumd4905451991-05-05 20:00:36 +00001242}
1243
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001244PyDoc_STRVAR(pow_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001245"pow(x, y[, z]) -> number\n\
1246\n\
1247With two arguments, equivalent to x**y. With three arguments,\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001248equivalent to (x**y) % z, but may be more efficient (e.g. for longs).");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001249
1250
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001251
1252/* Return number of items in range (lo, hi, step), when arguments are
1253 * PyInt or PyLong objects. step > 0 required. Return a value < 0 if
1254 * & only if the true value is too large to fit in a signed long.
1255 * Arguments MUST return 1 with either PyInt_Check() or
1256 * PyLong_Check(). Return -1 when there is an error.
1257 */
1258static long
1259get_len_of_range_longs(PyObject *lo, PyObject *hi, PyObject *step)
1260{
1261 /* -------------------------------------------------------------
1262 Algorithm is equal to that of get_len_of_range(), but it operates
1263 on PyObjects (which are assumed to be PyLong or PyInt objects).
1264 ---------------------------------------------------------------*/
1265 long n;
1266 PyObject *diff = NULL;
1267 PyObject *one = NULL;
1268 PyObject *tmp1 = NULL, *tmp2 = NULL, *tmp3 = NULL;
1269 /* holds sub-expression evaluations */
1270
1271 /* if (lo >= hi), return length of 0. */
1272 if (PyObject_Compare(lo, hi) >= 0)
1273 return 0;
1274
1275 if ((one = PyLong_FromLong(1L)) == NULL)
1276 goto Fail;
1277
1278 if ((tmp1 = PyNumber_Subtract(hi, lo)) == NULL)
1279 goto Fail;
1280
1281 if ((diff = PyNumber_Subtract(tmp1, one)) == NULL)
1282 goto Fail;
1283
1284 if ((tmp2 = PyNumber_FloorDivide(diff, step)) == NULL)
1285 goto Fail;
1286
1287 if ((tmp3 = PyNumber_Add(tmp2, one)) == NULL)
1288 goto Fail;
1289
1290 n = PyLong_AsLong(tmp3);
1291 if (PyErr_Occurred()) { /* Check for Overflow */
1292 PyErr_Clear();
1293 goto Fail;
1294 }
1295
1296 Py_DECREF(tmp3);
1297 Py_DECREF(tmp2);
1298 Py_DECREF(diff);
1299 Py_DECREF(tmp1);
1300 Py_DECREF(one);
1301 return n;
1302
1303 Fail:
1304 Py_XDECREF(tmp3);
1305 Py_XDECREF(tmp2);
1306 Py_XDECREF(diff);
1307 Py_XDECREF(tmp1);
1308 Py_XDECREF(one);
1309 return -1;
1310}
1311
1312/* An extension of builtin_range() that handles the case when PyLong
1313 * arguments are given. */
1314static PyObject *
1315handle_range_longs(PyObject *self, PyObject *args)
1316{
1317 PyObject *ilow;
Tim Peters874e1f72003-04-13 22:13:08 +00001318 PyObject *ihigh = NULL;
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001319 PyObject *istep = NULL;
Tim Peters874e1f72003-04-13 22:13:08 +00001320
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001321 PyObject *curnum = NULL;
1322 PyObject *v = NULL;
1323 long bign;
1324 int i, n;
1325 int cmp_result;
1326
Tim Peters874e1f72003-04-13 22:13:08 +00001327 PyObject *zero = PyLong_FromLong(0);
1328
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001329 if (zero == NULL)
1330 return NULL;
1331
Tim Peters874e1f72003-04-13 22:13:08 +00001332 if (!PyArg_UnpackTuple(args, "range", 1, 3, &ilow, &ihigh, &istep)) {
1333 Py_DECREF(zero);
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001334 return NULL;
1335 }
1336
Tim Peters874e1f72003-04-13 22:13:08 +00001337 /* Figure out which way we were called, supply defaults, and be
1338 * sure to incref everything so that the decrefs at the end
1339 * are correct.
1340 */
1341 assert(ilow != NULL);
1342 if (ihigh == NULL) {
1343 /* only 1 arg -- it's the upper limit */
1344 ihigh = ilow;
1345 ilow = NULL;
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001346 }
Tim Peters874e1f72003-04-13 22:13:08 +00001347 assert(ihigh != NULL);
1348 Py_INCREF(ihigh);
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001349
Tim Peters874e1f72003-04-13 22:13:08 +00001350 /* ihigh correct now; do ilow */
1351 if (ilow == NULL)
1352 ilow = zero;
1353 Py_INCREF(ilow);
1354
1355 /* ilow and ihigh correct now; do istep */
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001356 if (istep == NULL) {
1357 istep = PyLong_FromLong(1L);
1358 if (istep == NULL)
1359 goto Fail;
1360 }
1361 else {
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001362 Py_INCREF(istep);
1363 }
1364
Tim Peters874e1f72003-04-13 22:13:08 +00001365 if (!PyInt_Check(ilow) && !PyLong_Check(ilow)) {
Guido van Rossum28e83e32003-04-15 12:43:26 +00001366 PyErr_Format(PyExc_TypeError,
Alex Martellif471d472003-04-23 13:00:44 +00001367 "range() integer start argument expected, got %s.",
Guido van Rossum817d6c92003-04-14 18:25:04 +00001368 ilow->ob_type->tp_name);
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001369 goto Fail;
1370 }
1371
Tim Peters874e1f72003-04-13 22:13:08 +00001372 if (!PyInt_Check(ihigh) && !PyLong_Check(ihigh)) {
Guido van Rossum28e83e32003-04-15 12:43:26 +00001373 PyErr_Format(PyExc_TypeError,
Alex Martellif471d472003-04-23 13:00:44 +00001374 "range() integer end argument expected, got %s.",
Guido van Rossum817d6c92003-04-14 18:25:04 +00001375 ihigh->ob_type->tp_name);
Tim Peters874e1f72003-04-13 22:13:08 +00001376 goto Fail;
1377 }
1378
1379 if (!PyInt_Check(istep) && !PyLong_Check(istep)) {
Guido van Rossum28e83e32003-04-15 12:43:26 +00001380 PyErr_Format(PyExc_TypeError,
Alex Martellif471d472003-04-23 13:00:44 +00001381 "range() integer step argument expected, got %s.",
Guido van Rossum817d6c92003-04-14 18:25:04 +00001382 istep->ob_type->tp_name);
Tim Peters874e1f72003-04-13 22:13:08 +00001383 goto Fail;
1384 }
1385
1386 if (PyObject_Cmp(istep, zero, &cmp_result) == -1)
1387 goto Fail;
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001388 if (cmp_result == 0) {
1389 PyErr_SetString(PyExc_ValueError,
Alex Martellif471d472003-04-23 13:00:44 +00001390 "range() step argument must not be zero");
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001391 goto Fail;
1392 }
1393
1394 if (cmp_result > 0)
1395 bign = get_len_of_range_longs(ilow, ihigh, istep);
1396 else {
1397 PyObject *neg_istep = PyNumber_Negative(istep);
1398 if (neg_istep == NULL)
1399 goto Fail;
1400 bign = get_len_of_range_longs(ihigh, ilow, neg_istep);
1401 Py_DECREF(neg_istep);
1402 }
1403
1404 n = (int)bign;
1405 if (bign < 0 || (long)n != bign) {
1406 PyErr_SetString(PyExc_OverflowError,
1407 "range() result has too many items");
1408 goto Fail;
1409 }
1410
1411 v = PyList_New(n);
1412 if (v == NULL)
1413 goto Fail;
1414
1415 curnum = ilow;
1416 Py_INCREF(curnum);
1417
1418 for (i = 0; i < n; i++) {
1419 PyObject *w = PyNumber_Long(curnum);
1420 PyObject *tmp_num;
1421 if (w == NULL)
1422 goto Fail;
1423
1424 PyList_SET_ITEM(v, i, w);
1425
1426 tmp_num = PyNumber_Add(curnum, istep);
1427 if (tmp_num == NULL)
1428 goto Fail;
1429
1430 Py_DECREF(curnum);
1431 curnum = tmp_num;
1432 }
Tim Peters874e1f72003-04-13 22:13:08 +00001433 Py_DECREF(ilow);
1434 Py_DECREF(ihigh);
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001435 Py_DECREF(istep);
1436 Py_DECREF(zero);
Tim Peters874e1f72003-04-13 22:13:08 +00001437 Py_DECREF(curnum);
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001438 return v;
1439
1440 Fail:
Tim Peters874e1f72003-04-13 22:13:08 +00001441 Py_DECREF(ilow);
1442 Py_DECREF(ihigh);
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001443 Py_XDECREF(istep);
Tim Peters874e1f72003-04-13 22:13:08 +00001444 Py_DECREF(zero);
1445 Py_XDECREF(curnum);
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001446 Py_XDECREF(v);
1447 return NULL;
1448}
1449
Guido van Rossum124eff01999-02-23 16:11:01 +00001450/* Return number of items in range/xrange (lo, hi, step). step > 0
1451 * required. Return a value < 0 if & only if the true value is too
1452 * large to fit in a signed long.
1453 */
1454static long
Thomas Wouterse28c2962000-07-23 22:21:32 +00001455get_len_of_range(long lo, long hi, long step)
Guido van Rossum124eff01999-02-23 16:11:01 +00001456{
1457 /* -------------------------------------------------------------
1458 If lo >= hi, the range is empty.
1459 Else if n values are in the range, the last one is
1460 lo + (n-1)*step, which must be <= hi-1. Rearranging,
1461 n <= (hi - lo - 1)/step + 1, so taking the floor of the RHS gives
1462 the proper value. Since lo < hi in this case, hi-lo-1 >= 0, so
1463 the RHS is non-negative and so truncation is the same as the
1464 floor. Letting M be the largest positive long, the worst case
1465 for the RHS numerator is hi=M, lo=-M-1, and then
1466 hi-lo-1 = M-(-M-1)-1 = 2*M. Therefore unsigned long has enough
1467 precision to compute the RHS exactly.
1468 ---------------------------------------------------------------*/
1469 long n = 0;
1470 if (lo < hi) {
1471 unsigned long uhi = (unsigned long)hi;
1472 unsigned long ulo = (unsigned long)lo;
1473 unsigned long diff = uhi - ulo - 1;
1474 n = (long)(diff / (unsigned long)step + 1);
1475 }
1476 return n;
1477}
1478
Guido van Rossum79f25d91997-04-29 20:08:16 +00001479static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001480builtin_range(PyObject *self, PyObject *args)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001481{
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001482 long ilow = 0, ihigh = 0, istep = 1;
Guido van Rossum124eff01999-02-23 16:11:01 +00001483 long bign;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001484 int i, n;
Guido van Rossum124eff01999-02-23 16:11:01 +00001485
Guido van Rossum79f25d91997-04-29 20:08:16 +00001486 PyObject *v;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001487
Guido van Rossum79f25d91997-04-29 20:08:16 +00001488 if (PyTuple_Size(args) <= 1) {
1489 if (!PyArg_ParseTuple(args,
Guido van Rossum0865dd91995-01-17 16:30:22 +00001490 "l;range() requires 1-3 int arguments",
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001491 &ihigh)) {
1492 PyErr_Clear();
1493 return handle_range_longs(self, args);
1494 }
Guido van Rossum3f5da241990-12-20 15:06:42 +00001495 }
1496 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001497 if (!PyArg_ParseTuple(args,
Guido van Rossum0865dd91995-01-17 16:30:22 +00001498 "ll|l;range() requires 1-3 int arguments",
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001499 &ilow, &ihigh, &istep)) {
1500 PyErr_Clear();
1501 return handle_range_longs(self, args);
1502 }
Guido van Rossum3f5da241990-12-20 15:06:42 +00001503 }
1504 if (istep == 0) {
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001505 PyErr_SetString(PyExc_ValueError,
Alex Martellif471d472003-04-23 13:00:44 +00001506 "range() step argument must not be zero");
Guido van Rossum3f5da241990-12-20 15:06:42 +00001507 return NULL;
1508 }
Guido van Rossum124eff01999-02-23 16:11:01 +00001509 if (istep > 0)
1510 bign = get_len_of_range(ilow, ihigh, istep);
1511 else
1512 bign = get_len_of_range(ihigh, ilow, -istep);
1513 n = (int)bign;
1514 if (bign < 0 || (long)n != bign) {
Guido van Rossume23cde21999-01-12 05:07:47 +00001515 PyErr_SetString(PyExc_OverflowError,
Fred Drake661ea262000-10-24 19:57:45 +00001516 "range() result has too many items");
Guido van Rossume23cde21999-01-12 05:07:47 +00001517 return NULL;
1518 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00001519 v = PyList_New(n);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001520 if (v == NULL)
1521 return NULL;
1522 for (i = 0; i < n; i++) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001523 PyObject *w = PyInt_FromLong(ilow);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001524 if (w == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001525 Py_DECREF(v);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001526 return NULL;
1527 }
Guido van Rossuma937d141998-04-24 18:22:02 +00001528 PyList_SET_ITEM(v, i, w);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001529 ilow += istep;
1530 }
1531 return v;
1532}
1533
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001534PyDoc_STRVAR(range_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001535"range([start,] stop[, step]) -> list of integers\n\
1536\n\
1537Return a list containing an arithmetic progression of integers.\n\
1538range(i, j) returns [i, i+1, i+2, ..., j-1]; start (!) defaults to 0.\n\
1539When step is given, it specifies the increment (or decrement).\n\
1540For example, range(4) returns [0, 1, 2, 3]. The end point is omitted!\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001541These are exactly the valid indices for a list of 4 elements.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001542
1543
Guido van Rossum79f25d91997-04-29 20:08:16 +00001544static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001545builtin_raw_input(PyObject *self, PyObject *args)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001546{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001547 PyObject *v = NULL;
Martin v. Löwis31d2df52002-08-14 15:46:02 +00001548 PyObject *fin = PySys_GetObject("stdin");
1549 PyObject *fout = PySys_GetObject("stdout");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001550
Raymond Hettingerea3fdf42002-12-29 16:33:45 +00001551 if (!PyArg_UnpackTuple(args, "[raw_]input", 0, 1, &v))
Guido van Rossum3165fe61992-09-25 21:59:05 +00001552 return NULL;
Martin v. Löwis31d2df52002-08-14 15:46:02 +00001553
1554 if (fin == NULL) {
Alex Martellia9b9c9f2003-04-23 13:34:35 +00001555 PyErr_SetString(PyExc_RuntimeError, "[raw_]input: lost sys.stdin");
Martin v. Löwis31d2df52002-08-14 15:46:02 +00001556 return NULL;
1557 }
1558 if (fout == NULL) {
Alex Martellia9b9c9f2003-04-23 13:34:35 +00001559 PyErr_SetString(PyExc_RuntimeError, "[raw_]input: lost sys.stdout");
Martin v. Löwis31d2df52002-08-14 15:46:02 +00001560 return NULL;
1561 }
1562 if (PyFile_SoftSpace(fout, 0)) {
1563 if (PyFile_WriteString(" ", fout) != 0)
1564 return NULL;
1565 }
Martin v. Löwis566f6af2002-10-26 14:39:10 +00001566 if (PyFile_Check (fin) && PyFile_Check (fout)
1567 && isatty(fileno(PyFile_AsFile(fin)))
1568 && isatty(fileno(PyFile_AsFile(fout)))) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001569 PyObject *po;
Guido van Rossum872537c1995-07-07 22:43:42 +00001570 char *prompt;
1571 char *s;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001572 PyObject *result;
Guido van Rossum872537c1995-07-07 22:43:42 +00001573 if (v != NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001574 po = PyObject_Str(v);
Guido van Rossum872537c1995-07-07 22:43:42 +00001575 if (po == NULL)
1576 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001577 prompt = PyString_AsString(po);
Guido van Rossumd9b52081998-06-26 18:25:38 +00001578 if (prompt == NULL)
1579 return NULL;
Guido van Rossum872537c1995-07-07 22:43:42 +00001580 }
1581 else {
1582 po = NULL;
1583 prompt = "";
1584 }
Martin v. Löwis566f6af2002-10-26 14:39:10 +00001585 s = PyOS_Readline(PyFile_AsFile (fin), PyFile_AsFile (fout),
1586 prompt);
Guido van Rossum79f25d91997-04-29 20:08:16 +00001587 Py_XDECREF(po);
Guido van Rossum872537c1995-07-07 22:43:42 +00001588 if (s == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001589 PyErr_SetNone(PyExc_KeyboardInterrupt);
Guido van Rossum872537c1995-07-07 22:43:42 +00001590 return NULL;
1591 }
1592 if (*s == '\0') {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001593 PyErr_SetNone(PyExc_EOFError);
Guido van Rossum872537c1995-07-07 22:43:42 +00001594 result = NULL;
1595 }
1596 else { /* strip trailing '\n' */
Guido van Rossum106f2da2000-06-28 21:12:25 +00001597 size_t len = strlen(s);
1598 if (len > INT_MAX) {
Martin v. Löwis31d2df52002-08-14 15:46:02 +00001599 PyErr_SetString(PyExc_OverflowError,
Alex Martellia9b9c9f2003-04-23 13:34:35 +00001600 "[raw_]input: input too long");
Guido van Rossum106f2da2000-06-28 21:12:25 +00001601 result = NULL;
1602 }
1603 else {
Martin v. Löwis31d2df52002-08-14 15:46:02 +00001604 result = PyString_FromStringAndSize(s,
1605 (int)(len-1));
Guido van Rossum106f2da2000-06-28 21:12:25 +00001606 }
Guido van Rossum872537c1995-07-07 22:43:42 +00001607 }
Guido van Rossumb18618d2000-05-03 23:44:39 +00001608 PyMem_FREE(s);
Guido van Rossum872537c1995-07-07 22:43:42 +00001609 return result;
1610 }
Guido van Rossum90933611991-06-07 16:10:43 +00001611 if (v != NULL) {
Martin v. Löwis31d2df52002-08-14 15:46:02 +00001612 if (PyFile_WriteObject(v, fout, Py_PRINT_RAW) != 0)
Guido van Rossum90933611991-06-07 16:10:43 +00001613 return NULL;
1614 }
Martin v. Löwis31d2df52002-08-14 15:46:02 +00001615 return PyFile_GetLine(fin, -1);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001616}
1617
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001618PyDoc_STRVAR(raw_input_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001619"raw_input([prompt]) -> string\n\
1620\n\
1621Read a string from standard input. The trailing newline is stripped.\n\
1622If the user hits EOF (Unix: Ctl-D, Windows: Ctl-Z+Return), raise EOFError.\n\
1623On Unix, GNU readline is used if enabled. The prompt string, if given,\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001624is printed without a trailing newline before reading.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001625
1626
Guido van Rossum79f25d91997-04-29 20:08:16 +00001627static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001628builtin_reduce(PyObject *self, PyObject *args)
Guido van Rossum12d12c51993-10-26 17:58:25 +00001629{
Tim Peters15d81ef2001-05-04 04:39:21 +00001630 PyObject *seq, *func, *result = NULL, *it;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001631
Raymond Hettingerea3fdf42002-12-29 16:33:45 +00001632 if (!PyArg_UnpackTuple(args, "reduce", 2, 3, &func, &seq, &result))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001633 return NULL;
1634 if (result != NULL)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001635 Py_INCREF(result);
Guido van Rossum12d12c51993-10-26 17:58:25 +00001636
Tim Peters15d81ef2001-05-04 04:39:21 +00001637 it = PyObject_GetIter(seq);
1638 if (it == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001639 PyErr_SetString(PyExc_TypeError,
Tim Peters15d81ef2001-05-04 04:39:21 +00001640 "reduce() arg 2 must support iteration");
1641 Py_XDECREF(result);
Guido van Rossum12d12c51993-10-26 17:58:25 +00001642 return NULL;
1643 }
1644
Guido van Rossum79f25d91997-04-29 20:08:16 +00001645 if ((args = PyTuple_New(2)) == NULL)
Guido van Rossum2d951851994-08-29 12:52:16 +00001646 goto Fail;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001647
Tim Peters15d81ef2001-05-04 04:39:21 +00001648 for (;;) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001649 PyObject *op2;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001650
1651 if (args->ob_refcnt > 1) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001652 Py_DECREF(args);
1653 if ((args = PyTuple_New(2)) == NULL)
Guido van Rossum2d951851994-08-29 12:52:16 +00001654 goto Fail;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001655 }
1656
Tim Peters15d81ef2001-05-04 04:39:21 +00001657 op2 = PyIter_Next(it);
1658 if (op2 == NULL) {
Tim Petersf4848da2001-05-05 00:14:56 +00001659 if (PyErr_Occurred())
1660 goto Fail;
1661 break;
Guido van Rossum2d951851994-08-29 12:52:16 +00001662 }
Guido van Rossum12d12c51993-10-26 17:58:25 +00001663
Guido van Rossum2d951851994-08-29 12:52:16 +00001664 if (result == NULL)
1665 result = op2;
1666 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001667 PyTuple_SetItem(args, 0, result);
1668 PyTuple_SetItem(args, 1, op2);
1669 if ((result = PyEval_CallObject(func, args)) == NULL)
Guido van Rossum2d951851994-08-29 12:52:16 +00001670 goto Fail;
1671 }
Guido van Rossum12d12c51993-10-26 17:58:25 +00001672 }
1673
Guido van Rossum79f25d91997-04-29 20:08:16 +00001674 Py_DECREF(args);
Guido van Rossum12d12c51993-10-26 17:58:25 +00001675
Guido van Rossum2d951851994-08-29 12:52:16 +00001676 if (result == NULL)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001677 PyErr_SetString(PyExc_TypeError,
Fred Drake661ea262000-10-24 19:57:45 +00001678 "reduce() of empty sequence with no initial value");
Guido van Rossum2d951851994-08-29 12:52:16 +00001679
Tim Peters15d81ef2001-05-04 04:39:21 +00001680 Py_DECREF(it);
Guido van Rossum12d12c51993-10-26 17:58:25 +00001681 return result;
1682
Guido van Rossum2d951851994-08-29 12:52:16 +00001683Fail:
Guido van Rossum79f25d91997-04-29 20:08:16 +00001684 Py_XDECREF(args);
1685 Py_XDECREF(result);
Tim Peters15d81ef2001-05-04 04:39:21 +00001686 Py_DECREF(it);
Guido van Rossum12d12c51993-10-26 17:58:25 +00001687 return NULL;
1688}
1689
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001690PyDoc_STRVAR(reduce_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001691"reduce(function, sequence[, initial]) -> value\n\
1692\n\
1693Apply a function of two arguments cumulatively to the items of a sequence,\n\
1694from left to right, so as to reduce the sequence to a single value.\n\
1695For example, reduce(lambda x, y: x+y, [1, 2, 3, 4, 5]) calculates\n\
1696((((1+2)+3)+4)+5). If initial is present, it is placed before the items\n\
1697of the sequence in the calculation, and serves as a default when the\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001698sequence is empty.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001699
1700
Guido van Rossum79f25d91997-04-29 20:08:16 +00001701static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001702builtin_reload(PyObject *self, PyObject *v)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001703{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001704 return PyImport_ReloadModule(v);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001705}
1706
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001707PyDoc_STRVAR(reload_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001708"reload(module) -> module\n\
1709\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001710Reload the module. The module must have been successfully imported before.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001711
1712
Guido van Rossum79f25d91997-04-29 20:08:16 +00001713static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001714builtin_repr(PyObject *self, PyObject *v)
Guido van Rossumc89705d1992-11-26 08:54:07 +00001715{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001716 return PyObject_Repr(v);
Guido van Rossumc89705d1992-11-26 08:54:07 +00001717}
1718
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001719PyDoc_STRVAR(repr_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001720"repr(object) -> string\n\
1721\n\
1722Return the canonical string representation of the object.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001723For most object types, eval(repr(object)) == object.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001724
1725
Guido van Rossum79f25d91997-04-29 20:08:16 +00001726static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001727builtin_round(PyObject *self, PyObject *args)
Guido van Rossum9e51f9b1993-02-12 16:29:05 +00001728{
Guido van Rossum9e51f9b1993-02-12 16:29:05 +00001729 double x;
1730 double f;
1731 int ndigits = 0;
Guido van Rossum9e51f9b1993-02-12 16:29:05 +00001732 int i;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001733
Guido van Rossum79f25d91997-04-29 20:08:16 +00001734 if (!PyArg_ParseTuple(args, "d|i:round", &x, &ndigits))
Guido van Rossum9e51f9b1993-02-12 16:29:05 +00001735 return NULL;
Guido van Rossum9e51f9b1993-02-12 16:29:05 +00001736 f = 1.0;
Guido van Rossum1e162d31998-05-09 14:42:25 +00001737 i = abs(ndigits);
1738 while (--i >= 0)
Guido van Rossum9e51f9b1993-02-12 16:29:05 +00001739 f = f*10.0;
Guido van Rossum1e162d31998-05-09 14:42:25 +00001740 if (ndigits < 0)
1741 x /= f;
Guido van Rossum9e51f9b1993-02-12 16:29:05 +00001742 else
Guido van Rossum1e162d31998-05-09 14:42:25 +00001743 x *= f;
1744 if (x >= 0.0)
1745 x = floor(x + 0.5);
1746 else
1747 x = ceil(x - 0.5);
1748 if (ndigits < 0)
1749 x *= f;
1750 else
1751 x /= f;
1752 return PyFloat_FromDouble(x);
Guido van Rossum9e51f9b1993-02-12 16:29:05 +00001753}
1754
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001755PyDoc_STRVAR(round_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001756"round(number[, ndigits]) -> floating point number\n\
1757\n\
1758Round a number to a given precision in decimal digits (default 0 digits).\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001759This always returns a floating point number. Precision may be negative.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001760
1761
Guido van Rossum79f25d91997-04-29 20:08:16 +00001762static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001763builtin_vars(PyObject *self, PyObject *args)
Guido van Rossum2d951851994-08-29 12:52:16 +00001764{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001765 PyObject *v = NULL;
1766 PyObject *d;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001767
Raymond Hettingerea3fdf42002-12-29 16:33:45 +00001768 if (!PyArg_UnpackTuple(args, "vars", 0, 1, &v))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001769 return NULL;
Guido van Rossum2d951851994-08-29 12:52:16 +00001770 if (v == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001771 d = PyEval_GetLocals();
Guido van Rossum53bb7ff1995-07-26 16:26:31 +00001772 if (d == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001773 if (!PyErr_Occurred())
1774 PyErr_SetString(PyExc_SystemError,
Alex Martellia9b9c9f2003-04-23 13:34:35 +00001775 "vars(): no locals!?");
Guido van Rossum53bb7ff1995-07-26 16:26:31 +00001776 }
1777 else
Guido van Rossum79f25d91997-04-29 20:08:16 +00001778 Py_INCREF(d);
Guido van Rossum2d951851994-08-29 12:52:16 +00001779 }
1780 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001781 d = PyObject_GetAttrString(v, "__dict__");
Guido van Rossum2d951851994-08-29 12:52:16 +00001782 if (d == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001783 PyErr_SetString(PyExc_TypeError,
Guido van Rossum2d951851994-08-29 12:52:16 +00001784 "vars() argument must have __dict__ attribute");
1785 return NULL;
1786 }
1787 }
1788 return d;
1789}
1790
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001791PyDoc_STRVAR(vars_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001792"vars([object]) -> dictionary\n\
1793\n\
1794Without arguments, equivalent to locals().\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001795With an argument, equivalent to object.__dict__.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001796
Alex Martellia70b1912003-04-22 08:12:33 +00001797
1798static PyObject*
1799builtin_sum(PyObject *self, PyObject *args)
1800{
1801 PyObject *seq;
1802 PyObject *result = NULL;
1803 PyObject *temp, *item, *iter;
1804
Raymond Hettinger5cf63942003-10-25 06:41:37 +00001805 if (!PyArg_UnpackTuple(args, "sum", 1, 2, &seq, &result))
Alex Martellia70b1912003-04-22 08:12:33 +00001806 return NULL;
1807
1808 iter = PyObject_GetIter(seq);
1809 if (iter == NULL)
1810 return NULL;
1811
1812 if (result == NULL) {
1813 result = PyInt_FromLong(0);
1814 if (result == NULL) {
1815 Py_DECREF(iter);
1816 return NULL;
1817 }
1818 } else {
1819 /* reject string values for 'start' parameter */
1820 if (PyObject_TypeCheck(result, &PyBaseString_Type)) {
1821 PyErr_SetString(PyExc_TypeError,
Alex Martellia9b9c9f2003-04-23 13:34:35 +00001822 "sum() can't sum strings [use ''.join(seq) instead]");
Alex Martellia70b1912003-04-22 08:12:33 +00001823 Py_DECREF(iter);
1824 return NULL;
1825 }
Alex Martelli41c9f882003-04-22 09:24:48 +00001826 Py_INCREF(result);
Alex Martellia70b1912003-04-22 08:12:33 +00001827 }
1828
1829 for(;;) {
1830 item = PyIter_Next(iter);
1831 if (item == NULL) {
1832 /* error, or end-of-sequence */
1833 if (PyErr_Occurred()) {
1834 Py_DECREF(result);
1835 result = NULL;
1836 }
1837 break;
1838 }
Alex Martellia253e182003-10-25 23:24:14 +00001839 temp = PyNumber_Add(result, item);
Alex Martellia70b1912003-04-22 08:12:33 +00001840 Py_DECREF(result);
1841 Py_DECREF(item);
1842 result = temp;
1843 if (result == NULL)
1844 break;
1845 }
1846 Py_DECREF(iter);
1847 return result;
1848}
1849
1850PyDoc_STRVAR(sum_doc,
1851"sum(sequence, start=0) -> value\n\
1852\n\
1853Returns the sum of a sequence of numbers (NOT strings) plus the value\n\
1854of parameter 'start'. When the sequence is empty, returns start.");
1855
1856
Barry Warsawcde8b1b1997-08-22 21:14:38 +00001857static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001858builtin_isinstance(PyObject *self, PyObject *args)
Barry Warsawcde8b1b1997-08-22 21:14:38 +00001859{
1860 PyObject *inst;
1861 PyObject *cls;
Guido van Rossum823649d2001-03-21 18:40:58 +00001862 int retval;
Barry Warsawcde8b1b1997-08-22 21:14:38 +00001863
Raymond Hettingerea3fdf42002-12-29 16:33:45 +00001864 if (!PyArg_UnpackTuple(args, "isinstance", 2, 2, &inst, &cls))
Barry Warsawcde8b1b1997-08-22 21:14:38 +00001865 return NULL;
Guido van Rossumf5dd9141997-12-02 19:11:45 +00001866
Guido van Rossum823649d2001-03-21 18:40:58 +00001867 retval = PyObject_IsInstance(inst, cls);
1868 if (retval < 0)
Guido van Rossumad991772001-01-12 16:03:05 +00001869 return NULL;
Guido van Rossum77f6a652002-04-03 22:41:51 +00001870 return PyBool_FromLong(retval);
Barry Warsawcde8b1b1997-08-22 21:14:38 +00001871}
1872
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001873PyDoc_STRVAR(isinstance_doc,
Guido van Rossum77f6a652002-04-03 22:41:51 +00001874"isinstance(object, class-or-type-or-tuple) -> bool\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001875\n\
1876Return whether an object is an instance of a class or of a subclass thereof.\n\
Guido van Rossum03290ec2001-10-07 20:54:12 +00001877With a type as second argument, return whether that is the object's type.\n\
1878The form using a tuple, isinstance(x, (A, B, ...)), is a shortcut for\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001879isinstance(x, A) or isinstance(x, B) or ... (etc.).");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001880
Barry Warsawcde8b1b1997-08-22 21:14:38 +00001881
1882static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001883builtin_issubclass(PyObject *self, PyObject *args)
Barry Warsawcde8b1b1997-08-22 21:14:38 +00001884{
1885 PyObject *derived;
1886 PyObject *cls;
1887 int retval;
1888
Raymond Hettingerea3fdf42002-12-29 16:33:45 +00001889 if (!PyArg_UnpackTuple(args, "issubclass", 2, 2, &derived, &cls))
Barry Warsawcde8b1b1997-08-22 21:14:38 +00001890 return NULL;
Guido van Rossum668213d1999-06-16 17:28:37 +00001891
Guido van Rossum823649d2001-03-21 18:40:58 +00001892 retval = PyObject_IsSubclass(derived, cls);
1893 if (retval < 0)
1894 return NULL;
Guido van Rossum77f6a652002-04-03 22:41:51 +00001895 return PyBool_FromLong(retval);
Barry Warsawcde8b1b1997-08-22 21:14:38 +00001896}
1897
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001898PyDoc_STRVAR(issubclass_doc,
Guido van Rossum77f6a652002-04-03 22:41:51 +00001899"issubclass(C, B) -> bool\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001900\n\
Walter Dörwaldd9a6ad32002-12-12 16:41:44 +00001901Return whether class C is a subclass (i.e., a derived class) of class B.\n\
1902When using a tuple as the second argument issubclass(X, (A, B, ...)),\n\
1903is a shortcut for issubclass(X, A) or issubclass(X, B) or ... (etc.).");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001904
Barry Warsawcde8b1b1997-08-22 21:14:38 +00001905
Barry Warsawbd599b52000-08-03 15:45:29 +00001906static PyObject*
1907builtin_zip(PyObject *self, PyObject *args)
1908{
1909 PyObject *ret;
Tim Peters67d687a2002-04-29 21:27:32 +00001910 const int itemsize = PySequence_Length(args);
Tim Peters8572b4f2001-05-06 01:05:02 +00001911 int i;
1912 PyObject *itlist; /* tuple of iterators */
Tim Peters67d687a2002-04-29 21:27:32 +00001913 int len; /* guess at result length */
Barry Warsawbd599b52000-08-03 15:45:29 +00001914
Raymond Hettingereaef6152003-08-02 07:42:57 +00001915 if (itemsize == 0)
1916 return PyList_New(0);
1917
Barry Warsawbd599b52000-08-03 15:45:29 +00001918 /* args must be a tuple */
1919 assert(PyTuple_Check(args));
1920
Tim Peters39a86c22002-05-12 07:19:38 +00001921 /* Guess at result length: the shortest of the input lengths.
1922 If some argument refuses to say, we refuse to guess too, lest
1923 an argument like xrange(sys.maxint) lead us astray.*/
Tim Peters67d687a2002-04-29 21:27:32 +00001924 len = -1; /* unknown */
1925 for (i = 0; i < itemsize; ++i) {
1926 PyObject *item = PyTuple_GET_ITEM(args, i);
1927 int thislen = PySequence_Length(item);
Tim Peters39a86c22002-05-12 07:19:38 +00001928 if (thislen < 0) {
Tim Peters67d687a2002-04-29 21:27:32 +00001929 PyErr_Clear();
Tim Peters39a86c22002-05-12 07:19:38 +00001930 len = -1;
1931 break;
1932 }
Tim Peters67d687a2002-04-29 21:27:32 +00001933 else if (len < 0 || thislen < len)
1934 len = thislen;
1935 }
1936
Tim Peters8572b4f2001-05-06 01:05:02 +00001937 /* allocate result list */
Tim Peters67d687a2002-04-29 21:27:32 +00001938 if (len < 0)
1939 len = 10; /* arbitrary */
1940 if ((ret = PyList_New(len)) == NULL)
Barry Warsawbd599b52000-08-03 15:45:29 +00001941 return NULL;
1942
Tim Peters8572b4f2001-05-06 01:05:02 +00001943 /* obtain iterators */
1944 itlist = PyTuple_New(itemsize);
1945 if (itlist == NULL)
1946 goto Fail_ret;
1947 for (i = 0; i < itemsize; ++i) {
1948 PyObject *item = PyTuple_GET_ITEM(args, i);
1949 PyObject *it = PyObject_GetIter(item);
1950 if (it == NULL) {
1951 if (PyErr_ExceptionMatches(PyExc_TypeError))
1952 PyErr_Format(PyExc_TypeError,
1953 "zip argument #%d must support iteration",
1954 i+1);
1955 goto Fail_ret_itlist;
Barry Warsawbd599b52000-08-03 15:45:29 +00001956 }
Tim Peters8572b4f2001-05-06 01:05:02 +00001957 PyTuple_SET_ITEM(itlist, i, it);
1958 }
Barry Warsawbd599b52000-08-03 15:45:29 +00001959
Tim Peters8572b4f2001-05-06 01:05:02 +00001960 /* build result into ret list */
Tim Peters67d687a2002-04-29 21:27:32 +00001961 for (i = 0; ; ++i) {
1962 int j;
Tim Peters8572b4f2001-05-06 01:05:02 +00001963 PyObject *next = PyTuple_New(itemsize);
1964 if (!next)
1965 goto Fail_ret_itlist;
1966
Tim Peters67d687a2002-04-29 21:27:32 +00001967 for (j = 0; j < itemsize; j++) {
1968 PyObject *it = PyTuple_GET_ITEM(itlist, j);
Tim Peters8572b4f2001-05-06 01:05:02 +00001969 PyObject *item = PyIter_Next(it);
Barry Warsawbd599b52000-08-03 15:45:29 +00001970 if (!item) {
Tim Peters8572b4f2001-05-06 01:05:02 +00001971 if (PyErr_Occurred()) {
1972 Py_DECREF(ret);
1973 ret = NULL;
Barry Warsawbd599b52000-08-03 15:45:29 +00001974 }
1975 Py_DECREF(next);
Tim Peters8572b4f2001-05-06 01:05:02 +00001976 Py_DECREF(itlist);
Tim Peters67d687a2002-04-29 21:27:32 +00001977 goto Done;
Barry Warsawbd599b52000-08-03 15:45:29 +00001978 }
Tim Peters67d687a2002-04-29 21:27:32 +00001979 PyTuple_SET_ITEM(next, j, item);
Barry Warsawbd599b52000-08-03 15:45:29 +00001980 }
Tim Peters8572b4f2001-05-06 01:05:02 +00001981
Tim Peters67d687a2002-04-29 21:27:32 +00001982 if (i < len)
1983 PyList_SET_ITEM(ret, i, next);
1984 else {
1985 int status = PyList_Append(ret, next);
1986 Py_DECREF(next);
1987 ++len;
1988 if (status < 0)
1989 goto Fail_ret_itlist;
1990 }
Barry Warsawbd599b52000-08-03 15:45:29 +00001991 }
Tim Peters8572b4f2001-05-06 01:05:02 +00001992
Tim Peters67d687a2002-04-29 21:27:32 +00001993Done:
1994 if (ret != NULL && i < len) {
1995 /* The list is too big. */
1996 if (PyList_SetSlice(ret, i, len, NULL) < 0)
1997 return NULL;
1998 }
1999 return ret;
2000
Tim Peters8572b4f2001-05-06 01:05:02 +00002001Fail_ret_itlist:
2002 Py_DECREF(itlist);
2003Fail_ret:
2004 Py_DECREF(ret);
2005 return NULL;
Barry Warsawbd599b52000-08-03 15:45:29 +00002006}
2007
2008
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002009PyDoc_STRVAR(zip_doc,
Barry Warsawbd599b52000-08-03 15:45:29 +00002010"zip(seq1 [, seq2 [...]]) -> [(seq1[0], seq2[0] ...), (...)]\n\
2011\n\
2012Return a list of tuples, where each tuple contains the i-th element\n\
2013from each of the argument sequences. The returned list is truncated\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002014in length to the length of the shortest argument sequence.");
Barry Warsawbd599b52000-08-03 15:45:29 +00002015
2016
Guido van Rossum79f25d91997-04-29 20:08:16 +00002017static PyMethodDef builtin_methods[] = {
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002018 {"__import__", builtin___import__, METH_VARARGS, import_doc},
2019 {"abs", builtin_abs, METH_O, abs_doc},
2020 {"apply", builtin_apply, METH_VARARGS, apply_doc},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002021 {"callable", builtin_callable, METH_O, callable_doc},
2022 {"chr", builtin_chr, METH_VARARGS, chr_doc},
2023 {"cmp", builtin_cmp, METH_VARARGS, cmp_doc},
2024 {"coerce", builtin_coerce, METH_VARARGS, coerce_doc},
2025 {"compile", builtin_compile, METH_VARARGS, compile_doc},
2026 {"delattr", builtin_delattr, METH_VARARGS, delattr_doc},
2027 {"dir", builtin_dir, METH_VARARGS, dir_doc},
2028 {"divmod", builtin_divmod, METH_VARARGS, divmod_doc},
2029 {"eval", builtin_eval, METH_VARARGS, eval_doc},
2030 {"execfile", builtin_execfile, METH_VARARGS, execfile_doc},
2031 {"filter", builtin_filter, METH_VARARGS, filter_doc},
2032 {"getattr", builtin_getattr, METH_VARARGS, getattr_doc},
2033 {"globals", (PyCFunction)builtin_globals, METH_NOARGS, globals_doc},
2034 {"hasattr", builtin_hasattr, METH_VARARGS, hasattr_doc},
2035 {"hash", builtin_hash, METH_O, hash_doc},
2036 {"hex", builtin_hex, METH_O, hex_doc},
2037 {"id", builtin_id, METH_O, id_doc},
2038 {"input", builtin_input, METH_VARARGS, input_doc},
2039 {"intern", builtin_intern, METH_VARARGS, intern_doc},
2040 {"isinstance", builtin_isinstance, METH_VARARGS, isinstance_doc},
2041 {"issubclass", builtin_issubclass, METH_VARARGS, issubclass_doc},
2042 {"iter", builtin_iter, METH_VARARGS, iter_doc},
2043 {"len", builtin_len, METH_O, len_doc},
2044 {"locals", (PyCFunction)builtin_locals, METH_NOARGS, locals_doc},
2045 {"map", builtin_map, METH_VARARGS, map_doc},
2046 {"max", builtin_max, METH_VARARGS, max_doc},
2047 {"min", builtin_min, METH_VARARGS, min_doc},
2048 {"oct", builtin_oct, METH_O, oct_doc},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002049 {"ord", builtin_ord, METH_O, ord_doc},
2050 {"pow", builtin_pow, METH_VARARGS, pow_doc},
2051 {"range", builtin_range, METH_VARARGS, range_doc},
2052 {"raw_input", builtin_raw_input, METH_VARARGS, raw_input_doc},
2053 {"reduce", builtin_reduce, METH_VARARGS, reduce_doc},
2054 {"reload", builtin_reload, METH_O, reload_doc},
2055 {"repr", builtin_repr, METH_O, repr_doc},
2056 {"round", builtin_round, METH_VARARGS, round_doc},
2057 {"setattr", builtin_setattr, METH_VARARGS, setattr_doc},
Alex Martellia70b1912003-04-22 08:12:33 +00002058 {"sum", builtin_sum, METH_VARARGS, sum_doc},
Martin v. Löwis339d0f72001-08-17 18:39:25 +00002059#ifdef Py_USING_UNICODE
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002060 {"unichr", builtin_unichr, METH_VARARGS, unichr_doc},
Martin v. Löwis339d0f72001-08-17 18:39:25 +00002061#endif
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002062 {"vars", builtin_vars, METH_VARARGS, vars_doc},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002063 {"zip", builtin_zip, METH_VARARGS, zip_doc},
Guido van Rossumc02e15c1991-12-16 13:03:00 +00002064 {NULL, NULL},
Guido van Rossum3f5da241990-12-20 15:06:42 +00002065};
2066
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002067PyDoc_STRVAR(builtin_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002068"Built-in functions, exceptions, and other objects.\n\
2069\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002070Noteworthy: None is the `nil' object; Ellipsis represents `...' in slices.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002071
Guido van Rossum25ce5661997-08-02 03:10:38 +00002072PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002073_PyBuiltin_Init(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +00002074{
Fred Drake5550de32000-06-20 04:54:19 +00002075 PyObject *mod, *dict, *debug;
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002076 mod = Py_InitModule4("__builtin__", builtin_methods,
2077 builtin_doc, (PyObject *)NULL,
2078 PYTHON_API_VERSION);
Guido van Rossum25ce5661997-08-02 03:10:38 +00002079 if (mod == NULL)
2080 return NULL;
2081 dict = PyModule_GetDict(mod);
Tim Peters4b7625e2001-09-13 21:37:17 +00002082
Tim Peters7571a0f2003-03-23 17:52:28 +00002083#ifdef Py_TRACE_REFS
2084 /* __builtin__ exposes a number of statically allocated objects
2085 * that, before this code was added in 2.3, never showed up in
2086 * the list of "all objects" maintained by Py_TRACE_REFS. As a
2087 * result, programs leaking references to None and False (etc)
2088 * couldn't be diagnosed by examining sys.getobjects(0).
2089 */
2090#define ADD_TO_ALL(OBJECT) _Py_AddToAllObjects((PyObject *)(OBJECT), 0)
2091#else
2092#define ADD_TO_ALL(OBJECT) (void)0
2093#endif
2094
Tim Peters4b7625e2001-09-13 21:37:17 +00002095#define SETBUILTIN(NAME, OBJECT) \
Tim Peters7571a0f2003-03-23 17:52:28 +00002096 if (PyDict_SetItemString(dict, NAME, (PyObject *)OBJECT) < 0) \
2097 return NULL; \
2098 ADD_TO_ALL(OBJECT)
Tim Peters4b7625e2001-09-13 21:37:17 +00002099
2100 SETBUILTIN("None", Py_None);
2101 SETBUILTIN("Ellipsis", Py_Ellipsis);
2102 SETBUILTIN("NotImplemented", Py_NotImplemented);
Guido van Rossum77f6a652002-04-03 22:41:51 +00002103 SETBUILTIN("False", Py_False);
2104 SETBUILTIN("True", Py_True);
Neal Norwitz32a7e7f2002-05-31 19:58:02 +00002105 SETBUILTIN("basestring", &PyBaseString_Type);
Guido van Rossum77f6a652002-04-03 22:41:51 +00002106 SETBUILTIN("bool", &PyBool_Type);
Guido van Rossumbea18cc2002-06-14 20:41:17 +00002107 SETBUILTIN("buffer", &PyBuffer_Type);
Tim Peters4b7625e2001-09-13 21:37:17 +00002108 SETBUILTIN("classmethod", &PyClassMethod_Type);
Tim Peters6d6c1a32001-08-02 04:15:00 +00002109#ifndef WITHOUT_COMPLEX
Tim Peters4b7625e2001-09-13 21:37:17 +00002110 SETBUILTIN("complex", &PyComplex_Type);
Tim Peters6d6c1a32001-08-02 04:15:00 +00002111#endif
Tim Petersa427a2b2001-10-29 22:25:45 +00002112 SETBUILTIN("dict", &PyDict_Type);
Tim Peters67d687a2002-04-29 21:27:32 +00002113 SETBUILTIN("enumerate", &PyEnum_Type);
Tim Peters4b7625e2001-09-13 21:37:17 +00002114 SETBUILTIN("float", &PyFloat_Type);
Raymond Hettingera690a992003-11-16 16:17:49 +00002115 SETBUILTIN("frozenset", &PyFrozenSet_Type);
Tim Peters4b7625e2001-09-13 21:37:17 +00002116 SETBUILTIN("property", &PyProperty_Type);
2117 SETBUILTIN("int", &PyInt_Type);
2118 SETBUILTIN("list", &PyList_Type);
2119 SETBUILTIN("long", &PyLong_Type);
2120 SETBUILTIN("object", &PyBaseObject_Type);
Raymond Hettinger85c20a42003-11-06 14:06:48 +00002121 SETBUILTIN("reversed", &PyReversed_Type);
Raymond Hettingera690a992003-11-16 16:17:49 +00002122 SETBUILTIN("set", &PySet_Type);
Guido van Rossumbea18cc2002-06-14 20:41:17 +00002123 SETBUILTIN("slice", &PySlice_Type);
Tim Peters4b7625e2001-09-13 21:37:17 +00002124 SETBUILTIN("staticmethod", &PyStaticMethod_Type);
2125 SETBUILTIN("str", &PyString_Type);
2126 SETBUILTIN("super", &PySuper_Type);
2127 SETBUILTIN("tuple", &PyTuple_Type);
2128 SETBUILTIN("type", &PyType_Type);
Raymond Hettingerc4c453f2002-06-05 23:12:45 +00002129 SETBUILTIN("xrange", &PyRange_Type);
Tim Peters742dfd62001-09-13 21:49:44 +00002130
2131 /* Note that open() is just an alias of file(). */
2132 SETBUILTIN("open", &PyFile_Type);
Tim Peters4b7625e2001-09-13 21:37:17 +00002133 SETBUILTIN("file", &PyFile_Type);
Martin v. Löwis339d0f72001-08-17 18:39:25 +00002134#ifdef Py_USING_UNICODE
Tim Peters4b7625e2001-09-13 21:37:17 +00002135 SETBUILTIN("unicode", &PyUnicode_Type);
Martin v. Löwis339d0f72001-08-17 18:39:25 +00002136#endif
Guido van Rossum77f6a652002-04-03 22:41:51 +00002137 debug = PyBool_FromLong(Py_OptimizeFlag == 0);
Fred Drake5550de32000-06-20 04:54:19 +00002138 if (PyDict_SetItemString(dict, "__debug__", debug) < 0) {
2139 Py_XDECREF(debug);
Guido van Rossum25ce5661997-08-02 03:10:38 +00002140 return NULL;
Fred Drake5550de32000-06-20 04:54:19 +00002141 }
2142 Py_XDECREF(debug);
Barry Warsaw757af0e1997-08-29 22:13:51 +00002143
Guido van Rossum25ce5661997-08-02 03:10:38 +00002144 return mod;
Tim Peters7571a0f2003-03-23 17:52:28 +00002145#undef ADD_TO_ALL
Tim Peters4b7625e2001-09-13 21:37:17 +00002146#undef SETBUILTIN
Guido van Rossum3f5da241990-12-20 15:06:42 +00002147}
2148
Guido van Rossume77a7571993-11-03 15:01:26 +00002149/* Helper for filter(): filter a tuple through a function */
Guido van Rossum12d12c51993-10-26 17:58:25 +00002150
Guido van Rossum79f25d91997-04-29 20:08:16 +00002151static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002152filtertuple(PyObject *func, PyObject *tuple)
Guido van Rossum12d12c51993-10-26 17:58:25 +00002153{
Guido van Rossum79f25d91997-04-29 20:08:16 +00002154 PyObject *result;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002155 register int i, j;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002156 int len = PyTuple_Size(tuple);
Guido van Rossum12d12c51993-10-26 17:58:25 +00002157
Guido van Rossumb7b45621995-08-04 04:07:45 +00002158 if (len == 0) {
Walter Dörwaldc3da83f2003-02-04 20:24:45 +00002159 if (PyTuple_CheckExact(tuple))
2160 Py_INCREF(tuple);
2161 else
2162 tuple = PyTuple_New(0);
Guido van Rossumb7b45621995-08-04 04:07:45 +00002163 return tuple;
2164 }
2165
Guido van Rossum79f25d91997-04-29 20:08:16 +00002166 if ((result = PyTuple_New(len)) == NULL)
Guido van Rossum2586bf01993-11-01 16:21:44 +00002167 return NULL;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002168
Guido van Rossum12d12c51993-10-26 17:58:25 +00002169 for (i = j = 0; i < len; ++i) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002170 PyObject *item, *good;
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002171 int ok;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002172
Walter Dörwald8dd19322003-02-10 17:36:40 +00002173 if (tuple->ob_type->tp_as_sequence &&
2174 tuple->ob_type->tp_as_sequence->sq_item) {
2175 item = tuple->ob_type->tp_as_sequence->sq_item(tuple, i);
Walter Dörwaldc58a3a12003-08-18 18:28:45 +00002176 if (item == NULL)
2177 goto Fail_1;
Walter Dörwald8dd19322003-02-10 17:36:40 +00002178 } else {
Alex Martellia9b9c9f2003-04-23 13:34:35 +00002179 PyErr_SetString(PyExc_TypeError, "filter(): unsubscriptable tuple");
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002180 goto Fail_1;
Walter Dörwald8dd19322003-02-10 17:36:40 +00002181 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002182 if (func == Py_None) {
2183 Py_INCREF(item);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002184 good = item;
2185 }
2186 else {
Raymond Hettinger8ae46892003-10-12 19:09:37 +00002187 PyObject *arg = PyTuple_Pack(1, item);
Walter Dörwaldc58a3a12003-08-18 18:28:45 +00002188 if (arg == NULL) {
2189 Py_DECREF(item);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002190 goto Fail_1;
Walter Dörwaldc58a3a12003-08-18 18:28:45 +00002191 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002192 good = PyEval_CallObject(func, arg);
2193 Py_DECREF(arg);
Walter Dörwaldc58a3a12003-08-18 18:28:45 +00002194 if (good == NULL) {
2195 Py_DECREF(item);
Guido van Rossum12d12c51993-10-26 17:58:25 +00002196 goto Fail_1;
Walter Dörwaldc58a3a12003-08-18 18:28:45 +00002197 }
Guido van Rossum12d12c51993-10-26 17:58:25 +00002198 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002199 ok = PyObject_IsTrue(good);
2200 Py_DECREF(good);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002201 if (ok) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002202 if (PyTuple_SetItem(result, j++, item) < 0)
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002203 goto Fail_1;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002204 }
Walter Dörwaldc58a3a12003-08-18 18:28:45 +00002205 else
2206 Py_DECREF(item);
Guido van Rossum12d12c51993-10-26 17:58:25 +00002207 }
2208
Tim Peters4324aa32001-05-28 22:30:08 +00002209 if (_PyTuple_Resize(&result, j) < 0)
Guido van Rossum12d12c51993-10-26 17:58:25 +00002210 return NULL;
2211
Guido van Rossum12d12c51993-10-26 17:58:25 +00002212 return result;
2213
Guido van Rossum12d12c51993-10-26 17:58:25 +00002214Fail_1:
Guido van Rossum79f25d91997-04-29 20:08:16 +00002215 Py_DECREF(result);
Guido van Rossum12d12c51993-10-26 17:58:25 +00002216 return NULL;
2217}
2218
2219
Guido van Rossume77a7571993-11-03 15:01:26 +00002220/* Helper for filter(): filter a string through a function */
Guido van Rossum12d12c51993-10-26 17:58:25 +00002221
Guido van Rossum79f25d91997-04-29 20:08:16 +00002222static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002223filterstring(PyObject *func, PyObject *strobj)
Guido van Rossum12d12c51993-10-26 17:58:25 +00002224{
Guido van Rossum79f25d91997-04-29 20:08:16 +00002225 PyObject *result;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002226 register int i, j;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002227 int len = PyString_Size(strobj);
Walter Dörwald903f1e02003-02-04 16:28:00 +00002228 int outlen = len;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002229
Guido van Rossum79f25d91997-04-29 20:08:16 +00002230 if (func == Py_None) {
Walter Dörwald1918f772003-02-10 13:19:13 +00002231 /* If it's a real string we can return the original,
2232 * as no character is ever false and __getitem__
2233 * does return this character. If it's a subclass
2234 * we must go through the __getitem__ loop */
2235 if (PyString_CheckExact(strobj)) {
Walter Dörwaldc3da83f2003-02-04 20:24:45 +00002236 Py_INCREF(strobj);
Walter Dörwald1918f772003-02-10 13:19:13 +00002237 return strobj;
2238 }
Guido van Rossum12d12c51993-10-26 17:58:25 +00002239 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002240 if ((result = PyString_FromStringAndSize(NULL, len)) == NULL)
Guido van Rossum2586bf01993-11-01 16:21:44 +00002241 return NULL;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002242
Guido van Rossum12d12c51993-10-26 17:58:25 +00002243 for (i = j = 0; i < len; ++i) {
Walter Dörwald1918f772003-02-10 13:19:13 +00002244 PyObject *item;
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002245 int ok;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002246
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002247 item = (*strobj->ob_type->tp_as_sequence->sq_item)(strobj, i);
2248 if (item == NULL)
2249 goto Fail_1;
Walter Dörwald1918f772003-02-10 13:19:13 +00002250 if (func==Py_None) {
2251 ok = 1;
2252 } else {
2253 PyObject *arg, *good;
Raymond Hettinger8ae46892003-10-12 19:09:37 +00002254 arg = PyTuple_Pack(1, item);
Walter Dörwald1918f772003-02-10 13:19:13 +00002255 if (arg == NULL) {
2256 Py_DECREF(item);
2257 goto Fail_1;
2258 }
2259 good = PyEval_CallObject(func, arg);
2260 Py_DECREF(arg);
2261 if (good == NULL) {
2262 Py_DECREF(item);
2263 goto Fail_1;
2264 }
2265 ok = PyObject_IsTrue(good);
2266 Py_DECREF(good);
Tim Peters388ed082001-04-07 20:34:48 +00002267 }
Walter Dörwald903f1e02003-02-04 16:28:00 +00002268 if (ok) {
2269 int reslen;
2270 if (!PyString_Check(item)) {
2271 PyErr_SetString(PyExc_TypeError, "can't filter str to str:"
2272 " __getitem__ returned different type");
2273 Py_DECREF(item);
2274 goto Fail_1;
2275 }
2276 reslen = PyString_GET_SIZE(item);
2277 if (reslen == 1) {
2278 PyString_AS_STRING(result)[j++] =
2279 PyString_AS_STRING(item)[0];
2280 } else {
2281 /* do we need more space? */
2282 int need = j + reslen + len-i-1;
2283 if (need > outlen) {
2284 /* overallocate, to avoid reallocations */
2285 if (need<2*outlen)
2286 need = 2*outlen;
2287 if (_PyString_Resize(&result, need)) {
2288 Py_DECREF(item);
2289 return NULL;
2290 }
2291 outlen = need;
2292 }
2293 memcpy(
2294 PyString_AS_STRING(result) + j,
2295 PyString_AS_STRING(item),
2296 reslen
2297 );
2298 j += reslen;
2299 }
2300 }
Tim Peters388ed082001-04-07 20:34:48 +00002301 Py_DECREF(item);
Guido van Rossum12d12c51993-10-26 17:58:25 +00002302 }
2303
Walter Dörwald903f1e02003-02-04 16:28:00 +00002304 if (j < outlen)
Tim Peters5de98422002-04-27 18:44:32 +00002305 _PyString_Resize(&result, j);
Guido van Rossum12d12c51993-10-26 17:58:25 +00002306
Guido van Rossum12d12c51993-10-26 17:58:25 +00002307 return result;
2308
Guido van Rossum12d12c51993-10-26 17:58:25 +00002309Fail_1:
Guido van Rossum79f25d91997-04-29 20:08:16 +00002310 Py_DECREF(result);
Guido van Rossum12d12c51993-10-26 17:58:25 +00002311 return NULL;
2312}
Martin v. Löwis8afd7572003-01-25 22:46:11 +00002313
2314#ifdef Py_USING_UNICODE
2315/* Helper for filter(): filter a Unicode object through a function */
2316
2317static PyObject *
2318filterunicode(PyObject *func, PyObject *strobj)
2319{
2320 PyObject *result;
2321 register int i, j;
2322 int len = PyUnicode_GetSize(strobj);
Walter Dörwald903f1e02003-02-04 16:28:00 +00002323 int outlen = len;
Martin v. Löwis8afd7572003-01-25 22:46:11 +00002324
2325 if (func == Py_None) {
Walter Dörwald1918f772003-02-10 13:19:13 +00002326 /* If it's a real string we can return the original,
2327 * as no character is ever false and __getitem__
2328 * does return this character. If it's a subclass
2329 * we must go through the __getitem__ loop */
2330 if (PyUnicode_CheckExact(strobj)) {
Walter Dörwaldc3da83f2003-02-04 20:24:45 +00002331 Py_INCREF(strobj);
Walter Dörwald1918f772003-02-10 13:19:13 +00002332 return strobj;
2333 }
Martin v. Löwis8afd7572003-01-25 22:46:11 +00002334 }
2335 if ((result = PyUnicode_FromUnicode(NULL, len)) == NULL)
2336 return NULL;
2337
2338 for (i = j = 0; i < len; ++i) {
2339 PyObject *item, *arg, *good;
2340 int ok;
2341
2342 item = (*strobj->ob_type->tp_as_sequence->sq_item)(strobj, i);
2343 if (item == NULL)
2344 goto Fail_1;
Walter Dörwald1918f772003-02-10 13:19:13 +00002345 if (func == Py_None) {
2346 ok = 1;
2347 } else {
Raymond Hettinger8ae46892003-10-12 19:09:37 +00002348 arg = PyTuple_Pack(1, item);
Walter Dörwald1918f772003-02-10 13:19:13 +00002349 if (arg == NULL) {
2350 Py_DECREF(item);
2351 goto Fail_1;
2352 }
2353 good = PyEval_CallObject(func, arg);
2354 Py_DECREF(arg);
2355 if (good == NULL) {
2356 Py_DECREF(item);
2357 goto Fail_1;
2358 }
2359 ok = PyObject_IsTrue(good);
2360 Py_DECREF(good);
Martin v. Löwis8afd7572003-01-25 22:46:11 +00002361 }
Walter Dörwald903f1e02003-02-04 16:28:00 +00002362 if (ok) {
2363 int reslen;
2364 if (!PyUnicode_Check(item)) {
Jeremy Hylton1aad9c72003-09-16 03:10:59 +00002365 PyErr_SetString(PyExc_TypeError,
2366 "can't filter unicode to unicode:"
2367 " __getitem__ returned different type");
Walter Dörwald903f1e02003-02-04 16:28:00 +00002368 Py_DECREF(item);
2369 goto Fail_1;
2370 }
2371 reslen = PyUnicode_GET_SIZE(item);
Jeremy Hylton1aad9c72003-09-16 03:10:59 +00002372 if (reslen == 1)
Walter Dörwald903f1e02003-02-04 16:28:00 +00002373 PyUnicode_AS_UNICODE(result)[j++] =
2374 PyUnicode_AS_UNICODE(item)[0];
Jeremy Hylton1aad9c72003-09-16 03:10:59 +00002375 else {
Walter Dörwald903f1e02003-02-04 16:28:00 +00002376 /* do we need more space? */
Jeremy Hylton1aad9c72003-09-16 03:10:59 +00002377 int need = j + reslen + len - i - 1;
Walter Dörwald903f1e02003-02-04 16:28:00 +00002378 if (need > outlen) {
Jeremy Hylton1aad9c72003-09-16 03:10:59 +00002379 /* overallocate,
2380 to avoid reallocations */
2381 if (need < 2 * outlen)
2382 need = 2 * outlen;
Jeremy Hylton364f6be2003-09-16 03:17:16 +00002383 if (PyUnicode_Resize(
2384 &result, need) < 0) {
Walter Dörwald903f1e02003-02-04 16:28:00 +00002385 Py_DECREF(item);
Walter Dörwald531e0002003-02-04 16:57:49 +00002386 goto Fail_1;
Walter Dörwald903f1e02003-02-04 16:28:00 +00002387 }
2388 outlen = need;
2389 }
Jeremy Hylton1aad9c72003-09-16 03:10:59 +00002390 memcpy(PyUnicode_AS_UNICODE(result) + j,
2391 PyUnicode_AS_UNICODE(item),
2392 reslen*sizeof(Py_UNICODE));
Walter Dörwald903f1e02003-02-04 16:28:00 +00002393 j += reslen;
2394 }
2395 }
Martin v. Löwis8afd7572003-01-25 22:46:11 +00002396 Py_DECREF(item);
2397 }
2398
Walter Dörwald903f1e02003-02-04 16:28:00 +00002399 if (j < outlen)
Martin v. Löwis8afd7572003-01-25 22:46:11 +00002400 PyUnicode_Resize(&result, j);
2401
2402 return result;
2403
2404Fail_1:
2405 Py_DECREF(result);
2406 return NULL;
2407}
2408#endif