blob: 9a3135634e09c2093914795d9a30bb1f9dac38ed [file] [log] [blame]
Guido van Rossum3f5da241990-12-20 15:06:42 +00001/* Built-in functions */
2
Guido van Rossum79f25d91997-04-29 20:08:16 +00003#include "Python.h"
Guido van Rossum3f5da241990-12-20 15:06:42 +00004
5#include "node.h"
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006#include "code.h"
Guido van Rossum5b722181993-03-30 17:46:03 +00007#include "eval.h"
Guido van Rossum3f5da241990-12-20 15:06:42 +00008
Guido van Rossum6bf62da1997-04-11 20:37:35 +00009#include <ctype.h>
10
Guido van Rossume2ae77b2001-10-24 20:42:55 +000011#ifdef RISCOS
12#include "unixstuff.h"
13#endif
14
Mark Hammond26cffde42001-05-14 12:17:34 +000015/* The default encoding used by the platform file system APIs
16 Can remain NULL for all platforms that don't have such a concept
17*/
Martin v. Löwis6238d2b2002-06-30 15:26:10 +000018#if defined(MS_WINDOWS) && defined(HAVE_USABLE_WCHAR_T)
Mark Hammond26cffde42001-05-14 12:17:34 +000019const char *Py_FileSystemDefaultEncoding = "mbcs";
Just van Rossumb9b8e9c2003-02-10 09:22:01 +000020#elif defined(__APPLE__)
21const char *Py_FileSystemDefaultEncoding = "utf-8";
Mark Hammond26cffde42001-05-14 12:17:34 +000022#else
23const char *Py_FileSystemDefaultEncoding = NULL; /* use default */
24#endif
Mark Hammondef8b6542001-05-13 08:04:26 +000025
Guido van Rossum12d12c51993-10-26 17:58:25 +000026/* Forward */
Tim Petersdbd9ba62000-07-09 03:09:57 +000027static PyObject *filterstring(PyObject *, PyObject *);
Martin v. Löwis8afd7572003-01-25 22:46:11 +000028#ifdef Py_USING_UNICODE
29static PyObject *filterunicode(PyObject *, PyObject *);
30#endif
Tim Petersdbd9ba62000-07-09 03:09:57 +000031static PyObject *filtertuple (PyObject *, PyObject *);
Guido van Rossum12d12c51993-10-26 17:58:25 +000032
Guido van Rossum79f25d91997-04-29 20:08:16 +000033static PyObject *
Neal Norwitz92e212f2006-04-03 04:48:37 +000034builtin___import__(PyObject *self, PyObject *args, PyObject *kwds)
Guido van Rossum3f5da241990-12-20 15:06:42 +000035{
Neal Norwitz92e212f2006-04-03 04:48:37 +000036 static char *kwlist[] = {"name", "globals", "locals", "fromlist",
37 "level", 0};
Guido van Rossum1ae940a1995-01-02 19:04:15 +000038 char *name;
Guido van Rossum79f25d91997-04-29 20:08:16 +000039 PyObject *globals = NULL;
40 PyObject *locals = NULL;
41 PyObject *fromlist = NULL;
Thomas Woutersf7f438b2006-02-28 16:09:29 +000042 int level = -1;
Guido van Rossum1ae940a1995-01-02 19:04:15 +000043
Neal Norwitz92e212f2006-04-03 04:48:37 +000044 if (!PyArg_ParseTupleAndKeywords(args, kwds, "s|OOOi:__import__",
45 kwlist, &name, &globals, &locals, &fromlist, &level))
Guido van Rossum1ae940a1995-01-02 19:04:15 +000046 return NULL;
Thomas Woutersf7f438b2006-02-28 16:09:29 +000047 return PyImport_ImportModuleLevel(name, globals, locals,
48 fromlist, level);
Guido van Rossum1ae940a1995-01-02 19:04:15 +000049}
50
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000051PyDoc_STRVAR(import_doc,
Neal Norwitz92e212f2006-04-03 04:48:37 +000052"__import__(name, globals={}, locals={}, fromlist=[], level=-1) -> module\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +000053\n\
54Import a module. The globals are only used to determine the context;\n\
55they are not modified. The locals are currently unused. The fromlist\n\
56should be a list of names to emulate ``from name import ...'', or an\n\
57empty list to emulate ``import name''.\n\
58When importing a module from a package, note that __import__('A.B', ...)\n\
59returns package A when fromlist is empty, but its submodule B when\n\
Neal Norwitz92e212f2006-04-03 04:48:37 +000060fromlist is not empty. Level is used to determine whether to perform \n\
61absolute or relative imports. -1 is the original strategy of attempting\n\
62both absolute and relative imports, 0 is absolute, a positive number\n\
63is the number of parent directories to search relative to the current module.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +000064
Guido van Rossum1ae940a1995-01-02 19:04:15 +000065
Guido van Rossum79f25d91997-04-29 20:08:16 +000066static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +000067builtin_abs(PyObject *self, PyObject *v)
Guido van Rossum1ae940a1995-01-02 19:04:15 +000068{
Guido van Rossum09df08a1998-05-22 00:51:39 +000069 return PyNumber_Absolute(v);
Guido van Rossum3f5da241990-12-20 15:06:42 +000070}
71
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000072PyDoc_STRVAR(abs_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +000073"abs(number) -> number\n\
74\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000075Return the absolute value of the argument.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +000076
Raymond Hettinger96229b12005-03-11 06:49:40 +000077static PyObject *
78builtin_all(PyObject *self, PyObject *v)
79{
80 PyObject *it, *item;
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 *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000342builtin_chr(PyObject *self, PyObject *args)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000343{
344 long x;
345 char s[1];
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000346
Guido van Rossum79f25d91997-04-29 20:08:16 +0000347 if (!PyArg_ParseTuple(args, "l:chr", &x))
Guido van Rossum3f5da241990-12-20 15:06:42 +0000348 return NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000349 if (x < 0 || x >= 256) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000350 PyErr_SetString(PyExc_ValueError,
351 "chr() arg not in range(256)");
Guido van Rossum3f5da241990-12-20 15:06:42 +0000352 return NULL;
353 }
Guido van Rossum6bf62da1997-04-11 20:37:35 +0000354 s[0] = (char)x;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000355 return PyString_FromStringAndSize(s, 1);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000356}
357
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000358PyDoc_STRVAR(chr_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000359"chr(i) -> character\n\
360\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000361Return a string of one character with ordinal i; 0 <= i < 256.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000362
363
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000364#ifdef Py_USING_UNICODE
Guido van Rossum79f25d91997-04-29 20:08:16 +0000365static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000366builtin_unichr(PyObject *self, PyObject *args)
Guido van Rossum09095f32000-03-10 23:00:52 +0000367{
368 long x;
Guido van Rossum09095f32000-03-10 23:00:52 +0000369
370 if (!PyArg_ParseTuple(args, "l:unichr", &x))
371 return NULL;
Fredrik Lundh0dcf67e2001-06-26 20:01:56 +0000372
Marc-André Lemburgcc8764c2002-08-11 12:23:04 +0000373 return PyUnicode_FromOrdinal(x);
Guido van Rossum09095f32000-03-10 23:00:52 +0000374}
375
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000376PyDoc_STRVAR(unichr_doc,
Fred Drake078b24f2000-04-13 02:42:50 +0000377"unichr(i) -> Unicode character\n\
Guido van Rossum09095f32000-03-10 23:00:52 +0000378\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000379Return a Unicode string of one character with ordinal i; 0 <= i <= 0x10ffff.");
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000380#endif
Guido van Rossum09095f32000-03-10 23:00:52 +0000381
382
383static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000384builtin_cmp(PyObject *self, PyObject *args)
Guido van Rossuma9e7dc11992-10-18 18:53:57 +0000385{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000386 PyObject *a, *b;
Guido van Rossum09df08a1998-05-22 00:51:39 +0000387 int c;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000388
Raymond Hettingerea3fdf42002-12-29 16:33:45 +0000389 if (!PyArg_UnpackTuple(args, "cmp", 2, 2, &a, &b))
Guido van Rossuma9e7dc11992-10-18 18:53:57 +0000390 return NULL;
Guido van Rossum09df08a1998-05-22 00:51:39 +0000391 if (PyObject_Cmp(a, b, &c) < 0)
Guido van Rossumc8b6df91997-05-23 00:06:51 +0000392 return NULL;
Guido van Rossum09df08a1998-05-22 00:51:39 +0000393 return PyInt_FromLong((long)c);
Guido van Rossuma9e7dc11992-10-18 18:53:57 +0000394}
395
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000396PyDoc_STRVAR(cmp_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000397"cmp(x, y) -> integer\n\
398\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000399Return negative if x<y, zero if x==y, positive if x>y.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000400
401
Guido van Rossum79f25d91997-04-29 20:08:16 +0000402static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000403builtin_coerce(PyObject *self, PyObject *args)
Guido van Rossum6a00cd81995-01-07 12:39:01 +0000404{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000405 PyObject *v, *w;
406 PyObject *res;
Guido van Rossum5524a591995-01-10 15:26:20 +0000407
Neal Norwitzdf25efe2007-05-23 06:58:36 +0000408 if (Py_Py3kWarningFlag &&
409 PyErr_Warn(PyExc_DeprecationWarning,
410 "coerce() not supported in 3.x") < 0)
411 return NULL;
412
Raymond Hettingerea3fdf42002-12-29 16:33:45 +0000413 if (!PyArg_UnpackTuple(args, "coerce", 2, 2, &v, &w))
Guido van Rossum5524a591995-01-10 15:26:20 +0000414 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000415 if (PyNumber_Coerce(&v, &w) < 0)
Guido van Rossum04691fc1992-08-12 15:35:34 +0000416 return NULL;
Raymond Hettinger8ae46892003-10-12 19:09:37 +0000417 res = PyTuple_Pack(2, v, w);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000418 Py_DECREF(v);
419 Py_DECREF(w);
Guido van Rossum04691fc1992-08-12 15:35:34 +0000420 return res;
421}
422
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000423PyDoc_STRVAR(coerce_doc,
Martin v. Löwis8d494f32004-08-25 10:42:41 +0000424"coerce(x, y) -> (x1, y1)\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000425\n\
Martin v. Löwis8d494f32004-08-25 10:42:41 +0000426Return a tuple consisting of the two numeric arguments converted to\n\
427a common type, using the same rules as used by arithmetic operations.\n\
428If coercion is not possible, raise TypeError.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000429
Guido van Rossum79f25d91997-04-29 20:08:16 +0000430static PyObject *
Georg Brandl5240d742007-03-13 20:46:32 +0000431builtin_compile(PyObject *self, PyObject *args, PyObject *kwds)
Guido van Rossum5b722181993-03-30 17:46:03 +0000432{
433 char *str;
434 char *filename;
435 char *startstr;
436 int start;
Tim Peters6cd6a822001-08-17 22:11:27 +0000437 int dont_inherit = 0;
438 int supplied_flags = 0;
Tim Peters5ba58662001-07-16 02:29:45 +0000439 PyCompilerFlags cf;
Neal Norwitz3a9a3e72005-11-27 20:38:31 +0000440 PyObject *result = NULL, *cmd, *tmp = NULL;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000441 Py_ssize_t length;
Georg Brandl5240d742007-03-13 20:46:32 +0000442 static char *kwlist[] = {"source", "filename", "mode", "flags",
443 "dont_inherit", NULL};
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000444
Georg Brandl5240d742007-03-13 20:46:32 +0000445 if (!PyArg_ParseTupleAndKeywords(args, kwds, "Oss|ii:compile",
446 kwlist, &cmd, &filename, &startstr,
447 &supplied_flags, &dont_inherit))
Guido van Rossum5b722181993-03-30 17:46:03 +0000448 return NULL;
Tim Peters6cd6a822001-08-17 22:11:27 +0000449
Just van Rossum3aaf42c2003-02-10 08:21:10 +0000450 cf.cf_flags = supplied_flags;
451
452#ifdef Py_USING_UNICODE
453 if (PyUnicode_Check(cmd)) {
454 tmp = PyUnicode_AsUTF8String(cmd);
455 if (tmp == NULL)
456 return NULL;
457 cmd = tmp;
458 cf.cf_flags |= PyCF_SOURCE_IS_UTF8;
459 }
460#endif
Just van Rossumb9b8e9c2003-02-10 09:22:01 +0000461 if (PyObject_AsReadBuffer(cmd, (const void **)&str, &length))
462 return NULL;
Tim Peters2c646c92003-02-10 14:48:29 +0000463 if ((size_t)length != strlen(str)) {
Just van Rossum3aaf42c2003-02-10 08:21:10 +0000464 PyErr_SetString(PyExc_TypeError,
Alex Martellia9b9c9f2003-04-23 13:34:35 +0000465 "compile() expected string without null bytes");
Neal Norwitz3a9a3e72005-11-27 20:38:31 +0000466 goto cleanup;
Just van Rossum3aaf42c2003-02-10 08:21:10 +0000467 }
468
Guido van Rossum5b722181993-03-30 17:46:03 +0000469 if (strcmp(startstr, "exec") == 0)
Guido van Rossumb05a5c71997-05-07 17:46:13 +0000470 start = Py_file_input;
Guido van Rossum5b722181993-03-30 17:46:03 +0000471 else if (strcmp(startstr, "eval") == 0)
Guido van Rossumb05a5c71997-05-07 17:46:13 +0000472 start = Py_eval_input;
Guido van Rossum872537c1995-07-07 22:43:42 +0000473 else if (strcmp(startstr, "single") == 0)
Guido van Rossumb05a5c71997-05-07 17:46:13 +0000474 start = Py_single_input;
Guido van Rossum5b722181993-03-30 17:46:03 +0000475 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000476 PyErr_SetString(PyExc_ValueError,
Fred Drake661ea262000-10-24 19:57:45 +0000477 "compile() arg 3 must be 'exec' or 'eval' or 'single'");
Neal Norwitz3a9a3e72005-11-27 20:38:31 +0000478 goto cleanup;
Guido van Rossum5b722181993-03-30 17:46:03 +0000479 }
Tim Peters6cd6a822001-08-17 22:11:27 +0000480
Guido van Rossum4b499dd32003-02-13 22:07:59 +0000481 if (supplied_flags &
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000482 ~(PyCF_MASK | PyCF_MASK_OBSOLETE | PyCF_DONT_IMPLY_DEDENT | PyCF_ONLY_AST))
Guido van Rossum4b499dd32003-02-13 22:07:59 +0000483 {
Tim Peters6cd6a822001-08-17 22:11:27 +0000484 PyErr_SetString(PyExc_ValueError,
485 "compile(): unrecognised flags");
Neal Norwitz3a9a3e72005-11-27 20:38:31 +0000486 goto cleanup;
Tim Peters6cd6a822001-08-17 22:11:27 +0000487 }
488 /* XXX Warn if (supplied_flags & PyCF_MASK_OBSOLETE) != 0? */
489
Tim Peters6cd6a822001-08-17 22:11:27 +0000490 if (!dont_inherit) {
491 PyEval_MergeCompilerFlags(&cf);
492 }
Just van Rossum3aaf42c2003-02-10 08:21:10 +0000493 result = Py_CompileStringFlags(str, filename, start, &cf);
Neal Norwitz3a9a3e72005-11-27 20:38:31 +0000494cleanup:
Just van Rossum3aaf42c2003-02-10 08:21:10 +0000495 Py_XDECREF(tmp);
496 return result;
Guido van Rossum5b722181993-03-30 17:46:03 +0000497}
498
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000499PyDoc_STRVAR(compile_doc,
Tim Peters6cd6a822001-08-17 22:11:27 +0000500"compile(source, filename, mode[, flags[, dont_inherit]]) -> code object\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000501\n\
502Compile the source string (a Python module, statement or expression)\n\
503into a code object that can be executed by the exec statement or eval().\n\
504The filename will be used for run-time error messages.\n\
505The mode must be 'exec' to compile a module, 'single' to compile a\n\
Tim Peters6cd6a822001-08-17 22:11:27 +0000506single (interactive) statement, or 'eval' to compile an expression.\n\
507The flags argument, if present, controls which future statements influence\n\
508the compilation of the code.\n\
509The dont_inherit argument, if non-zero, stops the compilation inheriting\n\
510the effects of any future statements in effect in the code calling\n\
511compile; if absent or zero these statements do influence the compilation,\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000512in addition to any features explicitly specified.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000513
Guido van Rossum79f25d91997-04-29 20:08:16 +0000514static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000515builtin_dir(PyObject *self, PyObject *args)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000516{
Tim Peters5d2b77c2001-09-03 05:47:38 +0000517 PyObject *arg = NULL;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000518
Raymond Hettingerea3fdf42002-12-29 16:33:45 +0000519 if (!PyArg_UnpackTuple(args, "dir", 0, 1, &arg))
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000520 return NULL;
Tim Peters7eea37e2001-09-04 22:08:56 +0000521 return PyObject_Dir(arg);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000522}
523
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000524PyDoc_STRVAR(dir_doc,
Tim Peters5d2b77c2001-09-03 05:47:38 +0000525"dir([object]) -> list of strings\n"
526"\n"
Georg Brandl871f1bc2007-03-12 13:17:36 +0000527"If called without an argument, return the names in the current scope.\n"
528"Else, return an alphabetized list of names comprising (some of) the attributes\n"
529"of the given object, and of attributes reachable from it.\n"
530"If the object supplies a method named __dir__, it will be used; otherwise\n"
531"the default dir() logic is used and returns:\n"
532" for a module object: the module's attributes.\n"
533" for a class object: its attributes, and recursively the attributes\n"
534" of its bases.\n"
Georg Brandl3bb15672007-03-13 07:23:16 +0000535" for any other object: its attributes, its class's attributes, and\n"
Georg Brandl871f1bc2007-03-12 13:17:36 +0000536" recursively the attributes of its class's base classes.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000537
Guido van Rossum79f25d91997-04-29 20:08:16 +0000538static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000539builtin_divmod(PyObject *self, PyObject *args)
Guido van Rossum6a00cd81995-01-07 12:39:01 +0000540{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000541 PyObject *v, *w;
Guido van Rossum6a00cd81995-01-07 12:39:01 +0000542
Raymond Hettingerea3fdf42002-12-29 16:33:45 +0000543 if (!PyArg_UnpackTuple(args, "divmod", 2, 2, &v, &w))
Guido van Rossum6a00cd81995-01-07 12:39:01 +0000544 return NULL;
Guido van Rossum09df08a1998-05-22 00:51:39 +0000545 return PyNumber_Divmod(v, w);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000546}
547
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000548PyDoc_STRVAR(divmod_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000549"divmod(x, y) -> (div, mod)\n\
550\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000551Return the tuple ((x-x%y)/y, x%y). Invariant: div*y + mod == x.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000552
553
Guido van Rossum79f25d91997-04-29 20:08:16 +0000554static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000555builtin_eval(PyObject *self, PyObject *args)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000556{
Just van Rossum3aaf42c2003-02-10 08:21:10 +0000557 PyObject *cmd, *result, *tmp = NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000558 PyObject *globals = Py_None, *locals = Py_None;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000559 char *str;
Tim Peters9fa96be2001-08-17 23:04:59 +0000560 PyCompilerFlags cf;
Guido van Rossum590baa41993-11-30 13:40:46 +0000561
Raymond Hettinger214b1c32004-07-02 06:41:07 +0000562 if (!PyArg_UnpackTuple(args, "eval", 1, 3, &cmd, &globals, &locals))
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000563 return NULL;
Raymond Hettinger214b1c32004-07-02 06:41:07 +0000564 if (locals != Py_None && !PyMapping_Check(locals)) {
565 PyErr_SetString(PyExc_TypeError, "locals must be a mapping");
Raymond Hettinger513ffe82004-07-06 13:44:41 +0000566 return NULL;
Raymond Hettinger214b1c32004-07-02 06:41:07 +0000567 }
568 if (globals != Py_None && !PyDict_Check(globals)) {
Georg Brandl99d7e4e2005-08-31 22:21:15 +0000569 PyErr_SetString(PyExc_TypeError, PyMapping_Check(globals) ?
Raymond Hettinger214b1c32004-07-02 06:41:07 +0000570 "globals must be a real dict; try eval(expr, {}, mapping)"
571 : "globals must be a dict");
Raymond Hettinger513ffe82004-07-06 13:44:41 +0000572 return NULL;
Raymond Hettinger214b1c32004-07-02 06:41:07 +0000573 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000574 if (globals == Py_None) {
575 globals = PyEval_GetGlobals();
576 if (locals == Py_None)
577 locals = PyEval_GetLocals();
Guido van Rossum6135a871995-01-09 17:53:26 +0000578 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000579 else if (locals == Py_None)
Guido van Rossum6135a871995-01-09 17:53:26 +0000580 locals = globals;
Tim Peters9fa96be2001-08-17 23:04:59 +0000581
Georg Brandl77c85e62005-09-15 10:46:13 +0000582 if (globals == NULL || locals == NULL) {
583 PyErr_SetString(PyExc_TypeError,
584 "eval must be given globals and locals "
585 "when called without a frame");
586 return NULL;
587 }
588
Guido van Rossum79f25d91997-04-29 20:08:16 +0000589 if (PyDict_GetItemString(globals, "__builtins__") == NULL) {
590 if (PyDict_SetItemString(globals, "__builtins__",
591 PyEval_GetBuiltins()) != 0)
Guido van Rossum6135a871995-01-09 17:53:26 +0000592 return NULL;
593 }
Tim Peters9fa96be2001-08-17 23:04:59 +0000594
Jeremy Hylton15c1c4f2001-07-30 21:50:55 +0000595 if (PyCode_Check(cmd)) {
Jeremy Hylton733c8932001-12-13 19:51:56 +0000596 if (PyCode_GetNumFree((PyCodeObject *)cmd) > 0) {
Jeremy Hylton15c1c4f2001-07-30 21:50:55 +0000597 PyErr_SetString(PyExc_TypeError,
598 "code object passed to eval() may not contain free variables");
599 return NULL;
600 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000601 return PyEval_EvalCode((PyCodeObject *) cmd, globals, locals);
Jeremy Hylton15c1c4f2001-07-30 21:50:55 +0000602 }
Tim Peters9fa96be2001-08-17 23:04:59 +0000603
Marc-André Lemburgd1ba4432000-09-19 21:04:18 +0000604 if (!PyString_Check(cmd) &&
605 !PyUnicode_Check(cmd)) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000606 PyErr_SetString(PyExc_TypeError,
Fred Drake661ea262000-10-24 19:57:45 +0000607 "eval() arg 1 must be a string or code object");
Guido van Rossum94390a41992-08-14 15:14:30 +0000608 return NULL;
609 }
Just van Rossum3aaf42c2003-02-10 08:21:10 +0000610 cf.cf_flags = 0;
611
612#ifdef Py_USING_UNICODE
613 if (PyUnicode_Check(cmd)) {
614 tmp = PyUnicode_AsUTF8String(cmd);
615 if (tmp == NULL)
616 return NULL;
617 cmd = tmp;
618 cf.cf_flags |= PyCF_SOURCE_IS_UTF8;
619 }
620#endif
Neal Norwitz3a9a3e72005-11-27 20:38:31 +0000621 if (PyString_AsStringAndSize(cmd, &str, NULL)) {
622 Py_XDECREF(tmp);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000623 return NULL;
Neal Norwitz3a9a3e72005-11-27 20:38:31 +0000624 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000625 while (*str == ' ' || *str == '\t')
626 str++;
Tim Peters9fa96be2001-08-17 23:04:59 +0000627
Tim Peters9fa96be2001-08-17 23:04:59 +0000628 (void)PyEval_MergeCompilerFlags(&cf);
Just van Rossum3aaf42c2003-02-10 08:21:10 +0000629 result = PyRun_StringFlags(str, Py_eval_input, globals, locals, &cf);
630 Py_XDECREF(tmp);
631 return result;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000632}
633
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000634PyDoc_STRVAR(eval_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000635"eval(source[, globals[, locals]]) -> value\n\
636\n\
637Evaluate the source in the context of globals and locals.\n\
638The source may be a string representing a Python expression\n\
639or a code object as returned by compile().\n\
Neal Norwitz477ca1c2006-09-05 02:25:41 +0000640The globals must be a dictionary and locals can be any mapping,\n\
Raymond Hettinger214b1c32004-07-02 06:41:07 +0000641defaulting to the current globals and locals.\n\
642If only globals is given, locals defaults to it.\n");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000643
644
Guido van Rossum79f25d91997-04-29 20:08:16 +0000645static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000646builtin_execfile(PyObject *self, PyObject *args)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000647{
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000648 char *filename;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000649 PyObject *globals = Py_None, *locals = Py_None;
650 PyObject *res;
Guido van Rossumb3a639e2001-09-05 13:37:47 +0000651 FILE* fp = NULL;
Tim Peters5ba58662001-07-16 02:29:45 +0000652 PyCompilerFlags cf;
Martin v. Löwis6b3a2c42001-08-08 05:30:36 +0000653 int exists;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000654
Neal Norwitzdf25efe2007-05-23 06:58:36 +0000655 if (Py_Py3kWarningFlag &&
656 PyErr_Warn(PyExc_DeprecationWarning,
657 "execfile() not supported in 3.x") < 0)
658 return NULL;
659
Raymond Hettinger66bd2332004-08-02 08:30:07 +0000660 if (!PyArg_ParseTuple(args, "s|O!O:execfile",
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000661 &filename,
Guido van Rossum79f25d91997-04-29 20:08:16 +0000662 &PyDict_Type, &globals,
Raymond Hettinger66bd2332004-08-02 08:30:07 +0000663 &locals))
Guido van Rossum0f61f8a1992-02-25 18:55:05 +0000664 return NULL;
Raymond Hettinger66bd2332004-08-02 08:30:07 +0000665 if (locals != Py_None && !PyMapping_Check(locals)) {
666 PyErr_SetString(PyExc_TypeError, "locals must be a mapping");
667 return NULL;
668 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000669 if (globals == Py_None) {
670 globals = PyEval_GetGlobals();
671 if (locals == Py_None)
672 locals = PyEval_GetLocals();
Guido van Rossum6135a871995-01-09 17:53:26 +0000673 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000674 else if (locals == Py_None)
Guido van Rossum6135a871995-01-09 17:53:26 +0000675 locals = globals;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000676 if (PyDict_GetItemString(globals, "__builtins__") == NULL) {
677 if (PyDict_SetItemString(globals, "__builtins__",
678 PyEval_GetBuiltins()) != 0)
Guido van Rossum6135a871995-01-09 17:53:26 +0000679 return NULL;
680 }
Martin v. Löwis6b3a2c42001-08-08 05:30:36 +0000681
682 exists = 0;
683 /* Test for existence or directory. */
Martin v. Löwis3484a182002-03-09 12:07:51 +0000684#if defined(PLAN9)
685 {
686 Dir *d;
687
688 if ((d = dirstat(filename))!=nil) {
689 if(d->mode & DMDIR)
690 werrstr("is a directory");
691 else
692 exists = 1;
693 free(d);
694 }
Martin v. Löwis6b3a2c42001-08-08 05:30:36 +0000695 }
Martin v. Löwis3484a182002-03-09 12:07:51 +0000696#elif defined(RISCOS)
Guido van Rossume2ae77b2001-10-24 20:42:55 +0000697 if (object_exists(filename)) {
698 if (isdir(filename))
699 errno = EISDIR;
700 else
701 exists = 1;
702 }
Martin v. Löwis3484a182002-03-09 12:07:51 +0000703#else /* standard Posix */
704 {
705 struct stat s;
706 if (stat(filename, &s) == 0) {
707 if (S_ISDIR(s.st_mode))
Andrew MacIntyreda4d6cb2004-03-29 11:53:38 +0000708# if defined(PYOS_OS2) && defined(PYCC_VACPP)
Martin v. Löwis3484a182002-03-09 12:07:51 +0000709 errno = EOS2ERR;
710# else
711 errno = EISDIR;
712# endif
713 else
714 exists = 1;
715 }
716 }
717#endif
Martin v. Löwis6b3a2c42001-08-08 05:30:36 +0000718
719 if (exists) {
720 Py_BEGIN_ALLOW_THREADS
Jack Jansen7b8c7542002-04-14 20:12:41 +0000721 fp = fopen(filename, "r" PY_STDIOTEXTMODE);
Martin v. Löwis6b3a2c42001-08-08 05:30:36 +0000722 Py_END_ALLOW_THREADS
723
724 if (fp == NULL) {
725 exists = 0;
726 }
727 }
728
729 if (!exists) {
Peter Schneider-Kamp4c013422002-08-27 16:58:00 +0000730 PyErr_SetFromErrnoWithFilename(PyExc_IOError, filename);
Guido van Rossum0f61f8a1992-02-25 18:55:05 +0000731 return NULL;
732 }
Tim Peters5ba58662001-07-16 02:29:45 +0000733 cf.cf_flags = 0;
734 if (PyEval_MergeCompilerFlags(&cf))
Tim Peters748b8bb2001-04-28 08:20:22 +0000735 res = PyRun_FileExFlags(fp, filename, Py_file_input, globals,
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000736 locals, 1, &cf);
Tim Peters5ba58662001-07-16 02:29:45 +0000737 else
Tim Peters748b8bb2001-04-28 08:20:22 +0000738 res = PyRun_FileEx(fp, filename, Py_file_input, globals,
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000739 locals, 1);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000740 return res;
Guido van Rossum0f61f8a1992-02-25 18:55:05 +0000741}
742
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000743PyDoc_STRVAR(execfile_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000744"execfile(filename[, globals[, locals]])\n\
745\n\
746Read and execute a Python script from a file.\n\
747The globals and locals are dictionaries, defaulting to the current\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000748globals and locals. If only globals is given, locals defaults to it.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000749
750
Guido van Rossum79f25d91997-04-29 20:08:16 +0000751static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000752builtin_getattr(PyObject *self, PyObject *args)
Guido van Rossum33894be1992-01-27 16:53:09 +0000753{
Guido van Rossum950ff291998-06-29 13:38:57 +0000754 PyObject *v, *result, *dflt = NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000755 PyObject *name;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000756
Raymond Hettingerea3fdf42002-12-29 16:33:45 +0000757 if (!PyArg_UnpackTuple(args, "getattr", 2, 3, &v, &name, &dflt))
Guido van Rossum33894be1992-01-27 16:53:09 +0000758 return NULL;
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000759#ifdef Py_USING_UNICODE
Jeremy Hylton0eb11152001-07-30 22:39:31 +0000760 if (PyUnicode_Check(name)) {
761 name = _PyUnicode_AsDefaultEncodedString(name, NULL);
762 if (name == NULL)
763 return NULL;
764 }
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000765#endif
Jeremy Hylton0eb11152001-07-30 22:39:31 +0000766
767 if (!PyString_Check(name)) {
768 PyErr_SetString(PyExc_TypeError,
Alex Martellia9b9c9f2003-04-23 13:34:35 +0000769 "getattr(): attribute name must be string");
Jeremy Hylton0eb11152001-07-30 22:39:31 +0000770 return NULL;
771 }
Guido van Rossum950ff291998-06-29 13:38:57 +0000772 result = PyObject_GetAttr(v, name);
Guido van Rossumd8923572001-10-16 21:31:32 +0000773 if (result == NULL && dflt != NULL &&
774 PyErr_ExceptionMatches(PyExc_AttributeError))
775 {
Guido van Rossum950ff291998-06-29 13:38:57 +0000776 PyErr_Clear();
777 Py_INCREF(dflt);
778 result = dflt;
779 }
780 return result;
Guido van Rossum9bfef441993-03-29 10:43:31 +0000781}
782
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000783PyDoc_STRVAR(getattr_doc,
Guido van Rossum950ff291998-06-29 13:38:57 +0000784"getattr(object, name[, default]) -> value\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000785\n\
Guido van Rossum950ff291998-06-29 13:38:57 +0000786Get a named attribute from an object; getattr(x, 'y') is equivalent to x.y.\n\
787When a default argument is given, it is returned when the attribute doesn't\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000788exist; without it, an exception is raised in that case.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000789
790
Guido van Rossum79f25d91997-04-29 20:08:16 +0000791static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000792builtin_globals(PyObject *self)
Guido van Rossum872537c1995-07-07 22:43:42 +0000793{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000794 PyObject *d;
Guido van Rossum872537c1995-07-07 22:43:42 +0000795
Guido van Rossum79f25d91997-04-29 20:08:16 +0000796 d = PyEval_GetGlobals();
Neal Norwitz43bd4db2006-08-12 01:46:42 +0000797 Py_XINCREF(d);
Guido van Rossum872537c1995-07-07 22:43:42 +0000798 return d;
799}
800
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000801PyDoc_STRVAR(globals_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000802"globals() -> dictionary\n\
803\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000804Return the dictionary containing the current scope's global variables.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000805
806
Guido van Rossum79f25d91997-04-29 20:08:16 +0000807static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000808builtin_hasattr(PyObject *self, PyObject *args)
Guido van Rossum9bfef441993-03-29 10:43:31 +0000809{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000810 PyObject *v;
811 PyObject *name;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000812
Raymond Hettingerea3fdf42002-12-29 16:33:45 +0000813 if (!PyArg_UnpackTuple(args, "hasattr", 2, 2, &v, &name))
Guido van Rossum9bfef441993-03-29 10:43:31 +0000814 return NULL;
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000815#ifdef Py_USING_UNICODE
Jeremy Hylton302b54a2001-07-30 22:45:19 +0000816 if (PyUnicode_Check(name)) {
817 name = _PyUnicode_AsDefaultEncodedString(name, NULL);
818 if (name == NULL)
819 return NULL;
820 }
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000821#endif
Jeremy Hylton302b54a2001-07-30 22:45:19 +0000822
823 if (!PyString_Check(name)) {
824 PyErr_SetString(PyExc_TypeError,
Alex Martellia9b9c9f2003-04-23 13:34:35 +0000825 "hasattr(): attribute name must be string");
Jeremy Hylton302b54a2001-07-30 22:45:19 +0000826 return NULL;
827 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000828 v = PyObject_GetAttr(v, name);
Guido van Rossum9bfef441993-03-29 10:43:31 +0000829 if (v == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000830 PyErr_Clear();
Guido van Rossum09df08a1998-05-22 00:51:39 +0000831 Py_INCREF(Py_False);
832 return Py_False;
Guido van Rossum9bfef441993-03-29 10:43:31 +0000833 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000834 Py_DECREF(v);
Guido van Rossum09df08a1998-05-22 00:51:39 +0000835 Py_INCREF(Py_True);
836 return Py_True;
Guido van Rossum33894be1992-01-27 16:53:09 +0000837}
838
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000839PyDoc_STRVAR(hasattr_doc,
Guido van Rossum77f6a652002-04-03 22:41:51 +0000840"hasattr(object, name) -> bool\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000841\n\
842Return whether the object has an attribute with the given name.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000843(This is done by calling getattr(object, name) and catching exceptions.)");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000844
845
Guido van Rossum79f25d91997-04-29 20:08:16 +0000846static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000847builtin_id(PyObject *self, PyObject *v)
Guido van Rossum5b722181993-03-30 17:46:03 +0000848{
Guido van Rossum106f2da2000-06-28 21:12:25 +0000849 return PyLong_FromVoidPtr(v);
Guido van Rossum5b722181993-03-30 17:46:03 +0000850}
851
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000852PyDoc_STRVAR(id_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000853"id(object) -> integer\n\
854\n\
855Return the identity of an object. This is guaranteed to be unique among\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000856simultaneously existing objects. (Hint: it's the object's memory address.)");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000857
858
Guido van Rossum79f25d91997-04-29 20:08:16 +0000859static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000860builtin_map(PyObject *self, PyObject *args)
Guido van Rossum12d12c51993-10-26 17:58:25 +0000861{
862 typedef struct {
Tim Peters4e9afdc2001-05-03 23:54:49 +0000863 PyObject *it; /* the iterator object */
864 int saw_StopIteration; /* bool: did the iterator end? */
Guido van Rossum12d12c51993-10-26 17:58:25 +0000865 } sequence;
866
Guido van Rossum79f25d91997-04-29 20:08:16 +0000867 PyObject *func, *result;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000868 sequence *seqs = NULL, *sqp;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000869 Py_ssize_t n, len;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000870 register int i, j;
871
Guido van Rossum79f25d91997-04-29 20:08:16 +0000872 n = PyTuple_Size(args);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000873 if (n < 2) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000874 PyErr_SetString(PyExc_TypeError,
875 "map() requires at least two args");
Guido van Rossum12d12c51993-10-26 17:58:25 +0000876 return NULL;
877 }
878
Guido van Rossum79f25d91997-04-29 20:08:16 +0000879 func = PyTuple_GetItem(args, 0);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000880 n--;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000881
Guido van Rossumfa4ac711998-07-10 17:37:30 +0000882 if (func == Py_None && n == 1) {
883 /* map(None, S) is the same as list(S). */
884 return PySequence_List(PyTuple_GetItem(args, 1));
885 }
886
Tim Peters4e9afdc2001-05-03 23:54:49 +0000887 /* Get space for sequence descriptors. Must NULL out the iterator
888 * pointers so that jumping to Fail_2 later doesn't see trash.
889 */
Guido van Rossum79f25d91997-04-29 20:08:16 +0000890 if ((seqs = PyMem_NEW(sequence, n)) == NULL) {
891 PyErr_NoMemory();
Tim Peters4e9afdc2001-05-03 23:54:49 +0000892 return NULL;
893 }
894 for (i = 0; i < n; ++i) {
895 seqs[i].it = (PyObject*)NULL;
896 seqs[i].saw_StopIteration = 0;
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000897 }
Guido van Rossum12d12c51993-10-26 17:58:25 +0000898
Tim Peters4e9afdc2001-05-03 23:54:49 +0000899 /* Do a first pass to obtain iterators for the arguments, and set len
900 * to the largest of their lengths.
Tim Peters748b8bb2001-04-28 08:20:22 +0000901 */
Tim Peters4e9afdc2001-05-03 23:54:49 +0000902 len = 0;
903 for (i = 0, sqp = seqs; i < n; ++i, ++sqp) {
904 PyObject *curseq;
Martin v. Löwisd96ee902006-02-16 14:37:16 +0000905 Py_ssize_t curlen;
Guido van Rossumad991772001-01-12 16:03:05 +0000906
Tim Peters4e9afdc2001-05-03 23:54:49 +0000907 /* Get iterator. */
908 curseq = PyTuple_GetItem(args, i+1);
909 sqp->it = PyObject_GetIter(curseq);
910 if (sqp->it == NULL) {
Guido van Rossum12d12c51993-10-26 17:58:25 +0000911 static char errmsg[] =
Tim Peters4e9afdc2001-05-03 23:54:49 +0000912 "argument %d to map() must support iteration";
Guido van Rossum15e33a41997-04-30 19:00:27 +0000913 char errbuf[sizeof(errmsg) + 25];
Jeremy Hylton518ab1c2001-11-28 20:42:20 +0000914 PyOS_snprintf(errbuf, sizeof(errbuf), errmsg, i+2);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000915 PyErr_SetString(PyExc_TypeError, errbuf);
Guido van Rossum12d12c51993-10-26 17:58:25 +0000916 goto Fail_2;
917 }
918
Tim Peters4e9afdc2001-05-03 23:54:49 +0000919 /* Update len. */
Raymond Hettinger4e2f7142007-12-06 00:56:53 +0000920 curlen = _PyObject_LengthHint(curseq, 8);
Raymond Hettinger77f3c872004-01-04 08:54:44 +0000921 if (curlen > len)
922 len = curlen;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000923 }
924
Tim Peters4e9afdc2001-05-03 23:54:49 +0000925 /* Get space for the result list. */
Guido van Rossum79f25d91997-04-29 20:08:16 +0000926 if ((result = (PyObject *) PyList_New(len)) == NULL)
Guido van Rossum12d12c51993-10-26 17:58:25 +0000927 goto Fail_2;
928
Tim Peters4e9afdc2001-05-03 23:54:49 +0000929 /* Iterate over the sequences until all have stopped. */
Guido van Rossum2d951851994-08-29 12:52:16 +0000930 for (i = 0; ; ++i) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000931 PyObject *alist, *item=NULL, *value;
Tim Peters4e9afdc2001-05-03 23:54:49 +0000932 int numactive = 0;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000933
Guido van Rossum79f25d91997-04-29 20:08:16 +0000934 if (func == Py_None && n == 1)
Guido van Rossum32120311995-07-10 13:52:21 +0000935 alist = NULL;
Tim Peters4e9afdc2001-05-03 23:54:49 +0000936 else if ((alist = PyTuple_New(n)) == NULL)
937 goto Fail_1;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000938
939 for (j = 0, sqp = seqs; j < n; ++j, ++sqp) {
Tim Peters4e9afdc2001-05-03 23:54:49 +0000940 if (sqp->saw_StopIteration) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000941 Py_INCREF(Py_None);
942 item = Py_None;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000943 }
Guido van Rossum12d12c51993-10-26 17:58:25 +0000944 else {
Tim Peters4e9afdc2001-05-03 23:54:49 +0000945 item = PyIter_Next(sqp->it);
946 if (item)
947 ++numactive;
948 else {
Tim Peters4e9afdc2001-05-03 23:54:49 +0000949 if (PyErr_Occurred()) {
Tim Petersf4848da2001-05-05 00:14:56 +0000950 Py_XDECREF(alist);
951 goto Fail_1;
Guido van Rossum2d951851994-08-29 12:52:16 +0000952 }
Tim Peters4e9afdc2001-05-03 23:54:49 +0000953 Py_INCREF(Py_None);
954 item = Py_None;
955 sqp->saw_StopIteration = 1;
Guido van Rossum2d951851994-08-29 12:52:16 +0000956 }
Guido van Rossum12d12c51993-10-26 17:58:25 +0000957 }
Tim Peters4e9afdc2001-05-03 23:54:49 +0000958 if (alist)
959 PyTuple_SET_ITEM(alist, j, item);
960 else
Guido van Rossum2d951851994-08-29 12:52:16 +0000961 break;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000962 }
963
Guido van Rossum32120311995-07-10 13:52:21 +0000964 if (!alist)
965 alist = item;
Guido van Rossum2d951851994-08-29 12:52:16 +0000966
Tim Peters4e9afdc2001-05-03 23:54:49 +0000967 if (numactive == 0) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000968 Py_DECREF(alist);
Guido van Rossum2d951851994-08-29 12:52:16 +0000969 break;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000970 }
Guido van Rossum2d951851994-08-29 12:52:16 +0000971
Guido van Rossum79f25d91997-04-29 20:08:16 +0000972 if (func == Py_None)
Guido van Rossum32120311995-07-10 13:52:21 +0000973 value = alist;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000974 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000975 value = PyEval_CallObject(func, alist);
976 Py_DECREF(alist);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +0000977 if (value == NULL)
978 goto Fail_1;
Guido van Rossum2d951851994-08-29 12:52:16 +0000979 }
980 if (i >= len) {
Barry Warsawfa77e091999-01-28 18:49:12 +0000981 int status = PyList_Append(result, value);
Barry Warsaw21332871999-01-28 04:21:35 +0000982 Py_DECREF(value);
Barry Warsawfa77e091999-01-28 18:49:12 +0000983 if (status < 0)
984 goto Fail_1;
Guido van Rossum2d951851994-08-29 12:52:16 +0000985 }
Tim Peters4e9afdc2001-05-03 23:54:49 +0000986 else if (PyList_SetItem(result, i, value) < 0)
987 goto Fail_1;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000988 }
989
Guido van Rossumfa4ac711998-07-10 17:37:30 +0000990 if (i < len && PyList_SetSlice(result, i, len, NULL) < 0)
991 goto Fail_1;
992
Tim Peters4e9afdc2001-05-03 23:54:49 +0000993 goto Succeed;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000994
Guido van Rossum12d12c51993-10-26 17:58:25 +0000995Fail_1:
Guido van Rossum79f25d91997-04-29 20:08:16 +0000996 Py_DECREF(result);
Guido van Rossum12d12c51993-10-26 17:58:25 +0000997Fail_2:
Tim Peters4e9afdc2001-05-03 23:54:49 +0000998 result = NULL;
999Succeed:
1000 assert(seqs);
1001 for (i = 0; i < n; ++i)
1002 Py_XDECREF(seqs[i].it);
1003 PyMem_DEL(seqs);
1004 return result;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001005}
1006
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001007PyDoc_STRVAR(map_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001008"map(function, sequence[, sequence, ...]) -> list\n\
1009\n\
1010Return a list of the results of applying the function to the items of\n\
1011the argument sequence(s). If more than one sequence is given, the\n\
1012function is called with an argument list consisting of the corresponding\n\
1013item of each sequence, substituting None for missing values when not all\n\
1014sequences have the same length. If the function is None, return a list of\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001015the items of the sequence (or a list of tuples if more than one sequence).");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001016
1017
Guido van Rossum79f25d91997-04-29 20:08:16 +00001018static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001019builtin_setattr(PyObject *self, PyObject *args)
Guido van Rossum33894be1992-01-27 16:53:09 +00001020{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001021 PyObject *v;
1022 PyObject *name;
1023 PyObject *value;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001024
Raymond Hettingerea3fdf42002-12-29 16:33:45 +00001025 if (!PyArg_UnpackTuple(args, "setattr", 3, 3, &v, &name, &value))
Guido van Rossum33894be1992-01-27 16:53:09 +00001026 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001027 if (PyObject_SetAttr(v, name, value) != 0)
Guido van Rossum33894be1992-01-27 16:53:09 +00001028 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001029 Py_INCREF(Py_None);
1030 return Py_None;
Guido van Rossum33894be1992-01-27 16:53:09 +00001031}
1032
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001033PyDoc_STRVAR(setattr_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001034"setattr(object, name, value)\n\
1035\n\
1036Set a named attribute on an object; setattr(x, 'y', v) is equivalent to\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001037``x.y = v''.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001038
1039
Guido van Rossum79f25d91997-04-29 20:08:16 +00001040static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001041builtin_delattr(PyObject *self, PyObject *args)
Guido van Rossum14144fc1994-08-29 12:53:40 +00001042{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001043 PyObject *v;
1044 PyObject *name;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001045
Raymond Hettingerea3fdf42002-12-29 16:33:45 +00001046 if (!PyArg_UnpackTuple(args, "delattr", 2, 2, &v, &name))
Guido van Rossum14144fc1994-08-29 12:53:40 +00001047 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001048 if (PyObject_SetAttr(v, name, (PyObject *)NULL) != 0)
Guido van Rossum14144fc1994-08-29 12:53:40 +00001049 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001050 Py_INCREF(Py_None);
1051 return Py_None;
Guido van Rossum14144fc1994-08-29 12:53:40 +00001052}
1053
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001054PyDoc_STRVAR(delattr_doc,
Guido van Rossumdf12a591998-11-23 22:13:04 +00001055"delattr(object, name)\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001056\n\
1057Delete a named attribute on an object; delattr(x, 'y') is equivalent to\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001058``del x.y''.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001059
1060
Guido van Rossum79f25d91997-04-29 20:08:16 +00001061static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001062builtin_hash(PyObject *self, PyObject *v)
Guido van Rossum9bfef441993-03-29 10:43:31 +00001063{
Guido van Rossum9bfef441993-03-29 10:43:31 +00001064 long x;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001065
Guido van Rossum79f25d91997-04-29 20:08:16 +00001066 x = PyObject_Hash(v);
Guido van Rossum9bfef441993-03-29 10:43:31 +00001067 if (x == -1)
1068 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001069 return PyInt_FromLong(x);
Guido van Rossum9bfef441993-03-29 10:43:31 +00001070}
1071
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001072PyDoc_STRVAR(hash_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001073"hash(object) -> integer\n\
1074\n\
1075Return a hash value for the object. Two objects with the same value have\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001076the same hash value. The reverse is not necessarily true, but likely.");
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_hex(PyObject *self, PyObject *v)
Guido van Rossum006bcd41991-10-24 14:54:44 +00001081{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001082 PyNumberMethods *nb;
Neil Schemenauer3a313e32004-07-19 16:29:17 +00001083 PyObject *res;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001084
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001085 if ((nb = v->ob_type->tp_as_number) == NULL ||
Guido van Rossum1899c2e1992-09-12 11:09:23 +00001086 nb->nb_hex == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001087 PyErr_SetString(PyExc_TypeError,
Guido van Rossum1899c2e1992-09-12 11:09:23 +00001088 "hex() argument can't be converted to hex");
1089 return NULL;
Guido van Rossum006bcd41991-10-24 14:54:44 +00001090 }
Neil Schemenauer3a313e32004-07-19 16:29:17 +00001091 res = (*nb->nb_hex)(v);
1092 if (res && !PyString_Check(res)) {
1093 PyErr_Format(PyExc_TypeError,
1094 "__hex__ returned non-string (type %.200s)",
1095 res->ob_type->tp_name);
1096 Py_DECREF(res);
1097 return NULL;
1098 }
1099 return res;
Guido van Rossum006bcd41991-10-24 14:54:44 +00001100}
1101
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001102PyDoc_STRVAR(hex_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001103"hex(number) -> string\n\
1104\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001105Return the hexadecimal representation of an integer or long integer.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001106
1107
Tim Petersdbd9ba62000-07-09 03:09:57 +00001108static PyObject *builtin_raw_input(PyObject *, PyObject *);
Guido van Rossum3165fe61992-09-25 21:59:05 +00001109
Guido van Rossum79f25d91997-04-29 20:08:16 +00001110static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001111builtin_input(PyObject *self, PyObject *args)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001112{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001113 PyObject *line;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001114 char *str;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001115 PyObject *res;
1116 PyObject *globals, *locals;
Hye-Shik Changff83c2b2004-02-02 13:39:01 +00001117 PyCompilerFlags cf;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001118
1119 line = builtin_raw_input(self, args);
Guido van Rossum3165fe61992-09-25 21:59:05 +00001120 if (line == NULL)
1121 return line;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001122 if (!PyArg_Parse(line, "s;embedded '\\0' in input line", &str))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001123 return NULL;
1124 while (*str == ' ' || *str == '\t')
1125 str++;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001126 globals = PyEval_GetGlobals();
1127 locals = PyEval_GetLocals();
1128 if (PyDict_GetItemString(globals, "__builtins__") == NULL) {
1129 if (PyDict_SetItemString(globals, "__builtins__",
1130 PyEval_GetBuiltins()) != 0)
Guido van Rossum6135a871995-01-09 17:53:26 +00001131 return NULL;
1132 }
Hye-Shik Changff83c2b2004-02-02 13:39:01 +00001133 cf.cf_flags = 0;
1134 PyEval_MergeCompilerFlags(&cf);
1135 res = PyRun_StringFlags(str, Py_eval_input, globals, locals, &cf);
Guido van Rossum79f25d91997-04-29 20:08:16 +00001136 Py_DECREF(line);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001137 return res;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001138}
1139
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001140PyDoc_STRVAR(input_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001141"input([prompt]) -> value\n\
1142\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001143Equivalent to eval(raw_input(prompt)).");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001144
1145
Guido van Rossume8811f81997-02-14 15:48:05 +00001146static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001147builtin_intern(PyObject *self, PyObject *args)
Guido van Rossume8811f81997-02-14 15:48:05 +00001148{
1149 PyObject *s;
Guido van Rossum43713e52000-02-29 13:59:29 +00001150 if (!PyArg_ParseTuple(args, "S:intern", &s))
Guido van Rossume8811f81997-02-14 15:48:05 +00001151 return NULL;
Jeremy Hylton4c989dd2004-08-07 19:20:05 +00001152 if (!PyString_CheckExact(s)) {
1153 PyErr_SetString(PyExc_TypeError,
1154 "can't intern subclass of string");
1155 return NULL;
1156 }
Guido van Rossume8811f81997-02-14 15:48:05 +00001157 Py_INCREF(s);
1158 PyString_InternInPlace(&s);
1159 return s;
1160}
1161
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001162PyDoc_STRVAR(intern_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001163"intern(string) -> string\n\
1164\n\
1165``Intern'' the given string. This enters the string in the (global)\n\
1166table of interned strings whose purpose is to speed up dictionary lookups.\n\
1167Return the string itself or the previously interned string object with the\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001168same value.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001169
1170
Guido van Rossum79f25d91997-04-29 20:08:16 +00001171static PyObject *
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001172builtin_iter(PyObject *self, PyObject *args)
1173{
1174 PyObject *v, *w = NULL;
1175
Raymond Hettingerea3fdf42002-12-29 16:33:45 +00001176 if (!PyArg_UnpackTuple(args, "iter", 1, 2, &v, &w))
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001177 return NULL;
1178 if (w == NULL)
1179 return PyObject_GetIter(v);
1180 if (!PyCallable_Check(v)) {
1181 PyErr_SetString(PyExc_TypeError,
1182 "iter(v, w): v must be callable");
1183 return NULL;
1184 }
1185 return PyCallIter_New(v, w);
1186}
1187
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001188PyDoc_STRVAR(iter_doc,
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001189"iter(collection) -> iterator\n\
1190iter(callable, sentinel) -> iterator\n\
1191\n\
1192Get an iterator from an object. In the first form, the argument must\n\
1193supply its own iterator, or be a sequence.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001194In the second form, the callable is called until it returns the sentinel.");
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001195
1196
1197static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001198builtin_len(PyObject *self, PyObject *v)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001199{
Martin v. Löwis18e16552006-02-15 17:27:45 +00001200 Py_ssize_t res;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001201
Jeremy Hylton03657cf2000-07-12 13:05:33 +00001202 res = PyObject_Size(v);
Guido van Rossum8ea9f4d1998-06-29 22:26:50 +00001203 if (res < 0 && PyErr_Occurred())
1204 return NULL;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001205 return PyInt_FromSsize_t(res);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001206}
1207
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001208PyDoc_STRVAR(len_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001209"len(object) -> integer\n\
1210\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001211Return the number of items of a sequence or mapping.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001212
1213
Guido van Rossum79f25d91997-04-29 20:08:16 +00001214static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001215builtin_locals(PyObject *self)
Guido van Rossum872537c1995-07-07 22:43:42 +00001216{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001217 PyObject *d;
Guido van Rossum872537c1995-07-07 22:43:42 +00001218
Guido van Rossum79f25d91997-04-29 20:08:16 +00001219 d = PyEval_GetLocals();
Neal Norwitz43bd4db2006-08-12 01:46:42 +00001220 Py_XINCREF(d);
Guido van Rossum872537c1995-07-07 22:43:42 +00001221 return d;
1222}
1223
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001224PyDoc_STRVAR(locals_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001225"locals() -> dictionary\n\
1226\n\
Raymond Hettinger69bf8f32003-01-04 02:16:22 +00001227Update and return a dictionary containing the current scope's local variables.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001228
1229
Guido van Rossum79f25d91997-04-29 20:08:16 +00001230static PyObject *
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001231min_max(PyObject *args, PyObject *kwds, int op)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001232{
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001233 PyObject *v, *it, *item, *val, *maxitem, *maxval, *keyfunc=NULL;
Martin v. Löwisfd39ad42004-08-12 14:42:37 +00001234 const char *name = op == Py_LT ? "min" : "max";
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001235
Guido van Rossum79f25d91997-04-29 20:08:16 +00001236 if (PyTuple_Size(args) > 1)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001237 v = args;
Martin v. Löwisfd39ad42004-08-12 14:42:37 +00001238 else if (!PyArg_UnpackTuple(args, (char *)name, 1, 1, &v))
Guido van Rossum3f5da241990-12-20 15:06:42 +00001239 return NULL;
Tim Peters67d687a2002-04-29 21:27:32 +00001240
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001241 if (kwds != NULL && PyDict_Check(kwds) && PyDict_Size(kwds)) {
1242 keyfunc = PyDict_GetItemString(kwds, "key");
1243 if (PyDict_Size(kwds)!=1 || keyfunc == NULL) {
Georg Brandl99d7e4e2005-08-31 22:21:15 +00001244 PyErr_Format(PyExc_TypeError,
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001245 "%s() got an unexpected keyword argument", name);
1246 return NULL;
1247 }
Georg Brandl99d7e4e2005-08-31 22:21:15 +00001248 }
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001249
Tim Petersc3074532001-05-03 07:00:32 +00001250 it = PyObject_GetIter(v);
1251 if (it == NULL)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001252 return NULL;
Tim Petersc3074532001-05-03 07:00:32 +00001253
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001254 maxitem = NULL; /* the result */
1255 maxval = NULL; /* the value associated with the result */
Brett Cannon9e635cf2004-12-07 00:25:35 +00001256 while (( item = PyIter_Next(it) )) {
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001257 /* get the value from the key function */
1258 if (keyfunc != NULL) {
1259 val = PyObject_CallFunctionObjArgs(keyfunc, item, NULL);
1260 if (val == NULL)
1261 goto Fail_it_item;
1262 }
1263 /* no key function; the value is the item */
1264 else {
1265 val = item;
1266 Py_INCREF(val);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001267 }
Tim Petersc3074532001-05-03 07:00:32 +00001268
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001269 /* maximum value and item are unset; set them */
1270 if (maxval == NULL) {
1271 maxitem = item;
1272 maxval = val;
1273 }
1274 /* maximum value and item are set; update them as necessary */
Guido van Rossum2d951851994-08-29 12:52:16 +00001275 else {
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001276 int cmp = PyObject_RichCompareBool(val, maxval, op);
1277 if (cmp < 0)
1278 goto Fail_it_item_and_val;
1279 else if (cmp > 0) {
1280 Py_DECREF(maxval);
1281 Py_DECREF(maxitem);
1282 maxval = val;
1283 maxitem = item;
Guido van Rossum53451b32001-01-17 15:47:24 +00001284 }
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001285 else {
1286 Py_DECREF(item);
1287 Py_DECREF(val);
Guido van Rossumc8b6df91997-05-23 00:06:51 +00001288 }
Guido van Rossum2d951851994-08-29 12:52:16 +00001289 }
Guido van Rossum3f5da241990-12-20 15:06:42 +00001290 }
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001291 if (PyErr_Occurred())
1292 goto Fail_it;
1293 if (maxval == NULL) {
Martin v. Löwisfd39ad42004-08-12 14:42:37 +00001294 PyErr_Format(PyExc_ValueError,
1295 "%s() arg is an empty sequence", name);
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001296 assert(maxitem == NULL);
1297 }
1298 else
1299 Py_DECREF(maxval);
Tim Petersc3074532001-05-03 07:00:32 +00001300 Py_DECREF(it);
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001301 return maxitem;
1302
1303Fail_it_item_and_val:
1304 Py_DECREF(val);
1305Fail_it_item:
1306 Py_DECREF(item);
1307Fail_it:
1308 Py_XDECREF(maxval);
1309 Py_XDECREF(maxitem);
1310 Py_DECREF(it);
1311 return NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001312}
1313
Guido van Rossum79f25d91997-04-29 20:08:16 +00001314static PyObject *
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001315builtin_min(PyObject *self, PyObject *args, PyObject *kwds)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001316{
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001317 return min_max(args, kwds, Py_LT);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001318}
1319
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001320PyDoc_STRVAR(min_doc,
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001321"min(iterable[, key=func]) -> value\n\
1322min(a, b, c, ...[, key=func]) -> value\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001323\n\
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001324With a single iterable argument, return its smallest item.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001325With two or more arguments, return the smallest argument.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001326
1327
Guido van Rossum79f25d91997-04-29 20:08:16 +00001328static PyObject *
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001329builtin_max(PyObject *self, PyObject *args, PyObject *kwds)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001330{
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001331 return min_max(args, kwds, Py_GT);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001332}
1333
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001334PyDoc_STRVAR(max_doc,
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001335"max(iterable[, key=func]) -> value\n\
1336max(a, b, c, ...[, key=func]) -> value\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001337\n\
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001338With a single iterable argument, return its largest item.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001339With two or more arguments, return the largest argument.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001340
1341
Guido van Rossum79f25d91997-04-29 20:08:16 +00001342static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001343builtin_oct(PyObject *self, PyObject *v)
Guido van Rossum006bcd41991-10-24 14:54:44 +00001344{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001345 PyNumberMethods *nb;
Neil Schemenauer3a313e32004-07-19 16:29:17 +00001346 PyObject *res;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001347
Guido van Rossum1899c2e1992-09-12 11:09:23 +00001348 if (v == NULL || (nb = v->ob_type->tp_as_number) == NULL ||
1349 nb->nb_oct == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001350 PyErr_SetString(PyExc_TypeError,
Guido van Rossum1899c2e1992-09-12 11:09:23 +00001351 "oct() argument can't be converted to oct");
1352 return NULL;
Guido van Rossum006bcd41991-10-24 14:54:44 +00001353 }
Neil Schemenauer3a313e32004-07-19 16:29:17 +00001354 res = (*nb->nb_oct)(v);
1355 if (res && !PyString_Check(res)) {
1356 PyErr_Format(PyExc_TypeError,
1357 "__oct__ returned non-string (type %.200s)",
1358 res->ob_type->tp_name);
1359 Py_DECREF(res);
1360 return NULL;
1361 }
1362 return res;
Guido van Rossum006bcd41991-10-24 14:54:44 +00001363}
1364
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001365PyDoc_STRVAR(oct_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001366"oct(number) -> string\n\
1367\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001368Return the octal representation of an integer or long integer.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001369
1370
Guido van Rossum79f25d91997-04-29 20:08:16 +00001371static PyObject *
Neal Norwitzc4edb0e2006-05-02 04:43:14 +00001372builtin_open(PyObject *self, PyObject *args, PyObject *kwds)
1373{
1374 return PyObject_Call((PyObject*)&PyFile_Type, args, kwds);
1375}
1376
1377PyDoc_STRVAR(open_doc,
1378"open(name[, mode[, buffering]]) -> file object\n\
1379\n\
Skip Montanaro4e3ebe02007-12-08 14:37:43 +00001380Open a file using the file() type, returns a file object. This is the\n\
1381preferred way to open a file.");
Neal Norwitzc4edb0e2006-05-02 04:43:14 +00001382
1383
1384static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001385builtin_ord(PyObject *self, PyObject* obj)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001386{
Guido van Rossum09095f32000-03-10 23:00:52 +00001387 long ord;
Martin v. Löwisd96ee902006-02-16 14:37:16 +00001388 Py_ssize_t size;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001389
Jeremy Hylton394b54d2000-04-12 21:19:47 +00001390 if (PyString_Check(obj)) {
1391 size = PyString_GET_SIZE(obj);
Andrew M. Kuchling34c20cf2000-12-20 14:36:56 +00001392 if (size == 1) {
Jeremy Hylton394b54d2000-04-12 21:19:47 +00001393 ord = (long)((unsigned char)*PyString_AS_STRING(obj));
Andrew M. Kuchling34c20cf2000-12-20 14:36:56 +00001394 return PyInt_FromLong(ord);
1395 }
Martin v. Löwis339d0f72001-08-17 18:39:25 +00001396#ifdef Py_USING_UNICODE
Jeremy Hylton394b54d2000-04-12 21:19:47 +00001397 } else if (PyUnicode_Check(obj)) {
1398 size = PyUnicode_GET_SIZE(obj);
Andrew M. Kuchling34c20cf2000-12-20 14:36:56 +00001399 if (size == 1) {
Jeremy Hylton394b54d2000-04-12 21:19:47 +00001400 ord = (long)*PyUnicode_AS_UNICODE(obj);
Andrew M. Kuchling34c20cf2000-12-20 14:36:56 +00001401 return PyInt_FromLong(ord);
1402 }
Martin v. Löwis339d0f72001-08-17 18:39:25 +00001403#endif
Jeremy Hylton394b54d2000-04-12 21:19:47 +00001404 } else {
1405 PyErr_Format(PyExc_TypeError,
Andrew M. Kuchlingf07aad12000-12-23 14:11:28 +00001406 "ord() expected string of length 1, but " \
Jeremy Hylton394b54d2000-04-12 21:19:47 +00001407 "%.200s found", obj->ob_type->tp_name);
Guido van Rossum09095f32000-03-10 23:00:52 +00001408 return NULL;
1409 }
1410
Guido van Rossumad991772001-01-12 16:03:05 +00001411 PyErr_Format(PyExc_TypeError,
Andrew M. Kuchlingf07aad12000-12-23 14:11:28 +00001412 "ord() expected a character, "
Martin v. Löwisd96ee902006-02-16 14:37:16 +00001413 "but string of length %zd found",
Jeremy Hylton394b54d2000-04-12 21:19:47 +00001414 size);
1415 return NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001416}
1417
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001418PyDoc_STRVAR(ord_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001419"ord(c) -> integer\n\
1420\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001421Return the integer ordinal of a one-character string.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001422
1423
Guido van Rossum79f25d91997-04-29 20:08:16 +00001424static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001425builtin_pow(PyObject *self, PyObject *args)
Guido van Rossum6a00cd81995-01-07 12:39:01 +00001426{
Guido van Rossum09df08a1998-05-22 00:51:39 +00001427 PyObject *v, *w, *z = Py_None;
Guido van Rossum6a00cd81995-01-07 12:39:01 +00001428
Raymond Hettingerea3fdf42002-12-29 16:33:45 +00001429 if (!PyArg_UnpackTuple(args, "pow", 2, 3, &v, &w, &z))
Guido van Rossum6a00cd81995-01-07 12:39:01 +00001430 return NULL;
Guido van Rossum09df08a1998-05-22 00:51:39 +00001431 return PyNumber_Power(v, w, z);
Guido van Rossumd4905451991-05-05 20:00:36 +00001432}
1433
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001434PyDoc_STRVAR(pow_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001435"pow(x, y[, z]) -> number\n\
1436\n\
1437With two arguments, equivalent to x**y. With three arguments,\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001438equivalent to (x**y) % z, but may be more efficient (e.g. for longs).");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001439
1440
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001441
1442/* Return number of items in range (lo, hi, step), when arguments are
1443 * PyInt or PyLong objects. step > 0 required. Return a value < 0 if
1444 * & only if the true value is too large to fit in a signed long.
1445 * Arguments MUST return 1 with either PyInt_Check() or
1446 * PyLong_Check(). Return -1 when there is an error.
1447 */
1448static long
1449get_len_of_range_longs(PyObject *lo, PyObject *hi, PyObject *step)
1450{
1451 /* -------------------------------------------------------------
1452 Algorithm is equal to that of get_len_of_range(), but it operates
1453 on PyObjects (which are assumed to be PyLong or PyInt objects).
1454 ---------------------------------------------------------------*/
1455 long n;
1456 PyObject *diff = NULL;
1457 PyObject *one = NULL;
1458 PyObject *tmp1 = NULL, *tmp2 = NULL, *tmp3 = NULL;
1459 /* holds sub-expression evaluations */
1460
1461 /* if (lo >= hi), return length of 0. */
1462 if (PyObject_Compare(lo, hi) >= 0)
1463 return 0;
1464
1465 if ((one = PyLong_FromLong(1L)) == NULL)
1466 goto Fail;
1467
1468 if ((tmp1 = PyNumber_Subtract(hi, lo)) == NULL)
1469 goto Fail;
1470
1471 if ((diff = PyNumber_Subtract(tmp1, one)) == NULL)
1472 goto Fail;
1473
1474 if ((tmp2 = PyNumber_FloorDivide(diff, step)) == NULL)
1475 goto Fail;
1476
1477 if ((tmp3 = PyNumber_Add(tmp2, one)) == NULL)
1478 goto Fail;
1479
1480 n = PyLong_AsLong(tmp3);
1481 if (PyErr_Occurred()) { /* Check for Overflow */
1482 PyErr_Clear();
1483 goto Fail;
1484 }
1485
1486 Py_DECREF(tmp3);
1487 Py_DECREF(tmp2);
1488 Py_DECREF(diff);
1489 Py_DECREF(tmp1);
1490 Py_DECREF(one);
1491 return n;
1492
1493 Fail:
1494 Py_XDECREF(tmp3);
1495 Py_XDECREF(tmp2);
1496 Py_XDECREF(diff);
1497 Py_XDECREF(tmp1);
1498 Py_XDECREF(one);
1499 return -1;
1500}
1501
1502/* An extension of builtin_range() that handles the case when PyLong
1503 * arguments are given. */
1504static PyObject *
1505handle_range_longs(PyObject *self, PyObject *args)
1506{
1507 PyObject *ilow;
Tim Peters874e1f72003-04-13 22:13:08 +00001508 PyObject *ihigh = NULL;
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001509 PyObject *istep = NULL;
Tim Peters874e1f72003-04-13 22:13:08 +00001510
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001511 PyObject *curnum = NULL;
1512 PyObject *v = NULL;
1513 long bign;
1514 int i, n;
1515 int cmp_result;
1516
Tim Peters874e1f72003-04-13 22:13:08 +00001517 PyObject *zero = PyLong_FromLong(0);
1518
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001519 if (zero == NULL)
1520 return NULL;
1521
Tim Peters874e1f72003-04-13 22:13:08 +00001522 if (!PyArg_UnpackTuple(args, "range", 1, 3, &ilow, &ihigh, &istep)) {
1523 Py_DECREF(zero);
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001524 return NULL;
1525 }
1526
Tim Peters874e1f72003-04-13 22:13:08 +00001527 /* Figure out which way we were called, supply defaults, and be
1528 * sure to incref everything so that the decrefs at the end
1529 * are correct.
1530 */
1531 assert(ilow != NULL);
1532 if (ihigh == NULL) {
1533 /* only 1 arg -- it's the upper limit */
1534 ihigh = ilow;
1535 ilow = NULL;
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001536 }
Tim Peters874e1f72003-04-13 22:13:08 +00001537 assert(ihigh != NULL);
1538 Py_INCREF(ihigh);
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001539
Tim Peters874e1f72003-04-13 22:13:08 +00001540 /* ihigh correct now; do ilow */
1541 if (ilow == NULL)
1542 ilow = zero;
1543 Py_INCREF(ilow);
1544
1545 /* ilow and ihigh correct now; do istep */
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001546 if (istep == NULL) {
1547 istep = PyLong_FromLong(1L);
1548 if (istep == NULL)
1549 goto Fail;
1550 }
1551 else {
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001552 Py_INCREF(istep);
1553 }
1554
Tim Peters874e1f72003-04-13 22:13:08 +00001555 if (!PyInt_Check(ilow) && !PyLong_Check(ilow)) {
Guido van Rossum28e83e32003-04-15 12:43:26 +00001556 PyErr_Format(PyExc_TypeError,
Alex Martellif471d472003-04-23 13:00:44 +00001557 "range() integer start argument expected, got %s.",
Guido van Rossum817d6c92003-04-14 18:25:04 +00001558 ilow->ob_type->tp_name);
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001559 goto Fail;
1560 }
1561
Tim Peters874e1f72003-04-13 22:13:08 +00001562 if (!PyInt_Check(ihigh) && !PyLong_Check(ihigh)) {
Guido van Rossum28e83e32003-04-15 12:43:26 +00001563 PyErr_Format(PyExc_TypeError,
Alex Martellif471d472003-04-23 13:00:44 +00001564 "range() integer end argument expected, got %s.",
Guido van Rossum817d6c92003-04-14 18:25:04 +00001565 ihigh->ob_type->tp_name);
Tim Peters874e1f72003-04-13 22:13:08 +00001566 goto Fail;
1567 }
1568
1569 if (!PyInt_Check(istep) && !PyLong_Check(istep)) {
Guido van Rossum28e83e32003-04-15 12:43:26 +00001570 PyErr_Format(PyExc_TypeError,
Alex Martellif471d472003-04-23 13:00:44 +00001571 "range() integer step argument expected, got %s.",
Guido van Rossum817d6c92003-04-14 18:25:04 +00001572 istep->ob_type->tp_name);
Tim Peters874e1f72003-04-13 22:13:08 +00001573 goto Fail;
1574 }
1575
1576 if (PyObject_Cmp(istep, zero, &cmp_result) == -1)
1577 goto Fail;
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001578 if (cmp_result == 0) {
1579 PyErr_SetString(PyExc_ValueError,
Alex Martellif471d472003-04-23 13:00:44 +00001580 "range() step argument must not be zero");
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001581 goto Fail;
1582 }
1583
1584 if (cmp_result > 0)
1585 bign = get_len_of_range_longs(ilow, ihigh, istep);
1586 else {
1587 PyObject *neg_istep = PyNumber_Negative(istep);
1588 if (neg_istep == NULL)
1589 goto Fail;
1590 bign = get_len_of_range_longs(ihigh, ilow, neg_istep);
1591 Py_DECREF(neg_istep);
1592 }
1593
1594 n = (int)bign;
1595 if (bign < 0 || (long)n != bign) {
1596 PyErr_SetString(PyExc_OverflowError,
1597 "range() result has too many items");
1598 goto Fail;
1599 }
1600
1601 v = PyList_New(n);
1602 if (v == NULL)
1603 goto Fail;
1604
1605 curnum = ilow;
1606 Py_INCREF(curnum);
1607
1608 for (i = 0; i < n; i++) {
1609 PyObject *w = PyNumber_Long(curnum);
1610 PyObject *tmp_num;
1611 if (w == NULL)
1612 goto Fail;
1613
1614 PyList_SET_ITEM(v, i, w);
1615
1616 tmp_num = PyNumber_Add(curnum, istep);
1617 if (tmp_num == NULL)
1618 goto Fail;
1619
1620 Py_DECREF(curnum);
1621 curnum = tmp_num;
1622 }
Tim Peters874e1f72003-04-13 22:13:08 +00001623 Py_DECREF(ilow);
1624 Py_DECREF(ihigh);
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001625 Py_DECREF(istep);
1626 Py_DECREF(zero);
Tim Peters874e1f72003-04-13 22:13:08 +00001627 Py_DECREF(curnum);
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001628 return v;
1629
1630 Fail:
Tim Peters874e1f72003-04-13 22:13:08 +00001631 Py_DECREF(ilow);
1632 Py_DECREF(ihigh);
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001633 Py_XDECREF(istep);
Tim Peters874e1f72003-04-13 22:13:08 +00001634 Py_DECREF(zero);
1635 Py_XDECREF(curnum);
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001636 Py_XDECREF(v);
1637 return NULL;
1638}
1639
Guido van Rossum124eff01999-02-23 16:11:01 +00001640/* Return number of items in range/xrange (lo, hi, step). step > 0
1641 * required. Return a value < 0 if & only if the true value is too
1642 * large to fit in a signed long.
1643 */
1644static long
Thomas Wouterse28c2962000-07-23 22:21:32 +00001645get_len_of_range(long lo, long hi, long step)
Guido van Rossum124eff01999-02-23 16:11:01 +00001646{
1647 /* -------------------------------------------------------------
1648 If lo >= hi, the range is empty.
1649 Else if n values are in the range, the last one is
1650 lo + (n-1)*step, which must be <= hi-1. Rearranging,
1651 n <= (hi - lo - 1)/step + 1, so taking the floor of the RHS gives
1652 the proper value. Since lo < hi in this case, hi-lo-1 >= 0, so
1653 the RHS is non-negative and so truncation is the same as the
1654 floor. Letting M be the largest positive long, the worst case
1655 for the RHS numerator is hi=M, lo=-M-1, and then
1656 hi-lo-1 = M-(-M-1)-1 = 2*M. Therefore unsigned long has enough
1657 precision to compute the RHS exactly.
1658 ---------------------------------------------------------------*/
1659 long n = 0;
1660 if (lo < hi) {
1661 unsigned long uhi = (unsigned long)hi;
1662 unsigned long ulo = (unsigned long)lo;
1663 unsigned long diff = uhi - ulo - 1;
1664 n = (long)(diff / (unsigned long)step + 1);
1665 }
1666 return n;
1667}
1668
Guido van Rossum79f25d91997-04-29 20:08:16 +00001669static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001670builtin_range(PyObject *self, PyObject *args)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001671{
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001672 long ilow = 0, ihigh = 0, istep = 1;
Guido van Rossum124eff01999-02-23 16:11:01 +00001673 long bign;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001674 int i, n;
Guido van Rossum124eff01999-02-23 16:11:01 +00001675
Guido van Rossum79f25d91997-04-29 20:08:16 +00001676 PyObject *v;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001677
Guido van Rossum79f25d91997-04-29 20:08:16 +00001678 if (PyTuple_Size(args) <= 1) {
1679 if (!PyArg_ParseTuple(args,
Guido van Rossum0865dd91995-01-17 16:30:22 +00001680 "l;range() requires 1-3 int arguments",
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001681 &ihigh)) {
1682 PyErr_Clear();
1683 return handle_range_longs(self, args);
1684 }
Guido van Rossum3f5da241990-12-20 15:06:42 +00001685 }
1686 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001687 if (!PyArg_ParseTuple(args,
Guido van Rossum0865dd91995-01-17 16:30:22 +00001688 "ll|l;range() requires 1-3 int arguments",
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001689 &ilow, &ihigh, &istep)) {
1690 PyErr_Clear();
1691 return handle_range_longs(self, args);
1692 }
Guido van Rossum3f5da241990-12-20 15:06:42 +00001693 }
1694 if (istep == 0) {
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001695 PyErr_SetString(PyExc_ValueError,
Alex Martellif471d472003-04-23 13:00:44 +00001696 "range() step argument must not be zero");
Guido van Rossum3f5da241990-12-20 15:06:42 +00001697 return NULL;
1698 }
Guido van Rossum124eff01999-02-23 16:11:01 +00001699 if (istep > 0)
1700 bign = get_len_of_range(ilow, ihigh, istep);
1701 else
1702 bign = get_len_of_range(ihigh, ilow, -istep);
1703 n = (int)bign;
1704 if (bign < 0 || (long)n != bign) {
Guido van Rossume23cde21999-01-12 05:07:47 +00001705 PyErr_SetString(PyExc_OverflowError,
Fred Drake661ea262000-10-24 19:57:45 +00001706 "range() result has too many items");
Guido van Rossume23cde21999-01-12 05:07:47 +00001707 return NULL;
1708 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00001709 v = PyList_New(n);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001710 if (v == NULL)
1711 return NULL;
1712 for (i = 0; i < n; i++) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001713 PyObject *w = PyInt_FromLong(ilow);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001714 if (w == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001715 Py_DECREF(v);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001716 return NULL;
1717 }
Guido van Rossuma937d141998-04-24 18:22:02 +00001718 PyList_SET_ITEM(v, i, w);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001719 ilow += istep;
1720 }
1721 return v;
1722}
1723
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001724PyDoc_STRVAR(range_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001725"range([start,] stop[, step]) -> list of integers\n\
1726\n\
1727Return a list containing an arithmetic progression of integers.\n\
1728range(i, j) returns [i, i+1, i+2, ..., j-1]; start (!) defaults to 0.\n\
1729When step is given, it specifies the increment (or decrement).\n\
1730For example, range(4) returns [0, 1, 2, 3]. The end point is omitted!\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001731These are exactly the valid indices for a list of 4 elements.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001732
1733
Guido van Rossum79f25d91997-04-29 20:08:16 +00001734static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001735builtin_raw_input(PyObject *self, PyObject *args)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001736{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001737 PyObject *v = NULL;
Martin v. Löwis31d2df52002-08-14 15:46:02 +00001738 PyObject *fin = PySys_GetObject("stdin");
1739 PyObject *fout = PySys_GetObject("stdout");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001740
Raymond Hettingerea3fdf42002-12-29 16:33:45 +00001741 if (!PyArg_UnpackTuple(args, "[raw_]input", 0, 1, &v))
Guido van Rossum3165fe61992-09-25 21:59:05 +00001742 return NULL;
Martin v. Löwis31d2df52002-08-14 15:46:02 +00001743
1744 if (fin == NULL) {
Alex Martellia9b9c9f2003-04-23 13:34:35 +00001745 PyErr_SetString(PyExc_RuntimeError, "[raw_]input: lost sys.stdin");
Martin v. Löwis31d2df52002-08-14 15:46:02 +00001746 return NULL;
1747 }
1748 if (fout == NULL) {
Alex Martellia9b9c9f2003-04-23 13:34:35 +00001749 PyErr_SetString(PyExc_RuntimeError, "[raw_]input: lost sys.stdout");
Martin v. Löwis31d2df52002-08-14 15:46:02 +00001750 return NULL;
1751 }
1752 if (PyFile_SoftSpace(fout, 0)) {
1753 if (PyFile_WriteString(" ", fout) != 0)
1754 return NULL;
1755 }
Georg Brandl7e3ba2a2006-08-06 08:23:54 +00001756 if (PyFile_AsFile(fin) && PyFile_AsFile(fout)
Martin v. Löwis566f6af2002-10-26 14:39:10 +00001757 && isatty(fileno(PyFile_AsFile(fin)))
1758 && isatty(fileno(PyFile_AsFile(fout)))) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001759 PyObject *po;
Guido van Rossum872537c1995-07-07 22:43:42 +00001760 char *prompt;
1761 char *s;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001762 PyObject *result;
Guido van Rossum872537c1995-07-07 22:43:42 +00001763 if (v != NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001764 po = PyObject_Str(v);
Guido van Rossum872537c1995-07-07 22:43:42 +00001765 if (po == NULL)
1766 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001767 prompt = PyString_AsString(po);
Guido van Rossumd9b52081998-06-26 18:25:38 +00001768 if (prompt == NULL)
1769 return NULL;
Guido van Rossum872537c1995-07-07 22:43:42 +00001770 }
1771 else {
1772 po = NULL;
1773 prompt = "";
1774 }
Michael W. Hudson52db5192004-08-02 13:21:09 +00001775 s = PyOS_Readline(PyFile_AsFile(fin), PyFile_AsFile(fout),
Martin v. Löwis566f6af2002-10-26 14:39:10 +00001776 prompt);
Guido van Rossum79f25d91997-04-29 20:08:16 +00001777 Py_XDECREF(po);
Guido van Rossum872537c1995-07-07 22:43:42 +00001778 if (s == NULL) {
Michael W. Hudson30ea2f22004-07-07 17:44:12 +00001779 if (!PyErr_Occurred())
1780 PyErr_SetNone(PyExc_KeyboardInterrupt);
Guido van Rossum872537c1995-07-07 22:43:42 +00001781 return NULL;
1782 }
1783 if (*s == '\0') {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001784 PyErr_SetNone(PyExc_EOFError);
Guido van Rossum872537c1995-07-07 22:43:42 +00001785 result = NULL;
1786 }
1787 else { /* strip trailing '\n' */
Guido van Rossum106f2da2000-06-28 21:12:25 +00001788 size_t len = strlen(s);
Martin v. Löwisb1ed7fa2006-04-13 07:52:27 +00001789 if (len > PY_SSIZE_T_MAX) {
Martin v. Löwis31d2df52002-08-14 15:46:02 +00001790 PyErr_SetString(PyExc_OverflowError,
Alex Martellia9b9c9f2003-04-23 13:34:35 +00001791 "[raw_]input: input too long");
Guido van Rossum106f2da2000-06-28 21:12:25 +00001792 result = NULL;
1793 }
1794 else {
Martin v. Löwisb1ed7fa2006-04-13 07:52:27 +00001795 result = PyString_FromStringAndSize(s, len-1);
Guido van Rossum106f2da2000-06-28 21:12:25 +00001796 }
Guido van Rossum872537c1995-07-07 22:43:42 +00001797 }
Guido van Rossumb18618d2000-05-03 23:44:39 +00001798 PyMem_FREE(s);
Guido van Rossum872537c1995-07-07 22:43:42 +00001799 return result;
1800 }
Guido van Rossum90933611991-06-07 16:10:43 +00001801 if (v != NULL) {
Martin v. Löwis31d2df52002-08-14 15:46:02 +00001802 if (PyFile_WriteObject(v, fout, Py_PRINT_RAW) != 0)
Guido van Rossum90933611991-06-07 16:10:43 +00001803 return NULL;
1804 }
Martin v. Löwis31d2df52002-08-14 15:46:02 +00001805 return PyFile_GetLine(fin, -1);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001806}
1807
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001808PyDoc_STRVAR(raw_input_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001809"raw_input([prompt]) -> string\n\
1810\n\
1811Read a string from standard input. The trailing newline is stripped.\n\
1812If the user hits EOF (Unix: Ctl-D, Windows: Ctl-Z+Return), raise EOFError.\n\
1813On Unix, GNU readline is used if enabled. The prompt string, if given,\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001814is printed without a trailing newline before reading.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001815
1816
Guido van Rossum79f25d91997-04-29 20:08:16 +00001817static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001818builtin_reduce(PyObject *self, PyObject *args)
Guido van Rossum12d12c51993-10-26 17:58:25 +00001819{
Tim Peters15d81ef2001-05-04 04:39:21 +00001820 PyObject *seq, *func, *result = NULL, *it;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001821
Neal Norwitzdf25efe2007-05-23 06:58:36 +00001822 if (Py_Py3kWarningFlag &&
1823 PyErr_Warn(PyExc_DeprecationWarning,
1824 "reduce() not supported in 3.x") < 0)
1825 return NULL;
1826
Raymond Hettingerea3fdf42002-12-29 16:33:45 +00001827 if (!PyArg_UnpackTuple(args, "reduce", 2, 3, &func, &seq, &result))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001828 return NULL;
1829 if (result != NULL)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001830 Py_INCREF(result);
Guido van Rossum12d12c51993-10-26 17:58:25 +00001831
Tim Peters15d81ef2001-05-04 04:39:21 +00001832 it = PyObject_GetIter(seq);
1833 if (it == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001834 PyErr_SetString(PyExc_TypeError,
Tim Peters15d81ef2001-05-04 04:39:21 +00001835 "reduce() arg 2 must support iteration");
1836 Py_XDECREF(result);
Guido van Rossum12d12c51993-10-26 17:58:25 +00001837 return NULL;
1838 }
1839
Guido van Rossum79f25d91997-04-29 20:08:16 +00001840 if ((args = PyTuple_New(2)) == NULL)
Guido van Rossum2d951851994-08-29 12:52:16 +00001841 goto Fail;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001842
Tim Peters15d81ef2001-05-04 04:39:21 +00001843 for (;;) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001844 PyObject *op2;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001845
1846 if (args->ob_refcnt > 1) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001847 Py_DECREF(args);
1848 if ((args = PyTuple_New(2)) == NULL)
Guido van Rossum2d951851994-08-29 12:52:16 +00001849 goto Fail;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001850 }
1851
Tim Peters15d81ef2001-05-04 04:39:21 +00001852 op2 = PyIter_Next(it);
1853 if (op2 == NULL) {
Tim Petersf4848da2001-05-05 00:14:56 +00001854 if (PyErr_Occurred())
1855 goto Fail;
1856 break;
Guido van Rossum2d951851994-08-29 12:52:16 +00001857 }
Guido van Rossum12d12c51993-10-26 17:58:25 +00001858
Guido van Rossum2d951851994-08-29 12:52:16 +00001859 if (result == NULL)
1860 result = op2;
1861 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001862 PyTuple_SetItem(args, 0, result);
1863 PyTuple_SetItem(args, 1, op2);
1864 if ((result = PyEval_CallObject(func, args)) == NULL)
Guido van Rossum2d951851994-08-29 12:52:16 +00001865 goto Fail;
1866 }
Guido van Rossum12d12c51993-10-26 17:58:25 +00001867 }
1868
Guido van Rossum79f25d91997-04-29 20:08:16 +00001869 Py_DECREF(args);
Guido van Rossum12d12c51993-10-26 17:58:25 +00001870
Guido van Rossum2d951851994-08-29 12:52:16 +00001871 if (result == NULL)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001872 PyErr_SetString(PyExc_TypeError,
Fred Drake661ea262000-10-24 19:57:45 +00001873 "reduce() of empty sequence with no initial value");
Guido van Rossum2d951851994-08-29 12:52:16 +00001874
Tim Peters15d81ef2001-05-04 04:39:21 +00001875 Py_DECREF(it);
Guido van Rossum12d12c51993-10-26 17:58:25 +00001876 return result;
1877
Guido van Rossum2d951851994-08-29 12:52:16 +00001878Fail:
Guido van Rossum79f25d91997-04-29 20:08:16 +00001879 Py_XDECREF(args);
1880 Py_XDECREF(result);
Tim Peters15d81ef2001-05-04 04:39:21 +00001881 Py_DECREF(it);
Guido van Rossum12d12c51993-10-26 17:58:25 +00001882 return NULL;
1883}
1884
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001885PyDoc_STRVAR(reduce_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001886"reduce(function, sequence[, initial]) -> value\n\
1887\n\
1888Apply a function of two arguments cumulatively to the items of a sequence,\n\
1889from left to right, so as to reduce the sequence to a single value.\n\
1890For example, reduce(lambda x, y: x+y, [1, 2, 3, 4, 5]) calculates\n\
1891((((1+2)+3)+4)+5). If initial is present, it is placed before the items\n\
1892of the sequence in the calculation, and serves as a default when the\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001893sequence is empty.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001894
1895
Guido van Rossum79f25d91997-04-29 20:08:16 +00001896static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001897builtin_reload(PyObject *self, PyObject *v)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001898{
Neal Norwitzdf25efe2007-05-23 06:58:36 +00001899 if (Py_Py3kWarningFlag &&
1900 PyErr_Warn(PyExc_DeprecationWarning,
1901 "reload() not supported in 3.x") < 0)
1902 return NULL;
1903
Guido van Rossum79f25d91997-04-29 20:08:16 +00001904 return PyImport_ReloadModule(v);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001905}
1906
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001907PyDoc_STRVAR(reload_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001908"reload(module) -> module\n\
1909\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001910Reload the module. The module must have been successfully imported before.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001911
1912
Guido van Rossum79f25d91997-04-29 20:08:16 +00001913static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001914builtin_repr(PyObject *self, PyObject *v)
Guido van Rossumc89705d1992-11-26 08:54:07 +00001915{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001916 return PyObject_Repr(v);
Guido van Rossumc89705d1992-11-26 08:54:07 +00001917}
1918
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001919PyDoc_STRVAR(repr_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001920"repr(object) -> string\n\
1921\n\
1922Return the canonical string representation of the object.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001923For most object types, eval(repr(object)) == object.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001924
1925
Guido van Rossum79f25d91997-04-29 20:08:16 +00001926static PyObject *
Georg Brandlccadf842006-03-31 18:54:53 +00001927builtin_round(PyObject *self, PyObject *args, PyObject *kwds)
Guido van Rossum9e51f9b1993-02-12 16:29:05 +00001928{
Jeffrey Yasskin9871d8f2008-01-05 08:47:13 +00001929 double number;
1930 double f;
1931 int ndigits = 0;
1932 int i;
Georg Brandlccadf842006-03-31 18:54:53 +00001933 static char *kwlist[] = {"number", "ndigits", 0};
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001934
Jeffrey Yasskin9871d8f2008-01-05 08:47:13 +00001935 if (!PyArg_ParseTupleAndKeywords(args, kwds, "d|i:round",
1936 kwlist, &number, &ndigits))
1937 return NULL;
1938 f = 1.0;
1939 i = abs(ndigits);
1940 while (--i >= 0)
1941 f = f*10.0;
1942 if (ndigits < 0)
1943 number /= f;
1944 else
1945 number *= f;
1946 if (number >= 0.0)
1947 number = floor(number + 0.5);
1948 else
1949 number = ceil(number - 0.5);
1950 if (ndigits < 0)
1951 number *= f;
1952 else
1953 number /= f;
1954 return PyFloat_FromDouble(number);
Guido van Rossum9e51f9b1993-02-12 16:29:05 +00001955}
1956
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001957PyDoc_STRVAR(round_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001958"round(number[, ndigits]) -> floating point number\n\
1959\n\
1960Round a number to a given precision in decimal digits (default 0 digits).\n\
Jeffrey Yasskin9871d8f2008-01-05 08:47:13 +00001961This always returns a floating point number. Precision may be negative.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001962
Raymond Hettinger64958a12003-12-17 20:43:33 +00001963static PyObject *
1964builtin_sorted(PyObject *self, PyObject *args, PyObject *kwds)
1965{
1966 PyObject *newlist, *v, *seq, *compare=NULL, *keyfunc=NULL, *newargs;
1967 PyObject *callable;
Martin v. Löwis15e62742006-02-27 16:46:16 +00001968 static char *kwlist[] = {"iterable", "cmp", "key", "reverse", 0};
Neal Norwitz6f0d4792005-12-15 06:40:36 +00001969 int reverse;
Raymond Hettinger64958a12003-12-17 20:43:33 +00001970
Neal Norwitz6f0d4792005-12-15 06:40:36 +00001971 /* args 1-4 should match listsort in Objects/listobject.c */
Armin Rigo71d7e702005-09-20 18:13:03 +00001972 if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|OOi:sorted",
1973 kwlist, &seq, &compare, &keyfunc, &reverse))
1974 return NULL;
Raymond Hettinger64958a12003-12-17 20:43:33 +00001975
1976 newlist = PySequence_List(seq);
1977 if (newlist == NULL)
1978 return NULL;
1979
1980 callable = PyObject_GetAttrString(newlist, "sort");
1981 if (callable == NULL) {
1982 Py_DECREF(newlist);
1983 return NULL;
1984 }
Georg Brandl99d7e4e2005-08-31 22:21:15 +00001985
Raymond Hettinger64958a12003-12-17 20:43:33 +00001986 newargs = PyTuple_GetSlice(args, 1, 4);
1987 if (newargs == NULL) {
1988 Py_DECREF(newlist);
1989 Py_DECREF(callable);
1990 return NULL;
1991 }
1992
1993 v = PyObject_Call(callable, newargs, kwds);
1994 Py_DECREF(newargs);
1995 Py_DECREF(callable);
1996 if (v == NULL) {
1997 Py_DECREF(newlist);
1998 return NULL;
1999 }
2000 Py_DECREF(v);
2001 return newlist;
2002}
2003
2004PyDoc_STRVAR(sorted_doc,
2005"sorted(iterable, cmp=None, key=None, reverse=False) --> new sorted list");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002006
Guido van Rossum79f25d91997-04-29 20:08:16 +00002007static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002008builtin_vars(PyObject *self, PyObject *args)
Guido van Rossum2d951851994-08-29 12:52:16 +00002009{
Guido van Rossum79f25d91997-04-29 20:08:16 +00002010 PyObject *v = NULL;
2011 PyObject *d;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002012
Raymond Hettingerea3fdf42002-12-29 16:33:45 +00002013 if (!PyArg_UnpackTuple(args, "vars", 0, 1, &v))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002014 return NULL;
Guido van Rossum2d951851994-08-29 12:52:16 +00002015 if (v == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002016 d = PyEval_GetLocals();
Guido van Rossum53bb7ff1995-07-26 16:26:31 +00002017 if (d == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002018 if (!PyErr_Occurred())
2019 PyErr_SetString(PyExc_SystemError,
Alex Martellia9b9c9f2003-04-23 13:34:35 +00002020 "vars(): no locals!?");
Guido van Rossum53bb7ff1995-07-26 16:26:31 +00002021 }
2022 else
Guido van Rossum79f25d91997-04-29 20:08:16 +00002023 Py_INCREF(d);
Guido van Rossum2d951851994-08-29 12:52:16 +00002024 }
2025 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002026 d = PyObject_GetAttrString(v, "__dict__");
Guido van Rossum2d951851994-08-29 12:52:16 +00002027 if (d == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002028 PyErr_SetString(PyExc_TypeError,
Guido van Rossum2d951851994-08-29 12:52:16 +00002029 "vars() argument must have __dict__ attribute");
2030 return NULL;
2031 }
2032 }
2033 return d;
2034}
2035
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002036PyDoc_STRVAR(vars_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002037"vars([object]) -> dictionary\n\
2038\n\
2039Without arguments, equivalent to locals().\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002040With an argument, equivalent to object.__dict__.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002041
Jeffrey Yasskin2f3c16b2008-01-03 02:21:52 +00002042static PyObject *
2043builtin_trunc(PyObject *self, PyObject *number)
2044{
Georg Brandl9fcd8ce2008-01-05 17:49:17 +00002045 /* XXX: The py3k branch gets better errors for this by using
2046 _PyType_Lookup(), but since float's mro isn't set in py2.6,
2047 we just use PyObject_CallMethod here. */
Jeffrey Yasskin2f3c16b2008-01-03 02:21:52 +00002048 return PyObject_CallMethod(number, "__trunc__", "");
2049}
2050
2051PyDoc_STRVAR(trunc_doc,
2052"trunc(Real) -> Integral\n\
2053\n\
2054returns the integral closest to x between 0 and x.");
2055
Alex Martellia70b1912003-04-22 08:12:33 +00002056
2057static PyObject*
2058builtin_sum(PyObject *self, PyObject *args)
2059{
2060 PyObject *seq;
2061 PyObject *result = NULL;
2062 PyObject *temp, *item, *iter;
2063
Raymond Hettinger5cf63942003-10-25 06:41:37 +00002064 if (!PyArg_UnpackTuple(args, "sum", 1, 2, &seq, &result))
Alex Martellia70b1912003-04-22 08:12:33 +00002065 return NULL;
2066
2067 iter = PyObject_GetIter(seq);
2068 if (iter == NULL)
2069 return NULL;
2070
2071 if (result == NULL) {
2072 result = PyInt_FromLong(0);
2073 if (result == NULL) {
2074 Py_DECREF(iter);
2075 return NULL;
2076 }
2077 } else {
2078 /* reject string values for 'start' parameter */
2079 if (PyObject_TypeCheck(result, &PyBaseString_Type)) {
2080 PyErr_SetString(PyExc_TypeError,
Alex Martellia9b9c9f2003-04-23 13:34:35 +00002081 "sum() can't sum strings [use ''.join(seq) instead]");
Alex Martellia70b1912003-04-22 08:12:33 +00002082 Py_DECREF(iter);
2083 return NULL;
2084 }
Alex Martelli41c9f882003-04-22 09:24:48 +00002085 Py_INCREF(result);
Alex Martellia70b1912003-04-22 08:12:33 +00002086 }
2087
Raymond Hettinger3f8caa32007-10-24 01:28:33 +00002088#ifndef SLOW_SUM
2089 /* Fast addition by keeping temporary sums in C instead of new Python objects.
2090 Assumes all inputs are the same type. If the assumption fails, default
2091 to the more general routine.
2092 */
2093 if (PyInt_CheckExact(result)) {
2094 long i_result = PyInt_AS_LONG(result);
2095 Py_DECREF(result);
2096 result = NULL;
2097 while(result == NULL) {
2098 item = PyIter_Next(iter);
2099 if (item == NULL) {
2100 Py_DECREF(iter);
2101 if (PyErr_Occurred())
2102 return NULL;
2103 return PyInt_FromLong(i_result);
2104 }
2105 if (PyInt_CheckExact(item)) {
2106 long b = PyInt_AS_LONG(item);
2107 long x = i_result + b;
2108 if ((x^i_result) >= 0 || (x^b) >= 0) {
2109 i_result = x;
2110 Py_DECREF(item);
2111 continue;
2112 }
2113 }
2114 /* Either overflowed or is not an int. Restore real objects and process normally */
2115 result = PyInt_FromLong(i_result);
2116 temp = PyNumber_Add(result, item);
2117 Py_DECREF(result);
2118 Py_DECREF(item);
2119 result = temp;
2120 if (result == NULL) {
2121 Py_DECREF(iter);
2122 return NULL;
2123 }
2124 }
2125 }
2126
2127 if (PyFloat_CheckExact(result)) {
2128 double f_result = PyFloat_AS_DOUBLE(result);
2129 Py_DECREF(result);
2130 result = NULL;
2131 while(result == NULL) {
2132 item = PyIter_Next(iter);
2133 if (item == NULL) {
2134 Py_DECREF(iter);
2135 if (PyErr_Occurred())
2136 return NULL;
2137 return PyFloat_FromDouble(f_result);
2138 }
2139 if (PyFloat_CheckExact(item)) {
2140 PyFPE_START_PROTECT("add", return 0)
2141 f_result += PyFloat_AS_DOUBLE(item);
Raymond Hettinger3a8daf52007-10-24 02:05:51 +00002142 PyFPE_END_PROTECT(f_result)
Raymond Hettingera45c4872007-10-25 02:26:58 +00002143 Py_DECREF(item);
Raymond Hettinger3a8daf52007-10-24 02:05:51 +00002144 continue;
2145 }
2146 if (PyInt_CheckExact(item)) {
2147 PyFPE_START_PROTECT("add", return 0)
2148 f_result += (double)PyInt_AS_LONG(item);
2149 PyFPE_END_PROTECT(f_result)
Raymond Hettingera45c4872007-10-25 02:26:58 +00002150 Py_DECREF(item);
Raymond Hettinger3f8caa32007-10-24 01:28:33 +00002151 continue;
2152 }
2153 result = PyFloat_FromDouble(f_result);
2154 temp = PyNumber_Add(result, item);
2155 Py_DECREF(result);
2156 Py_DECREF(item);
2157 result = temp;
2158 if (result == NULL) {
2159 Py_DECREF(iter);
2160 return NULL;
2161 }
2162 }
2163 }
2164#endif
2165
Alex Martellia70b1912003-04-22 08:12:33 +00002166 for(;;) {
2167 item = PyIter_Next(iter);
2168 if (item == NULL) {
2169 /* error, or end-of-sequence */
2170 if (PyErr_Occurred()) {
2171 Py_DECREF(result);
2172 result = NULL;
2173 }
2174 break;
2175 }
Alex Martellia253e182003-10-25 23:24:14 +00002176 temp = PyNumber_Add(result, item);
Alex Martellia70b1912003-04-22 08:12:33 +00002177 Py_DECREF(result);
2178 Py_DECREF(item);
2179 result = temp;
2180 if (result == NULL)
2181 break;
2182 }
2183 Py_DECREF(iter);
2184 return result;
2185}
2186
2187PyDoc_STRVAR(sum_doc,
Georg Brandl8134d062006-10-12 12:33:07 +00002188"sum(sequence[, start]) -> value\n\
Alex Martellia70b1912003-04-22 08:12:33 +00002189\n\
2190Returns the sum of a sequence of numbers (NOT strings) plus the value\n\
Georg Brandl8134d062006-10-12 12:33:07 +00002191of parameter 'start' (which defaults to 0). When the sequence is\n\
2192empty, returns start.");
Alex Martellia70b1912003-04-22 08:12:33 +00002193
2194
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002195static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002196builtin_isinstance(PyObject *self, PyObject *args)
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002197{
2198 PyObject *inst;
2199 PyObject *cls;
Guido van Rossum823649d2001-03-21 18:40:58 +00002200 int retval;
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002201
Raymond Hettingerea3fdf42002-12-29 16:33:45 +00002202 if (!PyArg_UnpackTuple(args, "isinstance", 2, 2, &inst, &cls))
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002203 return NULL;
Guido van Rossumf5dd9141997-12-02 19:11:45 +00002204
Guido van Rossum823649d2001-03-21 18:40:58 +00002205 retval = PyObject_IsInstance(inst, cls);
2206 if (retval < 0)
Guido van Rossumad991772001-01-12 16:03:05 +00002207 return NULL;
Guido van Rossum77f6a652002-04-03 22:41:51 +00002208 return PyBool_FromLong(retval);
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002209}
2210
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002211PyDoc_STRVAR(isinstance_doc,
Guido van Rossum77f6a652002-04-03 22:41:51 +00002212"isinstance(object, class-or-type-or-tuple) -> bool\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002213\n\
2214Return whether an object is an instance of a class or of a subclass thereof.\n\
Guido van Rossum03290ec2001-10-07 20:54:12 +00002215With a type as second argument, return whether that is the object's type.\n\
2216The form using a tuple, isinstance(x, (A, B, ...)), is a shortcut for\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002217isinstance(x, A) or isinstance(x, B) or ... (etc.).");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002218
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002219
2220static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002221builtin_issubclass(PyObject *self, PyObject *args)
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002222{
2223 PyObject *derived;
2224 PyObject *cls;
2225 int retval;
2226
Raymond Hettingerea3fdf42002-12-29 16:33:45 +00002227 if (!PyArg_UnpackTuple(args, "issubclass", 2, 2, &derived, &cls))
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002228 return NULL;
Guido van Rossum668213d1999-06-16 17:28:37 +00002229
Guido van Rossum823649d2001-03-21 18:40:58 +00002230 retval = PyObject_IsSubclass(derived, cls);
2231 if (retval < 0)
2232 return NULL;
Guido van Rossum77f6a652002-04-03 22:41:51 +00002233 return PyBool_FromLong(retval);
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002234}
2235
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002236PyDoc_STRVAR(issubclass_doc,
Guido van Rossum77f6a652002-04-03 22:41:51 +00002237"issubclass(C, B) -> bool\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002238\n\
Walter Dörwaldd9a6ad32002-12-12 16:41:44 +00002239Return whether class C is a subclass (i.e., a derived class) of class B.\n\
2240When using a tuple as the second argument issubclass(X, (A, B, ...)),\n\
2241is a shortcut for issubclass(X, A) or issubclass(X, B) or ... (etc.).");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002242
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002243
Barry Warsawbd599b52000-08-03 15:45:29 +00002244static PyObject*
2245builtin_zip(PyObject *self, PyObject *args)
2246{
2247 PyObject *ret;
Martin v. Löwisd96ee902006-02-16 14:37:16 +00002248 const Py_ssize_t itemsize = PySequence_Length(args);
2249 Py_ssize_t i;
Tim Peters8572b4f2001-05-06 01:05:02 +00002250 PyObject *itlist; /* tuple of iterators */
Martin v. Löwisd96ee902006-02-16 14:37:16 +00002251 Py_ssize_t len; /* guess at result length */
Barry Warsawbd599b52000-08-03 15:45:29 +00002252
Raymond Hettingereaef6152003-08-02 07:42:57 +00002253 if (itemsize == 0)
2254 return PyList_New(0);
2255
Barry Warsawbd599b52000-08-03 15:45:29 +00002256 /* args must be a tuple */
2257 assert(PyTuple_Check(args));
2258
Tim Peters39a86c22002-05-12 07:19:38 +00002259 /* Guess at result length: the shortest of the input lengths.
2260 If some argument refuses to say, we refuse to guess too, lest
2261 an argument like xrange(sys.maxint) lead us astray.*/
Tim Peters67d687a2002-04-29 21:27:32 +00002262 len = -1; /* unknown */
2263 for (i = 0; i < itemsize; ++i) {
2264 PyObject *item = PyTuple_GET_ITEM(args, i);
Raymond Hettinger4e2f7142007-12-06 00:56:53 +00002265 Py_ssize_t thislen = _PyObject_LengthHint(item, -1);
Tim Peters39a86c22002-05-12 07:19:38 +00002266 if (thislen < 0) {
Tim Peters39a86c22002-05-12 07:19:38 +00002267 len = -1;
2268 break;
2269 }
Tim Peters67d687a2002-04-29 21:27:32 +00002270 else if (len < 0 || thislen < len)
2271 len = thislen;
2272 }
2273
Tim Peters8572b4f2001-05-06 01:05:02 +00002274 /* allocate result list */
Tim Peters67d687a2002-04-29 21:27:32 +00002275 if (len < 0)
2276 len = 10; /* arbitrary */
2277 if ((ret = PyList_New(len)) == NULL)
Barry Warsawbd599b52000-08-03 15:45:29 +00002278 return NULL;
2279
Tim Peters8572b4f2001-05-06 01:05:02 +00002280 /* obtain iterators */
2281 itlist = PyTuple_New(itemsize);
2282 if (itlist == NULL)
2283 goto Fail_ret;
2284 for (i = 0; i < itemsize; ++i) {
2285 PyObject *item = PyTuple_GET_ITEM(args, i);
2286 PyObject *it = PyObject_GetIter(item);
2287 if (it == NULL) {
2288 if (PyErr_ExceptionMatches(PyExc_TypeError))
2289 PyErr_Format(PyExc_TypeError,
Neal Norwitza361bd82006-02-19 19:31:50 +00002290 "zip argument #%zd must support iteration",
Tim Peters8572b4f2001-05-06 01:05:02 +00002291 i+1);
2292 goto Fail_ret_itlist;
Barry Warsawbd599b52000-08-03 15:45:29 +00002293 }
Tim Peters8572b4f2001-05-06 01:05:02 +00002294 PyTuple_SET_ITEM(itlist, i, it);
2295 }
Barry Warsawbd599b52000-08-03 15:45:29 +00002296
Tim Peters8572b4f2001-05-06 01:05:02 +00002297 /* build result into ret list */
Tim Peters67d687a2002-04-29 21:27:32 +00002298 for (i = 0; ; ++i) {
2299 int j;
Tim Peters8572b4f2001-05-06 01:05:02 +00002300 PyObject *next = PyTuple_New(itemsize);
2301 if (!next)
2302 goto Fail_ret_itlist;
2303
Tim Peters67d687a2002-04-29 21:27:32 +00002304 for (j = 0; j < itemsize; j++) {
2305 PyObject *it = PyTuple_GET_ITEM(itlist, j);
Tim Peters8572b4f2001-05-06 01:05:02 +00002306 PyObject *item = PyIter_Next(it);
Barry Warsawbd599b52000-08-03 15:45:29 +00002307 if (!item) {
Tim Peters8572b4f2001-05-06 01:05:02 +00002308 if (PyErr_Occurred()) {
2309 Py_DECREF(ret);
2310 ret = NULL;
Barry Warsawbd599b52000-08-03 15:45:29 +00002311 }
2312 Py_DECREF(next);
Tim Peters8572b4f2001-05-06 01:05:02 +00002313 Py_DECREF(itlist);
Tim Peters67d687a2002-04-29 21:27:32 +00002314 goto Done;
Barry Warsawbd599b52000-08-03 15:45:29 +00002315 }
Tim Peters67d687a2002-04-29 21:27:32 +00002316 PyTuple_SET_ITEM(next, j, item);
Barry Warsawbd599b52000-08-03 15:45:29 +00002317 }
Tim Peters8572b4f2001-05-06 01:05:02 +00002318
Tim Peters67d687a2002-04-29 21:27:32 +00002319 if (i < len)
2320 PyList_SET_ITEM(ret, i, next);
2321 else {
2322 int status = PyList_Append(ret, next);
2323 Py_DECREF(next);
2324 ++len;
2325 if (status < 0)
2326 goto Fail_ret_itlist;
2327 }
Barry Warsawbd599b52000-08-03 15:45:29 +00002328 }
Tim Peters8572b4f2001-05-06 01:05:02 +00002329
Tim Peters67d687a2002-04-29 21:27:32 +00002330Done:
2331 if (ret != NULL && i < len) {
2332 /* The list is too big. */
2333 if (PyList_SetSlice(ret, i, len, NULL) < 0)
2334 return NULL;
2335 }
2336 return ret;
2337
Tim Peters8572b4f2001-05-06 01:05:02 +00002338Fail_ret_itlist:
2339 Py_DECREF(itlist);
2340Fail_ret:
2341 Py_DECREF(ret);
2342 return NULL;
Barry Warsawbd599b52000-08-03 15:45:29 +00002343}
2344
2345
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002346PyDoc_STRVAR(zip_doc,
Barry Warsawbd599b52000-08-03 15:45:29 +00002347"zip(seq1 [, seq2 [...]]) -> [(seq1[0], seq2[0] ...), (...)]\n\
2348\n\
2349Return a list of tuples, where each tuple contains the i-th element\n\
2350from each of the argument sequences. The returned list is truncated\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002351in length to the length of the shortest argument sequence.");
Barry Warsawbd599b52000-08-03 15:45:29 +00002352
2353
Guido van Rossum79f25d91997-04-29 20:08:16 +00002354static PyMethodDef builtin_methods[] = {
Neal Norwitz92e212f2006-04-03 04:48:37 +00002355 {"__import__", (PyCFunction)builtin___import__, METH_VARARGS | METH_KEYWORDS, import_doc},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002356 {"abs", builtin_abs, METH_O, abs_doc},
Raymond Hettinger96229b12005-03-11 06:49:40 +00002357 {"all", builtin_all, METH_O, all_doc},
2358 {"any", builtin_any, METH_O, any_doc},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002359 {"apply", builtin_apply, METH_VARARGS, apply_doc},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002360 {"callable", builtin_callable, METH_O, callable_doc},
2361 {"chr", builtin_chr, METH_VARARGS, chr_doc},
2362 {"cmp", builtin_cmp, METH_VARARGS, cmp_doc},
2363 {"coerce", builtin_coerce, METH_VARARGS, coerce_doc},
Georg Brandl5240d742007-03-13 20:46:32 +00002364 {"compile", (PyCFunction)builtin_compile, METH_VARARGS | METH_KEYWORDS, compile_doc},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002365 {"delattr", builtin_delattr, METH_VARARGS, delattr_doc},
2366 {"dir", builtin_dir, METH_VARARGS, dir_doc},
2367 {"divmod", builtin_divmod, METH_VARARGS, divmod_doc},
2368 {"eval", builtin_eval, METH_VARARGS, eval_doc},
2369 {"execfile", builtin_execfile, METH_VARARGS, execfile_doc},
2370 {"filter", builtin_filter, METH_VARARGS, filter_doc},
2371 {"getattr", builtin_getattr, METH_VARARGS, getattr_doc},
2372 {"globals", (PyCFunction)builtin_globals, METH_NOARGS, globals_doc},
2373 {"hasattr", builtin_hasattr, METH_VARARGS, hasattr_doc},
2374 {"hash", builtin_hash, METH_O, hash_doc},
2375 {"hex", builtin_hex, METH_O, hex_doc},
2376 {"id", builtin_id, METH_O, id_doc},
2377 {"input", builtin_input, METH_VARARGS, input_doc},
2378 {"intern", builtin_intern, METH_VARARGS, intern_doc},
2379 {"isinstance", builtin_isinstance, METH_VARARGS, isinstance_doc},
2380 {"issubclass", builtin_issubclass, METH_VARARGS, issubclass_doc},
2381 {"iter", builtin_iter, METH_VARARGS, iter_doc},
2382 {"len", builtin_len, METH_O, len_doc},
2383 {"locals", (PyCFunction)builtin_locals, METH_NOARGS, locals_doc},
2384 {"map", builtin_map, METH_VARARGS, map_doc},
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00002385 {"max", (PyCFunction)builtin_max, METH_VARARGS | METH_KEYWORDS, max_doc},
2386 {"min", (PyCFunction)builtin_min, METH_VARARGS | METH_KEYWORDS, min_doc},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002387 {"oct", builtin_oct, METH_O, oct_doc},
Neal Norwitzc4edb0e2006-05-02 04:43:14 +00002388 {"open", (PyCFunction)builtin_open, METH_VARARGS | METH_KEYWORDS, open_doc},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002389 {"ord", builtin_ord, METH_O, ord_doc},
2390 {"pow", builtin_pow, METH_VARARGS, pow_doc},
2391 {"range", builtin_range, METH_VARARGS, range_doc},
2392 {"raw_input", builtin_raw_input, METH_VARARGS, raw_input_doc},
2393 {"reduce", builtin_reduce, METH_VARARGS, reduce_doc},
2394 {"reload", builtin_reload, METH_O, reload_doc},
2395 {"repr", builtin_repr, METH_O, repr_doc},
Georg Brandlccadf842006-03-31 18:54:53 +00002396 {"round", (PyCFunction)builtin_round, METH_VARARGS | METH_KEYWORDS, round_doc},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002397 {"setattr", builtin_setattr, METH_VARARGS, setattr_doc},
Raymond Hettinger64958a12003-12-17 20:43:33 +00002398 {"sorted", (PyCFunction)builtin_sorted, METH_VARARGS | METH_KEYWORDS, sorted_doc},
Alex Martellia70b1912003-04-22 08:12:33 +00002399 {"sum", builtin_sum, METH_VARARGS, sum_doc},
Martin v. Löwis339d0f72001-08-17 18:39:25 +00002400#ifdef Py_USING_UNICODE
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002401 {"unichr", builtin_unichr, METH_VARARGS, unichr_doc},
Martin v. Löwis339d0f72001-08-17 18:39:25 +00002402#endif
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002403 {"vars", builtin_vars, METH_VARARGS, vars_doc},
Jeffrey Yasskin2f3c16b2008-01-03 02:21:52 +00002404 {"trunc", builtin_trunc, METH_O, trunc_doc},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002405 {"zip", builtin_zip, METH_VARARGS, zip_doc},
Guido van Rossumc02e15c1991-12-16 13:03:00 +00002406 {NULL, NULL},
Guido van Rossum3f5da241990-12-20 15:06:42 +00002407};
2408
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002409PyDoc_STRVAR(builtin_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002410"Built-in functions, exceptions, and other objects.\n\
2411\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002412Noteworthy: None is the `nil' object; Ellipsis represents `...' in slices.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002413
Guido van Rossum25ce5661997-08-02 03:10:38 +00002414PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002415_PyBuiltin_Init(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +00002416{
Fred Drake5550de32000-06-20 04:54:19 +00002417 PyObject *mod, *dict, *debug;
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002418 mod = Py_InitModule4("__builtin__", builtin_methods,
2419 builtin_doc, (PyObject *)NULL,
2420 PYTHON_API_VERSION);
Guido van Rossum25ce5661997-08-02 03:10:38 +00002421 if (mod == NULL)
2422 return NULL;
2423 dict = PyModule_GetDict(mod);
Tim Peters4b7625e2001-09-13 21:37:17 +00002424
Tim Peters7571a0f2003-03-23 17:52:28 +00002425#ifdef Py_TRACE_REFS
2426 /* __builtin__ exposes a number of statically allocated objects
2427 * that, before this code was added in 2.3, never showed up in
2428 * the list of "all objects" maintained by Py_TRACE_REFS. As a
2429 * result, programs leaking references to None and False (etc)
2430 * couldn't be diagnosed by examining sys.getobjects(0).
2431 */
2432#define ADD_TO_ALL(OBJECT) _Py_AddToAllObjects((PyObject *)(OBJECT), 0)
2433#else
2434#define ADD_TO_ALL(OBJECT) (void)0
2435#endif
2436
Tim Peters4b7625e2001-09-13 21:37:17 +00002437#define SETBUILTIN(NAME, OBJECT) \
Tim Peters7571a0f2003-03-23 17:52:28 +00002438 if (PyDict_SetItemString(dict, NAME, (PyObject *)OBJECT) < 0) \
2439 return NULL; \
2440 ADD_TO_ALL(OBJECT)
Tim Peters4b7625e2001-09-13 21:37:17 +00002441
2442 SETBUILTIN("None", Py_None);
2443 SETBUILTIN("Ellipsis", Py_Ellipsis);
2444 SETBUILTIN("NotImplemented", Py_NotImplemented);
Guido van Rossum77f6a652002-04-03 22:41:51 +00002445 SETBUILTIN("False", Py_False);
2446 SETBUILTIN("True", Py_True);
Neal Norwitz32a7e7f2002-05-31 19:58:02 +00002447 SETBUILTIN("basestring", &PyBaseString_Type);
Guido van Rossum77f6a652002-04-03 22:41:51 +00002448 SETBUILTIN("bool", &PyBool_Type);
Christian Heimes288e89a2008-01-18 18:24:07 +00002449 SETBUILTIN("bytes", &PyString_Type);
Guido van Rossumbea18cc2002-06-14 20:41:17 +00002450 SETBUILTIN("buffer", &PyBuffer_Type);
Tim Peters4b7625e2001-09-13 21:37:17 +00002451 SETBUILTIN("classmethod", &PyClassMethod_Type);
Tim Peters6d6c1a32001-08-02 04:15:00 +00002452#ifndef WITHOUT_COMPLEX
Tim Peters4b7625e2001-09-13 21:37:17 +00002453 SETBUILTIN("complex", &PyComplex_Type);
Tim Peters6d6c1a32001-08-02 04:15:00 +00002454#endif
Tim Petersa427a2b2001-10-29 22:25:45 +00002455 SETBUILTIN("dict", &PyDict_Type);
Tim Peters67d687a2002-04-29 21:27:32 +00002456 SETBUILTIN("enumerate", &PyEnum_Type);
Neal Norwitzc4edb0e2006-05-02 04:43:14 +00002457 SETBUILTIN("file", &PyFile_Type);
Tim Peters4b7625e2001-09-13 21:37:17 +00002458 SETBUILTIN("float", &PyFloat_Type);
Raymond Hettingera690a992003-11-16 16:17:49 +00002459 SETBUILTIN("frozenset", &PyFrozenSet_Type);
Tim Peters4b7625e2001-09-13 21:37:17 +00002460 SETBUILTIN("property", &PyProperty_Type);
2461 SETBUILTIN("int", &PyInt_Type);
2462 SETBUILTIN("list", &PyList_Type);
2463 SETBUILTIN("long", &PyLong_Type);
2464 SETBUILTIN("object", &PyBaseObject_Type);
Raymond Hettinger85c20a42003-11-06 14:06:48 +00002465 SETBUILTIN("reversed", &PyReversed_Type);
Raymond Hettingera690a992003-11-16 16:17:49 +00002466 SETBUILTIN("set", &PySet_Type);
Guido van Rossumbea18cc2002-06-14 20:41:17 +00002467 SETBUILTIN("slice", &PySlice_Type);
Tim Peters4b7625e2001-09-13 21:37:17 +00002468 SETBUILTIN("staticmethod", &PyStaticMethod_Type);
2469 SETBUILTIN("str", &PyString_Type);
2470 SETBUILTIN("super", &PySuper_Type);
2471 SETBUILTIN("tuple", &PyTuple_Type);
2472 SETBUILTIN("type", &PyType_Type);
Raymond Hettingerc4c453f2002-06-05 23:12:45 +00002473 SETBUILTIN("xrange", &PyRange_Type);
Martin v. Löwis339d0f72001-08-17 18:39:25 +00002474#ifdef Py_USING_UNICODE
Tim Peters4b7625e2001-09-13 21:37:17 +00002475 SETBUILTIN("unicode", &PyUnicode_Type);
Martin v. Löwis339d0f72001-08-17 18:39:25 +00002476#endif
Guido van Rossum77f6a652002-04-03 22:41:51 +00002477 debug = PyBool_FromLong(Py_OptimizeFlag == 0);
Fred Drake5550de32000-06-20 04:54:19 +00002478 if (PyDict_SetItemString(dict, "__debug__", debug) < 0) {
2479 Py_XDECREF(debug);
Guido van Rossum25ce5661997-08-02 03:10:38 +00002480 return NULL;
Fred Drake5550de32000-06-20 04:54:19 +00002481 }
2482 Py_XDECREF(debug);
Barry Warsaw757af0e1997-08-29 22:13:51 +00002483
Guido van Rossum25ce5661997-08-02 03:10:38 +00002484 return mod;
Tim Peters7571a0f2003-03-23 17:52:28 +00002485#undef ADD_TO_ALL
Tim Peters4b7625e2001-09-13 21:37:17 +00002486#undef SETBUILTIN
Guido van Rossum3f5da241990-12-20 15:06:42 +00002487}
2488
Guido van Rossume77a7571993-11-03 15:01:26 +00002489/* Helper for filter(): filter a tuple through a function */
Guido van Rossum12d12c51993-10-26 17:58:25 +00002490
Guido van Rossum79f25d91997-04-29 20:08:16 +00002491static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002492filtertuple(PyObject *func, PyObject *tuple)
Guido van Rossum12d12c51993-10-26 17:58:25 +00002493{
Guido van Rossum79f25d91997-04-29 20:08:16 +00002494 PyObject *result;
Martin v. Löwis18e16552006-02-15 17:27:45 +00002495 Py_ssize_t i, j;
2496 Py_ssize_t len = PyTuple_Size(tuple);
Guido van Rossum12d12c51993-10-26 17:58:25 +00002497
Guido van Rossumb7b45621995-08-04 04:07:45 +00002498 if (len == 0) {
Walter Dörwaldc3da83f2003-02-04 20:24:45 +00002499 if (PyTuple_CheckExact(tuple))
2500 Py_INCREF(tuple);
2501 else
2502 tuple = PyTuple_New(0);
Guido van Rossumb7b45621995-08-04 04:07:45 +00002503 return tuple;
2504 }
2505
Guido van Rossum79f25d91997-04-29 20:08:16 +00002506 if ((result = PyTuple_New(len)) == NULL)
Guido van Rossum2586bf01993-11-01 16:21:44 +00002507 return NULL;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002508
Guido van Rossum12d12c51993-10-26 17:58:25 +00002509 for (i = j = 0; i < len; ++i) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002510 PyObject *item, *good;
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002511 int ok;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002512
Walter Dörwald8dd19322003-02-10 17:36:40 +00002513 if (tuple->ob_type->tp_as_sequence &&
2514 tuple->ob_type->tp_as_sequence->sq_item) {
2515 item = tuple->ob_type->tp_as_sequence->sq_item(tuple, i);
Walter Dörwaldc58a3a12003-08-18 18:28:45 +00002516 if (item == NULL)
2517 goto Fail_1;
Walter Dörwald8dd19322003-02-10 17:36:40 +00002518 } else {
Alex Martellia9b9c9f2003-04-23 13:34:35 +00002519 PyErr_SetString(PyExc_TypeError, "filter(): unsubscriptable tuple");
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002520 goto Fail_1;
Walter Dörwald8dd19322003-02-10 17:36:40 +00002521 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002522 if (func == Py_None) {
2523 Py_INCREF(item);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002524 good = item;
2525 }
2526 else {
Raymond Hettinger8ae46892003-10-12 19:09:37 +00002527 PyObject *arg = PyTuple_Pack(1, item);
Walter Dörwaldc58a3a12003-08-18 18:28:45 +00002528 if (arg == NULL) {
2529 Py_DECREF(item);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002530 goto Fail_1;
Walter Dörwaldc58a3a12003-08-18 18:28:45 +00002531 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002532 good = PyEval_CallObject(func, arg);
2533 Py_DECREF(arg);
Walter Dörwaldc58a3a12003-08-18 18:28:45 +00002534 if (good == NULL) {
2535 Py_DECREF(item);
Guido van Rossum12d12c51993-10-26 17:58:25 +00002536 goto Fail_1;
Walter Dörwaldc58a3a12003-08-18 18:28:45 +00002537 }
Guido van Rossum12d12c51993-10-26 17:58:25 +00002538 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002539 ok = PyObject_IsTrue(good);
2540 Py_DECREF(good);
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002541 if (ok) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002542 if (PyTuple_SetItem(result, j++, item) < 0)
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002543 goto Fail_1;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002544 }
Walter Dörwaldc58a3a12003-08-18 18:28:45 +00002545 else
2546 Py_DECREF(item);
Guido van Rossum12d12c51993-10-26 17:58:25 +00002547 }
2548
Tim Peters4324aa32001-05-28 22:30:08 +00002549 if (_PyTuple_Resize(&result, j) < 0)
Guido van Rossum12d12c51993-10-26 17:58:25 +00002550 return NULL;
2551
Guido van Rossum12d12c51993-10-26 17:58:25 +00002552 return result;
2553
Guido van Rossum12d12c51993-10-26 17:58:25 +00002554Fail_1:
Guido van Rossum79f25d91997-04-29 20:08:16 +00002555 Py_DECREF(result);
Guido van Rossum12d12c51993-10-26 17:58:25 +00002556 return NULL;
2557}
2558
2559
Guido van Rossume77a7571993-11-03 15:01:26 +00002560/* Helper for filter(): filter a string through a function */
Guido van Rossum12d12c51993-10-26 17:58:25 +00002561
Guido van Rossum79f25d91997-04-29 20:08:16 +00002562static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002563filterstring(PyObject *func, PyObject *strobj)
Guido van Rossum12d12c51993-10-26 17:58:25 +00002564{
Guido van Rossum79f25d91997-04-29 20:08:16 +00002565 PyObject *result;
Martin v. Löwis18e16552006-02-15 17:27:45 +00002566 Py_ssize_t i, j;
2567 Py_ssize_t len = PyString_Size(strobj);
2568 Py_ssize_t outlen = len;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002569
Guido van Rossum79f25d91997-04-29 20:08:16 +00002570 if (func == Py_None) {
Walter Dörwald1918f772003-02-10 13:19:13 +00002571 /* If it's a real string we can return the original,
2572 * as no character is ever false and __getitem__
2573 * does return this character. If it's a subclass
2574 * we must go through the __getitem__ loop */
2575 if (PyString_CheckExact(strobj)) {
Walter Dörwaldc3da83f2003-02-04 20:24:45 +00002576 Py_INCREF(strobj);
Walter Dörwald1918f772003-02-10 13:19:13 +00002577 return strobj;
2578 }
Guido van Rossum12d12c51993-10-26 17:58:25 +00002579 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002580 if ((result = PyString_FromStringAndSize(NULL, len)) == NULL)
Guido van Rossum2586bf01993-11-01 16:21:44 +00002581 return NULL;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002582
Guido van Rossum12d12c51993-10-26 17:58:25 +00002583 for (i = j = 0; i < len; ++i) {
Walter Dörwald1918f772003-02-10 13:19:13 +00002584 PyObject *item;
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002585 int ok;
Guido van Rossum12d12c51993-10-26 17:58:25 +00002586
Guido van Rossumdc4b93d1993-10-27 14:56:44 +00002587 item = (*strobj->ob_type->tp_as_sequence->sq_item)(strobj, i);
2588 if (item == NULL)
2589 goto Fail_1;
Walter Dörwald1918f772003-02-10 13:19:13 +00002590 if (func==Py_None) {
2591 ok = 1;
2592 } else {
2593 PyObject *arg, *good;
Raymond Hettinger8ae46892003-10-12 19:09:37 +00002594 arg = PyTuple_Pack(1, item);
Walter Dörwald1918f772003-02-10 13:19:13 +00002595 if (arg == NULL) {
2596 Py_DECREF(item);
2597 goto Fail_1;
2598 }
2599 good = PyEval_CallObject(func, arg);
2600 Py_DECREF(arg);
2601 if (good == NULL) {
2602 Py_DECREF(item);
2603 goto Fail_1;
2604 }
2605 ok = PyObject_IsTrue(good);
2606 Py_DECREF(good);
Tim Peters388ed082001-04-07 20:34:48 +00002607 }
Walter Dörwald903f1e02003-02-04 16:28:00 +00002608 if (ok) {
Martin v. Löwisd96ee902006-02-16 14:37:16 +00002609 Py_ssize_t reslen;
Walter Dörwald903f1e02003-02-04 16:28:00 +00002610 if (!PyString_Check(item)) {
2611 PyErr_SetString(PyExc_TypeError, "can't filter str to str:"
2612 " __getitem__ returned different type");
2613 Py_DECREF(item);
2614 goto Fail_1;
2615 }
2616 reslen = PyString_GET_SIZE(item);
2617 if (reslen == 1) {
2618 PyString_AS_STRING(result)[j++] =
2619 PyString_AS_STRING(item)[0];
2620 } else {
2621 /* do we need more space? */
Martin v. Löwisd96ee902006-02-16 14:37:16 +00002622 Py_ssize_t need = j + reslen + len-i-1;
Walter Dörwald903f1e02003-02-04 16:28:00 +00002623 if (need > outlen) {
2624 /* overallocate, to avoid reallocations */
2625 if (need<2*outlen)
2626 need = 2*outlen;
2627 if (_PyString_Resize(&result, need)) {
2628 Py_DECREF(item);
2629 return NULL;
2630 }
2631 outlen = need;
2632 }
2633 memcpy(
2634 PyString_AS_STRING(result) + j,
2635 PyString_AS_STRING(item),
2636 reslen
2637 );
2638 j += reslen;
2639 }
2640 }
Tim Peters388ed082001-04-07 20:34:48 +00002641 Py_DECREF(item);
Guido van Rossum12d12c51993-10-26 17:58:25 +00002642 }
2643
Walter Dörwald903f1e02003-02-04 16:28:00 +00002644 if (j < outlen)
Tim Peters5de98422002-04-27 18:44:32 +00002645 _PyString_Resize(&result, j);
Guido van Rossum12d12c51993-10-26 17:58:25 +00002646
Guido van Rossum12d12c51993-10-26 17:58:25 +00002647 return result;
2648
Guido van Rossum12d12c51993-10-26 17:58:25 +00002649Fail_1:
Guido van Rossum79f25d91997-04-29 20:08:16 +00002650 Py_DECREF(result);
Guido van Rossum12d12c51993-10-26 17:58:25 +00002651 return NULL;
2652}
Martin v. Löwis8afd7572003-01-25 22:46:11 +00002653
2654#ifdef Py_USING_UNICODE
2655/* Helper for filter(): filter a Unicode object through a function */
2656
2657static PyObject *
2658filterunicode(PyObject *func, PyObject *strobj)
2659{
2660 PyObject *result;
Martin v. Löwis725507b2006-03-07 12:08:51 +00002661 register Py_ssize_t i, j;
Martin v. Löwisd96ee902006-02-16 14:37:16 +00002662 Py_ssize_t len = PyUnicode_GetSize(strobj);
2663 Py_ssize_t outlen = len;
Martin v. Löwis8afd7572003-01-25 22:46:11 +00002664
2665 if (func == Py_None) {
Walter Dörwald1918f772003-02-10 13:19:13 +00002666 /* If it's a real string we can return the original,
2667 * as no character is ever false and __getitem__
2668 * does return this character. If it's a subclass
2669 * we must go through the __getitem__ loop */
2670 if (PyUnicode_CheckExact(strobj)) {
Walter Dörwaldc3da83f2003-02-04 20:24:45 +00002671 Py_INCREF(strobj);
Walter Dörwald1918f772003-02-10 13:19:13 +00002672 return strobj;
2673 }
Martin v. Löwis8afd7572003-01-25 22:46:11 +00002674 }
2675 if ((result = PyUnicode_FromUnicode(NULL, len)) == NULL)
2676 return NULL;
2677
2678 for (i = j = 0; i < len; ++i) {
2679 PyObject *item, *arg, *good;
2680 int ok;
2681
2682 item = (*strobj->ob_type->tp_as_sequence->sq_item)(strobj, i);
2683 if (item == NULL)
2684 goto Fail_1;
Walter Dörwald1918f772003-02-10 13:19:13 +00002685 if (func == Py_None) {
2686 ok = 1;
2687 } else {
Raymond Hettinger8ae46892003-10-12 19:09:37 +00002688 arg = PyTuple_Pack(1, item);
Walter Dörwald1918f772003-02-10 13:19:13 +00002689 if (arg == NULL) {
2690 Py_DECREF(item);
2691 goto Fail_1;
2692 }
2693 good = PyEval_CallObject(func, arg);
2694 Py_DECREF(arg);
2695 if (good == NULL) {
2696 Py_DECREF(item);
2697 goto Fail_1;
2698 }
2699 ok = PyObject_IsTrue(good);
2700 Py_DECREF(good);
Martin v. Löwis8afd7572003-01-25 22:46:11 +00002701 }
Walter Dörwald903f1e02003-02-04 16:28:00 +00002702 if (ok) {
Martin v. Löwisd96ee902006-02-16 14:37:16 +00002703 Py_ssize_t reslen;
Walter Dörwald903f1e02003-02-04 16:28:00 +00002704 if (!PyUnicode_Check(item)) {
Georg Brandl99d7e4e2005-08-31 22:21:15 +00002705 PyErr_SetString(PyExc_TypeError,
Jeremy Hylton1aad9c72003-09-16 03:10:59 +00002706 "can't filter unicode to unicode:"
2707 " __getitem__ returned different type");
Walter Dörwald903f1e02003-02-04 16:28:00 +00002708 Py_DECREF(item);
2709 goto Fail_1;
2710 }
2711 reslen = PyUnicode_GET_SIZE(item);
Georg Brandl99d7e4e2005-08-31 22:21:15 +00002712 if (reslen == 1)
Walter Dörwald903f1e02003-02-04 16:28:00 +00002713 PyUnicode_AS_UNICODE(result)[j++] =
2714 PyUnicode_AS_UNICODE(item)[0];
Jeremy Hylton1aad9c72003-09-16 03:10:59 +00002715 else {
Walter Dörwald903f1e02003-02-04 16:28:00 +00002716 /* do we need more space? */
Martin v. Löwisd96ee902006-02-16 14:37:16 +00002717 Py_ssize_t need = j + reslen + len - i - 1;
Walter Dörwald903f1e02003-02-04 16:28:00 +00002718 if (need > outlen) {
Georg Brandl99d7e4e2005-08-31 22:21:15 +00002719 /* overallocate,
Jeremy Hylton1aad9c72003-09-16 03:10:59 +00002720 to avoid reallocations */
2721 if (need < 2 * outlen)
2722 need = 2 * outlen;
Jeremy Hylton364f6be2003-09-16 03:17:16 +00002723 if (PyUnicode_Resize(
2724 &result, need) < 0) {
Walter Dörwald903f1e02003-02-04 16:28:00 +00002725 Py_DECREF(item);
Walter Dörwald531e0002003-02-04 16:57:49 +00002726 goto Fail_1;
Walter Dörwald903f1e02003-02-04 16:28:00 +00002727 }
2728 outlen = need;
2729 }
Jeremy Hylton1aad9c72003-09-16 03:10:59 +00002730 memcpy(PyUnicode_AS_UNICODE(result) + j,
2731 PyUnicode_AS_UNICODE(item),
2732 reslen*sizeof(Py_UNICODE));
Walter Dörwald903f1e02003-02-04 16:28:00 +00002733 j += reslen;
2734 }
2735 }
Martin v. Löwis8afd7572003-01-25 22:46:11 +00002736 Py_DECREF(item);
2737 }
2738
Walter Dörwald903f1e02003-02-04 16:28:00 +00002739 if (j < outlen)
Martin v. Löwis8afd7572003-01-25 22:46:11 +00002740 PyUnicode_Resize(&result, j);
2741
2742 return result;
2743
2744Fail_1:
2745 Py_DECREF(result);
2746 return NULL;
2747}
2748#endif