blob: a9d68caa3dcf674bc558fdba2ad59f32be013b32 [file] [log] [blame]
Guido van Rossum3f5da241990-12-20 15:06:42 +00001/* Built-in functions */
2
Guido van Rossum79f25d91997-04-29 20:08:16 +00003#include "Python.h"
Guido van Rossum3f5da241990-12-20 15:06:42 +00004
5#include "node.h"
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006#include "code.h"
Guido van Rossum5b722181993-03-30 17:46:03 +00007#include "eval.h"
Guido van Rossum3f5da241990-12-20 15:06:42 +00008
Guido van Rossum6bf62da1997-04-11 20:37:35 +00009#include <ctype.h>
10
Guido van Rossume2ae77b2001-10-24 20:42:55 +000011#ifdef RISCOS
12#include "unixstuff.h"
13#endif
14
Mark Hammond26cffde2001-05-14 12:17:34 +000015/* The default encoding used by the platform file system APIs
16 Can remain NULL for all platforms that don't have such a concept
17*/
Martin v. Löwis6238d2b2002-06-30 15:26:10 +000018#if defined(MS_WINDOWS) && defined(HAVE_USABLE_WCHAR_T)
Mark Hammond26cffde2001-05-14 12:17:34 +000019const char *Py_FileSystemDefaultEncoding = "mbcs";
Just van Rossumb9b8e9c2003-02-10 09:22:01 +000020#elif defined(__APPLE__)
21const char *Py_FileSystemDefaultEncoding = "utf-8";
Mark Hammond26cffde2001-05-14 12:17:34 +000022#else
23const char *Py_FileSystemDefaultEncoding = NULL; /* use default */
24#endif
Mark Hammondef8b6542001-05-13 08:04:26 +000025
Guido van Rossum12d12c51993-10-26 17:58:25 +000026/* Forward */
Tim Petersdbd9ba62000-07-09 03:09:57 +000027static PyObject *filterstring(PyObject *, PyObject *);
Martin v. Löwis8afd7572003-01-25 22:46:11 +000028#ifdef Py_USING_UNICODE
29static PyObject *filterunicode(PyObject *, PyObject *);
30#endif
Tim Petersdbd9ba62000-07-09 03:09:57 +000031static PyObject *filtertuple (PyObject *, PyObject *);
Guido van Rossum12d12c51993-10-26 17:58:25 +000032
Guido van Rossum79f25d91997-04-29 20:08:16 +000033static PyObject *
Neal Norwitz92e212f2006-04-03 04:48:37 +000034builtin___import__(PyObject *self, PyObject *args, PyObject *kwds)
Guido van Rossum3f5da241990-12-20 15:06:42 +000035{
Neal Norwitz92e212f2006-04-03 04:48:37 +000036 static char *kwlist[] = {"name", "globals", "locals", "fromlist",
37 "level", 0};
Guido van Rossum1ae940a1995-01-02 19:04:15 +000038 char *name;
Guido van Rossum79f25d91997-04-29 20:08:16 +000039 PyObject *globals = NULL;
40 PyObject *locals = NULL;
41 PyObject *fromlist = NULL;
Thomas Woutersf7f438b2006-02-28 16:09:29 +000042 int level = -1;
Guido van Rossum1ae940a1995-01-02 19:04:15 +000043
Neal Norwitz92e212f2006-04-03 04:48:37 +000044 if (!PyArg_ParseTupleAndKeywords(args, kwds, "s|OOOi:__import__",
45 kwlist, &name, &globals, &locals, &fromlist, &level))
Guido van Rossum1ae940a1995-01-02 19:04:15 +000046 return NULL;
Thomas Woutersf7f438b2006-02-28 16:09:29 +000047 return PyImport_ImportModuleLevel(name, globals, locals,
48 fromlist, level);
Guido van Rossum1ae940a1995-01-02 19:04:15 +000049}
50
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000051PyDoc_STRVAR(import_doc,
Neal Norwitz92e212f2006-04-03 04:48:37 +000052"__import__(name, globals={}, locals={}, fromlist=[], level=-1) -> module\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +000053\n\
54Import a module. The globals are only used to determine the context;\n\
55they are not modified. The locals are currently unused. The fromlist\n\
56should be a list of names to emulate ``from name import ...'', or an\n\
57empty list to emulate ``import name''.\n\
58When importing a module from a package, note that __import__('A.B', ...)\n\
59returns package A when fromlist is empty, but its submodule B when\n\
Neal Norwitz92e212f2006-04-03 04:48:37 +000060fromlist is not empty. Level is used to determine whether to perform \n\
61absolute or relative imports. -1 is the original strategy of attempting\n\
62both absolute and relative imports, 0 is absolute, a positive number\n\
63is the number of parent directories to search relative to the current module.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +000064
Guido van Rossum1ae940a1995-01-02 19:04:15 +000065
Guido van Rossum79f25d91997-04-29 20:08:16 +000066static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +000067builtin_abs(PyObject *self, PyObject *v)
Guido van Rossum1ae940a1995-01-02 19:04:15 +000068{
Guido van Rossum09df08a1998-05-22 00:51:39 +000069 return PyNumber_Absolute(v);
Guido van Rossum3f5da241990-12-20 15:06:42 +000070}
71
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000072PyDoc_STRVAR(abs_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +000073"abs(number) -> number\n\
74\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000075Return the absolute value of the argument.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +000076
Raymond Hettinger96229b12005-03-11 06:49:40 +000077static PyObject *
78builtin_all(PyObject *self, PyObject *v)
79{
80 PyObject *it, *item;
Guido van Rossum01dbc102007-12-20 23:48:28 +000081 PyObject *(*iternext)(PyObject *);
82 int cmp;
Raymond Hettinger96229b12005-03-11 06:49:40 +000083
84 it = PyObject_GetIter(v);
85 if (it == NULL)
86 return NULL;
Guido van Rossum01dbc102007-12-20 23:48:28 +000087 iternext = *Py_TYPE(it)->tp_iternext;
Raymond Hettinger96229b12005-03-11 06:49:40 +000088
Guido van Rossum01dbc102007-12-20 23:48:28 +000089 for (;;) {
90 item = iternext(it);
91 if (item == NULL)
92 break;
93 cmp = PyObject_IsTrue(item);
Raymond Hettinger96229b12005-03-11 06:49:40 +000094 Py_DECREF(item);
95 if (cmp < 0) {
96 Py_DECREF(it);
97 return NULL;
98 }
99 if (cmp == 0) {
100 Py_DECREF(it);
101 Py_RETURN_FALSE;
102 }
103 }
104 Py_DECREF(it);
Guido van Rossum01dbc102007-12-20 23:48:28 +0000105 if (PyErr_Occurred()) {
106 if (PyErr_ExceptionMatches(PyExc_StopIteration))
107 PyErr_Clear();
108 else
109 return NULL;
110 }
Raymond Hettinger96229b12005-03-11 06:49:40 +0000111 Py_RETURN_TRUE;
112}
113
114PyDoc_STRVAR(all_doc,
115"all(iterable) -> bool\n\
116\n\
117Return True if bool(x) is True for all values x in the iterable.");
118
119static PyObject *
120builtin_any(PyObject *self, PyObject *v)
121{
122 PyObject *it, *item;
Guido van Rossum01dbc102007-12-20 23:48:28 +0000123 PyObject *(*iternext)(PyObject *);
124 int cmp;
Raymond Hettinger96229b12005-03-11 06:49:40 +0000125
126 it = PyObject_GetIter(v);
127 if (it == NULL)
128 return NULL;
Guido van Rossum01dbc102007-12-20 23:48:28 +0000129 iternext = *Py_TYPE(it)->tp_iternext;
Raymond Hettinger96229b12005-03-11 06:49:40 +0000130
Guido van Rossum01dbc102007-12-20 23:48:28 +0000131 for (;;) {
132 item = iternext(it);
133 if (item == NULL)
134 break;
135 cmp = PyObject_IsTrue(item);
Raymond Hettinger96229b12005-03-11 06:49:40 +0000136 Py_DECREF(item);
137 if (cmp < 0) {
138 Py_DECREF(it);
139 return NULL;
140 }
141 if (cmp == 1) {
142 Py_DECREF(it);
143 Py_RETURN_TRUE;
144 }
145 }
146 Py_DECREF(it);
Guido van Rossum01dbc102007-12-20 23:48:28 +0000147 if (PyErr_Occurred()) {
148 if (PyErr_ExceptionMatches(PyExc_StopIteration))
149 PyErr_Clear();
150 else
151 return NULL;
152 }
Raymond Hettinger96229b12005-03-11 06:49:40 +0000153 Py_RETURN_FALSE;
154}
155
156PyDoc_STRVAR(any_doc,
157"any(iterable) -> bool\n\
158\n\
159Return True if bool(x) is True for any x in the iterable.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000160
Guido van Rossum79f25d91997-04-29 20:08:16 +0000161static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000162builtin_apply(PyObject *self, PyObject *args)
Guido van Rossumc02e15c1991-12-16 13:03:00 +0000163{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000164 PyObject *func, *alist = NULL, *kwdict = NULL;
Barry Warsaw968f8cb1998-10-01 15:33:12 +0000165 PyObject *t = NULL, *retval = NULL;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000166
Neal Norwitz8b2bfbc2007-05-23 06:35:32 +0000167 if (Py_Py3kWarningFlag &&
168 PyErr_Warn(PyExc_DeprecationWarning,
169 "apply() not supported in 3.x") < 0)
170 return NULL;
171
Raymond Hettingerea3fdf42002-12-29 16:33:45 +0000172 if (!PyArg_UnpackTuple(args, "apply", 1, 3, &func, &alist, &kwdict))
Guido van Rossumc02e15c1991-12-16 13:03:00 +0000173 return NULL;
Barry Warsaw968f8cb1998-10-01 15:33:12 +0000174 if (alist != NULL) {
175 if (!PyTuple_Check(alist)) {
176 if (!PySequence_Check(alist)) {
Jeremy Hyltonc862cf42001-01-19 03:25:05 +0000177 PyErr_Format(PyExc_TypeError,
Alex Martellia9b9c9f2003-04-23 13:34:35 +0000178 "apply() arg 2 expected sequence, found %s",
Jeremy Hyltonc862cf42001-01-19 03:25:05 +0000179 alist->ob_type->tp_name);
Barry Warsaw968f8cb1998-10-01 15:33:12 +0000180 return NULL;
181 }
182 t = PySequence_Tuple(alist);
183 if (t == NULL)
184 return NULL;
185 alist = t;
186 }
Guido van Rossum2d951851994-08-29 12:52:16 +0000187 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000188 if (kwdict != NULL && !PyDict_Check(kwdict)) {
Jeremy Hyltonc862cf42001-01-19 03:25:05 +0000189 PyErr_Format(PyExc_TypeError,
190 "apply() arg 3 expected dictionary, found %s",
191 kwdict->ob_type->tp_name);
Barry Warsaw968f8cb1998-10-01 15:33:12 +0000192 goto finally;
Guido van Rossum681d79a1995-07-18 14:51:37 +0000193 }
Barry Warsaw968f8cb1998-10-01 15:33:12 +0000194 retval = PyEval_CallObjectWithKeywords(func, alist, kwdict);
195 finally:
196 Py_XDECREF(t);
197 return retval;
Guido van Rossumc02e15c1991-12-16 13:03:00 +0000198}
199
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000200PyDoc_STRVAR(apply_doc,
Fred Drakef1fbc622001-01-12 17:05:05 +0000201"apply(object[, args[, kwargs]]) -> value\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000202\n\
Fred Drake7b912121999-12-23 14:16:55 +0000203Call a callable object with positional arguments taken from the tuple args,\n\
204and keyword arguments taken from the optional dictionary kwargs.\n\
Raymond Hettingerff41c482003-04-06 09:01:11 +0000205Note that classes are callable, as are instances with a __call__() method.\n\
206\n\
207Deprecated since release 2.3. Instead, use the extended call syntax:\n\
208 function(*args, **keywords).");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000209
210
Guido van Rossum79f25d91997-04-29 20:08:16 +0000211static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000212builtin_callable(PyObject *self, PyObject *v)
Guido van Rossum2d951851994-08-29 12:52:16 +0000213{
Neal Norwitzdf25efe2007-05-23 06:58:36 +0000214 if (Py_Py3kWarningFlag &&
215 PyErr_Warn(PyExc_DeprecationWarning,
216 "callable() not supported in 3.x") < 0)
217 return NULL;
Guido van Rossum77f6a652002-04-03 22:41:51 +0000218 return PyBool_FromLong((long)PyCallable_Check(v));
Guido van Rossum2d951851994-08-29 12:52:16 +0000219}
220
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000221PyDoc_STRVAR(callable_doc,
Guido van Rossum77f6a652002-04-03 22:41:51 +0000222"callable(object) -> bool\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000223\n\
224Return whether the object is callable (i.e., some kind of function).\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000225Note that classes are callable, as are instances with a __call__() method.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000226
227
Guido van Rossum79f25d91997-04-29 20:08:16 +0000228static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000229builtin_filter(PyObject *self, PyObject *args)
Guido van Rossum12d12c51993-10-26 17:58:25 +0000230{
Guido van Rossumc7903a12002-08-16 07:04:56 +0000231 PyObject *func, *seq, *result, *it, *arg;
Martin v. Löwisd96ee902006-02-16 14:37:16 +0000232 Py_ssize_t len; /* guess for result list size */
233 register Py_ssize_t j;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000234
Raymond Hettingerea3fdf42002-12-29 16:33:45 +0000235 if (!PyArg_UnpackTuple(args, "filter", 2, 2, &func, &seq))
Guido van Rossum12d12c51993-10-26 17:58:25 +0000236 return NULL;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000237
Tim Peters0e57abf2001-05-02 07:39:38 +0000238 /* Strings and tuples return a result of the same type. */
239 if (PyString_Check(seq))
240 return filterstring(func, seq);
Martin v. Löwis8afd7572003-01-25 22:46:11 +0000241#ifdef Py_USING_UNICODE
242 if (PyUnicode_Check(seq))
243 return filterunicode(func, seq);
244#endif
Tim Peters0e57abf2001-05-02 07:39:38 +0000245 if (PyTuple_Check(seq))
246 return filtertuple(func, seq);
247
Georg Brandle35b6572005-07-19 22:20:20 +0000248 /* Pre-allocate argument list tuple. */
249 arg = PyTuple_New(1);
250 if (arg == NULL)
251 return NULL;
252
Tim Peters0e57abf2001-05-02 07:39:38 +0000253 /* Get iterator. */
254 it = PyObject_GetIter(seq);
255 if (it == NULL)
Georg Brandle35b6572005-07-19 22:20:20 +0000256 goto Fail_arg;
Tim Peters0e57abf2001-05-02 07:39:38 +0000257
258 /* Guess a result list size. */
Raymond Hettinger4e2f7142007-12-06 00:56:53 +0000259 len = _PyObject_LengthHint(seq, 8);
Guido van Rossum12d12c51993-10-26 17:58:25 +0000260
Tim Peters0e57abf2001-05-02 07:39:38 +0000261 /* Get a result list. */
Guido van Rossum79f25d91997-04-29 20:08:16 +0000262 if (PyList_Check(seq) && seq->ob_refcnt == 1) {
Tim Peters0e57abf2001-05-02 07:39:38 +0000263 /* Eww - can modify the list in-place. */
Guido van Rossum79f25d91997-04-29 20:08:16 +0000264 Py_INCREF(seq);
Guido van Rossum12d12c51993-10-26 17:58:25 +0000265 result = seq;
266 }
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000267 else {
Tim Peters0e57abf2001-05-02 07:39:38 +0000268 result = PyList_New(len);
269 if (result == NULL)
270 goto Fail_it;
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000271 }
Guido van Rossum12d12c51993-10-26 17:58:25 +0000272
Tim Peters0e57abf2001-05-02 07:39:38 +0000273 /* Build the result list. */
Tim Petersf4848da2001-05-05 00:14:56 +0000274 j = 0;
275 for (;;) {
Guido van Rossumc7903a12002-08-16 07:04:56 +0000276 PyObject *item;
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000277 int ok;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000278
Tim Peters0e57abf2001-05-02 07:39:38 +0000279 item = PyIter_Next(it);
280 if (item == NULL) {
Tim Petersf4848da2001-05-05 00:14:56 +0000281 if (PyErr_Occurred())
282 goto Fail_result_it;
Tim Peters0e57abf2001-05-02 07:39:38 +0000283 break;
Guido van Rossum2d951851994-08-29 12:52:16 +0000284 }
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000285
Neil Schemenauer68973552003-08-14 20:37:34 +0000286 if (func == (PyObject *)&PyBool_Type || func == Py_None) {
Guido van Rossumc7903a12002-08-16 07:04:56 +0000287 ok = PyObject_IsTrue(item);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000288 }
289 else {
Guido van Rossumc7903a12002-08-16 07:04:56 +0000290 PyObject *good;
291 PyTuple_SET_ITEM(arg, 0, item);
292 good = PyObject_Call(func, arg, NULL);
293 PyTuple_SET_ITEM(arg, 0, NULL);
Guido van Rossum58b68731995-01-10 17:40:55 +0000294 if (good == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000295 Py_DECREF(item);
Tim Peters0e57abf2001-05-02 07:39:38 +0000296 goto Fail_result_it;
Guido van Rossum58b68731995-01-10 17:40:55 +0000297 }
Guido van Rossumc7903a12002-08-16 07:04:56 +0000298 ok = PyObject_IsTrue(good);
299 Py_DECREF(good);
Guido van Rossum12d12c51993-10-26 17:58:25 +0000300 }
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000301 if (ok) {
Tim Peters0e57abf2001-05-02 07:39:38 +0000302 if (j < len)
303 PyList_SET_ITEM(result, j, item);
Guido van Rossum2d951851994-08-29 12:52:16 +0000304 else {
Barry Warsawfa77e091999-01-28 18:49:12 +0000305 int status = PyList_Append(result, item);
Barry Warsawfa77e091999-01-28 18:49:12 +0000306 Py_DECREF(item);
307 if (status < 0)
Tim Peters0e57abf2001-05-02 07:39:38 +0000308 goto Fail_result_it;
Guido van Rossum2d951851994-08-29 12:52:16 +0000309 }
Tim Peters0e57abf2001-05-02 07:39:38 +0000310 ++j;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000311 }
Tim Peters0e57abf2001-05-02 07:39:38 +0000312 else
313 Py_DECREF(item);
Guido van Rossum12d12c51993-10-26 17:58:25 +0000314 }
315
Guido van Rossum12d12c51993-10-26 17:58:25 +0000316
Tim Peters0e57abf2001-05-02 07:39:38 +0000317 /* Cut back result list if len is too big. */
Guido van Rossum79f25d91997-04-29 20:08:16 +0000318 if (j < len && PyList_SetSlice(result, j, len, NULL) < 0)
Tim Peters0e57abf2001-05-02 07:39:38 +0000319 goto Fail_result_it;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000320
Tim Peters3c6b1482001-05-21 08:07:05 +0000321 Py_DECREF(it);
Guido van Rossumc7903a12002-08-16 07:04:56 +0000322 Py_DECREF(arg);
Guido van Rossum12d12c51993-10-26 17:58:25 +0000323 return result;
324
Tim Peters0e57abf2001-05-02 07:39:38 +0000325Fail_result_it:
Guido van Rossum79f25d91997-04-29 20:08:16 +0000326 Py_DECREF(result);
Tim Peters0e57abf2001-05-02 07:39:38 +0000327Fail_it:
328 Py_DECREF(it);
Guido van Rossumc7903a12002-08-16 07:04:56 +0000329Fail_arg:
330 Py_DECREF(arg);
Guido van Rossum12d12c51993-10-26 17:58:25 +0000331 return NULL;
332}
333
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000334PyDoc_STRVAR(filter_doc,
Tim Petersd50e5442002-03-09 00:06:26 +0000335"filter(function or None, sequence) -> list, tuple, or string\n"
336"\n"
337"Return those items of sequence for which function(item) is true. If\n"
338"function is None, return the items that are true. If sequence is a tuple\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000339"or string, return the same type, else return a list.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000340
Guido van Rossum79f25d91997-04-29 20:08:16 +0000341static PyObject *
Eric Smitha9f7d622008-02-17 19:46:49 +0000342builtin_format(PyObject *self, PyObject *args)
343{
344 PyObject *value;
345 PyObject *format_spec = NULL;
346
347 if (!PyArg_ParseTuple(args, "O|O:format", &value, &format_spec))
348 return NULL;
349
350 return PyObject_Format(value, format_spec);
351}
352
353PyDoc_STRVAR(format_doc,
354"format(value[, format_spec]) -> string\n\
355\n\
356Returns value.__format__(format_spec)\n\
357format_spec defaults to \"\"");
358
359static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000360builtin_chr(PyObject *self, PyObject *args)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000361{
362 long x;
363 char s[1];
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000364
Guido van Rossum79f25d91997-04-29 20:08:16 +0000365 if (!PyArg_ParseTuple(args, "l:chr", &x))
Guido van Rossum3f5da241990-12-20 15:06:42 +0000366 return NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000367 if (x < 0 || x >= 256) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000368 PyErr_SetString(PyExc_ValueError,
369 "chr() arg not in range(256)");
Guido van Rossum3f5da241990-12-20 15:06:42 +0000370 return NULL;
371 }
Guido van Rossum6bf62da1997-04-11 20:37:35 +0000372 s[0] = (char)x;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000373 return PyString_FromStringAndSize(s, 1);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000374}
375
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000376PyDoc_STRVAR(chr_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000377"chr(i) -> character\n\
378\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000379Return a string of one character with ordinal i; 0 <= i < 256.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000380
381
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000382#ifdef Py_USING_UNICODE
Guido van Rossum79f25d91997-04-29 20:08:16 +0000383static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000384builtin_unichr(PyObject *self, PyObject *args)
Guido van Rossum09095f32000-03-10 23:00:52 +0000385{
386 long x;
Guido van Rossum09095f32000-03-10 23:00:52 +0000387
388 if (!PyArg_ParseTuple(args, "l:unichr", &x))
389 return NULL;
Fredrik Lundh0dcf67e2001-06-26 20:01:56 +0000390
Marc-André Lemburgcc8764c2002-08-11 12:23:04 +0000391 return PyUnicode_FromOrdinal(x);
Guido van Rossum09095f32000-03-10 23:00:52 +0000392}
393
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000394PyDoc_STRVAR(unichr_doc,
Fred Drake078b24f2000-04-13 02:42:50 +0000395"unichr(i) -> Unicode character\n\
Guido van Rossum09095f32000-03-10 23:00:52 +0000396\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000397Return a Unicode string of one character with ordinal i; 0 <= i <= 0x10ffff.");
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000398#endif
Guido van Rossum09095f32000-03-10 23:00:52 +0000399
400
401static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000402builtin_cmp(PyObject *self, PyObject *args)
Guido van Rossuma9e7dc11992-10-18 18:53:57 +0000403{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000404 PyObject *a, *b;
Guido van Rossum09df08a1998-05-22 00:51:39 +0000405 int c;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000406
Raymond Hettingerea3fdf42002-12-29 16:33:45 +0000407 if (!PyArg_UnpackTuple(args, "cmp", 2, 2, &a, &b))
Guido van Rossuma9e7dc11992-10-18 18:53:57 +0000408 return NULL;
Guido van Rossum09df08a1998-05-22 00:51:39 +0000409 if (PyObject_Cmp(a, b, &c) < 0)
Guido van Rossumc8b6df91997-05-23 00:06:51 +0000410 return NULL;
Guido van Rossum09df08a1998-05-22 00:51:39 +0000411 return PyInt_FromLong((long)c);
Guido van Rossuma9e7dc11992-10-18 18:53:57 +0000412}
413
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000414PyDoc_STRVAR(cmp_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000415"cmp(x, y) -> integer\n\
416\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000417Return negative if x<y, zero if x==y, positive if x>y.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000418
419
Guido van Rossum79f25d91997-04-29 20:08:16 +0000420static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000421builtin_coerce(PyObject *self, PyObject *args)
Guido van Rossum6a00cd81995-01-07 12:39:01 +0000422{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000423 PyObject *v, *w;
424 PyObject *res;
Guido van Rossum5524a591995-01-10 15:26:20 +0000425
Neal Norwitzdf25efe2007-05-23 06:58:36 +0000426 if (Py_Py3kWarningFlag &&
427 PyErr_Warn(PyExc_DeprecationWarning,
428 "coerce() not supported in 3.x") < 0)
429 return NULL;
430
Raymond Hettingerea3fdf42002-12-29 16:33:45 +0000431 if (!PyArg_UnpackTuple(args, "coerce", 2, 2, &v, &w))
Guido van Rossum5524a591995-01-10 15:26:20 +0000432 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000433 if (PyNumber_Coerce(&v, &w) < 0)
Guido van Rossum04691fc1992-08-12 15:35:34 +0000434 return NULL;
Raymond Hettinger8ae46892003-10-12 19:09:37 +0000435 res = PyTuple_Pack(2, v, w);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000436 Py_DECREF(v);
437 Py_DECREF(w);
Guido van Rossum04691fc1992-08-12 15:35:34 +0000438 return res;
439}
440
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000441PyDoc_STRVAR(coerce_doc,
Martin v. Löwis8d494f32004-08-25 10:42:41 +0000442"coerce(x, y) -> (x1, y1)\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000443\n\
Martin v. Löwis8d494f32004-08-25 10:42:41 +0000444Return a tuple consisting of the two numeric arguments converted to\n\
445a common type, using the same rules as used by arithmetic operations.\n\
446If coercion is not possible, raise TypeError.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000447
Guido van Rossum79f25d91997-04-29 20:08:16 +0000448static PyObject *
Georg Brandl5240d742007-03-13 20:46:32 +0000449builtin_compile(PyObject *self, PyObject *args, PyObject *kwds)
Guido van Rossum5b722181993-03-30 17:46:03 +0000450{
451 char *str;
452 char *filename;
453 char *startstr;
454 int start;
Tim Peters6cd6a822001-08-17 22:11:27 +0000455 int dont_inherit = 0;
456 int supplied_flags = 0;
Tim Peters5ba58662001-07-16 02:29:45 +0000457 PyCompilerFlags cf;
Neal Norwitz3a9a3e72005-11-27 20:38:31 +0000458 PyObject *result = NULL, *cmd, *tmp = NULL;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000459 Py_ssize_t length;
Georg Brandl5240d742007-03-13 20:46:32 +0000460 static char *kwlist[] = {"source", "filename", "mode", "flags",
461 "dont_inherit", NULL};
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000462
Georg Brandl5240d742007-03-13 20:46:32 +0000463 if (!PyArg_ParseTupleAndKeywords(args, kwds, "Oss|ii:compile",
464 kwlist, &cmd, &filename, &startstr,
465 &supplied_flags, &dont_inherit))
Guido van Rossum5b722181993-03-30 17:46:03 +0000466 return NULL;
Tim Peters6cd6a822001-08-17 22:11:27 +0000467
Just van Rossum3aaf42c2003-02-10 08:21:10 +0000468 cf.cf_flags = supplied_flags;
469
470#ifdef Py_USING_UNICODE
471 if (PyUnicode_Check(cmd)) {
472 tmp = PyUnicode_AsUTF8String(cmd);
473 if (tmp == NULL)
474 return NULL;
475 cmd = tmp;
476 cf.cf_flags |= PyCF_SOURCE_IS_UTF8;
477 }
478#endif
Just van Rossumb9b8e9c2003-02-10 09:22:01 +0000479 if (PyObject_AsReadBuffer(cmd, (const void **)&str, &length))
480 return NULL;
Tim Peters2c646c92003-02-10 14:48:29 +0000481 if ((size_t)length != strlen(str)) {
Just van Rossum3aaf42c2003-02-10 08:21:10 +0000482 PyErr_SetString(PyExc_TypeError,
Alex Martellia9b9c9f2003-04-23 13:34:35 +0000483 "compile() expected string without null bytes");
Neal Norwitz3a9a3e72005-11-27 20:38:31 +0000484 goto cleanup;
Just van Rossum3aaf42c2003-02-10 08:21:10 +0000485 }
486
Guido van Rossum5b722181993-03-30 17:46:03 +0000487 if (strcmp(startstr, "exec") == 0)
Guido van Rossumb05a5c71997-05-07 17:46:13 +0000488 start = Py_file_input;
Guido van Rossum5b722181993-03-30 17:46:03 +0000489 else if (strcmp(startstr, "eval") == 0)
Guido van Rossumb05a5c71997-05-07 17:46:13 +0000490 start = Py_eval_input;
Guido van Rossum872537c1995-07-07 22:43:42 +0000491 else if (strcmp(startstr, "single") == 0)
Guido van Rossumb05a5c71997-05-07 17:46:13 +0000492 start = Py_single_input;
Guido van Rossum5b722181993-03-30 17:46:03 +0000493 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000494 PyErr_SetString(PyExc_ValueError,
Fred Drake661ea262000-10-24 19:57:45 +0000495 "compile() arg 3 must be 'exec' or 'eval' or 'single'");
Neal Norwitz3a9a3e72005-11-27 20:38:31 +0000496 goto cleanup;
Guido van Rossum5b722181993-03-30 17:46:03 +0000497 }
Tim Peters6cd6a822001-08-17 22:11:27 +0000498
Guido van Rossum4b499dd32003-02-13 22:07:59 +0000499 if (supplied_flags &
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000500 ~(PyCF_MASK | PyCF_MASK_OBSOLETE | PyCF_DONT_IMPLY_DEDENT | PyCF_ONLY_AST))
Guido van Rossum4b499dd32003-02-13 22:07:59 +0000501 {
Tim Peters6cd6a822001-08-17 22:11:27 +0000502 PyErr_SetString(PyExc_ValueError,
503 "compile(): unrecognised flags");
Neal Norwitz3a9a3e72005-11-27 20:38:31 +0000504 goto cleanup;
Tim Peters6cd6a822001-08-17 22:11:27 +0000505 }
506 /* XXX Warn if (supplied_flags & PyCF_MASK_OBSOLETE) != 0? */
507
Tim Peters6cd6a822001-08-17 22:11:27 +0000508 if (!dont_inherit) {
509 PyEval_MergeCompilerFlags(&cf);
510 }
Just van Rossum3aaf42c2003-02-10 08:21:10 +0000511 result = Py_CompileStringFlags(str, filename, start, &cf);
Neal Norwitz3a9a3e72005-11-27 20:38:31 +0000512cleanup:
Just van Rossum3aaf42c2003-02-10 08:21:10 +0000513 Py_XDECREF(tmp);
514 return result;
Guido van Rossum5b722181993-03-30 17:46:03 +0000515}
516
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000517PyDoc_STRVAR(compile_doc,
Tim Peters6cd6a822001-08-17 22:11:27 +0000518"compile(source, filename, mode[, flags[, dont_inherit]]) -> code object\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000519\n\
520Compile the source string (a Python module, statement or expression)\n\
521into a code object that can be executed by the exec statement or eval().\n\
522The filename will be used for run-time error messages.\n\
523The mode must be 'exec' to compile a module, 'single' to compile a\n\
Tim Peters6cd6a822001-08-17 22:11:27 +0000524single (interactive) statement, or 'eval' to compile an expression.\n\
525The flags argument, if present, controls which future statements influence\n\
526the compilation of the code.\n\
527The dont_inherit argument, if non-zero, stops the compilation inheriting\n\
528the effects of any future statements in effect in the code calling\n\
529compile; if absent or zero these statements do influence the compilation,\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000530in addition to any features explicitly specified.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000531
Guido van Rossum79f25d91997-04-29 20:08:16 +0000532static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000533builtin_dir(PyObject *self, PyObject *args)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000534{
Tim Peters5d2b77c2001-09-03 05:47:38 +0000535 PyObject *arg = NULL;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000536
Raymond Hettingerea3fdf42002-12-29 16:33:45 +0000537 if (!PyArg_UnpackTuple(args, "dir", 0, 1, &arg))
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000538 return NULL;
Tim Peters7eea37e2001-09-04 22:08:56 +0000539 return PyObject_Dir(arg);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000540}
541
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000542PyDoc_STRVAR(dir_doc,
Tim Peters5d2b77c2001-09-03 05:47:38 +0000543"dir([object]) -> list of strings\n"
544"\n"
Georg Brandl871f1bc2007-03-12 13:17:36 +0000545"If called without an argument, return the names in the current scope.\n"
546"Else, return an alphabetized list of names comprising (some of) the attributes\n"
547"of the given object, and of attributes reachable from it.\n"
548"If the object supplies a method named __dir__, it will be used; otherwise\n"
549"the default dir() logic is used and returns:\n"
550" for a module object: the module's attributes.\n"
551" for a class object: its attributes, and recursively the attributes\n"
552" of its bases.\n"
Georg Brandl3bb15672007-03-13 07:23:16 +0000553" for any other object: its attributes, its class's attributes, and\n"
Georg Brandl871f1bc2007-03-12 13:17:36 +0000554" recursively the attributes of its class's base classes.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000555
Guido van Rossum79f25d91997-04-29 20:08:16 +0000556static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000557builtin_divmod(PyObject *self, PyObject *args)
Guido van Rossum6a00cd81995-01-07 12:39:01 +0000558{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000559 PyObject *v, *w;
Guido van Rossum6a00cd81995-01-07 12:39:01 +0000560
Raymond Hettingerea3fdf42002-12-29 16:33:45 +0000561 if (!PyArg_UnpackTuple(args, "divmod", 2, 2, &v, &w))
Guido van Rossum6a00cd81995-01-07 12:39:01 +0000562 return NULL;
Guido van Rossum09df08a1998-05-22 00:51:39 +0000563 return PyNumber_Divmod(v, w);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000564}
565
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000566PyDoc_STRVAR(divmod_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000567"divmod(x, y) -> (div, mod)\n\
568\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000569Return the tuple ((x-x%y)/y, x%y). Invariant: div*y + mod == x.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000570
571
Guido van Rossum79f25d91997-04-29 20:08:16 +0000572static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000573builtin_eval(PyObject *self, PyObject *args)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000574{
Just van Rossum3aaf42c2003-02-10 08:21:10 +0000575 PyObject *cmd, *result, *tmp = NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000576 PyObject *globals = Py_None, *locals = Py_None;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000577 char *str;
Tim Peters9fa96be2001-08-17 23:04:59 +0000578 PyCompilerFlags cf;
Guido van Rossum590baa41993-11-30 13:40:46 +0000579
Raymond Hettinger214b1c32004-07-02 06:41:07 +0000580 if (!PyArg_UnpackTuple(args, "eval", 1, 3, &cmd, &globals, &locals))
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000581 return NULL;
Raymond Hettinger214b1c32004-07-02 06:41:07 +0000582 if (locals != Py_None && !PyMapping_Check(locals)) {
583 PyErr_SetString(PyExc_TypeError, "locals must be a mapping");
Raymond Hettinger513ffe82004-07-06 13:44:41 +0000584 return NULL;
Raymond Hettinger214b1c32004-07-02 06:41:07 +0000585 }
586 if (globals != Py_None && !PyDict_Check(globals)) {
Georg Brandl99d7e4e2005-08-31 22:21:15 +0000587 PyErr_SetString(PyExc_TypeError, PyMapping_Check(globals) ?
Raymond Hettinger214b1c32004-07-02 06:41:07 +0000588 "globals must be a real dict; try eval(expr, {}, mapping)"
589 : "globals must be a dict");
Raymond Hettinger513ffe82004-07-06 13:44:41 +0000590 return NULL;
Raymond Hettinger214b1c32004-07-02 06:41:07 +0000591 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000592 if (globals == Py_None) {
593 globals = PyEval_GetGlobals();
594 if (locals == Py_None)
595 locals = PyEval_GetLocals();
Guido van Rossum6135a871995-01-09 17:53:26 +0000596 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000597 else if (locals == Py_None)
Guido van Rossum6135a871995-01-09 17:53:26 +0000598 locals = globals;
Tim Peters9fa96be2001-08-17 23:04:59 +0000599
Georg Brandl77c85e62005-09-15 10:46:13 +0000600 if (globals == NULL || locals == NULL) {
601 PyErr_SetString(PyExc_TypeError,
602 "eval must be given globals and locals "
603 "when called without a frame");
604 return NULL;
605 }
606
Guido van Rossum79f25d91997-04-29 20:08:16 +0000607 if (PyDict_GetItemString(globals, "__builtins__") == NULL) {
608 if (PyDict_SetItemString(globals, "__builtins__",
609 PyEval_GetBuiltins()) != 0)
Guido van Rossum6135a871995-01-09 17:53:26 +0000610 return NULL;
611 }
Tim Peters9fa96be2001-08-17 23:04:59 +0000612
Jeremy Hylton15c1c4f2001-07-30 21:50:55 +0000613 if (PyCode_Check(cmd)) {
Jeremy Hylton733c8932001-12-13 19:51:56 +0000614 if (PyCode_GetNumFree((PyCodeObject *)cmd) > 0) {
Jeremy Hylton15c1c4f2001-07-30 21:50:55 +0000615 PyErr_SetString(PyExc_TypeError,
616 "code object passed to eval() may not contain free variables");
617 return NULL;
618 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000619 return PyEval_EvalCode((PyCodeObject *) cmd, globals, locals);
Jeremy Hylton15c1c4f2001-07-30 21:50:55 +0000620 }
Tim Peters9fa96be2001-08-17 23:04:59 +0000621
Marc-André Lemburgd1ba4432000-09-19 21:04:18 +0000622 if (!PyString_Check(cmd) &&
623 !PyUnicode_Check(cmd)) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000624 PyErr_SetString(PyExc_TypeError,
Fred Drake661ea262000-10-24 19:57:45 +0000625 "eval() arg 1 must be a string or code object");
Guido van Rossum94390a41992-08-14 15:14:30 +0000626 return NULL;
627 }
Just van Rossum3aaf42c2003-02-10 08:21:10 +0000628 cf.cf_flags = 0;
629
630#ifdef Py_USING_UNICODE
631 if (PyUnicode_Check(cmd)) {
632 tmp = PyUnicode_AsUTF8String(cmd);
633 if (tmp == NULL)
634 return NULL;
635 cmd = tmp;
636 cf.cf_flags |= PyCF_SOURCE_IS_UTF8;
637 }
638#endif
Neal Norwitz3a9a3e72005-11-27 20:38:31 +0000639 if (PyString_AsStringAndSize(cmd, &str, NULL)) {
640 Py_XDECREF(tmp);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000641 return NULL;
Neal Norwitz3a9a3e72005-11-27 20:38:31 +0000642 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000643 while (*str == ' ' || *str == '\t')
644 str++;
Tim Peters9fa96be2001-08-17 23:04:59 +0000645
Tim Peters9fa96be2001-08-17 23:04:59 +0000646 (void)PyEval_MergeCompilerFlags(&cf);
Just van Rossum3aaf42c2003-02-10 08:21:10 +0000647 result = PyRun_StringFlags(str, Py_eval_input, globals, locals, &cf);
648 Py_XDECREF(tmp);
649 return result;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000650}
651
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000652PyDoc_STRVAR(eval_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000653"eval(source[, globals[, locals]]) -> value\n\
654\n\
655Evaluate the source in the context of globals and locals.\n\
656The source may be a string representing a Python expression\n\
657or a code object as returned by compile().\n\
Neal Norwitz477ca1c2006-09-05 02:25:41 +0000658The globals must be a dictionary and locals can be any mapping,\n\
Raymond Hettinger214b1c32004-07-02 06:41:07 +0000659defaulting to the current globals and locals.\n\
660If only globals is given, locals defaults to it.\n");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000661
662
Guido van Rossum79f25d91997-04-29 20:08:16 +0000663static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000664builtin_execfile(PyObject *self, PyObject *args)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000665{
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000666 char *filename;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000667 PyObject *globals = Py_None, *locals = Py_None;
668 PyObject *res;
Guido van Rossumb3a639e2001-09-05 13:37:47 +0000669 FILE* fp = NULL;
Tim Peters5ba58662001-07-16 02:29:45 +0000670 PyCompilerFlags cf;
Martin v. Löwis6b3a2c42001-08-08 05:30:36 +0000671 int exists;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000672
Neal Norwitzdf25efe2007-05-23 06:58:36 +0000673 if (Py_Py3kWarningFlag &&
674 PyErr_Warn(PyExc_DeprecationWarning,
675 "execfile() not supported in 3.x") < 0)
676 return NULL;
677
Raymond Hettinger66bd2332004-08-02 08:30:07 +0000678 if (!PyArg_ParseTuple(args, "s|O!O:execfile",
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000679 &filename,
Guido van Rossum79f25d91997-04-29 20:08:16 +0000680 &PyDict_Type, &globals,
Raymond Hettinger66bd2332004-08-02 08:30:07 +0000681 &locals))
Guido van Rossum0f61f8a1992-02-25 18:55:05 +0000682 return NULL;
Raymond Hettinger66bd2332004-08-02 08:30:07 +0000683 if (locals != Py_None && !PyMapping_Check(locals)) {
684 PyErr_SetString(PyExc_TypeError, "locals must be a mapping");
685 return NULL;
686 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000687 if (globals == Py_None) {
688 globals = PyEval_GetGlobals();
689 if (locals == Py_None)
690 locals = PyEval_GetLocals();
Guido van Rossum6135a871995-01-09 17:53:26 +0000691 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000692 else if (locals == Py_None)
Guido van Rossum6135a871995-01-09 17:53:26 +0000693 locals = globals;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000694 if (PyDict_GetItemString(globals, "__builtins__") == NULL) {
695 if (PyDict_SetItemString(globals, "__builtins__",
696 PyEval_GetBuiltins()) != 0)
Guido van Rossum6135a871995-01-09 17:53:26 +0000697 return NULL;
698 }
Martin v. Löwis6b3a2c42001-08-08 05:30:36 +0000699
700 exists = 0;
701 /* Test for existence or directory. */
Martin v. Löwis3484a182002-03-09 12:07:51 +0000702#if defined(PLAN9)
703 {
704 Dir *d;
705
706 if ((d = dirstat(filename))!=nil) {
707 if(d->mode & DMDIR)
708 werrstr("is a directory");
709 else
710 exists = 1;
711 free(d);
712 }
Martin v. Löwis6b3a2c42001-08-08 05:30:36 +0000713 }
Martin v. Löwis3484a182002-03-09 12:07:51 +0000714#elif defined(RISCOS)
Guido van Rossume2ae77b2001-10-24 20:42:55 +0000715 if (object_exists(filename)) {
716 if (isdir(filename))
717 errno = EISDIR;
718 else
719 exists = 1;
720 }
Martin v. Löwis3484a182002-03-09 12:07:51 +0000721#else /* standard Posix */
722 {
723 struct stat s;
724 if (stat(filename, &s) == 0) {
725 if (S_ISDIR(s.st_mode))
Andrew MacIntyreda4d6cb2004-03-29 11:53:38 +0000726# if defined(PYOS_OS2) && defined(PYCC_VACPP)
Martin v. Löwis3484a182002-03-09 12:07:51 +0000727 errno = EOS2ERR;
728# else
729 errno = EISDIR;
730# endif
731 else
732 exists = 1;
733 }
734 }
735#endif
Martin v. Löwis6b3a2c42001-08-08 05:30:36 +0000736
737 if (exists) {
738 Py_BEGIN_ALLOW_THREADS
Jack Jansen7b8c7542002-04-14 20:12:41 +0000739 fp = fopen(filename, "r" PY_STDIOTEXTMODE);
Martin v. Löwis6b3a2c42001-08-08 05:30:36 +0000740 Py_END_ALLOW_THREADS
741
742 if (fp == NULL) {
743 exists = 0;
744 }
745 }
746
747 if (!exists) {
Peter Schneider-Kamp4c013422002-08-27 16:58:00 +0000748 PyErr_SetFromErrnoWithFilename(PyExc_IOError, filename);
Guido van Rossum0f61f8a1992-02-25 18:55:05 +0000749 return NULL;
750 }
Tim Peters5ba58662001-07-16 02:29:45 +0000751 cf.cf_flags = 0;
752 if (PyEval_MergeCompilerFlags(&cf))
Tim Peters748b8bb2001-04-28 08:20:22 +0000753 res = PyRun_FileExFlags(fp, filename, Py_file_input, globals,
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000754 locals, 1, &cf);
Tim Peters5ba58662001-07-16 02:29:45 +0000755 else
Tim Peters748b8bb2001-04-28 08:20:22 +0000756 res = PyRun_FileEx(fp, filename, Py_file_input, globals,
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000757 locals, 1);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000758 return res;
Guido van Rossum0f61f8a1992-02-25 18:55:05 +0000759}
760
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000761PyDoc_STRVAR(execfile_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000762"execfile(filename[, globals[, locals]])\n\
763\n\
764Read and execute a Python script from a file.\n\
765The globals and locals are dictionaries, defaulting to the current\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000766globals and locals. If only globals is given, locals defaults to it.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000767
768
Guido van Rossum79f25d91997-04-29 20:08:16 +0000769static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000770builtin_getattr(PyObject *self, PyObject *args)
Guido van Rossum33894be1992-01-27 16:53:09 +0000771{
Guido van Rossum950ff291998-06-29 13:38:57 +0000772 PyObject *v, *result, *dflt = NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000773 PyObject *name;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000774
Raymond Hettingerea3fdf42002-12-29 16:33:45 +0000775 if (!PyArg_UnpackTuple(args, "getattr", 2, 3, &v, &name, &dflt))
Guido van Rossum33894be1992-01-27 16:53:09 +0000776 return NULL;
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000777#ifdef Py_USING_UNICODE
Jeremy Hylton0eb11152001-07-30 22:39:31 +0000778 if (PyUnicode_Check(name)) {
779 name = _PyUnicode_AsDefaultEncodedString(name, NULL);
780 if (name == NULL)
781 return NULL;
782 }
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000783#endif
Jeremy Hylton0eb11152001-07-30 22:39:31 +0000784
785 if (!PyString_Check(name)) {
786 PyErr_SetString(PyExc_TypeError,
Alex Martellia9b9c9f2003-04-23 13:34:35 +0000787 "getattr(): attribute name must be string");
Jeremy Hylton0eb11152001-07-30 22:39:31 +0000788 return NULL;
789 }
Guido van Rossum950ff291998-06-29 13:38:57 +0000790 result = PyObject_GetAttr(v, name);
Guido van Rossumd8923572001-10-16 21:31:32 +0000791 if (result == NULL && dflt != NULL &&
792 PyErr_ExceptionMatches(PyExc_AttributeError))
793 {
Guido van Rossum950ff291998-06-29 13:38:57 +0000794 PyErr_Clear();
795 Py_INCREF(dflt);
796 result = dflt;
797 }
798 return result;
Guido van Rossum9bfef441993-03-29 10:43:31 +0000799}
800
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000801PyDoc_STRVAR(getattr_doc,
Guido van Rossum950ff291998-06-29 13:38:57 +0000802"getattr(object, name[, default]) -> value\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000803\n\
Guido van Rossum950ff291998-06-29 13:38:57 +0000804Get a named attribute from an object; getattr(x, 'y') is equivalent to x.y.\n\
805When a default argument is given, it is returned when the attribute doesn't\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000806exist; without it, an exception is raised in that case.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000807
808
Guido van Rossum79f25d91997-04-29 20:08:16 +0000809static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000810builtin_globals(PyObject *self)
Guido van Rossum872537c1995-07-07 22:43:42 +0000811{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000812 PyObject *d;
Guido van Rossum872537c1995-07-07 22:43:42 +0000813
Guido van Rossum79f25d91997-04-29 20:08:16 +0000814 d = PyEval_GetGlobals();
Neal Norwitz43bd4db2006-08-12 01:46:42 +0000815 Py_XINCREF(d);
Guido van Rossum872537c1995-07-07 22:43:42 +0000816 return d;
817}
818
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000819PyDoc_STRVAR(globals_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000820"globals() -> dictionary\n\
821\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000822Return the dictionary containing the current scope's global variables.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000823
824
Guido van Rossum79f25d91997-04-29 20:08:16 +0000825static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000826builtin_hasattr(PyObject *self, PyObject *args)
Guido van Rossum9bfef441993-03-29 10:43:31 +0000827{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000828 PyObject *v;
829 PyObject *name;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000830
Raymond Hettingerea3fdf42002-12-29 16:33:45 +0000831 if (!PyArg_UnpackTuple(args, "hasattr", 2, 2, &v, &name))
Guido van Rossum9bfef441993-03-29 10:43:31 +0000832 return NULL;
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000833#ifdef Py_USING_UNICODE
Jeremy Hylton302b54a2001-07-30 22:45:19 +0000834 if (PyUnicode_Check(name)) {
835 name = _PyUnicode_AsDefaultEncodedString(name, NULL);
836 if (name == NULL)
837 return NULL;
838 }
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000839#endif
Jeremy Hylton302b54a2001-07-30 22:45:19 +0000840
841 if (!PyString_Check(name)) {
842 PyErr_SetString(PyExc_TypeError,
Alex Martellia9b9c9f2003-04-23 13:34:35 +0000843 "hasattr(): attribute name must be string");
Jeremy Hylton302b54a2001-07-30 22:45:19 +0000844 return NULL;
845 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000846 v = PyObject_GetAttr(v, name);
Guido van Rossum9bfef441993-03-29 10:43:31 +0000847 if (v == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000848 PyErr_Clear();
Guido van Rossum09df08a1998-05-22 00:51:39 +0000849 Py_INCREF(Py_False);
850 return Py_False;
Guido van Rossum9bfef441993-03-29 10:43:31 +0000851 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000852 Py_DECREF(v);
Guido van Rossum09df08a1998-05-22 00:51:39 +0000853 Py_INCREF(Py_True);
854 return Py_True;
Guido van Rossum33894be1992-01-27 16:53:09 +0000855}
856
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000857PyDoc_STRVAR(hasattr_doc,
Guido van Rossum77f6a652002-04-03 22:41:51 +0000858"hasattr(object, name) -> bool\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000859\n\
860Return whether the object has an attribute with the given name.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000861(This is done by calling getattr(object, name) and catching exceptions.)");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000862
863
Guido van Rossum79f25d91997-04-29 20:08:16 +0000864static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000865builtin_id(PyObject *self, PyObject *v)
Guido van Rossum5b722181993-03-30 17:46:03 +0000866{
Guido van Rossum106f2da2000-06-28 21:12:25 +0000867 return PyLong_FromVoidPtr(v);
Guido van Rossum5b722181993-03-30 17:46:03 +0000868}
869
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000870PyDoc_STRVAR(id_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000871"id(object) -> integer\n\
872\n\
873Return the identity of an object. This is guaranteed to be unique among\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000874simultaneously existing objects. (Hint: it's the object's memory address.)");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000875
876
Guido van Rossum79f25d91997-04-29 20:08:16 +0000877static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000878builtin_map(PyObject *self, PyObject *args)
Guido van Rossum12d12c51993-10-26 17:58:25 +0000879{
880 typedef struct {
Tim Peters4e9afdc2001-05-03 23:54:49 +0000881 PyObject *it; /* the iterator object */
882 int saw_StopIteration; /* bool: did the iterator end? */
Guido van Rossum12d12c51993-10-26 17:58:25 +0000883 } sequence;
884
Guido van Rossum79f25d91997-04-29 20:08:16 +0000885 PyObject *func, *result;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000886 sequence *seqs = NULL, *sqp;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000887 Py_ssize_t n, len;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000888 register int i, j;
889
Guido van Rossum79f25d91997-04-29 20:08:16 +0000890 n = PyTuple_Size(args);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000891 if (n < 2) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000892 PyErr_SetString(PyExc_TypeError,
893 "map() requires at least two args");
Guido van Rossum12d12c51993-10-26 17:58:25 +0000894 return NULL;
895 }
896
Guido van Rossum79f25d91997-04-29 20:08:16 +0000897 func = PyTuple_GetItem(args, 0);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000898 n--;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000899
Guido van Rossumfa4ac711998-07-10 17:37:30 +0000900 if (func == Py_None && n == 1) {
901 /* map(None, S) is the same as list(S). */
902 return PySequence_List(PyTuple_GetItem(args, 1));
903 }
904
Tim Peters4e9afdc2001-05-03 23:54:49 +0000905 /* Get space for sequence descriptors. Must NULL out the iterator
906 * pointers so that jumping to Fail_2 later doesn't see trash.
907 */
Guido van Rossum79f25d91997-04-29 20:08:16 +0000908 if ((seqs = PyMem_NEW(sequence, n)) == NULL) {
909 PyErr_NoMemory();
Tim Peters4e9afdc2001-05-03 23:54:49 +0000910 return NULL;
911 }
912 for (i = 0; i < n; ++i) {
913 seqs[i].it = (PyObject*)NULL;
914 seqs[i].saw_StopIteration = 0;
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000915 }
Guido van Rossum12d12c51993-10-26 17:58:25 +0000916
Tim Peters4e9afdc2001-05-03 23:54:49 +0000917 /* Do a first pass to obtain iterators for the arguments, and set len
918 * to the largest of their lengths.
Tim Peters748b8bb2001-04-28 08:20:22 +0000919 */
Tim Peters4e9afdc2001-05-03 23:54:49 +0000920 len = 0;
921 for (i = 0, sqp = seqs; i < n; ++i, ++sqp) {
922 PyObject *curseq;
Martin v. Löwisd96ee902006-02-16 14:37:16 +0000923 Py_ssize_t curlen;
Guido van Rossumad991772001-01-12 16:03:05 +0000924
Tim Peters4e9afdc2001-05-03 23:54:49 +0000925 /* Get iterator. */
926 curseq = PyTuple_GetItem(args, i+1);
927 sqp->it = PyObject_GetIter(curseq);
928 if (sqp->it == NULL) {
Guido van Rossum12d12c51993-10-26 17:58:25 +0000929 static char errmsg[] =
Tim Peters4e9afdc2001-05-03 23:54:49 +0000930 "argument %d to map() must support iteration";
Guido van Rossum15e33a41997-04-30 19:00:27 +0000931 char errbuf[sizeof(errmsg) + 25];
Jeremy Hylton518ab1c2001-11-28 20:42:20 +0000932 PyOS_snprintf(errbuf, sizeof(errbuf), errmsg, i+2);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000933 PyErr_SetString(PyExc_TypeError, errbuf);
Guido van Rossum12d12c51993-10-26 17:58:25 +0000934 goto Fail_2;
935 }
936
Tim Peters4e9afdc2001-05-03 23:54:49 +0000937 /* Update len. */
Raymond Hettinger4e2f7142007-12-06 00:56:53 +0000938 curlen = _PyObject_LengthHint(curseq, 8);
Raymond Hettinger77f3c872004-01-04 08:54:44 +0000939 if (curlen > len)
940 len = curlen;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000941 }
942
Tim Peters4e9afdc2001-05-03 23:54:49 +0000943 /* Get space for the result list. */
Guido van Rossum79f25d91997-04-29 20:08:16 +0000944 if ((result = (PyObject *) PyList_New(len)) == NULL)
Guido van Rossum12d12c51993-10-26 17:58:25 +0000945 goto Fail_2;
946
Tim Peters4e9afdc2001-05-03 23:54:49 +0000947 /* Iterate over the sequences until all have stopped. */
Guido van Rossum2d951851994-08-29 12:52:16 +0000948 for (i = 0; ; ++i) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000949 PyObject *alist, *item=NULL, *value;
Tim Peters4e9afdc2001-05-03 23:54:49 +0000950 int numactive = 0;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000951
Guido van Rossum79f25d91997-04-29 20:08:16 +0000952 if (func == Py_None && n == 1)
Guido van Rossum32120311995-07-10 13:52:21 +0000953 alist = NULL;
Tim Peters4e9afdc2001-05-03 23:54:49 +0000954 else if ((alist = PyTuple_New(n)) == NULL)
955 goto Fail_1;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000956
957 for (j = 0, sqp = seqs; j < n; ++j, ++sqp) {
Tim Peters4e9afdc2001-05-03 23:54:49 +0000958 if (sqp->saw_StopIteration) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000959 Py_INCREF(Py_None);
960 item = Py_None;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000961 }
Guido van Rossum12d12c51993-10-26 17:58:25 +0000962 else {
Tim Peters4e9afdc2001-05-03 23:54:49 +0000963 item = PyIter_Next(sqp->it);
964 if (item)
965 ++numactive;
966 else {
Tim Peters4e9afdc2001-05-03 23:54:49 +0000967 if (PyErr_Occurred()) {
Tim Petersf4848da2001-05-05 00:14:56 +0000968 Py_XDECREF(alist);
969 goto Fail_1;
Guido van Rossum2d951851994-08-29 12:52:16 +0000970 }
Tim Peters4e9afdc2001-05-03 23:54:49 +0000971 Py_INCREF(Py_None);
972 item = Py_None;
973 sqp->saw_StopIteration = 1;
Guido van Rossum2d951851994-08-29 12:52:16 +0000974 }
Guido van Rossum12d12c51993-10-26 17:58:25 +0000975 }
Tim Peters4e9afdc2001-05-03 23:54:49 +0000976 if (alist)
977 PyTuple_SET_ITEM(alist, j, item);
978 else
Guido van Rossum2d951851994-08-29 12:52:16 +0000979 break;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000980 }
981
Guido van Rossum32120311995-07-10 13:52:21 +0000982 if (!alist)
983 alist = item;
Guido van Rossum2d951851994-08-29 12:52:16 +0000984
Tim Peters4e9afdc2001-05-03 23:54:49 +0000985 if (numactive == 0) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000986 Py_DECREF(alist);
Guido van Rossum2d951851994-08-29 12:52:16 +0000987 break;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000988 }
Guido van Rossum2d951851994-08-29 12:52:16 +0000989
Guido van Rossum79f25d91997-04-29 20:08:16 +0000990 if (func == Py_None)
Guido van Rossum32120311995-07-10 13:52:21 +0000991 value = alist;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000992 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000993 value = PyEval_CallObject(func, alist);
994 Py_DECREF(alist);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000995 if (value == NULL)
996 goto Fail_1;
Guido van Rossum2d951851994-08-29 12:52:16 +0000997 }
998 if (i >= len) {
Barry Warsawfa77e091999-01-28 18:49:12 +0000999 int status = PyList_Append(result, value);
Barry Warsaw21332871999-01-28 04:21:35 +00001000 Py_DECREF(value);
Barry Warsawfa77e091999-01-28 18:49:12 +00001001 if (status < 0)
1002 goto Fail_1;
Guido van Rossum2d951851994-08-29 12:52:16 +00001003 }
Tim Peters4e9afdc2001-05-03 23:54:49 +00001004 else if (PyList_SetItem(result, i, value) < 0)
1005 goto Fail_1;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001006 }
1007
Guido van Rossumfa4ac711998-07-10 17:37:30 +00001008 if (i < len && PyList_SetSlice(result, i, len, NULL) < 0)
1009 goto Fail_1;
1010
Tim Peters4e9afdc2001-05-03 23:54:49 +00001011 goto Succeed;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001012
Guido van Rossum12d12c51993-10-26 17:58:25 +00001013Fail_1:
Guido van Rossum79f25d91997-04-29 20:08:16 +00001014 Py_DECREF(result);
Guido van Rossum12d12c51993-10-26 17:58:25 +00001015Fail_2:
Tim Peters4e9afdc2001-05-03 23:54:49 +00001016 result = NULL;
1017Succeed:
1018 assert(seqs);
1019 for (i = 0; i < n; ++i)
1020 Py_XDECREF(seqs[i].it);
1021 PyMem_DEL(seqs);
1022 return result;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001023}
1024
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001025PyDoc_STRVAR(map_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001026"map(function, sequence[, sequence, ...]) -> list\n\
1027\n\
1028Return a list of the results of applying the function to the items of\n\
1029the argument sequence(s). If more than one sequence is given, the\n\
1030function is called with an argument list consisting of the corresponding\n\
1031item of each sequence, substituting None for missing values when not all\n\
1032sequences have the same length. If the function is None, return a list of\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001033the items of the sequence (or a list of tuples if more than one sequence).");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001034
1035
Guido van Rossum79f25d91997-04-29 20:08:16 +00001036static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001037builtin_setattr(PyObject *self, PyObject *args)
Guido van Rossum33894be1992-01-27 16:53:09 +00001038{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001039 PyObject *v;
1040 PyObject *name;
1041 PyObject *value;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001042
Raymond Hettingerea3fdf42002-12-29 16:33:45 +00001043 if (!PyArg_UnpackTuple(args, "setattr", 3, 3, &v, &name, &value))
Guido van Rossum33894be1992-01-27 16:53:09 +00001044 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001045 if (PyObject_SetAttr(v, name, value) != 0)
Guido van Rossum33894be1992-01-27 16:53:09 +00001046 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001047 Py_INCREF(Py_None);
1048 return Py_None;
Guido van Rossum33894be1992-01-27 16:53:09 +00001049}
1050
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001051PyDoc_STRVAR(setattr_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001052"setattr(object, name, value)\n\
1053\n\
1054Set a named attribute on an object; setattr(x, 'y', v) is equivalent to\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001055``x.y = v''.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001056
1057
Guido van Rossum79f25d91997-04-29 20:08:16 +00001058static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001059builtin_delattr(PyObject *self, PyObject *args)
Guido van Rossum14144fc1994-08-29 12:53:40 +00001060{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001061 PyObject *v;
1062 PyObject *name;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001063
Raymond Hettingerea3fdf42002-12-29 16:33:45 +00001064 if (!PyArg_UnpackTuple(args, "delattr", 2, 2, &v, &name))
Guido van Rossum14144fc1994-08-29 12:53:40 +00001065 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001066 if (PyObject_SetAttr(v, name, (PyObject *)NULL) != 0)
Guido van Rossum14144fc1994-08-29 12:53:40 +00001067 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001068 Py_INCREF(Py_None);
1069 return Py_None;
Guido van Rossum14144fc1994-08-29 12:53:40 +00001070}
1071
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001072PyDoc_STRVAR(delattr_doc,
Guido van Rossumdf12a591998-11-23 22:13:04 +00001073"delattr(object, name)\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001074\n\
1075Delete a named attribute on an object; delattr(x, 'y') is equivalent to\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001076``del x.y''.");
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_hash(PyObject *self, PyObject *v)
Guido van Rossum9bfef441993-03-29 10:43:31 +00001081{
Guido van Rossum9bfef441993-03-29 10:43:31 +00001082 long x;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001083
Guido van Rossum79f25d91997-04-29 20:08:16 +00001084 x = PyObject_Hash(v);
Guido van Rossum9bfef441993-03-29 10:43:31 +00001085 if (x == -1)
1086 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001087 return PyInt_FromLong(x);
Guido van Rossum9bfef441993-03-29 10:43:31 +00001088}
1089
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001090PyDoc_STRVAR(hash_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001091"hash(object) -> integer\n\
1092\n\
1093Return a hash value for the object. Two objects with the same value have\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001094the same hash value. The reverse is not necessarily true, but likely.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001095
1096
Guido van Rossum79f25d91997-04-29 20:08:16 +00001097static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001098builtin_hex(PyObject *self, PyObject *v)
Guido van Rossum006bcd41991-10-24 14:54:44 +00001099{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001100 PyNumberMethods *nb;
Neil Schemenauer3a313e32004-07-19 16:29:17 +00001101 PyObject *res;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001102
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001103 if ((nb = v->ob_type->tp_as_number) == NULL ||
Guido van Rossum1899c2e1992-09-12 11:09:23 +00001104 nb->nb_hex == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001105 PyErr_SetString(PyExc_TypeError,
Guido van Rossum1899c2e1992-09-12 11:09:23 +00001106 "hex() argument can't be converted to hex");
1107 return NULL;
Guido van Rossum006bcd41991-10-24 14:54:44 +00001108 }
Neil Schemenauer3a313e32004-07-19 16:29:17 +00001109 res = (*nb->nb_hex)(v);
1110 if (res && !PyString_Check(res)) {
1111 PyErr_Format(PyExc_TypeError,
1112 "__hex__ returned non-string (type %.200s)",
1113 res->ob_type->tp_name);
1114 Py_DECREF(res);
1115 return NULL;
1116 }
1117 return res;
Guido van Rossum006bcd41991-10-24 14:54:44 +00001118}
1119
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001120PyDoc_STRVAR(hex_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001121"hex(number) -> string\n\
1122\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001123Return the hexadecimal representation of an integer or long integer.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001124
1125
Tim Petersdbd9ba62000-07-09 03:09:57 +00001126static PyObject *builtin_raw_input(PyObject *, PyObject *);
Guido van Rossum3165fe61992-09-25 21:59:05 +00001127
Guido van Rossum79f25d91997-04-29 20:08:16 +00001128static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001129builtin_input(PyObject *self, PyObject *args)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001130{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001131 PyObject *line;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001132 char *str;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001133 PyObject *res;
1134 PyObject *globals, *locals;
Hye-Shik Changff83c2b2004-02-02 13:39:01 +00001135 PyCompilerFlags cf;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001136
1137 line = builtin_raw_input(self, args);
Guido van Rossum3165fe61992-09-25 21:59:05 +00001138 if (line == NULL)
1139 return line;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001140 if (!PyArg_Parse(line, "s;embedded '\\0' in input line", &str))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001141 return NULL;
1142 while (*str == ' ' || *str == '\t')
1143 str++;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001144 globals = PyEval_GetGlobals();
1145 locals = PyEval_GetLocals();
1146 if (PyDict_GetItemString(globals, "__builtins__") == NULL) {
1147 if (PyDict_SetItemString(globals, "__builtins__",
1148 PyEval_GetBuiltins()) != 0)
Guido van Rossum6135a871995-01-09 17:53:26 +00001149 return NULL;
1150 }
Hye-Shik Changff83c2b2004-02-02 13:39:01 +00001151 cf.cf_flags = 0;
1152 PyEval_MergeCompilerFlags(&cf);
1153 res = PyRun_StringFlags(str, Py_eval_input, globals, locals, &cf);
Guido van Rossum79f25d91997-04-29 20:08:16 +00001154 Py_DECREF(line);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001155 return res;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001156}
1157
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001158PyDoc_STRVAR(input_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001159"input([prompt]) -> value\n\
1160\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001161Equivalent to eval(raw_input(prompt)).");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001162
1163
Guido van Rossume8811f81997-02-14 15:48:05 +00001164static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001165builtin_intern(PyObject *self, PyObject *args)
Guido van Rossume8811f81997-02-14 15:48:05 +00001166{
1167 PyObject *s;
Guido van Rossum43713e52000-02-29 13:59:29 +00001168 if (!PyArg_ParseTuple(args, "S:intern", &s))
Guido van Rossume8811f81997-02-14 15:48:05 +00001169 return NULL;
Jeremy Hylton4c989dd2004-08-07 19:20:05 +00001170 if (!PyString_CheckExact(s)) {
1171 PyErr_SetString(PyExc_TypeError,
1172 "can't intern subclass of string");
1173 return NULL;
1174 }
Guido van Rossume8811f81997-02-14 15:48:05 +00001175 Py_INCREF(s);
1176 PyString_InternInPlace(&s);
1177 return s;
1178}
1179
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001180PyDoc_STRVAR(intern_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001181"intern(string) -> string\n\
1182\n\
1183``Intern'' the given string. This enters the string in the (global)\n\
1184table of interned strings whose purpose is to speed up dictionary lookups.\n\
1185Return the string itself or the previously interned string object with the\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001186same value.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001187
1188
Guido van Rossum79f25d91997-04-29 20:08:16 +00001189static PyObject *
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001190builtin_iter(PyObject *self, PyObject *args)
1191{
1192 PyObject *v, *w = NULL;
1193
Raymond Hettingerea3fdf42002-12-29 16:33:45 +00001194 if (!PyArg_UnpackTuple(args, "iter", 1, 2, &v, &w))
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001195 return NULL;
1196 if (w == NULL)
1197 return PyObject_GetIter(v);
1198 if (!PyCallable_Check(v)) {
1199 PyErr_SetString(PyExc_TypeError,
1200 "iter(v, w): v must be callable");
1201 return NULL;
1202 }
1203 return PyCallIter_New(v, w);
1204}
1205
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001206PyDoc_STRVAR(iter_doc,
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001207"iter(collection) -> iterator\n\
1208iter(callable, sentinel) -> iterator\n\
1209\n\
1210Get an iterator from an object. In the first form, the argument must\n\
1211supply its own iterator, or be a sequence.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001212In the second form, the callable is called until it returns the sentinel.");
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001213
1214
1215static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001216builtin_len(PyObject *self, PyObject *v)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001217{
Martin v. Löwis18e16552006-02-15 17:27:45 +00001218 Py_ssize_t res;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001219
Jeremy Hylton03657cf2000-07-12 13:05:33 +00001220 res = PyObject_Size(v);
Guido van Rossum8ea9f4d1998-06-29 22:26:50 +00001221 if (res < 0 && PyErr_Occurred())
1222 return NULL;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001223 return PyInt_FromSsize_t(res);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001224}
1225
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001226PyDoc_STRVAR(len_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001227"len(object) -> integer\n\
1228\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001229Return the number of items of a sequence or mapping.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001230
1231
Guido van Rossum79f25d91997-04-29 20:08:16 +00001232static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001233builtin_locals(PyObject *self)
Guido van Rossum872537c1995-07-07 22:43:42 +00001234{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001235 PyObject *d;
Guido van Rossum872537c1995-07-07 22:43:42 +00001236
Guido van Rossum79f25d91997-04-29 20:08:16 +00001237 d = PyEval_GetLocals();
Neal Norwitz43bd4db2006-08-12 01:46:42 +00001238 Py_XINCREF(d);
Guido van Rossum872537c1995-07-07 22:43:42 +00001239 return d;
1240}
1241
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001242PyDoc_STRVAR(locals_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001243"locals() -> dictionary\n\
1244\n\
Raymond Hettinger69bf8f32003-01-04 02:16:22 +00001245Update and return a dictionary containing the current scope's local variables.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001246
1247
Guido van Rossum79f25d91997-04-29 20:08:16 +00001248static PyObject *
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001249min_max(PyObject *args, PyObject *kwds, int op)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001250{
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001251 PyObject *v, *it, *item, *val, *maxitem, *maxval, *keyfunc=NULL;
Martin v. Löwisfd39ad42004-08-12 14:42:37 +00001252 const char *name = op == Py_LT ? "min" : "max";
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001253
Guido van Rossum79f25d91997-04-29 20:08:16 +00001254 if (PyTuple_Size(args) > 1)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001255 v = args;
Martin v. Löwisfd39ad42004-08-12 14:42:37 +00001256 else if (!PyArg_UnpackTuple(args, (char *)name, 1, 1, &v))
Guido van Rossum3f5da241990-12-20 15:06:42 +00001257 return NULL;
Tim Peters67d687a2002-04-29 21:27:32 +00001258
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001259 if (kwds != NULL && PyDict_Check(kwds) && PyDict_Size(kwds)) {
1260 keyfunc = PyDict_GetItemString(kwds, "key");
1261 if (PyDict_Size(kwds)!=1 || keyfunc == NULL) {
Georg Brandl99d7e4e2005-08-31 22:21:15 +00001262 PyErr_Format(PyExc_TypeError,
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001263 "%s() got an unexpected keyword argument", name);
1264 return NULL;
1265 }
Guido van Rossum1d9a9ea2008-01-23 20:19:01 +00001266 Py_INCREF(keyfunc);
Georg Brandl99d7e4e2005-08-31 22:21:15 +00001267 }
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001268
Tim Petersc3074532001-05-03 07:00:32 +00001269 it = PyObject_GetIter(v);
Guido van Rossum1d9a9ea2008-01-23 20:19:01 +00001270 if (it == NULL) {
1271 Py_XDECREF(keyfunc);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001272 return NULL;
Guido van Rossum1d9a9ea2008-01-23 20:19:01 +00001273 }
Tim Petersc3074532001-05-03 07:00:32 +00001274
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001275 maxitem = NULL; /* the result */
1276 maxval = NULL; /* the value associated with the result */
Brett Cannon9e635cf2004-12-07 00:25:35 +00001277 while (( item = PyIter_Next(it) )) {
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001278 /* get the value from the key function */
1279 if (keyfunc != NULL) {
1280 val = PyObject_CallFunctionObjArgs(keyfunc, item, NULL);
1281 if (val == NULL)
1282 goto Fail_it_item;
1283 }
1284 /* no key function; the value is the item */
1285 else {
1286 val = item;
1287 Py_INCREF(val);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001288 }
Tim Petersc3074532001-05-03 07:00:32 +00001289
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001290 /* maximum value and item are unset; set them */
1291 if (maxval == NULL) {
1292 maxitem = item;
1293 maxval = val;
1294 }
1295 /* maximum value and item are set; update them as necessary */
Guido van Rossum2d951851994-08-29 12:52:16 +00001296 else {
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001297 int cmp = PyObject_RichCompareBool(val, maxval, op);
1298 if (cmp < 0)
1299 goto Fail_it_item_and_val;
1300 else if (cmp > 0) {
1301 Py_DECREF(maxval);
1302 Py_DECREF(maxitem);
1303 maxval = val;
1304 maxitem = item;
Guido van Rossum53451b32001-01-17 15:47:24 +00001305 }
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001306 else {
1307 Py_DECREF(item);
1308 Py_DECREF(val);
Guido van Rossumc8b6df91997-05-23 00:06:51 +00001309 }
Guido van Rossum2d951851994-08-29 12:52:16 +00001310 }
Guido van Rossum3f5da241990-12-20 15:06:42 +00001311 }
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001312 if (PyErr_Occurred())
1313 goto Fail_it;
1314 if (maxval == NULL) {
Martin v. Löwisfd39ad42004-08-12 14:42:37 +00001315 PyErr_Format(PyExc_ValueError,
1316 "%s() arg is an empty sequence", name);
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001317 assert(maxitem == NULL);
1318 }
1319 else
1320 Py_DECREF(maxval);
Tim Petersc3074532001-05-03 07:00:32 +00001321 Py_DECREF(it);
Guido van Rossum1d9a9ea2008-01-23 20:19:01 +00001322 Py_XDECREF(keyfunc);
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001323 return maxitem;
1324
1325Fail_it_item_and_val:
1326 Py_DECREF(val);
1327Fail_it_item:
1328 Py_DECREF(item);
1329Fail_it:
1330 Py_XDECREF(maxval);
1331 Py_XDECREF(maxitem);
1332 Py_DECREF(it);
Guido van Rossum1d9a9ea2008-01-23 20:19:01 +00001333 Py_XDECREF(keyfunc);
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001334 return NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001335}
1336
Guido van Rossum79f25d91997-04-29 20:08:16 +00001337static PyObject *
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001338builtin_min(PyObject *self, PyObject *args, PyObject *kwds)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001339{
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001340 return min_max(args, kwds, Py_LT);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001341}
1342
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001343PyDoc_STRVAR(min_doc,
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001344"min(iterable[, key=func]) -> value\n\
1345min(a, b, c, ...[, key=func]) -> value\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001346\n\
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001347With a single iterable argument, return its smallest item.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001348With two or more arguments, return the smallest argument.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001349
1350
Guido van Rossum79f25d91997-04-29 20:08:16 +00001351static PyObject *
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001352builtin_max(PyObject *self, PyObject *args, PyObject *kwds)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001353{
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001354 return min_max(args, kwds, Py_GT);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001355}
1356
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001357PyDoc_STRVAR(max_doc,
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001358"max(iterable[, key=func]) -> value\n\
1359max(a, b, c, ...[, key=func]) -> value\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001360\n\
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001361With a single iterable argument, return its largest item.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001362With two or more arguments, return the largest argument.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001363
1364
Guido van Rossum79f25d91997-04-29 20:08:16 +00001365static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001366builtin_oct(PyObject *self, PyObject *v)
Guido van Rossum006bcd41991-10-24 14:54:44 +00001367{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001368 PyNumberMethods *nb;
Neil Schemenauer3a313e32004-07-19 16:29:17 +00001369 PyObject *res;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001370
Guido van Rossum1899c2e1992-09-12 11:09:23 +00001371 if (v == NULL || (nb = v->ob_type->tp_as_number) == NULL ||
1372 nb->nb_oct == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001373 PyErr_SetString(PyExc_TypeError,
Guido van Rossum1899c2e1992-09-12 11:09:23 +00001374 "oct() argument can't be converted to oct");
1375 return NULL;
Guido van Rossum006bcd41991-10-24 14:54:44 +00001376 }
Neil Schemenauer3a313e32004-07-19 16:29:17 +00001377 res = (*nb->nb_oct)(v);
1378 if (res && !PyString_Check(res)) {
1379 PyErr_Format(PyExc_TypeError,
1380 "__oct__ returned non-string (type %.200s)",
1381 res->ob_type->tp_name);
1382 Py_DECREF(res);
1383 return NULL;
1384 }
1385 return res;
Guido van Rossum006bcd41991-10-24 14:54:44 +00001386}
1387
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001388PyDoc_STRVAR(oct_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001389"oct(number) -> string\n\
1390\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001391Return the octal representation of an integer or long integer.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001392
1393
Guido van Rossum79f25d91997-04-29 20:08:16 +00001394static PyObject *
Neal Norwitzc4edb0e2006-05-02 04:43:14 +00001395builtin_open(PyObject *self, PyObject *args, PyObject *kwds)
1396{
1397 return PyObject_Call((PyObject*)&PyFile_Type, args, kwds);
1398}
1399
1400PyDoc_STRVAR(open_doc,
1401"open(name[, mode[, buffering]]) -> file object\n\
1402\n\
Skip Montanaro4e3ebe02007-12-08 14:37:43 +00001403Open a file using the file() type, returns a file object. This is the\n\
1404preferred way to open a file.");
Neal Norwitzc4edb0e2006-05-02 04:43:14 +00001405
1406
1407static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001408builtin_ord(PyObject *self, PyObject* obj)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001409{
Guido van Rossum09095f32000-03-10 23:00:52 +00001410 long ord;
Martin v. Löwisd96ee902006-02-16 14:37:16 +00001411 Py_ssize_t size;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001412
Jeremy Hylton394b54d2000-04-12 21:19:47 +00001413 if (PyString_Check(obj)) {
1414 size = PyString_GET_SIZE(obj);
Andrew M. Kuchling34c20cf2000-12-20 14:36:56 +00001415 if (size == 1) {
Jeremy Hylton394b54d2000-04-12 21:19:47 +00001416 ord = (long)((unsigned char)*PyString_AS_STRING(obj));
Andrew M. Kuchling34c20cf2000-12-20 14:36:56 +00001417 return PyInt_FromLong(ord);
1418 }
Martin v. Löwis339d0f72001-08-17 18:39:25 +00001419#ifdef Py_USING_UNICODE
Jeremy Hylton394b54d2000-04-12 21:19:47 +00001420 } else if (PyUnicode_Check(obj)) {
1421 size = PyUnicode_GET_SIZE(obj);
Andrew M. Kuchling34c20cf2000-12-20 14:36:56 +00001422 if (size == 1) {
Jeremy Hylton394b54d2000-04-12 21:19:47 +00001423 ord = (long)*PyUnicode_AS_UNICODE(obj);
Andrew M. Kuchling34c20cf2000-12-20 14:36:56 +00001424 return PyInt_FromLong(ord);
1425 }
Martin v. Löwis339d0f72001-08-17 18:39:25 +00001426#endif
Jeremy Hylton394b54d2000-04-12 21:19:47 +00001427 } else {
1428 PyErr_Format(PyExc_TypeError,
Andrew M. Kuchlingf07aad12000-12-23 14:11:28 +00001429 "ord() expected string of length 1, but " \
Jeremy Hylton394b54d2000-04-12 21:19:47 +00001430 "%.200s found", obj->ob_type->tp_name);
Guido van Rossum09095f32000-03-10 23:00:52 +00001431 return NULL;
1432 }
1433
Guido van Rossumad991772001-01-12 16:03:05 +00001434 PyErr_Format(PyExc_TypeError,
Andrew M. Kuchlingf07aad12000-12-23 14:11:28 +00001435 "ord() expected a character, "
Martin v. Löwisd96ee902006-02-16 14:37:16 +00001436 "but string of length %zd found",
Jeremy Hylton394b54d2000-04-12 21:19:47 +00001437 size);
1438 return NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001439}
1440
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001441PyDoc_STRVAR(ord_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001442"ord(c) -> integer\n\
1443\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001444Return the integer ordinal of a one-character string.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001445
1446
Guido van Rossum79f25d91997-04-29 20:08:16 +00001447static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001448builtin_pow(PyObject *self, PyObject *args)
Guido van Rossum6a00cd81995-01-07 12:39:01 +00001449{
Guido van Rossum09df08a1998-05-22 00:51:39 +00001450 PyObject *v, *w, *z = Py_None;
Guido van Rossum6a00cd81995-01-07 12:39:01 +00001451
Raymond Hettingerea3fdf42002-12-29 16:33:45 +00001452 if (!PyArg_UnpackTuple(args, "pow", 2, 3, &v, &w, &z))
Guido van Rossum6a00cd81995-01-07 12:39:01 +00001453 return NULL;
Guido van Rossum09df08a1998-05-22 00:51:39 +00001454 return PyNumber_Power(v, w, z);
Guido van Rossumd4905451991-05-05 20:00:36 +00001455}
1456
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001457PyDoc_STRVAR(pow_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001458"pow(x, y[, z]) -> number\n\
1459\n\
1460With two arguments, equivalent to x**y. With three arguments,\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001461equivalent to (x**y) % z, but may be more efficient (e.g. for longs).");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001462
1463
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001464
1465/* Return number of items in range (lo, hi, step), when arguments are
1466 * PyInt or PyLong objects. step > 0 required. Return a value < 0 if
1467 * & only if the true value is too large to fit in a signed long.
1468 * Arguments MUST return 1 with either PyInt_Check() or
1469 * PyLong_Check(). Return -1 when there is an error.
1470 */
1471static long
1472get_len_of_range_longs(PyObject *lo, PyObject *hi, PyObject *step)
1473{
1474 /* -------------------------------------------------------------
1475 Algorithm is equal to that of get_len_of_range(), but it operates
1476 on PyObjects (which are assumed to be PyLong or PyInt objects).
1477 ---------------------------------------------------------------*/
1478 long n;
1479 PyObject *diff = NULL;
1480 PyObject *one = NULL;
1481 PyObject *tmp1 = NULL, *tmp2 = NULL, *tmp3 = NULL;
1482 /* holds sub-expression evaluations */
1483
1484 /* if (lo >= hi), return length of 0. */
1485 if (PyObject_Compare(lo, hi) >= 0)
1486 return 0;
1487
1488 if ((one = PyLong_FromLong(1L)) == NULL)
1489 goto Fail;
1490
1491 if ((tmp1 = PyNumber_Subtract(hi, lo)) == NULL)
1492 goto Fail;
1493
1494 if ((diff = PyNumber_Subtract(tmp1, one)) == NULL)
1495 goto Fail;
1496
1497 if ((tmp2 = PyNumber_FloorDivide(diff, step)) == NULL)
1498 goto Fail;
1499
1500 if ((tmp3 = PyNumber_Add(tmp2, one)) == NULL)
1501 goto Fail;
1502
1503 n = PyLong_AsLong(tmp3);
1504 if (PyErr_Occurred()) { /* Check for Overflow */
1505 PyErr_Clear();
1506 goto Fail;
1507 }
1508
1509 Py_DECREF(tmp3);
1510 Py_DECREF(tmp2);
1511 Py_DECREF(diff);
1512 Py_DECREF(tmp1);
1513 Py_DECREF(one);
1514 return n;
1515
1516 Fail:
1517 Py_XDECREF(tmp3);
1518 Py_XDECREF(tmp2);
1519 Py_XDECREF(diff);
1520 Py_XDECREF(tmp1);
1521 Py_XDECREF(one);
1522 return -1;
1523}
1524
1525/* An extension of builtin_range() that handles the case when PyLong
1526 * arguments are given. */
1527static PyObject *
1528handle_range_longs(PyObject *self, PyObject *args)
1529{
1530 PyObject *ilow;
Tim Peters874e1f72003-04-13 22:13:08 +00001531 PyObject *ihigh = NULL;
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001532 PyObject *istep = NULL;
Tim Peters874e1f72003-04-13 22:13:08 +00001533
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001534 PyObject *curnum = NULL;
1535 PyObject *v = NULL;
1536 long bign;
1537 int i, n;
1538 int cmp_result;
1539
Tim Peters874e1f72003-04-13 22:13:08 +00001540 PyObject *zero = PyLong_FromLong(0);
1541
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001542 if (zero == NULL)
1543 return NULL;
1544
Tim Peters874e1f72003-04-13 22:13:08 +00001545 if (!PyArg_UnpackTuple(args, "range", 1, 3, &ilow, &ihigh, &istep)) {
1546 Py_DECREF(zero);
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001547 return NULL;
1548 }
1549
Tim Peters874e1f72003-04-13 22:13:08 +00001550 /* Figure out which way we were called, supply defaults, and be
1551 * sure to incref everything so that the decrefs at the end
1552 * are correct.
1553 */
1554 assert(ilow != NULL);
1555 if (ihigh == NULL) {
1556 /* only 1 arg -- it's the upper limit */
1557 ihigh = ilow;
1558 ilow = NULL;
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001559 }
Tim Peters874e1f72003-04-13 22:13:08 +00001560 assert(ihigh != NULL);
1561 Py_INCREF(ihigh);
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001562
Tim Peters874e1f72003-04-13 22:13:08 +00001563 /* ihigh correct now; do ilow */
1564 if (ilow == NULL)
1565 ilow = zero;
1566 Py_INCREF(ilow);
1567
1568 /* ilow and ihigh correct now; do istep */
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001569 if (istep == NULL) {
1570 istep = PyLong_FromLong(1L);
1571 if (istep == NULL)
1572 goto Fail;
1573 }
1574 else {
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001575 Py_INCREF(istep);
1576 }
1577
Tim Peters874e1f72003-04-13 22:13:08 +00001578 if (!PyInt_Check(ilow) && !PyLong_Check(ilow)) {
Guido van Rossum28e83e32003-04-15 12:43:26 +00001579 PyErr_Format(PyExc_TypeError,
Alex Martellif471d472003-04-23 13:00:44 +00001580 "range() integer start argument expected, got %s.",
Guido van Rossum817d6c92003-04-14 18:25:04 +00001581 ilow->ob_type->tp_name);
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001582 goto Fail;
1583 }
1584
Tim Peters874e1f72003-04-13 22:13:08 +00001585 if (!PyInt_Check(ihigh) && !PyLong_Check(ihigh)) {
Guido van Rossum28e83e32003-04-15 12:43:26 +00001586 PyErr_Format(PyExc_TypeError,
Alex Martellif471d472003-04-23 13:00:44 +00001587 "range() integer end argument expected, got %s.",
Guido van Rossum817d6c92003-04-14 18:25:04 +00001588 ihigh->ob_type->tp_name);
Tim Peters874e1f72003-04-13 22:13:08 +00001589 goto Fail;
1590 }
1591
1592 if (!PyInt_Check(istep) && !PyLong_Check(istep)) {
Guido van Rossum28e83e32003-04-15 12:43:26 +00001593 PyErr_Format(PyExc_TypeError,
Alex Martellif471d472003-04-23 13:00:44 +00001594 "range() integer step argument expected, got %s.",
Guido van Rossum817d6c92003-04-14 18:25:04 +00001595 istep->ob_type->tp_name);
Tim Peters874e1f72003-04-13 22:13:08 +00001596 goto Fail;
1597 }
1598
1599 if (PyObject_Cmp(istep, zero, &cmp_result) == -1)
1600 goto Fail;
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001601 if (cmp_result == 0) {
1602 PyErr_SetString(PyExc_ValueError,
Alex Martellif471d472003-04-23 13:00:44 +00001603 "range() step argument must not be zero");
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001604 goto Fail;
1605 }
1606
1607 if (cmp_result > 0)
1608 bign = get_len_of_range_longs(ilow, ihigh, istep);
1609 else {
1610 PyObject *neg_istep = PyNumber_Negative(istep);
1611 if (neg_istep == NULL)
1612 goto Fail;
1613 bign = get_len_of_range_longs(ihigh, ilow, neg_istep);
1614 Py_DECREF(neg_istep);
1615 }
1616
1617 n = (int)bign;
1618 if (bign < 0 || (long)n != bign) {
1619 PyErr_SetString(PyExc_OverflowError,
1620 "range() result has too many items");
1621 goto Fail;
1622 }
1623
1624 v = PyList_New(n);
1625 if (v == NULL)
1626 goto Fail;
1627
1628 curnum = ilow;
1629 Py_INCREF(curnum);
1630
1631 for (i = 0; i < n; i++) {
1632 PyObject *w = PyNumber_Long(curnum);
1633 PyObject *tmp_num;
1634 if (w == NULL)
1635 goto Fail;
1636
1637 PyList_SET_ITEM(v, i, w);
1638
1639 tmp_num = PyNumber_Add(curnum, istep);
1640 if (tmp_num == NULL)
1641 goto Fail;
1642
1643 Py_DECREF(curnum);
1644 curnum = tmp_num;
1645 }
Tim Peters874e1f72003-04-13 22:13:08 +00001646 Py_DECREF(ilow);
1647 Py_DECREF(ihigh);
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001648 Py_DECREF(istep);
1649 Py_DECREF(zero);
Tim Peters874e1f72003-04-13 22:13:08 +00001650 Py_DECREF(curnum);
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001651 return v;
1652
1653 Fail:
Tim Peters874e1f72003-04-13 22:13:08 +00001654 Py_DECREF(ilow);
1655 Py_DECREF(ihigh);
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001656 Py_XDECREF(istep);
Tim Peters874e1f72003-04-13 22:13:08 +00001657 Py_DECREF(zero);
1658 Py_XDECREF(curnum);
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001659 Py_XDECREF(v);
1660 return NULL;
1661}
1662
Guido van Rossum124eff01999-02-23 16:11:01 +00001663/* Return number of items in range/xrange (lo, hi, step). step > 0
1664 * required. Return a value < 0 if & only if the true value is too
1665 * large to fit in a signed long.
1666 */
1667static long
Thomas Wouterse28c2962000-07-23 22:21:32 +00001668get_len_of_range(long lo, long hi, long step)
Guido van Rossum124eff01999-02-23 16:11:01 +00001669{
1670 /* -------------------------------------------------------------
1671 If lo >= hi, the range is empty.
1672 Else if n values are in the range, the last one is
1673 lo + (n-1)*step, which must be <= hi-1. Rearranging,
1674 n <= (hi - lo - 1)/step + 1, so taking the floor of the RHS gives
1675 the proper value. Since lo < hi in this case, hi-lo-1 >= 0, so
1676 the RHS is non-negative and so truncation is the same as the
1677 floor. Letting M be the largest positive long, the worst case
1678 for the RHS numerator is hi=M, lo=-M-1, and then
1679 hi-lo-1 = M-(-M-1)-1 = 2*M. Therefore unsigned long has enough
1680 precision to compute the RHS exactly.
1681 ---------------------------------------------------------------*/
1682 long n = 0;
1683 if (lo < hi) {
1684 unsigned long uhi = (unsigned long)hi;
1685 unsigned long ulo = (unsigned long)lo;
1686 unsigned long diff = uhi - ulo - 1;
1687 n = (long)(diff / (unsigned long)step + 1);
1688 }
1689 return n;
1690}
1691
Guido van Rossum79f25d91997-04-29 20:08:16 +00001692static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001693builtin_range(PyObject *self, PyObject *args)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001694{
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001695 long ilow = 0, ihigh = 0, istep = 1;
Guido van Rossum124eff01999-02-23 16:11:01 +00001696 long bign;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001697 int i, n;
Guido van Rossum124eff01999-02-23 16:11:01 +00001698
Guido van Rossum79f25d91997-04-29 20:08:16 +00001699 PyObject *v;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001700
Guido van Rossum79f25d91997-04-29 20:08:16 +00001701 if (PyTuple_Size(args) <= 1) {
1702 if (!PyArg_ParseTuple(args,
Guido van Rossum0865dd91995-01-17 16:30:22 +00001703 "l;range() requires 1-3 int arguments",
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001704 &ihigh)) {
1705 PyErr_Clear();
1706 return handle_range_longs(self, args);
1707 }
Guido van Rossum3f5da241990-12-20 15:06:42 +00001708 }
1709 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001710 if (!PyArg_ParseTuple(args,
Guido van Rossum0865dd91995-01-17 16:30:22 +00001711 "ll|l;range() requires 1-3 int arguments",
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001712 &ilow, &ihigh, &istep)) {
1713 PyErr_Clear();
1714 return handle_range_longs(self, args);
1715 }
Guido van Rossum3f5da241990-12-20 15:06:42 +00001716 }
1717 if (istep == 0) {
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001718 PyErr_SetString(PyExc_ValueError,
Alex Martellif471d472003-04-23 13:00:44 +00001719 "range() step argument must not be zero");
Guido van Rossum3f5da241990-12-20 15:06:42 +00001720 return NULL;
1721 }
Guido van Rossum124eff01999-02-23 16:11:01 +00001722 if (istep > 0)
1723 bign = get_len_of_range(ilow, ihigh, istep);
1724 else
1725 bign = get_len_of_range(ihigh, ilow, -istep);
1726 n = (int)bign;
1727 if (bign < 0 || (long)n != bign) {
Guido van Rossume23cde21999-01-12 05:07:47 +00001728 PyErr_SetString(PyExc_OverflowError,
Fred Drake661ea262000-10-24 19:57:45 +00001729 "range() result has too many items");
Guido van Rossume23cde21999-01-12 05:07:47 +00001730 return NULL;
1731 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00001732 v = PyList_New(n);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001733 if (v == NULL)
1734 return NULL;
1735 for (i = 0; i < n; i++) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001736 PyObject *w = PyInt_FromLong(ilow);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001737 if (w == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001738 Py_DECREF(v);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001739 return NULL;
1740 }
Guido van Rossuma937d141998-04-24 18:22:02 +00001741 PyList_SET_ITEM(v, i, w);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001742 ilow += istep;
1743 }
1744 return v;
1745}
1746
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001747PyDoc_STRVAR(range_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001748"range([start,] stop[, step]) -> list of integers\n\
1749\n\
1750Return a list containing an arithmetic progression of integers.\n\
1751range(i, j) returns [i, i+1, i+2, ..., j-1]; start (!) defaults to 0.\n\
1752When step is given, it specifies the increment (or decrement).\n\
1753For example, range(4) returns [0, 1, 2, 3]. The end point is omitted!\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001754These are exactly the valid indices for a list of 4 elements.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001755
1756
Guido van Rossum79f25d91997-04-29 20:08:16 +00001757static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001758builtin_raw_input(PyObject *self, PyObject *args)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001759{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001760 PyObject *v = NULL;
Martin v. Löwis31d2df52002-08-14 15:46:02 +00001761 PyObject *fin = PySys_GetObject("stdin");
1762 PyObject *fout = PySys_GetObject("stdout");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001763
Raymond Hettingerea3fdf42002-12-29 16:33:45 +00001764 if (!PyArg_UnpackTuple(args, "[raw_]input", 0, 1, &v))
Guido van Rossum3165fe61992-09-25 21:59:05 +00001765 return NULL;
Martin v. Löwis31d2df52002-08-14 15:46:02 +00001766
1767 if (fin == NULL) {
Alex Martellia9b9c9f2003-04-23 13:34:35 +00001768 PyErr_SetString(PyExc_RuntimeError, "[raw_]input: lost sys.stdin");
Martin v. Löwis31d2df52002-08-14 15:46:02 +00001769 return NULL;
1770 }
1771 if (fout == NULL) {
Alex Martellia9b9c9f2003-04-23 13:34:35 +00001772 PyErr_SetString(PyExc_RuntimeError, "[raw_]input: lost sys.stdout");
Martin v. Löwis31d2df52002-08-14 15:46:02 +00001773 return NULL;
1774 }
1775 if (PyFile_SoftSpace(fout, 0)) {
1776 if (PyFile_WriteString(" ", fout) != 0)
1777 return NULL;
1778 }
Georg Brandl7e3ba2a2006-08-06 08:23:54 +00001779 if (PyFile_AsFile(fin) && PyFile_AsFile(fout)
Martin v. Löwis566f6af2002-10-26 14:39:10 +00001780 && isatty(fileno(PyFile_AsFile(fin)))
1781 && isatty(fileno(PyFile_AsFile(fout)))) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001782 PyObject *po;
Guido van Rossum872537c1995-07-07 22:43:42 +00001783 char *prompt;
1784 char *s;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001785 PyObject *result;
Guido van Rossum872537c1995-07-07 22:43:42 +00001786 if (v != NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001787 po = PyObject_Str(v);
Guido van Rossum872537c1995-07-07 22:43:42 +00001788 if (po == NULL)
1789 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001790 prompt = PyString_AsString(po);
Guido van Rossumd9b52081998-06-26 18:25:38 +00001791 if (prompt == NULL)
1792 return NULL;
Guido van Rossum872537c1995-07-07 22:43:42 +00001793 }
1794 else {
1795 po = NULL;
1796 prompt = "";
1797 }
Michael W. Hudson52db5192004-08-02 13:21:09 +00001798 s = PyOS_Readline(PyFile_AsFile(fin), PyFile_AsFile(fout),
Martin v. Löwis566f6af2002-10-26 14:39:10 +00001799 prompt);
Guido van Rossum79f25d91997-04-29 20:08:16 +00001800 Py_XDECREF(po);
Guido van Rossum872537c1995-07-07 22:43:42 +00001801 if (s == NULL) {
Michael W. Hudson30ea2f22004-07-07 17:44:12 +00001802 if (!PyErr_Occurred())
1803 PyErr_SetNone(PyExc_KeyboardInterrupt);
Guido van Rossum872537c1995-07-07 22:43:42 +00001804 return NULL;
1805 }
1806 if (*s == '\0') {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001807 PyErr_SetNone(PyExc_EOFError);
Guido van Rossum872537c1995-07-07 22:43:42 +00001808 result = NULL;
1809 }
1810 else { /* strip trailing '\n' */
Guido van Rossum106f2da2000-06-28 21:12:25 +00001811 size_t len = strlen(s);
Martin v. Löwisb1ed7fa2006-04-13 07:52:27 +00001812 if (len > PY_SSIZE_T_MAX) {
Martin v. Löwis31d2df52002-08-14 15:46:02 +00001813 PyErr_SetString(PyExc_OverflowError,
Alex Martellia9b9c9f2003-04-23 13:34:35 +00001814 "[raw_]input: input too long");
Guido van Rossum106f2da2000-06-28 21:12:25 +00001815 result = NULL;
1816 }
1817 else {
Martin v. Löwisb1ed7fa2006-04-13 07:52:27 +00001818 result = PyString_FromStringAndSize(s, len-1);
Guido van Rossum106f2da2000-06-28 21:12:25 +00001819 }
Guido van Rossum872537c1995-07-07 22:43:42 +00001820 }
Guido van Rossumb18618d2000-05-03 23:44:39 +00001821 PyMem_FREE(s);
Guido van Rossum872537c1995-07-07 22:43:42 +00001822 return result;
1823 }
Guido van Rossum90933611991-06-07 16:10:43 +00001824 if (v != NULL) {
Martin v. Löwis31d2df52002-08-14 15:46:02 +00001825 if (PyFile_WriteObject(v, fout, Py_PRINT_RAW) != 0)
Guido van Rossum90933611991-06-07 16:10:43 +00001826 return NULL;
1827 }
Martin v. Löwis31d2df52002-08-14 15:46:02 +00001828 return PyFile_GetLine(fin, -1);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001829}
1830
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001831PyDoc_STRVAR(raw_input_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001832"raw_input([prompt]) -> string\n\
1833\n\
1834Read a string from standard input. The trailing newline is stripped.\n\
1835If the user hits EOF (Unix: Ctl-D, Windows: Ctl-Z+Return), raise EOFError.\n\
1836On Unix, GNU readline is used if enabled. The prompt string, if given,\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001837is printed without a trailing newline before reading.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001838
1839
Guido van Rossum79f25d91997-04-29 20:08:16 +00001840static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001841builtin_reduce(PyObject *self, PyObject *args)
Guido van Rossum12d12c51993-10-26 17:58:25 +00001842{
Tim Peters15d81ef2001-05-04 04:39:21 +00001843 PyObject *seq, *func, *result = NULL, *it;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001844
Neal Norwitzdf25efe2007-05-23 06:58:36 +00001845 if (Py_Py3kWarningFlag &&
1846 PyErr_Warn(PyExc_DeprecationWarning,
1847 "reduce() not supported in 3.x") < 0)
1848 return NULL;
1849
Raymond Hettingerea3fdf42002-12-29 16:33:45 +00001850 if (!PyArg_UnpackTuple(args, "reduce", 2, 3, &func, &seq, &result))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001851 return NULL;
1852 if (result != NULL)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001853 Py_INCREF(result);
Guido van Rossum12d12c51993-10-26 17:58:25 +00001854
Tim Peters15d81ef2001-05-04 04:39:21 +00001855 it = PyObject_GetIter(seq);
1856 if (it == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001857 PyErr_SetString(PyExc_TypeError,
Tim Peters15d81ef2001-05-04 04:39:21 +00001858 "reduce() arg 2 must support iteration");
1859 Py_XDECREF(result);
Guido van Rossum12d12c51993-10-26 17:58:25 +00001860 return NULL;
1861 }
1862
Guido van Rossum79f25d91997-04-29 20:08:16 +00001863 if ((args = PyTuple_New(2)) == NULL)
Guido van Rossum2d951851994-08-29 12:52:16 +00001864 goto Fail;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001865
Tim Peters15d81ef2001-05-04 04:39:21 +00001866 for (;;) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001867 PyObject *op2;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001868
1869 if (args->ob_refcnt > 1) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001870 Py_DECREF(args);
1871 if ((args = PyTuple_New(2)) == NULL)
Guido van Rossum2d951851994-08-29 12:52:16 +00001872 goto Fail;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001873 }
1874
Tim Peters15d81ef2001-05-04 04:39:21 +00001875 op2 = PyIter_Next(it);
1876 if (op2 == NULL) {
Tim Petersf4848da2001-05-05 00:14:56 +00001877 if (PyErr_Occurred())
1878 goto Fail;
1879 break;
Guido van Rossum2d951851994-08-29 12:52:16 +00001880 }
Guido van Rossum12d12c51993-10-26 17:58:25 +00001881
Guido van Rossum2d951851994-08-29 12:52:16 +00001882 if (result == NULL)
1883 result = op2;
1884 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001885 PyTuple_SetItem(args, 0, result);
1886 PyTuple_SetItem(args, 1, op2);
1887 if ((result = PyEval_CallObject(func, args)) == NULL)
Guido van Rossum2d951851994-08-29 12:52:16 +00001888 goto Fail;
1889 }
Guido van Rossum12d12c51993-10-26 17:58:25 +00001890 }
1891
Guido van Rossum79f25d91997-04-29 20:08:16 +00001892 Py_DECREF(args);
Guido van Rossum12d12c51993-10-26 17:58:25 +00001893
Guido van Rossum2d951851994-08-29 12:52:16 +00001894 if (result == NULL)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001895 PyErr_SetString(PyExc_TypeError,
Fred Drake661ea262000-10-24 19:57:45 +00001896 "reduce() of empty sequence with no initial value");
Guido van Rossum2d951851994-08-29 12:52:16 +00001897
Tim Peters15d81ef2001-05-04 04:39:21 +00001898 Py_DECREF(it);
Guido van Rossum12d12c51993-10-26 17:58:25 +00001899 return result;
1900
Guido van Rossum2d951851994-08-29 12:52:16 +00001901Fail:
Guido van Rossum79f25d91997-04-29 20:08:16 +00001902 Py_XDECREF(args);
1903 Py_XDECREF(result);
Tim Peters15d81ef2001-05-04 04:39:21 +00001904 Py_DECREF(it);
Guido van Rossum12d12c51993-10-26 17:58:25 +00001905 return NULL;
1906}
1907
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001908PyDoc_STRVAR(reduce_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001909"reduce(function, sequence[, initial]) -> value\n\
1910\n\
1911Apply a function of two arguments cumulatively to the items of a sequence,\n\
1912from left to right, so as to reduce the sequence to a single value.\n\
1913For example, reduce(lambda x, y: x+y, [1, 2, 3, 4, 5]) calculates\n\
1914((((1+2)+3)+4)+5). If initial is present, it is placed before the items\n\
1915of the sequence in the calculation, and serves as a default when the\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001916sequence is empty.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001917
1918
Guido van Rossum79f25d91997-04-29 20:08:16 +00001919static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001920builtin_reload(PyObject *self, PyObject *v)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001921{
Neal Norwitzdf25efe2007-05-23 06:58:36 +00001922 if (Py_Py3kWarningFlag &&
1923 PyErr_Warn(PyExc_DeprecationWarning,
1924 "reload() not supported in 3.x") < 0)
1925 return NULL;
1926
Guido van Rossum79f25d91997-04-29 20:08:16 +00001927 return PyImport_ReloadModule(v);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001928}
1929
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001930PyDoc_STRVAR(reload_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001931"reload(module) -> module\n\
1932\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001933Reload the module. The module must have been successfully imported before.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001934
1935
Guido van Rossum79f25d91997-04-29 20:08:16 +00001936static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001937builtin_repr(PyObject *self, PyObject *v)
Guido van Rossumc89705d1992-11-26 08:54:07 +00001938{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001939 return PyObject_Repr(v);
Guido van Rossumc89705d1992-11-26 08:54:07 +00001940}
1941
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001942PyDoc_STRVAR(repr_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001943"repr(object) -> string\n\
1944\n\
1945Return the canonical string representation of the object.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001946For most object types, eval(repr(object)) == object.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001947
1948
Guido van Rossum79f25d91997-04-29 20:08:16 +00001949static PyObject *
Georg Brandlccadf842006-03-31 18:54:53 +00001950builtin_round(PyObject *self, PyObject *args, PyObject *kwds)
Guido van Rossum9e51f9b1993-02-12 16:29:05 +00001951{
Jeffrey Yasskin9871d8f2008-01-05 08:47:13 +00001952 double number;
1953 double f;
1954 int ndigits = 0;
1955 int i;
Georg Brandlccadf842006-03-31 18:54:53 +00001956 static char *kwlist[] = {"number", "ndigits", 0};
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001957
Jeffrey Yasskin9871d8f2008-01-05 08:47:13 +00001958 if (!PyArg_ParseTupleAndKeywords(args, kwds, "d|i:round",
1959 kwlist, &number, &ndigits))
1960 return NULL;
1961 f = 1.0;
1962 i = abs(ndigits);
1963 while (--i >= 0)
1964 f = f*10.0;
1965 if (ndigits < 0)
1966 number /= f;
1967 else
1968 number *= f;
1969 if (number >= 0.0)
1970 number = floor(number + 0.5);
1971 else
1972 number = ceil(number - 0.5);
1973 if (ndigits < 0)
1974 number *= f;
1975 else
1976 number /= f;
1977 return PyFloat_FromDouble(number);
Guido van Rossum9e51f9b1993-02-12 16:29:05 +00001978}
1979
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001980PyDoc_STRVAR(round_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001981"round(number[, ndigits]) -> floating point number\n\
1982\n\
1983Round a number to a given precision in decimal digits (default 0 digits).\n\
Jeffrey Yasskin9871d8f2008-01-05 08:47:13 +00001984This always returns a floating point number. Precision may be negative.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001985
Raymond Hettinger64958a12003-12-17 20:43:33 +00001986static PyObject *
1987builtin_sorted(PyObject *self, PyObject *args, PyObject *kwds)
1988{
1989 PyObject *newlist, *v, *seq, *compare=NULL, *keyfunc=NULL, *newargs;
1990 PyObject *callable;
Martin v. Löwis15e62742006-02-27 16:46:16 +00001991 static char *kwlist[] = {"iterable", "cmp", "key", "reverse", 0};
Neal Norwitz6f0d4792005-12-15 06:40:36 +00001992 int reverse;
Raymond Hettinger64958a12003-12-17 20:43:33 +00001993
Neal Norwitz6f0d4792005-12-15 06:40:36 +00001994 /* args 1-4 should match listsort in Objects/listobject.c */
Armin Rigo71d7e702005-09-20 18:13:03 +00001995 if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|OOi:sorted",
1996 kwlist, &seq, &compare, &keyfunc, &reverse))
1997 return NULL;
Raymond Hettinger64958a12003-12-17 20:43:33 +00001998
1999 newlist = PySequence_List(seq);
2000 if (newlist == NULL)
2001 return NULL;
2002
2003 callable = PyObject_GetAttrString(newlist, "sort");
2004 if (callable == NULL) {
2005 Py_DECREF(newlist);
2006 return NULL;
2007 }
Georg Brandl99d7e4e2005-08-31 22:21:15 +00002008
Raymond Hettinger64958a12003-12-17 20:43:33 +00002009 newargs = PyTuple_GetSlice(args, 1, 4);
2010 if (newargs == NULL) {
2011 Py_DECREF(newlist);
2012 Py_DECREF(callable);
2013 return NULL;
2014 }
2015
2016 v = PyObject_Call(callable, newargs, kwds);
2017 Py_DECREF(newargs);
2018 Py_DECREF(callable);
2019 if (v == NULL) {
2020 Py_DECREF(newlist);
2021 return NULL;
2022 }
2023 Py_DECREF(v);
2024 return newlist;
2025}
2026
2027PyDoc_STRVAR(sorted_doc,
2028"sorted(iterable, cmp=None, key=None, reverse=False) --> new sorted list");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002029
Guido van Rossum79f25d91997-04-29 20:08:16 +00002030static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002031builtin_vars(PyObject *self, PyObject *args)
Guido van Rossum2d951851994-08-29 12:52:16 +00002032{
Guido van Rossum79f25d91997-04-29 20:08:16 +00002033 PyObject *v = NULL;
2034 PyObject *d;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002035
Raymond Hettingerea3fdf42002-12-29 16:33:45 +00002036 if (!PyArg_UnpackTuple(args, "vars", 0, 1, &v))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002037 return NULL;
Guido van Rossum2d951851994-08-29 12:52:16 +00002038 if (v == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002039 d = PyEval_GetLocals();
Guido van Rossum53bb7ff1995-07-26 16:26:31 +00002040 if (d == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002041 if (!PyErr_Occurred())
2042 PyErr_SetString(PyExc_SystemError,
Alex Martellia9b9c9f2003-04-23 13:34:35 +00002043 "vars(): no locals!?");
Guido van Rossum53bb7ff1995-07-26 16:26:31 +00002044 }
2045 else
Guido van Rossum79f25d91997-04-29 20:08:16 +00002046 Py_INCREF(d);
Guido van Rossum2d951851994-08-29 12:52:16 +00002047 }
2048 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002049 d = PyObject_GetAttrString(v, "__dict__");
Guido van Rossum2d951851994-08-29 12:52:16 +00002050 if (d == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002051 PyErr_SetString(PyExc_TypeError,
Guido van Rossum2d951851994-08-29 12:52:16 +00002052 "vars() argument must have __dict__ attribute");
2053 return NULL;
2054 }
2055 }
2056 return d;
2057}
2058
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002059PyDoc_STRVAR(vars_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002060"vars([object]) -> dictionary\n\
2061\n\
2062Without arguments, equivalent to locals().\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002063With an argument, equivalent to object.__dict__.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002064
Alex Martellia70b1912003-04-22 08:12:33 +00002065
2066static PyObject*
2067builtin_sum(PyObject *self, PyObject *args)
2068{
2069 PyObject *seq;
2070 PyObject *result = NULL;
2071 PyObject *temp, *item, *iter;
2072
Raymond Hettinger5cf63942003-10-25 06:41:37 +00002073 if (!PyArg_UnpackTuple(args, "sum", 1, 2, &seq, &result))
Alex Martellia70b1912003-04-22 08:12:33 +00002074 return NULL;
2075
2076 iter = PyObject_GetIter(seq);
2077 if (iter == NULL)
2078 return NULL;
2079
2080 if (result == NULL) {
2081 result = PyInt_FromLong(0);
2082 if (result == NULL) {
2083 Py_DECREF(iter);
2084 return NULL;
2085 }
2086 } else {
2087 /* reject string values for 'start' parameter */
2088 if (PyObject_TypeCheck(result, &PyBaseString_Type)) {
2089 PyErr_SetString(PyExc_TypeError,
Alex Martellia9b9c9f2003-04-23 13:34:35 +00002090 "sum() can't sum strings [use ''.join(seq) instead]");
Alex Martellia70b1912003-04-22 08:12:33 +00002091 Py_DECREF(iter);
2092 return NULL;
2093 }
Alex Martelli41c9f882003-04-22 09:24:48 +00002094 Py_INCREF(result);
Alex Martellia70b1912003-04-22 08:12:33 +00002095 }
2096
Raymond Hettinger3f8caa32007-10-24 01:28:33 +00002097#ifndef SLOW_SUM
2098 /* Fast addition by keeping temporary sums in C instead of new Python objects.
2099 Assumes all inputs are the same type. If the assumption fails, default
2100 to the more general routine.
2101 */
2102 if (PyInt_CheckExact(result)) {
2103 long i_result = PyInt_AS_LONG(result);
2104 Py_DECREF(result);
2105 result = NULL;
2106 while(result == NULL) {
2107 item = PyIter_Next(iter);
2108 if (item == NULL) {
2109 Py_DECREF(iter);
2110 if (PyErr_Occurred())
2111 return NULL;
2112 return PyInt_FromLong(i_result);
2113 }
2114 if (PyInt_CheckExact(item)) {
2115 long b = PyInt_AS_LONG(item);
2116 long x = i_result + b;
2117 if ((x^i_result) >= 0 || (x^b) >= 0) {
2118 i_result = x;
2119 Py_DECREF(item);
2120 continue;
2121 }
2122 }
2123 /* Either overflowed or is not an int. Restore real objects and process normally */
2124 result = PyInt_FromLong(i_result);
2125 temp = PyNumber_Add(result, item);
2126 Py_DECREF(result);
2127 Py_DECREF(item);
2128 result = temp;
2129 if (result == NULL) {
2130 Py_DECREF(iter);
2131 return NULL;
2132 }
2133 }
2134 }
2135
2136 if (PyFloat_CheckExact(result)) {
2137 double f_result = PyFloat_AS_DOUBLE(result);
2138 Py_DECREF(result);
2139 result = NULL;
2140 while(result == NULL) {
2141 item = PyIter_Next(iter);
2142 if (item == NULL) {
2143 Py_DECREF(iter);
2144 if (PyErr_Occurred())
2145 return NULL;
2146 return PyFloat_FromDouble(f_result);
2147 }
2148 if (PyFloat_CheckExact(item)) {
2149 PyFPE_START_PROTECT("add", return 0)
2150 f_result += PyFloat_AS_DOUBLE(item);
Raymond Hettinger3a8daf52007-10-24 02:05:51 +00002151 PyFPE_END_PROTECT(f_result)
Raymond Hettingera45c4872007-10-25 02:26:58 +00002152 Py_DECREF(item);
Raymond Hettinger3a8daf52007-10-24 02:05:51 +00002153 continue;
2154 }
2155 if (PyInt_CheckExact(item)) {
2156 PyFPE_START_PROTECT("add", return 0)
2157 f_result += (double)PyInt_AS_LONG(item);
2158 PyFPE_END_PROTECT(f_result)
Raymond Hettingera45c4872007-10-25 02:26:58 +00002159 Py_DECREF(item);
Raymond Hettinger3f8caa32007-10-24 01:28:33 +00002160 continue;
2161 }
2162 result = PyFloat_FromDouble(f_result);
2163 temp = PyNumber_Add(result, item);
2164 Py_DECREF(result);
2165 Py_DECREF(item);
2166 result = temp;
2167 if (result == NULL) {
2168 Py_DECREF(iter);
2169 return NULL;
2170 }
2171 }
2172 }
2173#endif
2174
Alex Martellia70b1912003-04-22 08:12:33 +00002175 for(;;) {
2176 item = PyIter_Next(iter);
2177 if (item == NULL) {
2178 /* error, or end-of-sequence */
2179 if (PyErr_Occurred()) {
2180 Py_DECREF(result);
2181 result = NULL;
2182 }
2183 break;
2184 }
Alex Martellia253e182003-10-25 23:24:14 +00002185 temp = PyNumber_Add(result, item);
Alex Martellia70b1912003-04-22 08:12:33 +00002186 Py_DECREF(result);
2187 Py_DECREF(item);
2188 result = temp;
2189 if (result == NULL)
2190 break;
2191 }
2192 Py_DECREF(iter);
2193 return result;
2194}
2195
2196PyDoc_STRVAR(sum_doc,
Georg Brandl8134d062006-10-12 12:33:07 +00002197"sum(sequence[, start]) -> value\n\
Alex Martellia70b1912003-04-22 08:12:33 +00002198\n\
2199Returns the sum of a sequence of numbers (NOT strings) plus the value\n\
Georg Brandl8134d062006-10-12 12:33:07 +00002200of parameter 'start' (which defaults to 0). When the sequence is\n\
2201empty, returns start.");
Alex Martellia70b1912003-04-22 08:12:33 +00002202
2203
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002204static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002205builtin_isinstance(PyObject *self, PyObject *args)
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002206{
2207 PyObject *inst;
2208 PyObject *cls;
Guido van Rossum823649d2001-03-21 18:40:58 +00002209 int retval;
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002210
Raymond Hettingerea3fdf42002-12-29 16:33:45 +00002211 if (!PyArg_UnpackTuple(args, "isinstance", 2, 2, &inst, &cls))
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002212 return NULL;
Guido van Rossumf5dd9141997-12-02 19:11:45 +00002213
Guido van Rossum823649d2001-03-21 18:40:58 +00002214 retval = PyObject_IsInstance(inst, cls);
2215 if (retval < 0)
Guido van Rossumad991772001-01-12 16:03:05 +00002216 return NULL;
Guido van Rossum77f6a652002-04-03 22:41:51 +00002217 return PyBool_FromLong(retval);
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002218}
2219
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002220PyDoc_STRVAR(isinstance_doc,
Guido van Rossum77f6a652002-04-03 22:41:51 +00002221"isinstance(object, class-or-type-or-tuple) -> bool\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002222\n\
2223Return whether an object is an instance of a class or of a subclass thereof.\n\
Guido van Rossum03290ec2001-10-07 20:54:12 +00002224With a type as second argument, return whether that is the object's type.\n\
2225The form using a tuple, isinstance(x, (A, B, ...)), is a shortcut for\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002226isinstance(x, A) or isinstance(x, B) or ... (etc.).");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002227
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002228
2229static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002230builtin_issubclass(PyObject *self, PyObject *args)
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002231{
2232 PyObject *derived;
2233 PyObject *cls;
2234 int retval;
2235
Raymond Hettingerea3fdf42002-12-29 16:33:45 +00002236 if (!PyArg_UnpackTuple(args, "issubclass", 2, 2, &derived, &cls))
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002237 return NULL;
Guido van Rossum668213d1999-06-16 17:28:37 +00002238
Guido van Rossum823649d2001-03-21 18:40:58 +00002239 retval = PyObject_IsSubclass(derived, cls);
2240 if (retval < 0)
2241 return NULL;
Guido van Rossum77f6a652002-04-03 22:41:51 +00002242 return PyBool_FromLong(retval);
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002243}
2244
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002245PyDoc_STRVAR(issubclass_doc,
Guido van Rossum77f6a652002-04-03 22:41:51 +00002246"issubclass(C, B) -> bool\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002247\n\
Walter Dörwaldd9a6ad32002-12-12 16:41:44 +00002248Return whether class C is a subclass (i.e., a derived class) of class B.\n\
2249When using a tuple as the second argument issubclass(X, (A, B, ...)),\n\
2250is a shortcut for issubclass(X, A) or issubclass(X, B) or ... (etc.).");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002251
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002252
Barry Warsawbd599b52000-08-03 15:45:29 +00002253static PyObject*
2254builtin_zip(PyObject *self, PyObject *args)
2255{
2256 PyObject *ret;
Martin v. Löwisd96ee902006-02-16 14:37:16 +00002257 const Py_ssize_t itemsize = PySequence_Length(args);
2258 Py_ssize_t i;
Tim Peters8572b4f2001-05-06 01:05:02 +00002259 PyObject *itlist; /* tuple of iterators */
Martin v. Löwisd96ee902006-02-16 14:37:16 +00002260 Py_ssize_t len; /* guess at result length */
Barry Warsawbd599b52000-08-03 15:45:29 +00002261
Raymond Hettingereaef6152003-08-02 07:42:57 +00002262 if (itemsize == 0)
2263 return PyList_New(0);
2264
Barry Warsawbd599b52000-08-03 15:45:29 +00002265 /* args must be a tuple */
2266 assert(PyTuple_Check(args));
2267
Tim Peters39a86c22002-05-12 07:19:38 +00002268 /* Guess at result length: the shortest of the input lengths.
2269 If some argument refuses to say, we refuse to guess too, lest
2270 an argument like xrange(sys.maxint) lead us astray.*/
Tim Peters67d687a2002-04-29 21:27:32 +00002271 len = -1; /* unknown */
2272 for (i = 0; i < itemsize; ++i) {
2273 PyObject *item = PyTuple_GET_ITEM(args, i);
Raymond Hettinger4e2f7142007-12-06 00:56:53 +00002274 Py_ssize_t thislen = _PyObject_LengthHint(item, -1);
Tim Peters39a86c22002-05-12 07:19:38 +00002275 if (thislen < 0) {
Tim Peters39a86c22002-05-12 07:19:38 +00002276 len = -1;
2277 break;
2278 }
Tim Peters67d687a2002-04-29 21:27:32 +00002279 else if (len < 0 || thislen < len)
2280 len = thislen;
2281 }
2282
Tim Peters8572b4f2001-05-06 01:05:02 +00002283 /* allocate result list */
Tim Peters67d687a2002-04-29 21:27:32 +00002284 if (len < 0)
2285 len = 10; /* arbitrary */
2286 if ((ret = PyList_New(len)) == NULL)
Barry Warsawbd599b52000-08-03 15:45:29 +00002287 return NULL;
2288
Tim Peters8572b4f2001-05-06 01:05:02 +00002289 /* obtain iterators */
2290 itlist = PyTuple_New(itemsize);
2291 if (itlist == NULL)
2292 goto Fail_ret;
2293 for (i = 0; i < itemsize; ++i) {
2294 PyObject *item = PyTuple_GET_ITEM(args, i);
2295 PyObject *it = PyObject_GetIter(item);
2296 if (it == NULL) {
2297 if (PyErr_ExceptionMatches(PyExc_TypeError))
2298 PyErr_Format(PyExc_TypeError,
Neal Norwitza361bd82006-02-19 19:31:50 +00002299 "zip argument #%zd must support iteration",
Tim Peters8572b4f2001-05-06 01:05:02 +00002300 i+1);
2301 goto Fail_ret_itlist;
Barry Warsawbd599b52000-08-03 15:45:29 +00002302 }
Tim Peters8572b4f2001-05-06 01:05:02 +00002303 PyTuple_SET_ITEM(itlist, i, it);
2304 }
Barry Warsawbd599b52000-08-03 15:45:29 +00002305
Tim Peters8572b4f2001-05-06 01:05:02 +00002306 /* build result into ret list */
Tim Peters67d687a2002-04-29 21:27:32 +00002307 for (i = 0; ; ++i) {
2308 int j;
Tim Peters8572b4f2001-05-06 01:05:02 +00002309 PyObject *next = PyTuple_New(itemsize);
2310 if (!next)
2311 goto Fail_ret_itlist;
2312
Tim Peters67d687a2002-04-29 21:27:32 +00002313 for (j = 0; j < itemsize; j++) {
2314 PyObject *it = PyTuple_GET_ITEM(itlist, j);
Tim Peters8572b4f2001-05-06 01:05:02 +00002315 PyObject *item = PyIter_Next(it);
Barry Warsawbd599b52000-08-03 15:45:29 +00002316 if (!item) {
Tim Peters8572b4f2001-05-06 01:05:02 +00002317 if (PyErr_Occurred()) {
2318 Py_DECREF(ret);
2319 ret = NULL;
Barry Warsawbd599b52000-08-03 15:45:29 +00002320 }
2321 Py_DECREF(next);
Tim Peters8572b4f2001-05-06 01:05:02 +00002322 Py_DECREF(itlist);
Tim Peters67d687a2002-04-29 21:27:32 +00002323 goto Done;
Barry Warsawbd599b52000-08-03 15:45:29 +00002324 }
Tim Peters67d687a2002-04-29 21:27:32 +00002325 PyTuple_SET_ITEM(next, j, item);
Barry Warsawbd599b52000-08-03 15:45:29 +00002326 }
Tim Peters8572b4f2001-05-06 01:05:02 +00002327
Tim Peters67d687a2002-04-29 21:27:32 +00002328 if (i < len)
2329 PyList_SET_ITEM(ret, i, next);
2330 else {
2331 int status = PyList_Append(ret, next);
2332 Py_DECREF(next);
2333 ++len;
2334 if (status < 0)
2335 goto Fail_ret_itlist;
2336 }
Barry Warsawbd599b52000-08-03 15:45:29 +00002337 }
Tim Peters8572b4f2001-05-06 01:05:02 +00002338
Tim Peters67d687a2002-04-29 21:27:32 +00002339Done:
2340 if (ret != NULL && i < len) {
2341 /* The list is too big. */
2342 if (PyList_SetSlice(ret, i, len, NULL) < 0)
2343 return NULL;
2344 }
2345 return ret;
2346
Tim Peters8572b4f2001-05-06 01:05:02 +00002347Fail_ret_itlist:
2348 Py_DECREF(itlist);
2349Fail_ret:
2350 Py_DECREF(ret);
2351 return NULL;
Barry Warsawbd599b52000-08-03 15:45:29 +00002352}
2353
2354
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002355PyDoc_STRVAR(zip_doc,
Barry Warsawbd599b52000-08-03 15:45:29 +00002356"zip(seq1 [, seq2 [...]]) -> [(seq1[0], seq2[0] ...), (...)]\n\
2357\n\
2358Return a list of tuples, where each tuple contains the i-th element\n\
2359from each of the argument sequences. The returned list is truncated\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002360in length to the length of the shortest argument sequence.");
Barry Warsawbd599b52000-08-03 15:45:29 +00002361
2362
Guido van Rossum79f25d91997-04-29 20:08:16 +00002363static PyMethodDef builtin_methods[] = {
Neal Norwitz92e212f2006-04-03 04:48:37 +00002364 {"__import__", (PyCFunction)builtin___import__, METH_VARARGS | METH_KEYWORDS, import_doc},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002365 {"abs", builtin_abs, METH_O, abs_doc},
Raymond Hettinger96229b12005-03-11 06:49:40 +00002366 {"all", builtin_all, METH_O, all_doc},
2367 {"any", builtin_any, METH_O, any_doc},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002368 {"apply", builtin_apply, METH_VARARGS, apply_doc},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002369 {"callable", builtin_callable, METH_O, callable_doc},
2370 {"chr", builtin_chr, METH_VARARGS, chr_doc},
2371 {"cmp", builtin_cmp, METH_VARARGS, cmp_doc},
2372 {"coerce", builtin_coerce, METH_VARARGS, coerce_doc},
Georg Brandl5240d742007-03-13 20:46:32 +00002373 {"compile", (PyCFunction)builtin_compile, METH_VARARGS | METH_KEYWORDS, compile_doc},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002374 {"delattr", builtin_delattr, METH_VARARGS, delattr_doc},
2375 {"dir", builtin_dir, METH_VARARGS, dir_doc},
2376 {"divmod", builtin_divmod, METH_VARARGS, divmod_doc},
2377 {"eval", builtin_eval, METH_VARARGS, eval_doc},
2378 {"execfile", builtin_execfile, METH_VARARGS, execfile_doc},
2379 {"filter", builtin_filter, METH_VARARGS, filter_doc},
Eric Smitha9f7d622008-02-17 19:46:49 +00002380 {"format", builtin_format, METH_VARARGS, format_doc},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002381 {"getattr", builtin_getattr, METH_VARARGS, getattr_doc},
2382 {"globals", (PyCFunction)builtin_globals, METH_NOARGS, globals_doc},
2383 {"hasattr", builtin_hasattr, METH_VARARGS, hasattr_doc},
2384 {"hash", builtin_hash, METH_O, hash_doc},
2385 {"hex", builtin_hex, METH_O, hex_doc},
2386 {"id", builtin_id, METH_O, id_doc},
2387 {"input", builtin_input, METH_VARARGS, input_doc},
2388 {"intern", builtin_intern, METH_VARARGS, intern_doc},
2389 {"isinstance", builtin_isinstance, METH_VARARGS, isinstance_doc},
2390 {"issubclass", builtin_issubclass, METH_VARARGS, issubclass_doc},
2391 {"iter", builtin_iter, METH_VARARGS, iter_doc},
2392 {"len", builtin_len, METH_O, len_doc},
2393 {"locals", (PyCFunction)builtin_locals, METH_NOARGS, locals_doc},
2394 {"map", builtin_map, METH_VARARGS, map_doc},
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00002395 {"max", (PyCFunction)builtin_max, METH_VARARGS | METH_KEYWORDS, max_doc},
2396 {"min", (PyCFunction)builtin_min, METH_VARARGS | METH_KEYWORDS, min_doc},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002397 {"oct", builtin_oct, METH_O, oct_doc},
Neal Norwitzc4edb0e2006-05-02 04:43:14 +00002398 {"open", (PyCFunction)builtin_open, METH_VARARGS | METH_KEYWORDS, open_doc},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002399 {"ord", builtin_ord, METH_O, ord_doc},
2400 {"pow", builtin_pow, METH_VARARGS, pow_doc},
2401 {"range", builtin_range, METH_VARARGS, range_doc},
2402 {"raw_input", builtin_raw_input, METH_VARARGS, raw_input_doc},
2403 {"reduce", builtin_reduce, METH_VARARGS, reduce_doc},
2404 {"reload", builtin_reload, METH_O, reload_doc},
2405 {"repr", builtin_repr, METH_O, repr_doc},
Georg Brandlccadf842006-03-31 18:54:53 +00002406 {"round", (PyCFunction)builtin_round, METH_VARARGS | METH_KEYWORDS, round_doc},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002407 {"setattr", builtin_setattr, METH_VARARGS, setattr_doc},
Raymond Hettinger64958a12003-12-17 20:43:33 +00002408 {"sorted", (PyCFunction)builtin_sorted, METH_VARARGS | METH_KEYWORDS, sorted_doc},
Alex Martellia70b1912003-04-22 08:12:33 +00002409 {"sum", builtin_sum, METH_VARARGS, sum_doc},
Martin v. Löwis339d0f72001-08-17 18:39:25 +00002410#ifdef Py_USING_UNICODE
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002411 {"unichr", builtin_unichr, METH_VARARGS, unichr_doc},
Martin v. Löwis339d0f72001-08-17 18:39:25 +00002412#endif
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002413 {"vars", builtin_vars, METH_VARARGS, vars_doc},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002414 {"zip", builtin_zip, METH_VARARGS, zip_doc},
Guido van Rossumc02e15c1991-12-16 13:03:00 +00002415 {NULL, NULL},
Guido van Rossum3f5da241990-12-20 15:06:42 +00002416};
2417
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002418PyDoc_STRVAR(builtin_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002419"Built-in functions, exceptions, and other objects.\n\
2420\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002421Noteworthy: None is the `nil' object; Ellipsis represents `...' in slices.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002422
Guido van Rossum25ce5661997-08-02 03:10:38 +00002423PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002424_PyBuiltin_Init(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +00002425{
Fred Drake5550de32000-06-20 04:54:19 +00002426 PyObject *mod, *dict, *debug;
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002427 mod = Py_InitModule4("__builtin__", builtin_methods,
2428 builtin_doc, (PyObject *)NULL,
2429 PYTHON_API_VERSION);
Guido van Rossum25ce5661997-08-02 03:10:38 +00002430 if (mod == NULL)
2431 return NULL;
2432 dict = PyModule_GetDict(mod);
Tim Peters4b7625e2001-09-13 21:37:17 +00002433
Tim Peters7571a0f2003-03-23 17:52:28 +00002434#ifdef Py_TRACE_REFS
2435 /* __builtin__ exposes a number of statically allocated objects
2436 * that, before this code was added in 2.3, never showed up in
2437 * the list of "all objects" maintained by Py_TRACE_REFS. As a
2438 * result, programs leaking references to None and False (etc)
2439 * couldn't be diagnosed by examining sys.getobjects(0).
2440 */
2441#define ADD_TO_ALL(OBJECT) _Py_AddToAllObjects((PyObject *)(OBJECT), 0)
2442#else
2443#define ADD_TO_ALL(OBJECT) (void)0
2444#endif
2445
Tim Peters4b7625e2001-09-13 21:37:17 +00002446#define SETBUILTIN(NAME, OBJECT) \
Tim Peters7571a0f2003-03-23 17:52:28 +00002447 if (PyDict_SetItemString(dict, NAME, (PyObject *)OBJECT) < 0) \
2448 return NULL; \
2449 ADD_TO_ALL(OBJECT)
Tim Peters4b7625e2001-09-13 21:37:17 +00002450
2451 SETBUILTIN("None", Py_None);
2452 SETBUILTIN("Ellipsis", Py_Ellipsis);
2453 SETBUILTIN("NotImplemented", Py_NotImplemented);
Guido van Rossum77f6a652002-04-03 22:41:51 +00002454 SETBUILTIN("False", Py_False);
2455 SETBUILTIN("True", Py_True);
Neal Norwitz32a7e7f2002-05-31 19:58:02 +00002456 SETBUILTIN("basestring", &PyBaseString_Type);
Guido van Rossum77f6a652002-04-03 22:41:51 +00002457 SETBUILTIN("bool", &PyBool_Type);
Christian Heimes288e89a2008-01-18 18:24:07 +00002458 SETBUILTIN("bytes", &PyString_Type);
Guido van Rossumbea18cc2002-06-14 20:41:17 +00002459 SETBUILTIN("buffer", &PyBuffer_Type);
Tim Peters4b7625e2001-09-13 21:37:17 +00002460 SETBUILTIN("classmethod", &PyClassMethod_Type);
Tim Peters6d6c1a32001-08-02 04:15:00 +00002461#ifndef WITHOUT_COMPLEX
Tim Peters4b7625e2001-09-13 21:37:17 +00002462 SETBUILTIN("complex", &PyComplex_Type);
Tim Peters6d6c1a32001-08-02 04:15:00 +00002463#endif
Tim Petersa427a2b2001-10-29 22:25:45 +00002464 SETBUILTIN("dict", &PyDict_Type);
Tim Peters67d687a2002-04-29 21:27:32 +00002465 SETBUILTIN("enumerate", &PyEnum_Type);
Neal Norwitzc4edb0e2006-05-02 04:43:14 +00002466 SETBUILTIN("file", &PyFile_Type);
Tim Peters4b7625e2001-09-13 21:37:17 +00002467 SETBUILTIN("float", &PyFloat_Type);
Raymond Hettingera690a992003-11-16 16:17:49 +00002468 SETBUILTIN("frozenset", &PyFrozenSet_Type);
Tim Peters4b7625e2001-09-13 21:37:17 +00002469 SETBUILTIN("property", &PyProperty_Type);
2470 SETBUILTIN("int", &PyInt_Type);
2471 SETBUILTIN("list", &PyList_Type);
2472 SETBUILTIN("long", &PyLong_Type);
2473 SETBUILTIN("object", &PyBaseObject_Type);
Raymond Hettinger85c20a42003-11-06 14:06:48 +00002474 SETBUILTIN("reversed", &PyReversed_Type);
Raymond Hettingera690a992003-11-16 16:17:49 +00002475 SETBUILTIN("set", &PySet_Type);
Guido van Rossumbea18cc2002-06-14 20:41:17 +00002476 SETBUILTIN("slice", &PySlice_Type);
Tim Peters4b7625e2001-09-13 21:37:17 +00002477 SETBUILTIN("staticmethod", &PyStaticMethod_Type);
2478 SETBUILTIN("str", &PyString_Type);
2479 SETBUILTIN("super", &PySuper_Type);
2480 SETBUILTIN("tuple", &PyTuple_Type);
2481 SETBUILTIN("type", &PyType_Type);
Raymond Hettingerc4c453f2002-06-05 23:12:45 +00002482 SETBUILTIN("xrange", &PyRange_Type);
Martin v. Löwis339d0f72001-08-17 18:39:25 +00002483#ifdef Py_USING_UNICODE
Tim Peters4b7625e2001-09-13 21:37:17 +00002484 SETBUILTIN("unicode", &PyUnicode_Type);
Martin v. Löwis339d0f72001-08-17 18:39:25 +00002485#endif
Guido van Rossum77f6a652002-04-03 22:41:51 +00002486 debug = PyBool_FromLong(Py_OptimizeFlag == 0);
Fred Drake5550de32000-06-20 04:54:19 +00002487 if (PyDict_SetItemString(dict, "__debug__", debug) < 0) {
2488 Py_XDECREF(debug);
Guido van Rossum25ce5661997-08-02 03:10:38 +00002489 return NULL;
Fred Drake5550de32000-06-20 04:54:19 +00002490 }
2491 Py_XDECREF(debug);
Barry Warsaw757af0e1997-08-29 22:13:51 +00002492
Guido van Rossum25ce5661997-08-02 03:10:38 +00002493 return mod;
Tim Peters7571a0f2003-03-23 17:52:28 +00002494#undef ADD_TO_ALL
Tim Peters4b7625e2001-09-13 21:37:17 +00002495#undef SETBUILTIN
Guido van Rossum3f5da241990-12-20 15:06:42 +00002496}
2497
Guido van Rossume77a7571993-11-03 15:01:26 +00002498/* Helper for filter(): filter a tuple through a function */
Guido van Rossum12d12c51993-10-26 17:58:25 +00002499
Guido van Rossum79f25d91997-04-29 20:08:16 +00002500static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002501filtertuple(PyObject *func, PyObject *tuple)
Guido van Rossum12d12c51993-10-26 17:58:25 +00002502{
Guido van Rossum79f25d91997-04-29 20:08:16 +00002503 PyObject *result;
Martin v. Löwis18e16552006-02-15 17:27:45 +00002504 Py_ssize_t i, j;
2505 Py_ssize_t len = PyTuple_Size(tuple);
Guido van Rossum12d12c51993-10-26 17:58:25 +00002506
Guido van Rossumb7b45621995-08-04 04:07:45 +00002507 if (len == 0) {
Walter Dörwaldc3da83f2003-02-04 20:24:45 +00002508 if (PyTuple_CheckExact(tuple))
2509 Py_INCREF(tuple);
2510 else
2511 tuple = PyTuple_New(0);
Guido van Rossumb7b45621995-08-04 04:07:45 +00002512 return tuple;
2513 }
2514
Guido van Rossum79f25d91997-04-29 20:08:16 +00002515 if ((result = PyTuple_New(len)) == NULL)
Guido van Rossum2586bf01993-11-01 16:21:44 +00002516 return NULL;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002517
Guido van Rossum12d12c51993-10-26 17:58:25 +00002518 for (i = j = 0; i < len; ++i) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002519 PyObject *item, *good;
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002520 int ok;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002521
Walter Dörwald8dd19322003-02-10 17:36:40 +00002522 if (tuple->ob_type->tp_as_sequence &&
2523 tuple->ob_type->tp_as_sequence->sq_item) {
2524 item = tuple->ob_type->tp_as_sequence->sq_item(tuple, i);
Walter Dörwaldc58a3a12003-08-18 18:28:45 +00002525 if (item == NULL)
2526 goto Fail_1;
Walter Dörwald8dd19322003-02-10 17:36:40 +00002527 } else {
Alex Martellia9b9c9f2003-04-23 13:34:35 +00002528 PyErr_SetString(PyExc_TypeError, "filter(): unsubscriptable tuple");
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002529 goto Fail_1;
Walter Dörwald8dd19322003-02-10 17:36:40 +00002530 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002531 if (func == Py_None) {
2532 Py_INCREF(item);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002533 good = item;
2534 }
2535 else {
Raymond Hettinger8ae46892003-10-12 19:09:37 +00002536 PyObject *arg = PyTuple_Pack(1, item);
Walter Dörwaldc58a3a12003-08-18 18:28:45 +00002537 if (arg == NULL) {
2538 Py_DECREF(item);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002539 goto Fail_1;
Walter Dörwaldc58a3a12003-08-18 18:28:45 +00002540 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002541 good = PyEval_CallObject(func, arg);
2542 Py_DECREF(arg);
Walter Dörwaldc58a3a12003-08-18 18:28:45 +00002543 if (good == NULL) {
2544 Py_DECREF(item);
Guido van Rossum12d12c51993-10-26 17:58:25 +00002545 goto Fail_1;
Walter Dörwaldc58a3a12003-08-18 18:28:45 +00002546 }
Guido van Rossum12d12c51993-10-26 17:58:25 +00002547 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002548 ok = PyObject_IsTrue(good);
2549 Py_DECREF(good);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002550 if (ok) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002551 if (PyTuple_SetItem(result, j++, item) < 0)
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002552 goto Fail_1;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002553 }
Walter Dörwaldc58a3a12003-08-18 18:28:45 +00002554 else
2555 Py_DECREF(item);
Guido van Rossum12d12c51993-10-26 17:58:25 +00002556 }
2557
Tim Peters4324aa32001-05-28 22:30:08 +00002558 if (_PyTuple_Resize(&result, j) < 0)
Guido van Rossum12d12c51993-10-26 17:58:25 +00002559 return NULL;
2560
Guido van Rossum12d12c51993-10-26 17:58:25 +00002561 return result;
2562
Guido van Rossum12d12c51993-10-26 17:58:25 +00002563Fail_1:
Guido van Rossum79f25d91997-04-29 20:08:16 +00002564 Py_DECREF(result);
Guido van Rossum12d12c51993-10-26 17:58:25 +00002565 return NULL;
2566}
2567
2568
Guido van Rossume77a7571993-11-03 15:01:26 +00002569/* Helper for filter(): filter a string through a function */
Guido van Rossum12d12c51993-10-26 17:58:25 +00002570
Guido van Rossum79f25d91997-04-29 20:08:16 +00002571static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002572filterstring(PyObject *func, PyObject *strobj)
Guido van Rossum12d12c51993-10-26 17:58:25 +00002573{
Guido van Rossum79f25d91997-04-29 20:08:16 +00002574 PyObject *result;
Martin v. Löwis18e16552006-02-15 17:27:45 +00002575 Py_ssize_t i, j;
2576 Py_ssize_t len = PyString_Size(strobj);
2577 Py_ssize_t outlen = len;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002578
Guido van Rossum79f25d91997-04-29 20:08:16 +00002579 if (func == Py_None) {
Walter Dörwald1918f772003-02-10 13:19:13 +00002580 /* If it's a real string we can return the original,
2581 * as no character is ever false and __getitem__
2582 * does return this character. If it's a subclass
2583 * we must go through the __getitem__ loop */
2584 if (PyString_CheckExact(strobj)) {
Walter Dörwaldc3da83f2003-02-04 20:24:45 +00002585 Py_INCREF(strobj);
Walter Dörwald1918f772003-02-10 13:19:13 +00002586 return strobj;
2587 }
Guido van Rossum12d12c51993-10-26 17:58:25 +00002588 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002589 if ((result = PyString_FromStringAndSize(NULL, len)) == NULL)
Guido van Rossum2586bf01993-11-01 16:21:44 +00002590 return NULL;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002591
Guido van Rossum12d12c51993-10-26 17:58:25 +00002592 for (i = j = 0; i < len; ++i) {
Walter Dörwald1918f772003-02-10 13:19:13 +00002593 PyObject *item;
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002594 int ok;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002595
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002596 item = (*strobj->ob_type->tp_as_sequence->sq_item)(strobj, i);
2597 if (item == NULL)
2598 goto Fail_1;
Walter Dörwald1918f772003-02-10 13:19:13 +00002599 if (func==Py_None) {
2600 ok = 1;
2601 } else {
2602 PyObject *arg, *good;
Raymond Hettinger8ae46892003-10-12 19:09:37 +00002603 arg = PyTuple_Pack(1, item);
Walter Dörwald1918f772003-02-10 13:19:13 +00002604 if (arg == NULL) {
2605 Py_DECREF(item);
2606 goto Fail_1;
2607 }
2608 good = PyEval_CallObject(func, arg);
2609 Py_DECREF(arg);
2610 if (good == NULL) {
2611 Py_DECREF(item);
2612 goto Fail_1;
2613 }
2614 ok = PyObject_IsTrue(good);
2615 Py_DECREF(good);
Tim Peters388ed082001-04-07 20:34:48 +00002616 }
Walter Dörwald903f1e02003-02-04 16:28:00 +00002617 if (ok) {
Martin v. Löwisd96ee902006-02-16 14:37:16 +00002618 Py_ssize_t reslen;
Walter Dörwald903f1e02003-02-04 16:28:00 +00002619 if (!PyString_Check(item)) {
2620 PyErr_SetString(PyExc_TypeError, "can't filter str to str:"
2621 " __getitem__ returned different type");
2622 Py_DECREF(item);
2623 goto Fail_1;
2624 }
2625 reslen = PyString_GET_SIZE(item);
2626 if (reslen == 1) {
2627 PyString_AS_STRING(result)[j++] =
2628 PyString_AS_STRING(item)[0];
2629 } else {
2630 /* do we need more space? */
Martin v. Löwisd96ee902006-02-16 14:37:16 +00002631 Py_ssize_t need = j + reslen + len-i-1;
Walter Dörwald903f1e02003-02-04 16:28:00 +00002632 if (need > outlen) {
2633 /* overallocate, to avoid reallocations */
2634 if (need<2*outlen)
2635 need = 2*outlen;
2636 if (_PyString_Resize(&result, need)) {
2637 Py_DECREF(item);
2638 return NULL;
2639 }
2640 outlen = need;
2641 }
2642 memcpy(
2643 PyString_AS_STRING(result) + j,
2644 PyString_AS_STRING(item),
2645 reslen
2646 );
2647 j += reslen;
2648 }
2649 }
Tim Peters388ed082001-04-07 20:34:48 +00002650 Py_DECREF(item);
Guido van Rossum12d12c51993-10-26 17:58:25 +00002651 }
2652
Walter Dörwald903f1e02003-02-04 16:28:00 +00002653 if (j < outlen)
Tim Peters5de98422002-04-27 18:44:32 +00002654 _PyString_Resize(&result, j);
Guido van Rossum12d12c51993-10-26 17:58:25 +00002655
Guido van Rossum12d12c51993-10-26 17:58:25 +00002656 return result;
2657
Guido van Rossum12d12c51993-10-26 17:58:25 +00002658Fail_1:
Guido van Rossum79f25d91997-04-29 20:08:16 +00002659 Py_DECREF(result);
Guido van Rossum12d12c51993-10-26 17:58:25 +00002660 return NULL;
2661}
Martin v. Löwis8afd7572003-01-25 22:46:11 +00002662
2663#ifdef Py_USING_UNICODE
2664/* Helper for filter(): filter a Unicode object through a function */
2665
2666static PyObject *
2667filterunicode(PyObject *func, PyObject *strobj)
2668{
2669 PyObject *result;
Martin v. Löwis725507b2006-03-07 12:08:51 +00002670 register Py_ssize_t i, j;
Martin v. Löwisd96ee902006-02-16 14:37:16 +00002671 Py_ssize_t len = PyUnicode_GetSize(strobj);
2672 Py_ssize_t outlen = len;
Martin v. Löwis8afd7572003-01-25 22:46:11 +00002673
2674 if (func == Py_None) {
Walter Dörwald1918f772003-02-10 13:19:13 +00002675 /* If it's a real string we can return the original,
2676 * as no character is ever false and __getitem__
2677 * does return this character. If it's a subclass
2678 * we must go through the __getitem__ loop */
2679 if (PyUnicode_CheckExact(strobj)) {
Walter Dörwaldc3da83f2003-02-04 20:24:45 +00002680 Py_INCREF(strobj);
Walter Dörwald1918f772003-02-10 13:19:13 +00002681 return strobj;
2682 }
Martin v. Löwis8afd7572003-01-25 22:46:11 +00002683 }
2684 if ((result = PyUnicode_FromUnicode(NULL, len)) == NULL)
2685 return NULL;
2686
2687 for (i = j = 0; i < len; ++i) {
2688 PyObject *item, *arg, *good;
2689 int ok;
2690
2691 item = (*strobj->ob_type->tp_as_sequence->sq_item)(strobj, i);
2692 if (item == NULL)
2693 goto Fail_1;
Walter Dörwald1918f772003-02-10 13:19:13 +00002694 if (func == Py_None) {
2695 ok = 1;
2696 } else {
Raymond Hettinger8ae46892003-10-12 19:09:37 +00002697 arg = PyTuple_Pack(1, item);
Walter Dörwald1918f772003-02-10 13:19:13 +00002698 if (arg == NULL) {
2699 Py_DECREF(item);
2700 goto Fail_1;
2701 }
2702 good = PyEval_CallObject(func, arg);
2703 Py_DECREF(arg);
2704 if (good == NULL) {
2705 Py_DECREF(item);
2706 goto Fail_1;
2707 }
2708 ok = PyObject_IsTrue(good);
2709 Py_DECREF(good);
Martin v. Löwis8afd7572003-01-25 22:46:11 +00002710 }
Walter Dörwald903f1e02003-02-04 16:28:00 +00002711 if (ok) {
Martin v. Löwisd96ee902006-02-16 14:37:16 +00002712 Py_ssize_t reslen;
Walter Dörwald903f1e02003-02-04 16:28:00 +00002713 if (!PyUnicode_Check(item)) {
Georg Brandl99d7e4e2005-08-31 22:21:15 +00002714 PyErr_SetString(PyExc_TypeError,
Jeremy Hylton1aad9c72003-09-16 03:10:59 +00002715 "can't filter unicode to unicode:"
2716 " __getitem__ returned different type");
Walter Dörwald903f1e02003-02-04 16:28:00 +00002717 Py_DECREF(item);
2718 goto Fail_1;
2719 }
2720 reslen = PyUnicode_GET_SIZE(item);
Georg Brandl99d7e4e2005-08-31 22:21:15 +00002721 if (reslen == 1)
Walter Dörwald903f1e02003-02-04 16:28:00 +00002722 PyUnicode_AS_UNICODE(result)[j++] =
2723 PyUnicode_AS_UNICODE(item)[0];
Jeremy Hylton1aad9c72003-09-16 03:10:59 +00002724 else {
Walter Dörwald903f1e02003-02-04 16:28:00 +00002725 /* do we need more space? */
Martin v. Löwisd96ee902006-02-16 14:37:16 +00002726 Py_ssize_t need = j + reslen + len - i - 1;
Walter Dörwald903f1e02003-02-04 16:28:00 +00002727 if (need > outlen) {
Georg Brandl99d7e4e2005-08-31 22:21:15 +00002728 /* overallocate,
Jeremy Hylton1aad9c72003-09-16 03:10:59 +00002729 to avoid reallocations */
2730 if (need < 2 * outlen)
2731 need = 2 * outlen;
Jeremy Hylton364f6be2003-09-16 03:17:16 +00002732 if (PyUnicode_Resize(
2733 &result, need) < 0) {
Walter Dörwald903f1e02003-02-04 16:28:00 +00002734 Py_DECREF(item);
Walter Dörwald531e0002003-02-04 16:57:49 +00002735 goto Fail_1;
Walter Dörwald903f1e02003-02-04 16:28:00 +00002736 }
2737 outlen = need;
2738 }
Jeremy Hylton1aad9c72003-09-16 03:10:59 +00002739 memcpy(PyUnicode_AS_UNICODE(result) + j,
2740 PyUnicode_AS_UNICODE(item),
2741 reslen*sizeof(Py_UNICODE));
Walter Dörwald903f1e02003-02-04 16:28:00 +00002742 j += reslen;
2743 }
2744 }
Martin v. Löwis8afd7572003-01-25 22:46:11 +00002745 Py_DECREF(item);
2746 }
2747
Walter Dörwald903f1e02003-02-04 16:28:00 +00002748 if (j < outlen)
Martin v. Löwis8afd7572003-01-25 22:46:11 +00002749 PyUnicode_Resize(&result, j);
2750
2751 return result;
2752
2753Fail_1:
2754 Py_DECREF(result);
2755 return NULL;
2756}
2757#endif